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
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
31 /* Get the declaration of the parser function. */
33 #define STRUCTURE passwd
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;
45 _nss_nis_setpwent (int stayopen
)
47 __libc_lock_lock (lock
);
57 __libc_lock_unlock (lock
);
59 return NSS_STATUS_SUCCESS
;
63 _nss_nis_endpwent (void)
65 __libc_lock_lock (lock
);
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
,
84 struct parser_data
*data
= (void *) buffer
;
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
;
100 retval
= yperr2nss (yp_first (domain
, "passwd.byname",
101 &outkey
, &keylen
, &result
, &len
));
103 retval
= yperr2nss ( yp_next (domain
, "passwd.byname",
105 &outkey
, &keylen
, &result
, &len
));
107 if (retval
!= NSS_STATUS_SUCCESS
)
109 if (retval
== NSS_STATUS_TRYAGAIN
)
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
, ':');
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. */
140 restlen
= len
- (p
- result
);
141 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
146 return NSS_STATUS_TRYAGAIN
;
149 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
151 encrypted
, endp
- encrypted
),
160 if ((size_t) (len
+ 1) > buflen
)
164 return NSS_STATUS_TRYAGAIN
;
167 p
= strncpy (buffer
, result
, len
);
175 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
180 return NSS_STATUS_TRYAGAIN
;
188 while (parse_res
< 1);
190 return NSS_STATUS_SUCCESS
;
194 _nss_nis_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
199 __libc_lock_lock (lock
);
201 status
= internal_nis_getpwent_r (result
, buffer
, buflen
, errnop
);
203 __libc_lock_unlock (lock
);
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
;
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
)
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
, ':');
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. */
264 restlen
= len
- (p
- result
);
265 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
270 return NSS_STATUS_TRYAGAIN
;
273 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, name
, namelen
),
275 encrypted
, endp
- encrypted
),
284 if ((size_t) (len
+ 1) > buflen
)
288 return NSS_STATUS_TRYAGAIN
;
291 p
= strncpy (buffer
, result
, len
);
299 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
303 return NSS_STATUS_TRYAGAIN
;
305 return NSS_STATUS_NOTFOUND
;
308 return NSS_STATUS_SUCCESS
;
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
;
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
)
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
, ':');
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. */
363 restlen
= len
- (p
- result
);
364 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
369 return NSS_STATUS_TRYAGAIN
;
372 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
374 encrypted
, endp
- encrypted
),
383 if ((size_t) (len
+ 1) > buflen
)
387 return NSS_STATUS_TRYAGAIN
;
390 p
= strncpy (buffer
, result
, len
);
398 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
402 return NSS_STATUS_TRYAGAIN
;
404 return NSS_STATUS_NOTFOUND
;
407 return NSS_STATUS_SUCCESS
;