1 /* Public key file parser in nss_files module.
2 Copyright (C) 1996-2014 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/>. */
23 #include <rpc/key_prot.h>
24 #include <rpc/des_crypt.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");
35 return errno
== EAGAIN
? NSS_STATUS_TRYAGAIN
: NSS_STATUS_UNAVAIL
;
39 char buffer
[HEXKEYBYTES
* 2 + KEYCHECKSUMSIZE
+ MAXNETNAMELEN
+ 17];
43 buffer
[sizeof (buffer
) - 1] = '\xff';
44 p
= fgets_unlocked (buffer
, sizeof (buffer
), stream
);
47 /* End of file or read error. */
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')
62 p
= __strtok_r (buffer
, "# \t:\n", &save_ptr
);
63 if (p
== NULL
) /* Skip empty and comment lines. */
65 if (strcmp (p
, netname
) != 0)
68 /* A hit! Find the field we want and return. */
69 p
= __strtok_r (NULL
, ":\n", &save_ptr
);
70 if (p
== NULL
) /* malformed line? */
73 p
= __strtok_r (NULL
, ":\n", &save_ptr
);
74 if (p
== NULL
) /* malformed line? */
78 return NSS_STATUS_SUCCESS
;
83 _nss_files_getpublickey (const char *netname
, char *pkey
, int *errnop
)
85 return search (netname
, pkey
, errnop
, 0);
89 _nss_files_getsecretkey (const char *netname
, char *skey
, char *passwd
,
92 enum nss_status status
;
93 char buf
[HEXKEYBYTES
+ KEYCHECKSUMSIZE
+ 16];
97 status
= search (netname
, buf
, errnop
, 1);
98 if (status
!= NSS_STATUS_SUCCESS
)
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;
110 return NSS_STATUS_SUCCESS
;