summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/id/id.c25
-rw-r--r--usr.bin/newgrp/newgrp.c12
-rw-r--r--usr.bin/quota/quota.c10
3 files changed, 34 insertions, 13 deletions
diff --git a/usr.bin/id/id.c b/usr.bin/id/id.c
index d18e8b0ea788..1929d968dd1d 100644
--- a/usr.bin/id/id.c
+++ b/usr.bin/id/id.c
@@ -258,7 +258,8 @@ id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
gid_t gid, egid, lastgid;
uid_t uid, euid;
int cnt, ngroups;
- gid_t groups[NGROUPS + 1];
+ long ngroups_max;
+ gid_t *groups;
const char *fmt;
if (pw != NULL) {
@@ -270,12 +271,16 @@ id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
gid = getgid();
}
+ ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
+ if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
+ err(1, "malloc");
+
if (use_ggl && pw != NULL) {
- ngroups = NGROUPS + 1;
+ ngroups = ngroups_max;
getgrouplist(pw->pw_name, gid, groups, &ngroups);
}
else {
- ngroups = getgroups(NGROUPS + 1, groups);
+ ngroups = getgroups(ngroups_max, groups);
}
if (pw != NULL)
@@ -306,6 +311,7 @@ id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
lastgid = gid;
}
printf("\n");
+ free(groups);
}
#ifdef USE_BSM_AUDIT
@@ -361,15 +367,19 @@ group(struct passwd *pw, int nflag)
{
struct group *gr;
int cnt, id, lastid, ngroups;
- gid_t groups[NGROUPS + 1];
+ long ngroups_max;
+ gid_t *groups;
const char *fmt;
+ ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
+ if ((groups = malloc(sizeof(gid_t) * (ngroups_max))) == NULL)
+ err(1, "malloc");
+
if (pw) {
- ngroups = NGROUPS + 1;
+ ngroups = ngroups_max;
(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
} else {
- groups[0] = getgid();
- ngroups = getgroups(NGROUPS, groups + 1) + 1;
+ ngroups = getgroups(ngroups_max, groups);
}
fmt = nflag ? "%s" : "%u";
for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
@@ -389,6 +399,7 @@ group(struct passwd *pw, int nflag)
lastid = id;
}
(void)printf("\n");
+ free(groups);
}
void
diff --git a/usr.bin/newgrp/newgrp.c b/usr.bin/newgrp/newgrp.c
index 62d552fa05d3..91b62a52779d 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
diff --git a/usr.bin/quota/quota.c b/usr.bin/quota/quota.c
index c007e529dbf4..18ef92182c00 100644
--- a/usr.bin/quota/quota.c
+++ b/usr.bin/quota/quota.c
@@ -117,7 +117,8 @@ int
main(int argc, char *argv[])
{
int ngroups;
- gid_t mygid, gidset[NGROUPS];
+ long ngroups_max;
+ gid_t mygid, *gidset;
int i, ch, gflag = 0, uflag = 0, errflag = 0;
while ((ch = getopt(argc, argv, "f:ghlrquv")) != -1) {
@@ -159,13 +160,18 @@ main(int argc, char *argv[])
errflag += showuid(getuid());
if (gflag) {
mygid = getgid();
- ngroups = getgroups(NGROUPS, gidset);
+ ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
+ if ((gidset = malloc(sizeof(gid_t) * ngroups_max))
+ == NULL)
+ err(1, "malloc");
+ ngroups = getgroups(ngroups_max, gidset);
if (ngroups < 0)
err(1, "getgroups");
errflag += showgid(mygid);
for (i = 0; i < ngroups; i++)
if (gidset[i] != mygid)
errflag += showgid(gidset[i]);
+ free(gidset);
}
return(errflag);
}