1 /* Public key file parser in nss_files module.
2 Copyright (C) 1996, 1997, 1998, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <rpc/key_prot.h>
27 #define DATAFILE "/etc/publickey"
29 /* Prototype for function in xcyrpt.c. */
30 extern int xdecrypt (char *, char *);
33 static enum nss_status
34 search (const char *netname, char *result, int *errnop, int secret)
38 stream = fopen (DATAFILE, "r");
40 return errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
44 char buffer[HEXKEYBYTES * 2 + KEYCHECKSUMSIZE + MAXNETNAMELEN + 17];
48 buffer[sizeof (buffer) - 1] = '\xff';
49 p = fgets (buffer, sizeof (buffer), stream);
52 /* End of file or read error. */
55 return NSS_STATUS_NOTFOUND;
57 else if (buffer[sizeof (buffer) - 1] != '\xff')
59 /* Invalid line in file? Skip remainder of line. */
60 if (buffer[sizeof (buffer) - 2] != '\0')
61 while (getc (stream) != '\n')
67 p = __strtok_r (buffer, "# \t:\n", &save_ptr);
68 if (p == NULL) /* Skip empty and comment lines. */
70 if (strcmp (p, netname) != 0)
73 /* A hit! Find the field we want and return. */
74 p = __strtok_r (NULL, ":\n", &save_ptr);
75 if (p == NULL) /* malformed line? */
78 p = __strtok_r (NULL, ":\n", &save_ptr);
79 if (p == NULL) /* malformed line? */
83 return NSS_STATUS_SUCCESS;
88 _nss_files_getpublickey (const char *netname, char *pkey, int *errnop)
90 return search (netname, pkey, errnop, 0);
94 _nss_files_getsecretkey (const char *netname, char *skey, char *passwd,
97 enum nss_status status;
98 char buf[HEXKEYBYTES + KEYCHECKSUMSIZE + 16];
102 status = search (netname, buf, errnop, 1);
103 if (status != NSS_STATUS_SUCCESS)
106 if (!xdecrypt (buf, passwd))
107 return NSS_STATUS_SUCCESS;
109 if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0)
110 return NSS_STATUS_SUCCESS;
112 buf[HEXKEYBYTES] = 0;
115 return NSS_STATUS_SUCCESS;