2.9
[glibc/nacl-glibc.git] / nss / nss_files / files-key.c
blob5c7ad0999a536c6415d8140e8ac2abaede2d14f3
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
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 fclose (stream);
82 strcpy (result, p);
83 return NSS_STATUS_SUCCESS;
87 enum nss_status
88 _nss_files_getpublickey (const char *netname, char *pkey, int *errnop)
90 return search (netname, pkey, errnop, 0);
93 enum nss_status
94 _nss_files_getsecretkey (const char *netname, char *skey, char *passwd,
95 int *errnop)
97 enum nss_status status;
98 char buf[HEXKEYBYTES + KEYCHECKSUMSIZE + 16];
100 skey[0] = 0;
102 status = search (netname, buf, errnop, 1);
103 if (status != NSS_STATUS_SUCCESS)
104 return status;
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;
113 strcpy (skey, buf);
115 return NSS_STATUS_SUCCESS;