]>
Commit | Line | Data |
---|---|---|
a334319f | 1 | /* Copyright (C) 2004 Free Software Foundation, Inc. |
f7e7a396 UD |
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 | ||
ed2ced8a | 32 | /* We use the same mapping as in nscd_getgr. */ |
a334319f | 33 | libc_locked_map_ptr (extern, __gr_map_handle); |
f7e7a396 UD |
34 | |
35 | ||
36 | int | |
37 | __nscd_getgrouplist (const char *user, gid_t group, long int *size, | |
38 | gid_t **groupsp, long int limit) | |
39 | { | |
40 | size_t userlen = strlen (user) + 1; | |
0891f970 UD |
41 | int gc_cycle; |
42 | ||
43 | /* If the mapping is available, try to search there instead of | |
44 | communicating with the nscd. */ | |
45 | struct mapped_database *mapped; | |
ed2ced8a | 46 | mapped = __nscd_get_map_ref (GETFDGR, "group", &__gr_map_handle, &gc_cycle); |
0891f970 UD |
47 | |
48 | retry:; | |
a334319f | 49 | const initgr_response_header *initgr_resp = NULL; |
f7e7a396 UD |
50 | char *respdata = NULL; |
51 | int retval = -1; | |
52 | int sock = -1; | |
f7e7a396 | 53 | |
f7e7a396 UD |
54 | if (mapped != NO_MAPPING) |
55 | { | |
a334319f UD |
56 | const struct datahead *found = __nscd_cache_search (INITGROUPS, user, |
57 | userlen, mapped); | |
f7e7a396 UD |
58 | if (found != NULL) |
59 | { | |
a334319f UD |
60 | initgr_resp = &found->data[0].initgrdata; |
61 | respdata = (char *) (initgr_resp + 1); | |
f7e7a396 UD |
62 | char *recend = (char *) found->data + found->recsize; |
63 | ||
a334319f | 64 | if (respdata + initgr_resp->ngrps * sizeof (int32_t) > recend) |
f7e7a396 UD |
65 | goto out; |
66 | } | |
67 | } | |
68 | ||
69 | /* If we do not have the cache mapped, try to get the data over the | |
70 | socket. */ | |
a334319f UD |
71 | initgr_response_header initgr_resp_mem; |
72 | if (initgr_resp == NULL) | |
f7e7a396 | 73 | { |
a334319f UD |
74 | sock = __nscd_open_socket (user, userlen, INITGROUPS, &initgr_resp_mem, |
75 | sizeof (initgr_resp_mem)); | |
f7e7a396 | 76 | if (sock == -1) |
48ad81fa | 77 | { |
a334319f | 78 | /* nscd not running or wrong version or hosts caching disabled. */ |
48ad81fa UD |
79 | __nss_not_use_nscd_group = 1; |
80 | goto out; | |
81 | } | |
a334319f UD |
82 | |
83 | initgr_resp = &initgr_resp_mem; | |
f7e7a396 UD |
84 | } |
85 | ||
a334319f | 86 | if (initgr_resp->found == 1) |
f7e7a396 UD |
87 | { |
88 | /* The following code assumes that gid_t and int32_t are the | |
89 | same size. This is the case for al existing implementation. | |
90 | If this should change some code needs to be added which | |
91 | doesn't use memcpy but instead copies each array element one | |
92 | by one. */ | |
93 | assert (sizeof (int32_t) == sizeof (gid_t)); | |
a334319f | 94 | assert (initgr_resp->ngrps > 0); |
f7e7a396 UD |
95 | |
96 | /* Make sure we have enough room. We always count GROUP in even | |
97 | though we might not end up adding it. */ | |
a334319f | 98 | if (*size < initgr_resp->ngrps + 1) |
f7e7a396 UD |
99 | { |
100 | gid_t *newp = realloc (*groupsp, | |
a334319f | 101 | (initgr_resp->ngrps + 1) * sizeof (gid_t)); |
f7e7a396 UD |
102 | if (newp == NULL) |
103 | /* We cannot increase the buffer size. */ | |
a334319f | 104 | goto out; |
f7e7a396 UD |
105 | |
106 | *groupsp = newp; | |
a334319f | 107 | *size = initgr_resp->ngrps + 1; |
f7e7a396 UD |
108 | } |
109 | ||
110 | if (respdata == NULL) | |
111 | { | |
112 | /* Read the data from the socket. */ | |
a334319f UD |
113 | if ((size_t) TEMP_FAILURE_RETRY (__read (sock, *groupsp, |
114 | initgr_resp->ngrps | |
115 | * sizeof (gid_t))) | |
116 | == initgr_resp->ngrps * sizeof (gid_t)) | |
117 | retval = initgr_resp->ngrps; | |
f7e7a396 UD |
118 | } |
119 | else | |
120 | { | |
121 | /* Just copy the data. */ | |
a334319f | 122 | retval = initgr_resp->ngrps; |
f7e7a396 UD |
123 | memcpy (*groupsp, respdata, retval * sizeof (gid_t)); |
124 | } | |
f7e7a396 UD |
125 | } |
126 | else | |
f7fa2b19 UD |
127 | { |
128 | /* No group found yet. */ | |
129 | retval = 0; | |
130 | ||
131 | assert (*size >= 1); | |
132 | } | |
927f0673 UD |
133 | |
134 | /* Check whether GROUP is part of the mix. If not, add it. */ | |
135 | if (retval >= 0) | |
f7e7a396 | 136 | { |
927f0673 UD |
137 | int cnt; |
138 | for (cnt = 0; cnt < retval; ++cnt) | |
139 | if ((*groupsp)[cnt] == group) | |
140 | break; | |
141 | ||
142 | if (cnt == retval) | |
143 | (*groupsp)[retval++] = group; | |
f7e7a396 UD |
144 | } |
145 | ||
146 | if (sock != -1) | |
147 | close_not_cancel_no_status (sock); | |
148 | out: | |
a334319f | 149 | if (__nscd_drop_map_ref (mapped, &gc_cycle) != 0 && retval != -1) |
0891f970 UD |
150 | { |
151 | /* When we come here this means there has been a GC cycle while we | |
152 | were looking for the data. This means the data might have been | |
153 | inconsistent. Retry if possible. */ | |
a334319f | 154 | if ((gc_cycle & 1) != 0) |
0891f970 UD |
155 | { |
156 | /* nscd is just running gc now. Disable using the mapping. */ | |
a334319f | 157 | __nscd_unmap (mapped); |
0891f970 UD |
158 | mapped = NO_MAPPING; |
159 | } | |
160 | ||
a334319f | 161 | goto retry; |
0891f970 | 162 | } |
f7e7a396 UD |
163 | |
164 | return retval; | |
165 | } |