17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5facf4a8dSllai * Common Development and Distribution License (the "License").
6facf4a8dSllai * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22a20ee416SGlenn Faden * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23c2589d13SGarrett D'Amore * Copyright 2013 DEY Storage Systems, Inc.
24279721bfSGary Mills * Copyright (c) 2014 Gary Mills
25741343adSAlexander Eremin * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
26d3b5f563SJohn Levon * Copyright 2019 Joyent, Inc.
277d8deab2SAndy Fiddaman * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * zlogin provides three types of login which allow users in the global
327c478bd9Sstevel@tonic-gate * zone to access non-global zones.
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * - "interactive login" is similar to rlogin(1); for example, the user could
357c478bd9Sstevel@tonic-gate * issue 'zlogin my-zone' or 'zlogin -e ^ -l me my-zone'. The user is
367c478bd9Sstevel@tonic-gate * granted a new pty (which is then shoved into the zone), and an I/O
377c478bd9Sstevel@tonic-gate * loop between parent and child processes takes care of the interactive
387c478bd9Sstevel@tonic-gate * session. In this mode, login(1) (and its -c option, which means
397c478bd9Sstevel@tonic-gate * "already authenticated") is employed to take care of the initialization
407c478bd9Sstevel@tonic-gate * of the user's session.
417c478bd9Sstevel@tonic-gate *
42*bbf21555SRichard Lowe * - "non-interactive login" is similar to su(8); the user could issue
437c478bd9Sstevel@tonic-gate * 'zlogin my-zone ls -l' and the command would be run as specified.
447c478bd9Sstevel@tonic-gate * In this mode, zlogin sets up pipes as the communication channel, and
457c478bd9Sstevel@tonic-gate * 'su' is used to do the login setup work.
467c478bd9Sstevel@tonic-gate *
477c478bd9Sstevel@tonic-gate * - "console login" is the equivalent to accessing the tip line for a
487c478bd9Sstevel@tonic-gate * zone. For example, the user can issue 'zlogin -C my-zone'.
497c478bd9Sstevel@tonic-gate * In this mode, zlogin contacts the zoneadmd process via unix domain
507c478bd9Sstevel@tonic-gate * socket. If zoneadmd is not running, it starts it. This allows the
517c478bd9Sstevel@tonic-gate * console to be available anytime the zone is installed, regardless of
527c478bd9Sstevel@tonic-gate * whether it is running.
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #include <sys/socket.h>
567c478bd9Sstevel@tonic-gate #include <sys/termios.h>
577c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
617c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
629acbbeafSnn #include <sys/brand.h>
63858a4b99Ssl #include <sys/wait.h>
647c478bd9Sstevel@tonic-gate #include <alloca.h>
657c478bd9Sstevel@tonic-gate #include <assert.h>
667c478bd9Sstevel@tonic-gate #include <ctype.h>
67279721bfSGary Mills #include <paths.h>
687c478bd9Sstevel@tonic-gate #include <door.h>
697c478bd9Sstevel@tonic-gate #include <errno.h>
70858a4b99Ssl #include <nss_dbdefs.h>
717c478bd9Sstevel@tonic-gate #include <poll.h>
727c478bd9Sstevel@tonic-gate #include <priv.h>
737c478bd9Sstevel@tonic-gate #include <pwd.h>
747c478bd9Sstevel@tonic-gate #include <unistd.h>
757c478bd9Sstevel@tonic-gate #include <utmpx.h>
767c478bd9Sstevel@tonic-gate #include <sac.h>
777c478bd9Sstevel@tonic-gate #include <signal.h>
787c478bd9Sstevel@tonic-gate #include <stdarg.h>
797c478bd9Sstevel@tonic-gate #include <stdio.h>
807c478bd9Sstevel@tonic-gate #include <stdlib.h>
817c478bd9Sstevel@tonic-gate #include <string.h>
827c478bd9Sstevel@tonic-gate #include <strings.h>
837c478bd9Sstevel@tonic-gate #include <stropts.h>
847c478bd9Sstevel@tonic-gate #include <wait.h>
857c478bd9Sstevel@tonic-gate #include <zone.h>
867c478bd9Sstevel@tonic-gate #include <fcntl.h>
877c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
887c478bd9Sstevel@tonic-gate #include <libintl.h>
897c478bd9Sstevel@tonic-gate #include <locale.h>
907c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
917c478bd9Sstevel@tonic-gate #include <libcontract.h>
929acbbeafSnn #include <libbrand.h>
93cb8a054bSGlenn Faden #include <auth_list.h>
94cb8a054bSGlenn Faden #include <auth_attr.h>
95cb8a054bSGlenn Faden #include <secdb.h>
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate static int masterfd;
987c478bd9Sstevel@tonic-gate static struct termios save_termios;
997c478bd9Sstevel@tonic-gate static struct termios effective_termios;
1007c478bd9Sstevel@tonic-gate static int save_fd;
1017c478bd9Sstevel@tonic-gate static struct winsize winsize;
1027c478bd9Sstevel@tonic-gate static volatile int dead;
1037c478bd9Sstevel@tonic-gate static volatile pid_t child_pid = -1;
1047c478bd9Sstevel@tonic-gate static int interactive = 0;
1057c478bd9Sstevel@tonic-gate static priv_set_t *dropprivs;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate static int nocmdchar = 0;
1087c478bd9Sstevel@tonic-gate static int failsafe = 0;
109741343adSAlexander Eremin static int disconnect = 0;
1107c478bd9Sstevel@tonic-gate static char cmdchar = '~';
111c2589d13SGarrett D'Amore static int quiet = 0;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate static int pollerr = 0;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate static const char *pname;
116cb8a054bSGlenn Faden static char *username;
117cb8a054bSGlenn Faden
118cb8a054bSGlenn Faden /*
119cb8a054bSGlenn Faden * When forced_login is true, the user is not prompted
120cb8a054bSGlenn Faden * for an authentication password in the target zone.
121cb8a054bSGlenn Faden */
122cb8a054bSGlenn Faden static boolean_t forced_login = B_FALSE;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
1257c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate #define SUPATH "/usr/bin/su"
1297c478bd9Sstevel@tonic-gate #define FAILSAFESHELL "/sbin/sh"
1307c478bd9Sstevel@tonic-gate #define DEFAULTSHELL "/sbin/sh"
1317c478bd9Sstevel@tonic-gate #define DEF_PATH "/usr/sbin:/usr/bin"
1327c478bd9Sstevel@tonic-gate
13362868012SSteve Lawrence #define CLUSTER_BRAND_NAME "cluster"
13462868012SSteve Lawrence
135b52a7fd7Sgjelinek /*
136b52a7fd7Sgjelinek * The ZLOGIN_BUFSIZ is larger than PIPE_BUF so we can be sure we're clearing
137b52a7fd7Sgjelinek * out the pipe when the child is exiting. The ZLOGIN_RDBUFSIZ must be less
138b52a7fd7Sgjelinek * than ZLOGIN_BUFSIZ (because we share the buffer in doio). This value is
139b52a7fd7Sgjelinek * also chosen in conjunction with the HI_WATER setting to make sure we
140b52a7fd7Sgjelinek * don't fill up the pipe. We can write FIFOHIWAT (16k) into the pipe before
141b52a7fd7Sgjelinek * blocking. By having ZLOGIN_RDBUFSIZ set to 1k and HI_WATER set to 8k, we
142b52a7fd7Sgjelinek * know we can always write a ZLOGIN_RDBUFSIZ chunk into the pipe when there
143b52a7fd7Sgjelinek * is less than HI_WATER data already in the pipe.
144b52a7fd7Sgjelinek */
145e6e67599Sdp #define ZLOGIN_BUFSIZ 8192
146b52a7fd7Sgjelinek #define ZLOGIN_RDBUFSIZ 1024
147b52a7fd7Sgjelinek #define HI_WATER 8192
148e6e67599Sdp
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * See canonify() below. CANONIFY_LEN is the maximum length that a
1517c478bd9Sstevel@tonic-gate * "canonical" sequence will expand to (backslash, three octal digits, NUL).
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate #define CANONIFY_LEN 5
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate static void
usage(void)1567c478bd9Sstevel@tonic-gate usage(void)
1577c478bd9Sstevel@tonic-gate {
158741343adSAlexander Eremin (void) fprintf(stderr, gettext("usage: %s [ -dnQCES ] [ -e cmdchar ] "
1597c478bd9Sstevel@tonic-gate "[-l user] zonename [command [args ...] ]\n"), pname);
1607c478bd9Sstevel@tonic-gate exit(2);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate static const char *
getpname(const char * arg0)1647c478bd9Sstevel@tonic-gate getpname(const char *arg0)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate const char *p = strrchr(arg0, '/');
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate if (p == NULL)
1697c478bd9Sstevel@tonic-gate p = arg0;
1707c478bd9Sstevel@tonic-gate else
1717c478bd9Sstevel@tonic-gate p++;
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate pname = p;
1747c478bd9Sstevel@tonic-gate return (p);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate static void
zerror(const char * fmt,...)1787c478bd9Sstevel@tonic-gate zerror(const char *fmt, ...)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate va_list alist;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", pname);
1837c478bd9Sstevel@tonic-gate va_start(alist, fmt);
1847c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, alist);
1857c478bd9Sstevel@tonic-gate va_end(alist);
1867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n");
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate static void
zperror(const char * str)1907c478bd9Sstevel@tonic-gate zperror(const char *str)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate const char *estr;
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if ((estr = strerror(errno)) != NULL)
1957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: %s\n", pname, str, estr);
1967c478bd9Sstevel@tonic-gate else
1977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s: errno %d\n", pname, str, errno);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate * The first part of our privilege dropping scheme needs to be called before
2027c478bd9Sstevel@tonic-gate * fork(), since we must have it for security; we don't want to be surprised
2037c478bd9Sstevel@tonic-gate * later that we couldn't allocate the privset.
2047c478bd9Sstevel@tonic-gate */
2057c478bd9Sstevel@tonic-gate static int
prefork_dropprivs()2067c478bd9Sstevel@tonic-gate prefork_dropprivs()
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate if ((dropprivs = priv_allocset()) == NULL)
2097c478bd9Sstevel@tonic-gate return (1);
210634e26ecSCasper H.S. Dik
211634e26ecSCasper H.S. Dik priv_basicset(dropprivs);
212634e26ecSCasper H.S. Dik (void) priv_delset(dropprivs, PRIV_PROC_INFO);
213634e26ecSCasper H.S. Dik (void) priv_delset(dropprivs, PRIV_PROC_FORK);
214634e26ecSCasper H.S. Dik (void) priv_delset(dropprivs, PRIV_PROC_EXEC);
215634e26ecSCasper H.S. Dik (void) priv_delset(dropprivs, PRIV_FILE_LINK_ANY);
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate /*
218634e26ecSCasper H.S. Dik * We need to keep the basic privilege PROC_SESSION and all unknown
219634e26ecSCasper H.S. Dik * basic privileges as well as the privileges PROC_ZONE and
220634e26ecSCasper H.S. Dik * PROC_OWNER in order to query session information and
2217c478bd9Sstevel@tonic-gate * send signals.
2227c478bd9Sstevel@tonic-gate */
2237c478bd9Sstevel@tonic-gate if (interactive == 0) {
224634e26ecSCasper H.S. Dik (void) priv_addset(dropprivs, PRIV_PROC_ZONE);
225634e26ecSCasper H.S. Dik (void) priv_addset(dropprivs, PRIV_PROC_OWNER);
226634e26ecSCasper H.S. Dik } else {
227634e26ecSCasper H.S. Dik (void) priv_delset(dropprivs, PRIV_PROC_SESSION);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate return (0);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * The second part of the privilege drop. We are paranoid about being attacked
2357c478bd9Sstevel@tonic-gate * by the zone, so we drop all privileges. This should prevent a compromise
2367c478bd9Sstevel@tonic-gate * which gets us to fork(), exec(), symlink(), etc.
2377c478bd9Sstevel@tonic-gate */
2387c478bd9Sstevel@tonic-gate static void
postfork_dropprivs()2397c478bd9Sstevel@tonic-gate postfork_dropprivs()
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate if ((setppriv(PRIV_SET, PRIV_PERMITTED, dropprivs)) == -1) {
2427c478bd9Sstevel@tonic-gate zperror(gettext("Warning: could not set permitted privileges"));
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate if ((setppriv(PRIV_SET, PRIV_LIMIT, dropprivs)) == -1) {
2457c478bd9Sstevel@tonic-gate zperror(gettext("Warning: could not set limit privileges"));
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate if ((setppriv(PRIV_SET, PRIV_INHERITABLE, dropprivs)) == -1) {
2487c478bd9Sstevel@tonic-gate zperror(gettext("Warning: could not set inheritable "
2497c478bd9Sstevel@tonic-gate "privileges"));
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate * Create the unix domain socket and call the zoneadmd server; handshake
2557c478bd9Sstevel@tonic-gate * with it to determine whether it will allow us to connect.
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate static int
get_console_master(const char * zname)2587c478bd9Sstevel@tonic-gate get_console_master(const char *zname)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate int sockfd = -1;
2617c478bd9Sstevel@tonic-gate struct sockaddr_un servaddr;
2627c478bd9Sstevel@tonic-gate char clientid[MAXPATHLEN];
2637c478bd9Sstevel@tonic-gate char handshake[MAXPATHLEN], c;
2647c478bd9Sstevel@tonic-gate int msglen;
2657c478bd9Sstevel@tonic-gate int i = 0, err = 0;
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
2687c478bd9Sstevel@tonic-gate zperror(gettext("could not create socket"));
2697c478bd9Sstevel@tonic-gate return (-1);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate bzero(&servaddr, sizeof (servaddr));
2737c478bd9Sstevel@tonic-gate servaddr.sun_family = AF_UNIX;
2747c478bd9Sstevel@tonic-gate (void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path),
2757c478bd9Sstevel@tonic-gate "%s/%s.console_sock", ZONES_TMPDIR, zname);
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate if (connect(sockfd, (struct sockaddr *)&servaddr,
2787c478bd9Sstevel@tonic-gate sizeof (servaddr)) == -1) {
2797c478bd9Sstevel@tonic-gate zperror(gettext("Could not connect to zone console"));
2807c478bd9Sstevel@tonic-gate goto bad;
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate masterfd = sockfd;
2837c478bd9Sstevel@tonic-gate
284741343adSAlexander Eremin msglen = snprintf(clientid, sizeof (clientid), "IDENT %lu %s %d\n",
285741343adSAlexander Eremin getpid(), setlocale(LC_MESSAGES, NULL), disconnect);
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate if (msglen >= sizeof (clientid) || msglen < 0) {
2887c478bd9Sstevel@tonic-gate zerror("protocol error");
2897c478bd9Sstevel@tonic-gate goto bad;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate if (write(masterfd, clientid, msglen) != msglen) {
2937c478bd9Sstevel@tonic-gate zerror("protocol error");
2947c478bd9Sstevel@tonic-gate goto bad;
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate
2977c478bd9Sstevel@tonic-gate bzero(handshake, sizeof (handshake));
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * Take care not to accumulate more than our fill, and leave room for
3017c478bd9Sstevel@tonic-gate * the NUL at the end.
3027c478bd9Sstevel@tonic-gate */
3037c478bd9Sstevel@tonic-gate while ((err = read(masterfd, &c, 1)) == 1) {
3047c478bd9Sstevel@tonic-gate if (i >= (sizeof (handshake) - 1))
3057c478bd9Sstevel@tonic-gate break;
3067c478bd9Sstevel@tonic-gate if (c == '\n')
3077c478bd9Sstevel@tonic-gate break;
3087c478bd9Sstevel@tonic-gate handshake[i] = c;
3097c478bd9Sstevel@tonic-gate i++;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * If something went wrong during the handshake we bail; perhaps
3147c478bd9Sstevel@tonic-gate * the server died off.
3157c478bd9Sstevel@tonic-gate */
3167c478bd9Sstevel@tonic-gate if (err == -1) {
3177c478bd9Sstevel@tonic-gate zperror(gettext("Could not connect to zone console"));
3187c478bd9Sstevel@tonic-gate goto bad;
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate if (strncmp(handshake, "OK", sizeof (handshake)) == 0)
3227c478bd9Sstevel@tonic-gate return (0);
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate zerror(gettext("Console is already in use by process ID %s."),
3257c478bd9Sstevel@tonic-gate handshake);
3267c478bd9Sstevel@tonic-gate bad:
3277c478bd9Sstevel@tonic-gate (void) close(sockfd);
3287c478bd9Sstevel@tonic-gate masterfd = -1;
3297c478bd9Sstevel@tonic-gate return (-1);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate * Routines to handle pty creation upon zone entry and to shuttle I/O back
3357c478bd9Sstevel@tonic-gate * and forth between the two terminals. We also compute and store the
3367c478bd9Sstevel@tonic-gate * name of the slave terminal associated with the master side.
3377c478bd9Sstevel@tonic-gate */
3387c478bd9Sstevel@tonic-gate static int
get_master_pty()3397c478bd9Sstevel@tonic-gate get_master_pty()
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate if ((masterfd = open("/dev/ptmx", O_RDWR|O_NONBLOCK)) < 0) {
3427c478bd9Sstevel@tonic-gate zperror(gettext("failed to obtain a pseudo-tty"));
3437c478bd9Sstevel@tonic-gate return (-1);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate if (tcgetattr(STDIN_FILENO, &save_termios) == -1) {
3467c478bd9Sstevel@tonic-gate zperror(gettext("failed to get terminal settings from stdin"));
3477c478bd9Sstevel@tonic-gate return (-1);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate (void) ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&winsize);
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate return (0);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate * This is a bit tricky; normally a pts device will belong to the zone it
3567c478bd9Sstevel@tonic-gate * is granted to. But in the case of "entering" a zone, we need to establish
3577c478bd9Sstevel@tonic-gate * the pty before entering the zone so that we can vector I/O to and from it
3587c478bd9Sstevel@tonic-gate * from the global zone.
3597c478bd9Sstevel@tonic-gate *
3607c478bd9Sstevel@tonic-gate * We use the zonept() call to let the ptm driver know what we are up to;
3617c478bd9Sstevel@tonic-gate * the only other hairy bit is the setting of zoneslavename (which happens
3627c478bd9Sstevel@tonic-gate * above, in get_master_pty()).
3637c478bd9Sstevel@tonic-gate */
3647c478bd9Sstevel@tonic-gate static int
init_slave_pty(zoneid_t zoneid,char * devroot)365facf4a8dSllai init_slave_pty(zoneid_t zoneid, char *devroot)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate int slavefd = -1;
3687c478bd9Sstevel@tonic-gate char *slavename, zoneslavename[MAXPATHLEN];
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate * Set slave permissions, zone the pts, then unlock it.
3727c478bd9Sstevel@tonic-gate */
3737c478bd9Sstevel@tonic-gate if (grantpt(masterfd) != 0) {
3747c478bd9Sstevel@tonic-gate zperror(gettext("grantpt failed"));
3757c478bd9Sstevel@tonic-gate return (-1);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate if (unlockpt(masterfd) != 0) {
3797c478bd9Sstevel@tonic-gate zperror(gettext("unlockpt failed"));
3807c478bd9Sstevel@tonic-gate return (-1);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate * We must open the slave side before zoning this pty; otherwise
3857c478bd9Sstevel@tonic-gate * the kernel would refuse us the open-- zoning a pty makes it
386facf4a8dSllai * inaccessible to the global zone. Note we are trying to open
387facf4a8dSllai * the device node via the $ZONEROOT/dev path for this pty.
3887c478bd9Sstevel@tonic-gate *
3897c478bd9Sstevel@tonic-gate * Later we'll close the slave out when once we've opened it again
3907c478bd9Sstevel@tonic-gate * from within the target zone. Blarg.
3917c478bd9Sstevel@tonic-gate */
3927c478bd9Sstevel@tonic-gate if ((slavename = ptsname(masterfd)) == NULL) {
3937c478bd9Sstevel@tonic-gate zperror(gettext("failed to get name for pseudo-tty"));
3947c478bd9Sstevel@tonic-gate return (-1);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate (void) snprintf(zoneslavename, sizeof (zoneslavename), "%s%s",
398facf4a8dSllai devroot, slavename);
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate if ((slavefd = open(zoneslavename, O_RDWR)) < 0) {
401facf4a8dSllai zerror(gettext("failed to open %s: %s"), zoneslavename,
402facf4a8dSllai strerror(errno));
403facf4a8dSllai return (-1);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate /*
4077c478bd9Sstevel@tonic-gate * Push hardware emulation (ptem), line discipline (ldterm),
4087c478bd9Sstevel@tonic-gate * and V7/4BSD/Xenix compatibility (ttcompat) modules.
4097c478bd9Sstevel@tonic-gate */
4107c478bd9Sstevel@tonic-gate if (ioctl(slavefd, I_PUSH, "ptem") == -1) {
4117c478bd9Sstevel@tonic-gate zperror(gettext("failed to push ptem module"));
4127c478bd9Sstevel@tonic-gate if (!failsafe)
4137c478bd9Sstevel@tonic-gate goto bad;
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate * Anchor the stream to prevent malicious I_POPs; we prefer to do
4187c478bd9Sstevel@tonic-gate * this prior to entering the zone so that we can detect any errors
4197c478bd9Sstevel@tonic-gate * early, and so that we can set the anchor from the global zone.
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate if (ioctl(slavefd, I_ANCHOR) == -1) {
4227c478bd9Sstevel@tonic-gate zperror(gettext("failed to set stream anchor"));
4237c478bd9Sstevel@tonic-gate if (!failsafe)
4247c478bd9Sstevel@tonic-gate goto bad;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate
4277c478bd9Sstevel@tonic-gate if (ioctl(slavefd, I_PUSH, "ldterm") == -1) {
4287c478bd9Sstevel@tonic-gate zperror(gettext("failed to push ldterm module"));
4297c478bd9Sstevel@tonic-gate if (!failsafe)
4307c478bd9Sstevel@tonic-gate goto bad;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate if (ioctl(slavefd, I_PUSH, "ttcompat") == -1) {
4337c478bd9Sstevel@tonic-gate zperror(gettext("failed to push ttcompat module"));
4347c478bd9Sstevel@tonic-gate if (!failsafe)
4357c478bd9Sstevel@tonic-gate goto bad;
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate * Propagate terminal settings from the external term to the new one.
4407c478bd9Sstevel@tonic-gate */
4417c478bd9Sstevel@tonic-gate if (tcsetattr(slavefd, TCSAFLUSH, &save_termios) == -1) {
4427c478bd9Sstevel@tonic-gate zperror(gettext("failed to set terminal settings"));
4437c478bd9Sstevel@tonic-gate if (!failsafe)
4447c478bd9Sstevel@tonic-gate goto bad;
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate (void) ioctl(slavefd, TIOCSWINSZ, (char *)&winsize);
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate if (zonept(masterfd, zoneid) != 0) {
4497c478bd9Sstevel@tonic-gate zperror(gettext("could not set zoneid of pty"));
4507c478bd9Sstevel@tonic-gate goto bad;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate return (slavefd);
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate bad:
4567c478bd9Sstevel@tonic-gate (void) close(slavefd);
4577c478bd9Sstevel@tonic-gate return (-1);
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate * Place terminal into raw mode.
4627c478bd9Sstevel@tonic-gate */
4637c478bd9Sstevel@tonic-gate static int
set_tty_rawmode(int fd)4647c478bd9Sstevel@tonic-gate set_tty_rawmode(int fd)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate struct termios term;
4677c478bd9Sstevel@tonic-gate if (tcgetattr(fd, &term) < 0) {
4687c478bd9Sstevel@tonic-gate zperror(gettext("failed to get user terminal settings"));
4697c478bd9Sstevel@tonic-gate return (-1);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate /* Stash for later, so we can revert back to previous mode */
4737c478bd9Sstevel@tonic-gate save_termios = term;
4747c478bd9Sstevel@tonic-gate save_fd = fd;
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate /* disable 8->7 bit strip, start/stop, enable any char to restart */
4777c478bd9Sstevel@tonic-gate term.c_iflag &= ~(ISTRIP|IXON|IXANY);
4787c478bd9Sstevel@tonic-gate /* disable NL->CR, CR->NL, ignore CR, UPPER->lower */
4797c478bd9Sstevel@tonic-gate term.c_iflag &= ~(INLCR|ICRNL|IGNCR|IUCLC);
4807c478bd9Sstevel@tonic-gate /* disable output post-processing */
4817c478bd9Sstevel@tonic-gate term.c_oflag &= ~OPOST;
4827c478bd9Sstevel@tonic-gate /* disable canonical mode, signal chars, echo & extended functions */
4837c478bd9Sstevel@tonic-gate term.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN);
4847c478bd9Sstevel@tonic-gate
4857c478bd9Sstevel@tonic-gate term.c_cc[VMIN] = 1; /* byte-at-a-time */
4867c478bd9Sstevel@tonic-gate term.c_cc[VTIME] = 0;
4877c478bd9Sstevel@tonic-gate
4887c478bd9Sstevel@tonic-gate if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term)) {
4897c478bd9Sstevel@tonic-gate zperror(gettext("failed to set user terminal to raw mode"));
4907c478bd9Sstevel@tonic-gate return (-1);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate * We need to know the value of VEOF so that we can properly process for
4957c478bd9Sstevel@tonic-gate * client-side ~<EOF>. But we have obliterated VEOF in term,
4967c478bd9Sstevel@tonic-gate * because VMIN overloads the same array slot in non-canonical mode.
4977c478bd9Sstevel@tonic-gate * Stupid @&^%!
4987c478bd9Sstevel@tonic-gate *
4997c478bd9Sstevel@tonic-gate * So here we construct the "effective" termios from the current
5007c478bd9Sstevel@tonic-gate * terminal settings, and the corrected VEOF and VEOL settings.
5017c478bd9Sstevel@tonic-gate */
5027c478bd9Sstevel@tonic-gate if (tcgetattr(STDIN_FILENO, &effective_termios) < 0) {
5037c478bd9Sstevel@tonic-gate zperror(gettext("failed to get user terminal settings"));
5047c478bd9Sstevel@tonic-gate return (-1);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate effective_termios.c_cc[VEOF] = save_termios.c_cc[VEOF];
5077c478bd9Sstevel@tonic-gate effective_termios.c_cc[VEOL] = save_termios.c_cc[VEOL];
5087c478bd9Sstevel@tonic-gate
5097c478bd9Sstevel@tonic-gate return (0);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate * Copy terminal window size from our terminal to the pts.
5147c478bd9Sstevel@tonic-gate */
5157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5167c478bd9Sstevel@tonic-gate static void
sigwinch(int s)5177c478bd9Sstevel@tonic-gate sigwinch(int s)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate struct winsize ws;
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate if (ioctl(0, TIOCGWINSZ, &ws) == 0)
5227c478bd9Sstevel@tonic-gate (void) ioctl(masterfd, TIOCSWINSZ, &ws);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate
525ff17c8bfSgjelinek static volatile int close_on_sig = -1;
526ff17c8bfSgjelinek
5277c478bd9Sstevel@tonic-gate static void
5287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
sigcld(int s)5297c478bd9Sstevel@tonic-gate sigcld(int s)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate int status;
5327c478bd9Sstevel@tonic-gate pid_t pid;
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate * Peek at the exit status. If this isn't the process we cared
5367c478bd9Sstevel@tonic-gate * about, then just reap it.
5377c478bd9Sstevel@tonic-gate */
5387c478bd9Sstevel@tonic-gate if ((pid = waitpid(child_pid, &status, WNOHANG|WNOWAIT)) != -1) {
5397c478bd9Sstevel@tonic-gate if (pid == child_pid &&
540ff17c8bfSgjelinek (WIFEXITED(status) || WIFSIGNALED(status))) {
5417c478bd9Sstevel@tonic-gate dead = 1;
542ff17c8bfSgjelinek if (close_on_sig != -1) {
543ff17c8bfSgjelinek (void) write(close_on_sig, "a", 1);
544ff17c8bfSgjelinek (void) close(close_on_sig);
545ff17c8bfSgjelinek close_on_sig = -1;
546ff17c8bfSgjelinek }
547ff17c8bfSgjelinek } else {
5487c478bd9Sstevel@tonic-gate (void) waitpid(pid, &status, WNOHANG);
549ff17c8bfSgjelinek }
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate * Some signals (currently, SIGINT) must be forwarded on to the process
5557c478bd9Sstevel@tonic-gate * group of the child process.
5567c478bd9Sstevel@tonic-gate */
5577c478bd9Sstevel@tonic-gate static void
sig_forward(int s)5587c478bd9Sstevel@tonic-gate sig_forward(int s)
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate if (child_pid != -1) {
561136dc9cbSAlexander Eremin (void) sigsend(P_PGID, child_pid, s);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate * reset terminal settings for global environment
5677c478bd9Sstevel@tonic-gate */
5687c478bd9Sstevel@tonic-gate static void
reset_tty()5697c478bd9Sstevel@tonic-gate reset_tty()
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate (void) tcsetattr(save_fd, TCSADRAIN, &save_termios);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate * Convert character to printable representation, for display with locally
5767c478bd9Sstevel@tonic-gate * echoed command characters (like when we need to display ~^D)
5777c478bd9Sstevel@tonic-gate */
5787c478bd9Sstevel@tonic-gate static void
canonify(char c,char * cc)5797c478bd9Sstevel@tonic-gate canonify(char c, char *cc)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate if (isprint(c)) {
5827c478bd9Sstevel@tonic-gate cc[0] = c;
5837c478bd9Sstevel@tonic-gate cc[1] = '\0';
5847c478bd9Sstevel@tonic-gate } else if (c >= 0 && c <= 31) { /* ^@ through ^_ */
5857c478bd9Sstevel@tonic-gate cc[0] = '^';
5867c478bd9Sstevel@tonic-gate cc[1] = c + '@';
5877c478bd9Sstevel@tonic-gate cc[2] = '\0';
5887c478bd9Sstevel@tonic-gate } else {
5897c478bd9Sstevel@tonic-gate cc[0] = '\\';
5907c478bd9Sstevel@tonic-gate cc[1] = ((c >> 6) & 7) + '0';
5917c478bd9Sstevel@tonic-gate cc[2] = ((c >> 3) & 7) + '0';
5927c478bd9Sstevel@tonic-gate cc[3] = (c & 7) + '0';
5937c478bd9Sstevel@tonic-gate cc[4] = '\0';
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate * process_user_input watches the input stream for the escape sequence for
5997c478bd9Sstevel@tonic-gate * 'quit' (by default, tilde-period). Because we might be fed just one
6007c478bd9Sstevel@tonic-gate * keystroke at a time, state associated with the user input (are we at the
6017c478bd9Sstevel@tonic-gate * beginning of the line? are we locally echoing the next character?) is
6027c478bd9Sstevel@tonic-gate * maintained by beginning_of_line and local_echo across calls to the routine.
603d9e728a2Sgjelinek * If the write to outfd fails, we'll try to read from infd in an attempt
604d9e728a2Sgjelinek * to prevent deadlock between the two processes.
6057c478bd9Sstevel@tonic-gate *
6067c478bd9Sstevel@tonic-gate * This routine returns -1 when the 'quit' escape sequence has been issued,
607ff17c8bfSgjelinek * or an error is encountered, 1 if stdin is EOF, and 0 otherwise.
6087c478bd9Sstevel@tonic-gate */
6097c478bd9Sstevel@tonic-gate static int
process_user_input(int outfd,int infd)610ff17c8bfSgjelinek process_user_input(int outfd, int infd)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate static boolean_t beginning_of_line = B_TRUE;
6137c478bd9Sstevel@tonic-gate static boolean_t local_echo = B_FALSE;
614ff17c8bfSgjelinek char ibuf[ZLOGIN_BUFSIZ];
615ff17c8bfSgjelinek int nbytes;
616ff17c8bfSgjelinek char *buf = ibuf;
617ff17c8bfSgjelinek
618ff17c8bfSgjelinek nbytes = read(STDIN_FILENO, ibuf, ZLOGIN_RDBUFSIZ);
619ff17c8bfSgjelinek if (nbytes == -1 && (errno != EINTR || dead))
620ff17c8bfSgjelinek return (-1);
621ff17c8bfSgjelinek
622ff17c8bfSgjelinek if (nbytes == -1) /* The read was interrupted. */
623ff17c8bfSgjelinek return (0);
624ff17c8bfSgjelinek
625ff17c8bfSgjelinek /* 0 read means EOF, close the pipe to the child */
626ff17c8bfSgjelinek if (nbytes == 0)
627ff17c8bfSgjelinek return (1);
628ff17c8bfSgjelinek
629d3b5f563SJohn Levon for (char c = *buf; nbytes > 0; c = *buf, --nbytes) {
6307c478bd9Sstevel@tonic-gate buf++;
6317c478bd9Sstevel@tonic-gate if (beginning_of_line && !nocmdchar) {
6327c478bd9Sstevel@tonic-gate beginning_of_line = B_FALSE;
6337c478bd9Sstevel@tonic-gate if (c == cmdchar) {
6347c478bd9Sstevel@tonic-gate local_echo = B_TRUE;
6357c478bd9Sstevel@tonic-gate continue;
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate } else if (local_echo) {
6387c478bd9Sstevel@tonic-gate local_echo = B_FALSE;
6397c478bd9Sstevel@tonic-gate if (c == '.' || c == effective_termios.c_cc[VEOF]) {
6407c478bd9Sstevel@tonic-gate char cc[CANONIFY_LEN];
641d9e728a2Sgjelinek
6427c478bd9Sstevel@tonic-gate canonify(c, cc);
6437c478bd9Sstevel@tonic-gate (void) write(STDOUT_FILENO, &cmdchar, 1);
6447c478bd9Sstevel@tonic-gate (void) write(STDOUT_FILENO, cc, strlen(cc));
6457c478bd9Sstevel@tonic-gate return (-1);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate }
648d9e728a2Sgjelinek retry:
649d9e728a2Sgjelinek if (write(outfd, &c, 1) <= 0) {
650d9e728a2Sgjelinek /*
651d9e728a2Sgjelinek * Since the fd we are writing to is opened with
652d9e728a2Sgjelinek * O_NONBLOCK it is possible to get EAGAIN if the
653d9e728a2Sgjelinek * pipe is full. One way this could happen is if we
654d9e728a2Sgjelinek * are writing a lot of data into the pipe in this loop
655d9e728a2Sgjelinek * and the application on the other end is echoing that
656d9e728a2Sgjelinek * data back out to its stdout. The output pipe can
657d9e728a2Sgjelinek * fill up since we are stuck here in this loop and not
658d9e728a2Sgjelinek * draining the other pipe. We can try to read some of
659d9e728a2Sgjelinek * the data to see if we can drain the pipe so that the
660d9e728a2Sgjelinek * application can continue to make progress. The read
661d9e728a2Sgjelinek * is non-blocking so we won't hang here. We also wait
662d9e728a2Sgjelinek * a bit before retrying since there could be other
663d9e728a2Sgjelinek * reasons why the pipe is full and we don't want to
664d9e728a2Sgjelinek * continuously retry.
665d9e728a2Sgjelinek */
666d9e728a2Sgjelinek if (errno == EAGAIN) {
667d9e728a2Sgjelinek struct timespec rqtp;
668d9e728a2Sgjelinek int ln;
669ff17c8bfSgjelinek char obuf[ZLOGIN_BUFSIZ];
670d9e728a2Sgjelinek
671ff17c8bfSgjelinek if ((ln = read(infd, obuf, ZLOGIN_BUFSIZ)) > 0)
672ff17c8bfSgjelinek (void) write(STDOUT_FILENO, obuf, ln);
673d9e728a2Sgjelinek
674d9e728a2Sgjelinek /* sleep for 10 milliseconds */
675d9e728a2Sgjelinek rqtp.tv_sec = 0;
67619449258SJosef 'Jeff' Sipek rqtp.tv_nsec = MSEC2NSEC(10);
677d9e728a2Sgjelinek (void) nanosleep(&rqtp, NULL);
678d9e728a2Sgjelinek if (!dead)
679d9e728a2Sgjelinek goto retry;
680d9e728a2Sgjelinek }
681d9e728a2Sgjelinek
6827c478bd9Sstevel@tonic-gate return (-1);
683d9e728a2Sgjelinek }
6847c478bd9Sstevel@tonic-gate beginning_of_line = (c == '\r' || c == '\n' ||
6857c478bd9Sstevel@tonic-gate c == effective_termios.c_cc[VKILL] ||
6867c478bd9Sstevel@tonic-gate c == effective_termios.c_cc[VEOL] ||
6877c478bd9Sstevel@tonic-gate c == effective_termios.c_cc[VSUSP] ||
6887c478bd9Sstevel@tonic-gate c == effective_termios.c_cc[VINTR]);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate return (0);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
693b52a7fd7Sgjelinek /*
694b52a7fd7Sgjelinek * This function prevents deadlock between zlogin and the application in the
695b52a7fd7Sgjelinek * zone that it is talking to. This can happen when we read from zlogin's
696b52a7fd7Sgjelinek * stdin and write the data down the pipe to the application. If the pipe
697b52a7fd7Sgjelinek * is full, we'll block in the write. Because zlogin could be blocked in
698b52a7fd7Sgjelinek * the write, it would never read the application's stdout/stderr so the
699b52a7fd7Sgjelinek * application can then block on those writes (when the pipe fills up). If the
700b52a7fd7Sgjelinek * the application gets blocked this way, it can never get around to reading
701b52a7fd7Sgjelinek * its stdin so that zlogin can unblock from its write. Once in this state,
702b52a7fd7Sgjelinek * the two processes are deadlocked.
703b52a7fd7Sgjelinek *
704b52a7fd7Sgjelinek * To prevent this, we want to verify that we can write into the pipe before we
705b52a7fd7Sgjelinek * read from our stdin. If the pipe already is pretty full, we bypass the read
706b52a7fd7Sgjelinek * for now. We'll circle back here again after the poll() so that we can
707b52a7fd7Sgjelinek * try again. When this function is called, we already know there is data
708ff17c8bfSgjelinek * ready to read on STDIN_FILENO. We return -1 if there is a problem, 1 if
709ff17c8bfSgjelinek * stdin is EOF, and 0 if everything is ok (even though we might not have
710ff17c8bfSgjelinek * read/written any data into the pipe on this iteration).
711b52a7fd7Sgjelinek */
712b52a7fd7Sgjelinek static int
process_raw_input(int stdin_fd,int appin_fd)713b52a7fd7Sgjelinek process_raw_input(int stdin_fd, int appin_fd)
714b52a7fd7Sgjelinek {
715b52a7fd7Sgjelinek int cc;
716ad593b7fSArindam Sarkar struct stat64 sb;
717b52a7fd7Sgjelinek char ibuf[ZLOGIN_RDBUFSIZ];
718b52a7fd7Sgjelinek
719b52a7fd7Sgjelinek /* Check how much data is already in the pipe */
720ad593b7fSArindam Sarkar if (fstat64(appin_fd, &sb) == -1) {
721b52a7fd7Sgjelinek perror("stat failed");
722b52a7fd7Sgjelinek return (-1);
723b52a7fd7Sgjelinek }
724b52a7fd7Sgjelinek
725b52a7fd7Sgjelinek if (dead)
726b52a7fd7Sgjelinek return (-1);
727b52a7fd7Sgjelinek
728b52a7fd7Sgjelinek /*
729b52a7fd7Sgjelinek * The pipe already has a lot of data in it, don't write any more
730b52a7fd7Sgjelinek * right now.
731b52a7fd7Sgjelinek */
732b52a7fd7Sgjelinek if (sb.st_size >= HI_WATER)
733b52a7fd7Sgjelinek return (0);
734b52a7fd7Sgjelinek
735b52a7fd7Sgjelinek cc = read(STDIN_FILENO, ibuf, ZLOGIN_RDBUFSIZ);
736b52a7fd7Sgjelinek if (cc == -1 && (errno != EINTR || dead))
737b52a7fd7Sgjelinek return (-1);
738b52a7fd7Sgjelinek
739b52a7fd7Sgjelinek if (cc == -1) /* The read was interrupted. */
740b52a7fd7Sgjelinek return (0);
741b52a7fd7Sgjelinek
742ff17c8bfSgjelinek /* 0 read means EOF, close the pipe to the child */
743ff17c8bfSgjelinek if (cc == 0)
744ff17c8bfSgjelinek return (1);
745ff17c8bfSgjelinek
746b52a7fd7Sgjelinek /*
747b52a7fd7Sgjelinek * stdin_fd is stdin of the target; so, the thing we'll write the user
748ff17c8bfSgjelinek * data *to*.
749b52a7fd7Sgjelinek */
750b52a7fd7Sgjelinek if (write(stdin_fd, ibuf, cc) == -1)
751b52a7fd7Sgjelinek return (-1);
752b52a7fd7Sgjelinek
753b52a7fd7Sgjelinek return (0);
754b52a7fd7Sgjelinek }
755b52a7fd7Sgjelinek
756b52a7fd7Sgjelinek /*
757b52a7fd7Sgjelinek * Write the output from the application running in the zone. We can get
758b52a7fd7Sgjelinek * a signal during the write (usually it would be SIGCHLD when the application
759b52a7fd7Sgjelinek * has exited) so we loop to make sure we have written all of the data we read.
760b52a7fd7Sgjelinek */
761b52a7fd7Sgjelinek static int
process_output(int in_fd,int out_fd)762b52a7fd7Sgjelinek process_output(int in_fd, int out_fd)
763b52a7fd7Sgjelinek {
764b52a7fd7Sgjelinek int wrote = 0;
765b52a7fd7Sgjelinek int cc;
766b52a7fd7Sgjelinek char ibuf[ZLOGIN_BUFSIZ];
767b52a7fd7Sgjelinek
768b52a7fd7Sgjelinek cc = read(in_fd, ibuf, ZLOGIN_BUFSIZ);
769b52a7fd7Sgjelinek if (cc == -1 && (errno != EINTR || dead))
770b52a7fd7Sgjelinek return (-1);
7717d8deab2SAndy Fiddaman if (cc == 0)
7727d8deab2SAndy Fiddaman return (-1); /* EOF */
773b52a7fd7Sgjelinek if (cc == -1) /* The read was interrupted. */
774b52a7fd7Sgjelinek return (0);
775b52a7fd7Sgjelinek
776b52a7fd7Sgjelinek do {
777b52a7fd7Sgjelinek int len;
778b52a7fd7Sgjelinek
779b52a7fd7Sgjelinek len = write(out_fd, ibuf + wrote, cc - wrote);
780b52a7fd7Sgjelinek if (len == -1 && errno != EINTR)
781b52a7fd7Sgjelinek return (-1);
782b52a7fd7Sgjelinek if (len != -1)
783b52a7fd7Sgjelinek wrote += len;
784b52a7fd7Sgjelinek } while (wrote < cc);
785b52a7fd7Sgjelinek
786b52a7fd7Sgjelinek return (0);
787b52a7fd7Sgjelinek }
788b52a7fd7Sgjelinek
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate * This is the main I/O loop, and is shared across all zlogin modes.
7917c478bd9Sstevel@tonic-gate * Parameters:
7927b2eb3f3SAndy Fiddaman * stdin_fd: The fd representing 'stdin' for the slave side; input to
793858a4b99Ssl * the zone will be written here.
7947c478bd9Sstevel@tonic-gate *
7957b2eb3f3SAndy Fiddaman * appin_fd: The fd representing the other end of the 'stdin' pipe (when
796b52a7fd7Sgjelinek * we're running non-interactive); used in process_raw_input
797b52a7fd7Sgjelinek * to ensure we don't fill up the application's stdin pipe.
798b52a7fd7Sgjelinek *
7997c478bd9Sstevel@tonic-gate * stdout_fd: The fd representing 'stdout' for the slave side; output
800858a4b99Ssl * from the zone will arrive here.
8017c478bd9Sstevel@tonic-gate *
8027c478bd9Sstevel@tonic-gate * stderr_fd: The fd representing 'stderr' for the slave side; output
803858a4b99Ssl * from the zone will arrive here.
8047c478bd9Sstevel@tonic-gate *
8057c478bd9Sstevel@tonic-gate * raw_mode: If TRUE, then no processing (for example, for '~.') will
806858a4b99Ssl * be performed on the input coming from STDIN.
8077c478bd9Sstevel@tonic-gate *
8087c478bd9Sstevel@tonic-gate * stderr_fd may be specified as -1 if there is no stderr (only non-interactive
8097c478bd9Sstevel@tonic-gate * mode supplies a stderr).
8107c478bd9Sstevel@tonic-gate *
8117c478bd9Sstevel@tonic-gate */
8127c478bd9Sstevel@tonic-gate static void
doio(int stdin_fd,int appin_fd,int stdout_fd,int stderr_fd,int sig_fd,boolean_t raw_mode)813ff17c8bfSgjelinek doio(int stdin_fd, int appin_fd, int stdout_fd, int stderr_fd, int sig_fd,
814b52a7fd7Sgjelinek boolean_t raw_mode)
8157c478bd9Sstevel@tonic-gate {
816ff17c8bfSgjelinek struct pollfd pollfds[4];
817e6e67599Sdp char ibuf[ZLOGIN_BUFSIZ];
8187c478bd9Sstevel@tonic-gate int cc, ret;
8197c478bd9Sstevel@tonic-gate
8207c478bd9Sstevel@tonic-gate /* read from stdout of zone and write to stdout of global zone */
8217c478bd9Sstevel@tonic-gate pollfds[0].fd = stdout_fd;
8227c478bd9Sstevel@tonic-gate pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
8237c478bd9Sstevel@tonic-gate
8247c478bd9Sstevel@tonic-gate /* read from stderr of zone and write to stderr of global zone */
8257c478bd9Sstevel@tonic-gate pollfds[1].fd = stderr_fd;
8267c478bd9Sstevel@tonic-gate pollfds[1].events = pollfds[0].events;
8277c478bd9Sstevel@tonic-gate
8287c478bd9Sstevel@tonic-gate /* read from stdin of global zone and write to stdin of zone */
8297c478bd9Sstevel@tonic-gate pollfds[2].fd = STDIN_FILENO;
8307c478bd9Sstevel@tonic-gate pollfds[2].events = pollfds[0].events;
8317c478bd9Sstevel@tonic-gate
832ff17c8bfSgjelinek /* read from signalling pipe so we know when child dies */
833ff17c8bfSgjelinek pollfds[3].fd = sig_fd;
834ff17c8bfSgjelinek pollfds[3].events = pollfds[0].events;
835ff17c8bfSgjelinek
8367c478bd9Sstevel@tonic-gate for (;;) {
8377c478bd9Sstevel@tonic-gate pollfds[0].revents = pollfds[1].revents =
838ff17c8bfSgjelinek pollfds[2].revents = pollfds[3].revents = 0;
8397c478bd9Sstevel@tonic-gate
8407c478bd9Sstevel@tonic-gate if (dead)
8417c478bd9Sstevel@tonic-gate break;
8427c478bd9Sstevel@tonic-gate
843ff17c8bfSgjelinek /*
844ff17c8bfSgjelinek * There is a race condition here where we can receive the
845ff17c8bfSgjelinek * child death signal, set the dead flag, but since we have
846ff17c8bfSgjelinek * passed the test above, we would go into poll and hang.
847ff17c8bfSgjelinek * To avoid this we use the sig_fd as an additional poll fd.
848ff17c8bfSgjelinek * The signal handler writes into the other end of this pipe
849ff17c8bfSgjelinek * when the child dies so that the poll will always see that
850ff17c8bfSgjelinek * input and proceed. We just loop around at that point and
851ff17c8bfSgjelinek * then notice the dead flag.
852ff17c8bfSgjelinek */
853ff17c8bfSgjelinek
8547c478bd9Sstevel@tonic-gate ret = poll(pollfds,
8557c478bd9Sstevel@tonic-gate sizeof (pollfds) / sizeof (struct pollfd), -1);
856ff17c8bfSgjelinek
8577c478bd9Sstevel@tonic-gate if (ret == -1 && errno != EINTR) {
8587c478bd9Sstevel@tonic-gate perror("poll failed");
8597c478bd9Sstevel@tonic-gate break;
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate
8627c478bd9Sstevel@tonic-gate if (errno == EINTR && dead) {
8637c478bd9Sstevel@tonic-gate break;
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate
8667c478bd9Sstevel@tonic-gate /* event from master side stdout */
8677c478bd9Sstevel@tonic-gate if (pollfds[0].revents) {
8687c478bd9Sstevel@tonic-gate if (pollfds[0].revents &
8697c478bd9Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
870b52a7fd7Sgjelinek if (process_output(stdout_fd, STDOUT_FILENO)
871b52a7fd7Sgjelinek != 0)
8727c478bd9Sstevel@tonic-gate break;
8737c478bd9Sstevel@tonic-gate } else {
8747c478bd9Sstevel@tonic-gate pollerr = pollfds[0].revents;
8757c478bd9Sstevel@tonic-gate break;
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate /* event from master side stderr */
8807c478bd9Sstevel@tonic-gate if (pollfds[1].revents) {
8817c478bd9Sstevel@tonic-gate if (pollfds[1].revents &
8827c478bd9Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
883b52a7fd7Sgjelinek if (process_output(stderr_fd, STDERR_FILENO)
884b52a7fd7Sgjelinek != 0)
8857c478bd9Sstevel@tonic-gate break;
8867c478bd9Sstevel@tonic-gate } else {
8877c478bd9Sstevel@tonic-gate pollerr = pollfds[1].revents;
8887c478bd9Sstevel@tonic-gate break;
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate
8927c478bd9Sstevel@tonic-gate /* event from user STDIN side */
8937c478bd9Sstevel@tonic-gate if (pollfds[2].revents) {
8947c478bd9Sstevel@tonic-gate if (pollfds[2].revents &
8957c478bd9Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
8967c478bd9Sstevel@tonic-gate /*
8977c478bd9Sstevel@tonic-gate * stdin fd is stdin of the target; so,
8987c478bd9Sstevel@tonic-gate * the thing we'll write the user data *to*.
8997c478bd9Sstevel@tonic-gate *
9007c478bd9Sstevel@tonic-gate * Also, unlike on the output side, we
901ff17c8bfSgjelinek * close the pipe on a zero-length message.
9027c478bd9Sstevel@tonic-gate */
903ff17c8bfSgjelinek int res;
904ff17c8bfSgjelinek
905ff17c8bfSgjelinek if (raw_mode)
906ff17c8bfSgjelinek res = process_raw_input(stdin_fd,
907ff17c8bfSgjelinek appin_fd);
908ff17c8bfSgjelinek else
909ff17c8bfSgjelinek res = process_user_input(stdin_fd,
910ff17c8bfSgjelinek stdout_fd);
911b52a7fd7Sgjelinek
912ff17c8bfSgjelinek if (res < 0)
913ff17c8bfSgjelinek break;
914ff17c8bfSgjelinek if (res > 0) {
915ff17c8bfSgjelinek /* EOF (close) child's stdin_fd */
916ff17c8bfSgjelinek pollfds[2].fd = -1;
917ff17c8bfSgjelinek while ((res = close(stdin_fd)) != 0 &&
918ff17c8bfSgjelinek errno == EINTR)
919ff17c8bfSgjelinek ;
920ff17c8bfSgjelinek if (res != 0)
9217c478bd9Sstevel@tonic-gate break;
9227c478bd9Sstevel@tonic-gate }
923ff17c8bfSgjelinek
924ff17c8bfSgjelinek } else if (raw_mode && pollfds[2].revents & POLLHUP) {
9257c478bd9Sstevel@tonic-gate /*
9267c478bd9Sstevel@tonic-gate * It's OK to get a POLLHUP on STDIN-- it
9277c478bd9Sstevel@tonic-gate * always happens if you do:
9287c478bd9Sstevel@tonic-gate *
9297c478bd9Sstevel@tonic-gate * echo foo | zlogin <zone> <command>
9307c478bd9Sstevel@tonic-gate *
9317c478bd9Sstevel@tonic-gate * We reset fd to -1 in this case to clear
932ff17c8bfSgjelinek * the condition and close the pipe (EOF) to
933ff17c8bfSgjelinek * the other side in order to wrap things up.
9347c478bd9Sstevel@tonic-gate */
935ff17c8bfSgjelinek int res;
936ff17c8bfSgjelinek
9377c478bd9Sstevel@tonic-gate pollfds[2].fd = -1;
938ff17c8bfSgjelinek while ((res = close(stdin_fd)) != 0 &&
939ff17c8bfSgjelinek errno == EINTR)
940ff17c8bfSgjelinek ;
941ff17c8bfSgjelinek if (res != 0)
942ff17c8bfSgjelinek break;
9437c478bd9Sstevel@tonic-gate } else {
9447c478bd9Sstevel@tonic-gate pollerr = pollfds[2].revents;
9457c478bd9Sstevel@tonic-gate break;
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate /*
9517c478bd9Sstevel@tonic-gate * We are in the midst of dying, but try to poll with a short
9527c478bd9Sstevel@tonic-gate * timeout to see if we can catch the last bit of I/O from the
9537c478bd9Sstevel@tonic-gate * children.
9547c478bd9Sstevel@tonic-gate */
955b52a7fd7Sgjelinek retry:
956b52a7fd7Sgjelinek pollfds[0].revents = pollfds[1].revents = 0;
957b52a7fd7Sgjelinek (void) poll(pollfds, 2, 100);
9587c478bd9Sstevel@tonic-gate if (pollfds[0].revents &
9597c478bd9Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
960b52a7fd7Sgjelinek if ((cc = read(stdout_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) {
9617c478bd9Sstevel@tonic-gate (void) write(STDOUT_FILENO, ibuf, cc);
962b52a7fd7Sgjelinek goto retry;
963b52a7fd7Sgjelinek }
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate if (pollfds[1].revents &
9667c478bd9Sstevel@tonic-gate (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
967b52a7fd7Sgjelinek if ((cc = read(stderr_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) {
9687c478bd9Sstevel@tonic-gate (void) write(STDERR_FILENO, ibuf, cc);
969b52a7fd7Sgjelinek goto retry;
970b52a7fd7Sgjelinek }
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate }
9737c478bd9Sstevel@tonic-gate
974858a4b99Ssl /*
975*bbf21555SRichard Lowe * Fetch the user_cmd brand hook for getting a user's passwd(5) entry.
976858a4b99Ssl */
977858a4b99Ssl static const char *
zone_get_user_cmd(brand_handle_t bh,const char * login,char * user_cmd,size_t len)978858a4b99Ssl zone_get_user_cmd(brand_handle_t bh, const char *login, char *user_cmd,
979858a4b99Ssl size_t len)
980858a4b99Ssl {
981858a4b99Ssl bzero(user_cmd, sizeof (user_cmd));
982858a4b99Ssl if (brand_get_user_cmd(bh, login, user_cmd, len) != 0)
983858a4b99Ssl return (NULL);
984858a4b99Ssl
985858a4b99Ssl return (user_cmd);
986858a4b99Ssl }
987858a4b99Ssl
988858a4b99Ssl /* From libc */
989858a4b99Ssl extern int str2passwd(const char *, int, void *, char *, int);
990858a4b99Ssl
991858a4b99Ssl /*
992858a4b99Ssl * exec() the user_cmd brand hook, and convert the output string to a
993858a4b99Ssl * struct passwd. This is to be called after zone_enter().
994858a4b99Ssl *
995858a4b99Ssl */
996858a4b99Ssl static struct passwd *
zone_get_user_pw(const char * user_cmd,struct passwd * pwent,char * pwbuf,int pwbuflen)997858a4b99Ssl zone_get_user_pw(const char *user_cmd, struct passwd *pwent, char *pwbuf,
998858a4b99Ssl int pwbuflen)
999858a4b99Ssl {
1000858a4b99Ssl char pwline[NSS_BUFLEN_PASSWD];
1001858a4b99Ssl char *cin = NULL;
1002858a4b99Ssl FILE *fin;
1003858a4b99Ssl int status;
1004858a4b99Ssl
1005858a4b99Ssl assert(getzoneid() != GLOBAL_ZONEID);
1006858a4b99Ssl
1007858a4b99Ssl if ((fin = popen(user_cmd, "r")) == NULL)
1008858a4b99Ssl return (NULL);
1009858a4b99Ssl
1010858a4b99Ssl while (cin == NULL && !feof(fin))
1011858a4b99Ssl cin = fgets(pwline, sizeof (pwline), fin);
1012858a4b99Ssl
1013858a4b99Ssl if (cin == NULL) {
1014858a4b99Ssl (void) pclose(fin);
1015858a4b99Ssl return (NULL);
1016858a4b99Ssl }
1017858a4b99Ssl
1018858a4b99Ssl status = pclose(fin);
1019858a4b99Ssl if (!WIFEXITED(status))
1020858a4b99Ssl return (NULL);
1021858a4b99Ssl if (WEXITSTATUS(status) != 0)
1022858a4b99Ssl return (NULL);
1023858a4b99Ssl
1024858a4b99Ssl if (str2passwd(pwline, sizeof (pwline), pwent, pwbuf, pwbuflen) == 0)
1025858a4b99Ssl return (pwent);
1026858a4b99Ssl else
1027858a4b99Ssl return (NULL);
1028858a4b99Ssl }
1029858a4b99Ssl
10309acbbeafSnn static char **
zone_login_cmd(brand_handle_t bh,const char * login)1031123807fbSedp zone_login_cmd(brand_handle_t bh, const char *login)
10329acbbeafSnn {
10339acbbeafSnn static char result_buf[ARG_MAX];
10349acbbeafSnn char **new_argv, *ptr, *lasts;
10359acbbeafSnn int n, a;
10369acbbeafSnn
10379acbbeafSnn /* Get the login command for the target zone. */
10389acbbeafSnn bzero(result_buf, sizeof (result_buf));
1039cb8a054bSGlenn Faden
1040cb8a054bSGlenn Faden if (forced_login) {
1041cb8a054bSGlenn Faden if (brand_get_forcedlogin_cmd(bh, login,
1042cb8a054bSGlenn Faden result_buf, sizeof (result_buf)) != 0)
1043cb8a054bSGlenn Faden return (NULL);
1044cb8a054bSGlenn Faden } else {
1045cb8a054bSGlenn Faden if (brand_get_login_cmd(bh, login,
1046cb8a054bSGlenn Faden result_buf, sizeof (result_buf)) != 0)
1047cb8a054bSGlenn Faden return (NULL);
1048cb8a054bSGlenn Faden }
10499acbbeafSnn
10509acbbeafSnn /*
10519acbbeafSnn * We got back a string that we'd like to execute. But since
10529acbbeafSnn * we're not doing the execution via a shell we'll need to convert
10539acbbeafSnn * the exec string to an array of strings. We'll do that here
10549acbbeafSnn * but we're going to be very simplistic about it and break stuff
10559acbbeafSnn * up based on spaces. We're not even going to support any kind
10569acbbeafSnn * of quoting or escape characters. It's truly amazing that
10579acbbeafSnn * there is no library function in OpenSolaris to do this for us.
10589acbbeafSnn */
10599acbbeafSnn
10609acbbeafSnn /*
10619acbbeafSnn * Be paranoid. Since we're deliniating based on spaces make
10629acbbeafSnn * sure there are no adjacent spaces.
10639acbbeafSnn */
10649acbbeafSnn if (strstr(result_buf, " ") != NULL)
10659acbbeafSnn return (NULL);
10669acbbeafSnn
10679acbbeafSnn /* Remove any trailing whitespace. */
10689acbbeafSnn n = strlen(result_buf);
10699acbbeafSnn if (result_buf[n - 1] == ' ')
10709acbbeafSnn result_buf[n - 1] = '\0';
10719acbbeafSnn
10729acbbeafSnn /* Count how many elements there are in the exec string. */
10739acbbeafSnn ptr = result_buf;
10749acbbeafSnn for (n = 2; ((ptr = strchr(ptr + 1, (int)' ')) != NULL); n++)
10759acbbeafSnn ;
10769acbbeafSnn
10779acbbeafSnn /* Allocate the argv array that we're going to return. */
10789acbbeafSnn if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
10799acbbeafSnn return (NULL);
10809acbbeafSnn
10819acbbeafSnn /* Tokenize the exec string and return. */
10829acbbeafSnn a = 0;
10839acbbeafSnn new_argv[a++] = result_buf;
10849acbbeafSnn if (n > 2) {
10859acbbeafSnn (void) strtok_r(result_buf, " ", &lasts);
10869acbbeafSnn while ((new_argv[a++] = strtok_r(NULL, " ", &lasts)) != NULL)
10879acbbeafSnn ;
10889acbbeafSnn } else {
10899acbbeafSnn new_argv[a++] = NULL;
10909acbbeafSnn }
10919acbbeafSnn assert(n == a);
10929acbbeafSnn return (new_argv);
10939acbbeafSnn }
10949acbbeafSnn
10957c478bd9Sstevel@tonic-gate /*
10967c478bd9Sstevel@tonic-gate * Prepare argv array for exec'd process; if we're passing commands to the
1097*bbf21555SRichard Lowe * new process, then use su(8) to do the invocation. Otherwise, use
10987c478bd9Sstevel@tonic-gate * 'login -z <from_zonename> -f' (-z is an undocumented option which tells
10997c478bd9Sstevel@tonic-gate * login that we're coming from another zone, and to disregard its CONSOLE
11007c478bd9Sstevel@tonic-gate * checks).
11017c478bd9Sstevel@tonic-gate */
11027c478bd9Sstevel@tonic-gate static char **
prep_args(brand_handle_t bh,const char * login,char ** argv)1103123807fbSedp prep_args(brand_handle_t bh, const char *login, char **argv)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate int argc = 0, a = 0, i, n = -1;
11067c478bd9Sstevel@tonic-gate char **new_argv;
11077c478bd9Sstevel@tonic-gate
11087c478bd9Sstevel@tonic-gate if (argv != NULL) {
11097c478bd9Sstevel@tonic-gate size_t subshell_len = 1;
11107c478bd9Sstevel@tonic-gate char *subshell;
11117c478bd9Sstevel@tonic-gate
11127c478bd9Sstevel@tonic-gate while (argv[argc] != NULL)
11137c478bd9Sstevel@tonic-gate argc++;
11147c478bd9Sstevel@tonic-gate
11157c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++) {
11167c478bd9Sstevel@tonic-gate subshell_len += strlen(argv[i]) + 1;
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate if ((subshell = calloc(1, subshell_len)) == NULL)
11197c478bd9Sstevel@tonic-gate return (NULL);
11207c478bd9Sstevel@tonic-gate
11217c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++) {
11227c478bd9Sstevel@tonic-gate (void) strcat(subshell, argv[i]);
11237c478bd9Sstevel@tonic-gate (void) strcat(subshell, " ");
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate
11267c478bd9Sstevel@tonic-gate if (failsafe) {
11277c478bd9Sstevel@tonic-gate n = 4;
11287c478bd9Sstevel@tonic-gate if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
11297c478bd9Sstevel@tonic-gate return (NULL);
11307c478bd9Sstevel@tonic-gate
11317c478bd9Sstevel@tonic-gate new_argv[a++] = FAILSAFESHELL;
11327c478bd9Sstevel@tonic-gate } else {
11337c478bd9Sstevel@tonic-gate n = 5;
11347c478bd9Sstevel@tonic-gate if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
11357c478bd9Sstevel@tonic-gate return (NULL);
11367c478bd9Sstevel@tonic-gate
11377c478bd9Sstevel@tonic-gate new_argv[a++] = SUPATH;
1138cb8a054bSGlenn Faden if (strcmp(login, "root") != 0) {
1139cb8a054bSGlenn Faden new_argv[a++] = "-";
1140cb8a054bSGlenn Faden n++;
1141cb8a054bSGlenn Faden }
11429acbbeafSnn new_argv[a++] = (char *)login;
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate new_argv[a++] = "-c";
11457c478bd9Sstevel@tonic-gate new_argv[a++] = subshell;
11467c478bd9Sstevel@tonic-gate new_argv[a++] = NULL;
11477c478bd9Sstevel@tonic-gate assert(a == n);
11487c478bd9Sstevel@tonic-gate } else {
11497c478bd9Sstevel@tonic-gate if (failsafe) {
11507c478bd9Sstevel@tonic-gate n = 2;
11517c478bd9Sstevel@tonic-gate if ((new_argv = malloc(sizeof (char *) * n)) == NULL)
11527c478bd9Sstevel@tonic-gate return (NULL);
11537c478bd9Sstevel@tonic-gate new_argv[a++] = FAILSAFESHELL;
11547c478bd9Sstevel@tonic-gate new_argv[a++] = NULL;
11559acbbeafSnn assert(n == a);
11567c478bd9Sstevel@tonic-gate } else {
1157123807fbSedp new_argv = zone_login_cmd(bh, login);
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate }
11609acbbeafSnn
11617c478bd9Sstevel@tonic-gate return (new_argv);
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate
11647c478bd9Sstevel@tonic-gate /*
11657c478bd9Sstevel@tonic-gate * Helper routine for prep_env below.
11667c478bd9Sstevel@tonic-gate */
11677c478bd9Sstevel@tonic-gate static char *
add_env(char * name,char * value)11687c478bd9Sstevel@tonic-gate add_env(char *name, char *value)
11697c478bd9Sstevel@tonic-gate {
11707c478bd9Sstevel@tonic-gate size_t sz = strlen(name) + strlen(value) + 2; /* name, =, value, NUL */
11717c478bd9Sstevel@tonic-gate char *str;
11727c478bd9Sstevel@tonic-gate
11737c478bd9Sstevel@tonic-gate if ((str = malloc(sz)) == NULL)
11747c478bd9Sstevel@tonic-gate return (NULL);
11757c478bd9Sstevel@tonic-gate
11767c478bd9Sstevel@tonic-gate (void) snprintf(str, sz, "%s=%s", name, value);
11777c478bd9Sstevel@tonic-gate return (str);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate
11807c478bd9Sstevel@tonic-gate /*
11817c478bd9Sstevel@tonic-gate * Prepare envp array for exec'd process.
11827c478bd9Sstevel@tonic-gate */
11837c478bd9Sstevel@tonic-gate static char **
prep_env()11847c478bd9Sstevel@tonic-gate prep_env()
11857c478bd9Sstevel@tonic-gate {
11867c478bd9Sstevel@tonic-gate int e = 0, size = 1;
11877c478bd9Sstevel@tonic-gate char **new_env, *estr;
11887c478bd9Sstevel@tonic-gate char *term = getenv("TERM");
11897c478bd9Sstevel@tonic-gate
11907c478bd9Sstevel@tonic-gate size++; /* for $PATH */
11917c478bd9Sstevel@tonic-gate if (term != NULL)
11927c478bd9Sstevel@tonic-gate size++;
11937c478bd9Sstevel@tonic-gate
11947c478bd9Sstevel@tonic-gate /*
11957c478bd9Sstevel@tonic-gate * In failsafe mode we set $HOME, since '-l' isn't valid in this mode.
11967c478bd9Sstevel@tonic-gate * We also set $SHELL, since neither login nor su will be around to do
11977c478bd9Sstevel@tonic-gate * it.
11987c478bd9Sstevel@tonic-gate */
11997c478bd9Sstevel@tonic-gate if (failsafe)
12007c478bd9Sstevel@tonic-gate size += 2;
12017c478bd9Sstevel@tonic-gate
12027c478bd9Sstevel@tonic-gate if ((new_env = malloc(sizeof (char *) * size)) == NULL)
12037c478bd9Sstevel@tonic-gate return (NULL);
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate if ((estr = add_env("PATH", DEF_PATH)) == NULL)
12067c478bd9Sstevel@tonic-gate return (NULL);
12077c478bd9Sstevel@tonic-gate new_env[e++] = estr;
12087c478bd9Sstevel@tonic-gate
12097c478bd9Sstevel@tonic-gate if (term != NULL) {
12107c478bd9Sstevel@tonic-gate if ((estr = add_env("TERM", term)) == NULL)
12117c478bd9Sstevel@tonic-gate return (NULL);
12127c478bd9Sstevel@tonic-gate new_env[e++] = estr;
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate
12157c478bd9Sstevel@tonic-gate if (failsafe) {
12167c478bd9Sstevel@tonic-gate if ((estr = add_env("HOME", "/")) == NULL)
12177c478bd9Sstevel@tonic-gate return (NULL);
12187c478bd9Sstevel@tonic-gate new_env[e++] = estr;
12197c478bd9Sstevel@tonic-gate
12207c478bd9Sstevel@tonic-gate if ((estr = add_env("SHELL", FAILSAFESHELL)) == NULL)
12217c478bd9Sstevel@tonic-gate return (NULL);
12227c478bd9Sstevel@tonic-gate new_env[e++] = estr;
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate
12257c478bd9Sstevel@tonic-gate new_env[e++] = NULL;
12267c478bd9Sstevel@tonic-gate
12277c478bd9Sstevel@tonic-gate assert(e == size);
12287c478bd9Sstevel@tonic-gate
12297c478bd9Sstevel@tonic-gate return (new_env);
12307c478bd9Sstevel@tonic-gate }
12317c478bd9Sstevel@tonic-gate
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate * Finish the preparation of the envp array for exec'd non-interactive
12347c478bd9Sstevel@tonic-gate * zlogins. This is called in the child process *after* we zone_enter(), since
12357c478bd9Sstevel@tonic-gate * it derives things we can only know within the zone, such as $HOME, $SHELL,
12367c478bd9Sstevel@tonic-gate * etc. We need only do this in the non-interactive, mode, since otherwise
12377c478bd9Sstevel@tonic-gate * login(1) will do it. We don't do this in failsafe mode, since it presents
12387c478bd9Sstevel@tonic-gate * additional ways in which the command could fail, and we'd prefer to avoid
12397c478bd9Sstevel@tonic-gate * that.
12407c478bd9Sstevel@tonic-gate */
12417c478bd9Sstevel@tonic-gate static char **
prep_env_noninteractive(const char * user_cmd,char ** env)1242858a4b99Ssl prep_env_noninteractive(const char *user_cmd, char **env)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate size_t size;
12457c478bd9Sstevel@tonic-gate char **new_env;
12467c478bd9Sstevel@tonic-gate int e, i;
12477c478bd9Sstevel@tonic-gate char *estr;
12487c478bd9Sstevel@tonic-gate char varmail[LOGNAME_MAX + 11]; /* strlen(/var/mail/) = 10, NUL */
1249858a4b99Ssl char pwbuf[NSS_BUFLEN_PASSWD + 1];
1250858a4b99Ssl struct passwd pwent;
1251858a4b99Ssl struct passwd *pw = NULL;
12527c478bd9Sstevel@tonic-gate
12537c478bd9Sstevel@tonic-gate assert(env != NULL);
12547c478bd9Sstevel@tonic-gate assert(failsafe == 0);
12557c478bd9Sstevel@tonic-gate
1256858a4b99Ssl /*
1257858a4b99Ssl * Exec the "user_cmd" brand hook to get a pwent for the
1258858a4b99Ssl * login user. If this fails, HOME will be set to "/", SHELL
1259858a4b99Ssl * will be set to $DEFAULTSHELL, and we will continue to exec
1260858a4b99Ssl * SUPATH <login> -c <cmd>.
1261858a4b99Ssl */
1262858a4b99Ssl pw = zone_get_user_pw(user_cmd, &pwent, pwbuf, sizeof (pwbuf));
1263858a4b99Ssl
12647c478bd9Sstevel@tonic-gate /*
12657c478bd9Sstevel@tonic-gate * Get existing envp size.
12667c478bd9Sstevel@tonic-gate */
12677c478bd9Sstevel@tonic-gate for (size = 0; env[size] != NULL; size++)
12687c478bd9Sstevel@tonic-gate ;
1269858a4b99Ssl
12707c478bd9Sstevel@tonic-gate e = size;
12717c478bd9Sstevel@tonic-gate
12727c478bd9Sstevel@tonic-gate /*
12737c478bd9Sstevel@tonic-gate * Finish filling out the environment; we duplicate the environment
12747c478bd9Sstevel@tonic-gate * setup described in login(1), for lack of a better precedent.
12757c478bd9Sstevel@tonic-gate */
1276858a4b99Ssl if (pw != NULL)
12777c478bd9Sstevel@tonic-gate size += 3; /* LOGNAME, HOME, MAIL */
1278858a4b99Ssl else
1279858a4b99Ssl size += 1; /* HOME */
1280858a4b99Ssl
12817c478bd9Sstevel@tonic-gate size++; /* always fill in SHELL */
12827c478bd9Sstevel@tonic-gate size++; /* terminating NULL */
12837c478bd9Sstevel@tonic-gate
12847c478bd9Sstevel@tonic-gate if ((new_env = malloc(sizeof (char *) * size)) == NULL)
12857c478bd9Sstevel@tonic-gate goto malloc_fail;
12867c478bd9Sstevel@tonic-gate
12877c478bd9Sstevel@tonic-gate /*
12887c478bd9Sstevel@tonic-gate * Copy existing elements of env into new_env.
12897c478bd9Sstevel@tonic-gate */
12907c478bd9Sstevel@tonic-gate for (i = 0; env[i] != NULL; i++) {
12917c478bd9Sstevel@tonic-gate if ((new_env[i] = strdup(env[i])) == NULL)
12927c478bd9Sstevel@tonic-gate goto malloc_fail;
12937c478bd9Sstevel@tonic-gate }
12947c478bd9Sstevel@tonic-gate assert(e == i);
12957c478bd9Sstevel@tonic-gate
12967c478bd9Sstevel@tonic-gate if (pw != NULL) {
12977c478bd9Sstevel@tonic-gate if ((estr = add_env("LOGNAME", pw->pw_name)) == NULL)
12987c478bd9Sstevel@tonic-gate goto malloc_fail;
12997c478bd9Sstevel@tonic-gate new_env[e++] = estr;
13007c478bd9Sstevel@tonic-gate
13017c478bd9Sstevel@tonic-gate if ((estr = add_env("HOME", pw->pw_dir)) == NULL)
13027c478bd9Sstevel@tonic-gate goto malloc_fail;
13037c478bd9Sstevel@tonic-gate new_env[e++] = estr;
13047c478bd9Sstevel@tonic-gate
13057c478bd9Sstevel@tonic-gate if (chdir(pw->pw_dir) != 0)
13067c478bd9Sstevel@tonic-gate zerror(gettext("Could not chdir to home directory "
13077c478bd9Sstevel@tonic-gate "%s: %s"), pw->pw_dir, strerror(errno));
13087c478bd9Sstevel@tonic-gate
13097c478bd9Sstevel@tonic-gate (void) snprintf(varmail, sizeof (varmail), "/var/mail/%s",
13107c478bd9Sstevel@tonic-gate pw->pw_name);
13117c478bd9Sstevel@tonic-gate if ((estr = add_env("MAIL", varmail)) == NULL)
13127c478bd9Sstevel@tonic-gate goto malloc_fail;
13137c478bd9Sstevel@tonic-gate new_env[e++] = estr;
1314858a4b99Ssl } else {
1315858a4b99Ssl if ((estr = add_env("HOME", "/")) == NULL)
1316858a4b99Ssl goto malloc_fail;
1317858a4b99Ssl new_env[e++] = estr;
13187c478bd9Sstevel@tonic-gate }
13197c478bd9Sstevel@tonic-gate
13207c478bd9Sstevel@tonic-gate if (pw != NULL && strlen(pw->pw_shell) > 0) {
13217c478bd9Sstevel@tonic-gate if ((estr = add_env("SHELL", pw->pw_shell)) == NULL)
13227c478bd9Sstevel@tonic-gate goto malloc_fail;
13237c478bd9Sstevel@tonic-gate new_env[e++] = estr;
13247c478bd9Sstevel@tonic-gate } else {
13257c478bd9Sstevel@tonic-gate if ((estr = add_env("SHELL", DEFAULTSHELL)) == NULL)
13267c478bd9Sstevel@tonic-gate goto malloc_fail;
13277c478bd9Sstevel@tonic-gate new_env[e++] = estr;
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate
13307c478bd9Sstevel@tonic-gate new_env[e++] = NULL; /* add terminating NULL */
13317c478bd9Sstevel@tonic-gate
13327c478bd9Sstevel@tonic-gate assert(e == size);
13337c478bd9Sstevel@tonic-gate return (new_env);
13347c478bd9Sstevel@tonic-gate
13357c478bd9Sstevel@tonic-gate malloc_fail:
13367c478bd9Sstevel@tonic-gate zperror(gettext("failed to allocate memory for process environment"));
13377c478bd9Sstevel@tonic-gate return (NULL);
13387c478bd9Sstevel@tonic-gate }
13397c478bd9Sstevel@tonic-gate
13407c478bd9Sstevel@tonic-gate static int
close_func(void * slavefd,int fd)13417c478bd9Sstevel@tonic-gate close_func(void *slavefd, int fd)
13427c478bd9Sstevel@tonic-gate {
13437c478bd9Sstevel@tonic-gate if (fd != *(int *)slavefd)
13447c478bd9Sstevel@tonic-gate (void) close(fd);
13457c478bd9Sstevel@tonic-gate return (0);
13467c478bd9Sstevel@tonic-gate }
13477c478bd9Sstevel@tonic-gate
13487c478bd9Sstevel@tonic-gate static void
set_cmdchar(char * cmdcharstr)13497c478bd9Sstevel@tonic-gate set_cmdchar(char *cmdcharstr)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate char c;
13527c478bd9Sstevel@tonic-gate long lc;
13537c478bd9Sstevel@tonic-gate
13547c478bd9Sstevel@tonic-gate if ((c = *cmdcharstr) != '\\') {
13557c478bd9Sstevel@tonic-gate cmdchar = c;
13567c478bd9Sstevel@tonic-gate return;
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate
13597c478bd9Sstevel@tonic-gate c = cmdcharstr[1];
13607c478bd9Sstevel@tonic-gate if (c == '\0' || c == '\\') {
13617c478bd9Sstevel@tonic-gate cmdchar = '\\';
13627c478bd9Sstevel@tonic-gate return;
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate
13657c478bd9Sstevel@tonic-gate if (c < '0' || c > '7') {
13667c478bd9Sstevel@tonic-gate zerror(gettext("Unrecognized escape character option %s"),
13677c478bd9Sstevel@tonic-gate cmdcharstr);
13687c478bd9Sstevel@tonic-gate usage();
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate
13717c478bd9Sstevel@tonic-gate lc = strtol(cmdcharstr + 1, NULL, 8);
13727c478bd9Sstevel@tonic-gate if (lc < 0 || lc > 255) {
13737c478bd9Sstevel@tonic-gate zerror(gettext("Octal escape character '%s' too large"),
13747c478bd9Sstevel@tonic-gate cmdcharstr);
13757c478bd9Sstevel@tonic-gate usage();
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate cmdchar = (char)lc;
13787c478bd9Sstevel@tonic-gate }
13797c478bd9Sstevel@tonic-gate
13807c478bd9Sstevel@tonic-gate static int
setup_utmpx(char * slavename)13817c478bd9Sstevel@tonic-gate setup_utmpx(char *slavename)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate struct utmpx ut;
13847c478bd9Sstevel@tonic-gate
13857c478bd9Sstevel@tonic-gate bzero(&ut, sizeof (ut));
13867c478bd9Sstevel@tonic-gate (void) strncpy(ut.ut_user, ".zlogin", sizeof (ut.ut_user));
13877c478bd9Sstevel@tonic-gate (void) strncpy(ut.ut_line, slavename, sizeof (ut.ut_line));
13887c478bd9Sstevel@tonic-gate ut.ut_pid = getpid();
13897c478bd9Sstevel@tonic-gate ut.ut_id[0] = 'z';
13907c478bd9Sstevel@tonic-gate ut.ut_id[1] = ut.ut_id[2] = ut.ut_id[3] = (char)SC_WILDC;
13917c478bd9Sstevel@tonic-gate ut.ut_type = LOGIN_PROCESS;
13927c478bd9Sstevel@tonic-gate (void) time(&ut.ut_tv.tv_sec);
13937c478bd9Sstevel@tonic-gate
13947c478bd9Sstevel@tonic-gate if (makeutx(&ut) == NULL) {
13957c478bd9Sstevel@tonic-gate zerror(gettext("makeutx failed"));
13967c478bd9Sstevel@tonic-gate return (-1);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate return (0);
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate
14017c478bd9Sstevel@tonic-gate static void
release_lock_file(int lockfd)14027c478bd9Sstevel@tonic-gate release_lock_file(int lockfd)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate (void) close(lockfd);
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate
14077c478bd9Sstevel@tonic-gate static int
grab_lock_file(const char * zone_name,int * lockfd)14087c478bd9Sstevel@tonic-gate grab_lock_file(const char *zone_name, int *lockfd)
14097c478bd9Sstevel@tonic-gate {
14107c478bd9Sstevel@tonic-gate char pathbuf[PATH_MAX];
14117c478bd9Sstevel@tonic-gate struct flock flock;
14127c478bd9Sstevel@tonic-gate
14137c478bd9Sstevel@tonic-gate if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
14147c478bd9Sstevel@tonic-gate zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR,
14157c478bd9Sstevel@tonic-gate strerror(errno));
14167c478bd9Sstevel@tonic-gate return (-1);
14177c478bd9Sstevel@tonic-gate }
14187c478bd9Sstevel@tonic-gate (void) chmod(ZONES_TMPDIR, S_IRWXU);
14197c478bd9Sstevel@tonic-gate (void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock",
14207c478bd9Sstevel@tonic-gate ZONES_TMPDIR, zone_name);
14217c478bd9Sstevel@tonic-gate
14227c478bd9Sstevel@tonic-gate if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) {
14237c478bd9Sstevel@tonic-gate zerror(gettext("could not open %s: %s"), pathbuf,
14247c478bd9Sstevel@tonic-gate strerror(errno));
14257c478bd9Sstevel@tonic-gate return (-1);
14267c478bd9Sstevel@tonic-gate }
14277c478bd9Sstevel@tonic-gate /*
14287c478bd9Sstevel@tonic-gate * Lock the file to synchronize with other zoneadmds
14297c478bd9Sstevel@tonic-gate */
14307c478bd9Sstevel@tonic-gate flock.l_type = F_WRLCK;
14317c478bd9Sstevel@tonic-gate flock.l_whence = SEEK_SET;
14327c478bd9Sstevel@tonic-gate flock.l_start = (off_t)0;
14337c478bd9Sstevel@tonic-gate flock.l_len = (off_t)0;
14347c478bd9Sstevel@tonic-gate if (fcntl(*lockfd, F_SETLKW, &flock) < 0) {
14357c478bd9Sstevel@tonic-gate zerror(gettext("unable to lock %s: %s"), pathbuf,
14367c478bd9Sstevel@tonic-gate strerror(errno));
14377c478bd9Sstevel@tonic-gate release_lock_file(*lockfd);
14387c478bd9Sstevel@tonic-gate return (-1);
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate return (Z_OK);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate
14437c478bd9Sstevel@tonic-gate static int
start_zoneadmd(const char * zone_name)14447c478bd9Sstevel@tonic-gate start_zoneadmd(const char *zone_name)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate pid_t retval;
14477c478bd9Sstevel@tonic-gate int pstatus = 0, error = -1, lockfd, doorfd;
14487c478bd9Sstevel@tonic-gate struct door_info info;
14497c478bd9Sstevel@tonic-gate char doorpath[MAXPATHLEN];
14507c478bd9Sstevel@tonic-gate
14517c478bd9Sstevel@tonic-gate (void) snprintf(doorpath, sizeof (doorpath), ZONE_DOOR_PATH, zone_name);
14527c478bd9Sstevel@tonic-gate
14537c478bd9Sstevel@tonic-gate if (grab_lock_file(zone_name, &lockfd) != Z_OK)
14547c478bd9Sstevel@tonic-gate return (-1);
14557c478bd9Sstevel@tonic-gate /*
14567c478bd9Sstevel@tonic-gate * We must do the door check with the lock held. Otherwise, we
14577c478bd9Sstevel@tonic-gate * might race against another zoneadm/zlogin process and wind
14587c478bd9Sstevel@tonic-gate * up with two processes trying to start zoneadmd at the same
14597c478bd9Sstevel@tonic-gate * time. zoneadmd will detect this, and fail, but we prefer this
14607c478bd9Sstevel@tonic-gate * to be as seamless as is practical, from a user perspective.
14617c478bd9Sstevel@tonic-gate */
14627c478bd9Sstevel@tonic-gate if ((doorfd = open(doorpath, O_RDONLY)) < 0) {
14637c478bd9Sstevel@tonic-gate if (errno != ENOENT) {
14647c478bd9Sstevel@tonic-gate zerror("failed to open %s: %s", doorpath,
14657c478bd9Sstevel@tonic-gate strerror(errno));
14667c478bd9Sstevel@tonic-gate goto out;
14677c478bd9Sstevel@tonic-gate }
14687c478bd9Sstevel@tonic-gate } else {
14697c478bd9Sstevel@tonic-gate /*
14707c478bd9Sstevel@tonic-gate * Seems to be working ok.
14717c478bd9Sstevel@tonic-gate */
14727c478bd9Sstevel@tonic-gate if (door_info(doorfd, &info) == 0 &&
14737c478bd9Sstevel@tonic-gate ((info.di_attributes & DOOR_REVOKED) == 0)) {
14747c478bd9Sstevel@tonic-gate error = 0;
14757c478bd9Sstevel@tonic-gate goto out;
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate
14797c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) {
14807c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"));
14817c478bd9Sstevel@tonic-gate goto out;
14827c478bd9Sstevel@tonic-gate } else if (child_pid == 0) {
14837c478bd9Sstevel@tonic-gate /* child process */
14847c478bd9Sstevel@tonic-gate (void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z",
14857c478bd9Sstevel@tonic-gate zone_name, NULL);
14867c478bd9Sstevel@tonic-gate zperror(gettext("could not exec zoneadmd"));
14877c478bd9Sstevel@tonic-gate _exit(1);
14887c478bd9Sstevel@tonic-gate }
14897c478bd9Sstevel@tonic-gate
14907c478bd9Sstevel@tonic-gate /* parent process */
14917c478bd9Sstevel@tonic-gate do {
14927c478bd9Sstevel@tonic-gate retval = waitpid(child_pid, &pstatus, 0);
14937c478bd9Sstevel@tonic-gate } while (retval != child_pid);
14947c478bd9Sstevel@tonic-gate if (WIFSIGNALED(pstatus) ||
14957c478bd9Sstevel@tonic-gate (WIFEXITED(pstatus) && WEXITSTATUS(pstatus) != 0)) {
14967c478bd9Sstevel@tonic-gate zerror(gettext("could not start %s"), "zoneadmd");
14977c478bd9Sstevel@tonic-gate goto out;
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate error = 0;
15007c478bd9Sstevel@tonic-gate out:
15017c478bd9Sstevel@tonic-gate release_lock_file(lockfd);
15027c478bd9Sstevel@tonic-gate (void) close(doorfd);
15037c478bd9Sstevel@tonic-gate return (error);
15047c478bd9Sstevel@tonic-gate }
15057c478bd9Sstevel@tonic-gate
15067c478bd9Sstevel@tonic-gate static int
init_template(void)15077c478bd9Sstevel@tonic-gate init_template(void)
15087c478bd9Sstevel@tonic-gate {
15097c478bd9Sstevel@tonic-gate int fd;
15107c478bd9Sstevel@tonic-gate int err = 0;
15117c478bd9Sstevel@tonic-gate
15127c478bd9Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/template", O_RDWR);
15137c478bd9Sstevel@tonic-gate if (fd == -1)
15147c478bd9Sstevel@tonic-gate return (-1);
15157c478bd9Sstevel@tonic-gate
15167c478bd9Sstevel@tonic-gate /*
15177c478bd9Sstevel@tonic-gate * zlogin doesn't do anything with the contract.
15187c478bd9Sstevel@tonic-gate * Deliver no events, don't inherit, and allow it to be orphaned.
15197c478bd9Sstevel@tonic-gate */
15207c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_critical(fd, 0);
15217c478bd9Sstevel@tonic-gate err |= ct_tmpl_set_informative(fd, 0);
15227c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
15237c478bd9Sstevel@tonic-gate err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
15247c478bd9Sstevel@tonic-gate if (err || ct_tmpl_activate(fd)) {
15257c478bd9Sstevel@tonic-gate (void) close(fd);
15267c478bd9Sstevel@tonic-gate return (-1);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate
15297c478bd9Sstevel@tonic-gate return (fd);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate
15327c478bd9Sstevel@tonic-gate static int
noninteractive_login(char * zonename,const char * user_cmd,zoneid_t zoneid,char ** new_args,char ** new_env)1533858a4b99Ssl noninteractive_login(char *zonename, const char *user_cmd, zoneid_t zoneid,
15347c478bd9Sstevel@tonic-gate char **new_args, char **new_env)
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate pid_t retval;
1537ff17c8bfSgjelinek int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2], dead_child_pipe[2];
15387c478bd9Sstevel@tonic-gate int child_status;
15397c478bd9Sstevel@tonic-gate int tmpl_fd;
15407c478bd9Sstevel@tonic-gate sigset_t block_cld;
15417c478bd9Sstevel@tonic-gate
15427c478bd9Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) {
15437c478bd9Sstevel@tonic-gate reset_tty();
15447c478bd9Sstevel@tonic-gate zperror(gettext("could not create contract"));
15457c478bd9Sstevel@tonic-gate return (1);
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate
15487c478bd9Sstevel@tonic-gate if (pipe(stdin_pipe) != 0) {
15497c478bd9Sstevel@tonic-gate zperror(gettext("could not create STDIN pipe"));
15507c478bd9Sstevel@tonic-gate return (1);
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate /*
15537c478bd9Sstevel@tonic-gate * When the user types ^D, we get a zero length message on STDIN.
15547c478bd9Sstevel@tonic-gate * We need to echo that down the pipe to send it to the other side;
15557c478bd9Sstevel@tonic-gate * but by default, pipes don't propagate zero-length messages. We
1556*bbf21555SRichard Lowe * toggle that behavior off using I_SWROPT. See streamio(4I).
15577c478bd9Sstevel@tonic-gate */
15587c478bd9Sstevel@tonic-gate if (ioctl(stdin_pipe[0], I_SWROPT, SNDZERO) != 0) {
15597c478bd9Sstevel@tonic-gate zperror(gettext("could not configure STDIN pipe"));
15607c478bd9Sstevel@tonic-gate return (1);
15617c478bd9Sstevel@tonic-gate
15627c478bd9Sstevel@tonic-gate }
15637c478bd9Sstevel@tonic-gate if (pipe(stdout_pipe) != 0) {
15647c478bd9Sstevel@tonic-gate zperror(gettext("could not create STDOUT pipe"));
15657c478bd9Sstevel@tonic-gate return (1);
15667c478bd9Sstevel@tonic-gate }
15677c478bd9Sstevel@tonic-gate if (pipe(stderr_pipe) != 0) {
15687c478bd9Sstevel@tonic-gate zperror(gettext("could not create STDERR pipe"));
15697c478bd9Sstevel@tonic-gate return (1);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate
1572ff17c8bfSgjelinek if (pipe(dead_child_pipe) != 0) {
1573ff17c8bfSgjelinek zperror(gettext("could not create signalling pipe"));
1574ff17c8bfSgjelinek return (1);
1575ff17c8bfSgjelinek }
1576ff17c8bfSgjelinek close_on_sig = dead_child_pipe[0];
1577ff17c8bfSgjelinek
15787c478bd9Sstevel@tonic-gate /*
15797c478bd9Sstevel@tonic-gate * If any of the pipe FD's winds up being less than STDERR, then we
15807c478bd9Sstevel@tonic-gate * have a mess on our hands-- and we are lacking some of the I/O
15817c478bd9Sstevel@tonic-gate * streams we would expect anyway. So we bail.
15827c478bd9Sstevel@tonic-gate */
15837c478bd9Sstevel@tonic-gate if (stdin_pipe[0] <= STDERR_FILENO ||
15847c478bd9Sstevel@tonic-gate stdin_pipe[1] <= STDERR_FILENO ||
15857c478bd9Sstevel@tonic-gate stdout_pipe[0] <= STDERR_FILENO ||
15867c478bd9Sstevel@tonic-gate stdout_pipe[1] <= STDERR_FILENO ||
15877c478bd9Sstevel@tonic-gate stderr_pipe[0] <= STDERR_FILENO ||
1588ff17c8bfSgjelinek stderr_pipe[1] <= STDERR_FILENO ||
1589ff17c8bfSgjelinek dead_child_pipe[0] <= STDERR_FILENO ||
1590ff17c8bfSgjelinek dead_child_pipe[1] <= STDERR_FILENO) {
15917c478bd9Sstevel@tonic-gate zperror(gettext("process lacks valid STDIN, STDOUT, STDERR"));
15927c478bd9Sstevel@tonic-gate return (1);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate
15957c478bd9Sstevel@tonic-gate if (prefork_dropprivs() != 0) {
15967c478bd9Sstevel@tonic-gate zperror(gettext("could not allocate privilege set"));
15977c478bd9Sstevel@tonic-gate return (1);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate
16007c478bd9Sstevel@tonic-gate (void) sigset(SIGCLD, sigcld);
16017c478bd9Sstevel@tonic-gate (void) sigemptyset(&block_cld);
16027c478bd9Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCLD);
16037c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
16047c478bd9Sstevel@tonic-gate
16057c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) {
16067c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
16077c478bd9Sstevel@tonic-gate (void) close(tmpl_fd);
16087c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"));
16097c478bd9Sstevel@tonic-gate return (1);
16107c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { /* child process */
16117c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
16127c478bd9Sstevel@tonic-gate
16137c478bd9Sstevel@tonic-gate /*
16147c478bd9Sstevel@tonic-gate * Do a dance to get the pipes hooked up as FD's 0, 1 and 2.
16157c478bd9Sstevel@tonic-gate */
16167c478bd9Sstevel@tonic-gate (void) close(STDIN_FILENO);
16177c478bd9Sstevel@tonic-gate (void) close(STDOUT_FILENO);
16187c478bd9Sstevel@tonic-gate (void) close(STDERR_FILENO);
16197c478bd9Sstevel@tonic-gate (void) dup2(stdin_pipe[1], STDIN_FILENO);
16207c478bd9Sstevel@tonic-gate (void) dup2(stdout_pipe[1], STDOUT_FILENO);
16217c478bd9Sstevel@tonic-gate (void) dup2(stderr_pipe[1], STDERR_FILENO);
16227c478bd9Sstevel@tonic-gate (void) closefrom(STDERR_FILENO + 1);
16237c478bd9Sstevel@tonic-gate
16247c478bd9Sstevel@tonic-gate (void) sigset(SIGCLD, SIG_DFL);
16257c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
16267c478bd9Sstevel@tonic-gate /*
16277c478bd9Sstevel@tonic-gate * In case any of stdin, stdout or stderr are streams,
16287c478bd9Sstevel@tonic-gate * anchor them to prevent malicious I_POPs.
16297c478bd9Sstevel@tonic-gate */
16307c478bd9Sstevel@tonic-gate (void) ioctl(STDIN_FILENO, I_ANCHOR);
16317c478bd9Sstevel@tonic-gate (void) ioctl(STDOUT_FILENO, I_ANCHOR);
16327c478bd9Sstevel@tonic-gate (void) ioctl(STDERR_FILENO, I_ANCHOR);
16337c478bd9Sstevel@tonic-gate
16347c478bd9Sstevel@tonic-gate if (zone_enter(zoneid) == -1) {
16357c478bd9Sstevel@tonic-gate zerror(gettext("could not enter zone %s: %s"),
16367c478bd9Sstevel@tonic-gate zonename, strerror(errno));
16377c478bd9Sstevel@tonic-gate _exit(1);
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate
1640858a4b99Ssl /*
1641858a4b99Ssl * For non-native zones, tell libc where it can find locale
1642858a4b99Ssl * specific getttext() messages.
1643858a4b99Ssl */
16441100f00dSgjelinek if (access("/.SUNWnative/usr/lib/locale", R_OK) == 0)
16451100f00dSgjelinek (void) bindtextdomain(TEXT_DOMAIN,
16461100f00dSgjelinek "/.SUNWnative/usr/lib/locale");
16471100f00dSgjelinek else if (access("/native/usr/lib/locale", R_OK) == 0)
1648858a4b99Ssl (void) bindtextdomain(TEXT_DOMAIN,
1649858a4b99Ssl "/native/usr/lib/locale");
1650858a4b99Ssl
16517c478bd9Sstevel@tonic-gate if (!failsafe)
1652858a4b99Ssl new_env = prep_env_noninteractive(user_cmd, new_env);
16537c478bd9Sstevel@tonic-gate
16547c478bd9Sstevel@tonic-gate if (new_env == NULL) {
16557c478bd9Sstevel@tonic-gate _exit(1);
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate
16587c478bd9Sstevel@tonic-gate /*
16597c478bd9Sstevel@tonic-gate * Move into a new process group; the zone_enter will have
16607c478bd9Sstevel@tonic-gate * placed us into zsched's session, and we want to be in
16617c478bd9Sstevel@tonic-gate * a unique process group.
16627c478bd9Sstevel@tonic-gate */
16637c478bd9Sstevel@tonic-gate (void) setpgid(getpid(), getpid());
16647c478bd9Sstevel@tonic-gate
1665cb8a054bSGlenn Faden /*
1666cb8a054bSGlenn Faden * The child needs to run as root to
1667cb8a054bSGlenn Faden * execute the su program.
1668cb8a054bSGlenn Faden */
1669cb8a054bSGlenn Faden if (setuid(0) == -1) {
1670cb8a054bSGlenn Faden zperror(gettext("insufficient privilege"));
1671cb8a054bSGlenn Faden return (1);
1672cb8a054bSGlenn Faden }
1673cb8a054bSGlenn Faden
16747c478bd9Sstevel@tonic-gate (void) execve(new_args[0], new_args, new_env);
16757c478bd9Sstevel@tonic-gate zperror(gettext("exec failure"));
16767c478bd9Sstevel@tonic-gate _exit(1);
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate /* parent */
1679ff17c8bfSgjelinek
1680ff17c8bfSgjelinek /* close pipe sides written by child */
1681ff17c8bfSgjelinek (void) close(stdout_pipe[1]);
1682ff17c8bfSgjelinek (void) close(stderr_pipe[1]);
1683ff17c8bfSgjelinek
16847c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, sig_forward);
16857c478bd9Sstevel@tonic-gate
16867c478bd9Sstevel@tonic-gate postfork_dropprivs();
16877c478bd9Sstevel@tonic-gate
16887c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
16897c478bd9Sstevel@tonic-gate (void) close(tmpl_fd);
16907c478bd9Sstevel@tonic-gate
16917c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
1692b52a7fd7Sgjelinek doio(stdin_pipe[0], stdin_pipe[1], stdout_pipe[0], stderr_pipe[0],
1693ff17c8bfSgjelinek dead_child_pipe[1], B_TRUE);
16947c478bd9Sstevel@tonic-gate do {
16957c478bd9Sstevel@tonic-gate retval = waitpid(child_pid, &child_status, 0);
16967c478bd9Sstevel@tonic-gate if (retval == -1) {
16977c478bd9Sstevel@tonic-gate child_status = 0;
16987c478bd9Sstevel@tonic-gate }
16997c478bd9Sstevel@tonic-gate } while (retval != child_pid && errno != ECHILD);
17007c478bd9Sstevel@tonic-gate
17017c478bd9Sstevel@tonic-gate return (WEXITSTATUS(child_status));
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate
1704cb8a054bSGlenn Faden static char *
get_username()1705cb8a054bSGlenn Faden get_username()
1706cb8a054bSGlenn Faden {
1707cb8a054bSGlenn Faden uid_t uid;
1708cb8a054bSGlenn Faden struct passwd *nptr;
1709cb8a054bSGlenn Faden
1710cb8a054bSGlenn Faden /*
1711cb8a054bSGlenn Faden * Authorizations are checked to restrict access based on the
1712cb8a054bSGlenn Faden * requested operation and zone name, It is assumed that the
1713cb8a054bSGlenn Faden * program is running with all privileges, but that the real
1714cb8a054bSGlenn Faden * user ID is that of the user or role on whose behalf we are
1715cb8a054bSGlenn Faden * operating. So we start by getting the username that will be
1716cb8a054bSGlenn Faden * used for subsequent authorization checks.
1717cb8a054bSGlenn Faden */
1718cb8a054bSGlenn Faden
1719cb8a054bSGlenn Faden uid = getuid();
1720cb8a054bSGlenn Faden if ((nptr = getpwuid(uid)) == NULL) {
1721cb8a054bSGlenn Faden zerror(gettext("could not get user name."));
1722cb8a054bSGlenn Faden _exit(1);
1723cb8a054bSGlenn Faden }
1724cb8a054bSGlenn Faden return (nptr->pw_name);
1725cb8a054bSGlenn Faden }
1726cb8a054bSGlenn Faden
17277c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)17287c478bd9Sstevel@tonic-gate main(int argc, char **argv)
17297c478bd9Sstevel@tonic-gate {
17307c478bd9Sstevel@tonic-gate int arg, console = 0;
17317c478bd9Sstevel@tonic-gate zoneid_t zoneid;
17327c478bd9Sstevel@tonic-gate zone_state_t st;
17337c478bd9Sstevel@tonic-gate char *login = "root";
17347c478bd9Sstevel@tonic-gate int lflag = 0;
1735279721bfSGary Mills int nflag = 0;
17367c478bd9Sstevel@tonic-gate char *zonename = NULL;
17377c478bd9Sstevel@tonic-gate char **proc_args = NULL;
17387c478bd9Sstevel@tonic-gate char **new_args, **new_env;
17397c478bd9Sstevel@tonic-gate sigset_t block_cld;
1740facf4a8dSllai char devroot[MAXPATHLEN];
17417c478bd9Sstevel@tonic-gate char *slavename, slaveshortname[MAXPATHLEN];
17427c478bd9Sstevel@tonic-gate priv_set_t *privset;
17437c478bd9Sstevel@tonic-gate int tmpl_fd;
17449acbbeafSnn char zonebrand[MAXNAMELEN];
1745e5816e35SEdward Pilatowicz char default_brand[MAXNAMELEN];
1746108322fbScarlsonj struct stat sb;
1747108322fbScarlsonj char kernzone[ZONENAME_MAX];
1748123807fbSedp brand_handle_t bh;
1749858a4b99Ssl char user_cmd[MAXPATHLEN];
1750cb8a054bSGlenn Faden char authname[MAXAUTHS];
17517c478bd9Sstevel@tonic-gate
17527c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
17537c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
17547c478bd9Sstevel@tonic-gate
17557c478bd9Sstevel@tonic-gate (void) getpname(argv[0]);
1756cb8a054bSGlenn Faden username = get_username();
17577c478bd9Sstevel@tonic-gate
1758741343adSAlexander Eremin while ((arg = getopt(argc, argv, "dnECR:Se:l:Q")) != EOF) {
17597c478bd9Sstevel@tonic-gate switch (arg) {
17607c478bd9Sstevel@tonic-gate case 'C':
17617c478bd9Sstevel@tonic-gate console = 1;
17627c478bd9Sstevel@tonic-gate break;
17637c478bd9Sstevel@tonic-gate case 'E':
17647c478bd9Sstevel@tonic-gate nocmdchar = 1;
17657c478bd9Sstevel@tonic-gate break;
1766108322fbScarlsonj case 'R': /* undocumented */
1767108322fbScarlsonj if (*optarg != '/') {
1768108322fbScarlsonj zerror(gettext("root path must be absolute."));
1769108322fbScarlsonj exit(2);
1770108322fbScarlsonj }
1771108322fbScarlsonj if (stat(optarg, &sb) == -1 || !S_ISDIR(sb.st_mode)) {
1772108322fbScarlsonj zerror(
1773108322fbScarlsonj gettext("root path must be a directory."));
1774108322fbScarlsonj exit(2);
1775108322fbScarlsonj }
1776108322fbScarlsonj zonecfg_set_root(optarg);
1777108322fbScarlsonj break;
1778c2589d13SGarrett D'Amore case 'Q':
1779c2589d13SGarrett D'Amore quiet = 1;
1780c2589d13SGarrett D'Amore break;
17817c478bd9Sstevel@tonic-gate case 'S':
17827c478bd9Sstevel@tonic-gate failsafe = 1;
17837c478bd9Sstevel@tonic-gate break;
1784741343adSAlexander Eremin case 'd':
1785741343adSAlexander Eremin disconnect = 1;
1786741343adSAlexander Eremin break;
17877c478bd9Sstevel@tonic-gate case 'e':
17887c478bd9Sstevel@tonic-gate set_cmdchar(optarg);
17897c478bd9Sstevel@tonic-gate break;
17907c478bd9Sstevel@tonic-gate case 'l':
17917c478bd9Sstevel@tonic-gate login = optarg;
17927c478bd9Sstevel@tonic-gate lflag = 1;
17937c478bd9Sstevel@tonic-gate break;
1794279721bfSGary Mills case 'n':
1795279721bfSGary Mills nflag = 1;
1796279721bfSGary Mills break;
17977c478bd9Sstevel@tonic-gate default:
17987c478bd9Sstevel@tonic-gate usage();
17997c478bd9Sstevel@tonic-gate }
18007c478bd9Sstevel@tonic-gate }
18017c478bd9Sstevel@tonic-gate
1802279721bfSGary Mills if (console != 0) {
18037c478bd9Sstevel@tonic-gate
1804279721bfSGary Mills if (lflag != 0) {
1805279721bfSGary Mills zerror(gettext(
1806279721bfSGary Mills "-l may not be specified for console login"));
1807279721bfSGary Mills usage();
1808279721bfSGary Mills }
1809279721bfSGary Mills
1810279721bfSGary Mills if (nflag != 0) {
1811279721bfSGary Mills zerror(gettext(
1812279721bfSGary Mills "-n may not be specified for console login"));
1813279721bfSGary Mills usage();
1814279721bfSGary Mills }
1815279721bfSGary Mills
1816279721bfSGary Mills if (failsafe != 0) {
1817279721bfSGary Mills zerror(gettext(
1818279721bfSGary Mills "-S may not be specified for console login"));
1819279721bfSGary Mills usage();
1820279721bfSGary Mills }
1821279721bfSGary Mills
1822279721bfSGary Mills if (zonecfg_in_alt_root()) {
1823279721bfSGary Mills zerror(gettext(
1824279721bfSGary Mills "-R may not be specified for console login"));
1825279721bfSGary Mills exit(2);
1826279721bfSGary Mills }
18277c478bd9Sstevel@tonic-gate
1828108322fbScarlsonj }
1829108322fbScarlsonj
18307c478bd9Sstevel@tonic-gate if (failsafe != 0 && lflag != 0) {
18317c478bd9Sstevel@tonic-gate zerror(gettext("-l may not be specified for failsafe login"));
18327c478bd9Sstevel@tonic-gate usage();
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate
1835741343adSAlexander Eremin if (!console && disconnect != 0) {
1836741343adSAlexander Eremin zerror(gettext(
1837741343adSAlexander Eremin "-d may only be specified with console login"));
1838741343adSAlexander Eremin usage();
1839741343adSAlexander Eremin }
1840741343adSAlexander Eremin
18417c478bd9Sstevel@tonic-gate if (optind == (argc - 1)) {
18427c478bd9Sstevel@tonic-gate /*
18437c478bd9Sstevel@tonic-gate * zone name, no process name; this should be an interactive
18447c478bd9Sstevel@tonic-gate * as long as STDIN is really a tty.
18457c478bd9Sstevel@tonic-gate */
1846279721bfSGary Mills if (nflag != 0) {
1847279721bfSGary Mills zerror(gettext(
1848279721bfSGary Mills "-n may not be specified for interactive login"));
1849279721bfSGary Mills usage();
1850279721bfSGary Mills }
18517c478bd9Sstevel@tonic-gate if (isatty(STDIN_FILENO))
18527c478bd9Sstevel@tonic-gate interactive = 1;
18537c478bd9Sstevel@tonic-gate zonename = argv[optind];
18547c478bd9Sstevel@tonic-gate } else if (optind < (argc - 1)) {
18557c478bd9Sstevel@tonic-gate if (console) {
18567c478bd9Sstevel@tonic-gate zerror(gettext("Commands may not be specified for "
18577c478bd9Sstevel@tonic-gate "console login."));
18587c478bd9Sstevel@tonic-gate usage();
18597c478bd9Sstevel@tonic-gate }
18607c478bd9Sstevel@tonic-gate /* zone name and process name, and possibly some args */
18617c478bd9Sstevel@tonic-gate zonename = argv[optind];
18627c478bd9Sstevel@tonic-gate proc_args = &argv[optind + 1];
18637c478bd9Sstevel@tonic-gate interactive = 0;
18647c478bd9Sstevel@tonic-gate } else {
18657c478bd9Sstevel@tonic-gate usage();
18667c478bd9Sstevel@tonic-gate }
18677c478bd9Sstevel@tonic-gate
18687c478bd9Sstevel@tonic-gate if (getzoneid() != GLOBAL_ZONEID) {
18697c478bd9Sstevel@tonic-gate zerror(gettext("'%s' may only be used from the global zone"),
18707c478bd9Sstevel@tonic-gate pname);
18717c478bd9Sstevel@tonic-gate return (1);
18727c478bd9Sstevel@tonic-gate }
18737c478bd9Sstevel@tonic-gate
18747c478bd9Sstevel@tonic-gate if (strcmp(zonename, GLOBAL_ZONENAME) == 0) {
18757c478bd9Sstevel@tonic-gate zerror(gettext("'%s' not applicable to the global zone"),
18767c478bd9Sstevel@tonic-gate pname);
18777c478bd9Sstevel@tonic-gate return (1);
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate
18807c478bd9Sstevel@tonic-gate if (zone_get_state(zonename, &st) != Z_OK) {
18817c478bd9Sstevel@tonic-gate zerror(gettext("zone '%s' unknown"), zonename);
18827c478bd9Sstevel@tonic-gate return (1);
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate
18857c478bd9Sstevel@tonic-gate if (st < ZONE_STATE_INSTALLED) {
18867c478bd9Sstevel@tonic-gate zerror(gettext("cannot login to a zone which is '%s'"),
18877c478bd9Sstevel@tonic-gate zone_state_str(st));
18887c478bd9Sstevel@tonic-gate return (1);
18897c478bd9Sstevel@tonic-gate }
18907c478bd9Sstevel@tonic-gate
18917c478bd9Sstevel@tonic-gate /*
18927c478bd9Sstevel@tonic-gate * In both console and non-console cases, we require all privs.
18937c478bd9Sstevel@tonic-gate * In the console case, because we may need to startup zoneadmd.
18947c478bd9Sstevel@tonic-gate * In the non-console case in order to do zone_enter(2), zonept()
18957c478bd9Sstevel@tonic-gate * and other tasks.
18967c478bd9Sstevel@tonic-gate */
1897cb8a054bSGlenn Faden
18987c478bd9Sstevel@tonic-gate if ((privset = priv_allocset()) == NULL) {
18997c478bd9Sstevel@tonic-gate zperror(gettext("priv_allocset failed"));
19007c478bd9Sstevel@tonic-gate return (1);
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate
19037c478bd9Sstevel@tonic-gate if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
19047c478bd9Sstevel@tonic-gate zperror(gettext("getppriv failed"));
19057c478bd9Sstevel@tonic-gate priv_freeset(privset);
19067c478bd9Sstevel@tonic-gate return (1);
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate
19097c478bd9Sstevel@tonic-gate if (priv_isfullset(privset) == B_FALSE) {
19107c478bd9Sstevel@tonic-gate zerror(gettext("You lack sufficient privilege to run "
19117c478bd9Sstevel@tonic-gate "this command (all privs required)"));
19127c478bd9Sstevel@tonic-gate priv_freeset(privset);
19137c478bd9Sstevel@tonic-gate return (1);
19147c478bd9Sstevel@tonic-gate }
19157c478bd9Sstevel@tonic-gate priv_freeset(privset);
19167c478bd9Sstevel@tonic-gate
1917cb8a054bSGlenn Faden /*
1918cb8a054bSGlenn Faden * Check if user is authorized for requested usage of the zone
1919cb8a054bSGlenn Faden */
1920cb8a054bSGlenn Faden
1921cb8a054bSGlenn Faden (void) snprintf(authname, MAXAUTHS, "%s%s%s",
1922cb8a054bSGlenn Faden ZONE_MANAGE_AUTH, KV_OBJECT, zonename);
1923cb8a054bSGlenn Faden if (chkauthattr(authname, username) == 0) {
1924cb8a054bSGlenn Faden if (console) {
1925cb8a054bSGlenn Faden zerror(gettext("%s is not authorized for console "
1926cb8a054bSGlenn Faden "access to %s zone."),
1927cb8a054bSGlenn Faden username, zonename);
1928cb8a054bSGlenn Faden return (1);
1929cb8a054bSGlenn Faden } else {
1930cb8a054bSGlenn Faden (void) snprintf(authname, MAXAUTHS, "%s%s%s",
1931cb8a054bSGlenn Faden ZONE_LOGIN_AUTH, KV_OBJECT, zonename);
1932cb8a054bSGlenn Faden if (failsafe || !interactive) {
1933cb8a054bSGlenn Faden zerror(gettext("%s is not authorized for "
1934cb8a054bSGlenn Faden "failsafe or non-interactive login "
1935cb8a054bSGlenn Faden "to %s zone."), username, zonename);
1936cb8a054bSGlenn Faden return (1);
1937cb8a054bSGlenn Faden } else if (chkauthattr(authname, username) == 0) {
1938cb8a054bSGlenn Faden zerror(gettext("%s is not authorized "
1939cb8a054bSGlenn Faden " to login to %s zone."),
1940cb8a054bSGlenn Faden username, zonename);
1941cb8a054bSGlenn Faden return (1);
1942cb8a054bSGlenn Faden }
1943cb8a054bSGlenn Faden }
1944cb8a054bSGlenn Faden } else {
1945cb8a054bSGlenn Faden forced_login = B_TRUE;
1946cb8a054bSGlenn Faden }
1947cb8a054bSGlenn Faden
19487c478bd9Sstevel@tonic-gate /*
19497c478bd9Sstevel@tonic-gate * The console is a separate case from the rest of the code; handle
19507c478bd9Sstevel@tonic-gate * it first.
19517c478bd9Sstevel@tonic-gate */
19527c478bd9Sstevel@tonic-gate if (console) {
19537c478bd9Sstevel@tonic-gate /*
19547c478bd9Sstevel@tonic-gate * Ensure that zoneadmd for this zone is running.
19557c478bd9Sstevel@tonic-gate */
19567c478bd9Sstevel@tonic-gate if (start_zoneadmd(zonename) == -1)
19577c478bd9Sstevel@tonic-gate return (1);
19587c478bd9Sstevel@tonic-gate
19597c478bd9Sstevel@tonic-gate /*
19607c478bd9Sstevel@tonic-gate * Make contact with zoneadmd.
19617c478bd9Sstevel@tonic-gate */
19627c478bd9Sstevel@tonic-gate if (get_console_master(zonename) == -1)
19637c478bd9Sstevel@tonic-gate return (1);
19647c478bd9Sstevel@tonic-gate
1965c2589d13SGarrett D'Amore if (!quiet)
1966c2589d13SGarrett D'Amore (void) printf(
1967c2589d13SGarrett D'Amore gettext("[Connected to zone '%s' console]\n"),
1968c2589d13SGarrett D'Amore zonename);
19697c478bd9Sstevel@tonic-gate
19707c478bd9Sstevel@tonic-gate if (set_tty_rawmode(STDIN_FILENO) == -1) {
19717c478bd9Sstevel@tonic-gate reset_tty();
19727c478bd9Sstevel@tonic-gate zperror(gettext("failed to set stdin pty to raw mode"));
19737c478bd9Sstevel@tonic-gate return (1);
19747c478bd9Sstevel@tonic-gate }
19757c478bd9Sstevel@tonic-gate
19767c478bd9Sstevel@tonic-gate (void) sigset(SIGWINCH, sigwinch);
19777c478bd9Sstevel@tonic-gate (void) sigwinch(0);
19787c478bd9Sstevel@tonic-gate
19797c478bd9Sstevel@tonic-gate /*
19807c478bd9Sstevel@tonic-gate * Run the I/O loop until we get disconnected.
19817c478bd9Sstevel@tonic-gate */
1982ff17c8bfSgjelinek doio(masterfd, -1, masterfd, -1, -1, B_FALSE);
19837c478bd9Sstevel@tonic-gate reset_tty();
1984c2589d13SGarrett D'Amore if (!quiet)
1985c2589d13SGarrett D'Amore (void) printf(
1986c2589d13SGarrett D'Amore gettext("\n[Connection to zone '%s' console "
1987c2589d13SGarrett D'Amore "closed]\n"), zonename);
19887c478bd9Sstevel@tonic-gate
19897c478bd9Sstevel@tonic-gate return (0);
19907c478bd9Sstevel@tonic-gate }
19917c478bd9Sstevel@tonic-gate
1992108322fbScarlsonj if (st != ZONE_STATE_RUNNING && st != ZONE_STATE_MOUNTED) {
19937c478bd9Sstevel@tonic-gate zerror(gettext("login allowed only to running zones "
19947c478bd9Sstevel@tonic-gate "(%s is '%s')."), zonename, zone_state_str(st));
19957c478bd9Sstevel@tonic-gate return (1);
19967c478bd9Sstevel@tonic-gate }
19977c478bd9Sstevel@tonic-gate
1998108322fbScarlsonj (void) strlcpy(kernzone, zonename, sizeof (kernzone));
1999108322fbScarlsonj if (zonecfg_in_alt_root()) {
2000108322fbScarlsonj FILE *fp = zonecfg_open_scratch("", B_FALSE);
2001108322fbScarlsonj
2002108322fbScarlsonj if (fp == NULL || zonecfg_find_scratch(fp, zonename,
2003108322fbScarlsonj zonecfg_get_root(), kernzone, sizeof (kernzone)) == -1) {
2004108322fbScarlsonj zerror(gettext("cannot find scratch zone %s"),
2005108322fbScarlsonj zonename);
2006108322fbScarlsonj if (fp != NULL)
2007108322fbScarlsonj zonecfg_close_scratch(fp);
2008108322fbScarlsonj return (1);
2009108322fbScarlsonj }
2010108322fbScarlsonj zonecfg_close_scratch(fp);
2011108322fbScarlsonj }
2012108322fbScarlsonj
2013108322fbScarlsonj if ((zoneid = getzoneidbyname(kernzone)) == -1) {
20147c478bd9Sstevel@tonic-gate zerror(gettext("failed to get zoneid for zone '%s'"),
20157c478bd9Sstevel@tonic-gate zonename);
20167c478bd9Sstevel@tonic-gate return (1);
20177c478bd9Sstevel@tonic-gate }
20187c478bd9Sstevel@tonic-gate
2019108322fbScarlsonj /*
2020facf4a8dSllai * We need the zone root path only if we are setting up a pty.
2021108322fbScarlsonj */
2022facf4a8dSllai if (zone_get_devroot(zonename, devroot, sizeof (devroot)) == -1) {
2023facf4a8dSllai zerror(gettext("could not get dev path for zone %s"),
20247c478bd9Sstevel@tonic-gate zonename);
20257c478bd9Sstevel@tonic-gate return (1);
20267c478bd9Sstevel@tonic-gate }
20277c478bd9Sstevel@tonic-gate
202862868012SSteve Lawrence if (zone_get_brand(zonename, zonebrand, sizeof (zonebrand)) != Z_OK) {
20299acbbeafSnn zerror(gettext("could not get brand for zone %s"), zonename);
20309acbbeafSnn return (1);
20319acbbeafSnn }
203262868012SSteve Lawrence /*
203362868012SSteve Lawrence * In the alternate root environment, the only supported
203462868012SSteve Lawrence * operations are mount and unmount. In this case, just treat
203562868012SSteve Lawrence * the zone as native if it is cluster. Cluster zones can be
203662868012SSteve Lawrence * native for the purpose of LU or upgrade, and the cluster
203762868012SSteve Lawrence * brand may not exist in the miniroot (such as in net install
203862868012SSteve Lawrence * upgrade).
203962868012SSteve Lawrence */
2040e5816e35SEdward Pilatowicz if (zonecfg_default_brand(default_brand,
2041e5816e35SEdward Pilatowicz sizeof (default_brand)) != Z_OK) {
2042e5816e35SEdward Pilatowicz zerror(gettext("unable to determine default brand"));
2043e5816e35SEdward Pilatowicz return (1);
2044e5816e35SEdward Pilatowicz }
204562868012SSteve Lawrence if (zonecfg_in_alt_root() &&
204662868012SSteve Lawrence strcmp(zonebrand, CLUSTER_BRAND_NAME) == 0) {
2047e5816e35SEdward Pilatowicz (void) strlcpy(zonebrand, default_brand, sizeof (zonebrand));
204862868012SSteve Lawrence }
2049e5816e35SEdward Pilatowicz
205062868012SSteve Lawrence if ((bh = brand_open(zonebrand)) == NULL) {
205162868012SSteve Lawrence zerror(gettext("could not open brand for zone %s"), zonename);
205262868012SSteve Lawrence return (1);
205362868012SSteve Lawrence }
205462868012SSteve Lawrence
2055123807fbSedp if ((new_args = prep_args(bh, login, proc_args)) == NULL) {
20567c478bd9Sstevel@tonic-gate zperror(gettext("could not assemble new arguments"));
2057123807fbSedp brand_close(bh);
20587c478bd9Sstevel@tonic-gate return (1);
20597c478bd9Sstevel@tonic-gate }
2060858a4b99Ssl /*
2061858a4b99Ssl * Get the brand specific user_cmd. This command is used to get
2062*bbf21555SRichard Lowe * a passwd(5) entry for login.
2063858a4b99Ssl */
2064858a4b99Ssl if (!interactive && !failsafe) {
2065858a4b99Ssl if (zone_get_user_cmd(bh, login, user_cmd,
2066858a4b99Ssl sizeof (user_cmd)) == NULL) {
2067858a4b99Ssl zerror(gettext("could not get user_cmd for zone %s"),
2068858a4b99Ssl zonename);
2069858a4b99Ssl brand_close(bh);
2070858a4b99Ssl return (1);
2071858a4b99Ssl }
2072858a4b99Ssl }
2073123807fbSedp brand_close(bh);
20747c478bd9Sstevel@tonic-gate
20757c478bd9Sstevel@tonic-gate if ((new_env = prep_env()) == NULL) {
20767c478bd9Sstevel@tonic-gate zperror(gettext("could not assemble new environment"));
20777c478bd9Sstevel@tonic-gate return (1);
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate
2080279721bfSGary Mills if (!interactive) {
2081279721bfSGary Mills if (nflag) {
2082279721bfSGary Mills int nfd;
2083279721bfSGary Mills
2084279721bfSGary Mills if ((nfd = open(_PATH_DEVNULL, O_RDONLY)) < 0) {
2085279721bfSGary Mills zperror(gettext("failed to open null device"));
2086279721bfSGary Mills return (1);
2087279721bfSGary Mills }
2088279721bfSGary Mills if (nfd != STDIN_FILENO) {
2089279721bfSGary Mills if (dup2(nfd, STDIN_FILENO) < 0) {
2090279721bfSGary Mills zperror(gettext(
2091279721bfSGary Mills "failed to dup2 null device"));
2092279721bfSGary Mills return (1);
2093279721bfSGary Mills }
2094279721bfSGary Mills (void) close(nfd);
2095279721bfSGary Mills }
2096279721bfSGary Mills /* /dev/null is now standard input */
2097279721bfSGary Mills }
2098858a4b99Ssl return (noninteractive_login(zonename, user_cmd, zoneid,
2099858a4b99Ssl new_args, new_env));
2100279721bfSGary Mills }
21017c478bd9Sstevel@tonic-gate
2102108322fbScarlsonj if (zonecfg_in_alt_root()) {
2103108322fbScarlsonj zerror(gettext("cannot use interactive login with scratch "
2104108322fbScarlsonj "zone"));
2105108322fbScarlsonj return (1);
2106108322fbScarlsonj }
2107108322fbScarlsonj
21087c478bd9Sstevel@tonic-gate /*
21097c478bd9Sstevel@tonic-gate * Things are more complex in interactive mode; we get the
21107c478bd9Sstevel@tonic-gate * master side of the pty, then place the user's terminal into
21117c478bd9Sstevel@tonic-gate * raw mode.
21127c478bd9Sstevel@tonic-gate */
21137c478bd9Sstevel@tonic-gate if (get_master_pty() == -1) {
21147c478bd9Sstevel@tonic-gate zerror(gettext("could not setup master pty device"));
21157c478bd9Sstevel@tonic-gate return (1);
21167c478bd9Sstevel@tonic-gate }
21177c478bd9Sstevel@tonic-gate
21187c478bd9Sstevel@tonic-gate /*
21197c478bd9Sstevel@tonic-gate * Compute the "short name" of the pts. /dev/pts/2 --> pts/2
21207c478bd9Sstevel@tonic-gate */
21217c478bd9Sstevel@tonic-gate if ((slavename = ptsname(masterfd)) == NULL) {
21227c478bd9Sstevel@tonic-gate zperror(gettext("failed to get name for pseudo-tty"));
21237c478bd9Sstevel@tonic-gate return (1);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate if (strncmp(slavename, "/dev/", strlen("/dev/")) == 0)
21267c478bd9Sstevel@tonic-gate (void) strlcpy(slaveshortname, slavename + strlen("/dev/"),
21277c478bd9Sstevel@tonic-gate sizeof (slaveshortname));
21287c478bd9Sstevel@tonic-gate else
21297c478bd9Sstevel@tonic-gate (void) strlcpy(slaveshortname, slavename,
21307c478bd9Sstevel@tonic-gate sizeof (slaveshortname));
21317c478bd9Sstevel@tonic-gate
2132c2589d13SGarrett D'Amore if (!quiet)
2133c2589d13SGarrett D'Amore (void) printf(gettext("[Connected to zone '%s' %s]\n"),
2134c2589d13SGarrett D'Amore zonename, slaveshortname);
21357c478bd9Sstevel@tonic-gate
21367c478bd9Sstevel@tonic-gate if (set_tty_rawmode(STDIN_FILENO) == -1) {
21377c478bd9Sstevel@tonic-gate reset_tty();
21387c478bd9Sstevel@tonic-gate zperror(gettext("failed to set stdin pty to raw mode"));
21397c478bd9Sstevel@tonic-gate return (1);
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate
21427c478bd9Sstevel@tonic-gate if (prefork_dropprivs() != 0) {
21437c478bd9Sstevel@tonic-gate reset_tty();
21447c478bd9Sstevel@tonic-gate zperror(gettext("could not allocate privilege set"));
21457c478bd9Sstevel@tonic-gate return (1);
21467c478bd9Sstevel@tonic-gate }
21477c478bd9Sstevel@tonic-gate
21487c478bd9Sstevel@tonic-gate /*
21497c478bd9Sstevel@tonic-gate * We must mask SIGCLD until after we have coped with the fork
21507c478bd9Sstevel@tonic-gate * sufficiently to deal with it; otherwise we can race and receive the
21517c478bd9Sstevel@tonic-gate * signal before child_pid has been initialized (yes, this really
21527c478bd9Sstevel@tonic-gate * happens).
21537c478bd9Sstevel@tonic-gate */
21547c478bd9Sstevel@tonic-gate (void) sigset(SIGCLD, sigcld);
21557c478bd9Sstevel@tonic-gate (void) sigemptyset(&block_cld);
21567c478bd9Sstevel@tonic-gate (void) sigaddset(&block_cld, SIGCLD);
21577c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
21587c478bd9Sstevel@tonic-gate
21597c478bd9Sstevel@tonic-gate /*
21607c478bd9Sstevel@tonic-gate * We activate the contract template at the last minute to
21617c478bd9Sstevel@tonic-gate * avoid intermediate functions that could be using fork(2)
21627c478bd9Sstevel@tonic-gate * internally.
21637c478bd9Sstevel@tonic-gate */
21647c478bd9Sstevel@tonic-gate if ((tmpl_fd = init_template()) == -1) {
21657c478bd9Sstevel@tonic-gate reset_tty();
21667c478bd9Sstevel@tonic-gate zperror(gettext("could not create contract"));
21677c478bd9Sstevel@tonic-gate return (1);
21687c478bd9Sstevel@tonic-gate }
21697c478bd9Sstevel@tonic-gate
21707c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) {
21717c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
21727c478bd9Sstevel@tonic-gate reset_tty();
21737c478bd9Sstevel@tonic-gate zperror(gettext("could not fork"));
21747c478bd9Sstevel@tonic-gate return (1);
21757c478bd9Sstevel@tonic-gate } else if (child_pid == 0) { /* child process */
21767c478bd9Sstevel@tonic-gate int slavefd, newslave;
21777c478bd9Sstevel@tonic-gate
21787c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
21797c478bd9Sstevel@tonic-gate (void) close(tmpl_fd);
21807c478bd9Sstevel@tonic-gate
21817c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
21827c478bd9Sstevel@tonic-gate
2183facf4a8dSllai if ((slavefd = init_slave_pty(zoneid, devroot)) == -1)
21847c478bd9Sstevel@tonic-gate return (1);
21857c478bd9Sstevel@tonic-gate
21867c478bd9Sstevel@tonic-gate /*
21877c478bd9Sstevel@tonic-gate * Close all fds except for the slave pty.
21887c478bd9Sstevel@tonic-gate */
21897c478bd9Sstevel@tonic-gate (void) fdwalk(close_func, &slavefd);
21907c478bd9Sstevel@tonic-gate
21917c478bd9Sstevel@tonic-gate /*
21927c478bd9Sstevel@tonic-gate * Temporarily dup slavefd to stderr; that way if we have
21937c478bd9Sstevel@tonic-gate * to print out that zone_enter failed, the output will
21947c478bd9Sstevel@tonic-gate * have somewhere to go.
21957c478bd9Sstevel@tonic-gate */
21967c478bd9Sstevel@tonic-gate if (slavefd != STDERR_FILENO)
21977c478bd9Sstevel@tonic-gate (void) dup2(slavefd, STDERR_FILENO);
21987c478bd9Sstevel@tonic-gate
21997c478bd9Sstevel@tonic-gate if (zone_enter(zoneid) == -1) {
22007c478bd9Sstevel@tonic-gate zerror(gettext("could not enter zone %s: %s"),
22017c478bd9Sstevel@tonic-gate zonename, strerror(errno));
22027c478bd9Sstevel@tonic-gate return (1);
22037c478bd9Sstevel@tonic-gate }
22047c478bd9Sstevel@tonic-gate
22057c478bd9Sstevel@tonic-gate if (slavefd != STDERR_FILENO)
22067c478bd9Sstevel@tonic-gate (void) close(STDERR_FILENO);
22077c478bd9Sstevel@tonic-gate
22087c478bd9Sstevel@tonic-gate /*
22097c478bd9Sstevel@tonic-gate * We take pains to get this process into a new process
22107c478bd9Sstevel@tonic-gate * group, and subsequently a new session. In this way,
22117c478bd9Sstevel@tonic-gate * we'll have a session which doesn't yet have a controlling
22127c478bd9Sstevel@tonic-gate * terminal. When we open the slave, it will become the
22137c478bd9Sstevel@tonic-gate * controlling terminal; no PIDs concerning pgrps or sids
22147c478bd9Sstevel@tonic-gate * will leak inappropriately into the zone.
22157c478bd9Sstevel@tonic-gate */
22167c478bd9Sstevel@tonic-gate (void) setpgrp();
22177c478bd9Sstevel@tonic-gate
22187c478bd9Sstevel@tonic-gate /*
22197c478bd9Sstevel@tonic-gate * We need the slave pty to be referenced from the zone's
22207c478bd9Sstevel@tonic-gate * /dev in order to ensure that the devt's, etc are all
22217c478bd9Sstevel@tonic-gate * correct. Otherwise we break ttyname and the like.
22227c478bd9Sstevel@tonic-gate */
22237c478bd9Sstevel@tonic-gate if ((newslave = open(slavename, O_RDWR)) == -1) {
22247c478bd9Sstevel@tonic-gate (void) close(slavefd);
22257c478bd9Sstevel@tonic-gate return (1);
22267c478bd9Sstevel@tonic-gate }
22277c478bd9Sstevel@tonic-gate (void) close(slavefd);
22287c478bd9Sstevel@tonic-gate slavefd = newslave;
22297c478bd9Sstevel@tonic-gate
22307c478bd9Sstevel@tonic-gate /*
22317c478bd9Sstevel@tonic-gate * dup the slave to the various FDs, so that when the
22327c478bd9Sstevel@tonic-gate * spawned process does a write/read it maps to the slave
22337c478bd9Sstevel@tonic-gate * pty.
22347c478bd9Sstevel@tonic-gate */
22357c478bd9Sstevel@tonic-gate (void) dup2(slavefd, STDIN_FILENO);
22367c478bd9Sstevel@tonic-gate (void) dup2(slavefd, STDOUT_FILENO);
22377c478bd9Sstevel@tonic-gate (void) dup2(slavefd, STDERR_FILENO);
22387c478bd9Sstevel@tonic-gate if (slavefd != STDIN_FILENO && slavefd != STDOUT_FILENO &&
22397c478bd9Sstevel@tonic-gate slavefd != STDERR_FILENO) {
22407c478bd9Sstevel@tonic-gate (void) close(slavefd);
22417c478bd9Sstevel@tonic-gate }
22427c478bd9Sstevel@tonic-gate
22437c478bd9Sstevel@tonic-gate /*
22447c478bd9Sstevel@tonic-gate * In failsafe mode, we don't use login(1), so don't try
22457c478bd9Sstevel@tonic-gate * setting up a utmpx entry.
22467c478bd9Sstevel@tonic-gate */
2247eb5a5c78SSurya Prakki if (!failsafe)
22487c478bd9Sstevel@tonic-gate if (setup_utmpx(slaveshortname) == -1)
22497c478bd9Sstevel@tonic-gate return (1);
22507c478bd9Sstevel@tonic-gate
2251cb8a054bSGlenn Faden /*
2252cb8a054bSGlenn Faden * The child needs to run as root to
2253cb8a054bSGlenn Faden * execute the brand's login program.
2254cb8a054bSGlenn Faden */
2255cb8a054bSGlenn Faden if (setuid(0) == -1) {
2256cb8a054bSGlenn Faden zperror(gettext("insufficient privilege"));
2257cb8a054bSGlenn Faden return (1);
2258cb8a054bSGlenn Faden }
2259cb8a054bSGlenn Faden
22607c478bd9Sstevel@tonic-gate (void) execve(new_args[0], new_args, new_env);
22617c478bd9Sstevel@tonic-gate zperror(gettext("exec failure"));
22627c478bd9Sstevel@tonic-gate return (1);
22637c478bd9Sstevel@tonic-gate }
2264cb8a054bSGlenn Faden
22657c478bd9Sstevel@tonic-gate (void) ct_tmpl_clear(tmpl_fd);
22667c478bd9Sstevel@tonic-gate (void) close(tmpl_fd);
22677c478bd9Sstevel@tonic-gate
22687c478bd9Sstevel@tonic-gate /*
22697c478bd9Sstevel@tonic-gate * The rest is only for the parent process.
22707c478bd9Sstevel@tonic-gate */
22717c478bd9Sstevel@tonic-gate (void) sigset(SIGWINCH, sigwinch);
22727c478bd9Sstevel@tonic-gate
22737c478bd9Sstevel@tonic-gate postfork_dropprivs();
22747c478bd9Sstevel@tonic-gate
22757c478bd9Sstevel@tonic-gate (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
2276ff17c8bfSgjelinek doio(masterfd, -1, masterfd, -1, -1, B_FALSE);
22777c478bd9Sstevel@tonic-gate
22787c478bd9Sstevel@tonic-gate reset_tty();
2279c2589d13SGarrett D'Amore if (!quiet)
2280c2589d13SGarrett D'Amore (void) fprintf(stderr,
2281c2589d13SGarrett D'Amore gettext("\n[Connection to zone '%s' %s closed]\n"),
2282c2589d13SGarrett D'Amore zonename, slaveshortname);
22837c478bd9Sstevel@tonic-gate
22847c478bd9Sstevel@tonic-gate if (pollerr != 0) {
22857c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("Error: connection closed due "
22867c478bd9Sstevel@tonic-gate "to unexpected pollevents=0x%x.\n"), pollerr);
22877c478bd9Sstevel@tonic-gate return (1);
22887c478bd9Sstevel@tonic-gate }
22897c478bd9Sstevel@tonic-gate
22907c478bd9Sstevel@tonic-gate return (0);
22917c478bd9Sstevel@tonic-gate }
2292