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