1 /* $OpenBSD$ */ 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 22 #include <errno.h> 23 #include <event.h> 24 #include <fcntl.h> 25 #include <langinfo.h> 26 #include <locale.h> 27 #include <pwd.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <time.h> 31 #include <unistd.h> 32 33 #include "tmux.h" 34 35 struct options *global_options; /* server options */ 36 struct options *global_s_options; /* session options */ 37 struct options *global_w_options; /* window options */ 38 struct environ *global_environ; 39 struct hooks *global_hooks; 40 41 struct timeval start_time; 42 const char *socket_path; 43 int ptm_fd = -1; 44 const char *shell_command; 45 46 static __dead void usage(void); 47 static char *make_label(const char *, char **); 48 49 static const char *getshell(void); 50 static int checkshell(const char *); 51 52 static __dead void 53 usage(void) 54 { 55 fprintf(stderr, 56 "usage: %s [-2CluvV] [-c shell-command] [-f file] [-L socket-name]\n" 57 " [-S socket-path] [command [flags]]\n", 58 getprogname()); 59 exit(1); 60 } 61 62 static const char * 63 getshell(void) 64 { 65 struct passwd *pw; 66 const char *shell; 67 68 shell = getenv("SHELL"); 69 if (checkshell(shell)) 70 return (shell); 71 72 pw = getpwuid(getuid()); 73 if (pw != NULL && checkshell(pw->pw_shell)) 74 return (pw->pw_shell); 75 76 return (_PATH_BSHELL); 77 } 78 79 static int 80 checkshell(const char *shell) 81 { 82 if (shell == NULL || *shell != '/') 83 return (0); 84 if (areshell(shell)) 85 return (0); 86 if (access(shell, X_OK) != 0) 87 return (0); 88 return (1); 89 } 90 91 int 92 areshell(const char *shell) 93 { 94 const char *progname, *ptr; 95 96 if ((ptr = strrchr(shell, '/')) != NULL) 97 ptr++; 98 else 99 ptr = shell; 100 progname = getprogname(); 101 if (*progname == '-') 102 progname++; 103 if (strcmp(ptr, progname) == 0) 104 return (1); 105 return (0); 106 } 107 108 static char * 109 make_label(const char *label, char **cause) 110 { 111 char *base, resolved[PATH_MAX], *path, *s; 112 struct stat sb; 113 uid_t uid; 114 115 *cause = NULL; 116 117 if (label == NULL) 118 label = "default"; 119 uid = getuid(); 120 121 if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0') 122 xasprintf(&base, "%s/tmux-%ld", s, (long)uid); 123 else 124 xasprintf(&base, "%s/tmux-%ld", _PATH_TMP, (long)uid); 125 if (realpath(base, resolved) == NULL && 126 strlcpy(resolved, base, sizeof resolved) >= sizeof resolved) { 127 errno = ERANGE; 128 free(base); 129 goto fail; 130 } 131 132 if (mkdir(resolved, S_IRWXU) != 0 && errno != EEXIST) 133 goto fail; 134 if (lstat(resolved, &sb) != 0) 135 goto fail; 136 if (!S_ISDIR(sb.st_mode)) { 137 errno = ENOTDIR; 138 goto fail; 139 } 140 if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) { 141 errno = EACCES; 142 goto fail; 143 } 144 xasprintf(&path, "%s/%s", resolved, label); 145 return (path); 146 147 fail: 148 xasprintf(cause, "error creating %s (%s)", resolved, strerror(errno)); 149 return (NULL); 150 } 151 152 void 153 setblocking(int fd, int state) 154 { 155 int mode; 156 157 if ((mode = fcntl(fd, F_GETFL)) != -1) { 158 if (!state) 159 mode |= O_NONBLOCK; 160 else 161 mode &= ~O_NONBLOCK; 162 fcntl(fd, F_SETFL, mode); 163 } 164 } 165 166 const char * 167 find_home(void) 168 { 169 struct passwd *pw; 170 static const char *home; 171 172 if (home != NULL) 173 return (home); 174 175 home = getenv("HOME"); 176 if (home == NULL || *home == '\0') { 177 pw = getpwuid(getuid()); 178 if (pw != NULL) 179 home = pw->pw_dir; 180 else 181 home = NULL; 182 } 183 184 return (home); 185 } 186 187 int 188 main(int argc, char **argv) 189 { 190 char *path, *label, *cause, **var; 191 char tmp[PATH_MAX]; 192 const char *s, *shell, *cwd; 193 int opt, flags, keys; 194 const struct options_table_entry *oe; 195 196 if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL && 197 setlocale(LC_CTYPE, "C.UTF-8") == NULL) { 198 if (setlocale(LC_CTYPE, "") == NULL) 199 errx(1, "invalid LC_ALL, LC_CTYPE or LANG"); 200 s = nl_langinfo(CODESET); 201 if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0) 202 errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s); 203 } 204 205 setlocale(LC_TIME, ""); 206 tzset(); 207 208 if (**argv == '-') 209 flags = CLIENT_LOGIN; 210 else 211 flags = 0; 212 213 label = path = NULL; 214 while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUVv")) != -1) { 215 switch (opt) { 216 case '2': 217 flags |= CLIENT_256COLOURS; 218 break; 219 case 'c': 220 shell_command = optarg; 221 break; 222 case 'C': 223 if (flags & CLIENT_CONTROL) 224 flags |= CLIENT_CONTROLCONTROL; 225 else 226 flags |= CLIENT_CONTROL; 227 break; 228 case 'V': 229 printf("%s %s\n", getprogname(), VERSION); 230 exit(0); 231 case 'f': 232 set_cfg_file(optarg); 233 break; 234 case 'l': 235 flags |= CLIENT_LOGIN; 236 break; 237 case 'L': 238 free(label); 239 label = xstrdup(optarg); 240 break; 241 case 'q': 242 break; 243 case 'S': 244 free(path); 245 path = xstrdup(optarg); 246 break; 247 case 'u': 248 flags |= CLIENT_UTF8; 249 break; 250 case 'v': 251 log_add_level(); 252 break; 253 default: 254 usage(); 255 } 256 } 257 argc -= optind; 258 argv += optind; 259 260 if (shell_command != NULL && argc != 0) 261 usage(); 262 263 if ((ptm_fd = getptmfd()) == -1) 264 err(1, "getptmfd"); 265 if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd " 266 "recvfd proc exec tty ps", NULL) != 0) 267 err(1, "pledge"); 268 269 /* 270 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8. 271 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain 272 * UTF-8, it is a safe assumption that either they are using a UTF-8 273 * terminal, or if not they know that output from UTF-8-capable 274 * programs may be wrong. 275 */ 276 if (getenv("TMUX") != NULL) 277 flags |= CLIENT_UTF8; 278 else { 279 s = getenv("LC_ALL"); 280 if (s == NULL || *s == '\0') 281 s = getenv("LC_CTYPE"); 282 if (s == NULL || *s == '\0') 283 s = getenv("LANG"); 284 if (s == NULL || *s == '\0') 285 s = ""; 286 if (strcasestr(s, "UTF-8") != NULL || 287 strcasestr(s, "UTF8") != NULL) 288 flags |= CLIENT_UTF8; 289 } 290 291 global_hooks = hooks_create(NULL); 292 293 global_environ = environ_create(); 294 for (var = environ; *var != NULL; var++) 295 environ_put(global_environ, *var); 296 if ((cwd = getenv("PWD")) == NULL && 297 (cwd = getcwd(tmp, sizeof tmp)) != NULL) 298 environ_set(global_environ, "PWD", "%s", cwd); 299 300 global_options = options_create(NULL); 301 global_s_options = options_create(NULL); 302 global_w_options = options_create(NULL); 303 for (oe = options_table; oe->name != NULL; oe++) { 304 if (oe->scope == OPTIONS_TABLE_SERVER) 305 options_default(global_options, oe); 306 if (oe->scope == OPTIONS_TABLE_SESSION) 307 options_default(global_s_options, oe); 308 if (oe->scope == OPTIONS_TABLE_WINDOW) 309 options_default(global_w_options, oe); 310 } 311 312 /* 313 * The default shell comes from SHELL or from the user's passwd entry 314 * if available. 315 */ 316 shell = getshell(); 317 options_set_string(global_s_options, "default-shell", 0, "%s", shell); 318 319 /* Override keys to vi if VISUAL or EDITOR are set. */ 320 if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) { 321 if (strrchr(s, '/') != NULL) 322 s = strrchr(s, '/') + 1; 323 if (strstr(s, "vi") != NULL) 324 keys = MODEKEY_VI; 325 else 326 keys = MODEKEY_EMACS; 327 options_set_number(global_s_options, "status-keys", keys); 328 options_set_number(global_w_options, "mode-keys", keys); 329 } 330 331 /* 332 * If socket is specified on the command-line with -S or -L, it is 333 * used. Otherwise, $TMUX is checked and if that fails "default" is 334 * used. 335 */ 336 if (path == NULL && label == NULL) { 337 s = getenv("TMUX"); 338 if (s != NULL && *s != '\0' && *s != ',') { 339 path = xstrdup(s); 340 path[strcspn(path, ",")] = '\0'; 341 } 342 } 343 if (path == NULL && (path = make_label(label, &cause)) == NULL) { 344 if (cause != NULL) { 345 fprintf(stderr, "%s\n", cause); 346 free(cause); 347 } 348 exit(1); 349 } 350 socket_path = path; 351 free(label); 352 353 /* Pass control to the client. */ 354 exit(client_main(osdep_event_init(), argc, argv, flags)); 355 } 356