]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
networkd: add shared parser for mud urls
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 9 Jul 2021 13:01:00 +0000 (15:01 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 9 Jul 2021 13:13:12 +0000 (15:13 +0200)
The same buggy code was triplicated…

src/network/networkd-dhcp4.c
src/network/networkd-dhcp6.c
src/network/networkd-lldp-tx.c
src/network/networkd-util.c
src/network/networkd-util.h

index 1ba21fc9de0274d57bc0b1f05e17180c7096904e..8609a63b83bffed01bfc3c55920f00cb38ad4270 100644 (file)
@@ -5,7 +5,6 @@
 #include <linux/if.h>
 #include <linux/if_arp.h>
 
-#include "escape.h"
 #include "alloc-util.h"
 #include "dhcp-client-internal.h"
 #include "hostname-setup.h"
@@ -26,7 +25,6 @@
 #include "string-table.h"
 #include "strv.h"
 #include "sysctl-util.h"
-#include "web-util.h"
 
 static int dhcp4_request_address_and_routes(Link *link, bool announce);
 static int dhcp4_remove_all(Link *link);
@@ -1733,34 +1731,12 @@ int config_parse_dhcp_mud_url(
                 void *data,
                 void *userdata) {
 
-        _cleanup_free_ char *unescaped = NULL;
         Network *network = data;
-        ssize_t l;
 
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-
-        if (isempty(rvalue)) {
-                network->dhcp_mudurl = mfree(network->dhcp_mudurl);
-                return 0;
-        }
-
-        l = cunescape(rvalue, 0, &unescaped);
-        if (l < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, l,
-                           "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
-                return 0;
-        }
-
-        if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
-                log_syntax(unit, LOG_WARNING, filename, line, 0,
-                           "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
-
-                return 0;
-        }
+        assert(network);
 
-        return free_and_strdup_warn(&network->dhcp_mudurl, unescaped);
+        return config_parse_mud_url(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
+                                    &network->dhcp_mudurl);
 }
 
 int config_parse_dhcp_fallback_lease_lifetime(const char *unit,
index 4506932a5877fdf32b25564c230cc0709d095e66..367be0a7567c0fb828ba88a7281dd3eb275997d3 100644 (file)
@@ -9,7 +9,6 @@
 
 #include "sd-dhcp6-client.h"
 
-#include "escape.h"
 #include "hashmap.h"
 #include "hostname-setup.h"
 #include "hostname-util.h"
@@ -24,7 +23,6 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "radv-internal.h"
-#include "web-util.h"
 
 bool link_dhcp6_with_address_enabled(Link *link) {
         if (!link_dhcp6_enabled(link))
@@ -1812,33 +1810,12 @@ int config_parse_dhcp6_mud_url(
                 void *data,
                 void *userdata) {
 
-        _cleanup_free_ char *unescaped = NULL;
         Network *network = data;
-        ssize_t l;
 
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-
-        if (isempty(rvalue)) {
-                network->dhcp6_mudurl = mfree(network->dhcp6_mudurl);
-                return 0;
-        }
-
-        l = cunescape(rvalue, 0, &unescaped);
-        if (l < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, l,
-                           "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue);
-                return 0;
-        }
-
-        if (!http_url_is_valid(unescaped) || strlen(unescaped) > UINT8_MAX) {
-                log_syntax(unit, LOG_WARNING, filename, line, 0,
-                           "Failed to parse MUD URL '%s', ignoring: %m", rvalue);
-                return 0;
-        }
+        assert(network);
 
-        return free_and_replace(network->dhcp6_mudurl, unescaped);
+        return config_parse_mud_url(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
+                                    &network->dhcp6_mudurl);
 }
 
 DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp6_client_start_mode, dhcp6_client_start_mode, DHCP6ClientStartMode,
index c2d269156e831174d7e25bf6854483ca332f6776..e6e0b5ee54c0137b84cc54f0458d17725a9be55b 100644 (file)
@@ -7,7 +7,6 @@
 
 #include "alloc-util.h"
 #include "env-file.h"
-#include "escape.h"
 #include "fd-util.h"
 #include "hostname-util.h"
 #include "missing_network.h"
@@ -21,7 +20,6 @@
 #include "string-util.h"
 #include "strv.h"
 #include "unaligned.h"
-#include "web-util.h"
 
 /* The LLDP spec calls this "txFastInit", see 9.2.5.19 */
 #define LLDP_TX_FAST_INIT 4U
@@ -428,29 +426,12 @@ int config_parse_lldp_mud(
                 void *data,
                 void *userdata) {
 
-        _cleanup_free_ char *unescaped = NULL;
-        Network *n = data;
-        ssize_t l;
-
-        assert(filename);
-        assert(lvalue);
-        assert(rvalue);
-
-        l = cunescape(rvalue, 0, &unescaped);
-        if (l < 0) {
-                log_syntax(unit, LOG_WARNING, filename, line, l,
-                           "Failed to Failed to unescape LLDP MUD URL, ignoring: %s", rvalue);
-                return 0;
-        }
-
-        if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) {
-                log_syntax(unit, LOG_WARNING, filename, line, 0,
-                           "Failed to parse LLDP MUD URL '%s', ignoring: %m", rvalue);
+        Network *network = data;
 
-                return 0;
-        }
+        assert(network);
 
-        return free_and_replace(n->lldp_mud, unescaped);
+        return config_parse_mud_url(unit, filename, line, section, section_line, lvalue, ltype, rvalue,
+                                    &network->lldp_mud);
 }
 
 static const char * const lldp_emit_table[_LLDP_EMIT_MAX] = {
index 43ddf124d4457236a33bee8365a339823f0b8467..a6b9a7f8b6244ab3d05ee8965b697b12c7b307d7 100644 (file)
@@ -2,12 +2,13 @@
 
 #include "condition.h"
 #include "conf-parser.h"
+#include "escape.h"
 #include "networkd-link.h"
 #include "networkd-util.h"
 #include "parse-util.h"
 #include "string-table.h"
 #include "string-util.h"
-#include "util.h"
+#include "web-util.h"
 
 static const char* const address_family_table[_ADDRESS_FAMILY_MAX] = {
         [ADDRESS_FAMILY_NO]   = "no",
@@ -161,6 +162,41 @@ int config_parse_ip_masquerade(
         return 0;
 }
 
+int config_parse_mud_url(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                char **ret) {
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(ret);
+
+        _cleanup_free_ char *unescaped = NULL;
+        ssize_t l;
+
+        l = cunescape(rvalue, 0, &unescaped);
+        if (l < 0) {
+                log_syntax(unit, LOG_WARNING, filename, line, l,
+                           "Failed to unescape MUD URL, ignoring: %s", rvalue);
+                return 0;
+        }
+
+        if (l > UINT8_MAX || !http_url_is_valid(unescaped)) {
+                log_syntax(unit, LOG_WARNING, filename, line, 0,
+                           "Invalid MUD URL, ignoring: %s", rvalue);
+                return 0;
+        }
+
+        return free_and_replace(*ret, unescaped);
+}
+
 /* Router lifetime can be set with netlink interface since kernel >= 4.5
  * so for the supported kernel we don't need to expire routes in userspace */
 int kernel_route_expiration_supported(void) {
index 62f38bcdc2c0b51d3409ce16ac7aee884edc7ac2..7471f189702c762c22d2b257a47646183782dd1b 100644 (file)
@@ -23,6 +23,17 @@ CONFIG_PARSER_PROTOTYPE(config_parse_link_local_address_family);
 CONFIG_PARSER_PROTOTYPE(config_parse_address_family_with_kernel);
 CONFIG_PARSER_PROTOTYPE(config_parse_ip_masquerade);
 
+int config_parse_mud_url(
+                const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                char **ret);
+
 const char *address_family_to_string(AddressFamily b) _const_;
 AddressFamily address_family_from_string(const char *s) _pure_;