1 /* Copyright (C) 1996-1998,2001-2003,2006,2009,2010
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
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 new_start
= true;
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)
116 struct response_t
*curr
= intern
.start
;
120 struct response_t
*last
= curr
;
125 intern
.next
= intern
.start
= NULL
;
130 _nss_nis_endpwent (void)
132 __libc_lock_lock (lock
);
134 internal_nis_endpwent ();
136 __libc_lock_unlock (lock
);
138 return NSS_STATUS_SUCCESS
;
143 internal_nis_setpwent (void)
145 /* We have to read all the data now. */
147 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
148 return NSS_STATUS_UNAVAIL
;
150 struct ypall_callback ypcb
;
152 ypcb
.foreach
= _nis_saveit
;
153 ypcb
.data
= (char *) &intern
;
154 enum nss_status status
= yperr2nss (yp_all (domain
, "passwd.byname", &ypcb
));
157 /* Mark the last buffer as full. */
158 if (intern
.next
!= NULL
)
159 intern
.next
->size
= intern
.offset
;
161 intern
.next
= intern
.start
;
169 _nss_nis_setpwent (int stayopen
)
171 enum nss_status result
= NSS_STATUS_SUCCESS
;
173 __libc_lock_lock (lock
);
175 internal_nis_endpwent ();
177 if (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
178 result
= internal_nis_setpwent ();
180 __libc_lock_unlock (lock
);
186 static enum nss_status
187 internal_nis_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
190 /* If we read the entire database at setpwent time we just iterate
191 over the data we have in memory. */
192 bool batch_read
= intern
.start
!= NULL
;
195 if (!batch_read
&& __builtin_expect (yp_get_default_domain (&domain
), 0))
196 return NSS_STATUS_UNAVAIL
;
198 /* Get the next entry until we found a correct one. */
209 struct response_t
*bucket
;
212 bucket
= intern
.next
;
214 if (__builtin_expect (intern
.offset
>= bucket
->size
, 0))
216 if (bucket
->next
== NULL
)
217 return NSS_STATUS_NOTFOUND
;
219 /* We look at all the content in the current bucket. Go on
221 bucket
= intern
.next
= bucket
->next
;
225 for (result
= &bucket
->mem
[intern
.offset
]; isspace (*result
);
229 len
= strlen (result
);
237 /* Maybe we should read the database in one piece. */
238 if ((_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
239 && internal_nis_setpwent () == NSS_STATUS_SUCCESS
240 && intern
.start
!= NULL
)
243 goto handle_batch_read
;
246 yperr
= yp_first (domain
, "passwd.byname", &outkey
, &keylen
,
250 yperr
= yp_next (domain
, "passwd.byname", oldkey
, oldkeylen
,
251 &outkey
, &keylen
, &result
, &len
);
253 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
255 enum nss_status retval
= yperr2nss (yperr
);
257 if (retval
== NSS_STATUS_TRYAGAIN
)
263 /* Check for adjunct style secret passwords. They can be
264 recognized by a password starting with "##". We do not use
265 it if the passwd.adjunct.byname table is supposed to be used
266 as a shadow.byname replacement. */
267 char *p
= strchr (result
, ':');
271 if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
) == 0
272 && 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.byname 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 "##". We do not use it if the
403 passwd.adjunct.byname table is supposed to be used as a shadow.byname
407 char *p
= strchr (result
, ':');
408 if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
) == 0
409 && p
!= NULL
/* This better should be true in all cases. */
410 && p
[1] == '#' && p
[2] == '#'
411 && yp_match (domain
, "passwd.adjunct.byname", name
, namelen
,
412 &result2
, &len2
) == YPERR_SUCCESS
)
414 /* We found a passwd.adjunct.byname entry. Merge encrypted password
415 therein into original result. */
416 char *encrypted
= strchr (result2
, ':');
419 if (encrypted
== NULL
420 || (endp
= strchr (++encrypted
, ':')) == NULL
421 || (p
= strchr (p
+ 1, ':')) == NULL
)
423 /* Invalid format of the entry. This never should happen
424 unless the data from which the NIS table is generated is
425 wrong. We simply ignore it. */
430 size_t restlen
= len
- (p
- result
);
431 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
432 + restlen
+ 2) > buflen
, 0))
437 return NSS_STATUS_TRYAGAIN
;
440 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, name
, namelen
),
442 encrypted
, endp
- encrypted
),
451 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
455 return NSS_STATUS_TRYAGAIN
;
458 p
= strncpy (buffer
, result
, len
);
466 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
468 if (__builtin_expect (parse_res
< 1, 0))
471 return NSS_STATUS_TRYAGAIN
;
473 return NSS_STATUS_NOTFOUND
;
476 return NSS_STATUS_SUCCESS
;
480 _nss_nis_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
481 char *buffer
, size_t buflen
, int *errnop
)
484 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
485 return NSS_STATUS_UNAVAIL
;
488 int nlen
= snprintf (buf
, sizeof (buf
), "%lu", (unsigned long int) uid
);
492 int yperr
= yp_match (domain
, "passwd.byuid", buf
, nlen
, &result
, &len
);
494 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
496 enum nss_status retval
= yperr2nss (yperr
);
498 if (retval
== NSS_STATUS_TRYAGAIN
)
503 /* Check for adjunct style secret passwords. They can be recognized
504 by a password starting with "##". We do not use it if the
505 passwd.adjunct.byname table is supposed to be used as a shadow.byname
510 char *p
= strchr (result
, ':');
511 if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
) == 0
512 && p
!= NULL
/* This better should be true in all cases. */
513 && p
[1] == '#' && p
[2] == '#'
514 && (namelen
= p
- result
,
515 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
516 &result2
, &len2
)) == YPERR_SUCCESS
)
518 /* We found a passwd.adjunct.byname entry. Merge encrypted password
519 therein into original result. */
520 char *encrypted
= strchr (result2
, ':');
524 if (encrypted
== NULL
525 || (endp
= strchr (++encrypted
, ':')) == NULL
526 || (p
= strchr (p
+ 1, ':')) == NULL
)
528 /* Invalid format of the entry. This never should happen
529 unless the data from which the NIS table is generated is
530 wrong. We simply ignore it. */
535 restlen
= len
- (p
- result
);
536 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
537 + restlen
+ 2) > buflen
, 0))
542 return NSS_STATUS_TRYAGAIN
;
545 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
547 encrypted
, endp
- encrypted
),
556 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
560 return NSS_STATUS_TRYAGAIN
;
563 p
= strncpy (buffer
, result
, len
);
571 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
573 if (__builtin_expect (parse_res
< 1, 0))
576 return NSS_STATUS_TRYAGAIN
;
578 return NSS_STATUS_NOTFOUND
;
581 return NSS_STATUS_SUCCESS
;