xref: /openbsd-src/usr.bin/tmux/tmux.c (revision 8e0c768258d4632c51876b4397034bc3152bf8db)
1 /* $OpenBSD: tmux.c,v 1.190 2019/10/14 08:38:07 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 	free(base);
134 
135 	if (mkdir(resolved, S_IRWXU) != 0 && errno != EEXIST)
136 		goto fail;
137 	if (lstat(resolved, &sb) != 0)
138 		goto fail;
139 	if (!S_ISDIR(sb.st_mode)) {
140 		errno = ENOTDIR;
141 		goto fail;
142 	}
143 	if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
144 		errno = EACCES;
145 		goto fail;
146 	}
147 	xasprintf(&path, "%s/%s", resolved, label);
148 	return (path);
149 
150 fail:
151 	xasprintf(cause, "error creating %s (%s)", resolved, strerror(errno));
152 	return (NULL);
153 }
154 
155 void
156 setblocking(int fd, int state)
157 {
158 	int mode;
159 
160 	if ((mode = fcntl(fd, F_GETFL)) != -1) {
161 		if (!state)
162 			mode |= O_NONBLOCK;
163 		else
164 			mode &= ~O_NONBLOCK;
165 		fcntl(fd, F_SETFL, mode);
166 	}
167 }
168 
169 const char *
170 find_cwd(void)
171 {
172 	char		 resolved1[PATH_MAX], resolved2[PATH_MAX];
173 	static char	 cwd[PATH_MAX];
174 	const char	*pwd;
175 
176 	if (getcwd(cwd, sizeof cwd) == NULL)
177 		return (NULL);
178 	if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
179 		return (cwd);
180 
181 	/*
182 	 * We want to use PWD so that symbolic links are maintained,
183 	 * but only if it matches the actual working directory.
184 	 */
185 	if (realpath(pwd, resolved1) == NULL)
186 		return (cwd);
187 	if (realpath(cwd, resolved2) == NULL)
188 		return (cwd);
189 	if (strcmp(resolved1, resolved2) != 0)
190 		return (cwd);
191 	return (pwd);
192 }
193 
194 const char *
195 find_home(void)
196 {
197 	struct passwd		*pw;
198 	static const char	*home;
199 
200 	if (home != NULL)
201 		return (home);
202 
203 	home = getenv("HOME");
204 	if (home == NULL || *home == '\0') {
205 		pw = getpwuid(getuid());
206 		if (pw != NULL)
207 			home = pw->pw_dir;
208 		else
209 			home = NULL;
210 	}
211 
212 	return (home);
213 }
214 
215 int
216 main(int argc, char **argv)
217 {
218 	char					*path, *label, *cause, **var;
219 	const char				*s, *shell, *cwd;
220 	int					 opt, flags, keys;
221 	const struct options_table_entry	*oe;
222 
223 	if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL &&
224 	    setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
225 		if (setlocale(LC_CTYPE, "") == NULL)
226 			errx(1, "invalid LC_ALL, LC_CTYPE or LANG");
227 		s = nl_langinfo(CODESET);
228 		if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0)
229 			errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
230 	}
231 
232 	setlocale(LC_TIME, "");
233 	tzset();
234 
235 	if (**argv == '-')
236 		flags = CLIENT_LOGIN;
237 	else
238 		flags = 0;
239 
240 	label = path = NULL;
241 	while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUv")) != -1) {
242 		switch (opt) {
243 		case '2':
244 			flags |= CLIENT_256COLOURS;
245 			break;
246 		case 'c':
247 			shell_command = optarg;
248 			break;
249 		case 'C':
250 			if (flags & CLIENT_CONTROL)
251 				flags |= CLIENT_CONTROLCONTROL;
252 			else
253 				flags |= CLIENT_CONTROL;
254 			break;
255 		case 'f':
256 			set_cfg_file(optarg);
257 			break;
258 		case 'l':
259 			flags |= CLIENT_LOGIN;
260 			break;
261 		case 'L':
262 			free(label);
263 			label = xstrdup(optarg);
264 			break;
265 		case 'q':
266 			break;
267 		case 'S':
268 			free(path);
269 			path = xstrdup(optarg);
270 			break;
271 		case 'u':
272 			flags |= CLIENT_UTF8;
273 			break;
274 		case 'v':
275 			log_add_level();
276 			break;
277 		default:
278 			usage();
279 		}
280 	}
281 	argc -= optind;
282 	argv += optind;
283 
284 	if (shell_command != NULL && argc != 0)
285 		usage();
286 
287 	if ((ptm_fd = getptmfd()) == -1)
288 		err(1, "getptmfd");
289 	if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
290 	    "recvfd proc exec tty ps", NULL) != 0)
291 		err(1, "pledge");
292 
293 	/*
294 	 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
295 	 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
296 	 * UTF-8, it is a safe assumption that either they are using a UTF-8
297 	 * terminal, or if not they know that output from UTF-8-capable
298 	 * programs may be wrong.
299 	 */
300 	if (getenv("TMUX") != NULL)
301 		flags |= CLIENT_UTF8;
302 	else {
303 		s = getenv("LC_ALL");
304 		if (s == NULL || *s == '\0')
305 			s = getenv("LC_CTYPE");
306 		if (s == NULL || *s == '\0')
307 			s = getenv("LANG");
308 		if (s == NULL || *s == '\0')
309 			s = "";
310 		if (strcasestr(s, "UTF-8") != NULL ||
311 		    strcasestr(s, "UTF8") != NULL)
312 			flags |= CLIENT_UTF8;
313 	}
314 
315 	global_environ = environ_create();
316 	for (var = environ; *var != NULL; var++)
317 		environ_put(global_environ, *var);
318 	if ((cwd = find_cwd()) != NULL)
319 		environ_set(global_environ, "PWD", "%s", cwd);
320 
321 	global_options = options_create(NULL);
322 	global_s_options = options_create(NULL);
323 	global_w_options = options_create(NULL);
324 	for (oe = options_table; oe->name != NULL; oe++) {
325 		if (oe->scope & OPTIONS_TABLE_SERVER)
326 			options_default(global_options, oe);
327 		if (oe->scope & OPTIONS_TABLE_SESSION)
328 			options_default(global_s_options, oe);
329 		if (oe->scope & OPTIONS_TABLE_WINDOW)
330 			options_default(global_w_options, oe);
331 	}
332 
333 	/*
334 	 * The default shell comes from SHELL or from the user's passwd entry
335 	 * if available.
336 	 */
337 	shell = getshell();
338 	options_set_string(global_s_options, "default-shell", 0, "%s", shell);
339 
340 	/* Override keys to vi if VISUAL or EDITOR are set. */
341 	if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
342 		if (strrchr(s, '/') != NULL)
343 			s = strrchr(s, '/') + 1;
344 		if (strstr(s, "vi") != NULL)
345 			keys = MODEKEY_VI;
346 		else
347 			keys = MODEKEY_EMACS;
348 		options_set_number(global_s_options, "status-keys", keys);
349 		options_set_number(global_w_options, "mode-keys", keys);
350 	}
351 
352 	/*
353 	 * If socket is specified on the command-line with -S or -L, it is
354 	 * used. Otherwise, $TMUX is checked and if that fails "default" is
355 	 * used.
356 	 */
357 	if (path == NULL && label == NULL) {
358 		s = getenv("TMUX");
359 		if (s != NULL && *s != '\0' && *s != ',') {
360 			path = xstrdup(s);
361 			path[strcspn(path, ",")] = '\0';
362 		}
363 	}
364 	if (path == NULL && (path = make_label(label, &cause)) == NULL) {
365 		if (cause != NULL) {
366 			fprintf(stderr, "%s\n", cause);
367 			free(cause);
368 		}
369 		exit(1);
370 	}
371 	socket_path = path;
372 	free(label);
373 
374 	/* Pass control to the client. */
375 	exit(client_main(event_init(), argc, argv, flags));
376 }
377