xref: /titanic_50/usr/src/cmd/newgrp/newgrp.c (revision 7c1a057666319a524c052ff0e99e9e26c2695e77)
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
5*7c1a0576Sgww  * Common Development and Distribution License (the "License").
6*7c1a0576Sgww  * 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 /*
23*7c1a0576Sgww  * Copyright 2006 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * newgrp [-l | -] [group]
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * rules
367c478bd9Sstevel@tonic-gate  *	if no arg, group id in password file is used
377c478bd9Sstevel@tonic-gate  *	else if group id == id in password file
387c478bd9Sstevel@tonic-gate  *	else if login name is in member list
397c478bd9Sstevel@tonic-gate  *	else if password is present and user knows it
407c478bd9Sstevel@tonic-gate  *	else too bad
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <pwd.h>
457c478bd9Sstevel@tonic-gate #include <grp.h>
467c478bd9Sstevel@tonic-gate #include <crypt.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
487c478bd9Sstevel@tonic-gate #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
50*7c1a0576Sgww #include <syslog.h>
51*7c1a0576Sgww #include <unistd.h>
52*7c1a0576Sgww 
53*7c1a0576Sgww #include <bsm/adt_event.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	SHELL	"/usr/bin/sh"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	PATH	"PATH=:/usr/bin:"
587c478bd9Sstevel@tonic-gate #define	SUPATH	"PATH=:/usr/sbin:/usr/bin"
597c478bd9Sstevel@tonic-gate #define	ELIM	128
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate char	PW[] = "newgrp: Password: ";
627c478bd9Sstevel@tonic-gate char	NG[] = "newgrp: Sorry";
637c478bd9Sstevel@tonic-gate char	PD[] = "newgrp: Permission denied";
647c478bd9Sstevel@tonic-gate char	UG[] = "newgrp: Unknown group";
657c478bd9Sstevel@tonic-gate char	NS[] = "newgrp: You have no shell";
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate char *homedir;
687c478bd9Sstevel@tonic-gate char *logname;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate char *envinit[ELIM];
717c478bd9Sstevel@tonic-gate extern char **environ;
727c478bd9Sstevel@tonic-gate char *path = PATH;
737c478bd9Sstevel@tonic-gate char *supath = SUPATH;
747c478bd9Sstevel@tonic-gate 
7568300791Scf46844 void error(char *s) __NORETURN;
7668300791Scf46844 void warn(char *s);
7768300791Scf46844 void usage(void);
7868300791Scf46844 
7968300791Scf46844 int
8068300791Scf46844 main(int argc, char *argv[])
817c478bd9Sstevel@tonic-gate {
8268300791Scf46844 	struct passwd *p;
837c478bd9Sstevel@tonic-gate 	gid_t chkgrp();
847c478bd9Sstevel@tonic-gate 	int eflag = 0;
857c478bd9Sstevel@tonic-gate 	int flag;
867c478bd9Sstevel@tonic-gate 	uid_t uid;
877c478bd9Sstevel@tonic-gate 	char *shell, *dir, *name;
887c478bd9Sstevel@tonic-gate 	size_t len;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #ifdef	DEBUG
917c478bd9Sstevel@tonic-gate 	chroot(".");
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
957c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
967c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if ((p = getpwuid(getuid())) == NULL)
1017c478bd9Sstevel@tonic-gate 		error(NG);
1027c478bd9Sstevel@tonic-gate 	endpwent();
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	while ((flag = getopt(argc, argv, "l")) != EOF) {
1057c478bd9Sstevel@tonic-gate 		switch (flag) {
1067c478bd9Sstevel@tonic-gate 		case 'l':
1077c478bd9Sstevel@tonic-gate 			eflag++;
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		default:
1117c478bd9Sstevel@tonic-gate 			usage();
1127c478bd9Sstevel@tonic-gate 			break;
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	argc -= optind;
1177c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (argc > 0 && *argv[0] == '-') {
1207c478bd9Sstevel@tonic-gate 		if (eflag)
1217c478bd9Sstevel@tonic-gate 			usage();
1227c478bd9Sstevel@tonic-gate 		eflag++;
1237c478bd9Sstevel@tonic-gate 		argv++;
1247c478bd9Sstevel@tonic-gate 		--argc;
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (argc > 0)
1287c478bd9Sstevel@tonic-gate 		p->pw_gid = chkgrp(argv[0], p);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	uid = p->pw_uid;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	len = strlen(p->pw_dir) + 1;
1337c478bd9Sstevel@tonic-gate 	if ((dir = (char *)malloc(len)) == NULL)
1347c478bd9Sstevel@tonic-gate 		error("newgrp: Memory request failed");
1357c478bd9Sstevel@tonic-gate 	(void) strncpy(dir, p->pw_dir, len);
1367c478bd9Sstevel@tonic-gate 	len = strlen(p->pw_name) + 1;
1377c478bd9Sstevel@tonic-gate 	if ((name = (char *)malloc(len)) == NULL)
1387c478bd9Sstevel@tonic-gate 		error("newgrp: Memory request failed");
1397c478bd9Sstevel@tonic-gate 	(void) strncpy(name, p->pw_name, len);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (setgid(p->pw_gid) < 0 || setuid(getuid()) < 0)
1427c478bd9Sstevel@tonic-gate 		error(NG);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (!*p->pw_shell) {
1457c478bd9Sstevel@tonic-gate 		if ((shell = getenv("SHELL")) != NULL) {
1467c478bd9Sstevel@tonic-gate 			p->pw_shell = shell;
1477c478bd9Sstevel@tonic-gate 		} else {
1487c478bd9Sstevel@tonic-gate 			p->pw_shell = SHELL;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (eflag) {
1537c478bd9Sstevel@tonic-gate 		char *simple;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		len = strlen(dir) + 6;
1567c478bd9Sstevel@tonic-gate 		if ((homedir = (char *)malloc(len)) == NULL)
1577c478bd9Sstevel@tonic-gate 			error("newgrp: Memory request failed");
1587c478bd9Sstevel@tonic-gate 		(void) snprintf(homedir, len, "HOME=%s", dir);
1597c478bd9Sstevel@tonic-gate 		len = strlen(name) + 9;
1607c478bd9Sstevel@tonic-gate 		if ((logname = (char *)malloc(len)) == NULL)
1617c478bd9Sstevel@tonic-gate 			error("newgrp: Memory request failed");
1627c478bd9Sstevel@tonic-gate 		(void) snprintf(logname, len, "LOGNAME=%s", name);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		envinit[2] = logname;
166*7c1a0576Sgww 		(void) chdir(dir);
1677c478bd9Sstevel@tonic-gate 		envinit[0] = homedir;
1687c478bd9Sstevel@tonic-gate 		if (uid == 0)
1697c478bd9Sstevel@tonic-gate 			envinit[1] = supath;
1707c478bd9Sstevel@tonic-gate 		else
1717c478bd9Sstevel@tonic-gate 			envinit[1] = path;
1727c478bd9Sstevel@tonic-gate 		envinit[3] = NULL;
1737c478bd9Sstevel@tonic-gate 		environ = envinit;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		len = strlen(p->pw_shell) + 2;
1767c478bd9Sstevel@tonic-gate 		if ((shell = (char *)malloc(len)) == NULL)
1777c478bd9Sstevel@tonic-gate 			error("newgrp: Memory request failed");
1787c478bd9Sstevel@tonic-gate 		(void) snprintf(shell, len, "-%s", p->pw_shell);
1797c478bd9Sstevel@tonic-gate 		simple = strrchr(shell, '/');
1807c478bd9Sstevel@tonic-gate 		if (simple) {
1817c478bd9Sstevel@tonic-gate 			*(shell+1) = '\0';
1827c478bd9Sstevel@tonic-gate 			shell = strcat(shell, ++simple);
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	else
1867c478bd9Sstevel@tonic-gate 		shell = p->pw_shell;
1877c478bd9Sstevel@tonic-gate 
188*7c1a0576Sgww 	(void) execl(p->pw_shell, shell, NULL);
189*7c1a0576Sgww 	warn(NS);
190*7c1a0576Sgww 	return (1);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
19368300791Scf46844 void
19468300791Scf46844 warn(char *s)
1957c478bd9Sstevel@tonic-gate {
196*7c1a0576Sgww 	(void) fprintf(stderr, "%s\n", gettext(s));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
19968300791Scf46844 void
20068300791Scf46844 error(char *s)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	warn(s);
2037c478bd9Sstevel@tonic-gate 	exit(1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate gid_t
2077c478bd9Sstevel@tonic-gate chkgrp(gname, p)
2087c478bd9Sstevel@tonic-gate char	*gname;
2097c478bd9Sstevel@tonic-gate struct	passwd *p;
2107c478bd9Sstevel@tonic-gate {
21168300791Scf46844 	char **t;
21268300791Scf46844 	struct group *g;
213*7c1a0576Sgww 	gid_t	gid;
214*7c1a0576Sgww 	adt_session_data_t	*ah;
215*7c1a0576Sgww 	adt_event_data_t	*event;
216*7c1a0576Sgww 	int			sorf = ADT_SUCCESS;
217*7c1a0576Sgww 
218*7c1a0576Sgww 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
219*7c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
220*7c1a0576Sgww 		    "adt_start_session(ADT_newgrp_login): %m");
221*7c1a0576Sgww 	}
222*7c1a0576Sgww 	if ((event = adt_alloc_event(ah, ADT_newgrp_login)) == NULL) {
223*7c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
224*7c1a0576Sgww 		    "adt_alloc_event(ADT_newgrp_login): %m");
225*7c1a0576Sgww 	} else {
226*7c1a0576Sgww 		event->adt_newgrp_login.groupname = gname;
227*7c1a0576Sgww 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	g = getgrnam(gname);
2307c478bd9Sstevel@tonic-gate 	endgrent();
2317c478bd9Sstevel@tonic-gate 	if (g == NULL) {
2327c478bd9Sstevel@tonic-gate 		warn(UG);
233*7c1a0576Sgww 		gid = getgid();
234*7c1a0576Sgww 		goto audit;
2357c478bd9Sstevel@tonic-gate 	}
236*7c1a0576Sgww 	gid = g->gr_gid;
2377c478bd9Sstevel@tonic-gate 	if (p->pw_gid == g->gr_gid || getuid() == 0)
238*7c1a0576Sgww 		goto audit;
2397c478bd9Sstevel@tonic-gate 	for (t = g->gr_mem; *t; ++t) {
2407c478bd9Sstevel@tonic-gate 		if (strcmp(p->pw_name, *t) == 0)
241*7c1a0576Sgww 			goto audit;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	if (*g->gr_passwd) {
2447c478bd9Sstevel@tonic-gate 		if (!isatty(fileno(stdin))) {
2457c478bd9Sstevel@tonic-gate 			error(PD);
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		if (strcmp(g->gr_passwd,
248*7c1a0576Sgww 		    crypt(getpassphrase(PW), g->gr_passwd)) == 0) {
249*7c1a0576Sgww 			goto audit;
2507c478bd9Sstevel@tonic-gate 		}
251*7c1a0576Sgww 		sorf = ADT_FAILURE;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 	warn(NG);
254*7c1a0576Sgww audit:
255*7c1a0576Sgww 	if (adt_put_event(event, sorf, sorf) != 0) {
256*7c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
257*7c1a0576Sgww 		    "adt_put_event(ADT_newgrp, %d): %m", sorf);
2587c478bd9Sstevel@tonic-gate 	}
259*7c1a0576Sgww 	adt_free_event(event);
260*7c1a0576Sgww 	(void) adt_end_session(ah);
2617c478bd9Sstevel@tonic-gate 
262*7c1a0576Sgww 	return (gid);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
26568300791Scf46844 void
26668300791Scf46844 usage(void)
2677c478bd9Sstevel@tonic-gate {
268*7c1a0576Sgww 	(void) fprintf(stderr, gettext(
2697c478bd9Sstevel@tonic-gate 		"usage: newgrp [-l | -] [group]\n"));
2707c478bd9Sstevel@tonic-gate 	exit(2);
2717c478bd9Sstevel@tonic-gate }
272