Home | History | Annotate | Line # | Download | only in newgrp
newgrp.c revision 1.3.2.1
      1      1.1  ginsbach /*-
      2      1.1  ginsbach  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      3      1.1  ginsbach  * All rights reserved.
      4      1.1  ginsbach  *
      5      1.1  ginsbach  * This code is derived from software contributed to The NetBSD Foundation
      6      1.1  ginsbach  * by Brian Ginsbach.
      7      1.1  ginsbach  *
      8      1.1  ginsbach  * Redistribution and use in source and binary forms, with or without
      9      1.1  ginsbach  * modification, are permitted provided that the following conditions
     10      1.1  ginsbach  * are met:
     11      1.1  ginsbach  * 1. Redistributions of source code must retain the above copyright
     12      1.1  ginsbach  *    notice, this list of conditions and the following disclaimer.
     13      1.1  ginsbach  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1  ginsbach  *    notice, this list of conditions and the following disclaimer in the
     15      1.1  ginsbach  *    documentation and/or other materials provided with the distribution.
     16      1.1  ginsbach  * 3. All advertising materials mentioning features or use of this software
     17      1.1  ginsbach  *    must display the following acknowledgement:
     18      1.1  ginsbach  *        This product includes software developed by the NetBSD
     19      1.1  ginsbach  *        Foundation, Inc. and its contributors.
     20      1.1  ginsbach  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21      1.1  ginsbach  *    contributors may be used to endorse or promote products derived
     22      1.1  ginsbach  *    from this software without specific prior written permission.
     23      1.1  ginsbach  *
     24      1.1  ginsbach  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25      1.1  ginsbach  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26      1.1  ginsbach  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27      1.1  ginsbach  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28      1.1  ginsbach  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29      1.1  ginsbach  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30      1.1  ginsbach  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31      1.1  ginsbach  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32      1.1  ginsbach  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33      1.1  ginsbach  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34      1.1  ginsbach  * POSSIBILITY OF SUCH DAMAGE.
     35      1.1  ginsbach  */
     36      1.1  ginsbach 
     37      1.1  ginsbach #include <sys/cdefs.h>
     38      1.1  ginsbach 
     39      1.1  ginsbach #ifndef lint
     40  1.3.2.1  ginsbach __RCSID("$NetBSD: newgrp.c,v 1.3.2.1 2007/10/28 03:13:37 ginsbach Exp $");
     41      1.1  ginsbach #endif /* not lint */
     42      1.1  ginsbach 
     43      1.1  ginsbach #include <sys/param.h>
     44      1.1  ginsbach #include <sys/types.h>
     45      1.1  ginsbach 
     46      1.1  ginsbach #include <err.h>
     47      1.1  ginsbach #include <grp.h>
     48      1.1  ginsbach #include <libgen.h>
     49      1.1  ginsbach #include <paths.h>
     50      1.1  ginsbach #include <pwd.h>
     51      1.1  ginsbach #include <stdio.h>
     52      1.1  ginsbach #include <stdlib.h>
     53      1.1  ginsbach #include <string.h>
     54      1.1  ginsbach #include <unistd.h>
     55      1.1  ginsbach 
     56      1.1  ginsbach #ifdef LOGIN_CAP
     57      1.1  ginsbach #include <login_cap.h>
     58      1.1  ginsbach #endif
     59      1.1  ginsbach 
     60      1.1  ginsbach int addgrp(gid_t);
     61      1.1  ginsbach gid_t newgrp(const char *, struct passwd *);
     62      1.1  ginsbach void usage(void);
     63      1.1  ginsbach 
     64      1.1  ginsbach int
     65      1.1  ginsbach main(int argc, char *argv[])
     66      1.1  ginsbach {
     67      1.1  ginsbach 	extern char **environ;
     68      1.1  ginsbach 	struct passwd *pwd;
     69      1.1  ginsbach 	int c, lflag;
     70      1.1  ginsbach 	char *shell, sbuf[MAXPATHLEN + 2];
     71      1.1  ginsbach 	uid_t uid;
     72      1.1  ginsbach #ifdef LOGIN_CAP
     73      1.1  ginsbach 	login_cap_t *lc;
     74      1.1  ginsbach 	u_int flags = LOGIN_SETUSER;
     75      1.1  ginsbach #endif
     76      1.1  ginsbach 
     77      1.1  ginsbach 	uid = getuid();
     78      1.1  ginsbach 	pwd = getpwuid(uid);
     79      1.1  ginsbach 	if (pwd == NULL)
     80      1.1  ginsbach 		errx(1, "who are you?");
     81      1.1  ginsbach 
     82      1.1  ginsbach #ifdef LOGIN_CAP
     83      1.1  ginsbach 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
     84      1.1  ginsbach 		errx(1, "%s: unknown login class", pwd->pw_class);
     85      1.1  ginsbach #endif
     86      1.1  ginsbach 
     87      1.1  ginsbach 	lflag = 0;
     88      1.1  ginsbach 	while ((c = getopt(argc, argv, "-l")) != -1) {
     89      1.1  ginsbach 		switch (c) {
     90      1.1  ginsbach 		case '-':
     91      1.1  ginsbach 		case 'l':
     92      1.1  ginsbach 			if (lflag)
     93      1.1  ginsbach 				usage();
     94      1.1  ginsbach 			lflag = 1;
     95      1.1  ginsbach 			break;
     96      1.1  ginsbach 		default:
     97      1.1  ginsbach 			usage();
     98      1.1  ginsbach 			break;
     99      1.1  ginsbach 		}
    100      1.1  ginsbach 	}
    101      1.1  ginsbach 
    102      1.1  ginsbach 	argc -= optind;
    103      1.1  ginsbach 	argv += optind;
    104      1.1  ginsbach 
    105      1.1  ginsbach 	if (argc > 0) {
    106      1.1  ginsbach 		pwd->pw_gid = newgrp(*argv, pwd);
    107      1.1  ginsbach 		addgrp(pwd->pw_gid);
    108      1.1  ginsbach 		if (setgid(pwd->pw_gid) < 0)
    109      1.1  ginsbach 			err(1, "setgid");
    110      1.1  ginsbach 	} else {
    111      1.1  ginsbach #ifdef LOGIN_CAP
    112      1.1  ginsbach 		flags |= LOGIN_SETGROUP;
    113      1.1  ginsbach #else
    114      1.1  ginsbach 		if (initgroups(pwd->pw_name, pwd->pw_gid) < 0)
    115      1.1  ginsbach 			err(1, "initgroups");
    116      1.1  ginsbach 		if (setgid(pwd->pw_gid) < 0)
    117      1.1  ginsbach 			err(1, "setgid");
    118      1.1  ginsbach #endif
    119      1.1  ginsbach 	}
    120      1.1  ginsbach 
    121      1.1  ginsbach #ifdef LOGIN_CAP
    122      1.1  ginsbach 	if (setusercontext(lc, pwd, uid, flags))
    123      1.1  ginsbach 		err(1, "setusercontext");
    124      1.1  ginsbach 	if (!lflag)
    125      1.1  ginsbach 		login_close(lc);
    126      1.1  ginsbach #else
    127      1.1  ginsbach 	if (setuid(pwd->pw_uid) < 0)
    128      1.1  ginsbach 		err(1, "setuid");
    129      1.1  ginsbach #endif
    130      1.1  ginsbach 
    131      1.1  ginsbach 	if (*pwd->pw_shell == '\0') {
    132      1.1  ginsbach #ifdef TRUST_ENV_SHELL
    133      1.1  ginsbach 		shell = getenv("SHELL");
    134      1.1  ginsbach 		if (shell != NULL)
    135      1.1  ginsbach 			pwd->pw_shell = shell;
    136      1.1  ginsbach 		else
    137      1.1  ginsbach #endif
    138      1.1  ginsbach 			pwd->pw_shell = _PATH_BSHELL;
    139      1.1  ginsbach 	}
    140      1.1  ginsbach 
    141      1.1  ginsbach 	shell = pwd->pw_shell;
    142      1.1  ginsbach 
    143      1.1  ginsbach 	if (lflag) {
    144      1.1  ginsbach 		char *term;
    145      1.1  ginsbach #ifdef KERBEROS
    146      1.1  ginsbach 		char *krbtkfile;
    147      1.1  ginsbach #endif
    148      1.1  ginsbach 
    149      1.1  ginsbach 		if (chdir(pwd->pw_dir) < 0)
    150      1.1  ginsbach 			warn("%s", pwd->pw_dir);
    151      1.1  ginsbach 
    152      1.1  ginsbach 		term = getenv("TERM");
    153      1.1  ginsbach #ifdef KERBEROS
    154      1.1  ginsbach 		krbtkfile = getenv("KRBTKFILE");
    155      1.1  ginsbach #endif
    156      1.1  ginsbach 
    157      1.1  ginsbach 		/* create an empty environment */
    158      1.1  ginsbach 		if ((environ = malloc(sizeof(char *))) == NULL)
    159      1.1  ginsbach 			err(1, NULL);
    160      1.1  ginsbach 		environ[0] = NULL;
    161      1.1  ginsbach #ifdef LOGIN_CAP
    162      1.1  ginsbach 		if (setusercontext(lc, pwd, uid, LOGIN_SETENV|LOGIN_SETPATH))
    163      1.1  ginsbach 			err(1, "setusercontext");
    164      1.1  ginsbach 		login_close(lc);
    165      1.1  ginsbach #else
    166      1.1  ginsbach 		(void)setenv("PATH", _PATH_DEFPATH, 1);
    167      1.1  ginsbach #endif
    168      1.1  ginsbach 		if (term != NULL)
    169      1.1  ginsbach 			(void)setenv("TERM", term, 1);
    170      1.1  ginsbach #ifdef KERBEROS
    171      1.1  ginsbach 		if (krbtkfile != NULL)
    172      1.1  ginsbach 			(void)setenv("KRBTKFILE", krbtkfile, 1);
    173      1.1  ginsbach #endif
    174      1.1  ginsbach 
    175      1.1  ginsbach 		(void)setenv("LOGNAME", pwd->pw_name, 1);
    176      1.1  ginsbach 		(void)setenv("USER", pwd->pw_name, 1);
    177      1.1  ginsbach 		(void)setenv("HOME", pwd->pw_dir, 1);
    178      1.1  ginsbach 		(void)setenv("SHELL", pwd->pw_shell, 1);
    179      1.1  ginsbach 
    180      1.1  ginsbach 		sbuf[0] = '-';
    181      1.1  ginsbach 		(void)strlcpy(sbuf + 1, basename(pwd->pw_shell),
    182      1.1  ginsbach 			      sizeof(sbuf) - 1);
    183      1.1  ginsbach 		shell = sbuf;
    184      1.1  ginsbach 	}
    185      1.1  ginsbach 
    186      1.1  ginsbach 	execl(pwd->pw_shell, shell, NULL);
    187      1.1  ginsbach 	err(1, "%s", pwd->pw_shell);
    188      1.1  ginsbach }
    189      1.1  ginsbach 
    190      1.1  ginsbach gid_t
    191      1.1  ginsbach newgrp(const char *group, struct passwd *pwd)
    192      1.1  ginsbach {
    193      1.1  ginsbach 	struct group *grp;
    194      1.1  ginsbach 	char *ep, **p;
    195      1.1  ginsbach 	gid_t gid;
    196      1.1  ginsbach 
    197      1.1  ginsbach 	grp = getgrnam(group);
    198      1.1  ginsbach 	if (grp == NULL) {
    199      1.1  ginsbach 		if (*group != '-') {
    200      1.1  ginsbach 		    gid = (gid_t)strtol(group, &ep, 10);
    201      1.1  ginsbach 		    if (*ep == '\0')
    202      1.1  ginsbach 			    grp = getgrgid(gid);
    203      1.1  ginsbach 		}
    204      1.1  ginsbach 	}
    205      1.1  ginsbach 
    206      1.1  ginsbach 	if (grp == NULL) {
    207      1.1  ginsbach 		warnx("%s: unknown group", group);
    208      1.1  ginsbach 		return getgid();
    209      1.1  ginsbach 	}
    210      1.1  ginsbach 
    211      1.1  ginsbach 	if (pwd->pw_gid == grp->gr_gid || getuid() == 0)
    212      1.1  ginsbach 		return grp->gr_gid;
    213      1.1  ginsbach 
    214      1.2  christos 	for (p = grp->gr_mem; *p != NULL; p++)
    215      1.1  ginsbach 		if (strcmp(*p, pwd->pw_name) == 0)
    216      1.1  ginsbach 			return grp->gr_gid;
    217      1.1  ginsbach 
    218      1.1  ginsbach 	if (*grp->gr_passwd != '\0') {
    219      1.1  ginsbach 		ep = getpass("Password:");
    220      1.1  ginsbach 		if (strcmp(grp->gr_passwd, crypt(ep, grp->gr_passwd)) == 0) {
    221      1.3  christos 			(void)memset(ep, '\0', _PASSWORD_LEN);
    222      1.1  ginsbach 			return grp->gr_gid;
    223      1.1  ginsbach 		}
    224      1.3  christos 		(void)memset(ep, '\0', _PASSWORD_LEN);
    225      1.1  ginsbach 	}
    226      1.1  ginsbach 
    227      1.1  ginsbach 	warnx("Sorry");
    228      1.1  ginsbach 	return getgid();
    229      1.1  ginsbach }
    230      1.1  ginsbach 
    231      1.1  ginsbach int
    232      1.1  ginsbach addgrp(gid_t group)
    233      1.1  ginsbach {
    234      1.1  ginsbach 	int i, ngroups, ngroupsmax, rval;
    235      1.1  ginsbach 	gid_t *groups;
    236  1.3.2.1  ginsbach 	gid_t ogroup;
    237      1.1  ginsbach 
    238      1.1  ginsbach 	rval = 0;
    239      1.1  ginsbach 
    240  1.3.2.1  ginsbach 	ogroup = getegid();
    241  1.3.2.1  ginsbach 	if (group == ogroup)
    242  1.3.2.1  ginsbach 		return rval;		/* nothing to do */
    243  1.3.2.1  ginsbach 
    244      1.1  ginsbach 	ngroupsmax = (int)sysconf(_SC_NGROUPS_MAX);
    245      1.1  ginsbach 	if (ngroupsmax < 0)
    246      1.1  ginsbach 		ngroupsmax = NGROUPS_MAX;
    247      1.1  ginsbach 
    248      1.1  ginsbach 	groups = malloc(ngroupsmax * sizeof(*groups));
    249  1.3.2.1  ginsbach 	if (groups == NULL) {
    250  1.3.2.1  ginsbach 		warn(NULL);
    251      1.1  ginsbach 		return -1;
    252  1.3.2.1  ginsbach 	}
    253      1.1  ginsbach 
    254      1.1  ginsbach 	ngroups = getgroups(ngroupsmax, groups);
    255      1.1  ginsbach 	if (ngroups < 0) {
    256      1.1  ginsbach 		free(groups);
    257  1.3.2.1  ginsbach 		warn("getgroups");
    258      1.1  ginsbach 		return -1;
    259      1.1  ginsbach 	}
    260      1.1  ginsbach 
    261      1.1  ginsbach 	/*
    262      1.1  ginsbach 	 * BSD based systems normally have the egid in the supplemental
    263      1.1  ginsbach 	 * group list.
    264      1.1  ginsbach 	 */
    265      1.1  ginsbach #if (defined(BSD) && BSD >= 199306)
    266      1.1  ginsbach 	/*
    267      1.1  ginsbach 	 * According to POSIX/XPG6:
    268      1.1  ginsbach 	 * On system where the egid is normally in the supplemental group list
    269      1.1  ginsbach 	 * (or whenever the old egid actually is in the supplemental group
    270      1.1  ginsbach 	 * list):
    271      1.1  ginsbach 	 *	o If the new egid is in the supplemental group list,
    272      1.1  ginsbach 	 *	  just change the egid.
    273      1.1  ginsbach 	 *	o If the new egid is not in the supplemental group list,
    274      1.1  ginsbach 	 *	  add the new egid to the list if there is room.
    275      1.1  ginsbach 	 */
    276      1.1  ginsbach 
    277      1.1  ginsbach 	/* search for new egid in supplemental group list */
    278      1.1  ginsbach 	for (i = 0; i < ngroups && groups[i] != group; i++)
    279      1.1  ginsbach 		continue;
    280      1.1  ginsbach 
    281      1.1  ginsbach 	/* add the new egid to the supplemental group list */
    282      1.1  ginsbach 	if (i == ngroups && ngroups < ngroupsmax) {
    283      1.1  ginsbach 		groups[ngroups++] = group;
    284      1.1  ginsbach 		if (setgroups(ngroups, groups) < 0) {
    285      1.1  ginsbach 			warn("setgroups");
    286      1.1  ginsbach 			rval = -1;
    287      1.1  ginsbach 		}
    288      1.1  ginsbach 	}
    289      1.1  ginsbach #else
    290      1.1  ginsbach 	/*
    291      1.1  ginsbach 	 * According to POSIX/XPG6:
    292      1.1  ginsbach 	 * On systems where the egid is not normally in the supplemental group
    293      1.1  ginsbach 	 * list (or whenever the old egid is not in the supplemental group
    294      1.1  ginsbach 	 * list):
    295      1.1  ginsbach 	 *	o If the new egid is in the supplemental group list, delete
    296      1.1  ginsbach 	 *	  it from the list.
    297      1.1  ginsbach 	 *	o If the old egid is not in the supplemental group list,
    298      1.1  ginsbach 	 *	  add the old egid to the list if there is room.
    299      1.1  ginsbach 	 */
    300      1.1  ginsbach 
    301      1.1  ginsbach 	/* search for new egid in supplemental group list */
    302  1.3.2.1  ginsbach 	for (i = 0; i < ngroups && groups[i] != group; i++)
    303      1.1  ginsbach 		continue;
    304      1.1  ginsbach 
    305      1.1  ginsbach 	/* remove new egid from supplemental group list */
    306  1.3.2.1  ginsbach 	if (i != ngroups) {
    307      1.1  ginsbach 		for (--ngroups; i < ngroups; i++)
    308      1.1  ginsbach 			groups[i] = groups[i + 1];
    309      1.1  ginsbach 	}
    310      1.1  ginsbach 
    311      1.1  ginsbach 	/* search for old egid in supplemental group list */
    312      1.1  ginsbach 	for (i = 0; i < ngroups && groups[i] != egid; i++)
    313      1.1  ginsbach 		continue;
    314      1.1  ginsbach 
    315      1.1  ginsbach 	/* add old egid from supplemental group list */
    316  1.3.2.1  ginsbach 	if (i == ngroups && ngroups < ngroupsmax) {
    317  1.3.2.1  ginsbach 		groups[ngroups++] = ogroup;
    318      1.1  ginsbach 		if (setgroups(ngroups, groups) < 0) {
    319      1.1  ginsbach 			warn("setgroups");
    320      1.1  ginsbach 			rval = -1;
    321      1.1  ginsbach 		}
    322      1.1  ginsbach 	}
    323      1.1  ginsbach #endif
    324      1.1  ginsbach 
    325      1.1  ginsbach 	free(groups);
    326      1.1  ginsbach 	return rval;
    327      1.1  ginsbach }
    328      1.1  ginsbach 
    329      1.1  ginsbach void
    330      1.1  ginsbach usage()
    331      1.1  ginsbach {
    332      1.1  ginsbach 
    333      1.1  ginsbach 	(void)fprintf(stderr, "usage: %s [-l] [group]\n", getprogname());
    334      1.1  ginsbach 	exit(1);
    335      1.1  ginsbach }
    336