1 /* $OpenBSD: tmux.c,v 1.49 2009/10/10 14:51:16 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 char *makesockpath(const char *); 57 int prepare_cmd(enum msgtype *, void **, size_t *, int, char **); 58 int dispatch_imsg(struct client_ctx *, 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 [-28dlquv] [-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 char * 219 makesockpath(const char *label) 220 { 221 char base[MAXPATHLEN], *path; 222 struct stat sb; 223 u_int uid; 224 225 uid = getuid(); 226 xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid); 227 228 if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) 229 return (NULL); 230 231 if (lstat(base, &sb) != 0) 232 return (NULL); 233 if (!S_ISDIR(sb.st_mode)) { 234 errno = ENOTDIR; 235 return (NULL); 236 } 237 if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) { 238 errno = EACCES; 239 return (NULL); 240 } 241 242 xasprintf(&path, "%s/%s", base, label); 243 return (path); 244 } 245 246 int 247 prepare_cmd(enum msgtype *msg, void **buf, size_t *len, int argc, char **argv) 248 { 249 static struct msg_command_data cmddata; 250 251 client_fill_session(&cmddata); 252 253 cmddata.argc = argc; 254 if (cmd_pack_argv(argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) { 255 log_warnx("command too long"); 256 return (-1); 257 } 258 259 *buf = &cmddata; 260 *len = sizeof cmddata; 261 262 *msg = MSG_COMMAND; 263 return (0); 264 } 265 266 int 267 main(int argc, char **argv) 268 { 269 struct client_ctx cctx; 270 struct cmd_list *cmdlist; 271 struct cmd *cmd; 272 struct pollfd pfd; 273 enum msgtype msg; 274 struct passwd *pw; 275 struct options *so, *wo; 276 struct keylist *keylist; 277 char *s, *shellcmd, *path, *label, *home, *cause; 278 char cwd[MAXPATHLEN], **var; 279 void *buf; 280 size_t len; 281 int retcode, opt, flags, cmdflags = 0; 282 int nfds; 283 284 flags = 0; 285 shellcmd = label = path = NULL; 286 login_shell = (**argv == '-'); 287 while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) { 288 switch (opt) { 289 case '2': 290 flags |= IDENTIFY_256COLOURS; 291 flags &= ~IDENTIFY_88COLOURS; 292 break; 293 case '8': 294 flags |= IDENTIFY_88COLOURS; 295 flags &= ~IDENTIFY_256COLOURS; 296 break; 297 case 'c': 298 if (shellcmd != NULL) 299 xfree(shellcmd); 300 shellcmd = xstrdup(optarg); 301 break; 302 case 'd': 303 flags |= IDENTIFY_HASDEFAULTS; 304 break; 305 case 'f': 306 if (cfg_file != NULL) 307 xfree(cfg_file); 308 cfg_file = xstrdup(optarg); 309 break; 310 case 'l': 311 login_shell = 1; 312 break; 313 case 'L': 314 if (label != NULL) 315 xfree(label); 316 label = xstrdup(optarg); 317 break; 318 case 'q': 319 be_quiet = 1; 320 break; 321 case 'S': 322 if (path != NULL) 323 xfree(path); 324 path = xstrdup(optarg); 325 break; 326 case 'u': 327 flags |= IDENTIFY_UTF8; 328 break; 329 case 'v': 330 debug_level++; 331 break; 332 default: 333 usage(); 334 } 335 } 336 argc -= optind; 337 argv += optind; 338 339 if (shellcmd != NULL && argc != 0) 340 usage(); 341 342 log_open_tty(debug_level); 343 siginit(); 344 345 if (!(flags & IDENTIFY_UTF8)) { 346 /* 347 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG 348 * exist (in that order) to contain UTF-8, it is a safe 349 * assumption that either they are using a UTF-8 terminal, or 350 * if not they know that output from UTF-8-capable programs may 351 * be wrong. 352 */ 353 if ((s = getenv("LC_ALL")) == NULL) { 354 if ((s = getenv("LC_CTYPE")) == NULL) 355 s = getenv("LANG"); 356 } 357 if (s != NULL && (strcasestr(s, "UTF-8") != NULL || 358 strcasestr(s, "UTF8") != NULL)) 359 flags |= IDENTIFY_UTF8; 360 } 361 362 environ_init(&global_environ); 363 for (var = environ; *var != NULL; var++) 364 environ_put(&global_environ, *var); 365 366 options_init(&global_s_options, NULL); 367 so = &global_s_options; 368 options_set_number(so, "base-index", 0); 369 options_set_number(so, "bell-action", BELL_ANY); 370 options_set_number(so, "buffer-limit", 9); 371 options_set_string(so, "default-command", "%s", ""); 372 options_set_string(so, "default-shell", "%s", getshell()); 373 options_set_string(so, "default-terminal", "screen"); 374 options_set_number(so, "display-panes-colour", 4); 375 options_set_number(so, "display-panes-time", 1000); 376 options_set_number(so, "display-time", 750); 377 options_set_number(so, "history-limit", 2000); 378 options_set_number(so, "lock-after-time", 0); 379 options_set_string(so, "lock-command", "lock -np"); 380 options_set_number(so, "lock-server", 1); 381 options_set_number(so, "message-attr", 0); 382 options_set_number(so, "message-bg", 3); 383 options_set_number(so, "message-fg", 0); 384 options_set_number(so, "mouse-select-pane", 0); 385 options_set_number(so, "repeat-time", 500); 386 options_set_number(so, "set-remain-on-exit", 0); 387 options_set_number(so, "set-titles", 0); 388 options_set_string(so, "set-titles-string", "#S:#I:#W - \"#T\""); 389 options_set_number(so, "status", 1); 390 options_set_number(so, "status-attr", 0); 391 options_set_number(so, "status-bg", 2); 392 options_set_number(so, "status-fg", 0); 393 options_set_number(so, "status-interval", 15); 394 options_set_number(so, "status-justify", 0); 395 options_set_number(so, "status-keys", MODEKEY_EMACS); 396 options_set_string(so, "status-left", "[#S]"); 397 options_set_number(so, "status-left-attr", 0); 398 options_set_number(so, "status-left-bg", 8); 399 options_set_number(so, "status-left-fg", 8); 400 options_set_number(so, "status-left-length", 10); 401 options_set_string(so, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y"); 402 options_set_number(so, "status-right-attr", 0); 403 options_set_number(so, "status-right-bg", 8); 404 options_set_number(so, "status-right-fg", 8); 405 options_set_number(so, "status-right-length", 40); 406 options_set_string(so, "terminal-overrides", 407 "*88col*:colors=88,*256col*:colors=256"); 408 options_set_string(so, "update-environment", "DISPLAY " 409 "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION"); 410 options_set_number(so, "visual-activity", 0); 411 options_set_number(so, "visual-bell", 0); 412 options_set_number(so, "visual-content", 0); 413 414 keylist = xmalloc(sizeof *keylist); 415 ARRAY_INIT(keylist); 416 ARRAY_ADD(keylist, '\002'); 417 options_set_data(so, "prefix", keylist, xfree); 418 419 options_init(&global_w_options, NULL); 420 wo = &global_w_options; 421 options_set_number(wo, "aggressive-resize", 0); 422 options_set_number(wo, "automatic-rename", 1); 423 options_set_number(wo, "clock-mode-colour", 4); 424 options_set_number(wo, "clock-mode-style", 1); 425 options_set_number(wo, "force-height", 0); 426 options_set_number(wo, "force-width", 0); 427 options_set_number(wo, "main-pane-height", 24); 428 options_set_number(wo, "main-pane-width", 81); 429 options_set_number(wo, "mode-attr", 0); 430 options_set_number(wo, "mode-bg", 3); 431 options_set_number(wo, "mode-fg", 0); 432 options_set_number(wo, "mode-keys", MODEKEY_EMACS); 433 options_set_number(wo, "mode-mouse", 0); 434 options_set_number(wo, "monitor-activity", 0); 435 options_set_string(wo, "monitor-content", "%s", ""); 436 options_set_number(wo, "window-status-attr", 0); 437 options_set_number(wo, "window-status-bg", 8); 438 options_set_number(wo, "window-status-current-attr", 0); 439 options_set_number(wo, "window-status-current-bg", 8); 440 options_set_number(wo, "window-status-current-fg", 8); 441 options_set_number(wo, "window-status-fg", 8); 442 options_set_number(wo, "xterm-keys", 0); 443 options_set_number(wo, "remain-on-exit", 0); 444 options_set_number(wo, "synchronize-panes", 0); 445 446 if (flags & IDENTIFY_UTF8) { 447 options_set_number(so, "status-utf8", 1); 448 options_set_number(wo, "utf8", 1); 449 } else { 450 options_set_number(so, "status-utf8", 0); 451 options_set_number(wo, "utf8", 0); 452 } 453 454 if (getcwd(cwd, sizeof cwd) == NULL) { 455 pw = getpwuid(getuid()); 456 if (pw->pw_dir != NULL && *pw->pw_dir != '\0') 457 strlcpy(cwd, pw->pw_dir, sizeof cwd); 458 else 459 strlcpy(cwd, "/", sizeof cwd); 460 } 461 options_set_string(so, "default-path", "%s", cwd); 462 463 if (cfg_file == NULL) { 464 home = getenv("HOME"); 465 if (home == NULL || *home == '\0') { 466 pw = getpwuid(getuid()); 467 if (pw != NULL) 468 home = pw->pw_dir; 469 } 470 xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG); 471 if (access(cfg_file, R_OK) != 0) { 472 xfree(cfg_file); 473 cfg_file = NULL; 474 } 475 } else { 476 if (access(cfg_file, R_OK) != 0) { 477 log_warn("%s", cfg_file); 478 exit(1); 479 } 480 } 481 482 if (label == NULL) 483 label = xstrdup("default"); 484 if (path == NULL && (path = makesockpath(label)) == NULL) { 485 log_warn("can't create socket"); 486 exit(1); 487 } 488 xfree(label); 489 490 if (shellcmd != NULL) { 491 msg = MSG_SHELL; 492 buf = NULL; 493 len = 0; 494 } else if (prepare_cmd(&msg, &buf, &len, argc, argv) != 0) 495 exit(1); 496 497 if (shellcmd != NULL) 498 cmdflags |= CMD_STARTSERVER; 499 else if (argc == 0) /* new-session is the default */ 500 cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON; 501 else { 502 /* 503 * It sucks parsing the command string twice (in client and 504 * later in server) but it is necessary to get the start server 505 * flag. 506 */ 507 if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) { 508 log_warnx("%s", cause); 509 exit(1); 510 } 511 cmdflags &= ~CMD_STARTSERVER; 512 TAILQ_FOREACH(cmd, cmdlist, qentry) { 513 if (cmd->entry->flags & CMD_STARTSERVER) 514 cmdflags |= CMD_STARTSERVER; 515 if (cmd->entry->flags & CMD_SENDENVIRON) 516 cmdflags |= CMD_SENDENVIRON; 517 } 518 cmd_list_free(cmdlist); 519 } 520 521 memset(&cctx, 0, sizeof cctx); 522 if (client_init(path, &cctx, cmdflags, flags) != 0) 523 exit(1); 524 xfree(path); 525 526 client_write_server(&cctx, msg, buf, len); 527 memset(buf, 0, len); 528 529 retcode = 0; 530 for (;;) { 531 pfd.fd = cctx.ibuf.fd; 532 pfd.events = POLLIN; 533 if (cctx.ibuf.w.queued != 0) 534 pfd.events |= POLLOUT; 535 536 if ((nfds = poll(&pfd, 1, INFTIM)) == -1) { 537 if (errno == EAGAIN || errno == EINTR) 538 continue; 539 fatal("poll failed"); 540 } 541 if (nfds == 0) 542 continue; 543 544 if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL)) 545 fatalx("socket error"); 546 547 if (pfd.revents & POLLIN) { 548 if (dispatch_imsg(&cctx, shellcmd, &retcode) != 0) 549 break; 550 } 551 552 if (pfd.revents & POLLOUT) { 553 if (msgbuf_write(&cctx.ibuf.w) < 0) 554 fatalx("msgbuf_write failed"); 555 } 556 } 557 558 options_free(&global_s_options); 559 options_free(&global_w_options); 560 561 return (retcode); 562 } 563 564 int 565 dispatch_imsg(struct client_ctx *cctx, const char *shellcmd, int *retcode) 566 { 567 struct imsg imsg; 568 ssize_t n, datalen; 569 struct msg_print_data printdata; 570 struct msg_shell_data shelldata; 571 572 if ((n = imsg_read(&cctx->ibuf)) == -1 || n == 0) 573 fatalx("imsg_read failed"); 574 575 for (;;) { 576 if ((n = imsg_get(&cctx->ibuf, &imsg)) == -1) 577 fatalx("imsg_get failed"); 578 if (n == 0) 579 return (0); 580 datalen = imsg.hdr.len - IMSG_HEADER_SIZE; 581 582 switch (imsg.hdr.type) { 583 case MSG_EXIT: 584 case MSG_SHUTDOWN: 585 if (datalen != 0) 586 fatalx("bad MSG_EXIT size"); 587 588 return (-1); 589 case MSG_ERROR: 590 *retcode = 1; 591 /* FALLTHROUGH */ 592 case MSG_PRINT: 593 if (datalen != sizeof printdata) 594 fatalx("bad MSG_PRINT size"); 595 memcpy(&printdata, imsg.data, sizeof printdata); 596 printdata.msg[(sizeof printdata.msg) - 1] = '\0'; 597 598 log_info("%s", printdata.msg); 599 break; 600 case MSG_READY: 601 if (datalen != 0) 602 fatalx("bad MSG_READY size"); 603 604 *retcode = client_main(cctx); 605 return (-1); 606 case MSG_VERSION: 607 if (datalen != 0) 608 fatalx("bad MSG_VERSION size"); 609 610 log_warnx("protocol version mismatch (client %u, " 611 "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid); 612 *retcode = 1; 613 return (-1); 614 case MSG_SHELL: 615 if (datalen != sizeof shelldata) 616 fatalx("bad MSG_SHELL size"); 617 memcpy(&shelldata, imsg.data, sizeof shelldata); 618 shelldata.shell[(sizeof shelldata.shell) - 1] = '\0'; 619 620 shell_exec(shelldata.shell, shellcmd); 621 default: 622 fatalx("unexpected message"); 623 } 624 625 imsg_free(&imsg); 626 } 627 } 628 629 __dead void 630 shell_exec(const char *shell, const char *shellcmd) 631 { 632 const char *shellname, *ptr; 633 char *argv0; 634 635 sigreset(); 636 637 ptr = strrchr(shell, '/'); 638 if (ptr != NULL && *(ptr + 1) != '\0') 639 shellname = ptr + 1; 640 else 641 shellname = shell; 642 if (login_shell) 643 xasprintf(&argv0, "-%s", shellname); 644 else 645 xasprintf(&argv0, "%s", shellname); 646 setenv("SHELL", shell, 1); 647 648 execl(shell, argv0, "-c", shellcmd, (char *) NULL); 649 fatal("execl failed"); 650 } 651