chiark / gitweb /
bus: implement server mode, and anonymous authentication
[elogind.git] / src / libsystemd-bus / sd-bus.c
index 6acc59e82c49401ce486e618401a4afbd1c93b78..047f286f041d92bbe3c495ed5864a8c25ff64974 100644 (file)
@@ -52,7 +52,7 @@ static void bus_free(sd_bus *b) {
 
         free(b->rbuffer);
         free(b->unique_name);
-        free(b->auth_uid);
+        free(b->auth_buffer);
         free(b->address);
 
         free(b->exec_path);
@@ -197,6 +197,29 @@ int sd_bus_set_negotiate_fds(sd_bus *bus, int b) {
         return 0;
 }
 
+int sd_bus_set_server(sd_bus *bus, int b, sd_id128_t server) {
+        if (!bus)
+                return -EINVAL;
+        if (!b && !sd_id128_equal(server, SD_ID128_NULL))
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+
+        bus->is_server = !!b;
+        bus->peer = server;
+        return 0;
+}
+
+int sd_bus_set_anonymous(sd_bus *bus, int b) {
+        if (!bus)
+                return -EINVAL;
+        if (bus->state != BUS_UNSET)
+                return -EPERM;
+
+        bus->anonymous_auth = !!b;
+        return 0;
+}
+
 static int hello_callback(sd_bus *bus, int error, sd_bus_message *reply, void *userdata) {
         const char *s;
         int r;
@@ -244,11 +267,7 @@ static int bus_send_hello(sd_bus *bus) {
         if (r < 0)
                 return r;
 
-        r = sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, NULL);
-        if (r < 0)
-                return r;
-
-        return r;
+        return sd_bus_send_with_reply(bus, m, hello_callback, NULL, 0, &bus->hello_serial);
 }
 
 int bus_start_running(sd_bus *bus) {
@@ -507,7 +526,7 @@ static int parse_exec_address(sd_bus *b, const char **p, char **guid) {
 
                         errno = 0;
                         ul = strtoul(*p + 4, (char**) p, 10);
-                        if (errno != 0 || **p != '=' || ul > 256) {
+                        if (errno > 0 || **p != '=' || ul > 256) {
                                 r = -EINVAL;
                                 goto fail;
                         }
@@ -720,9 +739,12 @@ int sd_bus_start(sd_bus *bus) {
 
         bus->state = BUS_OPENING;
 
+        if (bus->is_server && bus->bus_client)
+                return -EINVAL;
+
         if (bus->fd >= 0)
                 r = bus_start_fd(bus);
-        else if (bus->address)
+        else if (bus->address || bus->sockaddr.sa.sa_family != AF_UNSPEC || bus->exec_path)
                 r = bus_start_address(bus);
         else
                 return -EINVAL;
@@ -1354,7 +1376,7 @@ int sd_bus_get_events(sd_bus *bus) {
                 flags |= POLLOUT;
         else if (bus->state == BUS_AUTHENTICATING) {
 
-                if (bus->auth_index < ELEMENTSOF(bus->auth_iovec))
+                if (bus_socket_auth_needs_write(bus))
                         flags |= POLLOUT;
 
                 flags |= POLLIN;
@@ -1421,6 +1443,28 @@ static int process_timeout(sd_bus *bus) {
         return r < 0 ? r : 1;
 }
 
+static int process_hello(sd_bus *bus, sd_bus_message *m) {
+        assert(bus);
+        assert(m);
+
+        if (bus->state != BUS_HELLO)
+                return 0;
+
+        /* Let's make sure the first message on the bus is the HELLO
+         * reply. But note that we don't actually parse the message
+         * here (we leave that to the usual handling), we just verify
+         * we don't let any earlier msg through. */
+
+        if (m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_RETURN &&
+            m->header->type != SD_BUS_MESSAGE_TYPE_METHOD_ERROR)
+                return -EIO;
+
+        if (m->reply_serial != bus->hello_serial)
+                return -EIO;
+
+        return 0;
+}
+
 static int process_reply(sd_bus *bus, sd_bus_message *m) {
         struct reply_callback *c;
         int r;
@@ -1675,6 +1719,10 @@ static int process_message(sd_bus *bus, sd_bus_message *m) {
         assert(bus);
         assert(m);
 
+        r = process_hello(bus, m);
+        if (r != 0)
+                return r;
+
         r = process_reply(bus, m);
         if (r != 0)
                 return r;