hurd: Fix glob64 compatibility symbols
[glibc.git] / nss / nss_files / files-key.c
blob44d4348a8009d5d8db69912d3e7510527d0f96b8
1 /* Public key file parser in nss_files module.
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
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"
27 #define DATAFILE "/etc/publickey"
30 static enum nss_status
31 search (const char *netname, char *result, int *errnop, int secret)
33 FILE *stream = fopen (DATAFILE, "rce");
34 if (stream == NULL)
35 return errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
37 for (;;)
39 char buffer[HEXKEYBYTES * 2 + KEYCHECKSUMSIZE + MAXNETNAMELEN + 17];
40 char *p;
41 char *save_ptr;
43 buffer[sizeof (buffer) - 1] = '\xff';
44 p = fgets_unlocked (buffer, sizeof (buffer), stream);
45 if (p == NULL)
47 /* End of file or read error. */
48 *errnop = errno;
49 fclose (stream);
50 return NSS_STATUS_NOTFOUND;
52 else if (buffer[sizeof (buffer) - 1] != '\xff')
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;
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;
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;
82 enum nss_status
83 _nss_files_getpublickey (const char *netname, char *pkey, int *errnop)
85 return search (netname, pkey, errnop, 0);
88 enum nss_status
89 _nss_files_getsecretkey (const char *netname, char *skey, char *passwd,
90 int *errnop)
92 enum nss_status status;
93 char buf[HEXKEYBYTES + KEYCHECKSUMSIZE + 16];
95 skey[0] = 0;
97 status = search (netname, buf, errnop, 1);
98 if (status != NSS_STATUS_SUCCESS)
99 return status;
101 if (!xdecrypt (buf, passwd))
102 return NSS_STATUS_SUCCESS;
104 if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0)
105 return NSS_STATUS_SUCCESS;
107 buf[HEXKEYBYTES] = 0;
108 strcpy (skey, buf);
110 return NSS_STATUS_SUCCESS;