1 /* Copyright (C) 1996-1998, 2001, 2002, 2003 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
21 /* The following is an ugly trick to avoid a prototype declaration for
23 #define _nss_nis_endpwent _nss_nis_endpwent_XXX
25 #undef _nss_nis_endpwent
29 #include <bits/libc-lock.h>
30 #include <rpcsvc/yp.h>
31 #include <rpcsvc/ypclnt.h>
35 /* Get the declaration of the parser function. */
37 #define STRUCTURE passwd
39 #include <nss/nss_files/files-parse.c>
41 /* Protect global state against multiple changers */
42 __libc_lock_define_initialized (static, lock
)
44 static bool_t new_start
= 1;
49 _nss_nis_setpwent (int stayopen
)
51 __libc_lock_lock (lock
);
61 __libc_lock_unlock (lock
);
63 return NSS_STATUS_SUCCESS
;
65 /* Make _nss_nis_endpwent an alias of _nss_nis_setpwent. We do this
66 even though the prototypes don't match. The argument of setpwent
67 is not used so this makes no difference. */
68 strong_alias (_nss_nis_setpwent
, _nss_nis_endpwent
)
70 static enum nss_status
71 internal_nis_getpwent_r (struct passwd
*pwd
, char *buffer
, size_t buflen
,
74 struct parser_data
*data
= (void *) buffer
;
78 if (yp_get_default_domain (&domain
))
79 return NSS_STATUS_UNAVAIL
;
81 /* Get the next entry until we found a correct one. */
84 enum nss_status retval
;
85 char *result
, *outkey
, *result2
, *p
;
86 int len
, keylen
, len2
;
90 retval
= yperr2nss (yp_first (domain
, "passwd.byname",
91 &outkey
, &keylen
, &result
, &len
));
93 retval
= yperr2nss ( yp_next (domain
, "passwd.byname",
95 &outkey
, &keylen
, &result
, &len
));
97 if (retval
!= NSS_STATUS_SUCCESS
)
99 if (retval
== NSS_STATUS_TRYAGAIN
)
104 /* Check for adjunct style secret passwords. They can be
105 recognized by a password starting with "##". */
106 p
= strchr (result
, ':');
107 if (p
!= NULL
/* This better should be true in all cases. */
108 && p
[1] == '#' && p
[2] == '#'
109 && (namelen
= p
- result
,
110 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
111 &result2
, &len2
)) == YPERR_SUCCESS
)
113 /* We found a passwd.adjunct entry. Merge encrypted
114 password therein into original result. */
115 char *encrypted
= strchr (result2
, ':');
119 if (encrypted
== NULL
120 || (endp
= strchr (++encrypted
, ':')) == NULL
121 || (p
= strchr (p
+ 1, ':')) == NULL
)
123 /* Invalid format of the entry. This never should happen
124 unless the data from which the NIS table is generated is
125 wrong. We simply ignore it. */
130 restlen
= len
- (p
- result
);
131 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
136 return NSS_STATUS_TRYAGAIN
;
139 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
141 encrypted
, endp
- encrypted
),
150 if ((size_t) (len
+ 1) > buflen
)
154 return NSS_STATUS_TRYAGAIN
;
157 p
= strncpy (buffer
, result
, len
);
165 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
170 return NSS_STATUS_TRYAGAIN
;
178 while (parse_res
< 1);
180 return NSS_STATUS_SUCCESS
;
184 _nss_nis_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
189 __libc_lock_lock (lock
);
191 status
= internal_nis_getpwent_r (result
, buffer
, buflen
, errnop
);
193 __libc_lock_unlock (lock
);
199 _nss_nis_getpwnam_r (const char *name
, struct passwd
*pwd
,
200 char *buffer
, size_t buflen
, int *errnop
)
202 struct parser_data
*data
= (void *) buffer
;
203 enum nss_status retval
;
204 char *domain
, *result
, *result2
, *p
;
205 int len
, len2
, parse_res
;
211 return NSS_STATUS_UNAVAIL
;
214 if (yp_get_default_domain (&domain
))
215 return NSS_STATUS_UNAVAIL
;
217 namelen
= strlen (name
);
219 retval
= yperr2nss (yp_match (domain
, "passwd.byname", name
,
220 namelen
, &result
, &len
));
222 if (retval
!= NSS_STATUS_SUCCESS
)
224 if (retval
== NSS_STATUS_TRYAGAIN
)
229 /* Check for adjunct style secret passwords. They can be recognized
230 by a password starting with "##". */
231 p
= strchr (result
, ':');
232 if (p
!= NULL
/* This better should be true in all cases. */
233 && p
[1] == '#' && p
[2] == '#'
234 && yp_match (domain
, "passwd.adjunct.byname", name
, namelen
,
235 &result2
, &len2
) == YPERR_SUCCESS
)
237 /* We found a passwd.adjunct entry. Merge encrypted password
238 therein into original result. */
239 char *encrypted
= strchr (result2
, ':');
243 if (encrypted
== NULL
244 || (endp
= strchr (++encrypted
, ':')) == NULL
245 || (p
= strchr (p
+ 1, ':')) == NULL
)
247 /* Invalid format of the entry. This never should happen
248 unless the data from which the NIS table is generated is
249 wrong. We simply ignore it. */
254 restlen
= len
- (p
- result
);
255 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
260 return NSS_STATUS_TRYAGAIN
;
263 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, name
, namelen
),
265 encrypted
, endp
- encrypted
),
274 if ((size_t) (len
+ 1) > buflen
)
278 return NSS_STATUS_TRYAGAIN
;
281 p
= strncpy (buffer
, result
, len
);
289 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
293 return NSS_STATUS_TRYAGAIN
;
295 return NSS_STATUS_NOTFOUND
;
298 return NSS_STATUS_SUCCESS
;
302 _nss_nis_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
303 char *buffer
, size_t buflen
, int *errnop
)
305 struct parser_data
*data
= (void *) buffer
;
306 enum nss_status retval
;
307 char *domain
, *result
, *p
, *result2
;
308 int len
, nlen
, parse_res
, len2
;
312 if (yp_get_default_domain (&domain
))
313 return NSS_STATUS_UNAVAIL
;
315 nlen
= sprintf (buf
, "%lu", (unsigned long int) uid
);
317 retval
= yperr2nss (yp_match (domain
, "passwd.byuid", buf
,
318 nlen
, &result
, &len
));
320 if (retval
!= NSS_STATUS_SUCCESS
)
322 if (retval
== NSS_STATUS_TRYAGAIN
)
327 /* Check for adjunct style secret passwords. They can be recognized
328 by a password starting with "##". */
329 p
= strchr (result
, ':');
330 if (p
!= NULL
/* This better should be true in all cases. */
331 && p
[1] == '#' && p
[2] == '#'
332 && (namelen
= p
- result
,
333 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
334 &result2
, &len2
)) == YPERR_SUCCESS
)
336 /* We found a passwd.adjunct entry. Merge encrypted password
337 therein into original result. */
338 char *encrypted
= strchr (result2
, ':');
342 if (encrypted
== NULL
343 || (endp
= strchr (++encrypted
, ':')) == NULL
344 || (p
= strchr (p
+ 1, ':')) == NULL
)
346 /* Invalid format of the entry. This never should happen
347 unless the data from which the NIS table is generated is
348 wrong. We simply ignore it. */
353 restlen
= len
- (p
- result
);
354 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
359 return NSS_STATUS_TRYAGAIN
;
362 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
364 encrypted
, endp
- encrypted
),
373 if ((size_t) (len
+ 1) > buflen
)
377 return NSS_STATUS_TRYAGAIN
;
380 p
= strncpy (buffer
, result
, len
);
388 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
392 return NSS_STATUS_TRYAGAIN
;
394 return NSS_STATUS_NOTFOUND
;
397 return NSS_STATUS_SUCCESS
;