xref: /titanic_52/usr/src/cmd/zlogin/zlogin.c (revision 123807fb8274cf8e22f09732e34303ba6baf55a5)
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
5facf4a8dSllai1  * Common Development and Distribution License (the "License").
6facf4a8dSllai1  * 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 /*
22facf4a8dSllai1  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * zlogin provides three types of login which allow users in the global
307c478bd9Sstevel@tonic-gate  * zone to access non-global zones.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * - "interactive login" is similar to rlogin(1); for example, the user could
337c478bd9Sstevel@tonic-gate  *   issue 'zlogin my-zone' or 'zlogin -e ^ -l me my-zone'.   The user is
347c478bd9Sstevel@tonic-gate  *   granted a new pty (which is then shoved into the zone), and an I/O
357c478bd9Sstevel@tonic-gate  *   loop between parent and child processes takes care of the interactive
367c478bd9Sstevel@tonic-gate  *   session.  In this mode, login(1) (and its -c option, which means
377c478bd9Sstevel@tonic-gate  *   "already authenticated") is employed to take care of the initialization
387c478bd9Sstevel@tonic-gate  *   of the user's session.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * - "non-interactive login" is similar to su(1M); the user could issue
417c478bd9Sstevel@tonic-gate  *   'zlogin my-zone ls -l' and the command would be run as specified.
427c478bd9Sstevel@tonic-gate  *   In this mode, zlogin sets up pipes as the communication channel, and
437c478bd9Sstevel@tonic-gate  *   'su' is used to do the login setup work.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * - "console login" is the equivalent to accessing the tip line for a
467c478bd9Sstevel@tonic-gate  *   zone.  For example, the user can issue 'zlogin -C my-zone'.
477c478bd9Sstevel@tonic-gate  *   In this mode, zlogin contacts the zoneadmd process via unix domain
487c478bd9Sstevel@tonic-gate  *   socket.  If zoneadmd is not running, it starts it.  This allows the
497c478bd9Sstevel@tonic-gate  *   console to be available anytime the zone is installed, regardless of
507c478bd9Sstevel@tonic-gate  *   whether it is running.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <sys/socket.h>
547c478bd9Sstevel@tonic-gate #include <sys/termios.h>
557c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
567c478bd9Sstevel@tonic-gate #include <sys/stat.h>
577c478bd9Sstevel@tonic-gate #include <sys/types.h>
587c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
597c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
609acbbeafSnn35248 #include <sys/brand.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <alloca.h>
637c478bd9Sstevel@tonic-gate #include <assert.h>
647c478bd9Sstevel@tonic-gate #include <ctype.h>
657c478bd9Sstevel@tonic-gate #include <door.h>
667c478bd9Sstevel@tonic-gate #include <errno.h>
677c478bd9Sstevel@tonic-gate #include <poll.h>
687c478bd9Sstevel@tonic-gate #include <priv.h>
697c478bd9Sstevel@tonic-gate #include <pwd.h>
707c478bd9Sstevel@tonic-gate #include <unistd.h>
717c478bd9Sstevel@tonic-gate #include <utmpx.h>
727c478bd9Sstevel@tonic-gate #include <sac.h>
737c478bd9Sstevel@tonic-gate #include <signal.h>
747c478bd9Sstevel@tonic-gate #include <stdarg.h>
757c478bd9Sstevel@tonic-gate #include <stdio.h>
767c478bd9Sstevel@tonic-gate #include <stdlib.h>
777c478bd9Sstevel@tonic-gate #include <string.h>
787c478bd9Sstevel@tonic-gate #include <strings.h>
797c478bd9Sstevel@tonic-gate #include <stropts.h>
807c478bd9Sstevel@tonic-gate #include <wait.h>
817c478bd9Sstevel@tonic-gate #include <zone.h>
827c478bd9Sstevel@tonic-gate #include <fcntl.h>
837c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
847c478bd9Sstevel@tonic-gate #include <libintl.h>
857c478bd9Sstevel@tonic-gate #include <locale.h>
867c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
877c478bd9Sstevel@tonic-gate #include <libcontract.h>
889acbbeafSnn35248 #include <libbrand.h>
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int masterfd;
917c478bd9Sstevel@tonic-gate static struct termios save_termios;
927c478bd9Sstevel@tonic-gate static struct termios effective_termios;
937c478bd9Sstevel@tonic-gate static int save_fd;
947c478bd9Sstevel@tonic-gate static struct winsize winsize;
957c478bd9Sstevel@tonic-gate static volatile int dead;
967c478bd9Sstevel@tonic-gate static volatile pid_t child_pid = -1;
977c478bd9Sstevel@tonic-gate static int interactive = 0;
987c478bd9Sstevel@tonic-gate static priv_set_t *dropprivs;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static int nocmdchar = 0;
1017c478bd9Sstevel@tonic-gate static int failsafe = 0;
1027c478bd9Sstevel@tonic-gate static char cmdchar = '~';
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int pollerr = 0;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static const char *pname;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
1097c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
1107c478bd9Sstevel@tonic-gate #endif
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define	SUPATH	"/usr/bin/su"
1137c478bd9Sstevel@tonic-gate #define	FAILSAFESHELL	"/sbin/sh"
1147c478bd9Sstevel@tonic-gate #define	DEFAULTSHELL	"/sbin/sh"
1157c478bd9Sstevel@tonic-gate #define	DEF_PATH	"/usr/sbin:/usr/bin"
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * See canonify() below.  CANONIFY_LEN is the maximum length that a
1197c478bd9Sstevel@tonic-gate  * "canonical" sequence will expand to (backslash, three octal digits, NUL).
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate #define	CANONIFY_LEN 5
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static void
1247c478bd9Sstevel@tonic-gate usage(void)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: %s [ -CES ] [ -e cmdchar ] "
1277c478bd9Sstevel@tonic-gate 	    "[-l user] zonename [command [args ...] ]\n"), pname);
1287c478bd9Sstevel@tonic-gate 	exit(2);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static const char *
1327c478bd9Sstevel@tonic-gate getpname(const char *arg0)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	const char *p = strrchr(arg0, '/');
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (p == NULL)
1377c478bd9Sstevel@tonic-gate 		p = arg0;
1387c478bd9Sstevel@tonic-gate 	else
1397c478bd9Sstevel@tonic-gate 		p++;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	pname = p;
1427c478bd9Sstevel@tonic-gate 	return (p);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static void
1467c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	va_list alist;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", pname);
1517c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
1527c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
1537c478bd9Sstevel@tonic-gate 	va_end(alist);
1547c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static void
1587c478bd9Sstevel@tonic-gate zperror(const char *str)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	const char *estr;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if ((estr = strerror(errno)) != NULL)
1637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", pname, str, estr);
1647c478bd9Sstevel@tonic-gate 	else
1657c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: errno %d\n", pname, str, errno);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * The first part of our privilege dropping scheme needs to be called before
1707c478bd9Sstevel@tonic-gate  * fork(), since we must have it for security; we don't want to be surprised
1717c478bd9Sstevel@tonic-gate  * later that we couldn't allocate the privset.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate static int
1747c478bd9Sstevel@tonic-gate prefork_dropprivs()
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	if ((dropprivs = priv_allocset()) == NULL)
1777c478bd9Sstevel@tonic-gate 		return (1);
1787c478bd9Sstevel@tonic-gate 	priv_emptyset(dropprivs);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * We need these privileges in order to query session information and
1827c478bd9Sstevel@tonic-gate 	 * send signals.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	if (interactive == 0) {
1857c478bd9Sstevel@tonic-gate 		if (priv_addset(dropprivs, "proc_session") == -1)
1867c478bd9Sstevel@tonic-gate 			return (1);
1877c478bd9Sstevel@tonic-gate 		if (priv_addset(dropprivs, "proc_zone") == -1)
1887c478bd9Sstevel@tonic-gate 			return (1);
1897c478bd9Sstevel@tonic-gate 		if (priv_addset(dropprivs, "proc_owner") == -1)
1907c478bd9Sstevel@tonic-gate 			return (1);
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * The second part of the privilege drop.  We are paranoid about being attacked
1987c478bd9Sstevel@tonic-gate  * by the zone, so we drop all privileges.  This should prevent a compromise
1997c478bd9Sstevel@tonic-gate  * which gets us to fork(), exec(), symlink(), etc.
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate static void
2027c478bd9Sstevel@tonic-gate postfork_dropprivs()
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate 	if ((setppriv(PRIV_SET, PRIV_PERMITTED, dropprivs)) == -1) {
2057c478bd9Sstevel@tonic-gate 		zperror(gettext("Warning: could not set permitted privileges"));
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	if ((setppriv(PRIV_SET, PRIV_LIMIT, dropprivs)) == -1) {
2087c478bd9Sstevel@tonic-gate 		zperror(gettext("Warning: could not set limit privileges"));
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	if ((setppriv(PRIV_SET, PRIV_INHERITABLE, dropprivs)) == -1) {
2117c478bd9Sstevel@tonic-gate 		zperror(gettext("Warning: could not set inheritable "
2127c478bd9Sstevel@tonic-gate 		    "privileges"));
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * Create the unix domain socket and call the zoneadmd server; handshake
2187c478bd9Sstevel@tonic-gate  * with it to determine whether it will allow us to connect.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate static int
2217c478bd9Sstevel@tonic-gate get_console_master(const char *zname)
2227c478bd9Sstevel@tonic-gate {
2237c478bd9Sstevel@tonic-gate 	int sockfd = -1;
2247c478bd9Sstevel@tonic-gate 	struct sockaddr_un servaddr;
2257c478bd9Sstevel@tonic-gate 	char clientid[MAXPATHLEN];
2267c478bd9Sstevel@tonic-gate 	char handshake[MAXPATHLEN], c;
2277c478bd9Sstevel@tonic-gate 	int msglen;
2287c478bd9Sstevel@tonic-gate 	int i = 0, err = 0;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
2317c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create socket"));
2327c478bd9Sstevel@tonic-gate 		return (-1);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	bzero(&servaddr, sizeof (servaddr));
2367c478bd9Sstevel@tonic-gate 	servaddr.sun_family = AF_UNIX;
2377c478bd9Sstevel@tonic-gate 	(void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path),
2387c478bd9Sstevel@tonic-gate 	    "%s/%s.console_sock", ZONES_TMPDIR, zname);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (connect(sockfd, (struct sockaddr *)&servaddr,
2417c478bd9Sstevel@tonic-gate 	    sizeof (servaddr)) == -1) {
2427c478bd9Sstevel@tonic-gate 		zperror(gettext("Could not connect to zone console"));
2437c478bd9Sstevel@tonic-gate 		goto bad;
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	masterfd = sockfd;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	msglen = snprintf(clientid, sizeof (clientid), "IDENT %lu %s\n",
2487c478bd9Sstevel@tonic-gate 	    getpid(), setlocale(LC_MESSAGES, NULL));
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if (msglen >= sizeof (clientid) || msglen < 0) {
2517c478bd9Sstevel@tonic-gate 		zerror("protocol error");
2527c478bd9Sstevel@tonic-gate 		goto bad;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (write(masterfd, clientid, msglen) != msglen) {
2567c478bd9Sstevel@tonic-gate 		zerror("protocol error");
2577c478bd9Sstevel@tonic-gate 		goto bad;
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	bzero(handshake, sizeof (handshake));
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	/*
2637c478bd9Sstevel@tonic-gate 	 * Take care not to accumulate more than our fill, and leave room for
2647c478bd9Sstevel@tonic-gate 	 * the NUL at the end.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	while ((err = read(masterfd, &c, 1)) == 1) {
2677c478bd9Sstevel@tonic-gate 		if (i >= (sizeof (handshake) - 1))
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 		if (c == '\n')
2707c478bd9Sstevel@tonic-gate 			break;
2717c478bd9Sstevel@tonic-gate 		handshake[i] = c;
2727c478bd9Sstevel@tonic-gate 		i++;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * If something went wrong during the handshake we bail; perhaps
2777c478bd9Sstevel@tonic-gate 	 * the server died off.
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	if (err == -1) {
2807c478bd9Sstevel@tonic-gate 		zperror(gettext("Could not connect to zone console"));
2817c478bd9Sstevel@tonic-gate 		goto bad;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if (strncmp(handshake, "OK", sizeof (handshake)) == 0)
2857c478bd9Sstevel@tonic-gate 		return (0);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	zerror(gettext("Console is already in use by process ID %s."),
2887c478bd9Sstevel@tonic-gate 	    handshake);
2897c478bd9Sstevel@tonic-gate bad:
2907c478bd9Sstevel@tonic-gate 	(void) close(sockfd);
2917c478bd9Sstevel@tonic-gate 	masterfd = -1;
2927c478bd9Sstevel@tonic-gate 	return (-1);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * Routines to handle pty creation upon zone entry and to shuttle I/O back
2987c478bd9Sstevel@tonic-gate  * and forth between the two terminals.  We also compute and store the
2997c478bd9Sstevel@tonic-gate  * name of the slave terminal associated with the master side.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate static int
3027c478bd9Sstevel@tonic-gate get_master_pty()
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	if ((masterfd = open("/dev/ptmx", O_RDWR|O_NONBLOCK)) < 0) {
3057c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to obtain a pseudo-tty"));
3067c478bd9Sstevel@tonic-gate 		return (-1);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	if (tcgetattr(STDIN_FILENO, &save_termios) == -1) {
3097c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get terminal settings from stdin"));
3107c478bd9Sstevel@tonic-gate 		return (-1);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 	(void) ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&winsize);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	return (0);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * This is a bit tricky; normally a pts device will belong to the zone it
3197c478bd9Sstevel@tonic-gate  * is granted to.  But in the case of "entering" a zone, we need to establish
3207c478bd9Sstevel@tonic-gate  * the pty before entering the zone so that we can vector I/O to and from it
3217c478bd9Sstevel@tonic-gate  * from the global zone.
3227c478bd9Sstevel@tonic-gate  *
3237c478bd9Sstevel@tonic-gate  * We use the zonept() call to let the ptm driver know what we are up to;
3247c478bd9Sstevel@tonic-gate  * the only other hairy bit is the setting of zoneslavename (which happens
3257c478bd9Sstevel@tonic-gate  * above, in get_master_pty()).
3267c478bd9Sstevel@tonic-gate  */
3277c478bd9Sstevel@tonic-gate static int
328facf4a8dSllai1 init_slave_pty(zoneid_t zoneid, char *devroot)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	int slavefd = -1;
3317c478bd9Sstevel@tonic-gate 	char *slavename, zoneslavename[MAXPATHLEN];
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * Set slave permissions, zone the pts, then unlock it.
3357c478bd9Sstevel@tonic-gate 	 */
3367c478bd9Sstevel@tonic-gate 	if (grantpt(masterfd) != 0) {
3377c478bd9Sstevel@tonic-gate 		zperror(gettext("grantpt failed"));
3387c478bd9Sstevel@tonic-gate 		return (-1);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (unlockpt(masterfd) != 0) {
3427c478bd9Sstevel@tonic-gate 		zperror(gettext("unlockpt failed"));
3437c478bd9Sstevel@tonic-gate 		return (-1);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/*
3477c478bd9Sstevel@tonic-gate 	 * We must open the slave side before zoning this pty; otherwise
3487c478bd9Sstevel@tonic-gate 	 * the kernel would refuse us the open-- zoning a pty makes it
349facf4a8dSllai1 	 * inaccessible to the global zone.  Note we are trying to open
350facf4a8dSllai1 	 * the device node via the $ZONEROOT/dev path for this pty.
3517c478bd9Sstevel@tonic-gate 	 *
3527c478bd9Sstevel@tonic-gate 	 * Later we'll close the slave out when once we've opened it again
3537c478bd9Sstevel@tonic-gate 	 * from within the target zone.  Blarg.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	if ((slavename = ptsname(masterfd)) == NULL) {
3567c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get name for pseudo-tty"));
3577c478bd9Sstevel@tonic-gate 		return (-1);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	(void) snprintf(zoneslavename, sizeof (zoneslavename), "%s%s",
361facf4a8dSllai1 	    devroot, slavename);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if ((slavefd = open(zoneslavename, O_RDWR)) < 0) {
3647c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to open %s: %s"), zoneslavename,
3657c478bd9Sstevel@tonic-gate 		    strerror(errno));
3667c478bd9Sstevel@tonic-gate 		return (-1);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * Push hardware emulation (ptem), line discipline (ldterm),
3717c478bd9Sstevel@tonic-gate 	 * and V7/4BSD/Xenix compatibility (ttcompat) modules.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_PUSH, "ptem") == -1) {
3747c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to push ptem module"));
3757c478bd9Sstevel@tonic-gate 		if (!failsafe)
3767c478bd9Sstevel@tonic-gate 			goto bad;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * Anchor the stream to prevent malicious I_POPs; we prefer to do
3817c478bd9Sstevel@tonic-gate 	 * this prior to entering the zone so that we can detect any errors
3827c478bd9Sstevel@tonic-gate 	 * early, and so that we can set the anchor from the global zone.
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_ANCHOR) == -1) {
3857c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set stream anchor"));
3867c478bd9Sstevel@tonic-gate 		if (!failsafe)
3877c478bd9Sstevel@tonic-gate 			goto bad;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_PUSH, "ldterm") == -1) {
3917c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to push ldterm module"));
3927c478bd9Sstevel@tonic-gate 		if (!failsafe)
3937c478bd9Sstevel@tonic-gate 			goto bad;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_PUSH, "ttcompat") == -1) {
3967c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to push ttcompat module"));
3977c478bd9Sstevel@tonic-gate 		if (!failsafe)
3987c478bd9Sstevel@tonic-gate 			goto bad;
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * Propagate terminal settings from the external term to the new one.
4037c478bd9Sstevel@tonic-gate 	 */
4047c478bd9Sstevel@tonic-gate 	if (tcsetattr(slavefd, TCSAFLUSH, &save_termios) == -1) {
4057c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set terminal settings"));
4067c478bd9Sstevel@tonic-gate 		if (!failsafe)
4077c478bd9Sstevel@tonic-gate 			goto bad;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	(void) ioctl(slavefd, TIOCSWINSZ, (char *)&winsize);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (zonept(masterfd, zoneid) != 0) {
4127c478bd9Sstevel@tonic-gate 		zperror(gettext("could not set zoneid of pty"));
4137c478bd9Sstevel@tonic-gate 		goto bad;
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	return (slavefd);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate bad:
4197c478bd9Sstevel@tonic-gate 	(void) close(slavefd);
4207c478bd9Sstevel@tonic-gate 	return (-1);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * Place terminal into raw mode.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate static int
4277c478bd9Sstevel@tonic-gate set_tty_rawmode(int fd)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	struct termios term;
4307c478bd9Sstevel@tonic-gate 	if (tcgetattr(fd, &term) < 0) {
4317c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get user terminal settings"));
4327c478bd9Sstevel@tonic-gate 		return (-1);
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	/* Stash for later, so we can revert back to previous mode */
4367c478bd9Sstevel@tonic-gate 	save_termios = term;
4377c478bd9Sstevel@tonic-gate 	save_fd = fd;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	/* disable 8->7 bit strip, start/stop, enable any char to restart */
4407c478bd9Sstevel@tonic-gate 	term.c_iflag &= ~(ISTRIP|IXON|IXANY);
4417c478bd9Sstevel@tonic-gate 	/* disable NL->CR, CR->NL, ignore CR, UPPER->lower */
4427c478bd9Sstevel@tonic-gate 	term.c_iflag &= ~(INLCR|ICRNL|IGNCR|IUCLC);
4437c478bd9Sstevel@tonic-gate 	/* disable output post-processing */
4447c478bd9Sstevel@tonic-gate 	term.c_oflag &= ~OPOST;
4457c478bd9Sstevel@tonic-gate 	/* disable canonical mode, signal chars, echo & extended functions */
4467c478bd9Sstevel@tonic-gate 	term.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	term.c_cc[VMIN] = 1;    /* byte-at-a-time */
4497c478bd9Sstevel@tonic-gate 	term.c_cc[VTIME] = 0;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term)) {
4527c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set user terminal to raw mode"));
4537c478bd9Sstevel@tonic-gate 		return (-1);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/*
4577c478bd9Sstevel@tonic-gate 	 * We need to know the value of VEOF so that we can properly process for
4587c478bd9Sstevel@tonic-gate 	 * client-side ~<EOF>.  But we have obliterated VEOF in term,
4597c478bd9Sstevel@tonic-gate 	 * because VMIN overloads the same array slot in non-canonical mode.
4607c478bd9Sstevel@tonic-gate 	 * Stupid @&^%!
4617c478bd9Sstevel@tonic-gate 	 *
4627c478bd9Sstevel@tonic-gate 	 * So here we construct the "effective" termios from the current
4637c478bd9Sstevel@tonic-gate 	 * terminal settings, and the corrected VEOF and VEOL settings.
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	if (tcgetattr(STDIN_FILENO, &effective_termios) < 0) {
4667c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get user terminal settings"));
4677c478bd9Sstevel@tonic-gate 		return (-1);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	effective_termios.c_cc[VEOF] = save_termios.c_cc[VEOF];
4707c478bd9Sstevel@tonic-gate 	effective_termios.c_cc[VEOL] = save_termios.c_cc[VEOL];
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	return (0);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate  * Copy terminal window size from our terminal to the pts.
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4797c478bd9Sstevel@tonic-gate static void
4807c478bd9Sstevel@tonic-gate sigwinch(int s)
4817c478bd9Sstevel@tonic-gate {
4827c478bd9Sstevel@tonic-gate 	struct winsize ws;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	if (ioctl(0, TIOCGWINSZ, &ws) == 0)
4857c478bd9Sstevel@tonic-gate 		(void) ioctl(masterfd, TIOCSWINSZ, &ws);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate static void
4897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4907c478bd9Sstevel@tonic-gate sigcld(int s)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	int status;
4937c478bd9Sstevel@tonic-gate 	pid_t pid;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/*
4967c478bd9Sstevel@tonic-gate 	 * Peek at the exit status.  If this isn't the process we cared
4977c478bd9Sstevel@tonic-gate 	 * about, then just reap it.
4987c478bd9Sstevel@tonic-gate 	 */
4997c478bd9Sstevel@tonic-gate 	if ((pid = waitpid(child_pid, &status, WNOHANG|WNOWAIT)) != -1) {
5007c478bd9Sstevel@tonic-gate 		if (pid == child_pid &&
5017c478bd9Sstevel@tonic-gate 		    (WIFEXITED(status) || WIFSIGNALED(status)))
5027c478bd9Sstevel@tonic-gate 			dead = 1;
5037c478bd9Sstevel@tonic-gate 		else
5047c478bd9Sstevel@tonic-gate 			(void) waitpid(pid, &status, WNOHANG);
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * Some signals (currently, SIGINT) must be forwarded on to the process
5107c478bd9Sstevel@tonic-gate  * group of the child process.
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate static void
5137c478bd9Sstevel@tonic-gate sig_forward(int s)
5147c478bd9Sstevel@tonic-gate {
5157c478bd9Sstevel@tonic-gate 	if (child_pid != -1) {
5167c478bd9Sstevel@tonic-gate 		pid_t pgid = getpgid(child_pid);
5177c478bd9Sstevel@tonic-gate 		if (pgid != -1)
5187c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PGID, pgid, s);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate  * reset terminal settings for global environment
5247c478bd9Sstevel@tonic-gate  */
5257c478bd9Sstevel@tonic-gate static void
5267c478bd9Sstevel@tonic-gate reset_tty()
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	(void) tcsetattr(save_fd, TCSADRAIN, &save_termios);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * Convert character to printable representation, for display with locally
5337c478bd9Sstevel@tonic-gate  * echoed command characters (like when we need to display ~^D)
5347c478bd9Sstevel@tonic-gate  */
5357c478bd9Sstevel@tonic-gate static void
5367c478bd9Sstevel@tonic-gate canonify(char c, char *cc)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	if (isprint(c)) {
5397c478bd9Sstevel@tonic-gate 		cc[0] = c;
5407c478bd9Sstevel@tonic-gate 		cc[1] = '\0';
5417c478bd9Sstevel@tonic-gate 	} else if (c >= 0 && c <= 31) {	/* ^@ through ^_ */
5427c478bd9Sstevel@tonic-gate 		cc[0] = '^';
5437c478bd9Sstevel@tonic-gate 		cc[1] = c + '@';
5447c478bd9Sstevel@tonic-gate 		cc[2] = '\0';
5457c478bd9Sstevel@tonic-gate 	} else {
5467c478bd9Sstevel@tonic-gate 		cc[0] = '\\';
5477c478bd9Sstevel@tonic-gate 		cc[1] = ((c >> 6) & 7) + '0';
5487c478bd9Sstevel@tonic-gate 		cc[2] = ((c >> 3) & 7) + '0';
5497c478bd9Sstevel@tonic-gate 		cc[3] = (c & 7) + '0';
5507c478bd9Sstevel@tonic-gate 		cc[4] = '\0';
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * process_user_input watches the input stream for the escape sequence for
5567c478bd9Sstevel@tonic-gate  * 'quit' (by default, tilde-period).  Because we might be fed just one
5577c478bd9Sstevel@tonic-gate  * keystroke at a time, state associated with the user input (are we at the
5587c478bd9Sstevel@tonic-gate  * beginning of the line?  are we locally echoing the next character?) is
5597c478bd9Sstevel@tonic-gate  * maintained by beginning_of_line and local_echo across calls to the routine.
5607c478bd9Sstevel@tonic-gate  *
5617c478bd9Sstevel@tonic-gate  * This routine returns -1 when the 'quit' escape sequence has been issued,
5627c478bd9Sstevel@tonic-gate  * and 0 otherwise.
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate static int
5657c478bd9Sstevel@tonic-gate process_user_input(int outfd, char *buf, size_t nbytes)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	static boolean_t beginning_of_line = B_TRUE;
5687c478bd9Sstevel@tonic-gate 	static boolean_t local_echo = B_FALSE;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	char c = *buf;
5717c478bd9Sstevel@tonic-gate 	for (c = *buf; nbytes > 0; c = *buf, --nbytes) {
5727c478bd9Sstevel@tonic-gate 		buf++;
5737c478bd9Sstevel@tonic-gate 		if (beginning_of_line && !nocmdchar) {
5747c478bd9Sstevel@tonic-gate 			beginning_of_line = B_FALSE;
5757c478bd9Sstevel@tonic-gate 			if (c == cmdchar) {
5767c478bd9Sstevel@tonic-gate 				local_echo = B_TRUE;
5777c478bd9Sstevel@tonic-gate 				continue;
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 		} else if (local_echo) {
5807c478bd9Sstevel@tonic-gate 			local_echo = B_FALSE;
5817c478bd9Sstevel@tonic-gate 			if (c == '.' || c == effective_termios.c_cc[VEOF]) {
5827c478bd9Sstevel@tonic-gate 				char cc[CANONIFY_LEN];
5837c478bd9Sstevel@tonic-gate 				canonify(c, cc);
5847c478bd9Sstevel@tonic-gate 				(void) write(STDOUT_FILENO, &cmdchar, 1);
5857c478bd9Sstevel@tonic-gate 				(void) write(STDOUT_FILENO, cc, strlen(cc));
5867c478bd9Sstevel@tonic-gate 				return (-1);
5877c478bd9Sstevel@tonic-gate 			}
5887c478bd9Sstevel@tonic-gate 		}
5897c478bd9Sstevel@tonic-gate 		if (write(outfd, &c, 1) <= 0)
5907c478bd9Sstevel@tonic-gate 			return (-1);
5917c478bd9Sstevel@tonic-gate 		beginning_of_line = (c == '\r' || c == '\n' ||
5927c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VKILL] ||
5937c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VEOL] ||
5947c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VSUSP] ||
5957c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VINTR]);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 	return (0);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate  * This is the main I/O loop, and is shared across all zlogin modes.
6027c478bd9Sstevel@tonic-gate  * Parameters:
6037c478bd9Sstevel@tonic-gate  * 	stdin_fd:  The fd representing 'stdin' for the slave side; input to
6047c478bd9Sstevel@tonic-gate  *	           the zone will be written here.
6057c478bd9Sstevel@tonic-gate  *
6067c478bd9Sstevel@tonic-gate  *	stdout_fd: The fd representing 'stdout' for the slave side; output
6077c478bd9Sstevel@tonic-gate  *	           from the zone will arrive here.
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  *	stderr_fd: The fd representing 'stderr' for the slave side; output
6107c478bd9Sstevel@tonic-gate  *	           from the zone will arrive here.
6117c478bd9Sstevel@tonic-gate  *
6127c478bd9Sstevel@tonic-gate  *	raw_mode:  If TRUE, then no processing (for example, for '~.') will
6137c478bd9Sstevel@tonic-gate  *	           be performed on the input coming from STDIN.
6147c478bd9Sstevel@tonic-gate  *
6157c478bd9Sstevel@tonic-gate  * stderr_fd may be specified as -1 if there is no stderr (only non-interactive
6167c478bd9Sstevel@tonic-gate  * mode supplies a stderr).
6177c478bd9Sstevel@tonic-gate  *
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate static void
6207c478bd9Sstevel@tonic-gate doio(int stdin_fd, int stdout_fd, int stderr_fd, boolean_t raw_mode)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	struct pollfd pollfds[3];
6237c478bd9Sstevel@tonic-gate 	char ibuf[BUFSIZ];
6247c478bd9Sstevel@tonic-gate 	int cc, ret;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/* read from stdout of zone and write to stdout of global zone */
6277c478bd9Sstevel@tonic-gate 	pollfds[0].fd = stdout_fd;
6287c478bd9Sstevel@tonic-gate 	pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	/* read from stderr of zone and write to stderr of global zone */
6317c478bd9Sstevel@tonic-gate 	pollfds[1].fd = stderr_fd;
6327c478bd9Sstevel@tonic-gate 	pollfds[1].events = pollfds[0].events;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/* read from stdin of global zone and write to stdin of zone */
6357c478bd9Sstevel@tonic-gate 	pollfds[2].fd = STDIN_FILENO;
6367c478bd9Sstevel@tonic-gate 	pollfds[2].events = pollfds[0].events;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	for (;;) {
6397c478bd9Sstevel@tonic-gate 		pollfds[0].revents = pollfds[1].revents =
6407c478bd9Sstevel@tonic-gate 		    pollfds[2].revents = 0;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		if (dead)
6437c478bd9Sstevel@tonic-gate 			break;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		ret = poll(pollfds,
6467c478bd9Sstevel@tonic-gate 		    sizeof (pollfds) / sizeof (struct pollfd), -1);
6477c478bd9Sstevel@tonic-gate 		if (ret == -1 && errno != EINTR) {
6487c478bd9Sstevel@tonic-gate 			perror("poll failed");
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		if (errno == EINTR && dead) {
6537c478bd9Sstevel@tonic-gate 			break;
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		/* event from master side stdout */
6577c478bd9Sstevel@tonic-gate 		if (pollfds[0].revents) {
6587c478bd9Sstevel@tonic-gate 			if (pollfds[0].revents &
6597c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
6607c478bd9Sstevel@tonic-gate 				cc = read(stdout_fd, ibuf, BUFSIZ);
6617c478bd9Sstevel@tonic-gate 				if (cc == -1 && (errno != EINTR || dead))
6627c478bd9Sstevel@tonic-gate 					break;
6637c478bd9Sstevel@tonic-gate 				if (cc == 0)	/* EOF */
6647c478bd9Sstevel@tonic-gate 					break;
6657c478bd9Sstevel@tonic-gate 				(void) write(STDOUT_FILENO, ibuf, cc);
6667c478bd9Sstevel@tonic-gate 			} else {
6677c478bd9Sstevel@tonic-gate 				pollerr = pollfds[0].revents;
6687c478bd9Sstevel@tonic-gate 				break;
6697c478bd9Sstevel@tonic-gate 			}
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		/* event from master side stderr */
6737c478bd9Sstevel@tonic-gate 		if (pollfds[1].revents) {
6747c478bd9Sstevel@tonic-gate 			if (pollfds[1].revents &
6757c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
6767c478bd9Sstevel@tonic-gate 				cc = read(stderr_fd, ibuf, BUFSIZ);
6777c478bd9Sstevel@tonic-gate 				if (cc == -1 && (errno != EINTR || dead))
6787c478bd9Sstevel@tonic-gate 					break;
6797c478bd9Sstevel@tonic-gate 				if (cc == 0)	/* EOF */
6807c478bd9Sstevel@tonic-gate 					break;
6817c478bd9Sstevel@tonic-gate 				(void) write(STDERR_FILENO, ibuf, cc);
6827c478bd9Sstevel@tonic-gate 			} else {
6837c478bd9Sstevel@tonic-gate 				pollerr = pollfds[1].revents;
6847c478bd9Sstevel@tonic-gate 				break;
6857c478bd9Sstevel@tonic-gate 			}
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		/* event from user STDIN side */
6897c478bd9Sstevel@tonic-gate 		if (pollfds[2].revents) {
6907c478bd9Sstevel@tonic-gate 			if (pollfds[2].revents &
6917c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
6927c478bd9Sstevel@tonic-gate 				cc = read(STDIN_FILENO, ibuf, BUFSIZ);
6937c478bd9Sstevel@tonic-gate 				if (cc == -1 && (errno != EINTR || dead))
6947c478bd9Sstevel@tonic-gate 					break;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 				/*
6977c478bd9Sstevel@tonic-gate 				 * stdin fd is stdin of the target; so,
6987c478bd9Sstevel@tonic-gate 				 * the thing we'll write the user data *to*.
6997c478bd9Sstevel@tonic-gate 				 *
7007c478bd9Sstevel@tonic-gate 				 * Also, unlike on the output side, we
7017c478bd9Sstevel@tonic-gate 				 * propagate zero-length messages to the
7027c478bd9Sstevel@tonic-gate 				 * other side.
7037c478bd9Sstevel@tonic-gate 				 */
7047c478bd9Sstevel@tonic-gate 				if (raw_mode == B_TRUE) {
7057c478bd9Sstevel@tonic-gate 					if (write(stdin_fd, ibuf, cc) == -1)
7067c478bd9Sstevel@tonic-gate 						break;
7077c478bd9Sstevel@tonic-gate 				} else {
7087c478bd9Sstevel@tonic-gate 					if (process_user_input(stdin_fd, ibuf,
7097c478bd9Sstevel@tonic-gate 					    cc) == -1)
7107c478bd9Sstevel@tonic-gate 						break;
7117c478bd9Sstevel@tonic-gate 				}
7127c478bd9Sstevel@tonic-gate 			} else if (raw_mode == B_TRUE &&
7137c478bd9Sstevel@tonic-gate 			    pollfds[2].revents & POLLHUP) {
7147c478bd9Sstevel@tonic-gate 				/*
7157c478bd9Sstevel@tonic-gate 				 * It's OK to get a POLLHUP on STDIN-- it
7167c478bd9Sstevel@tonic-gate 				 * always happens if you do:
7177c478bd9Sstevel@tonic-gate 				 *
7187c478bd9Sstevel@tonic-gate 				 * echo foo | zlogin <zone> <command>
7197c478bd9Sstevel@tonic-gate 				 *
7207c478bd9Sstevel@tonic-gate 				 * We reset fd to -1 in this case to clear
7217c478bd9Sstevel@tonic-gate 				 * the condition and write an EOF to the
7227c478bd9Sstevel@tonic-gate 				 * other side in order to wrap things up.
7237c478bd9Sstevel@tonic-gate 				 */
7247c478bd9Sstevel@tonic-gate 				pollfds[2].fd = -1;
7257c478bd9Sstevel@tonic-gate 				(void) write(stdin_fd, ibuf, 0);
7267c478bd9Sstevel@tonic-gate 			} else {
7277c478bd9Sstevel@tonic-gate 				pollerr = pollfds[2].revents;
7287c478bd9Sstevel@tonic-gate 				break;
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * We are in the midst of dying, but try to poll with a short
7357c478bd9Sstevel@tonic-gate 	 * timeout to see if we can catch the last bit of I/O from the
7367c478bd9Sstevel@tonic-gate 	 * children.
7377c478bd9Sstevel@tonic-gate 	 */
7387c478bd9Sstevel@tonic-gate 	pollfds[0].revents = pollfds[1].revents = pollfds[2].revents = 0;
7397c478bd9Sstevel@tonic-gate 	(void) poll(pollfds,
7407c478bd9Sstevel@tonic-gate 	    sizeof (pollfds) / sizeof (struct pollfd), 100);
7417c478bd9Sstevel@tonic-gate 	if (pollfds[0].revents &
7427c478bd9Sstevel@tonic-gate 	    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
7437c478bd9Sstevel@tonic-gate 		if ((cc = read(stdout_fd, ibuf, BUFSIZ)) > 0)
7447c478bd9Sstevel@tonic-gate 			(void) write(STDOUT_FILENO, ibuf, cc);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	if (pollfds[1].revents &
7477c478bd9Sstevel@tonic-gate 	    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
7487c478bd9Sstevel@tonic-gate 		if ((cc = read(stderr_fd, ibuf, BUFSIZ)) > 0)
7497c478bd9Sstevel@tonic-gate 			(void) write(STDERR_FILENO, ibuf, cc);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7539acbbeafSnn35248 static char **
754*123807fbSedp zone_login_cmd(brand_handle_t bh, const char *login)
7559acbbeafSnn35248 {
7569acbbeafSnn35248 	static char result_buf[ARG_MAX];
7579acbbeafSnn35248 	char **new_argv, *ptr, *lasts;
7589acbbeafSnn35248 	int n, a;
7599acbbeafSnn35248 
7609acbbeafSnn35248 	/* Get the login command for the target zone. */
7619acbbeafSnn35248 	bzero(result_buf, sizeof (result_buf));
762*123807fbSedp 	if (brand_get_login_cmd(bh, login,
7639acbbeafSnn35248 	    result_buf, sizeof (result_buf)) != 0)
7649acbbeafSnn35248 		return (NULL);
7659acbbeafSnn35248 
7669acbbeafSnn35248 	/*
7679acbbeafSnn35248 	 * We got back a string that we'd like to execute.  But since
7689acbbeafSnn35248 	 * we're not doing the execution via a shell we'll need to convert
7699acbbeafSnn35248 	 * the exec string to an array of strings.  We'll do that here
7709acbbeafSnn35248 	 * but we're going to be very simplistic about it and break stuff
7719acbbeafSnn35248 	 * up based on spaces.  We're not even going to support any kind
7729acbbeafSnn35248 	 * of quoting or escape characters.  It's truly amazing that
7739acbbeafSnn35248 	 * there is no library function in OpenSolaris to do this for us.
7749acbbeafSnn35248 	 */
7759acbbeafSnn35248 
7769acbbeafSnn35248 	/*
7779acbbeafSnn35248 	 * Be paranoid.  Since we're deliniating based on spaces make
7789acbbeafSnn35248 	 * sure there are no adjacent spaces.
7799acbbeafSnn35248 	 */
7809acbbeafSnn35248 	if (strstr(result_buf, "  ") != NULL)
7819acbbeafSnn35248 		return (NULL);
7829acbbeafSnn35248 
7839acbbeafSnn35248 	/* Remove any trailing whitespace.  */
7849acbbeafSnn35248 	n = strlen(result_buf);
7859acbbeafSnn35248 	if (result_buf[n - 1] == ' ')
7869acbbeafSnn35248 		result_buf[n - 1] = '\0';
7879acbbeafSnn35248 
7889acbbeafSnn35248 	/* Count how many elements there are in the exec string. */
7899acbbeafSnn35248 	ptr = result_buf;
7909acbbeafSnn35248 	for (n = 2; ((ptr = strchr(ptr + 1, (int)' ')) != NULL); n++)
7919acbbeafSnn35248 		;
7929acbbeafSnn35248 
7939acbbeafSnn35248 	/* Allocate the argv array that we're going to return. */
7949acbbeafSnn35248 	if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
7959acbbeafSnn35248 		return (NULL);
7969acbbeafSnn35248 
7979acbbeafSnn35248 	/* Tokenize the exec string and return. */
7989acbbeafSnn35248 	a = 0;
7999acbbeafSnn35248 	new_argv[a++] = result_buf;
8009acbbeafSnn35248 	if (n > 2) {
8019acbbeafSnn35248 		(void) strtok_r(result_buf, " ", &lasts);
8029acbbeafSnn35248 		while ((new_argv[a++] = strtok_r(NULL, " ", &lasts)) != NULL)
8039acbbeafSnn35248 			;
8049acbbeafSnn35248 	} else {
8059acbbeafSnn35248 		new_argv[a++] = NULL;
8069acbbeafSnn35248 	}
8079acbbeafSnn35248 	assert(n == a);
8089acbbeafSnn35248 	return (new_argv);
8099acbbeafSnn35248 }
8109acbbeafSnn35248 
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate  * Prepare argv array for exec'd process; if we're passing commands to the
8137c478bd9Sstevel@tonic-gate  * new process, then use su(1M) to do the invocation.  Otherwise, use
8147c478bd9Sstevel@tonic-gate  * 'login -z <from_zonename> -f' (-z is an undocumented option which tells
8157c478bd9Sstevel@tonic-gate  * login that we're coming from another zone, and to disregard its CONSOLE
8167c478bd9Sstevel@tonic-gate  * checks).
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate static char **
819*123807fbSedp prep_args(brand_handle_t bh, const char *login, char **argv)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	int argc = 0, a = 0, i, n = -1;
8227c478bd9Sstevel@tonic-gate 	char **new_argv;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if (argv != NULL) {
8257c478bd9Sstevel@tonic-gate 		size_t subshell_len = 1;
8267c478bd9Sstevel@tonic-gate 		char *subshell;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		while (argv[argc] != NULL)
8297c478bd9Sstevel@tonic-gate 			argc++;
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
8327c478bd9Sstevel@tonic-gate 			subshell_len += strlen(argv[i]) + 1;
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 		if ((subshell = calloc(1, subshell_len)) == NULL)
8357c478bd9Sstevel@tonic-gate 			return (NULL);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
8387c478bd9Sstevel@tonic-gate 			(void) strcat(subshell, argv[i]);
8397c478bd9Sstevel@tonic-gate 			(void) strcat(subshell, " ");
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (failsafe) {
8437c478bd9Sstevel@tonic-gate 			n = 4;
8447c478bd9Sstevel@tonic-gate 			if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
8457c478bd9Sstevel@tonic-gate 				return (NULL);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 			new_argv[a++] = FAILSAFESHELL;
8487c478bd9Sstevel@tonic-gate 		} else {
8497c478bd9Sstevel@tonic-gate 			n = 5;
8507c478bd9Sstevel@tonic-gate 			if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
8517c478bd9Sstevel@tonic-gate 				return (NULL);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 			new_argv[a++] = SUPATH;
8549acbbeafSnn35248 			new_argv[a++] = (char *)login;
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 		new_argv[a++] = "-c";
8577c478bd9Sstevel@tonic-gate 		new_argv[a++] = subshell;
8587c478bd9Sstevel@tonic-gate 		new_argv[a++] = NULL;
8597c478bd9Sstevel@tonic-gate 		assert(a == n);
8607c478bd9Sstevel@tonic-gate 	} else {
8617c478bd9Sstevel@tonic-gate 		if (failsafe) {
8627c478bd9Sstevel@tonic-gate 			n = 2;
8637c478bd9Sstevel@tonic-gate 			if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
8647c478bd9Sstevel@tonic-gate 				return (NULL);
8657c478bd9Sstevel@tonic-gate 			new_argv[a++] = FAILSAFESHELL;
8667c478bd9Sstevel@tonic-gate 			new_argv[a++] = NULL;
8677c478bd9Sstevel@tonic-gate 			assert(n == a);
8689acbbeafSnn35248 		} else {
869*123807fbSedp 			new_argv = zone_login_cmd(bh, login);
8709acbbeafSnn35248 		}
8719acbbeafSnn35248 	}
8729acbbeafSnn35248 
8737c478bd9Sstevel@tonic-gate 	return (new_argv);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * Helper routine for prep_env below.
8787c478bd9Sstevel@tonic-gate  */
8797c478bd9Sstevel@tonic-gate static char *
8807c478bd9Sstevel@tonic-gate add_env(char *name, char *value)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	size_t sz = strlen(name) + strlen(value) + 2; /* name, =, value, NUL */
8837c478bd9Sstevel@tonic-gate 	char *str;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	if ((str = malloc(sz)) == NULL)
8867c478bd9Sstevel@tonic-gate 		return (NULL);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	(void) snprintf(str, sz, "%s=%s", name, value);
8897c478bd9Sstevel@tonic-gate 	return (str);
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate /*
8937c478bd9Sstevel@tonic-gate  * Prepare envp array for exec'd process.
8947c478bd9Sstevel@tonic-gate  */
8957c478bd9Sstevel@tonic-gate static char **
8967c478bd9Sstevel@tonic-gate prep_env()
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	int e = 0, size = 1;
8997c478bd9Sstevel@tonic-gate 	char **new_env, *estr;
9007c478bd9Sstevel@tonic-gate 	char *term = getenv("TERM");
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	size++;	/* for $PATH */
9037c478bd9Sstevel@tonic-gate 	if (term != NULL)
9047c478bd9Sstevel@tonic-gate 		size++;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/*
9077c478bd9Sstevel@tonic-gate 	 * In failsafe mode we set $HOME, since '-l' isn't valid in this mode.
9087c478bd9Sstevel@tonic-gate 	 * We also set $SHELL, since neither login nor su will be around to do
9097c478bd9Sstevel@tonic-gate 	 * it.
9107c478bd9Sstevel@tonic-gate 	 */
9117c478bd9Sstevel@tonic-gate 	if (failsafe)
9127c478bd9Sstevel@tonic-gate 		size += 2;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if ((new_env = malloc(sizeof (char *) * size)) == NULL)
9157c478bd9Sstevel@tonic-gate 		return (NULL);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if ((estr = add_env("PATH", DEF_PATH)) == NULL)
9187c478bd9Sstevel@tonic-gate 		return (NULL);
9197c478bd9Sstevel@tonic-gate 	new_env[e++] = estr;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	if (term != NULL) {
9227c478bd9Sstevel@tonic-gate 		if ((estr = add_env("TERM", term)) == NULL)
9237c478bd9Sstevel@tonic-gate 			return (NULL);
9247c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	if (failsafe) {
9287c478bd9Sstevel@tonic-gate 		if ((estr = add_env("HOME", "/")) == NULL)
9297c478bd9Sstevel@tonic-gate 			return (NULL);
9307c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		if ((estr = add_env("SHELL", FAILSAFESHELL)) == NULL)
9337c478bd9Sstevel@tonic-gate 			return (NULL);
9347c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	new_env[e++] = NULL;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	assert(e == size);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	return (new_env);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  * Finish the preparation of the envp array for exec'd non-interactive
9467c478bd9Sstevel@tonic-gate  * zlogins.  This is called in the child process *after* we zone_enter(), since
9477c478bd9Sstevel@tonic-gate  * it derives things we can only know within the zone, such as $HOME, $SHELL,
9487c478bd9Sstevel@tonic-gate  * etc.  We need only do this in the non-interactive, mode, since otherwise
9497c478bd9Sstevel@tonic-gate  * login(1) will do it.  We don't do this in failsafe mode, since it presents
9507c478bd9Sstevel@tonic-gate  * additional ways in which the command could fail, and we'd prefer to avoid
9517c478bd9Sstevel@tonic-gate  * that.
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate static char **
9547c478bd9Sstevel@tonic-gate prep_env_noninteractive(char *login, char **env)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	size_t size;
9577c478bd9Sstevel@tonic-gate 	struct passwd *pw;
9587c478bd9Sstevel@tonic-gate 	char **new_env;
9597c478bd9Sstevel@tonic-gate 	int e, i;
9607c478bd9Sstevel@tonic-gate 	char *estr;
9617c478bd9Sstevel@tonic-gate 	char varmail[LOGNAME_MAX + 11]; /* strlen(/var/mail/) = 10, NUL */
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	assert(env != NULL);
9647c478bd9Sstevel@tonic-gate 	assert(failsafe == 0);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/*
9677c478bd9Sstevel@tonic-gate 	 * Get existing envp size.
9687c478bd9Sstevel@tonic-gate 	 */
9697c478bd9Sstevel@tonic-gate 	for (size = 0; env[size] != NULL; size++)
9707c478bd9Sstevel@tonic-gate 		;
9717c478bd9Sstevel@tonic-gate 	e = size;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	/*
9747c478bd9Sstevel@tonic-gate 	 * Finish filling out the environment; we duplicate the environment
9757c478bd9Sstevel@tonic-gate 	 * setup described in login(1), for lack of a better precedent.
9767c478bd9Sstevel@tonic-gate 	 */
9777c478bd9Sstevel@tonic-gate 	if ((pw = getpwnam(login)) != NULL) {
9787c478bd9Sstevel@tonic-gate 		size += 3;	/* LOGNAME, HOME, MAIL */
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	size++;	/* always fill in SHELL */
9817c478bd9Sstevel@tonic-gate 	size++; /* terminating NULL */
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	if ((new_env = malloc(sizeof (char *) * size)) == NULL)
9847c478bd9Sstevel@tonic-gate 		goto malloc_fail;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/*
9877c478bd9Sstevel@tonic-gate 	 * Copy existing elements of env into new_env.
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	for (i = 0; env[i] != NULL; i++) {
9907c478bd9Sstevel@tonic-gate 		if ((new_env[i] = strdup(env[i])) == NULL)
9917c478bd9Sstevel@tonic-gate 			goto malloc_fail;
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 	assert(e == i);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	if (pw != NULL) {
9967c478bd9Sstevel@tonic-gate 		if ((estr = add_env("LOGNAME", pw->pw_name)) == NULL)
9977c478bd9Sstevel@tonic-gate 			goto malloc_fail;
9987c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 		if ((estr = add_env("HOME", pw->pw_dir)) == NULL)
10017c478bd9Sstevel@tonic-gate 			goto malloc_fail;
10027c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 		if (chdir(pw->pw_dir) != 0)
10057c478bd9Sstevel@tonic-gate 			zerror(gettext("Could not chdir to home directory "
10067c478bd9Sstevel@tonic-gate 			    "%s: %s"), pw->pw_dir, strerror(errno));
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 		(void) snprintf(varmail, sizeof (varmail), "/var/mail/%s",
10097c478bd9Sstevel@tonic-gate 		    pw->pw_name);
10107c478bd9Sstevel@tonic-gate 		if ((estr = add_env("MAIL", varmail)) == NULL)
10117c478bd9Sstevel@tonic-gate 			goto malloc_fail;
10127c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	if (pw != NULL && strlen(pw->pw_shell) > 0) {
10167c478bd9Sstevel@tonic-gate 		if ((estr = add_env("SHELL", pw->pw_shell)) == NULL)
10177c478bd9Sstevel@tonic-gate 			goto malloc_fail;
10187c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
10197c478bd9Sstevel@tonic-gate 	} else {
10207c478bd9Sstevel@tonic-gate 		if ((estr = add_env("SHELL", DEFAULTSHELL)) == NULL)
10217c478bd9Sstevel@tonic-gate 			goto malloc_fail;
10227c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	new_env[e++] = NULL;	/* add terminating NULL */
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	assert(e == size);
10287c478bd9Sstevel@tonic-gate 	return (new_env);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate malloc_fail:
10317c478bd9Sstevel@tonic-gate 	zperror(gettext("failed to allocate memory for process environment"));
10327c478bd9Sstevel@tonic-gate 	return (NULL);
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate static int
10367c478bd9Sstevel@tonic-gate close_func(void *slavefd, int fd)
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	if (fd != *(int *)slavefd)
10397c478bd9Sstevel@tonic-gate 		(void) close(fd);
10407c478bd9Sstevel@tonic-gate 	return (0);
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate static void
10447c478bd9Sstevel@tonic-gate set_cmdchar(char *cmdcharstr)
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	char c;
10477c478bd9Sstevel@tonic-gate 	long lc;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	if ((c = *cmdcharstr) != '\\') {
10507c478bd9Sstevel@tonic-gate 		cmdchar = c;
10517c478bd9Sstevel@tonic-gate 		return;
10527c478bd9Sstevel@tonic-gate 	}
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	c = cmdcharstr[1];
10557c478bd9Sstevel@tonic-gate 	if (c == '\0' || c == '\\') {
10567c478bd9Sstevel@tonic-gate 		cmdchar = '\\';
10577c478bd9Sstevel@tonic-gate 		return;
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	if (c < '0' || c > '7') {
10617c478bd9Sstevel@tonic-gate 		zerror(gettext("Unrecognized escape character option %s"),
10627c478bd9Sstevel@tonic-gate 		    cmdcharstr);
10637c478bd9Sstevel@tonic-gate 		usage();
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	lc = strtol(cmdcharstr + 1, NULL, 8);
10677c478bd9Sstevel@tonic-gate 	if (lc < 0 || lc > 255) {
10687c478bd9Sstevel@tonic-gate 		zerror(gettext("Octal escape character '%s' too large"),
10697c478bd9Sstevel@tonic-gate 		    cmdcharstr);
10707c478bd9Sstevel@tonic-gate 		usage();
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 	cmdchar = (char)lc;
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate static int
10767c478bd9Sstevel@tonic-gate setup_utmpx(char *slavename)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	struct utmpx ut;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	bzero(&ut, sizeof (ut));
10817c478bd9Sstevel@tonic-gate 	(void) strncpy(ut.ut_user, ".zlogin", sizeof (ut.ut_user));
10827c478bd9Sstevel@tonic-gate 	(void) strncpy(ut.ut_line, slavename, sizeof (ut.ut_line));
10837c478bd9Sstevel@tonic-gate 	ut.ut_pid = getpid();
10847c478bd9Sstevel@tonic-gate 	ut.ut_id[0] = 'z';
10857c478bd9Sstevel@tonic-gate 	ut.ut_id[1] = ut.ut_id[2] = ut.ut_id[3] = (char)SC_WILDC;
10867c478bd9Sstevel@tonic-gate 	ut.ut_type = LOGIN_PROCESS;
10877c478bd9Sstevel@tonic-gate 	(void) time(&ut.ut_tv.tv_sec);
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (makeutx(&ut) == NULL) {
10907c478bd9Sstevel@tonic-gate 		zerror(gettext("makeutx failed"));
10917c478bd9Sstevel@tonic-gate 		return (-1);
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 	return (0);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate static void
10977c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
10987c478bd9Sstevel@tonic-gate {
10997c478bd9Sstevel@tonic-gate 	(void) close(lockfd);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate static int
11037c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
11067c478bd9Sstevel@tonic-gate 	struct flock flock;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
11097c478bd9Sstevel@tonic-gate 		zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR,
11107c478bd9Sstevel@tonic-gate 		    strerror(errno));
11117c478bd9Sstevel@tonic-gate 		return (-1);
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
11147c478bd9Sstevel@tonic-gate 	(void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock",
11157c478bd9Sstevel@tonic-gate 	    ZONES_TMPDIR, zone_name);
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
11187c478bd9Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
11197c478bd9Sstevel@tonic-gate 		    strerror(errno));
11207c478bd9Sstevel@tonic-gate 		return (-1);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 	/*
11237c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
11247c478bd9Sstevel@tonic-gate 	 */
11257c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
11267c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
11277c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
11287c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
11297c478bd9Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
11307c478bd9Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
11317c478bd9Sstevel@tonic-gate 		    strerror(errno));
11327c478bd9Sstevel@tonic-gate 		release_lock_file(*lockfd);
11337c478bd9Sstevel@tonic-gate 		return (-1);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 	return (Z_OK);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate static int
11397c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	pid_t retval;
11427c478bd9Sstevel@tonic-gate 	int pstatus = 0, error = -1, lockfd, doorfd;
11437c478bd9Sstevel@tonic-gate 	struct door_info info;
11447c478bd9Sstevel@tonic-gate 	char doorpath[MAXPATHLEN];
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	(void) snprintf(doorpath, sizeof (doorpath), ZONE_DOOR_PATH, zone_name);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
11497c478bd9Sstevel@tonic-gate 		return (-1);
11507c478bd9Sstevel@tonic-gate 	/*
11517c478bd9Sstevel@tonic-gate 	 * We must do the door check with the lock held.  Otherwise, we
11527c478bd9Sstevel@tonic-gate 	 * might race against another zoneadm/zlogin process and wind
11537c478bd9Sstevel@tonic-gate 	 * up with two processes trying to start zoneadmd at the same
11547c478bd9Sstevel@tonic-gate 	 * time.  zoneadmd will detect this, and fail, but we prefer this
11557c478bd9Sstevel@tonic-gate 	 * to be as seamless as is practical, from a user perspective.
11567c478bd9Sstevel@tonic-gate 	 */
11577c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
11587c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
11597c478bd9Sstevel@tonic-gate 			zerror("failed to open %s: %s", doorpath,
11607c478bd9Sstevel@tonic-gate 			    strerror(errno));
11617c478bd9Sstevel@tonic-gate 			goto out;
11627c478bd9Sstevel@tonic-gate 		}
11637c478bd9Sstevel@tonic-gate 	} else {
11647c478bd9Sstevel@tonic-gate 		/*
11657c478bd9Sstevel@tonic-gate 		 * Seems to be working ok.
11667c478bd9Sstevel@tonic-gate 		 */
11677c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
11687c478bd9Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
11697c478bd9Sstevel@tonic-gate 			error = 0;
11707c478bd9Sstevel@tonic-gate 			goto out;
11717c478bd9Sstevel@tonic-gate 		}
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
11757c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"));
11767c478bd9Sstevel@tonic-gate 		goto out;
11777c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
11787c478bd9Sstevel@tonic-gate 		/* child process */
11797c478bd9Sstevel@tonic-gate 		(void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z",
11807c478bd9Sstevel@tonic-gate 		    zone_name, NULL);
11817c478bd9Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"));
11827c478bd9Sstevel@tonic-gate 		_exit(1);
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	/* parent process */
11867c478bd9Sstevel@tonic-gate 	do {
11877c478bd9Sstevel@tonic-gate 		retval = waitpid(child_pid, &pstatus, 0);
11887c478bd9Sstevel@tonic-gate 	} while (retval != child_pid);
11897c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(pstatus) ||
11907c478bd9Sstevel@tonic-gate 	    (WIFEXITED(pstatus) && WEXITSTATUS(pstatus) != 0)) {
11917c478bd9Sstevel@tonic-gate 		zerror(gettext("could not start %s"), "zoneadmd");
11927c478bd9Sstevel@tonic-gate 		goto out;
11937c478bd9Sstevel@tonic-gate 	}
11947c478bd9Sstevel@tonic-gate 	error = 0;
11957c478bd9Sstevel@tonic-gate out:
11967c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
11977c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
11987c478bd9Sstevel@tonic-gate 	return (error);
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate static int
12027c478bd9Sstevel@tonic-gate init_template(void)
12037c478bd9Sstevel@tonic-gate {
12047c478bd9Sstevel@tonic-gate 	int fd;
12057c478bd9Sstevel@tonic-gate 	int err = 0;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
12087c478bd9Sstevel@tonic-gate 	if (fd == -1)
12097c478bd9Sstevel@tonic-gate 		return (-1);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	/*
12127c478bd9Sstevel@tonic-gate 	 * zlogin doesn't do anything with the contract.
12137c478bd9Sstevel@tonic-gate 	 * Deliver no events, don't inherit, and allow it to be orphaned.
12147c478bd9Sstevel@tonic-gate 	 */
12157c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_critical(fd, 0);
12167c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(fd, 0);
12177c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
12187c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
12197c478bd9Sstevel@tonic-gate 	if (err || ct_tmpl_activate(fd)) {
12207c478bd9Sstevel@tonic-gate 		(void) close(fd);
12217c478bd9Sstevel@tonic-gate 		return (-1);
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	return (fd);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate static int
12287c478bd9Sstevel@tonic-gate noninteractive_login(char *zonename, zoneid_t zoneid, char *login,
12297c478bd9Sstevel@tonic-gate     char **new_args, char **new_env)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	pid_t retval;
12327c478bd9Sstevel@tonic-gate 	int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2];
12337c478bd9Sstevel@tonic-gate 	int child_status;
12347c478bd9Sstevel@tonic-gate 	int tmpl_fd;
12357c478bd9Sstevel@tonic-gate 	sigset_t block_cld;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
12387c478bd9Sstevel@tonic-gate 		reset_tty();
12397c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create contract"));
12407c478bd9Sstevel@tonic-gate 		return (1);
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	if (pipe(stdin_pipe) != 0) {
12447c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create STDIN pipe"));
12457c478bd9Sstevel@tonic-gate 		return (1);
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 	/*
12487c478bd9Sstevel@tonic-gate 	 * When the user types ^D, we get a zero length message on STDIN.
12497c478bd9Sstevel@tonic-gate 	 * We need to echo that down the pipe to send it to the other side;
12507c478bd9Sstevel@tonic-gate 	 * but by default, pipes don't propagate zero-length messages.  We
12517c478bd9Sstevel@tonic-gate 	 * toggle that behavior off using I_SWROPT.  See streamio(7i).
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	if (ioctl(stdin_pipe[0], I_SWROPT, SNDZERO) != 0) {
12547c478bd9Sstevel@tonic-gate 		zperror(gettext("could not configure STDIN pipe"));
12557c478bd9Sstevel@tonic-gate 		return (1);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 	if (pipe(stdout_pipe) != 0) {
12597c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create STDOUT pipe"));
12607c478bd9Sstevel@tonic-gate 		return (1);
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 	if (pipe(stderr_pipe) != 0) {
12637c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create STDERR pipe"));
12647c478bd9Sstevel@tonic-gate 		return (1);
12657c478bd9Sstevel@tonic-gate 	}
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	/*
12687c478bd9Sstevel@tonic-gate 	 * If any of the pipe FD's winds up being less than STDERR, then we
12697c478bd9Sstevel@tonic-gate 	 * have a mess on our hands-- and we are lacking some of the I/O
12707c478bd9Sstevel@tonic-gate 	 * streams we would expect anyway.  So we bail.
12717c478bd9Sstevel@tonic-gate 	 */
12727c478bd9Sstevel@tonic-gate 	if (stdin_pipe[0] <= STDERR_FILENO ||
12737c478bd9Sstevel@tonic-gate 	    stdin_pipe[1] <= STDERR_FILENO ||
12747c478bd9Sstevel@tonic-gate 	    stdout_pipe[0] <= STDERR_FILENO ||
12757c478bd9Sstevel@tonic-gate 	    stdout_pipe[1] <= STDERR_FILENO ||
12767c478bd9Sstevel@tonic-gate 	    stderr_pipe[0] <= STDERR_FILENO ||
12777c478bd9Sstevel@tonic-gate 	    stderr_pipe[1] <= STDERR_FILENO) {
12787c478bd9Sstevel@tonic-gate 		zperror(gettext("process lacks valid STDIN, STDOUT, STDERR"));
12797c478bd9Sstevel@tonic-gate 		return (1);
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	if (prefork_dropprivs() != 0) {
12837c478bd9Sstevel@tonic-gate 		zperror(gettext("could not allocate privilege set"));
12847c478bd9Sstevel@tonic-gate 		return (1);
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCLD, sigcld);
12887c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
12897c478bd9Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCLD);
12907c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
12937c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
12947c478bd9Sstevel@tonic-gate 		(void) close(tmpl_fd);
12957c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"));
12967c478bd9Sstevel@tonic-gate 		return (1);
12977c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) { /* child process */
12987c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 		/*
13017c478bd9Sstevel@tonic-gate 		 * Do a dance to get the pipes hooked up as FD's 0, 1 and 2.
13027c478bd9Sstevel@tonic-gate 		 */
13037c478bd9Sstevel@tonic-gate 		(void) close(STDIN_FILENO);
13047c478bd9Sstevel@tonic-gate 		(void) close(STDOUT_FILENO);
13057c478bd9Sstevel@tonic-gate 		(void) close(STDERR_FILENO);
13067c478bd9Sstevel@tonic-gate 		(void) dup2(stdin_pipe[1], STDIN_FILENO);
13077c478bd9Sstevel@tonic-gate 		(void) dup2(stdout_pipe[1], STDOUT_FILENO);
13087c478bd9Sstevel@tonic-gate 		(void) dup2(stderr_pipe[1], STDERR_FILENO);
13097c478bd9Sstevel@tonic-gate 		(void) closefrom(STDERR_FILENO + 1);
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 		(void) sigset(SIGCLD, SIG_DFL);
13127c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
13137c478bd9Sstevel@tonic-gate 		/*
13147c478bd9Sstevel@tonic-gate 		 * In case any of stdin, stdout or stderr are streams,
13157c478bd9Sstevel@tonic-gate 		 * anchor them to prevent malicious I_POPs.
13167c478bd9Sstevel@tonic-gate 		 */
13177c478bd9Sstevel@tonic-gate 		(void) ioctl(STDIN_FILENO, I_ANCHOR);
13187c478bd9Sstevel@tonic-gate 		(void) ioctl(STDOUT_FILENO, I_ANCHOR);
13197c478bd9Sstevel@tonic-gate 		(void) ioctl(STDERR_FILENO, I_ANCHOR);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
13227c478bd9Sstevel@tonic-gate 			zerror(gettext("could not enter zone %s: %s"),
13237c478bd9Sstevel@tonic-gate 			    zonename, strerror(errno));
13247c478bd9Sstevel@tonic-gate 			_exit(1);
13257c478bd9Sstevel@tonic-gate 		}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 		if (!failsafe)
13287c478bd9Sstevel@tonic-gate 			new_env = prep_env_noninteractive(login, new_env);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 		if (new_env == NULL) {
13317c478bd9Sstevel@tonic-gate 			_exit(1);
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		/*
13357c478bd9Sstevel@tonic-gate 		 * Move into a new process group; the zone_enter will have
13367c478bd9Sstevel@tonic-gate 		 * placed us into zsched's session, and we want to be in
13377c478bd9Sstevel@tonic-gate 		 * a unique process group.
13387c478bd9Sstevel@tonic-gate 		 */
13397c478bd9Sstevel@tonic-gate 		(void) setpgid(getpid(), getpid());
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 		(void) execve(new_args[0], new_args, new_env);
13427c478bd9Sstevel@tonic-gate 		zperror(gettext("exec failure"));
13437c478bd9Sstevel@tonic-gate 		_exit(1);
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 	/* parent */
13467c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sig_forward);
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	postfork_dropprivs();
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
13517c478bd9Sstevel@tonic-gate 	(void) close(tmpl_fd);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
13547c478bd9Sstevel@tonic-gate 	doio(stdin_pipe[0], stdout_pipe[0], stderr_pipe[0], B_TRUE);
13557c478bd9Sstevel@tonic-gate 	do {
13567c478bd9Sstevel@tonic-gate 		retval = waitpid(child_pid, &child_status, 0);
13577c478bd9Sstevel@tonic-gate 		if (retval == -1) {
13587c478bd9Sstevel@tonic-gate 			child_status = 0;
13597c478bd9Sstevel@tonic-gate 		}
13607c478bd9Sstevel@tonic-gate 	} while (retval != child_pid && errno != ECHILD);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate int
13667c478bd9Sstevel@tonic-gate main(int argc, char **argv)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	int arg, console = 0;
13697c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
13707c478bd9Sstevel@tonic-gate 	zone_state_t st;
13717c478bd9Sstevel@tonic-gate 	char *login = "root";
13727c478bd9Sstevel@tonic-gate 	int lflag = 0;
13737c478bd9Sstevel@tonic-gate 	char *zonename = NULL;
13747c478bd9Sstevel@tonic-gate 	char **proc_args = NULL;
13757c478bd9Sstevel@tonic-gate 	char **new_args, **new_env;
13767c478bd9Sstevel@tonic-gate 	sigset_t block_cld;
1377facf4a8dSllai1 	char devroot[MAXPATHLEN];
13787c478bd9Sstevel@tonic-gate 	char *slavename, slaveshortname[MAXPATHLEN];
13797c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
13807c478bd9Sstevel@tonic-gate 	int tmpl_fd;
13819acbbeafSnn35248 	char zonebrand[MAXNAMELEN];
1382108322fbScarlsonj 	struct stat sb;
1383108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1384*123807fbSedp 	brand_handle_t bh;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
13877c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	(void) getpname(argv[0]);
13907c478bd9Sstevel@tonic-gate 
1391108322fbScarlsonj 	while ((arg = getopt(argc, argv, "ECR:Se:l:")) != EOF) {
13927c478bd9Sstevel@tonic-gate 		switch (arg) {
13937c478bd9Sstevel@tonic-gate 		case 'C':
13947c478bd9Sstevel@tonic-gate 			console = 1;
13957c478bd9Sstevel@tonic-gate 			break;
13967c478bd9Sstevel@tonic-gate 		case 'E':
13977c478bd9Sstevel@tonic-gate 			nocmdchar = 1;
13987c478bd9Sstevel@tonic-gate 			break;
1399108322fbScarlsonj 		case 'R':	/* undocumented */
1400108322fbScarlsonj 			if (*optarg != '/') {
1401108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
1402108322fbScarlsonj 				exit(2);
1403108322fbScarlsonj 			}
1404108322fbScarlsonj 			if (stat(optarg, &sb) == -1 || !S_ISDIR(sb.st_mode)) {
1405108322fbScarlsonj 				zerror(
1406108322fbScarlsonj 				    gettext("root path must be a directory."));
1407108322fbScarlsonj 				exit(2);
1408108322fbScarlsonj 			}
1409108322fbScarlsonj 			zonecfg_set_root(optarg);
1410108322fbScarlsonj 			break;
14117c478bd9Sstevel@tonic-gate 		case 'S':
14127c478bd9Sstevel@tonic-gate 			failsafe = 1;
14137c478bd9Sstevel@tonic-gate 			break;
14147c478bd9Sstevel@tonic-gate 		case 'e':
14157c478bd9Sstevel@tonic-gate 			set_cmdchar(optarg);
14167c478bd9Sstevel@tonic-gate 			break;
14177c478bd9Sstevel@tonic-gate 		case 'l':
14187c478bd9Sstevel@tonic-gate 			login = optarg;
14197c478bd9Sstevel@tonic-gate 			lflag = 1;
14207c478bd9Sstevel@tonic-gate 			break;
14217c478bd9Sstevel@tonic-gate 		default:
14227c478bd9Sstevel@tonic-gate 			usage();
14237c478bd9Sstevel@tonic-gate 		}
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	if (console != 0 && lflag != 0) {
14277c478bd9Sstevel@tonic-gate 		zerror(gettext("-l may not be specified for console login"));
14287c478bd9Sstevel@tonic-gate 		usage();
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 	if (console != 0 && failsafe != 0) {
14327c478bd9Sstevel@tonic-gate 		zerror(gettext("-S may not be specified for console login"));
14337c478bd9Sstevel@tonic-gate 		usage();
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
1436108322fbScarlsonj 	if (console != 0 && zonecfg_in_alt_root()) {
1437108322fbScarlsonj 		zerror(gettext("-R may not be specified for console login"));
1438108322fbScarlsonj 		exit(2);
1439108322fbScarlsonj 	}
1440108322fbScarlsonj 
14417c478bd9Sstevel@tonic-gate 	if (failsafe != 0 && lflag != 0) {
14427c478bd9Sstevel@tonic-gate 		zerror(gettext("-l may not be specified for failsafe login"));
14437c478bd9Sstevel@tonic-gate 		usage();
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	if (optind == (argc - 1)) {
14477c478bd9Sstevel@tonic-gate 		/*
14487c478bd9Sstevel@tonic-gate 		 * zone name, no process name; this should be an interactive
14497c478bd9Sstevel@tonic-gate 		 * as long as STDIN is really a tty.
14507c478bd9Sstevel@tonic-gate 		 */
14517c478bd9Sstevel@tonic-gate 		if (isatty(STDIN_FILENO))
14527c478bd9Sstevel@tonic-gate 			interactive = 1;
14537c478bd9Sstevel@tonic-gate 		zonename = argv[optind];
14547c478bd9Sstevel@tonic-gate 	} else if (optind < (argc - 1)) {
14557c478bd9Sstevel@tonic-gate 		if (console) {
14567c478bd9Sstevel@tonic-gate 			zerror(gettext("Commands may not be specified for "
14577c478bd9Sstevel@tonic-gate 			    "console login."));
14587c478bd9Sstevel@tonic-gate 			usage();
14597c478bd9Sstevel@tonic-gate 		}
14607c478bd9Sstevel@tonic-gate 		/* zone name and process name, and possibly some args */
14617c478bd9Sstevel@tonic-gate 		zonename = argv[optind];
14627c478bd9Sstevel@tonic-gate 		proc_args = &argv[optind + 1];
14637c478bd9Sstevel@tonic-gate 		interactive = 0;
14647c478bd9Sstevel@tonic-gate 	} else {
14657c478bd9Sstevel@tonic-gate 		usage();
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
14697c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' may only be used from the global zone"),
14707c478bd9Sstevel@tonic-gate 		    pname);
14717c478bd9Sstevel@tonic-gate 		return (1);
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if (strcmp(zonename, GLOBAL_ZONENAME) == 0) {
14757c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' not applicable to the global zone"),
14767c478bd9Sstevel@tonic-gate 		    pname);
14777c478bd9Sstevel@tonic-gate 		return (1);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (zone_get_state(zonename, &st) != Z_OK) {
14817c478bd9Sstevel@tonic-gate 		zerror(gettext("zone '%s' unknown"), zonename);
14827c478bd9Sstevel@tonic-gate 		return (1);
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	if (st < ZONE_STATE_INSTALLED) {
14867c478bd9Sstevel@tonic-gate 		zerror(gettext("cannot login to a zone which is '%s'"),
14877c478bd9Sstevel@tonic-gate 		    zone_state_str(st));
14887c478bd9Sstevel@tonic-gate 		return (1);
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	/*
14927c478bd9Sstevel@tonic-gate 	 * In both console and non-console cases, we require all privs.
14937c478bd9Sstevel@tonic-gate 	 * In the console case, because we may need to startup zoneadmd.
14947c478bd9Sstevel@tonic-gate 	 * In the non-console case in order to do zone_enter(2), zonept()
14957c478bd9Sstevel@tonic-gate 	 * and other tasks.
14967c478bd9Sstevel@tonic-gate 	 *
14977c478bd9Sstevel@tonic-gate 	 * Future work: this solution is temporary.  Ultimately, we need to
14987c478bd9Sstevel@tonic-gate 	 * move to a flexible system which allows the global admin to
14997c478bd9Sstevel@tonic-gate 	 * designate that a particular user can zlogin (and probably zlogin
15007c478bd9Sstevel@tonic-gate 	 * -C) to a particular zone.  This all-root business we have now is
15017c478bd9Sstevel@tonic-gate 	 * quite sketchy.
15027c478bd9Sstevel@tonic-gate 	 */
15037c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
15047c478bd9Sstevel@tonic-gate 		zperror(gettext("priv_allocset failed"));
15057c478bd9Sstevel@tonic-gate 		return (1);
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
15097c478bd9Sstevel@tonic-gate 		zperror(gettext("getppriv failed"));
15107c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
15117c478bd9Sstevel@tonic-gate 		return (1);
15127c478bd9Sstevel@tonic-gate 	}
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
15157c478bd9Sstevel@tonic-gate 		zerror(gettext("You lack sufficient privilege to run "
15167c478bd9Sstevel@tonic-gate 		    "this command (all privs required)"));
15177c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
15187c478bd9Sstevel@tonic-gate 		return (1);
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	/*
15237c478bd9Sstevel@tonic-gate 	 * The console is a separate case from the rest of the code; handle
15247c478bd9Sstevel@tonic-gate 	 * it first.
15257c478bd9Sstevel@tonic-gate 	 */
15267c478bd9Sstevel@tonic-gate 	if (console) {
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 		/*
15297c478bd9Sstevel@tonic-gate 		 * Ensure that zoneadmd for this zone is running.
15307c478bd9Sstevel@tonic-gate 		 */
15317c478bd9Sstevel@tonic-gate 		if (start_zoneadmd(zonename) == -1)
15327c478bd9Sstevel@tonic-gate 			return (1);
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 		/*
15357c478bd9Sstevel@tonic-gate 		 * Make contact with zoneadmd.
15367c478bd9Sstevel@tonic-gate 		 */
15377c478bd9Sstevel@tonic-gate 		if (get_console_master(zonename) == -1)
15387c478bd9Sstevel@tonic-gate 			return (1);
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 		(void) printf(gettext("[Connected to zone '%s' console]\n"),
15417c478bd9Sstevel@tonic-gate 		    zonename);
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 		if (set_tty_rawmode(STDIN_FILENO) == -1) {
15447c478bd9Sstevel@tonic-gate 			reset_tty();
15457c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to set stdin pty to raw mode"));
15467c478bd9Sstevel@tonic-gate 			return (1);
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 		(void) sigset(SIGWINCH, sigwinch);
15507c478bd9Sstevel@tonic-gate 		(void) sigwinch(0);
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 		/*
15537c478bd9Sstevel@tonic-gate 		 * Run the I/O loop until we get disconnected.
15547c478bd9Sstevel@tonic-gate 		 */
15557c478bd9Sstevel@tonic-gate 		doio(masterfd, masterfd, -1, B_FALSE);
15567c478bd9Sstevel@tonic-gate 		reset_tty();
15577c478bd9Sstevel@tonic-gate 		(void) printf(gettext("\n[Connection to zone '%s' console "
15587c478bd9Sstevel@tonic-gate 		    "closed]\n"), zonename);
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 		return (0);
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 
1563108322fbScarlsonj 	if (st != ZONE_STATE_RUNNING && st != ZONE_STATE_MOUNTED) {
15647c478bd9Sstevel@tonic-gate 		zerror(gettext("login allowed only to running zones "
15657c478bd9Sstevel@tonic-gate 		    "(%s is '%s')."), zonename, zone_state_str(st));
15667c478bd9Sstevel@tonic-gate 		return (1);
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 
1569108322fbScarlsonj 	(void) strlcpy(kernzone, zonename, sizeof (kernzone));
1570108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1571108322fbScarlsonj 		FILE *fp = zonecfg_open_scratch("", B_FALSE);
1572108322fbScarlsonj 
1573108322fbScarlsonj 		if (fp == NULL || zonecfg_find_scratch(fp, zonename,
1574108322fbScarlsonj 		    zonecfg_get_root(), kernzone, sizeof (kernzone)) == -1) {
1575108322fbScarlsonj 			zerror(gettext("cannot find scratch zone %s"),
1576108322fbScarlsonj 			    zonename);
1577108322fbScarlsonj 			if (fp != NULL)
1578108322fbScarlsonj 				zonecfg_close_scratch(fp);
1579108322fbScarlsonj 			return (1);
1580108322fbScarlsonj 		}
1581108322fbScarlsonj 		zonecfg_close_scratch(fp);
1582108322fbScarlsonj 	}
1583108322fbScarlsonj 
1584108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kernzone)) == -1) {
15857c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to get zoneid for zone '%s'"),
15867c478bd9Sstevel@tonic-gate 		    zonename);
15877c478bd9Sstevel@tonic-gate 		return (1);
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
1590108322fbScarlsonj 	/*
1591facf4a8dSllai1 	 * We need the zone root path only if we are setting up a pty.
1592108322fbScarlsonj 	 */
1593facf4a8dSllai1 	if (zone_get_devroot(zonename, devroot, sizeof (devroot)) == -1) {
1594facf4a8dSllai1 		zerror(gettext("could not get dev path for zone %s"),
15957c478bd9Sstevel@tonic-gate 		    zonename);
15967c478bd9Sstevel@tonic-gate 		return (1);
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 
15999acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
16009acbbeafSnn35248 	if ((zone_get_brand(zonename, zonebrand, sizeof (zonebrand)) != Z_OK) ||
1601*123807fbSedp 	    ((bh = brand_open(zonebrand)) == NULL)) {
16029acbbeafSnn35248 		zerror(gettext("could not get brand for zone %s"), zonename);
16037c478bd9Sstevel@tonic-gate 		return (1);
16047c478bd9Sstevel@tonic-gate 	}
1605*123807fbSedp 	if ((new_args = prep_args(bh, login, proc_args)) == NULL) {
16069acbbeafSnn35248 		zperror(gettext("could not assemble new arguments"));
1607*123807fbSedp 		brand_close(bh);
16089acbbeafSnn35248 		return (1);
16099acbbeafSnn35248 	}
1610*123807fbSedp 	brand_close(bh);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if ((new_env = prep_env()) == NULL) {
16137c478bd9Sstevel@tonic-gate 		zperror(gettext("could not assemble new environment"));
16147c478bd9Sstevel@tonic-gate 		return (1);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	if (!interactive)
16187c478bd9Sstevel@tonic-gate 		return (noninteractive_login(zonename, zoneid, login, new_args,
16197c478bd9Sstevel@tonic-gate 		    new_env));
16207c478bd9Sstevel@tonic-gate 
1621108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1622108322fbScarlsonj 		zerror(gettext("cannot use interactive login with scratch "
1623108322fbScarlsonj 		    "zone"));
1624108322fbScarlsonj 		return (1);
1625108322fbScarlsonj 	}
1626108322fbScarlsonj 
16277c478bd9Sstevel@tonic-gate 	/*
16287c478bd9Sstevel@tonic-gate 	 * Things are more complex in interactive mode; we get the
16297c478bd9Sstevel@tonic-gate 	 * master side of the pty, then place the user's terminal into
16307c478bd9Sstevel@tonic-gate 	 * raw mode.
16317c478bd9Sstevel@tonic-gate 	 */
16327c478bd9Sstevel@tonic-gate 	if (get_master_pty() == -1) {
16337c478bd9Sstevel@tonic-gate 		zerror(gettext("could not setup master pty device"));
16347c478bd9Sstevel@tonic-gate 		return (1);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	/*
16387c478bd9Sstevel@tonic-gate 	 * Compute the "short name" of the pts.  /dev/pts/2 --> pts/2
16397c478bd9Sstevel@tonic-gate 	 */
16407c478bd9Sstevel@tonic-gate 	if ((slavename = ptsname(masterfd)) == NULL) {
16417c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get name for pseudo-tty"));
16427c478bd9Sstevel@tonic-gate 		return (1);
16437c478bd9Sstevel@tonic-gate 	}
16447c478bd9Sstevel@tonic-gate 	if (strncmp(slavename, "/dev/", strlen("/dev/")) == 0)
16457c478bd9Sstevel@tonic-gate 		(void) strlcpy(slaveshortname, slavename + strlen("/dev/"),
16467c478bd9Sstevel@tonic-gate 		    sizeof (slaveshortname));
16477c478bd9Sstevel@tonic-gate 	else
16487c478bd9Sstevel@tonic-gate 		(void) strlcpy(slaveshortname, slavename,
16497c478bd9Sstevel@tonic-gate 		    sizeof (slaveshortname));
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	(void) printf(gettext("[Connected to zone '%s' %s]\n"), zonename,
16527c478bd9Sstevel@tonic-gate 	    slaveshortname);
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	if (set_tty_rawmode(STDIN_FILENO) == -1) {
16557c478bd9Sstevel@tonic-gate 		reset_tty();
16567c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set stdin pty to raw mode"));
16577c478bd9Sstevel@tonic-gate 		return (1);
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	if (prefork_dropprivs() != 0) {
16617c478bd9Sstevel@tonic-gate 		reset_tty();
16627c478bd9Sstevel@tonic-gate 		zperror(gettext("could not allocate privilege set"));
16637c478bd9Sstevel@tonic-gate 		return (1);
16647c478bd9Sstevel@tonic-gate 	}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	/*
16677c478bd9Sstevel@tonic-gate 	 * We must mask SIGCLD until after we have coped with the fork
16687c478bd9Sstevel@tonic-gate 	 * sufficiently to deal with it; otherwise we can race and receive the
16697c478bd9Sstevel@tonic-gate 	 * signal before child_pid has been initialized (yes, this really
16707c478bd9Sstevel@tonic-gate 	 * happens).
16717c478bd9Sstevel@tonic-gate 	 */
16727c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCLD, sigcld);
16737c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
16747c478bd9Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCLD);
16757c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	/*
16787c478bd9Sstevel@tonic-gate 	 * We activate the contract template at the last minute to
16797c478bd9Sstevel@tonic-gate 	 * avoid intermediate functions that could be using fork(2)
16807c478bd9Sstevel@tonic-gate 	 * internally.
16817c478bd9Sstevel@tonic-gate 	 */
16827c478bd9Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
16837c478bd9Sstevel@tonic-gate 		reset_tty();
16847c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create contract"));
16857c478bd9Sstevel@tonic-gate 		return (1);
16867c478bd9Sstevel@tonic-gate 	}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
16897c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
16907c478bd9Sstevel@tonic-gate 		reset_tty();
16917c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"));
16927c478bd9Sstevel@tonic-gate 		return (1);
16937c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) { /* child process */
16947c478bd9Sstevel@tonic-gate 		int slavefd, newslave;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
16977c478bd9Sstevel@tonic-gate 		(void) close(tmpl_fd);
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
17007c478bd9Sstevel@tonic-gate 
1701facf4a8dSllai1 		if ((slavefd = init_slave_pty(zoneid, devroot)) == -1)
17027c478bd9Sstevel@tonic-gate 			return (1);
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 		/*
17057c478bd9Sstevel@tonic-gate 		 * Close all fds except for the slave pty.
17067c478bd9Sstevel@tonic-gate 		 */
17077c478bd9Sstevel@tonic-gate 		(void) fdwalk(close_func, &slavefd);
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		/*
17107c478bd9Sstevel@tonic-gate 		 * Temporarily dup slavefd to stderr; that way if we have
17117c478bd9Sstevel@tonic-gate 		 * to print out that zone_enter failed, the output will
17127c478bd9Sstevel@tonic-gate 		 * have somewhere to go.
17137c478bd9Sstevel@tonic-gate 		 */
17147c478bd9Sstevel@tonic-gate 		if (slavefd != STDERR_FILENO)
17157c478bd9Sstevel@tonic-gate 			(void) dup2(slavefd, STDERR_FILENO);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
17187c478bd9Sstevel@tonic-gate 			zerror(gettext("could not enter zone %s: %s"),
17197c478bd9Sstevel@tonic-gate 			    zonename, strerror(errno));
17207c478bd9Sstevel@tonic-gate 			return (1);
17217c478bd9Sstevel@tonic-gate 		}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		if (slavefd != STDERR_FILENO)
17247c478bd9Sstevel@tonic-gate 			(void) close(STDERR_FILENO);
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 		/*
17277c478bd9Sstevel@tonic-gate 		 * We take pains to get this process into a new process
17287c478bd9Sstevel@tonic-gate 		 * group, and subsequently a new session.  In this way,
17297c478bd9Sstevel@tonic-gate 		 * we'll have a session which doesn't yet have a controlling
17307c478bd9Sstevel@tonic-gate 		 * terminal.  When we open the slave, it will become the
17317c478bd9Sstevel@tonic-gate 		 * controlling terminal; no PIDs concerning pgrps or sids
17327c478bd9Sstevel@tonic-gate 		 * will leak inappropriately into the zone.
17337c478bd9Sstevel@tonic-gate 		 */
17347c478bd9Sstevel@tonic-gate 		(void) setpgrp();
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 		/*
17377c478bd9Sstevel@tonic-gate 		 * We need the slave pty to be referenced from the zone's
17387c478bd9Sstevel@tonic-gate 		 * /dev in order to ensure that the devt's, etc are all
17397c478bd9Sstevel@tonic-gate 		 * correct.  Otherwise we break ttyname and the like.
17407c478bd9Sstevel@tonic-gate 		 */
17417c478bd9Sstevel@tonic-gate 		if ((newslave = open(slavename, O_RDWR)) == -1) {
17427c478bd9Sstevel@tonic-gate 			(void) close(slavefd);
17437c478bd9Sstevel@tonic-gate 			return (1);
17447c478bd9Sstevel@tonic-gate 		}
17457c478bd9Sstevel@tonic-gate 		(void) close(slavefd);
17467c478bd9Sstevel@tonic-gate 		slavefd = newslave;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		/*
17497c478bd9Sstevel@tonic-gate 		 * dup the slave to the various FDs, so that when the
17507c478bd9Sstevel@tonic-gate 		 * spawned process does a write/read it maps to the slave
17517c478bd9Sstevel@tonic-gate 		 * pty.
17527c478bd9Sstevel@tonic-gate 		 */
17537c478bd9Sstevel@tonic-gate 		(void) dup2(slavefd, STDIN_FILENO);
17547c478bd9Sstevel@tonic-gate 		(void) dup2(slavefd, STDOUT_FILENO);
17557c478bd9Sstevel@tonic-gate 		(void) dup2(slavefd, STDERR_FILENO);
17567c478bd9Sstevel@tonic-gate 		if (slavefd != STDIN_FILENO && slavefd != STDOUT_FILENO &&
17577c478bd9Sstevel@tonic-gate 		    slavefd != STDERR_FILENO) {
17587c478bd9Sstevel@tonic-gate 			(void) close(slavefd);
17597c478bd9Sstevel@tonic-gate 		}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 		/*
17627c478bd9Sstevel@tonic-gate 		 * In failsafe mode, we don't use login(1), so don't try
17637c478bd9Sstevel@tonic-gate 		 * setting up a utmpx entry.
17649acbbeafSnn35248 		 *
17659acbbeafSnn35248 		 * A branded zone may have very different utmpx semantics.
17669acbbeafSnn35248 		 * At the moment, we only have two brand types:
17679acbbeafSnn35248 		 * Solaris-like (native, sn1) and Linux.  In the Solaris
17689acbbeafSnn35248 		 * case, we know exactly how to do the necessary utmpx
17699acbbeafSnn35248 		 * setup.  Fortunately for us, the Linux /bin/login is
17709acbbeafSnn35248 		 * prepared to deal with a non-initialized utmpx entry, so
17719acbbeafSnn35248 		 * we can simply skip it.  If future brands don't fall into
17729acbbeafSnn35248 		 * either category, we'll have to add a per-brand utmpx
17739acbbeafSnn35248 		 * setup hook.
17747c478bd9Sstevel@tonic-gate 		 */
17759acbbeafSnn35248 		if (!failsafe && (strcmp(zonebrand, "lx") != 0))
17767c478bd9Sstevel@tonic-gate 			if (setup_utmpx(slaveshortname) == -1)
17777c478bd9Sstevel@tonic-gate 				return (1);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 		(void) execve(new_args[0], new_args, new_env);
17807c478bd9Sstevel@tonic-gate 		zperror(gettext("exec failure"));
17817c478bd9Sstevel@tonic-gate 		return (1);
17827c478bd9Sstevel@tonic-gate 	}
17837c478bd9Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
17847c478bd9Sstevel@tonic-gate 	(void) close(tmpl_fd);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	/*
17877c478bd9Sstevel@tonic-gate 	 * The rest is only for the parent process.
17887c478bd9Sstevel@tonic-gate 	 */
17897c478bd9Sstevel@tonic-gate 	(void) sigset(SIGWINCH, sigwinch);
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 	postfork_dropprivs();
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
17947c478bd9Sstevel@tonic-gate 	doio(masterfd, masterfd, -1, B_FALSE);
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	reset_tty();
17977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
17987c478bd9Sstevel@tonic-gate 	    gettext("\n[Connection to zone '%s' %s closed]\n"), zonename,
17997c478bd9Sstevel@tonic-gate 	    slaveshortname);
18007c478bd9Sstevel@tonic-gate 
18017c478bd9Sstevel@tonic-gate 	if (pollerr != 0) {
18027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Error: connection closed due "
18037c478bd9Sstevel@tonic-gate 		    "to unexpected pollevents=0x%x.\n"), pollerr);
18047c478bd9Sstevel@tonic-gate 		return (1);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	return (0);
18087c478bd9Sstevel@tonic-gate }
1809