Parcourir la source

admin-api: create and install actual Admin API RPC endpoints

Install files in /etc/qubes-rpc for all methods defined in API
documentation, even if not yet implemented (qubesd will handle it
raising appropriate exception).
Use minimal program written in C (qubesd-query-fast), instead of
qubesd-query in python for performance reasons:
 - a single qubesd-query run: ~300ms
 - equivalent in shell (echo | nc -U): ~40ms
 - qubesd-query-fast: ~20ms

Many tools makes multiple API calls, so performance here do matter. For
example qvm-ls (from VM) currently takes about 60s on a system with 24
VMs.

Also make use of `$include:` directive in policy file, to make it easier
defining a VM with full Admin API access.

QubesOS/qubes-issues#853
Marek Marczykowski-Górecki il y a 7 ans
Parent
commit
f42cd28901

+ 101 - 0
Makefile

@@ -7,6 +7,94 @@ DIST_DOM0 ?= fc18
 OS ?= Linux
 PYTHON ?= python3
 
+ADMIN_API_METHODS_SIMPLE = \
+	admin.vm.List \
+	admin.vmclass.List \
+	admin.Events \
+	admin.backup.Execute \
+	admin.backup.Info \
+	admin.backup.Restore \
+	admin.label.Create \
+	admin.label.Get \
+	admin.label.List \
+	admin.label.Remove \
+	admin.pool.Add \
+	admin.pool.Info \
+	admin.pool.List \
+	admin.pool.ListDrivers \
+	admin.pool.Remove \
+	admin.pool.volume.Info \
+	admin.pool.volume.List \
+	admin.pool.volume.ListSnapshots \
+	admin.pool.volume.Resize \
+	admin.pool.volume.Revert \
+	admin.pool.volume.Snapshot \
+	admin.property.Get \
+	admin.property.Help \
+	admin.property.HelpRst \
+	admin.property.List \
+	admin.property.Reset \
+	admin.property.Set \
+	admin.vm.Clone \
+	admin.vm.Create.AppVM \
+	admin.vm.Create.DispVM \
+	admin.vm.Create.StandaloneVM \
+	admin.vm.Create.TemplateVM \
+	admin.vm.CreateInPool.AppVM \
+	admin.vm.CreateInPool.DispVM \
+	admin.vm.CreateInPool.StandaloneVM \
+	admin.vm.CreateInPool.TemplateVM \
+	admin.vm.Kill \
+	admin.vm.List \
+	admin.vm.Pause \
+	admin.vm.Remove \
+	admin.vm.Shutdown \
+	admin.vm.Start \
+	admin.vm.Unpause \
+	admin.vm.device.pci.Attach \
+	admin.vm.device.pci.Available \
+	admin.vm.device.pci.Detach \
+	admin.vm.device.pci.List \
+	admin.vm.device.block.Attach \
+	admin.vm.device.block.Available \
+	admin.vm.device.block.Detach \
+	admin.vm.device.block.List \
+	admin.vm.device.mic.Attach \
+	admin.vm.device.mic.Available \
+	admin.vm.device.mic.Detach \
+	admin.vm.device.mic.List \
+	admin.vm.feature.CheckWithTemplate \
+	admin.vm.feature.Get \
+	admin.vm.feature.List \
+	admin.vm.feature.Remove \
+	admin.vm.feature.Set \
+	admin.vm.firewall.Flush \
+	admin.vm.firewall.Get \
+	admin.vm.firewall.Set \
+	admin.vm.firewall.GetPolicy \
+	admin.vm.firewall.SetPolicy \
+	admin.vm.firewall.Reload \
+	admin.vm.property.Get \
+	admin.vm.property.Help \
+	admin.vm.property.HelpRst \
+	admin.vm.property.List \
+	admin.vm.property.Reset \
+	admin.vm.property.Set \
+	admin.vm.tag.Get \
+	admin.vm.tag.List \
+	admin.vm.tag.Remove \
+	admin.vm.tag.Set \
+	admin.vm.volume.Info \
+	admin.vm.volume.List \
+	admin.vm.volume.ListSnapshots \
+	admin.vm.volume.Resize \
+	admin.vm.volume.Revert \
+	$(null)
+
+ADMIN_API_METHODS := $(ADMIN_API_METHODS_SIMPLE) \
+	 admin.vm.volume.Import \
+	 $(null)
+
 ifeq ($(OS),Linux)
 DATADIR ?= /var/lib/qubes
 STATEDIR ?= /var/run/qubes
@@ -42,6 +130,7 @@ rpms-dom0:
 
 all:
 	$(PYTHON) setup.py build
+	$(MAKE) -C qubes-rpc all
 #	make all -C tests
 	# Currently supported only on xen
 
@@ -82,6 +171,18 @@ endif
 	cp qubes-rpc/qubes.NotifyUpdates $(DESTDIR)/etc/qubes-rpc/
 	cp qubes-rpc/qubes-notify-updates $(DESTDIR)/usr/libexec/qubes/
 	cp qubes-rpc/qubes-notify-tools $(DESTDIR)/usr/libexec/qubes/
+	install qubes-rpc/qubesd-query-fast $(DESTDIR)/usr/libexec/qubes/
+	for method in $(ADMIN_API_METHODS_SIMPLE); do \
+		ln -s ../../usr/libexec/qubes/qubesd-query-fast \
+			$(DESTDIR)/etc/qubes-rpc/$$method; \
+	done
+	for method in $(ADMIN_API_METHODS); do \
+		install -m 0644 qubes-rpc-policy/admin-default \
+			$(DESTDIR)/etc/qubes-rpc/policy/$$method; \
+	done
+	install -d $(DESTDIR)/etc/qubes-rpc/policy/include
+	install -m 0644 qubes-rpc-policy/admin-all \
+		$(DESTDIR)/etc/qubes-rpc/policy/include/
 
 	mkdir -p "$(DESTDIR)$(FILESDIR)"
 	cp -r templates "$(DESTDIR)$(FILESDIR)/templates"

+ 11 - 0
qubes-rpc-policy/admin-all

@@ -0,0 +1,11 @@
+## This file is included from all admin.* policy files _in default
+## configuration_. To allow only specific action, edit specific policy file.
+
+## Note that policy parsing stops at the first match,
+## so adding anything below "$anyvm $anyvm action" line will have no effect
+
+## Please use a single # to start your custom comments
+
+## Add your entries here, make sure to append ",target=dom0" to all allow/ask actions
+
+$anyvm $anyvm deny

+ 13 - 0
qubes-rpc-policy/admin-default

@@ -0,0 +1,13 @@
+## Note that policy parsing stops at the first match,
+## so adding anything below "$anyvm $anyvm action" line will have no effect
+
+## Please use a single # to start your custom comments
+
+## Add your entries here, make sure to append ",target=dom0" to all allow/ask actions
+
+## Include a single file for all admin.* methods to ease setting up Management VM.
+## To allow only specific actions, edit specific policy file, like this one. To
+## allow all of them, edit /etc/qubes-rpc/include/admin-all.
+$include:/etc/qubes-rpc/policy/include/admin-all
+
+$anyvm $anyvm deny

+ 1 - 0
qubes-rpc/.gitignore

@@ -1 +1,2 @@
 qfile-dom0-unpacker
+qubesd-query-fast

+ 8 - 0
qubes-rpc/Makefile

@@ -0,0 +1,8 @@
+CFLAGS=-O3
+
+all: qubesd-query-fast
+
+qubesd-query-fast: qubesd-query-fast.c
+
+clean:
+	rm -f qubesd-query-fast

+ 93 - 0
qubes-rpc/qubesd-query-fast.c

@@ -0,0 +1,93 @@
+#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) {
+    int 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 = getenv("QREXEC_REQUESTED_TARGET");
+    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 || !service_name || argc > 2) {
+        fprintf(stderr, "Usage: %s [service-argument]\n");
+        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 - 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;
+    }
+
+    // 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;
+}
+

+ 4 - 0
rpm_spec/core-dom0.spec

@@ -383,6 +383,7 @@ fi
 /usr/lib/qubes/startup-misc.sh
 /usr/libexec/qubes/qubes-notify-tools
 /usr/libexec/qubes/qubes-notify-updates
+/usr/libexec/qubes/qubesd-query-fast
 %{_unitdir}/qubes-core.service
 %{_unitdir}/qubes-netvm.service
 %{_unitdir}/qubes-qmemman.service
@@ -404,6 +405,8 @@ fi
 /etc/xen/scripts/block-snapshot
 /etc/xen/scripts/block-origin
 /etc/xen/scripts/vif-route-qubes
+%attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/admin.*
+%attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/include/admin-all
 %attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/qubes.FeaturesRequest
 %attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/qubes.Filecopy
 %attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/qubes.GetImageRGBA
@@ -413,6 +416,7 @@ fi
 %attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/qubes.OpenInVM
 %attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/qubes.OpenURL
 %attr(0664,root,qubes) %config(noreplace) /etc/qubes-rpc/policy/qubes.VMShell
+/etc/qubes-rpc/admin.*
 /etc/qubes-rpc/qubes.FeaturesRequest
 /etc/qubes-rpc/qubes.GetRandomizedTime
 /etc/qubes-rpc/qubes.NotifyTools