Support Intel processor model 6 and model 0x2.
[glibc.git] / nis / nss_nis / nis-spwd.c
blob3cf913b4cd38ee3ca9dca592575806061eb822c4
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
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"
34 #include <libnsl.h>
36 /* Get the declaration of the parser function. */
37 #define ENTNAME spent
38 #define STRUCTURE spwd
39 #define EXTERN_PARSER
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;
47 static char *oldkey;
48 static int oldkeylen;
50 enum nss_status
51 _nss_nis_setspent (int stayopen)
53 __libc_lock_lock (lock);
55 new_start = true;
56 ent_adjunct_used = false;
57 free (oldkey);
58 oldkey = NULL;
59 oldkeylen = 0;
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,
72 int *errnop)
74 char *domain;
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. */
79 int parse_res;
82 char *result;
83 char *outkey;
84 int len;
85 int keylen;
86 int yperr;
88 if (new_start)
90 yperr = yp_first (domain, "shadow.byname", &outkey, &keylen, &result,
91 &len);
92 if (__builtin_expect (yperr == YPERR_MAP, 0)
93 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW))
95 free (result);
96 yperr = yp_first (domain, "passwd.adjunct.byname", &outkey,
97 &keylen, &result, &len);
98 ent_adjunct_used = true;
101 else
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)
111 *errnop = errno;
112 return retval;
115 if (__builtin_expect ((size_t) (len + (ent_adjunct_used ? 3 : 1))
116 > buflen, 0))
118 free (result);
119 *errnop = ERANGE;
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], "::");
129 else
130 buffer[len] = '\0';
131 while (isspace (*p))
132 ++p;
133 free (result);
135 parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
136 errnop);
137 if (__builtin_expect (parse_res == -1, 0))
139 free (outkey);
140 *errnop = ERANGE;
141 return NSS_STATUS_TRYAGAIN;
144 free (oldkey);
145 oldkey = outkey;
146 oldkeylen = keylen;
147 new_start = false;
149 while (!parse_res);
151 return NSS_STATUS_SUCCESS;
154 enum nss_status
155 _nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen,
156 int *errnop)
158 int status;
160 __libc_lock_lock (lock);
162 status = internal_nis_getspent_r (result, buffer, buflen, errnop);
164 __libc_lock_unlock (lock);
166 return status;
169 enum nss_status
170 _nss_nis_getspnam_r (const char *name, struct spwd *sp,
171 char *buffer, size_t buflen, int *errnop)
173 if (name == NULL)
175 *errnop = EINVAL;
176 return NSS_STATUS_UNAVAIL;
178 const size_t name_len = strlen (name);
180 char *domain;
181 if (__builtin_expect (yp_get_default_domain (&domain), 0))
182 return NSS_STATUS_UNAVAIL;
184 bool adjunct_used = false;
185 char *result;
186 int len;
187 int yperr = yp_match (domain, "shadow.byname", name, name_len, &result,
188 &len);
189 if (__builtin_expect (yperr == YPERR_MAP, 0)
190 && (_nsl_default_nss () & NSS_FLAG_ADJUNCT_AS_SHADOW))
192 free (result);
193 yperr = yp_match (domain, "passwd.adjunct.byname", name, name_len,
194 &result, &len);
195 adjunct_used = true;
198 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
200 enum nss_status retval = yperr2nss (yperr);
202 if (retval == NSS_STATUS_TRYAGAIN)
203 *errnop = errno;
204 return retval;
207 if (__builtin_expect ((size_t) (len + (adjunct_used ? 3 : 1)) > buflen, 0))
209 free (result);
210 *errnop = ERANGE;
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], "::");
220 else
221 buffer[len] = '\0';
222 while (isspace (*p))
223 ++p;
224 free (result);
226 int parse_res = _nss_files_parse_spent (p, sp, (void *) buffer, buflen,
227 errnop);
228 if (__builtin_expect (parse_res < 1, 0))
230 if (parse_res == -1)
231 return NSS_STATUS_TRYAGAIN;
232 else
233 return NSS_STATUS_NOTFOUND;
235 return NSS_STATUS_SUCCESS;