(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nis / nss_nis / nis-spwd.c
blob99a9ed5f48baa121d2ecfb5e74c877004d3c3b3c
1 /* Copyright (C) 1996-1998, 2001, 2002, 2003 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 struct parser_data *data = (void *) buffer;
72 char *domain, *result, *outkey;
73 int len, keylen, parse_res;
75 if (yp_get_default_domain (&domain))
76 return NSS_STATUS_UNAVAIL;
78 /* Get the next entry until we found a correct one. */
81 enum nss_status retval;
82 char *p;
84 if (new_start)
85 retval = yperr2nss (yp_first (domain, "shadow.byname",
86 &outkey, &keylen, &result, &len));
87 else
88 retval = yperr2nss ( yp_next (domain, "shadow.byname",
89 oldkey, oldkeylen,
90 &outkey, &keylen, &result, &len));
92 if (retval != NSS_STATUS_SUCCESS)
94 if (retval == NSS_STATUS_TRYAGAIN)
95 *errnop = errno;
96 return retval;
99 if ((size_t) (len + 1) > buflen)
101 free (result);
102 *errnop = ERANGE;
103 return NSS_STATUS_TRYAGAIN;
106 p = strncpy (buffer, result, len);
107 buffer[len] = '\0';
108 while (isspace (*p))
109 ++p;
110 free (result);
112 parse_res = _nss_files_parse_spent (p, sp, data, buflen, errnop);
113 if (parse_res == -1)
115 free (outkey);
116 *errnop = ERANGE;
117 return NSS_STATUS_TRYAGAIN;
120 free (oldkey);
121 oldkey = outkey;
122 oldkeylen = keylen;
123 new_start = 0;
125 while (!parse_res);
127 return NSS_STATUS_SUCCESS;
130 enum nss_status
131 _nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen,
132 int *errnop)
134 int status;
136 __libc_lock_lock (lock);
138 status = internal_nis_getspent_r (result, buffer, buflen, errnop);
140 __libc_lock_unlock (lock);
142 return status;
145 enum nss_status
146 _nss_nis_getspnam_r (const char *name, struct spwd *sp,
147 char *buffer, size_t buflen, int *errnop)
149 struct parser_data *data = (void *) buffer;
150 enum nss_status retval;
151 char *domain, *result, *p;
152 int len, parse_res;
154 if (name == NULL)
156 *errnop = EINVAL;
157 return NSS_STATUS_UNAVAIL;
160 if (yp_get_default_domain (&domain))
161 return NSS_STATUS_UNAVAIL;
163 retval = yperr2nss (yp_match (domain, "shadow.byname", name,
164 strlen (name), &result, &len));
166 if (retval != NSS_STATUS_SUCCESS)
168 if (retval == NSS_STATUS_TRYAGAIN)
169 *errnop = errno;
170 return retval;
173 if ((size_t) (len + 1) > buflen)
175 free (result);
176 *errnop = ERANGE;
177 return NSS_STATUS_TRYAGAIN;
180 p = strncpy (buffer, result, len);
181 buffer[len] = '\0';
182 while (isspace (*p))
183 ++p;
184 free (result);
186 parse_res = _nss_files_parse_spent (p, sp, data, buflen, errnop);
187 if (parse_res < 1)
189 if (parse_res == -1)
190 return NSS_STATUS_TRYAGAIN;
191 else
192 return NSS_STATUS_NOTFOUND;
194 return NSS_STATUS_SUCCESS;