Update.
[platform/upstream/glibc.git] / nscd / nscd_initgroups.c
1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
4
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.
9
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.
14
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
18    02111-1307 USA.  */
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <not-cancel.h>
27
28 #include "nscd-client.h"
29 #include "nscd_proto.h"
30
31
32 libc_locked_map_ptr (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
35    handling.  */
36 libc_freeres_fn (gr_map_free)
37 {
38   if (map_handle.mapped != NO_MAPPING)
39     free (map_handle.mapped);
40 }
41
42
43 int
44 __nscd_getgrouplist (const char *user, gid_t group, long int *size,
45                      gid_t **groupsp, long int limit)
46 {
47   size_t userlen = strlen (user) + 1;
48   int gc_cycle;
49
50   /* If the mapping is available, try to search there instead of
51      communicating with the nscd.  */
52   struct mapped_database *mapped;
53   mapped = __nscd_get_map_ref (GETFDGR, "group", &map_handle, &gc_cycle);
54
55  retry:;
56   const initgr_response_header *initgr_resp = NULL;
57   char *respdata = NULL;
58   int retval = -1;
59   int sock = -1;
60
61   if (mapped != NO_MAPPING)
62     {
63       const struct datahead *found = __nscd_cache_search (INITGROUPS, user,
64                                                           userlen, mapped);
65       if (found != NULL)
66         {
67           initgr_resp = &found->data[0].initgrdata;
68           respdata = (char *) (initgr_resp + 1);
69           char *recend = (char *) found->data + found->recsize;
70
71           if (respdata + initgr_resp->ngrps * sizeof (int32_t) > recend)
72             goto out;
73         }
74     }
75
76   /* If we do not have the cache mapped, try to get the data over the
77      socket.  */
78   initgr_response_header initgr_resp_mem;
79   if (initgr_resp == NULL)
80     {
81       sock = __nscd_open_socket (user, userlen, INITGROUPS, &initgr_resp_mem,
82                                  sizeof (initgr_resp_mem));
83       if (sock == -1)
84         /* nscd not running or wrong version or hosts caching disabled.  */
85         __nss_not_use_nscd_group = 1;
86
87       initgr_resp = &initgr_resp_mem;
88     }
89
90   if (initgr_resp->found == 1)
91     {
92       /* The following code assumes that gid_t and int32_t are the
93          same size.  This is the case for al existing implementation.
94          If this should change some code needs to be added which
95          doesn't use memcpy but instead copies each array element one
96          by one.  */
97       assert (sizeof (int32_t) == sizeof (gid_t));
98
99       /* Make sure we have enough room.  We always count GROUP in even
100          though we might not end up adding it.  */
101       if (*size < initgr_resp->ngrps + 1)
102         {
103           gid_t *newp = realloc (*groupsp,
104                                  (initgr_resp->ngrps + 1) * sizeof (gid_t));
105           if (newp == NULL)
106             /* We cannot increase the buffer size.  */
107             goto out;
108
109           *groupsp = newp;
110           *size = initgr_resp->ngrps + 1;
111         }
112
113       if (respdata == NULL)
114         {
115           /* Read the data from the socket.  */
116           if ((size_t) TEMP_FAILURE_RETRY (__read (sock, *groupsp,
117                                                    initgr_resp->ngrps
118                                                    * sizeof (gid_t)))
119               == initgr_resp->ngrps * sizeof (gid_t))
120             retval = initgr_resp->ngrps;
121         }
122       else
123         {
124           /* Just copy the data.  */
125           retval = initgr_resp->ngrps;
126           memcpy (*groupsp, respdata, retval * sizeof (gid_t));
127         }
128
129       /* Check whether GROUP is part of the mix.  If not, add it.  */
130       if (retval >= 0)
131         {
132           int cnt;
133           for (cnt = 0; cnt < retval; ++cnt)
134             if ((*groupsp)[cnt] == group)
135               break;
136
137           if (cnt == retval)
138             (*groupsp)[retval++] = group;
139         }
140     }
141   else
142     {
143       /* The `errno' to some value != ERANGE.  */
144       __set_errno (ENOENT);
145       /* Even though we have not found anything, the result is zero.  */
146       retval = 0;
147     }
148
149   if (sock != -1)
150     close_not_cancel_no_status (sock);
151  out:
152   if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1)
153     {
154       /* When we come here this means there has been a GC cycle while we
155          were looking for the data.  This means the data might have been
156          inconsistent.  Retry if possible.  */
157       if ((gc_cycle & 1) != 0)
158         {
159           /* nscd is just running gc now.  Disable using the mapping.  */
160           __nscd_unmap (mapped);
161           mapped = NO_MAPPING;
162         }
163
164       goto retry;
165     }
166
167   return retval;
168 }