]>
Commit | Line | Data |
---|---|---|
36ecfe56 | 1 | /* Public key file parser in nss_files module. |
04277e02 | 2 | Copyright (C) 1996-2019 Free Software Foundation, Inc. |
36ecfe56 UD |
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 | |
41bdb6e2 AJ |
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. | |
36ecfe56 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
36ecfe56 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
36ecfe56 UD |
18 | |
19 | #include <stdio.h> | |
20 | #include <errno.h> | |
21 | #include <string.h> | |
22 | #include <netdb.h> | |
23 | #include <rpc/key_prot.h> | |
59d112a4 | 24 | #include <rpc/des_crypt.h> |
36ecfe56 UD |
25 | #include "nsswitch.h" |
26 | ||
27 | #define DATAFILE "/etc/publickey" | |
28 | ||
36ecfe56 UD |
29 | |
30 | static enum nss_status | |
31 | search (const char *netname, char *result, int *errnop, int secret) | |
32 | { | |
312be3f9 | 33 | FILE *stream = fopen (DATAFILE, "rce"); |
36ecfe56 UD |
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'; | |
775a77e7 | 44 | p = fgets_unlocked (buffer, sizeof (buffer), stream); |
36ecfe56 UD |
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') | |
775a77e7 | 56 | while (getc_unlocked (stream) != '\n') |
36ecfe56 UD |
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; | |
0292b0dd | 76 | fclose (stream); |
36ecfe56 UD |
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 | } |