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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/yp.h>
29 #include <rpcsvc/ypclnt.h>
34 /* Get the declaration of the parser function. */
36 #define STRUCTURE passwd
38 #include <nss/nss_files/files-parse.c>
40 /* Protect global state against multiple changers */
41 __libc_lock_define_initialized (static, lock
)
43 static bool new_start
= true;
46 static intern_t intern
;
50 _nis_saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
51 int invallen
, char *indata
)
53 intern_t
*intern
= (intern_t
*) indata
;
55 if (instatus
!= YP_TRUE
)
58 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
60 struct response_t
*bucket
= intern
->next
;
62 if (__builtin_expect (bucket
== NULL
, 0))
64 #define MINSIZE 4096 - 4 * sizeof (void *)
65 const size_t minsize
= MAX (MINSIZE
, 2 * (invallen
+ 1));
66 bucket
= malloc (sizeof (struct response_t
) + minsize
);
68 /* We have no error code for out of memory. */
72 bucket
->size
= minsize
;
73 intern
->start
= intern
->next
= bucket
;
76 else if (__builtin_expect (invallen
+ 1 > bucket
->size
- intern
->offset
,
79 /* We need a new (larger) buffer. */
80 const size_t newsize
= 2 * MAX (bucket
->size
, invallen
+ 1);
81 struct response_t
*newp
= malloc (sizeof (struct response_t
)
84 /* We have no error code for out of memory. */
87 /* Mark the old bucket as full. */
88 bucket
->size
= intern
->offset
;
92 bucket
= intern
->next
= bucket
->next
= newp
;
96 char *p
= mempcpy (&bucket
->mem
[intern
->offset
], inval
, invallen
);
97 if (__builtin_expect (p
[-1] != '\0', 0))
102 intern
->offset
+= invallen
;
110 internal_nis_endpwent (void)
117 struct response_t
*curr
= intern
.start
;
121 struct response_t
*last
= curr
;
126 intern
.next
= intern
.start
= NULL
;
131 _nss_nis_endpwent (void)
133 __libc_lock_lock (lock
);
135 internal_nis_endpwent ();
137 __libc_lock_unlock (lock
);
139 return NSS_STATUS_SUCCESS
;
144 internal_nis_setpwent (void)
146 /* We have to read all the data now. */
148 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
149 return NSS_STATUS_UNAVAIL
;
151 struct ypall_callback ypcb
;
153 ypcb
.foreach
= _nis_saveit
;
154 ypcb
.data
= (char *) &intern
;
155 enum nss_status status
= yperr2nss (yp_all (domain
, "passwd.byname", &ypcb
));
158 /* Mark the last buffer as full. */
159 if (intern
.next
!= NULL
)
160 intern
.next
->size
= intern
.offset
;
162 intern
.next
= intern
.start
;
170 _nss_nis_setpwent (int stayopen
)
172 enum nss_status result
= NSS_STATUS_SUCCESS
;
174 __libc_lock_lock (lock
);
176 internal_nis_endpwent ();
178 if (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
179 result
= internal_nis_setpwent ();
181 __libc_lock_unlock (lock
);
187 static enum nss_status
188 internal_nis_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
191 /* If we read the entire database at setpwent time we just iterate
192 over the data we have in memory. */
193 bool batch_read
= intern
.start
!= NULL
;
196 if (!batch_read
&& __builtin_expect (yp_get_default_domain (&domain
), 0))
197 return NSS_STATUS_UNAVAIL
;
199 /* Get the next entry until we found a correct one. */
210 struct response_t
*bucket
;
213 bucket
= intern
.next
;
215 if (__builtin_expect (intern
.offset
>= bucket
->size
, 0))
217 if (bucket
->next
== NULL
)
218 return NSS_STATUS_NOTFOUND
;
220 /* We look at all the content in the current bucket. Go on
222 bucket
= intern
.next
= bucket
->next
;
226 for (result
= &bucket
->mem
[intern
.offset
]; isspace (*result
);
230 len
= strlen (result
);
238 /* Maybe we should read the database in one piece. */
239 if ((_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ
)
240 && internal_nis_setpwent () == NSS_STATUS_SUCCESS
241 && intern
.start
!= NULL
)
244 goto handle_batch_read
;
247 yperr
= yp_first (domain
, "passwd.byname", &outkey
, &keylen
,
251 yperr
= yp_next (domain
, "passwd.byname", oldkey
, oldkeylen
,
252 &outkey
, &keylen
, &result
, &len
);
254 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
256 enum nss_status retval
= yperr2nss (yperr
);
258 if (retval
== NSS_STATUS_TRYAGAIN
)
264 /* Check for adjunct style secret passwords. They can be
265 recognized by a password starting with "##". We do not use
266 it if the passwd.adjunct.byname table is supposed to be used
267 as a shadow.byname replacement. */
268 char *p
= strchr (result
, ':');
272 if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
) == 0
273 && p
!= NULL
/* This better should be true in all cases. */
274 && p
[1] == '#' && p
[2] == '#'
275 && (namelen
= p
- result
,
276 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
277 &result2
, &len2
)) == YPERR_SUCCESS
)
279 /* We found a passwd.adjunct.byname entry. Merge encrypted
280 password therein into original result. */
281 char *encrypted
= strchr (result2
, ':');
285 if (encrypted
== NULL
286 || (endp
= strchr (++encrypted
, ':')) == NULL
287 || (p
= strchr (p
+ 1, ':')) == NULL
)
289 /* Invalid format of the entry. This never should happen
290 unless the data from which the NIS table is generated is
291 wrong. We simply ignore it. */
296 restlen
= len
- (p
- result
);
297 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
298 + restlen
+ 2) > buflen
, 0))
303 return NSS_STATUS_TRYAGAIN
;
306 mempcpy (mempcpy (mempcpy (mempcpy (buffer
, result
, namelen
),
308 encrypted
, endp
- encrypted
),
317 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
321 return NSS_STATUS_TRYAGAIN
;
325 *((char *) mempcpy (buffer
, result
, len
)) = '\0';
333 parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
335 if (__builtin_expect (parse_res
== -1, 0))
340 return NSS_STATUS_TRYAGAIN
;
344 intern
.offset
+= len
+ 1;
353 while (parse_res
< 1);
355 return NSS_STATUS_SUCCESS
;
359 _nss_nis_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
364 __libc_lock_lock (lock
);
366 status
= internal_nis_getpwent_r (result
, buffer
, buflen
, errnop
);
368 __libc_lock_unlock (lock
);
374 _nss_nis_getpwnam_r (const char *name
, struct passwd
*pwd
,
375 char *buffer
, size_t buflen
, int *errnop
)
380 return NSS_STATUS_UNAVAIL
;
384 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
385 return NSS_STATUS_UNAVAIL
;
387 size_t namelen
= strlen (name
);
391 int yperr
= yp_match (domain
, "passwd.byname", name
, namelen
, &result
, &len
);
393 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
395 enum nss_status retval
= yperr2nss (yperr
);
397 if (retval
== NSS_STATUS_TRYAGAIN
)
402 /* Check for adjunct style secret passwords. They can be recognized
403 by a password starting with "##". We do not use it if the
404 passwd.adjunct.byname table is supposed to be used as a shadow.byname
408 char *p
= strchr (result
, ':');
409 if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
) == 0
410 && p
!= NULL
/* This better should be true in all cases. */
411 && p
[1] == '#' && p
[2] == '#'
412 && yp_match (domain
, "passwd.adjunct.byname", name
, namelen
,
413 &result2
, &len2
) == YPERR_SUCCESS
)
415 /* We found a passwd.adjunct.byname entry. Merge encrypted password
416 therein into original result. */
417 char *encrypted
= strchr (result2
, ':');
420 if (encrypted
== NULL
421 || (endp
= strchr (++encrypted
, ':')) == NULL
422 || (p
= strchr (p
+ 1, ':')) == NULL
)
424 /* Invalid format of the entry. This never should happen
425 unless the data from which the NIS table is generated is
426 wrong. We simply ignore it. */
431 size_t restlen
= len
- (p
- result
);
432 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
433 + restlen
+ 2) > buflen
, 0))
438 return NSS_STATUS_TRYAGAIN
;
441 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, name
, namelen
),
443 encrypted
, endp
- encrypted
),
452 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
456 return NSS_STATUS_TRYAGAIN
;
459 p
= strncpy (buffer
, result
, len
);
467 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
469 if (__builtin_expect (parse_res
< 1, 0))
472 return NSS_STATUS_TRYAGAIN
;
474 return NSS_STATUS_NOTFOUND
;
477 return NSS_STATUS_SUCCESS
;
481 _nss_nis_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
482 char *buffer
, size_t buflen
, int *errnop
)
485 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
486 return NSS_STATUS_UNAVAIL
;
489 int nlen
= snprintf (buf
, sizeof (buf
), "%lu", (unsigned long int) uid
);
493 int yperr
= yp_match (domain
, "passwd.byuid", buf
, nlen
, &result
, &len
);
495 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
497 enum nss_status retval
= yperr2nss (yperr
);
499 if (retval
== NSS_STATUS_TRYAGAIN
)
504 /* Check for adjunct style secret passwords. They can be recognized
505 by a password starting with "##". We do not use it if the
506 passwd.adjunct.byname table is supposed to be used as a shadow.byname
511 char *p
= strchr (result
, ':');
512 if ((_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
) == 0
513 && p
!= NULL
/* This better should be true in all cases. */
514 && p
[1] == '#' && p
[2] == '#'
515 && (namelen
= p
- result
,
516 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
517 &result2
, &len2
)) == YPERR_SUCCESS
)
519 /* We found a passwd.adjunct.byname entry. Merge encrypted password
520 therein into original result. */
521 char *encrypted
= strchr (result2
, ':');
525 if (encrypted
== NULL
526 || (endp
= strchr (++encrypted
, ':')) == NULL
527 || (p
= strchr (p
+ 1, ':')) == NULL
)
529 /* Invalid format of the entry. This never should happen
530 unless the data from which the NIS table is generated is
531 wrong. We simply ignore it. */
536 restlen
= len
- (p
- result
);
537 if (__builtin_expect ((size_t) (namelen
+ (endp
- encrypted
)
538 + restlen
+ 2) > buflen
, 0))
543 return NSS_STATUS_TRYAGAIN
;
546 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
548 encrypted
, endp
- encrypted
),
557 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
561 return NSS_STATUS_TRYAGAIN
;
564 p
= strncpy (buffer
, result
, len
);
572 int parse_res
= _nss_files_parse_pwent (p
, pwd
, (void *) buffer
, buflen
,
574 if (__builtin_expect (parse_res
< 1, 0))
577 return NSS_STATUS_TRYAGAIN
;
579 return NSS_STATUS_NOTFOUND
;
582 return NSS_STATUS_SUCCESS
;