+ 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;
+}
+
+static gboolean
+on_handle_set_chassis(Hostname1 *hn1_passed_interf,
+ GDBusMethodInvocation *invoc,
+ const gchar *greet,
+ gpointer data) {
+
+ GVariant *params;
+ gchar *proposed_chassis_name, *valid_chassis_name_buf;
+ const gchar *bus_name;
+ gboolean policykit_auth, ret, try_to_set;
+ check_auth_result is_authed;
+ GKeyFile *config;
+
+ config = g_key_file_new();
+ proposed_chassis_name = NULL;
+ ret = try_to_set = FALSE;
+ valid_chassis_name_buf = (gchar *)g_malloc0(8192);
+
+ params = g_dbus_method_invocation_get_parameters(invoc);
+ g_variant_get(params, "(sb)", &proposed_chassis_name, &policykit_auth);
+ bus_name = g_dbus_method_invocation_get_sender(invoc);
+
+ g_strlcpy(valid_chassis_name_buf, proposed_chassis_name, (gsize)64);
+
+ /* verify caller has correct permissions via polkit */
+ is_authed = polkit_try_auth(bus_name, "org.freedesktop.hostname1.SetChassis", 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 chassis type.");
+ 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 chassis type for unknown reason.");
+ break;
+ }
+
+ /* verify passed chassis type's validity */
+ if(try_to_set && proposed_chassis_name) {
+
+ if(!is_valid_chassis_type(proposed_chassis_name)) {
+
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Chassis type must be 'desktop', 'laptop', 'server', 'tablet', 'handset', 'vm', or 'container'.");
+ g_free(valid_chassis_name_buf);
+
+ } else if(!(CHASSIS = valid_chassis_name_buf)) {
+
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set chassis type for unknown reason.");
+ g_free(valid_chassis_name_buf);
+
+ } else {
+
+ hostname1_set_chassis(hn1_passed_interf, CHASSIS);
+ g_ptr_array_add(hostnamed_freeable, valid_chassis_name_buf);
+ hostname1_complete_set_chassis(hn1_passed_interf, invoc);
+
+ if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)) {
+
+ ret = TRUE;
+ g_key_file_set_string(config, "hostnamed", "ChassisType", valid_chassis_name_buf);
+
+ }
+ }
+ }
+
+ g_key_file_save_to_file(config, "/etc/systemd_compat.conf", NULL);
+ g_key_file_unref(config);
+
+ return ret;
+}
+
+static gboolean
+on_handle_set_icon_name(Hostname1 *hn1_passed_interf,
+ GDBusMethodInvocation *invoc,
+ const gchar *greet,
+ gpointer data) {
+
+ GVariant *params;
+ gchar *proposed_icon_name, *valid_icon_name_buf;
+ const gchar *bus_name;
+ gboolean policykit_auth, ret, try_to_set;
+ check_auth_result is_authed;
+ GKeyFile *config;
+
+ config = g_key_file_new();
+ proposed_icon_name = NULL;
+ ret = try_to_set = FALSE;
+
+ params = g_dbus_method_invocation_get_parameters(invoc);
+ g_variant_get(params, "(sb)", &proposed_icon_name, &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.SetIconName", 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 icon name.");
+ 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 icon name for unknown reason.");
+ break;
+ }
+
+ /* verify passed chassis type's validity */
+ if(try_to_set && proposed_icon_name) {
+
+ g_strlcpy(valid_icon_name_buf, proposed_icon_name, (gsize)64);
+
+ if(!(ICON = valid_icon_name_buf)) {
+
+ g_dbus_method_invocation_return_dbus_error(invoc, "org.freedesktop.hostname1.Error.ECANCELED", "Failed to set icon name for unknown reason.");
+ g_free(valid_icon_name_buf);
+
+ } else {
+
+ hostname1_set_icon_name(hn1_passed_interf, ICON);
+ g_ptr_array_add(hostnamed_freeable, valid_icon_name_buf);
+ hostname1_complete_set_icon_name(hn1_passed_interf, invoc);
+
+ if(g_key_file_load_from_file(config, "/etc/systemd_compat.conf", G_KEY_FILE_NONE, NULL)) {
+
+ ret = TRUE;
+ g_key_file_set_string(config, "hostnamed", "IconName", valid_icon_name_buf);
+
+ }
+ }
+ }
+
+ g_key_file_save_to_file(config, "/etc/systemd_compat.conf", NULL);
+ g_key_file_unref(config);
+
+ return ret;
+}
+
+/* note: all hostnamed/hostname1's properties are read-only,
+ * and do not need set_ functions, gdbus-codegen realized
+ * this from the XML and handled the to-be error of trying
+ * to set a read-only property's value
+ */
+
+const gchar *
+our_get_hostname() {
+
+ gchar *hostname_buf;
+ hostname_buf = (gchar *)g_malloc0(MAXHOSTNAMELEN);
+
+ if(gethostname(hostname_buf, MAXHOSTNAMELEN))
+ return "localhost.home.network"; /* TODO bomb out here probably */
+
+ else if(!g_strcmp0(HOSTNAME, hostname_buf)) {
+
+ g_free(hostname_buf);
+ return HOSTNAME;
+ }
+
+ g_ptr_array_add(hostnamed_freeable, hostname_buf);
+ HOSTNAME = hostname_buf;
+ hostname1_set_hostname(hostnamed_interf, HOSTNAME);
+
+ return HOSTNAME;
+}
+
+const gchar *
+our_get_static_hostname() {
+
+ if(STATIC_HOSTNAME && g_strcmp0(STATIC_HOSTNAME, ""))
+ return STATIC_HOSTNAME;
+ else if(HOSTNAME)
+ return HOSTNAME;
+
+ return "localhost.home.network";
+}
+
+const gchar *
+our_get_pretty_hostname() {
+
+ if(PRETTY_HOSTNAME)
+ return PRETTY_HOSTNAME;
+
+ return "";
+}
+
+const gchar *
+our_get_chassis() {
+
+ if(CHASSIS)
+ return CHASSIS;
+
+ return "desktop"; /* this leads to the most generic beheivor in the unlikely case its returned */
+}
+
+const gchar *
+our_get_icon_name() {
+
+ if(ICON)
+ return ICON;
+
+ return "";
+}
+
+const gchar *
+our_get_kernel_name() {
+
+ if(KERN_NAME)
+ return KERN_NAME;
+
+ return "";
+}
+
+const gchar *
+our_get_kernel_version() {
+
+ if(KERN_VERS)
+ return KERN_VERS;
+
+ return "";
+}
+
+const gchar *
+our_get_kernel_release() {
+
+ if(KERN_RELEASE)
+ return KERN_RELEASE;
+
+ return "";
+}
+
+const gchar *
+our_get_os_cpename() {
+
+ return "ONEDAY";
+}
+
+const gchar *
+our_get_os_pretty_name() {
+
+ return "OpenBSD";
+}
+
+/* --- end method/property/dbus signal code, begin bus/name handlers --- */
+
+static void hostnamed_on_bus_acquired(GDBusConnection *conn,
+ const gchar *name,
+ gpointer user_data) {
+
+ g_printf("got bus/name, exporting %s's interface...\n", name);
+
+ hostnamed_interf = hostname1_skeleton_new();
+
+ /* attach function pointers to generated struct's method handlers */
+ g_signal_connect(hostnamed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
+ g_signal_connect(hostnamed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
+ g_signal_connect(hostnamed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
+ g_signal_connect(hostnamed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
+ g_signal_connect(hostnamed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL);
+
+ /* set our properties before export */
+ hostname1_set_hostname(hostnamed_interf, our_get_hostname());
+ hostname1_set_static_hostname(hostnamed_interf, our_get_static_hostname());
+ hostname1_set_pretty_hostname(hostnamed_interf, our_get_pretty_hostname());
+ hostname1_set_chassis(hostnamed_interf, our_get_chassis());
+ hostname1_set_icon_name(hostnamed_interf, our_get_icon_name());
+ hostname1_set_kernel_name(hostnamed_interf, our_get_kernel_name());
+ hostname1_set_kernel_version(hostnamed_interf, our_get_kernel_version());
+ hostname1_set_kernel_release(hostnamed_interf, our_get_kernel_release());
+ hostname1_set_operating_system_cpename(hostnamed_interf, our_get_os_cpename());
+ hostname1_set_operating_system_pretty_name(hostnamed_interf, our_get_os_pretty_name());
+
+ if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(hostnamed_interf),
+ conn,
+ "/org/freedesktop/hostname1",
+ NULL)) {
+
+ g_printf("failed to export %s's interface!\n", name); /* unusual edge case, TODO check errno */
+ hostnamed_mem_clean();
+
+ } else {
+
+ dbus_interface_exported = TRUE;
+ g_printf("exported %s's interface on the system bus...\n", name);
+ }
+}
+
+static void hostnamed_on_name_acquired(GDBusConnection *conn,
+ const gchar *name,
+ gpointer user_data) {
+
+ g_printf("success!\n");
+}
+
+static void hostnamed_on_name_lost(GDBusConnection *conn,
+ const gchar *name,
+ gpointer user_data) {
+
+ if(!conn) {
+
+ g_printf("failed to connect to the system bus while trying to acquire name '%s': either dbus-daemon isn't running or we don't have permission to push names and/or their interfaces to it.\n", name);
+ hostnamed_mem_clean();
+ }
+
+ g_printf("lost name %s, exiting...\n", name);