2013-03-20 06:21:16 +01: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/select.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <sys/stat.h>
|
2013-12-27 06:07:33 +01:00
|
|
|
#include <assert.h>
|
2013-03-20 06:21:16 +01:00
|
|
|
#include "qrexec.h"
|
2013-04-22 05:21:52 +02:00
|
|
|
#include <libvchan.h>
|
2013-03-20 06:21:16 +01:00
|
|
|
#include "libqrexec-utils.h"
|
2013-12-27 06:07:33 +01:00
|
|
|
#include "qrexec-agent.h"
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
struct _connection_info {
|
2013-12-27 06:06:12 +01:00
|
|
|
int pid;
|
2013-12-27 06:07:33 +01:00
|
|
|
int connect_domain;
|
|
|
|
int connect_port;
|
2013-03-20 06:21:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
int max_process_fd = -1;
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
/* */
|
|
|
|
struct _connection_info connection_info[MAX_FDS];
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
libvchan_t *ctrl_vchan;
|
2013-04-22 05:21:52 +02:00
|
|
|
|
2013-03-20 06:21:16 +01:00
|
|
|
int trigger_fd;
|
|
|
|
int passfd_socket;
|
|
|
|
|
|
|
|
int meminfo_write_started = 0;
|
|
|
|
|
2015-02-17 04:06:19 +01:00
|
|
|
void no_colon_in_cmd()
|
2013-12-27 06:07:33 +01:00
|
|
|
{
|
2015-02-17 04:06:19 +01:00
|
|
|
fprintf(stderr,
|
|
|
|
"cmdline is supposed to be in user:command form\n");
|
2013-12-27 06:06:12 +01:00
|
|
|
exit(1);
|
2013-04-22 05:21:52 +02:00
|
|
|
}
|
2014-02-07 05:32:57 +01:00
|
|
|
|
2015-02-17 04:06:19 +01:00
|
|
|
void do_exec(const char *cmd)
|
2013-12-27 06:07:33 +01:00
|
|
|
{
|
2015-02-17 04:06:19 +01:00
|
|
|
char buf[strlen(QUBES_RPC_MULTIPLEXER_PATH) + strlen(cmd) - strlen(RPC_REQUEST_COMMAND) + 1];
|
|
|
|
char *realcmd = index(cmd, ':');
|
|
|
|
if (!realcmd)
|
|
|
|
no_colon_in_cmd();
|
|
|
|
/* mark end of username and move to command */
|
|
|
|
*realcmd = 0;
|
|
|
|
realcmd++;
|
|
|
|
/* ignore "nogui:" prefix in linux agent */
|
|
|
|
if (strncmp(realcmd, "nogui:", 6) == 0)
|
|
|
|
realcmd+=6;
|
|
|
|
/* replace magic RPC cmd with RPC multiplexer path */
|
|
|
|
if (strncmp(realcmd, RPC_REQUEST_COMMAND " ", strlen(RPC_REQUEST_COMMAND)+1)==0) {
|
|
|
|
strcpy(buf, QUBES_RPC_MULTIPLEXER_PATH);
|
|
|
|
strcpy(buf + strlen(QUBES_RPC_MULTIPLEXER_PATH), realcmd + strlen(RPC_REQUEST_COMMAND));
|
|
|
|
realcmd = buf;
|
2013-12-27 06:07:33 +01:00
|
|
|
}
|
2015-02-17 04:06:19 +01:00
|
|
|
signal(SIGCHLD, SIG_DFL);
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
2013-12-27 06:07:33 +01:00
|
|
|
|
2015-02-17 04:06:19 +01:00
|
|
|
execl("/bin/su", "su", "-", cmd, "-c", realcmd, NULL);
|
|
|
|
perror("execl");
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-12-27 06:07:33 +01:00
|
|
|
|
2015-02-17 04:06:19 +01:00
|
|
|
void handle_vchan_error(const char *op)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error while vchan %s, exiting\n", op);
|
|
|
|
exit(1);
|
2013-12-27 06:07:33 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 06:21:16 +01:00
|
|
|
void init()
|
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
/* FIXME: This 0 is remote domain ID */
|
2013-12-27 06:07:33 +01:00
|
|
|
ctrl_vchan = libvchan_server_init(0, VCHAN_BASE_PORT, 4096, 4096);
|
|
|
|
if (!ctrl_vchan)
|
2013-12-27 06:06:12 +01:00
|
|
|
handle_vchan_error("server_init");
|
2013-12-27 06:07:33 +01:00
|
|
|
if (handle_handshake(ctrl_vchan) < 0)
|
|
|
|
exit(1);
|
2013-12-27 06:06:12 +01:00
|
|
|
umask(0);
|
|
|
|
mkfifo(QREXEC_AGENT_TRIGGER_PATH, 0666);
|
|
|
|
passfd_socket = get_server_socket(QREXEC_AGENT_FDPASS_PATH);
|
|
|
|
umask(077);
|
|
|
|
trigger_fd =
|
|
|
|
open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
|
2014-06-26 01:20:13 +02:00
|
|
|
register_exec_func(do_exec);
|
2013-12-27 06:06:12 +01:00
|
|
|
|
|
|
|
/* wait for qrexec daemon */
|
2013-12-27 06:07:33 +01:00
|
|
|
while (!libvchan_is_open(ctrl_vchan))
|
|
|
|
libvchan_wait(ctrl_vchan);
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
void wake_meminfo_writer()
|
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
FILE *f;
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
if (meminfo_write_started)
|
|
|
|
/* wake meminfo-writer only once */
|
|
|
|
return;
|
|
|
|
|
|
|
|
f = fopen(MEMINFO_WRITER_PIDFILE, "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
/* no meminfo-writer found, ignoring */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fscanf(f, "%d", &pid) < 1) {
|
|
|
|
fclose(f);
|
|
|
|
/* no meminfo-writer found, ignoring */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
if (pid <= 1 || pid > 0xffff) {
|
|
|
|
/* check within acceptable range */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (kill(pid, SIGUSR1) < 0) {
|
|
|
|
/* Can't send signal */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
meminfo_write_started = 1;
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
void register_vchan_connection(pid_t pid, int domain, int port)
|
2013-03-20 06:21:16 +01:00
|
|
|
{
|
2013-12-27 06:07:33 +01:00
|
|
|
int i;
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
for (i = 0; i < MAX_FDS; i++) {
|
|
|
|
if (connection_info[i].pid == 0) {
|
|
|
|
connection_info[i].pid = pid;
|
|
|
|
connection_info[i].connect_domain = domain;
|
|
|
|
connection_info[i].connect_port = port;
|
|
|
|
return;
|
|
|
|
}
|
2013-12-27 06:06:12 +01:00
|
|
|
}
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
fprintf(stderr, "No free slot for child %d (connection to %d:%d)\n", pid, domain, port);
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
void handle_server_exec_request(struct msg_header *hdr)
|
2013-03-20 06:21:16 +01:00
|
|
|
{
|
2013-12-27 06:07:33 +01:00
|
|
|
struct exec_params params;
|
|
|
|
char buf[hdr->len-sizeof(params)];
|
|
|
|
pid_t child_agent;
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
assert(hdr->len >= sizeof(params));
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
if (libvchan_recv(ctrl_vchan, ¶ms, sizeof(params)) < 0)
|
|
|
|
handle_vchan_error("read exec params");
|
|
|
|
if (libvchan_recv(ctrl_vchan, buf, hdr->len-sizeof(params)) < 0)
|
|
|
|
handle_vchan_error("read exec cmd");
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
child_agent = handle_new_process(hdr->type,
|
|
|
|
params.connect_domain, params.connect_port,
|
|
|
|
buf, hdr->len-sizeof(params));
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
register_vchan_connection(child_agent,
|
|
|
|
params.connect_domain, params.connect_port);
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
void handle_service_refused(struct msg_header *hdr)
|
2013-03-20 06:21:16 +01:00
|
|
|
{
|
2013-12-27 06:07:33 +01:00
|
|
|
struct service_params params;
|
2013-12-27 06:06:12 +01:00
|
|
|
int stdin_fd, stdout_fd, stderr_fd;
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
if (hdr->len != sizeof(params)) {
|
|
|
|
fprintf(stderr, "Invalid msg 0x%x length (%d)\n", MSG_SERVICE_REFUSED, hdr->len);
|
|
|
|
exit(1);
|
2013-12-27 06:06:12 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
if (libvchan_recv(ctrl_vchan, ¶ms, sizeof(params)) < 0)
|
|
|
|
handle_vchan_error("read exec params");
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
sscanf(params.ident, "%d %d %d", &stdin_fd, &stdout_fd, &stderr_fd);
|
|
|
|
/* TODO: send some signal? some response? */
|
|
|
|
close(stdin_fd);
|
|
|
|
close(stdout_fd);
|
|
|
|
close(stderr_fd);
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
void handle_server_cmd()
|
2013-03-20 06:21:16 +01:00
|
|
|
{
|
2013-12-27 06:07:33 +01:00
|
|
|
struct msg_header s_hdr;
|
2013-03-20 06:21:16 +01:00
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
if (libvchan_recv(ctrl_vchan, &s_hdr, sizeof(s_hdr)) < 0)
|
2013-12-27 06:06:12 +01:00
|
|
|
handle_vchan_error("read s_hdr");
|
|
|
|
|
|
|
|
// fprintf(stderr, "got %x %x %x\n", s_hdr.type, s_hdr.client_id,
|
|
|
|
// s_hdr.len);
|
|
|
|
|
|
|
|
switch (s_hdr.type) {
|
2013-12-27 06:07:33 +01:00
|
|
|
case MSG_EXEC_CMDLINE:
|
|
|
|
case MSG_JUST_EXEC:
|
|
|
|
case MSG_SERVICE_CONNECT:
|
2013-12-27 06:06:12 +01:00
|
|
|
wake_meminfo_writer();
|
2013-12-27 06:07:33 +01:00
|
|
|
handle_server_exec_request(&s_hdr);
|
2013-12-27 06:06:12 +01:00
|
|
|
break;
|
2013-12-27 06:07:33 +01:00
|
|
|
case MSG_SERVICE_REFUSED:
|
|
|
|
handle_service_refused(&s_hdr);
|
2013-12-27 06:06:12 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "msg type from daemon is %d ?\n",
|
|
|
|
s_hdr.type);
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
volatile int child_exited;
|
|
|
|
|
2014-02-16 11:34:22 +01:00
|
|
|
void sigchld_handler(int x __attribute__((__unused__)))
|
2013-03-20 06:21:16 +01:00
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
child_exited = 1;
|
|
|
|
signal(SIGCHLD, sigchld_handler);
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
int find_connection(int pid)
|
2013-03-20 06:21:16 +01:00
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < MAX_FDS; i++)
|
2013-12-27 06:07:33 +01:00
|
|
|
if (connection_info[i].pid == pid)
|
2013-12-27 06:06:12 +01:00
|
|
|
return i;
|
|
|
|
return -1;
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-17 04:06:19 +01:00
|
|
|
void release_connection(int id) {
|
|
|
|
struct msg_header hdr;
|
|
|
|
struct exec_params params;
|
|
|
|
|
|
|
|
hdr.type = MSG_CONNECTION_TERMINATED;
|
|
|
|
hdr.len = sizeof(struct exec_params);
|
|
|
|
params.connect_domain = connection_info[id].connect_domain;
|
|
|
|
params.connect_port = connection_info[id].connect_port;
|
|
|
|
if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
|
|
|
|
handle_vchan_error("send");
|
|
|
|
if (libvchan_send(ctrl_vchan, ¶ms, sizeof(params)) < 0)
|
|
|
|
handle_vchan_error("send");
|
|
|
|
connection_info[id].pid = 0;
|
|
|
|
}
|
2013-03-20 06:21:16 +01:00
|
|
|
|
|
|
|
void reap_children()
|
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
int status;
|
|
|
|
int pid;
|
2013-12-27 06:07:33 +01:00
|
|
|
int id;
|
2013-12-27 06:06:12 +01:00
|
|
|
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
|
2013-12-27 06:07:33 +01:00
|
|
|
id = find_connection(pid);
|
|
|
|
if (id < 0)
|
2013-12-27 06:06:12 +01:00
|
|
|
continue;
|
2015-02-17 04:06:19 +01:00
|
|
|
release_connection(id);
|
2013-12-27 06:06:12 +01:00
|
|
|
}
|
|
|
|
child_exited = 0;
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int fill_fds_for_select(fd_set * rdset, fd_set * wrset)
|
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
int max = -1;
|
|
|
|
FD_ZERO(rdset);
|
|
|
|
FD_ZERO(wrset);
|
|
|
|
|
|
|
|
FD_SET(trigger_fd, rdset);
|
|
|
|
if (trigger_fd > max)
|
|
|
|
max = trigger_fd;
|
|
|
|
FD_SET(passfd_socket, rdset);
|
|
|
|
if (passfd_socket > max)
|
|
|
|
max = passfd_socket;
|
|
|
|
return max;
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_new_passfd()
|
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
int fd = do_accept(passfd_socket);
|
|
|
|
if (fd >= MAX_FDS) {
|
|
|
|
fprintf(stderr, "too many clients ?\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
// let client know what fd has been allocated
|
|
|
|
if (write(fd, &fd, sizeof(fd)) != sizeof(fd)) {
|
|
|
|
perror("write to client");
|
|
|
|
}
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_trigger_io()
|
|
|
|
{
|
2013-12-27 06:07:33 +01:00
|
|
|
struct msg_header hdr;
|
|
|
|
struct trigger_service_params params;
|
2013-12-27 06:06:12 +01:00
|
|
|
int ret;
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
hdr.len = sizeof(params);
|
2013-12-27 06:06:12 +01:00
|
|
|
ret = read(trigger_fd, ¶ms, sizeof(params));
|
|
|
|
if (ret == sizeof(params)) {
|
2013-12-27 06:07:33 +01:00
|
|
|
hdr.type = MSG_TRIGGER_SERVICE;
|
|
|
|
if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) < 0)
|
2013-12-27 06:06:12 +01:00
|
|
|
handle_vchan_error("write hdr");
|
2013-12-27 06:07:33 +01:00
|
|
|
if (libvchan_send(ctrl_vchan, ¶ms, sizeof(params)) < 0)
|
2013-12-27 06:06:12 +01:00
|
|
|
handle_vchan_error("write params");
|
|
|
|
}
|
|
|
|
// trigger_fd is nonblock - so no need to reopen
|
|
|
|
// not really, need to reopen at EOF
|
|
|
|
if (ret <= 0) {
|
|
|
|
close(trigger_fd);
|
|
|
|
trigger_fd =
|
|
|
|
open(QREXEC_AGENT_TRIGGER_PATH, O_RDONLY | O_NONBLOCK);
|
|
|
|
}
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2013-12-27 06:06:12 +01:00
|
|
|
fd_set rdset, wrset;
|
|
|
|
int max;
|
|
|
|
sigset_t chld_set;
|
|
|
|
|
|
|
|
init();
|
|
|
|
signal(SIGCHLD, sigchld_handler);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
sigemptyset(&chld_set);
|
|
|
|
sigaddset(&chld_set, SIGCHLD);
|
|
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
sigprocmask(SIG_BLOCK, &chld_set, NULL);
|
|
|
|
if (child_exited)
|
|
|
|
reap_children();
|
|
|
|
max = fill_fds_for_select(&rdset, &wrset);
|
2013-12-27 06:07:33 +01:00
|
|
|
if (libvchan_buffer_space(ctrl_vchan) <=
|
|
|
|
(int)sizeof(struct msg_header))
|
2013-12-27 06:06:12 +01:00
|
|
|
FD_ZERO(&rdset);
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
wait_for_vchan_or_argfd(ctrl_vchan, max, &rdset, &wrset);
|
2013-12-27 06:06:12 +01:00
|
|
|
sigprocmask(SIG_UNBLOCK, &chld_set, NULL);
|
|
|
|
|
|
|
|
if (FD_ISSET(passfd_socket, &rdset))
|
|
|
|
handle_new_passfd();
|
|
|
|
|
2013-12-27 06:07:33 +01:00
|
|
|
while (libvchan_data_ready(ctrl_vchan))
|
|
|
|
handle_server_cmd();
|
2013-12-27 06:06:12 +01:00
|
|
|
|
|
|
|
if (FD_ISSET(trigger_fd, &rdset))
|
|
|
|
handle_trigger_io();
|
|
|
|
}
|
2013-03-20 06:21:16 +01:00
|
|
|
}
|