From 775f7dc3865d6ddfa74d88e77c132f78e0667ac9 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sat, 26 Jun 2010 18:47:45 -0400 Subject: [PATCH] Add the primary nonlocal gid to local users in MAGIC_NONLOCAL_GROUPNAME Signed-off-by: Anders Kaseorg --- README | 4 ++-- nonlocal-group.c | 27 ++++++++++++++++++++++++++- nonlocal-passwd.c | 17 +++++++++++++++++ nonlocal.h | 3 +++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README b/README index 7125dc1..6dc464b 100644 --- a/README +++ b/README @@ -14,8 +14,8 @@ they exist: • If the local group ‘nss-nonlocal-users’ exists, then nonlocal users will be automatically added to it. Furthermore, if a local user is - added to this group, then that user will inherit any nonlocal - supplementary gids from a nonlocal user of the same name. + added to this group, then that user will inherit any nonlocal gids + from a nonlocal user of the same name, as supplementary gids. • If the local group ‘nss-local-users’ exists, then local users will be automatically added to it. diff --git a/nonlocal-group.c b/nonlocal-group.c index aa9c588..8ceeb23 100644 --- a/nonlocal-group.c +++ b/nonlocal-group.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include "nsswitch-internal.h" @@ -42,7 +43,8 @@ * If the MAGIC_NONLOCAL_GROUPNAME local group exists, then nonlocal * users will be automatically added to it. Furthermore, if a local * user is added to this group, then that user will inherit any - * nonlocal supplementary gids from a nonlocal user of the same name. + * nonlocal gids from a nonlocal user of the same name, as + * supplementary gids. */ #define MAGIC_NONLOCAL_GROUPNAME "nss-nonlocal-users" @@ -380,6 +382,29 @@ _nss_nonlocal_initgroups_dyn(const char *user, gid_t group, long int *start, break; } } + + if (is_nonlocal) { + struct passwd pwbuf; + char *buf; + int nonlocal_errno = *errnop; + status = get_nonlocal_passwd(user, &pwbuf, &buf, errnop); + + if (status == NSS_STATUS_SUCCESS) { + nonlocal_errno = *errnop; + status = check_nonlocal_gid(user, pwbuf.pw_gid, + &nonlocal_errno); + free(buf); + } + + if (status == NSS_STATUS_SUCCESS) { + if (!add_group(pwbuf.pw_gid, start, size, groupsp, limit, + errnop, &status)) + return status; + } else if (status == NSS_STATUS_TRYAGAIN) { + *errnop = nonlocal_errno; + return status; + } + } } } else if (status == NSS_STATUS_TRYAGAIN) { if (is_nonlocal) diff --git a/nonlocal-passwd.c b/nonlocal-passwd.c index 052af95..a401c49 100644 --- a/nonlocal-passwd.c +++ b/nonlocal-passwd.c @@ -143,6 +143,23 @@ check_nonlocal_user(const char *user, int *errnop) return status; } +enum nss_status +get_nonlocal_passwd(const char *name, struct passwd *pwd, char **buffer, + int *errnop) +{ + enum nss_status status; + size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); + const struct walk_nss w = { + .lookup = __nss_passwd_nonlocal_lookup, .fct_name = "getpwnam_r", + .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen + }; + const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL; +#define args (name, pwd, *buffer, buflen, errnop) +#include "walk_nss.h" +#undef args + return status; +} + static service_user *pwent_startp, *pwent_nip; static void *pwent_fct_start; diff --git a/nonlocal.h b/nonlocal.h index b31a9a9..da9294c 100644 --- a/nonlocal.h +++ b/nonlocal.h @@ -45,6 +45,7 @@ typedef bool _Bool; #endif #include "nsswitch-internal.h" +#include struct walk_nss { enum nss_status *status; @@ -59,6 +60,8 @@ struct walk_nss { enum nss_status check_nonlocal_uid(const char *user, uid_t uid, int *errnop); enum nss_status check_nonlocal_gid(const char *user, gid_t gid, int *errnop); enum nss_status check_nonlocal_user(const char *user, int *errnop); +enum nss_status get_nonlocal_passwd(const char *name, struct passwd *pwd, + char **buffer, int *errnop); #define NONLOCAL_IGNORE_ENV "NSS_NONLOCAL_IGNORE" -- 2.45.0