123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include <assert.h>
- #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) {
- size_t written = 0;
- 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");
- 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");
- 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++;
- if (!source_domain || !target_domain_type || !service_name || argc > 2) {
- fprintf(stderr, "Usage: %s [service-argument]\n", argv[0]);
- fprintf(stderr, "\n");
- fprintf(stderr, "Expected environment variables:\n");
- fprintf(stderr, " - QREXEC_REMOTE_DOMAIN - source domain for the call\n");
- fprintf(stderr, " - QREXEC_REQUESTED_TARGET_TYPE - target domain for the call\n");
- 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;
- }
- 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);
- // 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;
- }
|