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