]>
Commit | Line | Data |
---|---|---|
aacab402 | 1 | /* $OpenBSD: mux.c,v 1.3 2008/06/12 05:32:30 djm Exp $ */ |
3bcced4c | 2 | /* |
3 | * Copyright (c) 2002-2008 Damien Miller <djm@openbsd.org> | |
4 | * | |
5 | * Permission to use, copy, modify, and distribute this software for any | |
6 | * purpose with or without fee is hereby granted, provided that the above | |
7 | * copyright notice and this permission notice appear in all copies. | |
8 | * | |
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
16 | */ | |
17 | ||
18 | /* ssh session multiplexing support */ | |
19 | ||
b3ef88dc | 20 | #include "includes.h" |
21 | ||
f17f705b | 22 | /* |
23 | * TODO: | |
24 | * 1. partial reads in muxserver_accept_control (maybe make channels | |
25 | * from accepted connections) | |
26 | * 2. Better signalling from master to slave, especially passing of | |
27 | * error messages | |
28 | * 3. Better fall-back from mux slave error to new connection. | |
29 | * 3. Add/delete forwardings via slave | |
30 | * 4. ExitOnForwardingFailure (after #3 obviously) | |
31 | * 5. Maybe extension mechanisms for multi-X11/multi-agent forwarding | |
32 | * 6. Document the mux mini-protocol somewhere. | |
aacab402 | 33 | * 7. Support ~^Z in mux slaves. |
34 | * 8. Inspect or control sessions in master. | |
35 | * 9. If we ever support the "signal" channel request, send signals on | |
36 | * sessions in master. | |
f17f705b | 37 | */ |
38 | ||
3bcced4c | 39 | #include <sys/types.h> |
40 | #include <sys/param.h> | |
41 | #include <sys/stat.h> | |
42 | #include <sys/socket.h> | |
43 | #include <sys/un.h> | |
44 | ||
45 | #include <errno.h> | |
46 | #include <fcntl.h> | |
47 | #include <signal.h> | |
48 | #include <stdarg.h> | |
49 | #include <stddef.h> | |
50 | #include <stdlib.h> | |
51 | #include <stdio.h> | |
52 | #include <string.h> | |
53 | #include <unistd.h> | |
0894bbed | 54 | #ifdef HAVE_PATHS_H |
3bcced4c | 55 | #include <paths.h> |
0894bbed | 56 | #endif |
3bcced4c | 57 | |
b3ef88dc | 58 | #ifdef HAVE_UTIL_H |
59 | # include <util.h> | |
60 | #endif | |
61 | ||
62 | #ifdef HAVE_LIBUTIL_H | |
63 | # include <libutil.h> | |
64 | #endif | |
65 | ||
3bcced4c | 66 | #include "openbsd-compat/sys-queue.h" |
67 | #include "xmalloc.h" | |
68 | #include "log.h" | |
69 | #include "ssh.h" | |
70 | #include "pathnames.h" | |
71 | #include "misc.h" | |
72 | #include "match.h" | |
73 | #include "buffer.h" | |
74 | #include "channels.h" | |
75 | #include "msg.h" | |
76 | #include "packet.h" | |
77 | #include "monitor_fdpass.h" | |
78 | #include "sshpty.h" | |
79 | #include "key.h" | |
80 | #include "readconf.h" | |
81 | #include "clientloop.h" | |
82 | ||
83 | /* from ssh.c */ | |
84 | extern int tty_flag; | |
85 | extern Options options; | |
86 | extern int stdin_null_flag; | |
87 | extern char *host; | |
88 | int subsystem_flag; | |
89 | extern Buffer command; | |
90 | ||
f17f705b | 91 | /* Context for session open confirmation callback */ |
92 | struct mux_session_confirm_ctx { | |
93 | int want_tty; | |
94 | int want_subsys; | |
95 | int want_x_fwd; | |
96 | int want_agent_fwd; | |
97 | Buffer cmd; | |
98 | char *term; | |
99 | struct termios tio; | |
100 | char **env; | |
101 | }; | |
102 | ||
3bcced4c | 103 | /* fd to control socket */ |
104 | int muxserver_sock = -1; | |
105 | ||
106 | /* Multiplexing control command */ | |
107 | u_int muxclient_command = 0; | |
108 | ||
109 | /* Set when signalled. */ | |
110 | static volatile sig_atomic_t muxclient_terminate = 0; | |
111 | ||
112 | /* PID of multiplex server */ | |
113 | static u_int muxserver_pid = 0; | |
114 | ||
115 | ||
116 | /* ** Multiplexing master support */ | |
117 | ||
118 | /* Prepare a mux master to listen on a Unix domain socket. */ | |
119 | void | |
120 | muxserver_listen(void) | |
121 | { | |
122 | struct sockaddr_un addr; | |
123 | mode_t old_umask; | |
124 | int addr_len; | |
125 | ||
126 | if (options.control_path == NULL || | |
127 | options.control_master == SSHCTL_MASTER_NO) | |
128 | return; | |
129 | ||
130 | debug("setting up multiplex master socket"); | |
131 | ||
132 | memset(&addr, '\0', sizeof(addr)); | |
133 | addr.sun_family = AF_UNIX; | |
134 | addr_len = offsetof(struct sockaddr_un, sun_path) + | |
135 | strlen(options.control_path) + 1; | |
136 | ||
137 | if (strlcpy(addr.sun_path, options.control_path, | |
138 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) | |
139 | fatal("ControlPath too long"); | |
140 | ||
141 | if ((muxserver_sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) | |
142 | fatal("%s socket(): %s", __func__, strerror(errno)); | |
143 | ||
144 | old_umask = umask(0177); | |
145 | if (bind(muxserver_sock, (struct sockaddr *)&addr, addr_len) == -1) { | |
146 | muxserver_sock = -1; | |
147 | if (errno == EINVAL || errno == EADDRINUSE) | |
148 | fatal("ControlSocket %s already exists", | |
149 | options.control_path); | |
150 | else | |
151 | fatal("%s bind(): %s", __func__, strerror(errno)); | |
152 | } | |
153 | umask(old_umask); | |
154 | ||
155 | if (listen(muxserver_sock, 64) == -1) | |
156 | fatal("%s listen(): %s", __func__, strerror(errno)); | |
157 | ||
158 | set_nonblock(muxserver_sock); | |
159 | } | |
160 | ||
161 | /* Callback on open confirmation in mux master for a mux client session. */ | |
162 | static void | |
f17f705b | 163 | mux_session_confirm(int id, void *arg) |
3bcced4c | 164 | { |
165 | struct mux_session_confirm_ctx *cctx = arg; | |
166 | const char *display; | |
167 | Channel *c; | |
168 | int i; | |
169 | ||
170 | if (cctx == NULL) | |
171 | fatal("%s: cctx == NULL", __func__); | |
172 | if ((c = channel_lookup(id)) == NULL) | |
173 | fatal("%s: no channel for id %d", __func__, id); | |
174 | ||
175 | display = getenv("DISPLAY"); | |
176 | if (cctx->want_x_fwd && options.forward_x11 && display != NULL) { | |
177 | char *proto, *data; | |
178 | /* Get reasonable local authentication information. */ | |
179 | client_x11_get_proto(display, options.xauth_location, | |
180 | options.forward_x11_trusted, &proto, &data); | |
181 | /* Request forwarding with authentication spoofing. */ | |
182 | debug("Requesting X11 forwarding with authentication spoofing."); | |
183 | x11_request_forwarding_with_spoofing(id, display, proto, data); | |
184 | /* XXX wait for reply */ | |
185 | } | |
186 | ||
187 | if (cctx->want_agent_fwd && options.forward_agent) { | |
188 | debug("Requesting authentication agent forwarding."); | |
189 | channel_request_start(id, "auth-agent-req@openssh.com", 0); | |
190 | packet_send(); | |
191 | } | |
192 | ||
193 | client_session2_setup(id, cctx->want_tty, cctx->want_subsys, | |
194 | cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env); | |
195 | ||
196 | c->open_confirm_ctx = NULL; | |
197 | buffer_free(&cctx->cmd); | |
198 | xfree(cctx->term); | |
199 | if (cctx->env != NULL) { | |
200 | for (i = 0; cctx->env[i] != NULL; i++) | |
201 | xfree(cctx->env[i]); | |
202 | xfree(cctx->env); | |
203 | } | |
204 | xfree(cctx); | |
205 | } | |
206 | ||
207 | /* | |
208 | * Accept a connection on the mux master socket and process the | |
209 | * client's request. Returns flag indicating whether mux master should | |
210 | * begin graceful close. | |
211 | */ | |
212 | int | |
213 | muxserver_accept_control(void) | |
214 | { | |
215 | Buffer m; | |
216 | Channel *c; | |
217 | int client_fd, new_fd[3], ver, allowed, window, packetmax; | |
218 | socklen_t addrlen; | |
219 | struct sockaddr_storage addr; | |
220 | struct mux_session_confirm_ctx *cctx; | |
221 | char *cmd; | |
f17f705b | 222 | u_int i, j, len, env_len, mux_command, flags, escape_char; |
3bcced4c | 223 | uid_t euid; |
224 | gid_t egid; | |
225 | int start_close = 0; | |
226 | ||
227 | /* | |
228 | * Accept connection on control socket | |
229 | */ | |
230 | memset(&addr, 0, sizeof(addr)); | |
231 | addrlen = sizeof(addr); | |
232 | if ((client_fd = accept(muxserver_sock, | |
233 | (struct sockaddr*)&addr, &addrlen)) == -1) { | |
234 | error("%s accept: %s", __func__, strerror(errno)); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | if (getpeereid(client_fd, &euid, &egid) < 0) { | |
239 | error("%s getpeereid failed: %s", __func__, strerror(errno)); | |
240 | close(client_fd); | |
241 | return 0; | |
242 | } | |
243 | if ((euid != 0) && (getuid() != euid)) { | |
244 | error("control mode uid mismatch: peer euid %u != uid %u", | |
245 | (u_int) euid, (u_int) getuid()); | |
246 | close(client_fd); | |
247 | return 0; | |
248 | } | |
249 | ||
250 | /* XXX handle asynchronously */ | |
251 | unset_nonblock(client_fd); | |
252 | ||
253 | /* Read command */ | |
254 | buffer_init(&m); | |
255 | if (ssh_msg_recv(client_fd, &m) == -1) { | |
256 | error("%s: client msg_recv failed", __func__); | |
257 | close(client_fd); | |
258 | buffer_free(&m); | |
259 | return 0; | |
260 | } | |
261 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { | |
262 | error("%s: wrong client version %d", __func__, ver); | |
263 | buffer_free(&m); | |
264 | close(client_fd); | |
265 | return 0; | |
266 | } | |
267 | ||
268 | allowed = 1; | |
269 | mux_command = buffer_get_int(&m); | |
270 | flags = buffer_get_int(&m); | |
271 | ||
272 | buffer_clear(&m); | |
273 | ||
274 | switch (mux_command) { | |
275 | case SSHMUX_COMMAND_OPEN: | |
276 | if (options.control_master == SSHCTL_MASTER_ASK || | |
277 | options.control_master == SSHCTL_MASTER_AUTO_ASK) | |
278 | allowed = ask_permission("Allow shared connection " | |
279 | "to %s? ", host); | |
280 | /* continue below */ | |
281 | break; | |
282 | case SSHMUX_COMMAND_TERMINATE: | |
283 | if (options.control_master == SSHCTL_MASTER_ASK || | |
284 | options.control_master == SSHCTL_MASTER_AUTO_ASK) | |
285 | allowed = ask_permission("Terminate shared connection " | |
286 | "to %s? ", host); | |
287 | if (allowed) | |
288 | start_close = 1; | |
289 | /* FALLTHROUGH */ | |
290 | case SSHMUX_COMMAND_ALIVE_CHECK: | |
291 | /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */ | |
292 | buffer_clear(&m); | |
293 | buffer_put_int(&m, allowed); | |
294 | buffer_put_int(&m, getpid()); | |
295 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { | |
296 | error("%s: client msg_send failed", __func__); | |
297 | close(client_fd); | |
298 | buffer_free(&m); | |
299 | return start_close; | |
300 | } | |
301 | buffer_free(&m); | |
302 | close(client_fd); | |
303 | return start_close; | |
304 | default: | |
305 | error("Unsupported command %d", mux_command); | |
306 | buffer_free(&m); | |
307 | close(client_fd); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | /* Reply for SSHMUX_COMMAND_OPEN */ | |
312 | buffer_clear(&m); | |
313 | buffer_put_int(&m, allowed); | |
314 | buffer_put_int(&m, getpid()); | |
315 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { | |
316 | error("%s: client msg_send failed", __func__); | |
317 | close(client_fd); | |
318 | buffer_free(&m); | |
319 | return 0; | |
320 | } | |
321 | ||
322 | if (!allowed) { | |
323 | error("Refused control connection"); | |
324 | close(client_fd); | |
325 | buffer_free(&m); | |
326 | return 0; | |
327 | } | |
328 | ||
329 | buffer_clear(&m); | |
330 | if (ssh_msg_recv(client_fd, &m) == -1) { | |
331 | error("%s: client msg_recv failed", __func__); | |
332 | close(client_fd); | |
333 | buffer_free(&m); | |
334 | return 0; | |
335 | } | |
336 | if ((ver = buffer_get_char(&m)) != SSHMUX_VER) { | |
337 | error("%s: wrong client version %d", __func__, ver); | |
338 | buffer_free(&m); | |
339 | close(client_fd); | |
340 | return 0; | |
341 | } | |
342 | ||
343 | cctx = xcalloc(1, sizeof(*cctx)); | |
344 | cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0; | |
345 | cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0; | |
346 | cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0; | |
347 | cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0; | |
348 | cctx->term = buffer_get_string(&m, &len); | |
f17f705b | 349 | escape_char = buffer_get_int(&m); |
3bcced4c | 350 | |
351 | cmd = buffer_get_string(&m, &len); | |
352 | buffer_init(&cctx->cmd); | |
353 | buffer_append(&cctx->cmd, cmd, strlen(cmd)); | |
354 | ||
355 | env_len = buffer_get_int(&m); | |
356 | env_len = MIN(env_len, 4096); | |
357 | debug3("%s: receiving %d env vars", __func__, env_len); | |
358 | if (env_len != 0) { | |
359 | cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env)); | |
360 | for (i = 0; i < env_len; i++) | |
361 | cctx->env[i] = buffer_get_string(&m, &len); | |
362 | cctx->env[i] = NULL; | |
363 | } | |
364 | ||
365 | debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__, | |
366 | cctx->want_tty, cctx->want_subsys, cmd); | |
367 | xfree(cmd); | |
368 | ||
369 | /* Gather fds from client */ | |
370 | for(i = 0; i < 3; i++) { | |
371 | if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) { | |
372 | error("%s: failed to receive fd %d from slave", | |
373 | __func__, i); | |
374 | for (j = 0; j < i; j++) | |
375 | close(new_fd[j]); | |
376 | for (j = 0; j < env_len; j++) | |
377 | xfree(cctx->env[j]); | |
378 | if (env_len > 0) | |
379 | xfree(cctx->env); | |
380 | xfree(cctx->term); | |
381 | buffer_free(&cctx->cmd); | |
382 | close(client_fd); | |
383 | xfree(cctx); | |
384 | return 0; | |
385 | } | |
386 | } | |
387 | ||
388 | debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__, | |
389 | new_fd[0], new_fd[1], new_fd[2]); | |
390 | ||
391 | /* Try to pick up ttymodes from client before it goes raw */ | |
392 | if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1) | |
393 | error("%s: tcgetattr: %s", __func__, strerror(errno)); | |
394 | ||
395 | /* This roundtrip is just for synchronisation of ttymodes */ | |
396 | buffer_clear(&m); | |
397 | if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) { | |
398 | error("%s: client msg_send failed", __func__); | |
399 | close(client_fd); | |
400 | close(new_fd[0]); | |
401 | close(new_fd[1]); | |
402 | close(new_fd[2]); | |
403 | buffer_free(&m); | |
404 | xfree(cctx->term); | |
405 | if (env_len != 0) { | |
406 | for (i = 0; i < env_len; i++) | |
407 | xfree(cctx->env[i]); | |
408 | xfree(cctx->env); | |
409 | } | |
410 | return 0; | |
411 | } | |
412 | buffer_free(&m); | |
413 | ||
414 | /* enable nonblocking unless tty */ | |
415 | if (!isatty(new_fd[0])) | |
416 | set_nonblock(new_fd[0]); | |
417 | if (!isatty(new_fd[1])) | |
418 | set_nonblock(new_fd[1]); | |
419 | if (!isatty(new_fd[2])) | |
420 | set_nonblock(new_fd[2]); | |
421 | ||
422 | set_nonblock(client_fd); | |
423 | ||
424 | window = CHAN_SES_WINDOW_DEFAULT; | |
425 | packetmax = CHAN_SES_PACKET_DEFAULT; | |
426 | if (cctx->want_tty) { | |
427 | window >>= 1; | |
428 | packetmax >>= 1; | |
429 | } | |
430 | ||
431 | c = channel_new("session", SSH_CHANNEL_OPENING, | |
432 | new_fd[0], new_fd[1], new_fd[2], window, packetmax, | |
433 | CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0); | |
434 | ||
3bcced4c | 435 | c->ctl_fd = client_fd; |
f17f705b | 436 | if (cctx->want_tty && escape_char != 0xffffffff) { |
437 | channel_register_filter(c->self, | |
438 | client_simple_escape_filter, NULL, | |
439 | client_new_escape_filter_ctx((int)escape_char)); | |
440 | } | |
3bcced4c | 441 | |
442 | debug3("%s: channel_new: %d", __func__, c->self); | |
443 | ||
444 | channel_send_open(c->self); | |
f17f705b | 445 | channel_register_open_confirm(c->self, mux_session_confirm, cctx); |
3bcced4c | 446 | return 0; |
447 | } | |
448 | ||
449 | /* ** Multiplexing client support */ | |
450 | ||
451 | /* Exit signal handler */ | |
452 | static void | |
453 | control_client_sighandler(int signo) | |
454 | { | |
455 | muxclient_terminate = signo; | |
456 | } | |
457 | ||
458 | /* | |
459 | * Relay signal handler - used to pass some signals from mux client to | |
460 | * mux master. | |
461 | */ | |
462 | static void | |
463 | control_client_sigrelay(int signo) | |
464 | { | |
465 | int save_errno = errno; | |
466 | ||
467 | if (muxserver_pid > 1) | |
468 | kill(muxserver_pid, signo); | |
469 | ||
470 | errno = save_errno; | |
471 | } | |
472 | ||
473 | /* Check mux client environment variables before passing them to mux master. */ | |
474 | static int | |
475 | env_permitted(char *env) | |
476 | { | |
477 | int i, ret; | |
478 | char name[1024], *cp; | |
479 | ||
480 | if ((cp = strchr(env, '=')) == NULL || cp == env) | |
481 | return (0); | |
482 | ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env); | |
483 | if (ret <= 0 || (size_t)ret >= sizeof(name)) | |
484 | fatal("env_permitted: name '%.100s...' too long", env); | |
485 | ||
486 | for (i = 0; i < options.num_send_env; i++) | |
487 | if (match_pattern(name, options.send_env[i])) | |
488 | return (1); | |
489 | ||
490 | return (0); | |
491 | } | |
492 | ||
493 | /* Multiplex client main loop. */ | |
494 | void | |
495 | muxclient(const char *path) | |
496 | { | |
497 | struct sockaddr_un addr; | |
498 | int i, r, fd, sock, exitval[2], num_env, addr_len; | |
499 | Buffer m; | |
500 | char *term; | |
501 | extern char **environ; | |
502 | u_int flags; | |
503 | ||
504 | if (muxclient_command == 0) | |
505 | muxclient_command = SSHMUX_COMMAND_OPEN; | |
506 | ||
507 | switch (options.control_master) { | |
508 | case SSHCTL_MASTER_AUTO: | |
509 | case SSHCTL_MASTER_AUTO_ASK: | |
510 | debug("auto-mux: Trying existing master"); | |
511 | /* FALLTHROUGH */ | |
512 | case SSHCTL_MASTER_NO: | |
513 | break; | |
514 | default: | |
515 | return; | |
516 | } | |
517 | ||
518 | memset(&addr, '\0', sizeof(addr)); | |
519 | addr.sun_family = AF_UNIX; | |
520 | addr_len = offsetof(struct sockaddr_un, sun_path) + | |
521 | strlen(path) + 1; | |
522 | ||
523 | if (strlcpy(addr.sun_path, path, | |
524 | sizeof(addr.sun_path)) >= sizeof(addr.sun_path)) | |
525 | fatal("ControlPath too long"); | |
526 | ||
527 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) | |
528 | fatal("%s socket(): %s", __func__, strerror(errno)); | |
529 | ||
530 | if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) { | |
531 | if (muxclient_command != SSHMUX_COMMAND_OPEN) { | |
532 | fatal("Control socket connect(%.100s): %s", path, | |
533 | strerror(errno)); | |
534 | } | |
535 | if (errno == ENOENT) | |
536 | debug("Control socket \"%.100s\" does not exist", path); | |
537 | else { | |
538 | error("Control socket connect(%.100s): %s", path, | |
539 | strerror(errno)); | |
540 | } | |
541 | close(sock); | |
542 | return; | |
543 | } | |
544 | ||
545 | if (stdin_null_flag) { | |
546 | if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1) | |
547 | fatal("open(/dev/null): %s", strerror(errno)); | |
548 | if (dup2(fd, STDIN_FILENO) == -1) | |
549 | fatal("dup2: %s", strerror(errno)); | |
550 | if (fd > STDERR_FILENO) | |
551 | close(fd); | |
552 | } | |
553 | ||
554 | term = getenv("TERM"); | |
555 | ||
556 | flags = 0; | |
557 | if (tty_flag) | |
558 | flags |= SSHMUX_FLAG_TTY; | |
559 | if (subsystem_flag) | |
560 | flags |= SSHMUX_FLAG_SUBSYS; | |
561 | if (options.forward_x11) | |
562 | flags |= SSHMUX_FLAG_X11_FWD; | |
563 | if (options.forward_agent) | |
564 | flags |= SSHMUX_FLAG_AGENT_FWD; | |
565 | ||
566 | signal(SIGPIPE, SIG_IGN); | |
567 | ||
568 | buffer_init(&m); | |
569 | ||
570 | /* Send our command to server */ | |
571 | buffer_put_int(&m, muxclient_command); | |
572 | buffer_put_int(&m, flags); | |
573 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) | |
574 | fatal("%s: msg_send", __func__); | |
575 | buffer_clear(&m); | |
576 | ||
577 | /* Get authorisation status and PID of controlee */ | |
578 | if (ssh_msg_recv(sock, &m) == -1) | |
579 | fatal("%s: msg_recv", __func__); | |
580 | if (buffer_get_char(&m) != SSHMUX_VER) | |
581 | fatal("%s: wrong version", __func__); | |
582 | if (buffer_get_int(&m) != 1) | |
583 | fatal("Connection to master denied"); | |
584 | muxserver_pid = buffer_get_int(&m); | |
585 | ||
586 | buffer_clear(&m); | |
587 | ||
588 | switch (muxclient_command) { | |
589 | case SSHMUX_COMMAND_ALIVE_CHECK: | |
590 | fprintf(stderr, "Master running (pid=%d)\r\n", | |
591 | muxserver_pid); | |
592 | exit(0); | |
593 | case SSHMUX_COMMAND_TERMINATE: | |
594 | fprintf(stderr, "Exit request sent.\r\n"); | |
595 | exit(0); | |
596 | case SSHMUX_COMMAND_OPEN: | |
f17f705b | 597 | buffer_put_cstring(&m, term ? term : ""); |
598 | if (options.escape_char == SSH_ESCAPECHAR_NONE) | |
599 | buffer_put_int(&m, 0xffffffff); | |
600 | else | |
601 | buffer_put_int(&m, options.escape_char); | |
602 | buffer_append(&command, "\0", 1); | |
603 | buffer_put_cstring(&m, buffer_ptr(&command)); | |
604 | ||
605 | if (options.num_send_env == 0 || environ == NULL) { | |
606 | buffer_put_int(&m, 0); | |
607 | } else { | |
608 | /* Pass environment */ | |
609 | num_env = 0; | |
610 | for (i = 0; environ[i] != NULL; i++) { | |
611 | if (env_permitted(environ[i])) | |
612 | num_env++; /* Count */ | |
613 | } | |
614 | buffer_put_int(&m, num_env); | |
615 | for (i = 0; environ[i] != NULL && num_env >= 0; i++) { | |
616 | if (env_permitted(environ[i])) { | |
617 | num_env--; | |
618 | buffer_put_cstring(&m, environ[i]); | |
619 | } | |
620 | } | |
621 | } | |
3bcced4c | 622 | break; |
623 | default: | |
f17f705b | 624 | fatal("unrecognised muxclient_command %d", muxclient_command); |
3bcced4c | 625 | } |
626 | ||
627 | if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) | |
628 | fatal("%s: msg_send", __func__); | |
629 | ||
630 | if (mm_send_fd(sock, STDIN_FILENO) == -1 || | |
631 | mm_send_fd(sock, STDOUT_FILENO) == -1 || | |
632 | mm_send_fd(sock, STDERR_FILENO) == -1) | |
633 | fatal("%s: send fds failed", __func__); | |
634 | ||
635 | /* Wait for reply, so master has a chance to gather ttymodes */ | |
636 | buffer_clear(&m); | |
637 | if (ssh_msg_recv(sock, &m) == -1) | |
638 | fatal("%s: msg_recv", __func__); | |
639 | if (buffer_get_char(&m) != SSHMUX_VER) | |
640 | fatal("%s: wrong version", __func__); | |
641 | buffer_free(&m); | |
642 | ||
643 | signal(SIGHUP, control_client_sighandler); | |
644 | signal(SIGINT, control_client_sighandler); | |
645 | signal(SIGTERM, control_client_sighandler); | |
646 | signal(SIGWINCH, control_client_sigrelay); | |
647 | ||
648 | if (tty_flag) | |
649 | enter_raw_mode(); | |
650 | ||
651 | /* | |
652 | * Stick around until the controlee closes the client_fd. | |
653 | * Before it does, it is expected to write this process' exit | |
654 | * value (one int). This process must read the value and wait for | |
655 | * the closure of the client_fd; if this one closes early, the | |
656 | * multiplex master will terminate early too (possibly losing data). | |
657 | */ | |
658 | exitval[0] = 0; | |
659 | for (i = 0; !muxclient_terminate && i < (int)sizeof(exitval);) { | |
660 | r = read(sock, (char *)exitval + i, sizeof(exitval) - i); | |
661 | if (r == 0) { | |
662 | debug2("Received EOF from master"); | |
663 | break; | |
664 | } | |
665 | if (r == -1) { | |
666 | if (errno == EINTR) | |
667 | continue; | |
668 | fatal("%s: read %s", __func__, strerror(errno)); | |
669 | } | |
670 | i += r; | |
671 | } | |
672 | ||
673 | close(sock); | |
674 | leave_raw_mode(); | |
675 | if (i > (int)sizeof(int)) | |
676 | fatal("%s: master returned too much data (%d > %lu)", | |
677 | __func__, i, sizeof(int)); | |
678 | if (muxclient_terminate) { | |
679 | debug2("Exiting on signal %d", muxclient_terminate); | |
680 | exitval[0] = 255; | |
681 | } else if (i < (int)sizeof(int)) { | |
682 | debug2("Control master terminated unexpectedly"); | |
683 | exitval[0] = 255; | |
684 | } else | |
685 | debug2("Received exit status from master %d", exitval[0]); | |
686 | ||
687 | if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET) | |
688 | fprintf(stderr, "Shared connection to %s closed.\r\n", host); | |
689 | ||
690 | exit(exitval[0]); | |
691 | } |