summary refs log tree commit diff
path: root/src/core/nm-hostname-manager.c
diff options
context:
space:
mode:
authorMichael Biebl <biebl@debian.org>2022-02-14 19:23:28 +0100
committerMichael Biebl <biebl@debian.org>2022-02-14 19:23:28 +0100
commit2f94dba7385fd0e0ef19a06eb4a2fcf6c43d7946 (patch)
treee2222f5577115985dd52044d2991253403cdd952 /src/core/nm-hostname-manager.c
parent88c227d90a6b7b388c5c85d72802a0ca8f05ed5c (diff)
New upstream version 1.35.91 upstream/1.35.91
Diffstat (limited to 'src/core/nm-hostname-manager.c')
-rw-r--r--src/core/nm-hostname-manager.c129
1 files changed, 72 insertions, 57 deletions
diff --git a/src/core/nm-hostname-manager.c b/src/core/nm-hostname-manager.c
index 32dc2db9..64c2531e 100644
--- a/src/core/nm-hostname-manager.c
+++ b/src/core/nm-hostname-manager.c
@@ -52,15 +52,15 @@
 
 /*****************************************************************************/
 
-NM_GOBJECT_PROPERTIES_DEFINE(NMHostnameManager, PROP_HOSTNAME, );
+NM_GOBJECT_PROPERTIES_DEFINE(NMHostnameManager, PROP_STATIC_HOSTNAME, );
 
 typedef struct {
-    char *        current_hostname;
+    char         *static_hostname;
     GFileMonitor *monitor;
     GFileMonitor *dhcp_monitor;
     gulong        monitor_id;
     gulong        dhcp_monitor_id;
-    GDBusProxy *  hostnamed_proxy;
+    GDBusProxy   *hostnamed_proxy;
 } NMHostnameManagerPrivate;
 
 struct _NMHostnameManager {
@@ -103,9 +103,9 @@ _file_monitor_new(const char *path)
 static char *
 read_hostname_gentoo(const char *path)
 {
-    gs_free char *     contents  = NULL;
+    gs_free char      *contents  = NULL;
     gs_strfreev char **all_lines = NULL;
-    const char *       tmp;
+    const char        *tmp;
     guint              i;
 
     if (!g_file_get_contents(path, &contents, NULL, NULL))
@@ -129,7 +129,7 @@ read_hostname_gentoo(const char *path)
 static char *
 read_hostname_slackware(const char *path)
 {
-    gs_free char *     contents  = NULL;
+    gs_free char      *contents  = NULL;
     gs_strfreev char **all_lines = NULL;
     guint              i         = 0;
 
@@ -152,7 +152,7 @@ static gboolean
 hostname_is_dynamic(void)
 {
     GIOChannel *channel;
-    char *      str     = NULL;
+    char       *str     = NULL;
     gboolean    dynamic = FALSE;
 
     channel = g_io_channel_new_file(CONF_DHCP, "r", NULL);
@@ -178,39 +178,53 @@ hostname_is_dynamic(void)
 /*****************************************************************************/
 
 const char *
-nm_hostname_manager_get_hostname(NMHostnameManager *self)
+nm_hostname_manager_get_static_hostname(NMHostnameManager *self)
 {
     g_return_val_if_fail(NM_IS_HOSTNAME_MANAGER(self), NULL);
-    return NM_HOSTNAME_MANAGER_GET_PRIVATE(self)->current_hostname;
+
+    return NM_HOSTNAME_MANAGER_GET_PRIVATE(self)->static_hostname;
 }
 
 static void
 _set_hostname(NMHostnameManager *self, const char *hostname)
 {
-    NMHostnameManagerPrivate *priv = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
-    char *                    old_hostname;
+    NMHostnameManagerPrivate *priv          = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
+    gs_free char             *hostname_free = NULL;
+    char                     *old_hostname;
 
     hostname = nm_str_not_empty(hostname);
 
-    if (nm_streq0(hostname, priv->current_hostname))
+    if (hostname) {
+        /* as we also read the file from disk, it might not be in UTF-8 encoding.
+         *
+         * A hostname in non-UTF-8 encoding would be odd and cause issues when we
+         * try to expose them on D-Bus via the NM_SETTINGS_STATIC_HOSTNAME property.
+         *
+         * Sanitize somewhat. It's wrong anyway. */
+        hostname = nm_utils_str_utf8safe_escape(hostname,
+                                                NM_UTILS_STR_UTF8_SAFE_FLAG_ESCAPE_CTRL,
+                                                &hostname_free);
+    }
+
+    if (nm_streq0(hostname, priv->static_hostname))
         return;
 
-    _LOGI("hostname changed from %s%s%s to %s%s%s",
-          NM_PRINT_FMT_QUOTED(priv->current_hostname, "\"", priv->current_hostname, "\"", "(none)"),
+    _LOGI("static hostname changed from %s%s%s to %s%s%s",
+          NM_PRINT_FMT_QUOTED(priv->static_hostname, "\"", priv->static_hostname, "\"", "(none)"),
           NM_PRINT_FMT_QUOTED(hostname, "\"", hostname, "\"", "(none)"));
 
-    old_hostname           = priv->current_hostname;
-    priv->current_hostname = g_strdup(hostname);
+    old_hostname          = priv->static_hostname;
+    priv->static_hostname = g_strdup(hostname);
     g_free(old_hostname);
 
-    _notify(self, PROP_HOSTNAME);
+    _notify(self, PROP_STATIC_HOSTNAME);
 }
 
 static void
 _set_hostname_read_file(NMHostnameManager *self)
 {
     NMHostnameManagerPrivate *priv     = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
-    gs_free char *            hostname = NULL;
+    gs_free char             *hostname = NULL;
 
     if (priv->hostnamed_proxy) {
         /* read-hostname returns the current hostname with hostnamed. */
@@ -239,10 +253,10 @@ _set_hostname_read_file(NMHostnameManager *self)
 static void
 set_transient_hostname_done(GObject *object, GAsyncResult *res, gpointer user_data)
 {
-    GDBusProxy *     proxy                  = G_DBUS_PROXY(object);
-    gs_unref_variant GVariant *result       = NULL;
-    gs_free_error GError *         error    = NULL;
-    gs_free char *                 hostname = NULL;
+    GDBusProxy                    *proxy    = G_DBUS_PROXY(object);
+    gs_unref_variant GVariant     *result   = NULL;
+    gs_free_error GError          *error    = NULL;
+    gs_free char                  *hostname = NULL;
     NMHostnameManagerSetHostnameCb cb;
     gpointer                       cb_user_data;
 
@@ -260,8 +274,8 @@ set_transient_hostname_done(GObject *object, GAsyncResult *res, gpointer user_da
 }
 
 void
-nm_hostname_manager_set_transient_hostname(NMHostnameManager *            self,
-                                           const char *                   hostname,
+nm_hostname_manager_set_transient_hostname(NMHostnameManager             *self,
+                                           const char                    *hostname,
                                            NMHostnameManagerSetHostnameCb cb,
                                            gpointer                       user_data)
 {
@@ -290,7 +304,7 @@ gboolean
 nm_hostname_manager_get_transient_hostname(NMHostnameManager *self, char **hostname)
 {
     NMHostnameManagerPrivate *priv = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
-    GVariant *                v_hostname;
+    GVariant                 *v_hostname;
 
     if (!priv->hostnamed_proxy)
         return FALSE;
@@ -310,17 +324,17 @@ nm_hostname_manager_get_transient_hostname(NMHostnameManager *self, char **hostn
 gboolean
 nm_hostname_manager_write_hostname(NMHostnameManager *self, const char *hostname)
 {
-    NMHostnameManagerPrivate *priv;
-    char *                    hostname_eol;
-    gboolean                  ret;
-    gs_free_error GError *error     = NULL;
-    const char *          file      = HOSTNAME_FILE;
-    gs_free char *        link_path = NULL;
-    gs_unref_variant GVariant *var  = NULL;
+    NMHostnameManagerPrivate  *priv;
+    char                      *hostname_eol;
+    gboolean                   ret;
+    gs_free_error GError      *error     = NULL;
+    const char                *file      = HOSTNAME_FILE;
+    gs_free char              *link_path = NULL;
+    gs_unref_variant GVariant *var       = NULL;
     struct stat                file_stat;
 #if HAVE_SELINUX
     gboolean fcon_was_set = FALSE;
-    char *   fcon_prev    = NULL;
+    char    *fcon_prev    = NULL;
 #endif
 
     g_return_val_if_fail(NM_IS_HOSTNAME_MANAGER(self), FALSE);
@@ -365,7 +379,7 @@ nm_hostname_manager_write_hostname(NMHostnameManager *self, const char *hostname
         handle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
         if (handle) {
             mode_t st_mode = 0;
-            char * fcon    = NULL;
+            char  *fcon    = NULL;
 
             if (stat(file, &file_stat) == 0)
                 st_mode = file_stat.st_mode;
@@ -406,12 +420,12 @@ nm_hostname_manager_write_hostname(NMHostnameManager *self, const char *hostname
 
 static void
 hostnamed_properties_changed(GDBusProxy *proxy,
-                             GVariant *  changed_properties,
-                             char **     invalidated_properties,
+                             GVariant   *changed_properties,
+                             char      **invalidated_properties,
                              gpointer    user_data)
 {
-    NMHostnameManager *       self     = user_data;
-    NMHostnameManagerPrivate *priv     = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
+    NMHostnameManager         *self    = user_data;
+    NMHostnameManagerPrivate  *priv    = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
     gs_unref_variant GVariant *variant = NULL;
 
     variant = g_dbus_proxy_get_cached_property(priv->hostnamed_proxy, "StaticHostname");
@@ -422,9 +436,9 @@ hostnamed_properties_changed(GDBusProxy *proxy,
 /*****************************************************************************/
 
 static void
-_file_monitors_file_changed_cb(GFileMonitor *    monitor,
-                               GFile *           file,
-                               GFile *           other_file,
+_file_monitors_file_changed_cb(GFileMonitor     *monitor,
+                               GFile            *file,
+                               GFile            *other_file,
                                GFileMonitorEvent event_type,
                                gpointer          user_data)
 {
@@ -453,9 +467,9 @@ static void
 _file_monitors_setup(NMHostnameManager *self)
 {
     NMHostnameManagerPrivate *priv = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
-    GFileMonitor *            monitor;
-    const char *              path      = HOSTNAME_FILE;
-    gs_free char *            link_path = NULL;
+    GFileMonitor             *monitor;
+    const char               *path      = HOSTNAME_FILE;
+    gs_free char             *link_path = NULL;
     struct stat               file_stat;
 
     _file_monitors_clear(self);
@@ -499,8 +513,8 @@ get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
     NMHostnameManager *self = NM_HOSTNAME_MANAGER(object);
 
     switch (prop_id) {
-    case PROP_HOSTNAME:
-        g_value_set_string(value, nm_hostname_manager_get_hostname(self));
+    case PROP_STATIC_HOSTNAME:
+        g_value_set_string(value, nm_hostname_manager_get_static_hostname(self));
         break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
@@ -517,11 +531,11 @@ nm_hostname_manager_init(NMHostnameManager *self)
 static void
 constructed(GObject *object)
 {
-    NMHostnameManager *       self = NM_HOSTNAME_MANAGER(object);
+    NMHostnameManager        *self = NM_HOSTNAME_MANAGER(object);
     NMHostnameManagerPrivate *priv = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
-    GDBusProxy *              proxy;
-    GVariant *                variant;
-    gs_free_error GError *error = NULL;
+    GDBusProxy               *proxy;
+    GVariant                 *variant;
+    gs_free_error GError     *error = NULL;
 
     proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
                                           0,
@@ -560,7 +574,7 @@ constructed(GObject *object)
 static void
 dispose(GObject *object)
 {
-    NMHostnameManager *       self = NM_HOSTNAME_MANAGER(object);
+    NMHostnameManager        *self = NM_HOSTNAME_MANAGER(object);
     NMHostnameManagerPrivate *priv = NM_HOSTNAME_MANAGER_GET_PRIVATE(self);
 
     if (priv->hostnamed_proxy) {
@@ -572,7 +586,7 @@ dispose(GObject *object)
 
     _file_monitors_clear(self);
 
-    nm_clear_g_free(&priv->current_hostname);
+    nm_clear_g_free(&priv->static_hostname);
 
     G_OBJECT_CLASS(nm_hostname_manager_parent_class)->dispose(object);
 }
@@ -586,11 +600,12 @@ nm_hostname_manager_class_init(NMHostnameManagerClass *class)
     object_class->get_property = get_property;
     object_class->dispose      = dispose;
 
-    obj_properties[PROP_HOSTNAME] = g_param_spec_string(NM_HOSTNAME_MANAGER_HOSTNAME,
-                                                        "",
-                                                        "",
-                                                        NULL,
-                                                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+    obj_properties[PROP_STATIC_HOSTNAME] =
+        g_param_spec_string(NM_HOSTNAME_MANAGER_STATIC_HOSTNAME,
+                            "",
+                            "",
+                            NULL,
+                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
     g_object_class_install_properties(object_class, _PROPERTY_ENUMS_LAST, obj_properties);
 }