Get definition for time_t.
[glibc/history.git] / inet / getnetgrent_r.c
blobd7a2b58b78516b59dc18eca54be8fd3b79e5cc70
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,
96 int *errnop)
98 enum nss_status (*fct) (const char *, struct __netgrent *);
99 enum nss_status status = NSS_STATUS_UNAVAIL;
100 struct name_list *new_elem;
101 int no_more;
103 /* Cycle through all the services and run their setnetgrent functions. */
104 no_more = setup ((void **) &fct, "setnetgrent", 1);
105 while (! no_more)
107 /* Ignore status, we force check in `__nss_next'. */
108 status = (*fct) (group, datap);
110 no_more = __nss_next (&nip, "setnetgrent", (void **) &fct, status, 0);
113 /* Add the current group to the list of known groups. */
114 new_elem = (struct name_list *) malloc (sizeof (struct name_list));
115 if (new_elem == NULL || (new_elem->name = __strdup (group)) == NULL)
117 if (new_elem != NULL)
118 free (new_elem);
119 *errnop = errno;
120 status = NSS_STATUS_TRYAGAIN;
122 else
124 new_elem->next = datap->known_groups;
125 datap->known_groups = new_elem;
128 return status == NSS_STATUS_SUCCESS;
132 __internal_setnetgrent (const char *group, struct __netgrent *datap)
134 /* Free list of all netgroup names from last run. */
135 free_memory (datap);
137 return __internal_setnetgrent_reuse (group, datap, __errno_location ());
141 setnetgrent (const char *group)
143 int result;
145 __libc_lock_lock (lock);
147 result = __internal_setnetgrent (group, &dataset);
149 __libc_lock_unlock (lock);
151 return result;
155 void
156 __internal_endnetgrent (struct __netgrent *datap)
158 service_user *old_nip;
159 enum nss_status (*fct) (struct __netgrent *);
160 int no_more;
162 /* Remember which was the last used service. */
163 old_nip = nip;
165 /* Cycle through all the services and run their endnetgrent functions. */
166 no_more = setup ((void **) &fct, "endnetgrent", 1);
167 while (! no_more)
169 /* Ignore status, we force check in `__nss_next'. */
170 (void) (*fct) (datap);
172 no_more = (nip == old_nip
173 || __nss_next (&nip, "endnetgrent", (void **) &fct, 0, 1));
176 /* Now free list of all netgroup names from last run. */
177 free_memory (datap);
181 void
182 endnetgrent (void)
184 __libc_lock_lock (lock);
186 __internal_endnetgrent (&dataset);
188 __libc_lock_unlock (lock);
193 __internal_getnetgrent_r (char **hostp, char **userp, char **domainp,
194 struct __netgrent *datap,
195 char *buffer, size_t buflen, int *errnop)
197 enum nss_status (*fct) (struct __netgrent *, char *, size_t);
198 int no_more;
200 /* Initialize status to return if no more functions are found. */
201 enum nss_status status = NSS_STATUS_NOTFOUND;
203 /* Run through available functions, starting with the same function last
204 run. We will repeat each function as long as it succeeds, and then go
205 on to the next service action. */
206 no_more = setup ((void **) &fct, "getnetgrent_r", 0);
207 while (! no_more)
209 status = (*fct) (datap, buffer, buflen);
211 if (status == NSS_STATUS_RETURN)
213 /* This was the last one for this group. Look at next group
214 if available. */
215 int found = 0;
216 while (datap->needed_groups != NULL && ! found)
218 struct name_list *tmp = datap->needed_groups;
219 datap->needed_groups = datap->needed_groups->next;
220 tmp->next = datap->known_groups;
221 datap->known_groups = tmp;
223 found = __internal_setnetgrent_reuse (datap->known_groups->name,
224 datap, errnop);
227 if (found)
228 continue;
230 else if (status == NSS_STATUS_SUCCESS && datap->type == group_val)
232 /* The last entry was a name of another netgroup. */
233 struct name_list *namep;
235 /* Ignore if we've seen the name before. */
236 for (namep = datap->known_groups; namep != NULL;
237 namep = namep->next)
238 if (strcmp (datap->val.group, namep->name) == 0)
239 break;
240 if (namep != NULL)
241 /* Really ignore. */
242 continue;
244 namep = (struct name_list *) malloc (sizeof (struct name_list));
245 if (namep == NULL
246 || (namep->name = __strdup (datap->val.group)) == NULL)
248 /* We are out of memory. */
249 if (namep != NULL)
250 free (namep);
251 status = NSS_STATUS_RETURN;
253 else
255 namep->next = datap->needed_groups;
256 datap->needed_groups = namep;
257 /* And get the next entry. */
258 continue;
262 no_more = __nss_next (&nip, "getnetgrent_r", (void **) &fct, status, 0);
265 if (status == NSS_STATUS_SUCCESS)
267 *hostp = (char *) datap->val.triple.host;
268 *userp = (char *) datap->val.triple.user;
269 *domainp = (char *) datap->val.triple.domain;
272 return status == NSS_STATUS_SUCCESS ? 1 : 0;
275 /* The real entry point. */
277 __getnetgrent_r (char **hostp, char **userp, char **domainp,
278 char *buffer, size_t buflen)
280 enum nss_status status;
282 __libc_lock_lock (lock);
284 status = __internal_getnetgrent_r (hostp, userp, domainp, &dataset,
285 buffer, buflen, __errno_location ());
287 __libc_lock_unlock (lock);
289 return status;
291 weak_alias (__getnetgrent_r, getnetgrent_r)
293 /* Test whether given (host,user,domain) triple is in NETGROUP. */
295 innetgr (const char *netgroup, const char *host, const char *user,
296 const char *domain)
298 int (*setfct) (const char *, struct __netgrent *);
299 void (*endfct) (struct __netgrent *);
300 int (*getfct) (struct __netgrent *, char *, size_t);
301 struct name_list *known = NULL;
302 struct name_list *needed = NULL;
303 int result = 0;
304 int no_more;
305 const char *current_group = netgroup;
306 int real_entry = 0;
308 /* Walk through the services until we found an answer or we shall
309 not work further. We can do some optimization here. Since all
310 services must provide the `setnetgrent' function we can do all
311 the work during one walk through the service list. */
312 while (1)
314 no_more = setup ((void **) &setfct, "setnetgrent", 1);
315 while (! no_more)
317 enum nss_status status;
318 struct __netgrent entry;
320 /* Clear the space for the netgroup data. */
321 bzero (&entry, sizeof (entry));
323 /* Open netgroup. */
324 status = (*setfct) (current_group, &entry);
325 if (status == NSS_STATUS_SUCCESS
326 && __nss_lookup (&nip, "getnetgrent_r", (void **) &getfct) == 0)
328 char buffer[1024];
330 while ((*getfct) (&entry, buffer, sizeof buffer)
331 == NSS_STATUS_SUCCESS)
333 if (entry.type == group_val)
335 /* Make sure we haven't seen the name before. */
336 struct name_list *namep;
338 for (namep = known; namep != NULL; namep = namep->next)
339 if (strcmp (entry.val.group, namep->name) == 0)
340 break;
341 if (namep == NULL
342 && strcmp (netgroup, entry.val.group) != 0)
344 namep =
345 (struct name_list *) malloc (sizeof (*namep));
346 if (namep == NULL
347 || ((namep->name = __strdup (entry.val.group))
348 == NULL))
350 /* Out of memory, simply return. */
351 if (namep != NULL)
352 free (namep);
353 result = -1;
354 break;
357 namep->next = needed;
358 needed = namep;
361 else
363 real_entry = 1;
365 if ((entry.val.triple.host == NULL || host == NULL
366 || strcmp (entry.val.triple.host, host) == 0)
367 && (entry.val.triple.user == NULL || user == NULL
368 || strcmp (entry.val.triple.user, user) == 0)
369 && (entry.val.triple.domain == NULL || domain == NULL
370 || strcmp (entry.val.triple.domain, domain) == 0))
372 result = 1;
373 break;
378 if (result != 0)
379 break;
381 /* If we found one service which does know the given
382 netgroup we don't try further. */
383 status = NSS_STATUS_RETURN;
386 /* Free all resources of the service. */
387 if (__nss_lookup (&nip, "endnetgrent", (void **) &endfct) == 0)
388 (*endfct) (&entry);
390 /* Look for the next service. */
391 no_more = __nss_next (&nip, "setnetgrent",
392 (void **) &setfct, status, 0);
395 if (result == 0 && needed != NULL)
397 struct name_list *tmp = needed;
398 needed = tmp->next;
399 tmp->next = known;
400 known = tmp;
401 current_group = known->name;
402 continue;
405 /* No way out. */
406 break;
409 /* Free the memory. */
410 while (known != NULL)
412 struct name_list *tmp = known;
413 known = known->next;
414 free ((void *) tmp->name);
415 free (tmp);
417 while (needed != NULL)
419 struct name_list *tmp = needed;
420 needed = needed->next;
421 free ((void *) tmp->name);
422 free (tmp);
425 return result == 1;