Fix typo.
[glibc/history.git] / nis / nss_nis / nis-spwd.c
blob452f91dc0be25fe384ec6b2e7f1387c01ac05635
1 /* Copyright (C) 1996, 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <shadow.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
31 /* Get the declaration of the parser function. */
32 #define ENTNAME spent
33 #define STRUCTURE spwd
34 #define EXTERN_PARSER
35 #include <nss/nss_files/files-parse.c>
37 /* Protect global state against multiple changers */
38 __libc_lock_define_initialized (static, lock)
40 static bool_t new_start = 1;
41 static char *oldkey = NULL;
42 static int oldkeylen = 0;
44 enum nss_status
45 _nss_nis_setspent (void)
47 __libc_lock_lock (lock);
49 new_start = 1;
50 if (oldkey != NULL)
52 free (oldkey);
53 oldkey = NULL;
54 oldkeylen = 0;
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
62 enum nss_status
63 _nss_nis_endspent (void)
65 __libc_lock_lock (lock);
67 new_start = 1;
68 if (oldkey != NULL)
70 free (oldkey);
71 oldkey = NULL;
72 oldkeylen = 0;
75 __libc_lock_unlock (lock);
77 return NSS_STATUS_SUCCESS;
80 static enum nss_status
81 internal_nis_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
82 int *errnop)
84 struct parser_data *data = (void *) buffer;
85 char *domain, *result, *outkey;
86 int len, keylen, parse_res;
88 if (yp_get_default_domain (&domain))
89 return NSS_STATUS_UNAVAIL;
91 /* Get the next entry until we found a correct one. */
94 enum nss_status retval;
95 char *p;
97 if (new_start)
98 retval = yperr2nss (yp_first (domain, "shadow.byname",
99 &outkey, &keylen, &result, &len));
100 else
101 retval = yperr2nss ( yp_next (domain, "shadow.byname",
102 oldkey, oldkeylen,
103 &outkey, &keylen, &result, &len));
105 if (retval != NSS_STATUS_SUCCESS)
107 if (retval == NSS_STATUS_TRYAGAIN)
108 *errnop = errno;
109 return retval;
112 if ((size_t) (len + 1) > buflen)
114 free (result);
115 *errnop = ERANGE;
116 return NSS_STATUS_TRYAGAIN;
119 p = strncpy (buffer, result, len);
120 buffer[len] = '\0';
121 while (isspace (*p))
122 ++p;
123 free (result);
125 parse_res = _nss_files_parse_spent (p, sp, data, buflen, errnop);
126 if (parse_res == -1)
128 free (outkey);
129 *errnop = ERANGE;
130 return NSS_STATUS_TRYAGAIN;
133 free (oldkey);
134 oldkey = outkey;
135 oldkeylen = keylen;
136 new_start = 0;
138 while (!parse_res);
140 return NSS_STATUS_SUCCESS;
143 enum nss_status
144 _nss_nis_getspent_r (struct spwd *result, char *buffer, size_t buflen,
145 int *errnop)
147 int status;
149 __libc_lock_lock (lock);
151 status = internal_nis_getspent_r (result, buffer, buflen, errnop);
153 __libc_lock_unlock (lock);
155 return status;
158 enum nss_status
159 _nss_nis_getspnam_r (const char *name, struct spwd *sp,
160 char *buffer, size_t buflen, int *errnop)
162 struct parser_data *data = (void *) buffer;
163 enum nss_status retval;
164 char *domain, *result, *p;
165 int len, parse_res;
167 if (name == NULL)
169 *errnop = EINVAL;
170 return NSS_STATUS_UNAVAIL;
173 if (yp_get_default_domain (&domain))
174 return NSS_STATUS_UNAVAIL;
176 retval = yperr2nss (yp_match (domain, "shadow.byname", name,
177 strlen (name), &result, &len));
179 if (retval != NSS_STATUS_SUCCESS)
181 if (retval == NSS_STATUS_TRYAGAIN)
182 *errnop = errno;
183 return retval;
186 if ((size_t) (len + 1) > buflen)
188 free (result);
189 *errnop = ERANGE;
190 return NSS_STATUS_TRYAGAIN;
193 p = strncpy (buffer, result, len);
194 buffer[len] = '\0';
195 while (isspace (*p))
196 ++p;
197 free (result);
199 parse_res = _nss_files_parse_spent (p, sp, data, buflen, errnop);
200 if (parse_res < 1)
202 if (parse_res == -1)
203 return NSS_STATUS_TRYAGAIN;
204 else
205 return NSS_STATUS_NOTFOUND;
207 return NSS_STATUS_SUCCESS;