diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2020-10-31 10:50:56 +0100 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2020-11-16 16:43:39 +0100 |
commit | abd002642f19026c899611e3a7741e04f6c4e1f2 (patch) | |
tree | 3c237fbfee8027ca72a2dd9dc0c71885e518edf9 | |
parent | adf63c88750aec31177d393d332df71537e9b1be (diff) | |
download | NetworkManager-abd002642f19026c899611e3a7741e04f6c4e1f2.tar.gz |
all: add hostname setting
Add a new setting that contains properties related to how NM should
get the hostname from the connection.
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | clients/cli/connections.c | 3 | ||||
-rw-r--r-- | clients/cli/generate-docs-nm-settings-nmcli.xml.in | 10 | ||||
-rw-r--r-- | clients/common/nm-meta-setting-desc.c | 21 | ||||
-rw-r--r-- | clients/common/settings-docs.h.in | 4 | ||||
-rw-r--r-- | docs/libnm/libnm-docs.xml | 1 | ||||
-rw-r--r-- | libnm-core/meson.build | 2 | ||||
-rw-r--r-- | libnm-core/nm-core-internal.h | 1 | ||||
-rw-r--r-- | libnm-core/nm-core-types.h | 1 | ||||
-rw-r--r-- | libnm-core/nm-setting-hostname.c | 339 | ||||
-rw-r--r-- | libnm-core/nm-setting-hostname.h | 53 | ||||
-rw-r--r-- | libnm/NetworkManager.h | 1 | ||||
-rw-r--r-- | libnm/libnm.ver | 5 | ||||
-rw-r--r-- | man/NetworkManager.conf.xml | 40 | ||||
-rw-r--r-- | shared/nm-meta-setting.c | 8 | ||||
-rw-r--r-- | shared/nm-meta-setting.h | 1 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c | 42 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c | 4 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h | 2 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c | 24 |
20 files changed, 549 insertions, 15 deletions
diff --git a/Makefile.am b/Makefile.am index e05f759bbc..12b9736d2f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -943,6 +943,7 @@ libnm_core_lib_h_pub_real = \ libnm-core/nm-setting-ethtool.h \ libnm-core/nm-setting-generic.h \ libnm-core/nm-setting-gsm.h \ + libnm-core/nm-setting-hostname.h \ libnm-core/nm-setting-infiniband.h \ libnm-core/nm-setting-ip-config.h \ libnm-core/nm-setting-ip-tunnel.h \ @@ -1017,6 +1018,7 @@ libnm_core_lib_c_settings_real = \ libnm-core/nm-setting-ethtool.c \ libnm-core/nm-setting-generic.c \ libnm-core/nm-setting-gsm.c \ + libnm-core/nm-setting-hostname.c \ libnm-core/nm-setting-infiniband.c \ libnm-core/nm-setting-ip-config.c \ libnm-core/nm-setting-ip-tunnel.c \ diff --git a/clients/cli/connections.c b/clients/cli/connections.c index a321e53999..6ff48b84c5 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -881,7 +881,8 @@ const NmcMetaGenericInfo "," NM_SETTING_6LOWPAN_SETTING_NAME "," NM_SETTING_WIREGUARD_SETTING_NAME \ "," NM_SETTING_PROXY_SETTING_NAME "," NM_SETTING_TC_CONFIG_SETTING_NAME \ "," NM_SETTING_SRIOV_SETTING_NAME "," NM_SETTING_ETHTOOL_SETTING_NAME \ - "," NM_SETTING_OVS_DPDK_SETTING_NAME /* NM_SETTING_DUMMY_SETTING_NAME NM_SETTING_WIMAX_SETTING_NAME */ + "," NM_SETTING_OVS_DPDK_SETTING_NAME \ + "," NM_SETTING_HOSTNAME_SETTING_NAME /* NM_SETTING_DUMMY_SETTING_NAME NM_SETTING_WIMAX_SETTING_NAME */ const NmcMetaGenericInfo *const nmc_fields_con_active_details_groups[] = { NMC_META_GENERIC_WITH_NESTED("GENERAL", metagen_con_active_general), /* 0 */ diff --git a/clients/cli/generate-docs-nm-settings-nmcli.xml.in b/clients/cli/generate-docs-nm-settings-nmcli.xml.in index f2f589fe8d..3c3c3f9d41 100644 --- a/clients/cli/generate-docs-nm-settings-nmcli.xml.in +++ b/clients/cli/generate-docs-nm-settings-nmcli.xml.in @@ -564,6 +564,16 @@ <property name="mtu" description="If non-zero, only transmit packets of the specified size or smaller, breaking larger packets up into multiple frames." /> </setting> + <setting name="hostname" > + <property name="priority" + description="The relative priority of this connection to determine the system hostname. A lower numerical value is better (higher priority). A connection with higher priority is considered before connections with lower priority. If the value is zero, it can be overridden by a global value from NetworkManager configuration. If the property doesn't have a value in the global configuration, the value is assumed to be 100. Negative values have the special effect of excluding other connections with a greater numerical priority value; so in presence of at least one negative priority, only connections with the lowest priority value will be used to determine the hostname." /> + <property name="from-dhcp" + description="Whether the system hostname can be determined from DHCP on this connection. When set to NM_TERNARY_DEFAULT (-1), the value from global configuration is used. If the property doesn't have a value in the global configuration, NetworkManager assumes the value to be NM_TERNARY_TRUE (1)." /> + <property name="from-dns-lookup" + description="Whether the system hostname can be determined from reverse DNS lookup of addresses on this device. When set to NM_TERNARY_DEFAULT (-1), the value from global configuration is used. If the property doesn't have a value in the global configuration, NetworkManager assumes the value to be NM_TERNARY_TRUE (1)." /> + <property name="only-from-default" + description="If set to NM_TERNARY_TRUE (1), NetworkManager attempts to get the hostname via DHCPv4/DHCPv6 or reverse DNS lookup on this device only when the device has the default route for the given address family (IPv4/IPv6). If set to NM_TERNARY_FALSE (0), the hostname can be set from this device even if it doesn't have the default route. When set to NM_TERNARY_DEFAULT (-1), the value from global configuration is used. If the property doesn't have a value in the global configuration, NetworkManager assumes the value to be NM_TERNARY_TRUE (1)." /> + </setting> <setting name="infiniband" > <property name="mac-address" alias="mac" diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 86b555fe5f..35758b8f3a 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -5634,6 +5634,24 @@ static const NMMetaPropertyInfo *const property_infos_GSM[] = { }; #undef _CURRENT_NM_META_SETTING_TYPE +#define _CURRENT_NM_META_SETTING_TYPE NM_META_SETTING_TYPE_HOSTNAME +static const NMMetaPropertyInfo *const property_infos_HOSTNAME[] = { + PROPERTY_INFO (NM_SETTING_HOSTNAME_PRIORITY, DESCRIBE_DOC_NM_SETTING_HOSTNAME_PRIORITY, + .property_type = &_pt_gobject_int, + ), + PROPERTY_INFO (NM_SETTING_HOSTNAME_FROM_DHCP, DESCRIBE_DOC_NM_SETTING_HOSTNAME_FROM_DHCP, + .property_type = &_pt_gobject_enum, + ), + PROPERTY_INFO (NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP, DESCRIBE_DOC_NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP, + .property_type = &_pt_gobject_enum, + ), + PROPERTY_INFO (NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT, DESCRIBE_DOC_NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT, + .property_type = &_pt_gobject_enum, + ), + NULL +}; + +#undef _CURRENT_NM_META_SETTING_TYPE #define _CURRENT_NM_META_SETTING_TYPE NM_META_SETTING_TYPE_INFINIBAND static const NMMetaPropertyInfo *const property_infos_INFINIBAND[] = { PROPERTY_INFO_WITH_DESC (NM_SETTING_INFINIBAND_MAC_ADDRESS, @@ -7936,6 +7954,7 @@ _setting_init_fcn_wireless (ARGS_SETTING_INIT_FCN) #define SETTING_PRETTY_NAME_ETHTOOL N_("Ethtool settings") #define SETTING_PRETTY_NAME_GENERIC N_("Generic settings") #define SETTING_PRETTY_NAME_GSM N_("GSM mobile broadband connection") +#define SETTING_PRETTY_NAME_HOSTNAME N_("Hostname settings") #define SETTING_PRETTY_NAME_INFINIBAND N_("InfiniBand connection") #define SETTING_PRETTY_NAME_IP4_CONFIG N_("IPv4 protocol") #define SETTING_PRETTY_NAME_IP6_CONFIG N_("IPv6 protocol") @@ -8073,6 +8092,7 @@ const NMMetaSettingInfoEditor nm_meta_setting_infos_editor[] = { ), .setting_init_fcn = _setting_init_fcn_gsm, ), + SETTING_INFO (HOSTNAME), SETTING_INFO (INFINIBAND, .valid_parts = NM_META_SETTING_VALID_PARTS ( NM_META_SETTING_VALID_PART_ITEM (CONNECTION, TRUE), @@ -8284,6 +8304,7 @@ static const NMMetaSettingValidPartItem *const valid_settings_noslave[] = { NM_META_SETTING_VALID_PART_ITEM(MATCH, FALSE), NM_META_SETTING_VALID_PART_ITEM(IP4_CONFIG, FALSE), NM_META_SETTING_VALID_PART_ITEM(IP6_CONFIG, FALSE), + NM_META_SETTING_VALID_PART_ITEM(HOSTNAME, FALSE), NM_META_SETTING_VALID_PART_ITEM(TC_CONFIG, FALSE), NM_META_SETTING_VALID_PART_ITEM(PROXY, FALSE), NULL, diff --git a/clients/common/settings-docs.h.in b/clients/common/settings-docs.h.in index 78198ec973..b9231752f4 100644 --- a/clients/common/settings-docs.h.in +++ b/clients/common/settings-docs.h.in @@ -203,6 +203,10 @@ #define DESCRIBE_DOC_NM_SETTING_GSM_SIM_ID N_("The SIM card unique identifier (as given by the WWAN management service) which this connection applies to. If given, the connection will apply to any device also allowed by \"device-id\" which contains a SIM card matching the given identifier.") #define DESCRIBE_DOC_NM_SETTING_GSM_SIM_OPERATOR_ID N_("A MCC/MNC string like \"310260\" or \"21601\" identifying the specific mobile network operator which this connection applies to. If given, the connection will apply to any device also allowed by \"device-id\" and \"sim-id\" which contains a SIM card provisioned by the given operator.") #define DESCRIBE_DOC_NM_SETTING_GSM_USERNAME N_("The username used to authenticate with the network, if required. Many providers do not require a username, or accept any username. But if a username is required, it is specified here.") +#define DESCRIBE_DOC_NM_SETTING_HOSTNAME_FROM_DHCP N_("Whether the system hostname can be determined from DHCP on this connection. When set to NM_TERNARY_DEFAULT (-1), the value from global configuration is used. If the property doesn't have a value in the global configuration, NetworkManager assumes the value to be NM_TERNARY_TRUE (1).") +#define DESCRIBE_DOC_NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP N_("Whether the system hostname can be determined from reverse DNS lookup of addresses on this device. When set to NM_TERNARY_DEFAULT (-1), the value from global configuration is used. If the property doesn't have a value in the global configuration, NetworkManager assumes the value to be NM_TERNARY_TRUE (1).") +#define DESCRIBE_DOC_NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT N_("If set to NM_TERNARY_TRUE (1), NetworkManager attempts to get the hostname via DHCPv4/DHCPv6 or reverse DNS lookup on this device only when the device has the default route for the given address family (IPv4/IPv6). If set to NM_TERNARY_FALSE (0), the hostname can be set from this device even if it doesn't have the default route. When set to NM_TERNARY_DEFAULT (-1), the value from global configuration is used. If the property doesn't have a value in the global configuration, NetworkManager assumes the value to be NM_TERNARY_TRUE (1).") +#define DESCRIBE_DOC_NM_SETTING_HOSTNAME_PRIORITY N_("The relative priority of this connection to determine the system hostname. A lower numerical value is better (higher priority). A connection with higher priority is considered before connections with lower priority. If the value is zero, it can be overridden by a global value from NetworkManager configuration. If the property doesn't have a value in the global configuration, the value is assumed to be 100. Negative values have the special effect of excluding other connections with a greater numerical priority value; so in presence of at least one negative priority, only connections with the lowest priority value will be used to determine the hostname.") #define DESCRIBE_DOC_NM_SETTING_INFINIBAND_MAC_ADDRESS N_("If specified, this connection will only apply to the IPoIB device whose permanent MAC address matches. This property does not change the MAC address of the device (i.e. MAC spoofing).") #define DESCRIBE_DOC_NM_SETTING_INFINIBAND_MTU N_("If non-zero, only transmit packets of the specified size or smaller, breaking larger packets up into multiple frames.") #define DESCRIBE_DOC_NM_SETTING_INFINIBAND_P_KEY N_("The InfiniBand P_Key to use for this device. A value of -1 means to use the default P_Key (aka \"the P_Key at index 0\"). Otherwise, it is a 16-bit unsigned integer, whose high bit is set if it is a \"full membership\" P_Key.") diff --git a/docs/libnm/libnm-docs.xml b/docs/libnm/libnm-docs.xml index d0be0eb475..0a70694d3f 100644 --- a/docs/libnm/libnm-docs.xml +++ b/docs/libnm/libnm-docs.xml @@ -324,6 +324,7 @@ print ("NetworkManager version " + client.get_version())]]></programlisting></in <xi:include href="xml/nm-setting-ethtool.xml"/> <xi:include href="xml/nm-setting-generic.xml"/> <xi:include href="xml/nm-setting-gsm.xml"/> + <xi:include href="xml/nm-setting-hostname.xml"/> <xi:include href="xml/nm-setting-infiniband.xml"/> <xi:include href="xml/nm-setting-ip4-config.xml"/> <xi:include href="xml/nm-setting-ip6-config.xml"/> diff --git a/libnm-core/meson.build b/libnm-core/meson.build index 7b59a8c204..15d8931c0f 100644 --- a/libnm-core/meson.build +++ b/libnm-core/meson.build @@ -33,6 +33,7 @@ libnm_core_headers = files( 'nm-setting-ethtool.h', 'nm-setting-generic.h', 'nm-setting-gsm.h', + 'nm-setting-hostname.h', 'nm-setting-infiniband.h', 'nm-setting-ip-config.h', 'nm-setting-ip-tunnel.h', @@ -134,6 +135,7 @@ libnm_core_settings_sources = files( 'nm-setting-ethtool.c', 'nm-setting-generic.c', 'nm-setting-gsm.c', + 'nm-setting-hostname.c', 'nm-setting-infiniband.c', 'nm-setting-ip-config.c', 'nm-setting-ip-tunnel.c', diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index fcc38565d3..c3beb71ea3 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -37,6 +37,7 @@ #include "nm-setting-dummy.h" #include "nm-setting-generic.h" #include "nm-setting-gsm.h" +#include "nm-setting-hostname.h" #include "nm-setting-infiniband.h" #include "nm-setting-ip-tunnel.h" #include "nm-setting-ip4-config.h" diff --git a/libnm-core/nm-core-types.h b/libnm-core/nm-core-types.h index 64d6464e2b..cb940a1ee9 100644 --- a/libnm-core/nm-core-types.h +++ b/libnm-core/nm-core-types.h @@ -28,6 +28,7 @@ typedef struct _NMSettingDummy NMSettingDummy; typedef struct _NMSettingEthtool NMSettingEthtool; typedef struct _NMSettingGeneric NMSettingGeneric; typedef struct _NMSettingGsm NMSettingGsm; +typedef struct _NMSettingHostname NMSettingHostname; typedef struct _NMSettingIP4Config NMSettingIP4Config; typedef struct _NMSettingIP6Config NMSettingIP6Config; typedef struct _NMSettingIPConfig NMSettingIPConfig; diff --git a/libnm-core/nm-setting-hostname.c b/libnm-core/nm-setting-hostname.c new file mode 100644 index 0000000000..8218978705 --- /dev/null +++ b/libnm-core/nm-setting-hostname.c @@ -0,0 +1,339 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +/* + * Copyright (C) 2020 Red Hat, Inc. + */ + +#include "nm-default.h" + +#include "nm-setting-hostname.h" + +#include "nm-setting-private.h" +#include "nm-utils-private.h" + +/** + * SECTION:nm-setting-hostname + * @short_description: Contains properties related to the hostname + * @include: nm-setting-hostname.h + **/ + +/*****************************************************************************/ + +NM_GOBJECT_PROPERTIES_DEFINE(NMSettingHostname, + PROP_PRIORITY, + PROP_FROM_DHCP, + PROP_FROM_DNS_LOOKUP, + PROP_ONLY_FROM_DEFAULT, ); + +/** + * NMSettingHostname: + * + * Hostname settings + * + * Since: 1.30 + */ +struct _NMSettingHostname { + NMSetting parent; + int priority; + NMTernary from_dhcp; + NMTernary from_dns_lookup; + NMTernary only_from_default; +}; + +struct _NMSettingHostnameClass { + NMSettingClass parent; +}; + +G_DEFINE_TYPE(NMSettingHostname, nm_setting_hostname, NM_TYPE_SETTING) + +/** + * nm_setting_hostname_get_priority: + * @setting: the #NMSettingHostname + * + * Returns the value contained in the #NMSettingHostname:priority + * property. + * + * Returns: the 'priority' property value + * + * Since: 1.30 + **/ +int +nm_setting_hostname_get_priority(NMSettingHostname *setting) +{ + g_return_val_if_fail(NM_IS_SETTING_HOSTNAME(setting), 0); + + return setting->priority; +} + +/** + * nm_setting_hostname_get_from_dhcp: + * @setting: the #NMSettingHostname + * + * Returns the value contained in the #NMSettingHostname:from-dhcp + * property. + * + * Returns: the 'from-dhcp' property value + * + * Since: 1.30 + **/ +NMTernary +nm_setting_hostname_get_from_dhcp(NMSettingHostname *setting) +{ + g_return_val_if_fail(NM_IS_SETTING_HOSTNAME(setting), NM_TERNARY_DEFAULT); + + return setting->from_dhcp; +} + +/** + * nm_setting_hostname_get_from_dns_lookup: + * @setting: the #NMSettingHostname + * + * Returns the value contained in the #NMSettingHostname:from-dns-lookup + * property. + * + * Returns: the 'from-dns-lookup' property value + * + * Since: 1.30 + **/ +NMTernary +nm_setting_hostname_get_from_dns_lookup(NMSettingHostname *setting) +{ + g_return_val_if_fail(NM_IS_SETTING_HOSTNAME(setting), NM_TERNARY_DEFAULT); + + return setting->from_dns_lookup; +} + +/** + * nm_setting_hostname_get_only_from_default: + * @setting: the #NMSettingHostname + * + * Returns the value contained in the #NMSettingHostname:only-from-default + * property. + * + * Returns: the 'only-from-default' property value + * + * Since: 1.30 + **/ +NMTernary +nm_setting_hostname_get_only_from_default(NMSettingHostname *setting) +{ + g_return_val_if_fail(NM_IS_SETTING_HOSTNAME(setting), NM_TERNARY_DEFAULT); + + return setting->only_from_default; +} + +/*****************************************************************************/ + +static void +get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) +{ + NMSettingHostname *self = NM_SETTING_HOSTNAME(object); + + switch (prop_id) { + case PROP_PRIORITY: + g_value_set_int(value, self->priority); + break; + case PROP_FROM_DHCP: + g_value_set_enum(value, self->from_dhcp); + break; + case PROP_FROM_DNS_LOOKUP: + g_value_set_enum(value, self->from_dns_lookup); + break; + case PROP_ONLY_FROM_DEFAULT: + g_value_set_enum(value, self->only_from_default); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +static void +set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) +{ + NMSettingHostname *self = NM_SETTING_HOSTNAME(object); + + switch (prop_id) { + case PROP_PRIORITY: + self->priority = g_value_get_int(value); + break; + case PROP_FROM_DHCP: + self->from_dhcp = g_value_get_enum(value); + break; + case PROP_FROM_DNS_LOOKUP: + self->from_dns_lookup = g_value_get_enum(value); + break; + case PROP_ONLY_FROM_DEFAULT: + self->only_from_default = g_value_get_enum(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + +/*****************************************************************************/ + +static void +nm_setting_hostname_init(NMSettingHostname *setting) +{ + setting->from_dhcp = NM_TERNARY_DEFAULT; + setting->from_dns_lookup = NM_TERNARY_DEFAULT; + setting->only_from_default = NM_TERNARY_DEFAULT; +} + +/** + * nm_setting_hostname_new: + * + * Creates a new #NMSettingHostname object with default values. + * + * Returns: (transfer full): the new empty #NMSettingHostname object + * + * Since: 1.30 + **/ +NMSetting * +nm_setting_hostname_new(void) +{ + return g_object_new(NM_TYPE_SETTING_HOSTNAME, NULL); +} + +static void +nm_setting_hostname_class_init(NMSettingHostnameClass *klass) +{ + GObjectClass * object_class = G_OBJECT_CLASS(klass); + NMSettingClass *setting_class = NM_SETTING_CLASS(klass); + + object_class->get_property = get_property; + object_class->set_property = set_property; + + /** + * NMSettingHostname:priority + * + * The relative priority of this connection to determine the + * system hostname. A lower numerical value is better (higher + * priority). A connection with higher priority is considered + * before connections with lower priority. + * + * If the value is zero, it can be overridden by a global value + * from NetworkManager configuration. If the property doesn't have + * a value in the global configuration, the value is assumed to be + * 100. + * + * Negative values have the special effect of excluding other + * connections with a greater numerical priority value; so in + * presence of at least one negative priority, only connections + * with the lowest priority value will be used to determine the + * hostname. + * + * Since: 1.30 + **/ + /* ---ifcfg-rh--- + * property: priority + * variable: HOSTNAME_PRIORITY(+) + * default: missing variable means global value or 100 + * description: hostname priority + * example: HOSTNAME_PRIORITY=50 + * ---end--- + */ + obj_properties[PROP_PRIORITY] = g_param_spec_int(NM_SETTING_HOSTNAME_PRIORITY, + "", + "", + G_MININT32, + G_MAXINT32, + 0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + /** + * NMSettingHostname:from-dhcp + * + * Whether the system hostname can be determined from DHCP on + * this connection. + * + * When set to %NM_TERNARY_DEFAULT, the value from global configuration + * is used. If the property doesn't have a value in the global + * configuration, NetworkManager assumes the value to be %NM_TERNARY_TRUE. + * + * Since: 1.30 + **/ + /* ---ifcfg-rh--- + * property: from-dhcp + * variable: HOSTNAME_FROM_DHCP(+) + * default: missing variable means global default or 1 + * description: whether the system hostname can be determined from DHCP + * example: HOSTNAME_FROM_DHCP=0,1 + * ---end--- + */ + obj_properties[PROP_FROM_DHCP] = g_param_spec_enum( + NM_SETTING_HOSTNAME_FROM_DHCP, + "", + "", + NM_TYPE_TERNARY, + NM_TERNARY_DEFAULT, + NM_SETTING_PARAM_FUZZY_IGNORE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + /** + * NMSettingHostname:from-dns-lookup + * + * Whether the system hostname can be determined from reverse + * DNS lookup of addresses on this device. + * + * When set to %NM_TERNARY_DEFAULT, the value from global configuration + * is used. If the property doesn't have a value in the global + * configuration, NetworkManager assumes the value to be %NM_TERNARY_TRUE. + * + * Since: 1.30 + **/ + /* ---ifcfg-rh--- + * property: from-dhcp + * variable: HOSTNAME_FROM_DNS_LOOKUP(+) + * default: missing variable means global default or 1 + * description: whether the system hostname can be determined from reverse + * DNS lookup + * example: HOSTNAME_FROM_DNS_LOOKUP=0,1 + * ---end--- + */ + obj_properties[PROP_FROM_DNS_LOOKUP] = g_param_spec_enum( + NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP, + "", + "", + NM_TYPE_TERNARY, + NM_TERNARY_DEFAULT, + NM_SETTING_PARAM_FUZZY_IGNORE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + /** + * NMSettingHostname:only-from-default + * + * If set to %NM_TERNARY_TRUE, NetworkManager attempts to get + * the hostname via DHCPv4/DHCPv6 or reverse DNS lookup on this + * device only when the device has the default route for the given + * address family (IPv4/IPv6). + * + * If set to %NM_TERNARY_FALSE, the hostname can be set from this + * device even if it doesn't have the default route. + * + * When set to %NM_TERNARY_DEFAULT, the value from global configuration + * is used. If the property doesn't have a value in the global + * configuration, NetworkManager assumes the value to be %NM_TERNARY_TRUE. + * + * Since: 1.30 + **/ + /* ---ifcfg-rh--- + * property: only-best-device + * variable: HOSTNAME_ONLY_FROM_DEFAULT(+) + * default: missing variable means global default or 1 + * description: whether the hostname can be determined only from + * devices with the default route + * example: HOSTNAME_ONLY_FROM_DEFAULT=0,1 + * ---end--- + */ + obj_properties[PROP_ONLY_FROM_DEFAULT] = g_param_spec_enum( + NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT, + "", + "", + NM_TYPE_TERNARY, + NM_TERNARY_DEFAULT, + NM_SETTING_PARAM_FUZZY_IGNORE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties); + + _nm_setting_class_commit(setting_class, NM_META_SETTING_TYPE_HOSTNAME); +} diff --git a/libnm-core/nm-setting-hostname.h b/libnm-core/nm-setting-hostname.h new file mode 100644 index 0000000000..184d76c483 --- /dev/null +++ b/libnm-core/nm-setting-hostname.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +/* + * Copyright (C) 2020 Red Hat, Inc. + */ + +#ifndef NM_SETTING_HOSTNAME_H +#define NM_SETTING_HOSTNAME_H + +#if !defined(__NETWORKMANAGER_H_INSIDE__) && !defined(NETWORKMANAGER_COMPILATION) + #error "Only <NetworkManager.h> can be included directly." +#endif + +#include "nm-setting.h" + +G_BEGIN_DECLS + +#define NM_TYPE_SETTING_HOSTNAME (nm_setting_hostname_get_type()) +#define NM_SETTING_HOSTNAME(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_SETTING_HOSTNAME, NMSettingHostname)) +#define NM_SETTING_HOSTNAME_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_SETTING_HOSTNAME, NMSettingHostnameClass)) +#define NM_IS_SETTING_HOSTNAME(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_SETTING_HOSTNAME)) +#define NM_IS_SETTING_HOSTNAME_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_SETTING_HOSTNAME)) +#define NM_SETTING_HOSTNAME_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_SETTING_HOSTNAME, NMSettingHostnameClass)) + +#define NM_SETTING_HOSTNAME_SETTING_NAME "hostname" + +#define NM_SETTING_HOSTNAME_PRIORITY "priority" +#define NM_SETTING_HOSTNAME_FROM_DHCP "from-dhcp" +#define NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP "from-dns-lookup" +#define NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT "only-from-default" + +typedef struct _NMSettingHostnameClass NMSettingHostnameClass; + +NM_AVAILABLE_IN_1_30 +GType nm_setting_hostname_get_type(void); +NM_AVAILABLE_IN_1_30 +NMSetting *nm_setting_hostname_new(void); + +NM_AVAILABLE_IN_1_30 +int nm_setting_hostname_get_priority(NMSettingHostname *setting); +NM_AVAILABLE_IN_1_30 +NMTernary nm_setting_hostname_get_from_dhcp(NMSettingHostname *setting); +NM_AVAILABLE_IN_1_30 +NMTernary nm_setting_hostname_get_from_dns_lookup(NMSettingHostname *setting); +NM_AVAILABLE_IN_1_30 +NMTernary nm_setting_hostname_get_only_from_default(NMSettingHostname *setting); + +G_END_DECLS + +#endif /* NM_SETTING_HOSTNAME_H */ diff --git a/libnm/NetworkManager.h b/libnm/NetworkManager.h index 9193a08a0f..6864f7e84c 100644 --- a/libnm/NetworkManager.h +++ b/libnm/NetworkManager.h @@ -62,6 +62,7 @@ #include "nm-setting-ethtool.h" #include "nm-setting-generic.h" #include "nm-setting-gsm.h" +#include "nm-setting-hostname.h" #include "nm-setting-infiniband.h" #include "nm-setting-ip4-config.h" #include "nm-setting-ip6-config.h" diff --git a/libnm/libnm.ver b/libnm/libnm.ver index 0086fc01d2..100b9b93ee 100644 --- a/libnm/libnm.ver +++ b/libnm/libnm.ver @@ -1766,6 +1766,11 @@ global: nm_keyfile_read; nm_keyfile_warn_severity_get_type; nm_keyfile_write; + nm_setting_hostname_get_from_dhcp; + nm_setting_hostname_get_from_dns_lookup; + nm_setting_hostname_get_only_from_default; + nm_setting_hostname_get_priority; + nm_setting_hostname_get_type; nm_setting_ovs_external_ids_check_key; nm_setting_ovs_external_ids_check_val; nm_setting_ovs_external_ids_get_data; diff --git a/man/NetworkManager.conf.xml b/man/NetworkManager.conf.xml index 6b573890f8..a235cf2553 100644 --- a/man/NetworkManager.conf.xml +++ b/man/NetworkManager.conf.xml @@ -278,18 +278,24 @@ no-auto-default=* this option. An hostname empty or equal to 'localhost', 'localhost6', 'localhost.localdomain' or 'localhost6.localdomain' is considered invalid. </para> - <para><literal>default</literal>: NetworkManager will update the hostname - with the one provided via DHCP on the main connection (the one with a default - route). If not present, the hostname will be updated to the last one set - outside NetworkManager. If it is not valid, NetworkManager will try to recover - the hostname from the reverse lookup of the IP address of the main connection. - If this fails too, the hostname will be set to 'localhost.localdomain'. + <para><literal>default</literal>: NetworkManager will update the + hostname with the one provided via DHCP or reverse DNS lookup of the + IP address on the connection with the default route or on any + connection with the property hostname.only-from-default set to + '<literal>false</literal>'. Connections are considered in order of + increasing value of the <literal>hostname.priority</literal> + property. In case multiple connections have the same priority, + connections activated earlier are considered first. If no hostname can + be determined in such way, the hostname will be updated to the last + one set outside NetworkManager or to 'localhost.localdomain'. </para> - <para><literal>dhcp</literal>: NetworkManager will update the transient hostname - only with information coming from DHCP. No fallback nor reverse lookup will be - performed, but when the dhcp connection providing the hostname is deactivated, - the hostname is reset to the last hostname set outside NetworkManager or - 'localhost' if none valid is there. + <para><literal>dhcp</literal>: this is similar to + '<literal>default</literal>', with the difference that after trying to + get the DHCP hostname, reverse DNS lookup is not done. Note that + selecting this option is equivalent to setting the property + '<literal>hostname.from-dns-lookup</literal>' to + '<literal>false</literal>' globally for all connections in + NetworkManager.conf. </para> <para><literal>none</literal>: NetworkManager will not manage the transient hostname and will never set it. @@ -735,6 +741,18 @@ ipv6.ip6-privacy=0 <term><varname>gsm.mtu</varname></term> </varlistentry> <varlistentry> + <term><varname>hostname.from-dhcp</varname></term> + </varlistentry> + <varlistentry> + <term><varname>hostname.from-dns-lookup</varname></term> + </varlistentry> + <varlistentry> + <term><varname>hostname.only-from-default</varname></term> + </varlistentry> + <varlistentry> + <term><varname>hostname.priority</varname></term> + </varlistentry> + <varlistentry> <term><varname>infiniband.mtu</varname></term> <listitem><para>If configured explicitly to 0, the MTU is not reconfigured during device activation unless it is required due to IPv6 constraints. If left unspecified, a DHCP/IPv6 SLAAC provided value is used or the MTU is left unspecified on activation.</para></listitem> </varlistentry> diff --git a/shared/nm-meta-setting.c b/shared/nm-meta-setting.c index 5356b9a5de..c137e6208c 100644 --- a/shared/nm-meta-setting.c +++ b/shared/nm-meta-setting.c @@ -21,6 +21,7 @@ #include "nm-setting-ethtool.h" #include "nm-setting-generic.h" #include "nm-setting-gsm.h" +#include "nm-setting-hostname.h" #include "nm-setting-infiniband.h" #include "nm-setting-ip-config.h" #include "nm-setting-ip-tunnel.h" @@ -243,6 +244,13 @@ const NMMetaSettingInfo nm_meta_setting_infos[] = { .setting_name = NM_SETTING_GSM_SETTING_NAME, .get_setting_gtype = nm_setting_gsm_get_type, }, + [NM_META_SETTING_TYPE_HOSTNAME] = + { + .meta_type = NM_META_SETTING_TYPE_HOSTNAME, + .setting_priority = NM_SETTING_PRIORITY_IP, + .setting_name = NM_SETTING_HOSTNAME_SETTING_NAME, + .get_setting_gtype = nm_setting_hostname_get_type, + }, [NM_META_SETTING_TYPE_INFINIBAND] = { .meta_type = NM_META_SETTING_TYPE_INFINIBAND, diff --git a/shared/nm-meta-setting.h b/shared/nm-meta-setting.h index 9d268e0ec2..c1023fb5ab 100644 --- a/shared/nm-meta-setting.h +++ b/shared/nm-meta-setting.h @@ -119,6 +119,7 @@ typedef enum { NM_META_SETTING_TYPE_ETHTOOL, NM_META_SETTING_TYPE_GENERIC, NM_META_SETTING_TYPE_GSM, + NM_META_SETTING_TYPE_HOSTNAME, NM_META_SETTING_TYPE_INFINIBAND, NM_META_SETTING_TYPE_IP_TUNNEL, NM_META_SETTING_TYPE_IP4_CONFIG, diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index 582a1c3fec..9b7873c01b 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -2547,6 +2547,42 @@ make_ip6_setting(shvarFile *ifcfg, shvarFile *network_ifcfg, gboolean routes_rea } static NMSetting * +make_hostname_setting(shvarFile *ifcfg) +{ + NMSetting *setting; + NMTernary from_dhcp; + NMTernary from_dns_lookup; + NMTernary only_from_default; + int priority; + + priority = svGetValueInt64(ifcfg, "HOSTNAME_PRIORITY", 10, G_MININT32, G_MAXINT32, 0); + + from_dhcp = svGetValueTernary(ifcfg, "HOSTNAME_FROM_DHCP"); + from_dns_lookup = svGetValueTernary(ifcfg, "HOSTNAME_FROM_DNS_LOOKUP"); + only_from_default = svGetValueTernary(ifcfg, "HOSTNAME_ONLY_FROM_DEFAULT"); + + /* Create the setting when at least one key is not default*/ + if (priority == 0 && from_dhcp == NM_TERNARY_DEFAULT && from_dns_lookup == NM_TERNARY_DEFAULT + && only_from_default == NM_TERNARY_DEFAULT) + return NULL; + + setting = nm_setting_hostname_new(); + + g_object_set(setting, + NM_SETTING_HOSTNAME_PRIORITY, + priority, + NM_SETTING_HOSTNAME_FROM_DHCP, + from_dhcp, + NM_SETTING_HOSTNAME_FROM_DNS_LOOKUP, + from_dns_lookup, + NM_SETTING_HOSTNAME_ONLY_FROM_DEFAULT, + only_from_default, + NULL); + + return setting; +} + +static NMSetting * make_sriov_setting(shvarFile *ifcfg) { gs_unref_hashtable GHashTable *keys = NULL; @@ -2951,7 +2987,7 @@ make_dcb_setting(shvarFile *ifcfg, NMSetting **out_setting, GError **error) gboolean dcb_on; NMSettingDcbFlags flags = NM_SETTING_DCB_FLAG_NONE; - g_return_val_if_fail(out_setting != NULL, FALSE); + g_return_val_if_fail(out_setting, FALSE); *out_setting = NULL; dcb_on = !!svGetValueBoolean(ifcfg, "DCB", FALSE); @@ -6552,6 +6588,10 @@ connection_from_file_full(const char *filename, if (setting) nm_connection_add_setting(connection, setting); + setting = make_hostname_setting(main_ifcfg); + if (setting) + nm_connection_add_setting(connection, setting); + setting = make_user_setting(main_ifcfg); if (setting) nm_connection_add_setting(connection, setting); diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c index 567dc8a847..0ebc085330 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c @@ -873,6 +873,10 @@ const NMSIfcfgKeyTypeInfo nms_ifcfg_well_known_keys[] = { _KEY_TYPE("GATEWAY_PING_TIMEOUT", NMS_IFCFG_KEY_TYPE_IS_PLAIN), _KEY_TYPE("GENERATE_MAC_ADDRESS_MASK", NMS_IFCFG_KEY_TYPE_IS_PLAIN), _KEY_TYPE("GVRP", NMS_IFCFG_KEY_TYPE_IS_PLAIN), + _KEY_TYPE("HOSTNAME_FROM_DHCP", NMS_IFCFG_KEY_TYPE_IS_PLAIN), + _KEY_TYPE("HOSTNAME_FROM_DNS_LOOKUP", NMS_IFCFG_KEY_TYPE_IS_PLAIN), + _KEY_TYPE("HOSTNAME_ONLY_FROM_DEFAULT", NMS_IFCFG_KEY_TYPE_IS_PLAIN), + _KEY_TYPE("HOSTNAME_PRIORITY", NMS_IFCFG_KEY_TYPE_IS_PLAIN), _KEY_TYPE("HWADDR", NMS_IFCFG_KEY_TYPE_IS_PLAIN), _KEY_TYPE("HWADDR_BLACKLIST", NMS_IFCFG_KEY_TYPE_IS_PLAIN), _KEY_TYPE("IEEE_8021X_ALTSUBJECT_MATCHES", NMS_IFCFG_KEY_TYPE_IS_PLAIN), diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h index 16ea7f0f88..97111e76ea 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.h @@ -33,7 +33,7 @@ typedef struct { NMSIfcfgKeyTypeFlags key_flags; } NMSIfcfgKeyTypeInfo; -extern const NMSIfcfgKeyTypeInfo nms_ifcfg_well_known_keys[243]; +extern const NMSIfcfgKeyTypeInfo nms_ifcfg_well_known_keys[247]; const NMSIfcfgKeyTypeInfo *nms_ifcfg_well_known_key_find_info(const char *key, gssize *out_idx); diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c index 648846727f..ee78780282 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -1051,6 +1051,28 @@ write_infiniband_setting(NMConnection *connection, shvarFile *ifcfg, GError **er return TRUE; } +static void +write_hostname_setting(NMConnection *connection, shvarFile *ifcfg) +{ + NMSettingHostname *s_hostname; + NMTernary t; + + s_hostname = _nm_connection_get_setting(connection, NM_TYPE_SETTING_HOSTNAME); + if (!s_hostname) + return; + + svSetValueInt64(ifcfg, "HOSTNAME_PRIORITY", nm_setting_hostname_get_priority(s_hostname)); + + t = nm_setting_hostname_get_from_dhcp(s_hostname); + svSetValueInt64_cond(ifcfg, "HOSTNAME_FROM_DHCP", t != NM_TERNARY_DEFAULT, t); + + t = nm_setting_hostname_get_from_dns_lookup(s_hostname); + svSetValueInt64_cond(ifcfg, "HOSTNAME_FROM_DNS_LOOKUP", t != NM_TERNARY_DEFAULT, t); + + t = nm_setting_hostname_get_only_from_default(s_hostname); + svSetValueInt64_cond(ifcfg, "HOSTNAME_ONLY_FROM_DEFAULT", t != NM_TERNARY_DEFAULT, t); +} + static gboolean write_wired_setting(NMConnection *connection, shvarFile *ifcfg, GError **error) { @@ -3347,7 +3369,7 @@ do_write_construct(NMConnection * connection, return FALSE; write_match_setting(connection, ifcfg); - + write_hostname_setting(connection, ifcfg); write_sriov_setting(connection, ifcfg); if (!write_tc_setting(connection, ifcfg, error)) |