xref: /titanic_50/usr/src/cmd/newgrp/newgrp.c (revision d362b7492b8bcb5ed70f92aa0a6a39bc93b059de)
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 #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>
507c1a0576Sgww #include <syslog.h>
517c1a0576Sgww #include <unistd.h>
527c1a0576Sgww 
537c1a0576Sgww #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;
76*d362b749Svk199839 static void warn(char *s);
7768300791Scf46844 void usage(void);
7868300791Scf46844 
7968300791Scf46844 int
main(int argc,char * argv[])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;
1667c1a0576Sgww 		(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 
1887c1a0576Sgww 	(void) execl(p->pw_shell, shell, NULL);
1897c1a0576Sgww 	warn(NS);
1907c1a0576Sgww 	return (1);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
193*d362b749Svk199839 static void
warn(char * s)19468300791Scf46844 warn(char *s)
1957c478bd9Sstevel@tonic-gate {
1967c1a0576Sgww 	(void) fprintf(stderr, "%s\n", gettext(s));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
19968300791Scf46844 void
error(char * s)20068300791Scf46844 error(char *s)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	warn(s);
2037c478bd9Sstevel@tonic-gate 	exit(1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
20616cac786Sgww void
put_event(char * gname,int sorf)20716cac786Sgww put_event(char *gname, int sorf)
2087c478bd9Sstevel@tonic-gate {
2097c1a0576Sgww 	adt_session_data_t	*ah;
2107c1a0576Sgww 	adt_event_data_t	*event;
2117c1a0576Sgww 
2127c1a0576Sgww 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
2137c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
2147c1a0576Sgww 		    "adt_start_session(ADT_newgrp_login): %m");
2157c1a0576Sgww 	}
2167c1a0576Sgww 	if ((event = adt_alloc_event(ah, ADT_newgrp_login)) == NULL) {
2177c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
2187c1a0576Sgww 		    "adt_alloc_event(ADT_newgrp_login): %m");
2197c1a0576Sgww 	} else {
2207c1a0576Sgww 		event->adt_newgrp_login.groupname = gname;
2217c1a0576Sgww 	}
2227c478bd9Sstevel@tonic-gate 
2237c1a0576Sgww 	if (adt_put_event(event, sorf, sorf) != 0) {
2247c1a0576Sgww 		syslog(LOG_AUTH | LOG_ALERT,
2257c1a0576Sgww 		    "adt_put_event(ADT_newgrp, %d): %m", sorf);
2267c478bd9Sstevel@tonic-gate 	}
2277c1a0576Sgww 	adt_free_event(event);
2287c1a0576Sgww 	(void) adt_end_session(ah);
22916cac786Sgww }
2307c478bd9Sstevel@tonic-gate 
23116cac786Sgww gid_t
chkgrp(gname,p)23216cac786Sgww chkgrp(gname, p)
23316cac786Sgww char	*gname;
23416cac786Sgww struct	passwd *p;
23516cac786Sgww {
23616cac786Sgww 	char **t;
23716cac786Sgww 	struct group *g;
23816cac786Sgww 
23916cac786Sgww 	g = getgrnam(gname);
24016cac786Sgww 	endgrent();
24116cac786Sgww 	if (g == NULL) {
24216cac786Sgww 		warn(UG);
24316cac786Sgww 		put_event(gname, ADT_FAILURE);
24416cac786Sgww 		return (getgid());
24516cac786Sgww 	}
24616cac786Sgww 	if (p->pw_gid == g->gr_gid || getuid() == 0) {
24716cac786Sgww 		put_event(gname, ADT_SUCCESS);
24816cac786Sgww 		return (g->gr_gid);
24916cac786Sgww 	}
25016cac786Sgww 	for (t = g->gr_mem; *t; ++t) {
25116cac786Sgww 		if (strcmp(p->pw_name, *t) == 0) {
25216cac786Sgww 			put_event(gname, ADT_SUCCESS);
25316cac786Sgww 			return (g->gr_gid);
25416cac786Sgww 		}
25516cac786Sgww 	}
25616cac786Sgww 	if (*g->gr_passwd) {
25716cac786Sgww 		if (!isatty(fileno(stdin))) {
25816cac786Sgww 			put_event(gname, ADT_FAILURE);
25916cac786Sgww 			error(PD);
26016cac786Sgww 		}
26116cac786Sgww 		if (strcmp(g->gr_passwd,
26216cac786Sgww 		    crypt(getpassphrase(PW), g->gr_passwd)) == 0) {
26316cac786Sgww 			put_event(gname, ADT_SUCCESS);
26416cac786Sgww 			return (g->gr_gid);
26516cac786Sgww 		}
26616cac786Sgww 	}
26716cac786Sgww 	put_event(gname, ADT_FAILURE);
26816cac786Sgww 	warn(NG);
26916cac786Sgww 	return (getgid());
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
27268300791Scf46844 void
usage(void)27368300791Scf46844 usage(void)
2747c478bd9Sstevel@tonic-gate {
2757c1a0576Sgww 	(void) fprintf(stderr, gettext(
2767c478bd9Sstevel@tonic-gate 	    "usage: newgrp [-l | -] [group]\n"));
2777c478bd9Sstevel@tonic-gate 	exit(2);
2787c478bd9Sstevel@tonic-gate }
279