Update.
[glibc.git] / nis / nss_nisplus / nisplus-pwd.c
blob45c364ac39edc8481ba89b5fdbc9ddf6f35b78a9
1 /* Copyright (C) 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>, 1997.
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 <errno.h>
22 #include <pwd.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
27 #include "nss-nisplus.h"
29 __libc_lock_define_initialized (static, lock)
31 static nis_result *result = NULL;
32 static nis_name tablename_val = NULL;
33 static u_long tablename_len = 0;
35 extern int _nss_nisplus_parse_pwent (nis_result *res, struct passwd *pw,
36 char *buffer, size_t buflen, int *errnop);
38 static enum nss_status
39 _nss_create_tablename (int *errnop)
41 if (tablename_val == NULL)
43 char buf [40 + strlen (nis_local_directory ())];
44 char *p;
46 p = __stpcpy (buf, "passwd.org_dir.");
47 p = __stpcpy (p, nis_local_directory ());
48 tablename_val = __strdup (buf);
49 if (tablename_val == NULL)
51 *errnop = errno;
52 return NSS_STATUS_TRYAGAIN;
54 tablename_len = strlen (tablename_val);
56 return NSS_STATUS_SUCCESS;
60 enum nss_status
61 _nss_nisplus_setpwent (void)
63 enum nss_status status = NSS_STATUS_SUCCESS;
64 int err;
66 __libc_lock_lock (lock);
68 if (result)
69 nis_freeresult (result);
70 result = NULL;
72 if (tablename_val == NULL)
73 status = _nss_create_tablename (&err);
75 __libc_lock_unlock (lock);
77 return status;
80 enum nss_status
81 _nss_nisplus_endpwent (void)
83 __libc_lock_lock (lock);
85 if (result)
86 nis_freeresult (result);
87 result = NULL;
89 __libc_lock_unlock (lock);
91 return NSS_STATUS_SUCCESS;
94 static enum nss_status
95 internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen,
96 int *errnop)
98 int parse_res;
100 /* Get the next entry until we found a correct one. */
103 nis_result *saved_res;
105 if (result == NULL)
107 saved_res = NULL;
108 if (tablename_val == NULL)
110 enum nss_status status = _nss_create_tablename (errnop);
112 if (status != NSS_STATUS_SUCCESS)
113 return status;
116 result = nis_first_entry (tablename_val);
117 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
118 return niserr2nss (result->status);
120 else
122 nis_result *res;
124 saved_res = result;
125 res = nis_next_entry (tablename_val, &result->cookie);
126 result = res;
127 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
129 nis_freeresult (saved_res);
130 return niserr2nss (result->status);
134 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer,
135 buflen, errnop);
136 if (parse_res == -1)
138 nis_freeresult (result);
139 result = saved_res;
140 *errnop = ERANGE;
141 return NSS_STATUS_TRYAGAIN;
143 else
145 if (saved_res)
146 nis_freeresult (saved_res);
148 } while (!parse_res);
150 return NSS_STATUS_SUCCESS;
153 enum nss_status
154 _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
155 int *errnop)
157 int status;
159 __libc_lock_lock (lock);
161 status = internal_nisplus_getpwent_r (result, buffer, buflen, errnop);
163 __libc_lock_unlock (lock);
165 return status;
168 enum nss_status
169 _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
170 char *buffer, size_t buflen, int *errnop)
172 int parse_res;
174 if (tablename_val == NULL)
176 enum nss_status status = _nss_create_tablename (errnop);
178 if (status != NSS_STATUS_SUCCESS)
179 return status;
182 if (name == NULL)
184 *errnop = EINVAL;
185 return NSS_STATUS_UNAVAIL;
187 else
189 nis_result *result;
190 char buf[strlen (name) + 24 + tablename_len];
192 sprintf (buf, "[name=%s],%s", name, tablename_val);
194 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
196 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
198 enum nss_status status = niserr2nss (result->status);
200 nis_freeresult (result);
201 return status;
204 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen,
205 errnop);
207 nis_freeresult (result);
209 if (parse_res < 1)
211 if (parse_res == -1)
213 *errnop = ERANGE;
214 return NSS_STATUS_TRYAGAIN;
216 else
217 return NSS_STATUS_NOTFOUND;
219 return NSS_STATUS_SUCCESS;
223 enum nss_status
224 _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
225 char *buffer, size_t buflen, int *errnop)
227 if (tablename_val == NULL)
229 enum nss_status status = _nss_create_tablename (errnop);
231 if (status != NSS_STATUS_SUCCESS)
232 return status;
236 int parse_res;
237 nis_result *result;
238 char buf[100 + tablename_len];
240 sprintf (buf, "[uid=%d],%s", uid, tablename_val);
242 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
244 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
246 enum nss_status status = niserr2nss (result->status);
248 nis_freeresult (result);
249 return status;
252 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop);
254 nis_freeresult (result);
256 if (parse_res < 1)
258 if (parse_res == -1)
260 *errnop = ERANGE;
261 return NSS_STATUS_TRYAGAIN;
263 else
264 return NSS_STATUS_NOTFOUND;
266 return NSS_STATUS_SUCCESS;