1 /* Copyright (C) 1996, 1997, 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
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;
41 static char *oldkey
= NULL
;
42 static int oldkeylen
= 0;
45 _nss_nis_setpwent (void)
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_NOTFOUND
)
111 else if (retval
== NSS_STATUS_TRYAGAIN
)
116 /* Check for adjunct style secret passwords. They can be
117 recognized by a password starting with "##". */
118 p
= strchr (result
, ':');
119 if (p
!= NULL
/* This better should be true in all cases. */
120 && p
[1] == '#' && p
[2] == '#'
121 && (namelen
= p
- result
,
122 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
123 &result2
, &len2
)) == YPERR_SUCCESS
)
125 /* We found a passwd.adjunct entry. Merge encrypted
126 password therein into original result. */
127 char *encrypted
= strchr (result2
, ':');
131 if (encrypted
== NULL
132 || (endp
= strchr (++encrypted
, ':')) == NULL
133 || (p
= strchr (p
+ 1, ':')) == NULL
)
135 /* Invalid format of the entry. This never should happen
136 unless the data from which the NIS table is generated is
137 wrong. We simply ignore it. */
142 restlen
= len
- (p
- result
);
143 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
148 return NSS_STATUS_TRYAGAIN
;
151 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
153 encrypted
, endp
- encrypted
),
162 if ((size_t) (len
+ 1) > buflen
)
166 return NSS_STATUS_TRYAGAIN
;
169 p
= strncpy (buffer
, result
, len
);
177 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
182 return NSS_STATUS_TRYAGAIN
;
190 while (parse_res
< 1);
192 return NSS_STATUS_SUCCESS
;
196 _nss_nis_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
,
201 __libc_lock_lock (lock
);
203 status
= internal_nis_getpwent_r (result
, buffer
, buflen
, errnop
);
205 __libc_lock_unlock (lock
);
211 _nss_nis_getpwnam_r (const char *name
, struct passwd
*pwd
,
212 char *buffer
, size_t buflen
, int *errnop
)
214 struct parser_data
*data
= (void *) buffer
;
215 enum nss_status retval
;
216 char *domain
, *result
, *result2
, *p
;
217 int len
, len2
, parse_res
;
223 return NSS_STATUS_UNAVAIL
;
226 if (yp_get_default_domain (&domain
))
227 return NSS_STATUS_UNAVAIL
;
229 namelen
= strlen (name
);
231 retval
= yperr2nss (yp_match (domain
, "passwd.byname", name
,
232 namelen
, &result
, &len
));
234 if (retval
!= NSS_STATUS_SUCCESS
)
236 if (retval
== NSS_STATUS_NOTFOUND
)
238 else if (retval
== NSS_STATUS_TRYAGAIN
)
243 /* Check for adjunct style secret passwords. They can be recognized
244 by a password starting with "##". */
245 p
= strchr (result
, ':');
246 if (p
!= NULL
/* This better should be true in all cases. */
247 && p
[1] == '#' && p
[2] == '#'
248 && yp_match (domain
, "passwd.adjunct.byname", name
, namelen
,
249 &result2
, &len2
) == YPERR_SUCCESS
)
251 /* We found a passwd.adjunct entry. Merge encrypted password
252 therein into original result. */
253 char *encrypted
= strchr (result2
, ':');
257 if (encrypted
== NULL
258 || (endp
= strchr (++encrypted
, ':')) == NULL
259 || (p
= strchr (p
+ 1, ':')) == NULL
)
261 /* Invalid format of the entry. This never should happen
262 unless the data from which the NIS table is generated is
263 wrong. We simply ignore it. */
268 restlen
= len
- (p
- result
);
269 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
274 return NSS_STATUS_TRYAGAIN
;
277 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, name
, namelen
),
279 encrypted
, endp
- encrypted
),
288 if ((size_t) (len
+ 1) > buflen
)
292 return NSS_STATUS_TRYAGAIN
;
295 p
= strncpy (buffer
, result
, len
);
303 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
307 return NSS_STATUS_TRYAGAIN
;
311 return NSS_STATUS_NOTFOUND
;
315 return NSS_STATUS_SUCCESS
;
319 _nss_nis_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
320 char *buffer
, size_t buflen
, int *errnop
)
322 struct parser_data
*data
= (void *) buffer
;
323 enum nss_status retval
;
324 char *domain
, *result
, *p
, *result2
;
325 int len
, nlen
, parse_res
, len2
;
329 if (yp_get_default_domain (&domain
))
330 return NSS_STATUS_UNAVAIL
;
332 nlen
= sprintf (buf
, "%d", uid
);
334 retval
= yperr2nss (yp_match (domain
, "passwd.byuid", buf
,
335 nlen
, &result
, &len
));
337 if (retval
!= NSS_STATUS_SUCCESS
)
339 if (retval
== NSS_STATUS_NOTFOUND
)
341 else if (retval
== NSS_STATUS_TRYAGAIN
)
346 /* Check for adjunct style secret passwords. They can be recognized
347 by a password starting with "##". */
348 p
= strchr (result
, ':');
349 if (p
!= NULL
/* This better should be true in all cases. */
350 && p
[1] == '#' && p
[2] == '#'
351 && (namelen
= p
- result
,
352 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
353 &result2
, &len2
)) == YPERR_SUCCESS
)
355 /* We found a passwd.adjunct entry. Merge encrypted password
356 therein into original result. */
357 char *encrypted
= strchr (result2
, ':');
361 if (encrypted
== NULL
362 || (endp
= strchr (++encrypted
, ':')) == NULL
363 || (p
= strchr (p
+ 1, ':')) == NULL
)
365 /* Invalid format of the entry. This never should happen
366 unless the data from which the NIS table is generated is
367 wrong. We simply ignore it. */
372 restlen
= len
- (p
- result
);
373 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
378 return NSS_STATUS_TRYAGAIN
;
381 __mempcpy (__mempcpy (__mempcpy (__mempcpy (buffer
, result
, namelen
),
383 encrypted
, endp
- encrypted
),
392 if ((size_t) (len
+ 1) > buflen
)
396 return NSS_STATUS_TRYAGAIN
;
399 p
= strncpy (buffer
, result
, len
);
407 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
, errnop
);
411 return NSS_STATUS_TRYAGAIN
;
415 return NSS_STATUS_NOTFOUND
;
419 return NSS_STATUS_SUCCESS
;