Update copyright dates with scripts/update-copyrights.
[glibc.git] / nis / nss_nisplus / nisplus-spwd.c
blob91e539b5c0a370b7decd77dc71adcbc8e5f87403
1 /* Copyright (C) 1997-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <nss.h>
20 #include <errno.h>
21 #include <shadow.h>
22 #include <string.h>
23 #include <bits/libc-lock.h>
24 #include <rpcsvc/nis.h>
26 #include "nss-nisplus.h"
27 #include "nisplus-parser.h"
29 __libc_lock_define_initialized (static, lock)
31 static nis_result *result;
33 /* Defined in nisplus-pwd.c. */
34 extern nis_name pwd_tablename_val attribute_hidden;
35 extern size_t pwd_tablename_len attribute_hidden;
36 extern enum nss_status _nss_pwd_create_tablename (int *errnop);
39 enum nss_status
40 _nss_nisplus_setspent (int stayopen)
42 enum nss_status status = NSS_STATUS_SUCCESS;
43 int err;
45 __libc_lock_lock (lock);
47 if (result != NULL)
49 nis_freeresult (result);
50 result = NULL;
53 if (pwd_tablename_val == NULL)
54 status = _nss_pwd_create_tablename (&err);
56 __libc_lock_unlock (lock);
58 return status;
61 enum nss_status
62 _nss_nisplus_endspent (void)
64 __libc_lock_lock (lock);
66 if (result != NULL)
68 nis_freeresult (result);
69 result = NULL;
72 __libc_lock_unlock (lock);
74 return NSS_STATUS_SUCCESS;
77 static enum nss_status
78 internal_nisplus_getspent_r (struct spwd *sp, char *buffer, size_t buflen,
79 int *errnop)
81 int parse_res;
83 /* Get the next entry until we found a correct one. */
86 nis_result *saved_res;
88 if (result == NULL)
90 saved_res = NULL;
92 if (pwd_tablename_val == NULL)
94 enum nss_status status = _nss_pwd_create_tablename (errnop);
96 if (status != NSS_STATUS_SUCCESS)
97 return status;
100 result = nis_first_entry (pwd_tablename_val);
101 if (result == NULL)
103 *errnop = errno;
104 return NSS_STATUS_TRYAGAIN;
106 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
107 return niserr2nss (result->status);
109 else
111 saved_res = result;
112 result = nis_next_entry (pwd_tablename_val, &result->cookie);
113 if (result == NULL)
115 *errnop = errno;
116 return NSS_STATUS_TRYAGAIN;
118 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
120 nis_freeresult (saved_res);
121 return niserr2nss (result->status);
125 parse_res = _nss_nisplus_parse_spent (result, sp, buffer,
126 buflen, errnop);
127 if (__glibc_unlikely (parse_res == -1))
129 nis_freeresult (result);
130 result = saved_res;
131 *errnop = ERANGE;
132 return NSS_STATUS_TRYAGAIN;
135 if (saved_res != NULL)
136 nis_freeresult (saved_res);
138 while (!parse_res);
140 return NSS_STATUS_SUCCESS;
143 enum nss_status
144 _nss_nisplus_getspent_r (struct spwd *result, char *buffer, size_t buflen,
145 int *errnop)
147 int status;
149 __libc_lock_lock (lock);
151 status = internal_nisplus_getspent_r (result, buffer, buflen, errnop);
153 __libc_lock_unlock (lock);
155 return status;
158 enum nss_status
159 _nss_nisplus_getspnam_r (const char *name, struct spwd *sp,
160 char *buffer, size_t buflen, int *errnop)
162 int parse_res;
164 if (pwd_tablename_val == NULL)
166 enum nss_status status = _nss_pwd_create_tablename (errnop);
168 if (status != NSS_STATUS_SUCCESS)
169 return status;
172 if (name == NULL)
174 *errnop = EINVAL;
175 return NSS_STATUS_NOTFOUND;
178 nis_result *result;
179 char buf[strlen (name) + 9 + pwd_tablename_len];
180 int olderr = errno;
182 snprintf (buf, sizeof (buf), "[name=%s],%s", name, pwd_tablename_val);
184 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM, NULL, NULL);
186 if (result == NULL)
188 *errnop = ENOMEM;
189 return NSS_STATUS_TRYAGAIN;
192 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
194 enum nss_status status = niserr2nss (result->status);
196 __set_errno (olderr);
198 nis_freeresult (result);
199 return status;
202 parse_res = _nss_nisplus_parse_spent (result, sp, buffer, buflen, errnop);
203 nis_freeresult (result);
205 if (__glibc_unlikely (parse_res < 1))
207 if (parse_res == -1)
209 *errnop = ERANGE;
210 return NSS_STATUS_TRYAGAIN;
212 else
214 __set_errno (olderr);
215 return NSS_STATUS_NOTFOUND;
219 return NSS_STATUS_SUCCESS;