| 1 | /* Public key file parser in nss_files module. |
| 2 | Copyright (C) 1996-2019 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | #include <errno.h> |
| 21 | #include <string.h> |
| 22 | #include <netdb.h> |
| 23 | #include <rpc/key_prot.h> |
| 24 | #include <rpc/des_crypt.h> |
| 25 | #include "nsswitch.h" |
| 26 | |
| 27 | #define DATAFILE "/etc/publickey" |
| 28 | |
| 29 | |
| 30 | static enum nss_status |
| 31 | search (const char *netname, char *result, int *errnop, int secret) |
| 32 | { |
| 33 | FILE *stream = fopen (DATAFILE, "rce" ); |
| 34 | if (stream == NULL) |
| 35 | return errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; |
| 36 | |
| 37 | for (;;) |
| 38 | { |
| 39 | char buffer[HEXKEYBYTES * 2 + KEYCHECKSUMSIZE + MAXNETNAMELEN + 17]; |
| 40 | char *p; |
| 41 | char *save_ptr; |
| 42 | |
| 43 | buffer[sizeof (buffer) - 1] = '\xff'; |
| 44 | p = fgets_unlocked (buffer, sizeof (buffer), stream); |
| 45 | if (p == NULL) |
| 46 | { |
| 47 | /* End of file or read error. */ |
| 48 | *errnop = errno; |
| 49 | fclose (stream); |
| 50 | return NSS_STATUS_NOTFOUND; |
| 51 | } |
| 52 | else if (buffer[sizeof (buffer) - 1] != '\xff') |
| 53 | { |
| 54 | /* Invalid line in file? Skip remainder of line. */ |
| 55 | if (buffer[sizeof (buffer) - 2] != '\0') |
| 56 | while (getc_unlocked (stream) != '\n') |
| 57 | continue; |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | /* Parse line. */ |
| 62 | p = __strtok_r (buffer, "# \t:\n" , &save_ptr); |
| 63 | if (p == NULL) /* Skip empty and comment lines. */ |
| 64 | continue; |
| 65 | if (strcmp (p, netname) != 0) |
| 66 | continue; |
| 67 | |
| 68 | /* A hit! Find the field we want and return. */ |
| 69 | p = __strtok_r (NULL, ":\n" , &save_ptr); |
| 70 | if (p == NULL) /* malformed line? */ |
| 71 | continue; |
| 72 | if (secret) |
| 73 | p = __strtok_r (NULL, ":\n" , &save_ptr); |
| 74 | if (p == NULL) /* malformed line? */ |
| 75 | continue; |
| 76 | fclose (stream); |
| 77 | strcpy (result, p); |
| 78 | return NSS_STATUS_SUCCESS; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | enum nss_status |
| 83 | _nss_files_getpublickey (const char *netname, char *pkey, int *errnop) |
| 84 | { |
| 85 | return search (netname, pkey, errnop, 0); |
| 86 | } |
| 87 | |
| 88 | enum nss_status |
| 89 | _nss_files_getsecretkey (const char *netname, char *skey, char *passwd, |
| 90 | int *errnop) |
| 91 | { |
| 92 | enum nss_status status; |
| 93 | char buf[HEXKEYBYTES + KEYCHECKSUMSIZE + 16]; |
| 94 | |
| 95 | skey[0] = 0; |
| 96 | |
| 97 | status = search (netname, buf, errnop, 1); |
| 98 | if (status != NSS_STATUS_SUCCESS) |
| 99 | return status; |
| 100 | |
| 101 | if (!xdecrypt (buf, passwd)) |
| 102 | return NSS_STATUS_SUCCESS; |
| 103 | |
| 104 | if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0) |
| 105 | return NSS_STATUS_SUCCESS; |
| 106 | |
| 107 | buf[HEXKEYBYTES] = 0; |
| 108 | strcpy (skey, buf); |
| 109 | |
| 110 | return NSS_STATUS_SUCCESS; |
| 111 | } |
| 112 | |