Jelajahi Sumber

Change daemon arguments

Billy Barrow 1 tahun lalu
induk
melakukan
23ed62bd6c

+ 95 - 10
src/infra/daemon/Daemon.vala

@@ -3,26 +3,111 @@ using Riddle;
 namespace RiddleDaemon {
 
     private static RiddleDaemonDBusService dbus_service;
-    
+    private static MainLoop loop;
+    private static int return_code = 0;
+
     public static int main(string[] args) {
-    
-        if(args.length != 4) {
-            printerr("Please specify address, port, and store location only\n");
+
+        int family = Posix.AF_INET;
+        string? iface = null;
+        int? port = 352;
+        string store_location = "/var/cache/riddled";
+
+        for(var i = 1; i < args.length; i++) {
+            if(args[i] == "-6") {
+                family = Posix.AF_INET6;
+                continue;
+            }
+            if(args[i] == "-p") {
+                i++;
+                if(args.length <= i) {
+                    printerr(@"Please specify port with -p\n");
+                    return -1;
+                }
+                if(!int.try_parse (args[i], out port)) {
+                    printerr(@"Invalid port number '$(args[i])'\n");
+                    return -1;
+                }
+                continue;
+            }
+            if(args[i] == "-s") {
+                i++;
+                if(args.length <= i) {
+                    printerr(@"Please specify store location with -s\n");
+                    return -1;
+                }
+                store_location = args[i];
+                continue;
+            }
+            if(iface == null) {
+                iface = args[i];
+                continue;
+            }
+
+            printerr(@"Unknown argument '$(args[i])'\n");
             return -1;
         }
+
+        var addr = get_iface_address(family, iface);
+        if(addr == null) {
+            if(family == Posix.AF_INET) {
+                printerr(@"Could not find an IPv4 address on interface '$(iface)'\n");
+                return -4;
+            }
+            else {
+                printerr(@"Could not find an IPv6 address on interface '$(iface)'\n");
+                return -4;
+            }
+        }
     
-        var store = new FilesystemNameInfoStore(args[3]);
-        var server = new Server(args[1], (uint16)int.parse(args[2]), store);
-        dbus_service = new RiddleDaemonDBusService (args[1], (uint16)int.parse(args[2]), server.client);
+        var store = new FilesystemNameInfoStore(store_location);
+        var server = new Server(addr, (uint16)port, store);
+        dbus_service = new RiddleDaemonDBusService (addr, (uint16)port, server.client);
         server.riddle_received.connect(riddle_received);
     
     
-        var loop = new MainLoop();
-        Bus.own_name (BusType.SESSION, "nz.astrologue.RiddleService", BusNameOwnerFlags.NONE, on_bus_aquired, () => {}, () => stderr.printf ("Could not aquire name\n"));
+        loop = new MainLoop();
+        Bus.own_name (BusType.SYSTEM, "nz.astrologue.RiddleService", BusNameOwnerFlags.NONE, on_bus_aquired, () => {}, lost_bus);
         server.start.begin();
         loop.run();
     
-        return 0;
+        return return_code;
+    }
+
+    public static string? get_iface_address(int family, string iface) {
+        Linux.Network.IfAddrs? address;
+        Linux.Network.getifaddrs(out address);
+
+        unowned Linux.Network.IfAddrs? next = address;
+        while(next != null) {
+            unowned var addr = next.ifa_addr;
+            if(addr != null) {
+                string? ip = null;
+                if(addr.sa_family == Posix.AF_INET) {
+                    var sa = (Posix.SockAddrIn *)addr;
+                    ip = Posix.inet_ntoa(sa.sin_addr);
+                }
+                if(addr.sa_family == Posix.AF_INET6) {
+                    var sa = (Posix.SockAddrIn6 *)addr;
+                    var saddr = new uint8[Posix.INET6_ADDRSTRLEN];
+                    ip = Posix.inet_ntop(Posix.AF_INET6, &sa.sin6_addr, saddr);
+                }
+
+                if(ip != null && addr.sa_family == family && next.ifa_name == iface) {
+                    printerr(@"Using IP: $(ip) on $(next.ifa_name)\n");
+                    return ip;
+                }
+            }
+            next = next.ifa_next;
+        }   
+        
+        return null;
+    }
+
+    private static void lost_bus() {
+        stderr.printf ("Could not acquire name on system bus\n");
+        return_code = -2;
+        loop.quit();
     }
     
     private static void on_bus_aquired (DBusConnection conn) {

+ 6 - 1
src/infra/daemon/meson.build

@@ -1,4 +1,9 @@
 sources = files('Daemon.vala')
 sources += files('Service.vala')
 
-executable('riddled', sources, dependencies: dependencies)
+deps = dependencies
+deps += meson.get_compiler('vala').find_library('posix')
+deps += meson.get_compiler('vala').find_library('linux')
+
+executable('riddled', sources, dependencies: deps, install: true)
+install_data('nz.astrologue.RiddleService.conf', install_dir: '/etc/dbus-1/system.d/')

+ 15 - 0
src/infra/daemon/nz.astrologue.RiddleService.conf

@@ -0,0 +1,15 @@
+<busconfig>
+  <!-- Only root or user riddle can own the Riddle service -->
+  <policy user="riddle">
+    <allow own="nz.astrologue.RiddleService"/>
+  </policy>
+  <policy user="root">
+    <allow own="nz.astrologue.RiddleService"/>
+  </policy>
+
+  <!-- Allow anyone to invoke methods on riddle daemon -->
+  <policy context="default">
+    <allow send_destination="nz.astrologue.RiddleService"/>
+    <allow receive_sender="nz.astrologue.RiddleService"/>
+  </policy>
+</busconfig>

+ 1 - 1
src/lib/DaemonService.vala

@@ -10,7 +10,7 @@ namespace Riddle {
         private InetSocketAddress daemon_server_address;
 
         public DaemonService() throws Error {
-            dbus_service = Bus.get_proxy_sync (BusType.SESSION, "nz.astrologue.RiddleService", "/nz/astrologue/RiddleService");
+            dbus_service = Bus.get_proxy_sync (BusType.SYSTEM, "nz.astrologue.RiddleService", "/nz/astrologue/RiddleService");
             client = new Client.with_dbus (dbus_service);
             riddles = new Invercargill.Sequence<Riddle> ();