Update.
[glibc.git] / inet / getnetgrent_r.c
bloba388d86ba8d92804738c655227b030bd5a5f341c
1 /* Copyright (C) 1996, 1997 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 <netdb.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include "netgroup.h"
24 #include "nsswitch.h"
27 /* Protect above variable against multiple uses at the same time. */
28 __libc_lock_define_initialized (static, lock)
30 /* This handle for the NSS data base is shared between all
31 set/get/endXXXent functions. */
32 static service_user *nip;
34 /* The whole information for the set/get/endnetgrent functions are
35 kept in this structure. */
36 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);
43 /* Set up NIP to run through the services. If ALL is zero, use NIP's
44 current location if it's not nil. Return nonzero if there are no
45 services (left). */
46 static enum nss_status
47 setup (void **fctp, const char *func_name, int all)
49 /* Remember the first service_entry, it's always the same. */
50 static service_user *startp = NULL;
51 int no_more;
53 if (startp == NULL)
55 no_more = __nss_netgroup_lookup (&nip, func_name, fctp);
56 startp = no_more ? (service_user *) -1 : nip;
58 else if (startp == (service_user *) -1)
59 /* No services at all. */
60 return 1;
61 else
63 if (all || !nip)
64 /* Reset to the beginning of the service list. */
65 nip = startp;
66 /* Look up the first function. */
67 no_more = __nss_lookup (&nip, func_name, fctp);
69 return no_more;
72 /* Free used memory. */
73 static void
74 free_memory (struct __netgrent *data)
76 while (data->known_groups != NULL)
78 struct name_list *tmp = data->known_groups;
79 data->known_groups = data->known_groups->next;
80 free ((void *) tmp->name);
81 free (tmp);
84 while (data->needed_groups != NULL)
86 struct name_list *tmp = data->needed_groups;
87 data->needed_groups = data->needed_groups->next;
88 free ((void *) tmp->name);
89 free (tmp);
93 static int
94 internal_function
95 __internal_setnetgrent_reuse (const char *group, struct __netgrent *datap)
97 enum nss_status (*fct) (const char *, struct __netgrent *);
98 enum nss_status status = NSS_STATUS_UNAVAIL;
99 struct name_list *new_elem;
100 int no_more;
102 /* Cycle through all the services and run their setnetgrent functions. */
103 no_more = setup ((void **) &fct, "setnetgrent", 1);
104 while (! no_more)
106 /* Ignore status, we force check in `__nss_next'. */
107 status = (*fct) (group, datap);
109 no_more = __nss_next (&nip, "setnetgrent", (void **) &fct, status, 0);
112 /* Add the current group to the list of known groups. */
113 new_elem = (struct name_list *) malloc (sizeof (struct name_list));
114 if (new_elem == NULL || (new_elem->name = __strdup (group)) == NULL)
116 if (new_elem != NULL)
117 free (new_elem);
118 status = NSS_STATUS_UNAVAIL;
120 else
122 new_elem->next = datap->known_groups;
123 datap->known_groups = new_elem;
126 return status == NSS_STATUS_SUCCESS;
130 __internal_setnetgrent (const char *group, struct __netgrent *datap)
132 /* Free list of all netgroup names from last run. */
133 free_memory (datap);
135 return __internal_setnetgrent_reuse (group, datap);
139 setnetgrent (const char *group)
141 int result;
143 __libc_lock_lock (lock);
145 result = __internal_setnetgrent (group, &dataset);
147 __libc_lock_unlock (lock);
149 return result;
153 void
154 __internal_endnetgrent (struct __netgrent *datap)
156 service_user *old_nip;
157 enum nss_status (*fct) (struct __netgrent *);
158 int no_more;
160 /* Remember which was the last used service. */
161 old_nip = nip;
163 /* Cycle through all the services and run their endnetgrent functions. */
164 no_more = setup ((void **) &fct, "endnetgrent", 1);
165 while (! no_more)
167 /* Ignore status, we force check in `__nss_next'. */
168 (void) (*fct) (datap);
170 no_more = (nip == old_nip
171 || __nss_next (&nip, "endnetgrent", (void **) &fct, 0, 1));
174 /* Now free list of all netgroup names from last run. */
175 free_memory (datap);
179 void
180 endnetgrent (void)
182 __libc_lock_lock (lock);
184 __internal_endnetgrent (&dataset);
186 __libc_lock_unlock (lock);
191 __internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
192 struct __netgrent *datap,
193 char *buffer, size_t buflen)
195 enum nss_status (*fct) (struct __netgrent *, char *, size_t);
196 int no_more;
198 /* Initialize status to return if no more functions are found. */
199 enum nss_status status = NSS_STATUS_NOTFOUND;
201 /* Run through available functions, starting with the same function last
202 run. We will repeat each function as long as it succeeds, and then go
203 on to the next service action. */
204 no_more = setup ((void **) &fct, "getnetgrent_r", 0);
205 while (! no_more)
207 status = (*fct) (datap, buffer, buflen);
209 if (status == NSS_STATUS_RETURN)
211 /* This was the last one for this group. Look at next group
212 if available. */
213 int found = 0;
214 while (datap->needed_groups != NULL && ! found)
216 struct name_list *tmp = datap->needed_groups;
217 datap->needed_groups = datap->needed_groups->next;
218 tmp->next = datap->known_groups;
219 datap->known_groups = tmp;
221 found = __internal_setnetgrent_reuse (datap->known_groups->name,
222 datap);
225 if (found)
226 continue;
228 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
230 /* The last entry was a name of another netgroup. */
231 struct name_list *namep;
233 /* Ignore if we've seen the name before. */
234 for (namep = datap->known_groups; namep != NULL;
235 namep = namep->next)
236 if (strcmp (datap->val.group, namep->name) == 0)
237 break;
238 if (namep != NULL)
239 /* Really ignore. */
240 continue;
242 namep = (struct name_list *) malloc (sizeof (struct name_list));
243 if (namep == NULL
244 || (namep->name = __strdup (datap->val.group)) == NULL)
246 /* We are out of memory. */
247 if (namep != NULL)
248 free (namep);
249 status = NSS_STATUS_RETURN;
251 else
253 namep->next = datap->needed_groups;
254 datap->needed_groups = namep;
255 /* And get the next entry. */
256 continue;
260 no_more = __nss_next (&nip, "getnetgrent_r", (void **) &fct, status, 0);
263 if (status == NSS_STATUS_SUCCESS)
265 *hostp = (char *) datap->val.triple.host;
266 *userp = (char *) datap->val.triple.user;
267 *domainp = (char *) datap->val.triple.domain;
270 return status == NSS_STATUS_SUCCESS ? 1 : 0;
273 /* The real entry point. */
275 __getnetgrent_r (char **hostp, char **userp, char **domainp,
276 char *buffer, size_t buflen)
278 enum nss_status status;
280 __libc_lock_lock (lock);
282 status = __internal_getnetgrent_r (hostp, userp, domainp, &dataset,
283 buffer, buflen);
285 __libc_lock_unlock (lock);
287 return status;
289 weak_alias (__getnetgrent_r, getnetgrent_r)
291 /* Test whether given (host,user,domain) triple is in NETGROUP. */
293 innetgr (const char *netgroup, const char *host, const char *user,
294 const char *domain)
296 int (*setfct) (const char *, struct __netgrent *);
297 void (*endfct) (struct __netgrent *);
298 int (*getfct) (struct __netgrent *, char *, size_t);
299 struct name_list *known = NULL;
300 struct name_list *needed = NULL;
301 int result = 0;
302 int no_more;
303 const char *current_group = netgroup;
304 int real_entry = 0;
306 /* Walk through the services until we found an answer or we shall
307 not work further. We can do some optimization here. Since all
308 services must provide the `setnetgrent' function we can do all
309 the work during one walk through the service list. */
310 while (1)
312 no_more = setup ((void **) &setfct, "setnetgrent", 1);
313 while (! no_more)
315 enum nss_status status;
316 struct __netgrent entry;
318 /* Clear the space for the netgroup data. */
319 bzero (&entry, sizeof (entry));
321 /* Open netgroup. */
322 status = (*setfct) (current_group, &entry);
323 if (status == NSS_STATUS_SUCCESS
324 && __nss_lookup (&nip, "getnetgrent_r", (void **) &getfct) == 0)
326 char buffer[1024];
328 while ((*getfct) (&entry, buffer, sizeof buffer)
329 == NSS_STATUS_SUCCESS)
331 if (entry.type == group_val)
333 /* Make sure we haven't seen the name before. */
334 struct name_list *namep;
336 for (namep = known; namep != NULL; namep = namep->next)
337 if (strcmp (entry.val.group, namep->name) == 0)
338 break;
339 if (namep == NULL
340 && strcmp (netgroup, entry.val.group) != 0)
342 namep =
343 (struct name_list *) malloc (sizeof (*namep));
344 if (namep == NULL
345 || ((namep->name = __strdup (entry.val.group))
346 == NULL))
348 /* Out of memory, simply return. */
349 if (namep != NULL)
350 free (namep);
351 result = -1;
352 break;
355 namep->next = needed;
356 needed = namep;
359 else
361 real_entry = 1;
363 if ((entry.val.triple.host == NULL || host == NULL
364 || strcmp (entry.val.triple.host, host) == 0)
365 && (entry.val.triple.user == NULL || user == NULL
366 || strcmp (entry.val.triple.user, user) == 0)
367 && (entry.val.triple.domain == NULL || domain == NULL
368 || strcmp (entry.val.triple.domain, domain) == 0))
370 result = 1;
371 break;
376 if (result != 0)
377 break;
379 /* If we found one service which does know the given
380 netgroup we don't try further. */
381 status = NSS_STATUS_RETURN;
384 /* Free all resources of the service. */
385 if (__nss_lookup (&nip, "endnetgrent", (void **) &endfct) == 0)
386 (*endfct) (&entry);
388 /* Look for the next service. */
389 no_more = __nss_next (&nip, "setnetgrent",
390 (void **) &setfct, status, 0);
393 if (result == 0 && needed != NULL)
395 struct name_list *tmp = needed;
396 needed = tmp->next;
397 tmp->next = known;
398 known = tmp;
399 current_group = known->name;
400 continue;
403 /* No way out. */
404 break;
407 /* Free the memory. */
408 while (known != NULL)
410 struct name_list *tmp = known;
411 known = known->next;
412 free ((void *) tmp->name);
413 free (tmp);
415 while (needed != NULL)
417 struct name_list *tmp = needed;
418 needed = needed->next;
419 free ((void *) tmp->name);
420 free (tmp);
423 return result == 1;