1 /* Copyright (C) 1996, 1997, 1998, 1999, 2002, 2004, 2005
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <bits/libc-lock.h>
30 /* Protect above variable against multiple uses at the same time. */
31 __libc_lock_define_initialized (static, lock
)
33 /* The whole information for the set/get/endnetgrent functions are
34 kept in this structure. */
35 static struct __netgrent dataset
;
37 /* The lookup function for the first entry of this service. */
38 extern int __nss_netgroup_lookup (service_user
**nipp
, const char *name
,
39 void **fctp
) internal_function
;
41 /* Set up NIP to run through the services. Return nonzero if there are no
44 setup (void **fctp
, 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
, "setnetgrent", fctp
);
55 startp
= no_more
? (service_user
*) -1 : *nipp
;
57 else if (startp
== (service_user
*) -1)
58 /* No services at all. */
62 /* Reset to the beginning of the service list. */
64 /* Look up the first function. */
65 no_more
= __nss_lookup (nipp
, "setnetgrent", fctp
);
70 /* Free used memory. */
72 free_memory (struct __netgrent
*data
)
74 while (data
->known_groups
!= NULL
)
76 struct name_list
*tmp
= data
->known_groups
;
77 data
->known_groups
= data
->known_groups
->next
;
81 while (data
->needed_groups
!= NULL
)
83 struct name_list
*tmp
= data
->needed_groups
;
84 data
->needed_groups
= data
->needed_groups
->next
;
90 endnetgrent_hook (struct __netgrent
*datap
)
92 enum nss_status (*endfct
) (struct __netgrent
*);
94 if (datap
->nip
== NULL
)
97 endfct
= __nss_lookup_function (datap
->nip
, "endnetgrent");
99 (void) (*endfct
) (datap
);
105 __internal_setnetgrent_reuse (const char *group
, struct __netgrent
*datap
,
110 enum nss_status (*f
) (const char *, struct __netgrent
*);
113 enum nss_status status
= NSS_STATUS_UNAVAIL
;
114 struct name_list
*new_elem
;
116 /* Free data from previous service. */
117 endnetgrent_hook (datap
);
119 /* Cycle through all the services and run their setnetgrent functions. */
120 int no_more
= setup (&fct
.ptr
, &datap
->nip
);
123 assert (datap
->data
== NULL
);
125 /* Ignore status, we force check in `__nss_next'. */
126 status
= (*fct
.f
) (group
, datap
);
128 service_user
*old_nip
= datap
->nip
;
129 no_more
= __nss_next (&datap
->nip
, "setnetgrent", &fct
.ptr
, status
, 0);
131 if (status
== NSS_STATUS_SUCCESS
&& ! no_more
)
133 enum nss_status (*endfct
) (struct __netgrent
*);
135 endfct
= __nss_lookup_function (old_nip
, "endnetgrent");
137 (void) (*endfct
) (datap
);
141 /* Add the current group to the list of known groups. */
142 size_t group_len
= strlen (group
) + 1;
143 new_elem
= (struct name_list
*) malloc (sizeof (struct name_list
)
145 if (new_elem
== NULL
)
148 status
= NSS_STATUS_TRYAGAIN
;
152 new_elem
->next
= datap
->known_groups
;
153 memcpy (new_elem
->name
, group
, group_len
);
154 datap
->known_groups
= new_elem
;
157 return status
== NSS_STATUS_SUCCESS
;
160 int internal_setnetgrent (const char *group
, struct __netgrent
*datap
);
161 libc_hidden_proto (internal_setnetgrent
)
164 internal_setnetgrent (const char *group
, struct __netgrent
*datap
)
166 /* Free list of all netgroup names from last run. */
169 return __internal_setnetgrent_reuse (group
, datap
, &errno
);
171 libc_hidden_def (internal_setnetgrent
)
172 strong_alias (internal_setnetgrent
, __internal_setnetgrent
)
175 setnetgrent (const char *group
)
179 __libc_lock_lock (lock
);
181 result
= internal_setnetgrent (group
, &dataset
);
183 __libc_lock_unlock (lock
);
188 void internal_endnetgrent (struct __netgrent
*datap
);
189 libc_hidden_proto (internal_endnetgrent
)
192 internal_endnetgrent (struct __netgrent
*datap
)
194 endnetgrent_hook (datap
);
195 /* Now free list of all netgroup names from last run. */
198 libc_hidden_def (internal_endnetgrent
)
199 strong_alias (internal_endnetgrent
, __internal_endnetgrent
)
205 __libc_lock_lock (lock
);
207 internal_endnetgrent (&dataset
);
209 __libc_lock_unlock (lock
);
213 int internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
214 struct __netgrent
*datap
,
215 char *buffer
, size_t buflen
, int *errnop
);
216 libc_hidden_proto (internal_getnetgrent_r
)
219 internal_getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
220 struct __netgrent
*datap
,
221 char *buffer
, size_t buflen
, int *errnop
)
223 enum nss_status (*fct
) (struct __netgrent
*, char *, size_t, int *);
225 /* Initialize status to return if no more functions are found. */
226 enum nss_status status
= NSS_STATUS_NOTFOUND
;
228 /* Run through available functions, starting with the same function last
229 run. We will repeat each function as long as it succeeds, and then go
230 on to the next service action. */
231 int no_more
= (datap
->nip
== NULL
232 || (fct
= __nss_lookup_function (datap
->nip
, "getnetgrent_r"))
236 status
= (*fct
) (datap
, buffer
, buflen
, &errno
);
238 if (status
== NSS_STATUS_RETURN
)
240 /* This was the last one for this group. Look at next group
243 while (datap
->needed_groups
!= NULL
&& ! found
)
245 struct name_list
*tmp
= datap
->needed_groups
;
246 datap
->needed_groups
= datap
->needed_groups
->next
;
247 tmp
->next
= datap
->known_groups
;
248 datap
->known_groups
= tmp
;
250 found
= __internal_setnetgrent_reuse (datap
->known_groups
->name
,
254 if (found
&& datap
->nip
!= NULL
)
256 fct
= __nss_lookup_function (datap
->nip
, "getnetgrent_r");
261 else if (status
== NSS_STATUS_SUCCESS
&& datap
->type
== group_val
)
263 /* The last entry was a name of another netgroup. */
264 struct name_list
*namep
;
266 /* Ignore if we've seen the name before. */
267 for (namep
= datap
->known_groups
; namep
!= NULL
;
269 if (strcmp (datap
->val
.group
, namep
->name
) == 0)
275 size_t group_len
= strlen (datap
->val
.group
) + 1;
276 namep
= (struct name_list
*) malloc (sizeof (struct name_list
)
279 /* We are out of memory. */
280 status
= NSS_STATUS_RETURN
;
283 namep
->next
= datap
->needed_groups
;
284 memcpy (namep
->name
, datap
->val
.group
, group_len
);
285 datap
->needed_groups
= namep
;
286 /* And get the next entry. */
294 if (status
== NSS_STATUS_SUCCESS
)
296 *hostp
= (char *) datap
->val
.triple
.host
;
297 *userp
= (char *) datap
->val
.triple
.user
;
298 *domainp
= (char *) datap
->val
.triple
.domain
;
301 return status
== NSS_STATUS_SUCCESS
? 1 : 0;
303 libc_hidden_def (internal_getnetgrent_r
)
304 strong_alias (internal_getnetgrent_r
, __internal_getnetgrent_r
)
306 /* The real entry point. */
308 __getnetgrent_r (char **hostp
, char **userp
, char **domainp
,
309 char *buffer
, size_t buflen
)
311 enum nss_status status
;
313 __libc_lock_lock (lock
);
315 status
= internal_getnetgrent_r (hostp
, userp
, domainp
, &dataset
,
316 buffer
, buflen
, &errno
);
318 __libc_lock_unlock (lock
);
322 weak_alias (__getnetgrent_r
, getnetgrent_r
)
324 /* Test whether given (host,user,domain) triple is in NETGROUP. */
326 innetgr (const char *netgroup
, const char *host
, const char *user
,
331 int (*f
) (const char *, struct __netgrent
*);
334 void (*endfct
) (struct __netgrent
*);
335 int (*getfct
) (struct __netgrent
*, char *, size_t, int *);
336 struct __netgrent entry
;
338 const char *current_group
= netgroup
;
341 memset (&entry
, '\0', sizeof (entry
));
343 /* Walk through the services until we found an answer or we shall
344 not work further. We can do some optimization here. Since all
345 services must provide the `setnetgrent' function we can do all
346 the work during one walk through the service list. */
349 int no_more
= setup (&setfct
.ptr
, &entry
.nip
);
352 assert (entry
.data
== NULL
);
355 enum nss_status status
= (*setfct
.f
) (current_group
, &entry
);
357 if (status
== NSS_STATUS_SUCCESS
358 && (getfct
= __nss_lookup_function (entry
.nip
, "getnetgrent_r"))
363 while ((*getfct
) (&entry
, buffer
, sizeof buffer
, &errno
)
364 == NSS_STATUS_SUCCESS
)
366 if (entry
.type
== group_val
)
368 /* Make sure we haven't seen the name before. */
369 struct name_list
*namep
;
371 for (namep
= entry
.known_groups
; namep
!= NULL
;
373 if (strcmp (entry
.val
.group
, namep
->name
) == 0)
376 && strcmp (netgroup
, entry
.val
.group
) != 0)
378 size_t group_len
= strlen (entry
.val
.group
) + 1;
380 (struct name_list
*) malloc (sizeof (*namep
)
384 /* Out of memory, simply return. */
389 namep
->next
= entry
.needed_groups
;
390 memcpy (namep
->name
, entry
.val
.group
, group_len
);
391 entry
.needed_groups
= namep
;
398 if ((entry
.val
.triple
.host
== NULL
|| host
== NULL
399 || __strcasecmp (entry
.val
.triple
.host
, host
) == 0)
400 && (entry
.val
.triple
.user
== NULL
|| user
== NULL
401 || strcmp (entry
.val
.triple
.user
, user
) == 0)
402 && (entry
.val
.triple
.domain
== NULL
|| domain
== NULL
403 || __strcasecmp (entry
.val
.triple
.domain
,
412 /* If we found one service which does know the given
413 netgroup we don't try further. */
414 status
= NSS_STATUS_RETURN
;
417 /* Free all resources of the service. */
418 endfct
= __nss_lookup_function (entry
.nip
, "endnetgrent");
425 /* Look for the next service. */
426 no_more
= __nss_next (&entry
.nip
, "setnetgrent",
427 &setfct
.ptr
, status
, 0);
430 if (result
== 0 && entry
.needed_groups
!= NULL
)
432 struct name_list
*tmp
= entry
.needed_groups
;
433 entry
.needed_groups
= tmp
->next
;
434 tmp
->next
= entry
.known_groups
;
435 entry
.known_groups
= tmp
;
436 current_group
= entry
.known_groups
->name
;
444 /* Free the memory. */
445 free_memory (&entry
);
449 libc_hidden_def (innetgr
)