2019-09-14 23:32:31 +02:00
|
|
|
#include <assert.h>
|
2017-05-23 03:24:15 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define QUBESD_SOCKET "/var/run/qubesd.sock"
|
|
|
|
|
|
|
|
void write_wrapper(int fd, char *data, size_t len) {
|
2017-05-29 06:47:40 +02:00
|
|
|
size_t written = 0;
|
2017-05-23 03:24:15 +02:00
|
|
|
int ret;
|
|
|
|
while (written < len) {
|
|
|
|
ret = write(fd, data+written, len-written);
|
|
|
|
if (ret == -1) {
|
|
|
|
perror("write");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
written += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
char *source_domain = getenv("QREXEC_REMOTE_DOMAIN");
|
2019-09-14 23:32:31 +02:00
|
|
|
char *target_domain = NULL;
|
|
|
|
char *target_domain_type = getenv("QREXEC_REQUESTED_TARGET_TYPE");
|
|
|
|
char *target_domain_name = getenv("QREXEC_REQUESTED_TARGET");
|
|
|
|
char *target_domain_keyword = getenv("QREXEC_REQUESTED_TARGET_KEYWORD");
|
2017-05-23 03:24:15 +02:00
|
|
|
char *service_name = strrchr(argv[0], '/');
|
|
|
|
int fd;
|
|
|
|
char buf[4096];
|
|
|
|
int read_ret;
|
|
|
|
struct sockaddr_un qubesd_addr = {
|
|
|
|
.sun_family = AF_UNIX,
|
|
|
|
.sun_path = QUBESD_SOCKET,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (service_name)
|
|
|
|
service_name++;
|
|
|
|
|
2019-09-14 23:32:31 +02:00
|
|
|
if (!source_domain || !target_domain_type || !service_name || argc > 2) {
|
2017-05-29 06:47:40 +02:00
|
|
|
fprintf(stderr, "Usage: %s [service-argument]\n", argv[0]);
|
2017-05-23 03:24:15 +02:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "Expected environment variables:\n");
|
|
|
|
fprintf(stderr, " - QREXEC_REMOTE_DOMAIN - source domain for the call\n");
|
2019-09-14 23:32:31 +02:00
|
|
|
fprintf(stderr, " - QREXEC_REQUESTED_TARGET_TYPE - target domain for the call\n");
|
2017-05-23 03:24:15 +02:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "Additionally, this program assumes being called with desired service name as argv[0] (use symlink)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
perror("socket");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr *)&qubesd_addr, sizeof(qubesd_addr)) == -1) {
|
|
|
|
perror("connect to qubesd");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-09-14 23:32:31 +02:00
|
|
|
if (strcmp(target_domain_type, "keyword") == 0) {
|
|
|
|
// translate @adminvm back to dom0
|
|
|
|
if (strcmp(target_domain_keyword, "adminvm") == 0)
|
|
|
|
target_domain = "dom0";
|
|
|
|
else if (strcmp(target_domain_keyword, "default") == 0)
|
|
|
|
target_domain = "";
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Unsupported keyword: %s", target_domain_keyword);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
target_domain = target_domain_name;
|
|
|
|
|
|
|
|
assert(target_domain);
|
|
|
|
|
2017-05-23 03:24:15 +02:00
|
|
|
// write parameters, including trailing zero as separator
|
|
|
|
write_wrapper(fd, source_domain, strlen(source_domain) + 1);
|
|
|
|
write_wrapper(fd, service_name, strlen(service_name) + 1);
|
|
|
|
write_wrapper(fd, target_domain, strlen(target_domain) + 1);
|
|
|
|
if (argc == 2)
|
|
|
|
write_wrapper(fd, argv[1], strlen(argv[1]) + 1);
|
|
|
|
else
|
|
|
|
// empty argument
|
|
|
|
write_wrapper(fd, "\0", 1);
|
|
|
|
|
|
|
|
// now, read from stdin and write it to qubesd
|
|
|
|
while ((read_ret = read(0, buf, sizeof(buf))) > 0)
|
|
|
|
write_wrapper(fd, buf, read_ret);
|
|
|
|
|
|
|
|
if (read_ret == -1) {
|
|
|
|
perror("read from stdin");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// end of request, now let qubesd execute the action and return response
|
|
|
|
shutdown(fd, SHUT_WR);
|
|
|
|
|
|
|
|
// then, retrieve the response from qubesd and send it to stdout
|
|
|
|
while ((read_ret = read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
write_wrapper(1, buf, read_ret);
|
|
|
|
|
|
|
|
if (read_ret == -1) {
|
|
|
|
perror("read from qubesd");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|