]>
Commit | Line | Data |
---|---|---|
f7e7a396 | 1 | /* Cache handling for host lookup. |
d4697bc9 | 2 | Copyright (C) 2004-2014 Free Software Foundation, Inc. |
f7e7a396 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2004. | |
5 | ||
43bc8ac6 | 6 | This program is free software; you can redistribute it and/or modify |
2e2efe65 RM |
7 | it under the terms of the GNU General Public License as published |
8 | by the Free Software Foundation; version 2 of the License, or | |
9 | (at your option) any later version. | |
f7e7a396 | 10 | |
43bc8ac6 | 11 | This program is distributed in the hope that it will be useful, |
f7e7a396 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
43bc8ac6 UD |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. | |
f7e7a396 | 15 | |
43bc8ac6 | 16 | You should have received a copy of the GNU General Public License |
59ba27a6 | 17 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
f7e7a396 UD |
18 | |
19 | #include <assert.h> | |
20 | #include <errno.h> | |
21 | #include <grp.h> | |
22 | #include <libintl.h> | |
23 | #include <string.h> | |
24 | #include <time.h> | |
25 | #include <unistd.h> | |
26 | #include <sys/mman.h> | |
eac10791 UD |
27 | |
28 | #include "dbg_log.h" | |
29 | #include "nscd.h" | |
30 | #ifdef HAVE_SENDFILE | |
31 | # include <kernel-features.h> | |
32 | #endif | |
f7e7a396 UD |
33 | |
34 | #include "../nss/nsswitch.h" | |
35 | ||
36 | ||
37 | /* Type of the lookup function. */ | |
38 | typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t, | |
39 | long int *, long int *, | |
40 | gid_t **, long int, int *); | |
41 | ||
42 | ||
43 | static const initgr_response_header notfound = | |
44 | { | |
45 | .version = NSCD_VERSION, | |
46 | .found = 0, | |
47 | .ngrps = 0 | |
48 | }; | |
49 | ||
50 | ||
51 | #include "../grp/compat-initgroups.c" | |
52 | ||
53 | ||
a4c7ea7b | 54 | static time_t |
f7e7a396 | 55 | addinitgroupsX (struct database_dyn *db, int fd, request_header *req, |
20e498bd | 56 | void *key, uid_t uid, struct hashentry *const he, |
f7e7a396 UD |
57 | struct datahead *dh) |
58 | { | |
59 | /* Search for the entry matching the key. Please note that we don't | |
60 | look again in the table whether the dataset is now available. We | |
61 | simply insert it. It does not matter if it is in there twice. The | |
62 | pruning function only will look at the timestamp. */ | |
63 | ||
64 | ||
65 | /* We allocate all data in one memory block: the iov vector, | |
66 | the response header and the dataset itself. */ | |
67 | struct dataset | |
68 | { | |
69 | struct datahead head; | |
70 | initgr_response_header resp; | |
71 | char strdata[0]; | |
72 | } *dataset = NULL; | |
73 | ||
74 | if (__builtin_expect (debug_level > 0, 0)) | |
75 | { | |
76 | if (he == NULL) | |
77 | dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key); | |
78 | else | |
79 | dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key); | |
80 | } | |
81 | ||
82 | static service_user *group_database; | |
b2179107 | 83 | service_user *nip; |
f7e7a396 UD |
84 | int no_more; |
85 | ||
b2179107 | 86 | if (group_database == NULL) |
f7e7a396 | 87 | no_more = __nss_database_lookup ("group", NULL, |
b2179107 AS |
88 | "compat [NOTFOUND=return] files", |
89 | &group_database); | |
90 | else | |
91 | no_more = 0; | |
92 | nip = group_database; | |
f7e7a396 UD |
93 | |
94 | /* We always use sysconf even if NGROUPS_MAX is defined. That way, the | |
95 | limit can be raised in the kernel configuration without having to | |
96 | recompile libc. */ | |
97 | long int limit = __sysconf (_SC_NGROUPS_MAX); | |
98 | ||
99 | long int size; | |
100 | if (limit > 0) | |
101 | /* We limit the size of the intially allocated array. */ | |
102 | size = MIN (limit, 64); | |
103 | else | |
104 | /* No fixed limit on groups. Pick a starting buffer size. */ | |
105 | size = 16; | |
106 | ||
107 | long int start = 0; | |
108 | bool all_tryagain = true; | |
cd248c3f | 109 | bool any_success = false; |
f7e7a396 | 110 | |
e8667ddc | 111 | /* This is temporary memory, we need not (and must not) call |
f7e7a396 UD |
112 | mempool_alloc. */ |
113 | // XXX This really should use alloca. need to change the backends. | |
114 | gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t)); | |
115 | if (__builtin_expect (groups == NULL, 0)) | |
116 | /* No more memory. */ | |
117 | goto out; | |
118 | ||
119 | /* Nothing added yet. */ | |
120 | while (! no_more) | |
121 | { | |
695c4370 | 122 | long int prev_start = start; |
f7e7a396 UD |
123 | enum nss_status status; |
124 | initgroups_dyn_function fct; | |
125 | fct = __nss_lookup_function (nip, "initgroups_dyn"); | |
126 | ||
127 | if (fct == NULL) | |
128 | { | |
129 | status = compat_call (nip, key, -1, &start, &size, &groups, | |
130 | limit, &errno); | |
131 | ||
132 | if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE) | |
133 | break; | |
134 | } | |
135 | else | |
136 | status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups, | |
137 | limit, &errno)); | |
138 | ||
695c4370 UD |
139 | /* Remove duplicates. */ |
140 | long int cnt = prev_start; | |
141 | while (cnt < start) | |
142 | { | |
143 | long int inner; | |
144 | for (inner = 0; inner < prev_start; ++inner) | |
145 | if (groups[inner] == groups[cnt]) | |
146 | break; | |
147 | ||
148 | if (inner < prev_start) | |
149 | groups[cnt] = groups[--start]; | |
150 | else | |
151 | ++cnt; | |
152 | } | |
153 | ||
f7e7a396 UD |
154 | if (status != NSS_STATUS_TRYAGAIN) |
155 | all_tryagain = false; | |
156 | ||
157 | /* This is really only for debugging. */ | |
158 | if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN) | |
159 | __libc_fatal ("illegal status in internal_getgrouplist"); | |
160 | ||
fbbc73b3 UD |
161 | any_success |= status == NSS_STATUS_SUCCESS; |
162 | ||
f7e7a396 UD |
163 | if (status != NSS_STATUS_SUCCESS |
164 | && nss_next_action (nip, status) == NSS_ACTION_RETURN) | |
165 | break; | |
166 | ||
167 | if (nip->next == NULL) | |
168 | no_more = -1; | |
169 | else | |
170 | nip = nip->next; | |
171 | } | |
172 | ||
306dfba9 | 173 | bool all_written; |
f7e7a396 | 174 | ssize_t total; |
a4c7ea7b | 175 | time_t timeout; |
f7e7a396 | 176 | out: |
306dfba9 | 177 | all_written = true; |
a4c7ea7b | 178 | timeout = MAX_TIMEOUT_VALUE; |
fbbc73b3 | 179 | if (!any_success) |
f7e7a396 UD |
180 | { |
181 | /* Nothing found. Create a negative result record. */ | |
306dfba9 | 182 | total = sizeof (notfound); |
f7e7a396 UD |
183 | |
184 | if (he != NULL && all_tryagain) | |
185 | { | |
186 | /* If we have an old record available but cannot find one now | |
187 | because the service is not available we keep the old record | |
188 | and make sure it does not get removed. */ | |
189 | if (reload_count != UINT_MAX && dh->nreloads == reload_count) | |
190 | /* Do not reset the value if we never not reload the record. */ | |
191 | dh->nreloads = reload_count - 1; | |
a4c7ea7b UD |
192 | |
193 | /* Reload with the same time-to-live value. */ | |
194 | timeout = dh->timeout = time (NULL) + db->postimeout; | |
f7e7a396 UD |
195 | } |
196 | else | |
197 | { | |
198 | /* We have no data. This means we send the standard reply for this | |
199 | case. */ | |
306dfba9 AS |
200 | if (fd != -1 |
201 | && TEMP_FAILURE_RETRY (send (fd, ¬found, total, | |
202 | MSG_NOSIGNAL)) != total) | |
203 | all_written = false; | |
f7e7a396 | 204 | |
3e1aa84e UD |
205 | /* If we have a transient error or cannot permanently store |
206 | the result, so be it. */ | |
207 | if (all_tryagain || __builtin_expect (db->negtimeout == 0, 0)) | |
445b4a53 TK |
208 | { |
209 | /* Mark the old entry as obsolete. */ | |
210 | if (dh != NULL) | |
211 | dh->usable = false; | |
212 | } | |
99231d9a UD |
213 | else if ((dataset = mempool_alloc (db, (sizeof (struct dataset) |
214 | + req->key_len), 1)) != NULL) | |
f7e7a396 UD |
215 | { |
216 | dataset->head.allocsize = sizeof (struct dataset) + req->key_len; | |
217 | dataset->head.recsize = total; | |
218 | dataset->head.notfound = true; | |
219 | dataset->head.nreloads = 0; | |
220 | dataset->head.usable = true; | |
221 | ||
222 | /* Compute the timeout time. */ | |
a4c7ea7b | 223 | timeout = dataset->head.timeout = time (NULL) + db->negtimeout; |
f7e7a396 UD |
224 | |
225 | /* This is the reply. */ | |
226 | memcpy (&dataset->resp, ¬found, total); | |
227 | ||
228 | /* Copy the key data. */ | |
229 | char *key_copy = memcpy (dataset->strdata, key, req->key_len); | |
230 | ||
231 | /* If necessary, we also propagate the data to disk. */ | |
232 | if (db->persistent) | |
233 | { | |
234 | // XXX async OK? | |
235 | uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1; | |
236 | msync ((void *) pval, | |
237 | ((uintptr_t) dataset & pagesize_m1) | |
238 | + sizeof (struct dataset) + req->key_len, MS_ASYNC); | |
239 | } | |
240 | ||
7e71e55f | 241 | (void) cache_add (req->type, key_copy, req->key_len, |
528741cb | 242 | &dataset->head, true, db, uid, he == NULL); |
f7e7a396 | 243 | |
00ebd7ed UD |
244 | pthread_rwlock_unlock (&db->lock); |
245 | ||
f7e7a396 UD |
246 | /* Mark the old entry as obsolete. */ |
247 | if (dh != NULL) | |
248 | dh->usable = false; | |
249 | } | |
f7e7a396 UD |
250 | } |
251 | } | |
252 | else | |
253 | { | |
254 | ||
306dfba9 | 255 | total = offsetof (struct dataset, strdata) + start * sizeof (int32_t); |
f7e7a396 UD |
256 | |
257 | /* If we refill the cache, first assume the reconrd did not | |
258 | change. Allocate memory on the cache since it is likely | |
259 | discarded anyway. If it turns out to be necessary to have a | |
260 | new record we can still allocate real memory. */ | |
261 | bool alloca_used = false; | |
262 | dataset = NULL; | |
263 | ||
264 | if (he == NULL) | |
20e498bd UD |
265 | dataset = (struct dataset *) mempool_alloc (db, total + req->key_len, |
266 | 1); | |
f7e7a396 UD |
267 | |
268 | if (dataset == NULL) | |
269 | { | |
270 | /* We cannot permanently add the result in the moment. But | |
271 | we can provide the result as is. Store the data in some | |
272 | temporary memory. */ | |
273 | dataset = (struct dataset *) alloca (total + req->key_len); | |
274 | ||
275 | /* We cannot add this record to the permanent database. */ | |
276 | alloca_used = true; | |
277 | } | |
278 | ||
279 | dataset->head.allocsize = total + req->key_len; | |
280 | dataset->head.recsize = total - offsetof (struct dataset, resp); | |
281 | dataset->head.notfound = false; | |
282 | dataset->head.nreloads = he == NULL ? 0 : (dh->nreloads + 1); | |
283 | dataset->head.usable = true; | |
284 | ||
285 | /* Compute the timeout time. */ | |
a4c7ea7b | 286 | timeout = dataset->head.timeout = time (NULL) + db->postimeout; |
f7e7a396 UD |
287 | |
288 | dataset->resp.version = NSCD_VERSION; | |
289 | dataset->resp.found = 1; | |
290 | dataset->resp.ngrps = start; | |
291 | ||
292 | char *cp = dataset->strdata; | |
293 | ||
294 | /* Copy the GID values. If the size of the types match this is | |
295 | very simple. */ | |
296 | if (sizeof (gid_t) == sizeof (int32_t)) | |
297 | cp = mempcpy (cp, groups, start * sizeof (gid_t)); | |
298 | else | |
299 | { | |
300 | gid_t *gcp = (gid_t *) cp; | |
301 | ||
302 | for (int i = 0; i < start; ++i) | |
303 | *gcp++ = groups[i]; | |
304 | ||
305 | cp = (char *) gcp; | |
306 | } | |
307 | ||
308 | /* Finally the user name. */ | |
309 | memcpy (cp, key, req->key_len); | |
310 | ||
5a337776 UD |
311 | assert (cp == dataset->strdata + total - offsetof (struct dataset, |
312 | strdata)); | |
313 | ||
f7e7a396 UD |
314 | /* Now we can determine whether on refill we have to create a new |
315 | record or not. */ | |
316 | if (he != NULL) | |
317 | { | |
318 | assert (fd == -1); | |
319 | ||
320 | if (total + req->key_len == dh->allocsize | |
321 | && total - offsetof (struct dataset, resp) == dh->recsize | |
322 | && memcmp (&dataset->resp, dh->data, | |
323 | dh->allocsize - offsetof (struct dataset, resp)) == 0) | |
324 | { | |
325 | /* The data has not changed. We will just bump the | |
326 | timeout value. Note that the new record has been | |
327 | allocated on the stack and need not be freed. */ | |
328 | dh->timeout = dataset->head.timeout; | |
329 | ++dh->nreloads; | |
330 | } | |
331 | else | |
332 | { | |
333 | /* We have to create a new record. Just allocate | |
334 | appropriate memory and copy it. */ | |
335 | struct dataset *newp | |
c52137d3 | 336 | = (struct dataset *) mempool_alloc (db, total + req->key_len, |
20e498bd | 337 | 1); |
f7e7a396 UD |
338 | if (newp != NULL) |
339 | { | |
340 | /* Adjust pointer into the memory block. */ | |
341 | cp = (char *) newp + (cp - (char *) dataset); | |
342 | ||
343 | dataset = memcpy (newp, dataset, total + req->key_len); | |
344 | alloca_used = false; | |
345 | } | |
346 | ||
347 | /* Mark the old record as obsolete. */ | |
348 | dh->usable = false; | |
349 | } | |
350 | } | |
351 | else | |
352 | { | |
353 | /* We write the dataset before inserting it to the database | |
354 | since while inserting this thread might block and so would | |
355 | unnecessarily let the receiver wait. */ | |
356 | assert (fd != -1); | |
357 | ||
eac10791 | 358 | #ifdef HAVE_SENDFILE |
74158740 | 359 | if (__builtin_expect (db->mmap_used, 1) && !alloca_used) |
eac10791 UD |
360 | { |
361 | assert (db->wr_fd != -1); | |
362 | assert ((char *) &dataset->resp > (char *) db->data); | |
ea547a1a | 363 | assert ((char *) dataset - (char *) db->head |
eac10791 UD |
364 | + total |
365 | <= (sizeof (struct database_pers_head) | |
366 | + db->head->module * sizeof (ref_t) | |
367 | + db->head->data_size)); | |
306dfba9 AS |
368 | ssize_t written = sendfileall (fd, db->wr_fd, |
369 | (char *) &dataset->resp | |
370 | - (char *) db->head, | |
371 | dataset->head.recsize); | |
372 | if (written != dataset->head.recsize) | |
373 | { | |
eac10791 | 374 | # ifndef __ASSUME_SENDFILE |
306dfba9 AS |
375 | if (written == -1 && errno == ENOSYS) |
376 | goto use_write; | |
eac10791 | 377 | # endif |
306dfba9 AS |
378 | all_written = false; |
379 | } | |
eac10791 UD |
380 | } |
381 | else | |
382 | # ifndef __ASSUME_SENDFILE | |
383 | use_write: | |
384 | # endif | |
385 | #endif | |
306dfba9 AS |
386 | if (writeall (fd, &dataset->resp, dataset->head.recsize) |
387 | != dataset->head.recsize) | |
388 | all_written = false; | |
f7e7a396 UD |
389 | } |
390 | ||
391 | ||
392 | /* Add the record to the database. But only if it has not been | |
393 | stored on the stack. */ | |
394 | if (! alloca_used) | |
395 | { | |
396 | /* If necessary, we also propagate the data to disk. */ | |
397 | if (db->persistent) | |
398 | { | |
399 | // XXX async OK? | |
400 | uintptr_t pval = (uintptr_t) dataset & ~pagesize_m1; | |
401 | msync ((void *) pval, | |
402 | ((uintptr_t) dataset & pagesize_m1) + total + | |
403 | req->key_len, MS_ASYNC); | |
404 | } | |
405 | ||
7e71e55f | 406 | (void) cache_add (INITGROUPS, cp, req->key_len, &dataset->head, true, |
528741cb | 407 | db, uid, he == NULL); |
00ebd7ed UD |
408 | |
409 | pthread_rwlock_unlock (&db->lock); | |
f7e7a396 UD |
410 | } |
411 | } | |
412 | ||
413 | free (groups); | |
414 | ||
306dfba9 | 415 | if (__builtin_expect (!all_written, 0) && debug_level > 0) |
f7e7a396 UD |
416 | { |
417 | char buf[256]; | |
418 | dbg_log (_("short write in %s: %s"), __FUNCTION__, | |
419 | strerror_r (errno, buf, sizeof (buf))); | |
420 | } | |
a4c7ea7b UD |
421 | |
422 | return timeout; | |
f7e7a396 UD |
423 | } |
424 | ||
425 | ||
426 | void | |
427 | addinitgroups (struct database_dyn *db, int fd, request_header *req, void *key, | |
428 | uid_t uid) | |
429 | { | |
430 | addinitgroupsX (db, fd, req, key, uid, NULL, NULL); | |
431 | } | |
432 | ||
433 | ||
a4c7ea7b | 434 | time_t |
f7e7a396 UD |
435 | readdinitgroups (struct database_dyn *db, struct hashentry *he, |
436 | struct datahead *dh) | |
437 | { | |
438 | request_header req = | |
439 | { | |
440 | .type = INITGROUPS, | |
441 | .key_len = he->len | |
442 | }; | |
443 | ||
a4c7ea7b | 444 | return addinitgroupsX (db, -1, &req, db->data + he->key, he->owner, he, dh); |
f7e7a396 | 445 | } |