summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-07-09 15:01:00 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-07-09 15:13:12 +0200
commit89fa9a6b7b2505aa2ce18febf1e28e79510dfec2 (patch)
treec4652a7b876333a5c1af89ef7d63aa4067e3ceae /src
parentbc1f27ff5515d8e35b881281d6f2f1a7ff1fed27 (diff)
downloadsystemd-89fa9a6b7b2505aa2ce18febf1e28e79510dfec2.tar.gz
networkd: add shared parser for mud urls
The same buggy code was triplicated…
Diffstat (limited to 'src')
-rw-r--r--src/network/networkd-dhcp4.c30
-rw-r--r--src/network/networkd-dhcp6.c29
-rw-r--r--src/network/networkd-lldp-tx.c27
-rw-r--r--src/network/networkd-util.c38
-rw-r--r--src/network/networkd-util.h11
5 files changed, 58 insertions, 77 deletions
diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c
index 1ba21fc9de..8609a63b83 100644
--- a/src/network/networkd-dhcp4.c
+++ b/src/network/networkd-dhcp4.c
@@ -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,
diff --git a/src/network/networkd-dhcp6.c b/src/network/networkd-dhcp6.c
index 4506932a58..367be0a756 100644
--- a/src/network/networkd-dhcp6.c
+++ b/src/network/networkd-dhcp6.c
@@ -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,
diff --git a/src/network/networkd-lldp-tx.c b/src/network/networkd-lldp-tx.c
index c2d269156e..e6e0b5ee54 100644
--- a/src/network/networkd-lldp-tx.c
+++ b/src/network/networkd-lldp-tx.c
@@ -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] = {
diff --git a/src/network/networkd-util.c b/src/network/networkd-util.c
index 43ddf124d4..a6b9a7f8b6 100644
--- a/src/network/networkd-util.c
+++ b/src/network/networkd-util.c
@@ -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) {
diff --git a/src/network/networkd-util.h b/src/network/networkd-util.h
index 62f38bcdc2..7471f18970 100644
--- a/src/network/networkd-util.h
+++ b/src/network/networkd-util.h
@@ -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_;