2006-03-05 Roland McGrath <roland@frob.com>
[glibc/history.git] / nis / nss_nisplus / nisplus-grp.c
blob423f7e72911f82529267e94d892e5a730e7b06b4
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 <atomic.h>
21 #include <nss.h>
22 #include <grp.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
30 #include "nisplus-parser.h"
33 __libc_lock_define_initialized (static, lock);
35 static nis_result *result;
36 static unsigned long next_entry;
37 static nis_name tablename_val;
38 static size_t tablename_len;
40 static enum nss_status
41 _nss_create_tablename (int *errnop)
43 if (tablename_val == NULL)
45 const char *local_dir = nis_local_directory ();
46 size_t local_dir_len = strlen (local_dir);
47 static const char prefix[] = "group.org_dir.";
49 char *p = malloc (sizeof (prefix) + local_dir_len);
50 if (tablename_val == NULL)
52 *errnop = errno;
53 return NSS_STATUS_TRYAGAIN;
56 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
58 tablename_len = sizeof (prefix) - 1 + local_dir_len;
60 atomic_write_barrier ();
62 tablename_val = p;
65 return NSS_STATUS_SUCCESS;
68 static enum nss_status
69 internal_setgrent (void)
71 enum nss_status status;
73 if (result != NULL)
75 nis_freeresult (result);
76 result = NULL;
78 next_entry = 0;
80 if (tablename_val == NULL)
82 int err;
83 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
84 return NSS_STATUS_UNAVAIL;
87 result = nis_list (tablename_val, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
88 if (result == NULL)
90 status = NSS_STATUS_TRYAGAIN;
91 __set_errno (ENOMEM);
93 else
95 status = niserr2nss (result->status);
96 if (status != NSS_STATUS_SUCCESS)
98 nis_freeresult (result);
99 result = NULL;
102 return status;
105 enum nss_status
106 _nss_nisplus_setgrent (int stayopen)
108 enum nss_status status;
110 __libc_lock_lock (lock);
112 status = internal_setgrent ();
114 __libc_lock_unlock (lock);
116 return status;
119 enum nss_status
120 _nss_nisplus_endgrent (void)
122 __libc_lock_lock (lock);
124 if (result != NULL)
126 nis_freeresult (result);
127 result = NULL;
130 __libc_lock_unlock (lock);
132 return NSS_STATUS_SUCCESS;
135 static enum nss_status
136 internal_nisplus_getgrent_r (struct group *gr, char *buffer, size_t buflen,
137 int *errnop)
139 int parse_res;
141 if (result == NULL)
143 enum nss_status status;
145 status = internal_setgrent ();
146 if (result == NULL || status != NSS_STATUS_SUCCESS)
147 return status;
150 /* Get the next entry until we found a correct one. */
153 if (next_entry >= result->objects.objects_len)
154 return NSS_STATUS_NOTFOUND;
156 parse_res = _nss_nisplus_parse_grent (result, next_entry, gr,
157 buffer, buflen, errnop);
158 if (parse_res == -1)
159 return NSS_STATUS_TRYAGAIN;
161 ++next_entry;
163 while (!parse_res);
165 return NSS_STATUS_SUCCESS;
168 enum nss_status
169 _nss_nisplus_getgrent_r (struct group *result, char *buffer, size_t buflen,
170 int *errnop)
172 int status;
174 __libc_lock_lock (lock);
176 status = internal_nisplus_getgrent_r (result, buffer, buflen, errnop);
178 __libc_lock_unlock (lock);
180 return status;
183 enum nss_status
184 _nss_nisplus_getgrnam_r (const char *name, struct group *gr,
185 char *buffer, size_t buflen, int *errnop)
187 int parse_res;
189 if (tablename_val == NULL)
191 __libc_lock_lock (lock);
193 enum nss_status status = _nss_create_tablename (errnop);
195 __libc_lock_unlock (lock);
197 if (status != NSS_STATUS_SUCCESS)
198 return status;
201 if (name == NULL)
203 *errnop = EINVAL;
204 return NSS_STATUS_NOTFOUND;
207 nis_result *result;
208 char buf[strlen (name) + 9 + tablename_len];
209 int olderr = errno;
211 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
213 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL);
215 if (result == NULL)
217 *errnop = ENOMEM;
218 return NSS_STATUS_TRYAGAIN;
221 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
223 enum nss_status status = niserr2nss (result->status);
225 nis_freeresult (result);
226 return status;
229 parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen, errnop);
230 nis_freeresult (result);
231 if (__builtin_expect (parse_res < 1, 0))
233 if (parse_res == -1)
235 *errnop = ERANGE;
236 return NSS_STATUS_TRYAGAIN;
238 else
240 __set_errno (olderr);
241 return NSS_STATUS_NOTFOUND;
245 return NSS_STATUS_SUCCESS;
248 enum nss_status
249 _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr,
250 char *buffer, size_t buflen, int *errnop)
252 if (tablename_val == NULL)
254 __libc_lock_lock (lock);
256 enum nss_status status = _nss_create_tablename (errnop);
258 __libc_lock_unlock (lock);
260 if (status != NSS_STATUS_SUCCESS)
261 return status;
264 int parse_res;
265 nis_result *result;
266 char buf[8 + 3 * sizeof (unsigned long int) + tablename_len];
267 int olderr = errno;
269 snprintf (buf, sizeof (buf), "[gid=%lu],%s",
270 (unsigned long int) gid, tablename_val);
272 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
274 if (result == NULL)
276 *errnop = ENOMEM;
277 return NSS_STATUS_TRYAGAIN;
280 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
282 enum nss_status status = niserr2nss (result->status);
284 __set_errno (olderr);
286 nis_freeresult (result);
287 return status;
290 parse_res = _nss_nisplus_parse_grent (result, 0, gr, buffer, buflen, errnop);
292 nis_freeresult (result);
293 if (__builtin_expect (parse_res < 1, 0))
295 __set_errno (olderr);
297 if (parse_res == -1)
299 *errnop = ERANGE;
300 return NSS_STATUS_TRYAGAIN;
302 else
303 return NSS_STATUS_NOTFOUND;
306 return NSS_STATUS_SUCCESS;