1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
30 #include "dbus-common.h"
32 #define INTROSPECTION \
33 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
35 " <interface name=\"org.freedesktop.hostname1\">\n" \
36 " <property name=\"Hostname\" type=\"s\" access=\"read\"/>\n" \
37 " <property name=\"StaticHostname\" type=\"s\" access=\"read\"/>\n" \
38 " <property name=\"PrettyHostname\" type=\"s\" access=\"read\"/>\n" \
39 " <property name=\"IconName\" type=\"s\" access=\"read\"/>\n" \
40 " <method name=\"SetHostname\">\n" \
41 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
42 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
44 " <method name=\"SetStaticHostname\">\n" \
45 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
46 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
48 " <method name=\"SetPrettyHostname\">\n" \
49 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
50 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
52 " <method name=\"SetIconName\">\n" \
53 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
54 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
57 BUS_PROPERTIES_INTERFACE \
58 BUS_INTROSPECTABLE_INTERFACE \
62 #define INTERFACES_LIST \
63 BUS_GENERIC_INTERFACES_LIST \
64 "org.freedesktop.hostname1\0"
74 static char *data[_PROP_MAX] = {
81 static void free_data(void) {
84 for (p = 0; p < _PROP_MAX; p++) {
90 static int read_data(void) {
95 data[PROP_HOSTNAME] = gethostname_malloc();
96 if (!data[PROP_HOSTNAME])
99 r = read_one_line_file("/etc/hostname", &data[PROP_STATIC_HOSTNAME]);
100 if (r < 0 && r != -ENOENT)
103 r = parse_env_file("/etc/machine-info", NEWLINE,
104 "PRETTY_HOSTNAME", &data[PROP_PRETTY_HOSTNAME],
105 "ICON_NAME", &data[PROP_ICON_NAME],
107 if (r < 0 && r != -ENOENT)
113 static const char* fallback_icon_name(void) {
115 #if defined(__i386__) || defined(__x86_64__)
121 if (detect_virtualization(NULL) > 0)
122 return "computer-vm";
124 #if defined(__i386__) || defined(__x86_64__)
125 r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
129 r = safe_atou(type, &t);
135 /* We only list the really obvious cases here. The DMI data is
136 unreliable enough, so let's not do any additional guesswork
145 return "computer-desktop";
150 return "computer-laptop";
153 return "computer-server";
160 static int write_data_hostname(void) {
163 if (isempty(data[PROP_HOSTNAME]))
166 hn = data[PROP_HOSTNAME];
168 if (sethostname(hn, strlen(hn)) < 0)
174 static int write_data_static_hostname(void) {
176 if (isempty(data[PROP_STATIC_HOSTNAME])) {
178 if (unlink("/etc/hostname") < 0)
179 return errno == ENOENT ? 0 : -errno;
184 return write_one_line_file("/etc/hostname", data[PROP_STATIC_HOSTNAME]);
187 static int write_data_other(void) {
188 static const char * const name[_PROP_MAX] = {
189 [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
190 [PROP_ICON_NAME] = "ICON_NAME"
196 r = load_env_file("/etc/machine-info", &l);
197 if (r < 0 && r != -ENOENT)
200 for (p = 2; p < _PROP_MAX; p++) {
205 if (isempty(data[p])) {
206 l = strv_env_unset(l, name[p]);
210 if (asprintf(&t, "%s=%s", name[p], strempty(data[p])) < 0) {
215 u = strv_env_set(l, t);
224 if (strv_isempty(l)) {
226 if (unlink("/etc/machine-info") < 0)
227 return errno == ENOENT ? 0 : -errno;
232 r = write_env_file("/etc/machine-info", l);
238 /* This mimics dbus_bus_get_unix_user() */
239 static pid_t get_unix_process_id(
240 DBusConnection *connection,
244 DBusMessage *m = NULL, *reply = NULL;
247 m = dbus_message_new_method_call(
251 "GetConnectionUnixProcessID");
253 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
257 if (!dbus_message_append_args(
259 DBUS_TYPE_STRING, &name,
260 DBUS_TYPE_INVALID)) {
261 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, NULL);
265 reply = dbus_connection_send_with_reply_and_block(connection, m, -1, error);
269 if (dbus_set_error_from_message(error, reply))
272 if (!dbus_message_get_args(
274 DBUS_TYPE_UINT32, &pid,
280 dbus_message_unref(m);
283 dbus_message_unref(reply);
288 static int verify_polkit(
290 DBusMessage *request,
295 DBusMessage *m = NULL, *reply = NULL;
296 const char *unix_process = "unix-process", *pid = "pid", *starttime = "start-time", *cancel_id = "";
298 uint32_t flags = interactive ? 1 : 0;
301 unsigned long long starttime_raw;
302 uint64_t starttime_u64;
303 DBusMessageIter iter_msg, iter_struct, iter_array, iter_dict, iter_variant;
305 dbus_bool_t authorized = FALSE;
310 sender = dbus_message_get_sender(request);
314 pid_raw = get_unix_process_id(c, sender, error);
318 r = get_starttime_of_pid(pid_raw, &starttime_raw);
322 m = dbus_message_new_method_call(
323 "org.freedesktop.PolicyKit1",
324 "/org/freedesktop/PolicyKit1/Authority",
325 "org.freedesktop.PolicyKit1.Authority",
326 "CheckAuthorization");
330 dbus_message_iter_init_append(m, &iter_msg);
332 pid_u32 = (uint32_t) pid_raw;
333 starttime_u64 = (uint64_t) starttime_raw;
335 if (!dbus_message_iter_open_container(&iter_msg, DBUS_TYPE_STRUCT, NULL, &iter_struct) ||
336 !dbus_message_iter_append_basic(&iter_struct, DBUS_TYPE_STRING, &unix_process) ||
337 !dbus_message_iter_open_container(&iter_struct, DBUS_TYPE_ARRAY, "{sv}", &iter_array) ||
338 !dbus_message_iter_open_container(&iter_array, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict) ||
339 !dbus_message_iter_append_basic(&iter_dict, DBUS_TYPE_STRING, &pid) ||
340 !dbus_message_iter_open_container(&iter_dict, DBUS_TYPE_VARIANT, "u", &iter_variant) ||
341 !dbus_message_iter_append_basic(&iter_variant, DBUS_TYPE_UINT32, &pid_u32) ||
342 !dbus_message_iter_close_container(&iter_dict, &iter_variant) ||
343 !dbus_message_iter_close_container(&iter_array, &iter_dict) ||
344 !dbus_message_iter_open_container(&iter_array, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict) ||
345 !dbus_message_iter_append_basic(&iter_dict, DBUS_TYPE_STRING, &starttime) ||
346 !dbus_message_iter_open_container(&iter_dict, DBUS_TYPE_VARIANT, "t", &iter_variant) ||
347 !dbus_message_iter_append_basic(&iter_variant, DBUS_TYPE_UINT64, &starttime_u64) ||
348 !dbus_message_iter_close_container(&iter_dict, &iter_variant) ||
349 !dbus_message_iter_close_container(&iter_array, &iter_dict) ||
350 !dbus_message_iter_close_container(&iter_struct, &iter_array) ||
351 !dbus_message_iter_close_container(&iter_msg, &iter_struct) ||
352 !dbus_message_iter_append_basic(&iter_msg, DBUS_TYPE_STRING, &action) ||
353 !dbus_message_iter_open_container(&iter_msg, DBUS_TYPE_ARRAY, "{ss}", &iter_array) ||
354 !dbus_message_iter_close_container(&iter_msg, &iter_array) ||
355 !dbus_message_iter_append_basic(&iter_msg, DBUS_TYPE_UINT32, &flags) ||
356 !dbus_message_iter_append_basic(&iter_msg, DBUS_TYPE_STRING, &cancel_id)) {
361 reply = dbus_connection_send_with_reply_and_block(c, m, -1, error);
367 if (dbus_set_error_from_message(error, reply)) {
372 if (!dbus_message_iter_init(reply, &iter_msg) ||
373 dbus_message_iter_get_arg_type(&iter_msg) != DBUS_TYPE_STRUCT) {
378 dbus_message_iter_recurse(&iter_msg, &iter_struct);
380 if (dbus_message_iter_get_arg_type(&iter_struct) != DBUS_TYPE_BOOLEAN) {
385 dbus_message_iter_get_basic(&iter_struct, &authorized);
387 r = authorized ? 0 : -EPERM;
392 dbus_message_unref(m);
395 dbus_message_unref(reply);
400 static int bus_hostname_append_icon_name(DBusMessageIter *i, const char *property, void *userdata) {
406 if (isempty(data[PROP_ICON_NAME]))
407 name = fallback_icon_name();
409 name = data[PROP_ICON_NAME];
411 return bus_property_append_string(i, property, (void*) name);
414 static DBusHandlerResult hostname_message_handler(
415 DBusConnection *connection,
416 DBusMessage *message,
419 const BusProperty properties[] = {
420 { "org.freedesktop.hostname1", "Hostname", bus_property_append_string, "s", data[PROP_HOSTNAME]},
421 { "org.freedesktop.hostname1", "StaticHostname", bus_property_append_string, "s", data[PROP_STATIC_HOSTNAME]},
422 { "org.freedesktop.hostname1", "PrettyHostname", bus_property_append_string, "s", data[PROP_PRETTY_HOSTNAME]},
423 { "org.freedesktop.hostname1", "IconName", bus_hostname_append_icon_name, "s", data[PROP_ICON_NAME]},
424 { NULL, NULL, NULL, NULL, NULL }
427 DBusMessage *reply = NULL, *changed = NULL;
434 dbus_error_init(&error);
436 if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetHostname")) {
438 dbus_bool_t interactive;
440 if (!dbus_message_get_args(
443 DBUS_TYPE_STRING, &name,
444 DBUS_TYPE_BOOLEAN, &interactive,
446 return bus_send_error_reply(connection, message, &error, -EINVAL);
449 name = data[PROP_STATIC_HOSTNAME];
454 if (!hostname_is_valid(name))
455 return bus_send_error_reply(connection, message, NULL, -EINVAL);
457 if (!streq_ptr(name, data[PROP_HOSTNAME])) {
460 r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-hostname", interactive, &error);
462 return bus_send_error_reply(connection, message, &error, r);
468 free(data[PROP_HOSTNAME]);
469 data[PROP_HOSTNAME] = h;
471 r = write_data_hostname();
473 return bus_send_error_reply(connection, message, NULL, r);
475 log_info("Changed host name to '%s'", data[PROP_HOSTNAME]);
477 changed = bus_properties_changed_new(
478 "/org/freedesktop/hostname1",
479 "org.freedesktop.hostname1",
485 } else if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetStaticHostname")) {
487 dbus_bool_t interactive;
489 if (!dbus_message_get_args(
492 DBUS_TYPE_STRING, &name,
493 DBUS_TYPE_BOOLEAN, &interactive,
495 return bus_send_error_reply(connection, message, &error, -EINVAL);
500 if (!streq_ptr(name, data[PROP_STATIC_HOSTNAME])) {
502 r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-static-hostname", interactive, &error);
504 return bus_send_error_reply(connection, message, &error, r);
507 free(data[PROP_STATIC_HOSTNAME]);
508 data[PROP_STATIC_HOSTNAME] = NULL;
512 if (!hostname_is_valid(name))
513 return bus_send_error_reply(connection, message, NULL, -EINVAL);
519 free(data[PROP_STATIC_HOSTNAME]);
520 data[PROP_STATIC_HOSTNAME] = h;
523 r = write_data_static_hostname();
525 return bus_send_error_reply(connection, message, NULL, r);
527 log_info("Changed static host name to '%s'", data[PROP_HOSTNAME]);
529 changed = bus_properties_changed_new(
530 "/org/freedesktop/hostname1",
531 "org.freedesktop.hostname1",
537 } else if (dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetPrettyHostname") ||
538 dbus_message_is_method_call(message, "org.freedesktop.hostname1", "SetIconName")) {
541 dbus_bool_t interactive;
544 if (!dbus_message_get_args(
547 DBUS_TYPE_STRING, &name,
548 DBUS_TYPE_BOOLEAN, &interactive,
550 return bus_send_error_reply(connection, message, &error, -EINVAL);
555 k = streq(dbus_message_get_member(message), "SetPrettyHostname") ? PROP_PRETTY_HOSTNAME : PROP_ICON_NAME;
557 if (!streq_ptr(name, data[k])) {
559 r = verify_polkit(connection, message, "org.freedesktop.hostname1.set-machine-info", interactive, &error);
561 return bus_send_error_reply(connection, message, &error, r);
577 r = write_data_other();
579 return bus_send_error_reply(connection, message, NULL, r);
581 log_info("Changed %s to '%s'", k == PROP_PRETTY_HOSTNAME ? "pretty host name" : "icon name", data[k]);
583 changed = bus_properties_changed_new(
584 "/org/freedesktop/hostname1",
585 "org.freedesktop.hostname1",
586 k == PROP_PRETTY_HOSTNAME ? "PrettyHostname\0" : "IconName\0");
592 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
594 if (!(reply = dbus_message_new_method_return(message)))
597 if (!dbus_connection_send(connection, reply, NULL))
600 dbus_message_unref(reply);
605 if (!dbus_connection_send(connection, changed, NULL))
608 dbus_message_unref(changed);
611 return DBUS_HANDLER_RESULT_HANDLED;
615 dbus_message_unref(reply);
618 dbus_message_unref(changed);
620 dbus_error_free(&error);
622 return DBUS_HANDLER_RESULT_NEED_MEMORY;
625 int main(int argc, char *argv[]) {
626 const DBusObjectPathVTable hostname_vtable = {
627 .message_function = hostname_message_handler
630 DBusConnection *bus = NULL;
634 dbus_error_init(&error);
636 log_set_target(LOG_TARGET_AUTO);
637 log_parse_environment();
641 log_error("This program takes no arguments.");
650 log_error("Failed to read hostname data: %s", strerror(-r));
654 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
656 log_error("Failed to get system D-Bus connection: %s", error.message);
661 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL)) {
662 log_error("Not enough memory");
667 if (dbus_bus_request_name(bus, "org.freedesktop.hostname1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
668 log_error("Failed to register name on bus: %s", error.message);
672 while (dbus_connection_read_write_dispatch(bus, -1))
681 dbus_connection_flush(bus);
682 dbus_connection_close(bus);
683 dbus_connection_unref(bus);
686 dbus_error_free(&error);
688 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;