2.9
[glibc/nacl-glibc.git] / nis / nss_nis / nis-spwd.c
blob0fc4e17c4234f7dc6901d6e86c78c7c1d56603a6
1 /* Copyright (C) 1996-1998,2001,2002,2003,2006 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
18 02111-1307 USA. */
20 #include <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 /* The following is an ugly trick to avoid a prototype declaration for
25 _nss_nis_endspent. */
26 #define _nss_nis_endspent _nss_nis_endspent_XXX
27 #include <shadow.h>
28 #undef _nss_nis_endspent
29 #include <bits/libc-lock.h>
30 #include <rpcsvc/yp.h>
31 #include <rpcsvc/ypclnt.h>
33 #include "nss-nis.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_t new_start = 1;
45 static char *oldkey;
46 static int oldkeylen;
48 enum nss_status
49 _nss_nis_setspent (int stayopen)
51 __libc_lock_lock (lock);
53 new_start = 1;
54 free (oldkey);
55 oldkey = NULL;
56 oldkeylen = 0;
58 __libc_lock_unlock (lock);
60 return NSS_STATUS_SUCCESS;
62 /* Make _nss_nis_endspent an alias of _nss_nis_setspent. We do this
63 even though the prototypes don't match. The argument of setspent
64 is not used so this makes no difference. */
65 strong_alias (_nss_nis_setspent, _nss_nis_endspent)
67 static enum nss_status
68 internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
69 int *errnop)
71 char *domain;
72 if (__builtin_expect (yp_get_default_domain (&domain), 0))
73 return NSS_STATUS_UNAVAIL;
75 /* Get the next entry until we found a correct one. */
76 int parse_res;
79 char *result;
80 char *outkey;
81 int len;
82 int keylen;
83 int yperr;
85 if (new_start)
86 yperr = yp_first (domain, "shadow.byname", &outkey, &keylen, &result,
87 &len);
88 else
89 yperr = yp_next (domain, "shadow.byname", oldkey, oldkeylen, &outkey,
90 &keylen, &result, &len);
92 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
94 enum nss_status retval = yperr2nss (yperr);
96 if (retval == NSS_STATUS_TRYAGAIN)
97 *errnop = errno;
98 return retval;
101 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
103 free (result);
104 *errnop = ERANGE;
105 return NSS_STATUS_TRYAGAIN;
108 char *p = strncpy (buffer, result, len);
109 buffer[len] = '\0';
110 while (isspace (*p))
111 ++p;
112 free (result);
114 parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
115 errnop);
116 if (__builtin_expect (parse_res == -1, 0))
118 free (outkey);
119 *errnop = ERANGE;
120 return NSS_STATUS_TRYAGAIN;
123 free (oldkey);
124 oldkey = outkey;
125 oldkeylen = keylen;
126 new_start = 0;
128 while (!parse_res);
130 return NSS_STATUS_SUCCESS;
133 enum nss_status
134 _nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen,
135 int *errnop)
137 int status;
139 __libc_lock_lock (lock);
141 status = internal_nis_getspent_r (result, buffer, buflen, errnop);
143 __libc_lock_unlock (lock);
145 return status;
148 enum nss_status
149 _nss_nis_getspnam_r (const char *name, struct spwd *sp,
150 char *buffer, size_t buflen, int *errnop)
152 if (name == NULL)
154 *errnop = EINVAL;
155 return NSS_STATUS_UNAVAIL;
158 char *domain;
159 if (__builtin_expect (yp_get_default_domain (&domain), 0))
160 return NSS_STATUS_UNAVAIL;
162 char *result;
163 int len;
164 int yperr = yp_match (domain, "shadow.byname", name, strlen (name), &result,
165 &len);
167 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
169 enum nss_status retval = yperr2nss (yperr);
171 if (retval == NSS_STATUS_TRYAGAIN)
172 *errnop = errno;
173 return retval;
176 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
178 free (result);
179 *errnop = ERANGE;
180 return NSS_STATUS_TRYAGAIN;
183 char *p = strncpy (buffer, result, len);
184 buffer[len] = '\0';
185 while (isspace (*p))
186 ++p;
187 free (result);
189 int parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
190 errnop);
191 if (__builtin_expect (parse_res < 1, 0))
193 if (parse_res == -1)
194 return NSS_STATUS_TRYAGAIN;
195 else
196 return NSS_STATUS_NOTFOUND;
198 return NSS_STATUS_SUCCESS;