2013-03-20 05:24:17 +00:00
|
|
|
/*
|
|
|
|
* The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Rafal Wojtczuk <rafal@invisiblethingslab.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
2013-12-27 04:38:16 +00:00
|
|
|
#include <signal.h>
|
2013-03-20 05:24:17 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <errno.h>
|
2013-12-27 04:38:16 +00:00
|
|
|
#include <assert.h>
|
2013-03-20 05:24:17 +00:00
|
|
|
#include "qrexec.h"
|
|
|
|
#include "libqrexec-utils.h"
|
|
|
|
|
2014-04-15 01:12:53 +00:00
|
|
|
// whether qrexec-client should replace ESC with _ before printing the output
|
|
|
|
int replace_esc_stdout = 0;
|
|
|
|
int replace_esc_stderr = 0;
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
#define VCHAN_BUFFER_SIZE 65536
|
|
|
|
|
|
|
|
int local_stdin_fd, local_stdout_fd;
|
|
|
|
pid_t local_pid = 0;
|
|
|
|
/* flag if this is "remote" end of service call. In this case swap STDIN/STDOUT
|
|
|
|
* msg types and send exit code at the end */
|
|
|
|
int is_service = 0;
|
|
|
|
int child_exited = 0;
|
|
|
|
|
|
|
|
static int handle_agent_handshake(libvchan_t *vchan, int remote_send_first)
|
|
|
|
{
|
|
|
|
struct msg_header hdr;
|
|
|
|
struct peer_info info;
|
|
|
|
int who = 0; // even - send to remote, odd - receive from remote
|
|
|
|
|
|
|
|
while (who < 2) {
|
|
|
|
if ((who+remote_send_first) & 1) {
|
|
|
|
if (!read_vchan_all(vchan, &hdr, sizeof(hdr))) {
|
|
|
|
perror("daemon handshake");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (hdr.type != MSG_HELLO || hdr.len != sizeof(info)) {
|
|
|
|
fprintf(stderr, "Invalid daemon MSG_HELLO\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!read_vchan_all(vchan, &info, sizeof(info))) {
|
|
|
|
perror("daemon handshake");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.version != QREXEC_PROTOCOL_VERSION) {
|
|
|
|
fprintf(stderr, "Incompatible daemon protocol version "
|
|
|
|
"(daemon %d, client %d)\n",
|
|
|
|
info.version, QREXEC_PROTOCOL_VERSION);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hdr.type = MSG_HELLO;
|
|
|
|
hdr.len = sizeof(info);
|
|
|
|
info.version = QREXEC_PROTOCOL_VERSION;
|
|
|
|
|
|
|
|
if (!write_vchan_all(vchan, &hdr, sizeof(hdr))) {
|
|
|
|
fprintf(stderr, "Failed to send MSG_HELLO hdr to daemon\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!write_vchan_all(vchan, &info, sizeof(info))) {
|
|
|
|
fprintf(stderr, "Failed to send MSG_HELLO to daemon\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
who++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_daemon_handshake(int fd)
|
|
|
|
{
|
|
|
|
struct msg_header hdr;
|
|
|
|
struct peer_info info;
|
|
|
|
|
|
|
|
/* daemon send MSG_HELLO first */
|
|
|
|
if (!read_all(fd, &hdr, sizeof(hdr))) {
|
|
|
|
perror("daemon handshake");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (hdr.type != MSG_HELLO || hdr.len != sizeof(info)) {
|
|
|
|
fprintf(stderr, "Invalid daemon MSG_HELLO\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!read_all(fd, &info, sizeof(info))) {
|
|
|
|
perror("daemon handshake");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.version != QREXEC_PROTOCOL_VERSION) {
|
|
|
|
fprintf(stderr, "Incompatible daemon protocol version "
|
|
|
|
"(daemon %d, client %d)\n",
|
|
|
|
info.version, QREXEC_PROTOCOL_VERSION);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr.type = MSG_HELLO;
|
|
|
|
hdr.len = sizeof(info);
|
|
|
|
info.version = QREXEC_PROTOCOL_VERSION;
|
|
|
|
|
|
|
|
if (!write_all(fd, &hdr, sizeof(hdr))) {
|
|
|
|
fprintf(stderr, "Failed to send MSG_HELLO hdr to daemon\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!write_all(fd, &info, sizeof(info))) {
|
|
|
|
fprintf(stderr, "Failed to send MSG_HELLO to daemon\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connect_unix_socket(const char *domname)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
int s, len;
|
|
|
|
struct sockaddr_un remote;
|
2013-03-20 05:24:17 +00:00
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
perror("socket");
|
|
|
|
return -1;
|
|
|
|
}
|
2013-03-20 05:24:17 +00:00
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
remote.sun_family = AF_UNIX;
|
|
|
|
snprintf(remote.sun_path, sizeof remote.sun_path,
|
|
|
|
QREXEC_DAEMON_SOCKET_DIR "/qrexec.%s", domname);
|
|
|
|
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
|
|
|
|
if (connect(s, (struct sockaddr *) &remote, len) == -1) {
|
|
|
|
perror("connect");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
if (handle_daemon_handshake(s) < 0)
|
|
|
|
exit(1);
|
2013-12-27 04:31:11 +00:00
|
|
|
return s;
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2014-04-23 02:55:29 +00:00
|
|
|
static void sigchld_handler(int x __attribute__((__unused__)))
|
2013-12-27 04:38:16 +00:00
|
|
|
{
|
|
|
|
child_exited = 1;
|
|
|
|
signal(SIGCHLD, sigchld_handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called from do_fork_exec */
|
2013-12-30 11:36:36 +00:00
|
|
|
void do_exec(const char *prog)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
execl("/bin/bash", "bash", "-c", prog, NULL);
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void do_exit(int code)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
int status;
|
|
|
|
// sever communication lines; wait for child, if any
|
|
|
|
// so that qrexec-daemon can count (recursively) spawned processes correctly
|
|
|
|
close(local_stdin_fd);
|
|
|
|
close(local_stdout_fd);
|
|
|
|
waitpid(-1, &status, 0);
|
|
|
|
exit(code);
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void prepare_local_fds(char *cmdline)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
if (!cmdline) {
|
|
|
|
local_stdin_fd = 1;
|
|
|
|
local_stdout_fd = 0;
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
signal(SIGCHLD, sigchld_handler);
|
|
|
|
do_fork_exec(cmdline, &local_pid, &local_stdin_fd, &local_stdout_fd,
|
2013-12-27 04:31:11 +00:00
|
|
|
NULL);
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
/* ask the daemon to allocate vchan port */
|
2014-04-23 02:55:29 +00:00
|
|
|
static void negotiate_connection_params(int s, int other_domid, unsigned type,
|
2013-12-27 04:38:16 +00:00
|
|
|
void *cmdline_param, int cmdline_size,
|
|
|
|
int *data_domain, int *data_port)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:38:16 +00:00
|
|
|
struct msg_header hdr;
|
|
|
|
struct exec_params params;
|
2013-12-27 04:31:11 +00:00
|
|
|
hdr.type = type;
|
2013-12-27 04:38:16 +00:00
|
|
|
hdr.len = sizeof(params) + cmdline_size;
|
|
|
|
params.connect_domain = other_domid;
|
|
|
|
params.connect_port = 0;
|
|
|
|
if (!write_all(s, &hdr, sizeof(hdr))
|
|
|
|
|| !write_all(s, ¶ms, sizeof(params))
|
|
|
|
|| !write_all(s, cmdline_param, cmdline_size)) {
|
|
|
|
perror("write daemon");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
/* the daemon will respond with the same message with connect_port filled
|
|
|
|
* and empty cmdline */
|
|
|
|
if (!read_all(s, &hdr, sizeof(hdr))) {
|
|
|
|
perror("read daemon");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
assert(hdr.type == type);
|
|
|
|
if (hdr.len != sizeof(params)) {
|
|
|
|
fprintf(stderr, "Invalid response for 0x%x\n", type);
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
if (!read_all(s, ¶ms, sizeof(params))) {
|
|
|
|
perror("read daemon");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
*data_port = params.connect_port;
|
|
|
|
*data_domain = params.connect_domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_service_connect(int s, char *conn_ident,
|
|
|
|
int connect_domain, int connect_port)
|
|
|
|
{
|
|
|
|
struct msg_header hdr;
|
|
|
|
struct exec_params exec_params;
|
|
|
|
struct service_params srv_params;
|
|
|
|
|
|
|
|
hdr.type = MSG_SERVICE_CONNECT;
|
|
|
|
hdr.len = sizeof(exec_params) + sizeof(srv_params);
|
|
|
|
|
|
|
|
exec_params.connect_domain = connect_domain;
|
|
|
|
exec_params.connect_port = connect_port;
|
|
|
|
strncpy(srv_params.ident, conn_ident, sizeof(srv_params.ident));
|
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
if (!write_all(s, &hdr, sizeof(hdr))
|
2013-12-27 04:38:16 +00:00
|
|
|
|| !write_all(s, &exec_params, sizeof(exec_params))
|
|
|
|
|| !write_all(s, &srv_params, sizeof(srv_params))) {
|
2013-12-27 04:31:11 +00:00
|
|
|
perror("write daemon");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void send_exit_code(libvchan_t *vchan, int status)
|
|
|
|
{
|
|
|
|
struct msg_header hdr;
|
|
|
|
|
|
|
|
hdr.type = MSG_DATA_EXIT_CODE;
|
|
|
|
hdr.len = sizeof(int);
|
2014-04-23 02:55:29 +00:00
|
|
|
if (libvchan_send(vchan, &hdr, sizeof(hdr)) != sizeof(hdr)) {
|
2013-12-27 04:38:16 +00:00
|
|
|
fprintf(stderr, "Failed to write exit code to the agent\n");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2014-04-23 02:55:29 +00:00
|
|
|
if (libvchan_send(vchan, &status, sizeof(status)) != sizeof(status)) {
|
2013-12-27 04:38:16 +00:00
|
|
|
fprintf(stderr, "Failed to write exit code(2) to the agent\n");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_input(libvchan_t *vchan)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
char buf[MAX_DATA_CHUNK];
|
|
|
|
int ret;
|
2013-12-27 04:38:16 +00:00
|
|
|
struct msg_header hdr;
|
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
ret = read(local_stdout_fd, buf, sizeof(buf));
|
|
|
|
if (ret < 0) {
|
|
|
|
perror("read");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
hdr.type = is_service ? MSG_DATA_STDOUT : MSG_DATA_STDIN;
|
|
|
|
hdr.len = ret;
|
2014-04-23 02:55:29 +00:00
|
|
|
if (libvchan_send(vchan, &hdr, sizeof(hdr)) != sizeof(hdr)) {
|
2013-12-27 04:38:16 +00:00
|
|
|
fprintf(stderr, "Failed to write STDIN data to the agent\n");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2013-12-27 04:31:11 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
close(local_stdout_fd);
|
|
|
|
local_stdout_fd = -1;
|
|
|
|
if (local_stdin_fd == -1) {
|
2015-02-17 02:57:17 +00:00
|
|
|
// if not a remote end of service call, wait for exit status
|
|
|
|
if (is_service) {
|
|
|
|
// if pipe in opposite direction already closed, no need to stay alive
|
|
|
|
if (local_pid == 0) {
|
|
|
|
/* if this is "remote" service end and no real local process
|
|
|
|
* exists (using own stdin/out) send also fake exit code */
|
|
|
|
send_exit_code(vchan, 0);
|
2015-03-17 21:55:15 +00:00
|
|
|
do_exit(0);
|
2015-02-17 02:57:17 +00:00
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
}
|
2013-12-27 04:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
if (!write_vchan_all(vchan, buf, ret)) {
|
|
|
|
if (!libvchan_is_open(vchan)) {
|
|
|
|
// agent disconnected its end of socket, so no future data will be
|
2013-12-27 04:31:11 +00:00
|
|
|
// send there; there is no sense to read from child stdout
|
|
|
|
//
|
2013-12-27 04:38:16 +00:00
|
|
|
// since vchan socket is buffered it doesn't mean all data was
|
2013-12-27 04:31:11 +00:00
|
|
|
// received from the agent
|
|
|
|
close(local_stdout_fd);
|
|
|
|
local_stdout_fd = -1;
|
|
|
|
if (local_stdin_fd == -1) {
|
|
|
|
// since child does no longer accept data on its stdin, doesn't
|
|
|
|
// make sense to process the data from the daemon
|
|
|
|
//
|
|
|
|
// we don't know real exit VM process code (exiting here, before
|
2013-12-27 04:38:16 +00:00
|
|
|
// MSG_DATA_EXIT_CODE message)
|
2013-12-27 04:31:11 +00:00
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
} else
|
2013-12-27 04:38:16 +00:00
|
|
|
perror("write agent");
|
2013-12-27 04:31:11 +00:00
|
|
|
}
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 01:12:53 +00:00
|
|
|
void do_replace_esc(char *buf, int len) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
if (buf[i] == '\033')
|
|
|
|
buf[i] = '_';
|
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void handle_vchan_data(libvchan_t *vchan)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
int status;
|
2013-12-27 04:38:16 +00:00
|
|
|
struct msg_header hdr;
|
|
|
|
char buf[MAX_DATA_CHUNK];
|
2013-03-20 05:24:17 +00:00
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
if (libvchan_recv(vchan, &hdr, sizeof hdr) < 0) {
|
|
|
|
perror("read vchan");
|
2013-12-27 04:31:11 +00:00
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
if (hdr.len > MAX_DATA_CHUNK) {
|
|
|
|
fprintf(stderr, "client_header.len=%d\n", hdr.len);
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
if (!read_vchan_all(vchan, buf, hdr.len)) {
|
2013-12-27 04:31:11 +00:00
|
|
|
perror("read daemon");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2013-03-20 05:24:17 +00:00
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
switch (hdr.type) {
|
2013-12-27 04:38:16 +00:00
|
|
|
/* both directions because we can serve as either end of service call */
|
|
|
|
case MSG_DATA_STDIN:
|
|
|
|
case MSG_DATA_STDOUT:
|
2013-12-27 04:31:11 +00:00
|
|
|
if (local_stdin_fd == -1)
|
|
|
|
break;
|
2013-12-27 04:38:16 +00:00
|
|
|
if (replace_esc_stdout)
|
|
|
|
do_replace_esc(buf, hdr.len);
|
2013-12-27 04:31:11 +00:00
|
|
|
if (hdr.len == 0) {
|
|
|
|
close(local_stdin_fd);
|
|
|
|
local_stdin_fd = -1;
|
|
|
|
} else if (!write_all(local_stdin_fd, buf, hdr.len)) {
|
|
|
|
if (errno == EPIPE) {
|
|
|
|
// remote side have closed its stdin, handle data in oposite
|
|
|
|
// direction (if any) before exit
|
|
|
|
local_stdin_fd = -1;
|
|
|
|
} else {
|
|
|
|
perror("write local stdout");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-12-27 04:38:16 +00:00
|
|
|
case MSG_DATA_STDERR:
|
2013-12-27 04:31:11 +00:00
|
|
|
if (replace_esc_stderr)
|
|
|
|
do_replace_esc(buf, hdr.len);
|
|
|
|
write_all(2, buf, hdr.len);
|
|
|
|
break;
|
2013-12-27 04:38:16 +00:00
|
|
|
case MSG_DATA_EXIT_CODE:
|
|
|
|
libvchan_close(vchan);
|
2015-03-20 11:16:19 +00:00
|
|
|
if (hdr.len < sizeof(status))
|
|
|
|
status = 255;
|
|
|
|
else
|
|
|
|
memcpy(&status, buf, sizeof(status));
|
|
|
|
|
2015-02-11 15:11:38 +00:00
|
|
|
do_exit(status);
|
2013-12-27 04:31:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "unknown msg %d\n", hdr.type);
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void check_child_status(libvchan_t *vchan)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:38:16 +00:00
|
|
|
pid_t pid;
|
|
|
|
int status;
|
2013-03-20 05:24:17 +00:00
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
pid = waitpid(local_pid, &status, WNOHANG);
|
|
|
|
if (pid < 0) {
|
|
|
|
perror("waitpid");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
if (pid == 0 || !WIFEXITED(status))
|
|
|
|
return;
|
|
|
|
if (is_service)
|
|
|
|
send_exit_code(vchan, WEXITSTATUS(status));
|
|
|
|
do_exit(status);
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void select_loop(libvchan_t *vchan)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
fd_set select_set;
|
2013-12-27 04:38:16 +00:00
|
|
|
int max_fd;
|
|
|
|
int ret;
|
|
|
|
int vchan_fd;
|
|
|
|
sigset_t selectmask;
|
|
|
|
struct timespec zero_timeout = { 0, 0 };
|
2015-02-22 17:25:14 +00:00
|
|
|
struct timespec select_timeout = { 10, 0 };
|
2013-12-27 04:38:16 +00:00
|
|
|
|
|
|
|
sigemptyset(&selectmask);
|
|
|
|
sigaddset(&selectmask, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &selectmask, NULL);
|
|
|
|
sigemptyset(&selectmask);
|
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
for (;;) {
|
2013-12-27 04:38:16 +00:00
|
|
|
vchan_fd = libvchan_fd_for_select(vchan);
|
2013-12-27 04:31:11 +00:00
|
|
|
FD_ZERO(&select_set);
|
2013-12-27 04:38:16 +00:00
|
|
|
FD_SET(vchan_fd, &select_set);
|
|
|
|
max_fd = vchan_fd;
|
|
|
|
if (local_stdout_fd != -1 && libvchan_buffer_space(vchan)) {
|
2013-12-27 04:31:11 +00:00
|
|
|
FD_SET(local_stdout_fd, &select_set);
|
2013-12-27 04:38:16 +00:00
|
|
|
if (local_stdout_fd > max_fd)
|
|
|
|
max_fd = local_stdout_fd;
|
2013-12-27 04:31:11 +00:00
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
if (child_exited)
|
|
|
|
check_child_status(vchan);
|
|
|
|
if (libvchan_data_ready(vchan) > 0) {
|
|
|
|
/* check for other FDs, but exit immediately */
|
|
|
|
ret = pselect(max_fd + 1, &select_set, NULL, NULL,
|
|
|
|
&zero_timeout, &selectmask);
|
|
|
|
} else
|
|
|
|
ret = pselect(max_fd + 1, &select_set, NULL, NULL,
|
2015-02-22 17:25:14 +00:00
|
|
|
&select_timeout, &selectmask);
|
2013-12-27 04:38:16 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EINTR && local_pid > 0) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
perror("select");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
2013-12-27 04:31:11 +00:00
|
|
|
}
|
2015-02-22 17:25:14 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
if (!libvchan_is_open(vchan)) {
|
|
|
|
/* remote disconnected witout a proper signaling */
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
if (FD_ISSET(vchan_fd, &select_set))
|
|
|
|
libvchan_wait(vchan);
|
|
|
|
while (libvchan_data_ready(vchan))
|
|
|
|
handle_vchan_data(vchan);
|
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
if (local_stdout_fd != -1
|
|
|
|
&& FD_ISSET(local_stdout_fd, &select_set))
|
2013-12-27 04:38:16 +00:00
|
|
|
handle_input(vchan);
|
2013-12-27 04:31:11 +00:00
|
|
|
}
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void usage(char *name)
|
2013-03-20 05:24:17 +00:00
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
fprintf(stderr,
|
2013-12-27 04:38:16 +00:00
|
|
|
"usage: %s [-t] [-T] -d domain_name ["
|
|
|
|
"-l local_prog|"
|
|
|
|
"-c request_id,src_domain_name,src_domain_id|"
|
|
|
|
"-e] remote_cmdline\n"
|
|
|
|
"-e means exit after sending cmd,\n"
|
|
|
|
"-t enables replacing ESC character with '_' in command output, -T is the same for stderr\n"
|
|
|
|
"-c: connect to existing process (response to trigger service call)\n",
|
2013-12-27 04:31:11 +00:00
|
|
|
name);
|
|
|
|
exit(1);
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
static void parse_connect(char *str, char **request_id,
|
|
|
|
char **src_domain_name, int *src_domain_id)
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
char *token = NULL;
|
|
|
|
char *separators = ",";
|
|
|
|
|
|
|
|
token = strtok(str, separators);
|
|
|
|
while (token)
|
|
|
|
{
|
|
|
|
switch (i)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
*request_id = token;
|
|
|
|
if (strlen(*request_id) >= sizeof(struct service_params)) {
|
|
|
|
fprintf(stderr, "Invalid -c parameter (request_id too long, max %lu)\n",
|
|
|
|
sizeof(struct service_params)-1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*src_domain_name = token;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*src_domain_id = atoi(token);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Invalid -c parameter (should be: \"-c request_id,src_domain_name,src_domain_id\")\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
token = strtok(NULL, separators);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-20 05:24:17 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-12-27 04:31:11 +00:00
|
|
|
int opt;
|
|
|
|
char *domname = NULL;
|
2013-12-27 04:38:16 +00:00
|
|
|
libvchan_t *data_vchan = NULL;
|
|
|
|
int data_port;
|
|
|
|
int data_domain;
|
|
|
|
int msg_type;
|
2013-12-27 04:31:11 +00:00
|
|
|
int s;
|
|
|
|
int just_exec = 0;
|
|
|
|
int connect_existing = 0;
|
|
|
|
char *local_cmdline = NULL;
|
2013-12-27 04:38:16 +00:00
|
|
|
char *remote_cmdline = NULL;
|
|
|
|
char *request_id;
|
2015-03-20 11:16:19 +00:00
|
|
|
char *src_domain_name = NULL;
|
2013-12-27 04:38:16 +00:00
|
|
|
int src_domain_id = 0; /* if not -c given, the process is run in dom0 */
|
|
|
|
struct service_params svc_params;
|
|
|
|
while ((opt = getopt(argc, argv, "d:l:ec:tT")) != -1) {
|
2013-12-27 04:31:11 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
|
|
|
domname = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
local_cmdline = strdup(optarg);
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
just_exec = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
2013-12-27 04:38:16 +00:00
|
|
|
parse_connect(optarg, &request_id, &src_domain_name, &src_domain_id);
|
2013-12-27 04:31:11 +00:00
|
|
|
connect_existing = 1;
|
2013-12-27 04:38:16 +00:00
|
|
|
is_service = 1;
|
2013-12-27 04:31:11 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
replace_esc_stdout = 1;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
replace_esc_stderr = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (optind >= argc || !domname)
|
|
|
|
usage(argv[0]);
|
2013-12-27 04:38:16 +00:00
|
|
|
remote_cmdline = argv[optind];
|
2013-03-20 05:24:17 +00:00
|
|
|
|
2013-12-27 04:31:11 +00:00
|
|
|
register_exec_func(&do_exec);
|
2014-02-07 04:35:24 +00:00
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
if (just_exec + connect_existing + (local_cmdline != 0) > 1) {
|
|
|
|
fprintf(stderr, "ERROR: only one of -e, -l, -c can be specified\n");
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(domname, "dom0") == 0 && !connect_existing) {
|
|
|
|
fprintf(stderr, "ERROR: when target domain is 'dom0', -c must be specified\n");
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(domname, "dom0") == 0) {
|
|
|
|
if (connect_existing) {
|
|
|
|
msg_type = MSG_SERVICE_CONNECT;
|
|
|
|
strncpy(svc_params.ident, request_id, sizeof(svc_params.ident));
|
|
|
|
} else if (just_exec)
|
|
|
|
msg_type = MSG_JUST_EXEC;
|
|
|
|
else
|
|
|
|
msg_type = MSG_EXEC_CMDLINE;
|
2015-03-20 11:16:19 +00:00
|
|
|
assert(src_domain_name);
|
2013-12-27 04:38:16 +00:00
|
|
|
setenv("QREXEC_REMOTE_DOMAIN", src_domain_name, 1);
|
|
|
|
s = connect_unix_socket(src_domain_name);
|
|
|
|
negotiate_connection_params(s,
|
|
|
|
0, /* dom0 */
|
|
|
|
msg_type,
|
|
|
|
connect_existing ? (void*)&svc_params : (void*)remote_cmdline,
|
|
|
|
connect_existing ? sizeof(svc_params) : strlen(remote_cmdline) + 1,
|
|
|
|
&data_domain,
|
|
|
|
&data_port);
|
2013-03-20 05:24:17 +00:00
|
|
|
|
2013-12-27 04:38:16 +00:00
|
|
|
prepare_local_fds(remote_cmdline);
|
2013-12-27 04:31:11 +00:00
|
|
|
if (connect_existing)
|
2013-12-27 04:38:16 +00:00
|
|
|
data_vchan = libvchan_client_init(data_domain, data_port);
|
|
|
|
else {
|
|
|
|
data_vchan = libvchan_server_init(data_domain, data_port,
|
|
|
|
VCHAN_BUFFER_SIZE, VCHAN_BUFFER_SIZE);
|
|
|
|
while (data_vchan && !libvchan_is_open(data_vchan))
|
|
|
|
libvchan_wait(data_vchan);
|
|
|
|
}
|
|
|
|
if (!data_vchan) {
|
|
|
|
fprintf(stderr, "Failed to open data vchan connection\n");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
if (handle_agent_handshake(data_vchan, connect_existing) < 0)
|
|
|
|
do_exit(1);
|
|
|
|
select_loop(data_vchan);
|
|
|
|
} else {
|
|
|
|
if (just_exec)
|
|
|
|
msg_type = MSG_JUST_EXEC;
|
2013-12-27 04:31:11 +00:00
|
|
|
else
|
2013-12-27 04:38:16 +00:00
|
|
|
msg_type = MSG_EXEC_CMDLINE;
|
|
|
|
s = connect_unix_socket(domname);
|
|
|
|
negotiate_connection_params(s,
|
|
|
|
src_domain_id,
|
|
|
|
msg_type,
|
|
|
|
remote_cmdline,
|
|
|
|
strlen(remote_cmdline) + 1,
|
|
|
|
&data_domain,
|
|
|
|
&data_port);
|
|
|
|
close(s);
|
|
|
|
setenv("QREXEC_REMOTE_DOMAIN", domname, 1);
|
|
|
|
prepare_local_fds(local_cmdline);
|
|
|
|
if (connect_existing) {
|
|
|
|
s = connect_unix_socket(src_domain_name);
|
|
|
|
send_service_connect(s, request_id, data_domain, data_port);
|
|
|
|
close(s);
|
|
|
|
} else {
|
|
|
|
data_vchan = libvchan_server_init(data_domain, data_port,
|
|
|
|
VCHAN_BUFFER_SIZE, VCHAN_BUFFER_SIZE);
|
|
|
|
if (!data_vchan) {
|
|
|
|
fprintf(stderr, "Failed to start data vchan server\n");
|
|
|
|
do_exit(1);
|
|
|
|
}
|
|
|
|
while (!libvchan_is_open(data_vchan))
|
|
|
|
libvchan_wait(data_vchan);
|
|
|
|
if (handle_agent_handshake(data_vchan, 0) < 0)
|
|
|
|
do_exit(1);
|
|
|
|
select_loop(data_vchan);
|
|
|
|
}
|
2013-12-27 04:31:11 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2013-03-20 05:24:17 +00:00
|
|
|
}
|
2013-12-27 04:38:16 +00:00
|
|
|
|
|
|
|
// vim:ts=4:sw=4:et:
|