Update.
[glibc.git] / inet / getnetgrent_r.c
blob5531844d14a45e38cf3f83e722511129862c7acf
1 /* Copyright (C) 1996,1997,1998,1999,2002,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <bits/libc-lock.h>
20 #include <errno.h>
21 #include <netdb.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "netgroup.h"
25 #include "nsswitch.h"
28 /* Protect above variable against multiple uses at the same time. */
29 __libc_lock_define_initialized (static, lock)
31 /* This handle for the NSS data base is shared between all
32 set/get/endXXXent functions. */
33 static service_user *nip;
35 /* The whole information for the set/get/endnetgrent functions are
36 kept in this structure. */
37 static struct __netgrent dataset;
39 /* The lookup function for the first entry of this service. */
40 extern int __nss_netgroup_lookup (service_user **nip, const char *name,
41 void **fctp) internal_function;
44 /* Set up NIP to run through the services. If ALL is zero, use NIP's
45 current location if it's not nil. Return nonzero if there are no
46 services (left). */
47 static enum nss_status
48 setup (void **fctp, const char *func_name, int all)
50 /* Remember the first service_entry, it's always the same. */
51 static service_user *startp;
52 int no_more;
54 if (startp == NULL)
56 no_more = __nss_netgroup_lookup (&nip, func_name, fctp);
57 startp = no_more ? (service_user *) -1 : nip;
59 else if (startp == (service_user *) -1)
60 /* No services at all. */
61 return 1;
62 else
64 if (all || !nip)
65 /* Reset to the beginning of the service list. */
66 nip = startp;
67 /* Look up the first function. */
68 no_more = __nss_lookup (&nip, func_name, fctp);
70 return no_more;
73 /* Free used memory. */
74 static void
75 free_memory (struct __netgrent *data)
77 while (data->known_groups != NULL)
79 struct name_list *tmp = data->known_groups;
80 data->known_groups = data->known_groups->next;
81 free ((void *) tmp->name);
82 free (tmp);
85 while (data->needed_groups != NULL)
87 struct name_list *tmp = data->needed_groups;
88 data->needed_groups = data->needed_groups->next;
89 free ((void *) tmp->name);
90 free (tmp);
94 static int
95 internal_function
96 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap,
97 int *errnop)
99 union
101 enum nss_status (*f) (const char *, struct __netgrent *);
102 void *ptr;
103 } fct;
104 enum nss_status status = NSS_STATUS_UNAVAIL;
105 struct name_list *new_elem;
106 int no_more;
108 /* Cycle through all the services and run their setnetgrent functions. */
109 no_more = setup (&fct.ptr, "setnetgrent", 1);
110 while (! no_more)
112 /* Ignore status, we force check in `__nss_next'. */
113 status = (*fct.f) (group, datap);
115 no_more = __nss_next (&nip, "setnetgrent", &fct.ptr, status, 0);
118 /* Add the current group to the list of known groups. */
119 new_elem = (struct name_list *) malloc (sizeof (struct name_list));
120 if (new_elem == NULL || (new_elem->name = __strdup (group)) == NULL)
122 if (new_elem != NULL)
123 free (new_elem);
124 *errnop = errno;
125 status = NSS_STATUS_TRYAGAIN;
127 else
129 new_elem->next = datap->known_groups;
130 datap->known_groups = new_elem;
133 return status == NSS_STATUS_SUCCESS;
136 int internal_setnetgrent (const char *group, struct __netgrent *datap);
137 libc_hidden_proto (internal_setnetgrent)
140 internal_setnetgrent (const char *group, struct __netgrent *datap)
142 /* Free list of all netgroup names from last run. */
143 free_memory (datap);
145 return __internal_setnetgrent_reuse (group, datap, &errno);
147 libc_hidden_def (internal_setnetgrent)
148 strong_alias (internal_setnetgrent, __internal_setnetgrent)
151 setnetgrent (const char *group)
153 int result;
155 __libc_lock_lock (lock);
157 result = internal_setnetgrent (group, &dataset);
159 __libc_lock_unlock (lock);
161 return result;
165 void internal_endnetgrent (struct __netgrent *datap);
166 libc_hidden_proto (internal_endnetgrent)
168 void
169 internal_endnetgrent (struct __netgrent *datap)
171 service_user *old_nip;
172 union
174 enum nss_status (*f) (struct __netgrent *);
175 void *ptr;
176 } fct;
177 int no_more;
179 /* Remember which was the last used service. */
180 old_nip = nip;
182 /* Cycle through all the services and run their endnetgrent functions. */
183 no_more = setup (&fct.ptr, "endnetgrent", 1);
184 while (! no_more)
186 /* Ignore status, we force check in `__nss_next'. */
187 (void) (*fct.f) (datap);
189 no_more = (nip == old_nip
190 || __nss_next (&nip, "endnetgrent", &fct.ptr, 0, 1));
193 /* Now free list of all netgroup names from last run. */
194 free_memory (datap);
196 libc_hidden_def (internal_endnetgrent)
197 strong_alias (internal_endnetgrent, __internal_endnetgrent)
200 void
201 endnetgrent (void)
203 __libc_lock_lock (lock);
205 internal_endnetgrent (&dataset);
207 __libc_lock_unlock (lock);
211 int internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
212 struct __netgrent *datap,
213 char *buffer, size_t buflen, int *errnop);
214 libc_hidden_proto (internal_getnetgrent_r)
217 internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
218 struct __netgrent *datap,
219 char *buffer, size_t buflen, int *errnop)
221 union
223 enum nss_status (*f) (struct __netgrent *, char *, size_t, int *);
224 void *ptr;
225 } fct;
226 int no_more;
228 /* Initialize status to return if no more functions are found. */
229 enum nss_status status = NSS_STATUS_NOTFOUND;
231 /* Run through available functions, starting with the same function last
232 run. We will repeat each function as long as it succeeds, and then go
233 on to the next service action. */
234 no_more = setup (&fct.ptr, "getnetgrent_r", 0);
235 while (! no_more)
237 status = (*fct.f) (datap, buffer, buflen, &errno);
239 if (status == NSS_STATUS_RETURN)
241 /* This was the last one for this group. Look at next group
242 if available. */
243 int found = 0;
244 while (datap->needed_groups != NULL && ! found)
246 struct name_list *tmp = datap->needed_groups;
247 datap->needed_groups = datap->needed_groups->next;
248 tmp->next = datap->known_groups;
249 datap->known_groups = tmp;
251 found = __internal_setnetgrent_reuse (datap->known_groups->name,
252 datap, errnop);
255 if (found)
256 continue;
258 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
260 /* The last entry was a name of another netgroup. */
261 struct name_list *namep;
263 /* Ignore if we've seen the name before. */
264 for (namep = datap->known_groups; namep != NULL;
265 namep = namep->next)
266 if (strcmp (datap->val.group, namep->name) == 0)
267 break;
268 if (namep != NULL)
269 /* Really ignore. */
270 continue;
272 namep = (struct name_list *) malloc (sizeof (struct name_list));
273 if (namep == NULL
274 || (namep->name = __strdup (datap->val.group)) == NULL)
276 /* We are out of memory. */
277 if (namep != NULL)
278 free (namep);
279 status = NSS_STATUS_RETURN;
281 else
283 namep->next = datap->needed_groups;
284 datap->needed_groups = namep;
285 /* And get the next entry. */
286 continue;
290 no_more = __nss_next (&nip, "getnetgrent_r", &fct.ptr, status, 0);
293 if (status == NSS_STATUS_SUCCESS)
295 *hostp = (char *) datap->val.triple.host;
296 *userp = (char *) datap->val.triple.user;
297 *domainp = (char *) datap->val.triple.domain;
300 return status == NSS_STATUS_SUCCESS ? 1 : 0;
302 libc_hidden_def (internal_getnetgrent_r)
303 strong_alias (internal_getnetgrent_r, __internal_getnetgrent_r)
305 /* The real entry point. */
307 __getnetgrent_r (char **hostp, char **userp, char **domainp,
308 char *buffer, size_t buflen)
310 enum nss_status status;
312 __libc_lock_lock (lock);
314 status = internal_getnetgrent_r (hostp, userp, domainp, &dataset,
315 buffer, buflen, &errno);
317 __libc_lock_unlock (lock);
319 return status;
321 weak_alias (__getnetgrent_r, getnetgrent_r)
323 /* Test whether given (host,user,domain) triple is in NETGROUP. */
325 innetgr (const char *netgroup, const char *host, const char *user,
326 const char *domain)
328 union
330 int (*f) (const char *, struct __netgrent *);
331 void *ptr;
332 } setfct;
333 union
335 void (*f) (struct __netgrent *);
336 void *ptr;
337 } endfct;
338 union
340 int (*f) (struct __netgrent *, char *, size_t, int *);
341 void *ptr;
342 } getfct;
343 struct name_list *known = NULL;
344 struct name_list *needed = NULL;
345 int result = 0;
346 int no_more;
347 const char *current_group = netgroup;
348 int real_entry = 0;
350 /* Walk through the services until we found an answer or we shall
351 not work further. We can do some optimization here. Since all
352 services must provide the `setnetgrent' function we can do all
353 the work during one walk through the service list. */
354 while (1)
356 no_more = setup (&setfct.ptr, "setnetgrent", 1);
357 while (! no_more)
359 enum nss_status status;
360 struct __netgrent entry;
362 /* Clear the space for the netgroup data. */
363 __bzero (&entry, sizeof (entry));
365 /* Open netgroup. */
366 status = (*setfct.f) (current_group, &entry);
367 if (status == NSS_STATUS_SUCCESS
368 && __nss_lookup (&nip, "getnetgrent_r", &getfct.ptr) == 0)
370 char buffer[1024];
372 while ((*getfct.f) (&entry, buffer, sizeof buffer, &errno)
373 == NSS_STATUS_SUCCESS)
375 if (entry.type == group_val)
377 /* Make sure we haven't seen the name before. */
378 struct name_list *namep;
380 for (namep = known; namep != NULL; namep = namep->next)
381 if (strcmp (entry.val.group, namep->name) == 0)
382 break;
383 if (namep == NULL
384 && strcmp (netgroup, entry.val.group) != 0)
386 namep =
387 (struct name_list *) malloc (sizeof (*namep));
388 if (namep == NULL
389 || ((namep->name = __strdup (entry.val.group))
390 == NULL))
392 /* Out of memory, simply return. */
393 if (namep != NULL)
394 free (namep);
395 result = -1;
396 break;
399 namep->next = needed;
400 needed = namep;
403 else
405 real_entry = 1;
407 if ((entry.val.triple.host == NULL || host == NULL
408 || __strcasecmp (entry.val.triple.host, host) == 0)
409 && (entry.val.triple.user == NULL || user == NULL
410 || strcmp (entry.val.triple.user, user) == 0)
411 && (entry.val.triple.domain == NULL || domain == NULL
412 || __strcasecmp (entry.val.triple.domain,
413 domain) == 0))
415 result = 1;
416 break;
421 if (result != 0)
422 break;
424 /* If we found one service which does know the given
425 netgroup we don't try further. */
426 status = NSS_STATUS_RETURN;
429 /* Free all resources of the service. */
430 if (__nss_lookup (&nip, "endnetgrent", &endfct.ptr) == 0)
431 (*endfct.f) (&entry);
433 /* Look for the next service. */
434 no_more = __nss_next (&nip, "setnetgrent",
435 &setfct.ptr, status, 0);
438 if (result == 0 && needed != NULL)
440 struct name_list *tmp = needed;
441 needed = tmp->next;
442 tmp->next = known;
443 known = tmp;
444 current_group = known->name;
445 continue;
448 /* No way out. */
449 break;
452 /* Free the memory. */
453 while (known != NULL)
455 struct name_list *tmp = known;
456 known = known->next;
457 free ((void *) tmp->name);
458 free (tmp);
460 while (needed != NULL)
462 struct name_list *tmp = needed;
463 needed = needed->next;
464 free ((void *) tmp->name);
465 free (tmp);
468 return result == 1;
470 libc_hidden_def (innetgr)