1 /* Copyright (C) 2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
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
24 #include <not-cancel.h>
26 #include "nscd-client.h"
27 #include "nscd_proto.h"
29 int __nss_not_use_nscd_netgroup;
32 libc_locked_map_ptr (static, map_handle);
33 /* Note that we only free the structure if necessary. The memory
34 mapping is not removed since it is not visible to the malloc
36 libc_freeres_fn (pw_map_free)
38 if (map_handle.mapped != NO_MAPPING)
40 void *p = map_handle.mapped;
41 map_handle.mapped = NO_MAPPING;
48 __nscd_setnetgrent (const char *group, struct __netgrent *datap)
52 size_t group_len = strlen (group);
54 /* If the mapping is available, try to search there instead of
55 communicating with the nscd. */
56 struct mapped_database *mapped;
57 mapped = __nscd_get_map_ref (GETFDNETGR, "netgroup", &map_handle, &gc_cycle);
60 char *respdata = NULL;
62 netgroup_response_header netgroup_resp;
64 if (mapped != NO_MAPPING)
66 struct datahead *found = __nscd_cache_search (GETNETGRENT, group,
68 sizeof netgroup_resp);
71 respdata = (char *) (&found->data[0].netgroupdata + 1);
72 netgroup_resp = found->data[0].netgroupdata;
73 /* Now check if we can trust pw_resp fields. If GC is
74 in progress, it can contain anything. */
75 if (mapped->head->gc_cycle != gc_cycle)
86 sock = __nscd_open_socket (group, group_len, GETNETGRENT,
87 &netgroup_resp, sizeof (netgroup_resp));
90 /* nscd not running or wrong version. */
91 __nss_not_use_nscd_netgroup = 1;
96 if (netgroup_resp.found == 1)
98 size_t datalen = netgroup_resp.result_len;
100 /* If we do not have to read the data here it comes from the
101 mapped data and does not have to be freed. */
102 if (respdata == NULL)
104 /* The data will come via the socket. */
105 respdata = malloc (datalen);
106 if (respdata == NULL)
109 if ((size_t) __readall (sock, respdata, datalen) != datalen)
116 datap->data = respdata;
117 datap->data_size = datalen;
118 datap->cursor = respdata;
120 datap->nip = (service_user *) -1l;
121 datap->known_groups = NULL;
122 datap->needed_groups = NULL;
128 if (__builtin_expect (netgroup_resp.found == -1, 0))
130 /* The daemon does not cache this database. */
131 __nss_not_use_nscd_netgroup = 1;
135 /* Set errno to 0 to indicate no error, just no found record. */
137 /* Even though we have not found anything, the result is zero. */
143 close_not_cancel_no_status (sock);
145 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
147 /* When we come here this means there has been a GC cycle while we
148 were looking for the data. This means the data might have been
149 inconsistent. Retry if possible. */
150 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
152 /* nscd is just running gc now. Disable using the mapping. */
153 if (atomic_decrement_val (&mapped->counter) == 0)
154 __nscd_unmap (mapped);
167 __nscd_innetgr (const char *netgroup, const char *host, const char *user,
170 size_t key_len = (strlen (netgroup) + strlen (host ?: "")
171 + strlen (user ?: "") + strlen (domain ?: "") + 7);
173 bool use_alloca = __libc_use_alloca (key_len);
175 key = alloca (key_len);
178 key = malloc (key_len);
182 char *wp = stpcpy (key, netgroup) + 1;
186 wp = stpcpy (wp, host) + 1;
193 wp = stpcpy (wp, user) + 1;
200 wp = stpcpy (wp, domain) + 1;
206 /* If the mapping is available, try to search there instead of
207 communicating with the nscd. */
210 struct mapped_database *mapped;
211 mapped = __nscd_get_map_ref (GETFDNETGR, "netgroup", &map_handle, &gc_cycle);
215 innetgroup_response_header innetgroup_resp;
218 if (mapped != NO_MAPPING)
220 struct datahead *found = __nscd_cache_search (INNETGR, key,
222 sizeof innetgroup_resp);
225 innetgroup_resp = found->data[0].innetgroupdata;
226 /* Now check if we can trust pw_resp fields. If GC is
227 in progress, it can contain anything. */
228 if (mapped->head->gc_cycle != gc_cycle)
238 sock = __nscd_open_socket (key, key_len, INNETGR,
239 &innetgroup_resp, sizeof (innetgroup_resp));
242 /* nscd not running or wrong version. */
243 __nss_not_use_nscd_netgroup = 1;
248 if (innetgroup_resp.found == 1)
249 retval = innetgroup_resp.result;
252 if (__builtin_expect (innetgroup_resp.found == -1, 0))
254 /* The daemon does not cache this database. */
255 __nss_not_use_nscd_netgroup = 1;
259 /* Set errno to 0 to indicate no error, just no found record. */
261 /* Even though we have not found anything, the result is zero. */
267 close_not_cancel_no_status (sock);
269 if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0)
271 /* When we come here this means there has been a GC cycle while we
272 were looking for the data. This means the data might have been
273 inconsistent. Retry if possible. */
274 if ((gc_cycle & 1) != 0 || ++nretries == 5 || retval == -1)
276 /* nscd is just running gc now. Disable using the mapping. */
277 if (atomic_decrement_val (&mapped->counter) == 0)
278 __nscd_unmap (mapped);