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. */
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);
38 static enum nss_status
39 _nss_create_tablename (void)
41 if (tablename_val == NULL)
43 char buf [40 + strlen (nis_local_directory ())];
46 p = __stpcpy (buf, "passwd.org_dir.");
47 p = __stpcpy (p, nis_local_directory ());
48 tablename_val = __strdup (buf);
49 if (tablename_val == NULL)
50 return NSS_STATUS_TRYAGAIN;
51 tablename_len = strlen (tablename_val);
53 return NSS_STATUS_SUCCESS;
58 _nss_nisplus_setpwent (void)
60 enum nss_status status = NSS_STATUS_SUCCESS;
62 __libc_lock_lock (lock);
65 nis_freeresult (result);
68 if (tablename_val == NULL)
69 status = _nss_create_tablename ();
71 __libc_lock_unlock (lock);
77 _nss_nisplus_endpwent (void)
79 __libc_lock_lock (lock);
82 nis_freeresult (result);
85 __libc_lock_unlock (lock);
87 return NSS_STATUS_SUCCESS;
90 static enum nss_status
91 internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
95 /* Get the next entry until we found a correct one. */
98 nis_result *saved_res;
103 if (tablename_val == NULL)
104 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
105 return NSS_STATUS_UNAVAIL;
107 result = nis_first_entry(tablename_val);
108 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
109 return niserr2nss (result->status);
116 res = nis_next_entry(tablename_val, &result->cookie);
118 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
120 nis_freeresult (saved_res);
121 return niserr2nss (result->status);
125 if ((parse_res = _nss_nisplus_parse_pwent (result, pw, buffer,
128 nis_freeresult (result);
130 return NSS_STATUS_TRYAGAIN;
135 nis_freeresult (saved_res);
137 } while (!parse_res);
139 return NSS_STATUS_SUCCESS;
143 _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
147 __libc_lock_lock (lock);
149 status = internal_nisplus_getpwent_r (result, buffer, buflen);
151 __libc_lock_unlock (lock);
157 _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
158 char *buffer, size_t buflen)
162 if (tablename_val == NULL)
163 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
164 return NSS_STATUS_UNAVAIL;
166 if (name == NULL || strlen (name) > 8)
167 return NSS_STATUS_NOTFOUND;
171 char buf[strlen (name) + 24 + tablename_len];
173 sprintf(buf, "[name=%s],%s", name, tablename_val);
175 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
177 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
179 enum nss_status status = niserr2nss (result->status);
181 nis_freeresult (result);
185 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
187 nis_freeresult (result);
190 return NSS_STATUS_TRYAGAIN;
193 return NSS_STATUS_SUCCESS;
195 return NSS_STATUS_NOTFOUND;
200 _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
201 char *buffer, size_t buflen)
203 if (tablename_val == NULL)
204 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
205 return NSS_STATUS_UNAVAIL;
209 char buf[100 + tablename_len];
211 sprintf(buf, "[uid=%d],%s", uid, tablename_val);
213 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
215 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
217 enum nss_status status = niserr2nss (result->status);
219 nis_freeresult (result);
223 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
225 nis_freeresult (result);
228 return NSS_STATUS_TRYAGAIN;
231 return NSS_STATUS_SUCCESS;
233 return NSS_STATUS_NOTFOUND;