Update.
[glibc.git] / inet / getnetgrent_r.c
blobd3443793bfac1b9607327c7051a2bfffd096f994
1 /* Copyright (C) 1996, 1997, 1998 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 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);
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 = NULL;
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 enum nss_status (*fct) (const char *, struct __netgrent *);
100 enum nss_status status = NSS_STATUS_UNAVAIL;
101 struct name_list *new_elem;
102 int no_more;
104 /* Cycle through all the services and run their setnetgrent functions. */
105 no_more = setup ((void **) &fct, "setnetgrent", 1);
106 while (! no_more)
108 /* Ignore status, we force check in `__nss_next'. */
109 status = (*fct) (group, datap);
111 no_more = __nss_next (&nip, "setnetgrent", (void **) &fct, status, 0);
114 /* Add the current group to the list of known groups. */
115 new_elem = (struct name_list *) malloc (sizeof (struct name_list));
116 if (new_elem == NULL || (new_elem->name = __strdup (group)) == NULL)
118 if (new_elem != NULL)
119 free (new_elem);
120 *errnop = errno;
121 status = NSS_STATUS_TRYAGAIN;
123 else
125 new_elem->next = datap->known_groups;
126 datap->known_groups = new_elem;
129 return status == NSS_STATUS_SUCCESS;
133 __internal_setnetgrent (const char *group, struct __netgrent *datap)
135 /* Free list of all netgroup names from last run. */
136 free_memory (datap);
138 return __internal_setnetgrent_reuse (group, datap, &errno);
142 setnetgrent (const char *group)
144 int result;
146 __libc_lock_lock (lock);
148 result = __internal_setnetgrent (group, &dataset);
150 __libc_lock_unlock (lock);
152 return result;
156 void
157 __internal_endnetgrent (struct __netgrent *datap)
159 service_user *old_nip;
160 enum nss_status (*fct) (struct __netgrent *);
161 int no_more;
163 /* Remember which was the last used service. */
164 old_nip = nip;
166 /* Cycle through all the services and run their endnetgrent functions. */
167 no_more = setup ((void **) &fct, "endnetgrent", 1);
168 while (! no_more)
170 /* Ignore status, we force check in `__nss_next'. */
171 (void) (*fct) (datap);
173 no_more = (nip == old_nip
174 || __nss_next (&nip, "endnetgrent", (void **) &fct, 0, 1));
177 /* Now free list of all netgroup names from last run. */
178 free_memory (datap);
182 void
183 endnetgrent (void)
185 __libc_lock_lock (lock);
187 __internal_endnetgrent (&dataset);
189 __libc_lock_unlock (lock);
194 __internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
195 struct __netgrent *datap,
196 char *buffer, size_t buflen, int *errnop)
198 enum nss_status (*fct) (struct __netgrent *, char *, size_t);
199 int no_more;
201 /* Initialize status to return if no more functions are found. */
202 enum nss_status status = NSS_STATUS_NOTFOUND;
204 /* Run through available functions, starting with the same function last
205 run. We will repeat each function as long as it succeeds, and then go
206 on to the next service action. */
207 no_more = setup ((void **) &fct, "getnetgrent_r", 0);
208 while (! no_more)
210 status = (*fct) (datap, buffer, buflen);
212 if (status == NSS_STATUS_RETURN)
214 /* This was the last one for this group. Look at next group
215 if available. */
216 int found = 0;
217 while (datap->needed_groups != NULL && ! found)
219 struct name_list *tmp = datap->needed_groups;
220 datap->needed_groups = datap->needed_groups->next;
221 tmp->next = datap->known_groups;
222 datap->known_groups = tmp;
224 found = __internal_setnetgrent_reuse (datap->known_groups->name,
225 datap, errnop);
228 if (found)
229 continue;
231 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
233 /* The last entry was a name of another netgroup. */
234 struct name_list *namep;
236 /* Ignore if we've seen the name before. */
237 for (namep = datap->known_groups; namep != NULL;
238 namep = namep->next)
239 if (strcmp (datap->val.group, namep->name) == 0)
240 break;
241 if (namep != NULL)
242 /* Really ignore. */
243 continue;
245 namep = (struct name_list *) malloc (sizeof (struct name_list));
246 if (namep == NULL
247 || (namep->name = __strdup (datap->val.group)) == NULL)
249 /* We are out of memory. */
250 if (namep != NULL)
251 free (namep);
252 status = NSS_STATUS_RETURN;
254 else
256 namep->next = datap->needed_groups;
257 datap->needed_groups = namep;
258 /* And get the next entry. */
259 continue;
263 no_more = __nss_next (&nip, "getnetgrent_r", (void **) &fct, status, 0);
266 if (status == NSS_STATUS_SUCCESS)
268 *hostp = (char *) datap->val.triple.host;
269 *userp = (char *) datap->val.triple.user;
270 *domainp = (char *) datap->val.triple.domain;
273 return status == NSS_STATUS_SUCCESS ? 1 : 0;
276 /* The real entry point. */
278 __getnetgrent_r (char **hostp, char **userp, char **domainp,
279 char *buffer, size_t buflen)
281 enum nss_status status;
283 __libc_lock_lock (lock);
285 status = __internal_getnetgrent_r (hostp, userp, domainp, &dataset,
286 buffer, buflen, &errno);
288 __libc_lock_unlock (lock);
290 return status;
292 weak_alias (__getnetgrent_r, getnetgrent_r)
294 /* Test whether given (host,user,domain) triple is in NETGROUP. */
296 innetgr (const char *netgroup, const char *host, const char *user,
297 const char *domain)
299 int (*setfct) (const char *, struct __netgrent *);
300 void (*endfct) (struct __netgrent *);
301 int (*getfct) (struct __netgrent *, char *, size_t);
302 struct name_list *known = NULL;
303 struct name_list *needed = NULL;
304 int result = 0;
305 int no_more;
306 const char *current_group = netgroup;
307 int real_entry = 0;
309 /* Walk through the services until we found an answer or we shall
310 not work further. We can do some optimization here. Since all
311 services must provide the `setnetgrent' function we can do all
312 the work during one walk through the service list. */
313 while (1)
315 no_more = setup ((void **) &setfct, "setnetgrent", 1);
316 while (! no_more)
318 enum nss_status status;
319 struct __netgrent entry;
321 /* Clear the space for the netgroup data. */
322 __bzero (&entry, sizeof (entry));
324 /* Open netgroup. */
325 status = (*setfct) (current_group, &entry);
326 if (status == NSS_STATUS_SUCCESS
327 && __nss_lookup (&nip, "getnetgrent_r", (void **) &getfct) == 0)
329 char buffer[1024];
331 while ((*getfct) (&entry, buffer, sizeof buffer)
332 == NSS_STATUS_SUCCESS)
334 if (entry.type == group_val)
336 /* Make sure we haven't seen the name before. */
337 struct name_list *namep;
339 for (namep = known; namep != NULL; namep = namep->next)
340 if (strcmp (entry.val.group, namep->name) == 0)
341 break;
342 if (namep == NULL
343 && strcmp (netgroup, entry.val.group) != 0)
345 namep =
346 (struct name_list *) malloc (sizeof (*namep));
347 if (namep == NULL
348 || ((namep->name = __strdup (entry.val.group))
349 == NULL))
351 /* Out of memory, simply return. */
352 if (namep != NULL)
353 free (namep);
354 result = -1;
355 break;
358 namep->next = needed;
359 needed = namep;
362 else
364 real_entry = 1;
366 if ((entry.val.triple.host == NULL || host == NULL
367 || __strcasecmp (entry.val.triple.host, host) == 0)
368 && (entry.val.triple.user == NULL || user == NULL
369 || strcmp (entry.val.triple.user, user) == 0)
370 && (entry.val.triple.domain == NULL || domain == NULL
371 || __strcasecmp (entry.val.triple.domain,
372 domain) == 0))
374 result = 1;
375 break;
380 if (result != 0)
381 break;
383 /* If we found one service which does know the given
384 netgroup we don't try further. */
385 status = NSS_STATUS_RETURN;
388 /* Free all resources of the service. */
389 if (__nss_lookup (&nip, "endnetgrent", (void **) &endfct) == 0)
390 (*endfct) (&entry);
392 /* Look for the next service. */
393 no_more = __nss_next (&nip, "setnetgrent",
394 (void **) &setfct, status, 0);
397 if (result == 0 && needed != NULL)
399 struct name_list *tmp = needed;
400 needed = tmp->next;
401 tmp->next = known;
402 known = tmp;
403 current_group = known->name;
404 continue;
407 /* No way out. */
408 break;
411 /* Free the memory. */
412 while (known != NULL)
414 struct name_list *tmp = known;
415 known = known->next;
416 free ((void *) tmp->name);
417 free (tmp);
419 while (needed != NULL)
421 struct name_list *tmp = needed;
422 needed = needed->next;
423 free ((void *) tmp->name);
424 free (tmp);
427 return result == 1;