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