+
+ GVariant *params;
+ gchar *proposed_pretty_hostname, *valid_pretty_hostname_buf, *computed_static_hostname;
+ const gchar *bus_name;
+ gboolean policykit_auth, ret, try_to_set;
+ size_t check_length;
+ check_auth_result is_authed;
+ GKeyFile *config;
+
+ config = g_key_file_new();
+ proposed_pretty_hostname = NULL;
+ ret = try_to_set = FALSE;
+
+ params = g_dbus_method_invocation_get_parameters(invoc);
+ g_variant_get(params, "(sb)", &proposed_pretty_hostname, &policykit_auth);
+ bus_name = g_dbus_method_invocation_get_sender(invoc);
+
+ /* verify caller has correct permissions via polkit */
+ is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.SetPrettyHostname", policykit_auth);
+
+ switch(is_authed) {
+
+ case AUTHORIZED_NATIVELY:
+ case AUTHORIZED_BY_PROMPT:
+ try_to_set = TRUE;
+ break;
+
+ case UNAUTHORIZED_NATIVELY:
+ case UNAUTHORIZED_FAILED_PROMPT:
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EACCES", "Insufficient permissions to set pretty hostname.");
+ break;
+
+ case ERROR_BADBUS:
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided bus name is invalid.");
+ break;
+
+ case ERROR_BADACTION:
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.EFAULT", "Provided action ID is invalid.");
+ break;
+
+ case ERROR_GENERIC:
+ default:
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason.");
+ break;
+ }
+
+ /* verify passed hostname's validity */
+ if(try_to_set && proposed_pretty_hostname && (valid_pretty_hostname_buf = g_locale_to_utf8(proposed_pretty_hostname, -1, 0, 0, NULL))) {
+
+ check_length = strnlen(valid_pretty_hostname_buf, MAXHOSTNAMELEN + 1);
+
+ if(check_length > MAXHOSTNAMELEN) {
+
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ENAMETOOLONG", "Static hostname string exceeded maximum length.");
+ g_free(valid_pretty_hostname_buf);
+
+ } else if(!(PRETTY_HOSTNAME = valid_pretty_hostname_buf)) {
+
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set pretty hostname for unknown reason.");
+ g_free(valid_pretty_hostname_buf);
+
+ } else {
+
+ hostname1_set_pretty_hostname(hn1_passed_interf, PRETTY_HOSTNAME);
+ g_ptr_array_add(hostnamed_freeable, valid_pretty_hostname_buf);
+ hostname1_complete_set_pretty_hostname(hn1_passed_interf, invoc);
+ ret = TRUE;
+
+ if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)) {
+
+ g_key_file_set_string(config, "hostnamed", "PrettyHostname", valid_pretty_hostname_buf);
+
+ if((computed_static_hostname = g_hostname_to_ascii(PRETTY_HOSTNAME))) {
+
+ g_strdelimit(computed_static_hostname, " ", '-');
+ hostname1_set_static_hostname(hn1_passed_interf, computed_static_hostname);
+ STATIC_HOSTNAME = computed_static_hostname;
+ g_ptr_array_add(hostnamed_freeable, computed_static_hostname);
+ g_key_file_set_string(config, "hostnamed", "StaticHostname", computed_static_hostname);
+
+ } else
+ g_free(computed_static_hostname);
+ }
+
+ }
+ }
+
+ g_key_file_save_to_file(config, "/etc/systemd_compat.conf", NULL);
+ g_key_file_unref(config);
+
+ return ret;