1 /* $OpenBSD: tmux.c,v 1.202 2020/06/02 08:17:27 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 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 #include <sys/utsname.h> 22 23 #include <err.h> 24 #include <errno.h> 25 #include <event.h> 26 #include <fcntl.h> 27 #include <langinfo.h> 28 #include <locale.h> 29 #include <paths.h> 30 #include <pwd.h> 31 #include <signal.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <time.h> 35 #include <unistd.h> 36 #include <util.h> 37 38 #include "tmux.h" 39 40 struct options *global_options; /* server options */ 41 struct options *global_s_options; /* session options */ 42 struct options *global_w_options; /* window options */ 43 struct environ *global_environ; 44 45 struct timeval start_time; 46 const char *socket_path; 47 int ptm_fd = -1; 48 const char *shell_command; 49 50 static __dead void usage(void); 51 static char *make_label(const char *, char **); 52 53 static int areshell(const char *); 54 static const char *getshell(void); 55 56 static __dead void 57 usage(void) 58 { 59 fprintf(stderr, 60 "usage: %s [-2CDluvV] [-c shell-command] [-f file] [-L socket-name]\n" 61 " [-S socket-path] [-T features] [command [flags]]\n", 62 getprogname()); 63 exit(1); 64 } 65 66 static const char * 67 getshell(void) 68 { 69 struct passwd *pw; 70 const char *shell; 71 72 shell = getenv("SHELL"); 73 if (checkshell(shell)) 74 return (shell); 75 76 pw = getpwuid(getuid()); 77 if (pw != NULL && checkshell(pw->pw_shell)) 78 return (pw->pw_shell); 79 80 return (_PATH_BSHELL); 81 } 82 83 int 84 checkshell(const char *shell) 85 { 86 if (shell == NULL || *shell != '/') 87 return (0); 88 if (areshell(shell)) 89 return (0); 90 if (access(shell, X_OK) != 0) 91 return (0); 92 return (1); 93 } 94 95 static int 96 areshell(const char *shell) 97 { 98 const char *progname, *ptr; 99 100 if ((ptr = strrchr(shell, '/')) != NULL) 101 ptr++; 102 else 103 ptr = shell; 104 progname = getprogname(); 105 if (*progname == '-') 106 progname++; 107 if (strcmp(ptr, progname) == 0) 108 return (1); 109 return (0); 110 } 111 112 static char * 113 expand_path(const char *path, const char *home) 114 { 115 char *expanded, *name; 116 const char *end; 117 struct environ_entry *value; 118 119 if (strncmp(path, "~/", 2) == 0) { 120 if (home == NULL) 121 return (NULL); 122 xasprintf(&expanded, "%s%s", home, path + 1); 123 return (expanded); 124 } 125 126 if (*path == '$') { 127 end = strchr(path, '/'); 128 if (end == NULL) 129 name = xstrdup(path + 1); 130 else 131 name = xstrndup(path + 1, end - path - 1); 132 value = environ_find(global_environ, name); 133 free(name); 134 if (value == NULL) 135 return (NULL); 136 if (end == NULL) 137 end = ""; 138 xasprintf(&expanded, "%s%s", value->value, end); 139 return (expanded); 140 } 141 142 return (xstrdup(path)); 143 } 144 145 void 146 expand_paths(const char *s, char ***paths, u_int *n) 147 { 148 const char *home = find_home(); 149 char *copy, *next, *tmp, resolved[PATH_MAX], *expanded; 150 u_int i; 151 152 *paths = NULL; 153 *n = 0; 154 155 copy = tmp = xstrdup(s); 156 while ((next = strsep(&tmp, ":")) != NULL) { 157 expanded = expand_path(next, home); 158 if (expanded == NULL) { 159 log_debug("%s: invalid path: %s", __func__, next); 160 continue; 161 } 162 if (realpath(expanded, resolved) == NULL) { 163 log_debug("%s: realpath(\"%s\") failed: %s", __func__, 164 expanded, strerror(errno)); 165 free(expanded); 166 continue; 167 } 168 free(expanded); 169 for (i = 0; i < *n; i++) { 170 if (strcmp(resolved, (*paths)[i]) == 0) 171 break; 172 } 173 if (i != *n) { 174 log_debug("%s: duplicate path: %s", __func__, resolved); 175 continue; 176 } 177 *paths = xreallocarray(*paths, (*n) + 1, sizeof *paths); 178 (*paths)[(*n)++] = xstrdup(resolved); 179 } 180 free(copy); 181 } 182 183 static char * 184 make_label(const char *label, char **cause) 185 { 186 char **paths, *path, *base; 187 u_int i, n; 188 struct stat sb; 189 uid_t uid; 190 191 *cause = NULL; 192 if (label == NULL) 193 label = "default"; 194 uid = getuid(); 195 196 expand_paths(TMUX_SOCK, &paths, &n); 197 if (n == 0) { 198 xasprintf(cause, "no suitable socket path"); 199 return (NULL); 200 } 201 path = paths[0]; /* can only have one socket! */ 202 for (i = 1; i < n; i++) 203 free(paths[i]); 204 free(paths); 205 206 xasprintf(&base, "%s/tmux-%ld", path, (long)uid); 207 if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) 208 goto fail; 209 if (lstat(base, &sb) != 0) 210 goto fail; 211 if (!S_ISDIR(sb.st_mode)) { 212 errno = ENOTDIR; 213 goto fail; 214 } 215 if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) { 216 errno = EACCES; 217 goto fail; 218 } 219 xasprintf(&path, "%s/%s", base, label); 220 free(base); 221 return (path); 222 223 fail: 224 xasprintf(cause, "error creating %s (%s)", base, strerror(errno)); 225 free(base); 226 return (NULL); 227 } 228 229 void 230 setblocking(int fd, int state) 231 { 232 int mode; 233 234 if ((mode = fcntl(fd, F_GETFL)) != -1) { 235 if (!state) 236 mode |= O_NONBLOCK; 237 else 238 mode &= ~O_NONBLOCK; 239 fcntl(fd, F_SETFL, mode); 240 } 241 } 242 243 uint64_t 244 get_timer(void) 245 { 246 struct timespec ts; 247 248 /* 249 * We want a timestamp in milliseconds suitable for time measurement, 250 * so prefer the monotonic clock. 251 */ 252 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) 253 clock_gettime(CLOCK_REALTIME, &ts); 254 return ((ts.tv_sec * 1000ULL) + (ts.tv_nsec / 1000000ULL)); 255 } 256 257 const char * 258 sig2name(int signo) 259 { 260 static char s[11]; 261 262 if (signo > 0 && signo < NSIG) 263 return (sys_signame[signo]); 264 xsnprintf(s, sizeof s, "%d", signo); 265 return (s); 266 } 267 268 const char * 269 find_cwd(void) 270 { 271 char resolved1[PATH_MAX], resolved2[PATH_MAX]; 272 static char cwd[PATH_MAX]; 273 const char *pwd; 274 275 if (getcwd(cwd, sizeof cwd) == NULL) 276 return (NULL); 277 if ((pwd = getenv("PWD")) == NULL || *pwd == '\0') 278 return (cwd); 279 280 /* 281 * We want to use PWD so that symbolic links are maintained, 282 * but only if it matches the actual working directory. 283 */ 284 if (realpath(pwd, resolved1) == NULL) 285 return (cwd); 286 if (realpath(cwd, resolved2) == NULL) 287 return (cwd); 288 if (strcmp(resolved1, resolved2) != 0) 289 return (cwd); 290 return (pwd); 291 } 292 293 const char * 294 find_home(void) 295 { 296 struct passwd *pw; 297 static const char *home; 298 299 if (home != NULL) 300 return (home); 301 302 home = getenv("HOME"); 303 if (home == NULL || *home == '\0') { 304 pw = getpwuid(getuid()); 305 if (pw != NULL) 306 home = pw->pw_dir; 307 else 308 home = NULL; 309 } 310 311 return (home); 312 } 313 314 const char * 315 getversion(void) 316 { 317 static char *version; 318 struct utsname u; 319 320 if (version == NULL) { 321 if (uname(&u) < 0) 322 fatalx("uname failed"); 323 xasprintf(&version, "openbsd-%s", u.release); 324 } 325 return (version); 326 } 327 328 int 329 main(int argc, char **argv) 330 { 331 char *path = NULL, *label = NULL; 332 char *cause, **var; 333 const char *s, *shell, *cwd; 334 int opt, flags = 0, keys; 335 int feat = 0; 336 const struct options_table_entry *oe; 337 338 if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL && 339 setlocale(LC_CTYPE, "C.UTF-8") == NULL) { 340 if (setlocale(LC_CTYPE, "") == NULL) 341 errx(1, "invalid LC_ALL, LC_CTYPE or LANG"); 342 s = nl_langinfo(CODESET); 343 if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0) 344 errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s); 345 } 346 347 setlocale(LC_TIME, ""); 348 tzset(); 349 350 if (**argv == '-') 351 flags = CLIENT_LOGIN; 352 353 while ((opt = getopt(argc, argv, "2c:CDdf:lL:qS:T:uUvV")) != -1) { 354 switch (opt) { 355 case '2': 356 tty_add_features(&feat, "256", ":,"); 357 break; 358 case 'c': 359 shell_command = optarg; 360 break; 361 case 'D': 362 flags |= CLIENT_NOFORK; 363 break; 364 case 'C': 365 if (flags & CLIENT_CONTROL) 366 flags |= CLIENT_CONTROLCONTROL; 367 else 368 flags |= CLIENT_CONTROL; 369 break; 370 case 'f': 371 set_cfg_file(optarg); 372 break; 373 case 'V': 374 printf("%s %s\n", getprogname(), getversion()); 375 exit(0); 376 case 'l': 377 flags |= CLIENT_LOGIN; 378 break; 379 case 'L': 380 free(label); 381 label = xstrdup(optarg); 382 break; 383 case 'q': 384 break; 385 case 'S': 386 free(path); 387 path = xstrdup(optarg); 388 break; 389 case 'T': 390 tty_add_features(&feat, optarg, ":,"); 391 break; 392 case 'u': 393 flags |= CLIENT_UTF8; 394 break; 395 case 'v': 396 log_add_level(); 397 break; 398 default: 399 usage(); 400 } 401 } 402 argc -= optind; 403 argv += optind; 404 405 if (shell_command != NULL && argc != 0) 406 usage(); 407 if ((flags & CLIENT_NOFORK) && argc != 0) 408 usage(); 409 410 if ((ptm_fd = getptmfd()) == -1) 411 err(1, "getptmfd"); 412 if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd " 413 "recvfd proc exec tty ps", NULL) != 0) 414 err(1, "pledge"); 415 416 /* 417 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8. 418 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain 419 * UTF-8, it is a safe assumption that either they are using a UTF-8 420 * terminal, or if not they know that output from UTF-8-capable 421 * programs may be wrong. 422 */ 423 if (getenv("TMUX") != NULL) 424 flags |= CLIENT_UTF8; 425 else { 426 s = getenv("LC_ALL"); 427 if (s == NULL || *s == '\0') 428 s = getenv("LC_CTYPE"); 429 if (s == NULL || *s == '\0') 430 s = getenv("LANG"); 431 if (s == NULL || *s == '\0') 432 s = ""; 433 if (strcasestr(s, "UTF-8") != NULL || 434 strcasestr(s, "UTF8") != NULL) 435 flags |= CLIENT_UTF8; 436 } 437 438 global_environ = environ_create(); 439 for (var = environ; *var != NULL; var++) 440 environ_put(global_environ, *var, 0); 441 if ((cwd = find_cwd()) != NULL) 442 environ_set(global_environ, "PWD", 0, "%s", cwd); 443 444 global_options = options_create(NULL); 445 global_s_options = options_create(NULL); 446 global_w_options = options_create(NULL); 447 for (oe = options_table; oe->name != NULL; oe++) { 448 if (oe->scope & OPTIONS_TABLE_SERVER) 449 options_default(global_options, oe); 450 if (oe->scope & OPTIONS_TABLE_SESSION) 451 options_default(global_s_options, oe); 452 if (oe->scope & OPTIONS_TABLE_WINDOW) 453 options_default(global_w_options, oe); 454 } 455 456 /* 457 * The default shell comes from SHELL or from the user's passwd entry 458 * if available. 459 */ 460 shell = getshell(); 461 options_set_string(global_s_options, "default-shell", 0, "%s", shell); 462 463 /* Override keys to vi if VISUAL or EDITOR are set. */ 464 if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) { 465 options_set_string(global_options, "editor", 0, "%s", s); 466 if (strrchr(s, '/') != NULL) 467 s = strrchr(s, '/') + 1; 468 if (strstr(s, "vi") != NULL) 469 keys = MODEKEY_VI; 470 else 471 keys = MODEKEY_EMACS; 472 options_set_number(global_s_options, "status-keys", keys); 473 options_set_number(global_w_options, "mode-keys", keys); 474 } 475 476 /* 477 * If socket is specified on the command-line with -S or -L, it is 478 * used. Otherwise, $TMUX is checked and if that fails "default" is 479 * used. 480 */ 481 if (path == NULL && label == NULL) { 482 s = getenv("TMUX"); 483 if (s != NULL && *s != '\0' && *s != ',') { 484 path = xstrdup(s); 485 path[strcspn(path, ",")] = '\0'; 486 } 487 } 488 if (path == NULL) { 489 if ((path = make_label(label, &cause)) == NULL) { 490 if (cause != NULL) { 491 fprintf(stderr, "%s\n", cause); 492 free(cause); 493 } 494 exit(1); 495 } 496 flags |= CLIENT_DEFAULTSOCKET; 497 } 498 socket_path = path; 499 free(label); 500 501 /* Pass control to the client. */ 502 exit(client_main(event_init(), argc, argv, flags, feat)); 503 } 504