xref: /illumos-gate/usr/src/cmd/newgrp/newgrp.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c1a0576Sgww  * Common Development and Distribution License (the "License").
67c1a0576Sgww  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
2316cac786Sgww  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
2768300791Scf46844 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2868300791Scf46844 /*	  All Rights Reserved  	*/
2968300791Scf46844 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * newgrp [-l | -] [group]
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * rules
347c478bd9Sstevel@tonic-gate  *	if no arg, group id in password file is used
357c478bd9Sstevel@tonic-gate  *	else if group id == id in password file
367c478bd9Sstevel@tonic-gate  *	else if login name is in member list
377c478bd9Sstevel@tonic-gate  *	else if password is present and user knows it
387c478bd9Sstevel@tonic-gate  *	else too bad
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <pwd.h>
437c478bd9Sstevel@tonic-gate #include <grp.h>
447c478bd9Sstevel@tonic-gate #include <crypt.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <locale.h>
487c1a0576Sgww #include <syslog.h>
497c1a0576Sgww #include <unistd.h>
507c1a0576Sgww 
517c1a0576Sgww #include <bsm/adt_event.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	SHELL	"/usr/bin/sh"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	PATH	"PATH=:/usr/bin:"
567c478bd9Sstevel@tonic-gate #define	SUPATH	"PATH=:/usr/sbin:/usr/bin"
577c478bd9Sstevel@tonic-gate #define	ELIM	128
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate char	PW[] = "newgrp: Password: ";
607c478bd9Sstevel@tonic-gate char	NG[] = "newgrp: Sorry";
617c478bd9Sstevel@tonic-gate char	PD[] = "newgrp: Permission denied";
627c478bd9Sstevel@tonic-gate char	UG[] = "newgrp: Unknown group";
637c478bd9Sstevel@tonic-gate char	NS[] = "newgrp: You have no shell";
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate char *homedir;
667c478bd9Sstevel@tonic-gate char *logname;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate char *envinit[ELIM];
697c478bd9Sstevel@tonic-gate extern char **environ;
707c478bd9Sstevel@tonic-gate char *path = PATH;
717c478bd9Sstevel@tonic-gate char *supath = SUPATH;
727c478bd9Sstevel@tonic-gate 
7368300791Scf46844 void error(char *s) __NORETURN;
74*d362b749Svk199839 static void warn(char *s);
7568300791Scf46844 void usage(void);
7668300791Scf46844 
7768300791Scf46844 int
main(int argc,char * argv[])7868300791Scf46844 main(int argc, char *argv[])
797c478bd9Sstevel@tonic-gate {
8068300791Scf46844 	struct passwd *p;
817c478bd9Sstevel@tonic-gate 	gid_t chkgrp();
827c478bd9Sstevel@tonic-gate 	int eflag = 0;
837c478bd9Sstevel@tonic-gate 	int flag;
847c478bd9Sstevel@tonic-gate 	uid_t uid;
857c478bd9Sstevel@tonic-gate 	char *shell, *dir, *name;
867c478bd9Sstevel@tonic-gate 	size_t len;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #ifdef	DEBUG
897c478bd9Sstevel@tonic-gate 	chroot(".");
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
937c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
947c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if ((p = getpwuid(getuid())) == NULL)
997c478bd9Sstevel@tonic-gate 		error(NG);
1007c478bd9Sstevel@tonic-gate 	endpwent();
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	while ((flag = getopt(argc, argv, "l")) != EOF) {
1037c478bd9Sstevel@tonic-gate 		switch (flag) {
1047c478bd9Sstevel@tonic-gate 		case 'l':
1057c478bd9Sstevel@tonic-gate 			eflag++;
1067c478bd9Sstevel@tonic-gate 			break;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		default:
1097c478bd9Sstevel@tonic-gate 			usage();
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	argc -= optind;
1157c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (argc > 0 && *argv[0] == '-') {
1187c478bd9Sstevel@tonic-gate 		if (eflag)
1197c478bd9Sstevel@tonic-gate 			usage();
1207c478bd9Sstevel@tonic-gate 		eflag++;
1217c478bd9Sstevel@tonic-gate 		argv++;
1227c478bd9Sstevel@tonic-gate 		--argc;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if (argc > 0)
1267c478bd9Sstevel@tonic-gate 		p->pw_gid = chkgrp(argv[0], p);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	uid = p->pw_uid;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	len = strlen(p->pw_dir) + 1;
1317c478bd9Sstevel@tonic-gate 	if ((dir = (char *)malloc(len)) == NULL)
1327c478bd9Sstevel@tonic-gate 		error("newgrp: Memory request failed");
1337c478bd9Sstevel@tonic-gate 	(void) strncpy(dir, p->pw_dir, len);
1347c478bd9Sstevel@tonic-gate 	len = strlen(p->pw_name) + 1;
1357c478bd9Sstevel@tonic-gate 	if ((name = (char *)malloc(len)) == NULL)
1367c478bd9Sstevel@tonic-gate 		error("newgrp: Memory request failed");
1377c478bd9Sstevel@tonic-gate 	(void) strncpy(name, p->pw_name, len);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	if (setgid(p->pw_gid) < 0 || setuid(getuid()) < 0)
1407c478bd9Sstevel@tonic-gate 		error(NG);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (!*p->pw_shell) {
1437c478bd9Sstevel@tonic-gate 		if ((shell = getenv("SHELL")) != NULL) {
1447c478bd9Sstevel@tonic-gate 			p->pw_shell = shell;
1457c478bd9Sstevel@tonic-gate 		} else {
1467c478bd9Sstevel@tonic-gate 			p->pw_shell = SHELL;
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	if (eflag) {
1517c478bd9Sstevel@tonic-gate 		char *simple;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		len = strlen(dir) + 6;
1547c478bd9Sstevel@tonic-gate 		if ((homedir = (char *)malloc(len)) == NULL)
1557c478bd9Sstevel@tonic-gate 			error("newgrp: Memory request failed");
1567c478bd9Sstevel@tonic-gate 		(void) snprintf(homedir, len, "HOME=%s", dir);
1577c478bd9Sstevel@tonic-gate 		len = strlen(name) + 9;
1587c478bd9Sstevel@tonic-gate 		if ((logname = (char *)malloc(len)) == NULL)
1597c478bd9Sstevel@tonic-gate 			error("newgrp: Memory request failed");
1607c478bd9Sstevel@tonic-gate 		(void) snprintf(logname, len, "LOGNAME=%s", name);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		envinit[2] = logname;
1647c1a0576Sgww 		(void) chdir(dir);
1657c478bd9Sstevel@tonic-gate 		envinit[0] = homedir;
1667c478bd9Sstevel@tonic-gate 		if (uid == 0)
1677c478bd9Sstevel@tonic-gate 			envinit[1] = supath;
1687c478bd9Sstevel@tonic-gate 		else
1697c478bd9Sstevel@tonic-gate 			envinit[1] = path;
1707c478bd9Sstevel@tonic-gate 		envinit[3] = NULL;
1717c478bd9Sstevel@tonic-gate 		environ = envinit;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		len = strlen(p->pw_shell) + 2;
1747c478bd9Sstevel@tonic-gate 		if ((shell = (char *)malloc(len)) == NULL)
1757c478bd9Sstevel@tonic-gate 			error("newgrp: Memory request failed");
1767c478bd9Sstevel@tonic-gate 		(void) snprintf(shell, len, "-%s", p->pw_shell);
1777c478bd9Sstevel@tonic-gate 		simple = strrchr(shell, '/');
1787c478bd9Sstevel@tonic-gate 		if (simple) {
1797c478bd9Sstevel@tonic-gate 			*(shell+1) = '\0';
1807c478bd9Sstevel@tonic-gate 			shell = strcat(shell, ++simple);
1817c478bd9Sstevel@tonic-gate 		}
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 	else
1847c478bd9Sstevel@tonic-gate 		shell = p->pw_shell;
1857c478bd9Sstevel@tonic-gate 
1867c1a0576Sgww 	(void) execl(p->pw_shell, shell, NULL);
1877c1a0576Sgww 	warn(NS);
1887c1a0576Sgww 	return (1);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
191*d362b749Svk199839 static void
warn(char * s)19268300791Scf46844 warn(char *s)
1937c478bd9Sstevel@tonic-gate {
1947c1a0576Sgww 	(void) fprintf(stderr, "%s\n", gettext(s));
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
19768300791Scf46844 void
error(char * s)19868300791Scf46844 error(char *s)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	warn(s);
2017c478bd9Sstevel@tonic-gate 	exit(1);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
20416cac786Sgww void
put_event(char * gname,int sorf)20516cac786Sgww put_event(char *gname, int sorf)
2067c478bd9Sstevel@tonic-gate {
2077c1a0576Sgww 	adt_session_data_t	*ah;
2087c1a0576Sgww 	adt_event_data_t	*event;
2097c1a0576Sgww 
2107c1a0576Sgww 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
2117c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
2127c1a0576Sgww 		    "adt_start_session(ADT_newgrp_login): %m");
2137c1a0576Sgww 	}
2147c1a0576Sgww 	if ((event = adt_alloc_event(ah, ADT_newgrp_login)) == NULL) {
2157c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
2167c1a0576Sgww 		    "adt_alloc_event(ADT_newgrp_login): %m");
2177c1a0576Sgww 	} else {
2187c1a0576Sgww 		event->adt_newgrp_login.groupname = gname;
2197c1a0576Sgww 	}
2207c478bd9Sstevel@tonic-gate 
2217c1a0576Sgww 	if (adt_put_event(event, sorf, sorf) != 0) {
2227c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
2237c1a0576Sgww 		    "adt_put_event(ADT_newgrp, %d): %m", sorf);
2247c478bd9Sstevel@tonic-gate 	}
2257c1a0576Sgww 	adt_free_event(event);
2267c1a0576Sgww 	(void) adt_end_session(ah);
22716cac786Sgww }
2287c478bd9Sstevel@tonic-gate 
22916cac786Sgww gid_t
chkgrp(gname,p)23016cac786Sgww chkgrp(gname, p)
23116cac786Sgww char	*gname;
23216cac786Sgww struct	passwd *p;
23316cac786Sgww {
23416cac786Sgww 	char **t;
23516cac786Sgww 	struct group *g;
23616cac786Sgww 
23716cac786Sgww 	g = getgrnam(gname);
23816cac786Sgww 	endgrent();
23916cac786Sgww 	if (g == NULL) {
24016cac786Sgww 		warn(UG);
24116cac786Sgww 		put_event(gname, ADT_FAILURE);
24216cac786Sgww 		return (getgid());
24316cac786Sgww 	}
24416cac786Sgww 	if (p->pw_gid == g->gr_gid || getuid() == 0) {
24516cac786Sgww 		put_event(gname, ADT_SUCCESS);
24616cac786Sgww 		return (g->gr_gid);
24716cac786Sgww 	}
24816cac786Sgww 	for (t = g->gr_mem; *t; ++t) {
24916cac786Sgww 		if (strcmp(p->pw_name, *t) == 0) {
25016cac786Sgww 			put_event(gname, ADT_SUCCESS);
25116cac786Sgww 			return (g->gr_gid);
25216cac786Sgww 		}
25316cac786Sgww 	}
25416cac786Sgww 	if (*g->gr_passwd) {
25516cac786Sgww 		if (!isatty(fileno(stdin))) {
25616cac786Sgww 			put_event(gname, ADT_FAILURE);
25716cac786Sgww 			error(PD);
25816cac786Sgww 		}
25916cac786Sgww 		if (strcmp(g->gr_passwd,
26016cac786Sgww 		    crypt(getpassphrase(PW), g->gr_passwd)) == 0) {
26116cac786Sgww 			put_event(gname, ADT_SUCCESS);
26216cac786Sgww 			return (g->gr_gid);
26316cac786Sgww 		}
26416cac786Sgww 	}
26516cac786Sgww 	put_event(gname, ADT_FAILURE);
26616cac786Sgww 	warn(NG);
26716cac786Sgww 	return (getgid());
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
27068300791Scf46844 void
usage(void)27168300791Scf46844 usage(void)
2727c478bd9Sstevel@tonic-gate {
2737c1a0576Sgww 	(void) fprintf(stderr, gettext(
2747c478bd9Sstevel@tonic-gate 	    "usage: newgrp [-l | -] [group]\n"));
2757c478bd9Sstevel@tonic-gate 	exit(2);
2767c478bd9Sstevel@tonic-gate }
277