xref: /titanic_52/usr/src/cmd/zlogin/zlogin.c (revision 1100f00d5652de2808b73c61bcfdb3fc87ef1fc8)
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 /*
22d9e728a2Sgjelinek  * Copyright 2007 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>
61858a4b99Ssl108498 #include <sys/wait.h>
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>
67858a4b99Ssl108498 #include <nss_dbdefs.h>
687c478bd9Sstevel@tonic-gate #include <poll.h>
697c478bd9Sstevel@tonic-gate #include <priv.h>
707c478bd9Sstevel@tonic-gate #include <pwd.h>
717c478bd9Sstevel@tonic-gate #include <unistd.h>
727c478bd9Sstevel@tonic-gate #include <utmpx.h>
737c478bd9Sstevel@tonic-gate #include <sac.h>
747c478bd9Sstevel@tonic-gate #include <signal.h>
757c478bd9Sstevel@tonic-gate #include <stdarg.h>
767c478bd9Sstevel@tonic-gate #include <stdio.h>
777c478bd9Sstevel@tonic-gate #include <stdlib.h>
787c478bd9Sstevel@tonic-gate #include <string.h>
797c478bd9Sstevel@tonic-gate #include <strings.h>
807c478bd9Sstevel@tonic-gate #include <stropts.h>
817c478bd9Sstevel@tonic-gate #include <wait.h>
827c478bd9Sstevel@tonic-gate #include <zone.h>
837c478bd9Sstevel@tonic-gate #include <fcntl.h>
847c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
857c478bd9Sstevel@tonic-gate #include <libintl.h>
867c478bd9Sstevel@tonic-gate #include <locale.h>
877c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
887c478bd9Sstevel@tonic-gate #include <libcontract.h>
899acbbeafSnn35248 #include <libbrand.h>
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int masterfd;
927c478bd9Sstevel@tonic-gate static struct termios save_termios;
937c478bd9Sstevel@tonic-gate static struct termios effective_termios;
947c478bd9Sstevel@tonic-gate static int save_fd;
957c478bd9Sstevel@tonic-gate static struct winsize winsize;
967c478bd9Sstevel@tonic-gate static volatile int dead;
977c478bd9Sstevel@tonic-gate static volatile pid_t child_pid = -1;
987c478bd9Sstevel@tonic-gate static int interactive = 0;
997c478bd9Sstevel@tonic-gate static priv_set_t *dropprivs;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static int nocmdchar = 0;
1027c478bd9Sstevel@tonic-gate static int failsafe = 0;
1037c478bd9Sstevel@tonic-gate static char cmdchar = '~';
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static int pollerr = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static const char *pname;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
1107c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #define	SUPATH	"/usr/bin/su"
1147c478bd9Sstevel@tonic-gate #define	FAILSAFESHELL	"/sbin/sh"
1157c478bd9Sstevel@tonic-gate #define	DEFAULTSHELL	"/sbin/sh"
1167c478bd9Sstevel@tonic-gate #define	DEF_PATH	"/usr/sbin:/usr/bin"
1177c478bd9Sstevel@tonic-gate 
118b52a7fd7Sgjelinek /*
119b52a7fd7Sgjelinek  * The ZLOGIN_BUFSIZ is larger than PIPE_BUF so we can be sure we're clearing
120b52a7fd7Sgjelinek  * out the pipe when the child is exiting.  The ZLOGIN_RDBUFSIZ must be less
121b52a7fd7Sgjelinek  * than ZLOGIN_BUFSIZ (because we share the buffer in doio).  This value is
122b52a7fd7Sgjelinek  * also chosen in conjunction with the HI_WATER setting to make sure we
123b52a7fd7Sgjelinek  * don't fill up the pipe.  We can write FIFOHIWAT (16k) into the pipe before
124b52a7fd7Sgjelinek  * blocking.  By having ZLOGIN_RDBUFSIZ set to 1k and HI_WATER set to 8k, we
125b52a7fd7Sgjelinek  * know we can always write a ZLOGIN_RDBUFSIZ chunk into the pipe when there
126b52a7fd7Sgjelinek  * is less than HI_WATER data already in the pipe.
127b52a7fd7Sgjelinek  */
128e6e67599Sdp #define	ZLOGIN_BUFSIZ	8192
129b52a7fd7Sgjelinek #define	ZLOGIN_RDBUFSIZ	1024
130b52a7fd7Sgjelinek #define	HI_WATER	8192
131e6e67599Sdp 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * See canonify() below.  CANONIFY_LEN is the maximum length that a
1347c478bd9Sstevel@tonic-gate  * "canonical" sequence will expand to (backslash, three octal digits, NUL).
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate #define	CANONIFY_LEN 5
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static void
1397c478bd9Sstevel@tonic-gate usage(void)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage: %s [ -CES ] [ -e cmdchar ] "
1427c478bd9Sstevel@tonic-gate 	    "[-l user] zonename [command [args ...] ]\n"), pname);
1437c478bd9Sstevel@tonic-gate 	exit(2);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static const char *
1477c478bd9Sstevel@tonic-gate getpname(const char *arg0)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	const char *p = strrchr(arg0, '/');
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (p == NULL)
1527c478bd9Sstevel@tonic-gate 		p = arg0;
1537c478bd9Sstevel@tonic-gate 	else
1547c478bd9Sstevel@tonic-gate 		p++;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	pname = p;
1577c478bd9Sstevel@tonic-gate 	return (p);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static void
1617c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	va_list alist;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", pname);
1667c478bd9Sstevel@tonic-gate 	va_start(alist, fmt);
1677c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, alist);
1687c478bd9Sstevel@tonic-gate 	va_end(alist);
1697c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate static void
1737c478bd9Sstevel@tonic-gate zperror(const char *str)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	const char *estr;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if ((estr = strerror(errno)) != NULL)
1787c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: %s\n", pname, str, estr);
1797c478bd9Sstevel@tonic-gate 	else
1807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s: errno %d\n", pname, str, errno);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * The first part of our privilege dropping scheme needs to be called before
1857c478bd9Sstevel@tonic-gate  * fork(), since we must have it for security; we don't want to be surprised
1867c478bd9Sstevel@tonic-gate  * later that we couldn't allocate the privset.
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate static int
1897c478bd9Sstevel@tonic-gate prefork_dropprivs()
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	if ((dropprivs = priv_allocset()) == NULL)
1927c478bd9Sstevel@tonic-gate 		return (1);
1937c478bd9Sstevel@tonic-gate 	priv_emptyset(dropprivs);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	/*
1967c478bd9Sstevel@tonic-gate 	 * We need these privileges in order to query session information and
1977c478bd9Sstevel@tonic-gate 	 * send signals.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	if (interactive == 0) {
2007c478bd9Sstevel@tonic-gate 		if (priv_addset(dropprivs, "proc_session") == -1)
2017c478bd9Sstevel@tonic-gate 			return (1);
2027c478bd9Sstevel@tonic-gate 		if (priv_addset(dropprivs, "proc_zone") == -1)
2037c478bd9Sstevel@tonic-gate 			return (1);
2047c478bd9Sstevel@tonic-gate 		if (priv_addset(dropprivs, "proc_owner") == -1)
2057c478bd9Sstevel@tonic-gate 			return (1);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	return (0);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * The second part of the privilege drop.  We are paranoid about being attacked
2137c478bd9Sstevel@tonic-gate  * by the zone, so we drop all privileges.  This should prevent a compromise
2147c478bd9Sstevel@tonic-gate  * which gets us to fork(), exec(), symlink(), etc.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate static void
2177c478bd9Sstevel@tonic-gate postfork_dropprivs()
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	if ((setppriv(PRIV_SET, PRIV_PERMITTED, dropprivs)) == -1) {
2207c478bd9Sstevel@tonic-gate 		zperror(gettext("Warning: could not set permitted privileges"));
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	if ((setppriv(PRIV_SET, PRIV_LIMIT, dropprivs)) == -1) {
2237c478bd9Sstevel@tonic-gate 		zperror(gettext("Warning: could not set limit privileges"));
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	if ((setppriv(PRIV_SET, PRIV_INHERITABLE, dropprivs)) == -1) {
2267c478bd9Sstevel@tonic-gate 		zperror(gettext("Warning: could not set inheritable "
2277c478bd9Sstevel@tonic-gate 		    "privileges"));
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Create the unix domain socket and call the zoneadmd server; handshake
2337c478bd9Sstevel@tonic-gate  * with it to determine whether it will allow us to connect.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate static int
2367c478bd9Sstevel@tonic-gate get_console_master(const char *zname)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int sockfd = -1;
2397c478bd9Sstevel@tonic-gate 	struct sockaddr_un servaddr;
2407c478bd9Sstevel@tonic-gate 	char clientid[MAXPATHLEN];
2417c478bd9Sstevel@tonic-gate 	char handshake[MAXPATHLEN], c;
2427c478bd9Sstevel@tonic-gate 	int msglen;
2437c478bd9Sstevel@tonic-gate 	int i = 0, err = 0;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
2467c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create socket"));
2477c478bd9Sstevel@tonic-gate 		return (-1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	bzero(&servaddr, sizeof (servaddr));
2517c478bd9Sstevel@tonic-gate 	servaddr.sun_family = AF_UNIX;
2527c478bd9Sstevel@tonic-gate 	(void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path),
2537c478bd9Sstevel@tonic-gate 	    "%s/%s.console_sock", ZONES_TMPDIR, zname);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (connect(sockfd, (struct sockaddr *)&servaddr,
2567c478bd9Sstevel@tonic-gate 	    sizeof (servaddr)) == -1) {
2577c478bd9Sstevel@tonic-gate 		zperror(gettext("Could not connect to zone console"));
2587c478bd9Sstevel@tonic-gate 		goto bad;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	masterfd = sockfd;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	msglen = snprintf(clientid, sizeof (clientid), "IDENT %lu %s\n",
2637c478bd9Sstevel@tonic-gate 	    getpid(), setlocale(LC_MESSAGES, NULL));
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (msglen >= sizeof (clientid) || msglen < 0) {
2667c478bd9Sstevel@tonic-gate 		zerror("protocol error");
2677c478bd9Sstevel@tonic-gate 		goto bad;
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (write(masterfd, clientid, msglen) != msglen) {
2717c478bd9Sstevel@tonic-gate 		zerror("protocol error");
2727c478bd9Sstevel@tonic-gate 		goto bad;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	bzero(handshake, sizeof (handshake));
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * Take care not to accumulate more than our fill, and leave room for
2797c478bd9Sstevel@tonic-gate 	 * the NUL at the end.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	while ((err = read(masterfd, &c, 1)) == 1) {
2827c478bd9Sstevel@tonic-gate 		if (i >= (sizeof (handshake) - 1))
2837c478bd9Sstevel@tonic-gate 			break;
2847c478bd9Sstevel@tonic-gate 		if (c == '\n')
2857c478bd9Sstevel@tonic-gate 			break;
2867c478bd9Sstevel@tonic-gate 		handshake[i] = c;
2877c478bd9Sstevel@tonic-gate 		i++;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * If something went wrong during the handshake we bail; perhaps
2927c478bd9Sstevel@tonic-gate 	 * the server died off.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	if (err == -1) {
2957c478bd9Sstevel@tonic-gate 		zperror(gettext("Could not connect to zone console"));
2967c478bd9Sstevel@tonic-gate 		goto bad;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (strncmp(handshake, "OK", sizeof (handshake)) == 0)
3007c478bd9Sstevel@tonic-gate 		return (0);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	zerror(gettext("Console is already in use by process ID %s."),
3037c478bd9Sstevel@tonic-gate 	    handshake);
3047c478bd9Sstevel@tonic-gate bad:
3057c478bd9Sstevel@tonic-gate 	(void) close(sockfd);
3067c478bd9Sstevel@tonic-gate 	masterfd = -1;
3077c478bd9Sstevel@tonic-gate 	return (-1);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate  * Routines to handle pty creation upon zone entry and to shuttle I/O back
3137c478bd9Sstevel@tonic-gate  * and forth between the two terminals.  We also compute and store the
3147c478bd9Sstevel@tonic-gate  * name of the slave terminal associated with the master side.
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate static int
3177c478bd9Sstevel@tonic-gate get_master_pty()
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	if ((masterfd = open("/dev/ptmx", O_RDWR|O_NONBLOCK)) < 0) {
3207c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to obtain a pseudo-tty"));
3217c478bd9Sstevel@tonic-gate 		return (-1);
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	if (tcgetattr(STDIN_FILENO, &save_termios) == -1) {
3247c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get terminal settings from stdin"));
3257c478bd9Sstevel@tonic-gate 		return (-1);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 	(void) ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&winsize);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	return (0);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * This is a bit tricky; normally a pts device will belong to the zone it
3347c478bd9Sstevel@tonic-gate  * is granted to.  But in the case of "entering" a zone, we need to establish
3357c478bd9Sstevel@tonic-gate  * the pty before entering the zone so that we can vector I/O to and from it
3367c478bd9Sstevel@tonic-gate  * from the global zone.
3377c478bd9Sstevel@tonic-gate  *
3387c478bd9Sstevel@tonic-gate  * We use the zonept() call to let the ptm driver know what we are up to;
3397c478bd9Sstevel@tonic-gate  * the only other hairy bit is the setting of zoneslavename (which happens
3407c478bd9Sstevel@tonic-gate  * above, in get_master_pty()).
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate static int
343facf4a8dSllai1 init_slave_pty(zoneid_t zoneid, char *devroot)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	int slavefd = -1;
3467c478bd9Sstevel@tonic-gate 	char *slavename, zoneslavename[MAXPATHLEN];
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	/*
3497c478bd9Sstevel@tonic-gate 	 * Set slave permissions, zone the pts, then unlock it.
3507c478bd9Sstevel@tonic-gate 	 */
3517c478bd9Sstevel@tonic-gate 	if (grantpt(masterfd) != 0) {
3527c478bd9Sstevel@tonic-gate 		zperror(gettext("grantpt failed"));
3537c478bd9Sstevel@tonic-gate 		return (-1);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	if (unlockpt(masterfd) != 0) {
3577c478bd9Sstevel@tonic-gate 		zperror(gettext("unlockpt failed"));
3587c478bd9Sstevel@tonic-gate 		return (-1);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	/*
3627c478bd9Sstevel@tonic-gate 	 * We must open the slave side before zoning this pty; otherwise
3637c478bd9Sstevel@tonic-gate 	 * the kernel would refuse us the open-- zoning a pty makes it
364facf4a8dSllai1 	 * inaccessible to the global zone.  Note we are trying to open
365facf4a8dSllai1 	 * the device node via the $ZONEROOT/dev path for this pty.
3667c478bd9Sstevel@tonic-gate 	 *
3677c478bd9Sstevel@tonic-gate 	 * Later we'll close the slave out when once we've opened it again
3687c478bd9Sstevel@tonic-gate 	 * from within the target zone.  Blarg.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	if ((slavename = ptsname(masterfd)) == NULL) {
3717c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get name for pseudo-tty"));
3727c478bd9Sstevel@tonic-gate 		return (-1);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	(void) snprintf(zoneslavename, sizeof (zoneslavename), "%s%s",
376facf4a8dSllai1 	    devroot, slavename);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	if ((slavefd = open(zoneslavename, O_RDWR)) < 0) {
3797c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to open %s: %s"), zoneslavename,
3807c478bd9Sstevel@tonic-gate 		    strerror(errno));
3817c478bd9Sstevel@tonic-gate 		return (-1);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * Push hardware emulation (ptem), line discipline (ldterm),
3867c478bd9Sstevel@tonic-gate 	 * and V7/4BSD/Xenix compatibility (ttcompat) modules.
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_PUSH, "ptem") == -1) {
3897c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to push ptem module"));
3907c478bd9Sstevel@tonic-gate 		if (!failsafe)
3917c478bd9Sstevel@tonic-gate 			goto bad;
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * Anchor the stream to prevent malicious I_POPs; we prefer to do
3967c478bd9Sstevel@tonic-gate 	 * this prior to entering the zone so that we can detect any errors
3977c478bd9Sstevel@tonic-gate 	 * early, and so that we can set the anchor from the global zone.
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_ANCHOR) == -1) {
4007c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set stream anchor"));
4017c478bd9Sstevel@tonic-gate 		if (!failsafe)
4027c478bd9Sstevel@tonic-gate 			goto bad;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_PUSH, "ldterm") == -1) {
4067c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to push ldterm module"));
4077c478bd9Sstevel@tonic-gate 		if (!failsafe)
4087c478bd9Sstevel@tonic-gate 			goto bad;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_PUSH, "ttcompat") == -1) {
4117c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to push ttcompat module"));
4127c478bd9Sstevel@tonic-gate 		if (!failsafe)
4137c478bd9Sstevel@tonic-gate 			goto bad;
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	/*
4177c478bd9Sstevel@tonic-gate 	 * Propagate terminal settings from the external term to the new one.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	if (tcsetattr(slavefd, TCSAFLUSH, &save_termios) == -1) {
4207c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set terminal settings"));
4217c478bd9Sstevel@tonic-gate 		if (!failsafe)
4227c478bd9Sstevel@tonic-gate 			goto bad;
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	(void) ioctl(slavefd, TIOCSWINSZ, (char *)&winsize);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (zonept(masterfd, zoneid) != 0) {
4277c478bd9Sstevel@tonic-gate 		zperror(gettext("could not set zoneid of pty"));
4287c478bd9Sstevel@tonic-gate 		goto bad;
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	return (slavefd);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate bad:
4347c478bd9Sstevel@tonic-gate 	(void) close(slavefd);
4357c478bd9Sstevel@tonic-gate 	return (-1);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * Place terminal into raw mode.
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate static int
4427c478bd9Sstevel@tonic-gate set_tty_rawmode(int fd)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	struct termios term;
4457c478bd9Sstevel@tonic-gate 	if (tcgetattr(fd, &term) < 0) {
4467c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get user terminal settings"));
4477c478bd9Sstevel@tonic-gate 		return (-1);
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/* Stash for later, so we can revert back to previous mode */
4517c478bd9Sstevel@tonic-gate 	save_termios = term;
4527c478bd9Sstevel@tonic-gate 	save_fd = fd;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/* disable 8->7 bit strip, start/stop, enable any char to restart */
4557c478bd9Sstevel@tonic-gate 	term.c_iflag &= ~(ISTRIP|IXON|IXANY);
4567c478bd9Sstevel@tonic-gate 	/* disable NL->CR, CR->NL, ignore CR, UPPER->lower */
4577c478bd9Sstevel@tonic-gate 	term.c_iflag &= ~(INLCR|ICRNL|IGNCR|IUCLC);
4587c478bd9Sstevel@tonic-gate 	/* disable output post-processing */
4597c478bd9Sstevel@tonic-gate 	term.c_oflag &= ~OPOST;
4607c478bd9Sstevel@tonic-gate 	/* disable canonical mode, signal chars, echo & extended functions */
4617c478bd9Sstevel@tonic-gate 	term.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	term.c_cc[VMIN] = 1;    /* byte-at-a-time */
4647c478bd9Sstevel@tonic-gate 	term.c_cc[VTIME] = 0;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term)) {
4677c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set user terminal to raw mode"));
4687c478bd9Sstevel@tonic-gate 		return (-1);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * We need to know the value of VEOF so that we can properly process for
4737c478bd9Sstevel@tonic-gate 	 * client-side ~<EOF>.  But we have obliterated VEOF in term,
4747c478bd9Sstevel@tonic-gate 	 * because VMIN overloads the same array slot in non-canonical mode.
4757c478bd9Sstevel@tonic-gate 	 * Stupid @&^%!
4767c478bd9Sstevel@tonic-gate 	 *
4777c478bd9Sstevel@tonic-gate 	 * So here we construct the "effective" termios from the current
4787c478bd9Sstevel@tonic-gate 	 * terminal settings, and the corrected VEOF and VEOL settings.
4797c478bd9Sstevel@tonic-gate 	 */
4807c478bd9Sstevel@tonic-gate 	if (tcgetattr(STDIN_FILENO, &effective_termios) < 0) {
4817c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get user terminal settings"));
4827c478bd9Sstevel@tonic-gate 		return (-1);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	effective_termios.c_cc[VEOF] = save_termios.c_cc[VEOF];
4857c478bd9Sstevel@tonic-gate 	effective_termios.c_cc[VEOL] = save_termios.c_cc[VEOL];
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	return (0);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate  * Copy terminal window size from our terminal to the pts.
4927c478bd9Sstevel@tonic-gate  */
4937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4947c478bd9Sstevel@tonic-gate static void
4957c478bd9Sstevel@tonic-gate sigwinch(int s)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	struct winsize ws;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if (ioctl(0, TIOCGWINSZ, &ws) == 0)
5007c478bd9Sstevel@tonic-gate 		(void) ioctl(masterfd, TIOCSWINSZ, &ws);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate static void
5047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5057c478bd9Sstevel@tonic-gate sigcld(int s)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	int status;
5087c478bd9Sstevel@tonic-gate 	pid_t pid;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * Peek at the exit status.  If this isn't the process we cared
5127c478bd9Sstevel@tonic-gate 	 * about, then just reap it.
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	if ((pid = waitpid(child_pid, &status, WNOHANG|WNOWAIT)) != -1) {
5157c478bd9Sstevel@tonic-gate 		if (pid == child_pid &&
5167c478bd9Sstevel@tonic-gate 		    (WIFEXITED(status) || WIFSIGNALED(status)))
5177c478bd9Sstevel@tonic-gate 			dead = 1;
5187c478bd9Sstevel@tonic-gate 		else
5197c478bd9Sstevel@tonic-gate 			(void) waitpid(pid, &status, WNOHANG);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Some signals (currently, SIGINT) must be forwarded on to the process
5257c478bd9Sstevel@tonic-gate  * group of the child process.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate static void
5287c478bd9Sstevel@tonic-gate sig_forward(int s)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	if (child_pid != -1) {
5317c478bd9Sstevel@tonic-gate 		pid_t pgid = getpgid(child_pid);
5327c478bd9Sstevel@tonic-gate 		if (pgid != -1)
5337c478bd9Sstevel@tonic-gate 			(void) sigsend(P_PGID, pgid, s);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate /*
5387c478bd9Sstevel@tonic-gate  * reset terminal settings for global environment
5397c478bd9Sstevel@tonic-gate  */
5407c478bd9Sstevel@tonic-gate static void
5417c478bd9Sstevel@tonic-gate reset_tty()
5427c478bd9Sstevel@tonic-gate {
5437c478bd9Sstevel@tonic-gate 	(void) tcsetattr(save_fd, TCSADRAIN, &save_termios);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  * Convert character to printable representation, for display with locally
5487c478bd9Sstevel@tonic-gate  * echoed command characters (like when we need to display ~^D)
5497c478bd9Sstevel@tonic-gate  */
5507c478bd9Sstevel@tonic-gate static void
5517c478bd9Sstevel@tonic-gate canonify(char c, char *cc)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	if (isprint(c)) {
5547c478bd9Sstevel@tonic-gate 		cc[0] = c;
5557c478bd9Sstevel@tonic-gate 		cc[1] = '\0';
5567c478bd9Sstevel@tonic-gate 	} else if (c >= 0 && c <= 31) {	/* ^@ through ^_ */
5577c478bd9Sstevel@tonic-gate 		cc[0] = '^';
5587c478bd9Sstevel@tonic-gate 		cc[1] = c + '@';
5597c478bd9Sstevel@tonic-gate 		cc[2] = '\0';
5607c478bd9Sstevel@tonic-gate 	} else {
5617c478bd9Sstevel@tonic-gate 		cc[0] = '\\';
5627c478bd9Sstevel@tonic-gate 		cc[1] = ((c >> 6) & 7) + '0';
5637c478bd9Sstevel@tonic-gate 		cc[2] = ((c >> 3) & 7) + '0';
5647c478bd9Sstevel@tonic-gate 		cc[3] = (c & 7) + '0';
5657c478bd9Sstevel@tonic-gate 		cc[4] = '\0';
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate  * process_user_input watches the input stream for the escape sequence for
5717c478bd9Sstevel@tonic-gate  * 'quit' (by default, tilde-period).  Because we might be fed just one
5727c478bd9Sstevel@tonic-gate  * keystroke at a time, state associated with the user input (are we at the
5737c478bd9Sstevel@tonic-gate  * beginning of the line?  are we locally echoing the next character?) is
5747c478bd9Sstevel@tonic-gate  * maintained by beginning_of_line and local_echo across calls to the routine.
575d9e728a2Sgjelinek  * If the write to outfd fails, we'll try to read from infd in an attempt
576d9e728a2Sgjelinek  * to prevent deadlock between the two processes.
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * This routine returns -1 when the 'quit' escape sequence has been issued,
5797c478bd9Sstevel@tonic-gate  * and 0 otherwise.
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate static int
582d9e728a2Sgjelinek process_user_input(int outfd, int infd, char *buf, size_t nbytes)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	static boolean_t beginning_of_line = B_TRUE;
5857c478bd9Sstevel@tonic-gate 	static boolean_t local_echo = B_FALSE;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	char c = *buf;
5887c478bd9Sstevel@tonic-gate 	for (c = *buf; nbytes > 0; c = *buf, --nbytes) {
5897c478bd9Sstevel@tonic-gate 		buf++;
5907c478bd9Sstevel@tonic-gate 		if (beginning_of_line && !nocmdchar) {
5917c478bd9Sstevel@tonic-gate 			beginning_of_line = B_FALSE;
5927c478bd9Sstevel@tonic-gate 			if (c == cmdchar) {
5937c478bd9Sstevel@tonic-gate 				local_echo = B_TRUE;
5947c478bd9Sstevel@tonic-gate 				continue;
5957c478bd9Sstevel@tonic-gate 			}
5967c478bd9Sstevel@tonic-gate 		} else if (local_echo) {
5977c478bd9Sstevel@tonic-gate 			local_echo = B_FALSE;
5987c478bd9Sstevel@tonic-gate 			if (c == '.' || c == effective_termios.c_cc[VEOF]) {
5997c478bd9Sstevel@tonic-gate 				char cc[CANONIFY_LEN];
600d9e728a2Sgjelinek 
6017c478bd9Sstevel@tonic-gate 				canonify(c, cc);
6027c478bd9Sstevel@tonic-gate 				(void) write(STDOUT_FILENO, &cmdchar, 1);
6037c478bd9Sstevel@tonic-gate 				(void) write(STDOUT_FILENO, cc, strlen(cc));
6047c478bd9Sstevel@tonic-gate 				return (-1);
6057c478bd9Sstevel@tonic-gate 			}
6067c478bd9Sstevel@tonic-gate 		}
607d9e728a2Sgjelinek retry:
608d9e728a2Sgjelinek 		if (write(outfd, &c, 1) <= 0) {
609d9e728a2Sgjelinek 			/*
610d9e728a2Sgjelinek 			 * Since the fd we are writing to is opened with
611d9e728a2Sgjelinek 			 * O_NONBLOCK it is possible to get EAGAIN if the
612d9e728a2Sgjelinek 			 * pipe is full.  One way this could happen is if we
613d9e728a2Sgjelinek 			 * are writing a lot of data into the pipe in this loop
614d9e728a2Sgjelinek 			 * and the application on the other end is echoing that
615d9e728a2Sgjelinek 			 * data back out to its stdout.  The output pipe can
616d9e728a2Sgjelinek 			 * fill up since we are stuck here in this loop and not
617d9e728a2Sgjelinek 			 * draining the other pipe.  We can try to read some of
618d9e728a2Sgjelinek 			 * the data to see if we can drain the pipe so that the
619d9e728a2Sgjelinek 			 * application can continue to make progress.  The read
620d9e728a2Sgjelinek 			 * is non-blocking so we won't hang here.  We also wait
621d9e728a2Sgjelinek 			 * a bit before retrying since there could be other
622d9e728a2Sgjelinek 			 * reasons why the pipe is full and we don't want to
623d9e728a2Sgjelinek 			 * continuously retry.
624d9e728a2Sgjelinek 			 */
625d9e728a2Sgjelinek 			if (errno == EAGAIN) {
626d9e728a2Sgjelinek 				struct timespec rqtp;
627d9e728a2Sgjelinek 				int ln;
628d9e728a2Sgjelinek 				char ibuf[ZLOGIN_BUFSIZ];
629d9e728a2Sgjelinek 
630d9e728a2Sgjelinek 				if ((ln = read(infd, ibuf, ZLOGIN_BUFSIZ)) > 0)
631d9e728a2Sgjelinek 					(void) write(STDOUT_FILENO, ibuf, ln);
632d9e728a2Sgjelinek 
633d9e728a2Sgjelinek 				/* sleep for 10 milliseconds */
634d9e728a2Sgjelinek 				rqtp.tv_sec = 0;
635d9e728a2Sgjelinek 				rqtp.tv_nsec = 10 * (NANOSEC / MILLISEC);
636d9e728a2Sgjelinek 				(void) nanosleep(&rqtp, NULL);
637d9e728a2Sgjelinek 				if (!dead)
638d9e728a2Sgjelinek 					goto retry;
639d9e728a2Sgjelinek 			}
640d9e728a2Sgjelinek 
6417c478bd9Sstevel@tonic-gate 			return (-1);
642d9e728a2Sgjelinek 		}
6437c478bd9Sstevel@tonic-gate 		beginning_of_line = (c == '\r' || c == '\n' ||
6447c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VKILL] ||
6457c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VEOL] ||
6467c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VSUSP] ||
6477c478bd9Sstevel@tonic-gate 		    c == effective_termios.c_cc[VINTR]);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 	return (0);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*
653b52a7fd7Sgjelinek  * This function prevents deadlock between zlogin and the application in the
654b52a7fd7Sgjelinek  * zone that it is talking to.  This can happen when we read from zlogin's
655b52a7fd7Sgjelinek  * stdin and write the data down the pipe to the application.  If the pipe
656b52a7fd7Sgjelinek  * is full, we'll block in the write.  Because zlogin could be blocked in
657b52a7fd7Sgjelinek  * the write, it would never read the application's stdout/stderr so the
658b52a7fd7Sgjelinek  * application can then block on those writes (when the pipe fills up).  If the
659b52a7fd7Sgjelinek  * the application gets blocked this way, it can never get around to reading
660b52a7fd7Sgjelinek  * its stdin so that zlogin can unblock from its write.  Once in this state,
661b52a7fd7Sgjelinek  * the two processes are deadlocked.
662b52a7fd7Sgjelinek  *
663b52a7fd7Sgjelinek  * To prevent this, we want to verify that we can write into the pipe before we
664b52a7fd7Sgjelinek  * read from our stdin.  If the pipe already is pretty full, we bypass the read
665b52a7fd7Sgjelinek  * for now.  We'll circle back here again after the poll() so that we can
666b52a7fd7Sgjelinek  * try again.  When this function is called, we already know there is data
667b52a7fd7Sgjelinek  * ready to read on STDIN_FILENO.  We return -1 if there is a problem, 0
668b52a7fd7Sgjelinek  * if everything is ok (even though we might not have read/written any data
669b52a7fd7Sgjelinek  * into the pipe on this iteration).
670b52a7fd7Sgjelinek  */
671b52a7fd7Sgjelinek static int
672b52a7fd7Sgjelinek process_raw_input(int stdin_fd, int appin_fd)
673b52a7fd7Sgjelinek {
674b52a7fd7Sgjelinek 	int cc;
675b52a7fd7Sgjelinek 	struct stat sb;
676b52a7fd7Sgjelinek 	char ibuf[ZLOGIN_RDBUFSIZ];
677b52a7fd7Sgjelinek 
678b52a7fd7Sgjelinek 	/* Check how much data is already in the pipe */
679b52a7fd7Sgjelinek 	if (fstat(appin_fd, &sb) == -1) {
680b52a7fd7Sgjelinek 		perror("stat failed");
681b52a7fd7Sgjelinek 		return (-1);
682b52a7fd7Sgjelinek 	}
683b52a7fd7Sgjelinek 
684b52a7fd7Sgjelinek 	if (dead)
685b52a7fd7Sgjelinek 		return (-1);
686b52a7fd7Sgjelinek 
687b52a7fd7Sgjelinek 	/*
688b52a7fd7Sgjelinek 	 * The pipe already has a lot of data in it,  don't write any more
689b52a7fd7Sgjelinek 	 * right now.
690b52a7fd7Sgjelinek 	 */
691b52a7fd7Sgjelinek 	if (sb.st_size >= HI_WATER)
692b52a7fd7Sgjelinek 		return (0);
693b52a7fd7Sgjelinek 
694b52a7fd7Sgjelinek 	cc = read(STDIN_FILENO, ibuf, ZLOGIN_RDBUFSIZ);
695b52a7fd7Sgjelinek 	if (cc == -1 && (errno != EINTR || dead))
696b52a7fd7Sgjelinek 		return (-1);
697b52a7fd7Sgjelinek 
698b52a7fd7Sgjelinek 	if (cc == -1)	/* The read was interrupted. */
699b52a7fd7Sgjelinek 		return (0);
700b52a7fd7Sgjelinek 
701b52a7fd7Sgjelinek 	/*
702b52a7fd7Sgjelinek 	 * stdin_fd is stdin of the target; so, the thing we'll write the user
703b52a7fd7Sgjelinek 	 * data *to*.  Also, unlike on the output side, we propagate
704b52a7fd7Sgjelinek 	 * zero-length messages to the other side.
705b52a7fd7Sgjelinek 	 */
706b52a7fd7Sgjelinek 	if (write(stdin_fd, ibuf, cc) == -1)
707b52a7fd7Sgjelinek 		return (-1);
708b52a7fd7Sgjelinek 
709b52a7fd7Sgjelinek 	return (0);
710b52a7fd7Sgjelinek }
711b52a7fd7Sgjelinek 
712b52a7fd7Sgjelinek /*
713b52a7fd7Sgjelinek  * Write the output from the application running in the zone.  We can get
714b52a7fd7Sgjelinek  * a signal during the write (usually it would be SIGCHLD when the application
715b52a7fd7Sgjelinek  * has exited) so we loop to make sure we have written all of the data we read.
716b52a7fd7Sgjelinek  */
717b52a7fd7Sgjelinek static int
718b52a7fd7Sgjelinek process_output(int in_fd, int out_fd)
719b52a7fd7Sgjelinek {
720b52a7fd7Sgjelinek 	int wrote = 0;
721b52a7fd7Sgjelinek 	int cc;
722b52a7fd7Sgjelinek 	char ibuf[ZLOGIN_BUFSIZ];
723b52a7fd7Sgjelinek 
724b52a7fd7Sgjelinek 	cc = read(in_fd, ibuf, ZLOGIN_BUFSIZ);
725b52a7fd7Sgjelinek 	if (cc == -1 && (errno != EINTR || dead))
726b52a7fd7Sgjelinek 		return (-1);
727b52a7fd7Sgjelinek 	if (cc == 0)	/* EOF */
728b52a7fd7Sgjelinek 		return (-1);
729b52a7fd7Sgjelinek 	if (cc == -1)	/* The read was interrupted. */
730b52a7fd7Sgjelinek 		return (0);
731b52a7fd7Sgjelinek 
732b52a7fd7Sgjelinek 	do {
733b52a7fd7Sgjelinek 		int len;
734b52a7fd7Sgjelinek 
735b52a7fd7Sgjelinek 		len = write(out_fd, ibuf + wrote, cc - wrote);
736b52a7fd7Sgjelinek 		if (len == -1 && errno != EINTR)
737b52a7fd7Sgjelinek 			return (-1);
738b52a7fd7Sgjelinek 		if (len != -1)
739b52a7fd7Sgjelinek 			wrote += len;
740b52a7fd7Sgjelinek 	} while (wrote < cc);
741b52a7fd7Sgjelinek 
742b52a7fd7Sgjelinek 	return (0);
743b52a7fd7Sgjelinek }
744b52a7fd7Sgjelinek 
745b52a7fd7Sgjelinek /*
7467c478bd9Sstevel@tonic-gate  * This is the main I/O loop, and is shared across all zlogin modes.
7477c478bd9Sstevel@tonic-gate  * Parameters:
7487c478bd9Sstevel@tonic-gate  * 	stdin_fd:  The fd representing 'stdin' for the slave side; input to
7497c478bd9Sstevel@tonic-gate  *		   the zone will be written here.
7507c478bd9Sstevel@tonic-gate  *
751b52a7fd7Sgjelinek  * 	appin_fd:  The fd representing the other end of the 'stdin' pipe (when
752b52a7fd7Sgjelinek  *		   we're running non-interactive); used in process_raw_input
753b52a7fd7Sgjelinek  *		   to ensure we don't fill up the application's stdin pipe.
754b52a7fd7Sgjelinek  *
7557c478bd9Sstevel@tonic-gate  *	stdout_fd: The fd representing 'stdout' for the slave side; output
7567c478bd9Sstevel@tonic-gate  *		   from the zone will arrive here.
7577c478bd9Sstevel@tonic-gate  *
7587c478bd9Sstevel@tonic-gate  *	stderr_fd: The fd representing 'stderr' for the slave side; output
7597c478bd9Sstevel@tonic-gate  *		   from the zone will arrive here.
7607c478bd9Sstevel@tonic-gate  *
7617c478bd9Sstevel@tonic-gate  *	raw_mode:  If TRUE, then no processing (for example, for '~.') will
7627c478bd9Sstevel@tonic-gate  *		   be performed on the input coming from STDIN.
7637c478bd9Sstevel@tonic-gate  *
7647c478bd9Sstevel@tonic-gate  * stderr_fd may be specified as -1 if there is no stderr (only non-interactive
7657c478bd9Sstevel@tonic-gate  * mode supplies a stderr).
7667c478bd9Sstevel@tonic-gate  *
7677c478bd9Sstevel@tonic-gate  */
7687c478bd9Sstevel@tonic-gate static void
769b52a7fd7Sgjelinek doio(int stdin_fd, int appin_fd, int stdout_fd, int stderr_fd,
770b52a7fd7Sgjelinek     boolean_t raw_mode)
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate 	struct pollfd pollfds[3];
773e6e67599Sdp 	char ibuf[ZLOGIN_BUFSIZ];
7747c478bd9Sstevel@tonic-gate 	int cc, ret;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/* read from stdout of zone and write to stdout of global zone */
7777c478bd9Sstevel@tonic-gate 	pollfds[0].fd = stdout_fd;
7787c478bd9Sstevel@tonic-gate 	pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/* read from stderr of zone and write to stderr of global zone */
7817c478bd9Sstevel@tonic-gate 	pollfds[1].fd = stderr_fd;
7827c478bd9Sstevel@tonic-gate 	pollfds[1].events = pollfds[0].events;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/* read from stdin of global zone and write to stdin of zone */
7857c478bd9Sstevel@tonic-gate 	pollfds[2].fd = STDIN_FILENO;
7867c478bd9Sstevel@tonic-gate 	pollfds[2].events = pollfds[0].events;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	for (;;) {
7897c478bd9Sstevel@tonic-gate 		pollfds[0].revents = pollfds[1].revents =
7907c478bd9Sstevel@tonic-gate 		    pollfds[2].revents = 0;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		if (dead)
7937c478bd9Sstevel@tonic-gate 			break;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		ret = poll(pollfds,
7967c478bd9Sstevel@tonic-gate 		    sizeof (pollfds) / sizeof (struct pollfd), -1);
7977c478bd9Sstevel@tonic-gate 		if (ret == -1 && errno != EINTR) {
7987c478bd9Sstevel@tonic-gate 			perror("poll failed");
7997c478bd9Sstevel@tonic-gate 			break;
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		if (errno == EINTR && dead) {
8037c478bd9Sstevel@tonic-gate 			break;
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 		/* event from master side stdout */
8077c478bd9Sstevel@tonic-gate 		if (pollfds[0].revents) {
8087c478bd9Sstevel@tonic-gate 			if (pollfds[0].revents &
8097c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
810b52a7fd7Sgjelinek 				if (process_output(stdout_fd, STDOUT_FILENO)
811b52a7fd7Sgjelinek 				    != 0)
8127c478bd9Sstevel@tonic-gate 					break;
8137c478bd9Sstevel@tonic-gate 			} else {
8147c478bd9Sstevel@tonic-gate 				pollerr = pollfds[0].revents;
8157c478bd9Sstevel@tonic-gate 				break;
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		/* event from master side stderr */
8207c478bd9Sstevel@tonic-gate 		if (pollfds[1].revents) {
8217c478bd9Sstevel@tonic-gate 			if (pollfds[1].revents &
8227c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
823b52a7fd7Sgjelinek 				if (process_output(stderr_fd, STDERR_FILENO)
824b52a7fd7Sgjelinek 				    != 0)
8257c478bd9Sstevel@tonic-gate 					break;
8267c478bd9Sstevel@tonic-gate 			} else {
8277c478bd9Sstevel@tonic-gate 				pollerr = pollfds[1].revents;
8287c478bd9Sstevel@tonic-gate 				break;
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		/* event from user STDIN side */
8337c478bd9Sstevel@tonic-gate 		if (pollfds[2].revents) {
8347c478bd9Sstevel@tonic-gate 			if (pollfds[2].revents &
8357c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
8367c478bd9Sstevel@tonic-gate 				/*
8377c478bd9Sstevel@tonic-gate 				 * stdin fd is stdin of the target; so,
8387c478bd9Sstevel@tonic-gate 				 * the thing we'll write the user data *to*.
8397c478bd9Sstevel@tonic-gate 				 *
8407c478bd9Sstevel@tonic-gate 				 * Also, unlike on the output side, we
8417c478bd9Sstevel@tonic-gate 				 * propagate zero-length messages to the
8427c478bd9Sstevel@tonic-gate 				 * other side.
8437c478bd9Sstevel@tonic-gate 				 */
8447c478bd9Sstevel@tonic-gate 				if (raw_mode == B_TRUE) {
845b52a7fd7Sgjelinek 					if (process_raw_input(stdin_fd,
846b52a7fd7Sgjelinek 					    appin_fd) == -1)
8477c478bd9Sstevel@tonic-gate 						break;
8487c478bd9Sstevel@tonic-gate 				} else {
849b52a7fd7Sgjelinek 					cc = read(STDIN_FILENO, ibuf,
850b52a7fd7Sgjelinek 					    ZLOGIN_RDBUFSIZ);
851b52a7fd7Sgjelinek 					if (cc == -1 &&
852b52a7fd7Sgjelinek 					    (errno != EINTR || dead))
853b52a7fd7Sgjelinek 						break;
854b52a7fd7Sgjelinek 
855b52a7fd7Sgjelinek 					if (cc != -1 &&
856b52a7fd7Sgjelinek 					    process_user_input(stdin_fd,
857d9e728a2Sgjelinek 					    stdout_fd, ibuf, cc) == -1)
8587c478bd9Sstevel@tonic-gate 						break;
8597c478bd9Sstevel@tonic-gate 				}
8607c478bd9Sstevel@tonic-gate 			} else if (raw_mode == B_TRUE &&
8617c478bd9Sstevel@tonic-gate 			    pollfds[2].revents & POLLHUP) {
8627c478bd9Sstevel@tonic-gate 				/*
8637c478bd9Sstevel@tonic-gate 				 * It's OK to get a POLLHUP on STDIN-- it
8647c478bd9Sstevel@tonic-gate 				 * always happens if you do:
8657c478bd9Sstevel@tonic-gate 				 *
8667c478bd9Sstevel@tonic-gate 				 * echo foo | zlogin <zone> <command>
8677c478bd9Sstevel@tonic-gate 				 *
8687c478bd9Sstevel@tonic-gate 				 * We reset fd to -1 in this case to clear
8697c478bd9Sstevel@tonic-gate 				 * the condition and write an EOF to the
8707c478bd9Sstevel@tonic-gate 				 * other side in order to wrap things up.
8717c478bd9Sstevel@tonic-gate 				 */
8727c478bd9Sstevel@tonic-gate 				pollfds[2].fd = -1;
8737c478bd9Sstevel@tonic-gate 				(void) write(stdin_fd, ibuf, 0);
8747c478bd9Sstevel@tonic-gate 			} else {
8757c478bd9Sstevel@tonic-gate 				pollerr = pollfds[2].revents;
8767c478bd9Sstevel@tonic-gate 				break;
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/*
8827c478bd9Sstevel@tonic-gate 	 * We are in the midst of dying, but try to poll with a short
8837c478bd9Sstevel@tonic-gate 	 * timeout to see if we can catch the last bit of I/O from the
8847c478bd9Sstevel@tonic-gate 	 * children.
8857c478bd9Sstevel@tonic-gate 	 */
886b52a7fd7Sgjelinek retry:
887b52a7fd7Sgjelinek 	pollfds[0].revents = pollfds[1].revents = 0;
888b52a7fd7Sgjelinek 	(void) poll(pollfds, 2, 100);
8897c478bd9Sstevel@tonic-gate 	if (pollfds[0].revents &
8907c478bd9Sstevel@tonic-gate 	    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
891b52a7fd7Sgjelinek 		if ((cc = read(stdout_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) {
8927c478bd9Sstevel@tonic-gate 			(void) write(STDOUT_FILENO, ibuf, cc);
893b52a7fd7Sgjelinek 			goto retry;
894b52a7fd7Sgjelinek 		}
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 	if (pollfds[1].revents &
8977c478bd9Sstevel@tonic-gate 	    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
898b52a7fd7Sgjelinek 		if ((cc = read(stderr_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) {
8997c478bd9Sstevel@tonic-gate 			(void) write(STDERR_FILENO, ibuf, cc);
900b52a7fd7Sgjelinek 			goto retry;
901b52a7fd7Sgjelinek 		}
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
905858a4b99Ssl108498 /*
906858a4b99Ssl108498  * Fetch the user_cmd brand hook for getting a user's passwd(4) entry.
907858a4b99Ssl108498  */
908858a4b99Ssl108498 static const char *
909858a4b99Ssl108498 zone_get_user_cmd(brand_handle_t bh, const char *login, char *user_cmd,
910858a4b99Ssl108498     size_t len)
911858a4b99Ssl108498 {
912858a4b99Ssl108498 	bzero(user_cmd, sizeof (user_cmd));
913858a4b99Ssl108498 	if (brand_get_user_cmd(bh, login, user_cmd, len) != 0)
914858a4b99Ssl108498 		return (NULL);
915858a4b99Ssl108498 
916858a4b99Ssl108498 	return (user_cmd);
917858a4b99Ssl108498 }
918858a4b99Ssl108498 
919858a4b99Ssl108498 /* From libc */
920858a4b99Ssl108498 extern int str2passwd(const char *, int, void *, char *, int);
921858a4b99Ssl108498 
922858a4b99Ssl108498 /*
923858a4b99Ssl108498  * exec() the user_cmd brand hook, and convert the output string to a
924858a4b99Ssl108498  * struct passwd.  This is to be called after zone_enter().
925858a4b99Ssl108498  *
926858a4b99Ssl108498  */
927858a4b99Ssl108498 static struct passwd *
928858a4b99Ssl108498 zone_get_user_pw(const char *user_cmd, struct passwd *pwent, char *pwbuf,
929858a4b99Ssl108498     int pwbuflen)
930858a4b99Ssl108498 {
931858a4b99Ssl108498 	char pwline[NSS_BUFLEN_PASSWD];
932858a4b99Ssl108498 	char *cin = NULL;
933858a4b99Ssl108498 	FILE *fin;
934858a4b99Ssl108498 	int status;
935858a4b99Ssl108498 
936858a4b99Ssl108498 	assert(getzoneid() != GLOBAL_ZONEID);
937858a4b99Ssl108498 
938858a4b99Ssl108498 	if ((fin = popen(user_cmd, "r")) == NULL)
939858a4b99Ssl108498 		return (NULL);
940858a4b99Ssl108498 
941858a4b99Ssl108498 	while (cin == NULL && !feof(fin))
942858a4b99Ssl108498 		cin = fgets(pwline, sizeof (pwline), fin);
943858a4b99Ssl108498 
944858a4b99Ssl108498 	if (cin == NULL) {
945858a4b99Ssl108498 		(void) pclose(fin);
946858a4b99Ssl108498 		return (NULL);
947858a4b99Ssl108498 	}
948858a4b99Ssl108498 
949858a4b99Ssl108498 	status = pclose(fin);
950858a4b99Ssl108498 	if (!WIFEXITED(status))
951858a4b99Ssl108498 		return (NULL);
952858a4b99Ssl108498 	if (WEXITSTATUS(status) != 0)
953858a4b99Ssl108498 		return (NULL);
954858a4b99Ssl108498 
955858a4b99Ssl108498 	if (str2passwd(pwline, sizeof (pwline), pwent, pwbuf, pwbuflen) == 0)
956858a4b99Ssl108498 		return (pwent);
957858a4b99Ssl108498 	else
958858a4b99Ssl108498 		return (NULL);
959858a4b99Ssl108498 }
960858a4b99Ssl108498 
9619acbbeafSnn35248 static char **
962123807fbSedp zone_login_cmd(brand_handle_t bh, const char *login)
9639acbbeafSnn35248 {
9649acbbeafSnn35248 	static char result_buf[ARG_MAX];
9659acbbeafSnn35248 	char **new_argv, *ptr, *lasts;
9669acbbeafSnn35248 	int n, a;
9679acbbeafSnn35248 
9689acbbeafSnn35248 	/* Get the login command for the target zone. */
9699acbbeafSnn35248 	bzero(result_buf, sizeof (result_buf));
970123807fbSedp 	if (brand_get_login_cmd(bh, login,
9719acbbeafSnn35248 	    result_buf, sizeof (result_buf)) != 0)
9729acbbeafSnn35248 		return (NULL);
9739acbbeafSnn35248 
9749acbbeafSnn35248 	/*
9759acbbeafSnn35248 	 * We got back a string that we'd like to execute.  But since
9769acbbeafSnn35248 	 * we're not doing the execution via a shell we'll need to convert
9779acbbeafSnn35248 	 * the exec string to an array of strings.  We'll do that here
9789acbbeafSnn35248 	 * but we're going to be very simplistic about it and break stuff
9799acbbeafSnn35248 	 * up based on spaces.  We're not even going to support any kind
9809acbbeafSnn35248 	 * of quoting or escape characters.  It's truly amazing that
9819acbbeafSnn35248 	 * there is no library function in OpenSolaris to do this for us.
9829acbbeafSnn35248 	 */
9839acbbeafSnn35248 
9849acbbeafSnn35248 	/*
9859acbbeafSnn35248 	 * Be paranoid.  Since we're deliniating based on spaces make
9869acbbeafSnn35248 	 * sure there are no adjacent spaces.
9879acbbeafSnn35248 	 */
9889acbbeafSnn35248 	if (strstr(result_buf, "  ") != NULL)
9899acbbeafSnn35248 		return (NULL);
9909acbbeafSnn35248 
9919acbbeafSnn35248 	/* Remove any trailing whitespace.  */
9929acbbeafSnn35248 	n = strlen(result_buf);
9939acbbeafSnn35248 	if (result_buf[n - 1] == ' ')
9949acbbeafSnn35248 		result_buf[n - 1] = '\0';
9959acbbeafSnn35248 
9969acbbeafSnn35248 	/* Count how many elements there are in the exec string. */
9979acbbeafSnn35248 	ptr = result_buf;
9989acbbeafSnn35248 	for (n = 2; ((ptr = strchr(ptr + 1, (int)' ')) != NULL); n++)
9999acbbeafSnn35248 		;
10009acbbeafSnn35248 
10019acbbeafSnn35248 	/* Allocate the argv array that we're going to return. */
10029acbbeafSnn35248 	if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
10039acbbeafSnn35248 		return (NULL);
10049acbbeafSnn35248 
10059acbbeafSnn35248 	/* Tokenize the exec string and return. */
10069acbbeafSnn35248 	a = 0;
10079acbbeafSnn35248 	new_argv[a++] = result_buf;
10089acbbeafSnn35248 	if (n > 2) {
10099acbbeafSnn35248 		(void) strtok_r(result_buf, " ", &lasts);
10109acbbeafSnn35248 		while ((new_argv[a++] = strtok_r(NULL, " ", &lasts)) != NULL)
10119acbbeafSnn35248 			;
10129acbbeafSnn35248 	} else {
10139acbbeafSnn35248 		new_argv[a++] = NULL;
10149acbbeafSnn35248 	}
10159acbbeafSnn35248 	assert(n == a);
10169acbbeafSnn35248 	return (new_argv);
10179acbbeafSnn35248 }
10189acbbeafSnn35248 
10197c478bd9Sstevel@tonic-gate /*
10207c478bd9Sstevel@tonic-gate  * Prepare argv array for exec'd process; if we're passing commands to the
10217c478bd9Sstevel@tonic-gate  * new process, then use su(1M) to do the invocation.  Otherwise, use
10227c478bd9Sstevel@tonic-gate  * 'login -z <from_zonename> -f' (-z is an undocumented option which tells
10237c478bd9Sstevel@tonic-gate  * login that we're coming from another zone, and to disregard its CONSOLE
10247c478bd9Sstevel@tonic-gate  * checks).
10257c478bd9Sstevel@tonic-gate  */
10267c478bd9Sstevel@tonic-gate static char **
1027123807fbSedp prep_args(brand_handle_t bh, const char *login, char **argv)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	int argc = 0, a = 0, i, n = -1;
10307c478bd9Sstevel@tonic-gate 	char **new_argv;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	if (argv != NULL) {
10337c478bd9Sstevel@tonic-gate 		size_t subshell_len = 1;
10347c478bd9Sstevel@tonic-gate 		char *subshell;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 		while (argv[argc] != NULL)
10377c478bd9Sstevel@tonic-gate 			argc++;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
10407c478bd9Sstevel@tonic-gate 			subshell_len += strlen(argv[i]) + 1;
10417c478bd9Sstevel@tonic-gate 		}
10427c478bd9Sstevel@tonic-gate 		if ((subshell = calloc(1, subshell_len)) == NULL)
10437c478bd9Sstevel@tonic-gate 			return (NULL);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
10467c478bd9Sstevel@tonic-gate 			(void) strcat(subshell, argv[i]);
10477c478bd9Sstevel@tonic-gate 			(void) strcat(subshell, " ");
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 		if (failsafe) {
10517c478bd9Sstevel@tonic-gate 			n = 4;
10527c478bd9Sstevel@tonic-gate 			if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
10537c478bd9Sstevel@tonic-gate 				return (NULL);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 			new_argv[a++] = FAILSAFESHELL;
10567c478bd9Sstevel@tonic-gate 		} else {
10577c478bd9Sstevel@tonic-gate 			n = 5;
10587c478bd9Sstevel@tonic-gate 			if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
10597c478bd9Sstevel@tonic-gate 				return (NULL);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 			new_argv[a++] = SUPATH;
10629acbbeafSnn35248 			new_argv[a++] = (char *)login;
10637c478bd9Sstevel@tonic-gate 		}
10647c478bd9Sstevel@tonic-gate 		new_argv[a++] = "-c";
10657c478bd9Sstevel@tonic-gate 		new_argv[a++] = subshell;
10667c478bd9Sstevel@tonic-gate 		new_argv[a++] = NULL;
10677c478bd9Sstevel@tonic-gate 		assert(a == n);
10687c478bd9Sstevel@tonic-gate 	} else {
10697c478bd9Sstevel@tonic-gate 		if (failsafe) {
10707c478bd9Sstevel@tonic-gate 			n = 2;
10717c478bd9Sstevel@tonic-gate 			if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
10727c478bd9Sstevel@tonic-gate 				return (NULL);
10737c478bd9Sstevel@tonic-gate 			new_argv[a++] = FAILSAFESHELL;
10747c478bd9Sstevel@tonic-gate 			new_argv[a++] = NULL;
10757c478bd9Sstevel@tonic-gate 			assert(n == a);
10769acbbeafSnn35248 		} else {
1077123807fbSedp 			new_argv = zone_login_cmd(bh, login);
10789acbbeafSnn35248 		}
10799acbbeafSnn35248 	}
10809acbbeafSnn35248 
10817c478bd9Sstevel@tonic-gate 	return (new_argv);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Helper routine for prep_env below.
10867c478bd9Sstevel@tonic-gate  */
10877c478bd9Sstevel@tonic-gate static char *
10887c478bd9Sstevel@tonic-gate add_env(char *name, char *value)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	size_t sz = strlen(name) + strlen(value) + 2; /* name, =, value, NUL */
10917c478bd9Sstevel@tonic-gate 	char *str;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	if ((str = malloc(sz)) == NULL)
10947c478bd9Sstevel@tonic-gate 		return (NULL);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	(void) snprintf(str, sz, "%s=%s", name, value);
10977c478bd9Sstevel@tonic-gate 	return (str);
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate /*
11017c478bd9Sstevel@tonic-gate  * Prepare envp array for exec'd process.
11027c478bd9Sstevel@tonic-gate  */
11037c478bd9Sstevel@tonic-gate static char **
11047c478bd9Sstevel@tonic-gate prep_env()
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	int e = 0, size = 1;
11077c478bd9Sstevel@tonic-gate 	char **new_env, *estr;
11087c478bd9Sstevel@tonic-gate 	char *term = getenv("TERM");
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	size++;	/* for $PATH */
11117c478bd9Sstevel@tonic-gate 	if (term != NULL)
11127c478bd9Sstevel@tonic-gate 		size++;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	/*
11157c478bd9Sstevel@tonic-gate 	 * In failsafe mode we set $HOME, since '-l' isn't valid in this mode.
11167c478bd9Sstevel@tonic-gate 	 * We also set $SHELL, since neither login nor su will be around to do
11177c478bd9Sstevel@tonic-gate 	 * it.
11187c478bd9Sstevel@tonic-gate 	 */
11197c478bd9Sstevel@tonic-gate 	if (failsafe)
11207c478bd9Sstevel@tonic-gate 		size += 2;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if ((new_env = malloc(sizeof (char *) * size)) == NULL)
11237c478bd9Sstevel@tonic-gate 		return (NULL);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	if ((estr = add_env("PATH", DEF_PATH)) == NULL)
11267c478bd9Sstevel@tonic-gate 		return (NULL);
11277c478bd9Sstevel@tonic-gate 	new_env[e++] = estr;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (term != NULL) {
11307c478bd9Sstevel@tonic-gate 		if ((estr = add_env("TERM", term)) == NULL)
11317c478bd9Sstevel@tonic-gate 			return (NULL);
11327c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	if (failsafe) {
11367c478bd9Sstevel@tonic-gate 		if ((estr = add_env("HOME", "/")) == NULL)
11377c478bd9Sstevel@tonic-gate 			return (NULL);
11387c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		if ((estr = add_env("SHELL", FAILSAFESHELL)) == NULL)
11417c478bd9Sstevel@tonic-gate 			return (NULL);
11427c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	new_env[e++] = NULL;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	assert(e == size);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	return (new_env);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate /*
11537c478bd9Sstevel@tonic-gate  * Finish the preparation of the envp array for exec'd non-interactive
11547c478bd9Sstevel@tonic-gate  * zlogins.  This is called in the child process *after* we zone_enter(), since
11557c478bd9Sstevel@tonic-gate  * it derives things we can only know within the zone, such as $HOME, $SHELL,
11567c478bd9Sstevel@tonic-gate  * etc.  We need only do this in the non-interactive, mode, since otherwise
11577c478bd9Sstevel@tonic-gate  * login(1) will do it.  We don't do this in failsafe mode, since it presents
11587c478bd9Sstevel@tonic-gate  * additional ways in which the command could fail, and we'd prefer to avoid
11597c478bd9Sstevel@tonic-gate  * that.
11607c478bd9Sstevel@tonic-gate  */
11617c478bd9Sstevel@tonic-gate static char **
1162858a4b99Ssl108498 prep_env_noninteractive(const char *user_cmd, char **env)
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate 	size_t size;
11657c478bd9Sstevel@tonic-gate 	char **new_env;
11667c478bd9Sstevel@tonic-gate 	int e, i;
11677c478bd9Sstevel@tonic-gate 	char *estr;
11687c478bd9Sstevel@tonic-gate 	char varmail[LOGNAME_MAX + 11]; /* strlen(/var/mail/) = 10, NUL */
1169858a4b99Ssl108498 	char pwbuf[NSS_BUFLEN_PASSWD + 1];
1170858a4b99Ssl108498 	struct passwd pwent;
1171858a4b99Ssl108498 	struct passwd *pw = NULL;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	assert(env != NULL);
11747c478bd9Sstevel@tonic-gate 	assert(failsafe == 0);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	/*
1177858a4b99Ssl108498 	 * Exec the "user_cmd" brand hook to get a pwent for the
1178858a4b99Ssl108498 	 * login user.  If this fails, HOME will be set to "/", SHELL
1179858a4b99Ssl108498 	 * will be set to $DEFAULTSHELL, and we will continue to exec
1180858a4b99Ssl108498 	 * SUPATH <login> -c <cmd>.
1181858a4b99Ssl108498 	 */
1182858a4b99Ssl108498 	pw = zone_get_user_pw(user_cmd, &pwent, pwbuf, sizeof (pwbuf));
1183858a4b99Ssl108498 
1184858a4b99Ssl108498 	/*
11857c478bd9Sstevel@tonic-gate 	 * Get existing envp size.
11867c478bd9Sstevel@tonic-gate 	 */
11877c478bd9Sstevel@tonic-gate 	for (size = 0; env[size] != NULL; size++)
11887c478bd9Sstevel@tonic-gate 		;
1189858a4b99Ssl108498 
11907c478bd9Sstevel@tonic-gate 	e = size;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/*
11937c478bd9Sstevel@tonic-gate 	 * Finish filling out the environment; we duplicate the environment
11947c478bd9Sstevel@tonic-gate 	 * setup described in login(1), for lack of a better precedent.
11957c478bd9Sstevel@tonic-gate 	 */
1196858a4b99Ssl108498 	if (pw != NULL)
11977c478bd9Sstevel@tonic-gate 		size += 3;	/* LOGNAME, HOME, MAIL */
1198858a4b99Ssl108498 	else
1199858a4b99Ssl108498 		size += 1;	/* HOME */
1200858a4b99Ssl108498 
12017c478bd9Sstevel@tonic-gate 	size++;	/* always fill in SHELL */
12027c478bd9Sstevel@tonic-gate 	size++; /* terminating NULL */
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if ((new_env = malloc(sizeof (char *) * size)) == NULL)
12057c478bd9Sstevel@tonic-gate 		goto malloc_fail;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/*
12087c478bd9Sstevel@tonic-gate 	 * Copy existing elements of env into new_env.
12097c478bd9Sstevel@tonic-gate 	 */
12107c478bd9Sstevel@tonic-gate 	for (i = 0; env[i] != NULL; i++) {
12117c478bd9Sstevel@tonic-gate 		if ((new_env[i] = strdup(env[i])) == NULL)
12127c478bd9Sstevel@tonic-gate 			goto malloc_fail;
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 	assert(e == i);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	if (pw != NULL) {
12177c478bd9Sstevel@tonic-gate 		if ((estr = add_env("LOGNAME", pw->pw_name)) == NULL)
12187c478bd9Sstevel@tonic-gate 			goto malloc_fail;
12197c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 		if ((estr = add_env("HOME", pw->pw_dir)) == NULL)
12227c478bd9Sstevel@tonic-gate 			goto malloc_fail;
12237c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 		if (chdir(pw->pw_dir) != 0)
12267c478bd9Sstevel@tonic-gate 			zerror(gettext("Could not chdir to home directory "
12277c478bd9Sstevel@tonic-gate 			    "%s: %s"), pw->pw_dir, strerror(errno));
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 		(void) snprintf(varmail, sizeof (varmail), "/var/mail/%s",
12307c478bd9Sstevel@tonic-gate 		    pw->pw_name);
12317c478bd9Sstevel@tonic-gate 		if ((estr = add_env("MAIL", varmail)) == NULL)
12327c478bd9Sstevel@tonic-gate 			goto malloc_fail;
12337c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
1234858a4b99Ssl108498 	} else {
1235858a4b99Ssl108498 		if ((estr = add_env("HOME", "/")) == NULL)
1236858a4b99Ssl108498 			goto malloc_fail;
1237858a4b99Ssl108498 		new_env[e++] = estr;
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	if (pw != NULL && strlen(pw->pw_shell) > 0) {
12417c478bd9Sstevel@tonic-gate 		if ((estr = add_env("SHELL", pw->pw_shell)) == NULL)
12427c478bd9Sstevel@tonic-gate 			goto malloc_fail;
12437c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
12447c478bd9Sstevel@tonic-gate 	} else {
12457c478bd9Sstevel@tonic-gate 		if ((estr = add_env("SHELL", DEFAULTSHELL)) == NULL)
12467c478bd9Sstevel@tonic-gate 			goto malloc_fail;
12477c478bd9Sstevel@tonic-gate 		new_env[e++] = estr;
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	new_env[e++] = NULL;	/* add terminating NULL */
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	assert(e == size);
12537c478bd9Sstevel@tonic-gate 	return (new_env);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate malloc_fail:
12567c478bd9Sstevel@tonic-gate 	zperror(gettext("failed to allocate memory for process environment"));
12577c478bd9Sstevel@tonic-gate 	return (NULL);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate static int
12617c478bd9Sstevel@tonic-gate close_func(void *slavefd, int fd)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	if (fd != *(int *)slavefd)
12647c478bd9Sstevel@tonic-gate 		(void) close(fd);
12657c478bd9Sstevel@tonic-gate 	return (0);
12667c478bd9Sstevel@tonic-gate }
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate static void
12697c478bd9Sstevel@tonic-gate set_cmdchar(char *cmdcharstr)
12707c478bd9Sstevel@tonic-gate {
12717c478bd9Sstevel@tonic-gate 	char c;
12727c478bd9Sstevel@tonic-gate 	long lc;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	if ((c = *cmdcharstr) != '\\') {
12757c478bd9Sstevel@tonic-gate 		cmdchar = c;
12767c478bd9Sstevel@tonic-gate 		return;
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	c = cmdcharstr[1];
12807c478bd9Sstevel@tonic-gate 	if (c == '\0' || c == '\\') {
12817c478bd9Sstevel@tonic-gate 		cmdchar = '\\';
12827c478bd9Sstevel@tonic-gate 		return;
12837c478bd9Sstevel@tonic-gate 	}
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	if (c < '0' || c > '7') {
12867c478bd9Sstevel@tonic-gate 		zerror(gettext("Unrecognized escape character option %s"),
12877c478bd9Sstevel@tonic-gate 		    cmdcharstr);
12887c478bd9Sstevel@tonic-gate 		usage();
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	lc = strtol(cmdcharstr + 1, NULL, 8);
12927c478bd9Sstevel@tonic-gate 	if (lc < 0 || lc > 255) {
12937c478bd9Sstevel@tonic-gate 		zerror(gettext("Octal escape character '%s' too large"),
12947c478bd9Sstevel@tonic-gate 		    cmdcharstr);
12957c478bd9Sstevel@tonic-gate 		usage();
12967c478bd9Sstevel@tonic-gate 	}
12977c478bd9Sstevel@tonic-gate 	cmdchar = (char)lc;
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate static int
13017c478bd9Sstevel@tonic-gate setup_utmpx(char *slavename)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	struct utmpx ut;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	bzero(&ut, sizeof (ut));
13067c478bd9Sstevel@tonic-gate 	(void) strncpy(ut.ut_user, ".zlogin", sizeof (ut.ut_user));
13077c478bd9Sstevel@tonic-gate 	(void) strncpy(ut.ut_line, slavename, sizeof (ut.ut_line));
13087c478bd9Sstevel@tonic-gate 	ut.ut_pid = getpid();
13097c478bd9Sstevel@tonic-gate 	ut.ut_id[0] = 'z';
13107c478bd9Sstevel@tonic-gate 	ut.ut_id[1] = ut.ut_id[2] = ut.ut_id[3] = (char)SC_WILDC;
13117c478bd9Sstevel@tonic-gate 	ut.ut_type = LOGIN_PROCESS;
13127c478bd9Sstevel@tonic-gate 	(void) time(&ut.ut_tv.tv_sec);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	if (makeutx(&ut) == NULL) {
13157c478bd9Sstevel@tonic-gate 		zerror(gettext("makeutx failed"));
13167c478bd9Sstevel@tonic-gate 		return (-1);
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 	return (0);
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate static void
13227c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
13237c478bd9Sstevel@tonic-gate {
13247c478bd9Sstevel@tonic-gate 	(void) close(lockfd);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate static int
13287c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
13297c478bd9Sstevel@tonic-gate {
13307c478bd9Sstevel@tonic-gate 	char pathbuf[PATH_MAX];
13317c478bd9Sstevel@tonic-gate 	struct flock flock;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
13347c478bd9Sstevel@tonic-gate 		zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR,
13357c478bd9Sstevel@tonic-gate 		    strerror(errno));
13367c478bd9Sstevel@tonic-gate 		return (-1);
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
13397c478bd9Sstevel@tonic-gate 	(void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock",
13407c478bd9Sstevel@tonic-gate 	    ZONES_TMPDIR, zone_name);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
13437c478bd9Sstevel@tonic-gate 		zerror(gettext("could not open %s: %s"), pathbuf,
13447c478bd9Sstevel@tonic-gate 		    strerror(errno));
13457c478bd9Sstevel@tonic-gate 		return (-1);
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * Lock the file to synchronize with other zoneadmds
13497c478bd9Sstevel@tonic-gate 	 */
13507c478bd9Sstevel@tonic-gate 	flock.l_type = F_WRLCK;
13517c478bd9Sstevel@tonic-gate 	flock.l_whence = SEEK_SET;
13527c478bd9Sstevel@tonic-gate 	flock.l_start = (off_t)0;
13537c478bd9Sstevel@tonic-gate 	flock.l_len = (off_t)0;
13547c478bd9Sstevel@tonic-gate 	if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
13557c478bd9Sstevel@tonic-gate 		zerror(gettext("unable to lock %s: %s"), pathbuf,
13567c478bd9Sstevel@tonic-gate 		    strerror(errno));
13577c478bd9Sstevel@tonic-gate 		release_lock_file(*lockfd);
13587c478bd9Sstevel@tonic-gate 		return (-1);
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 	return (Z_OK);
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate static int
13647c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
13657c478bd9Sstevel@tonic-gate {
13667c478bd9Sstevel@tonic-gate 	pid_t retval;
13677c478bd9Sstevel@tonic-gate 	int pstatus = 0, error = -1, lockfd, doorfd;
13687c478bd9Sstevel@tonic-gate 	struct door_info info;
13697c478bd9Sstevel@tonic-gate 	char doorpath[MAXPATHLEN];
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	(void) snprintf(doorpath, sizeof (doorpath), ZONE_DOOR_PATH, zone_name);
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (grab_lock_file(zone_name, &lockfd) != Z_OK)
13747c478bd9Sstevel@tonic-gate 		return (-1);
13757c478bd9Sstevel@tonic-gate 	/*
13767c478bd9Sstevel@tonic-gate 	 * We must do the door check with the lock held.  Otherwise, we
13777c478bd9Sstevel@tonic-gate 	 * might race against another zoneadm/zlogin process and wind
13787c478bd9Sstevel@tonic-gate 	 * up with two processes trying to start zoneadmd at the same
13797c478bd9Sstevel@tonic-gate 	 * time.  zoneadmd will detect this, and fail, but we prefer this
13807c478bd9Sstevel@tonic-gate 	 * to be as seamless as is practical, from a user perspective.
13817c478bd9Sstevel@tonic-gate 	 */
13827c478bd9Sstevel@tonic-gate 	if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
13837c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
13847c478bd9Sstevel@tonic-gate 			zerror("failed to open %s: %s", doorpath,
13857c478bd9Sstevel@tonic-gate 			    strerror(errno));
13867c478bd9Sstevel@tonic-gate 			goto out;
13877c478bd9Sstevel@tonic-gate 		}
13887c478bd9Sstevel@tonic-gate 	} else {
13897c478bd9Sstevel@tonic-gate 		/*
13907c478bd9Sstevel@tonic-gate 		 * Seems to be working ok.
13917c478bd9Sstevel@tonic-gate 		 */
13927c478bd9Sstevel@tonic-gate 		if (door_info(doorfd, &info) == 0 &&
13937c478bd9Sstevel@tonic-gate 		    ((info.di_attributes & DOOR_REVOKED) == 0)) {
13947c478bd9Sstevel@tonic-gate 			error = 0;
13957c478bd9Sstevel@tonic-gate 			goto out;
13967c478bd9Sstevel@tonic-gate 		}
13977c478bd9Sstevel@tonic-gate 	}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
14007c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"));
14017c478bd9Sstevel@tonic-gate 		goto out;
14027c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) {
14037c478bd9Sstevel@tonic-gate 		/* child process */
14047c478bd9Sstevel@tonic-gate 		(void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z",
14057c478bd9Sstevel@tonic-gate 		    zone_name, NULL);
14067c478bd9Sstevel@tonic-gate 		zperror(gettext("could not exec zoneadmd"));
14077c478bd9Sstevel@tonic-gate 		_exit(1);
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/* parent process */
14117c478bd9Sstevel@tonic-gate 	do {
14127c478bd9Sstevel@tonic-gate 		retval = waitpid(child_pid, &pstatus, 0);
14137c478bd9Sstevel@tonic-gate 	} while (retval != child_pid);
14147c478bd9Sstevel@tonic-gate 	if (WIFSIGNALED(pstatus) ||
14157c478bd9Sstevel@tonic-gate 	    (WIFEXITED(pstatus) && WEXITSTATUS(pstatus) != 0)) {
14167c478bd9Sstevel@tonic-gate 		zerror(gettext("could not start %s"), "zoneadmd");
14177c478bd9Sstevel@tonic-gate 		goto out;
14187c478bd9Sstevel@tonic-gate 	}
14197c478bd9Sstevel@tonic-gate 	error = 0;
14207c478bd9Sstevel@tonic-gate out:
14217c478bd9Sstevel@tonic-gate 	release_lock_file(lockfd);
14227c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
14237c478bd9Sstevel@tonic-gate 	return (error);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate static int
14277c478bd9Sstevel@tonic-gate init_template(void)
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	int fd;
14307c478bd9Sstevel@tonic-gate 	int err = 0;
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
14337c478bd9Sstevel@tonic-gate 	if (fd == -1)
14347c478bd9Sstevel@tonic-gate 		return (-1);
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	/*
14377c478bd9Sstevel@tonic-gate 	 * zlogin doesn't do anything with the contract.
14387c478bd9Sstevel@tonic-gate 	 * Deliver no events, don't inherit, and allow it to be orphaned.
14397c478bd9Sstevel@tonic-gate 	 */
14407c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_critical(fd, 0);
14417c478bd9Sstevel@tonic-gate 	err |= ct_tmpl_set_informative(fd, 0);
14427c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
14437c478bd9Sstevel@tonic-gate 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
14447c478bd9Sstevel@tonic-gate 	if (err || ct_tmpl_activate(fd)) {
14457c478bd9Sstevel@tonic-gate 		(void) close(fd);
14467c478bd9Sstevel@tonic-gate 		return (-1);
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	return (fd);
14507c478bd9Sstevel@tonic-gate }
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate static int
1453858a4b99Ssl108498 noninteractive_login(char *zonename, const char *user_cmd, zoneid_t zoneid,
14547c478bd9Sstevel@tonic-gate     char **new_args, char **new_env)
14557c478bd9Sstevel@tonic-gate {
14567c478bd9Sstevel@tonic-gate 	pid_t retval;
14577c478bd9Sstevel@tonic-gate 	int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2];
14587c478bd9Sstevel@tonic-gate 	int child_status;
14597c478bd9Sstevel@tonic-gate 	int tmpl_fd;
14607c478bd9Sstevel@tonic-gate 	sigset_t block_cld;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
14637c478bd9Sstevel@tonic-gate 		reset_tty();
14647c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create contract"));
14657c478bd9Sstevel@tonic-gate 		return (1);
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	if (pipe(stdin_pipe) != 0) {
14697c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create STDIN pipe"));
14707c478bd9Sstevel@tonic-gate 		return (1);
14717c478bd9Sstevel@tonic-gate 	}
14727c478bd9Sstevel@tonic-gate 	/*
14737c478bd9Sstevel@tonic-gate 	 * When the user types ^D, we get a zero length message on STDIN.
14747c478bd9Sstevel@tonic-gate 	 * We need to echo that down the pipe to send it to the other side;
14757c478bd9Sstevel@tonic-gate 	 * but by default, pipes don't propagate zero-length messages.  We
14767c478bd9Sstevel@tonic-gate 	 * toggle that behavior off using I_SWROPT.  See streamio(7i).
14777c478bd9Sstevel@tonic-gate 	 */
14787c478bd9Sstevel@tonic-gate 	if (ioctl(stdin_pipe[0], I_SWROPT, SNDZERO) != 0) {
14797c478bd9Sstevel@tonic-gate 		zperror(gettext("could not configure STDIN pipe"));
14807c478bd9Sstevel@tonic-gate 		return (1);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 	if (pipe(stdout_pipe) != 0) {
14847c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create STDOUT pipe"));
14857c478bd9Sstevel@tonic-gate 		return (1);
14867c478bd9Sstevel@tonic-gate 	}
14877c478bd9Sstevel@tonic-gate 	if (pipe(stderr_pipe) != 0) {
14887c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create STDERR pipe"));
14897c478bd9Sstevel@tonic-gate 		return (1);
14907c478bd9Sstevel@tonic-gate 	}
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	/*
14937c478bd9Sstevel@tonic-gate 	 * If any of the pipe FD's winds up being less than STDERR, then we
14947c478bd9Sstevel@tonic-gate 	 * have a mess on our hands-- and we are lacking some of the I/O
14957c478bd9Sstevel@tonic-gate 	 * streams we would expect anyway.  So we bail.
14967c478bd9Sstevel@tonic-gate 	 */
14977c478bd9Sstevel@tonic-gate 	if (stdin_pipe[0] <= STDERR_FILENO ||
14987c478bd9Sstevel@tonic-gate 	    stdin_pipe[1] <= STDERR_FILENO ||
14997c478bd9Sstevel@tonic-gate 	    stdout_pipe[0] <= STDERR_FILENO ||
15007c478bd9Sstevel@tonic-gate 	    stdout_pipe[1] <= STDERR_FILENO ||
15017c478bd9Sstevel@tonic-gate 	    stderr_pipe[0] <= STDERR_FILENO ||
15027c478bd9Sstevel@tonic-gate 	    stderr_pipe[1] <= STDERR_FILENO) {
15037c478bd9Sstevel@tonic-gate 		zperror(gettext("process lacks valid STDIN, STDOUT, STDERR"));
15047c478bd9Sstevel@tonic-gate 		return (1);
15057c478bd9Sstevel@tonic-gate 	}
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 	if (prefork_dropprivs() != 0) {
15087c478bd9Sstevel@tonic-gate 		zperror(gettext("could not allocate privilege set"));
15097c478bd9Sstevel@tonic-gate 		return (1);
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCLD, sigcld);
15137c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
15147c478bd9Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCLD);
15157c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
15187c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
15197c478bd9Sstevel@tonic-gate 		(void) close(tmpl_fd);
15207c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"));
15217c478bd9Sstevel@tonic-gate 		return (1);
15227c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) { /* child process */
15237c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		/*
15267c478bd9Sstevel@tonic-gate 		 * Do a dance to get the pipes hooked up as FD's 0, 1 and 2.
15277c478bd9Sstevel@tonic-gate 		 */
15287c478bd9Sstevel@tonic-gate 		(void) close(STDIN_FILENO);
15297c478bd9Sstevel@tonic-gate 		(void) close(STDOUT_FILENO);
15307c478bd9Sstevel@tonic-gate 		(void) close(STDERR_FILENO);
15317c478bd9Sstevel@tonic-gate 		(void) dup2(stdin_pipe[1], STDIN_FILENO);
15327c478bd9Sstevel@tonic-gate 		(void) dup2(stdout_pipe[1], STDOUT_FILENO);
15337c478bd9Sstevel@tonic-gate 		(void) dup2(stderr_pipe[1], STDERR_FILENO);
15347c478bd9Sstevel@tonic-gate 		(void) closefrom(STDERR_FILENO + 1);
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 		(void) sigset(SIGCLD, SIG_DFL);
15377c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
15387c478bd9Sstevel@tonic-gate 		/*
15397c478bd9Sstevel@tonic-gate 		 * In case any of stdin, stdout or stderr are streams,
15407c478bd9Sstevel@tonic-gate 		 * anchor them to prevent malicious I_POPs.
15417c478bd9Sstevel@tonic-gate 		 */
15427c478bd9Sstevel@tonic-gate 		(void) ioctl(STDIN_FILENO, I_ANCHOR);
15437c478bd9Sstevel@tonic-gate 		(void) ioctl(STDOUT_FILENO, I_ANCHOR);
15447c478bd9Sstevel@tonic-gate 		(void) ioctl(STDERR_FILENO, I_ANCHOR);
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
15477c478bd9Sstevel@tonic-gate 			zerror(gettext("could not enter zone %s: %s"),
15487c478bd9Sstevel@tonic-gate 			    zonename, strerror(errno));
15497c478bd9Sstevel@tonic-gate 			_exit(1);
15507c478bd9Sstevel@tonic-gate 		}
15517c478bd9Sstevel@tonic-gate 
1552858a4b99Ssl108498 		/*
1553858a4b99Ssl108498 		 * For non-native zones, tell libc where it can find locale
1554858a4b99Ssl108498 		 * specific getttext() messages.
1555858a4b99Ssl108498 		 */
1556*1100f00dSgjelinek 		if (access("/.SUNWnative/usr/lib/locale", R_OK) == 0)
1557*1100f00dSgjelinek 			(void) bindtextdomain(TEXT_DOMAIN,
1558*1100f00dSgjelinek 			    "/.SUNWnative/usr/lib/locale");
1559*1100f00dSgjelinek 		else if (access("/native/usr/lib/locale", R_OK) == 0)
1560858a4b99Ssl108498 			(void) bindtextdomain(TEXT_DOMAIN,
1561858a4b99Ssl108498 			    "/native/usr/lib/locale");
1562858a4b99Ssl108498 
15637c478bd9Sstevel@tonic-gate 		if (!failsafe)
1564858a4b99Ssl108498 			new_env = prep_env_noninteractive(user_cmd, new_env);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		if (new_env == NULL) {
15677c478bd9Sstevel@tonic-gate 			_exit(1);
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 		/*
15717c478bd9Sstevel@tonic-gate 		 * Move into a new process group; the zone_enter will have
15727c478bd9Sstevel@tonic-gate 		 * placed us into zsched's session, and we want to be in
15737c478bd9Sstevel@tonic-gate 		 * a unique process group.
15747c478bd9Sstevel@tonic-gate 		 */
15757c478bd9Sstevel@tonic-gate 		(void) setpgid(getpid(), getpid());
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		(void) execve(new_args[0], new_args, new_env);
15787c478bd9Sstevel@tonic-gate 		zperror(gettext("exec failure"));
15797c478bd9Sstevel@tonic-gate 		_exit(1);
15807c478bd9Sstevel@tonic-gate 	}
15817c478bd9Sstevel@tonic-gate 	/* parent */
15827c478bd9Sstevel@tonic-gate 	(void) sigset(SIGINT, sig_forward);
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	postfork_dropprivs();
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
15877c478bd9Sstevel@tonic-gate 	(void) close(tmpl_fd);
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
1590b52a7fd7Sgjelinek 	doio(stdin_pipe[0], stdin_pipe[1], stdout_pipe[0], stderr_pipe[0],
1591b52a7fd7Sgjelinek 	    B_TRUE);
15927c478bd9Sstevel@tonic-gate 	do {
15937c478bd9Sstevel@tonic-gate 		retval = waitpid(child_pid, &child_status, 0);
15947c478bd9Sstevel@tonic-gate 		if (retval == -1) {
15957c478bd9Sstevel@tonic-gate 			child_status = 0;
15967c478bd9Sstevel@tonic-gate 		}
15977c478bd9Sstevel@tonic-gate 	} while (retval != child_pid && errno != ECHILD);
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	return (WEXITSTATUS(child_status));
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate int
16037c478bd9Sstevel@tonic-gate main(int argc, char **argv)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	int arg, console = 0;
16067c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
16077c478bd9Sstevel@tonic-gate 	zone_state_t st;
16087c478bd9Sstevel@tonic-gate 	char *login = "root";
16097c478bd9Sstevel@tonic-gate 	int lflag = 0;
16107c478bd9Sstevel@tonic-gate 	char *zonename = NULL;
16117c478bd9Sstevel@tonic-gate 	char **proc_args = NULL;
16127c478bd9Sstevel@tonic-gate 	char **new_args, **new_env;
16137c478bd9Sstevel@tonic-gate 	sigset_t block_cld;
1614facf4a8dSllai1 	char devroot[MAXPATHLEN];
16157c478bd9Sstevel@tonic-gate 	char *slavename, slaveshortname[MAXPATHLEN];
16167c478bd9Sstevel@tonic-gate 	priv_set_t *privset;
16177c478bd9Sstevel@tonic-gate 	int tmpl_fd;
16189acbbeafSnn35248 	char zonebrand[MAXNAMELEN];
1619108322fbScarlsonj 	struct stat sb;
1620108322fbScarlsonj 	char kernzone[ZONENAME_MAX];
1621123807fbSedp 	brand_handle_t bh;
1622858a4b99Ssl108498 	char user_cmd[MAXPATHLEN];
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
16257c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	(void) getpname(argv[0]);
16287c478bd9Sstevel@tonic-gate 
1629108322fbScarlsonj 	while ((arg = getopt(argc, argv, "ECR:Se:l:")) != EOF) {
16307c478bd9Sstevel@tonic-gate 		switch (arg) {
16317c478bd9Sstevel@tonic-gate 		case 'C':
16327c478bd9Sstevel@tonic-gate 			console = 1;
16337c478bd9Sstevel@tonic-gate 			break;
16347c478bd9Sstevel@tonic-gate 		case 'E':
16357c478bd9Sstevel@tonic-gate 			nocmdchar = 1;
16367c478bd9Sstevel@tonic-gate 			break;
1637108322fbScarlsonj 		case 'R':	/* undocumented */
1638108322fbScarlsonj 			if (*optarg != '/') {
1639108322fbScarlsonj 				zerror(gettext("root path must be absolute."));
1640108322fbScarlsonj 				exit(2);
1641108322fbScarlsonj 			}
1642108322fbScarlsonj 			if (stat(optarg, &sb) == -1 || !S_ISDIR(sb.st_mode)) {
1643108322fbScarlsonj 				zerror(
1644108322fbScarlsonj 				    gettext("root path must be a directory."));
1645108322fbScarlsonj 				exit(2);
1646108322fbScarlsonj 			}
1647108322fbScarlsonj 			zonecfg_set_root(optarg);
1648108322fbScarlsonj 			break;
16497c478bd9Sstevel@tonic-gate 		case 'S':
16507c478bd9Sstevel@tonic-gate 			failsafe = 1;
16517c478bd9Sstevel@tonic-gate 			break;
16527c478bd9Sstevel@tonic-gate 		case 'e':
16537c478bd9Sstevel@tonic-gate 			set_cmdchar(optarg);
16547c478bd9Sstevel@tonic-gate 			break;
16557c478bd9Sstevel@tonic-gate 		case 'l':
16567c478bd9Sstevel@tonic-gate 			login = optarg;
16577c478bd9Sstevel@tonic-gate 			lflag = 1;
16587c478bd9Sstevel@tonic-gate 			break;
16597c478bd9Sstevel@tonic-gate 		default:
16607c478bd9Sstevel@tonic-gate 			usage();
16617c478bd9Sstevel@tonic-gate 		}
16627c478bd9Sstevel@tonic-gate 	}
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	if (console != 0 && lflag != 0) {
16657c478bd9Sstevel@tonic-gate 		zerror(gettext("-l may not be specified for console login"));
16667c478bd9Sstevel@tonic-gate 		usage();
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if (console != 0 && failsafe != 0) {
16707c478bd9Sstevel@tonic-gate 		zerror(gettext("-S may not be specified for console login"));
16717c478bd9Sstevel@tonic-gate 		usage();
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 
1674108322fbScarlsonj 	if (console != 0 && zonecfg_in_alt_root()) {
1675108322fbScarlsonj 		zerror(gettext("-R may not be specified for console login"));
1676108322fbScarlsonj 		exit(2);
1677108322fbScarlsonj 	}
1678108322fbScarlsonj 
16797c478bd9Sstevel@tonic-gate 	if (failsafe != 0 && lflag != 0) {
16807c478bd9Sstevel@tonic-gate 		zerror(gettext("-l may not be specified for failsafe login"));
16817c478bd9Sstevel@tonic-gate 		usage();
16827c478bd9Sstevel@tonic-gate 	}
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	if (optind == (argc - 1)) {
16857c478bd9Sstevel@tonic-gate 		/*
16867c478bd9Sstevel@tonic-gate 		 * zone name, no process name; this should be an interactive
16877c478bd9Sstevel@tonic-gate 		 * as long as STDIN is really a tty.
16887c478bd9Sstevel@tonic-gate 		 */
16897c478bd9Sstevel@tonic-gate 		if (isatty(STDIN_FILENO))
16907c478bd9Sstevel@tonic-gate 			interactive = 1;
16917c478bd9Sstevel@tonic-gate 		zonename = argv[optind];
16927c478bd9Sstevel@tonic-gate 	} else if (optind < (argc - 1)) {
16937c478bd9Sstevel@tonic-gate 		if (console) {
16947c478bd9Sstevel@tonic-gate 			zerror(gettext("Commands may not be specified for "
16957c478bd9Sstevel@tonic-gate 			    "console login."));
16967c478bd9Sstevel@tonic-gate 			usage();
16977c478bd9Sstevel@tonic-gate 		}
16987c478bd9Sstevel@tonic-gate 		/* zone name and process name, and possibly some args */
16997c478bd9Sstevel@tonic-gate 		zonename = argv[optind];
17007c478bd9Sstevel@tonic-gate 		proc_args = &argv[optind + 1];
17017c478bd9Sstevel@tonic-gate 		interactive = 0;
17027c478bd9Sstevel@tonic-gate 	} else {
17037c478bd9Sstevel@tonic-gate 		usage();
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	if (getzoneid() != GLOBAL_ZONEID) {
17077c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' may only be used from the global zone"),
17087c478bd9Sstevel@tonic-gate 		    pname);
17097c478bd9Sstevel@tonic-gate 		return (1);
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	if (strcmp(zonename, GLOBAL_ZONENAME) == 0) {
17137c478bd9Sstevel@tonic-gate 		zerror(gettext("'%s' not applicable to the global zone"),
17147c478bd9Sstevel@tonic-gate 		    pname);
17157c478bd9Sstevel@tonic-gate 		return (1);
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	if (zone_get_state(zonename, &st) != Z_OK) {
17197c478bd9Sstevel@tonic-gate 		zerror(gettext("zone '%s' unknown"), zonename);
17207c478bd9Sstevel@tonic-gate 		return (1);
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	if (st < ZONE_STATE_INSTALLED) {
17247c478bd9Sstevel@tonic-gate 		zerror(gettext("cannot login to a zone which is '%s'"),
17257c478bd9Sstevel@tonic-gate 		    zone_state_str(st));
17267c478bd9Sstevel@tonic-gate 		return (1);
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/*
17307c478bd9Sstevel@tonic-gate 	 * In both console and non-console cases, we require all privs.
17317c478bd9Sstevel@tonic-gate 	 * In the console case, because we may need to startup zoneadmd.
17327c478bd9Sstevel@tonic-gate 	 * In the non-console case in order to do zone_enter(2), zonept()
17337c478bd9Sstevel@tonic-gate 	 * and other tasks.
17347c478bd9Sstevel@tonic-gate 	 *
17357c478bd9Sstevel@tonic-gate 	 * Future work: this solution is temporary.  Ultimately, we need to
17367c478bd9Sstevel@tonic-gate 	 * move to a flexible system which allows the global admin to
17377c478bd9Sstevel@tonic-gate 	 * designate that a particular user can zlogin (and probably zlogin
17387c478bd9Sstevel@tonic-gate 	 * -C) to a particular zone.  This all-root business we have now is
17397c478bd9Sstevel@tonic-gate 	 * quite sketchy.
17407c478bd9Sstevel@tonic-gate 	 */
17417c478bd9Sstevel@tonic-gate 	if ((privset = priv_allocset()) == NULL) {
17427c478bd9Sstevel@tonic-gate 		zperror(gettext("priv_allocset failed"));
17437c478bd9Sstevel@tonic-gate 		return (1);
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
17477c478bd9Sstevel@tonic-gate 		zperror(gettext("getppriv failed"));
17487c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
17497c478bd9Sstevel@tonic-gate 		return (1);
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	if (priv_isfullset(privset) == B_FALSE) {
17537c478bd9Sstevel@tonic-gate 		zerror(gettext("You lack sufficient privilege to run "
17547c478bd9Sstevel@tonic-gate 		    "this command (all privs required)"));
17557c478bd9Sstevel@tonic-gate 		priv_freeset(privset);
17567c478bd9Sstevel@tonic-gate 		return (1);
17577c478bd9Sstevel@tonic-gate 	}
17587c478bd9Sstevel@tonic-gate 	priv_freeset(privset);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	/*
17617c478bd9Sstevel@tonic-gate 	 * The console is a separate case from the rest of the code; handle
17627c478bd9Sstevel@tonic-gate 	 * it first.
17637c478bd9Sstevel@tonic-gate 	 */
17647c478bd9Sstevel@tonic-gate 	if (console) {
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 		/*
17677c478bd9Sstevel@tonic-gate 		 * Ensure that zoneadmd for this zone is running.
17687c478bd9Sstevel@tonic-gate 		 */
17697c478bd9Sstevel@tonic-gate 		if (start_zoneadmd(zonename) == -1)
17707c478bd9Sstevel@tonic-gate 			return (1);
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		/*
17737c478bd9Sstevel@tonic-gate 		 * Make contact with zoneadmd.
17747c478bd9Sstevel@tonic-gate 		 */
17757c478bd9Sstevel@tonic-gate 		if (get_console_master(zonename) == -1)
17767c478bd9Sstevel@tonic-gate 			return (1);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 		(void) printf(gettext("[Connected to zone '%s' console]\n"),
17797c478bd9Sstevel@tonic-gate 		    zonename);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 		if (set_tty_rawmode(STDIN_FILENO) == -1) {
17827c478bd9Sstevel@tonic-gate 			reset_tty();
17837c478bd9Sstevel@tonic-gate 			zperror(gettext("failed to set stdin pty to raw mode"));
17847c478bd9Sstevel@tonic-gate 			return (1);
17857c478bd9Sstevel@tonic-gate 		}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 		(void) sigset(SIGWINCH, sigwinch);
17887c478bd9Sstevel@tonic-gate 		(void) sigwinch(0);
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 		/*
17917c478bd9Sstevel@tonic-gate 		 * Run the I/O loop until we get disconnected.
17927c478bd9Sstevel@tonic-gate 		 */
1793b52a7fd7Sgjelinek 		doio(masterfd, -1, masterfd, -1, B_FALSE);
17947c478bd9Sstevel@tonic-gate 		reset_tty();
17957c478bd9Sstevel@tonic-gate 		(void) printf(gettext("\n[Connection to zone '%s' console "
17967c478bd9Sstevel@tonic-gate 		    "closed]\n"), zonename);
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 		return (0);
17997c478bd9Sstevel@tonic-gate 	}
18007c478bd9Sstevel@tonic-gate 
1801108322fbScarlsonj 	if (st != ZONE_STATE_RUNNING && st != ZONE_STATE_MOUNTED) {
18027c478bd9Sstevel@tonic-gate 		zerror(gettext("login allowed only to running zones "
18037c478bd9Sstevel@tonic-gate 		    "(%s is '%s')."), zonename, zone_state_str(st));
18047c478bd9Sstevel@tonic-gate 		return (1);
18057c478bd9Sstevel@tonic-gate 	}
18067c478bd9Sstevel@tonic-gate 
1807108322fbScarlsonj 	(void) strlcpy(kernzone, zonename, sizeof (kernzone));
1808108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1809108322fbScarlsonj 		FILE *fp = zonecfg_open_scratch("", B_FALSE);
1810108322fbScarlsonj 
1811108322fbScarlsonj 		if (fp == NULL || zonecfg_find_scratch(fp, zonename,
1812108322fbScarlsonj 		    zonecfg_get_root(), kernzone, sizeof (kernzone)) == -1) {
1813108322fbScarlsonj 			zerror(gettext("cannot find scratch zone %s"),
1814108322fbScarlsonj 			    zonename);
1815108322fbScarlsonj 			if (fp != NULL)
1816108322fbScarlsonj 				zonecfg_close_scratch(fp);
1817108322fbScarlsonj 			return (1);
1818108322fbScarlsonj 		}
1819108322fbScarlsonj 		zonecfg_close_scratch(fp);
1820108322fbScarlsonj 	}
1821108322fbScarlsonj 
1822108322fbScarlsonj 	if ((zoneid = getzoneidbyname(kernzone)) == -1) {
18237c478bd9Sstevel@tonic-gate 		zerror(gettext("failed to get zoneid for zone '%s'"),
18247c478bd9Sstevel@tonic-gate 		    zonename);
18257c478bd9Sstevel@tonic-gate 		return (1);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
1828108322fbScarlsonj 	/*
1829facf4a8dSllai1 	 * We need the zone root path only if we are setting up a pty.
1830108322fbScarlsonj 	 */
1831facf4a8dSllai1 	if (zone_get_devroot(zonename, devroot, sizeof (devroot)) == -1) {
1832facf4a8dSllai1 		zerror(gettext("could not get dev path for zone %s"),
18337c478bd9Sstevel@tonic-gate 		    zonename);
18347c478bd9Sstevel@tonic-gate 		return (1);
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 
18379acbbeafSnn35248 	/* Get a handle to the brand info for this zone */
18389acbbeafSnn35248 	if ((zone_get_brand(zonename, zonebrand, sizeof (zonebrand)) != Z_OK) ||
1839123807fbSedp 	    ((bh = brand_open(zonebrand)) == NULL)) {
18409acbbeafSnn35248 		zerror(gettext("could not get brand for zone %s"), zonename);
18417c478bd9Sstevel@tonic-gate 		return (1);
18427c478bd9Sstevel@tonic-gate 	}
1843123807fbSedp 	if ((new_args = prep_args(bh, login, proc_args)) == NULL) {
18449acbbeafSnn35248 		zperror(gettext("could not assemble new arguments"));
1845123807fbSedp 		brand_close(bh);
18469acbbeafSnn35248 		return (1);
18479acbbeafSnn35248 	}
1848858a4b99Ssl108498 	/*
1849858a4b99Ssl108498 	 * Get the brand specific user_cmd.  This command is used to get
1850858a4b99Ssl108498 	 * a passwd(4) entry for login.
1851858a4b99Ssl108498 	 */
1852858a4b99Ssl108498 	if (!interactive && !failsafe) {
1853858a4b99Ssl108498 		if (zone_get_user_cmd(bh, login, user_cmd,
1854858a4b99Ssl108498 		    sizeof (user_cmd)) == NULL) {
1855858a4b99Ssl108498 			zerror(gettext("could not get user_cmd for zone %s"),
1856858a4b99Ssl108498 			    zonename);
1857858a4b99Ssl108498 			brand_close(bh);
1858858a4b99Ssl108498 			return (1);
1859858a4b99Ssl108498 		}
1860858a4b99Ssl108498 	}
1861123807fbSedp 	brand_close(bh);
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	if ((new_env = prep_env()) == NULL) {
18647c478bd9Sstevel@tonic-gate 		zperror(gettext("could not assemble new environment"));
18657c478bd9Sstevel@tonic-gate 		return (1);
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	if (!interactive)
1869858a4b99Ssl108498 		return (noninteractive_login(zonename, user_cmd, zoneid,
1870858a4b99Ssl108498 		    new_args, new_env));
18717c478bd9Sstevel@tonic-gate 
1872108322fbScarlsonj 	if (zonecfg_in_alt_root()) {
1873108322fbScarlsonj 		zerror(gettext("cannot use interactive login with scratch "
1874108322fbScarlsonj 		    "zone"));
1875108322fbScarlsonj 		return (1);
1876108322fbScarlsonj 	}
1877108322fbScarlsonj 
18787c478bd9Sstevel@tonic-gate 	/*
18797c478bd9Sstevel@tonic-gate 	 * Things are more complex in interactive mode; we get the
18807c478bd9Sstevel@tonic-gate 	 * master side of the pty, then place the user's terminal into
18817c478bd9Sstevel@tonic-gate 	 * raw mode.
18827c478bd9Sstevel@tonic-gate 	 */
18837c478bd9Sstevel@tonic-gate 	if (get_master_pty() == -1) {
18847c478bd9Sstevel@tonic-gate 		zerror(gettext("could not setup master pty device"));
18857c478bd9Sstevel@tonic-gate 		return (1);
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * Compute the "short name" of the pts.  /dev/pts/2 --> pts/2
18907c478bd9Sstevel@tonic-gate 	 */
18917c478bd9Sstevel@tonic-gate 	if ((slavename = ptsname(masterfd)) == NULL) {
18927c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to get name for pseudo-tty"));
18937c478bd9Sstevel@tonic-gate 		return (1);
18947c478bd9Sstevel@tonic-gate 	}
18957c478bd9Sstevel@tonic-gate 	if (strncmp(slavename, "/dev/", strlen("/dev/")) == 0)
18967c478bd9Sstevel@tonic-gate 		(void) strlcpy(slaveshortname, slavename + strlen("/dev/"),
18977c478bd9Sstevel@tonic-gate 		    sizeof (slaveshortname));
18987c478bd9Sstevel@tonic-gate 	else
18997c478bd9Sstevel@tonic-gate 		(void) strlcpy(slaveshortname, slavename,
19007c478bd9Sstevel@tonic-gate 		    sizeof (slaveshortname));
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	(void) printf(gettext("[Connected to zone '%s' %s]\n"), zonename,
19037c478bd9Sstevel@tonic-gate 	    slaveshortname);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	if (set_tty_rawmode(STDIN_FILENO) == -1) {
19067c478bd9Sstevel@tonic-gate 		reset_tty();
19077c478bd9Sstevel@tonic-gate 		zperror(gettext("failed to set stdin pty to raw mode"));
19087c478bd9Sstevel@tonic-gate 		return (1);
19097c478bd9Sstevel@tonic-gate 	}
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	if (prefork_dropprivs() != 0) {
19127c478bd9Sstevel@tonic-gate 		reset_tty();
19137c478bd9Sstevel@tonic-gate 		zperror(gettext("could not allocate privilege set"));
19147c478bd9Sstevel@tonic-gate 		return (1);
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	/*
19187c478bd9Sstevel@tonic-gate 	 * We must mask SIGCLD until after we have coped with the fork
19197c478bd9Sstevel@tonic-gate 	 * sufficiently to deal with it; otherwise we can race and receive the
19207c478bd9Sstevel@tonic-gate 	 * signal before child_pid has been initialized (yes, this really
19217c478bd9Sstevel@tonic-gate 	 * happens).
19227c478bd9Sstevel@tonic-gate 	 */
19237c478bd9Sstevel@tonic-gate 	(void) sigset(SIGCLD, sigcld);
19247c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&block_cld);
19257c478bd9Sstevel@tonic-gate 	(void) sigaddset(&block_cld, SIGCLD);
19267c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/*
19297c478bd9Sstevel@tonic-gate 	 * We activate the contract template at the last minute to
19307c478bd9Sstevel@tonic-gate 	 * avoid intermediate functions that could be using fork(2)
19317c478bd9Sstevel@tonic-gate 	 * internally.
19327c478bd9Sstevel@tonic-gate 	 */
19337c478bd9Sstevel@tonic-gate 	if ((tmpl_fd = init_template()) == -1) {
19347c478bd9Sstevel@tonic-gate 		reset_tty();
19357c478bd9Sstevel@tonic-gate 		zperror(gettext("could not create contract"));
19367c478bd9Sstevel@tonic-gate 		return (1);
19377c478bd9Sstevel@tonic-gate 	}
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 	if ((child_pid = fork()) == -1) {
19407c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
19417c478bd9Sstevel@tonic-gate 		reset_tty();
19427c478bd9Sstevel@tonic-gate 		zperror(gettext("could not fork"));
19437c478bd9Sstevel@tonic-gate 		return (1);
19447c478bd9Sstevel@tonic-gate 	} else if (child_pid == 0) { /* child process */
19457c478bd9Sstevel@tonic-gate 		int slavefd, newslave;
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		(void) ct_tmpl_clear(tmpl_fd);
19487c478bd9Sstevel@tonic-gate 		(void) close(tmpl_fd);
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
19517c478bd9Sstevel@tonic-gate 
1952facf4a8dSllai1 		if ((slavefd = init_slave_pty(zoneid, devroot)) == -1)
19537c478bd9Sstevel@tonic-gate 			return (1);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 		/*
19567c478bd9Sstevel@tonic-gate 		 * Close all fds except for the slave pty.
19577c478bd9Sstevel@tonic-gate 		 */
19587c478bd9Sstevel@tonic-gate 		(void) fdwalk(close_func, &slavefd);
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 		/*
19617c478bd9Sstevel@tonic-gate 		 * Temporarily dup slavefd to stderr; that way if we have
19627c478bd9Sstevel@tonic-gate 		 * to print out that zone_enter failed, the output will
19637c478bd9Sstevel@tonic-gate 		 * have somewhere to go.
19647c478bd9Sstevel@tonic-gate 		 */
19657c478bd9Sstevel@tonic-gate 		if (slavefd != STDERR_FILENO)
19667c478bd9Sstevel@tonic-gate 			(void) dup2(slavefd, STDERR_FILENO);
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 		if (zone_enter(zoneid) == -1) {
19697c478bd9Sstevel@tonic-gate 			zerror(gettext("could not enter zone %s: %s"),
19707c478bd9Sstevel@tonic-gate 			    zonename, strerror(errno));
19717c478bd9Sstevel@tonic-gate 			return (1);
19727c478bd9Sstevel@tonic-gate 		}
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 		if (slavefd != STDERR_FILENO)
19757c478bd9Sstevel@tonic-gate 			(void) close(STDERR_FILENO);
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 		/*
19787c478bd9Sstevel@tonic-gate 		 * We take pains to get this process into a new process
19797c478bd9Sstevel@tonic-gate 		 * group, and subsequently a new session.  In this way,
19807c478bd9Sstevel@tonic-gate 		 * we'll have a session which doesn't yet have a controlling
19817c478bd9Sstevel@tonic-gate 		 * terminal.  When we open the slave, it will become the
19827c478bd9Sstevel@tonic-gate 		 * controlling terminal; no PIDs concerning pgrps or sids
19837c478bd9Sstevel@tonic-gate 		 * will leak inappropriately into the zone.
19847c478bd9Sstevel@tonic-gate 		 */
19857c478bd9Sstevel@tonic-gate 		(void) setpgrp();
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 		/*
19887c478bd9Sstevel@tonic-gate 		 * We need the slave pty to be referenced from the zone's
19897c478bd9Sstevel@tonic-gate 		 * /dev in order to ensure that the devt's, etc are all
19907c478bd9Sstevel@tonic-gate 		 * correct.  Otherwise we break ttyname and the like.
19917c478bd9Sstevel@tonic-gate 		 */
19927c478bd9Sstevel@tonic-gate 		if ((newslave = open(slavename, O_RDWR)) == -1) {
19937c478bd9Sstevel@tonic-gate 			(void) close(slavefd);
19947c478bd9Sstevel@tonic-gate 			return (1);
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 		(void) close(slavefd);
19977c478bd9Sstevel@tonic-gate 		slavefd = newslave;
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 		/*
20007c478bd9Sstevel@tonic-gate 		 * dup the slave to the various FDs, so that when the
20017c478bd9Sstevel@tonic-gate 		 * spawned process does a write/read it maps to the slave
20027c478bd9Sstevel@tonic-gate 		 * pty.
20037c478bd9Sstevel@tonic-gate 		 */
20047c478bd9Sstevel@tonic-gate 		(void) dup2(slavefd, STDIN_FILENO);
20057c478bd9Sstevel@tonic-gate 		(void) dup2(slavefd, STDOUT_FILENO);
20067c478bd9Sstevel@tonic-gate 		(void) dup2(slavefd, STDERR_FILENO);
20077c478bd9Sstevel@tonic-gate 		if (slavefd != STDIN_FILENO && slavefd != STDOUT_FILENO &&
20087c478bd9Sstevel@tonic-gate 		    slavefd != STDERR_FILENO) {
20097c478bd9Sstevel@tonic-gate 			(void) close(slavefd);
20107c478bd9Sstevel@tonic-gate 		}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 		/*
20137c478bd9Sstevel@tonic-gate 		 * In failsafe mode, we don't use login(1), so don't try
20147c478bd9Sstevel@tonic-gate 		 * setting up a utmpx entry.
20159acbbeafSnn35248 		 *
20169acbbeafSnn35248 		 * A branded zone may have very different utmpx semantics.
20179acbbeafSnn35248 		 * At the moment, we only have two brand types:
20189acbbeafSnn35248 		 * Solaris-like (native, sn1) and Linux.  In the Solaris
20199acbbeafSnn35248 		 * case, we know exactly how to do the necessary utmpx
20209acbbeafSnn35248 		 * setup.  Fortunately for us, the Linux /bin/login is
20219acbbeafSnn35248 		 * prepared to deal with a non-initialized utmpx entry, so
20229acbbeafSnn35248 		 * we can simply skip it.  If future brands don't fall into
20239acbbeafSnn35248 		 * either category, we'll have to add a per-brand utmpx
20249acbbeafSnn35248 		 * setup hook.
20257c478bd9Sstevel@tonic-gate 		 */
20269acbbeafSnn35248 		if (!failsafe && (strcmp(zonebrand, "lx") != 0))
20277c478bd9Sstevel@tonic-gate 			if (setup_utmpx(slaveshortname) == -1)
20287c478bd9Sstevel@tonic-gate 				return (1);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 		(void) execve(new_args[0], new_args, new_env);
20317c478bd9Sstevel@tonic-gate 		zperror(gettext("exec failure"));
20327c478bd9Sstevel@tonic-gate 		return (1);
20337c478bd9Sstevel@tonic-gate 	}
20347c478bd9Sstevel@tonic-gate 	(void) ct_tmpl_clear(tmpl_fd);
20357c478bd9Sstevel@tonic-gate 	(void) close(tmpl_fd);
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	/*
20387c478bd9Sstevel@tonic-gate 	 * The rest is only for the parent process.
20397c478bd9Sstevel@tonic-gate 	 */
20407c478bd9Sstevel@tonic-gate 	(void) sigset(SIGWINCH, sigwinch);
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	postfork_dropprivs();
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
2045b52a7fd7Sgjelinek 	doio(masterfd, -1, masterfd, -1, B_FALSE);
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	reset_tty();
20487c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
20497c478bd9Sstevel@tonic-gate 	    gettext("\n[Connection to zone '%s' %s closed]\n"), zonename,
20507c478bd9Sstevel@tonic-gate 	    slaveshortname);
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	if (pollerr != 0) {
20537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Error: connection closed due "
20547c478bd9Sstevel@tonic-gate 		    "to unexpected pollevents=0x%x.\n"), pollerr);
20557c478bd9Sstevel@tonic-gate 		return (1);
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	return (0);
20597c478bd9Sstevel@tonic-gate }
2060