2004-12-19 Roland McGrath <roland@redhat.com>
[glibc.git] / nss / nss_files / files-key.c
blobf00fc1cfe9a1d322f63ed3dafcefba30128f5cd7
1 /* Public key file parser in nss_files module.
2 Copyright (C) 1996, 1997, 1998 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
18 02111-1307 USA. */
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <netdb.h>
24 #include <rpc/key_prot.h>
25 #include "nsswitch.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)
36 FILE *stream;
38 stream = fopen (DATAFILE, "r");
39 if (stream == NULL)
40 return errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
42 for (;;)
44 char buffer[HEXKEYBYTES * 2 + KEYCHECKSUMSIZE + MAXNETNAMELEN + 17];
45 char *p;
46 char *save_ptr;
48 buffer[sizeof (buffer) - 1] = '\xff';
49 p = fgets (buffer, sizeof (buffer), stream);
50 if (p == NULL)
52 /* End of file or read error. */
53 *errnop = errno;
54 fclose (stream);
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')
62 continue;
63 continue;
66 /* Parse line. */
67 p = __strtok_r (buffer, "# \t:\n", &save_ptr);
68 if (p == NULL) /* Skip empty and comment lines. */
69 continue;
70 if (strcmp (p, netname) != 0)
71 continue;
73 /* A hit! Find the field we want and return. */
74 p = __strtok_r (NULL, ":\n", &save_ptr);
75 if (p == NULL) /* malformed line? */
76 continue;
77 if (secret)
78 p = __strtok_r (NULL, ":\n", &save_ptr);
79 if (p == NULL) /* malformed line? */
80 continue;
81 strcpy (result, p);
82 return NSS_STATUS_SUCCESS;
86 enum nss_status
87 _nss_files_getpublickey (const char *netname, char *pkey, int *errnop)
89 return search (netname, pkey, errnop, 0);
92 enum nss_status
93 _nss_files_getsecretkey (const char *netname, char *skey, char *passwd,
94 int *errnop)
96 enum nss_status status;
97 char buf[HEXKEYBYTES + KEYCHECKSUMSIZE + 16];
99 skey[0] = 0;
101 status = search (netname, buf, errnop, 1);
102 if (status != NSS_STATUS_SUCCESS)
103 return status;
105 if (!xdecrypt (buf, passwd))
106 return NSS_STATUS_SUCCESS;
108 if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0)
109 return NSS_STATUS_SUCCESS;
111 buf[HEXKEYBYTES] = 0;
112 strcpy (skey, buf);
114 return NSS_STATUS_SUCCESS;