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