update from main archive 961109
[glibc.git] / inet / getnetgrent_r.c
blob1848ad7f35cd240e6e44a49ca91b900a193f46e9
1 /* Copyright (C) 1996 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
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <libc-lock.h>
20 #include <netdb.h>
21 #include <string.h>
22 #include "netgroup.h"
23 #include "nsswitch.h"
26 /* Protect above variable against multiple uses at the same time. */
27 __libc_lock_define_initialized (static, lock)
29 /* This handle for the NSS data base is shared between all
30 set/get/endXXXent functions. */
31 static service_user *nip;
33 /* The whole information for the set/get/endnetgrent functions are
34 kept in this structure. */
35 static struct __netgrent dataset;
38 /* The lookup function for the first entry of this service. */
39 extern int __nss_netgroup_lookup (service_user **nip, const char *name,
40 void **fctp);
42 /* Set up NIP to run through the services. If ALL is zero, use NIP's
43 current location if it's not nil. Return nonzero if there are no
44 services (left). */
45 static enum nss_status
46 setup (void **fctp, const char *func_name, int all)
48 /* Remember the first service_entry, it's always the same. */
49 static service_user *startp = NULL;
50 int no_more;
52 if (startp == NULL)
54 no_more = __nss_netgroup_lookup (&nip, func_name, fctp);
55 startp = no_more ? (service_user *) -1 : nip;
57 else if (startp == (service_user *) -1)
58 /* No services at all. */
59 return 1;
60 else
62 if (all || !nip)
63 /* Reset to the beginning of the service list. */
64 nip = startp;
65 /* Look up the first function. */
66 no_more = __nss_lookup (&nip, func_name, fctp);
68 return no_more;
71 /* Free used memory. */
72 static void
73 free_memory (struct __netgrent *data)
75 while (data->known_groups != NULL)
77 struct name_list *tmp = data->known_groups;
78 data->known_groups = data->known_groups->next;
79 free (tmp->name);
80 free (tmp);
83 while (data->needed_groups != NULL)
85 struct name_list *tmp = data->needed_groups;
86 data->needed_groups = data->needed_groups->next;
87 free (tmp->name);
88 free (tmp);
92 static int
93 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap)
95 enum nss_status (*fct) (const char *, struct __netgrent *);
96 enum nss_status status = NSS_STATUS_UNAVAIL;
97 struct name_list *new_elem;
98 int no_more;
100 /* Cycle through all the services and run their setnetgrent functions. */
101 no_more = setup ((void **) &fct, "setnetgrent", 1);
102 while (! no_more)
104 /* Ignore status, we force check in `__nss_next'. */
105 status = (*fct) (group, datap);
107 no_more = __nss_next (&nip, "setnetgrent", (void **) &fct, status, 0);
110 /* Add the current group to the list of known groups. */
111 new_elem = (struct name_list *) malloc (sizeof (struct name_list));
112 if (new_elem == NULL || (new_elem->name = __strdup (group)) == NULL)
114 if (new_elem != NULL)
115 free (new_elem);
116 status == NSS_STATUS_UNAVAIL;
118 else
120 new_elem->next = datap->known_groups;
121 datap->known_groups = new_elem;
124 return status == NSS_STATUS_SUCCESS;
128 __internal_setnetgrent (const char *group, struct __netgrent *datap)
130 /* Free list of all netgroup names from last run. */
131 free_memory (datap);
133 return __internal_setnetgrent_reuse (group, datap);
137 setnetgrent (const char *group)
139 int result;
141 __libc_lock_lock (lock);
143 result = __internal_setnetgrent (group, &dataset);
145 __libc_lock_unlock (lock);
147 return result;
151 void
152 __internal_endnetgrent (struct __netgrent *datap)
154 service_user *old_nip;
155 enum nss_status (*fct) (struct __netgrent *);
156 int no_more;
158 /* Remember which was the last used service. */
159 old_nip = nip;
161 /* Cycle through all the services and run their setnetgrent functions. */
162 no_more = setup ((void **) &fct, "endnetgrent", 1);
163 while (! no_more)
165 /* Ignore status, we force check in `__nss_next'. */
166 (void) (*fct) (datap);
168 no_more = (nip == old_nip
169 || __nss_next (&nip, "endnetgrent", (void **) &fct, 0, 1));
172 /* Now free list of all netgroup names from last run. */
173 free_memory (datap);
177 void
178 endnetgrent (void)
180 __libc_lock_lock (lock);
182 __internal_endnetgrent (&dataset);
184 __libc_lock_unlock (lock);
189 __internal_getnetgrent (char **hostp, char **userp, char **domainp,
190 struct __netgrent *datap,
191 char *buffer, size_t buflen)
193 enum nss_status (*fct) (struct __netgrent *, char *, int);
194 int no_more;
196 /* Initialize status to return if no more functions are found. */
197 enum nss_status status = NSS_STATUS_NOTFOUND;
199 /* Run through available functions, starting with the same function last
200 run. We will repeat each function as long as it succeeds, and then go
201 on to the next service action. */
202 no_more = setup ((void **) &fct, "getnetgrent_r", 0);
203 while (! no_more)
205 status = (*fct) (datap, buffer, buflen);
207 if (status == NSS_STATUS_RETURN)
209 /* This was the last one for this group. Look at next group
210 if available. */
211 int found = 0;
212 while (datap->needed_groups != NULL && ! found)
214 struct name_list *tmp = datap->needed_groups;
215 datap->needed_groups = datap->needed_groups->next;
216 tmp->next = datap->known_groups;
217 datap->known_groups = tmp;
219 found = __internal_setnetgrent_reuse (datap->known_groups->name,
220 datap);
223 if (found)
224 continue;
226 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
228 /* The last entry was a name of another netgroup. */
229 struct name_list *namep;
231 /* Ignore if we've seen the name before. */
232 for (namep = datap->known_groups; namep != NULL;
233 namep = namep->next)
234 if (strcmp (datap->val.group, namep->name) == 0)
235 break;
236 if (namep != NULL)
237 /* Really ignore. */
238 continue;
240 namep = (struct name_list *) malloc (sizeof (struct name_list));
241 if (namep == NULL
242 || (namep->name = __strdup (datap->val.group)) == NULL)
244 /* We are out of memory. */
245 if (namep != NULL)
246 free (namep);
247 status = NSS_STATUS_RETURN;
249 else
251 namep->next = datap->needed_groups;
252 datap->needed_groups = namep;
253 /* And get the next entry. */
254 continue;
258 no_more = __nss_next (&nip, "getnetgrent_r", (void **) &fct, status, 0);
261 if (status == NSS_STATUS_SUCCESS)
263 *hostp = datap->val.triple.host;
264 *userp = datap->val.triple.user;
265 *domainp = datap->val.triple.domain;
268 return status == NSS_STATUS_SUCCESS ? 1 : 0;
271 /* The real entry point. */
273 __getnetgrent_r (char **hostp, char **userp, char **domainp,
274 char *buffer, size_t buflen)
276 enum nss_status status;
278 __libc_lock_lock (lock);
280 status = __internal_getnetgrent (hostp, userp, domainp, &dataset,
281 buffer, buflen);
283 __libc_lock_unlock (lock);
285 return status;
287 weak_alias (__getnetgrent_r, getnetgrent_r)
289 /* Test whether given (host,user,domain) triple is in NETGROUP. */
291 innetgr (const char *netgroup, const char *host, const char *user,
292 const char *domain)
294 int (*setfct) (const char *, struct __netgrent *);
295 void (*endfct) (struct __netgrent *);
296 int (*getfct) (struct __netgrent *, char *, int);
297 struct name_list *known;
298 struct name_list *needed;
299 int result = 0;
300 int no_more;
301 const char *current_group = netgroup;
302 int real_entry = 0;
304 /* Walk through the services until we found an answer or we shall
305 not work further. We can do some optimization here. Since all
306 services must provide the `setnetgrent' function we can do all
307 the work during one walk through the service list. */
308 while (1)
310 no_more = setup ((void **) &setfct, "setnetgrent", 1);
311 while (! no_more)
313 enum nss_status status;
314 struct __netgrent entry;
316 /* Clear the space for the netgroup data. */
317 bzero (&entry, sizeof (entry));
319 /* Open netgroup. */
320 status = (*setfct) (current_group, &entry);
321 if (status == NSS_STATUS_SUCCESS
322 && __nss_lookup (&nip, "getnetgrent_r", (void **) &getfct) == 0)
324 char buffer[1024];
326 while ((*getfct) (&entry, buffer, sizeof buffer)
327 == NSS_STATUS_SUCCESS)
329 if (entry.type == group_val)
331 /* Make sure we haven't seen the name before. */
332 struct name_list *namep;
334 for (namep = known; namep != NULL; namep = namep->next)
335 if (strcmp (entry.val.group, namep->name) == 0)
336 break;
337 if (namep == NULL
338 && strcmp (netgroup, entry.val.group) != 0)
340 namep =
341 (struct name_list *) malloc (sizeof (*namep));
342 if (namep == NULL
343 || ((namep->name = __strdup (entry.val.group))
344 == NULL))
346 /* Out of memory, simply return. */
347 if (namep != NULL)
348 free (namep);
349 result = -1;
350 break;
353 namep->next = needed;
354 needed = namep;
357 else
359 real_entry = 1;
361 if ((entry.val.triple.host == NULL || host == NULL
362 || strcmp (entry.val.triple.host, host) == 0)
363 && (entry.val.triple.user == NULL || user == NULL
364 || strcmp (entry.val.triple.user, user) == 0)
365 && (entry.val.triple.domain == NULL || domain == NULL
366 || strcmp (entry.val.triple.domain, domain) == 0))
368 result = 1;
369 break;
374 if (result != 0)
375 break;
377 /* If we found one service which does know the given
378 netgroup we don't try further. */
379 status = NSS_STATUS_RETURN;
382 /* Free all resources of the service. */
383 if (__nss_lookup (&nip, "endnetgrent", (void **) &endfct) == 0)
384 (*endfct) (&entry);
386 /* Look for the next service. */
387 no_more = __nss_next (&nip, "setnetgrent",
388 (void **) &setfct, status, 0);
391 if (result == 0 && needed != NULL)
393 struct name_list *tmp = needed;
394 needed = tmp->next;
395 tmp->next = known;
396 known = tmp;
397 current_group = known->name;
398 continue;
401 /* No way out. */
402 break;
405 /* Free the memory. */
406 while (known != NULL)
408 struct name_list *tmp = known;
409 known = known->next;
410 free (tmp->name);
411 free (tmp);
413 while (needed != NULL)
415 struct name_list *tmp = needed;
416 needed = needed->next;
417 free (tmp->name);
418 free (tmp);
421 return result == 1;