]> andersk Git - gssapi-openssh.git/blob - openssh/mux.c
The man2html from jbasney on pkilab2 works whereas the standard one doesn't.
[gssapi-openssh.git] / openssh / mux.c
1 /* $OpenBSD: mux.c,v 1.7 2008/06/13 17:21:20 dtucker Exp $ */
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
20 #include "includes.h"
21
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.
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.
37  */
38
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>
54 #ifdef HAVE_PATHS_H
55 #include <paths.h>
56 #endif
57
58 #ifdef HAVE_UTIL_H
59 # include <util.h>
60 #endif
61
62 #ifdef HAVE_LIBUTIL_H
63 # include <libutil.h>
64 #endif
65
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 extern int subsystem_flag;
89 extern Buffer command;
90
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
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                         error("ControlSocket %s already exists, "
149                             "disabling multiplexing", options.control_path);
150                         close(muxserver_sock);
151                         muxserver_sock = -1;
152                         xfree(options.control_path);
153                         options.control_path = NULL;
154                         options.control_master = SSHCTL_MASTER_NO;
155                         return;
156                 } else
157                         fatal("%s bind(): %s", __func__, strerror(errno));
158         }
159         umask(old_umask);
160
161         if (listen(muxserver_sock, 64) == -1)
162                 fatal("%s listen(): %s", __func__, strerror(errno));
163
164         set_nonblock(muxserver_sock);
165 }
166
167 /* Callback on open confirmation in mux master for a mux client session. */
168 static void
169 mux_session_confirm(int id, void *arg)
170 {
171         struct mux_session_confirm_ctx *cctx = arg;
172         const char *display;
173         Channel *c;
174         int i;
175
176         if (cctx == NULL)
177                 fatal("%s: cctx == NULL", __func__);
178         if ((c = channel_lookup(id)) == NULL)
179                 fatal("%s: no channel for id %d", __func__, id);
180
181         display = getenv("DISPLAY");
182         if (cctx->want_x_fwd && options.forward_x11 && display != NULL) {
183                 char *proto, *data;
184                 /* Get reasonable local authentication information. */
185                 client_x11_get_proto(display, options.xauth_location,
186                     options.forward_x11_trusted, &proto, &data);
187                 /* Request forwarding with authentication spoofing. */
188                 debug("Requesting X11 forwarding with authentication spoofing.");
189                 x11_request_forwarding_with_spoofing(id, display, proto, data);
190                 /* XXX wait for reply */
191         }
192
193         if (cctx->want_agent_fwd && options.forward_agent) {
194                 debug("Requesting authentication agent forwarding.");
195                 channel_request_start(id, "auth-agent-req@openssh.com", 0);
196                 packet_send();
197         }
198
199         client_session2_setup(id, cctx->want_tty, cctx->want_subsys,
200             cctx->term, &cctx->tio, c->rfd, &cctx->cmd, cctx->env);
201
202         c->open_confirm_ctx = NULL;
203         buffer_free(&cctx->cmd);
204         xfree(cctx->term);
205         if (cctx->env != NULL) {
206                 for (i = 0; cctx->env[i] != NULL; i++)
207                         xfree(cctx->env[i]);
208                 xfree(cctx->env);
209         }
210         xfree(cctx);
211 }
212
213 /*
214  * Accept a connection on the mux master socket and process the
215  * client's request. Returns flag indicating whether mux master should
216  * begin graceful close.
217  */
218 int
219 muxserver_accept_control(void)
220 {
221         Buffer m;
222         Channel *c;
223         int client_fd, new_fd[3], ver, allowed, window, packetmax;
224         socklen_t addrlen;
225         struct sockaddr_storage addr;
226         struct mux_session_confirm_ctx *cctx;
227         char *cmd;
228         u_int i, j, len, env_len, mux_command, flags, escape_char;
229         uid_t euid;
230         gid_t egid;
231         int start_close = 0;
232
233         /*
234          * Accept connection on control socket
235          */
236         memset(&addr, 0, sizeof(addr));
237         addrlen = sizeof(addr);
238         if ((client_fd = accept(muxserver_sock,
239             (struct sockaddr*)&addr, &addrlen)) == -1) {
240                 error("%s accept: %s", __func__, strerror(errno));
241                 return 0;
242         }
243
244         if (getpeereid(client_fd, &euid, &egid) < 0) {
245                 error("%s getpeereid failed: %s", __func__, strerror(errno));
246                 close(client_fd);
247                 return 0;
248         }
249         if ((euid != 0) && (getuid() != euid)) {
250                 error("control mode uid mismatch: peer euid %u != uid %u",
251                     (u_int) euid, (u_int) getuid());
252                 close(client_fd);
253                 return 0;
254         }
255
256         /* XXX handle asynchronously */
257         unset_nonblock(client_fd);
258
259         /* Read command */
260         buffer_init(&m);
261         if (ssh_msg_recv(client_fd, &m) == -1) {
262                 error("%s: client msg_recv failed", __func__);
263                 close(client_fd);
264                 buffer_free(&m);
265                 return 0;
266         }
267         if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
268                 error("%s: wrong client version %d", __func__, ver);
269                 buffer_free(&m);
270                 close(client_fd);
271                 return 0;
272         }
273
274         allowed = 1;
275         mux_command = buffer_get_int(&m);
276         flags = buffer_get_int(&m);
277
278         buffer_clear(&m);
279
280         switch (mux_command) {
281         case SSHMUX_COMMAND_OPEN:
282                 if (options.control_master == SSHCTL_MASTER_ASK ||
283                     options.control_master == SSHCTL_MASTER_AUTO_ASK)
284                         allowed = ask_permission("Allow shared connection "
285                             "to %s? ", host);
286                 /* continue below */
287                 break;
288         case SSHMUX_COMMAND_TERMINATE:
289                 if (options.control_master == SSHCTL_MASTER_ASK ||
290                     options.control_master == SSHCTL_MASTER_AUTO_ASK)
291                         allowed = ask_permission("Terminate shared connection "
292                             "to %s? ", host);
293                 if (allowed)
294                         start_close = 1;
295                 /* FALLTHROUGH */
296         case SSHMUX_COMMAND_ALIVE_CHECK:
297                 /* Reply for SSHMUX_COMMAND_TERMINATE and ALIVE_CHECK */
298                 buffer_clear(&m);
299                 buffer_put_int(&m, allowed);
300                 buffer_put_int(&m, getpid());
301                 if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
302                         error("%s: client msg_send failed", __func__);
303                         close(client_fd);
304                         buffer_free(&m);
305                         return start_close;
306                 }
307                 buffer_free(&m);
308                 close(client_fd);
309                 return start_close;
310         default:
311                 error("Unsupported command %d", mux_command);
312                 buffer_free(&m);
313                 close(client_fd);
314                 return 0;
315         }
316
317         /* Reply for SSHMUX_COMMAND_OPEN */
318         buffer_clear(&m);
319         buffer_put_int(&m, allowed);
320         buffer_put_int(&m, getpid());
321         if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
322                 error("%s: client msg_send failed", __func__);
323                 close(client_fd);
324                 buffer_free(&m);
325                 return 0;
326         }
327
328         if (!allowed) {
329                 error("Refused control connection");
330                 close(client_fd);
331                 buffer_free(&m);
332                 return 0;
333         }
334
335         buffer_clear(&m);
336         if (ssh_msg_recv(client_fd, &m) == -1) {
337                 error("%s: client msg_recv failed", __func__);
338                 close(client_fd);
339                 buffer_free(&m);
340                 return 0;
341         }
342         if ((ver = buffer_get_char(&m)) != SSHMUX_VER) {
343                 error("%s: wrong client version %d", __func__, ver);
344                 buffer_free(&m);
345                 close(client_fd);
346                 return 0;
347         }
348
349         cctx = xcalloc(1, sizeof(*cctx));
350         cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
351         cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
352         cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
353         cctx->want_agent_fwd = (flags & SSHMUX_FLAG_AGENT_FWD) != 0;
354         cctx->term = buffer_get_string(&m, &len);
355         escape_char = buffer_get_int(&m);
356
357         cmd = buffer_get_string(&m, &len);
358         buffer_init(&cctx->cmd);
359         buffer_append(&cctx->cmd, cmd, strlen(cmd));
360
361         env_len = buffer_get_int(&m);
362         env_len = MIN(env_len, 4096);
363         debug3("%s: receiving %d env vars", __func__, env_len);
364         if (env_len != 0) {
365                 cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
366                 for (i = 0; i < env_len; i++)
367                         cctx->env[i] = buffer_get_string(&m, &len);
368                 cctx->env[i] = NULL;
369         }
370
371         debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
372             cctx->want_tty, cctx->want_subsys, cmd);
373         xfree(cmd);
374
375         /* Gather fds from client */
376         for(i = 0; i < 3; i++) {
377                 if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) {
378                         error("%s: failed to receive fd %d from slave",
379                             __func__, i);
380                         for (j = 0; j < i; j++)
381                                 close(new_fd[j]);
382                         for (j = 0; j < env_len; j++)
383                                 xfree(cctx->env[j]);
384                         if (env_len > 0)
385                                 xfree(cctx->env);
386                         xfree(cctx->term);
387                         buffer_free(&cctx->cmd);
388                         close(client_fd);
389                         xfree(cctx);
390                         return 0;
391                 }
392         }
393
394         debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
395             new_fd[0], new_fd[1], new_fd[2]);
396
397         /* Try to pick up ttymodes from client before it goes raw */
398         if (cctx->want_tty && tcgetattr(new_fd[0], &cctx->tio) == -1)
399                 error("%s: tcgetattr: %s", __func__, strerror(errno));
400
401         /* This roundtrip is just for synchronisation of ttymodes */
402         buffer_clear(&m);
403         if (ssh_msg_send(client_fd, SSHMUX_VER, &m) == -1) {
404                 error("%s: client msg_send failed", __func__);
405                 close(client_fd);
406                 close(new_fd[0]);
407                 close(new_fd[1]);
408                 close(new_fd[2]);
409                 buffer_free(&m);
410                 xfree(cctx->term);
411                 if (env_len != 0) {
412                         for (i = 0; i < env_len; i++)
413                                 xfree(cctx->env[i]);
414                         xfree(cctx->env);
415                 }
416                 return 0;
417         }
418         buffer_free(&m);
419
420         /* enable nonblocking unless tty */
421         if (!isatty(new_fd[0]))
422                 set_nonblock(new_fd[0]);
423         if (!isatty(new_fd[1]))
424                 set_nonblock(new_fd[1]);
425         if (!isatty(new_fd[2]))
426                 set_nonblock(new_fd[2]);
427
428         set_nonblock(client_fd);
429
430         if (options.hpn_disabled) 
431           window = CHAN_SES_WINDOW_DEFAULT;
432         else
433           window = options.hpn_buffer_size;
434         packetmax = CHAN_SES_PACKET_DEFAULT;
435         if (cctx->want_tty) {
436                 window >>= 1;
437                 packetmax >>= 1;
438         }
439         
440         c = channel_new("session", SSH_CHANNEL_OPENING,
441             new_fd[0], new_fd[1], new_fd[2], window, packetmax,
442             CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
443
444         c->ctl_fd = client_fd;
445         if (cctx->want_tty && escape_char != 0xffffffff) {
446                 channel_register_filter(c->self,
447                     client_simple_escape_filter, NULL,
448                     client_filter_cleanup,
449                     client_new_escape_filter_ctx((int)escape_char));
450         }
451
452         debug3("%s: channel_new: %d", __func__, c->self);
453
454         channel_send_open(c->self);
455         channel_register_open_confirm(c->self, mux_session_confirm, cctx);
456         return 0;
457 }
458
459 /* ** Multiplexing client support */
460
461 /* Exit signal handler */
462 static void
463 control_client_sighandler(int signo)
464 {
465         muxclient_terminate = signo;
466 }
467
468 /*
469  * Relay signal handler - used to pass some signals from mux client to
470  * mux master.
471  */
472 static void
473 control_client_sigrelay(int signo)
474 {
475         int save_errno = errno;
476
477         if (muxserver_pid > 1)
478                 kill(muxserver_pid, signo);
479
480         errno = save_errno;
481 }
482
483 /* Check mux client environment variables before passing them to mux master. */
484 static int
485 env_permitted(char *env)
486 {
487         int i, ret;
488         char name[1024], *cp;
489
490         if ((cp = strchr(env, '=')) == NULL || cp == env)
491                 return (0);
492         ret = snprintf(name, sizeof(name), "%.*s", (int)(cp - env), env);
493         if (ret <= 0 || (size_t)ret >= sizeof(name))
494                 fatal("env_permitted: name '%.100s...' too long", env);
495
496         for (i = 0; i < options.num_send_env; i++)
497                 if (match_pattern(name, options.send_env[i]))
498                         return (1);
499
500         return (0);
501 }
502
503 /* Multiplex client main loop. */
504 void
505 muxclient(const char *path)
506 {
507         struct sockaddr_un addr;
508         int i, r, fd, sock, exitval[2], num_env, addr_len;
509         Buffer m;
510         char *term;
511         extern char **environ;
512         u_int allowed, flags;
513
514         if (muxclient_command == 0)
515                 muxclient_command = SSHMUX_COMMAND_OPEN;
516
517         switch (options.control_master) {
518         case SSHCTL_MASTER_AUTO:
519         case SSHCTL_MASTER_AUTO_ASK:
520                 debug("auto-mux: Trying existing master");
521                 /* FALLTHROUGH */
522         case SSHCTL_MASTER_NO:
523                 break;
524         default:
525                 return;
526         }
527
528         memset(&addr, '\0', sizeof(addr));
529         addr.sun_family = AF_UNIX;
530         addr_len = offsetof(struct sockaddr_un, sun_path) +
531             strlen(path) + 1;
532
533         if (strlcpy(addr.sun_path, path,
534             sizeof(addr.sun_path)) >= sizeof(addr.sun_path))
535                 fatal("ControlPath too long");
536
537         if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
538                 fatal("%s socket(): %s", __func__, strerror(errno));
539
540         if (connect(sock, (struct sockaddr *)&addr, addr_len) == -1) {
541                 if (muxclient_command != SSHMUX_COMMAND_OPEN) {
542                         fatal("Control socket connect(%.100s): %s", path,
543                             strerror(errno));
544                 }
545                 if (errno == ENOENT)
546                         debug("Control socket \"%.100s\" does not exist", path);
547                 else {
548                         error("Control socket connect(%.100s): %s", path,
549                             strerror(errno));
550                 }
551                 close(sock);
552                 return;
553         }
554
555         if (stdin_null_flag) {
556                 if ((fd = open(_PATH_DEVNULL, O_RDONLY)) == -1)
557                         fatal("open(/dev/null): %s", strerror(errno));
558                 if (dup2(fd, STDIN_FILENO) == -1)
559                         fatal("dup2: %s", strerror(errno));
560                 if (fd > STDERR_FILENO)
561                         close(fd);
562         }
563
564         term = getenv("TERM");
565
566         flags = 0;
567         if (tty_flag)
568                 flags |= SSHMUX_FLAG_TTY;
569         if (subsystem_flag)
570                 flags |= SSHMUX_FLAG_SUBSYS;
571         if (options.forward_x11)
572                 flags |= SSHMUX_FLAG_X11_FWD;
573         if (options.forward_agent)
574                 flags |= SSHMUX_FLAG_AGENT_FWD;
575
576         signal(SIGPIPE, SIG_IGN);
577
578         buffer_init(&m);
579
580         /* Send our command to server */
581         buffer_put_int(&m, muxclient_command);
582         buffer_put_int(&m, flags);
583         if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) {
584                 error("%s: msg_send", __func__);
585  muxerr:
586                 close(sock);
587                 buffer_free(&m);
588                 if (muxclient_command != SSHMUX_COMMAND_OPEN)
589                         cleanup_exit(255);
590                 logit("Falling back to non-multiplexed connection");
591                 xfree(options.control_path);
592                 options.control_path = NULL;
593                 options.control_master = SSHCTL_MASTER_NO;
594                 return;
595         }
596         buffer_clear(&m);
597
598         /* Get authorisation status and PID of controlee */
599         if (ssh_msg_recv(sock, &m) == -1) {
600                 error("%s: Did not receive reply from master", __func__);
601                 goto muxerr;
602         }
603         if (buffer_get_char(&m) != SSHMUX_VER) {
604                 error("%s: Master replied with wrong version", __func__);
605                 goto muxerr;
606         }
607         if (buffer_get_int_ret(&allowed, &m) != 0) {
608                 error("%s: bad server reply", __func__);
609                 goto muxerr;
610         }
611         if (allowed != 1) {
612                 error("Connection to master denied");
613                 goto muxerr;
614         }
615         muxserver_pid = buffer_get_int(&m);
616
617         buffer_clear(&m);
618
619         switch (muxclient_command) {
620         case SSHMUX_COMMAND_ALIVE_CHECK:
621                 fprintf(stderr, "Master running (pid=%d)\r\n",
622                     muxserver_pid);
623                 exit(0);
624         case SSHMUX_COMMAND_TERMINATE:
625                 fprintf(stderr, "Exit request sent.\r\n");
626                 exit(0);
627         case SSHMUX_COMMAND_OPEN:
628                 buffer_put_cstring(&m, term ? term : "");
629                 if (options.escape_char == SSH_ESCAPECHAR_NONE)
630                         buffer_put_int(&m, 0xffffffff);
631                 else
632                         buffer_put_int(&m, options.escape_char);
633                 buffer_append(&command, "\0", 1);
634                 buffer_put_cstring(&m, buffer_ptr(&command));
635
636                 if (options.num_send_env == 0 || environ == NULL) {
637                         buffer_put_int(&m, 0);
638                 } else {
639                         /* Pass environment */
640                         num_env = 0;
641                         for (i = 0; environ[i] != NULL; i++) {
642                                 if (env_permitted(environ[i]))
643                                         num_env++; /* Count */
644                         }
645                         buffer_put_int(&m, num_env);
646                 for (i = 0; environ[i] != NULL && num_env >= 0; i++) {
647                                 if (env_permitted(environ[i])) {
648                                         num_env--;
649                                         buffer_put_cstring(&m, environ[i]);
650                                 }
651                         }
652                 }
653                 break;
654         default:
655                 fatal("unrecognised muxclient_command %d", muxclient_command);
656         }
657
658         if (ssh_msg_send(sock, SSHMUX_VER, &m) == -1) {
659                 error("%s: msg_send", __func__);
660                 goto muxerr;
661         }
662
663         if (mm_send_fd(sock, STDIN_FILENO) == -1 ||
664             mm_send_fd(sock, STDOUT_FILENO) == -1 ||
665             mm_send_fd(sock, STDERR_FILENO) == -1) {
666                 error("%s: send fds failed", __func__);
667                 goto muxerr;
668         }
669
670         /*
671          * Mux errors are non-recoverable from this point as the master
672          * has ownership of the session now.
673          */
674
675         /* Wait for reply, so master has a chance to gather ttymodes */
676         buffer_clear(&m);
677         if (ssh_msg_recv(sock, &m) == -1)
678                 fatal("%s: msg_recv", __func__);
679         if (buffer_get_char(&m) != SSHMUX_VER)
680                 fatal("%s: wrong version", __func__);
681         buffer_free(&m);
682
683         signal(SIGHUP, control_client_sighandler);
684         signal(SIGINT, control_client_sighandler);
685         signal(SIGTERM, control_client_sighandler);
686         signal(SIGWINCH, control_client_sigrelay);
687
688         if (tty_flag)
689                 enter_raw_mode();
690
691         /*
692          * Stick around until the controlee closes the client_fd.
693          * Before it does, it is expected to write this process' exit
694          * value (one int). This process must read the value and wait for
695          * the closure of the client_fd; if this one closes early, the 
696          * multiplex master will terminate early too (possibly losing data).
697          */
698         exitval[0] = 0;
699         for (i = 0; !muxclient_terminate && i < (int)sizeof(exitval);) {
700                 r = read(sock, (char *)exitval + i, sizeof(exitval) - i);
701                 if (r == 0) {
702                         debug2("Received EOF from master");
703                         break;
704                 }
705                 if (r == -1) {
706                         if (errno == EINTR)
707                                 continue;
708                         fatal("%s: read %s", __func__, strerror(errno));
709                 }
710                 i += r;
711         }
712
713         close(sock);
714         leave_raw_mode();
715         if (i > (int)sizeof(int))
716                 fatal("%s: master returned too much data (%d > %lu)",
717                     __func__, i, (u_long)sizeof(int));
718         if (muxclient_terminate) {
719                 debug2("Exiting on signal %d", muxclient_terminate);
720                 exitval[0] = 255;
721         } else if (i < (int)sizeof(int)) {
722                 debug2("Control master terminated unexpectedly");
723                 exitval[0] = 255;
724         } else
725                 debug2("Received exit status from master %d", exitval[0]);
726
727         if (tty_flag && options.log_level != SYSLOG_LEVEL_QUIET)
728                 fprintf(stderr, "Shared connection to %s closed.\r\n", host);
729
730         exit(exitval[0]);
731 }
This page took 0.104189 seconds and 5 git commands to generate.