[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nis / nss_nisplus / nisplus-spwd.c
blobe63e1eeaec185636b0947e455704e722bdab916e
1 /* Copyright (C) 1997, 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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 <errno.h>
22 #include <shadow.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
27 #include "nss-nisplus.h"
28 #include "nisplus-parser.h"
30 __libc_lock_define_initialized (static, lock)
32 static nis_result *result;
34 /* Defined in nisplus-pwd.c. */
35 extern nis_name pwd_tablename_val attribute_hidden;
36 extern size_t pwd_tablename_len attribute_hidden;
37 extern enum nss_status _nss_pwd_create_tablename (int *errnop);
40 enum nss_status
41 _nss_nisplus_setspent (int stayopen)
43 enum nss_status status = NSS_STATUS_SUCCESS;
44 int err;
46 __libc_lock_lock (lock);
48 if (result != NULL)
50 nis_freeresult (result);
51 result = NULL;
54 if (pwd_tablename_val == NULL)
55 status = _nss_pwd_create_tablename (&err);
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
62 enum nss_status
63 _nss_nisplus_endspent (void)
65 __libc_lock_lock (lock);
67 if (result != NULL)
69 nis_freeresult (result);
70 result = NULL;
73 __libc_lock_unlock (lock);
75 return NSS_STATUS_SUCCESS;
78 static enum nss_status
79 internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
80 int *errnop)
82 int parse_res;
84 /* Get the next entry until we found a correct one. */
87 nis_result *saved_res;
89 if (result == NULL)
91 saved_res = NULL;
93 if (pwd_tablename_val == NULL)
95 enum nss_status status = _nss_pwd_create_tablename (errnop);
97 if (status != NSS_STATUS_SUCCESS)
98 return status;
101 result = nis_first_entry (pwd_tablename_val);
102 if (result == NULL)
104 *errnop = errno;
105 return NSS_STATUS_TRYAGAIN;
107 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
108 return niserr2nss (result->status);
110 else
112 saved_res = result;
113 result = nis_next_entry (pwd_tablename_val, &result->cookie);
114 if (result == NULL)
116 *errnop = errno;
117 return NSS_STATUS_TRYAGAIN;
119 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
121 nis_freeresult (saved_res);
122 return niserr2nss (result->status);
126 parse_res = _nss_nisplus_parse_spent (result, sp, buffer,
127 buflen, errnop);
128 if (__builtin_expect (parse_res == -1, 0))
130 nis_freeresult (result);
131 result = saved_res;
132 *errnop = ERANGE;
133 return NSS_STATUS_TRYAGAIN;
136 if (saved_res != NULL)
137 nis_freeresult (saved_res);
139 while (!parse_res);
141 return NSS_STATUS_SUCCESS;
144 enum nss_status
145 _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen,
146 int *errnop)
148 int status;
150 __libc_lock_lock (lock);
152 status = internal_nisplus_getspent_r (result, buffer, buflen, errnop);
154 __libc_lock_unlock (lock);
156 return status;
159 enum nss_status
160 _nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
161 char *buffer, size_t buflen, int *errnop)
163 int parse_res;
165 if (pwd_tablename_val == NULL)
167 enum nss_status status = _nss_pwd_create_tablename (errnop);
169 if (status != NSS_STATUS_SUCCESS)
170 return status;
173 if (name == NULL)
175 *errnop = EINVAL;
176 return NSS_STATUS_NOTFOUND;
179 nis_result *result;
180 char buf[strlen (name) + 9 + pwd_tablename_len];
181 int olderr = errno;
183 snprintf (buf, sizeof (buf), "[name=%s],%s", name, pwd_tablename_val);
185 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
187 if (result == NULL)
189 *errnop = ENOMEM;
190 return NSS_STATUS_TRYAGAIN;
193 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
195 enum nss_status status = niserr2nss (result->status);
197 __set_errno (olderr);
199 nis_freeresult (result);
200 return status;
203 parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen, errnop);
204 nis_freeresult (result);
206 if (__builtin_expect (parse_res < 1, 0))
208 if (parse_res == -1)
210 *errnop = ERANGE;
211 return NSS_STATUS_TRYAGAIN;
213 else
215 __set_errno (olderr);
216 return NSS_STATUS_NOTFOUND;
220 return NSS_STATUS_SUCCESS;