1 /* Copyright (C) 1996-2014 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, see
17 <http://www.gnu.org/licenses/>. */
23 /* The following is an ugly trick to avoid a prototype declaration for
25 #define _nss_nis_endspent _nss_nis_endspent_XXX
27 #undef _nss_nis_endspent
28 #include <bits/libc-lock.h>
29 #include <rpcsvc/yp.h>
30 #include <rpcsvc/ypclnt.h>
35 /* Get the declaration of the parser function. */
37 #define STRUCTURE spwd
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 new_start
= true;
45 static bool ent_adjunct_used
;
50 _nss_nis_setspent (int stayopen
)
52 __libc_lock_lock (lock
);
55 ent_adjunct_used
= false;
60 __libc_lock_unlock (lock
);
62 return NSS_STATUS_SUCCESS
;
64 /* Make _nss_nis_endspent an alias of _nss_nis_setspent. We do this
65 even though the prototypes don't match. The argument of setspent
66 is not used so this makes no difference. */
67 strong_alias (_nss_nis_setspent
, _nss_nis_endspent
)
69 static enum nss_status
70 internal_nis_getspent_r (struct spwd
*sp
, char *buffer
, size_t buflen
,
74 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
75 return NSS_STATUS_UNAVAIL
;
77 /* Get the next entry until we found a correct one. */
89 yperr
= yp_first (domain
, "shadow.byname", &outkey
, &keylen
, &result
,
91 if (__builtin_expect (yperr
== YPERR_MAP
, 0)
92 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
))
95 yperr
= yp_first (domain
, "passwd.adjunct.byname", &outkey
,
96 &keylen
, &result
, &len
);
97 ent_adjunct_used
= true;
101 yperr
= yp_next (domain
, (ent_adjunct_used
102 ? "passwd.adjunct.byname" : "shadow.byname"),
103 oldkey
, oldkeylen
, &outkey
, &keylen
, &result
, &len
);
105 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
107 enum nss_status retval
= yperr2nss (yperr
);
109 if (retval
== NSS_STATUS_TRYAGAIN
)
114 if (__builtin_expect ((size_t) (len
+ (ent_adjunct_used
? 3 : 1))
119 return NSS_STATUS_TRYAGAIN
;
122 char *p
= strncpy (buffer
, result
, len
);
123 if (ent_adjunct_used
)
124 /* This is an ugly trick. The format of passwd.adjunct.byname almost
125 matches the shadow.byname format except that the last two fields
126 are missing. Synthesize them by marking them empty. */
127 strcpy (&buffer
[len
], "::");
134 parse_res
= _nss_files_parse_spent (p
, sp
, (void *) buffer
, buflen
,
136 if (__builtin_expect (parse_res
== -1, 0))
140 return NSS_STATUS_TRYAGAIN
;
150 return NSS_STATUS_SUCCESS
;
154 _nss_nis_getspent_r (struct spwd
*result
, char *buffer
, size_t buflen
,
159 __libc_lock_lock (lock
);
161 status
= internal_nis_getspent_r (result
, buffer
, buflen
, errnop
);
163 __libc_lock_unlock (lock
);
169 _nss_nis_getspnam_r (const char *name
, struct spwd
*sp
,
170 char *buffer
, size_t buflen
, int *errnop
)
175 return NSS_STATUS_UNAVAIL
;
177 const size_t name_len
= strlen (name
);
180 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
181 return NSS_STATUS_UNAVAIL
;
183 bool adjunct_used
= false;
186 int yperr
= yp_match (domain
, "shadow.byname", name
, name_len
, &result
,
188 if (__builtin_expect (yperr
== YPERR_MAP
, 0)
189 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW
))
192 yperr
= yp_match (domain
, "passwd.adjunct.byname", name
, name_len
,
197 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
199 enum nss_status retval
= yperr2nss (yperr
);
201 if (retval
== NSS_STATUS_TRYAGAIN
)
206 if (__glibc_unlikely ((size_t) (len
+ (adjunct_used
? 3 : 1)) > buflen
))
210 return NSS_STATUS_TRYAGAIN
;
213 char *p
= strncpy (buffer
, result
, len
);
214 if (__builtin_expect (adjunct_used
, false))
215 /* This is an ugly trick. The format of passwd.adjunct.byname almost
216 matches the shadow.byname format except that the last two fields
217 are missing. Synthesize them by marking them empty. */
218 strcpy (&buffer
[len
], "::");
225 int parse_res
= _nss_files_parse_spent (p
, sp
, (void *) buffer
, buflen
,
227 if (__glibc_unlikely (parse_res
< 1))
230 return NSS_STATUS_TRYAGAIN
;
232 return NSS_STATUS_NOTFOUND
;
234 return NSS_STATUS_SUCCESS
;