10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52621Sllai1 * Common Development and Distribution License (the "License").
62621Sllai1 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
2212582SGlenn.Faden@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * zlogin provides three types of login which allow users in the global
270Sstevel@tonic-gate * zone to access non-global zones.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * - "interactive login" is similar to rlogin(1); for example, the user could
300Sstevel@tonic-gate * issue 'zlogin my-zone' or 'zlogin -e ^ -l me my-zone'. The user is
310Sstevel@tonic-gate * granted a new pty (which is then shoved into the zone), and an I/O
320Sstevel@tonic-gate * loop between parent and child processes takes care of the interactive
330Sstevel@tonic-gate * session. In this mode, login(1) (and its -c option, which means
340Sstevel@tonic-gate * "already authenticated") is employed to take care of the initialization
350Sstevel@tonic-gate * of the user's session.
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * - "non-interactive login" is similar to su(1M); the user could issue
380Sstevel@tonic-gate * 'zlogin my-zone ls -l' and the command would be run as specified.
390Sstevel@tonic-gate * In this mode, zlogin sets up pipes as the communication channel, and
400Sstevel@tonic-gate * 'su' is used to do the login setup work.
410Sstevel@tonic-gate *
420Sstevel@tonic-gate * - "console login" is the equivalent to accessing the tip line for a
430Sstevel@tonic-gate * zone. For example, the user can issue 'zlogin -C my-zone'.
440Sstevel@tonic-gate * In this mode, zlogin contacts the zoneadmd process via unix domain
450Sstevel@tonic-gate * socket. If zoneadmd is not running, it starts it. This allows the
460Sstevel@tonic-gate * console to be available anytime the zone is installed, regardless of
470Sstevel@tonic-gate * whether it is running.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate #include <sys/socket.h>
510Sstevel@tonic-gate #include <sys/termios.h>
520Sstevel@tonic-gate #include <sys/utsname.h>
530Sstevel@tonic-gate #include <sys/stat.h>
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate #include <sys/contract/process.h>
560Sstevel@tonic-gate #include <sys/ctfs.h>
572712Snn35248 #include <sys/brand.h>
584344Ssl108498 #include <sys/wait.h>
590Sstevel@tonic-gate #include <alloca.h>
600Sstevel@tonic-gate #include <assert.h>
610Sstevel@tonic-gate #include <ctype.h>
620Sstevel@tonic-gate #include <door.h>
630Sstevel@tonic-gate #include <errno.h>
644344Ssl108498 #include <nss_dbdefs.h>
650Sstevel@tonic-gate #include <poll.h>
660Sstevel@tonic-gate #include <priv.h>
670Sstevel@tonic-gate #include <pwd.h>
680Sstevel@tonic-gate #include <unistd.h>
690Sstevel@tonic-gate #include <utmpx.h>
700Sstevel@tonic-gate #include <sac.h>
710Sstevel@tonic-gate #include <signal.h>
720Sstevel@tonic-gate #include <stdarg.h>
730Sstevel@tonic-gate #include <stdio.h>
740Sstevel@tonic-gate #include <stdlib.h>
750Sstevel@tonic-gate #include <string.h>
760Sstevel@tonic-gate #include <strings.h>
770Sstevel@tonic-gate #include <stropts.h>
780Sstevel@tonic-gate #include <wait.h>
790Sstevel@tonic-gate #include <zone.h>
800Sstevel@tonic-gate #include <fcntl.h>
810Sstevel@tonic-gate #include <libdevinfo.h>
820Sstevel@tonic-gate #include <libintl.h>
830Sstevel@tonic-gate #include <locale.h>
840Sstevel@tonic-gate #include <libzonecfg.h>
850Sstevel@tonic-gate #include <libcontract.h>
862712Snn35248 #include <libbrand.h>
8712578SGlenn.Faden@Sun.COM #include <auth_list.h>
8812578SGlenn.Faden@Sun.COM #include <auth_attr.h>
8912578SGlenn.Faden@Sun.COM #include <secdb.h>
900Sstevel@tonic-gate
910Sstevel@tonic-gate static int masterfd;
920Sstevel@tonic-gate static struct termios save_termios;
930Sstevel@tonic-gate static struct termios effective_termios;
940Sstevel@tonic-gate static int save_fd;
950Sstevel@tonic-gate static struct winsize winsize;
960Sstevel@tonic-gate static volatile int dead;
970Sstevel@tonic-gate static volatile pid_t child_pid = -1;
980Sstevel@tonic-gate static int interactive = 0;
990Sstevel@tonic-gate static priv_set_t *dropprivs;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate static int nocmdchar = 0;
1020Sstevel@tonic-gate static int failsafe = 0;
1030Sstevel@tonic-gate static char cmdchar = '~';
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate static int pollerr = 0;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate static const char *pname;
10812578SGlenn.Faden@Sun.COM static char *username;
10912578SGlenn.Faden@Sun.COM
11012578SGlenn.Faden@Sun.COM /*
11112578SGlenn.Faden@Sun.COM * When forced_login is true, the user is not prompted
11212578SGlenn.Faden@Sun.COM * for an authentication password in the target zone.
11312578SGlenn.Faden@Sun.COM */
11412578SGlenn.Faden@Sun.COM static boolean_t forced_login = B_FALSE;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
1170Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
1180Sstevel@tonic-gate #endif
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate #define SUPATH "/usr/bin/su"
1210Sstevel@tonic-gate #define FAILSAFESHELL "/sbin/sh"
1220Sstevel@tonic-gate #define DEFAULTSHELL "/sbin/sh"
1230Sstevel@tonic-gate #define DEF_PATH "/usr/sbin:/usr/bin"
1240Sstevel@tonic-gate
12510796SStephen.Lawrence@Sun.COM #define CLUSTER_BRAND_NAME "cluster"
12610796SStephen.Lawrence@Sun.COM
1273789Sgjelinek /*
1283789Sgjelinek * The ZLOGIN_BUFSIZ is larger than PIPE_BUF so we can be sure we're clearing
1293789Sgjelinek * out the pipe when the child is exiting. The ZLOGIN_RDBUFSIZ must be less
1303789Sgjelinek * than ZLOGIN_BUFSIZ (because we share the buffer in doio). This value is
1313789Sgjelinek * also chosen in conjunction with the HI_WATER setting to make sure we
1323789Sgjelinek * don't fill up the pipe. We can write FIFOHIWAT (16k) into the pipe before
1333789Sgjelinek * blocking. By having ZLOGIN_RDBUFSIZ set to 1k and HI_WATER set to 8k, we
1343789Sgjelinek * know we can always write a ZLOGIN_RDBUFSIZ chunk into the pipe when there
1353789Sgjelinek * is less than HI_WATER data already in the pipe.
1363789Sgjelinek */
1372852Sdp #define ZLOGIN_BUFSIZ 8192
1383789Sgjelinek #define ZLOGIN_RDBUFSIZ 1024
1393789Sgjelinek #define HI_WATER 8192
1402852Sdp
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * See canonify() below. CANONIFY_LEN is the maximum length that a
1430Sstevel@tonic-gate * "canonical" sequence will expand to (backslash, three octal digits, NUL).
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate #define CANONIFY_LEN 5
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate static void
usage(void)1480Sstevel@tonic-gate usage(void)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate (void) fprintf(stderr, gettext("usage: %s [ -CES ] [ -e cmdchar ] "
1510Sstevel@tonic-gate "[-l user] zonename [command [args ...] ]\n"), pname);
1520Sstevel@tonic-gate exit(2);
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate static const char *
getpname(const char * arg0)1560Sstevel@tonic-gate getpname(const char *arg0)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate const char *p = strrchr(arg0, '/');
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate if (p == NULL)
1610Sstevel@tonic-gate p = arg0;
1620Sstevel@tonic-gate else
1630Sstevel@tonic-gate p++;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate pname = p;
1660Sstevel@tonic-gate return (p);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate static void
zerror(const char * fmt,...)1700Sstevel@tonic-gate zerror(const char *fmt, ...)
1710Sstevel@tonic-gate {
1720Sstevel@tonic-gate va_list alist;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", pname);
1750Sstevel@tonic-gate va_start(alist, fmt);
1760Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist);
1770Sstevel@tonic-gate va_end(alist);
1780Sstevel@tonic-gate (void) fprintf(stderr, "\n");
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate static void
zperror(const char * str)1820Sstevel@tonic-gate zperror(const char *str)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate const char *estr;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if ((estr = strerror(errno)) != NULL)
1870Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", pname, str, estr);
1880Sstevel@tonic-gate else
1890Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: errno %d\n", pname, str, errno);
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate * The first part of our privilege dropping scheme needs to be called before
1940Sstevel@tonic-gate * fork(), since we must have it for security; we don't want to be surprised
1950Sstevel@tonic-gate * later that we couldn't allocate the privset.
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate static int
prefork_dropprivs()1980Sstevel@tonic-gate prefork_dropprivs()
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate if ((dropprivs = priv_allocset()) == NULL)
2010Sstevel@tonic-gate return (1);
20211537SCasper.Dik@Sun.COM
20311537SCasper.Dik@Sun.COM priv_basicset(dropprivs);
20411537SCasper.Dik@Sun.COM (void) priv_delset(dropprivs, PRIV_PROC_INFO);
20511537SCasper.Dik@Sun.COM (void) priv_delset(dropprivs, PRIV_PROC_FORK);
20611537SCasper.Dik@Sun.COM (void) priv_delset(dropprivs, PRIV_PROC_EXEC);
20711537SCasper.Dik@Sun.COM (void) priv_delset(dropprivs, PRIV_FILE_LINK_ANY);
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate /*
21011537SCasper.Dik@Sun.COM * We need to keep the basic privilege PROC_SESSION and all unknown
21111537SCasper.Dik@Sun.COM * basic privileges as well as the privileges PROC_ZONE and
21211537SCasper.Dik@Sun.COM * PROC_OWNER in order to query session information and
2130Sstevel@tonic-gate * send signals.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate if (interactive == 0) {
21611537SCasper.Dik@Sun.COM (void) priv_addset(dropprivs, PRIV_PROC_ZONE);
21711537SCasper.Dik@Sun.COM (void) priv_addset(dropprivs, PRIV_PROC_OWNER);
21811537SCasper.Dik@Sun.COM } else {
21911537SCasper.Dik@Sun.COM (void) priv_delset(dropprivs, PRIV_PROC_SESSION);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate return (0);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * The second part of the privilege drop. We are paranoid about being attacked
2270Sstevel@tonic-gate * by the zone, so we drop all privileges. This should prevent a compromise
2280Sstevel@tonic-gate * which gets us to fork(), exec(), symlink(), etc.
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate static void
postfork_dropprivs()2310Sstevel@tonic-gate postfork_dropprivs()
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate if ((setppriv(PRIV_SET, PRIV_PERMITTED, dropprivs)) == -1) {
2340Sstevel@tonic-gate zperror(gettext("Warning: could not set permitted privileges"));
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate if ((setppriv(PRIV_SET, PRIV_LIMIT, dropprivs)) == -1) {
2370Sstevel@tonic-gate zperror(gettext("Warning: could not set limit privileges"));
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate if ((setppriv(PRIV_SET, PRIV_INHERITABLE, dropprivs)) == -1) {
2400Sstevel@tonic-gate zperror(gettext("Warning: could not set inheritable "
2410Sstevel@tonic-gate "privileges"));
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate * Create the unix domain socket and call the zoneadmd server; handshake
2470Sstevel@tonic-gate * with it to determine whether it will allow us to connect.
2480Sstevel@tonic-gate */
2490Sstevel@tonic-gate static int
get_console_master(const char * zname)2500Sstevel@tonic-gate get_console_master(const char *zname)
2510Sstevel@tonic-gate {
2520Sstevel@tonic-gate int sockfd = -1;
2530Sstevel@tonic-gate struct sockaddr_un servaddr;
2540Sstevel@tonic-gate char clientid[MAXPATHLEN];
2550Sstevel@tonic-gate char handshake[MAXPATHLEN], c;
2560Sstevel@tonic-gate int msglen;
2570Sstevel@tonic-gate int i = 0, err = 0;
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
2600Sstevel@tonic-gate zperror(gettext("could not create socket"));
2610Sstevel@tonic-gate return (-1);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate bzero(&servaddr, sizeof (servaddr));
2650Sstevel@tonic-gate servaddr.sun_family = AF_UNIX;
2660Sstevel@tonic-gate (void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path),
2670Sstevel@tonic-gate "%s/%s.console_sock", ZONES_TMPDIR, zname);
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate if (connect(sockfd, (struct sockaddr *)&servaddr,
2700Sstevel@tonic-gate sizeof (servaddr)) == -1) {
2710Sstevel@tonic-gate zperror(gettext("Could not connect to zone console"));
2720Sstevel@tonic-gate goto bad;
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate masterfd = sockfd;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate msglen = snprintf(clientid, sizeof (clientid), "IDENT %lu %s\n",
2770Sstevel@tonic-gate getpid(), setlocale(LC_MESSAGES, NULL));
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate if (msglen >= sizeof (clientid) || msglen < 0) {
2800Sstevel@tonic-gate zerror("protocol error");
2810Sstevel@tonic-gate goto bad;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate if (write(masterfd, clientid, msglen) != msglen) {
2850Sstevel@tonic-gate zerror("protocol error");
2860Sstevel@tonic-gate goto bad;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate bzero(handshake, sizeof (handshake));
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate * Take care not to accumulate more than our fill, and leave room for
2930Sstevel@tonic-gate * the NUL at the end.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate while ((err = read(masterfd, &c, 1)) == 1) {
2960Sstevel@tonic-gate if (i >= (sizeof (handshake) - 1))
2970Sstevel@tonic-gate break;
2980Sstevel@tonic-gate if (c == '\n')
2990Sstevel@tonic-gate break;
3000Sstevel@tonic-gate handshake[i] = c;
3010Sstevel@tonic-gate i++;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate /*
3050Sstevel@tonic-gate * If something went wrong during the handshake we bail; perhaps
3060Sstevel@tonic-gate * the server died off.
3070Sstevel@tonic-gate */
3080Sstevel@tonic-gate if (err == -1) {
3090Sstevel@tonic-gate zperror(gettext("Could not connect to zone console"));
3100Sstevel@tonic-gate goto bad;
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate
3130Sstevel@tonic-gate if (strncmp(handshake, "OK", sizeof (handshake)) == 0)
3140Sstevel@tonic-gate return (0);
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate zerror(gettext("Console is already in use by process ID %s."),
3170Sstevel@tonic-gate handshake);
3180Sstevel@tonic-gate bad:
3190Sstevel@tonic-gate (void) close(sockfd);
3200Sstevel@tonic-gate masterfd = -1;
3210Sstevel@tonic-gate return (-1);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate * Routines to handle pty creation upon zone entry and to shuttle I/O back
3270Sstevel@tonic-gate * and forth between the two terminals. We also compute and store the
3280Sstevel@tonic-gate * name of the slave terminal associated with the master side.
3290Sstevel@tonic-gate */
3300Sstevel@tonic-gate static int
get_master_pty()3310Sstevel@tonic-gate get_master_pty()
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate if ((masterfd = open("/dev/ptmx", O_RDWR|O_NONBLOCK)) < 0) {
3340Sstevel@tonic-gate zperror(gettext("failed to obtain a pseudo-tty"));
3350Sstevel@tonic-gate return (-1);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate if (tcgetattr(STDIN_FILENO, &save_termios) == -1) {
3380Sstevel@tonic-gate zperror(gettext("failed to get terminal settings from stdin"));
3390Sstevel@tonic-gate return (-1);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate (void) ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&winsize);
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate return (0);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate * This is a bit tricky; normally a pts device will belong to the zone it
3480Sstevel@tonic-gate * is granted to. But in the case of "entering" a zone, we need to establish
3490Sstevel@tonic-gate * the pty before entering the zone so that we can vector I/O to and from it
3500Sstevel@tonic-gate * from the global zone.
3510Sstevel@tonic-gate *
3520Sstevel@tonic-gate * We use the zonept() call to let the ptm driver know what we are up to;
3530Sstevel@tonic-gate * the only other hairy bit is the setting of zoneslavename (which happens
3540Sstevel@tonic-gate * above, in get_master_pty()).
3550Sstevel@tonic-gate */
3560Sstevel@tonic-gate static int
init_slave_pty(zoneid_t zoneid,char * devroot)3572621Sllai1 init_slave_pty(zoneid_t zoneid, char *devroot)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate int slavefd = -1;
3600Sstevel@tonic-gate char *slavename, zoneslavename[MAXPATHLEN];
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate * Set slave permissions, zone the pts, then unlock it.
3640Sstevel@tonic-gate */
3650Sstevel@tonic-gate if (grantpt(masterfd) != 0) {
3660Sstevel@tonic-gate zperror(gettext("grantpt failed"));
3670Sstevel@tonic-gate return (-1);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if (unlockpt(masterfd) != 0) {
3710Sstevel@tonic-gate zperror(gettext("unlockpt failed"));
3720Sstevel@tonic-gate return (-1);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate * We must open the slave side before zoning this pty; otherwise
3770Sstevel@tonic-gate * the kernel would refuse us the open-- zoning a pty makes it
3782621Sllai1 * inaccessible to the global zone. Note we are trying to open
3792621Sllai1 * the device node via the $ZONEROOT/dev path for this pty.
3800Sstevel@tonic-gate *
3810Sstevel@tonic-gate * Later we'll close the slave out when once we've opened it again
3820Sstevel@tonic-gate * from within the target zone. Blarg.
3830Sstevel@tonic-gate */
3840Sstevel@tonic-gate if ((slavename = ptsname(masterfd)) == NULL) {
3850Sstevel@tonic-gate zperror(gettext("failed to get name for pseudo-tty"));
3860Sstevel@tonic-gate return (-1);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate (void) snprintf(zoneslavename, sizeof (zoneslavename), "%s%s",
3902621Sllai1 devroot, slavename);
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate if ((slavefd = open(zoneslavename, O_RDWR)) < 0) {
3932621Sllai1 zerror(gettext("failed to open %s: %s"), zoneslavename,
3942621Sllai1 strerror(errno));
3952621Sllai1 return (-1);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate * Push hardware emulation (ptem), line discipline (ldterm),
4000Sstevel@tonic-gate * and V7/4BSD/Xenix compatibility (ttcompat) modules.
4010Sstevel@tonic-gate */
4020Sstevel@tonic-gate if (ioctl(slavefd, I_PUSH, "ptem") == -1) {
4030Sstevel@tonic-gate zperror(gettext("failed to push ptem module"));
4040Sstevel@tonic-gate if (!failsafe)
4050Sstevel@tonic-gate goto bad;
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate * Anchor the stream to prevent malicious I_POPs; we prefer to do
4100Sstevel@tonic-gate * this prior to entering the zone so that we can detect any errors
4110Sstevel@tonic-gate * early, and so that we can set the anchor from the global zone.
4120Sstevel@tonic-gate */
4130Sstevel@tonic-gate if (ioctl(slavefd, I_ANCHOR) == -1) {
4140Sstevel@tonic-gate zperror(gettext("failed to set stream anchor"));
4150Sstevel@tonic-gate if (!failsafe)
4160Sstevel@tonic-gate goto bad;
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate if (ioctl(slavefd, I_PUSH, "ldterm") == -1) {
4200Sstevel@tonic-gate zperror(gettext("failed to push ldterm module"));
4210Sstevel@tonic-gate if (!failsafe)
4220Sstevel@tonic-gate goto bad;
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate if (ioctl(slavefd, I_PUSH, "ttcompat") == -1) {
4250Sstevel@tonic-gate zperror(gettext("failed to push ttcompat module"));
4260Sstevel@tonic-gate if (!failsafe)
4270Sstevel@tonic-gate goto bad;
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate * Propagate terminal settings from the external term to the new one.
4320Sstevel@tonic-gate */
4330Sstevel@tonic-gate if (tcsetattr(slavefd, TCSAFLUSH, &save_termios) == -1) {
4340Sstevel@tonic-gate zperror(gettext("failed to set terminal settings"));
4350Sstevel@tonic-gate if (!failsafe)
4360Sstevel@tonic-gate goto bad;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate (void) ioctl(slavefd, TIOCSWINSZ, (char *)&winsize);
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate if (zonept(masterfd, zoneid) != 0) {
4410Sstevel@tonic-gate zperror(gettext("could not set zoneid of pty"));
4420Sstevel@tonic-gate goto bad;
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate return (slavefd);
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate bad:
4480Sstevel@tonic-gate (void) close(slavefd);
4490Sstevel@tonic-gate return (-1);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate * Place terminal into raw mode.
4540Sstevel@tonic-gate */
4550Sstevel@tonic-gate static int
set_tty_rawmode(int fd)4560Sstevel@tonic-gate set_tty_rawmode(int fd)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate struct termios term;
4590Sstevel@tonic-gate if (tcgetattr(fd, &term) < 0) {
4600Sstevel@tonic-gate zperror(gettext("failed to get user terminal settings"));
4610Sstevel@tonic-gate return (-1);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate /* Stash for later, so we can revert back to previous mode */
4650Sstevel@tonic-gate save_termios = term;
4660Sstevel@tonic-gate save_fd = fd;
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate /* disable 8->7 bit strip, start/stop, enable any char to restart */
4690Sstevel@tonic-gate term.c_iflag &= ~(ISTRIP|IXON|IXANY);
4700Sstevel@tonic-gate /* disable NL->CR, CR->NL, ignore CR, UPPER->lower */
4710Sstevel@tonic-gate term.c_iflag &= ~(INLCR|ICRNL|IGNCR|IUCLC);
4720Sstevel@tonic-gate /* disable output post-processing */
4730Sstevel@tonic-gate term.c_oflag &= ~OPOST;
4740Sstevel@tonic-gate /* disable canonical mode, signal chars, echo & extended functions */
4750Sstevel@tonic-gate term.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate term.c_cc[VMIN] = 1; /* byte-at-a-time */
4780Sstevel@tonic-gate term.c_cc[VTIME] = 0;
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term)) {
4810Sstevel@tonic-gate zperror(gettext("failed to set user terminal to raw mode"));
4820Sstevel@tonic-gate return (-1);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate * We need to know the value of VEOF so that we can properly process for
4870Sstevel@tonic-gate * client-side ~<EOF>. But we have obliterated VEOF in term,
4880Sstevel@tonic-gate * because VMIN overloads the same array slot in non-canonical mode.
4890Sstevel@tonic-gate * Stupid @&^%!
4900Sstevel@tonic-gate *
4910Sstevel@tonic-gate * So here we construct the "effective" termios from the current
4920Sstevel@tonic-gate * terminal settings, and the corrected VEOF and VEOL settings.
4930Sstevel@tonic-gate */
4940Sstevel@tonic-gate if (tcgetattr(STDIN_FILENO, &effective_termios) < 0) {
4950Sstevel@tonic-gate zperror(gettext("failed to get user terminal settings"));
4960Sstevel@tonic-gate return (-1);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate effective_termios.c_cc[VEOF] = save_termios.c_cc[VEOF];
4990Sstevel@tonic-gate effective_termios.c_cc[VEOL] = save_termios.c_cc[VEOL];
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate return (0);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate * Copy terminal window size from our terminal to the pts.
5060Sstevel@tonic-gate */
5070Sstevel@tonic-gate /*ARGSUSED*/
5080Sstevel@tonic-gate static void
sigwinch(int s)5090Sstevel@tonic-gate sigwinch(int s)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate struct winsize ws;
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate if (ioctl(0, TIOCGWINSZ, &ws) == 0)
5140Sstevel@tonic-gate (void) ioctl(masterfd, TIOCSWINSZ, &ws);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5177089Sgjelinek static volatile int close_on_sig = -1;
5187089Sgjelinek
5190Sstevel@tonic-gate static void
5200Sstevel@tonic-gate /*ARGSUSED*/
sigcld(int s)5210Sstevel@tonic-gate sigcld(int s)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate int status;
5240Sstevel@tonic-gate pid_t pid;
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate * Peek at the exit status. If this isn't the process we cared
5280Sstevel@tonic-gate * about, then just reap it.
5290Sstevel@tonic-gate */
5300Sstevel@tonic-gate if ((pid = waitpid(child_pid, &status, WNOHANG|WNOWAIT)) != -1) {
5310Sstevel@tonic-gate if (pid == child_pid &&
5327089Sgjelinek (WIFEXITED(status) || WIFSIGNALED(status))) {
5330Sstevel@tonic-gate dead = 1;
5347089Sgjelinek if (close_on_sig != -1) {
5357089Sgjelinek (void) write(close_on_sig, "a", 1);
5367089Sgjelinek (void) close(close_on_sig);
5377089Sgjelinek close_on_sig = -1;
5387089Sgjelinek }
5397089Sgjelinek } else {
5400Sstevel@tonic-gate (void) waitpid(pid, &status, WNOHANG);
5417089Sgjelinek }
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * Some signals (currently, SIGINT) must be forwarded on to the process
5470Sstevel@tonic-gate * group of the child process.
5480Sstevel@tonic-gate */
5490Sstevel@tonic-gate static void
sig_forward(int s)5500Sstevel@tonic-gate sig_forward(int s)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate if (child_pid != -1) {
5530Sstevel@tonic-gate pid_t pgid = getpgid(child_pid);
5540Sstevel@tonic-gate if (pgid != -1)
5550Sstevel@tonic-gate (void) sigsend(P_PGID, pgid, s);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate /*
5600Sstevel@tonic-gate * reset terminal settings for global environment
5610Sstevel@tonic-gate */
5620Sstevel@tonic-gate static void
reset_tty()5630Sstevel@tonic-gate reset_tty()
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate (void) tcsetattr(save_fd, TCSADRAIN, &save_termios);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate * Convert character to printable representation, for display with locally
5700Sstevel@tonic-gate * echoed command characters (like when we need to display ~^D)
5710Sstevel@tonic-gate */
5720Sstevel@tonic-gate static void
canonify(char c,char * cc)5730Sstevel@tonic-gate canonify(char c, char *cc)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate if (isprint(c)) {
5760Sstevel@tonic-gate cc[0] = c;
5770Sstevel@tonic-gate cc[1] = '\0';
5780Sstevel@tonic-gate } else if (c >= 0 && c <= 31) { /* ^@ through ^_ */
5790Sstevel@tonic-gate cc[0] = '^';
5800Sstevel@tonic-gate cc[1] = c + '@';
5810Sstevel@tonic-gate cc[2] = '\0';
5820Sstevel@tonic-gate } else {
5830Sstevel@tonic-gate cc[0] = '\\';
5840Sstevel@tonic-gate cc[1] = ((c >> 6) & 7) + '0';
5850Sstevel@tonic-gate cc[2] = ((c >> 3) & 7) + '0';
5860Sstevel@tonic-gate cc[3] = (c & 7) + '0';
5870Sstevel@tonic-gate cc[4] = '\0';
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /*
5920Sstevel@tonic-gate * process_user_input watches the input stream for the escape sequence for
5930Sstevel@tonic-gate * 'quit' (by default, tilde-period). Because we might be fed just one
5940Sstevel@tonic-gate * keystroke at a time, state associated with the user input (are we at the
5950Sstevel@tonic-gate * beginning of the line? are we locally echoing the next character?) is
5960Sstevel@tonic-gate * maintained by beginning_of_line and local_echo across calls to the routine.
5973686Sgjelinek * If the write to outfd fails, we'll try to read from infd in an attempt
5983686Sgjelinek * to prevent deadlock between the two processes.
5990Sstevel@tonic-gate *
6000Sstevel@tonic-gate * This routine returns -1 when the 'quit' escape sequence has been issued,
6017089Sgjelinek * or an error is encountered, 1 if stdin is EOF, and 0 otherwise.
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate static int
process_user_input(int outfd,int infd)6047089Sgjelinek process_user_input(int outfd, int infd)
6050Sstevel@tonic-gate {
6060Sstevel@tonic-gate static boolean_t beginning_of_line = B_TRUE;
6070Sstevel@tonic-gate static boolean_t local_echo = B_FALSE;
6087089Sgjelinek char ibuf[ZLOGIN_BUFSIZ];
6097089Sgjelinek int nbytes;
6107089Sgjelinek char *buf = ibuf;
6117089Sgjelinek char c = *buf;
6120Sstevel@tonic-gate
6137089Sgjelinek nbytes = read(STDIN_FILENO, ibuf, ZLOGIN_RDBUFSIZ);
6147089Sgjelinek if (nbytes == -1 && (errno != EINTR || dead))
6157089Sgjelinek return (-1);
6167089Sgjelinek
6177089Sgjelinek if (nbytes == -1) /* The read was interrupted. */
6187089Sgjelinek return (0);
6197089Sgjelinek
6207089Sgjelinek /* 0 read means EOF, close the pipe to the child */
6217089Sgjelinek if (nbytes == 0)
6227089Sgjelinek return (1);
6237089Sgjelinek
6240Sstevel@tonic-gate for (c = *buf; nbytes > 0; c = *buf, --nbytes) {
6250Sstevel@tonic-gate buf++;
6260Sstevel@tonic-gate if (beginning_of_line && !nocmdchar) {
6270Sstevel@tonic-gate beginning_of_line = B_FALSE;
6280Sstevel@tonic-gate if (c == cmdchar) {
6290Sstevel@tonic-gate local_echo = B_TRUE;
6300Sstevel@tonic-gate continue;
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate } else if (local_echo) {
6330Sstevel@tonic-gate local_echo = B_FALSE;
6340Sstevel@tonic-gate if (c == '.' || c == effective_termios.c_cc[VEOF]) {
6350Sstevel@tonic-gate char cc[CANONIFY_LEN];
6363686Sgjelinek
6370Sstevel@tonic-gate canonify(c, cc);
6380Sstevel@tonic-gate (void) write(STDOUT_FILENO, &cmdchar, 1);
6390Sstevel@tonic-gate (void) write(STDOUT_FILENO, cc, strlen(cc));
6400Sstevel@tonic-gate return (-1);
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate }
6433686Sgjelinek retry:
6443686Sgjelinek if (write(outfd, &c, 1) <= 0) {
6453686Sgjelinek /*
6463686Sgjelinek * Since the fd we are writing to is opened with
6473686Sgjelinek * O_NONBLOCK it is possible to get EAGAIN if the
6483686Sgjelinek * pipe is full. One way this could happen is if we
6493686Sgjelinek * are writing a lot of data into the pipe in this loop
6503686Sgjelinek * and the application on the other end is echoing that
6513686Sgjelinek * data back out to its stdout. The output pipe can
6523686Sgjelinek * fill up since we are stuck here in this loop and not
6533686Sgjelinek * draining the other pipe. We can try to read some of
6543686Sgjelinek * the data to see if we can drain the pipe so that the
6553686Sgjelinek * application can continue to make progress. The read
6563686Sgjelinek * is non-blocking so we won't hang here. We also wait
6573686Sgjelinek * a bit before retrying since there could be other
6583686Sgjelinek * reasons why the pipe is full and we don't want to
6593686Sgjelinek * continuously retry.
6603686Sgjelinek */
6613686Sgjelinek if (errno == EAGAIN) {
6623686Sgjelinek struct timespec rqtp;
6633686Sgjelinek int ln;
6647089Sgjelinek char obuf[ZLOGIN_BUFSIZ];
6653686Sgjelinek
6667089Sgjelinek if ((ln = read(infd, obuf, ZLOGIN_BUFSIZ)) > 0)
6677089Sgjelinek (void) write(STDOUT_FILENO, obuf, ln);
6683686Sgjelinek
6693686Sgjelinek /* sleep for 10 milliseconds */
6703686Sgjelinek rqtp.tv_sec = 0;
6713686Sgjelinek rqtp.tv_nsec = 10 * (NANOSEC / MILLISEC);
6723686Sgjelinek (void) nanosleep(&rqtp, NULL);
6733686Sgjelinek if (!dead)
6743686Sgjelinek goto retry;
6753686Sgjelinek }
6763686Sgjelinek
6770Sstevel@tonic-gate return (-1);
6783686Sgjelinek }
6790Sstevel@tonic-gate beginning_of_line = (c == '\r' || c == '\n' ||
6800Sstevel@tonic-gate c == effective_termios.c_cc[VKILL] ||
6810Sstevel@tonic-gate c == effective_termios.c_cc[VEOL] ||
6820Sstevel@tonic-gate c == effective_termios.c_cc[VSUSP] ||
6830Sstevel@tonic-gate c == effective_termios.c_cc[VINTR]);
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate return (0);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate /*
6893789Sgjelinek * This function prevents deadlock between zlogin and the application in the
6903789Sgjelinek * zone that it is talking to. This can happen when we read from zlogin's
6913789Sgjelinek * stdin and write the data down the pipe to the application. If the pipe
6923789Sgjelinek * is full, we'll block in the write. Because zlogin could be blocked in
6933789Sgjelinek * the write, it would never read the application's stdout/stderr so the
6943789Sgjelinek * application can then block on those writes (when the pipe fills up). If the
6953789Sgjelinek * the application gets blocked this way, it can never get around to reading
6963789Sgjelinek * its stdin so that zlogin can unblock from its write. Once in this state,
6973789Sgjelinek * the two processes are deadlocked.
6983789Sgjelinek *
6993789Sgjelinek * To prevent this, we want to verify that we can write into the pipe before we
7003789Sgjelinek * read from our stdin. If the pipe already is pretty full, we bypass the read
7013789Sgjelinek * for now. We'll circle back here again after the poll() so that we can
7023789Sgjelinek * try again. When this function is called, we already know there is data
7037089Sgjelinek * ready to read on STDIN_FILENO. We return -1 if there is a problem, 1 if
7047089Sgjelinek * stdin is EOF, and 0 if everything is ok (even though we might not have
7057089Sgjelinek * read/written any data into the pipe on this iteration).
7063789Sgjelinek */
7073789Sgjelinek static int
process_raw_input(int stdin_fd,int appin_fd)7083789Sgjelinek process_raw_input(int stdin_fd, int appin_fd)
7093789Sgjelinek {
7103789Sgjelinek int cc;
71110171SArindam.Sarkar@Sun.COM struct stat64 sb;
7123789Sgjelinek char ibuf[ZLOGIN_RDBUFSIZ];
7133789Sgjelinek
7143789Sgjelinek /* Check how much data is already in the pipe */
71510171SArindam.Sarkar@Sun.COM if (fstat64(appin_fd, &sb) == -1) {
7163789Sgjelinek perror("stat failed");
7173789Sgjelinek return (-1);
7183789Sgjelinek }
7193789Sgjelinek
7203789Sgjelinek if (dead)
7213789Sgjelinek return (-1);
7223789Sgjelinek
7233789Sgjelinek /*
7243789Sgjelinek * The pipe already has a lot of data in it, don't write any more
7253789Sgjelinek * right now.
7263789Sgjelinek */
7273789Sgjelinek if (sb.st_size >= HI_WATER)
7283789Sgjelinek return (0);
7293789Sgjelinek
7303789Sgjelinek cc = read(STDIN_FILENO, ibuf, ZLOGIN_RDBUFSIZ);
7313789Sgjelinek if (cc == -1 && (errno != EINTR || dead))
7323789Sgjelinek return (-1);
7333789Sgjelinek
7343789Sgjelinek if (cc == -1) /* The read was interrupted. */
7353789Sgjelinek return (0);
7363789Sgjelinek
7377089Sgjelinek /* 0 read means EOF, close the pipe to the child */
7387089Sgjelinek if (cc == 0)
7397089Sgjelinek return (1);
7407089Sgjelinek
7413789Sgjelinek /*
7423789Sgjelinek * stdin_fd is stdin of the target; so, the thing we'll write the user
7437089Sgjelinek * data *to*.
7443789Sgjelinek */
7453789Sgjelinek if (write(stdin_fd, ibuf, cc) == -1)
7463789Sgjelinek return (-1);
7473789Sgjelinek
7483789Sgjelinek return (0);
7493789Sgjelinek }
7503789Sgjelinek
7513789Sgjelinek /*
7523789Sgjelinek * Write the output from the application running in the zone. We can get
7533789Sgjelinek * a signal during the write (usually it would be SIGCHLD when the application
7543789Sgjelinek * has exited) so we loop to make sure we have written all of the data we read.
7553789Sgjelinek */
7563789Sgjelinek static int
process_output(int in_fd,int out_fd)7573789Sgjelinek process_output(int in_fd, int out_fd)
7583789Sgjelinek {
7593789Sgjelinek int wrote = 0;
7603789Sgjelinek int cc;
7613789Sgjelinek char ibuf[ZLOGIN_BUFSIZ];
7623789Sgjelinek
7633789Sgjelinek cc = read(in_fd, ibuf, ZLOGIN_BUFSIZ);
7643789Sgjelinek if (cc == -1 && (errno != EINTR || dead))
7653789Sgjelinek return (-1);
7663789Sgjelinek if (cc == 0) /* EOF */
7673789Sgjelinek return (-1);
7683789Sgjelinek if (cc == -1) /* The read was interrupted. */
7693789Sgjelinek return (0);
7703789Sgjelinek
7713789Sgjelinek do {
7723789Sgjelinek int len;
7733789Sgjelinek
7743789Sgjelinek len = write(out_fd, ibuf + wrote, cc - wrote);
7753789Sgjelinek if (len == -1 && errno != EINTR)
7763789Sgjelinek return (-1);
7773789Sgjelinek if (len != -1)
7783789Sgjelinek wrote += len;
7793789Sgjelinek } while (wrote < cc);
7803789Sgjelinek
7813789Sgjelinek return (0);
7823789Sgjelinek }
7833789Sgjelinek
7843789Sgjelinek /*
7850Sstevel@tonic-gate * This is the main I/O loop, and is shared across all zlogin modes.
7860Sstevel@tonic-gate * Parameters:
7870Sstevel@tonic-gate * stdin_fd: The fd representing 'stdin' for the slave side; input to
7884344Ssl108498 * the zone will be written here.
7890Sstevel@tonic-gate *
7903789Sgjelinek * appin_fd: The fd representing the other end of the 'stdin' pipe (when
7913789Sgjelinek * we're running non-interactive); used in process_raw_input
7923789Sgjelinek * to ensure we don't fill up the application's stdin pipe.
7933789Sgjelinek *
7940Sstevel@tonic-gate * stdout_fd: The fd representing 'stdout' for the slave side; output
7954344Ssl108498 * from the zone will arrive here.
7960Sstevel@tonic-gate *
7970Sstevel@tonic-gate * stderr_fd: The fd representing 'stderr' for the slave side; output
7984344Ssl108498 * from the zone will arrive here.
7990Sstevel@tonic-gate *
8000Sstevel@tonic-gate * raw_mode: If TRUE, then no processing (for example, for '~.') will
8014344Ssl108498 * be performed on the input coming from STDIN.
8020Sstevel@tonic-gate *
8030Sstevel@tonic-gate * stderr_fd may be specified as -1 if there is no stderr (only non-interactive
8040Sstevel@tonic-gate * mode supplies a stderr).
8050Sstevel@tonic-gate *
8060Sstevel@tonic-gate */
8070Sstevel@tonic-gate static void
doio(int stdin_fd,int appin_fd,int stdout_fd,int stderr_fd,int sig_fd,boolean_t raw_mode)8087089Sgjelinek doio(int stdin_fd, int appin_fd, int stdout_fd, int stderr_fd, int sig_fd,
8093789Sgjelinek boolean_t raw_mode)
8100Sstevel@tonic-gate {
8117089Sgjelinek struct pollfd pollfds[4];
8122852Sdp char ibuf[ZLOGIN_BUFSIZ];
8130Sstevel@tonic-gate int cc, ret;
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate /* read from stdout of zone and write to stdout of global zone */
8160Sstevel@tonic-gate pollfds[0].fd = stdout_fd;
8170Sstevel@tonic-gate pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate /* read from stderr of zone and write to stderr of global zone */
8200Sstevel@tonic-gate pollfds[1].fd = stderr_fd;
8210Sstevel@tonic-gate pollfds[1].events = pollfds[0].events;
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate /* read from stdin of global zone and write to stdin of zone */
8240Sstevel@tonic-gate pollfds[2].fd = STDIN_FILENO;
8250Sstevel@tonic-gate pollfds[2].events = pollfds[0].events;
8260Sstevel@tonic-gate
8277089Sgjelinek /* read from signalling pipe so we know when child dies */
8287089Sgjelinek pollfds[3].fd = sig_fd;
8297089Sgjelinek pollfds[3].events = pollfds[0].events;
8307089Sgjelinek
8310Sstevel@tonic-gate for (;;) {
8320Sstevel@tonic-gate pollfds[0].revents = pollfds[1].revents =
8337089Sgjelinek pollfds[2].revents = pollfds[3].revents = 0;
8340Sstevel@tonic-gate
8350Sstevel@tonic-gate if (dead)
8360Sstevel@tonic-gate break;
8370Sstevel@tonic-gate
8387089Sgjelinek /*
8397089Sgjelinek * There is a race condition here where we can receive the
8407089Sgjelinek * child death signal, set the dead flag, but since we have
8417089Sgjelinek * passed the test above, we would go into poll and hang.
8427089Sgjelinek * To avoid this we use the sig_fd as an additional poll fd.
8437089Sgjelinek * The signal handler writes into the other end of this pipe
8447089Sgjelinek * when the child dies so that the poll will always see that
8457089Sgjelinek * input and proceed. We just loop around at that point and
8467089Sgjelinek * then notice the dead flag.
8477089Sgjelinek */
8487089Sgjelinek
8490Sstevel@tonic-gate ret = poll(pollfds,
8500Sstevel@tonic-gate sizeof (pollfds) / sizeof (struct pollfd), -1);
8517089Sgjelinek
8520Sstevel@tonic-gate if (ret == -1 && errno != EINTR) {
8530Sstevel@tonic-gate perror("poll failed");
8540Sstevel@tonic-gate break;
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate if (errno == EINTR && dead) {
8580Sstevel@tonic-gate break;
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate
8610Sstevel@tonic-gate /* event from master side stdout */
8620Sstevel@tonic-gate if (pollfds[0].revents) {
8630Sstevel@tonic-gate if (pollfds[0].revents &
8640Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
8653789Sgjelinek if (process_output(stdout_fd, STDOUT_FILENO)
8663789Sgjelinek != 0)
8670Sstevel@tonic-gate break;
8680Sstevel@tonic-gate } else {
8690Sstevel@tonic-gate pollerr = pollfds[0].revents;
8700Sstevel@tonic-gate break;
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate /* event from master side stderr */
8750Sstevel@tonic-gate if (pollfds[1].revents) {
8760Sstevel@tonic-gate if (pollfds[1].revents &
8770Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
8783789Sgjelinek if (process_output(stderr_fd, STDERR_FILENO)
8793789Sgjelinek != 0)
8800Sstevel@tonic-gate break;
8810Sstevel@tonic-gate } else {
8820Sstevel@tonic-gate pollerr = pollfds[1].revents;
8830Sstevel@tonic-gate break;
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate /* event from user STDIN side */
8880Sstevel@tonic-gate if (pollfds[2].revents) {
8890Sstevel@tonic-gate if (pollfds[2].revents &
8900Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate * stdin fd is stdin of the target; so,
8930Sstevel@tonic-gate * the thing we'll write the user data *to*.
8940Sstevel@tonic-gate *
8950Sstevel@tonic-gate * Also, unlike on the output side, we
8967089Sgjelinek * close the pipe on a zero-length message.
8970Sstevel@tonic-gate */
8987089Sgjelinek int res;
8997089Sgjelinek
9007089Sgjelinek if (raw_mode)
9017089Sgjelinek res = process_raw_input(stdin_fd,
9027089Sgjelinek appin_fd);
9037089Sgjelinek else
9047089Sgjelinek res = process_user_input(stdin_fd,
9057089Sgjelinek stdout_fd);
9063789Sgjelinek
9077089Sgjelinek if (res < 0)
9087089Sgjelinek break;
9097089Sgjelinek if (res > 0) {
9107089Sgjelinek /* EOF (close) child's stdin_fd */
9117089Sgjelinek pollfds[2].fd = -1;
9127089Sgjelinek while ((res = close(stdin_fd)) != 0 &&
9137089Sgjelinek errno == EINTR)
9147089Sgjelinek ;
9157089Sgjelinek if (res != 0)
9160Sstevel@tonic-gate break;
9170Sstevel@tonic-gate }
9187089Sgjelinek
9197089Sgjelinek } else if (raw_mode && pollfds[2].revents & POLLHUP) {
9200Sstevel@tonic-gate /*
9210Sstevel@tonic-gate * It's OK to get a POLLHUP on STDIN-- it
9220Sstevel@tonic-gate * always happens if you do:
9230Sstevel@tonic-gate *
9240Sstevel@tonic-gate * echo foo | zlogin <zone> <command>
9250Sstevel@tonic-gate *
9260Sstevel@tonic-gate * We reset fd to -1 in this case to clear
9277089Sgjelinek * the condition and close the pipe (EOF) to
9287089Sgjelinek * the other side in order to wrap things up.
9290Sstevel@tonic-gate */
9307089Sgjelinek int res;
9317089Sgjelinek
9320Sstevel@tonic-gate pollfds[2].fd = -1;
9337089Sgjelinek while ((res = close(stdin_fd)) != 0 &&
9347089Sgjelinek errno == EINTR)
9357089Sgjelinek ;
9367089Sgjelinek if (res != 0)
9377089Sgjelinek break;
9380Sstevel@tonic-gate } else {
9390Sstevel@tonic-gate pollerr = pollfds[2].revents;
9400Sstevel@tonic-gate break;
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate }
9430Sstevel@tonic-gate }
9440Sstevel@tonic-gate
9450Sstevel@tonic-gate /*
9460Sstevel@tonic-gate * We are in the midst of dying, but try to poll with a short
9470Sstevel@tonic-gate * timeout to see if we can catch the last bit of I/O from the
9480Sstevel@tonic-gate * children.
9490Sstevel@tonic-gate */
9503789Sgjelinek retry:
9513789Sgjelinek pollfds[0].revents = pollfds[1].revents = 0;
9523789Sgjelinek (void) poll(pollfds, 2, 100);
9530Sstevel@tonic-gate if (pollfds[0].revents &
9540Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
9553789Sgjelinek if ((cc = read(stdout_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) {
9560Sstevel@tonic-gate (void) write(STDOUT_FILENO, ibuf, cc);
9573789Sgjelinek goto retry;
9583789Sgjelinek }
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate if (pollfds[1].revents &
9610Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
9623789Sgjelinek if ((cc = read(stderr_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) {
9630Sstevel@tonic-gate (void) write(STDERR_FILENO, ibuf, cc);
9643789Sgjelinek goto retry;
9653789Sgjelinek }
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate }
9680Sstevel@tonic-gate
9694344Ssl108498 /*
9704344Ssl108498 * Fetch the user_cmd brand hook for getting a user's passwd(4) entry.
9714344Ssl108498 */
9724344Ssl108498 static const char *
zone_get_user_cmd(brand_handle_t bh,const char * login,char * user_cmd,size_t len)9734344Ssl108498 zone_get_user_cmd(brand_handle_t bh, const char *login, char *user_cmd,
9744344Ssl108498 size_t len)
9754344Ssl108498 {
9764344Ssl108498 bzero(user_cmd, sizeof (user_cmd));
9774344Ssl108498 if (brand_get_user_cmd(bh, login, user_cmd, len) != 0)
9784344Ssl108498 return (NULL);
9794344Ssl108498
9804344Ssl108498 return (user_cmd);
9814344Ssl108498 }
9824344Ssl108498
9834344Ssl108498 /* From libc */
9844344Ssl108498 extern int str2passwd(const char *, int, void *, char *, int);
9854344Ssl108498
9864344Ssl108498 /*
9874344Ssl108498 * exec() the user_cmd brand hook, and convert the output string to a
9884344Ssl108498 * struct passwd. This is to be called after zone_enter().
9894344Ssl108498 *
9904344Ssl108498 */
9914344Ssl108498 static struct passwd *
zone_get_user_pw(const char * user_cmd,struct passwd * pwent,char * pwbuf,int pwbuflen)9924344Ssl108498 zone_get_user_pw(const char *user_cmd, struct passwd *pwent, char *pwbuf,
9934344Ssl108498 int pwbuflen)
9944344Ssl108498 {
9954344Ssl108498 char pwline[NSS_BUFLEN_PASSWD];
9964344Ssl108498 char *cin = NULL;
9974344Ssl108498 FILE *fin;
9984344Ssl108498 int status;
9994344Ssl108498
10004344Ssl108498 assert(getzoneid() != GLOBAL_ZONEID);
10014344Ssl108498
10024344Ssl108498 if ((fin = popen(user_cmd, "r")) == NULL)
10034344Ssl108498 return (NULL);
10044344Ssl108498
10054344Ssl108498 while (cin == NULL && !feof(fin))
10064344Ssl108498 cin = fgets(pwline, sizeof (pwline), fin);
10074344Ssl108498
10084344Ssl108498 if (cin == NULL) {
10094344Ssl108498 (void) pclose(fin);
10104344Ssl108498 return (NULL);
10114344Ssl108498 }
10124344Ssl108498
10134344Ssl108498 status = pclose(fin);
10144344Ssl108498 if (!WIFEXITED(status))
10154344Ssl108498 return (NULL);
10164344Ssl108498 if (WEXITSTATUS(status) != 0)
10174344Ssl108498 return (NULL);
10184344Ssl108498
10194344Ssl108498 if (str2passwd(pwline, sizeof (pwline), pwent, pwbuf, pwbuflen) == 0)
10204344Ssl108498 return (pwent);
10214344Ssl108498 else
10224344Ssl108498 return (NULL);
10234344Ssl108498 }
10244344Ssl108498
10252712Snn35248 static char **
zone_login_cmd(brand_handle_t bh,const char * login)10262727Sedp zone_login_cmd(brand_handle_t bh, const char *login)
10272712Snn35248 {
10282712Snn35248 static char result_buf[ARG_MAX];
10292712Snn35248 char **new_argv, *ptr, *lasts;
10302712Snn35248 int n, a;
10312712Snn35248
10322712Snn35248 /* Get the login command for the target zone. */
10332712Snn35248 bzero(result_buf, sizeof (result_buf));
103412578SGlenn.Faden@Sun.COM
103512578SGlenn.Faden@Sun.COM if (forced_login) {
103612578SGlenn.Faden@Sun.COM if (brand_get_forcedlogin_cmd(bh, login,
103712578SGlenn.Faden@Sun.COM result_buf, sizeof (result_buf)) != 0)
103812578SGlenn.Faden@Sun.COM return (NULL);
103912578SGlenn.Faden@Sun.COM } else {
104012578SGlenn.Faden@Sun.COM if (brand_get_login_cmd(bh, login,
104112578SGlenn.Faden@Sun.COM result_buf, sizeof (result_buf)) != 0)
104212578SGlenn.Faden@Sun.COM return (NULL);
104312578SGlenn.Faden@Sun.COM }
10442712Snn35248
10452712Snn35248 /*
10462712Snn35248 * We got back a string that we'd like to execute. But since
10472712Snn35248 * we're not doing the execution via a shell we'll need to convert
10482712Snn35248 * the exec string to an array of strings. We'll do that here
10492712Snn35248 * but we're going to be very simplistic about it and break stuff
10502712Snn35248 * up based on spaces. We're not even going to support any kind
10512712Snn35248 * of quoting or escape characters. It's truly amazing that
10522712Snn35248 * there is no library function in OpenSolaris to do this for us.
10532712Snn35248 */
10542712Snn35248
10552712Snn35248 /*
10562712Snn35248 * Be paranoid. Since we're deliniating based on spaces make
10572712Snn35248 * sure there are no adjacent spaces.
10582712Snn35248 */
10592712Snn35248 if (strstr(result_buf, " ") != NULL)
10602712Snn35248 return (NULL);
10612712Snn35248
10622712Snn35248 /* Remove any trailing whitespace. */
10632712Snn35248 n = strlen(result_buf);
10642712Snn35248 if (result_buf[n - 1] == ' ')
10652712Snn35248 result_buf[n - 1] = '\0';
10662712Snn35248
10672712Snn35248 /* Count how many elements there are in the exec string. */
10682712Snn35248 ptr = result_buf;
10692712Snn35248 for (n = 2; ((ptr = strchr(ptr + 1, (int)' ')) != NULL); n++)
10702712Snn35248 ;
10712712Snn35248
10722712Snn35248 /* Allocate the argv array that we're going to return. */
10732712Snn35248 if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
10742712Snn35248 return (NULL);
10752712Snn35248
10762712Snn35248 /* Tokenize the exec string and return. */
10772712Snn35248 a = 0;
10782712Snn35248 new_argv[a++] = result_buf;
10792712Snn35248 if (n > 2) {
10802712Snn35248 (void) strtok_r(result_buf, " ", &lasts);
10812712Snn35248 while ((new_argv[a++] = strtok_r(NULL, " ", &lasts)) != NULL)
10822712Snn35248 ;
10832712Snn35248 } else {
10842712Snn35248 new_argv[a++] = NULL;
10852712Snn35248 }
10862712Snn35248 assert(n == a);
10872712Snn35248 return (new_argv);
10882712Snn35248 }
10892712Snn35248
10900Sstevel@tonic-gate /*
10910Sstevel@tonic-gate * Prepare argv array for exec'd process; if we're passing commands to the
10920Sstevel@tonic-gate * new process, then use su(1M) to do the invocation. Otherwise, use
10930Sstevel@tonic-gate * 'login -z <from_zonename> -f' (-z is an undocumented option which tells
10940Sstevel@tonic-gate * login that we're coming from another zone, and to disregard its CONSOLE
10950Sstevel@tonic-gate * checks).
10960Sstevel@tonic-gate */
10970Sstevel@tonic-gate static char **
prep_args(brand_handle_t bh,const char * login,char ** argv)10982727Sedp prep_args(brand_handle_t bh, const char *login, char **argv)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate int argc = 0, a = 0, i, n = -1;
11010Sstevel@tonic-gate char **new_argv;
11020Sstevel@tonic-gate
11030Sstevel@tonic-gate if (argv != NULL) {
11040Sstevel@tonic-gate size_t subshell_len = 1;
11050Sstevel@tonic-gate char *subshell;
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate while (argv[argc] != NULL)
11080Sstevel@tonic-gate argc++;
11090Sstevel@tonic-gate
11100Sstevel@tonic-gate for (i = 0; i < argc; i++) {
11110Sstevel@tonic-gate subshell_len += strlen(argv[i]) + 1;
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate if ((subshell = calloc(1, subshell_len)) == NULL)
11140Sstevel@tonic-gate return (NULL);
11150Sstevel@tonic-gate
11160Sstevel@tonic-gate for (i = 0; i < argc; i++) {
11170Sstevel@tonic-gate (void) strcat(subshell, argv[i]);
11180Sstevel@tonic-gate (void) strcat(subshell, " ");
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate
11210Sstevel@tonic-gate if (failsafe) {
11220Sstevel@tonic-gate n = 4;
11230Sstevel@tonic-gate if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
11240Sstevel@tonic-gate return (NULL);
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate new_argv[a++] = FAILSAFESHELL;
11270Sstevel@tonic-gate } else {
11280Sstevel@tonic-gate n = 5;
11290Sstevel@tonic-gate if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
11300Sstevel@tonic-gate return (NULL);
11310Sstevel@tonic-gate
11320Sstevel@tonic-gate new_argv[a++] = SUPATH;
113312578SGlenn.Faden@Sun.COM if (strcmp(login, "root") != 0) {
113412578SGlenn.Faden@Sun.COM new_argv[a++] = "-";
113512578SGlenn.Faden@Sun.COM n++;
113612578SGlenn.Faden@Sun.COM }
11372712Snn35248 new_argv[a++] = (char *)login;
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate new_argv[a++] = "-c";
11400Sstevel@tonic-gate new_argv[a++] = subshell;
11410Sstevel@tonic-gate new_argv[a++] = NULL;
11420Sstevel@tonic-gate assert(a == n);
11430Sstevel@tonic-gate } else {
11440Sstevel@tonic-gate if (failsafe) {
11450Sstevel@tonic-gate n = 2;
11460Sstevel@tonic-gate if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
11470Sstevel@tonic-gate return (NULL);
11480Sstevel@tonic-gate new_argv[a++] = FAILSAFESHELL;
11490Sstevel@tonic-gate new_argv[a++] = NULL;
11502712Snn35248 assert(n == a);
11510Sstevel@tonic-gate } else {
11522727Sedp new_argv = zone_login_cmd(bh, login);
11530Sstevel@tonic-gate }
11540Sstevel@tonic-gate }
11552712Snn35248
11560Sstevel@tonic-gate return (new_argv);
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate * Helper routine for prep_env below.
11610Sstevel@tonic-gate */
11620Sstevel@tonic-gate static char *
add_env(char * name,char * value)11630Sstevel@tonic-gate add_env(char *name, char *value)
11640Sstevel@tonic-gate {
11650Sstevel@tonic-gate size_t sz = strlen(name) + strlen(value) + 2; /* name, =, value, NUL */
11660Sstevel@tonic-gate char *str;
11670Sstevel@tonic-gate
11680Sstevel@tonic-gate if ((str = malloc(sz)) == NULL)
11690Sstevel@tonic-gate return (NULL);
11700Sstevel@tonic-gate
11710Sstevel@tonic-gate (void) snprintf(str, sz, "%s=%s", name, value);
11720Sstevel@tonic-gate return (str);
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate
11750Sstevel@tonic-gate /*
11760Sstevel@tonic-gate * Prepare envp array for exec'd process.
11770Sstevel@tonic-gate */
11780Sstevel@tonic-gate static char **
prep_env()11790Sstevel@tonic-gate prep_env()
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate int e = 0, size = 1;
11820Sstevel@tonic-gate char **new_env, *estr;
11830Sstevel@tonic-gate char *term = getenv("TERM");
11840Sstevel@tonic-gate
11850Sstevel@tonic-gate size++; /* for $PATH */
11860Sstevel@tonic-gate if (term != NULL)
11870Sstevel@tonic-gate size++;
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate /*
11900Sstevel@tonic-gate * In failsafe mode we set $HOME, since '-l' isn't valid in this mode.
11910Sstevel@tonic-gate * We also set $SHELL, since neither login nor su will be around to do
11920Sstevel@tonic-gate * it.
11930Sstevel@tonic-gate */
11940Sstevel@tonic-gate if (failsafe)
11950Sstevel@tonic-gate size += 2;
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate if ((new_env = malloc(sizeof (char *) * size)) == NULL)
11980Sstevel@tonic-gate return (NULL);
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate if ((estr = add_env("PATH", DEF_PATH)) == NULL)
12010Sstevel@tonic-gate return (NULL);
12020Sstevel@tonic-gate new_env[e++] = estr;
12030Sstevel@tonic-gate
12040Sstevel@tonic-gate if (term != NULL) {
12050Sstevel@tonic-gate if ((estr = add_env("TERM", term)) == NULL)
12060Sstevel@tonic-gate return (NULL);
12070Sstevel@tonic-gate new_env[e++] = estr;
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate
12100Sstevel@tonic-gate if (failsafe) {
12110Sstevel@tonic-gate if ((estr = add_env("HOME", "/")) == NULL)
12120Sstevel@tonic-gate return (NULL);
12130Sstevel@tonic-gate new_env[e++] = estr;
12140Sstevel@tonic-gate
12150Sstevel@tonic-gate if ((estr = add_env("SHELL", FAILSAFESHELL)) == NULL)
12160Sstevel@tonic-gate return (NULL);
12170Sstevel@tonic-gate new_env[e++] = estr;
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate new_env[e++] = NULL;
12210Sstevel@tonic-gate
12220Sstevel@tonic-gate assert(e == size);
12230Sstevel@tonic-gate
12240Sstevel@tonic-gate return (new_env);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate * Finish the preparation of the envp array for exec'd non-interactive
12290Sstevel@tonic-gate * zlogins. This is called in the child process *after* we zone_enter(), since
12300Sstevel@tonic-gate * it derives things we can only know within the zone, such as $HOME, $SHELL,
12310Sstevel@tonic-gate * etc. We need only do this in the non-interactive, mode, since otherwise
12320Sstevel@tonic-gate * login(1) will do it. We don't do this in failsafe mode, since it presents
12330Sstevel@tonic-gate * additional ways in which the command could fail, and we'd prefer to avoid
12340Sstevel@tonic-gate * that.
12350Sstevel@tonic-gate */
12360Sstevel@tonic-gate static char **
prep_env_noninteractive(const char * user_cmd,char ** env)12374344Ssl108498 prep_env_noninteractive(const char *user_cmd, char **env)
12380Sstevel@tonic-gate {
12390Sstevel@tonic-gate size_t size;
12400Sstevel@tonic-gate char **new_env;
12410Sstevel@tonic-gate int e, i;
12420Sstevel@tonic-gate char *estr;
12430Sstevel@tonic-gate char varmail[LOGNAME_MAX + 11]; /* strlen(/var/mail/) = 10, NUL */
12444344Ssl108498 char pwbuf[NSS_BUFLEN_PASSWD + 1];
12454344Ssl108498 struct passwd pwent;
12464344Ssl108498 struct passwd *pw = NULL;
12470Sstevel@tonic-gate
12480Sstevel@tonic-gate assert(env != NULL);
12490Sstevel@tonic-gate assert(failsafe == 0);
12500Sstevel@tonic-gate
12510Sstevel@tonic-gate /*
12524344Ssl108498 * Exec the "user_cmd" brand hook to get a pwent for the
12534344Ssl108498 * login user. If this fails, HOME will be set to "/", SHELL
12544344Ssl108498 * will be set to $DEFAULTSHELL, and we will continue to exec
12554344Ssl108498 * SUPATH <login> -c <cmd>.
12564344Ssl108498 */
12574344Ssl108498 pw = zone_get_user_pw(user_cmd, &pwent, pwbuf, sizeof (pwbuf));
12584344Ssl108498
12594344Ssl108498 /*
12600Sstevel@tonic-gate * Get existing envp size.
12610Sstevel@tonic-gate */
12620Sstevel@tonic-gate for (size = 0; env[size] != NULL; size++)
12630Sstevel@tonic-gate ;
12644344Ssl108498
12650Sstevel@tonic-gate e = size;
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate /*
12680Sstevel@tonic-gate * Finish filling out the environment; we duplicate the environment
12690Sstevel@tonic-gate * setup described in login(1), for lack of a better precedent.
12700Sstevel@tonic-gate */
12714344Ssl108498 if (pw != NULL)
12720Sstevel@tonic-gate size += 3; /* LOGNAME, HOME, MAIL */
12734344Ssl108498 else
12744344Ssl108498 size += 1; /* HOME */
12754344Ssl108498
12760Sstevel@tonic-gate size++; /* always fill in SHELL */
12770Sstevel@tonic-gate size++; /* terminating NULL */
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate if ((new_env = malloc(sizeof (char *) * size)) == NULL)
12800Sstevel@tonic-gate goto malloc_fail;
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate * Copy existing elements of env into new_env.
12840Sstevel@tonic-gate */
12850Sstevel@tonic-gate for (i = 0; env[i] != NULL; i++) {
12860Sstevel@tonic-gate if ((new_env[i] = strdup(env[i])) == NULL)
12870Sstevel@tonic-gate goto malloc_fail;
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate assert(e == i);
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate if (pw != NULL) {
12920Sstevel@tonic-gate if ((estr = add_env("LOGNAME", pw->pw_name)) == NULL)
12930Sstevel@tonic-gate goto malloc_fail;
12940Sstevel@tonic-gate new_env[e++] = estr;
12950Sstevel@tonic-gate
12960Sstevel@tonic-gate if ((estr = add_env("HOME", pw->pw_dir)) == NULL)
12970Sstevel@tonic-gate goto malloc_fail;
12980Sstevel@tonic-gate new_env[e++] = estr;
12990Sstevel@tonic-gate
13000Sstevel@tonic-gate if (chdir(pw->pw_dir) != 0)
13010Sstevel@tonic-gate zerror(gettext("Could not chdir to home directory "
13020Sstevel@tonic-gate "%s: %s"), pw->pw_dir, strerror(errno));
13030Sstevel@tonic-gate
13040Sstevel@tonic-gate (void) snprintf(varmail, sizeof (varmail), "/var/mail/%s",
13050Sstevel@tonic-gate pw->pw_name);
13060Sstevel@tonic-gate if ((estr = add_env("MAIL", varmail)) == NULL)
13070Sstevel@tonic-gate goto malloc_fail;
13080Sstevel@tonic-gate new_env[e++] = estr;
13094344Ssl108498 } else {
13104344Ssl108498 if ((estr = add_env("HOME", "/")) == NULL)
13114344Ssl108498 goto malloc_fail;
13124344Ssl108498 new_env[e++] = estr;
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate
13150Sstevel@tonic-gate if (pw != NULL && strlen(pw->pw_shell) > 0) {
13160Sstevel@tonic-gate if ((estr = add_env("SHELL", pw->pw_shell)) == NULL)
13170Sstevel@tonic-gate goto malloc_fail;
13180Sstevel@tonic-gate new_env[e++] = estr;
13190Sstevel@tonic-gate } else {
13200Sstevel@tonic-gate if ((estr = add_env("SHELL", DEFAULTSHELL)) == NULL)
13210Sstevel@tonic-gate goto malloc_fail;
13220Sstevel@tonic-gate new_env[e++] = estr;
13230Sstevel@tonic-gate }
13240Sstevel@tonic-gate
13250Sstevel@tonic-gate new_env[e++] = NULL; /* add terminating NULL */
13260Sstevel@tonic-gate
13270Sstevel@tonic-gate assert(e == size);
13280Sstevel@tonic-gate return (new_env);
13290Sstevel@tonic-gate
13300Sstevel@tonic-gate malloc_fail:
13310Sstevel@tonic-gate zperror(gettext("failed to allocate memory for process environment"));
13320Sstevel@tonic-gate return (NULL);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate
13350Sstevel@tonic-gate static int
close_func(void * slavefd,int fd)13360Sstevel@tonic-gate close_func(void *slavefd, int fd)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate if (fd != *(int *)slavefd)
13390Sstevel@tonic-gate (void) close(fd);
13400Sstevel@tonic-gate return (0);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate static void
set_cmdchar(char * cmdcharstr)13440Sstevel@tonic-gate set_cmdchar(char *cmdcharstr)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate char c;
13470Sstevel@tonic-gate long lc;
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate if ((c = *cmdcharstr) != '\\') {
13500Sstevel@tonic-gate cmdchar = c;
13510Sstevel@tonic-gate return;
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate
13540Sstevel@tonic-gate c = cmdcharstr[1];
13550Sstevel@tonic-gate if (c == '\0' || c == '\\') {
13560Sstevel@tonic-gate cmdchar = '\\';
13570Sstevel@tonic-gate return;
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate
13600Sstevel@tonic-gate if (c < '0' || c > '7') {
13610Sstevel@tonic-gate zerror(gettext("Unrecognized escape character option %s"),
13620Sstevel@tonic-gate cmdcharstr);
13630Sstevel@tonic-gate usage();
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate lc = strtol(cmdcharstr + 1, NULL, 8);
13670Sstevel@tonic-gate if (lc < 0 || lc > 255) {
13680Sstevel@tonic-gate zerror(gettext("Octal escape character '%s' too large"),
13690Sstevel@tonic-gate cmdcharstr);
13700Sstevel@tonic-gate usage();
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate cmdchar = (char)lc;
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate static int
setup_utmpx(char * slavename)13760Sstevel@tonic-gate setup_utmpx(char *slavename)
13770Sstevel@tonic-gate {
13780Sstevel@tonic-gate struct utmpx ut;
13790Sstevel@tonic-gate
13800Sstevel@tonic-gate bzero(&ut, sizeof (ut));
13810Sstevel@tonic-gate (void) strncpy(ut.ut_user, ".zlogin", sizeof (ut.ut_user));
13820Sstevel@tonic-gate (void) strncpy(ut.ut_line, slavename, sizeof (ut.ut_line));
13830Sstevel@tonic-gate ut.ut_pid = getpid();
13840Sstevel@tonic-gate ut.ut_id[0] = 'z';
13850Sstevel@tonic-gate ut.ut_id[1] = ut.ut_id[2] = ut.ut_id[3] = (char)SC_WILDC;
13860Sstevel@tonic-gate ut.ut_type = LOGIN_PROCESS;
13870Sstevel@tonic-gate (void) time(&ut.ut_tv.tv_sec);
13880Sstevel@tonic-gate
13890Sstevel@tonic-gate if (makeutx(&ut) == NULL) {
13900Sstevel@tonic-gate zerror(gettext("makeutx failed"));
13910Sstevel@tonic-gate return (-1);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate return (0);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate static void
release_lock_file(int lockfd)13970Sstevel@tonic-gate release_lock_file(int lockfd)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate (void) close(lockfd);
14000Sstevel@tonic-gate }
14010Sstevel@tonic-gate
14020Sstevel@tonic-gate static int
grab_lock_file(const char * zone_name,int * lockfd)14030Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate char pathbuf[PATH_MAX];
14060Sstevel@tonic-gate struct flock flock;
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
14090Sstevel@tonic-gate zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR,
14100Sstevel@tonic-gate strerror(errno));
14110Sstevel@tonic-gate return (-1);
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU);
14140Sstevel@tonic-gate (void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock",
14150Sstevel@tonic-gate ZONES_TMPDIR, zone_name);
14160Sstevel@tonic-gate
14170Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
14180Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf,
14190Sstevel@tonic-gate strerror(errno));
14200Sstevel@tonic-gate return (-1);
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate /*
14230Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds
14240Sstevel@tonic-gate */
14250Sstevel@tonic-gate flock.l_type = F_WRLCK;
14260Sstevel@tonic-gate flock.l_whence = SEEK_SET;
14270Sstevel@tonic-gate flock.l_start = (off_t)0;
14280Sstevel@tonic-gate flock.l_len = (off_t)0;
14290Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
14300Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf,
14310Sstevel@tonic-gate strerror(errno));
14320Sstevel@tonic-gate release_lock_file(*lockfd);
14330Sstevel@tonic-gate return (-1);
14340Sstevel@tonic-gate }
14350Sstevel@tonic-gate return (Z_OK);
14360Sstevel@tonic-gate }
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate static int
start_zoneadmd(const char * zone_name)14390Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
14400Sstevel@tonic-gate {
14410Sstevel@tonic-gate pid_t retval;
14420Sstevel@tonic-gate int pstatus = 0, error = -1, lockfd, doorfd;
14430Sstevel@tonic-gate struct door_info info;
14440Sstevel@tonic-gate char doorpath[MAXPATHLEN];
14450Sstevel@tonic-gate
14460Sstevel@tonic-gate (void) snprintf(doorpath, sizeof (doorpath), ZONE_DOOR_PATH, zone_name);
14470Sstevel@tonic-gate
14480Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK)
14490Sstevel@tonic-gate return (-1);
14500Sstevel@tonic-gate /*
14510Sstevel@tonic-gate * We must do the door check with the lock held. Otherwise, we
14520Sstevel@tonic-gate * might race against another zoneadm/zlogin process and wind
14530Sstevel@tonic-gate * up with two processes trying to start zoneadmd at the same
14540Sstevel@tonic-gate * time. zoneadmd will detect this, and fail, but we prefer this
14550Sstevel@tonic-gate * to be as seamless as is practical, from a user perspective.
14560Sstevel@tonic-gate */
14570Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
14580Sstevel@tonic-gate if (errno != ENOENT) {
14590Sstevel@tonic-gate zerror("failed to open %s: %s", doorpath,
14600Sstevel@tonic-gate strerror(errno));
14610Sstevel@tonic-gate goto out;
14620Sstevel@tonic-gate }
14630Sstevel@tonic-gate } else {
14640Sstevel@tonic-gate /*
14650Sstevel@tonic-gate * Seems to be working ok.
14660Sstevel@tonic-gate */
14670Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 &&
14680Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) {
14690Sstevel@tonic-gate error = 0;
14700Sstevel@tonic-gate goto out;
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate
14740Sstevel@tonic-gate if ((child_pid = fork()) == -1) {
14750Sstevel@tonic-gate zperror(gettext("could not fork"));
14760Sstevel@tonic-gate goto out;
14770Sstevel@tonic-gate } else if (child_pid == 0) {
14780Sstevel@tonic-gate /* child process */
14790Sstevel@tonic-gate (void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z",
14800Sstevel@tonic-gate zone_name, NULL);
14810Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"));
14820Sstevel@tonic-gate _exit(1);
14830Sstevel@tonic-gate }
14840Sstevel@tonic-gate
14850Sstevel@tonic-gate /* parent process */
14860Sstevel@tonic-gate do {
14870Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0);
14880Sstevel@tonic-gate } while (retval != child_pid);
14890Sstevel@tonic-gate if (WIFSIGNALED(pstatus) ||
14900Sstevel@tonic-gate (WIFEXITED(pstatus) && WEXITSTATUS(pstatus) != 0)) {
14910Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd");
14920Sstevel@tonic-gate goto out;
14930Sstevel@tonic-gate }
14940Sstevel@tonic-gate error = 0;
14950Sstevel@tonic-gate out:
14960Sstevel@tonic-gate release_lock_file(lockfd);
14970Sstevel@tonic-gate (void) close(doorfd);
14980Sstevel@tonic-gate return (error);
14990Sstevel@tonic-gate }
15000Sstevel@tonic-gate
15010Sstevel@tonic-gate static int
init_template(void)15020Sstevel@tonic-gate init_template(void)
15030Sstevel@tonic-gate {
15040Sstevel@tonic-gate int fd;
15050Sstevel@tonic-gate int err = 0;
15060Sstevel@tonic-gate
15070Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR);
15080Sstevel@tonic-gate if (fd == -1)
15090Sstevel@tonic-gate return (-1);
15100Sstevel@tonic-gate
15110Sstevel@tonic-gate /*
15120Sstevel@tonic-gate * zlogin doesn't do anything with the contract.
15130Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned.
15140Sstevel@tonic-gate */
15150Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0);
15160Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0);
15170Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
15180Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
15190Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) {
15200Sstevel@tonic-gate (void) close(fd);
15210Sstevel@tonic-gate return (-1);
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate
15240Sstevel@tonic-gate return (fd);
15250Sstevel@tonic-gate }
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate static int
noninteractive_login(char * zonename,const char * user_cmd,zoneid_t zoneid,char ** new_args,char ** new_env)15284344Ssl108498 noninteractive_login(char *zonename, const char *user_cmd, zoneid_t zoneid,
15290Sstevel@tonic-gate char **new_args, char **new_env)
15300Sstevel@tonic-gate {
15310Sstevel@tonic-gate pid_t retval;
15327089Sgjelinek int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2], dead_child_pipe[2];
15330Sstevel@tonic-gate int child_status;
15340Sstevel@tonic-gate int tmpl_fd;
15350Sstevel@tonic-gate sigset_t block_cld;
15360Sstevel@tonic-gate
15370Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) {
15380Sstevel@tonic-gate reset_tty();
15390Sstevel@tonic-gate zperror(gettext("could not create contract"));
15400Sstevel@tonic-gate return (1);
15410Sstevel@tonic-gate }
15420Sstevel@tonic-gate
15430Sstevel@tonic-gate if (pipe(stdin_pipe) != 0) {
15440Sstevel@tonic-gate zperror(gettext("could not create STDIN pipe"));
15450Sstevel@tonic-gate return (1);
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate /*
15480Sstevel@tonic-gate * When the user types ^D, we get a zero length message on STDIN.
15490Sstevel@tonic-gate * We need to echo that down the pipe to send it to the other side;
15500Sstevel@tonic-gate * but by default, pipes don't propagate zero-length messages. We
15510Sstevel@tonic-gate * toggle that behavior off using I_SWROPT. See streamio(7i).
15520Sstevel@tonic-gate */
15530Sstevel@tonic-gate if (ioctl(stdin_pipe[0], I_SWROPT, SNDZERO) != 0) {
15540Sstevel@tonic-gate zperror(gettext("could not configure STDIN pipe"));
15550Sstevel@tonic-gate return (1);
15560Sstevel@tonic-gate
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate if (pipe(stdout_pipe) != 0) {
15590Sstevel@tonic-gate zperror(gettext("could not create STDOUT pipe"));
15600Sstevel@tonic-gate return (1);
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate if (pipe(stderr_pipe) != 0) {
15630Sstevel@tonic-gate zperror(gettext("could not create STDERR pipe"));
15640Sstevel@tonic-gate return (1);
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate
15677089Sgjelinek if (pipe(dead_child_pipe) != 0) {
15687089Sgjelinek zperror(gettext("could not create signalling pipe"));
15697089Sgjelinek return (1);
15707089Sgjelinek }
15717089Sgjelinek close_on_sig = dead_child_pipe[0];
15727089Sgjelinek
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate * If any of the pipe FD's winds up being less than STDERR, then we
15750Sstevel@tonic-gate * have a mess on our hands-- and we are lacking some of the I/O
15760Sstevel@tonic-gate * streams we would expect anyway. So we bail.
15770Sstevel@tonic-gate */
15780Sstevel@tonic-gate if (stdin_pipe[0] <= STDERR_FILENO ||
15790Sstevel@tonic-gate stdin_pipe[1] <= STDERR_FILENO ||
15800Sstevel@tonic-gate stdout_pipe[0] <= STDERR_FILENO ||
15810Sstevel@tonic-gate stdout_pipe[1] <= STDERR_FILENO ||
15820Sstevel@tonic-gate stderr_pipe[0] <= STDERR_FILENO ||
15837089Sgjelinek stderr_pipe[1] <= STDERR_FILENO ||
15847089Sgjelinek dead_child_pipe[0] <= STDERR_FILENO ||
15857089Sgjelinek dead_child_pipe[1] <= STDERR_FILENO) {
15860Sstevel@tonic-gate zperror(gettext("process lacks valid STDIN, STDOUT, STDERR"));
15870Sstevel@tonic-gate return (1);
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate
15900Sstevel@tonic-gate if (prefork_dropprivs() != 0) {
15910Sstevel@tonic-gate zperror(gettext("could not allocate privilege set"));
15920Sstevel@tonic-gate return (1);
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate
15950Sstevel@tonic-gate (void) sigset(SIGCLD, sigcld);
15960Sstevel@tonic-gate (void) sigemptyset(&block_cld);
15970Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCLD);
15980Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate if ((child_pid = fork()) == -1) {
16010Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
16020Sstevel@tonic-gate (void) close(tmpl_fd);
16030Sstevel@tonic-gate zperror(gettext("could not fork"));
16040Sstevel@tonic-gate return (1);
16050Sstevel@tonic-gate } else if (child_pid == 0) { /* child process */
16060Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
16070Sstevel@tonic-gate
16080Sstevel@tonic-gate /*
16090Sstevel@tonic-gate * Do a dance to get the pipes hooked up as FD's 0, 1 and 2.
16100Sstevel@tonic-gate */
16110Sstevel@tonic-gate (void) close(STDIN_FILENO);
16120Sstevel@tonic-gate (void) close(STDOUT_FILENO);
16130Sstevel@tonic-gate (void) close(STDERR_FILENO);
16140Sstevel@tonic-gate (void) dup2(stdin_pipe[1], STDIN_FILENO);
16150Sstevel@tonic-gate (void) dup2(stdout_pipe[1], STDOUT_FILENO);
16160Sstevel@tonic-gate (void) dup2(stderr_pipe[1], STDERR_FILENO);
16170Sstevel@tonic-gate (void) closefrom(STDERR_FILENO + 1);
16180Sstevel@tonic-gate
16190Sstevel@tonic-gate (void) sigset(SIGCLD, SIG_DFL);
16200Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
16210Sstevel@tonic-gate /*
16220Sstevel@tonic-gate * In case any of stdin, stdout or stderr are streams,
16230Sstevel@tonic-gate * anchor them to prevent malicious I_POPs.
16240Sstevel@tonic-gate */
16250Sstevel@tonic-gate (void) ioctl(STDIN_FILENO, I_ANCHOR);
16260Sstevel@tonic-gate (void) ioctl(STDOUT_FILENO, I_ANCHOR);
16270Sstevel@tonic-gate (void) ioctl(STDERR_FILENO, I_ANCHOR);
16280Sstevel@tonic-gate
16290Sstevel@tonic-gate if (zone_enter(zoneid) == -1) {
16300Sstevel@tonic-gate zerror(gettext("could not enter zone %s: %s"),
16310Sstevel@tonic-gate zonename, strerror(errno));
16320Sstevel@tonic-gate _exit(1);
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate
16354344Ssl108498 /*
16364344Ssl108498 * For non-native zones, tell libc where it can find locale
16374344Ssl108498 * specific getttext() messages.
16384344Ssl108498 */
16394586Sgjelinek if (access("/.SUNWnative/usr/lib/locale", R_OK) == 0)
16404586Sgjelinek (void) bindtextdomain(TEXT_DOMAIN,
16414586Sgjelinek "/.SUNWnative/usr/lib/locale");
16424586Sgjelinek else if (access("/native/usr/lib/locale", R_OK) == 0)
16434344Ssl108498 (void) bindtextdomain(TEXT_DOMAIN,
16444344Ssl108498 "/native/usr/lib/locale");
16454344Ssl108498
16460Sstevel@tonic-gate if (!failsafe)
16474344Ssl108498 new_env = prep_env_noninteractive(user_cmd, new_env);
16480Sstevel@tonic-gate
16490Sstevel@tonic-gate if (new_env == NULL) {
16500Sstevel@tonic-gate _exit(1);
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate
16530Sstevel@tonic-gate /*
16540Sstevel@tonic-gate * Move into a new process group; the zone_enter will have
16550Sstevel@tonic-gate * placed us into zsched's session, and we want to be in
16560Sstevel@tonic-gate * a unique process group.
16570Sstevel@tonic-gate */
16580Sstevel@tonic-gate (void) setpgid(getpid(), getpid());
16590Sstevel@tonic-gate
166012578SGlenn.Faden@Sun.COM /*
166112578SGlenn.Faden@Sun.COM * The child needs to run as root to
166212578SGlenn.Faden@Sun.COM * execute the su program.
166312578SGlenn.Faden@Sun.COM */
166412578SGlenn.Faden@Sun.COM if (setuid(0) == -1) {
166512578SGlenn.Faden@Sun.COM zperror(gettext("insufficient privilege"));
166612578SGlenn.Faden@Sun.COM return (1);
166712578SGlenn.Faden@Sun.COM }
166812578SGlenn.Faden@Sun.COM
16690Sstevel@tonic-gate (void) execve(new_args[0], new_args, new_env);
16700Sstevel@tonic-gate zperror(gettext("exec failure"));
16710Sstevel@tonic-gate _exit(1);
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate /* parent */
16747089Sgjelinek
16757089Sgjelinek /* close pipe sides written by child */
16767089Sgjelinek (void) close(stdout_pipe[1]);
16777089Sgjelinek (void) close(stderr_pipe[1]);
16787089Sgjelinek
16790Sstevel@tonic-gate (void) sigset(SIGINT, sig_forward);
16800Sstevel@tonic-gate
16810Sstevel@tonic-gate postfork_dropprivs();
16820Sstevel@tonic-gate
16830Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
16840Sstevel@tonic-gate (void) close(tmpl_fd);
16850Sstevel@tonic-gate
16860Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
16873789Sgjelinek doio(stdin_pipe[0], stdin_pipe[1], stdout_pipe[0], stderr_pipe[0],
16887089Sgjelinek dead_child_pipe[1], B_TRUE);
16890Sstevel@tonic-gate do {
16900Sstevel@tonic-gate retval = waitpid(child_pid, &child_status, 0);
16910Sstevel@tonic-gate if (retval == -1) {
16920Sstevel@tonic-gate child_status = 0;
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate } while (retval != child_pid && errno != ECHILD);
16950Sstevel@tonic-gate
16960Sstevel@tonic-gate return (WEXITSTATUS(child_status));
16970Sstevel@tonic-gate }
16980Sstevel@tonic-gate
169912578SGlenn.Faden@Sun.COM static char *
get_username()170012578SGlenn.Faden@Sun.COM get_username()
170112578SGlenn.Faden@Sun.COM {
170212578SGlenn.Faden@Sun.COM uid_t uid;
170312578SGlenn.Faden@Sun.COM struct passwd *nptr;
170412578SGlenn.Faden@Sun.COM
170512578SGlenn.Faden@Sun.COM /*
170612578SGlenn.Faden@Sun.COM * Authorizations are checked to restrict access based on the
170712578SGlenn.Faden@Sun.COM * requested operation and zone name, It is assumed that the
170812578SGlenn.Faden@Sun.COM * program is running with all privileges, but that the real
170912578SGlenn.Faden@Sun.COM * user ID is that of the user or role on whose behalf we are
171012578SGlenn.Faden@Sun.COM * operating. So we start by getting the username that will be
171112578SGlenn.Faden@Sun.COM * used for subsequent authorization checks.
171212578SGlenn.Faden@Sun.COM */
171312578SGlenn.Faden@Sun.COM
171412578SGlenn.Faden@Sun.COM uid = getuid();
171512578SGlenn.Faden@Sun.COM if ((nptr = getpwuid(uid)) == NULL) {
171612578SGlenn.Faden@Sun.COM zerror(gettext("could not get user name."));
171712578SGlenn.Faden@Sun.COM _exit(1);
171812578SGlenn.Faden@Sun.COM }
171912578SGlenn.Faden@Sun.COM return (nptr->pw_name);
172012578SGlenn.Faden@Sun.COM }
172112578SGlenn.Faden@Sun.COM
17220Sstevel@tonic-gate int
main(int argc,char ** argv)17230Sstevel@tonic-gate main(int argc, char **argv)
17240Sstevel@tonic-gate {
17250Sstevel@tonic-gate int arg, console = 0;
17260Sstevel@tonic-gate zoneid_t zoneid;
17270Sstevel@tonic-gate zone_state_t st;
17280Sstevel@tonic-gate char *login = "root";
17290Sstevel@tonic-gate int lflag = 0;
17300Sstevel@tonic-gate char *zonename = NULL;
17310Sstevel@tonic-gate char **proc_args = NULL;
17320Sstevel@tonic-gate char **new_args, **new_env;
17330Sstevel@tonic-gate sigset_t block_cld;
17342621Sllai1 char devroot[MAXPATHLEN];
17350Sstevel@tonic-gate char *slavename, slaveshortname[MAXPATHLEN];
17360Sstevel@tonic-gate priv_set_t *privset;
17370Sstevel@tonic-gate int tmpl_fd;
17382712Snn35248 char zonebrand[MAXNAMELEN];
173910943SEdward.Pilatowicz@Sun.COM char default_brand[MAXNAMELEN];
1740766Scarlsonj struct stat sb;
1741766Scarlsonj char kernzone[ZONENAME_MAX];
17422727Sedp brand_handle_t bh;
17434344Ssl108498 char user_cmd[MAXPATHLEN];
174412578SGlenn.Faden@Sun.COM char authname[MAXAUTHS];
17450Sstevel@tonic-gate
17460Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
17470Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
17480Sstevel@tonic-gate
17490Sstevel@tonic-gate (void) getpname(argv[0]);
175012578SGlenn.Faden@Sun.COM username = get_username();
17510Sstevel@tonic-gate
1752766Scarlsonj while ((arg = getopt(argc, argv, "ECR:Se:l:")) != EOF) {
17530Sstevel@tonic-gate switch (arg) {
17540Sstevel@tonic-gate case 'C':
17550Sstevel@tonic-gate console = 1;
17560Sstevel@tonic-gate break;
17570Sstevel@tonic-gate case 'E':
17580Sstevel@tonic-gate nocmdchar = 1;
17590Sstevel@tonic-gate break;
1760766Scarlsonj case 'R': /* undocumented */
1761766Scarlsonj if (*optarg != '/') {
1762766Scarlsonj zerror(gettext("root path must be absolute."));
1763766Scarlsonj exit(2);
1764766Scarlsonj }
1765766Scarlsonj if (stat(optarg, &sb) == -1 || !S_ISDIR(sb.st_mode)) {
1766766Scarlsonj zerror(
1767766Scarlsonj gettext("root path must be a directory."));
1768766Scarlsonj exit(2);
1769766Scarlsonj }
1770766Scarlsonj zonecfg_set_root(optarg);
1771766Scarlsonj break;
17720Sstevel@tonic-gate case 'S':
17730Sstevel@tonic-gate failsafe = 1;
17740Sstevel@tonic-gate break;
17750Sstevel@tonic-gate case 'e':
17760Sstevel@tonic-gate set_cmdchar(optarg);
17770Sstevel@tonic-gate break;
17780Sstevel@tonic-gate case 'l':
17790Sstevel@tonic-gate login = optarg;
17800Sstevel@tonic-gate lflag = 1;
17810Sstevel@tonic-gate break;
17820Sstevel@tonic-gate default:
17830Sstevel@tonic-gate usage();
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate
17870Sstevel@tonic-gate if (console != 0 && lflag != 0) {
17880Sstevel@tonic-gate zerror(gettext("-l may not be specified for console login"));
17890Sstevel@tonic-gate usage();
17900Sstevel@tonic-gate }
17910Sstevel@tonic-gate
17920Sstevel@tonic-gate if (console != 0 && failsafe != 0) {
17930Sstevel@tonic-gate zerror(gettext("-S may not be specified for console login"));
17940Sstevel@tonic-gate usage();
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate
1797766Scarlsonj if (console != 0 && zonecfg_in_alt_root()) {
1798766Scarlsonj zerror(gettext("-R may not be specified for console login"));
1799766Scarlsonj exit(2);
1800766Scarlsonj }
1801766Scarlsonj
18020Sstevel@tonic-gate if (failsafe != 0 && lflag != 0) {
18030Sstevel@tonic-gate zerror(gettext("-l may not be specified for failsafe login"));
18040Sstevel@tonic-gate usage();
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate
18070Sstevel@tonic-gate if (optind == (argc - 1)) {
18080Sstevel@tonic-gate /*
18090Sstevel@tonic-gate * zone name, no process name; this should be an interactive
18100Sstevel@tonic-gate * as long as STDIN is really a tty.
18110Sstevel@tonic-gate */
18120Sstevel@tonic-gate if (isatty(STDIN_FILENO))
18130Sstevel@tonic-gate interactive = 1;
18140Sstevel@tonic-gate zonename = argv[optind];
18150Sstevel@tonic-gate } else if (optind < (argc - 1)) {
18160Sstevel@tonic-gate if (console) {
18170Sstevel@tonic-gate zerror(gettext("Commands may not be specified for "
18180Sstevel@tonic-gate "console login."));
18190Sstevel@tonic-gate usage();
18200Sstevel@tonic-gate }
18210Sstevel@tonic-gate /* zone name and process name, and possibly some args */
18220Sstevel@tonic-gate zonename = argv[optind];
18230Sstevel@tonic-gate proc_args = &argv[optind + 1];
18240Sstevel@tonic-gate interactive = 0;
18250Sstevel@tonic-gate } else {
18260Sstevel@tonic-gate usage();
18270Sstevel@tonic-gate }
18280Sstevel@tonic-gate
18290Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) {
18300Sstevel@tonic-gate zerror(gettext("'%s' may only be used from the global zone"),
18310Sstevel@tonic-gate pname);
18320Sstevel@tonic-gate return (1);
18330Sstevel@tonic-gate }
18340Sstevel@tonic-gate
18350Sstevel@tonic-gate if (strcmp(zonename, GLOBAL_ZONENAME) == 0) {
18360Sstevel@tonic-gate zerror(gettext("'%s' not applicable to the global zone"),
18370Sstevel@tonic-gate pname);
18380Sstevel@tonic-gate return (1);
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate if (zone_get_state(zonename, &st) != Z_OK) {
18420Sstevel@tonic-gate zerror(gettext("zone '%s' unknown"), zonename);
18430Sstevel@tonic-gate return (1);
18440Sstevel@tonic-gate }
18450Sstevel@tonic-gate
18460Sstevel@tonic-gate if (st < ZONE_STATE_INSTALLED) {
18470Sstevel@tonic-gate zerror(gettext("cannot login to a zone which is '%s'"),
18480Sstevel@tonic-gate zone_state_str(st));
18490Sstevel@tonic-gate return (1);
18500Sstevel@tonic-gate }
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate /*
18530Sstevel@tonic-gate * In both console and non-console cases, we require all privs.
18540Sstevel@tonic-gate * In the console case, because we may need to startup zoneadmd.
18550Sstevel@tonic-gate * In the non-console case in order to do zone_enter(2), zonept()
18560Sstevel@tonic-gate * and other tasks.
18570Sstevel@tonic-gate */
185812578SGlenn.Faden@Sun.COM
18590Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) {
18600Sstevel@tonic-gate zperror(gettext("priv_allocset failed"));
18610Sstevel@tonic-gate return (1);
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate
18640Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
18650Sstevel@tonic-gate zperror(gettext("getppriv failed"));
18660Sstevel@tonic-gate priv_freeset(privset);
18670Sstevel@tonic-gate return (1);
18680Sstevel@tonic-gate }
18690Sstevel@tonic-gate
18700Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) {
18710Sstevel@tonic-gate zerror(gettext("You lack sufficient privilege to run "
18720Sstevel@tonic-gate "this command (all privs required)"));
18730Sstevel@tonic-gate priv_freeset(privset);
18740Sstevel@tonic-gate return (1);
18750Sstevel@tonic-gate }
18760Sstevel@tonic-gate priv_freeset(privset);
18770Sstevel@tonic-gate
18780Sstevel@tonic-gate /*
187912578SGlenn.Faden@Sun.COM * Check if user is authorized for requested usage of the zone
188012578SGlenn.Faden@Sun.COM */
188112578SGlenn.Faden@Sun.COM
188212578SGlenn.Faden@Sun.COM (void) snprintf(authname, MAXAUTHS, "%s%s%s",
188312578SGlenn.Faden@Sun.COM ZONE_MANAGE_AUTH, KV_OBJECT, zonename);
188412578SGlenn.Faden@Sun.COM if (chkauthattr(authname, username) == 0) {
188512578SGlenn.Faden@Sun.COM if (console) {
188612578SGlenn.Faden@Sun.COM zerror(gettext("%s is not authorized for console "
188712578SGlenn.Faden@Sun.COM "access to %s zone."),
188812578SGlenn.Faden@Sun.COM username, zonename);
188912578SGlenn.Faden@Sun.COM return (1);
189012578SGlenn.Faden@Sun.COM } else {
189112578SGlenn.Faden@Sun.COM (void) snprintf(authname, MAXAUTHS, "%s%s%s",
189212578SGlenn.Faden@Sun.COM ZONE_LOGIN_AUTH, KV_OBJECT, zonename);
189312578SGlenn.Faden@Sun.COM if (failsafe || !interactive) {
189412578SGlenn.Faden@Sun.COM zerror(gettext("%s is not authorized for "
189512578SGlenn.Faden@Sun.COM "failsafe or non-interactive login "
189612578SGlenn.Faden@Sun.COM "to %s zone."), username, zonename);
189712578SGlenn.Faden@Sun.COM return (1);
189812578SGlenn.Faden@Sun.COM } else if (chkauthattr(authname, username) == 0) {
189912578SGlenn.Faden@Sun.COM zerror(gettext("%s is not authorized "
190012578SGlenn.Faden@Sun.COM " to login to %s zone."),
190112578SGlenn.Faden@Sun.COM username, zonename);
190212578SGlenn.Faden@Sun.COM return (1);
190312578SGlenn.Faden@Sun.COM }
190412578SGlenn.Faden@Sun.COM }
190512578SGlenn.Faden@Sun.COM } else {
190612578SGlenn.Faden@Sun.COM forced_login = B_TRUE;
190712578SGlenn.Faden@Sun.COM }
190812578SGlenn.Faden@Sun.COM
190912578SGlenn.Faden@Sun.COM /*
19100Sstevel@tonic-gate * The console is a separate case from the rest of the code; handle
19110Sstevel@tonic-gate * it first.
19120Sstevel@tonic-gate */
19130Sstevel@tonic-gate if (console) {
19140Sstevel@tonic-gate /*
19150Sstevel@tonic-gate * Ensure that zoneadmd for this zone is running.
19160Sstevel@tonic-gate */
19170Sstevel@tonic-gate if (start_zoneadmd(zonename) == -1)
19180Sstevel@tonic-gate return (1);
19190Sstevel@tonic-gate
19200Sstevel@tonic-gate /*
19210Sstevel@tonic-gate * Make contact with zoneadmd.
19220Sstevel@tonic-gate */
19230Sstevel@tonic-gate if (get_console_master(zonename) == -1)
19240Sstevel@tonic-gate return (1);
19250Sstevel@tonic-gate
19260Sstevel@tonic-gate (void) printf(gettext("[Connected to zone '%s' console]\n"),
19270Sstevel@tonic-gate zonename);
19280Sstevel@tonic-gate
19290Sstevel@tonic-gate if (set_tty_rawmode(STDIN_FILENO) == -1) {
19300Sstevel@tonic-gate reset_tty();
19310Sstevel@tonic-gate zperror(gettext("failed to set stdin pty to raw mode"));
19320Sstevel@tonic-gate return (1);
19330Sstevel@tonic-gate }
19340Sstevel@tonic-gate
19350Sstevel@tonic-gate (void) sigset(SIGWINCH, sigwinch);
19360Sstevel@tonic-gate (void) sigwinch(0);
19370Sstevel@tonic-gate
19380Sstevel@tonic-gate /*
19390Sstevel@tonic-gate * Run the I/O loop until we get disconnected.
19400Sstevel@tonic-gate */
19417089Sgjelinek doio(masterfd, -1, masterfd, -1, -1, B_FALSE);
19420Sstevel@tonic-gate reset_tty();
19430Sstevel@tonic-gate (void) printf(gettext("\n[Connection to zone '%s' console "
19440Sstevel@tonic-gate "closed]\n"), zonename);
19450Sstevel@tonic-gate
19460Sstevel@tonic-gate return (0);
19470Sstevel@tonic-gate }
19480Sstevel@tonic-gate
1949766Scarlsonj if (st != ZONE_STATE_RUNNING && st != ZONE_STATE_MOUNTED) {
19500Sstevel@tonic-gate zerror(gettext("login allowed only to running zones "
19510Sstevel@tonic-gate "(%s is '%s')."), zonename, zone_state_str(st));
19520Sstevel@tonic-gate return (1);
19530Sstevel@tonic-gate }
19540Sstevel@tonic-gate
1955766Scarlsonj (void) strlcpy(kernzone, zonename, sizeof (kernzone));
1956766Scarlsonj if (zonecfg_in_alt_root()) {
1957766Scarlsonj FILE *fp = zonecfg_open_scratch("", B_FALSE);
1958766Scarlsonj
1959766Scarlsonj if (fp == NULL || zonecfg_find_scratch(fp, zonename,
1960766Scarlsonj zonecfg_get_root(), kernzone, sizeof (kernzone)) == -1) {
1961766Scarlsonj zerror(gettext("cannot find scratch zone %s"),
1962766Scarlsonj zonename);
1963766Scarlsonj if (fp != NULL)
1964766Scarlsonj zonecfg_close_scratch(fp);
1965766Scarlsonj return (1);
1966766Scarlsonj }
1967766Scarlsonj zonecfg_close_scratch(fp);
1968766Scarlsonj }
1969766Scarlsonj
1970766Scarlsonj if ((zoneid = getzoneidbyname(kernzone)) == -1) {
19710Sstevel@tonic-gate zerror(gettext("failed to get zoneid for zone '%s'"),
19720Sstevel@tonic-gate zonename);
19730Sstevel@tonic-gate return (1);
19740Sstevel@tonic-gate }
19750Sstevel@tonic-gate
1976766Scarlsonj /*
19772621Sllai1 * We need the zone root path only if we are setting up a pty.
1978766Scarlsonj */
19792621Sllai1 if (zone_get_devroot(zonename, devroot, sizeof (devroot)) == -1) {
19802621Sllai1 zerror(gettext("could not get dev path for zone %s"),
19810Sstevel@tonic-gate zonename);
19820Sstevel@tonic-gate return (1);
19830Sstevel@tonic-gate }
19840Sstevel@tonic-gate
198510796SStephen.Lawrence@Sun.COM if (zone_get_brand(zonename, zonebrand, sizeof (zonebrand)) != Z_OK) {
19862712Snn35248 zerror(gettext("could not get brand for zone %s"), zonename);
19870Sstevel@tonic-gate return (1);
19880Sstevel@tonic-gate }
198910796SStephen.Lawrence@Sun.COM /*
199010796SStephen.Lawrence@Sun.COM * In the alternate root environment, the only supported
199110796SStephen.Lawrence@Sun.COM * operations are mount and unmount. In this case, just treat
199210796SStephen.Lawrence@Sun.COM * the zone as native if it is cluster. Cluster zones can be
199310796SStephen.Lawrence@Sun.COM * native for the purpose of LU or upgrade, and the cluster
199410796SStephen.Lawrence@Sun.COM * brand may not exist in the miniroot (such as in net install
199510796SStephen.Lawrence@Sun.COM * upgrade).
199610796SStephen.Lawrence@Sun.COM */
199710943SEdward.Pilatowicz@Sun.COM if (zonecfg_default_brand(default_brand,
199810943SEdward.Pilatowicz@Sun.COM sizeof (default_brand)) != Z_OK) {
199910943SEdward.Pilatowicz@Sun.COM zerror(gettext("unable to determine default brand"));
200010943SEdward.Pilatowicz@Sun.COM return (1);
200110943SEdward.Pilatowicz@Sun.COM }
200210796SStephen.Lawrence@Sun.COM if (zonecfg_in_alt_root() &&
200310796SStephen.Lawrence@Sun.COM strcmp(zonebrand, CLUSTER_BRAND_NAME) == 0) {
200410943SEdward.Pilatowicz@Sun.COM (void) strlcpy(zonebrand, default_brand, sizeof (zonebrand));
200510796SStephen.Lawrence@Sun.COM }
200610943SEdward.Pilatowicz@Sun.COM
200710796SStephen.Lawrence@Sun.COM if ((bh = brand_open(zonebrand)) == NULL) {
200810796SStephen.Lawrence@Sun.COM zerror(gettext("could not open brand for zone %s"), zonename);
200910796SStephen.Lawrence@Sun.COM return (1);
201010796SStephen.Lawrence@Sun.COM }
201110796SStephen.Lawrence@Sun.COM
20122727Sedp if ((new_args = prep_args(bh, login, proc_args)) == NULL) {
20132712Snn35248 zperror(gettext("could not assemble new arguments"));
20142727Sedp brand_close(bh);
20152712Snn35248 return (1);
20162712Snn35248 }
20174344Ssl108498 /*
20184344Ssl108498 * Get the brand specific user_cmd. This command is used to get
20194344Ssl108498 * a passwd(4) entry for login.
20204344Ssl108498 */
20214344Ssl108498 if (!interactive && !failsafe) {
20224344Ssl108498 if (zone_get_user_cmd(bh, login, user_cmd,
20234344Ssl108498 sizeof (user_cmd)) == NULL) {
20244344Ssl108498 zerror(gettext("could not get user_cmd for zone %s"),
20254344Ssl108498 zonename);
20264344Ssl108498 brand_close(bh);
20274344Ssl108498 return (1);
20284344Ssl108498 }
20294344Ssl108498 }
20302727Sedp brand_close(bh);
20310Sstevel@tonic-gate
20320Sstevel@tonic-gate if ((new_env = prep_env()) == NULL) {
20330Sstevel@tonic-gate zperror(gettext("could not assemble new environment"));
20340Sstevel@tonic-gate return (1);
20350Sstevel@tonic-gate }
20360Sstevel@tonic-gate
20370Sstevel@tonic-gate if (!interactive)
20384344Ssl108498 return (noninteractive_login(zonename, user_cmd, zoneid,
20394344Ssl108498 new_args, new_env));
20400Sstevel@tonic-gate
2041766Scarlsonj if (zonecfg_in_alt_root()) {
2042766Scarlsonj zerror(gettext("cannot use interactive login with scratch "
2043766Scarlsonj "zone"));
2044766Scarlsonj return (1);
2045766Scarlsonj }
2046766Scarlsonj
20470Sstevel@tonic-gate /*
20480Sstevel@tonic-gate * Things are more complex in interactive mode; we get the
20490Sstevel@tonic-gate * master side of the pty, then place the user's terminal into
20500Sstevel@tonic-gate * raw mode.
20510Sstevel@tonic-gate */
20520Sstevel@tonic-gate if (get_master_pty() == -1) {
20530Sstevel@tonic-gate zerror(gettext("could not setup master pty device"));
20540Sstevel@tonic-gate return (1);
20550Sstevel@tonic-gate }
20560Sstevel@tonic-gate
20570Sstevel@tonic-gate /*
20580Sstevel@tonic-gate * Compute the "short name" of the pts. /dev/pts/2 --> pts/2
20590Sstevel@tonic-gate */
20600Sstevel@tonic-gate if ((slavename = ptsname(masterfd)) == NULL) {
20610Sstevel@tonic-gate zperror(gettext("failed to get name for pseudo-tty"));
20620Sstevel@tonic-gate return (1);
20630Sstevel@tonic-gate }
20640Sstevel@tonic-gate if (strncmp(slavename, "/dev/", strlen("/dev/")) == 0)
20650Sstevel@tonic-gate (void) strlcpy(slaveshortname, slavename + strlen("/dev/"),
20660Sstevel@tonic-gate sizeof (slaveshortname));
20670Sstevel@tonic-gate else
20680Sstevel@tonic-gate (void) strlcpy(slaveshortname, slavename,
20690Sstevel@tonic-gate sizeof (slaveshortname));
20700Sstevel@tonic-gate
20710Sstevel@tonic-gate (void) printf(gettext("[Connected to zone '%s' %s]\n"), zonename,
20720Sstevel@tonic-gate slaveshortname);
20730Sstevel@tonic-gate
20740Sstevel@tonic-gate if (set_tty_rawmode(STDIN_FILENO) == -1) {
20750Sstevel@tonic-gate reset_tty();
20760Sstevel@tonic-gate zperror(gettext("failed to set stdin pty to raw mode"));
20770Sstevel@tonic-gate return (1);
20780Sstevel@tonic-gate }
20790Sstevel@tonic-gate
20800Sstevel@tonic-gate if (prefork_dropprivs() != 0) {
20810Sstevel@tonic-gate reset_tty();
20820Sstevel@tonic-gate zperror(gettext("could not allocate privilege set"));
20830Sstevel@tonic-gate return (1);
20840Sstevel@tonic-gate }
20850Sstevel@tonic-gate
20860Sstevel@tonic-gate /*
20870Sstevel@tonic-gate * We must mask SIGCLD until after we have coped with the fork
20880Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and receive the
20890Sstevel@tonic-gate * signal before child_pid has been initialized (yes, this really
20900Sstevel@tonic-gate * happens).
20910Sstevel@tonic-gate */
20920Sstevel@tonic-gate (void) sigset(SIGCLD, sigcld);
20930Sstevel@tonic-gate (void) sigemptyset(&block_cld);
20940Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCLD);
20950Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
20960Sstevel@tonic-gate
20970Sstevel@tonic-gate /*
20980Sstevel@tonic-gate * We activate the contract template at the last minute to
20990Sstevel@tonic-gate * avoid intermediate functions that could be using fork(2)
21000Sstevel@tonic-gate * internally.
21010Sstevel@tonic-gate */
21020Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) {
21030Sstevel@tonic-gate reset_tty();
21040Sstevel@tonic-gate zperror(gettext("could not create contract"));
21050Sstevel@tonic-gate return (1);
21060Sstevel@tonic-gate }
21070Sstevel@tonic-gate
21080Sstevel@tonic-gate if ((child_pid = fork()) == -1) {
21090Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
21100Sstevel@tonic-gate reset_tty();
21110Sstevel@tonic-gate zperror(gettext("could not fork"));
21120Sstevel@tonic-gate return (1);
21130Sstevel@tonic-gate } else if (child_pid == 0) { /* child process */
21140Sstevel@tonic-gate int slavefd, newslave;
21150Sstevel@tonic-gate
21160Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
21170Sstevel@tonic-gate (void) close(tmpl_fd);
21180Sstevel@tonic-gate
21190Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
21200Sstevel@tonic-gate
21212621Sllai1 if ((slavefd = init_slave_pty(zoneid, devroot)) == -1)
21220Sstevel@tonic-gate return (1);
21230Sstevel@tonic-gate
21240Sstevel@tonic-gate /*
21250Sstevel@tonic-gate * Close all fds except for the slave pty.
21260Sstevel@tonic-gate */
21270Sstevel@tonic-gate (void) fdwalk(close_func, &slavefd);
21280Sstevel@tonic-gate
21290Sstevel@tonic-gate /*
21300Sstevel@tonic-gate * Temporarily dup slavefd to stderr; that way if we have
21310Sstevel@tonic-gate * to print out that zone_enter failed, the output will
21320Sstevel@tonic-gate * have somewhere to go.
21330Sstevel@tonic-gate */
21340Sstevel@tonic-gate if (slavefd != STDERR_FILENO)
21350Sstevel@tonic-gate (void) dup2(slavefd, STDERR_FILENO);
21360Sstevel@tonic-gate
21370Sstevel@tonic-gate if (zone_enter(zoneid) == -1) {
21380Sstevel@tonic-gate zerror(gettext("could not enter zone %s: %s"),
21390Sstevel@tonic-gate zonename, strerror(errno));
21400Sstevel@tonic-gate return (1);
21410Sstevel@tonic-gate }
21420Sstevel@tonic-gate
21430Sstevel@tonic-gate if (slavefd != STDERR_FILENO)
21440Sstevel@tonic-gate (void) close(STDERR_FILENO);
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate /*
21470Sstevel@tonic-gate * We take pains to get this process into a new process
21480Sstevel@tonic-gate * group, and subsequently a new session. In this way,
21490Sstevel@tonic-gate * we'll have a session which doesn't yet have a controlling
21500Sstevel@tonic-gate * terminal. When we open the slave, it will become the
21510Sstevel@tonic-gate * controlling terminal; no PIDs concerning pgrps or sids
21520Sstevel@tonic-gate * will leak inappropriately into the zone.
21530Sstevel@tonic-gate */
21540Sstevel@tonic-gate (void) setpgrp();
21550Sstevel@tonic-gate
21560Sstevel@tonic-gate /*
21570Sstevel@tonic-gate * We need the slave pty to be referenced from the zone's
21580Sstevel@tonic-gate * /dev in order to ensure that the devt's, etc are all
21590Sstevel@tonic-gate * correct. Otherwise we break ttyname and the like.
21600Sstevel@tonic-gate */
21610Sstevel@tonic-gate if ((newslave = open(slavename, O_RDWR)) == -1) {
21620Sstevel@tonic-gate (void) close(slavefd);
21630Sstevel@tonic-gate return (1);
21640Sstevel@tonic-gate }
21650Sstevel@tonic-gate (void) close(slavefd);
21660Sstevel@tonic-gate slavefd = newslave;
21670Sstevel@tonic-gate
21680Sstevel@tonic-gate /*
21690Sstevel@tonic-gate * dup the slave to the various FDs, so that when the
21700Sstevel@tonic-gate * spawned process does a write/read it maps to the slave
21710Sstevel@tonic-gate * pty.
21720Sstevel@tonic-gate */
21730Sstevel@tonic-gate (void) dup2(slavefd, STDIN_FILENO);
21740Sstevel@tonic-gate (void) dup2(slavefd, STDOUT_FILENO);
21750Sstevel@tonic-gate (void) dup2(slavefd, STDERR_FILENO);
21760Sstevel@tonic-gate if (slavefd != STDIN_FILENO && slavefd != STDOUT_FILENO &&
21770Sstevel@tonic-gate slavefd != STDERR_FILENO) {
21780Sstevel@tonic-gate (void) close(slavefd);
21790Sstevel@tonic-gate }
21800Sstevel@tonic-gate
21810Sstevel@tonic-gate /*
21820Sstevel@tonic-gate * In failsafe mode, we don't use login(1), so don't try
21830Sstevel@tonic-gate * setting up a utmpx entry.
21840Sstevel@tonic-gate */
2185*12613SSurya.Prakki@Sun.COM if (!failsafe)
21860Sstevel@tonic-gate if (setup_utmpx(slaveshortname) == -1)
21870Sstevel@tonic-gate return (1);
21880Sstevel@tonic-gate
218912578SGlenn.Faden@Sun.COM /*
219012578SGlenn.Faden@Sun.COM * The child needs to run as root to
219112578SGlenn.Faden@Sun.COM * execute the brand's login program.
219212578SGlenn.Faden@Sun.COM */
219312578SGlenn.Faden@Sun.COM if (setuid(0) == -1) {
219412578SGlenn.Faden@Sun.COM zperror(gettext("insufficient privilege"));
219512578SGlenn.Faden@Sun.COM return (1);
219612578SGlenn.Faden@Sun.COM }
219712578SGlenn.Faden@Sun.COM
21980Sstevel@tonic-gate (void) execve(new_args[0], new_args, new_env);
21990Sstevel@tonic-gate zperror(gettext("exec failure"));
22000Sstevel@tonic-gate return (1);
22010Sstevel@tonic-gate }
220212578SGlenn.Faden@Sun.COM
22030Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
22040Sstevel@tonic-gate (void) close(tmpl_fd);
22050Sstevel@tonic-gate
22060Sstevel@tonic-gate /*
22070Sstevel@tonic-gate * The rest is only for the parent process.
22080Sstevel@tonic-gate */
22090Sstevel@tonic-gate (void) sigset(SIGWINCH, sigwinch);
22100Sstevel@tonic-gate
22110Sstevel@tonic-gate postfork_dropprivs();
22120Sstevel@tonic-gate
22130Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
22147089Sgjelinek doio(masterfd, -1, masterfd, -1, -1, B_FALSE);
22150Sstevel@tonic-gate
22160Sstevel@tonic-gate reset_tty();
22170Sstevel@tonic-gate (void) fprintf(stderr,
22180Sstevel@tonic-gate gettext("\n[Connection to zone '%s' %s closed]\n"), zonename,
22190Sstevel@tonic-gate slaveshortname);
22200Sstevel@tonic-gate
22210Sstevel@tonic-gate if (pollerr != 0) {
22220Sstevel@tonic-gate (void) fprintf(stderr, gettext("Error: connection closed due "
22230Sstevel@tonic-gate "to unexpected pollevents=0x%x.\n"), pollerr);
22240Sstevel@tonic-gate return (1);
22250Sstevel@tonic-gate }
22260Sstevel@tonic-gate
22270Sstevel@tonic-gate return (0);
22280Sstevel@tonic-gate }
2229