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. */
25 #include <libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
31 /* Get the declaration of the parser function. */
33 #define STRUCTURE passwd
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;
45 _nss_nis_setpwent (void)
47 __libc_lock_lock (lock);
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
63 _nss_nis_endpwent (void)
65 __libc_lock_lock (lock);
75 __libc_lock_unlock (lock);
77 return NSS_STATUS_SUCCESS;
80 static enum nss_status
81 internal_nis_getpwent_r (struct passwd *pwd, char *buffer, size_t buflen)
83 struct parser_data *data = (void *) buffer;
84 char *domain, *result, *outkey;
85 int len, keylen, parse_res;
87 if (yp_get_default_domain (&domain))
88 return NSS_STATUS_UNAVAIL;
90 /* Get the next entry until we found a correct one. */
93 enum nss_status retval;
97 retval = yperr2nss (yp_first (domain, "passwd.byname",
98 &outkey, &keylen, &result, &len));
100 retval = yperr2nss ( yp_next (domain, "passwd.byname",
102 &outkey, &keylen, &result, &len));
104 if (retval != NSS_STATUS_SUCCESS)
106 if (retval == NSS_STATUS_TRYAGAIN)
107 __set_errno (EAGAIN);
111 if ((size_t) (len + 1) > buflen)
114 __set_errno (ERANGE);
115 return NSS_STATUS_TRYAGAIN;
118 p = strncpy (buffer, result, len);
124 parse_res = _nss_files_parse_pwent (p, pwd, data, buflen);
125 if (!parse_res && errno == ERANGE)
126 return NSS_STATUS_TRYAGAIN;
135 return NSS_STATUS_SUCCESS;
139 _nss_nis_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
143 __libc_lock_lock (lock);
145 status = internal_nis_getpwent_r (result, buffer, buflen);
147 __libc_lock_unlock (lock);
153 _nss_nis_getpwnam_r (const char *name, struct passwd *pwd,
154 char *buffer, size_t buflen)
156 struct parser_data *data = (void *) buffer;
157 enum nss_status retval;
158 char *domain, *result, *p;
163 __set_errno (EINVAL);
164 return NSS_STATUS_UNAVAIL;
167 if (yp_get_default_domain (&domain))
168 return NSS_STATUS_UNAVAIL;
170 retval = yperr2nss (yp_match (domain, "passwd.byname", name,
171 strlen (name), &result, &len));
173 if (retval != NSS_STATUS_SUCCESS)
175 if (retval == NSS_STATUS_TRYAGAIN)
176 __set_errno (EAGAIN);
180 if ((size_t) (len + 1) > buflen)
183 __set_errno (ERANGE);
184 return NSS_STATUS_TRYAGAIN;
187 p = strncpy (buffer, result, len);
193 parse_res = _nss_files_parse_pwent (p, pwd, data, buflen);
198 return NSS_STATUS_TRYAGAIN;
200 return NSS_STATUS_NOTFOUND;
203 return NSS_STATUS_SUCCESS;
207 _nss_nis_getpwuid_r (uid_t uid, struct passwd *pwd,
208 char *buffer, size_t buflen)
210 struct parser_data *data = (void *) buffer;
211 enum nss_status retval;
212 char *domain, *result, *p;
213 int len, nlen, parse_res;
216 if (yp_get_default_domain (&domain))
217 return NSS_STATUS_UNAVAIL;
219 nlen = sprintf (buf, "%d", uid);
221 retval = yperr2nss (yp_match (domain, "passwd.byuid", buf,
222 nlen, &result, &len));
224 if (retval != NSS_STATUS_SUCCESS)
226 if (retval == NSS_STATUS_TRYAGAIN)
227 __set_errno (EAGAIN);
231 if ((size_t) (len + 1) > buflen)
234 __set_errno (ERANGE);
235 return NSS_STATUS_TRYAGAIN;
238 p = strncpy (buffer, result, len);
244 parse_res = _nss_files_parse_pwent (p, pwd, data, buflen);
249 return NSS_STATUS_TRYAGAIN;
251 return NSS_STATUS_NOTFOUND;
254 return NSS_STATUS_SUCCESS;