(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nis / nss_nis / nis-publickey.c
blob6e92112d11eb6b2adf145e6a636439de183f975b
1 /* Copyright (C) 1996,1997,1998,1999,2001,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 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 <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <syslog.h>
25 #include <rpc/rpc.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
28 #include <rpc/key_prot.h>
29 extern int xdecrypt (char *, char *);
31 #include "nss-nis.h"
33 /* If we haven't found the entry, we give a SUCCESS and an empty key back.
34 Solaris docu says: sizeof (pkey) == HEXKEYBYTES + 1.
36 enum nss_status
37 _nss_nis_getpublickey (const char *netname, char *pkey, int *errnop)
39 enum nss_status retval;
40 char *domain, *result;
41 int len;
43 pkey[0] = 0;
45 if (netname == NULL)
47 *errnop = EINVAL;
48 return NSS_STATUS_UNAVAIL;
51 domain = strchr (netname, '@');
52 if (!domain)
54 *errnop = EINVAL;
55 return NSS_STATUS_UNAVAIL;
57 ++domain;
59 retval = yperr2nss (yp_match (domain, "publickey.byname", netname,
60 strlen (netname), &result, &len));
62 if (retval != NSS_STATUS_SUCCESS)
64 if (retval == NSS_STATUS_TRYAGAIN)
65 *errnop = errno;
66 return retval;
69 if (result != NULL)
71 char *p = strchr (result, ':');
72 if (p != NULL)
73 *p = 0;
74 strncpy (pkey, result, HEXKEYBYTES + 1);
75 pkey[HEXKEYBYTES] = '\0';
77 return NSS_STATUS_SUCCESS;
80 enum nss_status
81 _nss_nis_getsecretkey (const char *netname, char *skey, char *passwd,
82 int *errnop)
84 enum nss_status retval;
85 char buf[2 * (HEXKEYBYTES + 1)];
86 char *domain, *result;
87 int len;
89 skey[0] = 0;
91 if (netname == NULL || passwd == NULL)
93 *errnop = EINVAL;
94 return NSS_STATUS_UNAVAIL;
97 domain = strchr (netname, '@');
98 if (!domain)
100 *errnop = EINVAL;
101 return NSS_STATUS_UNAVAIL;
103 ++domain;
105 retval = yperr2nss (yp_match (domain, "publickey.byname", netname,
106 strlen (netname), &result, &len));
108 if (retval != NSS_STATUS_SUCCESS)
110 if (retval == NSS_STATUS_TRYAGAIN)
111 *errnop = errno;
112 return retval;
115 if (result != NULL)
117 char *p = strchr (result, ':');
118 if (p == NULL)
119 return NSS_STATUS_SUCCESS;
121 ++p;
122 strncpy (buf, p, 2 * (HEXKEYBYTES + 1));
123 buf[2 * (HEXKEYBYTES + 1)] = '\0';
124 if (!xdecrypt (buf, passwd))
125 return NSS_STATUS_SUCCESS;
127 if (memcmp (buf, &(buf[HEXKEYBYTES]), KEYCHECKSUMSIZE) != 0)
128 return NSS_STATUS_SUCCESS;
130 buf[HEXKEYBYTES] = '\0';
131 strcpy (skey, buf);
133 return NSS_STATUS_SUCCESS;
136 /* Parse uid and group information from the passed string.
137 The format of the string passed is uid:gid,grp,grp, ... */
138 static enum nss_status
139 parse_netid_str (const char *s, uid_t *uidp, gid_t *gidp, int *gidlenp,
140 gid_t *gidlist)
142 char *p, *ep;
143 int gidlen;
145 if (!s || !isdigit (*s))
147 syslog (LOG_ERR, "netname2user: expecting uid '%s'", s);
148 return NSS_STATUS_NOTFOUND; /* XXX need a better error */
151 /* Fetch the uid */
152 *uidp = strtoul (s, NULL, 10);
154 if (*uidp == 0)
156 syslog (LOG_ERR, "netname2user: should not have uid 0");
157 return NSS_STATUS_NOTFOUND;
160 /* Now get the group list */
161 p = strchr (s, ':');
162 if (!p)
164 syslog (LOG_ERR, "netname2user: missing group id list in '%s'", s);
165 return NSS_STATUS_NOTFOUND;
167 ++p; /* skip ':' */
168 if (!p || (!isdigit (*p)))
170 syslog (LOG_ERR, "netname2user: missing group id list in '%s'.", p);
171 return NSS_STATUS_NOTFOUND;
174 *gidp = strtoul (p, &ep, 10);
176 gidlen = 0;
178 /* After strtoul() ep should point to the first invalid character.
179 This is the marker "," we search for the next value. */
180 while (ep != NULL && *ep == ',')
182 ep++;
183 p = ep;
184 gidlist[gidlen++] = strtoul (p, &ep, 10);
187 *gidlenp = gidlen;
189 return NSS_STATUS_SUCCESS;
193 enum nss_status
194 _nss_nis_netname2user (char netname[MAXNETNAMELEN + 1], uid_t *uidp,
195 gid_t *gidp, int *gidlenp, gid_t *gidlist, int *errnop)
197 char *domain;
198 int yperr;
199 char *lookup;
200 int len;
202 domain = strchr (netname, '@');
203 if (!domain)
205 *errnop = EINVAL;
206 return NSS_STATUS_UNAVAIL;
209 /* Point past the '@' character */
210 ++domain;
211 lookup = NULL;
212 yperr = yp_match (domain, "netid.byname", netname, strlen (netname),
213 &lookup, &len);
214 switch (yperr)
216 case YPERR_SUCCESS:
217 break; /* the successful case */
218 case YPERR_DOMAIN:
219 case YPERR_KEY:
220 return NSS_STATUS_NOTFOUND;
221 case YPERR_MAP:
222 default:
223 return NSS_STATUS_UNAVAIL;
226 if (lookup)
228 enum nss_status err;
230 lookup[len] = '\0';
231 err = parse_netid_str (lookup, uidp, gidp, gidlenp, gidlist);
232 free (lookup);
233 return err;
235 else
236 return NSS_STATUS_NOTFOUND;
238 return NSS_STATUS_SUCCESS;