qubesd-query-fast.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <sys/un.h>
  7. #define QUBESD_SOCKET "/var/run/qubesd.sock"
  8. void write_wrapper(int fd, char *data, size_t len) {
  9. size_t written = 0;
  10. int ret;
  11. while (written < len) {
  12. ret = write(fd, data+written, len-written);
  13. if (ret == -1) {
  14. perror("write");
  15. exit(1);
  16. }
  17. written += ret;
  18. }
  19. }
  20. int main(int argc, char **argv) {
  21. char *source_domain = getenv("QREXEC_REMOTE_DOMAIN");
  22. char *target_domain = NULL;
  23. char *target_domain_type = getenv("QREXEC_REQUESTED_TARGET_TYPE");
  24. char *target_domain_name = getenv("QREXEC_REQUESTED_TARGET");
  25. char *target_domain_keyword = getenv("QREXEC_REQUESTED_TARGET_KEYWORD");
  26. char *service_name = strrchr(argv[0], '/');
  27. int fd;
  28. char buf[4096];
  29. int read_ret;
  30. struct sockaddr_un qubesd_addr = {
  31. .sun_family = AF_UNIX,
  32. .sun_path = QUBESD_SOCKET,
  33. };
  34. if (service_name)
  35. service_name++;
  36. if (!source_domain || !target_domain_type || !service_name || argc > 2) {
  37. fprintf(stderr, "Usage: %s [service-argument]\n", argv[0]);
  38. fprintf(stderr, "\n");
  39. fprintf(stderr, "Expected environment variables:\n");
  40. fprintf(stderr, " - QREXEC_REMOTE_DOMAIN - source domain for the call\n");
  41. fprintf(stderr, " - QREXEC_REQUESTED_TARGET_TYPE - target domain for the call\n");
  42. fprintf(stderr, "\n");
  43. fprintf(stderr, "Additionally, this program assumes being called with desired service name as argv[0] (use symlink)\n");
  44. return 1;
  45. }
  46. fd = socket(AF_UNIX, SOCK_STREAM, 0);
  47. if (fd == -1) {
  48. perror("socket");
  49. return 1;
  50. }
  51. if (connect(fd, (struct sockaddr *)&qubesd_addr, sizeof(qubesd_addr)) == -1) {
  52. perror("connect to qubesd");
  53. return 1;
  54. }
  55. if (strcmp(target_domain_type, "keyword") == 0) {
  56. // translate @adminvm back to dom0
  57. if (strcmp(target_domain_keyword, "adminvm") == 0)
  58. target_domain = "dom0";
  59. else if (strcmp(target_domain_keyword, "default") == 0)
  60. target_domain = "";
  61. else {
  62. fprintf(stderr, "Unsupported keyword: %s", target_domain_keyword);
  63. return 1;
  64. }
  65. } else
  66. target_domain = target_domain_name;
  67. assert(target_domain);
  68. // write parameters, including trailing zero as separator
  69. write_wrapper(fd, source_domain, strlen(source_domain) + 1);
  70. write_wrapper(fd, service_name, strlen(service_name) + 1);
  71. write_wrapper(fd, target_domain, strlen(target_domain) + 1);
  72. if (argc == 2)
  73. write_wrapper(fd, argv[1], strlen(argv[1]) + 1);
  74. else
  75. // empty argument
  76. write_wrapper(fd, "\0", 1);
  77. // now, read from stdin and write it to qubesd
  78. while ((read_ret = read(0, buf, sizeof(buf))) > 0)
  79. write_wrapper(fd, buf, read_ret);
  80. if (read_ret == -1) {
  81. perror("read from stdin");
  82. return 1;
  83. }
  84. // end of request, now let qubesd execute the action and return response
  85. shutdown(fd, SHUT_WR);
  86. // then, retrieve the response from qubesd and send it to stdout
  87. while ((read_ret = read(fd, buf, sizeof(buf))) > 0)
  88. write_wrapper(1, buf, read_ret);
  89. if (read_ret == -1) {
  90. perror("read from qubesd");
  91. return 1;
  92. }
  93. return 0;
  94. }