diff options
author | brooks <brooks@FreeBSD.org> | 2009-06-19 15:58:24 +0000 |
---|---|---|
committer | brooks <brooks@FreeBSD.org> | 2009-06-19 15:58:24 +0000 |
commit | da4e70cf9ab3e05f67d77de37a7c6c335a5f7e4b (patch) | |
tree | b7d4401847de9bebf98c6fe35d5b61bfd749c7dc /usr.bin/newgrp | |
parent | 384550a3864abef16c80a82c2fb12d9726aab2d1 (diff) | |
download | FreeBSD-src-da4e70cf9ab3e05f67d77de37a7c6c335a5f7e4b.zip FreeBSD-src-da4e70cf9ab3e05f67d77de37a7c6c335a5f7e4b.tar.gz |
In preparation for raising NGROUPS and NGROUPS_MAX, change base
system callers of getgroups(), getgrouplist(), and setgroups() to
allocate buffers dynamically. Specifically, allocate a buffer of size
sysconf(_SC_NGROUPS_MAX)+1 (+2 in a few cases to allow for overflow).
This (or similar gymnastics) is required for the code to actually follow
the POSIX.1-2008 specification where {NGROUPS_MAX} may differ at runtime
and where getgroups may return {NGROUPS_MAX}+1 results on systems like
FreeBSD which include the primary group.
In id(1), don't pointlessly add the primary group to the list of all
groups, it is always the first result from getgroups(). In principle
the old code was more portable, but this was only done in one of the two
places where getgroups() was called to the overall effect was pointless.
Document the actual POSIX requirements in the getgroups(2) and
setgroups(2) manpages. We do not yet support a dynamic NGROUPS, but we
may in the future.
MFC after: 2 weeks
Diffstat (limited to 'usr.bin/newgrp')
-rw-r--r-- | usr.bin/newgrp/newgrp.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/usr.bin/newgrp/newgrp.c b/usr.bin/newgrp/newgrp.c index 62d552f..91b62a5 100644 --- a/usr.bin/newgrp/newgrp.c +++ b/usr.bin/newgrp/newgrp.c @@ -146,8 +146,8 @@ restoregrps(void) static void addgroup(const char *grpname) { - gid_t grps[NGROUPS_MAX]; - long lgid; + gid_t *grps; + long lgid, ngrps_max; int dbmember, i, ngrps; gid_t egid; struct group *grp; @@ -185,7 +185,10 @@ addgroup(const char *grpname) } } - if ((ngrps = getgroups(NGROUPS_MAX, (gid_t *)grps)) < 0) { + ngrps_max = sysconf(_SC_NGROUPS_MAX) + 1; + if ((grps = malloc(sizeof(gid_t) * ngrps_max)) == NULL) + err(1, "malloc"); + if ((ngrps = getgroups(ngrps_max, (gid_t *)grps)) < 0) { warn("getgroups"); return; } @@ -217,7 +220,7 @@ addgroup(const char *grpname) /* Add old effective gid to supp. list if it does not exist. */ if (egid != grp->gr_gid && !inarray(egid, grps, ngrps)) { - if (ngrps == NGROUPS_MAX) + if (ngrps == ngrps_max) warnx("too many groups"); else { grps[ngrps++] = egid; @@ -231,6 +234,7 @@ addgroup(const char *grpname) } } + free(grps); } static int |