1 /* Copyright (C) 1996-1998,2001,2002,2003,2006 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
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/yp.h>
28 #include <rpcsvc/ypclnt.h>
33 /* Get the declaration of the parser function. */
35 #define STRUCTURE passwd
37 #include <nss/nss_files/files-parse.c>
39 /* Protect global state against multiple changers */
40 __libc_lock_define_initialized (static, lock
)
42 static bool_t new_start
= 1;
45 static intern_t intern
;
49 _nis_saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
50 int invallen
, char *indata
)
52 intern_t
*intern
= (intern_t
*) indata
;
54 if (instatus
!= YP_TRUE
)
57 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
59 struct response_t
*bucket
= intern
->next
;
61 if (__builtin_expect (bucket
== NULL
, 0))
63 #define MINSIZE 4096 - 4 * sizeof (void *)
64 const size_t minsize
= MAX (MINSIZE
, 2 * (invallen
+ 1));
65 bucket
= malloc (sizeof (struct response_t
) + minsize
);
67 /* We have no error code for out of memory. */
71 bucket
->size
= minsize
;
72 intern
->start
= intern
->next
= bucket
;
75 else if (__builtin_expect (invallen
+ 1 > bucket
->size
- intern
->offset
,
78 /* We need a new (larger) buffer. */
79 const size_t newsize
= 2 * MAX (bucket
->size
, invallen
+ 1);
80 struct response_t
*newp
= malloc (sizeof (struct response_t
)
83 /* We have no error code for out of memory. */
86 /* Mark the old bucket as full. */
87 bucket
->size
= intern
->offset
;
91 bucket
= intern
->next
= bucket
->next
= newp
;
95 char *p
= mempcpy (&bucket
->mem
[intern
->offset
], inval
, invallen
);
96 if (__builtin_expect (p
[-1] != '\0', 0))
101 intern
->offset
+= invallen
;
109 internal_nis_endpwent (void)
119 struct response_t
*curr
= intern
.next
;
123 struct response_t
*last
= curr
;
128 intern
.next
= intern
.start
= NULL
;
133 _nss_nis_endpwent (void)
135 __libc_lock_lock (lock
);
137 internal_nis_endpwent ();
139 __libc_lock_unlock (lock
);
141 return NSS_STATUS_SUCCESS
;
146 internal_nis_setpwent (void)
148 /* We have to read all the data now. */
150 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
151 return NSS_STATUS_UNAVAIL
;
153 struct ypall_callback ypcb
;
155 ypcb
.foreach
= _nis_saveit
;
156 ypcb
.data
= (char *) &intern
;
157 enum nss_status status
= yperr2nss (yp_all (domain
, "passwd.byname", &ypcb
));
160 /* Mark the last buffer as full. */
161 if (intern
.next
!= NULL
)
162 intern
.next
->size
= intern
.offset
;
164 intern
.next
= intern
.start
;
172 _nss_nis_setpwent (int stayopen
)
174 enum nss_status result
= NSS_STATUS_SUCCESS
;
176 __libc_lock_lock (lock
);
178 internal_nis_endpwent ();
180 if (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
181 result
= internal_nis_setpwent ();
183 __libc_lock_unlock (lock
);
189 static enum nss_status
190 internal_nis_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
193 /* If we read the entire database at setpwent time we just iterate
194 over the data we have in memory. */
195 bool batch_read
= intern
.start
!= NULL
;
198 if (!batch_read
&& __builtin_expect (yp_get_default_domain (&domain
), 0))
199 return NSS_STATUS_UNAVAIL
;
201 /* Get the next entry until we found a correct one. */
212 struct response_t
*bucket
;
215 bucket
= intern
.next
;
217 if (__builtin_expect (intern
.offset
>= bucket
->size
, 0))
219 if (bucket
->next
== NULL
)
220 return NSS_STATUS_NOTFOUND
;
222 /* We look at all the content in the current bucket. Go on
224 bucket
= intern
.next
= bucket
->next
;
228 for (result
= &bucket
->mem
[intern
.offset
]; isspace (*result
);
232 len
= strlen (result
);
240 /* Maybe we should read the database in one piece. */
241 if ((_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
242 && internal_nis_setpwent () == NSS_STATUS_SUCCESS
243 && intern
.start
!= NULL
)
246 goto handle_batch_read
;
249 yperr
= yp_first (domain
, "passwd.byname", &outkey
, &keylen
,
253 yperr
= yp_next (domain
, "passwd.byname", oldkey
, oldkeylen
,
254 &outkey
, &keylen
, &result
, &len
);
256 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
258 enum nss_status retval
= yperr2nss (yperr
);
260 if (retval
== NSS_STATUS_TRYAGAIN
)
266 /* Check for adjunct style secret passwords. They can be
267 recognized by a password starting with "##". */
268 char *p
= strchr (result
, ':');
272 if (p
!= NULL
/* This better should be true in all cases. */
273 && p
[1] == '#' && p
[2] == '#'
274 && (namelen
= p
- result
,
275 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
276 &result2
, &len2
)) == YPERR_SUCCESS
)
278 /* We found a passwd.adjunct entry. Merge encrypted
279 password therein into original result. */
280 char *encrypted
= strchr (result2
, ':');
284 if (encrypted
== NULL
285 || (endp
= strchr (++encrypted
, ':')) == NULL
286 || (p
= strchr (p
+ 1, ':')) == NULL
)
288 /* Invalid format of the entry. This never should happen
289 unless the data from which the NIS table is generated is
290 wrong. We simply ignore it. */
295 restlen
= len
- (p
- result
);
296 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
297 + restlen
+ 2) > buflen
, 0))
302 return NSS_STATUS_TRYAGAIN
;
305 mempcpy (mempcpy (mempcpy (mempcpy (buffer
, result
, namelen
),
307 encrypted
, endp
- encrypted
),
316 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
320 return NSS_STATUS_TRYAGAIN
;
324 *((char *) mempcpy (buffer
, result
, len
)) = '\0';
332 parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
334 if (__builtin_expect (parse_res
== -1, 0))
339 return NSS_STATUS_TRYAGAIN
;
343 intern
.offset
+= len
+ 1;
352 while (parse_res
< 1);
354 return NSS_STATUS_SUCCESS
;
358 _nss_nis_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
363 __libc_lock_lock (lock
);
365 status
= internal_nis_getpwent_r (result
, buffer
, buflen
, errnop
);
367 __libc_lock_unlock (lock
);
373 _nss_nis_getpwnam_r (const char *name
, struct passwd
*pwd
,
374 char *buffer
, size_t buflen
, int *errnop
)
379 return NSS_STATUS_UNAVAIL
;
383 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
384 return NSS_STATUS_UNAVAIL
;
386 size_t namelen
= strlen (name
);
390 int yperr
= yp_match (domain
, "passwd.byname", name
, namelen
, &result
, &len
);
392 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
394 enum nss_status retval
= yperr2nss (yperr
);
396 if (retval
== NSS_STATUS_TRYAGAIN
)
401 /* Check for adjunct style secret passwords. They can be recognized
402 by a password starting with "##". */
405 char *p
= strchr (result
, ':');
406 if (p
!= NULL
/* This better should be true in all cases. */
407 && p
[1] == '#' && p
[2] == '#'
408 && yp_match (domain
, "passwd.adjunct.byname", name
, namelen
,
409 &result2
, &len2
) == YPERR_SUCCESS
)
411 /* We found a passwd.adjunct entry. Merge encrypted password
412 therein into original result. */
413 char *encrypted
= strchr (result2
, ':');
416 if (encrypted
== NULL
417 || (endp
= strchr (++encrypted
, ':')) == NULL
418 || (p
= strchr (p
+ 1, ':')) == NULL
)
420 /* Invalid format of the entry. This never should happen
421 unless the data from which the NIS table is generated is
422 wrong. We simply ignore it. */
427 size_t restlen
= len
- (p
- result
);
428 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
429 + restlen
+ 2) > buflen
, 0))
434 return NSS_STATUS_TRYAGAIN
;
437 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, name
, namelen
),
439 encrypted
, endp
- encrypted
),
448 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
452 return NSS_STATUS_TRYAGAIN
;
455 p
= strncpy (buffer
, result
, len
);
463 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
465 if (__builtin_expect (parse_res
< 1, 0))
468 return NSS_STATUS_TRYAGAIN
;
470 return NSS_STATUS_NOTFOUND
;
473 return NSS_STATUS_SUCCESS
;
477 _nss_nis_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
478 char *buffer
, size_t buflen
, int *errnop
)
481 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
482 return NSS_STATUS_UNAVAIL
;
485 int nlen
= snprintf (buf
, sizeof (buf
), "%lu", (unsigned long int) uid
);
489 int yperr
= yp_match (domain
, "passwd.byuid", buf
, nlen
, &result
, &len
);
491 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
493 enum nss_status retval
= yperr2nss (yperr
);
495 if (retval
== NSS_STATUS_TRYAGAIN
)
500 /* Check for adjunct style secret passwords. They can be recognized
501 by a password starting with "##". */
505 char *p
= strchr (result
, ':');
506 if (p
!= NULL
/* This better should be true in all cases. */
507 && p
[1] == '#' && p
[2] == '#'
508 && (namelen
= p
- result
,
509 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
510 &result2
, &len2
)) == YPERR_SUCCESS
)
512 /* We found a passwd.adjunct entry. Merge encrypted password
513 therein into original result. */
514 char *encrypted
= strchr (result2
, ':');
518 if (encrypted
== NULL
519 || (endp
= strchr (++encrypted
, ':')) == NULL
520 || (p
= strchr (p
+ 1, ':')) == NULL
)
522 /* Invalid format of the entry. This never should happen
523 unless the data from which the NIS table is generated is
524 wrong. We simply ignore it. */
529 restlen
= len
- (p
- result
);
530 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
531 + restlen
+ 2) > buflen
, 0))
536 return NSS_STATUS_TRYAGAIN
;
539 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
541 encrypted
, endp
- encrypted
),
550 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
554 return NSS_STATUS_TRYAGAIN
;
557 p
= strncpy (buffer
, result
, len
);
565 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
567 if (__builtin_expect (parse_res
< 1, 0))
570 return NSS_STATUS_TRYAGAIN
;
572 return NSS_STATUS_NOTFOUND
;
575 return NSS_STATUS_SUCCESS
;