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