Silence a missing-noreturn warning for _Unwind_Resume.
[glibc.git] / nis / nss_nis / nis-spwd.c
blob7cfc3ce61701d02955bd71d4dbff8762c6f87821
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/>. */
19 #include <nss.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <string.h>
23 /* The following is an ugly trick to avoid a prototype declaration for
24 _nss_nis_endspent. */
25 #define _nss_nis_endspent _nss_nis_endspent_XXX
26 #include <shadow.h>
27 #undef _nss_nis_endspent
28 #include <bits/libc-lock.h>
29 #include <rpcsvc/yp.h>
30 #include <rpcsvc/ypclnt.h>
32 #include "nss-nis.h"
33 #include <libnsl.h>
35 /* Get the declaration of the parser function. */
36 #define ENTNAME spent
37 #define STRUCTURE spwd
38 #define EXTERN_PARSER
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;
46 static char *oldkey;
47 static int oldkeylen;
49 enum nss_status
50 _nss_nis_setspent (int stayopen)
52 __libc_lock_lock (lock);
54 new_start = true;
55 ent_adjunct_used = false;
56 free (oldkey);
57 oldkey = NULL;
58 oldkeylen = 0;
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,
71 int *errnop)
73 char *domain;
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. */
78 int parse_res;
81 char *result;
82 char *outkey;
83 int len;
84 int keylen;
85 int yperr;
87 if (new_start)
89 yperr = yp_first (domain, "shadow.byname", &outkey, &keylen, &result,
90 &len);
91 if (__builtin_expect (yperr == YPERR_MAP, 0)
92 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW))
94 free (result);
95 yperr = yp_first (domain, "passwd.adjunct.byname", &outkey,
96 &keylen, &result, &len);
97 ent_adjunct_used = true;
100 else
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)
110 *errnop = errno;
111 return retval;
114 if (__builtin_expect ((size_t) (len + (ent_adjunct_used ? 3 : 1))
115 > buflen, 0))
117 free (result);
118 *errnop = ERANGE;
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], "::");
128 else
129 buffer[len] = '\0';
130 while (isspace (*p))
131 ++p;
132 free (result);
134 parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
135 errnop);
136 if (__builtin_expect (parse_res == -1, 0))
138 free (outkey);
139 *errnop = ERANGE;
140 return NSS_STATUS_TRYAGAIN;
143 free (oldkey);
144 oldkey = outkey;
145 oldkeylen = keylen;
146 new_start = false;
148 while (!parse_res);
150 return NSS_STATUS_SUCCESS;
153 enum nss_status
154 _nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen,
155 int *errnop)
157 int status;
159 __libc_lock_lock (lock);
161 status = internal_nis_getspent_r (result, buffer, buflen, errnop);
163 __libc_lock_unlock (lock);
165 return status;
168 enum nss_status
169 _nss_nis_getspnam_r (const char *name, struct spwd *sp,
170 char *buffer, size_t buflen, int *errnop)
172 if (name == NULL)
174 *errnop = EINVAL;
175 return NSS_STATUS_UNAVAIL;
177 const size_t name_len = strlen (name);
179 char *domain;
180 if (__glibc_unlikely (yp_get_default_domain (&domain)))
181 return NSS_STATUS_UNAVAIL;
183 bool adjunct_used = false;
184 char *result;
185 int len;
186 int yperr = yp_match (domain, "shadow.byname", name, name_len, &result,
187 &len);
188 if (__builtin_expect (yperr == YPERR_MAP, 0)
189 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW))
191 free (result);
192 yperr = yp_match (domain, "passwd.adjunct.byname", name, name_len,
193 &result, &len);
194 adjunct_used = true;
197 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
199 enum nss_status retval = yperr2nss (yperr);
201 if (retval == NSS_STATUS_TRYAGAIN)
202 *errnop = errno;
203 return retval;
206 if (__glibc_unlikely ((size_t) (len + (adjunct_used ? 3 : 1)) > buflen))
208 free (result);
209 *errnop = ERANGE;
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], "::");
219 else
220 buffer[len] = '\0';
221 while (isspace (*p))
222 ++p;
223 free (result);
225 int parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
226 errnop);
227 if (__glibc_unlikely (parse_res < 1))
229 if (parse_res == -1)
230 return NSS_STATUS_TRYAGAIN;
231 else
232 return NSS_STATUS_NOTFOUND;
234 return NSS_STATUS_SUCCESS;