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. */
21 #include <glibc-compat/include/pwd.h>
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_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
)
83 struct parser_data
*data
= (void *) buffer
;
87 if (yp_get_default_domain (&domain
))
88 return NSS_STATUS_UNAVAIL
;
90 /* Get the next entry until we found a correct one. */
93 enum nss_status retval
;
94 char *result
, *outkey
, *result2
, *p
;
95 int len
, keylen
, len2
;
99 retval
= yperr2nss (yp_first (domain
, "passwd.byname",
100 &outkey
, &keylen
, &result
, &len
));
102 retval
= yperr2nss ( yp_next (domain
, "passwd.byname",
104 &outkey
, &keylen
, &result
, &len
));
106 if (retval
!= NSS_STATUS_SUCCESS
)
108 if (retval
== NSS_STATUS_TRYAGAIN
)
109 __set_errno (EAGAIN
);
113 /* Check for adjunct style secret passwords. They can be
114 recognized by a password starting with "##". */
115 p
= strchr (result
, ':');
116 if (p
!= NULL
/* This better should be true in all cases. */
117 && p
[1] == '#' && p
[2] == '#'
118 && (namelen
= p
- result
,
119 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
120 &result2
, &len2
)) == YPERR_SUCCESS
)
122 /* We found a passwd.adjunct entry. Merge encrypted
123 password therein into original result. */
124 char *encrypted
= strchr (result2
, ':');
128 if (encrypted
== NULL
129 || (endp
= strchr (++encrypted
, ':')) == NULL
130 || (p
= strchr (p
+ 1, ':')) == NULL
)
132 /* Invalid format of the entry. This never should happen
133 unless the data from which the NIS table is generated is
134 wrong. We simply ignore it. */
139 restlen
= len
- (p
- result
);
140 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
144 __set_errno (ERANGE
);
145 return NSS_STATUS_TRYAGAIN
;
148 memcpy (buffer
, result
, namelen
);
149 tmp
= buffer
+ namelen
;
151 memcpy (tmp
, encrypted
, endp
- encrypted
);
152 tmp
+= endp
- encrypted
;
153 memcpy (tmp
, p
, restlen
+ 1);
161 if ((size_t) (len
+ 1) > buflen
)
164 __set_errno (ERANGE
);
165 return NSS_STATUS_TRYAGAIN
;
168 p
= strncpy (buffer
, result
, len
);
172 p
= strncpy (buffer
, result
, len
);
178 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
);
179 if (parse_res
== -1 && errno
== ERANGE
)
180 return NSS_STATUS_TRYAGAIN
;
189 return NSS_STATUS_SUCCESS
;
193 _nss_nis_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
)
197 __libc_lock_lock (lock
);
199 status
= internal_nis_getpwent_r (result
, buffer
, buflen
);
201 __libc_lock_unlock (lock
);
207 _nss_nis_getpwnam_r (const char *name
, struct passwd
*pwd
,
208 char *buffer
, size_t buflen
)
210 struct parser_data
*data
= (void *) buffer
;
211 enum nss_status retval
;
212 char *domain
, *result
, *result2
, *p
;
213 int len
, len2
, parse_res
;
218 __set_errno (EINVAL
);
219 return NSS_STATUS_UNAVAIL
;
222 if (yp_get_default_domain (&domain
))
223 return NSS_STATUS_UNAVAIL
;
225 namelen
= strlen (name
);
227 retval
= yperr2nss (yp_match (domain
, "passwd.byname", name
,
228 namelen
, &result
, &len
));
230 if (retval
!= NSS_STATUS_SUCCESS
)
232 if (retval
== NSS_STATUS_TRYAGAIN
)
233 __set_errno (EAGAIN
);
237 /* Check for adjunct style secret passwords. They can be recognized
238 by a password starting with "##". */
239 p
= strchr (result
, ':');
240 if (p
!= NULL
/* This better should be true in all cases. */
241 && p
[1] == '#' && p
[2] == '#'
242 && (namelen
= p
- result
,
243 yp_match (domain
, "passwd.adjunct.byname", name
, namelen
,
244 &result2
, &len2
)) == YPERR_SUCCESS
)
246 /* We found a passwd.adjunct entry. Merge encrypted password
247 therein into original result. */
248 char *encrypted
= strchr (result2
, ':');
252 if (encrypted
== NULL
253 || (endp
= strchr (++encrypted
, ':')) == NULL
254 || (p
= strchr (p
+ 1, ':')) == NULL
)
256 /* Invalid format of the entry. This never should happen
257 unless the data from which the NIS table is generated is
258 wrong. We simply ignore it. */
263 restlen
= len
- (p
- result
);
264 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
268 __set_errno (ERANGE
);
269 return NSS_STATUS_TRYAGAIN
;
272 memcpy (buffer
, name
, namelen
);
273 tmp
= buffer
+ namelen
;
275 memcpy (tmp
, encrypted
, endp
- encrypted
);
276 tmp
+= endp
- encrypted
;
277 memcpy (tmp
, p
, restlen
+ 1);
285 if ((size_t) (len
+ 1) > buflen
)
288 __set_errno (ERANGE
);
289 return NSS_STATUS_TRYAGAIN
;
292 p
= strncpy (buffer
, result
, len
);
300 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
);
302 if (parse_res
== -1 && errno
== ERANGE
)
303 return NSS_STATUS_TRYAGAIN
;
304 else if (parse_res
== 0)
305 return NSS_STATUS_NOTFOUND
;
307 return NSS_STATUS_SUCCESS
;
311 _nss_nis_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
312 char *buffer
, size_t buflen
)
314 struct parser_data
*data
= (void *) buffer
;
315 enum nss_status retval
;
316 char *domain
, *result
, *p
, *result2
;
317 int len
, nlen
, parse_res
, len2
;
321 if (yp_get_default_domain (&domain
))
322 return NSS_STATUS_UNAVAIL
;
324 nlen
= sprintf (buf
, "%d", uid
);
326 retval
= yperr2nss (yp_match (domain
, "passwd.byuid", buf
,
327 nlen
, &result
, &len
));
329 if (retval
!= NSS_STATUS_SUCCESS
)
331 if (retval
== NSS_STATUS_TRYAGAIN
)
332 __set_errno (EAGAIN
);
336 /* Check for adjunct style secret passwords. They can be recognized
337 by a password starting with "##". */
338 p
= strchr (result
, ':');
339 if (p
!= NULL
/* This better should be true in all cases. */
340 && p
[1] == '#' && p
[2] == '#'
341 && (namelen
= p
- result
,
342 yp_match (domain
, "passwd.adjunct.byname", result
, namelen
,
343 &result2
, &len2
)) == YPERR_SUCCESS
)
345 /* We found a passwd.adjunct entry. Merge encrypted password
346 therein into original result. */
347 char *encrypted
= strchr (result2
, ':');
351 if (encrypted
== NULL
352 || (endp
= strchr (++encrypted
, ':')) == NULL
353 || (p
= strchr (p
+ 1, ':')) == NULL
)
355 /* Invalid format of the entry. This never should happen
356 unless the data from which the NIS table is generated is
357 wrong. We simply ignore it. */
362 restlen
= len
- (p
- result
);
363 if ((size_t) (namelen
+ (endp
- encrypted
) + restlen
+ 2) > buflen
)
367 __set_errno (ERANGE
);
368 return NSS_STATUS_TRYAGAIN
;
371 memcpy (buffer
, result
, namelen
);
372 tmp
= buffer
+ namelen
;
374 memcpy (tmp
, encrypted
, endp
- encrypted
);
375 tmp
+= endp
- encrypted
;
376 memcpy (tmp
, p
, restlen
+ 1);
384 if ((size_t) (len
+ 1) > buflen
)
387 __set_errno (ERANGE
);
388 return NSS_STATUS_TRYAGAIN
;
391 p
= strncpy (buffer
, result
, len
);
399 parse_res
= _nss_files_parse_pwent (p
, pwd
, data
, buflen
);
401 if (parse_res
== -1 && errno
== ERANGE
)
402 return NSS_STATUS_TRYAGAIN
;
403 else if (parse_res
== 0)
404 return NSS_STATUS_NOTFOUND
;
406 return NSS_STATUS_SUCCESS
;