121346Sdist /* 221346Sdist * Copyright (c) 1983 Regents of the University of California. 335091Sbostic * All rights reserved. 435091Sbostic * 5*42625Sbostic * %sccs.include.redist.c% 621346Sdist */ 79110Ssam 826567Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*42625Sbostic static char sccsid[] = "@(#)initgroups.c 5.6 (Berkeley) 06/01/90"; 1035091Sbostic #endif /* LIBC_SCCS and not lint */ 1121346Sdist 129110Ssam /* 139110Ssam * initgroups 149110Ssam */ 159110Ssam #include <stdio.h> 169110Ssam #include <sys/param.h> 179110Ssam #include <grp.h> 189110Ssam 199110Ssam struct group *getgrent(); 209110Ssam 219110Ssam initgroups(uname, agroup) 229110Ssam char *uname; 239110Ssam int agroup; 249110Ssam { 259110Ssam int groups[NGROUPS], ngroups = 0; 269110Ssam register struct group *grp; 279110Ssam register int i; 289110Ssam 2938787Skarels /* 3038787Skarels * If installing primary group, duplicate it; 3138787Skarels * the first element of groups is the effective gid 3238787Skarels * and will be overwritten when a setgid file is executed. 3338787Skarels */ 3438787Skarels if (agroup >= 0) { 359110Ssam groups[ngroups++] = agroup; 3638787Skarels groups[ngroups++] = agroup; 3738787Skarels } 389110Ssam setgrent(); 3912168Ssam while (grp = getgrent()) { 4012168Ssam if (grp->gr_gid == agroup) 4112168Ssam continue; 429110Ssam for (i = 0; grp->gr_mem[i]; i++) 439110Ssam if (!strcmp(grp->gr_mem[i], uname)) { 449110Ssam if (ngroups == NGROUPS) { 4512168Ssam fprintf(stderr, "initgroups: %s is in too many groups\n", uname); 469110Ssam goto toomany; 479110Ssam } 4811073Smckusick groups[ngroups++] = grp->gr_gid; 499110Ssam } 5012168Ssam } 519110Ssam toomany: 5213176Ssam endgrent(); 539110Ssam if (setgroups(ngroups, groups) < 0) { 5412168Ssam perror("setgroups"); 5527457Sdonn return (-1); 569110Ssam } 579110Ssam return (0); 589110Ssam } 59