From 68361572527e50a1e8d21f2898faa3809181c946 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Fri, 6 Oct 2006 17:49:27 +0000 Subject: [PATCH] * nis/nis_table.c (nis_list): Handle FOLLOW_PATH | ALL_RESULTS when callback is NULL. * nis/Versions (libnss_nisplus): Add _nss_nisplus_initgroups_dyn@@GLIBC_PRIVATE. * nis/Makefile (libnss_nisplus-routines): Add nisplus-initgroups. * nis/nss_nisplus/nisplus-grp.c (tablename_val, tablename_len, _nss_create_tablename): Rename to... (grp_tablename_val, grp_tablename_len, _nss_grp_create_tablename): ... these. No longer static. (internal_setgrent): Adjust users. (_nss_nisplus_getgrnam_r, _nss_nisplus_getgrgid_r): Likewise. Don't use locking around _nss_grp_create_tablename call. * nis/nss_nisplus/nisplus-initgroups.c: New file. --- ChangeLog | 17 +++ nis/Makefile | 2 +- nis/Versions | 2 +- nis/nis_table.c | 56 ++++++++++ nis/nss_nisplus/nisplus-grp.c | 51 +++++---- nis/nss_nisplus/nisplus-initgroups.c | 150 +++++++++++++++++++++++++++ 6 files changed, 249 insertions(+), 29 deletions(-) create mode 100644 nis/nss_nisplus/nisplus-initgroups.c diff --git a/ChangeLog b/ChangeLog index c600f26444..21c329e066 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +2006-09-27 Jakub Jelinek + + * nis/nis_table.c (nis_list): Handle FOLLOW_PATH | ALL_RESULTS + when callback is NULL. + + * nis/Versions (libnss_nisplus): Add + _nss_nisplus_initgroups_dyn@@GLIBC_PRIVATE. + * nis/Makefile (libnss_nisplus-routines): Add nisplus-initgroups. + * nis/nss_nisplus/nisplus-grp.c (tablename_val, tablename_len, + _nss_create_tablename): Rename to... + (grp_tablename_val, grp_tablename_len, _nss_grp_create_tablename): + ... these. No longer static. + (internal_setgrent): Adjust users. + (_nss_nisplus_getgrnam_r, _nss_nisplus_getgrgid_r): Likewise. + Don't use locking around _nss_grp_create_tablename call. + * nis/nss_nisplus/nisplus-initgroups.c: New file. + 2006-10-06 Ulrich Drepper * version.h (VERSION): Bump to 2.5.90 for new development tree. diff --git a/nis/Makefile b/nis/Makefile index c912a2d824..8083ee8044 100644 --- a/nis/Makefile +++ b/nis/Makefile @@ -64,7 +64,7 @@ libnss_nis-routines := $(addprefix nis-,$(databases)) nis-initgroups \ libnss_nis-inhibit-o = $(filter-out .os,$(object-suffixes)) libnss_nisplus-routines := $(addprefix nisplus-,$(databases)) nisplus-parser \ - nss-nisplus + nss-nisplus nisplus-initgroups libnss_nisplus-inhibit-o = $(filter-out .os,$(object-suffixes)) include ../Rules diff --git a/nis/Versions b/nis/Versions index c7d87bd4ee..be4453e285 100644 --- a/nis/Versions +++ b/nis/Versions @@ -125,6 +125,6 @@ libnss_nisplus { _nss_nisplus_setetherent; _nss_nisplus_setgrent; _nss_nisplus_sethostent; _nss_nisplus_setnetent; _nss_nisplus_setnetgrent; _nss_nisplus_setprotoent; _nss_nisplus_setpwent; _nss_nisplus_setrpcent; _nss_nisplus_setservent; - _nss_nisplus_setspent; + _nss_nisplus_setspent; _nss_nisplus_initgroups_dyn; } } diff --git a/nis/nis_table.c b/nis/nis_table.c index a92a4b64ce..f898e3c66c 100644 --- a/nis/nis_table.c +++ b/nis/nis_table.c @@ -215,6 +215,7 @@ nis_list (const_nis_name name, unsigned int flags, char *tableptr; char *tablepath = NULL; int first_try = 0; /* Do we try the old binding at first ? */ + nis_result *allres = NULL; if (res == NULL) return NULL; @@ -223,6 +224,7 @@ nis_list (const_nis_name name, unsigned int flags, { status = NIS_BADNAME; err_out: + nis_freeresult (allres); memset (res, '\0', sizeof (nis_result)); NIS_RES_STATUS (res) = status; return res; @@ -349,6 +351,7 @@ nis_list (const_nis_name name, unsigned int flags, if (names != namebuf) nis_freenames (names); nis_free_request (ibreq); + nis_freeresult (allres); return res; } if (NIS_RES_OBJECT (res)->LI_data.li_attrs.li_attrs_len) @@ -392,6 +395,53 @@ nis_list (const_nis_name name, unsigned int flags, goto again; } } + else if ((flags & (FOLLOW_PATH | ALL_RESULTS)) + == (FOLLOW_PATH | ALL_RESULTS)) + { + if (allres == NULL) + { + allres = res; + res = malloc (sizeof (nis_result)); + if (res == NULL) + { + res = allres; + allres = NULL; + NIS_RES_STATUS (res) = NIS_NOMEMORY; + goto fail; + } + NIS_RES_STATUS (res) = NIS_RES_STATUS (allres); + } + else + { + nis_object *objects_val + = realloc (NIS_RES_OBJECT (allres), + (NIS_RES_NUMOBJ (allres) + + NIS_RES_NUMOBJ (res)) + * sizeof (nis_object)); + if (objects_val == NULL) + { + NIS_RES_STATUS (res) = NIS_NOMEMORY; + goto fail; + } + NIS_RES_OBJECT (allres) = objects_val; + memcpy (NIS_RES_OBJECT (allres) + NIS_RES_NUMOBJ (allres), + NIS_RES_OBJECT (res), + NIS_RES_NUMOBJ (res) * sizeof (nis_object)); + NIS_RES_NUMOBJ (allres) += NIS_RES_NUMOBJ (res); + NIS_RES_NUMOBJ (res) = 0; + free (NIS_RES_OBJECT (res)); + NIS_RES_OBJECT (res) = NULL; + NIS_RES_STATUS (allres) = NIS_RES_STATUS (res); + xdr_free ((xdrproc_t) _xdr_nis_result, (char *) res); + } + clnt_status = __follow_path (&tablepath, &tableptr, ibreq, + &bptr); + if (clnt_status != NIS_SUCCESS) + { + NIS_RES_STATUS (res) = clnt_status; + ++done; + } + } else ++done; break; @@ -485,6 +535,12 @@ nis_list (const_nis_name name, unsigned int flags, nis_free_request (ibreq); + if (allres) + { + nis_freeresult (res); + return allres; + } + return res; } libnsl_hidden_def (nis_list) diff --git a/nis/nss_nisplus/nisplus-grp.c b/nis/nss_nisplus/nisplus-grp.c index c1adc98aee..7cc762fea9 100644 --- a/nis/nss_nisplus/nisplus-grp.c +++ b/nis/nss_nisplus/nisplus-grp.c @@ -46,13 +46,13 @@ static char *tableptr; static netobj cursor; -static nis_name tablename_val; -static size_t tablename_len; +nis_name grp_tablename_val attribute_hidden; +size_t grp_tablename_len attribute_hidden; -static enum nss_status -_nss_create_tablename (int *errnop) +enum nss_status +_nss_grp_create_tablename (int *errnop) { - if (tablename_val == NULL) + if (grp_tablename_val == NULL) { const char *local_dir = nis_local_directory (); size_t local_dir_len = strlen (local_dir); @@ -67,11 +67,16 @@ _nss_create_tablename (int *errnop) memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1); - tablename_len = sizeof (prefix) - 1 + local_dir_len; + grp_tablename_len = sizeof (prefix) - 1 + local_dir_len; atomic_write_barrier (); - tablename_val = p; + if (atomic_compare_and_exchange_bool_acq (&grp_tablename_val, p, NULL)) + { + /* Another thread already installed the value. */ + free (p); + grp_tablename_len = strlen (grp_tablename_val); + } } return NSS_STATUS_SUCCESS; @@ -103,19 +108,19 @@ internal_setgrent (int *errnop) { enum nss_status status = NSS_STATUS_SUCCESS; - if (tablename_val == NULL) - status = _nss_create_tablename (errnop); + if (grp_tablename_val == NULL) + status = _nss_grp_create_tablename (errnop); if (status == NSS_STATUS_SUCCESS) { - ibreq = __create_ib_request (tablename_val, 0); + ibreq = __create_ib_request (grp_tablename_val, 0); if (ibreq == NULL) { *errnop = errno; return NSS_STATUS_TRYAGAIN; } - nis_error retcode = __prepare_niscall (tablename_val, &dir, &bptr, 0); + nis_error retcode = __prepare_niscall (grp_tablename_val, &dir, &bptr, 0); if (retcode != NIS_SUCCESS) { nis_free_request (ibreq); @@ -285,13 +290,9 @@ _nss_nisplus_getgrnam_r (const char *name, struct group *gr, { int parse_res; - if (tablename_val == NULL) + if (grp_tablename_val == NULL) { - __libc_lock_lock (lock); - - enum nss_status status = _nss_create_tablename (errnop); - - __libc_lock_unlock (lock); + enum nss_status status = _nss_grp_create_tablename (errnop); if (status != NSS_STATUS_SUCCESS) return status; @@ -304,10 +305,10 @@ _nss_nisplus_getgrnam_r (const char *name, struct group *gr, } nis_result *result; - char buf[strlen (name) + 9 + tablename_len]; + char buf[strlen (name) + 9 + grp_tablename_len]; int olderr = errno; - snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val); + snprintf (buf, sizeof (buf), "[name=%s],%s", name, grp_tablename_val); result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH, NULL, NULL); @@ -348,13 +349,9 @@ enum nss_status _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr, char *buffer, size_t buflen, int *errnop) { - if (tablename_val == NULL) + if (grp_tablename_val == NULL) { - __libc_lock_lock (lock); - - enum nss_status status = _nss_create_tablename (errnop); - - __libc_lock_unlock (lock); + enum nss_status status = _nss_grp_create_tablename (errnop); if (status != NSS_STATUS_SUCCESS) return status; @@ -362,11 +359,11 @@ _nss_nisplus_getgrgid_r (const gid_t gid, struct group *gr, int parse_res; nis_result *result; - char buf[8 + 3 * sizeof (unsigned long int) + tablename_len]; + char buf[8 + 3 * sizeof (unsigned long int) + grp_tablename_len]; int olderr = errno; snprintf (buf, sizeof (buf), "[gid=%lu],%s", - (unsigned long int) gid, tablename_val); + (unsigned long int) gid, grp_tablename_val); result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL); diff --git a/nis/nss_nisplus/nisplus-initgroups.c b/nis/nss_nisplus/nisplus-initgroups.c new file mode 100644 index 0000000000..6588ec2533 --- /dev/null +++ b/nis/nss_nisplus/nisplus-initgroups.c @@ -0,0 +1,150 @@ +/* Copyright (C) 1997, 2001, 2002, 2003, 2005, 2006 + Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 +#include +#include +#include +#include +#include +#include +#include + +#include "nss-nisplus.h" +#include "nisplus-parser.h" +#include +#include +#include + +#define NISOBJVAL(col, obj) \ + ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val) + +#define NISOBJLEN(col, obj) \ + ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len) + +extern nis_name grp_tablename_val attribute_hidden; +extern size_t grp_tablename_len attribute_hidden; +extern enum nss_status _nss_grp_create_tablename (int *errnop); + + +enum nss_status +_nss_nisplus_initgroups_dyn (const char *user, gid_t group, long int *start, + long int *size, gid_t **groupsp, long int limit, + int *errnop) +{ + if (grp_tablename_val == NULL) + { + enum nss_status status = _nss_grp_create_tablename (errnop); + + if (status != NSS_STATUS_SUCCESS) + return status; + } + + nis_result *result; + char buf[strlen (user) + 12 + grp_tablename_len]; + + snprintf (buf, sizeof (buf), "[members=%s],%s", user, grp_tablename_val); + + result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | ALL_RESULTS, NULL, NULL); + + if (result == NULL) + { + *errnop = ENOMEM; + return NSS_STATUS_TRYAGAIN; + } + + if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0)) + { + enum nss_status status = niserr2nss (result->status); + + nis_freeresult (result); + return status; + } + + if (NIS_RES_NUMOBJ (result) == 0) + { + errout: + nis_freeresult (result); + return NSS_STATUS_NOTFOUND; + } + + gid_t *groups = *groupsp; + nis_object *obj = NIS_RES_OBJECT (result); + for (unsigned int cnt = 0; cnt < NIS_RES_NUMOBJ (result); ++cnt, ++obj) + { + if (__type_of (obj) != NIS_ENTRY_OBJ + || strcmp (obj->EN_data.en_type, "group_tbl") != 0 + || obj->EN_data.en_cols.en_cols_len < 4) + continue; + + char *numstr = NISOBJVAL (2, obj); + size_t len = NISOBJLEN (2, obj); + if (len == 0 || numstr[0] == '\0') + continue; + + gid_t gid; + char *endp; + if (__builtin_expect (numstr[len - 1] != '\0', 0)) + { + char numstrbuf[len + 1]; + memcpy (numstrbuf, numstr, len); + numstrbuf[len] = '\0'; + gid = strtoul (numstrbuf, &endp, 10); + if (*endp) + continue; + } + else + { + gid = strtoul (numstr, &endp, 10); + if (*endp) + continue; + } + + if (gid == group) + continue; + + /* Insert this group. */ + if (*start == *size) + { + /* Need a bigger buffer. */ + long int newsize; + + if (limit > 0 && *size == limit) + /* We reached the maximum. */ + break; + + if (limit <= 0) + newsize = 2 * *size; + else + newsize = MIN (limit, 2 * *size); + + gid_t *newgroups = realloc (groups, newsize * sizeof (*groups)); + if (newgroups == NULL) + goto errout; + *groupsp = groups = newgroups; + *size = newsize; + } + + groups[*start] = gid; + *start += 1; + } + + nis_freeresult (result); + return NSS_STATUS_SUCCESS; +} -- 2.43.5