1 /* $OpenBSD: tmux.c,v 1.52 2009/10/25 21:11:21 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/stat.h> 21 22 #include <errno.h> 23 #include <paths.h> 24 #include <pwd.h> 25 #include <signal.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <syslog.h> 29 #include <unistd.h> 30 31 #include "tmux.h" 32 33 #ifdef DEBUG 34 const char *malloc_options = "AFGJPX"; 35 #endif 36 37 volatile sig_atomic_t sigwinch; 38 volatile sig_atomic_t sigterm; 39 volatile sig_atomic_t sigcont; 40 volatile sig_atomic_t sigchld; 41 volatile sig_atomic_t sigusr1; 42 volatile sig_atomic_t sigusr2; 43 44 char *cfg_file; 45 struct options global_s_options; /* session options */ 46 struct options global_w_options; /* window options */ 47 struct environ global_environ; 48 49 int debug_level; 50 int be_quiet; 51 time_t start_time; 52 char *socket_path; 53 int login_shell; 54 55 __dead void usage(void); 56 void fill_session(struct msg_command_data *); 57 char *makesockpath(const char *); 58 int dispatch_imsg(struct imsgbuf *, const char *, int *); 59 __dead void shell_exec(const char *, const char *); 60 61 __dead void 62 usage(void) 63 { 64 fprintf(stderr, 65 "usage: %s [-28lquv] [-c shell-command] [-f file] [-L socket-name]\n" 66 " [-S socket-path] [command [flags]]\n", 67 __progname); 68 exit(1); 69 } 70 71 void 72 logfile(const char *name) 73 { 74 char *path; 75 76 log_close(); 77 if (debug_level > 0) { 78 xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid()); 79 log_open_file(debug_level, path); 80 xfree(path); 81 } 82 } 83 84 void 85 sighandler(int sig) 86 { 87 int saved_errno; 88 89 saved_errno = errno; 90 switch (sig) { 91 case SIGWINCH: 92 sigwinch = 1; 93 break; 94 case SIGTERM: 95 sigterm = 1; 96 break; 97 case SIGCHLD: 98 sigchld = 1; 99 break; 100 case SIGCONT: 101 sigcont = 1; 102 break; 103 case SIGUSR1: 104 sigusr1 = 1; 105 break; 106 case SIGUSR2: 107 sigusr2 = 1; 108 break; 109 } 110 errno = saved_errno; 111 } 112 113 void 114 siginit(void) 115 { 116 struct sigaction act; 117 118 memset(&act, 0, sizeof act); 119 sigemptyset(&act.sa_mask); 120 act.sa_flags = SA_RESTART; 121 122 act.sa_handler = SIG_IGN; 123 if (sigaction(SIGPIPE, &act, NULL) != 0) 124 fatal("sigaction failed"); 125 if (sigaction(SIGINT, &act, NULL) != 0) 126 fatal("sigaction failed"); 127 if (sigaction(SIGTSTP, &act, NULL) != 0) 128 fatal("sigaction failed"); 129 if (sigaction(SIGQUIT, &act, NULL) != 0) 130 fatal("sigaction failed"); 131 132 act.sa_handler = sighandler; 133 if (sigaction(SIGWINCH, &act, NULL) != 0) 134 fatal("sigaction failed"); 135 if (sigaction(SIGTERM, &act, NULL) != 0) 136 fatal("sigaction failed"); 137 if (sigaction(SIGCHLD, &act, NULL) != 0) 138 fatal("sigaction failed"); 139 if (sigaction(SIGUSR1, &act, NULL) != 0) 140 fatal("sigaction failed"); 141 if (sigaction(SIGUSR2, &act, NULL) != 0) 142 fatal("sigaction failed"); 143 } 144 145 void 146 sigreset(void) 147 { 148 struct sigaction act; 149 150 memset(&act, 0, sizeof act); 151 sigemptyset(&act.sa_mask); 152 153 act.sa_handler = SIG_DFL; 154 if (sigaction(SIGPIPE, &act, NULL) != 0) 155 fatal("sigaction failed"); 156 if (sigaction(SIGUSR1, &act, NULL) != 0) 157 fatal("sigaction failed"); 158 if (sigaction(SIGUSR2, &act, NULL) != 0) 159 fatal("sigaction failed"); 160 if (sigaction(SIGINT, &act, NULL) != 0) 161 fatal("sigaction failed"); 162 if (sigaction(SIGTSTP, &act, NULL) != 0) 163 fatal("sigaction failed"); 164 if (sigaction(SIGQUIT, &act, NULL) != 0) 165 fatal("sigaction failed"); 166 if (sigaction(SIGWINCH, &act, NULL) != 0) 167 fatal("sigaction failed"); 168 if (sigaction(SIGTERM, &act, NULL) != 0) 169 fatal("sigaction failed"); 170 if (sigaction(SIGCHLD, &act, NULL) != 0) 171 fatal("sigaction failed"); 172 } 173 174 const char * 175 getshell(void) 176 { 177 struct passwd *pw; 178 const char *shell; 179 180 shell = getenv("SHELL"); 181 if (checkshell(shell)) 182 return (shell); 183 184 pw = getpwuid(getuid()); 185 if (pw != NULL && checkshell(pw->pw_shell)) 186 return (pw->pw_shell); 187 188 return (_PATH_BSHELL); 189 } 190 191 int 192 checkshell(const char *shell) 193 { 194 if (shell == NULL || *shell == '\0' || areshell(shell)) 195 return (0); 196 if (access(shell, X_OK) != 0) 197 return (0); 198 return (1); 199 } 200 201 int 202 areshell(const char *shell) 203 { 204 const char *progname, *ptr; 205 206 if ((ptr = strrchr(shell, '/')) != NULL) 207 ptr++; 208 else 209 ptr = shell; 210 progname = __progname; 211 if (*progname == '-') 212 progname++; 213 if (strcmp(ptr, progname) == 0) 214 return (1); 215 return (0); 216 } 217 218 void 219 fill_session(struct msg_command_data *data) 220 { 221 char *env, *ptr1, *ptr2, buf[256]; 222 size_t len; 223 const char *errstr; 224 long long ll; 225 226 data->pid = -1; 227 if ((env = getenv("TMUX")) == NULL) 228 return; 229 230 if ((ptr2 = strrchr(env, ',')) == NULL || ptr2 == env) 231 return; 232 for (ptr1 = ptr2 - 1; ptr1 > env && *ptr1 != ','; ptr1--) 233 ; 234 if (*ptr1 != ',') 235 return; 236 ptr1++; 237 ptr2++; 238 239 len = ptr2 - ptr1 - 1; 240 if (len > (sizeof buf) - 1) 241 return; 242 memcpy(buf, ptr1, len); 243 buf[len] = '\0'; 244 245 ll = strtonum(buf, 0, LONG_MAX, &errstr); 246 if (errstr != NULL) 247 return; 248 data->pid = ll; 249 250 ll = strtonum(ptr2, 0, UINT_MAX, &errstr); 251 if (errstr != NULL) 252 return; 253 data->idx = ll; 254 } 255 256 char * 257 makesockpath(const char *label) 258 { 259 char base[MAXPATHLEN], *path; 260 struct stat sb; 261 u_int uid; 262 263 uid = getuid(); 264 xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid); 265 266 if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) 267 return (NULL); 268 269 if (lstat(base, &sb) != 0) 270 return (NULL); 271 if (!S_ISDIR(sb.st_mode)) { 272 errno = ENOTDIR; 273 return (NULL); 274 } 275 if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) { 276 errno = EACCES; 277 return (NULL); 278 } 279 280 xasprintf(&path, "%s/%s", base, label); 281 return (path); 282 } 283 284 int 285 main(int argc, char **argv) 286 { 287 struct cmd_list *cmdlist; 288 struct cmd *cmd; 289 struct pollfd pfd; 290 enum msgtype msg; 291 struct passwd *pw; 292 struct options *so, *wo; 293 struct keylist *keylist; 294 struct imsgbuf *ibuf; 295 struct msg_command_data cmddata; 296 char *s, *shellcmd, *path, *label, *home, *cause; 297 char cwd[MAXPATHLEN], **var; 298 void *buf; 299 size_t len; 300 int nfds, retcode, opt, flags, cmdflags = 0; 301 302 flags = 0; 303 shellcmd = label = path = NULL; 304 login_shell = (**argv == '-'); 305 while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) { 306 switch (opt) { 307 case '2': 308 flags |= IDENTIFY_256COLOURS; 309 flags &= ~IDENTIFY_88COLOURS; 310 break; 311 case '8': 312 flags |= IDENTIFY_88COLOURS; 313 flags &= ~IDENTIFY_256COLOURS; 314 break; 315 case 'c': 316 if (shellcmd != NULL) 317 xfree(shellcmd); 318 shellcmd = xstrdup(optarg); 319 break; 320 case 'f': 321 if (cfg_file != NULL) 322 xfree(cfg_file); 323 cfg_file = xstrdup(optarg); 324 break; 325 case 'l': 326 login_shell = 1; 327 break; 328 case 'L': 329 if (label != NULL) 330 xfree(label); 331 label = xstrdup(optarg); 332 break; 333 case 'q': 334 be_quiet = 1; 335 break; 336 case 'S': 337 if (path != NULL) 338 xfree(path); 339 path = xstrdup(optarg); 340 break; 341 case 'u': 342 flags |= IDENTIFY_UTF8; 343 break; 344 case 'v': 345 debug_level++; 346 break; 347 default: 348 usage(); 349 } 350 } 351 argc -= optind; 352 argv += optind; 353 354 if (shellcmd != NULL && argc != 0) 355 usage(); 356 357 log_open_tty(debug_level); 358 siginit(); 359 360 if (!(flags & IDENTIFY_UTF8)) { 361 /* 362 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG 363 * exist (in that order) to contain UTF-8, it is a safe 364 * assumption that either they are using a UTF-8 terminal, or 365 * if not they know that output from UTF-8-capable programs may 366 * be wrong. 367 */ 368 if ((s = getenv("LC_ALL")) == NULL) { 369 if ((s = getenv("LC_CTYPE")) == NULL) 370 s = getenv("LANG"); 371 } 372 if (s != NULL && (strcasestr(s, "UTF-8") != NULL || 373 strcasestr(s, "UTF8") != NULL)) 374 flags |= IDENTIFY_UTF8; 375 } 376 377 environ_init(&global_environ); 378 for (var = environ; *var != NULL; var++) 379 environ_put(&global_environ, *var); 380 381 options_init(&global_s_options, NULL); 382 so = &global_s_options; 383 options_set_number(so, "base-index", 0); 384 options_set_number(so, "bell-action", BELL_ANY); 385 options_set_number(so, "buffer-limit", 9); 386 options_set_string(so, "default-command", "%s", ""); 387 options_set_string(so, "default-shell", "%s", getshell()); 388 options_set_string(so, "default-terminal", "screen"); 389 options_set_number(so, "display-panes-colour", 4); 390 options_set_number(so, "display-panes-time", 1000); 391 options_set_number(so, "display-time", 750); 392 options_set_number(so, "history-limit", 2000); 393 options_set_number(so, "lock-after-time", 0); 394 options_set_string(so, "lock-command", "lock -np"); 395 options_set_number(so, "lock-server", 1); 396 options_set_number(so, "message-attr", 0); 397 options_set_number(so, "message-bg", 3); 398 options_set_number(so, "message-fg", 0); 399 options_set_number(so, "mouse-select-pane", 0); 400 options_set_number(so, "repeat-time", 500); 401 options_set_number(so, "set-remain-on-exit", 0); 402 options_set_number(so, "set-titles", 0); 403 options_set_string(so, "set-titles-string", "#S:#I:#W - \"#T\""); 404 options_set_number(so, "status", 1); 405 options_set_number(so, "status-attr", 0); 406 options_set_number(so, "status-bg", 2); 407 options_set_number(so, "status-fg", 0); 408 options_set_number(so, "status-interval", 15); 409 options_set_number(so, "status-justify", 0); 410 options_set_number(so, "status-keys", MODEKEY_EMACS); 411 options_set_string(so, "status-left", "[#S]"); 412 options_set_number(so, "status-left-attr", 0); 413 options_set_number(so, "status-left-bg", 8); 414 options_set_number(so, "status-left-fg", 8); 415 options_set_number(so, "status-left-length", 10); 416 options_set_string(so, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y"); 417 options_set_number(so, "status-right-attr", 0); 418 options_set_number(so, "status-right-bg", 8); 419 options_set_number(so, "status-right-fg", 8); 420 options_set_number(so, "status-right-length", 40); 421 options_set_string(so, "terminal-overrides", 422 "*88col*:colors=88,*256col*:colors=256"); 423 options_set_string(so, "update-environment", "DISPLAY " 424 "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION"); 425 options_set_number(so, "visual-activity", 0); 426 options_set_number(so, "visual-bell", 0); 427 options_set_number(so, "visual-content", 0); 428 429 keylist = xmalloc(sizeof *keylist); 430 ARRAY_INIT(keylist); 431 ARRAY_ADD(keylist, '\002'); 432 options_set_data(so, "prefix", keylist, xfree); 433 434 options_init(&global_w_options, NULL); 435 wo = &global_w_options; 436 options_set_number(wo, "aggressive-resize", 0); 437 options_set_number(wo, "automatic-rename", 1); 438 options_set_number(wo, "clock-mode-colour", 4); 439 options_set_number(wo, "clock-mode-style", 1); 440 options_set_number(wo, "force-height", 0); 441 options_set_number(wo, "force-width", 0); 442 options_set_number(wo, "main-pane-height", 24); 443 options_set_number(wo, "main-pane-width", 81); 444 options_set_number(wo, "mode-attr", 0); 445 options_set_number(wo, "mode-bg", 3); 446 options_set_number(wo, "mode-fg", 0); 447 options_set_number(wo, "mode-keys", MODEKEY_EMACS); 448 options_set_number(wo, "mode-mouse", 0); 449 options_set_number(wo, "monitor-activity", 0); 450 options_set_string(wo, "monitor-content", "%s", ""); 451 options_set_number(wo, "window-status-attr", 0); 452 options_set_number(wo, "window-status-bg", 8); 453 options_set_number(wo, "window-status-current-attr", 0); 454 options_set_number(wo, "window-status-current-bg", 8); 455 options_set_number(wo, "window-status-current-fg", 8); 456 options_set_number(wo, "window-status-fg", 8); 457 options_set_number(wo, "xterm-keys", 0); 458 options_set_number(wo, "remain-on-exit", 0); 459 options_set_number(wo, "synchronize-panes", 0); 460 461 if (flags & IDENTIFY_UTF8) { 462 options_set_number(so, "status-utf8", 1); 463 options_set_number(wo, "utf8", 1); 464 } else { 465 options_set_number(so, "status-utf8", 0); 466 options_set_number(wo, "utf8", 0); 467 } 468 469 if (getcwd(cwd, sizeof cwd) == NULL) { 470 pw = getpwuid(getuid()); 471 if (pw->pw_dir != NULL && *pw->pw_dir != '\0') 472 strlcpy(cwd, pw->pw_dir, sizeof cwd); 473 else 474 strlcpy(cwd, "/", sizeof cwd); 475 } 476 options_set_string(so, "default-path", "%s", cwd); 477 478 if (cfg_file == NULL) { 479 home = getenv("HOME"); 480 if (home == NULL || *home == '\0') { 481 pw = getpwuid(getuid()); 482 if (pw != NULL) 483 home = pw->pw_dir; 484 } 485 xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); 486 if (access(cfg_file, R_OK) != 0) { 487 xfree(cfg_file); 488 cfg_file = NULL; 489 } 490 } else { 491 if (access(cfg_file, R_OK) != 0) { 492 log_warn("%s", cfg_file); 493 exit(1); 494 } 495 } 496 497 if (label == NULL) 498 label = xstrdup("default"); 499 if (path == NULL && (path = makesockpath(label)) == NULL) { 500 log_warn("can't create socket"); 501 exit(1); 502 } 503 xfree(label); 504 505 if (shellcmd != NULL) { 506 msg = MSG_SHELL; 507 buf = NULL; 508 len = 0; 509 } else { 510 fill_session(&cmddata); 511 512 cmddata.argc = argc; 513 if (cmd_pack_argv( 514 argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) { 515 log_warnx("command too long"); 516 exit(1); 517 } 518 519 msg = MSG_COMMAND; 520 buf = &cmddata; 521 len = sizeof cmddata; 522 } 523 524 if (shellcmd != NULL) 525 cmdflags |= CMD_STARTSERVER; 526 else if (argc == 0) /* new-session is the default */ 527 cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON; 528 else { 529 /* 530 * It sucks parsing the command string twice (in client and 531 * later in server) but it is necessary to get the start server 532 * flag. 533 */ 534 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) { 535 log_warnx("%s", cause); 536 exit(1); 537 } 538 cmdflags &= ~CMD_STARTSERVER; 539 TAILQ_FOREACH(cmd, cmdlist, qentry) { 540 if (cmd->entry->flags & CMD_STARTSERVER) 541 cmdflags |= CMD_STARTSERVER; 542 if (cmd->entry->flags & CMD_SENDENVIRON) 543 cmdflags |= CMD_SENDENVIRON; 544 } 545 cmd_list_free(cmdlist); 546 } 547 548 if ((ibuf = client_init(path, cmdflags, flags)) == NULL) 549 exit(1); 550 xfree(path); 551 552 imsg_compose(ibuf, msg, PROTOCOL_VERSION, -1, -1, buf, len); 553 554 retcode = 0; 555 for (;;) { 556 pfd.fd = ibuf->fd; 557 pfd.events = POLLIN; 558 if (ibuf->w.queued != 0) 559 pfd.events |= POLLOUT; 560 561 if ((nfds = poll(&pfd, 1, INFTIM)) == -1) { 562 if (errno == EAGAIN || errno == EINTR) 563 continue; 564 fatal("poll failed"); 565 } 566 if (nfds == 0) 567 continue; 568 569 if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL)) 570 fatalx("socket error"); 571 572 if (pfd.revents & POLLIN) { 573 if (dispatch_imsg(ibuf, shellcmd, &retcode) != 0) 574 break; 575 } 576 577 if (pfd.revents & POLLOUT) { 578 if (msgbuf_write(&ibuf->w) < 0) 579 fatalx("msgbuf_write failed"); 580 } 581 } 582 583 options_free(&global_s_options); 584 options_free(&global_w_options); 585 586 return (retcode); 587 } 588 589 int 590 dispatch_imsg(struct imsgbuf *ibuf, const char *shellcmd, int *retcode) 591 { 592 struct imsg imsg; 593 ssize_t n, datalen; 594 struct msg_print_data printdata; 595 struct msg_shell_data shelldata; 596 597 if ((n = imsg_read(ibuf)) == -1 || n == 0) 598 fatalx("imsg_read failed"); 599 600 for (;;) { 601 if ((n = imsg_get(ibuf, &imsg)) == -1) 602 fatalx("imsg_get failed"); 603 if (n == 0) 604 return (0); 605 datalen = imsg.hdr.len - IMSG_HEADER_SIZE; 606 607 switch (imsg.hdr.type) { 608 case MSG_EXIT: 609 case MSG_SHUTDOWN: 610 if (datalen != 0) 611 fatalx("bad MSG_EXIT size"); 612 613 return (-1); 614 case MSG_ERROR: 615 *retcode = 1; 616 /* FALLTHROUGH */ 617 case MSG_PRINT: 618 if (datalen != sizeof printdata) 619 fatalx("bad MSG_PRINT size"); 620 memcpy(&printdata, imsg.data, sizeof printdata); 621 printdata.msg[(sizeof printdata.msg) - 1] = '\0'; 622 623 log_info("%s", printdata.msg); 624 break; 625 case MSG_READY: 626 if (datalen != 0) 627 fatalx("bad MSG_READY size"); 628 629 client_main(); /* doesn't return */ 630 case MSG_VERSION: 631 if (datalen != 0) 632 fatalx("bad MSG_VERSION size"); 633 634 log_warnx("protocol version mismatch (client %u, " 635 "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid); 636 *retcode = 1; 637 return (-1); 638 case MSG_SHELL: 639 if (datalen != sizeof shelldata) 640 fatalx("bad MSG_SHELL size"); 641 memcpy(&shelldata, imsg.data, sizeof shelldata); 642 shelldata.shell[(sizeof shelldata.shell) - 1] = '\0'; 643 644 shell_exec(shelldata.shell, shellcmd); 645 default: 646 fatalx("unexpected message"); 647 } 648 649 imsg_free(&imsg); 650 } 651 } 652 653 __dead void 654 shell_exec(const char *shell, const char *shellcmd) 655 { 656 const char *shellname, *ptr; 657 char *argv0; 658 659 sigreset(); 660 661 ptr = strrchr(shell, '/'); 662 if (ptr != NULL && *(ptr + 1) != '\0') 663 shellname = ptr + 1; 664 else 665 shellname = shell; 666 if (login_shell) 667 xasprintf(&argv0, "-%s", shellname); 668 else 669 xasprintf(&argv0, "%s", shellname); 670 setenv("SHELL", shell, 1); 671 672 execl(shell, argv0, "-c", shellcmd, (char *) NULL); 673 fatal("execl failed"); 674 } 675