diff options
| author | Ulrich Drepper <drepper@redhat.com> | 2004-09-30 02:24:51 +0000 |
|---|---|---|
| committer | Ulrich Drepper <drepper@redhat.com> | 2004-09-30 02:24:51 +0000 |
| commit | f7e7a396812f5e6a6ec49ad84204d334630964a0 (patch) | |
| tree | d1a47d6911b5992497cc8aa30d392e7c0f175e12 | |
| parent | 04c785b27fd35c740cc9b759b324adb8d6ab9747 (diff) | |
| download | glibc-f7e7a396812f5e6a6ec49ad84204d334630964a0.tar.xz glibc-f7e7a396812f5e6a6ec49ad84204d334630964a0.zip | |
Update.
* grp/initgroups.c: Move compat_call implementation...
* grp/compat-initgroups.c: ...to here. New file.
* grp/Makefile (distribute): Add compat-initgroups.c.
(CFLAGS-initgroups.c): Add -DUSE_NSCD=1.
* mscd/initgrcache.c: New file.
* nscd/nscd_initgroups.c: New file.
* nscd/Makefile (routines): Add nscd_initgroups.
(nscd-modules): Add initgrcache.
* nscd/cache.c (prune_cache): Add support for INITGROUPS entries.
* nscd/connections.c: Handle INITGROUPS requests.
* nscd/nscd-client.h: Define INITGROUPS, initgr_response_header.
Add initgrdata element to struct datahead. Fix typo in comment.
* nscd/nscd_proto.h: Declare __nscd_getgrouplist. Fix parameter
type in __nscd_getgrgrid_r.
* nscd/selinux.c (perms): Add INITGROUPS entry.
* nscd/nscd_getai.c: No need to include <sys/mman.h>.
* sunrpc/get_myaddr.c (get_myaddress): Account for interfaces without
assigned addresses.
* sunrpc/pmap_clnt.c (__get_myaddress): Likewise.
* sunrpc/pmap_rmt.c (getbroadcastnets): Likewise.
* sunrpc/clnt_udp.c (is_network_up): Likewise.
| -rw-r--r-- | ChangeLog | 24 | ||||
| -rw-r--r-- | grp/Makefile | 4 | ||||
| -rw-r--r-- | grp/compat-initgroups.c | 98 | ||||
| -rw-r--r-- | grp/initgroups.c | 119 | ||||
| -rw-r--r-- | nscd/Makefile | 4 | ||||
| -rw-r--r-- | nscd/cache.c | 4 | ||||
| -rw-r--r-- | nscd/connections.c | 12 | ||||
| -rw-r--r-- | nscd/initgrcache.c | 391 | ||||
| -rw-r--r-- | nscd/nscd-client.h | 13 | ||||
| -rw-r--r-- | nscd/nscd_getai.c | 1 | ||||
| -rw-r--r-- | nscd/nscd_initgroups.c | 158 | ||||
| -rw-r--r-- | nscd/nscd_proto.h | 5 | ||||
| -rw-r--r-- | nscd/selinux.c | 3 | ||||
| -rw-r--r-- | sunrpc/clnt_udp.c | 1 | ||||
| -rw-r--r-- | sunrpc/get_myaddr.c | 4 | ||||
| -rw-r--r-- | sunrpc/pmap_clnt.c | 4 | ||||
| -rw-r--r-- | sunrpc/pmap_rmt.c | 1 |
17 files changed, 738 insertions, 108 deletions
@@ -1,5 +1,29 @@ 2004-09-29 Ulrich Drepper <drepper@redhat.com> + * grp/initgroups.c: Move compat_call implementation... + * grp/compat-initgroups.c: ...to here. New file. + * grp/Makefile (distribute): Add compat-initgroups.c. + (CFLAGS-initgroups.c): Add -DUSE_NSCD=1. + * mscd/initgrcache.c: New file. + * nscd/nscd_initgroups.c: New file. + * nscd/Makefile (routines): Add nscd_initgroups. + (nscd-modules): Add initgrcache. + * nscd/cache.c (prune_cache): Add support for INITGROUPS entries. + * nscd/connections.c: Handle INITGROUPS requests. + * nscd/nscd-client.h: Define INITGROUPS, initgr_response_header. + Add initgrdata element to struct datahead. Fix typo in comment. + * nscd/nscd_proto.h: Declare __nscd_getgrouplist. Fix parameter + type in __nscd_getgrgrid_r. + * nscd/selinux.c (perms): Add INITGROUPS entry. + + * nscd/nscd_getai.c: No need to include <sys/mman.h>. + + * sunrpc/get_myaddr.c (get_myaddress): Account for interfaces without + assigned addresses. + * sunrpc/pmap_clnt.c (__get_myaddress): Likewise. + * sunrpc/pmap_rmt.c (getbroadcastnets): Likewise. + * sunrpc/clnt_udp.c (is_network_up): Likewise. + * nscd/nscd.c: Define getaddrinfo hidden so that it is never found outside. diff --git a/grp/Makefile b/grp/Makefile index 54e86394ba..e6ca864a49 100644 --- a/grp/Makefile +++ b/grp/Makefile @@ -22,7 +22,7 @@ subdir := grp headers := grp.h -distribute := tst_fgetgrent.c tst_fgetgrent.sh +distribute := tst_fgetgrent.c tst_fgetgrent.sh compat-initgroups.c routines := fgetgrent initgroups setgroups \ getgrent getgrgid getgrnam putgrent \ @@ -54,7 +54,7 @@ CFLAGS-getgrent.c = -fexceptions CFLAGS-fgetgrent.c = -fexceptions CFLAGS-fgetgrent_r.c = -fexceptions -D_IO_MTSAFE_IO CFLAGS-putgrent.c = -fexceptions -D_IO_MTSAFE_IO -CFLAGS-initgroups.c = -fexceptions +CFLAGS-initgroups.c = -DUSE_NSCD=1 -fexceptions CFLAGS-getgrgid.c = -fexceptions endif diff --git a/grp/compat-initgroups.c b/grp/compat-initgroups.c new file mode 100644 index 0000000000..585c4aecbb --- /dev/null +++ b/grp/compat-initgroups.c @@ -0,0 +1,98 @@ +/* Prototype for the setgrent functions we use here. */ +typedef enum nss_status (*set_function) (void); + +/* Prototype for the endgrent functions we use here. */ +typedef enum nss_status (*end_function) (void); + +/* Prototype for the setgrent functions we use here. */ +typedef enum nss_status (*get_function) (struct group *, char *, + size_t, int *); + +static enum nss_status +compat_call (service_user *nip, const char *user, gid_t group, long int *start, + long int *size, gid_t **groupsp, long int limit, int *errnop) +{ + struct group grpbuf; + size_t buflen = __sysconf (_SC_GETGR_R_SIZE_MAX); + char *tmpbuf; + enum nss_status status; + set_function setgrent_fct; + get_function getgrent_fct; + end_function endgrent_fct; + gid_t *groups = *groupsp; + + getgrent_fct = __nss_lookup_function (nip, "getgrent_r"); + if (getgrent_fct == NULL) + return NSS_STATUS_UNAVAIL; + + setgrent_fct = __nss_lookup_function (nip, "setgrent"); + if (setgrent_fct) + { + status = DL_CALL_FCT (setgrent_fct, ()); + if (status != NSS_STATUS_SUCCESS) + return status; + } + + endgrent_fct = __nss_lookup_function (nip, "endgrent"); + + tmpbuf = __alloca (buflen); + + do + { + while ((status = DL_CALL_FCT (getgrent_fct, + (&grpbuf, tmpbuf, buflen, errnop)), + status == NSS_STATUS_TRYAGAIN) + && *errnop == ERANGE) + { + buflen *= 2; + tmpbuf = __alloca (buflen); + } + + if (status != NSS_STATUS_SUCCESS) + goto done; + + if (grpbuf.gr_gid != group) + { + char **m; + + for (m = grpbuf.gr_mem; *m != NULL; ++m) + if (strcmp (*m, user) == 0) + { + /* Matches user. Insert this group. */ + if (__builtin_expect (*start == *size, 0)) + { + /* Need a bigger buffer. */ + gid_t *newgroups; + long int newsize; + + if (limit > 0 && *size == limit) + /* We reached the maximum. */ + goto done; + + if (limit <= 0) + newsize = 2 * *size; + else + newsize = MIN (limit, 2 * *size); + + newgroups = realloc (groups, newsize * sizeof (*groups)); + if (newgroups == NULL) + goto done; + *groupsp = groups = newgroups; + *size = newsize; + } + + groups[*start] = grpbuf.gr_gid; + *start += 1; + + break; + } + } + } + while (status == NSS_STATUS_SUCCESS); + + done: + if (endgrent_fct) + DL_CALL_FCT (endgrent_fct, ()); + + return NSS_STATUS_SUCCESS; +} diff --git a/grp/initgroups.c b/grp/initgroups.c index 858dd314e5..9e79273f3e 100644 --- a/grp/initgroups.c +++ b/grp/initgroups.c @@ -27,19 +27,14 @@ #include <sys/types.h> #include <nsswitch.h> +#include "../nscd/nscd-client.h" +#include "../nscd/nscd_proto.h" + + /* Type of the lookup function. */ typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t, long int *, long int *, gid_t **, long int, int *); -/* Prototype for the setgrent functions we use here. */ -typedef enum nss_status (*set_function) (void); - -/* Prototype for the endgrent functions we use here. */ -typedef enum nss_status (*end_function) (void); - -/* Prototype for the setgrent functions we use here. */ -typedef enum nss_status (*get_function) (struct group *, char *, - size_t, int *); /* The lookup function for the first entry of this service. */ extern int __nss_group_lookup (service_user **nip, const char *name, @@ -48,99 +43,29 @@ extern void *__nss_lookup_function (service_user *ni, const char *fct_name); extern service_user *__nss_group_database attribute_hidden; -static enum nss_status -compat_call (service_user *nip, const char *user, gid_t group, long int *start, - long int *size, gid_t **groupsp, long int limit, int *errnop) -{ - struct group grpbuf; - size_t buflen = __sysconf (_SC_GETGR_R_SIZE_MAX); - char *tmpbuf; - enum nss_status status; - set_function setgrent_fct; - get_function getgrent_fct; - end_function endgrent_fct; - gid_t *groups = *groupsp; - - getgrent_fct = __nss_lookup_function (nip, "getgrent_r"); - if (getgrent_fct == NULL) - return NSS_STATUS_UNAVAIL; - - setgrent_fct = __nss_lookup_function (nip, "setgrent"); - if (setgrent_fct) - { - status = DL_CALL_FCT (setgrent_fct, ()); - if (status != NSS_STATUS_SUCCESS) - return status; - } - endgrent_fct = __nss_lookup_function (nip, "endgrent"); +#include "compat-initgroups.c" - tmpbuf = __alloca (buflen); - - do - { - while ((status = DL_CALL_FCT (getgrent_fct, - (&grpbuf, tmpbuf, buflen, errnop)), - status == NSS_STATUS_TRYAGAIN) - && *errnop == ERANGE) - { - buflen *= 2; - tmpbuf = __alloca (buflen); - } - - if (status != NSS_STATUS_SUCCESS) - goto done; - - if (grpbuf.gr_gid != group) - { - char **m; - - for (m = grpbuf.gr_mem; *m != NULL; ++m) - if (strcmp (*m, user) == 0) - { - /* Matches user. Insert this group. */ - if (__builtin_expect (*start == *size, 0)) - { - /* Need a bigger buffer. */ - gid_t *newgroups; - long int newsize; - - if (limit > 0 && *size == limit) - /* We reached the maximum. */ - goto done; - - if (limit <= 0) - newsize = 2 * *size; - else - newsize = MIN (limit, 2 * *size); - - newgroups = realloc (groups, newsize * sizeof (*groups)); - if (newgroups == NULL) - goto done; - *groupsp = groups = newgroups; - *size = newsize; - } - - groups[*start] = grpbuf.gr_gid; - *start += 1; - - break; - } - } - } - while (status == NSS_STATUS_SUCCESS); - - done: - if (endgrent_fct) - DL_CALL_FCT (endgrent_fct, ()); - - return NSS_STATUS_SUCCESS; -} static int internal_getgrouplist (const char *user, gid_t group, long int *size, gid_t **groupsp, long int limit) { +#ifdef USE_NSCD + if (__nss_not_use_nscd_group > 0 + && ++__nss_not_use_nscd_group > NSS_NSCD_RETRY) + __nss_not_use_nscd_group = 0; + if (!__nss_not_use_nscd_group) + { + int n = __nscd_getgrouplist (user, group, size, groupsp, limit); + if (n >= 0) + return n; + + /* nscd is not usable. */ + __nss_not_use_nscd_group = 1; + } +#endif + service_user *nip = NULL; initgroups_dyn_function fct; enum nss_status status = NSS_STATUS_UNAVAIL; @@ -205,6 +130,10 @@ getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups) newgroups = (gid_t *) malloc (size * sizeof (gid_t)); if (__builtin_expect (newgroups == NULL, 0)) /* No more memory. */ + // XXX This is wrong. The user provided memory, we have to use + // XXX it. The internal functions must be called with the user + // XXX provided buffer and not try to increase the size if it is + // XXX too small. For initgroups a flag could say: increase size. return -1; result = internal_getgrouplist (user, group, &size, &newgroups, -1); diff --git a/nscd/Makefile b/nscd/Makefile index d24bf9c2a6..bf79606f07 100644 --- a/nscd/Makefile +++ b/nscd/Makefile @@ -21,7 +21,7 @@ # subdir := nscd -routines := nscd_getpw_r nscd_getgr_r nscd_gethst_r nscd_getai +routines := nscd_getpw_r nscd_getgr_r nscd_gethst_r nscd_getai nscd_initgroups aux := nscd_helper include ../Makeconfig @@ -32,7 +32,7 @@ vpath %.c ../locale/programs nscd-modules := nscd connections pwdcache getpwnam_r getpwuid_r grpcache \ getgrnam_r getgrgid_r hstcache gethstbyad_r gethstbynm2_r \ dbg_log nscd_conf nscd_stat cache mem nscd_setup_thread \ - xmalloc xstrdup aicache + xmalloc xstrdup aicache initgrcache ifeq ($(have-thread-library),yes) diff --git a/nscd/cache.c b/nscd/cache.c index 468b8d272a..efac4b3bcc 100644 --- a/nscd/cache.c +++ b/nscd/cache.c @@ -317,6 +317,10 @@ prune_cache (struct database_dyn *table, time_t now) readdhstai (table, runp, dh); break; + case INITGROUPS: + readdinitgroups (table, runp, dh); + break; + default: assert (! "should never happen"); } diff --git a/nscd/connections.c b/nscd/connections.c index 164fdfbc84..5de7c3b331 100644 --- a/nscd/connections.c +++ b/nscd/connections.c @@ -85,7 +85,8 @@ const char *serv2str[LASTREQ] = [GETFDPW] = "GETFDPW", [GETFDGR] = "GETFDGR", [GETFDHST] = "GETFDHST", - [GETAI] = "GETAI" + [GETAI] = "GETAI", + [INITGROUPS] = "INITGROUPS" }; /* The control data structures for the services. */ @@ -154,6 +155,7 @@ static struct database_dyn *const serv2db[LASTREQ] = [GETFDGR] = &dbs[grpdb], [GETFDHST] = &dbs[hstdb], [GETAI] = &dbs[hstdb], + [INITGROUPS] = &dbs[grpdb] }; @@ -604,9 +606,11 @@ cannot handle old request version %d; current version is %d"), struct database_dyn *db = serv2db[req->type]; + // XXX Clean up so that each new command need not introduce a + // XXX new conditional. if ((__builtin_expect (req->type, GETPWBYNAME) >= GETPWBYNAME && __builtin_expect (req->type, LASTDBREQ) <= LASTDBREQ) - || req->type == GETAI) + || req->type == GETAI || req->type == INITGROUPS) { if (__builtin_expect (debug_level, 0) > 0) { @@ -719,6 +723,10 @@ cannot handle old request version %d; current version is %d"), addhstai (db, fd, req, key, uid); break; + case INITGROUPS: + addinitgroups (db, fd, req, key, uid); + break; + case GETSTAT: case SHUTDOWN: case INVALIDATE: diff --git a/nscd/initgrcache.c b/nscd/initgrcache.c new file mode 100644 index 0000000000..124b4bae62 --- /dev/null +++ b/nscd/initgrcache.c @@ -0,0 +1,391 @@ +/* Cache handling for host lookup. + Copyright (C) 2004 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper <drepper@redhat.com>, 2004. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include <assert.h> +#include <errno.h> +#include <grp.h> +#include <libintl.h> +#include <string.h> +#include <time.h> +#include <unistd.h> +#include <sys/mman.h> +#include <dbg_log.h> +#include <nscd.h> + +#include "../nss/nsswitch.h" + + +/* Type of the lookup function. */ +typedef enum nss_status (*initgroups_dyn_function) (const char *, gid_t, + long int *, long int *, + gid_t **, long int, int *); + + +static const initgr_response_header notfound = +{ + .version = NSCD_VERSION, + .found = 0, + .ngrps = 0 +}; + + +#include "../grp/compat-initgroups.c" + + +static void +addinitgroupsX (struct database_dyn *db, int fd, request_header *req, + void *key, uid_t uid, struct hashentry *he, + struct datahead *dh) +{ + /* Search for the entry matching the key. Please note that we don't + look again in the table whether the dataset is now available. We + simply insert it. It does not matter if it is in there twice. The + pruning function only will look at the timestamp. */ + + + /* We allocate all data in one memory block: the iov vector, + the response header and the dataset itself. */ + struct dataset + { + struct datahead head; + initgr_response_header resp; + char strdata[0]; + } *dataset = NULL; + + if (__builtin_expect (debug_level > 0, 0)) + { + if (he == NULL) + dbg_log (_("Haven't found \"%s\" in group cache!"), (char *) key); + else + dbg_log (_("Reloading \"%s\" in group cache!"), (char *) key); + } + + static service_user *group_database; + service_user *nip = NULL; + int no_more; + + if (group_database != NULL) + { + nip = group_database; + no_more = 0; + } + else + no_more = __nss_database_lookup ("group", NULL, + "compat [NOTFOUND=return] files", &nip); + + /* We always use sysconf even if NGROUPS_MAX is defined. That way, the + limit can be raised in the kernel configuration without having to + recompile libc. */ + long int limit = __sysconf (_SC_NGROUPS_MAX); + + long int size; + if (limit > 0) + /* We limit the size of the intially allocated array. */ + size = MIN (limit, 64); + else + /* No fixed limit on groups. Pick a starting buffer size. */ + size = 16; + + long int start = 0; + bool all_tryagain = true; + + /* This is temporary memory, we need not (ad must not) call + mempool_alloc. */ + // XXX This really should use alloca. need to change the backends. + gid_t *groups = (gid_t *) malloc (size * sizeof (gid_t)); + if (__builtin_expect (groups == NULL, 0)) + /* No more memory. */ + goto out; + + /* Nothing added yet. */ + while (! no_more) + { + enum nss_status status; + initgroups_dyn_function fct; + fct = __nss_lookup_function (nip, "initgroups_dyn"); + + if (fct == NULL) + { + status = compat_call (nip, key, -1, &start, &size, &groups, + limit, &errno); + + if (nss_next_action (nip, NSS_STATUS_UNAVAIL) != NSS_ACTION_CONTINUE) + break; + } + else + status = DL_CALL_FCT (fct, (key, -1, &start, &size, &groups, + limit, &errno)); + + if (status != NSS_STATUS_TRYAGAIN) + all_tryagain = false; + + /* This is really only for debugging. */ + if (NSS_STATUS_TRYAGAIN > status || status > NSS_STATUS_RETURN) + __libc_fatal ("illegal status in internal_getgrouplist"); + + if (status != NSS_STATUS_SUCCESS + && nss_next_action (nip, status) == NSS_ACTION_RETURN) + break; + + if (nip->next == NULL) + no_more = -1; + else + nip = nip->next; + } + + ssize_t total; + ssize_t written; + out: + if (start == 0) + { + /* Nothing found. Create a negative result record. */ + written = total = sizeof (notfound); + + if (he != NULL && all_tryagain) + { + /* If we have an old record available but cannot find one now + because the service is not available we keep the old record + and make sure it does not get removed. */ + if (reload_count != UINT_MAX && dh->nreloads == reload_count) + /* Do not reset the value if we never not reload the record. */ + dh->nreloads = reload_count - 1; + } + else + { + /* We have no data. This means we send the standard reply for this + case. */ + if (fd != -1) + written = TEMP_FAILURE_RETRY (write (fd, ¬found, total)); + + dataset = mempool_alloc (db, sizeof (struct dataset) + req->key_len); + /* If we cannot permanently store the result, so be it. */ + if (dataset != NULL) + { + dataset->head.allocsize = sizeof (struct dataset) + req->key_len; + dataset->head.recsize = total; + dataset->head.notfound = true; + dataset->head.nreloads = 0; + dataset->head.usable = true; + + /* Compute the timeout time. */ + dataset->head.timeout = time (NULL) + db->negtimeout; + + /* Th |
