1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * zlogin provides three types of login which allow users in the global 30 * zone to access non-global zones. 31 * 32 * - "interactive login" is similar to rlogin(1); for example, the user could 33 * issue 'zlogin my-zone' or 'zlogin -e ^ -l me my-zone'. The user is 34 * granted a new pty (which is then shoved into the zone), and an I/O 35 * loop between parent and child processes takes care of the interactive 36 * session. In this mode, login(1) (and its -c option, which means 37 * "already authenticated") is employed to take care of the initialization 38 * of the user's session. 39 * 40 * - "non-interactive login" is similar to su(1M); the user could issue 41 * 'zlogin my-zone ls -l' and the command would be run as specified. 42 * In this mode, zlogin sets up pipes as the communication channel, and 43 * 'su' is used to do the login setup work. 44 * 45 * - "console login" is the equivalent to accessing the tip line for a 46 * zone. For example, the user can issue 'zlogin -C my-zone'. 47 * In this mode, zlogin contacts the zoneadmd process via unix domain 48 * socket. If zoneadmd is not running, it starts it. This allows the 49 * console to be available anytime the zone is installed, regardless of 50 * whether it is running. 51 */ 52 53 #include <sys/socket.h> 54 #include <sys/termios.h> 55 #include <sys/utsname.h> 56 #include <sys/stat.h> 57 #include <sys/types.h> 58 #include <sys/contract/process.h> 59 #include <sys/ctfs.h> 60 #include <sys/brand.h> 61 #include <sys/wait.h> 62 #include <alloca.h> 63 #include <assert.h> 64 #include <ctype.h> 65 #include <door.h> 66 #include <errno.h> 67 #include <nss_dbdefs.h> 68 #include <poll.h> 69 #include <priv.h> 70 #include <pwd.h> 71 #include <unistd.h> 72 #include <utmpx.h> 73 #include <sac.h> 74 #include <signal.h> 75 #include <stdarg.h> 76 #include <stdio.h> 77 #include <stdlib.h> 78 #include <string.h> 79 #include <strings.h> 80 #include <stropts.h> 81 #include <wait.h> 82 #include <zone.h> 83 #include <fcntl.h> 84 #include <libdevinfo.h> 85 #include <libintl.h> 86 #include <locale.h> 87 #include <libzonecfg.h> 88 #include <libcontract.h> 89 #include <libbrand.h> 90 91 static int masterfd; 92 static struct termios save_termios; 93 static struct termios effective_termios; 94 static int save_fd; 95 static struct winsize winsize; 96 static volatile int dead; 97 static volatile pid_t child_pid = -1; 98 static int interactive = 0; 99 static priv_set_t *dropprivs; 100 101 static int nocmdchar = 0; 102 static int failsafe = 0; 103 static char cmdchar = '~'; 104 105 static int pollerr = 0; 106 107 static const char *pname; 108 109 #if !defined(TEXT_DOMAIN) /* should be defined by cc -D */ 110 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it wasn't */ 111 #endif 112 113 #define SUPATH "/usr/bin/su" 114 #define FAILSAFESHELL "/sbin/sh" 115 #define DEFAULTSHELL "/sbin/sh" 116 #define DEF_PATH "/usr/sbin:/usr/bin" 117 118 /* 119 * The ZLOGIN_BUFSIZ is larger than PIPE_BUF so we can be sure we're clearing 120 * out the pipe when the child is exiting. The ZLOGIN_RDBUFSIZ must be less 121 * than ZLOGIN_BUFSIZ (because we share the buffer in doio). This value is 122 * also chosen in conjunction with the HI_WATER setting to make sure we 123 * don't fill up the pipe. We can write FIFOHIWAT (16k) into the pipe before 124 * blocking. By having ZLOGIN_RDBUFSIZ set to 1k and HI_WATER set to 8k, we 125 * know we can always write a ZLOGIN_RDBUFSIZ chunk into the pipe when there 126 * is less than HI_WATER data already in the pipe. 127 */ 128 #define ZLOGIN_BUFSIZ 8192 129 #define ZLOGIN_RDBUFSIZ 1024 130 #define HI_WATER 8192 131 132 /* 133 * See canonify() below. CANONIFY_LEN is the maximum length that a 134 * "canonical" sequence will expand to (backslash, three octal digits, NUL). 135 */ 136 #define CANONIFY_LEN 5 137 138 static void 139 usage(void) 140 { 141 (void) fprintf(stderr, gettext("usage: %s [ -CES ] [ -e cmdchar ] " 142 "[-l user] zonename [command [args ...] ]\n"), pname); 143 exit(2); 144 } 145 146 static const char * 147 getpname(const char *arg0) 148 { 149 const char *p = strrchr(arg0, '/'); 150 151 if (p == NULL) 152 p = arg0; 153 else 154 p++; 155 156 pname = p; 157 return (p); 158 } 159 160 static void 161 zerror(const char *fmt, ...) 162 { 163 va_list alist; 164 165 (void) fprintf(stderr, "%s: ", pname); 166 va_start(alist, fmt); 167 (void) vfprintf(stderr, fmt, alist); 168 va_end(alist); 169 (void) fprintf(stderr, "\n"); 170 } 171 172 static void 173 zperror(const char *str) 174 { 175 const char *estr; 176 177 if ((estr = strerror(errno)) != NULL) 178 (void) fprintf(stderr, "%s: %s: %s\n", pname, str, estr); 179 else 180 (void) fprintf(stderr, "%s: %s: errno %d\n", pname, str, errno); 181 } 182 183 /* 184 * The first part of our privilege dropping scheme needs to be called before 185 * fork(), since we must have it for security; we don't want to be surprised 186 * later that we couldn't allocate the privset. 187 */ 188 static int 189 prefork_dropprivs() 190 { 191 if ((dropprivs = priv_allocset()) == NULL) 192 return (1); 193 priv_emptyset(dropprivs); 194 195 /* 196 * We need these privileges in order to query session information and 197 * send signals. 198 */ 199 if (interactive == 0) { 200 if (priv_addset(dropprivs, "proc_session") == -1) 201 return (1); 202 if (priv_addset(dropprivs, "proc_zone") == -1) 203 return (1); 204 if (priv_addset(dropprivs, "proc_owner") == -1) 205 return (1); 206 } 207 208 return (0); 209 } 210 211 /* 212 * The second part of the privilege drop. We are paranoid about being attacked 213 * by the zone, so we drop all privileges. This should prevent a compromise 214 * which gets us to fork(), exec(), symlink(), etc. 215 */ 216 static void 217 postfork_dropprivs() 218 { 219 if ((setppriv(PRIV_SET, PRIV_PERMITTED, dropprivs)) == -1) { 220 zperror(gettext("Warning: could not set permitted privileges")); 221 } 222 if ((setppriv(PRIV_SET, PRIV_LIMIT, dropprivs)) == -1) { 223 zperror(gettext("Warning: could not set limit privileges")); 224 } 225 if ((setppriv(PRIV_SET, PRIV_INHERITABLE, dropprivs)) == -1) { 226 zperror(gettext("Warning: could not set inheritable " 227 "privileges")); 228 } 229 } 230 231 /* 232 * Create the unix domain socket and call the zoneadmd server; handshake 233 * with it to determine whether it will allow us to connect. 234 */ 235 static int 236 get_console_master(const char *zname) 237 { 238 int sockfd = -1; 239 struct sockaddr_un servaddr; 240 char clientid[MAXPATHLEN]; 241 char handshake[MAXPATHLEN], c; 242 int msglen; 243 int i = 0, err = 0; 244 245 if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { 246 zperror(gettext("could not create socket")); 247 return (-1); 248 } 249 250 bzero(&servaddr, sizeof (servaddr)); 251 servaddr.sun_family = AF_UNIX; 252 (void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path), 253 "%s/%s.console_sock", ZONES_TMPDIR, zname); 254 255 if (connect(sockfd, (struct sockaddr *)&servaddr, 256 sizeof (servaddr)) == -1) { 257 zperror(gettext("Could not connect to zone console")); 258 goto bad; 259 } 260 masterfd = sockfd; 261 262 msglen = snprintf(clientid, sizeof (clientid), "IDENT %lu %s\n", 263 getpid(), setlocale(LC_MESSAGES, NULL)); 264 265 if (msglen >= sizeof (clientid) || msglen < 0) { 266 zerror("protocol error"); 267 goto bad; 268 } 269 270 if (write(masterfd, clientid, msglen) != msglen) { 271 zerror("protocol error"); 272 goto bad; 273 } 274 275 bzero(handshake, sizeof (handshake)); 276 277 /* 278 * Take care not to accumulate more than our fill, and leave room for 279 * the NUL at the end. 280 */ 281 while ((err = read(masterfd, &c, 1)) == 1) { 282 if (i >= (sizeof (handshake) - 1)) 283 break; 284 if (c == '\n') 285 break; 286 handshake[i] = c; 287 i++; 288 } 289 290 /* 291 * If something went wrong during the handshake we bail; perhaps 292 * the server died off. 293 */ 294 if (err == -1) { 295 zperror(gettext("Could not connect to zone console")); 296 goto bad; 297 } 298 299 if (strncmp(handshake, "OK", sizeof (handshake)) == 0) 300 return (0); 301 302 zerror(gettext("Console is already in use by process ID %s."), 303 handshake); 304 bad: 305 (void) close(sockfd); 306 masterfd = -1; 307 return (-1); 308 } 309 310 311 /* 312 * Routines to handle pty creation upon zone entry and to shuttle I/O back 313 * and forth between the two terminals. We also compute and store the 314 * name of the slave terminal associated with the master side. 315 */ 316 static int 317 get_master_pty() 318 { 319 if ((masterfd = open("/dev/ptmx", O_RDWR|O_NONBLOCK)) < 0) { 320 zperror(gettext("failed to obtain a pseudo-tty")); 321 return (-1); 322 } 323 if (tcgetattr(STDIN_FILENO, &save_termios) == -1) { 324 zperror(gettext("failed to get terminal settings from stdin")); 325 return (-1); 326 } 327 (void) ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&winsize); 328 329 return (0); 330 } 331 332 /* 333 * This is a bit tricky; normally a pts device will belong to the zone it 334 * is granted to. But in the case of "entering" a zone, we need to establish 335 * the pty before entering the zone so that we can vector I/O to and from it 336 * from the global zone. 337 * 338 * We use the zonept() call to let the ptm driver know what we are up to; 339 * the only other hairy bit is the setting of zoneslavename (which happens 340 * above, in get_master_pty()). 341 */ 342 static int 343 init_slave_pty(zoneid_t zoneid, char *devroot) 344 { 345 int slavefd = -1; 346 char *slavename, zoneslavename[MAXPATHLEN]; 347 348 /* 349 * Set slave permissions, zone the pts, then unlock it. 350 */ 351 if (grantpt(masterfd) != 0) { 352 zperror(gettext("grantpt failed")); 353 return (-1); 354 } 355 356 if (unlockpt(masterfd) != 0) { 357 zperror(gettext("unlockpt failed")); 358 return (-1); 359 } 360 361 /* 362 * We must open the slave side before zoning this pty; otherwise 363 * the kernel would refuse us the open-- zoning a pty makes it 364 * inaccessible to the global zone. Note we are trying to open 365 * the device node via the $ZONEROOT/dev path for this pty. 366 * 367 * Later we'll close the slave out when once we've opened it again 368 * from within the target zone. Blarg. 369 */ 370 if ((slavename = ptsname(masterfd)) == NULL) { 371 zperror(gettext("failed to get name for pseudo-tty")); 372 return (-1); 373 } 374 375 (void) snprintf(zoneslavename, sizeof (zoneslavename), "%s%s", 376 devroot, slavename); 377 378 if ((slavefd = open(zoneslavename, O_RDWR)) < 0) { 379 zerror(gettext("failed to open %s: %s"), zoneslavename, 380 strerror(errno)); 381 return (-1); 382 } 383 384 /* 385 * Push hardware emulation (ptem), line discipline (ldterm), 386 * and V7/4BSD/Xenix compatibility (ttcompat) modules. 387 */ 388 if (ioctl(slavefd, I_PUSH, "ptem") == -1) { 389 zperror(gettext("failed to push ptem module")); 390 if (!failsafe) 391 goto bad; 392 } 393 394 /* 395 * Anchor the stream to prevent malicious I_POPs; we prefer to do 396 * this prior to entering the zone so that we can detect any errors 397 * early, and so that we can set the anchor from the global zone. 398 */ 399 if (ioctl(slavefd, I_ANCHOR) == -1) { 400 zperror(gettext("failed to set stream anchor")); 401 if (!failsafe) 402 goto bad; 403 } 404 405 if (ioctl(slavefd, I_PUSH, "ldterm") == -1) { 406 zperror(gettext("failed to push ldterm module")); 407 if (!failsafe) 408 goto bad; 409 } 410 if (ioctl(slavefd, I_PUSH, "ttcompat") == -1) { 411 zperror(gettext("failed to push ttcompat module")); 412 if (!failsafe) 413 goto bad; 414 } 415 416 /* 417 * Propagate terminal settings from the external term to the new one. 418 */ 419 if (tcsetattr(slavefd, TCSAFLUSH, &save_termios) == -1) { 420 zperror(gettext("failed to set terminal settings")); 421 if (!failsafe) 422 goto bad; 423 } 424 (void) ioctl(slavefd, TIOCSWINSZ, (char *)&winsize); 425 426 if (zonept(masterfd, zoneid) != 0) { 427 zperror(gettext("could not set zoneid of pty")); 428 goto bad; 429 } 430 431 return (slavefd); 432 433 bad: 434 (void) close(slavefd); 435 return (-1); 436 } 437 438 /* 439 * Place terminal into raw mode. 440 */ 441 static int 442 set_tty_rawmode(int fd) 443 { 444 struct termios term; 445 if (tcgetattr(fd, &term) < 0) { 446 zperror(gettext("failed to get user terminal settings")); 447 return (-1); 448 } 449 450 /* Stash for later, so we can revert back to previous mode */ 451 save_termios = term; 452 save_fd = fd; 453 454 /* disable 8->7 bit strip, start/stop, enable any char to restart */ 455 term.c_iflag &= ~(ISTRIP|IXON|IXANY); 456 /* disable NL->CR, CR->NL, ignore CR, UPPER->lower */ 457 term.c_iflag &= ~(INLCR|ICRNL|IGNCR|IUCLC); 458 /* disable output post-processing */ 459 term.c_oflag &= ~OPOST; 460 /* disable canonical mode, signal chars, echo & extended functions */ 461 term.c_lflag &= ~(ICANON|ISIG|ECHO|IEXTEN); 462 463 term.c_cc[VMIN] = 1; /* byte-at-a-time */ 464 term.c_cc[VTIME] = 0; 465 466 if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term)) { 467 zperror(gettext("failed to set user terminal to raw mode")); 468 return (-1); 469 } 470 471 /* 472 * We need to know the value of VEOF so that we can properly process for 473 * client-side ~<EOF>. But we have obliterated VEOF in term, 474 * because VMIN overloads the same array slot in non-canonical mode. 475 * Stupid @&^%! 476 * 477 * So here we construct the "effective" termios from the current 478 * terminal settings, and the corrected VEOF and VEOL settings. 479 */ 480 if (tcgetattr(STDIN_FILENO, &effective_termios) < 0) { 481 zperror(gettext("failed to get user terminal settings")); 482 return (-1); 483 } 484 effective_termios.c_cc[VEOF] = save_termios.c_cc[VEOF]; 485 effective_termios.c_cc[VEOL] = save_termios.c_cc[VEOL]; 486 487 return (0); 488 } 489 490 /* 491 * Copy terminal window size from our terminal to the pts. 492 */ 493 /*ARGSUSED*/ 494 static void 495 sigwinch(int s) 496 { 497 struct winsize ws; 498 499 if (ioctl(0, TIOCGWINSZ, &ws) == 0) 500 (void) ioctl(masterfd, TIOCSWINSZ, &ws); 501 } 502 503 static void 504 /*ARGSUSED*/ 505 sigcld(int s) 506 { 507 int status; 508 pid_t pid; 509 510 /* 511 * Peek at the exit status. If this isn't the process we cared 512 * about, then just reap it. 513 */ 514 if ((pid = waitpid(child_pid, &status, WNOHANG|WNOWAIT)) != -1) { 515 if (pid == child_pid && 516 (WIFEXITED(status) || WIFSIGNALED(status))) 517 dead = 1; 518 else 519 (void) waitpid(pid, &status, WNOHANG); 520 } 521 } 522 523 /* 524 * Some signals (currently, SIGINT) must be forwarded on to the process 525 * group of the child process. 526 */ 527 static void 528 sig_forward(int s) 529 { 530 if (child_pid != -1) { 531 pid_t pgid = getpgid(child_pid); 532 if (pgid != -1) 533 (void) sigsend(P_PGID, pgid, s); 534 } 535 } 536 537 /* 538 * reset terminal settings for global environment 539 */ 540 static void 541 reset_tty() 542 { 543 (void) tcsetattr(save_fd, TCSADRAIN, &save_termios); 544 } 545 546 /* 547 * Convert character to printable representation, for display with locally 548 * echoed command characters (like when we need to display ~^D) 549 */ 550 static void 551 canonify(char c, char *cc) 552 { 553 if (isprint(c)) { 554 cc[0] = c; 555 cc[1] = '\0'; 556 } else if (c >= 0 && c <= 31) { /* ^@ through ^_ */ 557 cc[0] = '^'; 558 cc[1] = c + '@'; 559 cc[2] = '\0'; 560 } else { 561 cc[0] = '\\'; 562 cc[1] = ((c >> 6) & 7) + '0'; 563 cc[2] = ((c >> 3) & 7) + '0'; 564 cc[3] = (c & 7) + '0'; 565 cc[4] = '\0'; 566 } 567 } 568 569 /* 570 * process_user_input watches the input stream for the escape sequence for 571 * 'quit' (by default, tilde-period). Because we might be fed just one 572 * keystroke at a time, state associated with the user input (are we at the 573 * beginning of the line? are we locally echoing the next character?) is 574 * maintained by beginning_of_line and local_echo across calls to the routine. 575 * If the write to outfd fails, we'll try to read from infd in an attempt 576 * to prevent deadlock between the two processes. 577 * 578 * This routine returns -1 when the 'quit' escape sequence has been issued, 579 * and 0 otherwise. 580 */ 581 static int 582 process_user_input(int outfd, int infd, char *buf, size_t nbytes) 583 { 584 static boolean_t beginning_of_line = B_TRUE; 585 static boolean_t local_echo = B_FALSE; 586 587 char c = *buf; 588 for (c = *buf; nbytes > 0; c = *buf, --nbytes) { 589 buf++; 590 if (beginning_of_line && !nocmdchar) { 591 beginning_of_line = B_FALSE; 592 if (c == cmdchar) { 593 local_echo = B_TRUE; 594 continue; 595 } 596 } else if (local_echo) { 597 local_echo = B_FALSE; 598 if (c == '.' || c == effective_termios.c_cc[VEOF]) { 599 char cc[CANONIFY_LEN]; 600 601 canonify(c, cc); 602 (void) write(STDOUT_FILENO, &cmdchar, 1); 603 (void) write(STDOUT_FILENO, cc, strlen(cc)); 604 return (-1); 605 } 606 } 607 retry: 608 if (write(outfd, &c, 1) <= 0) { 609 /* 610 * Since the fd we are writing to is opened with 611 * O_NONBLOCK it is possible to get EAGAIN if the 612 * pipe is full. One way this could happen is if we 613 * are writing a lot of data into the pipe in this loop 614 * and the application on the other end is echoing that 615 * data back out to its stdout. The output pipe can 616 * fill up since we are stuck here in this loop and not 617 * draining the other pipe. We can try to read some of 618 * the data to see if we can drain the pipe so that the 619 * application can continue to make progress. The read 620 * is non-blocking so we won't hang here. We also wait 621 * a bit before retrying since there could be other 622 * reasons why the pipe is full and we don't want to 623 * continuously retry. 624 */ 625 if (errno == EAGAIN) { 626 struct timespec rqtp; 627 int ln; 628 char ibuf[ZLOGIN_BUFSIZ]; 629 630 if ((ln = read(infd, ibuf, ZLOGIN_BUFSIZ)) > 0) 631 (void) write(STDOUT_FILENO, ibuf, ln); 632 633 /* sleep for 10 milliseconds */ 634 rqtp.tv_sec = 0; 635 rqtp.tv_nsec = 10 * (NANOSEC / MILLISEC); 636 (void) nanosleep(&rqtp, NULL); 637 if (!dead) 638 goto retry; 639 } 640 641 return (-1); 642 } 643 beginning_of_line = (c == '\r' || c == '\n' || 644 c == effective_termios.c_cc[VKILL] || 645 c == effective_termios.c_cc[VEOL] || 646 c == effective_termios.c_cc[VSUSP] || 647 c == effective_termios.c_cc[VINTR]); 648 } 649 return (0); 650 } 651 652 /* 653 * This function prevents deadlock between zlogin and the application in the 654 * zone that it is talking to. This can happen when we read from zlogin's 655 * stdin and write the data down the pipe to the application. If the pipe 656 * is full, we'll block in the write. Because zlogin could be blocked in 657 * the write, it would never read the application's stdout/stderr so the 658 * application can then block on those writes (when the pipe fills up). If the 659 * the application gets blocked this way, it can never get around to reading 660 * its stdin so that zlogin can unblock from its write. Once in this state, 661 * the two processes are deadlocked. 662 * 663 * To prevent this, we want to verify that we can write into the pipe before we 664 * read from our stdin. If the pipe already is pretty full, we bypass the read 665 * for now. We'll circle back here again after the poll() so that we can 666 * try again. When this function is called, we already know there is data 667 * ready to read on STDIN_FILENO. We return -1 if there is a problem, 0 668 * if everything is ok (even though we might not have read/written any data 669 * into the pipe on this iteration). 670 */ 671 static int 672 process_raw_input(int stdin_fd, int appin_fd) 673 { 674 int cc; 675 struct stat sb; 676 char ibuf[ZLOGIN_RDBUFSIZ]; 677 678 /* Check how much data is already in the pipe */ 679 if (fstat(appin_fd, &sb) == -1) { 680 perror("stat failed"); 681 return (-1); 682 } 683 684 if (dead) 685 return (-1); 686 687 /* 688 * The pipe already has a lot of data in it, don't write any more 689 * right now. 690 */ 691 if (sb.st_size >= HI_WATER) 692 return (0); 693 694 cc = read(STDIN_FILENO, ibuf, ZLOGIN_RDBUFSIZ); 695 if (cc == -1 && (errno != EINTR || dead)) 696 return (-1); 697 698 if (cc == -1) /* The read was interrupted. */ 699 return (0); 700 701 /* 702 * stdin_fd is stdin of the target; so, the thing we'll write the user 703 * data *to*. Also, unlike on the output side, we propagate 704 * zero-length messages to the other side. 705 */ 706 if (write(stdin_fd, ibuf, cc) == -1) 707 return (-1); 708 709 return (0); 710 } 711 712 /* 713 * Write the output from the application running in the zone. We can get 714 * a signal during the write (usually it would be SIGCHLD when the application 715 * has exited) so we loop to make sure we have written all of the data we read. 716 */ 717 static int 718 process_output(int in_fd, int out_fd) 719 { 720 int wrote = 0; 721 int cc; 722 char ibuf[ZLOGIN_BUFSIZ]; 723 724 cc = read(in_fd, ibuf, ZLOGIN_BUFSIZ); 725 if (cc == -1 && (errno != EINTR || dead)) 726 return (-1); 727 if (cc == 0) /* EOF */ 728 return (-1); 729 if (cc == -1) /* The read was interrupted. */ 730 return (0); 731 732 do { 733 int len; 734 735 len = write(out_fd, ibuf + wrote, cc - wrote); 736 if (len == -1 && errno != EINTR) 737 return (-1); 738 if (len != -1) 739 wrote += len; 740 } while (wrote < cc); 741 742 return (0); 743 } 744 745 /* 746 * This is the main I/O loop, and is shared across all zlogin modes. 747 * Parameters: 748 * stdin_fd: The fd representing 'stdin' for the slave side; input to 749 * the zone will be written here. 750 * 751 * appin_fd: The fd representing the other end of the 'stdin' pipe (when 752 * we're running non-interactive); used in process_raw_input 753 * to ensure we don't fill up the application's stdin pipe. 754 * 755 * stdout_fd: The fd representing 'stdout' for the slave side; output 756 * from the zone will arrive here. 757 * 758 * stderr_fd: The fd representing 'stderr' for the slave side; output 759 * from the zone will arrive here. 760 * 761 * raw_mode: If TRUE, then no processing (for example, for '~.') will 762 * be performed on the input coming from STDIN. 763 * 764 * stderr_fd may be specified as -1 if there is no stderr (only non-interactive 765 * mode supplies a stderr). 766 * 767 */ 768 static void 769 doio(int stdin_fd, int appin_fd, int stdout_fd, int stderr_fd, 770 boolean_t raw_mode) 771 { 772 struct pollfd pollfds[3]; 773 char ibuf[ZLOGIN_BUFSIZ]; 774 int cc, ret; 775 776 /* read from stdout of zone and write to stdout of global zone */ 777 pollfds[0].fd = stdout_fd; 778 pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI; 779 780 /* read from stderr of zone and write to stderr of global zone */ 781 pollfds[1].fd = stderr_fd; 782 pollfds[1].events = pollfds[0].events; 783 784 /* read from stdin of global zone and write to stdin of zone */ 785 pollfds[2].fd = STDIN_FILENO; 786 pollfds[2].events = pollfds[0].events; 787 788 for (;;) { 789 pollfds[0].revents = pollfds[1].revents = 790 pollfds[2].revents = 0; 791 792 if (dead) 793 break; 794 795 ret = poll(pollfds, 796 sizeof (pollfds) / sizeof (struct pollfd), -1); 797 if (ret == -1 && errno != EINTR) { 798 perror("poll failed"); 799 break; 800 } 801 802 if (errno == EINTR && dead) { 803 break; 804 } 805 806 /* event from master side stdout */ 807 if (pollfds[0].revents) { 808 if (pollfds[0].revents & 809 (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { 810 if (process_output(stdout_fd, STDOUT_FILENO) 811 != 0) 812 break; 813 } else { 814 pollerr = pollfds[0].revents; 815 break; 816 } 817 } 818 819 /* event from master side stderr */ 820 if (pollfds[1].revents) { 821 if (pollfds[1].revents & 822 (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { 823 if (process_output(stderr_fd, STDERR_FILENO) 824 != 0) 825 break; 826 } else { 827 pollerr = pollfds[1].revents; 828 break; 829 } 830 } 831 832 /* event from user STDIN side */ 833 if (pollfds[2].revents) { 834 if (pollfds[2].revents & 835 (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { 836 /* 837 * stdin fd is stdin of the target; so, 838 * the thing we'll write the user data *to*. 839 * 840 * Also, unlike on the output side, we 841 * propagate zero-length messages to the 842 * other side. 843 */ 844 if (raw_mode == B_TRUE) { 845 if (process_raw_input(stdin_fd, 846 appin_fd) == -1) 847 break; 848 } else { 849 cc = read(STDIN_FILENO, ibuf, 850 ZLOGIN_RDBUFSIZ); 851 if (cc == -1 && 852 (errno != EINTR || dead)) 853 break; 854 855 if (cc != -1 && 856 process_user_input(stdin_fd, 857 stdout_fd, ibuf, cc) == -1) 858 break; 859 } 860 } else if (raw_mode == B_TRUE && 861 pollfds[2].revents & POLLHUP) { 862 /* 863 * It's OK to get a POLLHUP on STDIN-- it 864 * always happens if you do: 865 * 866 * echo foo | zlogin <zone> <command> 867 * 868 * We reset fd to -1 in this case to clear 869 * the condition and write an EOF to the 870 * other side in order to wrap things up. 871 */ 872 pollfds[2].fd = -1; 873 (void) write(stdin_fd, ibuf, 0); 874 } else { 875 pollerr = pollfds[2].revents; 876 break; 877 } 878 } 879 } 880 881 /* 882 * We are in the midst of dying, but try to poll with a short 883 * timeout to see if we can catch the last bit of I/O from the 884 * children. 885 */ 886 retry: 887 pollfds[0].revents = pollfds[1].revents = 0; 888 (void) poll(pollfds, 2, 100); 889 if (pollfds[0].revents & 890 (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { 891 if ((cc = read(stdout_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) { 892 (void) write(STDOUT_FILENO, ibuf, cc); 893 goto retry; 894 } 895 } 896 if (pollfds[1].revents & 897 (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) { 898 if ((cc = read(stderr_fd, ibuf, ZLOGIN_BUFSIZ)) > 0) { 899 (void) write(STDERR_FILENO, ibuf, cc); 900 goto retry; 901 } 902 } 903 } 904 905 /* 906 * Fetch the user_cmd brand hook for getting a user's passwd(4) entry. 907 */ 908 static const char * 909 zone_get_user_cmd(brand_handle_t bh, const char *login, char *user_cmd, 910 size_t len) 911 { 912 bzero(user_cmd, sizeof (user_cmd)); 913 if (brand_get_user_cmd(bh, login, user_cmd, len) != 0) 914 return (NULL); 915 916 return (user_cmd); 917 } 918 919 /* From libc */ 920 extern int str2passwd(const char *, int, void *, char *, int); 921 922 /* 923 * exec() the user_cmd brand hook, and convert the output string to a 924 * struct passwd. This is to be called after zone_enter(). 925 * 926 */ 927 static struct passwd * 928 zone_get_user_pw(const char *user_cmd, struct passwd *pwent, char *pwbuf, 929 int pwbuflen) 930 { 931 char pwline[NSS_BUFLEN_PASSWD]; 932 char *cin = NULL; 933 FILE *fin; 934 int status; 935 936 assert(getzoneid() != GLOBAL_ZONEID); 937 938 if ((fin = popen(user_cmd, "r")) == NULL) 939 return (NULL); 940 941 while (cin == NULL && !feof(fin)) 942 cin = fgets(pwline, sizeof (pwline), fin); 943 944 if (cin == NULL) { 945 (void) pclose(fin); 946 return (NULL); 947 } 948 949 status = pclose(fin); 950 if (!WIFEXITED(status)) 951 return (NULL); 952 if (WEXITSTATUS(status) != 0) 953 return (NULL); 954 955 if (str2passwd(pwline, sizeof (pwline), pwent, pwbuf, pwbuflen) == 0) 956 return (pwent); 957 else 958 return (NULL); 959 } 960 961 static char ** 962 zone_login_cmd(brand_handle_t bh, const char *login) 963 { 964 static char result_buf[ARG_MAX]; 965 char **new_argv, *ptr, *lasts; 966 int n, a; 967 968 /* Get the login command for the target zone. */ 969 bzero(result_buf, sizeof (result_buf)); 970 if (brand_get_login_cmd(bh, login, 971 result_buf, sizeof (result_buf)) != 0) 972 return (NULL); 973 974 /* 975 * We got back a string that we'd like to execute. But since 976 * we're not doing the execution via a shell we'll need to convert 977 * the exec string to an array of strings. We'll do that here 978 * but we're going to be very simplistic about it and break stuff 979 * up based on spaces. We're not even going to support any kind 980 * of quoting or escape characters. It's truly amazing that 981 * there is no library function in OpenSolaris to do this for us. 982 */ 983 984 /* 985 * Be paranoid. Since we're deliniating based on spaces make 986 * sure there are no adjacent spaces. 987 */ 988 if (strstr(result_buf, " ") != NULL) 989 return (NULL); 990 991 /* Remove any trailing whitespace. */ 992 n = strlen(result_buf); 993 if (result_buf[n - 1] == ' ') 994 result_buf[n - 1] = '\0'; 995 996 /* Count how many elements there are in the exec string. */ 997 ptr = result_buf; 998 for (n = 2; ((ptr = strchr(ptr + 1, (int)' ')) != NULL); n++) 999 ; 1000 1001 /* Allocate the argv array that we're going to return. */ 1002 if ((new_argv = malloc(sizeof (char *) * n)) == NULL) 1003 return (NULL); 1004 1005 /* Tokenize the exec string and return. */ 1006 a = 0; 1007 new_argv[a++] = result_buf; 1008 if (n > 2) { 1009 (void) strtok_r(result_buf, " ", &lasts); 1010 while ((new_argv[a++] = strtok_r(NULL, " ", &lasts)) != NULL) 1011 ; 1012 } else { 1013 new_argv[a++] = NULL; 1014 } 1015 assert(n == a); 1016 return (new_argv); 1017 } 1018 1019 /* 1020 * Prepare argv array for exec'd process; if we're passing commands to the 1021 * new process, then use su(1M) to do the invocation. Otherwise, use 1022 * 'login -z <from_zonename> -f' (-z is an undocumented option which tells 1023 * login that we're coming from another zone, and to disregard its CONSOLE 1024 * checks). 1025 */ 1026 static char ** 1027 prep_args(brand_handle_t bh, const char *login, char **argv) 1028 { 1029 int argc = 0, a = 0, i, n = -1; 1030 char **new_argv; 1031 1032 if (argv != NULL) { 1033 size_t subshell_len = 1; 1034 char *subshell; 1035 1036 while (argv[argc] != NULL) 1037 argc++; 1038 1039 for (i = 0; i < argc; i++) { 1040 subshell_len += strlen(argv[i]) + 1; 1041 } 1042 if ((subshell = calloc(1, subshell_len)) == NULL) 1043 return (NULL); 1044 1045 for (i = 0; i < argc; i++) { 1046 (void) strcat(subshell, argv[i]); 1047 (void) strcat(subshell, " "); 1048 } 1049 1050 if (failsafe) { 1051 n = 4; 1052 if ((new_argv = malloc(sizeof (char *) * n)) == NULL) 1053 return (NULL); 1054 1055 new_argv[a++] = FAILSAFESHELL; 1056 } else { 1057 n = 5; 1058 if ((new_argv = malloc(sizeof (char *) * n)) == NULL) 1059 return (NULL); 1060 1061 new_argv[a++] = SUPATH; 1062 new_argv[a++] = (char *)login; 1063 } 1064 new_argv[a++] = "-c"; 1065 new_argv[a++] = subshell; 1066 new_argv[a++] = NULL; 1067 assert(a == n); 1068 } else { 1069 if (failsafe) { 1070 n = 2; 1071 if ((new_argv = malloc(sizeof (char *) * n)) == NULL) 1072 return (NULL); 1073 new_argv[a++] = FAILSAFESHELL; 1074 new_argv[a++] = NULL; 1075 assert(n == a); 1076 } else { 1077 new_argv = zone_login_cmd(bh, login); 1078 } 1079 } 1080 1081 return (new_argv); 1082 } 1083 1084 /* 1085 * Helper routine for prep_env below. 1086 */ 1087 static char * 1088 add_env(char *name, char *value) 1089 { 1090 size_t sz = strlen(name) + strlen(value) + 2; /* name, =, value, NUL */ 1091 char *str; 1092 1093 if ((str = malloc(sz)) == NULL) 1094 return (NULL); 1095 1096 (void) snprintf(str, sz, "%s=%s", name, value); 1097 return (str); 1098 } 1099 1100 /* 1101 * Prepare envp array for exec'd process. 1102 */ 1103 static char ** 1104 prep_env() 1105 { 1106 int e = 0, size = 1; 1107 char **new_env, *estr; 1108 char *term = getenv("TERM"); 1109 1110 size++; /* for $PATH */ 1111 if (term != NULL) 1112 size++; 1113 1114 /* 1115 * In failsafe mode we set $HOME, since '-l' isn't valid in this mode. 1116 * We also set $SHELL, since neither login nor su will be around to do 1117 * it. 1118 */ 1119 if (failsafe) 1120 size += 2; 1121 1122 if ((new_env = malloc(sizeof (char *) * size)) == NULL) 1123 return (NULL); 1124 1125 if ((estr = add_env("PATH", DEF_PATH)) == NULL) 1126 return (NULL); 1127 new_env[e++] = estr; 1128 1129 if (term != NULL) { 1130 if ((estr = add_env("TERM", term)) == NULL) 1131 return (NULL); 1132 new_env[e++] = estr; 1133 } 1134 1135 if (failsafe) { 1136 if ((estr = add_env("HOME", "/")) == NULL) 1137 return (NULL); 1138 new_env[e++] = estr; 1139 1140 if ((estr = add_env("SHELL", FAILSAFESHELL)) == NULL) 1141 return (NULL); 1142 new_env[e++] = estr; 1143 } 1144 1145 new_env[e++] = NULL; 1146 1147 assert(e == size); 1148 1149 return (new_env); 1150 } 1151 1152 /* 1153 * Finish the preparation of the envp array for exec'd non-interactive 1154 * zlogins. This is called in the child process *after* we zone_enter(), since 1155 * it derives things we can only know within the zone, such as $HOME, $SHELL, 1156 * etc. We need only do this in the non-interactive, mode, since otherwise 1157 * login(1) will do it. We don't do this in failsafe mode, since it presents 1158 * additional ways in which the command could fail, and we'd prefer to avoid 1159 * that. 1160 */ 1161 static char ** 1162 prep_env_noninteractive(const char *user_cmd, char **env) 1163 { 1164 size_t size; 1165 char **new_env; 1166 int e, i; 1167 char *estr; 1168 char varmail[LOGNAME_MAX + 11]; /* strlen(/var/mail/) = 10, NUL */ 1169 char pwbuf[NSS_BUFLEN_PASSWD + 1]; 1170 struct passwd pwent; 1171 struct passwd *pw = NULL; 1172 1173 assert(env != NULL); 1174 assert(failsafe == 0); 1175 1176 /* 1177 * Exec the "user_cmd" brand hook to get a pwent for the 1178 * login user. If this fails, HOME will be set to "/", SHELL 1179 * will be set to $DEFAULTSHELL, and we will continue to exec 1180 * SUPATH <login> -c <cmd>. 1181 */ 1182 pw = zone_get_user_pw(user_cmd, &pwent, pwbuf, sizeof (pwbuf)); 1183 1184 /* 1185 * Get existing envp size. 1186 */ 1187 for (size = 0; env[size] != NULL; size++) 1188 ; 1189 1190 e = size; 1191 1192 /* 1193 * Finish filling out the environment; we duplicate the environment 1194 * setup described in login(1), for lack of a better precedent. 1195 */ 1196 if (pw != NULL) 1197 size += 3; /* LOGNAME, HOME, MAIL */ 1198 else 1199 size += 1; /* HOME */ 1200 1201 size++; /* always fill in SHELL */ 1202 size++; /* terminating NULL */ 1203 1204 if ((new_env = malloc(sizeof (char *) * size)) == NULL) 1205 goto malloc_fail; 1206 1207 /* 1208 * Copy existing elements of env into new_env. 1209 */ 1210 for (i = 0; env[i] != NULL; i++) { 1211 if ((new_env[i] = strdup(env[i])) == NULL) 1212 goto malloc_fail; 1213 } 1214 assert(e == i); 1215 1216 if (pw != NULL) { 1217 if ((estr = add_env("LOGNAME", pw->pw_name)) == NULL) 1218 goto malloc_fail; 1219 new_env[e++] = estr; 1220 1221 if ((estr = add_env("HOME", pw->pw_dir)) == NULL) 1222 goto malloc_fail; 1223 new_env[e++] = estr; 1224 1225 if (chdir(pw->pw_dir) != 0) 1226 zerror(gettext("Could not chdir to home directory " 1227 "%s: %s"), pw->pw_dir, strerror(errno)); 1228 1229 (void) snprintf(varmail, sizeof (varmail), "/var/mail/%s", 1230 pw->pw_name); 1231 if ((estr = add_env("MAIL", varmail)) == NULL) 1232 goto malloc_fail; 1233 new_env[e++] = estr; 1234 } else { 1235 if ((estr = add_env("HOME", "/")) == NULL) 1236 goto malloc_fail; 1237 new_env[e++] = estr; 1238 } 1239 1240 if (pw != NULL && strlen(pw->pw_shell) > 0) { 1241 if ((estr = add_env("SHELL", pw->pw_shell)) == NULL) 1242 goto malloc_fail; 1243 new_env[e++] = estr; 1244 } else { 1245 if ((estr = add_env("SHELL", DEFAULTSHELL)) == NULL) 1246 goto malloc_fail; 1247 new_env[e++] = estr; 1248 } 1249 1250 new_env[e++] = NULL; /* add terminating NULL */ 1251 1252 assert(e == size); 1253 return (new_env); 1254 1255 malloc_fail: 1256 zperror(gettext("failed to allocate memory for process environment")); 1257 return (NULL); 1258 } 1259 1260 static int 1261 close_func(void *slavefd, int fd) 1262 { 1263 if (fd != *(int *)slavefd) 1264 (void) close(fd); 1265 return (0); 1266 } 1267 1268 static void 1269 set_cmdchar(char *cmdcharstr) 1270 { 1271 char c; 1272 long lc; 1273 1274 if ((c = *cmdcharstr) != '\\') { 1275 cmdchar = c; 1276 return; 1277 } 1278 1279 c = cmdcharstr[1]; 1280 if (c == '\0' || c == '\\') { 1281 cmdchar = '\\'; 1282 return; 1283 } 1284 1285 if (c < '0' || c > '7') { 1286 zerror(gettext("Unrecognized escape character option %s"), 1287 cmdcharstr); 1288 usage(); 1289 } 1290 1291 lc = strtol(cmdcharstr + 1, NULL, 8); 1292 if (lc < 0 || lc > 255) { 1293 zerror(gettext("Octal escape character '%s' too large"), 1294 cmdcharstr); 1295 usage(); 1296 } 1297 cmdchar = (char)lc; 1298 } 1299 1300 static int 1301 setup_utmpx(char *slavename) 1302 { 1303 struct utmpx ut; 1304 1305 bzero(&ut, sizeof (ut)); 1306 (void) strncpy(ut.ut_user, ".zlogin", sizeof (ut.ut_user)); 1307 (void) strncpy(ut.ut_line, slavename, sizeof (ut.ut_line)); 1308 ut.ut_pid = getpid(); 1309 ut.ut_id[0] = 'z'; 1310 ut.ut_id[1] = ut.ut_id[2] = ut.ut_id[3] = (char)SC_WILDC; 1311 ut.ut_type = LOGIN_PROCESS; 1312 (void) time(&ut.ut_tv.tv_sec); 1313 1314 if (makeutx(&ut) == NULL) { 1315 zerror(gettext("makeutx failed")); 1316 return (-1); 1317 } 1318 return (0); 1319 } 1320 1321 static void 1322 release_lock_file(int lockfd) 1323 { 1324 (void) close(lockfd); 1325 } 1326 1327 static int 1328 grab_lock_file(const char *zone_name, int *lockfd) 1329 { 1330 char pathbuf[PATH_MAX]; 1331 struct flock flock; 1332 1333 if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) { 1334 zerror(gettext("could not mkdir %s: %s"), ZONES_TMPDIR, 1335 strerror(errno)); 1336 return (-1); 1337 } 1338 (void) chmod(ZONES_TMPDIR, S_IRWXU); 1339 (void) snprintf(pathbuf, sizeof (pathbuf), "%s/%s.zoneadm.lock", 1340 ZONES_TMPDIR, zone_name); 1341 1342 if ((*lockfd = open(pathbuf, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) { 1343 zerror(gettext("could not open %s: %s"), pathbuf, 1344 strerror(errno)); 1345 return (-1); 1346 } 1347 /* 1348 * Lock the file to synchronize with other zoneadmds 1349 */ 1350 flock.l_type = F_WRLCK; 1351 flock.l_whence = SEEK_SET; 1352 flock.l_start = (off_t)0; 1353 flock.l_len = (off_t)0; 1354 if (fcntl(*lockfd, F_SETLKW, &flock) < 0) { 1355 zerror(gettext("unable to lock %s: %s"), pathbuf, 1356 strerror(errno)); 1357 release_lock_file(*lockfd); 1358 return (-1); 1359 } 1360 return (Z_OK); 1361 } 1362 1363 static int 1364 start_zoneadmd(const char *zone_name) 1365 { 1366 pid_t retval; 1367 int pstatus = 0, error = -1, lockfd, doorfd; 1368 struct door_info info; 1369 char doorpath[MAXPATHLEN]; 1370 1371 (void) snprintf(doorpath, sizeof (doorpath), ZONE_DOOR_PATH, zone_name); 1372 1373 if (grab_lock_file(zone_name, &lockfd) != Z_OK) 1374 return (-1); 1375 /* 1376 * We must do the door check with the lock held. Otherwise, we 1377 * might race against another zoneadm/zlogin process and wind 1378 * up with two processes trying to start zoneadmd at the same 1379 * time. zoneadmd will detect this, and fail, but we prefer this 1380 * to be as seamless as is practical, from a user perspective. 1381 */ 1382 if ((doorfd = open(doorpath, O_RDONLY)) < 0) { 1383 if (errno != ENOENT) { 1384 zerror("failed to open %s: %s", doorpath, 1385 strerror(errno)); 1386 goto out; 1387 } 1388 } else { 1389 /* 1390 * Seems to be working ok. 1391 */ 1392 if (door_info(doorfd, &info) == 0 && 1393 ((info.di_attributes & DOOR_REVOKED) == 0)) { 1394 error = 0; 1395 goto out; 1396 } 1397 } 1398 1399 if ((child_pid = fork()) == -1) { 1400 zperror(gettext("could not fork")); 1401 goto out; 1402 } else if (child_pid == 0) { 1403 /* child process */ 1404 (void) execl("/usr/lib/zones/zoneadmd", "zoneadmd", "-z", 1405 zone_name, NULL); 1406 zperror(gettext("could not exec zoneadmd")); 1407 _exit(1); 1408 } 1409 1410 /* parent process */ 1411 do { 1412 retval = waitpid(child_pid, &pstatus, 0); 1413 } while (retval != child_pid); 1414 if (WIFSIGNALED(pstatus) || 1415 (WIFEXITED(pstatus) && WEXITSTATUS(pstatus) != 0)) { 1416 zerror(gettext("could not start %s"), "zoneadmd"); 1417 goto out; 1418 } 1419 error = 0; 1420 out: 1421 release_lock_file(lockfd); 1422 (void) close(doorfd); 1423 return (error); 1424 } 1425 1426 static int 1427 init_template(void) 1428 { 1429 int fd; 1430 int err = 0; 1431 1432 fd = open64(CTFS_ROOT "/process/template", O_RDWR); 1433 if (fd == -1) 1434 return (-1); 1435 1436 /* 1437 * zlogin doesn't do anything with the contract. 1438 * Deliver no events, don't inherit, and allow it to be orphaned. 1439 */ 1440 err |= ct_tmpl_set_critical(fd, 0); 1441 err |= ct_tmpl_set_informative(fd, 0); 1442 err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); 1443 err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); 1444 if (err || ct_tmpl_activate(fd)) { 1445 (void) close(fd); 1446 return (-1); 1447 } 1448 1449 return (fd); 1450 } 1451 1452 static int 1453 noninteractive_login(char *zonename, const char *user_cmd, zoneid_t zoneid, 1454 char **new_args, char **new_env) 1455 { 1456 pid_t retval; 1457 int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2]; 1458 int child_status; 1459 int tmpl_fd; 1460 sigset_t block_cld; 1461 1462 if ((tmpl_fd = init_template()) == -1) { 1463 reset_tty(); 1464 zperror(gettext("could not create contract")); 1465 return (1); 1466 } 1467 1468 if (pipe(stdin_pipe) != 0) { 1469 zperror(gettext("could not create STDIN pipe")); 1470 return (1); 1471 } 1472 /* 1473 * When the user types ^D, we get a zero length message on STDIN. 1474 * We need to echo that down the pipe to send it to the other side; 1475 * but by default, pipes don't propagate zero-length messages. We 1476 * toggle that behavior off using I_SWROPT. See streamio(7i). 1477 */ 1478 if (ioctl(stdin_pipe[0], I_SWROPT, SNDZERO) != 0) { 1479 zperror(gettext("could not configure STDIN pipe")); 1480 return (1); 1481 1482 } 1483 if (pipe(stdout_pipe) != 0) { 1484 zperror(gettext("could not create STDOUT pipe")); 1485 return (1); 1486 } 1487 if (pipe(stderr_pipe) != 0) { 1488 zperror(gettext("could not create STDERR pipe")); 1489 return (1); 1490 } 1491 1492 /* 1493 * If any of the pipe FD's winds up being less than STDERR, then we 1494 * have a mess on our hands-- and we are lacking some of the I/O 1495 * streams we would expect anyway. So we bail. 1496 */ 1497 if (stdin_pipe[0] <= STDERR_FILENO || 1498 stdin_pipe[1] <= STDERR_FILENO || 1499 stdout_pipe[0] <= STDERR_FILENO || 1500 stdout_pipe[1] <= STDERR_FILENO || 1501 stderr_pipe[0] <= STDERR_FILENO || 1502 stderr_pipe[1] <= STDERR_FILENO) { 1503 zperror(gettext("process lacks valid STDIN, STDOUT, STDERR")); 1504 return (1); 1505 } 1506 1507 if (prefork_dropprivs() != 0) { 1508 zperror(gettext("could not allocate privilege set")); 1509 return (1); 1510 } 1511 1512 (void) sigset(SIGCLD, sigcld); 1513 (void) sigemptyset(&block_cld); 1514 (void) sigaddset(&block_cld, SIGCLD); 1515 (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 1516 1517 if ((child_pid = fork()) == -1) { 1518 (void) ct_tmpl_clear(tmpl_fd); 1519 (void) close(tmpl_fd); 1520 zperror(gettext("could not fork")); 1521 return (1); 1522 } else if (child_pid == 0) { /* child process */ 1523 (void) ct_tmpl_clear(tmpl_fd); 1524 1525 /* 1526 * Do a dance to get the pipes hooked up as FD's 0, 1 and 2. 1527 */ 1528 (void) close(STDIN_FILENO); 1529 (void) close(STDOUT_FILENO); 1530 (void) close(STDERR_FILENO); 1531 (void) dup2(stdin_pipe[1], STDIN_FILENO); 1532 (void) dup2(stdout_pipe[1], STDOUT_FILENO); 1533 (void) dup2(stderr_pipe[1], STDERR_FILENO); 1534 (void) closefrom(STDERR_FILENO + 1); 1535 1536 (void) sigset(SIGCLD, SIG_DFL); 1537 (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 1538 /* 1539 * In case any of stdin, stdout or stderr are streams, 1540 * anchor them to prevent malicious I_POPs. 1541 */ 1542 (void) ioctl(STDIN_FILENO, I_ANCHOR); 1543 (void) ioctl(STDOUT_FILENO, I_ANCHOR); 1544 (void) ioctl(STDERR_FILENO, I_ANCHOR); 1545 1546 if (zone_enter(zoneid) == -1) { 1547 zerror(gettext("could not enter zone %s: %s"), 1548 zonename, strerror(errno)); 1549 _exit(1); 1550 } 1551 1552 /* 1553 * For non-native zones, tell libc where it can find locale 1554 * specific getttext() messages. 1555 */ 1556 if (access("/.SUNWnative/usr/lib/locale", R_OK) == 0) 1557 (void) bindtextdomain(TEXT_DOMAIN, 1558 "/.SUNWnative/usr/lib/locale"); 1559 else if (access("/native/usr/lib/locale", R_OK) == 0) 1560 (void) bindtextdomain(TEXT_DOMAIN, 1561 "/native/usr/lib/locale"); 1562 1563 if (!failsafe) 1564 new_env = prep_env_noninteractive(user_cmd, new_env); 1565 1566 if (new_env == NULL) { 1567 _exit(1); 1568 } 1569 1570 /* 1571 * Move into a new process group; the zone_enter will have 1572 * placed us into zsched's session, and we want to be in 1573 * a unique process group. 1574 */ 1575 (void) setpgid(getpid(), getpid()); 1576 1577 (void) execve(new_args[0], new_args, new_env); 1578 zperror(gettext("exec failure")); 1579 _exit(1); 1580 } 1581 /* parent */ 1582 (void) sigset(SIGINT, sig_forward); 1583 1584 postfork_dropprivs(); 1585 1586 (void) ct_tmpl_clear(tmpl_fd); 1587 (void) close(tmpl_fd); 1588 1589 (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 1590 doio(stdin_pipe[0], stdin_pipe[1], stdout_pipe[0], stderr_pipe[0], 1591 B_TRUE); 1592 do { 1593 retval = waitpid(child_pid, &child_status, 0); 1594 if (retval == -1) { 1595 child_status = 0; 1596 } 1597 } while (retval != child_pid && errno != ECHILD); 1598 1599 return (WEXITSTATUS(child_status)); 1600 } 1601 1602 int 1603 main(int argc, char **argv) 1604 { 1605 int arg, console = 0; 1606 zoneid_t zoneid; 1607 zone_state_t st; 1608 char *login = "root"; 1609 int lflag = 0; 1610 char *zonename = NULL; 1611 char **proc_args = NULL; 1612 char **new_args, **new_env; 1613 sigset_t block_cld; 1614 char devroot[MAXPATHLEN]; 1615 char *slavename, slaveshortname[MAXPATHLEN]; 1616 priv_set_t *privset; 1617 int tmpl_fd; 1618 char zonebrand[MAXNAMELEN]; 1619 struct stat sb; 1620 char kernzone[ZONENAME_MAX]; 1621 brand_handle_t bh; 1622 char user_cmd[MAXPATHLEN]; 1623 1624 (void) setlocale(LC_ALL, ""); 1625 (void) textdomain(TEXT_DOMAIN); 1626 1627 (void) getpname(argv[0]); 1628 1629 while ((arg = getopt(argc, argv, "ECR:Se:l:")) != EOF) { 1630 switch (arg) { 1631 case 'C': 1632 console = 1; 1633 break; 1634 case 'E': 1635 nocmdchar = 1; 1636 break; 1637 case 'R': /* undocumented */ 1638 if (*optarg != '/') { 1639 zerror(gettext("root path must be absolute.")); 1640 exit(2); 1641 } 1642 if (stat(optarg, &sb) == -1 || !S_ISDIR(sb.st_mode)) { 1643 zerror( 1644 gettext("root path must be a directory.")); 1645 exit(2); 1646 } 1647 zonecfg_set_root(optarg); 1648 break; 1649 case 'S': 1650 failsafe = 1; 1651 break; 1652 case 'e': 1653 set_cmdchar(optarg); 1654 break; 1655 case 'l': 1656 login = optarg; 1657 lflag = 1; 1658 break; 1659 default: 1660 usage(); 1661 } 1662 } 1663 1664 if (console != 0 && lflag != 0) { 1665 zerror(gettext("-l may not be specified for console login")); 1666 usage(); 1667 } 1668 1669 if (console != 0 && failsafe != 0) { 1670 zerror(gettext("-S may not be specified for console login")); 1671 usage(); 1672 } 1673 1674 if (console != 0 && zonecfg_in_alt_root()) { 1675 zerror(gettext("-R may not be specified for console login")); 1676 exit(2); 1677 } 1678 1679 if (failsafe != 0 && lflag != 0) { 1680 zerror(gettext("-l may not be specified for failsafe login")); 1681 usage(); 1682 } 1683 1684 if (optind == (argc - 1)) { 1685 /* 1686 * zone name, no process name; this should be an interactive 1687 * as long as STDIN is really a tty. 1688 */ 1689 if (isatty(STDIN_FILENO)) 1690 interactive = 1; 1691 zonename = argv[optind]; 1692 } else if (optind < (argc - 1)) { 1693 if (console) { 1694 zerror(gettext("Commands may not be specified for " 1695 "console login.")); 1696 usage(); 1697 } 1698 /* zone name and process name, and possibly some args */ 1699 zonename = argv[optind]; 1700 proc_args = &argv[optind + 1]; 1701 interactive = 0; 1702 } else { 1703 usage(); 1704 } 1705 1706 if (getzoneid() != GLOBAL_ZONEID) { 1707 zerror(gettext("'%s' may only be used from the global zone"), 1708 pname); 1709 return (1); 1710 } 1711 1712 if (strcmp(zonename, GLOBAL_ZONENAME) == 0) { 1713 zerror(gettext("'%s' not applicable to the global zone"), 1714 pname); 1715 return (1); 1716 } 1717 1718 if (zone_get_state(zonename, &st) != Z_OK) { 1719 zerror(gettext("zone '%s' unknown"), zonename); 1720 return (1); 1721 } 1722 1723 if (st < ZONE_STATE_INSTALLED) { 1724 zerror(gettext("cannot login to a zone which is '%s'"), 1725 zone_state_str(st)); 1726 return (1); 1727 } 1728 1729 /* 1730 * In both console and non-console cases, we require all privs. 1731 * In the console case, because we may need to startup zoneadmd. 1732 * In the non-console case in order to do zone_enter(2), zonept() 1733 * and other tasks. 1734 * 1735 * Future work: this solution is temporary. Ultimately, we need to 1736 * move to a flexible system which allows the global admin to 1737 * designate that a particular user can zlogin (and probably zlogin 1738 * -C) to a particular zone. This all-root business we have now is 1739 * quite sketchy. 1740 */ 1741 if ((privset = priv_allocset()) == NULL) { 1742 zperror(gettext("priv_allocset failed")); 1743 return (1); 1744 } 1745 1746 if (getppriv(PRIV_EFFECTIVE, privset) != 0) { 1747 zperror(gettext("getppriv failed")); 1748 priv_freeset(privset); 1749 return (1); 1750 } 1751 1752 if (priv_isfullset(privset) == B_FALSE) { 1753 zerror(gettext("You lack sufficient privilege to run " 1754 "this command (all privs required)")); 1755 priv_freeset(privset); 1756 return (1); 1757 } 1758 priv_freeset(privset); 1759 1760 /* 1761 * The console is a separate case from the rest of the code; handle 1762 * it first. 1763 */ 1764 if (console) { 1765 1766 /* 1767 * Ensure that zoneadmd for this zone is running. 1768 */ 1769 if (start_zoneadmd(zonename) == -1) 1770 return (1); 1771 1772 /* 1773 * Make contact with zoneadmd. 1774 */ 1775 if (get_console_master(zonename) == -1) 1776 return (1); 1777 1778 (void) printf(gettext("[Connected to zone '%s' console]\n"), 1779 zonename); 1780 1781 if (set_tty_rawmode(STDIN_FILENO) == -1) { 1782 reset_tty(); 1783 zperror(gettext("failed to set stdin pty to raw mode")); 1784 return (1); 1785 } 1786 1787 (void) sigset(SIGWINCH, sigwinch); 1788 (void) sigwinch(0); 1789 1790 /* 1791 * Run the I/O loop until we get disconnected. 1792 */ 1793 doio(masterfd, -1, masterfd, -1, B_FALSE); 1794 reset_tty(); 1795 (void) printf(gettext("\n[Connection to zone '%s' console " 1796 "closed]\n"), zonename); 1797 1798 return (0); 1799 } 1800 1801 if (st != ZONE_STATE_RUNNING && st != ZONE_STATE_MOUNTED) { 1802 zerror(gettext("login allowed only to running zones " 1803 "(%s is '%s')."), zonename, zone_state_str(st)); 1804 return (1); 1805 } 1806 1807 (void) strlcpy(kernzone, zonename, sizeof (kernzone)); 1808 if (zonecfg_in_alt_root()) { 1809 FILE *fp = zonecfg_open_scratch("", B_FALSE); 1810 1811 if (fp == NULL || zonecfg_find_scratch(fp, zonename, 1812 zonecfg_get_root(), kernzone, sizeof (kernzone)) == -1) { 1813 zerror(gettext("cannot find scratch zone %s"), 1814 zonename); 1815 if (fp != NULL) 1816 zonecfg_close_scratch(fp); 1817 return (1); 1818 } 1819 zonecfg_close_scratch(fp); 1820 } 1821 1822 if ((zoneid = getzoneidbyname(kernzone)) == -1) { 1823 zerror(gettext("failed to get zoneid for zone '%s'"), 1824 zonename); 1825 return (1); 1826 } 1827 1828 /* 1829 * We need the zone root path only if we are setting up a pty. 1830 */ 1831 if (zone_get_devroot(zonename, devroot, sizeof (devroot)) == -1) { 1832 zerror(gettext("could not get dev path for zone %s"), 1833 zonename); 1834 return (1); 1835 } 1836 1837 /* Get a handle to the brand info for this zone */ 1838 if ((zone_get_brand(zonename, zonebrand, sizeof (zonebrand)) != Z_OK) || 1839 ((bh = brand_open(zonebrand)) == NULL)) { 1840 zerror(gettext("could not get brand for zone %s"), zonename); 1841 return (1); 1842 } 1843 if ((new_args = prep_args(bh, login, proc_args)) == NULL) { 1844 zperror(gettext("could not assemble new arguments")); 1845 brand_close(bh); 1846 return (1); 1847 } 1848 /* 1849 * Get the brand specific user_cmd. This command is used to get 1850 * a passwd(4) entry for login. 1851 */ 1852 if (!interactive && !failsafe) { 1853 if (zone_get_user_cmd(bh, login, user_cmd, 1854 sizeof (user_cmd)) == NULL) { 1855 zerror(gettext("could not get user_cmd for zone %s"), 1856 zonename); 1857 brand_close(bh); 1858 return (1); 1859 } 1860 } 1861 brand_close(bh); 1862 1863 if ((new_env = prep_env()) == NULL) { 1864 zperror(gettext("could not assemble new environment")); 1865 return (1); 1866 } 1867 1868 if (!interactive) 1869 return (noninteractive_login(zonename, user_cmd, zoneid, 1870 new_args, new_env)); 1871 1872 if (zonecfg_in_alt_root()) { 1873 zerror(gettext("cannot use interactive login with scratch " 1874 "zone")); 1875 return (1); 1876 } 1877 1878 /* 1879 * Things are more complex in interactive mode; we get the 1880 * master side of the pty, then place the user's terminal into 1881 * raw mode. 1882 */ 1883 if (get_master_pty() == -1) { 1884 zerror(gettext("could not setup master pty device")); 1885 return (1); 1886 } 1887 1888 /* 1889 * Compute the "short name" of the pts. /dev/pts/2 --> pts/2 1890 */ 1891 if ((slavename = ptsname(masterfd)) == NULL) { 1892 zperror(gettext("failed to get name for pseudo-tty")); 1893 return (1); 1894 } 1895 if (strncmp(slavename, "/dev/", strlen("/dev/")) == 0) 1896 (void) strlcpy(slaveshortname, slavename + strlen("/dev/"), 1897 sizeof (slaveshortname)); 1898 else 1899 (void) strlcpy(slaveshortname, slavename, 1900 sizeof (slaveshortname)); 1901 1902 (void) printf(gettext("[Connected to zone '%s' %s]\n"), zonename, 1903 slaveshortname); 1904 1905 if (set_tty_rawmode(STDIN_FILENO) == -1) { 1906 reset_tty(); 1907 zperror(gettext("failed to set stdin pty to raw mode")); 1908 return (1); 1909 } 1910 1911 if (prefork_dropprivs() != 0) { 1912 reset_tty(); 1913 zperror(gettext("could not allocate privilege set")); 1914 return (1); 1915 } 1916 1917 /* 1918 * We must mask SIGCLD until after we have coped with the fork 1919 * sufficiently to deal with it; otherwise we can race and receive the 1920 * signal before child_pid has been initialized (yes, this really 1921 * happens). 1922 */ 1923 (void) sigset(SIGCLD, sigcld); 1924 (void) sigemptyset(&block_cld); 1925 (void) sigaddset(&block_cld, SIGCLD); 1926 (void) sigprocmask(SIG_BLOCK, &block_cld, NULL); 1927 1928 /* 1929 * We activate the contract template at the last minute to 1930 * avoid intermediate functions that could be using fork(2) 1931 * internally. 1932 */ 1933 if ((tmpl_fd = init_template()) == -1) { 1934 reset_tty(); 1935 zperror(gettext("could not create contract")); 1936 return (1); 1937 } 1938 1939 if ((child_pid = fork()) == -1) { 1940 (void) ct_tmpl_clear(tmpl_fd); 1941 reset_tty(); 1942 zperror(gettext("could not fork")); 1943 return (1); 1944 } else if (child_pid == 0) { /* child process */ 1945 int slavefd, newslave; 1946 1947 (void) ct_tmpl_clear(tmpl_fd); 1948 (void) close(tmpl_fd); 1949 1950 (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 1951 1952 if ((slavefd = init_slave_pty(zoneid, devroot)) == -1) 1953 return (1); 1954 1955 /* 1956 * Close all fds except for the slave pty. 1957 */ 1958 (void) fdwalk(close_func, &slavefd); 1959 1960 /* 1961 * Temporarily dup slavefd to stderr; that way if we have 1962 * to print out that zone_enter failed, the output will 1963 * have somewhere to go. 1964 */ 1965 if (slavefd != STDERR_FILENO) 1966 (void) dup2(slavefd, STDERR_FILENO); 1967 1968 if (zone_enter(zoneid) == -1) { 1969 zerror(gettext("could not enter zone %s: %s"), 1970 zonename, strerror(errno)); 1971 return (1); 1972 } 1973 1974 if (slavefd != STDERR_FILENO) 1975 (void) close(STDERR_FILENO); 1976 1977 /* 1978 * We take pains to get this process into a new process 1979 * group, and subsequently a new session. In this way, 1980 * we'll have a session which doesn't yet have a controlling 1981 * terminal. When we open the slave, it will become the 1982 * controlling terminal; no PIDs concerning pgrps or sids 1983 * will leak inappropriately into the zone. 1984 */ 1985 (void) setpgrp(); 1986 1987 /* 1988 * We need the slave pty to be referenced from the zone's 1989 * /dev in order to ensure that the devt's, etc are all 1990 * correct. Otherwise we break ttyname and the like. 1991 */ 1992 if ((newslave = open(slavename, O_RDWR)) == -1) { 1993 (void) close(slavefd); 1994 return (1); 1995 } 1996 (void) close(slavefd); 1997 slavefd = newslave; 1998 1999 /* 2000 * dup the slave to the various FDs, so that when the 2001 * spawned process does a write/read it maps to the slave 2002 * pty. 2003 */ 2004 (void) dup2(slavefd, STDIN_FILENO); 2005 (void) dup2(slavefd, STDOUT_FILENO); 2006 (void) dup2(slavefd, STDERR_FILENO); 2007 if (slavefd != STDIN_FILENO && slavefd != STDOUT_FILENO && 2008 slavefd != STDERR_FILENO) { 2009 (void) close(slavefd); 2010 } 2011 2012 /* 2013 * In failsafe mode, we don't use login(1), so don't try 2014 * setting up a utmpx entry. 2015 * 2016 * A branded zone may have very different utmpx semantics. 2017 * At the moment, we only have two brand types: 2018 * Solaris-like (native, sn1) and Linux. In the Solaris 2019 * case, we know exactly how to do the necessary utmpx 2020 * setup. Fortunately for us, the Linux /bin/login is 2021 * prepared to deal with a non-initialized utmpx entry, so 2022 * we can simply skip it. If future brands don't fall into 2023 * either category, we'll have to add a per-brand utmpx 2024 * setup hook. 2025 */ 2026 if (!failsafe && (strcmp(zonebrand, "lx") != 0)) 2027 if (setup_utmpx(slaveshortname) == -1) 2028 return (1); 2029 2030 (void) execve(new_args[0], new_args, new_env); 2031 zperror(gettext("exec failure")); 2032 return (1); 2033 } 2034 (void) ct_tmpl_clear(tmpl_fd); 2035 (void) close(tmpl_fd); 2036 2037 /* 2038 * The rest is only for the parent process. 2039 */ 2040 (void) sigset(SIGWINCH, sigwinch); 2041 2042 postfork_dropprivs(); 2043 2044 (void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL); 2045 doio(masterfd, -1, masterfd, -1, B_FALSE); 2046 2047 reset_tty(); 2048 (void) fprintf(stderr, 2049 gettext("\n[Connection to zone '%s' %s closed]\n"), zonename, 2050 slaveshortname); 2051 2052 if (pollerr != 0) { 2053 (void) fprintf(stderr, gettext("Error: connection closed due " 2054 "to unexpected pollevents=0x%x.\n"), pollerr); 2055 return (1); 2056 } 2057 2058 return (0); 2059 } 2060