xref: /illumos-gate/usr/src/cmd/su/su.c (revision 5435d801)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
317c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  *	su [-] [name [arg ...]] change userid, `-' changes environment.
377c478bd9Sstevel@tonic-gate  *	If SULOG is defined, all attempts to su to another user are
387c478bd9Sstevel@tonic-gate  *	logged there.
397c478bd9Sstevel@tonic-gate  *	If CONSOLE is defined, all successful attempts to su to uid 0
407c478bd9Sstevel@tonic-gate  *	are also logged there.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  *	If su cannot create, open, or write entries into SULOG,
437c478bd9Sstevel@tonic-gate  *	(or on the CONSOLE, if defined), the entry will not
447c478bd9Sstevel@tonic-gate  *	be logged -- thus losing a record of the su's attempted
457c478bd9Sstevel@tonic-gate  *	during this period.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <stdio.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/stat.h>
517c478bd9Sstevel@tonic-gate #include <sys/param.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
537c478bd9Sstevel@tonic-gate #include <stdlib.h>
547c478bd9Sstevel@tonic-gate #include <crypt.h>
557c478bd9Sstevel@tonic-gate #include <pwd.h>
567c478bd9Sstevel@tonic-gate #include <shadow.h>
577c478bd9Sstevel@tonic-gate #include <time.h>
587c478bd9Sstevel@tonic-gate #include <signal.h>
597c478bd9Sstevel@tonic-gate #include <fcntl.h>
607c478bd9Sstevel@tonic-gate #include <string.h>
617c478bd9Sstevel@tonic-gate #include <locale.h>
627c478bd9Sstevel@tonic-gate #include <syslog.h>
637c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
647c478bd9Sstevel@tonic-gate #include <grp.h>
657c478bd9Sstevel@tonic-gate #include <deflt.h>
667c478bd9Sstevel@tonic-gate #include <limits.h>
677c478bd9Sstevel@tonic-gate #include <errno.h>
687c478bd9Sstevel@tonic-gate #include <stdarg.h>
697c478bd9Sstevel@tonic-gate 
70*5435d801Sgww #include <bsm/adt.h>
71*5435d801Sgww #include <bsm/adt_event.h>
72*5435d801Sgww 
737c478bd9Sstevel@tonic-gate #include <security/pam_appl.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	PATH	"/usr/bin:"		/* path for users other than root */
767c478bd9Sstevel@tonic-gate #define	SUPATH	"/usr/sbin:/usr/bin"	/* path for root */
777c478bd9Sstevel@tonic-gate #define	SUPRMT	"PS1=# "		/* primary prompt for root */
787c478bd9Sstevel@tonic-gate #define	ELIM 128
797c478bd9Sstevel@tonic-gate #define	ROOT 0
807c478bd9Sstevel@tonic-gate #ifdef	DYNAMIC_SU
817c478bd9Sstevel@tonic-gate #define	EMBEDDED_NAME	"embedded_su"
82*5435d801Sgww #define	DEF_ATTEMPTS	3		/* attempts to change password */
837c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
847c478bd9Sstevel@tonic-gate 
85*5435d801Sgww #define	PW_FALSE	1		/* no password change */
86*5435d801Sgww #define	PW_TRUE		2		/* successful password change */
87*5435d801Sgww #define	PW_FAILED	3		/* failed password change */
88*5435d801Sgww 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Intervals to sleep after failed su
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate #ifndef SLEEPTIME
937c478bd9Sstevel@tonic-gate #define	SLEEPTIME	4
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	DEFAULT_LOGIN "/etc/default/login"
977c478bd9Sstevel@tonic-gate #define	DEFFILE "/etc/default/su"
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate char	*Sulog, *Console;
1017c478bd9Sstevel@tonic-gate char	*Path, *Supath;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Locale variables to be propagated to "su -" environment
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate static char *initvar;
1077c478bd9Sstevel@tonic-gate static char *initenv[] = {
1087c478bd9Sstevel@tonic-gate 	"TZ", "LANG", "LC_CTYPE",
1097c478bd9Sstevel@tonic-gate 	"LC_NUMERIC", "LC_TIME", "LC_COLLATE",
1107c478bd9Sstevel@tonic-gate 	"LC_MONETARY", "LC_MESSAGES", "LC_ALL", 0};
1117c478bd9Sstevel@tonic-gate static char mail[30] = { "MAIL=/var/mail/" };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static void envalt(void);
114*5435d801Sgww static void log(char *, char *, int);
115*5435d801Sgww static void to(int);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate enum messagemode { USAGE, ERR, WARN };
118*5435d801Sgww static void message(enum messagemode, char *, ...);
1197c478bd9Sstevel@tonic-gate 
120*5435d801Sgww static char *alloc_vsprintf(const char *, va_list);
121*5435d801Sgww static char *tail(char *);
1227c478bd9Sstevel@tonic-gate 
123*5435d801Sgww static void audit_success(int, struct passwd *);
124*5435d801Sgww static void audit_failure(int, struct passwd *, int);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #ifdef DYNAMIC_SU
127*5435d801Sgww static void validate(char *, int *);
128*5435d801Sgww static int legalenvvar(char *);
1297c478bd9Sstevel@tonic-gate static int su_conv(int, struct pam_message **, struct pam_response **, void *);
1307c478bd9Sstevel@tonic-gate static int emb_su_conv(int, struct pam_message **, struct pam_response **,
1317c478bd9Sstevel@tonic-gate     void *);
132*5435d801Sgww static void freeresponse(int, struct pam_response **response);
1337c478bd9Sstevel@tonic-gate static struct pam_conv pam_conv = {su_conv, NULL};
1347c478bd9Sstevel@tonic-gate static struct pam_conv emb_pam_conv = {emb_su_conv, NULL};
135*5435d801Sgww static void quotemsg(char *, ...);
1367c478bd9Sstevel@tonic-gate static void readinitblock(void);
137*5435d801Sgww #else	/* !DYNAMIC_SU */
138*5435d801Sgww static void update_audit(struct passwd *pwd);
1397c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
1407c478bd9Sstevel@tonic-gate 
141*5435d801Sgww static pam_handle_t	*pamh = NULL;	/* Authentication handle */
1427c478bd9Sstevel@tonic-gate struct	passwd pwd;
1437c478bd9Sstevel@tonic-gate char	pwdbuf[1024];			/* buffer for getpwnam_r() */
1447c478bd9Sstevel@tonic-gate char	shell[] = "/usr/bin/sh";	/* default shell */
1457c478bd9Sstevel@tonic-gate char	safe_shell[] = "/sbin/sh";	/* "fallback" shell */
1467c478bd9Sstevel@tonic-gate char	su[PATH_MAX] = "su";		/* arg0 for exec of shprog */
1477c478bd9Sstevel@tonic-gate char	homedir[PATH_MAX] = "HOME=";
1487c478bd9Sstevel@tonic-gate char	logname[20] = "LOGNAME=";
1497c478bd9Sstevel@tonic-gate char	*suprmt = SUPRMT;
1507c478bd9Sstevel@tonic-gate char	termtyp[PATH_MAX] = "TERM=";
1517c478bd9Sstevel@tonic-gate char	*term;
1527c478bd9Sstevel@tonic-gate char	shelltyp[PATH_MAX] = "SHELL=";
1537c478bd9Sstevel@tonic-gate char	*hz;
1547c478bd9Sstevel@tonic-gate char	tznam[PATH_MAX];
1557c478bd9Sstevel@tonic-gate char	hzname[10] = "HZ=";
1567c478bd9Sstevel@tonic-gate char	path[PATH_MAX] = "PATH=";
1577c478bd9Sstevel@tonic-gate char	supath[PATH_MAX] = "PATH=";
1587c478bd9Sstevel@tonic-gate char	*envinit[ELIM];
1597c478bd9Sstevel@tonic-gate extern	char **environ;
1607c478bd9Sstevel@tonic-gate char *ttyn;
1617c478bd9Sstevel@tonic-gate char *username;					/* the invoker */
1627c478bd9Sstevel@tonic-gate static	int	dosyslog = 0;			/* use syslog? */
1637c478bd9Sstevel@tonic-gate char	*myname;
1647c478bd9Sstevel@tonic-gate #ifdef	DYNAMIC_SU
165*5435d801Sgww int passreq = 0;
1667c478bd9Sstevel@tonic-gate boolean_t embedded = B_FALSE;
1677c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate int
1707c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate #ifndef DYNAMIC_SU
1737c478bd9Sstevel@tonic-gate 	struct spwd sp;
1747c478bd9Sstevel@tonic-gate 	char  spbuf[1024];		/* buffer for getspnam_r() */
1757c478bd9Sstevel@tonic-gate 	char *password;
176*5435d801Sgww #endif	/* !DYNAMIC_SU */
1777c478bd9Sstevel@tonic-gate 	char *nptr;
1787c478bd9Sstevel@tonic-gate 	char *pshell;
1797c478bd9Sstevel@tonic-gate 	int eflag = 0;
1807c478bd9Sstevel@tonic-gate 	int envidx = 0;
1817c478bd9Sstevel@tonic-gate 	uid_t uid;
1827c478bd9Sstevel@tonic-gate 	gid_t gid;
1837c478bd9Sstevel@tonic-gate 	char *dir, *shprog, *name;
1847c478bd9Sstevel@tonic-gate 	char *ptr;
1857c478bd9Sstevel@tonic-gate 	char *prog = argv[0];
1867c478bd9Sstevel@tonic-gate #ifdef DYNAMIC_SU
1877c478bd9Sstevel@tonic-gate 	int sleeptime = SLEEPTIME;
1887c478bd9Sstevel@tonic-gate 	char **pam_env = 0;
1897c478bd9Sstevel@tonic-gate 	int flags = 0;
1907c478bd9Sstevel@tonic-gate 	int retcode;
1917c478bd9Sstevel@tonic-gate 	int idx = 0;
1927c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
193*5435d801Sgww 	int pw_change = PW_FALSE;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1967c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1977c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
1987c478bd9Sstevel@tonic-gate #endif
1997c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	myname = tail(argv[0]);
2027c478bd9Sstevel@tonic-gate 
203*5435d801Sgww #ifdef	DYNAMIC_SU
2047c478bd9Sstevel@tonic-gate 	if (strcmp(myname, EMBEDDED_NAME) == 0) {
2057c478bd9Sstevel@tonic-gate 		embedded = B_TRUE;
2067c478bd9Sstevel@tonic-gate 		setbuf(stdin, NULL);
2077c478bd9Sstevel@tonic-gate 		setbuf(stdout, NULL);
2087c478bd9Sstevel@tonic-gate 		readinitblock();
2097c478bd9Sstevel@tonic-gate 	}
210*5435d801Sgww #endif	/* DYNAMIC_SU */
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (argc > 1 && *argv[1] == '-') {
2137c478bd9Sstevel@tonic-gate 		/* Explicitly check for just `-' (no trailing chars) */
2147c478bd9Sstevel@tonic-gate 		if (strlen(argv[1]) == 1) {
2157c478bd9Sstevel@tonic-gate 			eflag++;	/* set eflag if `-' is specified */
2167c478bd9Sstevel@tonic-gate 			argv++;
2177c478bd9Sstevel@tonic-gate 			argc--;
2187c478bd9Sstevel@tonic-gate 		} else {
2197c478bd9Sstevel@tonic-gate 			message(USAGE,
2207c478bd9Sstevel@tonic-gate 			    gettext("Usage: %s [-] [ username [ arg ... ] ]"),
2217c478bd9Sstevel@tonic-gate 				prog);
2227c478bd9Sstevel@tonic-gate 			exit(1);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/*
2277c478bd9Sstevel@tonic-gate 	 * Determine specified userid, get their password file entry,
2287c478bd9Sstevel@tonic-gate 	 * and set variables to values in password file entry fields.
2297c478bd9Sstevel@tonic-gate 	 */
2307c478bd9Sstevel@tonic-gate 	if (argc > 1) {
2317c478bd9Sstevel@tonic-gate 		/*
2327c478bd9Sstevel@tonic-gate 		 * Usernames can't start with a `-', so we check for that to
2337c478bd9Sstevel@tonic-gate 		 * catch bad usage (like "su - -c ls").
2347c478bd9Sstevel@tonic-gate 		 */
2357c478bd9Sstevel@tonic-gate 		if (*argv[1] == '-') {
2367c478bd9Sstevel@tonic-gate 			message(USAGE,
2377c478bd9Sstevel@tonic-gate 			    gettext("Usage: %s [-] [ username [ arg ... ] ]"),
2387c478bd9Sstevel@tonic-gate 				prog);
2397c478bd9Sstevel@tonic-gate 			exit(1);
2407c478bd9Sstevel@tonic-gate 		} else
2417c478bd9Sstevel@tonic-gate 			nptr = argv[1];	/* use valid command-line username */
2427c478bd9Sstevel@tonic-gate 	} else
2437c478bd9Sstevel@tonic-gate 		nptr = "root";		/* use default "root" username */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (defopen(DEFFILE) == 0) {
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 		if (Sulog = defread("SULOG="))
2487c478bd9Sstevel@tonic-gate 			Sulog = strdup(Sulog);
2497c478bd9Sstevel@tonic-gate 		if (Console = defread("CONSOLE="))
2507c478bd9Sstevel@tonic-gate 			Console = strdup(Console);
2517c478bd9Sstevel@tonic-gate 		if (Path = defread("PATH="))
2527c478bd9Sstevel@tonic-gate 			Path = strdup(Path);
2537c478bd9Sstevel@tonic-gate 		if (Supath = defread("SUPATH="))
2547c478bd9Sstevel@tonic-gate 			Supath = strdup(Supath);
2557c478bd9Sstevel@tonic-gate 		if ((ptr = defread("SYSLOG=")) != NULL)
2567c478bd9Sstevel@tonic-gate 			dosyslog = strcmp(ptr, "YES") == 0;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 		(void) defopen(NULL);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	(void) strlcat(path, (Path) ? Path : PATH, sizeof (path));
2617c478bd9Sstevel@tonic-gate 	(void) strlcat(supath, (Supath) ? Supath : SUPATH, sizeof (supath));
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if ((ttyn = ttyname(0)) == NULL)
2647c478bd9Sstevel@tonic-gate 		if ((ttyn = ttyname(1)) == NULL)
2657c478bd9Sstevel@tonic-gate 			if ((ttyn = ttyname(2)) == NULL)
2667c478bd9Sstevel@tonic-gate 				ttyn = "/dev/???";
2677c478bd9Sstevel@tonic-gate 	if ((username = cuserid(NULL)) == NULL)
2687c478bd9Sstevel@tonic-gate 		username = "(null)";
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * if Sulog defined, create SULOG, if it does not exist, with
2727c478bd9Sstevel@tonic-gate 	 * mode read/write user. Change owner and group to root
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	if (Sulog != NULL) {
2757c478bd9Sstevel@tonic-gate 		(void) close(open(Sulog, O_WRONLY | O_APPEND | O_CREAT,
2767c478bd9Sstevel@tonic-gate 		    (S_IRUSR|S_IWUSR)));
2777c478bd9Sstevel@tonic-gate 		(void) chown(Sulog, (uid_t)ROOT, (gid_t)ROOT);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate #ifdef DYNAMIC_SU
2817c478bd9Sstevel@tonic-gate 	if (pam_start(embedded ? EMBEDDED_NAME : "su", nptr,
2827c478bd9Sstevel@tonic-gate 	    embedded ? &emb_pam_conv : &pam_conv, &pamh) != PAM_SUCCESS)
2837c478bd9Sstevel@tonic-gate 		exit(1);
2847c478bd9Sstevel@tonic-gate 	if (pam_set_item(pamh, PAM_TTY, ttyn) != PAM_SUCCESS)
2857c478bd9Sstevel@tonic-gate 		exit(1);
2867c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	openlog("su", LOG_CONS, LOG_AUTH);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate #ifdef DYNAMIC_SU
2917c478bd9Sstevel@tonic-gate 
292*5435d801Sgww 	/*
293*5435d801Sgww 	 * Use the same value of sleeptime and password required that
294*5435d801Sgww 	 * login(1) uses.
295*5435d801Sgww 	 * This is obtained by reading the file /etc/default/login
296*5435d801Sgww 	 * using the def*() functions
297*5435d801Sgww 	 */
298*5435d801Sgww 	if (defopen(DEFAULT_LOGIN) == 0) {
299*5435d801Sgww 		if ((ptr = defread("SLEEPTIME=")) != NULL) {
300*5435d801Sgww 			sleeptime = atoi(ptr);
301*5435d801Sgww 			if (sleeptime < 0 || sleeptime > 5)
302*5435d801Sgww 				sleeptime = SLEEPTIME;
303*5435d801Sgww 		}
304*5435d801Sgww 
305*5435d801Sgww 		if ((ptr = defread("PASSREQ=")) != NULL &&
306*5435d801Sgww 		    strcasecmp("YES", ptr) == 0)
307*5435d801Sgww 			passreq = 1;
308*5435d801Sgww 
309*5435d801Sgww 		(void) defopen((char *)NULL);
310*5435d801Sgww 	}
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * Ignore SIGQUIT and SIGINT
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);
3157c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/* call pam_authenticate() to authenticate the user through PAM */
3187c478bd9Sstevel@tonic-gate 	if (getpwnam_r(nptr, &pwd, pwdbuf, sizeof (pwdbuf)) == NULL)
3197c478bd9Sstevel@tonic-gate 		retcode = PAM_USER_UNKNOWN;
3207c478bd9Sstevel@tonic-gate 	else if ((flags = (getuid() != (uid_t)ROOT)) != 0) {
3217c478bd9Sstevel@tonic-gate 		retcode = pam_authenticate(pamh, 0);
3227c478bd9Sstevel@tonic-gate 	} else /* root user does not need to authenticate */
3237c478bd9Sstevel@tonic-gate 		retcode = PAM_SUCCESS;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (retcode != PAM_SUCCESS) {
3267c478bd9Sstevel@tonic-gate 		/*
327*5435d801Sgww 		 * 1st step: audit and log the error.
3287c478bd9Sstevel@tonic-gate 		 * 2nd step: sleep.
3297c478bd9Sstevel@tonic-gate 		 * 3rd step: print out message to user.
3307c478bd9Sstevel@tonic-gate 		 */
331*5435d801Sgww 		audit_failure(PW_FALSE, NULL, retcode);
3327c478bd9Sstevel@tonic-gate 		switch (retcode) {
3337c478bd9Sstevel@tonic-gate 		case PAM_USER_UNKNOWN:
3347c478bd9Sstevel@tonic-gate 			closelog();
3357c478bd9Sstevel@tonic-gate 			(void) sleep(sleeptime);
3367c478bd9Sstevel@tonic-gate 			message(ERR, gettext("Unknown id: %s"), nptr);
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		case PAM_AUTH_ERR:
3407c478bd9Sstevel@tonic-gate 			if (Sulog != NULL)
3417c478bd9Sstevel@tonic-gate 				log(Sulog, nptr, 0);	/* log entry */
3427c478bd9Sstevel@tonic-gate 			if (dosyslog)
3437c478bd9Sstevel@tonic-gate 				syslog(LOG_CRIT, "'su %s' failed for %s on %s",
3447c478bd9Sstevel@tonic-gate 				    pwd.pw_name, username, ttyn);
3457c478bd9Sstevel@tonic-gate 			closelog();
3467c478bd9Sstevel@tonic-gate 			(void) sleep(sleeptime);
3477c478bd9Sstevel@tonic-gate 			message(ERR, gettext("Sorry"));
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		case PAM_CONV_ERR:
3517c478bd9Sstevel@tonic-gate 		default:
3527c478bd9Sstevel@tonic-gate 			if (dosyslog)
3537c478bd9Sstevel@tonic-gate 				syslog(LOG_CRIT, "'su %s' failed for %s on %s",
3547c478bd9Sstevel@tonic-gate 				    pwd.pw_name, username, ttyn);
3557c478bd9Sstevel@tonic-gate 			closelog();
3567c478bd9Sstevel@tonic-gate 			(void) sleep(sleeptime);
3577c478bd9Sstevel@tonic-gate 			message(ERR, gettext("Sorry"));
3587c478bd9Sstevel@tonic-gate 			break;
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_DFL);
3627c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_DFL);
3637c478bd9Sstevel@tonic-gate 		exit(1);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	if (flags)
366*5435d801Sgww 		validate(username, &pw_change);
3677c478bd9Sstevel@tonic-gate 	if (pam_setcred(pamh, PAM_REINITIALIZE_CRED) != PAM_SUCCESS) {
3687c478bd9Sstevel@tonic-gate 		message(ERR, gettext("unable to set credentials"));
3697c478bd9Sstevel@tonic-gate 		exit(2);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 	if (dosyslog)
3727c478bd9Sstevel@tonic-gate 		syslog(getuid() == 0 ? LOG_INFO : LOG_NOTICE,
3737c478bd9Sstevel@tonic-gate 		    "'su %s' succeeded for %s on %s",
3747c478bd9Sstevel@tonic-gate 		    pwd.pw_name, username, ttyn);
3757c478bd9Sstevel@tonic-gate 	closelog();
3767c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_DFL);
3777c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_DFL);
378*5435d801Sgww #else	/* !DYNAMIC_SU */
3797c478bd9Sstevel@tonic-gate 	if ((getpwnam_r(nptr, &pwd, pwdbuf, sizeof (pwdbuf)) == NULL) ||
3807c478bd9Sstevel@tonic-gate 	    (getspnam_r(nptr, &sp, spbuf, sizeof (spbuf)) == NULL)) {
3817c478bd9Sstevel@tonic-gate 		message(ERR, gettext("Unknown id: %s"), nptr);
382*5435d801Sgww 		audit_failure(PW_FALSE, NULL, PAM_USER_UNKNOWN);
3837c478bd9Sstevel@tonic-gate 		closelog();
3847c478bd9Sstevel@tonic-gate 		exit(1);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * Prompt for password if invoking user is not root or
3897c478bd9Sstevel@tonic-gate 	 * if specified(new) user requires a password
3907c478bd9Sstevel@tonic-gate 	 */
3917c478bd9Sstevel@tonic-gate 	if (sp.sp_pwdp[0] == '\0' || getuid() == (uid_t)ROOT)
3927c478bd9Sstevel@tonic-gate 		goto ok;
3937c478bd9Sstevel@tonic-gate 	password = getpass(gettext("Password:"));
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	if ((strcmp(sp.sp_pwdp, crypt(password, sp.sp_pwdp)) != 0)) {
3967c478bd9Sstevel@tonic-gate 		/* clear password file entry */
3977c478bd9Sstevel@tonic-gate 		(void) memset((void *)spbuf, 0, sizeof (spbuf));
3987c478bd9Sstevel@tonic-gate 		if (Sulog != NULL)
3997c478bd9Sstevel@tonic-gate 			log(Sulog, nptr, 0);    /* log entry */
4007c478bd9Sstevel@tonic-gate 		message(ERR, gettext("Sorry"));
401*5435d801Sgww 		audit_failure(PW_FALSE, NULL, PAM_AUTH_ERR);
4027c478bd9Sstevel@tonic-gate 		if (dosyslog)
4037c478bd9Sstevel@tonic-gate 			syslog(LOG_CRIT, "'su %s' failed for %s on %s",
4047c478bd9Sstevel@tonic-gate 			    pwd.pw_name, username, ttyn);
4057c478bd9Sstevel@tonic-gate 		closelog();
4067c478bd9Sstevel@tonic-gate 		exit(2);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	/* clear password file entry */
4097c478bd9Sstevel@tonic-gate 	(void) memset((void *)spbuf, 0, sizeof (spbuf));
4107c478bd9Sstevel@tonic-gate ok:
411*5435d801Sgww 	/* update audit session in a non-pam environment */
412*5435d801Sgww 	update_audit(&pwd);
4137c478bd9Sstevel@tonic-gate 	if (dosyslog)
4147c478bd9Sstevel@tonic-gate 		syslog(getuid() == 0 ? LOG_INFO : LOG_NOTICE,
4157c478bd9Sstevel@tonic-gate 		    "'su %s' succeeded for %s on %s",
4167c478bd9Sstevel@tonic-gate 		    pwd.pw_name, username, ttyn);
417*5435d801Sgww #endif	/* DYNAMIC_SU */
4187c478bd9Sstevel@tonic-gate 
419*5435d801Sgww 	audit_success(pw_change, &pwd);
4207c478bd9Sstevel@tonic-gate 	uid = pwd.pw_uid;
4217c478bd9Sstevel@tonic-gate 	gid = pwd.pw_gid;
4227c478bd9Sstevel@tonic-gate 	dir = strdup(pwd.pw_dir);
4237c478bd9Sstevel@tonic-gate 	shprog = strdup(pwd.pw_shell);
4247c478bd9Sstevel@tonic-gate 	name = strdup(pwd.pw_name);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (Sulog != NULL)
4277c478bd9Sstevel@tonic-gate 		log(Sulog, nptr, 1);	/* log entry */
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/* set user and group ids to specified user */
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/* set the real (and effective) GID */
4327c478bd9Sstevel@tonic-gate 	if (setgid(gid) == -1) {
4337c478bd9Sstevel@tonic-gate 		message(ERR, gettext("Invalid GID"));
4347c478bd9Sstevel@tonic-gate 		exit(2);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 	/* Initialize the supplementary group access list. */
4377c478bd9Sstevel@tonic-gate 	if (!nptr)
4387c478bd9Sstevel@tonic-gate 		exit(2);
4397c478bd9Sstevel@tonic-gate 	if (initgroups(nptr, gid) == -1) {
4407c478bd9Sstevel@tonic-gate 		exit(2);
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	/* set the real (and effective) UID */
4437c478bd9Sstevel@tonic-gate 	if (setuid(uid) == -1) {
4447c478bd9Sstevel@tonic-gate 		message(ERR, gettext("Invalid UID"));
4457c478bd9Sstevel@tonic-gate 		exit(2);
4467c478bd9Sstevel@tonic-gate 	}
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * If new user's shell field is neither NULL nor equal to /usr/bin/sh,
4507c478bd9Sstevel@tonic-gate 	 * set:
4517c478bd9Sstevel@tonic-gate 	 *
4527c478bd9Sstevel@tonic-gate 	 *	pshell = their shell
4537c478bd9Sstevel@tonic-gate 	 *	su = [-]last component of shell's pathname
4547c478bd9Sstevel@tonic-gate 	 *
4557c478bd9Sstevel@tonic-gate 	 * Otherwise, set the shell to /usr/bin/sh and set argv[0] to '[-]su'.
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	if (shprog[0] != '\0' && strcmp(shell, shprog) != 0) {
4587c478bd9Sstevel@tonic-gate 		char *p;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		pshell = shprog;
4617c478bd9Sstevel@tonic-gate 		(void) strcpy(su, eflag ? "-" : "");
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		if ((p = strrchr(pshell, '/')) != NULL)
4647c478bd9Sstevel@tonic-gate 			(void) strlcat(su, p + 1, sizeof (su));
4657c478bd9Sstevel@tonic-gate 		else
4667c478bd9Sstevel@tonic-gate 			(void) strlcat(su, pshell, sizeof (su));
4677c478bd9Sstevel@tonic-gate 	} else {
4687c478bd9Sstevel@tonic-gate 		pshell = shell;
4697c478bd9Sstevel@tonic-gate 		(void) strcpy(su, eflag ? "-su" : "su");
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/*
4737c478bd9Sstevel@tonic-gate 	 * set environment variables for new user;
4747c478bd9Sstevel@tonic-gate 	 * arg0 for exec of shprog must now contain `-'
4757c478bd9Sstevel@tonic-gate 	 * so that environment of new user is given
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 	if (eflag) {
4787c478bd9Sstevel@tonic-gate 		int j;
4797c478bd9Sstevel@tonic-gate 		char *var;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		if (strlen(dir) == 0) {
4827c478bd9Sstevel@tonic-gate 			(void) strcpy(dir, "/");
4837c478bd9Sstevel@tonic-gate 			message(WARN, gettext("No directory! Using home=/"));
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 		(void) strlcat(homedir, dir, sizeof (homedir));
4867c478bd9Sstevel@tonic-gate 		(void) strlcat(logname, name, sizeof (logname));
4877c478bd9Sstevel@tonic-gate 		if (hz = getenv("HZ"))
4887c478bd9Sstevel@tonic-gate 			(void) strlcat(hzname, hz, sizeof (hzname));
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		(void) strlcat(shelltyp, pshell, sizeof (shelltyp));
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if (chdir(dir) < 0) {
4937c478bd9Sstevel@tonic-gate 			message(ERR, gettext("No directory!"));
4947c478bd9Sstevel@tonic-gate 			exit(1);
4957c478bd9Sstevel@tonic-gate 		}
4967c478bd9Sstevel@tonic-gate 		envinit[envidx = 0] = homedir;
4977c478bd9Sstevel@tonic-gate 		envinit[++envidx] = ((uid == (uid_t)ROOT) ? supath : path);
4987c478bd9Sstevel@tonic-gate 		envinit[++envidx] = logname;
4997c478bd9Sstevel@tonic-gate 		envinit[++envidx] = hzname;
5007c478bd9Sstevel@tonic-gate 		if ((term = getenv("TERM")) != NULL) {
5017c478bd9Sstevel@tonic-gate 			(void) strlcat(termtyp, term, sizeof (termtyp));
5027c478bd9Sstevel@tonic-gate 			envinit[++envidx] = termtyp;
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 		envinit[++envidx] = shelltyp;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		(void) strlcat(mail, name, sizeof (mail));
5077c478bd9Sstevel@tonic-gate 		envinit[++envidx] = mail;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		/*
5107c478bd9Sstevel@tonic-gate 		 * Fetch the relevant locale/TZ environment variables from
5117c478bd9Sstevel@tonic-gate 		 * the inherited environment.
5127c478bd9Sstevel@tonic-gate 		 *
5137c478bd9Sstevel@tonic-gate 		 * We have a priority here for setting TZ. If TZ is set in
5147c478bd9Sstevel@tonic-gate 		 * in the inherited environment, that value remains top
5157c478bd9Sstevel@tonic-gate 		 * priority. If the file /etc/default/login has TIMEZONE set,
5167c478bd9Sstevel@tonic-gate 		 * that has second highest priority.
5177c478bd9Sstevel@tonic-gate 		 */
5187c478bd9Sstevel@tonic-gate 		tznam[0] = '\0';
5197c478bd9Sstevel@tonic-gate 		for (j = 0; initenv[j] != 0; j++) {
5207c478bd9Sstevel@tonic-gate 			if (initvar = getenv(initenv[j])) {
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 				/*
5237c478bd9Sstevel@tonic-gate 				 * Skip over values beginning with '/' for
5247c478bd9Sstevel@tonic-gate 				 * security.
5257c478bd9Sstevel@tonic-gate 				 */
5267c478bd9Sstevel@tonic-gate 				if (initvar[0] == '/')  continue;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 				if (strcmp(initenv[j], "TZ") == 0) {
5297c478bd9Sstevel@tonic-gate 					(void) strcpy(tznam, "TZ=");
5307c478bd9Sstevel@tonic-gate 					(void) strlcat(tznam, initvar,
5317c478bd9Sstevel@tonic-gate 						sizeof (tznam));
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 				} else {
5347c478bd9Sstevel@tonic-gate 					var = (char *)
5357c478bd9Sstevel@tonic-gate 						malloc(strlen(initenv[j])
5367c478bd9Sstevel@tonic-gate 							+ strlen(initvar)
5377c478bd9Sstevel@tonic-gate 							+ 2);
5387c478bd9Sstevel@tonic-gate 					(void) strcpy(var, initenv[j]);
5397c478bd9Sstevel@tonic-gate 					(void) strcat(var, "=");
5407c478bd9Sstevel@tonic-gate 					(void) strcat(var, initvar);
5417c478bd9Sstevel@tonic-gate 					envinit[++envidx] = var;
5427c478bd9Sstevel@tonic-gate 				}
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		/*
5477c478bd9Sstevel@tonic-gate 		 * Check if TZ was found. If not then try to read it from
5487c478bd9Sstevel@tonic-gate 		 * /etc/default/login.
5497c478bd9Sstevel@tonic-gate 		 */
5507c478bd9Sstevel@tonic-gate 		if (tznam[0] == '\0') {
5517c478bd9Sstevel@tonic-gate 			if (defopen(DEFAULT_LOGIN) == 0) {
5527c478bd9Sstevel@tonic-gate 				if (initvar = defread("TIMEZONE=")) {
5537c478bd9Sstevel@tonic-gate 					(void) strcpy(tznam, "TZ=");
5547c478bd9Sstevel@tonic-gate 					(void) strlcat(tznam, initvar,
5557c478bd9Sstevel@tonic-gate 							sizeof (tznam));
5567c478bd9Sstevel@tonic-gate 				}
5577c478bd9Sstevel@tonic-gate 				(void) defopen(NULL);
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		if (tznam[0] != '\0')
5627c478bd9Sstevel@tonic-gate 			envinit[++envidx] = tznam;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate #ifdef DYNAMIC_SU
5657c478bd9Sstevel@tonic-gate 		/*
5667c478bd9Sstevel@tonic-gate 		 * set the PAM environment variables -
5677c478bd9Sstevel@tonic-gate 		 * check for legal environment variables
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		if ((pam_env = pam_getenvlist(pamh)) != 0) {
5707c478bd9Sstevel@tonic-gate 			while (pam_env[idx] != 0) {
5717c478bd9Sstevel@tonic-gate 				if (envidx + 2 < ELIM &&
5727c478bd9Sstevel@tonic-gate 				    legalenvvar(pam_env[idx])) {
5737c478bd9Sstevel@tonic-gate 					envinit[++envidx] = pam_env[idx];
5747c478bd9Sstevel@tonic-gate 				}
5757c478bd9Sstevel@tonic-gate 				idx++;
5767c478bd9Sstevel@tonic-gate 			}
5777c478bd9Sstevel@tonic-gate 		}
578*5435d801Sgww #endif	/* DYNAMIC_SU */
5797c478bd9Sstevel@tonic-gate 		envinit[++envidx] = NULL;
5807c478bd9Sstevel@tonic-gate 		environ = envinit;
5817c478bd9Sstevel@tonic-gate 	} else {
5827c478bd9Sstevel@tonic-gate 		char **pp = environ, **qq, *p;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		while ((p = *pp) != NULL) {
5857c478bd9Sstevel@tonic-gate 			if (*p == 'L' && p[1] == 'D' && p[2] == '_') {
5867c478bd9Sstevel@tonic-gate 				for (qq = pp; (*qq = qq[1]) != NULL; qq++)
5877c478bd9Sstevel@tonic-gate 					;
5887c478bd9Sstevel@tonic-gate 				/* pp is not advanced */
5897c478bd9Sstevel@tonic-gate 			} else {
5907c478bd9Sstevel@tonic-gate 				pp++;
5917c478bd9Sstevel@tonic-gate 			}
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate #ifdef DYNAMIC_SU
5967c478bd9Sstevel@tonic-gate 	if (pamh)
5977c478bd9Sstevel@tonic-gate 		(void) pam_end(pamh, PAM_SUCCESS);
598*5435d801Sgww #endif	/* DYNAMIC_SU */
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * if new user is root:
6027c478bd9Sstevel@tonic-gate 	 *	if CONSOLE defined, log entry there;
6037c478bd9Sstevel@tonic-gate 	 *	if eflag not set, change environment to that of root.
6047c478bd9Sstevel@tonic-gate 	 */
6057c478bd9Sstevel@tonic-gate 	if (uid == (uid_t)ROOT) {
6067c478bd9Sstevel@tonic-gate 		if (Console != NULL)
6077c478bd9Sstevel@tonic-gate 			if (strcmp(ttyn, Console) != 0) {
6087c478bd9Sstevel@tonic-gate 				(void) signal(SIGALRM, to);
6097c478bd9Sstevel@tonic-gate 				(void) alarm(30);
6107c478bd9Sstevel@tonic-gate 				log(Console, nptr, 1);
6117c478bd9Sstevel@tonic-gate 				(void) alarm(0);
6127c478bd9Sstevel@tonic-gate 			}
6137c478bd9Sstevel@tonic-gate 		if (!eflag)
6147c478bd9Sstevel@tonic-gate 			envalt();
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * Default for SIGCPU and SIGXFSZ.  Shells inherit
6197c478bd9Sstevel@tonic-gate 	 * signal disposition from parent.  And the
6207c478bd9Sstevel@tonic-gate 	 * shells should have default dispositions for these
6217c478bd9Sstevel@tonic-gate 	 * signals.
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	(void) signal(SIGXCPU, SIG_DFL);
6247c478bd9Sstevel@tonic-gate 	(void) signal(SIGXFSZ, SIG_DFL);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate #ifdef	DYNAMIC_SU
6277c478bd9Sstevel@tonic-gate 	if (embedded) {
6287c478bd9Sstevel@tonic-gate 		(void) puts("SUCCESS");
6297c478bd9Sstevel@tonic-gate 		/*
6307c478bd9Sstevel@tonic-gate 		 * After this point, we're no longer talking the
6317c478bd9Sstevel@tonic-gate 		 * embedded_su protocol, so turn it off.
6327c478bd9Sstevel@tonic-gate 		 */
6337c478bd9Sstevel@tonic-gate 		embedded = B_FALSE;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * if additional arguments, exec shell program with array
6397c478bd9Sstevel@tonic-gate 	 * of pointers to arguments:
6407c478bd9Sstevel@tonic-gate 	 *	-> if shell = default, then su = [-]su
6417c478bd9Sstevel@tonic-gate 	 *	-> if shell != default, then su = [-]last component of
6427c478bd9Sstevel@tonic-gate 	 *						shell's pathname
6437c478bd9Sstevel@tonic-gate 	 *
6447c478bd9Sstevel@tonic-gate 	 * if no additional arguments, exec shell with arg0 of su
6457c478bd9Sstevel@tonic-gate 	 * where:
6467c478bd9Sstevel@tonic-gate 	 *	-> if shell = default, then su = [-]su
6477c478bd9Sstevel@tonic-gate 	 *	-> if shell != default, then su = [-]last component of
6487c478bd9Sstevel@tonic-gate 	 *						shell's pathname
6497c478bd9Sstevel@tonic-gate 	 */
6507c478bd9Sstevel@tonic-gate 	if (argc > 2) {
6517c478bd9Sstevel@tonic-gate 		argv[1] = su;
6527c478bd9Sstevel@tonic-gate 		(void) execv(pshell, &argv[1]);
6537c478bd9Sstevel@tonic-gate 	} else
6547c478bd9Sstevel@tonic-gate 		(void) execl(pshell, su, 0);
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * Try to clean up after an administrator who has made a mistake
6597c478bd9Sstevel@tonic-gate 	 * configuring root's shell; if root's shell is other than /sbin/sh,
6607c478bd9Sstevel@tonic-gate 	 * try exec'ing /sbin/sh instead.
6617c478bd9Sstevel@tonic-gate 	 */
6627c478bd9Sstevel@tonic-gate 	if ((uid == (uid_t)ROOT) && (strcmp(name, "root") == 0) &&
6637c478bd9Sstevel@tonic-gate 	    (strcmp(safe_shell, pshell) != 0)) {
6647c478bd9Sstevel@tonic-gate 		message(WARN,
6657c478bd9Sstevel@tonic-gate 		    gettext("No shell %s.  Trying fallback shell %s."),
6667c478bd9Sstevel@tonic-gate 		    pshell, safe_shell);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		if (eflag) {
6697c478bd9Sstevel@tonic-gate 			(void) strcpy(su, "-sh");
6707c478bd9Sstevel@tonic-gate 			(void) strlcpy(shelltyp + strlen("SHELL="),
6717c478bd9Sstevel@tonic-gate 			    safe_shell, sizeof (shelltyp) - strlen("SHELL="));
6727c478bd9Sstevel@tonic-gate 		} else {
6737c478bd9Sstevel@tonic-gate 			(void) strcpy(su, "sh");
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		if (argc > 2) {
6777c478bd9Sstevel@tonic-gate 			argv[1] = su;
6787c478bd9Sstevel@tonic-gate 			(void) execv(safe_shell, &argv[1]);
6797c478bd9Sstevel@tonic-gate 		} else {
6807c478bd9Sstevel@tonic-gate 			(void) execl(safe_shell, su, 0);
6817c478bd9Sstevel@tonic-gate 		}
6827c478bd9Sstevel@tonic-gate 		message(ERR, gettext("Couldn't exec fallback shell %s: %s"),
6837c478bd9Sstevel@tonic-gate 		    safe_shell, strerror(errno));
6847c478bd9Sstevel@tonic-gate 	} else {
6857c478bd9Sstevel@tonic-gate 		message(ERR, gettext("No shell"));
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	return (3);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * Environment altering routine -
6927c478bd9Sstevel@tonic-gate  *	This routine is called when a user is su'ing to root
6937c478bd9Sstevel@tonic-gate  *	without specifying the - flag.
6947c478bd9Sstevel@tonic-gate  *	The user's PATH and PS1 variables are reset
6957c478bd9Sstevel@tonic-gate  *	to the correct value for root.
6967c478bd9Sstevel@tonic-gate  *	All of the user's other environment variables retain
6977c478bd9Sstevel@tonic-gate  *	their current values after the su (if they are exported).
6987c478bd9Sstevel@tonic-gate  */
6997c478bd9Sstevel@tonic-gate static void
7007c478bd9Sstevel@tonic-gate envalt(void)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * If user has PATH variable in their environment, change its value
7047c478bd9Sstevel@tonic-gate 	 *		to /bin:/etc:/usr/bin ;
7057c478bd9Sstevel@tonic-gate 	 * if user does not have PATH variable, add it to the user's
7067c478bd9Sstevel@tonic-gate 	 *		environment;
7077c478bd9Sstevel@tonic-gate 	 * if either of the above fail, an error message is printed.
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 	if (putenv(supath) != 0) {
7107c478bd9Sstevel@tonic-gate 		message(ERR,
7117c478bd9Sstevel@tonic-gate 		    gettext("unable to obtain memory to expand environment"));
7127c478bd9Sstevel@tonic-gate 		exit(4);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * If user has PROMPT variable in their environment, change its value
7177c478bd9Sstevel@tonic-gate 	 *		to # ;
7187c478bd9Sstevel@tonic-gate 	 * if user does not have PROMPT variable, add it to the user's
7197c478bd9Sstevel@tonic-gate 	 *		environment;
7207c478bd9Sstevel@tonic-gate 	 * if either of the above fail, an error message is printed.
7217c478bd9Sstevel@tonic-gate 	 */
7227c478bd9Sstevel@tonic-gate 	if (putenv(suprmt) != 0) {
7237c478bd9Sstevel@tonic-gate 		message(ERR,
7247c478bd9Sstevel@tonic-gate 		    gettext("unable to obtain memory to expand environment"));
7257c478bd9Sstevel@tonic-gate 		exit(4);
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate  * Logging routine -
7317c478bd9Sstevel@tonic-gate  *	where = SULOG or CONSOLE
7327c478bd9Sstevel@tonic-gate  *	towho = specified user ( user being su'ed to )
7337c478bd9Sstevel@tonic-gate  *	how = 0 if su attempt failed; 1 if su attempt succeeded
7347c478bd9Sstevel@tonic-gate  */
7357c478bd9Sstevel@tonic-gate static void
7367c478bd9Sstevel@tonic-gate log(char *where, char *towho, int how)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	FILE *logf;
7397c478bd9Sstevel@tonic-gate 	time_t now;
7407c478bd9Sstevel@tonic-gate 	struct tm *tmp;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/*
7437c478bd9Sstevel@tonic-gate 	 * open SULOG or CONSOLE - if open fails, return
7447c478bd9Sstevel@tonic-gate 	 */
7457c478bd9Sstevel@tonic-gate 	if ((logf = fopen(where, "a")) == NULL)
7467c478bd9Sstevel@tonic-gate 		return;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	now = time(0);
7497c478bd9Sstevel@tonic-gate 	tmp = localtime(&now);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/*
7527c478bd9Sstevel@tonic-gate 	 * write entry into SULOG or onto CONSOLE - if write fails, return
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 	(void) fprintf(logf, "SU %.2d/%.2d %.2d:%.2d %c %s %s-%s\n",
7557c478bd9Sstevel@tonic-gate 	    tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min,
7567c478bd9Sstevel@tonic-gate 	    how ? '+' : '-', ttyn + sizeof ("/dev/") - 1, username, towho);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	(void) fclose(logf);	/* close SULOG or CONSOLE */
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7627c478bd9Sstevel@tonic-gate static void
7637c478bd9Sstevel@tonic-gate to(int sig)
7647c478bd9Sstevel@tonic-gate {}
7657c478bd9Sstevel@tonic-gate 
766*5435d801Sgww /*
767*5435d801Sgww  * audit_success - audit successful su
768*5435d801Sgww  *
769*5435d801Sgww  *	Entry	process audit context established -- i.e., pam_setcred()
770*5435d801Sgww  *			or equivalent called.
771*5435d801Sgww  *		pw_change = PW_TRUE, if successful password change audit
772*5435d801Sgww  *				required.
773*5435d801Sgww  *		pwd = passwd entry for new user.
774*5435d801Sgww  */
775*5435d801Sgww 
776*5435d801Sgww static void
777*5435d801Sgww audit_success(int pw_change, struct passwd *pwd)
778*5435d801Sgww {
779*5435d801Sgww 	adt_session_data_t	*ah = NULL;
780*5435d801Sgww 	adt_event_data_t	*event;
781*5435d801Sgww 
782*5435d801Sgww 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
783*5435d801Sgww 		syslog(LOG_AUTH | LOG_ALERT,
784*5435d801Sgww 		    "adt_start_session(ADT_su): %m");
785*5435d801Sgww 		return;
786*5435d801Sgww 	}
787*5435d801Sgww 	/* since proc uid/gid not yet updated */
788*5435d801Sgww 	if (adt_set_user(ah, pwd->pw_uid, pwd->pw_gid, pwd->pw_uid,
789*5435d801Sgww 	    pwd->pw_gid, NULL, ADT_USER) != 0) {
790*5435d801Sgww 		syslog(LOG_AUTH | LOG_ERR,
791*5435d801Sgww 		    "adt_set_user(ADT_su, ADT_FAILURE): %m");
792*5435d801Sgww 	}
793*5435d801Sgww 	if ((event = adt_alloc_event(ah, ADT_su)) == NULL) {
794*5435d801Sgww 		syslog(LOG_AUTH | LOG_ALERT, "adt_alloc_event(ADT_su): %m");
795*5435d801Sgww 	} else if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
796*5435d801Sgww 		syslog(LOG_AUTH | LOG_ALERT,
797*5435d801Sgww 		    "adt_put_event(ADT_su, ADT_SUCCESS): %m");
798*5435d801Sgww 	}
799*5435d801Sgww 
800*5435d801Sgww 	if (pw_change == PW_TRUE) {
801*5435d801Sgww 		/* Also audit password change */
802*5435d801Sgww 		adt_free_event(event);
803*5435d801Sgww 		if ((event = adt_alloc_event(ah, ADT_passwd)) == NULL) {
804*5435d801Sgww 			syslog(LOG_AUTH | LOG_ALERT,
805*5435d801Sgww 			    "adt_alloc_event(ADT_passwd): %m");
806*5435d801Sgww 		} else if (adt_put_event(event, ADT_SUCCESS,
807*5435d801Sgww 		    ADT_SUCCESS) != 0) {
808*5435d801Sgww 			syslog(LOG_AUTH | LOG_ALERT,
809*5435d801Sgww 			    "adt_put_event(ADT_passwd, ADT_SUCCESS): %m");
810*5435d801Sgww 		}
811*5435d801Sgww 	}
812*5435d801Sgww 	adt_free_event(event);
813*5435d801Sgww }
814*5435d801Sgww 
815*5435d801Sgww 
816*5435d801Sgww /*
817*5435d801Sgww  * audit_failure - audit failed su
818*5435d801Sgww  *
819*5435d801Sgww  *	Entry	New audit context not set.
820*5435d801Sgww  *		pw_change == PW_FALSE, if no password change requested.
821*5435d801Sgww  *			     PW_FAILED, if failed password change audit
822*5435d801Sgww  *				      required.
823*5435d801Sgww  *		pwent = NULL, or password entry to use.
824*5435d801Sgww  *		pamerr = PAM error code; reason for failure.
825*5435d801Sgww  */
826*5435d801Sgww 
827*5435d801Sgww static void
828*5435d801Sgww audit_failure(int pw_change, struct passwd *pwd, int pamerr)
829*5435d801Sgww {
830*5435d801Sgww 	adt_session_data_t	*ah;	/* audit session handle */
831*5435d801Sgww 	adt_event_data_t	*event;	/* event to generate */
832*5435d801Sgww 
833*5435d801Sgww 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
834*5435d801Sgww 		syslog(LOG_AUTH | LOG_ALERT,
835*5435d801Sgww 		    "adt_start_session(ADT_su, ADT_FAILURE): %m");
836*5435d801Sgww 		return;
837*5435d801Sgww 	}
838*5435d801Sgww 	if (pwd != NULL) {
839*5435d801Sgww 		/* target user authenticated, merge audit state */
840*5435d801Sgww 		if (adt_set_user(ah, pwd->pw_uid, pwd->pw_gid, pwd->pw_uid,
841*5435d801Sgww 		    pwd->pw_gid, NULL, ADT_UPDATE) != 0) {
842*5435d801Sgww 			syslog(LOG_AUTH | LOG_ERR,
843*5435d801Sgww 			    "adt_set_user(ADT_su, ADT_FAILURE): %m");
844*5435d801Sgww 		}
845*5435d801Sgww 	}
846*5435d801Sgww 	if ((event = adt_alloc_event(ah, ADT_su)) == NULL) {
847*5435d801Sgww 		syslog(LOG_AUTH | LOG_ALERT,
848*5435d801Sgww 		    "adt_alloc_event(ADT_su, ADT_FAILURE): %m");
849*5435d801Sgww 		return;
850*5435d801Sgww 	} else if (adt_put_event(event, ADT_FAILURE,
851*5435d801Sgww 	    ADT_FAIL_PAM + pamerr) != 0) {
852*5435d801Sgww 		syslog(LOG_AUTH | LOG_ALERT,
853*5435d801Sgww 		    "adt_put_event(ADT_su(ADT_FAIL, %s): %m",
854*5435d801Sgww 		    pam_strerror(pamh, pamerr));
855*5435d801Sgww 	}
856*5435d801Sgww 	if (pw_change != PW_FALSE) {
857*5435d801Sgww 		/* Also audit password change failed */
858*5435d801Sgww 		adt_free_event(event);
859*5435d801Sgww 		if ((event = adt_alloc_event(ah, ADT_passwd)) == NULL) {
860*5435d801Sgww 			syslog(LOG_AUTH | LOG_ALERT,
861*5435d801Sgww 			    "adt_alloc_event(ADT_passwd): %m");
862*5435d801Sgww 		} else if (adt_put_event(event, ADT_FAILURE,
863*5435d801Sgww 		    ADT_FAIL_PAM + pamerr) != 0) {
864*5435d801Sgww 			syslog(LOG_AUTH | LOG_ALERT,
865*5435d801Sgww 			    "adt_put_event(ADT_passwd, ADT_FAILURE): %m");
866*5435d801Sgww 		}
867*5435d801Sgww 	}
868*5435d801Sgww 	adt_free_event(event);
869*5435d801Sgww }
870*5435d801Sgww 
8717c478bd9Sstevel@tonic-gate #ifdef DYNAMIC_SU
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate  * su_conv():
8747c478bd9Sstevel@tonic-gate  *	This is the conv (conversation) function called from
8757c478bd9Sstevel@tonic-gate  *	a PAM authentication module to print error messages
8767c478bd9Sstevel@tonic-gate  *	or garner information from the user.
8777c478bd9Sstevel@tonic-gate  */
8787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8797c478bd9Sstevel@tonic-gate static int
8807c478bd9Sstevel@tonic-gate su_conv(int num_msg, struct pam_message **msg, struct pam_response **response,
8817c478bd9Sstevel@tonic-gate     void *appdata_ptr)
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	struct pam_message	*m;
8847c478bd9Sstevel@tonic-gate 	struct pam_response	*r;
8857c478bd9Sstevel@tonic-gate 	char			*temp;
8867c478bd9Sstevel@tonic-gate 	int			k;
8877c478bd9Sstevel@tonic-gate 	char			respbuf[PAM_MAX_RESP_SIZE];
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (num_msg <= 0)
8907c478bd9Sstevel@tonic-gate 		return (PAM_CONV_ERR);
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	*response = (struct pam_response *)calloc(num_msg,
8937c478bd9Sstevel@tonic-gate 	    sizeof (struct pam_response));
8947c478bd9Sstevel@tonic-gate 	if (*response == NULL)
8957c478bd9Sstevel@tonic-gate 		return (PAM_BUF_ERR);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	k = num_msg;
8987c478bd9Sstevel@tonic-gate 	m = *msg;
8997c478bd9Sstevel@tonic-gate 	r = *response;
9007c478bd9Sstevel@tonic-gate 	while (k--) {
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		switch (m->msg_style) {
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
9057c478bd9Sstevel@tonic-gate 			errno = 0;
9067c478bd9Sstevel@tonic-gate 			temp = getpassphrase(m->msg);
9077c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
9087c478bd9Sstevel@tonic-gate 				return (PAM_CONV_ERR);
9097c478bd9Sstevel@tonic-gate 			if (temp != NULL) {
9107c478bd9Sstevel@tonic-gate 				r->resp = strdup(temp);
9117c478bd9Sstevel@tonic-gate 				if (r->resp == NULL) {
9127c478bd9Sstevel@tonic-gate 					freeresponse(num_msg, response);
9137c478bd9Sstevel@tonic-gate 					return (PAM_BUF_ERR);
9147c478bd9Sstevel@tonic-gate 				}
9157c478bd9Sstevel@tonic-gate 			}
9167c478bd9Sstevel@tonic-gate 			break;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
9197c478bd9Sstevel@tonic-gate 			if (m->msg != NULL) {
9207c478bd9Sstevel@tonic-gate 				(void) fputs(m->msg, stdout);
9217c478bd9Sstevel@tonic-gate 			}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 			(void) fgets(respbuf, sizeof (respbuf), stdin);
9247c478bd9Sstevel@tonic-gate 			temp = strchr(respbuf, '\n');
9257c478bd9Sstevel@tonic-gate 			if (temp != NULL)
9267c478bd9Sstevel@tonic-gate 				*temp = '\0';
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 			r->resp = strdup(respbuf);
9297c478bd9Sstevel@tonic-gate 			if (r->resp == NULL) {
9307c478bd9Sstevel@tonic-gate 				freeresponse(num_msg, response);
9317c478bd9Sstevel@tonic-gate 				return (PAM_BUF_ERR);
9327c478bd9Sstevel@tonic-gate 			}
9337c478bd9Sstevel@tonic-gate 			break;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 		case PAM_ERROR_MSG:
9367c478bd9Sstevel@tonic-gate 			if (m->msg != NULL) {
9377c478bd9Sstevel@tonic-gate 				(void) fputs(m->msg, stderr);
9387c478bd9Sstevel@tonic-gate 				(void) fputs("\n", stderr);
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 			break;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		case PAM_TEXT_INFO:
9437c478bd9Sstevel@tonic-gate 			if (m->msg != NULL) {
9447c478bd9Sstevel@tonic-gate 				(void) fputs(m->msg, stdout);
9457c478bd9Sstevel@tonic-gate 				(void) fputs("\n", stdout);
9467c478bd9Sstevel@tonic-gate 			}
9477c478bd9Sstevel@tonic-gate 			break;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		default:
9507c478bd9Sstevel@tonic-gate 			break;
9517c478bd9Sstevel@tonic-gate 		}
9527c478bd9Sstevel@tonic-gate 		m++;
9537c478bd9Sstevel@tonic-gate 		r++;
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	return (PAM_SUCCESS);
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate /*
9597c478bd9Sstevel@tonic-gate  * emb_su_conv():
9607c478bd9Sstevel@tonic-gate  *	This is the conv (conversation) function called from
9617c478bd9Sstevel@tonic-gate  *	a PAM authentication module to print error messages
9627c478bd9Sstevel@tonic-gate  *	or garner information from the user.
9637c478bd9Sstevel@tonic-gate  *	This version is used for embedded_su.
9647c478bd9Sstevel@tonic-gate  */
9657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9667c478bd9Sstevel@tonic-gate static int
9677c478bd9Sstevel@tonic-gate emb_su_conv(int num_msg, struct pam_message **msg,
9687c478bd9Sstevel@tonic-gate     struct pam_response **response, void *appdata_ptr)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate 	struct pam_message	*m;
9717c478bd9Sstevel@tonic-gate 	struct pam_response	*r;
9727c478bd9Sstevel@tonic-gate 	char			*temp;
9737c478bd9Sstevel@tonic-gate 	int			k;
9747c478bd9Sstevel@tonic-gate 	char			respbuf[PAM_MAX_RESP_SIZE];
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if (num_msg <= 0)
9777c478bd9Sstevel@tonic-gate 		return (PAM_CONV_ERR);
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	*response = (struct pam_response *)calloc(num_msg,
9807c478bd9Sstevel@tonic-gate 	    sizeof (struct pam_response));
9817c478bd9Sstevel@tonic-gate 	if (*response == NULL)
9827c478bd9Sstevel@tonic-gate 		return (PAM_BUF_ERR);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/* First, send the prompts */
9857c478bd9Sstevel@tonic-gate 	(void) printf("CONV %d\n", num_msg);
9867c478bd9Sstevel@tonic-gate 	k = num_msg;
9877c478bd9Sstevel@tonic-gate 	m = *msg;
9887c478bd9Sstevel@tonic-gate 	while (k--) {
9897c478bd9Sstevel@tonic-gate 		switch (m->msg_style) {
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
9927c478bd9Sstevel@tonic-gate 			(void) puts("PAM_PROMPT_ECHO_OFF");
9937c478bd9Sstevel@tonic-gate 			goto msg_common;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
9967c478bd9Sstevel@tonic-gate 			(void) puts("PAM_PROMPT_ECHO_ON");
9977c478bd9Sstevel@tonic-gate 			goto msg_common;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		case PAM_ERROR_MSG:
10007c478bd9Sstevel@tonic-gate 			(void) puts("PAM_ERROR_MSG");
10017c478bd9Sstevel@tonic-gate 			goto msg_common;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		case PAM_TEXT_INFO:
10047c478bd9Sstevel@tonic-gate 			(void) puts("PAM_TEXT_INFO");
10057c478bd9Sstevel@tonic-gate 			/* fall through to msg_common */
10067c478bd9Sstevel@tonic-gate msg_common:
10077c478bd9Sstevel@tonic-gate 			if (m->msg == NULL)
10087c478bd9Sstevel@tonic-gate 				quotemsg(NULL);
10097c478bd9Sstevel@tonic-gate 			else
10107c478bd9Sstevel@tonic-gate 				quotemsg("%s", m->msg);
10117c478bd9Sstevel@tonic-gate 			break;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		default:
10147c478bd9Sstevel@tonic-gate 			break;
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 		m++;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/* Next, collect the responses */
10207c478bd9Sstevel@tonic-gate 	k = num_msg;
10217c478bd9Sstevel@tonic-gate 	m = *msg;
10227c478bd9Sstevel@tonic-gate 	r = *response;
10237c478bd9Sstevel@tonic-gate 	while (k--) {
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		switch (m->msg_style) {
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_OFF:
10287c478bd9Sstevel@tonic-gate 		case PAM_PROMPT_ECHO_ON:
10297c478bd9Sstevel@tonic-gate 			(void) fgets(respbuf, sizeof (respbuf), stdin);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 			temp = strchr(respbuf, '\n');
10327c478bd9Sstevel@tonic-gate 			if (temp != NULL)
10337c478bd9Sstevel@tonic-gate 				*temp = '\0';
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 			r->resp = strdup(respbuf);
10367c478bd9Sstevel@tonic-gate 			if (r->resp == NULL) {
10377c478bd9Sstevel@tonic-gate 				freeresponse(num_msg, response);
10387c478bd9Sstevel@tonic-gate 				return (PAM_BUF_ERR);
10397c478bd9Sstevel@tonic-gate 			}
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 			break;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		case PAM_ERROR_MSG:
10447c478bd9Sstevel@tonic-gate 		case PAM_TEXT_INFO:
10457c478bd9Sstevel@tonic-gate 			break;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 		default:
10487c478bd9Sstevel@tonic-gate 			break;
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 		m++;
10517c478bd9Sstevel@tonic-gate 		r++;
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 	return (PAM_SUCCESS);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate static void
10577c478bd9Sstevel@tonic-gate freeresponse(int num_msg, struct pam_response **response)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 	struct pam_response *r;
10607c478bd9Sstevel@tonic-gate 	int i;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	/* free responses */
10637c478bd9Sstevel@tonic-gate 	r = *response;
10647c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_msg; i++, r++) {
10657c478bd9Sstevel@tonic-gate 		if (r->resp != NULL) {
10667c478bd9Sstevel@tonic-gate 			/* Zap it in case it's a password */
10677c478bd9Sstevel@tonic-gate 			(void) memset(r->resp, '\0', strlen(r->resp));
10687c478bd9Sstevel@tonic-gate 			free(r->resp);
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 	free(*response);
10727c478bd9Sstevel@tonic-gate 	*response = NULL;
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /*
10767c478bd9Sstevel@tonic-gate  * Print a message, applying quoting for lines starting with '.'.
10777c478bd9Sstevel@tonic-gate  *
10787c478bd9Sstevel@tonic-gate  * I18n note:  \n is "safe" in all locales, and all locales use
10797c478bd9Sstevel@tonic-gate  * a high-bit-set character to start multibyte sequences, so
10807c478bd9Sstevel@tonic-gate  * scanning for a \n followed by a '.' is safe.
10817c478bd9Sstevel@tonic-gate  */
10827c478bd9Sstevel@tonic-gate static void
10837c478bd9Sstevel@tonic-gate quotemsg(char *fmt, ...)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate 	if (fmt != NULL) {
10867c478bd9Sstevel@tonic-gate 		char *msg;
10877c478bd9Sstevel@tonic-gate 		char *p;
10887c478bd9Sstevel@tonic-gate 		boolean_t bol;
10897c478bd9Sstevel@tonic-gate 		va_list v;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		va_start(v, fmt);
10927c478bd9Sstevel@tonic-gate 		msg = alloc_vsprintf(fmt, v);
10937c478bd9Sstevel@tonic-gate 		va_end(v);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		bol = B_TRUE;
10967c478bd9Sstevel@tonic-gate 		for (p = msg; *p != '\0'; p++) {
10977c478bd9Sstevel@tonic-gate 			if (bol) {
10987c478bd9Sstevel@tonic-gate 				if (*p == '.')
10997c478bd9Sstevel@tonic-gate 					(void) putchar('.');
11007c478bd9Sstevel@tonic-gate 				bol = B_FALSE;
11017c478bd9Sstevel@tonic-gate 			}
11027c478bd9Sstevel@tonic-gate 			(void) putchar(*p);
11037c478bd9Sstevel@tonic-gate 			if (*p == '\n')
11047c478bd9Sstevel@tonic-gate 				bol = B_TRUE;
11057c478bd9Sstevel@tonic-gate 		}
11067c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
11077c478bd9Sstevel@tonic-gate 		free(msg);
11087c478bd9Sstevel@tonic-gate 	}
11097c478bd9Sstevel@tonic-gate 	(void) putchar('.');
11107c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate /*
11147c478bd9Sstevel@tonic-gate  * validate - Check that the account is valid for switching to.
11157c478bd9Sstevel@tonic-gate  */
11167c478bd9Sstevel@tonic-gate static void
1117*5435d801Sgww validate(char *usernam, int *pw_change)
11187c478bd9Sstevel@tonic-gate {
1119*5435d801Sgww 	int error;
1120*5435d801Sgww 	int flag = 0;
1121*5435d801Sgww 	int tries;
1122*5435d801Sgww 
1123*5435d801Sgww 	if (passreq)
1124*5435d801Sgww 		flag = PAM_DISALLOW_NULL_AUTHTOK;
11257c478bd9Sstevel@tonic-gate 
1126*5435d801Sgww 	if ((error = pam_acct_mgmt(pamh, flag)) != PAM_SUCCESS) {
11277c478bd9Sstevel@tonic-gate 		if (Sulog != NULL)
11287c478bd9Sstevel@tonic-gate 			log(Sulog, pwd.pw_name, 0);    /* log entry */
11297c478bd9Sstevel@tonic-gate 		if (error == PAM_NEW_AUTHTOK_REQD) {
1130*5435d801Sgww 			tries = 0;
11317c478bd9Sstevel@tonic-gate 			message(ERR, gettext("Password for user "
1132*5435d801Sgww 			    "'%s' has expired"), pwd.pw_name);
1133*5435d801Sgww 			while ((error = pam_chauthtok(pamh, 0)) !=
1134*5435d801Sgww 			    PAM_SUCCESS) {
1135*5435d801Sgww 				if ((error == PAM_AUTHTOK_ERR ||
1136*5435d801Sgww 				    error == PAM_TRY_AGAIN) &&
1137*5435d801Sgww 				    (tries++ < DEF_ATTEMPTS)) {
1138*5435d801Sgww 					continue;
1139*5435d801Sgww 				}
1140*5435d801Sgww 				message(ERR, gettext("Sorry"));
1141*5435d801Sgww 				audit_failure(PW_FAILED, &pwd, error);
1142*5435d801Sgww 				if (dosyslog)
1143*5435d801Sgww 					syslog(LOG_CRIT,
1144*5435d801Sgww 					    "'su %s' failed for %s on %s",
1145*5435d801Sgww 					    pwd.pw_name, usernam, ttyn);
1146*5435d801Sgww 				closelog();
1147*5435d801Sgww 				exit(1);
1148*5435d801Sgww 			}
1149*5435d801Sgww 			*pw_change = PW_TRUE;
1150*5435d801Sgww 			return;
11517c478bd9Sstevel@tonic-gate 		} else {
11527c478bd9Sstevel@tonic-gate 			message(ERR, gettext("Sorry"));
1153*5435d801Sgww 			audit_failure(PW_FALSE, &pwd, error);
11547c478bd9Sstevel@tonic-gate 			if (dosyslog)
11557c478bd9Sstevel@tonic-gate 			    syslog(LOG_CRIT, "'su %s' failed for %s on %s",
11567c478bd9Sstevel@tonic-gate 				pwd.pw_name, usernam, ttyn);
11577c478bd9Sstevel@tonic-gate 			closelog();
11587c478bd9Sstevel@tonic-gate 			exit(3);
11597c478bd9Sstevel@tonic-gate 		}
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate static char *illegal[] = {
11647c478bd9Sstevel@tonic-gate 	"SHELL=",
11657c478bd9Sstevel@tonic-gate 	"HOME=",
11667c478bd9Sstevel@tonic-gate 	"LOGNAME=",
11677c478bd9Sstevel@tonic-gate #ifndef NO_MAIL
11687c478bd9Sstevel@tonic-gate 	"MAIL=",
11697c478bd9Sstevel@tonic-gate #endif
11707c478bd9Sstevel@tonic-gate 	"CDPATH=",
11717c478bd9Sstevel@tonic-gate 	"IFS=",
11727c478bd9Sstevel@tonic-gate 	"PATH=",
11737c478bd9Sstevel@tonic-gate 	"TZ=",
11747c478bd9Sstevel@tonic-gate 	"HZ=",
11757c478bd9Sstevel@tonic-gate 	"TERM=",
11767c478bd9Sstevel@tonic-gate 	0
11777c478bd9Sstevel@tonic-gate };
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate /*
11807c478bd9Sstevel@tonic-gate  * legalenvvar - can PAM modules insert this environmental variable?
11817c478bd9Sstevel@tonic-gate  */
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate static int
11847c478bd9Sstevel@tonic-gate legalenvvar(char *s)
11857c478bd9Sstevel@tonic-gate {
11867c478bd9Sstevel@tonic-gate 	register char **p;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	for (p = illegal; *p; p++)
11897c478bd9Sstevel@tonic-gate 		if (strncmp(s, *p, strlen(*p)) == 0)
11907c478bd9Sstevel@tonic-gate 			return (0);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (s[0] == 'L' && s[1] == 'D' && s[2] == '_')
11937c478bd9Sstevel@tonic-gate 		return (0);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	return (1);
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate /*
11997c478bd9Sstevel@tonic-gate  * The embedded_su protocol allows the client application to supply
12007c478bd9Sstevel@tonic-gate  * an initialization block terminated by a line with just a "." on it.
12017c478bd9Sstevel@tonic-gate  *
12027c478bd9Sstevel@tonic-gate  * This initialization block is currently unused, reserved for future
12037c478bd9Sstevel@tonic-gate  * expansion.  Ignore it.  This is made very slightly more complex by
12047c478bd9Sstevel@tonic-gate  * the desire to cleanly ignore input lines of any length, while still
12057c478bd9Sstevel@tonic-gate  * correctly detecting a line with just a "." on it.
12067c478bd9Sstevel@tonic-gate  *
12077c478bd9Sstevel@tonic-gate  * I18n note:  It appears that none of the Solaris-supported locales
12087c478bd9Sstevel@tonic-gate  * use 0x0a for any purpose other than newline, so looking for '\n'
12097c478bd9Sstevel@tonic-gate  * seems safe.
12107c478bd9Sstevel@tonic-gate  * All locales use high-bit-set leadin characters for their multi-byte
12117c478bd9Sstevel@tonic-gate  * sequences, so a line consisting solely of ".\n" is what it appears
12127c478bd9Sstevel@tonic-gate  * to be.
12137c478bd9Sstevel@tonic-gate  */
12147c478bd9Sstevel@tonic-gate static void
12157c478bd9Sstevel@tonic-gate readinitblock(void)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate 	char buf[100];
12187c478bd9Sstevel@tonic-gate 	boolean_t bol;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	bol = B_TRUE;
12217c478bd9Sstevel@tonic-gate 	for (;;) {
12227c478bd9Sstevel@tonic-gate 		if (fgets(buf, sizeof (buf), stdin) == NULL)
12237c478bd9Sstevel@tonic-gate 			return;
12247c478bd9Sstevel@tonic-gate 		if (bol && strcmp(buf, ".\n") == 0)
12257c478bd9Sstevel@tonic-gate 			return;
12267c478bd9Sstevel@tonic-gate 		bol = (strchr(buf, '\n') != NULL);
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate }
1229*5435d801Sgww #else	/* !DYNAMIC_SU */
1230*5435d801Sgww static void
1231*5435d801Sgww update_audit(struct passwd *pwd)
1232*5435d801Sgww {
1233*5435d801Sgww 	adt_session_data_t	*ah;	/* audit session handle */
1234*5435d801Sgww 
1235*5435d801Sgww 	if (adt_start_session(&ah, NULL, ADT_USE_PROC_DATA) != 0) {
1236*5435d801Sgww 		message(ERR, gettext("Sorry"));
1237*5435d801Sgww 		if (dosyslog)
1238*5435d801Sgww 			syslog(LOG_CRIT, "'su %s' failed for %s "
1239*5435d801Sgww 			    "cannot start audit session %m",
1240*5435d801Sgww 			    pwd->pw_name, username);
1241*5435d801Sgww 		closelog();
1242*5435d801Sgww 		exit(2);
1243*5435d801Sgww 	}
1244*5435d801Sgww 	if (adt_set_user(ah, pwd->pw_uid, pwd->pw_gid, pwd->pw_uid,
1245*5435d801Sgww 	    pwd->pw_gid, NULL, ADT_UPDATE) != 0) {
1246*5435d801Sgww 		if (dosyslog)
1247*5435d801Sgww 			syslog(LOG_CRIT, "'su %s' failed for %s "
1248*5435d801Sgww 			    "cannot update audit session %m",
1249*5435d801Sgww 			    pwd->pw_name, username);
1250*5435d801Sgww 		closelog();
1251*5435d801Sgww 		exit(2);
1252*5435d801Sgww 	}
1253*5435d801Sgww }
12547c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate /*
12577c478bd9Sstevel@tonic-gate  * Report an error, either a fatal one, a warning, or a usage message,
12587c478bd9Sstevel@tonic-gate  * depending on the mode parameter.
12597c478bd9Sstevel@tonic-gate  */
12607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12617c478bd9Sstevel@tonic-gate static void
12627c478bd9Sstevel@tonic-gate message(enum messagemode mode, char *fmt, ...)
12637c478bd9Sstevel@tonic-gate {
12647c478bd9Sstevel@tonic-gate 	char *s;
12657c478bd9Sstevel@tonic-gate 	va_list v;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	va_start(v, fmt);
12687c478bd9Sstevel@tonic-gate 	s = alloc_vsprintf(fmt, v);
12697c478bd9Sstevel@tonic-gate 	va_end(v);
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate #ifdef	DYNAMIC_SU
12727c478bd9Sstevel@tonic-gate 	if (embedded) {
12737c478bd9Sstevel@tonic-gate 		if (mode == WARN) {
12747c478bd9Sstevel@tonic-gate 			(void) printf("CONV 1\n");
12757c478bd9Sstevel@tonic-gate 			(void) printf("PAM_ERROR_MSG\n");
12767c478bd9Sstevel@tonic-gate 		} else { /* ERR, USAGE */
12777c478bd9Sstevel@tonic-gate 			(void) printf("ERROR\n");
12787c478bd9Sstevel@tonic-gate 		}
12797c478bd9Sstevel@tonic-gate 		if (mode == USAGE) {
12807c478bd9Sstevel@tonic-gate 			quotemsg("%s", s);
12817c478bd9Sstevel@tonic-gate 		} else { /* ERR, WARN */
12827c478bd9Sstevel@tonic-gate 			quotemsg("%s: %s", myname, s);
12837c478bd9Sstevel@tonic-gate 		}
12847c478bd9Sstevel@tonic-gate 	} else {
12857c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
12867c478bd9Sstevel@tonic-gate 		if (mode == USAGE) {
12877c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n", s);
12887c478bd9Sstevel@tonic-gate 		} else { /* ERR, WARN */
12897c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s\n", myname, s);
12907c478bd9Sstevel@tonic-gate 		}
12917c478bd9Sstevel@tonic-gate #ifdef	DYNAMIC_SU
12927c478bd9Sstevel@tonic-gate 	}
12937c478bd9Sstevel@tonic-gate #endif	/* DYNAMIC_SU */
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	free(s);
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate /*
12997c478bd9Sstevel@tonic-gate  * Return a pointer to the last path component of a.
13007c478bd9Sstevel@tonic-gate  */
13017c478bd9Sstevel@tonic-gate static char *
13027c478bd9Sstevel@tonic-gate tail(char *a)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate 	char *p;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	p = strrchr(a, '/');
13077c478bd9Sstevel@tonic-gate 	if (p == NULL)
13087c478bd9Sstevel@tonic-gate 		p = a;
13097c478bd9Sstevel@tonic-gate 	else
13107c478bd9Sstevel@tonic-gate 		p++;	/* step over the '/' */
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	return (p);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate static char *
13167c478bd9Sstevel@tonic-gate alloc_vsprintf(const char *fmt, va_list ap1)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	va_list ap2;
13197c478bd9Sstevel@tonic-gate 	int n;
13207c478bd9Sstevel@tonic-gate 	char buf[1];
13217c478bd9Sstevel@tonic-gate 	char *s;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	/*
13247c478bd9Sstevel@tonic-gate 	 * We need to scan the argument list twice.  Save off a copy
13257c478bd9Sstevel@tonic-gate 	 * of the argument list pointer(s) for the second pass.  Note that
13267c478bd9Sstevel@tonic-gate 	 * we are responsible for va_end'ing our copy.
13277c478bd9Sstevel@tonic-gate 	 */
13287c478bd9Sstevel@tonic-gate 	va_copy(ap2, ap1);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	/*
13317c478bd9Sstevel@tonic-gate 	 * vsnprintf into a dummy to get a length.  One might
13327c478bd9Sstevel@tonic-gate 	 * think that passing 0 as the length to snprintf would
13337c478bd9Sstevel@tonic-gate 	 * do what we want, but it's defined not to.
13347c478bd9Sstevel@tonic-gate 	 *
13357c478bd9Sstevel@tonic-gate 	 * Perhaps we should sprintf into a 100 character buffer
13367c478bd9Sstevel@tonic-gate 	 * or something like that, to avoid two calls to snprintf
13377c478bd9Sstevel@tonic-gate 	 * in most cases.
13387c478bd9Sstevel@tonic-gate 	 */
13397c478bd9Sstevel@tonic-gate 	n = vsnprintf(buf, sizeof (buf), fmt, ap2);
13407c478bd9Sstevel@tonic-gate 	va_end(ap2);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	/*
13437c478bd9Sstevel@tonic-gate 	 * Allocate an appropriately-sized buffer.
13447c478bd9Sstevel@tonic-gate 	 */
13457c478bd9Sstevel@tonic-gate 	s = malloc(n + 1);
13467c478bd9Sstevel@tonic-gate 	if (s == NULL) {
13477c478bd9Sstevel@tonic-gate 		perror("malloc");
13487c478bd9Sstevel@tonic-gate 		exit(4);
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	(void) vsnprintf(s, n+1, fmt, ap1);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	return (s);
13547c478bd9Sstevel@tonic-gate }
1355