2.5-18.1
[jlayton/glibc.git] / nss / nss_files / files-key.c
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.
4
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.
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
13    Lesser General Public License for more details.
14
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.  */
19
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"
26
27 #define DATAFILE "/etc/publickey"
28
29 /* Prototype for function in xcyrpt.c.  */
30 extern int xdecrypt (char *, char *);
31
32
33 static enum nss_status
34 search (const char *netname, char *result, int *errnop, int secret)
35 {
36   FILE *stream;
37
38   stream = fopen (DATAFILE, "r");
39   if (stream == NULL)
40     return errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
41
42   for (;;)
43     {
44       char buffer[HEXKEYBYTES * 2 + KEYCHECKSUMSIZE + MAXNETNAMELEN + 17];
45       char *p;
46       char *save_ptr;
47
48       buffer[sizeof (buffer) - 1] = '\xff';
49       p = fgets (buffer, sizeof (buffer), stream);
50       if (p == NULL)
51         {
52           /* End of file or read error.  */
53           *errnop = errno;
54           fclose (stream);
55           return NSS_STATUS_NOTFOUND;
56         }
57       else if (buffer[sizeof (buffer) - 1] != '\xff')
58         {
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;
64         }
65
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;
72
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;
84     }
85 }
86
87 enum nss_status
88 _nss_files_getpublickey (const char *netname, char *pkey, int *errnop)
89 {
90   return search (netname, pkey, errnop, 0);
91 }
92
93 enum nss_status
94 _nss_files_getsecretkey (const char *netname, char *skey, char *passwd,
95                          int *errnop)
96 {
97   enum nss_status status;
98   char buf[HEXKEYBYTES + KEYCHECKSUMSIZE + 16];
99
100   skey[0] = 0;
101
102   status = search (netname, buf, errnop, 1);
103   if (status != NSS_STATUS_SUCCESS)
104     return status;
105
106   if (!xdecrypt (buf, passwd))
107     return NSS_STATUS_SUCCESS;
108
109   if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0)
110     return NSS_STATUS_SUCCESS;
111
112   buf[HEXKEYBYTES] = 0;
113   strcpy (skey, buf);
114
115   return NSS_STATUS_SUCCESS;
116 }