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
19 #include <bits/libc-lock.h>
28 /* Protect above variable against multiple uses at the same time. */
29 __libc_lock_define_initialized (static, lock
)
31 /* The whole information for the set/get/endnetgrent functions are
32 kept in this structure. */
33 static struct __netgrent dataset
;
35 /* The lookup function for the first entry of this service. */
36 extern int __nss_netgroup_lookup (service_user
**nip
, const char *name
,
37 void **fctp
) internal_function
;
40 /* Set up NIP to run through the services. If ALL is zero, use NIP's
41 current location if it's not nil. Return nonzero if there are no
43 static enum nss_status
44 setup (void **fctp
, const char *func_name
, int all
, service_user
**nipp
)
46 /* Remember the first service_entry, it's always the same. */
47 static service_user
*startp
;
52 /* Executing this more than once at the same time must yield the
53 same result every time. So we need no locking. */
54 no_more
= __nss_netgroup_lookup (nipp
, func_name
, fctp
);
55 startp
= no_more
? (service_user
*) -1 : *nipp
;
57 else if (startp
== (service_user
*) -1)
58 /* No services at all. */
62 if (all
|| *nipp
== NULL
)
63 /* Reset to the beginning of the service list. */
65 /* Look up the first function. */
66 no_more
= __nss_lookup (nipp
, func_name
, fctp
);
71 /* Free used memory. */
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
;
82 while (data
->needed_groups
!= NULL
)
84 struct name_list
*tmp
= data
->needed_groups
;
85 data
->needed_groups
= data
->needed_groups
->next
;
92 __internal_setnetgrent_reuse (const char *group
, struct __netgrent
*datap
,
97 enum nss_status (*f
) (const char *, struct __netgrent
*);
100 enum nss_status status
= NSS_STATUS_UNAVAIL
;
101 struct name_list
*new_elem
;
103 /* Cycle through all the services and run their setnetgrent functions. */
104 int no_more
= setup (&fct
.ptr
, "setnetgrent", 1, &datap
->nip
);
107 /* Ignore status, we force check in `__nss_next'. */
108 status
= (*fct
.f
) (group
, datap
);
110 no_more
= __nss_next (&datap
->nip
, "setnetgrent", &fct
.ptr
, status
, 0);
113 /* Add the current group to the list of known groups. */
114 size_t group_len
= strlen (group
) + 1;
115 new_elem
= (struct name_list
*) malloc (sizeof (struct name_list
)
117 if (new_elem
== NULL
)
120 status
= NSS_STATUS_TRYAGAIN
;
124 new_elem
->next
= datap
->known_groups
;
125 memcpy (new_elem
->name
, group
, group_len
);
126 datap
->known_groups
= new_elem
;
129 return status
== NSS_STATUS_SUCCESS
;
132 int internal_setnetgrent (const char *group
, struct __netgrent
*datap
);
133 libc_hidden_proto (internal_setnetgrent
)
136 internal_setnetgrent (const char *group
, struct __netgrent
*datap
)
138 /* Free list of all netgroup names from last run. */
141 return __internal_setnetgrent_reuse (group
, datap
, &errno
);
143 libc_hidden_def (internal_setnetgrent
)
144 strong_alias (internal_setnetgrent
, __internal_setnetgrent
)
147 setnetgrent (const char *group
)
151 __libc_lock_lock (lock
);
153 result
= internal_setnetgrent (group
, &dataset
);
155 __libc_lock_unlock (lock
);
161 void internal_endnetgrent (struct __netgrent
*datap
);
162 libc_hidden_proto (internal_endnetgrent
)
165 internal_endnetgrent (struct __netgrent
*datap
)
167 service_user
*old_nip
;
170 enum nss_status (*f
) (struct __netgrent
*);
174 /* Remember which was the last used service. */
175 old_nip
= datap
->nip
;
177 /* Cycle through all the services and run their endnetgrent functions. */
178 int no_more
= setup (&fct
.ptr
, "endnetgrent", 1, &datap
->nip
);
181 /* Ignore status, we force check in `__nss_next'. */
182 (void) (*fct
.f
) (datap
);
184 no_more
= (datap
->nip
== old_nip
185 || __nss_next (&datap
->nip
, "endnetgrent", &fct
.ptr
, 0, 1));
188 /* Now free list of all netgroup names from last run. */
191 libc_hidden_def (internal_endnetgrent
)
192 strong_alias (internal_endnetgrent
, __internal_endnetgrent
)
198 __libc_lock_lock (lock
);
200 internal_endnetgrent (&dataset
);
202 __libc_lock_unlock (lock
);
206 int internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
207 struct __netgrent
*datap
,
208 char *buffer
, size_t buflen
, int *errnop
);
209 libc_hidden_proto (internal_getnetgrent_r
)
212 internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
213 struct __netgrent
*datap
,
214 char *buffer
, size_t buflen
, int *errnop
)
218 enum nss_status (*f
) (struct __netgrent
*, char *, size_t, int *);
222 /* Initialize status to return if no more functions are found. */
223 enum nss_status status
= NSS_STATUS_NOTFOUND
;
225 /* Run through available functions, starting with the same function last
226 run. We will repeat each function as long as it succeeds, and then go
227 on to the next service action. */
228 int no_more
= setup (&fct
.ptr
, "getnetgrent_r", 0, &datap
->nip
);
231 status
= (*fct
.f
) (datap
, buffer
, buflen
, &errno
);
233 if (status
== NSS_STATUS_RETURN
)
235 /* This was the last one for this group. Look at next group
238 while (datap
->needed_groups
!= NULL
&& ! found
)
240 struct name_list
*tmp
= datap
->needed_groups
;
241 datap
->needed_groups
= datap
->needed_groups
->next
;
242 tmp
->next
= datap
->known_groups
;
243 datap
->known_groups
= tmp
;
245 found
= __internal_setnetgrent_reuse (datap
->known_groups
->name
,
252 else if (status
== NSS_STATUS_SUCCESS
&& datap
->type
== group_val
)
254 /* The last entry was a name of another netgroup. */
255 struct name_list
*namep
;
257 /* Ignore if we've seen the name before. */
258 for (namep
= datap
->known_groups
; namep
!= NULL
;
260 if (strcmp (datap
->val
.group
, namep
->name
) == 0)
266 size_t group_len
= strlen (datap
->val
.group
) + 1;
267 namep
= (struct name_list
*) malloc (sizeof (struct name_list
)
270 /* We are out of memory. */
271 status
= NSS_STATUS_RETURN
;
274 namep
->next
= datap
->needed_groups
;
275 memcpy (namep
->name
, datap
->val
.group
, group_len
);
276 datap
->needed_groups
= namep
;
277 /* And get the next entry. */
282 no_more
= __nss_next (&datap
->nip
, "getnetgrent_r", &fct
.ptr
, status
, 0);
285 if (status
== NSS_STATUS_SUCCESS
)
287 *hostp
= (char *) datap
->val
.triple
.host
;
288 *userp
= (char *) datap
->val
.triple
.user
;
289 *domainp
= (char *) datap
->val
.triple
.domain
;
292 return status
== NSS_STATUS_SUCCESS
? 1 : 0;
294 libc_hidden_def (internal_getnetgrent_r
)
295 strong_alias (internal_getnetgrent_r
, __internal_getnetgrent_r
)
297 /* The real entry point. */
299 __getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
300 char *buffer
, size_t buflen
)
302 enum nss_status status
;
304 __libc_lock_lock (lock
);
306 status
= internal_getnetgrent_r (hostp
, userp
, domainp
, &dataset
,
307 buffer
, buflen
, &errno
);
309 __libc_lock_unlock (lock
);
313 weak_alias (__getnetgrent_r
, getnetgrent_r
)
315 /* Test whether given (host,user,domain) triple is in NETGROUP. */
317 innetgr (const char *netgroup
, const char *host
, const char *user
,
322 int (*f
) (const char *, struct __netgrent
*);
327 void (*f
) (struct __netgrent
*);
332 int (*f
) (struct __netgrent
*, char *, size_t, int *);
335 struct __netgrent entry
;
337 const char *current_group
= netgroup
;
340 memset (&entry
, '\0', sizeof (entry
));
342 /* Walk through the services until we found an answer or we shall
343 not work further. We can do some optimization here. Since all
344 services must provide the `setnetgrent' function we can do all
345 the work during one walk through the service list. */
348 int no_more
= setup (&setfct
.ptr
, "setnetgrent", 1, &entry
.nip
);
352 enum nss_status status
= (*setfct
.f
) (current_group
, &entry
);
354 if (status
== NSS_STATUS_SUCCESS
355 && __nss_lookup (&entry
.nip
, "getnetgrent_r", &getfct
.ptr
) == 0)
359 while ((*getfct
.f
) (&entry
, buffer
, sizeof buffer
, &errno
)
360 == NSS_STATUS_SUCCESS
)
362 if (entry
.type
== group_val
)
364 /* Make sure we haven't seen the name before. */
365 struct name_list
*namep
;
367 for (namep
= entry
.known_groups
; namep
!= NULL
;
369 if (strcmp (entry
.val
.group
, namep
->name
) == 0)
372 && strcmp (netgroup
, entry
.val
.group
) != 0)
374 size_t group_len
= strlen (entry
.val
.group
) + 1;
376 (struct name_list
*) malloc (sizeof (*namep
)
380 /* Out of memory, simply return. */
385 namep
->next
= entry
.needed_groups
;
386 memcpy (namep
->name
, entry
.val
.group
, group_len
);
387 entry
.needed_groups
= namep
;
394 if ((entry
.val
.triple
.host
== NULL
|| host
== NULL
395 || __strcasecmp (entry
.val
.triple
.host
, host
) == 0)
396 && (entry
.val
.triple
.user
== NULL
|| user
== NULL
397 || strcmp (entry
.val
.triple
.user
, user
) == 0)
398 && (entry
.val
.triple
.domain
== NULL
|| domain
== NULL
399 || __strcasecmp (entry
.val
.triple
.domain
,
411 /* If we found one service which does know the given
412 netgroup we don't try further. */
413 status
= NSS_STATUS_RETURN
;
416 /* Free all resources of the service. */
417 if (__nss_lookup (&entry
.nip
, "endnetgrent", &endfct
.ptr
) == 0)
418 (*endfct
.f
) (&entry
);
420 /* Look for the next service. */
421 no_more
= __nss_next (&entry
.nip
, "setnetgrent",
422 &setfct
.ptr
, status
, 0);
425 if (result
== 0 && entry
.needed_groups
!= NULL
)
427 struct name_list
*tmp
= entry
.needed_groups
;
428 entry
.needed_groups
= tmp
->next
;
429 tmp
->next
= entry
.known_groups
;
430 entry
.known_groups
= tmp
;
431 current_group
= entry
.known_groups
->name
;
439 /* Free the memory. */
440 free_memory (&entry
);
444 libc_hidden_def (innetgr
)