chiark / gitweb /
core: make sure we can combine DevicePolicy=closed with PrivateDevices=yes
[elogind.git] / src / libsystemd-network / sd-dhcp-lease.c
index 0529b6d8fa3cb4f26bfac8aadd3a9253ba9b32c3..e6d80d4c665da578971aae46d1e9bf392f9b2875 100644 (file)
@@ -96,6 +96,18 @@ int sd_dhcp_lease_get_hostname(sd_dhcp_lease *lease, const char **hostname) {
         return 0;
 }
 
+int sd_dhcp_lease_get_root_path(sd_dhcp_lease *lease, const char **root_path) {
+        assert_return(lease, -EINVAL);
+        assert_return(root_path, -EINVAL);
+
+        if (lease->root_path)
+                *root_path = lease->root_path;
+        else
+                return -ENOENT;
+
+        return 0;
+}
+
 int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) {
         assert_return(lease, -EINVAL);
         assert_return(addr, -EINVAL);
@@ -114,6 +126,24 @@ int sd_dhcp_lease_get_netmask(sd_dhcp_lease *lease, struct in_addr *addr) {
         return 0;
 }
 
+int sd_dhcp_lease_get_server_identifier(sd_dhcp_lease *lease, struct in_addr *addr) {
+        assert_return(lease, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        addr->s_addr = lease->server_address;
+
+        return 0;
+}
+
+int sd_dhcp_lease_get_next_server(sd_dhcp_lease *lease, struct in_addr *addr) {
+        assert_return(lease, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        addr->s_addr = lease->next_server;
+
+        return 0;
+}
+
 sd_dhcp_lease *sd_dhcp_lease_ref(sd_dhcp_lease *lease) {
         if (lease)
                 assert_se(REFCNT_INC(lease->n_ref) >= 2);
@@ -212,6 +242,14 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const uint8_t *option,
 
                 break;
 
+        case DHCP_OPTION_ROOT_PATH:
+                if (len >= 1) {
+                        free(lease->root_path);
+                        lease->root_path = strndup((const char *)option, len);
+                }
+
+                break;
+
         case DHCP_OPTION_RENEWAL_T1_TIME:
                 if (len == 4) {
                         memcpy(&val, option, 4);
@@ -259,10 +297,6 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         assert(lease);
         assert(lease_file);
 
-        r = mkdir_safe_label("/run/systemd/network/leases", 0755, 0, 0);
-        if (r < 0)
-                goto finish;
-
         r = fopen_temporary(lease_file, &f, &temp_path);
         if (r < 0)
                 goto finish;
@@ -309,6 +343,30 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         fprintf(f,
                 "NETMASK=%s\n", string);
 
+        r = sd_dhcp_lease_get_server_identifier(lease, &address);
+        if (r >= 0) {
+                string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
+                if (!string) {
+                        r = -errno;
+                        goto finish;
+                }
+
+                fprintf(f,
+                        "SERVER_ADDRESS=%s\n", string);
+        }
+
+        r = sd_dhcp_lease_get_next_server(lease, &address);
+        if (r >= 0) {
+                string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
+                if (!string) {
+                        r = -errno;
+                        goto finish;
+                }
+
+                fprintf(f,
+                        "NEXT_SERVER=%s\n", string);
+        }
+
         r = sd_dhcp_lease_get_mtu(lease, &mtu);
         if (r >= 0)
                 fprintf(f, "MTU=%" PRIu16 "\n", mtu);
@@ -323,6 +381,10 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
         if (r >= 0)
                 fprintf(f, "HOSTNAME=%s\n", string);
 
+        r = sd_dhcp_lease_get_root_path(lease, &string);
+        if (r >= 0)
+                fprintf(f, "ROOT_PATH=%s\n", string);
+
         r = 0;
 
         fflush(f);
@@ -343,6 +405,7 @@ finish:
 int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
         _cleanup_dhcp_lease_unref_ sd_dhcp_lease *lease = NULL;
         _cleanup_free_ char *address = NULL, *router = NULL, *netmask = NULL,
+                            *server_address = NULL, *next_server = NULL,
                             *mtu = NULL;
         struct in_addr addr;
         int r;
@@ -358,9 +421,12 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
                            "ADDRESS", &address,
                            "ROUTER", &router,
                            "NETMASK", &netmask,
+                           "SERVER_IDENTIFIER", &server_address,
+                           "NEXT_SERVER", &next_server,
                            "MTU", &mtu,
                            "DOMAINNAME", &lease->domainname,
                            "HOSTNAME", &lease->hostname,
+                           "ROOT_PATH", &lease->root_path,
                            NULL);
         if (r < 0) {
                 if (r == -ENOENT)
@@ -388,6 +454,22 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
 
         lease->subnet_mask = addr.s_addr;
 
+        if (server_address) {
+                r = inet_pton(AF_INET, server_address, &addr);
+                if (r < 0)
+                        return r;
+
+                lease->server_address = addr.s_addr;
+        }
+
+        if (next_server) {
+                r = inet_pton(AF_INET, next_server, &addr);
+                if (r < 0)
+                        return r;
+
+                lease->next_server = addr.s_addr;
+        }
+
         if (mtu) {
                 uint16_t u;
                 if (sscanf(mtu, "%" SCNu16, &u) > 0)