update from main arcive 961210
[glibc.git] / nis / nss_nis / nis-publickey.c
blobb9eda6a7424fede22b60089a806cd4e394020434
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <syslog.h>
25 #include <libc-lock.h>
26 #include <rpc/key_prot.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
30 #include "nss-nis.h"
32 extern int xdecrypt (char *, char *);
34 /* If we found the entry, we give a SUCCESS and an empty key back. */
35 enum nss_status
36 _nss_nis_getpublickey (const char *netname, char *pkey)
38 enum nss_status retval;
39 char *domain, *result;
40 int len;
42 pkey[0] = 0;
44 if (netname == NULL)
46 __set_errno (EINVAL);
47 return NSS_STATUS_UNAVAIL;
50 domain = strchr (netname, '@');
51 if (!domain)
52 return NSS_STATUS_UNAVAIL;
53 domain++;
55 retval = yperr2nss (yp_match (domain, "publickey.byname", netname,
56 strlen (netname), &result, &len));
58 if (retval != NSS_STATUS_SUCCESS)
60 if (retval == NSS_STATUS_TRYAGAIN)
61 __set_errno (EAGAIN);
62 return retval;
65 if (result != NULL)
67 char *p = strchr (result, ':');
68 if (p != NULL)
69 *p = 0;
70 strcpy (pkey, result);
72 return NSS_STATUS_SUCCESS;
75 enum nss_status
76 _nss_nis_getsecretkey (const char *netname, char *skey, char *passwd)
78 enum nss_status retval;
79 char buf[1024];
80 char *domain, *result;
81 int len;
83 skey[0] = 0;
85 if (netname == NULL || passwd == NULL)
87 __set_errno (EINVAL);
88 return NSS_STATUS_UNAVAIL;
91 domain = strchr (netname, '@');
92 if (!domain)
93 return NSS_STATUS_UNAVAIL;
94 domain++;
96 retval = yperr2nss (yp_match (domain, "publickey.byname", netname,
97 strlen (netname), &result, &len));
99 if (retval != NSS_STATUS_SUCCESS)
101 if (retval == NSS_STATUS_TRYAGAIN)
102 __set_errno (EAGAIN);
103 return retval;
106 if (result != NULL)
108 char *p = strchr (result, ':');
109 if (p == NULL)
110 return NSS_STATUS_SUCCESS;
112 p++;
113 strcpy (buf, p);
114 if (!xdecrypt (buf, passwd))
115 return NSS_STATUS_SUCCESS;
117 if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0)
118 return NSS_STATUS_SUCCESS;
120 buf[HEXKEYBYTES] = 0;
121 strcpy (skey, buf);
123 return NSS_STATUS_SUCCESS;
126 /* Parse uid and group information from the passed string.
127 The format of the string passed is uid:gid,grp,grp, ... */
128 static enum nss_status
129 parse_netid_str (const char *s, uid_t *uidp, gid_t *gidp, int *gidlenp,
130 gid_t *gidlist)
132 char *p;
134 if (!s || !isdigit (*s))
136 syslog (LOG_ERR, "netname2user: expecting uid '%s'", s);
137 return NSS_STATUS_NOTFOUND; /* XXX need a better error */
140 /* Fetch the uid */
141 *uidp = (atoi (s));
143 if (*uidp == 0)
145 syslog (LOG_ERR, "netname2user: should not have uid 0");
146 return NSS_STATUS_NOTFOUND;
149 /* Now get the group list */
150 p = strchr (s, ':');
151 if (!p)
153 syslog (LOG_ERR, "netname2user: missing group id list in '%s'", s);
154 return NSS_STATUS_NOTFOUND;
156 ++p; /* skip ':' */
157 if (!p || (!isdigit (*p)))
159 syslog (LOG_ERR, "netname2user: missing group id list in '%s'.", p);
160 return NSS_STATUS_NOTFOUND;
163 *gidp = (atoi (p));
165 *gidlenp = 0;
166 #if 0
167 while ((p = strchr (p, ',')) != NULL)
169 p++;
170 gidlist[*gidlenp++] = atoi (p);
172 #endif
174 return NSS_STATUS_SUCCESS;
178 enum nss_status
179 _nss_nis_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
180 gid_t *gidp, int *gidlenp, gid_t *gidlist)
182 char *domain;
183 int yperr;
184 char *lookup;
185 int len;
187 domain = strchr (netname, '@');
188 if (!domain)
189 return NSS_STATUS_UNAVAIL;
191 /* Point past the '@' character */
192 domain++;
193 lookup = NULL;
194 yperr = yp_match (domain, "netid.byname", netname, strlen (netname),
195 &lookup, &len);
196 switch (yperr)
198 case YPERR_SUCCESS:
199 break; /* the successful case */
200 case YPERR_DOMAIN:
201 case YPERR_KEY:
202 return NSS_STATUS_NOTFOUND;
203 case YPERR_MAP:
204 default:
205 return NSS_STATUS_UNAVAIL;
207 if (lookup)
209 enum nss_status err;
211 lookup[len] = '\0';
212 err = parse_netid_str (lookup, uidp, gidp, gidlenp, gidlist);
213 free (lookup);
214 return err;
216 else
217 return NSS_STATUS_NOTFOUND;
219 return NSS_STATUS_SUCCESS;