Remove duplicate entries.
[glibc/history.git] / nis / nss_nisplus / nisplus-spwd.c
blobf256f3eb907152762f23dc81521e26c5cd2382ac
1 /* Copyright (C) 1997, 2001, 2002, 2003, 2005, 2007
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <nss.h>
22 #include <errno.h>
23 #include <shadow.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
29 #include "nisplus-parser.h"
31 __libc_lock_define_initialized (static, lock)
33 static nis_result *result;
35 /* Defined in nisplus-pwd.c. */
36 extern nis_name pwd_tablename_val attribute_hidden;
37 extern size_t pwd_tablename_len attribute_hidden;
38 extern enum nss_status _nss_pwd_create_tablename (int *errnop);
41 enum nss_status
42 _nss_nisplus_setspent (int stayopen)
44 enum nss_status status = NSS_STATUS_SUCCESS;
45 int err;
47 __libc_lock_lock (lock);
49 if (result != NULL)
51 nis_freeresult (result);
52 result = NULL;
55 if (pwd_tablename_val == NULL)
56 status = _nss_pwd_create_tablename (&err);
58 __libc_lock_unlock (lock);
60 return NSS_STATUS_SUCCESS;
63 enum nss_status
64 _nss_nisplus_endspent (void)
66 __libc_lock_lock (lock);
68 if (result != NULL)
70 nis_freeresult (result);
71 result = NULL;
74 __libc_lock_unlock (lock);
76 return NSS_STATUS_SUCCESS;
79 static enum nss_status
80 internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
81 int *errnop)
83 int parse_res;
85 /* Get the next entry until we found a correct one. */
88 nis_result *saved_res;
90 if (result == NULL)
92 saved_res = NULL;
94 if (pwd_tablename_val == NULL)
96 enum nss_status status = _nss_pwd_create_tablename (errnop);
98 if (status != NSS_STATUS_SUCCESS)
99 return status;
102 result = nis_first_entry (pwd_tablename_val);
103 if (result == NULL)
105 *errnop = errno;
106 return NSS_STATUS_TRYAGAIN;
108 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
109 return niserr2nss (result->status);
111 else
113 saved_res = result;
114 result = nis_next_entry (pwd_tablename_val, &result->cookie);
115 if (result == NULL)
117 *errnop = errno;
118 return NSS_STATUS_TRYAGAIN;
120 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
122 nis_freeresult (saved_res);
123 return niserr2nss (result->status);
127 parse_res = _nss_nisplus_parse_spent (result, sp, buffer,
128 buflen, errnop);
129 if (__builtin_expect (parse_res == -1, 0))
131 nis_freeresult (result);
132 result = saved_res;
133 *errnop = ERANGE;
134 return NSS_STATUS_TRYAGAIN;
137 if (saved_res != NULL)
138 nis_freeresult (saved_res);
140 while (!parse_res);
142 return NSS_STATUS_SUCCESS;
145 enum nss_status
146 _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen,
147 int *errnop)
149 int status;
151 __libc_lock_lock (lock);
153 status = internal_nisplus_getspent_r (result, buffer, buflen, errnop);
155 __libc_lock_unlock (lock);
157 return status;
160 enum nss_status
161 _nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
162 char *buffer, size_t buflen, int *errnop)
164 int parse_res;
166 if (pwd_tablename_val == NULL)
168 enum nss_status status = _nss_pwd_create_tablename (errnop);
170 if (status != NSS_STATUS_SUCCESS)
171 return status;
174 if (name == NULL)
176 *errnop = EINVAL;
177 return NSS_STATUS_NOTFOUND;
180 nis_result *result;
181 char buf[strlen (name) + 9 + pwd_tablename_len];
182 int olderr = errno;
184 snprintf (buf, sizeof (buf), "[name=%s],%s", name, pwd_tablename_val);
186 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL);
188 if (result == NULL)
190 *errnop = ENOMEM;
191 return NSS_STATUS_TRYAGAIN;
194 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
196 enum nss_status status = niserr2nss (result->status);
198 __set_errno (olderr);
200 nis_freeresult (result);
201 return status;
204 parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen, errnop);
205 nis_freeresult (result);
207 if (__builtin_expect (parse_res < 1, 0))
209 if (parse_res == -1)
211 *errnop = ERANGE;
212 return NSS_STATUS_TRYAGAIN;
214 else
216 __set_errno (olderr);
217 return NSS_STATUS_NOTFOUND;
221 return NSS_STATUS_SUCCESS;