2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2007-09-26 13:43:15 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2009-02-08 16:11:26 +00:00
|
|
|
#include <sys/wait.h>
|
2018-06-07 08:17:05 +00:00
|
|
|
#include <sys/file.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2009-11-08 22:40:36 +00:00
|
|
|
#include <event.h>
|
2014-04-29 22:31:22 +00:00
|
|
|
#include <fcntl.h>
|
2014-10-20 23:27:14 +00:00
|
|
|
#include <signal.h>
|
2007-09-26 13:43:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static struct tmuxproc *client_proc;
|
|
|
|
static struct tmuxpeer *client_peer;
|
|
|
|
static int client_flags;
|
|
|
|
static enum {
|
2012-04-24 16:24:03 +00:00
|
|
|
CLIENT_EXIT_NONE,
|
|
|
|
CLIENT_EXIT_DETACHED,
|
|
|
|
CLIENT_EXIT_DETACHED_HUP,
|
|
|
|
CLIENT_EXIT_LOST_TTY,
|
|
|
|
CLIENT_EXIT_TERMINATED,
|
|
|
|
CLIENT_EXIT_LOST_SERVER,
|
|
|
|
CLIENT_EXIT_EXITED,
|
|
|
|
CLIENT_EXIT_SERVER_EXITED,
|
|
|
|
} client_exitreason = CLIENT_EXIT_NONE;
|
2019-12-13 06:55:12 +00:00
|
|
|
static int client_exitflag;
|
2016-10-03 22:52:11 +00:00
|
|
|
static int client_exitval;
|
|
|
|
static enum msgtype client_exittype;
|
|
|
|
static const char *client_exitsession;
|
2017-01-13 10:12:12 +00:00
|
|
|
static const char *client_execshell;
|
|
|
|
static const char *client_execcmd;
|
2016-10-03 22:52:11 +00:00
|
|
|
static int client_attached;
|
2019-12-12 11:39:56 +00:00
|
|
|
static struct client_files client_files = RB_INITIALIZER(&client_files);
|
2016-10-03 22:52:11 +00:00
|
|
|
|
|
|
|
static __dead void client_exec(const char *,const char *);
|
|
|
|
static int client_get_lock(char *);
|
|
|
|
static int client_connect(struct event_base *, const char *, int);
|
2020-04-20 13:25:36 +00:00
|
|
|
static void client_send_identify(const char *, const char *, int);
|
2016-10-03 22:52:11 +00:00
|
|
|
static void client_signal(int);
|
|
|
|
static void client_dispatch(struct imsg *, void *);
|
|
|
|
static void client_dispatch_attached(struct imsg *);
|
2017-07-12 09:21:25 +00:00
|
|
|
static void client_dispatch_wait(struct imsg *);
|
2016-10-03 22:52:11 +00:00
|
|
|
static const char *client_exit_message(void);
|
2009-10-23 17:32:26 +00:00
|
|
|
|
2012-03-18 01:28:10 +00:00
|
|
|
/*
|
|
|
|
* Get server create lock. If already held then server start is happening in
|
2015-11-24 23:01:51 +00:00
|
|
|
* another client, so block until the lock is released and return -2 to
|
|
|
|
* retry. Return -1 on failure to continue and start the server anyway.
|
2012-03-18 01:28:10 +00:00
|
|
|
*/
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2012-03-18 01:28:10 +00:00
|
|
|
client_get_lock(char *lockfile)
|
|
|
|
{
|
|
|
|
int lockfd;
|
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("lock file is %s", lockfile);
|
2012-03-18 01:28:10 +00:00
|
|
|
|
2015-11-24 22:45:44 +00:00
|
|
|
if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) {
|
2015-11-24 23:01:51 +00:00
|
|
|
log_debug("open failed: %s", strerror(errno));
|
|
|
|
return (-1);
|
2015-11-24 22:45:44 +00:00
|
|
|
}
|
2012-03-09 09:57:40 +00:00
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
|
|
|
|
log_debug("flock failed: %s", strerror(errno));
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
return (lockfd);
|
|
|
|
while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
|
2012-03-18 01:28:10 +00:00
|
|
|
/* nothing */;
|
|
|
|
close(lockfd);
|
2015-11-24 23:01:51 +00:00
|
|
|
return (-2);
|
2012-03-18 01:28:10 +00:00
|
|
|
}
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("flock succeeded");
|
2012-03-18 01:28:10 +00:00
|
|
|
|
|
|
|
return (lockfd);
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Connect client to server. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2020-03-12 09:26:34 +00:00
|
|
|
client_connect(struct event_base *base, const char *path, int flags)
|
2007-09-26 13:43:15 +00:00
|
|
|
{
|
2009-10-23 17:37:41 +00:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
size_t size;
|
2014-07-21 10:52:48 +00:00
|
|
|
int fd, lockfd = -1, locked = 0;
|
|
|
|
char *lockfile = NULL;
|
2007-09-26 13:43:15 +00:00
|
|
|
|
|
|
|
memset(&sa, 0, sizeof sa);
|
|
|
|
sa.sun_family = AF_UNIX;
|
2007-10-23 10:25:03 +00:00
|
|
|
size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
|
|
|
|
if (size >= sizeof sa.sun_path) {
|
2007-12-01 11:10:33 +00:00
|
|
|
errno = ENAMETOOLONG;
|
2010-10-24 01:31:08 +00:00
|
|
|
return (-1);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("socket is %s", path);
|
2007-09-26 13:43:15 +00:00
|
|
|
|
2012-03-18 01:28:10 +00:00
|
|
|
retry:
|
2009-08-09 17:43:00 +00:00
|
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2015-11-24 22:45:44 +00:00
|
|
|
return (-1);
|
2007-12-01 11:10:33 +00:00
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("trying connect");
|
2015-11-24 22:45:44 +00:00
|
|
|
if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
|
2014-07-21 10:52:48 +00:00
|
|
|
log_debug("connect failed: %s", strerror(errno));
|
2012-03-18 01:28:10 +00:00
|
|
|
if (errno != ECONNREFUSED && errno != ENOENT)
|
2010-10-24 01:31:08 +00:00
|
|
|
goto failed;
|
2020-03-12 09:26:34 +00:00
|
|
|
if (~flags & CLIENT_STARTSERVER)
|
2010-10-24 01:31:08 +00:00
|
|
|
goto failed;
|
2012-03-18 01:28:10 +00:00
|
|
|
close(fd);
|
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
if (!locked) {
|
|
|
|
xasprintf(&lockfile, "%s.lock", path);
|
2015-11-24 23:01:51 +00:00
|
|
|
if ((lockfd = client_get_lock(lockfile)) < 0) {
|
|
|
|
log_debug("didn't get lock (%d)", lockfd);
|
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
free(lockfile);
|
2015-11-24 23:01:51 +00:00
|
|
|
lockfile = NULL;
|
|
|
|
|
|
|
|
if (lockfd == -2)
|
|
|
|
goto retry;
|
2014-07-21 10:52:48 +00:00
|
|
|
}
|
2015-11-24 23:01:51 +00:00
|
|
|
log_debug("got lock (%d)", lockfd);
|
2014-07-21 10:52:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Always retry at least once, even if we got the lock,
|
|
|
|
* because another client could have taken the lock,
|
|
|
|
* started the server and released the lock between our
|
|
|
|
* connect() and flock().
|
|
|
|
*/
|
|
|
|
locked = 1;
|
2012-03-18 01:28:10 +00:00
|
|
|
goto retry;
|
2014-01-09 21:20:45 +00:00
|
|
|
}
|
2014-07-21 10:52:48 +00:00
|
|
|
|
2015-11-24 23:01:51 +00:00
|
|
|
if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) {
|
2014-01-09 21:20:45 +00:00
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
2012-03-18 01:28:10 +00:00
|
|
|
return (-1);
|
2014-01-09 21:20:45 +00:00
|
|
|
}
|
2020-03-12 09:26:34 +00:00
|
|
|
fd = server_start(client_proc, flags, base, lockfd, lockfile);
|
2014-07-21 10:52:48 +00:00
|
|
|
}
|
2015-09-24 12:06:20 +00:00
|
|
|
|
2015-11-24 23:01:51 +00:00
|
|
|
if (locked && lockfd >= 0) {
|
2012-07-11 19:34:16 +00:00
|
|
|
free(lockfile);
|
2012-03-18 01:28:10 +00:00
|
|
|
close(lockfd);
|
2007-09-26 13:43:15 +00:00
|
|
|
}
|
2011-01-21 23:44:13 +00:00
|
|
|
setblocking(fd, 0);
|
2010-10-24 01:31:08 +00:00
|
|
|
return (fd);
|
|
|
|
|
|
|
|
failed:
|
2015-09-24 12:06:20 +00:00
|
|
|
if (locked) {
|
|
|
|
free(lockfile);
|
|
|
|
close(lockfd);
|
|
|
|
}
|
2010-10-24 01:31:08 +00:00
|
|
|
close(fd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-04-24 16:24:03 +00:00
|
|
|
/* Get exit string from reason number. */
|
|
|
|
const char *
|
|
|
|
client_exit_message(void)
|
|
|
|
{
|
2013-10-06 20:21:52 +00:00
|
|
|
static char msg[256];
|
|
|
|
|
2012-04-24 16:24:03 +00:00
|
|
|
switch (client_exitreason) {
|
|
|
|
case CLIENT_EXIT_NONE:
|
|
|
|
break;
|
|
|
|
case CLIENT_EXIT_DETACHED:
|
2013-10-06 20:21:52 +00:00
|
|
|
if (client_exitsession != NULL) {
|
|
|
|
xsnprintf(msg, sizeof msg, "detached "
|
|
|
|
"(from session %s)", client_exitsession);
|
|
|
|
return (msg);
|
|
|
|
}
|
2012-04-24 16:24:03 +00:00
|
|
|
return ("detached");
|
|
|
|
case CLIENT_EXIT_DETACHED_HUP:
|
2013-10-06 20:21:52 +00:00
|
|
|
if (client_exitsession != NULL) {
|
|
|
|
xsnprintf(msg, sizeof msg, "detached and SIGHUP "
|
|
|
|
"(from session %s)", client_exitsession);
|
|
|
|
return (msg);
|
|
|
|
}
|
2012-04-24 16:24:03 +00:00
|
|
|
return ("detached and SIGHUP");
|
|
|
|
case CLIENT_EXIT_LOST_TTY:
|
|
|
|
return ("lost tty");
|
|
|
|
case CLIENT_EXIT_TERMINATED:
|
|
|
|
return ("terminated");
|
|
|
|
case CLIENT_EXIT_LOST_SERVER:
|
2019-07-26 20:08:40 +00:00
|
|
|
return ("server exited unexpectedly");
|
2012-04-24 16:24:03 +00:00
|
|
|
case CLIENT_EXIT_EXITED:
|
|
|
|
return ("exited");
|
|
|
|
case CLIENT_EXIT_SERVER_EXITED:
|
|
|
|
return ("server exited");
|
|
|
|
}
|
|
|
|
return ("unknown reason");
|
|
|
|
}
|
|
|
|
|
2019-12-13 06:55:12 +00:00
|
|
|
/* Exit if all streams flushed. */
|
|
|
|
static void
|
|
|
|
client_exit(void)
|
|
|
|
{
|
|
|
|
struct client_file *cf;
|
|
|
|
size_t left;
|
|
|
|
int waiting = 0;
|
|
|
|
|
|
|
|
RB_FOREACH (cf, client_files, &client_files) {
|
|
|
|
if (cf->event == NULL)
|
|
|
|
continue;
|
|
|
|
left = EVBUFFER_LENGTH(cf->event->output);
|
|
|
|
if (left != 0) {
|
|
|
|
waiting++;
|
|
|
|
log_debug("file %u %zu bytes left", cf->stream, left);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (waiting == 0)
|
|
|
|
proc_exit(client_proc);
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Client main loop. */
|
|
|
|
int
|
2020-04-20 13:25:36 +00:00
|
|
|
client_main(struct event_base *base, int argc, char **argv, int flags, int feat)
|
2010-10-24 01:31:08 +00:00
|
|
|
{
|
2019-05-25 07:18:20 +00:00
|
|
|
struct cmd_parse_result *pr;
|
2019-12-12 11:39:56 +00:00
|
|
|
struct msg_command *data;
|
2020-03-12 09:26:34 +00:00
|
|
|
int fd, i;
|
2015-10-18 20:42:42 +00:00
|
|
|
const char *ttynam, *cwd;
|
2011-03-19 23:27:35 +00:00
|
|
|
pid_t ppid;
|
2010-10-24 01:31:08 +00:00
|
|
|
enum msgtype msg;
|
2012-06-18 15:23:01 +00:00
|
|
|
struct termios tio, saved_tio;
|
2013-10-05 23:48:24 +00:00
|
|
|
size_t size;
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Set up the initial command. */
|
2017-07-12 09:21:25 +00:00
|
|
|
if (shell_command != NULL) {
|
2010-10-24 01:31:08 +00:00
|
|
|
msg = MSG_SHELL;
|
2020-03-30 15:49:23 +00:00
|
|
|
flags |= CLIENT_STARTSERVER;
|
2010-10-24 01:31:08 +00:00
|
|
|
} else if (argc == 0) {
|
|
|
|
msg = MSG_COMMAND;
|
2020-03-12 09:26:34 +00:00
|
|
|
flags |= CLIENT_STARTSERVER;
|
2010-10-24 01:31:08 +00:00
|
|
|
} else {
|
|
|
|
msg = MSG_COMMAND;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It sucks parsing the command string twice (in client and
|
|
|
|
* later in server) but it is necessary to get the start server
|
|
|
|
* flag.
|
|
|
|
*/
|
2019-05-25 07:18:20 +00:00
|
|
|
pr = cmd_parse_from_arguments(argc, argv, NULL);
|
|
|
|
if (pr->status == CMD_PARSE_SUCCESS) {
|
2020-04-13 08:26:27 +00:00
|
|
|
if (cmd_list_any_have(pr->cmdlist, CMD_STARTSERVER))
|
|
|
|
flags |= CLIENT_STARTSERVER;
|
2019-05-25 07:18:20 +00:00
|
|
|
cmd_list_free(pr->cmdlist);
|
|
|
|
} else
|
|
|
|
free(pr->error);
|
2010-10-24 01:31:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 09:26:34 +00:00
|
|
|
/* Save the flags. */
|
|
|
|
client_flags = flags;
|
|
|
|
|
2015-11-24 22:45:44 +00:00
|
|
|
/* Create client process structure (starts logging). */
|
2017-07-12 09:24:17 +00:00
|
|
|
client_proc = proc_start("client");
|
|
|
|
proc_set_signals(client_proc, client_signal);
|
2015-11-24 22:45:44 +00:00
|
|
|
|
2014-07-21 10:52:48 +00:00
|
|
|
/* Initialize the client socket and start the server. */
|
2020-03-12 09:26:34 +00:00
|
|
|
fd = client_connect(base, socket_path, client_flags);
|
2010-10-24 01:31:08 +00:00
|
|
|
if (fd == -1) {
|
2015-04-21 22:21:41 +00:00
|
|
|
if (errno == ECONNREFUSED) {
|
|
|
|
fprintf(stderr, "no server running on %s\n",
|
|
|
|
socket_path);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error connecting to %s (%s)\n",
|
|
|
|
socket_path, strerror(errno));
|
|
|
|
}
|
2010-10-24 01:31:08 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2017-07-12 09:21:25 +00:00
|
|
|
client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
|
2015-10-27 13:23:24 +00:00
|
|
|
|
2015-10-17 18:30:43 +00:00
|
|
|
/* Save these before pledge(). */
|
2018-11-22 10:36:40 +00:00
|
|
|
if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL)
|
2018-01-01 11:19:08 +00:00
|
|
|
cwd = "/";
|
2015-10-17 18:30:43 +00:00
|
|
|
if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
|
|
|
|
ttynam = "";
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop privileges for client. "proc exec" is needed for -c and for
|
|
|
|
* locking (which uses system(3)).
|
|
|
|
*
|
|
|
|
* "tty" is needed to restore termios(4) and also for some reason -CC
|
|
|
|
* does not work properly without it (input is not recognised).
|
|
|
|
*
|
|
|
|
* "sendfd" is dropped later in client_dispatch_wait().
|
|
|
|
*/
|
2019-12-12 11:39:56 +00:00
|
|
|
if (pledge(
|
|
|
|
"stdio rpath wpath cpath unix sendfd proc exec tty",
|
|
|
|
NULL) != 0)
|
2015-10-17 18:30:43 +00:00
|
|
|
fatal("pledge failed");
|
|
|
|
|
|
|
|
/* Free stuff that is not used in the client. */
|
2017-01-23 10:09:43 +00:00
|
|
|
if (ptm_fd != -1)
|
|
|
|
close(ptm_fd);
|
2015-10-27 15:58:42 +00:00
|
|
|
options_free(global_options);
|
|
|
|
options_free(global_s_options);
|
|
|
|
options_free(global_w_options);
|
2015-10-28 09:51:55 +00:00
|
|
|
environ_free(global_environ);
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
/* Set up control mode. */
|
2015-08-30 22:40:25 +00:00
|
|
|
if (client_flags & CLIENT_CONTROLCONTROL) {
|
2017-01-20 14:02:33 +00:00
|
|
|
if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
|
|
|
|
fprintf(stderr, "tcgetattr failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return (1);
|
|
|
|
}
|
2012-06-18 15:23:01 +00:00
|
|
|
cfmakeraw(&tio);
|
|
|
|
tio.c_iflag = ICRNL|IXANY;
|
|
|
|
tio.c_oflag = OPOST|ONLCR;
|
2012-06-18 16:23:42 +00:00
|
|
|
#ifdef NOKERNINFO
|
2012-06-18 15:23:01 +00:00
|
|
|
tio.c_lflag = NOKERNINFO;
|
2012-06-18 16:23:42 +00:00
|
|
|
#endif
|
2012-06-18 15:23:01 +00:00
|
|
|
tio.c_cflag = CREAD|CS8|HUPCL;
|
|
|
|
tio.c_cc[VMIN] = 1;
|
|
|
|
tio.c_cc[VTIME] = 0;
|
|
|
|
cfsetispeed(&tio, cfgetispeed(&saved_tio));
|
|
|
|
cfsetospeed(&tio, cfgetospeed(&saved_tio));
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &tio);
|
|
|
|
}
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
/* Send identify messages. */
|
2020-04-20 13:25:36 +00:00
|
|
|
client_send_identify(ttynam, cwd, feat);
|
2007-10-03 21:31:07 +00:00
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Send first command. */
|
|
|
|
if (msg == MSG_COMMAND) {
|
2013-10-05 23:48:24 +00:00
|
|
|
/* How big is the command? */
|
|
|
|
size = 0;
|
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
size += strlen(argv[i]) + 1;
|
2017-12-18 22:13:36 +00:00
|
|
|
if (size > MAX_IMSGSIZE - (sizeof *data)) {
|
|
|
|
fprintf(stderr, "command too long\n");
|
|
|
|
return (1);
|
|
|
|
}
|
2013-10-05 23:48:24 +00:00
|
|
|
data = xmalloc((sizeof *data) + size);
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Prepare command for server. */
|
2013-10-05 23:48:24 +00:00
|
|
|
data->argc = argc;
|
2014-09-01 21:50:18 +00:00
|
|
|
if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
|
2012-05-30 13:42:57 +00:00
|
|
|
fprintf(stderr, "command too long\n");
|
2013-10-05 23:48:24 +00:00
|
|
|
free(data);
|
2010-10-24 01:31:08 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2013-10-05 23:48:24 +00:00
|
|
|
size += sizeof *data;
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2013-10-05 23:48:24 +00:00
|
|
|
/* Send the command. */
|
2015-10-27 13:23:24 +00:00
|
|
|
if (proc_send(client_peer, msg, -1, data, size) != 0) {
|
2013-10-05 23:48:24 +00:00
|
|
|
fprintf(stderr, "failed to send command\n");
|
|
|
|
free(data);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
free(data);
|
2010-10-24 01:31:08 +00:00
|
|
|
} else if (msg == MSG_SHELL)
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, msg, -1, NULL, 0);
|
2007-12-01 11:10:33 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Start main loop. */
|
|
|
|
proc_loop(client_proc, NULL);
|
2008-06-07 07:27:28 +00:00
|
|
|
|
2017-01-13 10:12:12 +00:00
|
|
|
/* Run command if user requested exec, instead of exiting. */
|
|
|
|
if (client_exittype == MSG_EXEC) {
|
|
|
|
if (client_flags & CLIENT_CONTROLCONTROL)
|
|
|
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
|
|
|
|
client_exec(client_execshell, client_execcmd);
|
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Print the exit message, if any, and exit. */
|
2011-03-19 23:27:35 +00:00
|
|
|
if (client_attached) {
|
2015-08-30 22:40:25 +00:00
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE)
|
2012-04-24 16:24:03 +00:00
|
|
|
printf("[%s]\n", client_exit_message());
|
2011-03-19 23:27:35 +00:00
|
|
|
|
|
|
|
ppid = getppid();
|
|
|
|
if (client_exittype == MSG_DETACHKILL && ppid > 1)
|
|
|
|
kill(ppid, SIGHUP);
|
2015-08-30 22:40:25 +00:00
|
|
|
} else if (client_flags & CLIENT_CONTROLCONTROL) {
|
2013-10-05 23:10:40 +00:00
|
|
|
if (client_exitreason != CLIENT_EXIT_NONE)
|
|
|
|
printf("%%exit %s\n", client_exit_message());
|
|
|
|
else
|
|
|
|
printf("%%exit\n");
|
|
|
|
printf("\033\\");
|
2012-06-18 15:23:01 +00:00
|
|
|
tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
|
2015-11-25 07:58:55 +00:00
|
|
|
} else if (client_exitreason != CLIENT_EXIT_NONE)
|
2015-11-24 22:45:44 +00:00
|
|
|
fprintf(stderr, "%s\n", client_exit_message());
|
2012-05-22 20:56:35 +00:00
|
|
|
setblocking(STDIN_FILENO, 1);
|
2010-10-24 01:31:08 +00:00
|
|
|
return (client_exitval);
|
2007-09-28 19:04:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
/* Send identify messages to server. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2020-04-20 13:25:36 +00:00
|
|
|
client_send_identify(const char *ttynam, const char *cwd, int feat)
|
2009-10-23 17:37:41 +00:00
|
|
|
{
|
2015-06-14 10:07:44 +00:00
|
|
|
const char *s;
|
2013-10-06 20:02:23 +00:00
|
|
|
char **ss;
|
2015-07-13 18:10:26 +00:00
|
|
|
size_t sslen;
|
2015-08-30 22:40:25 +00:00
|
|
|
int fd, flags = client_flags;
|
2015-06-14 10:07:44 +00:00
|
|
|
pid_t pid;
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2013-10-06 20:02:23 +00:00
|
|
|
if ((s = getenv("TERM")) == NULL)
|
|
|
|
s = "";
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
|
2020-04-20 13:25:36 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_FEATURES, -1, &feat, sizeof feat);
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2015-11-24 22:04:36 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam,
|
|
|
|
strlen(ttynam) + 1);
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2012-03-29 21:02:34 +00:00
|
|
|
if ((fd = dup(STDIN_FILENO)) == -1)
|
|
|
|
fatal("dup failed");
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
|
2009-10-23 17:37:41 +00:00
|
|
|
|
2015-06-14 10:07:44 +00:00
|
|
|
pid = getpid();
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
|
2015-06-14 10:07:44 +00:00
|
|
|
|
2015-07-13 18:10:26 +00:00
|
|
|
for (ss = environ; *ss != NULL; ss++) {
|
|
|
|
sslen = strlen(*ss) + 1;
|
2015-11-24 22:04:36 +00:00
|
|
|
if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
|
|
|
|
continue;
|
|
|
|
proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
|
2009-11-08 22:40:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
|
2009-11-08 22:40:36 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
/* File write error callback. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2019-12-12 11:39:56 +00:00
|
|
|
client_write_error_callback(__unused struct bufferevent *bev,
|
|
|
|
__unused short what, void *arg)
|
2012-05-22 20:56:35 +00:00
|
|
|
{
|
2019-12-12 11:39:56 +00:00
|
|
|
struct client_file *cf = arg;
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
log_debug("write error file %d", cf->stream);
|
|
|
|
|
|
|
|
bufferevent_free(cf->event);
|
|
|
|
cf->event = NULL;
|
|
|
|
|
|
|
|
close(cf->fd);
|
|
|
|
cf->fd = -1;
|
2019-12-13 07:00:22 +00:00
|
|
|
|
|
|
|
if (client_exitflag)
|
|
|
|
client_exit();
|
2019-12-12 11:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* File write callback. */
|
|
|
|
static void
|
|
|
|
client_write_callback(__unused struct bufferevent *bev, void *arg)
|
|
|
|
{
|
|
|
|
struct client_file *cf = arg;
|
|
|
|
|
|
|
|
if (cf->closed && EVBUFFER_LENGTH(cf->event->output) == 0) {
|
|
|
|
bufferevent_free(cf->event);
|
|
|
|
close(cf->fd);
|
|
|
|
RB_REMOVE(client_files, &client_files, cf);
|
|
|
|
file_free(cf);
|
|
|
|
}
|
2019-12-13 06:55:12 +00:00
|
|
|
|
|
|
|
if (client_exitflag)
|
|
|
|
client_exit();
|
2019-12-12 11:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open write file. */
|
|
|
|
static void
|
|
|
|
client_write_open(void *data, size_t datalen)
|
|
|
|
{
|
|
|
|
struct msg_write_open *msg = data;
|
2019-12-16 15:48:50 +00:00
|
|
|
const char *path;
|
2019-12-12 11:39:56 +00:00
|
|
|
struct msg_write_ready reply;
|
|
|
|
struct client_file find, *cf;
|
|
|
|
const int flags = O_NONBLOCK|O_WRONLY|O_CREAT;
|
|
|
|
int error = 0;
|
|
|
|
|
2019-12-16 15:48:50 +00:00
|
|
|
if (datalen < sizeof *msg)
|
2019-12-12 11:39:56 +00:00
|
|
|
fatalx("bad MSG_WRITE_OPEN size");
|
2019-12-16 15:48:50 +00:00
|
|
|
if (datalen == sizeof *msg)
|
|
|
|
path = "-";
|
|
|
|
else
|
|
|
|
path = (const char *)(msg + 1);
|
|
|
|
log_debug("open write file %d %s", msg->stream, path);
|
2019-12-12 11:39:56 +00:00
|
|
|
|
|
|
|
find.stream = msg->stream;
|
|
|
|
if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
|
|
|
|
cf = file_create(NULL, msg->stream, NULL, NULL);
|
|
|
|
RB_INSERT(client_files, &client_files, cf);
|
|
|
|
} else {
|
|
|
|
error = EBADF;
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
if (cf->closed) {
|
|
|
|
error = EBADF;
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
cf->fd = -1;
|
|
|
|
if (msg->fd == -1)
|
2019-12-16 15:48:50 +00:00
|
|
|
cf->fd = open(path, msg->flags|flags, 0644);
|
2019-12-12 11:39:56 +00:00
|
|
|
else {
|
|
|
|
if (msg->fd != STDOUT_FILENO && msg->fd != STDERR_FILENO)
|
|
|
|
errno = EBADF;
|
|
|
|
else {
|
|
|
|
cf->fd = dup(msg->fd);
|
|
|
|
if (client_flags & CLIENT_CONTROL)
|
|
|
|
close(msg->fd); /* can only be used once */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cf->fd == -1) {
|
|
|
|
error = errno;
|
|
|
|
goto reply;
|
|
|
|
}
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
cf->event = bufferevent_new(cf->fd, NULL, client_write_callback,
|
|
|
|
client_write_error_callback, cf);
|
|
|
|
bufferevent_enable(cf->event, EV_WRITE);
|
|
|
|
goto reply;
|
|
|
|
|
|
|
|
reply:
|
|
|
|
reply.stream = msg->stream;
|
|
|
|
reply.error = error;
|
|
|
|
proc_send(client_peer, MSG_WRITE_READY, -1, &reply, sizeof reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write to client file. */
|
|
|
|
static void
|
|
|
|
client_write_data(void *data, size_t datalen)
|
|
|
|
{
|
|
|
|
struct msg_write_data *msg = data;
|
|
|
|
struct client_file find, *cf;
|
2019-12-16 15:48:50 +00:00
|
|
|
size_t size = datalen - sizeof *msg;
|
2019-12-12 11:39:56 +00:00
|
|
|
|
2019-12-16 15:48:50 +00:00
|
|
|
if (datalen < sizeof *msg)
|
2019-12-12 11:39:56 +00:00
|
|
|
fatalx("bad MSG_WRITE size");
|
|
|
|
find.stream = msg->stream;
|
|
|
|
if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
|
|
|
|
fatalx("unknown stream number");
|
2019-12-16 15:48:50 +00:00
|
|
|
log_debug("write %zu to file %d", size, cf->stream);
|
2019-12-12 11:39:56 +00:00
|
|
|
|
|
|
|
if (cf->event != NULL)
|
2019-12-16 15:48:50 +00:00
|
|
|
bufferevent_write(cf->event, msg + 1, size);
|
2019-12-12 11:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close client file. */
|
|
|
|
static void
|
|
|
|
client_write_close(void *data, size_t datalen)
|
|
|
|
{
|
|
|
|
struct msg_write_close *msg = data;
|
|
|
|
struct client_file find, *cf;
|
|
|
|
|
|
|
|
if (datalen != sizeof *msg)
|
|
|
|
fatalx("bad MSG_WRITE_CLOSE size");
|
|
|
|
find.stream = msg->stream;
|
|
|
|
if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL)
|
|
|
|
fatalx("unknown stream number");
|
|
|
|
log_debug("close file %d", cf->stream);
|
|
|
|
|
|
|
|
if (cf->event == NULL || EVBUFFER_LENGTH(cf->event->output) == 0) {
|
|
|
|
if (cf->event != NULL)
|
|
|
|
bufferevent_free(cf->event);
|
|
|
|
if (cf->fd != -1)
|
|
|
|
close(cf->fd);
|
|
|
|
RB_REMOVE(client_files, &client_files, cf);
|
|
|
|
file_free(cf);
|
|
|
|
}
|
2012-05-22 20:56:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
/* File read callback. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2019-12-12 11:39:56 +00:00
|
|
|
client_read_callback(__unused struct bufferevent *bev, void *arg)
|
2012-06-18 15:25:23 +00:00
|
|
|
{
|
2019-12-12 11:39:56 +00:00
|
|
|
struct client_file *cf = arg;
|
|
|
|
void *bdata;
|
|
|
|
size_t bsize;
|
2019-12-16 15:48:50 +00:00
|
|
|
struct msg_read_data *msg;
|
|
|
|
size_t msglen;
|
2019-12-12 11:39:56 +00:00
|
|
|
|
2019-12-16 15:48:50 +00:00
|
|
|
msg = xmalloc(sizeof *msg);
|
2019-12-12 11:39:56 +00:00
|
|
|
for (;;) {
|
|
|
|
bdata = EVBUFFER_DATA(cf->event->input);
|
|
|
|
bsize = EVBUFFER_LENGTH(cf->event->input);
|
|
|
|
|
|
|
|
if (bsize == 0)
|
2012-06-18 15:25:23 +00:00
|
|
|
break;
|
2019-12-16 16:09:28 +00:00
|
|
|
if (bsize > MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg)
|
|
|
|
bsize = MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof *msg;
|
2019-12-12 11:39:56 +00:00
|
|
|
log_debug("read %zu from file %d", bsize, cf->stream);
|
|
|
|
|
2019-12-16 15:48:50 +00:00
|
|
|
msglen = (sizeof *msg) + bsize;
|
|
|
|
msg = xrealloc(msg, msglen);
|
|
|
|
msg->stream = cf->stream;
|
|
|
|
memcpy(msg + 1, bdata, bsize);
|
|
|
|
proc_send(client_peer, MSG_READ, -1, msg, msglen);
|
2019-12-12 11:39:56 +00:00
|
|
|
|
|
|
|
evbuffer_drain(cf->event->input, bsize);
|
|
|
|
}
|
2019-12-16 15:48:50 +00:00
|
|
|
free(msg);
|
2019-12-12 11:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* File read error callback. */
|
|
|
|
static void
|
|
|
|
client_read_error_callback(__unused struct bufferevent *bev,
|
|
|
|
__unused short what, void *arg)
|
|
|
|
{
|
|
|
|
struct client_file *cf = arg;
|
|
|
|
struct msg_read_done msg;
|
|
|
|
|
|
|
|
log_debug("read error file %d", cf->stream);
|
|
|
|
|
|
|
|
msg.stream = cf->stream;
|
|
|
|
msg.error = 0;
|
|
|
|
proc_send(client_peer, MSG_READ_DONE, -1, &msg, sizeof msg);
|
|
|
|
|
|
|
|
bufferevent_free(cf->event);
|
|
|
|
close(cf->fd);
|
|
|
|
RB_REMOVE(client_files, &client_files, cf);
|
|
|
|
file_free(cf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open read file. */
|
|
|
|
static void
|
|
|
|
client_read_open(void *data, size_t datalen)
|
|
|
|
{
|
|
|
|
struct msg_read_open *msg = data;
|
2019-12-16 15:48:50 +00:00
|
|
|
const char *path;
|
2019-12-12 11:39:56 +00:00
|
|
|
struct msg_read_done reply;
|
|
|
|
struct client_file find, *cf;
|
|
|
|
const int flags = O_NONBLOCK|O_RDONLY;
|
2020-04-09 13:52:31 +00:00
|
|
|
int error;
|
2019-12-12 11:39:56 +00:00
|
|
|
|
2019-12-16 15:48:50 +00:00
|
|
|
if (datalen < sizeof *msg)
|
2019-12-12 11:39:56 +00:00
|
|
|
fatalx("bad MSG_READ_OPEN size");
|
2019-12-16 15:48:50 +00:00
|
|
|
if (datalen == sizeof *msg)
|
|
|
|
path = "-";
|
|
|
|
else
|
|
|
|
path = (const char *)(msg + 1);
|
|
|
|
log_debug("open read file %d %s", msg->stream, path);
|
2019-12-12 11:39:56 +00:00
|
|
|
|
|
|
|
find.stream = msg->stream;
|
|
|
|
if ((cf = RB_FIND(client_files, &client_files, &find)) == NULL) {
|
|
|
|
cf = file_create(NULL, msg->stream, NULL, NULL);
|
|
|
|
RB_INSERT(client_files, &client_files, cf);
|
|
|
|
} else {
|
|
|
|
error = EBADF;
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
if (cf->closed) {
|
|
|
|
error = EBADF;
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
cf->fd = -1;
|
|
|
|
if (msg->fd == -1)
|
2019-12-16 15:48:50 +00:00
|
|
|
cf->fd = open(path, flags);
|
2019-12-12 11:39:56 +00:00
|
|
|
else {
|
|
|
|
if (msg->fd != STDIN_FILENO)
|
|
|
|
errno = EBADF;
|
|
|
|
else {
|
|
|
|
cf->fd = dup(msg->fd);
|
|
|
|
close(msg->fd); /* can only be used once */
|
2012-06-18 15:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 11:39:56 +00:00
|
|
|
if (cf->fd == -1) {
|
|
|
|
error = errno;
|
|
|
|
goto reply;
|
|
|
|
}
|
|
|
|
|
|
|
|
cf->event = bufferevent_new(cf->fd, client_read_callback, NULL,
|
|
|
|
client_read_error_callback, cf);
|
|
|
|
bufferevent_enable(cf->event, EV_READ);
|
|
|
|
return;
|
|
|
|
|
|
|
|
reply:
|
|
|
|
reply.stream = msg->stream;
|
|
|
|
reply.error = error;
|
|
|
|
proc_send(client_peer, MSG_READ_DONE, -1, &reply, sizeof reply);
|
2012-06-18 15:25:23 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 22:40:25 +00:00
|
|
|
/* Run command in shell; used for -c. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static __dead void
|
2015-11-24 23:22:51 +00:00
|
|
|
client_exec(const char *shell, const char *shellcmd)
|
2015-08-30 22:40:25 +00:00
|
|
|
{
|
|
|
|
const char *name, *ptr;
|
|
|
|
char *argv0;
|
|
|
|
|
2015-11-24 23:22:51 +00:00
|
|
|
log_debug("shell %s, command %s", shell, shellcmd);
|
2015-08-30 22:40:25 +00:00
|
|
|
|
|
|
|
ptr = strrchr(shell, '/');
|
|
|
|
if (ptr != NULL && *(ptr + 1) != '\0')
|
|
|
|
name = ptr + 1;
|
|
|
|
else
|
|
|
|
name = shell;
|
|
|
|
if (client_flags & CLIENT_LOGIN)
|
|
|
|
xasprintf(&argv0, "-%s", name);
|
|
|
|
else
|
|
|
|
xasprintf(&argv0, "%s", name);
|
|
|
|
setenv("SHELL", shell, 1);
|
|
|
|
|
2017-07-14 18:49:07 +00:00
|
|
|
proc_clear_signals(client_proc, 1);
|
|
|
|
|
2015-08-30 22:40:25 +00:00
|
|
|
setblocking(STDIN_FILENO, 1);
|
|
|
|
setblocking(STDOUT_FILENO, 1);
|
|
|
|
setblocking(STDERR_FILENO, 1);
|
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
|
2015-11-24 23:22:51 +00:00
|
|
|
execl(shell, argv0, "-c", shellcmd, (char *) NULL);
|
2015-08-30 22:40:25 +00:00
|
|
|
fatal("execl failed");
|
|
|
|
}
|
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
/* Callback to handle signals in the client. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-10-27 13:23:24 +00:00
|
|
|
client_signal(int sig)
|
|
|
|
{
|
|
|
|
struct sigaction sigact;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (sig == SIGCHLD)
|
|
|
|
waitpid(WAIT_ANY, &status, WNOHANG);
|
|
|
|
else if (!client_attached) {
|
|
|
|
if (sig == SIGTERM)
|
|
|
|
proc_exit(client_proc);
|
|
|
|
} else {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGHUP:
|
|
|
|
client_exitreason = CLIENT_EXIT_LOST_TTY;
|
|
|
|
client_exitval = 1;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGTERM:
|
|
|
|
client_exitreason = CLIENT_EXIT_TERMINATED;
|
|
|
|
client_exitval = 1;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGWINCH:
|
|
|
|
proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case SIGCONT:
|
|
|
|
memset(&sigact, 0, sizeof sigact);
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigact.sa_handler = SIG_IGN;
|
|
|
|
if (sigaction(SIGTSTP, &sigact, NULL) != 0)
|
|
|
|
fatal("sigaction failed");
|
|
|
|
proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Callback for client read events. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2017-07-12 09:21:25 +00:00
|
|
|
client_dispatch(struct imsg *imsg, __unused void *arg)
|
2015-10-27 13:23:24 +00:00
|
|
|
{
|
|
|
|
if (imsg == NULL) {
|
|
|
|
client_exitreason = CLIENT_EXIT_LOST_SERVER;
|
|
|
|
client_exitval = 1;
|
2015-11-24 22:45:44 +00:00
|
|
|
proc_exit(client_proc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client_attached)
|
2015-10-27 13:23:24 +00:00
|
|
|
client_dispatch_attached(imsg);
|
|
|
|
else
|
2017-07-12 09:21:25 +00:00
|
|
|
client_dispatch_wait(imsg);
|
2015-10-27 13:23:24 +00:00
|
|
|
}
|
|
|
|
|
2010-10-24 01:31:08 +00:00
|
|
|
/* Dispatch imsgs when in wait state (before MSG_READY). */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2017-07-12 09:21:25 +00:00
|
|
|
client_dispatch_wait(struct imsg *imsg)
|
2010-10-24 01:31:08 +00:00
|
|
|
{
|
2019-12-12 11:39:56 +00:00
|
|
|
char *data;
|
|
|
|
ssize_t datalen;
|
|
|
|
int retval;
|
|
|
|
static int pledge_applied;
|
2015-10-17 18:30:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* "sendfd" is no longer required once all of the identify messages
|
|
|
|
* have been sent. We know the server won't send us anything until that
|
|
|
|
* point (because we don't ask it to), so we can drop "sendfd" once we
|
|
|
|
* get the first message from the server.
|
|
|
|
*/
|
|
|
|
if (!pledge_applied) {
|
2019-12-12 11:39:56 +00:00
|
|
|
if (pledge(
|
|
|
|
"stdio rpath wpath cpath unix proc exec tty",
|
|
|
|
NULL) != 0)
|
2015-10-17 18:30:43 +00:00
|
|
|
fatal("pledge failed");
|
|
|
|
pledge_applied = 1;
|
2019-12-12 11:39:56 +00:00
|
|
|
}
|
2010-10-24 01:31:08 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
data = imsg->data;
|
|
|
|
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
|
|
|
|
|
|
|
|
switch (imsg->hdr.type) {
|
|
|
|
case MSG_EXIT:
|
|
|
|
case MSG_SHUTDOWN:
|
|
|
|
if (datalen != sizeof retval && datalen != 0)
|
|
|
|
fatalx("bad MSG_EXIT size");
|
|
|
|
if (datalen == sizeof retval) {
|
|
|
|
memcpy(&retval, data, sizeof retval);
|
|
|
|
client_exitval = retval;
|
|
|
|
}
|
2019-12-13 06:55:12 +00:00
|
|
|
client_exitflag = 1;
|
|
|
|
client_exit();
|
2015-10-27 13:23:24 +00:00
|
|
|
break;
|
|
|
|
case MSG_READY:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_READY size");
|
2012-05-22 20:56:35 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
client_attached = 1;
|
|
|
|
proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case MSG_VERSION:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_VERSION size");
|
|
|
|
|
|
|
|
fprintf(stderr, "protocol version mismatch "
|
|
|
|
"(client %d, server %u)\n", PROTOCOL_VERSION,
|
2015-10-31 13:43:38 +00:00
|
|
|
imsg->hdr.peerid & 0xff);
|
2015-10-27 13:23:24 +00:00
|
|
|
client_exitval = 1;
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
|
|
|
case MSG_SHELL:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_SHELL string");
|
|
|
|
|
2017-07-12 09:21:25 +00:00
|
|
|
client_exec(data, shell_command);
|
2015-10-27 13:23:24 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
case MSG_DETACH:
|
|
|
|
case MSG_DETACHKILL:
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
2019-12-12 11:39:56 +00:00
|
|
|
case MSG_READ_OPEN:
|
|
|
|
client_read_open(data, datalen);
|
|
|
|
break;
|
|
|
|
case MSG_WRITE_OPEN:
|
|
|
|
client_write_open(data, datalen);
|
|
|
|
break;
|
|
|
|
case MSG_WRITE:
|
|
|
|
client_write_data(data, datalen);
|
|
|
|
break;
|
|
|
|
case MSG_WRITE_CLOSE:
|
|
|
|
client_write_close(data, datalen);
|
|
|
|
break;
|
2020-01-29 16:22:32 +00:00
|
|
|
case MSG_OLDSTDERR:
|
|
|
|
case MSG_OLDSTDIN:
|
|
|
|
case MSG_OLDSTDOUT:
|
|
|
|
fprintf(stderr, "server version is too old for client\n");
|
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
2010-10-24 01:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispatch imsgs in attached state (after MSG_READY). */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-10-27 13:23:24 +00:00
|
|
|
client_dispatch_attached(struct imsg *imsg)
|
2009-07-30 20:57:39 +00:00
|
|
|
{
|
2013-10-05 23:28:35 +00:00
|
|
|
struct sigaction sigact;
|
|
|
|
char *data;
|
2015-10-27 13:23:24 +00:00
|
|
|
ssize_t datalen;
|
2009-08-14 21:04:04 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
data = imsg->data;
|
|
|
|
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
switch (imsg->hdr.type) {
|
|
|
|
case MSG_DETACH:
|
|
|
|
case MSG_DETACHKILL:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_DETACH string");
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
client_exitsession = xstrdup(data);
|
|
|
|
client_exittype = imsg->hdr.type;
|
|
|
|
if (imsg->hdr.type == MSG_DETACHKILL)
|
|
|
|
client_exitreason = CLIENT_EXIT_DETACHED_HUP;
|
|
|
|
else
|
|
|
|
client_exitreason = CLIENT_EXIT_DETACHED;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
2017-01-13 10:12:12 +00:00
|
|
|
case MSG_EXEC:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0' ||
|
|
|
|
strlen(data) + 1 == (size_t)datalen)
|
|
|
|
fatalx("bad MSG_EXEC string");
|
|
|
|
client_execcmd = xstrdup(data);
|
|
|
|
client_execshell = xstrdup(data + strlen(data) + 1);
|
|
|
|
|
|
|
|
client_exittype = imsg->hdr.type;
|
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
break;
|
2015-10-27 13:23:24 +00:00
|
|
|
case MSG_EXIT:
|
|
|
|
if (datalen != 0 && datalen != sizeof (int))
|
|
|
|
fatalx("bad MSG_EXIT size");
|
2009-07-30 20:57:39 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
client_exitreason = CLIENT_EXIT_EXITED;
|
|
|
|
break;
|
|
|
|
case MSG_EXITED:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_EXITED size");
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_exit(client_proc);
|
|
|
|
break;
|
|
|
|
case MSG_SHUTDOWN:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_SHUTDOWN size");
|
2009-08-14 21:04:04 +00:00
|
|
|
|
2015-10-27 13:23:24 +00:00
|
|
|
proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
|
|
|
|
client_exitreason = CLIENT_EXIT_SERVER_EXITED;
|
|
|
|
client_exitval = 1;
|
|
|
|
break;
|
|
|
|
case MSG_SUSPEND:
|
|
|
|
if (datalen != 0)
|
|
|
|
fatalx("bad MSG_SUSPEND size");
|
|
|
|
|
|
|
|
memset(&sigact, 0, sizeof sigact);
|
|
|
|
sigemptyset(&sigact.sa_mask);
|
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigact.sa_handler = SIG_DFL;
|
|
|
|
if (sigaction(SIGTSTP, &sigact, NULL) != 0)
|
|
|
|
fatal("sigaction failed");
|
|
|
|
kill(getpid(), SIGTSTP);
|
|
|
|
break;
|
|
|
|
case MSG_LOCK:
|
|
|
|
if (datalen == 0 || data[datalen - 1] != '\0')
|
|
|
|
fatalx("bad MSG_LOCK string");
|
|
|
|
|
|
|
|
system(data);
|
|
|
|
proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
|
|
|
|
break;
|
2009-07-30 20:57:39 +00:00
|
|
|
}
|
|
|
|
}
|