(nice): Use getpriority() for the return value.
[glibc.git] / nis / nss_nis / nis-pwd.c
blobda5ffabff1645d7d48ebf3ac70bd5ccc3b15e2d1
1 /* Copyright (C) 1996, 1997, 1998, 2001, 2002 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 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 <pwd.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
31 /* Get the declaration of the parser function. */
32 #define ENTNAME pwent
33 #define STRUCTURE passwd
34 #define EXTERN_PARSER
35 #include <nss/nss_files/files-parse.c>
37 /* Protect global state against multiple changers */
38 __libc_lock_define_initialized (static, lock)
40 static bool_t new_start = 1;
41 static char *oldkey;
42 static int oldkeylen;
44 enum nss_status
45 _nss_nis_setpwent (int stayopen)
47 __libc_lock_lock (lock);
49 new_start = 1;
50 if (oldkey != NULL)
52 free (oldkey);
53 oldkey = NULL;
54 oldkeylen = 0;
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
62 enum nss_status
63 _nss_nis_endpwent (void)
65 __libc_lock_lock (lock);
67 new_start = 1;
68 if (oldkey != NULL)
70 free (oldkey);
71 oldkey = NULL;
72 oldkeylen = 0;
75 __libc_lock_unlock (lock);
77 return NSS_STATUS_SUCCESS;
80 static enum nss_status
81 internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen,
82 int *errnop)
84 struct parser_data *data = (void *) buffer;
85 char *domain;
86 int parse_res;
88 if (yp_get_default_domain (&domain))
89 return NSS_STATUS_UNAVAIL;
91 /* Get the next entry until we found a correct one. */
94 enum nss_status retval;
95 char *result, *outkey, *result2, *p;
96 int len, keylen, len2;
97 size_t namelen;
99 if (new_start)
100 retval = yperr2nss (yp_first (domain, "passwd.byname",
101 &outkey, &keylen, &result, &len));
102 else
103 retval = yperr2nss ( yp_next (domain, "passwd.byname",
104 oldkey, oldkeylen,
105 &outkey, &keylen, &result, &len));
107 if (retval != NSS_STATUS_SUCCESS)
109 if (retval == NSS_STATUS_TRYAGAIN)
110 *errnop = errno;
111 return retval;
114 /* Check for adjunct style secret passwords. They can be
115 recognized by a password starting with "##". */
116 p = strchr (result, ':');
117 if (p != NULL /* This better should be true in all cases. */
118 && p[1] == '#' && p[2] == '#'
119 && (namelen = p - result,
120 yp_match (domain, "passwd.adjunct.byname", result, namelen,
121 &result2, &len2)) == YPERR_SUCCESS)
123 /* We found a passwd.adjunct entry. Merge encrypted
124 password therein into original result. */
125 char *encrypted = strchr (result2, ':');
126 char *endp;
127 size_t restlen;
129 if (encrypted == NULL
130 || (endp = strchr (++encrypted, ':')) == NULL
131 || (p = strchr (p + 1, ':')) == NULL)
133 /* Invalid format of the entry. This never should happen
134 unless the data from which the NIS table is generated is
135 wrong. We simply ignore it. */
136 free (result2);
137 goto non_adjunct;
140 restlen = len - (p - result);
141 if ((size_t) (namelen + (endp - encrypted) + restlen + 2) > buflen)
143 free (result2);
144 free (result);
145 *errnop = ERANGE;
146 return NSS_STATUS_TRYAGAIN;
149 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer, result, namelen),
150 ":", 1),
151 encrypted, endp - encrypted),
152 p, restlen + 1);
153 p = buffer;
155 free (result2);
157 else
159 non_adjunct:
160 if ((size_t) (len + 1) > buflen)
162 free (result);
163 *errnop = ERANGE;
164 return NSS_STATUS_TRYAGAIN;
167 p = strncpy (buffer, result, len);
168 buffer[len] = '\0';
171 while (isspace (*p))
172 ++p;
173 free (result);
175 parse_res = _nss_files_parse_pwent (p, pwd, data, buflen, errnop);
176 if (parse_res == -1)
178 free (outkey);
179 *errnop = ERANGE;
180 return NSS_STATUS_TRYAGAIN;
183 free (oldkey);
184 oldkey = outkey;
185 oldkeylen = keylen;
186 new_start = 0;
188 while (parse_res < 1);
190 return NSS_STATUS_SUCCESS;
193 enum nss_status
194 _nss_nis_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
195 int *errnop)
197 int status;
199 __libc_lock_lock (lock);
201 status = internal_nis_getpwent_r (result, buffer, buflen, errnop);
203 __libc_lock_unlock (lock);
205 return status;
208 enum nss_status
209 _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
210 char *buffer, size_t buflen, int *errnop)
212 struct parser_data *data = (void *) buffer;
213 enum nss_status retval;
214 char *domain, *result, *result2, *p;
215 int len, len2, parse_res;
216 size_t namelen;
218 if (name == NULL)
220 *errnop = EINVAL;
221 return NSS_STATUS_UNAVAIL;
224 if (yp_get_default_domain (&domain))
225 return NSS_STATUS_UNAVAIL;
227 namelen = strlen (name);
229 retval = yperr2nss (yp_match (domain, "passwd.byname", name,
230 namelen, &result, &len));
232 if (retval != NSS_STATUS_SUCCESS)
234 if (retval == NSS_STATUS_TRYAGAIN)
235 *errnop = errno;
236 return retval;
239 /* Check for adjunct style secret passwords. They can be recognized
240 by a password starting with "##". */
241 p = strchr (result, ':');
242 if (p != NULL /* This better should be true in all cases. */
243 && p[1] == '#' && p[2] == '#'
244 && yp_match (domain, "passwd.adjunct.byname", name, namelen,
245 &result2, &len2) == YPERR_SUCCESS)
247 /* We found a passwd.adjunct entry. Merge encrypted password
248 therein into original result. */
249 char *encrypted = strchr (result2, ':');
250 char *endp;
251 size_t restlen;
253 if (encrypted == NULL
254 || (endp = strchr (++encrypted, ':')) == NULL
255 || (p = strchr (p + 1, ':')) == NULL)
257 /* Invalid format of the entry. This never should happen
258 unless the data from which the NIS table is generated is
259 wrong. We simply ignore it. */
260 free (result2);
261 goto non_adjunct;
264 restlen = len - (p - result);
265 if ((size_t) (namelen + (endp - encrypted) + restlen + 2) > buflen)
267 free (result2);
268 free (result);
269 *errnop = ERANGE;
270 return NSS_STATUS_TRYAGAIN;
273 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer, name, namelen),
274 ":", 1),
275 encrypted, endp - encrypted),
276 p, restlen + 1);
277 p = buffer;
279 free (result2);
281 else
283 non_adjunct:
284 if ((size_t) (len + 1) > buflen)
286 free (result);
287 *errnop = ERANGE;
288 return NSS_STATUS_TRYAGAIN;
291 p = strncpy (buffer, result, len);
292 buffer[len] = '\0';
295 while (isspace (*p))
296 ++p;
297 free (result);
299 parse_res = _nss_files_parse_pwent (p, pwd, data, buflen, errnop);
300 if (parse_res < 1)
302 if (parse_res == -1)
303 return NSS_STATUS_TRYAGAIN;
304 else
305 return NSS_STATUS_NOTFOUND;
307 else
308 return NSS_STATUS_SUCCESS;
311 enum nss_status
312 _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
313 char *buffer, size_t buflen, int *errnop)
315 struct parser_data *data = (void *) buffer;
316 enum nss_status retval;
317 char *domain, *result, *p, *result2;
318 int len, nlen, parse_res, len2;
319 char buf[32];
320 size_t namelen;
322 if (yp_get_default_domain (&domain))
323 return NSS_STATUS_UNAVAIL;
325 nlen = sprintf (buf, "%lu", (unsigned long int) uid);
327 retval = yperr2nss (yp_match (domain, "passwd.byuid", buf,
328 nlen, &result, &len));
330 if (retval != NSS_STATUS_SUCCESS)
332 if (retval == NSS_STATUS_TRYAGAIN)
333 *errnop = errno;
334 return retval;
337 /* Check for adjunct style secret passwords. They can be recognized
338 by a password starting with "##". */
339 p = strchr (result, ':');
340 if (p != NULL /* This better should be true in all cases. */
341 && p[1] == '#' && p[2] == '#'
342 && (namelen = p - result,
343 yp_match (domain, "passwd.adjunct.byname", result, namelen,
344 &result2, &len2)) == YPERR_SUCCESS)
346 /* We found a passwd.adjunct entry. Merge encrypted password
347 therein into original result. */
348 char *encrypted = strchr (result2, ':');
349 char *endp;
350 size_t restlen;
352 if (encrypted == NULL
353 || (endp = strchr (++encrypted, ':')) == NULL
354 || (p = strchr (p + 1, ':')) == NULL)
356 /* Invalid format of the entry. This never should happen
357 unless the data from which the NIS table is generated is
358 wrong. We simply ignore it. */
359 free (result2);
360 goto non_adjunct;
363 restlen = len - (p - result);
364 if ((size_t) (namelen + (endp - encrypted) + restlen + 2) > buflen)
366 free (result2);
367 free (result);
368 *errnop = ERANGE;
369 return NSS_STATUS_TRYAGAIN;
372 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer, result, namelen),
373 ":", 1),
374 encrypted, endp - encrypted),
375 p, restlen + 1);
376 p = buffer;
378 free (result2);
380 else
382 non_adjunct:
383 if ((size_t) (len + 1) > buflen)
385 free (result);
386 *errnop = ERANGE;
387 return NSS_STATUS_TRYAGAIN;
390 p = strncpy (buffer, result, len);
391 buffer[len] = '\0';
394 while (isspace (*p))
395 ++p;
396 free (result);
398 parse_res = _nss_files_parse_pwent (p, pwd, data, buflen, errnop);
399 if (parse_res < 1)
401 if (parse_res == -1)
402 return NSS_STATUS_TRYAGAIN;
403 else
404 return NSS_STATUS_NOTFOUND;
406 else
407 return NSS_STATUS_SUCCESS;