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;
48 struct response_t
*next
;
53 typedef struct intern_t
55 struct response_t
*start
;
56 struct response_t
*next
;
60 static intern_t intern
;
64 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
65 int invallen
, char *indata
)
67 if (instatus
!= YP_TRUE
)
70 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
72 struct response_t
*bucket
= intern
.next
;
74 if (__builtin_expect (bucket
== NULL
, 0))
76 #define MINSIZE 4096 - 4 * sizeof (void *)
77 const size_t minsize
= MAX (MINSIZE
, 2 * (invallen
+ 1));
78 bucket
= malloc (sizeof (struct response_t
) + minsize
);
80 /* We have no error code for out of memory. */
84 bucket
->size
= minsize
;
85 intern
.start
= intern
.next
= bucket
;
88 else if (__builtin_expect (invallen
+ 1 > bucket
->size
- intern
.offset
,
91 /* We need a new (larger) buffer. */
92 const size_t newsize
= 2 * MAX (bucket
->size
, invallen
+ 1);
93 struct response_t
*newp
= malloc (sizeof (struct response_t
)
96 /* We have no error code for out of memory. */
99 /* Mark the old bucket as full. */
100 bucket
->size
= intern
.offset
;
103 newp
->size
= newsize
;
104 bucket
= intern
.next
= bucket
->next
= newp
;
108 char *p
= mempcpy (&bucket
->mem
[intern
.offset
], inval
, invallen
);
109 if (__builtin_expect (p
[-1] != '\0', 0))
114 intern
.offset
+= invallen
;
122 internal_nis_endpwent (void)
132 struct response_t
*curr
= intern
.next
;
136 struct response_t
*last
= curr
;
141 intern
.next
= intern
.start
= NULL
;
146 _nss_nis_endpwent (void)
148 __libc_lock_lock (lock
);
150 internal_nis_endpwent ();
152 __libc_lock_unlock (lock
);
154 return NSS_STATUS_SUCCESS
;
159 internal_nis_setpwent (void)
161 /* We have to read all the data now. */
163 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
164 return NSS_STATUS_UNAVAIL
;
166 struct ypall_callback ypcb
;
168 ypcb
.foreach
= saveit
;
170 enum nss_status status
= yperr2nss (yp_all (domain
, "passwd.byname", &ypcb
));
173 /* Mark the last buffer as full. */
174 if (intern
.next
!= NULL
)
175 intern
.next
->size
= intern
.offset
;
177 intern
.next
= intern
.start
;
185 _nss_nis_setpwent (int stayopen
)
187 enum nss_status result
= NSS_STATUS_SUCCESS
;
189 __libc_lock_lock (lock
);
191 internal_nis_endpwent ();
193 if (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
194 result
= internal_nis_setpwent ();
196 __libc_lock_unlock (lock
);
202 static enum nss_status
203 internal_nis_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
206 /* If we read the entire database at setpwent time we just iterate
207 over the data we have in memory. */
208 bool batch_read
= intern
.start
!= NULL
;
211 if (!batch_read
&& __builtin_expect (yp_get_default_domain (&domain
), 0))
212 return NSS_STATUS_UNAVAIL
;
214 /* Get the next entry until we found a correct one. */
225 struct response_t
*bucket
;
228 bucket
= intern
.next
;
230 if (__builtin_expect (intern
.offset
>= bucket
->size
, 0))
232 if (bucket
->next
== NULL
)
233 return NSS_STATUS_NOTFOUND
;
235 /* We look at all the content in the current bucket. Go on
237 bucket
= intern
.next
= bucket
->next
;
241 for (result
= &bucket
->mem
[intern
.offset
]; isspace (*result
);
245 len
= strlen (result
);
253 /* Maybe we should read the database in one piece. */
254 if ((_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
255 && internal_nis_setpwent () == NSS_STATUS_SUCCESS
256 && intern
.start
!= NULL
)
259 goto handle_batch_read
;
262 yperr
= yp_first (domain
, "passwd.byname", &outkey
, &keylen
,
266 yperr
= yp_next (domain
, "passwd.byname", oldkey
, oldkeylen
,
267 &outkey
, &keylen
, &result
, &len
);
269 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
271 enum nss_status retval
= yperr2nss (yperr
);
273 if (retval
== NSS_STATUS_TRYAGAIN
)
279 /* Check for adjunct style secret passwords. They can be
280 recognized by a password starting with "##". */
281 char *p
= strchr (result
, ':');
285 if (p
!= NULL
/* This better should be true in all cases. */
286 && p
[1] == '#' && p
[2] == '#'
287 && (namelen
= p
- result
,
288 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
289 &result2
, &len2
)) == YPERR_SUCCESS
)
291 /* We found a passwd.adjunct entry. Merge encrypted
292 password therein into original result. */
293 char *encrypted
= strchr (result2
, ':');
297 if (encrypted
== NULL
298 || (endp
= strchr (++encrypted
, ':')) == NULL
299 || (p
= strchr (p
+ 1, ':')) == NULL
)
301 /* Invalid format of the entry. This never should happen
302 unless the data from which the NIS table is generated is
303 wrong. We simply ignore it. */
308 restlen
= len
- (p
- result
);
309 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
310 + restlen
+ 2) > buflen
, 0))
315 return NSS_STATUS_TRYAGAIN
;
318 mempcpy (mempcpy (mempcpy (mempcpy (buffer
, result
, namelen
),
320 encrypted
, endp
- encrypted
),
329 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
333 return NSS_STATUS_TRYAGAIN
;
337 *((char *) mempcpy (buffer
, result
, len
)) = '\0';
345 parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
347 if (__builtin_expect (parse_res
== -1, 0))
351 return NSS_STATUS_TRYAGAIN
;
355 intern
.offset
+= len
+ 1;
364 while (parse_res
< 1);
366 return NSS_STATUS_SUCCESS
;
370 _nss_nis_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
375 __libc_lock_lock (lock
);
377 status
= internal_nis_getpwent_r (result
, buffer
, buflen
, errnop
);
379 __libc_lock_unlock (lock
);
385 _nss_nis_getpwnam_r (const char *name
, struct passwd
*pwd
,
386 char *buffer
, size_t buflen
, int *errnop
)
391 return NSS_STATUS_UNAVAIL
;
395 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
396 return NSS_STATUS_UNAVAIL
;
398 size_t namelen
= strlen (name
);
402 int yperr
= yp_match (domain
, "passwd.byname", name
, namelen
, &result
, &len
);
404 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
406 enum nss_status retval
= yperr2nss (yperr
);
408 if (retval
== NSS_STATUS_TRYAGAIN
)
413 /* Check for adjunct style secret passwords. They can be recognized
414 by a password starting with "##". */
417 char *p
= strchr (result
, ':');
418 if (p
!= NULL
/* This better should be true in all cases. */
419 && p
[1] == '#' && p
[2] == '#'
420 && yp_match (domain
, "passwd.adjunct.byname", name
, namelen
,
421 &result2
, &len2
) == YPERR_SUCCESS
)
423 /* We found a passwd.adjunct entry. Merge encrypted password
424 therein into original result. */
425 char *encrypted
= strchr (result2
, ':');
428 if (encrypted
== NULL
429 || (endp
= strchr (++encrypted
, ':')) == NULL
430 || (p
= strchr (p
+ 1, ':')) == NULL
)
432 /* Invalid format of the entry. This never should happen
433 unless the data from which the NIS table is generated is
434 wrong. We simply ignore it. */
439 size_t restlen
= len
- (p
- result
);
440 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
441 + restlen
+ 2) > buflen
, 0))
446 return NSS_STATUS_TRYAGAIN
;
449 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, name
, namelen
),
451 encrypted
, endp
- encrypted
),
460 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
464 return NSS_STATUS_TRYAGAIN
;
467 p
= strncpy (buffer
, result
, len
);
475 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
477 if (__builtin_expect (parse_res
< 1, 0))
480 return NSS_STATUS_TRYAGAIN
;
482 return NSS_STATUS_NOTFOUND
;
485 return NSS_STATUS_SUCCESS
;
489 _nss_nis_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
490 char *buffer
, size_t buflen
, int *errnop
)
493 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
494 return NSS_STATUS_UNAVAIL
;
497 int nlen
= snprintf (buf
, sizeof (buf
), "%lu", (unsigned long int) uid
);
501 int yperr
= yp_match (domain
, "passwd.byuid", buf
, nlen
, &result
, &len
);
503 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
505 enum nss_status retval
= yperr2nss (yperr
);
507 if (retval
== NSS_STATUS_TRYAGAIN
)
512 /* Check for adjunct style secret passwords. They can be recognized
513 by a password starting with "##". */
517 char *p
= strchr (result
, ':');
518 if (p
!= NULL
/* This better should be true in all cases. */
519 && p
[1] == '#' && p
[2] == '#'
520 && (namelen
= p
- result
,
521 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
522 &result2
, &len2
)) == YPERR_SUCCESS
)
524 /* We found a passwd.adjunct entry. Merge encrypted password
525 therein into original result. */
526 char *encrypted
= strchr (result2
, ':');
530 if (encrypted
== NULL
531 || (endp
= strchr (++encrypted
, ':')) == NULL
532 || (p
= strchr (p
+ 1, ':')) == NULL
)
534 /* Invalid format of the entry. This never should happen
535 unless the data from which the NIS table is generated is
536 wrong. We simply ignore it. */
541 restlen
= len
- (p
- result
);
542 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
543 + restlen
+ 2) > buflen
, 0))
548 return NSS_STATUS_TRYAGAIN
;
551 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
553 encrypted
, endp
- encrypted
),
562 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
566 return NSS_STATUS_TRYAGAIN
;
569 p
= strncpy (buffer
, result
, len
);
577 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
579 if (__builtin_expect (parse_res
< 1, 0))
582 return NSS_STATUS_TRYAGAIN
;
584 return NSS_STATUS_NOTFOUND
;
587 return NSS_STATUS_SUCCESS
;