xref: /openbsd-src/usr.bin/tmux/tmux.c (revision 2bb2dba4efae314e63424c7f5ff1c8ee57c4c995)
1 /* $OpenBSD: tmux.c,v 1.173 2016/12/09 21:39:27 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 
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 struct hooks	*global_hooks;
43 
44 struct timeval	 start_time;
45 const char	*socket_path;
46 
47 static __dead void	 usage(void);
48 static char		*make_label(const char *);
49 
50 static const char	*getshell(void);
51 static int		 checkshell(const char *);
52 
53 static __dead void
54 usage(void)
55 {
56 	fprintf(stderr,
57 	    "usage: %s [-2Cluv] [-c shell-command] [-f file] [-L socket-name]\n"
58 	    "            [-S socket-path] [command [flags]]\n",
59 	    getprogname());
60 	exit(1);
61 }
62 
63 static const char *
64 getshell(void)
65 {
66 	struct passwd	*pw;
67 	const char	*shell;
68 
69 	shell = getenv("SHELL");
70 	if (checkshell(shell))
71 		return (shell);
72 
73 	pw = getpwuid(getuid());
74 	if (pw != NULL && checkshell(pw->pw_shell))
75 		return (pw->pw_shell);
76 
77 	return (_PATH_BSHELL);
78 }
79 
80 static int
81 checkshell(const char *shell)
82 {
83 	if (shell == NULL || *shell == '\0' || *shell != '/')
84 		return (0);
85 	if (areshell(shell))
86 		return (0);
87 	if (access(shell, X_OK) != 0)
88 		return (0);
89 	return (1);
90 }
91 
92 int
93 areshell(const char *shell)
94 {
95 	const char	*progname, *ptr;
96 
97 	if ((ptr = strrchr(shell, '/')) != NULL)
98 		ptr++;
99 	else
100 		ptr = shell;
101 	progname = getprogname();
102 	if (*progname == '-')
103 		progname++;
104 	if (strcmp(ptr, progname) == 0)
105 		return (1);
106 	return (0);
107 }
108 
109 static char *
110 make_label(const char *label)
111 {
112 	char		*base, resolved[PATH_MAX], *path, *s;
113 	struct stat	 sb;
114 	u_int		 uid;
115 	int		 saved_errno;
116 
117 	if (label == NULL)
118 		label = "default";
119 
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-%u", _PATH_TMP, 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 
146 fail:
147 	saved_errno = errno;
148 	free(base);
149 	errno = saved_errno;
150 	return (NULL);
151 }
152 
153 void
154 setblocking(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 
167 const char *
168 find_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 
188 int
189 main(int argc, char **argv)
190 {
191 	char		*path, *label, **var, tmp[PATH_MAX], *shellcmd = NULL;
192 	const char	*s;
193 	int		 opt, flags, keys;
194 
195 	if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL) {
196 		if (setlocale(LC_CTYPE, "") == NULL)
197 			errx(1, "invalid LC_ALL, LC_CTYPE or LANG");
198 		s = nl_langinfo(CODESET);
199 		if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0)
200 			errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
201 	}
202 
203 	setlocale(LC_TIME, "");
204 	tzset();
205 
206 	if (**argv == '-')
207 		flags = CLIENT_LOGIN;
208 	else
209 		flags = 0;
210 
211 	label = path = NULL;
212 	while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUv")) != -1) {
213 		switch (opt) {
214 		case '2':
215 			flags |= CLIENT_256COLOURS;
216 			break;
217 		case 'c':
218 			free(shellcmd);
219 			shellcmd = xstrdup(optarg);
220 			break;
221 		case 'C':
222 			if (flags & CLIENT_CONTROL)
223 				flags |= CLIENT_CONTROLCONTROL;
224 			else
225 				flags |= CLIENT_CONTROL;
226 			break;
227 		case 'f':
228 			set_cfg_file(optarg);
229 			break;
230 		case 'l':
231 			flags |= CLIENT_LOGIN;
232 			break;
233 		case 'L':
234 			free(label);
235 			label = xstrdup(optarg);
236 			break;
237 		case 'q':
238 			break;
239 		case 'S':
240 			free(path);
241 			path = xstrdup(optarg);
242 			break;
243 		case 'u':
244 			flags |= CLIENT_UTF8;
245 			break;
246 		case 'v':
247 			log_add_level();
248 			break;
249 		default:
250 			usage();
251 		}
252 	}
253 	argc -= optind;
254 	argv += optind;
255 
256 	if (shellcmd != NULL && argc != 0)
257 		usage();
258 
259 	if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
260 	    "recvfd proc exec tty ps", NULL) != 0)
261 		err(1, "pledge");
262 
263 	/*
264 	 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
265 	 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
266 	 * UTF-8, it is a safe assumption that either they are using a UTF-8
267 	 * terminal, or if not they know that output from UTF-8-capable
268 	 * programs may be wrong.
269 	 */
270 	if (getenv("TMUX") != NULL)
271 		flags |= CLIENT_UTF8;
272 	else {
273 		s = getenv("LC_ALL");
274 		if (s == NULL || *s == '\0')
275 			s = getenv("LC_CTYPE");
276 		if (s == NULL || *s == '\0')
277 			s = getenv("LANG");
278 		if (s == NULL || *s == '\0')
279 			s = "";
280 		if (strcasestr(s, "UTF-8") != NULL ||
281 		    strcasestr(s, "UTF8") != NULL)
282 			flags |= CLIENT_UTF8;
283 	}
284 
285 	global_hooks = hooks_create(NULL);
286 
287 	global_environ = environ_create();
288 	for (var = environ; *var != NULL; var++)
289 		environ_put(global_environ, *var);
290 	if (getcwd(tmp, sizeof tmp) != NULL)
291 		environ_set(global_environ, "PWD", "%s", tmp);
292 
293 	global_options = options_create(NULL);
294 	options_table_populate_tree(OPTIONS_TABLE_SERVER, global_options);
295 
296 	global_s_options = options_create(NULL);
297 	options_table_populate_tree(OPTIONS_TABLE_SESSION, global_s_options);
298 	options_set_string(global_s_options, "default-shell", "%s", getshell());
299 
300 	global_w_options = options_create(NULL);
301 	options_table_populate_tree(OPTIONS_TABLE_WINDOW, global_w_options);
302 
303 	/* Override keys to vi if VISUAL or EDITOR are set. */
304 	if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
305 		if (strrchr(s, '/') != NULL)
306 			s = strrchr(s, '/') + 1;
307 		if (strstr(s, "vi") != NULL)
308 			keys = MODEKEY_VI;
309 		else
310 			keys = MODEKEY_EMACS;
311 		options_set_number(global_s_options, "status-keys", keys);
312 		options_set_number(global_w_options, "mode-keys", keys);
313 	}
314 
315 	/*
316 	 * If socket is specified on the command-line with -S or -L, it is
317 	 * used. Otherwise, $TMUX is checked and if that fails "default" is
318 	 * used.
319 	 */
320 	if (path == NULL && label == NULL) {
321 		s = getenv("TMUX");
322 		if (s != NULL && *s != '\0' && *s != ',') {
323 			path = xstrdup(s);
324 			path[strcspn(path, ",")] = '\0';
325 		}
326 	}
327 	if (path == NULL && (path = make_label(label)) == NULL) {
328 		fprintf(stderr, "can't create socket: %s\n", strerror(errno));
329 		exit(1);
330 	}
331 	socket_path = path;
332 	free(label);
333 
334 	/* Pass control to the client. */
335 	exit(client_main(event_init(), argc, argv, flags, shellcmd));
336 }
337