1 /* Copyright (C) 1996-1998,2001-2003,2006,2010 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
24 /* The following is an ugly trick to avoid a prototype declaration for
26 #define _nss_nis_endspent _nss_nis_endspent_XXX
28 #undef _nss_nis_endspent
29 #include <bits/libc-lock.h>
30 #include <rpcsvc/yp.h>
31 #include <rpcsvc/ypclnt.h>
36 /* Get the declaration of the parser function. */
38 #define STRUCTURE spwd
40 #include <nss/nss_files/files-parse.c>
42 /* Protect global state against multiple changers */
43 __libc_lock_define_initialized (static, lock
)
45 static bool new_start
= true;
46 static bool ent_adjunct_used
;
51 _nss_nis_setspent (int stayopen
)
53 __libc_lock_lock (lock
);
56 ent_adjunct_used
= false;
61 __libc_lock_unlock (lock
);
63 return NSS_STATUS_SUCCESS
;
65 /* Make _nss_nis_endspent an alias of _nss_nis_setspent. We do this
66 even though the prototypes don't match. The argument of setspent
67 is not used so this makes no difference. */
68 strong_alias (_nss_nis_setspent
, _nss_nis_endspent
)
70 static enum nss_status
71 internal_nis_getspent_r (struct spwd
*sp
, char *buffer
, size_t buflen
,
75 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
76 return NSS_STATUS_UNAVAIL
;
78 /* Get the next entry until we found a correct one. */
90 yperr
= yp_first (domain
, "shadow.byname", &outkey
, &keylen
, &result
,
92 if (__builtin_expect (yperr
== YPERR_MAP
, 0)
93 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
))
96 yperr
= yp_first (domain
, "passwd.adjunct.byname", &outkey
,
97 &keylen
, &result
, &len
);
98 ent_adjunct_used
= true;
102 yperr
= yp_next (domain
, (ent_adjunct_used
103 ? "passwd.adjunct.byname" : "shadow.byname"),
104 oldkey
, oldkeylen
, &outkey
, &keylen
, &result
, &len
);
106 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
108 enum nss_status retval
= yperr2nss (yperr
);
110 if (retval
== NSS_STATUS_TRYAGAIN
)
115 if (__builtin_expect ((size_t) (len
+ (ent_adjunct_used
? 3 : 1))
120 return NSS_STATUS_TRYAGAIN
;
123 char *p
= strncpy (buffer
, result
, len
);
124 if (ent_adjunct_used
)
125 /* This is an ugly trick. The format of passwd.adjunct.byname almost
126 matches the shadow.byname format except that the last two fields
127 are missing. Synthesize them by marking them empty. */
128 strcpy (&buffer
[len
], "::");
135 parse_res
= _nss_files_parse_spent (p
, sp
, (void *) buffer
, buflen
,
137 if (__builtin_expect (parse_res
== -1, 0))
141 return NSS_STATUS_TRYAGAIN
;
151 return NSS_STATUS_SUCCESS
;
155 _nss_nis_getspent_r (struct spwd
*result
, char *buffer
, size_t buflen
,
160 __libc_lock_lock (lock
);
162 status
= internal_nis_getspent_r (result
, buffer
, buflen
, errnop
);
164 __libc_lock_unlock (lock
);
170 _nss_nis_getspnam_r (const char *name
, struct spwd
*sp
,
171 char *buffer
, size_t buflen
, int *errnop
)
176 return NSS_STATUS_UNAVAIL
;
178 const size_t name_len
= strlen (name
);
181 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
182 return NSS_STATUS_UNAVAIL
;
184 bool adjunct_used
= false;
187 int yperr
= yp_match (domain
, "shadow.byname", name
, name_len
, &result
,
189 if (__builtin_expect (yperr
== YPERR_MAP
, 0)
190 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
))
193 yperr
= yp_match (domain
, "passwd.adjunct.byname", name
, name_len
,
198 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
200 enum nss_status retval
= yperr2nss (yperr
);
202 if (retval
== NSS_STATUS_TRYAGAIN
)
207 if (__builtin_expect ((size_t) (len
+ (adjunct_used
? 3 : 1)) > buflen
, 0))
211 return NSS_STATUS_TRYAGAIN
;
214 char *p
= strncpy (buffer
, result
, len
);
215 if (__builtin_expect (adjunct_used
, false))
216 /* This is an ugly trick. The format of passwd.adjunct.byname almost
217 matches the shadow.byname format except that the last two fields
218 are missing. Synthesize them by marking them empty. */
219 strcpy (&buffer
[len
], "::");
226 int parse_res
= _nss_files_parse_spent (p
, sp
, (void *) buffer
, buflen
,
228 if (__builtin_expect (parse_res
< 1, 0))
231 return NSS_STATUS_TRYAGAIN
;
233 return NSS_STATUS_NOTFOUND
;
235 return NSS_STATUS_SUCCESS
;