Home | History | Annotate | Line # | Download | only in newgrp
newgrp.c revision 1.5
      1 /*-
      2  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Brian Ginsbach.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *        This product includes software developed by the NetBSD
     19  *        Foundation, Inc. and its contributors.
     20  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21  *    contributors may be used to endorse or promote products derived
     22  *    from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  * POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 
     39 #ifndef lint
     40 __RCSID("$NetBSD: newgrp.c,v 1.5 2008/04/05 15:59:39 christos Exp $");
     41 #endif /* not lint */
     42 
     43 #include <sys/param.h>
     44 #include <sys/types.h>
     45 
     46 #include <err.h>
     47 #include <grp.h>
     48 #include <libgen.h>
     49 #include <paths.h>
     50 #include <pwd.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #ifdef LOGIN_CAP
     57 #include <login_cap.h>
     58 #endif
     59 
     60 #include "grutil.h"
     61 
     62 static void
     63 usage(void)
     64 {
     65 	(void)fprintf(stderr, "usage: %s [-l] [group]\n", getprogname());
     66 	exit(EXIT_FAILURE);
     67 }
     68 
     69 int
     70 main(int argc, char *argv[])
     71 {
     72 	extern char **environ;
     73 	struct passwd *pwd;
     74 	int c, lflag;
     75 	char *shell, sbuf[MAXPATHLEN + 2];
     76 	uid_t uid;
     77 #ifdef LOGIN_CAP
     78 	login_cap_t *lc;
     79 	u_int flags = LOGIN_SETUSER;
     80 #endif
     81 
     82 	uid = getuid();
     83 	pwd = getpwuid(uid);
     84 	if (pwd == NULL)
     85 		errx(EXIT_FAILURE, "who are you?");
     86 
     87 #ifdef LOGIN_CAP
     88 	if ((lc = login_getclass(pwd->pw_class)) == NULL)
     89 		errx(EXIT_FAILURE, "%s: unknown login class", pwd->pw_class);
     90 #endif
     91 
     92 	(void)setprogname(argv[0]);
     93 	lflag = 0;
     94 	while ((c = getopt(argc, argv, "-l")) != -1) {
     95 		switch (c) {
     96 		case '-':
     97 		case 'l':
     98 			if (lflag)
     99 				usage();
    100 			lflag = 1;
    101 			break;
    102 		default:
    103 			usage();
    104 			break;
    105 		}
    106 	}
    107 
    108 	argc -= optind;
    109 	argv += optind;
    110 
    111 	if (argc > 0) {
    112 #if 0
    113 		pwd->pw_gid = newgrp(*argv, pwd);
    114 		addgrp(pwd->pw_gid);
    115 		if (setgid(pwd->pw_gid) < 0)
    116 			err(1, "setgid");
    117 #endif
    118 #ifdef LOGIN_CAP
    119 		addgroup(lc, *argv, pwd, getuid(), "Password:");
    120 #else
    121 		addgroup(*argv, pwd, getuid(), "Password:");
    122 #endif
    123 	} else {
    124 #ifdef LOGIN_CAP
    125 		flags |= LOGIN_SETGROUP;
    126 #else
    127 		if (initgroups(pwd->pw_name, pwd->pw_gid) == -1)
    128 			err(EXIT_FAILURE, "initgroups");
    129 		if (setgid(pwd->pw_gid) == -1)
    130 			err(EXIT_FAILURE, "setgid");
    131 #endif
    132 	}
    133 
    134 #ifdef LOGIN_CAP
    135 	if (setusercontext(lc, pwd, uid, flags) == -1)
    136 		err(EXIT_FAILURE, "setusercontext");
    137 	if (!lflag)
    138 		login_close(lc);
    139 #else
    140 	if (setuid(pwd->pw_uid) == -1)
    141 		err(EXIT_FAILURE, "setuid");
    142 #endif
    143 
    144 	if (*pwd->pw_shell == '\0') {
    145 #ifdef TRUST_ENV_SHELL
    146 		shell = getenv("SHELL");
    147 		if (shell != NULL)
    148 			pwd->pw_shell = shell;
    149 		else
    150 #endif
    151 			pwd->pw_shell = __UNCONST(_PATH_BSHELL);
    152 	}
    153 
    154 	shell = pwd->pw_shell;
    155 
    156 	if (lflag) {
    157 		char *term;
    158 #ifdef KERBEROS
    159 		char *krbtkfile;
    160 #endif
    161 
    162 		if (chdir(pwd->pw_dir) == -1)
    163 			warn("%s", pwd->pw_dir);
    164 
    165 		term = getenv("TERM");
    166 #ifdef KERBEROS
    167 		krbtkfile = getenv("KRBTKFILE");
    168 #endif
    169 
    170 		/* create an empty environment */
    171 		if ((environ = malloc(sizeof(char *))) == NULL)
    172 			err(EXIT_FAILURE, NULL);
    173 		environ[0] = NULL;
    174 #ifdef LOGIN_CAP
    175 		if (setusercontext(lc, pwd, uid, LOGIN_SETENV | LOGIN_SETPATH) == -1)
    176 			err(EXIT_FAILURE, "setusercontext");
    177 		login_close(lc);
    178 #else
    179 		(void)setenv("PATH", _PATH_DEFPATH, 1);
    180 #endif
    181 		if (term != NULL)
    182 			(void)setenv("TERM", term, 1);
    183 #ifdef KERBEROS
    184 		if (krbtkfile != NULL)
    185 			(void)setenv("KRBTKFILE", krbtkfile, 1);
    186 #endif
    187 
    188 		(void)setenv("LOGNAME", pwd->pw_name, 1);
    189 		(void)setenv("USER", pwd->pw_name, 1);
    190 		(void)setenv("HOME", pwd->pw_dir, 1);
    191 		(void)setenv("SHELL", pwd->pw_shell, 1);
    192 
    193 		sbuf[0] = '-';
    194 		(void)strlcpy(sbuf + 1, basename(pwd->pw_shell),
    195 			      sizeof(sbuf) - 1);
    196 		shell = sbuf;
    197 	}
    198 
    199 	(void)execl(pwd->pw_shell, shell, NULL);
    200 	err(EXIT_FAILURE, "%s", pwd->pw_shell);
    201 	/* NOTREACHED */
    202 }
    203