1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <sys/utsname.h>
9 #include "alloc-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-get-properties.h"
12 #include "bus-log-control-api.h"
13 #include "bus-polkit.h"
15 #include "env-file-label.h"
18 #include "fileio-label.h"
20 #include "hostname-setup.h"
21 #include "hostname-util.h"
22 #include "id128-util.h"
24 #include "main-func.h"
25 #include "missing_capability.h"
26 #include "nscd-flush.h"
27 #include "nulstr-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "sd-device.h"
32 #include "selinux-util.h"
33 #include "service-util.h"
34 #include "signal-util.h"
35 #include "stat-util.h"
36 #include "string-table.h"
38 #include "user-util.h"
42 #define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:")
44 /* Properties we cache are indexed by an enum, to make invalidation easy and systematic (as we can iterate
45 * through them all, and they are uniformly strings). */
47 /* Read from /etc/hostname */
50 /* Read from /etc/machine-info */
59 /* Read from /etc/os-release (or /usr/lib/os-release) */
64 _PROP_INVALID
= -EINVAL
,
67 typedef struct Context
{
68 char *data
[_PROP_MAX
];
70 HostnameSource hostname_source
;
72 struct stat etc_hostname_stat
;
73 struct stat etc_os_release_stat
;
74 struct stat etc_machine_info_stat
;
76 Hashmap
*polkit_registry
;
79 static void context_reset(Context
*c
, uint64_t mask
) {
82 for (int p
= 0; p
< _PROP_MAX
; p
++) {
83 if (!FLAGS_SET(mask
, UINT64_C(1) << p
))
86 c
->data
[p
] = mfree(c
->data
[p
]);
90 static void context_destroy(Context
*c
) {
93 context_reset(c
, UINT64_MAX
);
94 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
97 static void context_read_etc_hostname(Context
*c
) {
98 struct stat current_stat
= {};
103 if (stat("/etc/hostname", ¤t_stat
) >= 0 &&
104 stat_inode_unmodified(&c
->etc_hostname_stat
, ¤t_stat
))
107 context_reset(c
, UINT64_C(1) << PROP_STATIC_HOSTNAME
);
109 r
= read_etc_hostname(NULL
, &c
->data
[PROP_STATIC_HOSTNAME
]);
110 if (r
< 0 && r
!= -ENOENT
)
111 log_warning_errno(r
, "Failed to read /etc/hostname, ignoring: %m");
113 c
->etc_hostname_stat
= current_stat
;
116 static void context_read_machine_info(Context
*c
) {
117 struct stat current_stat
= {};
122 if (stat("/etc/machine-info", ¤t_stat
) >= 0 &&
123 stat_inode_unmodified(&c
->etc_machine_info_stat
, ¤t_stat
))
127 (UINT64_C(1) << PROP_PRETTY_HOSTNAME
) |
128 (UINT64_C(1) << PROP_ICON_NAME
) |
129 (UINT64_C(1) << PROP_CHASSIS
) |
130 (UINT64_C(1) << PROP_DEPLOYMENT
) |
131 (UINT64_C(1) << PROP_LOCATION
) |
132 (UINT64_C(1) << PROP_HARDWARE_VENDOR
) |
133 (UINT64_C(1) << PROP_HARDWARE_MODEL
));
135 r
= parse_env_file(NULL
, "/etc/machine-info",
136 "PRETTY_HOSTNAME", &c
->data
[PROP_PRETTY_HOSTNAME
],
137 "ICON_NAME", &c
->data
[PROP_ICON_NAME
],
138 "CHASSIS", &c
->data
[PROP_CHASSIS
],
139 "DEPLOYMENT", &c
->data
[PROP_DEPLOYMENT
],
140 "LOCATION", &c
->data
[PROP_LOCATION
],
141 "HARDWARE_VENDOR", &c
->data
[PROP_HARDWARE_VENDOR
],
142 "HARDWARE_MODEL", &c
->data
[PROP_HARDWARE_MODEL
]);
143 if (r
< 0 && r
!= -ENOENT
)
144 log_warning_errno(r
, "Failed to read /etc/machine-info, ignoring: %m");
146 c
->etc_machine_info_stat
= current_stat
;
149 static void context_read_os_release(Context
*c
) {
150 struct stat current_stat
= {};
155 if ((stat("/etc/os-release", ¤t_stat
) >= 0 ||
156 stat("/usr/lib/os-release", ¤t_stat
) >= 0) &&
157 stat_inode_unmodified(&c
->etc_os_release_stat
, ¤t_stat
))
161 (UINT64_C(1) << PROP_OS_PRETTY_NAME
) |
162 (UINT64_C(1) << PROP_OS_CPE_NAME
) |
163 (UINT64_C(1) << PROP_OS_HOME_URL
));
165 r
= parse_os_release(NULL
,
166 "PRETTY_NAME", &c
->data
[PROP_OS_PRETTY_NAME
],
167 "CPE_NAME", &c
->data
[PROP_OS_CPE_NAME
],
168 "HOME_URL", &c
->data
[PROP_OS_HOME_URL
]);
169 if (r
< 0 && r
!= -ENOENT
)
170 log_warning_errno(r
, "Failed to read os-release file, ignoring: %m");
172 c
->etc_os_release_stat
= current_stat
;
175 static int get_dmi_data(const char *database_key
, const char *regular_key
, char **ret
) {
176 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
177 _cleanup_free_
char *b
= NULL
;
178 const char *s
= NULL
;
181 r
= sd_device_new_from_syspath(&device
, "/sys/class/dmi/id");
183 return log_debug_errno(r
, "Failed to open /sys/class/dmi/id device, ignoring: %m");
186 (void) sd_device_get_property_value(device
, database_key
, &s
);
187 if (!s
&& regular_key
)
188 (void) sd_device_get_property_value(device
, regular_key
, &s
);
203 static int get_hardware_vendor(char **ret
) {
204 return get_dmi_data("ID_VENDOR_FROM_DATABASE", "ID_VENDOR", ret
);
207 static int get_hardware_model(char **ret
) {
208 return get_dmi_data("ID_MODEL_FROM_DATABASE", "ID_MODEL", ret
);
211 static int get_hardware_firmware_data(const char *sysattr
, char **ret
) {
212 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
213 _cleanup_free_
char *b
= NULL
;
214 const char *s
= NULL
;
219 r
= sd_device_new_from_syspath(&device
, "/sys/class/dmi/id");
221 return log_debug_errno(r
, "Failed to open /sys/class/dmi/id device, ignoring: %m");
223 (void) sd_device_get_sysattr_value(device
, sysattr
, &s
);
236 static int get_hardware_serial(char **ret
) {
239 r
= get_hardware_firmware_data("product_serial", ret
);
241 return get_hardware_firmware_data("board_serial", ret
);
246 static int get_firmware_version(char **ret
) {
247 return get_hardware_firmware_data("bios_version", ret
);
250 static const char* valid_chassis(const char *chassis
) {
267 static bool valid_deployment(const char *deployment
) {
270 return in_charset(deployment
, VALID_DEPLOYMENT_CHARS
);
273 static const char* fallback_chassis(void) {
275 _cleanup_free_
char *type
= NULL
;
280 v
= detect_virtualization();
282 log_debug_errno(v
, "Failed to detect virtualization, ignoring: %m");
283 else if (VIRTUALIZATION_IS_VM(v
))
285 else if (VIRTUALIZATION_IS_CONTAINER(v
))
288 r
= read_one_line_file("/sys/class/dmi/id/chassis_type", &type
);
290 log_debug_errno(r
, "Failed to read DMI chassis type, ignoring: %m");
294 r
= safe_atou(type
, &t
);
296 log_debug_errno(r
, "Failed to parse DMI chassis type \"%s\", ignoring: %m", type
);
300 /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any
301 * additional guesswork on top of that.
303 * See the SMBIOS Specification 3.5.0 section 7.4.1 for details about the values listed here:
305 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.5.0.pdf
310 case 0x03: /* Desktop */
311 case 0x04: /* Low Profile Desktop */
312 case 0x06: /* Mini Tower */
313 case 0x07: /* Tower */
314 case 0x0D: /* All in one (i.e. PC built into monitor) */
315 case 0x23: /* Mini PC */
316 case 0x24: /* Stick PC */
319 case 0x8: /* Portable */
320 case 0x9: /* Laptop */
321 case 0xA: /* Notebook */
322 case 0xE: /* Sub Notebook */
325 case 0xB: /* Hand Held */
328 case 0x11: /* Main Server Chassis */
329 case 0x1C: /* Blade */
330 case 0x1D: /* Blade Enclosure */
333 case 0x1E: /* Tablet */
336 case 0x1F: /* Convertible */
337 case 0x20: /* Detachable */
338 return "convertible";
340 case 0x21: /* IoT Gateway */
341 case 0x22: /* Embedded PC */
345 log_debug("Unhandled DMI chassis type 0x%02x, ignoring.", t
);
350 r
= read_one_line_file("/sys/firmware/acpi/pm_profile", &type
);
352 log_debug_errno(r
, "Failed read ACPI PM profile, ignoring: %m");
356 r
= safe_atou(type
, &t
);
358 log_debug_errno(r
, "Failed parse ACPI PM profile \"%s\", ignoring: %m", type
);
362 /* We only list the really obvious cases here as the ACPI data is not really super reliable.
364 * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
366 * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
371 case 1: /* Desktop */
372 case 3: /* Workstation */
373 case 6: /* Appliance PC */
379 case 4: /* Enterprise Server */
380 case 5: /* SOHO Server */
381 case 7: /* Performance Server */
388 log_debug("Unhandled ACPI PM profile 0x%02x, ignoring.", t
);
393 r
= read_one_line_file("/proc/device-tree/chassis-type", &type
);
395 log_debug_errno(r
, "Failed to read device-tree chassis type, ignoring: %m");
399 /* Note that the Devicetree specification uses the very same vocabulary
400 * of chassis types as we do, hence we do not need to translate these types:
402 * https://github.com/devicetree-org/devicetree-specification/blob/master/source/chapter3-devicenodes.rst */
403 chassis
= valid_chassis(type
);
405 log_debug("Invalid device-tree chassis type \"%s\", ignoring.", type
);
409 static char* context_get_chassis(Context
*c
) {
410 const char *fallback
;
415 if (!isempty(c
->data
[PROP_CHASSIS
]))
416 return strdup(c
->data
[PROP_CHASSIS
]);
418 if (get_dmi_data("ID_CHASSIS", NULL
, &dmi
) > 0)
421 fallback
= fallback_chassis();
423 return strdup(fallback
);
428 static char* context_fallback_icon_name(Context
*c
) {
429 _cleanup_free_
char *chassis
= NULL
;
433 chassis
= context_get_chassis(c
);
435 return strjoin("computer-", chassis
);
437 return strdup("computer");
440 static int context_update_kernel_hostname(
442 const char *transient_hn
) {
444 _cleanup_free_
char *_hn_free
= NULL
;
451 /* /etc/hostname has the highest preference ... */
452 if (c
->data
[PROP_STATIC_HOSTNAME
]) {
453 hn
= c
->data
[PROP_STATIC_HOSTNAME
];
454 hns
= HOSTNAME_STATIC
;
456 /* ... the transient hostname, (ie: DHCP) comes next ... */
457 } else if (transient_hn
) {
459 hns
= HOSTNAME_TRANSIENT
;
461 /* ... and the ultimate fallback */
463 hn
= _hn_free
= get_default_hostname();
467 hns
= HOSTNAME_DEFAULT
;
470 r
= sethostname_idempotent(hn
);
472 return log_error_errno(r
, "Failed to set hostname: %m");
474 if (c
->hostname_source
!= hns
) {
475 c
->hostname_source
= hns
;
479 (void) nscd_flush_cache(STRV_MAKE("hosts"));
482 log_debug("Hostname was already set to <%s>.", hn
);
484 log_info("Hostname set to <%s> (%s)", hn
, hostname_source_to_string(hns
));
486 hostname_update_source_hint(hn
, hns
);
489 return r
; /* 0 if no change, 1 if something was done */
492 static void unset_statp(struct stat
**p
) {
496 **p
= (struct stat
) {};
499 static int context_write_data_static_hostname(Context
*c
) {
500 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
505 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
506 * already, even if we can't make it hit the disk. */
507 s
= &c
->etc_hostname_stat
;
509 if (isempty(c
->data
[PROP_STATIC_HOSTNAME
])) {
510 if (unlink("/etc/hostname") < 0 && errno
!= ENOENT
)
517 r
= write_string_file_atomic_label("/etc/hostname", c
->data
[PROP_STATIC_HOSTNAME
]);
525 static int context_write_data_machine_info(Context
*c
) {
526 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
527 static const char * const name
[_PROP_MAX
] = {
528 [PROP_PRETTY_HOSTNAME
] = "PRETTY_HOSTNAME",
529 [PROP_ICON_NAME
] = "ICON_NAME",
530 [PROP_CHASSIS
] = "CHASSIS",
531 [PROP_DEPLOYMENT
] = "DEPLOYMENT",
532 [PROP_LOCATION
] = "LOCATION",
534 _cleanup_strv_free_
char **l
= NULL
;
539 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
540 * already, even if we can't make it hit the disk. */
541 s
= &c
->etc_machine_info_stat
;
543 r
= load_env_file(NULL
, "/etc/machine-info", &l
);
544 if (r
< 0 && r
!= -ENOENT
)
547 for (int p
= PROP_PRETTY_HOSTNAME
; p
<= PROP_LOCATION
; p
++) {
550 r
= strv_env_assign(&l
, name
[p
], empty_to_null(c
->data
[p
]));
555 if (strv_isempty(l
)) {
556 if (unlink("/etc/machine-info") < 0 && errno
!= ENOENT
)
563 r
= write_env_file_label("/etc/machine-info", l
);
571 static int property_get_hardware_property(
572 sd_bus_message
*reply
,
575 int (*getter
)(char **)) {
577 _cleanup_free_
char *from_dmi
= NULL
;
581 assert(IN_SET(prop
, PROP_HARDWARE_VENDOR
, PROP_HARDWARE_MODEL
));
584 context_read_machine_info(c
);
586 if (isempty(c
->data
[prop
]))
587 (void) getter(&from_dmi
);
589 return sd_bus_message_append(reply
, "s", from_dmi
?: c
->data
[prop
]);
592 static int property_get_hardware_vendor(
595 const char *interface
,
596 const char *property
,
597 sd_bus_message
*reply
,
599 sd_bus_error
*error
) {
601 return property_get_hardware_property(reply
, userdata
, PROP_HARDWARE_VENDOR
, get_hardware_vendor
);
604 static int property_get_hardware_model(
607 const char *interface
,
608 const char *property
,
609 sd_bus_message
*reply
,
611 sd_bus_error
*error
) {
613 return property_get_hardware_property(reply
, userdata
, PROP_HARDWARE_MODEL
, get_hardware_model
);
616 static int property_get_firmware_version(
619 const char *interface
,
620 const char *property
,
621 sd_bus_message
*reply
,
623 sd_bus_error
*error
) {
625 _cleanup_free_
char *firmware_version
= NULL
;
627 (void) get_firmware_version(&firmware_version
);
629 return sd_bus_message_append(reply
, "s", firmware_version
);
632 static int property_get_hostname(
635 const char *interface
,
636 const char *property
,
637 sd_bus_message
*reply
,
639 sd_bus_error
*error
) {
641 _cleanup_free_
char *hn
= NULL
;
644 r
= gethostname_strict(&hn
);
649 hn
= get_default_hostname();
654 return sd_bus_message_append(reply
, "s", hn
);
657 static int property_get_static_hostname(
660 const char *interface
,
661 const char *property
,
662 sd_bus_message
*reply
,
664 sd_bus_error
*error
) {
666 Context
*c
= ASSERT_PTR(userdata
);
668 context_read_etc_hostname(c
);
670 return sd_bus_message_append(reply
, "s", c
->data
[PROP_STATIC_HOSTNAME
]);
673 static int property_get_default_hostname(
676 const char *interface
,
677 const char *property
,
678 sd_bus_message
*reply
,
680 sd_bus_error
*error
) {
682 _cleanup_free_
char *hn
= NULL
;
684 hn
= get_default_hostname();
688 return sd_bus_message_append(reply
, "s", hn
);
691 static void context_determine_hostname_source(Context
*c
) {
692 _cleanup_free_
char *hostname
= NULL
;
697 if (c
->hostname_source
>= 0)
700 (void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST
, &hostname
);
702 if (streq_ptr(hostname
, c
->data
[PROP_STATIC_HOSTNAME
]))
703 c
->hostname_source
= HOSTNAME_STATIC
;
705 _cleanup_free_
char *fallback
= NULL
;
707 /* If the hostname was not set by us, try to figure out where it came from. If we set it to
708 * the default hostname, the file will tell us. We compare the string because it is possible
709 * that the hostname was set by an older version that had a different fallback, in the
710 * initramfs or before we reexecuted. */
712 r
= read_one_line_file("/run/systemd/default-hostname", &fallback
);
713 if (r
< 0 && r
!= -ENOENT
)
714 log_warning_errno(r
, "Failed to read /run/systemd/default-hostname, ignoring: %m");
716 if (streq_ptr(fallback
, hostname
))
717 c
->hostname_source
= HOSTNAME_DEFAULT
;
719 c
->hostname_source
= HOSTNAME_TRANSIENT
;
723 static int property_get_hostname_source(
726 const char *interface
,
727 const char *property
,
728 sd_bus_message
*reply
,
730 sd_bus_error
*error
) {
732 Context
*c
= ASSERT_PTR(userdata
);
734 context_read_etc_hostname(c
);
735 context_determine_hostname_source(c
);
737 return sd_bus_message_append(reply
, "s", hostname_source_to_string(c
->hostname_source
));
740 static int property_get_machine_info_field(
743 const char *interface
,
744 const char *property
,
745 sd_bus_message
*reply
,
747 sd_bus_error
*error
) {
752 /* Acquire the context object without this property's userdata offset added. Explanation: we want
753 * access to two pointers here: a) the main context object we cache all properties in, and b) the
754 * pointer to the property field inside the context object that we are supposed to update and
755 * use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us
756 * from the 'userdata' pointer we supplied when the vtable was registered, with the offset we
757 * specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from
758 * the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot
759 * object is (which encapsulates the vtable registration), and then query the 'userdata' field
760 * directly off it. */
761 assert_se(slot
= sd_bus_get_current_slot(bus
));
762 assert_se(c
= sd_bus_slot_get_userdata(slot
));
764 context_read_machine_info(c
);
766 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
769 static int property_get_os_release_field(
772 const char *interface
,
773 const char *property
,
774 sd_bus_message
*reply
,
776 sd_bus_error
*error
) {
781 /* As above, acquire the current context without this property's userdata offset added. */
782 assert_se(slot
= sd_bus_get_current_slot(bus
));
783 assert_se(c
= sd_bus_slot_get_userdata(slot
));
785 context_read_os_release(c
);
787 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
790 static int property_get_icon_name(
793 const char *interface
,
794 const char *property
,
795 sd_bus_message
*reply
,
797 sd_bus_error
*error
) {
799 _cleanup_free_
char *n
= NULL
;
800 Context
*c
= userdata
;
803 context_read_machine_info(c
);
805 if (isempty(c
->data
[PROP_ICON_NAME
]))
806 name
= n
= context_fallback_icon_name(c
);
808 name
= c
->data
[PROP_ICON_NAME
];
813 return sd_bus_message_append(reply
, "s", name
);
816 static int property_get_chassis(
819 const char *interface
,
820 const char *property
,
821 sd_bus_message
*reply
,
823 sd_bus_error
*error
) {
825 _cleanup_free_
char *chassis
= NULL
;
826 Context
*c
= userdata
;
828 context_read_machine_info(c
);
830 chassis
= context_get_chassis(c
);
832 return sd_bus_message_append(reply
, "s", chassis
);
835 static int property_get_uname_field(
838 const char *interface
,
839 const char *property
,
840 sd_bus_message
*reply
,
842 sd_bus_error
*error
) {
846 assert_se(uname(&u
) >= 0);
848 return sd_bus_message_append(reply
, "s", (char*) &u
+ PTR_TO_SIZE(userdata
));
851 static int method_set_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
852 Context
*c
= ASSERT_PTR(userdata
);
858 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
862 name
= empty_to_null(name
);
864 /* We always go through with the procedure below without comparing to the current hostname, because
865 * we might want to adjust hostname source information even if the actual hostname is unchanged. */
867 if (name
&& !hostname_is_valid(name
, 0))
868 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", name
);
870 context_read_etc_hostname(c
);
872 r
= bus_verify_polkit_async(
875 "org.freedesktop.hostname1.set-hostname",
884 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
886 r
= context_update_kernel_hostname(c
, name
);
888 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
890 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
891 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
892 "Hostname", "HostnameSource", NULL
);
894 return sd_bus_reply_method_return(m
, NULL
);
897 static int method_set_static_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
898 Context
*c
= ASSERT_PTR(userdata
);
905 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
909 name
= empty_to_null(name
);
911 context_read_etc_hostname(c
);
913 if (streq_ptr(name
, c
->data
[PROP_STATIC_HOSTNAME
]))
914 return sd_bus_reply_method_return(m
, NULL
);
916 if (name
&& !hostname_is_valid(name
, 0))
917 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid static hostname '%s'", name
);
919 r
= bus_verify_polkit_async(
922 "org.freedesktop.hostname1.set-static-hostname",
931 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
933 r
= free_and_strdup_warn(&c
->data
[PROP_STATIC_HOSTNAME
], name
);
937 r
= context_write_data_static_hostname(c
);
939 log_error_errno(r
, "Failed to write static hostname: %m");
940 if (ERRNO_IS_PRIVILEGE(r
))
941 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/hostname.");
943 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/hostname is in a read-only filesystem.");
944 return sd_bus_error_set_errnof(error
, r
, "Failed to set static hostname: %m");
947 r
= context_update_kernel_hostname(c
, NULL
);
949 log_error_errno(r
, "Failed to set hostname: %m");
950 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
953 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
954 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
955 "StaticHostname", "Hostname", "HostnameSource", NULL
);
957 return sd_bus_reply_method_return(m
, NULL
);
960 static int set_machine_info(Context
*c
, sd_bus_message
*m
, int prop
, sd_bus_message_handler_t cb
, sd_bus_error
*error
) {
968 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
972 name
= empty_to_null(name
);
974 context_read_machine_info(c
);
976 if (streq_ptr(name
, c
->data
[prop
]))
977 return sd_bus_reply_method_return(m
, NULL
);
979 if (!isempty(name
)) {
980 /* The icon name might ultimately be used as file
981 * name, so better be safe than sorry */
983 if (prop
== PROP_ICON_NAME
&& !filename_is_valid(name
))
984 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid icon name '%s'", name
);
985 if (prop
== PROP_PRETTY_HOSTNAME
&& string_has_cc(name
, NULL
))
986 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid pretty hostname '%s'", name
);
987 if (prop
== PROP_CHASSIS
&& !valid_chassis(name
))
988 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid chassis '%s'", name
);
989 if (prop
== PROP_DEPLOYMENT
&& !valid_deployment(name
))
990 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid deployment '%s'", name
);
991 if (prop
== PROP_LOCATION
&& string_has_cc(name
, NULL
))
992 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid location '%s'", name
);
995 /* Since the pretty hostname should always be changed at the
996 * same time as the static one, use the same policy action for
999 r
= bus_verify_polkit_async(
1002 prop
== PROP_PRETTY_HOSTNAME
? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
1006 &c
->polkit_registry
,
1011 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1013 r
= free_and_strdup_warn(&c
->data
[prop
], name
);
1017 r
= context_write_data_machine_info(c
);
1019 log_error_errno(r
, "Failed to write machine info: %m");
1020 if (ERRNO_IS_PRIVILEGE(r
))
1021 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/machine-info.");
1023 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/machine-info is in a read-only filesystem.");
1024 return sd_bus_error_set_errnof(error
, r
, "Failed to write machine info: %m");
1027 log_info("Changed %s to '%s'",
1028 prop
== PROP_PRETTY_HOSTNAME
? "pretty hostname" :
1029 prop
== PROP_DEPLOYMENT
? "deployment" :
1030 prop
== PROP_LOCATION
? "location" :
1031 prop
== PROP_CHASSIS
? "chassis" : "icon name", strna(c
->data
[prop
]));
1033 (void) sd_bus_emit_properties_changed(
1034 sd_bus_message_get_bus(m
),
1035 "/org/freedesktop/hostname1",
1036 "org.freedesktop.hostname1",
1037 prop
== PROP_PRETTY_HOSTNAME
? "PrettyHostname" :
1038 prop
== PROP_DEPLOYMENT
? "Deployment" :
1039 prop
== PROP_LOCATION
? "Location" :
1040 prop
== PROP_CHASSIS
? "Chassis" : "IconName" , NULL
);
1042 return sd_bus_reply_method_return(m
, NULL
);
1045 static int method_set_pretty_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1046 return set_machine_info(userdata
, m
, PROP_PRETTY_HOSTNAME
, method_set_pretty_hostname
, error
);
1049 static int method_set_icon_name(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1050 return set_machine_info(userdata
, m
, PROP_ICON_NAME
, method_set_icon_name
, error
);
1053 static int method_set_chassis(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1054 return set_machine_info(userdata
, m
, PROP_CHASSIS
, method_set_chassis
, error
);
1057 static int method_set_deployment(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1058 return set_machine_info(userdata
, m
, PROP_DEPLOYMENT
, method_set_deployment
, error
);
1061 static int method_set_location(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1062 return set_machine_info(userdata
, m
, PROP_LOCATION
, method_set_location
, error
);
1065 static int method_get_product_uuid(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1066 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1067 Context
*c
= ASSERT_PTR(userdata
);
1073 r
= sd_bus_message_read(m
, "b", &interactive
);
1077 r
= bus_verify_polkit_async(
1080 "org.freedesktop.hostname1.get-product-uuid",
1084 &c
->polkit_registry
,
1089 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1091 r
= id128_get_product(&uuid
);
1093 if (r
== -EADDRNOTAVAIL
)
1094 log_debug_errno(r
, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
1096 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
1097 "Failed to read product UUID, ignoring: %m");
1099 return sd_bus_error_set(error
, BUS_ERROR_NO_PRODUCT_UUID
,
1100 "Failed to read product UUID from firmware.");
1103 r
= sd_bus_message_new_method_return(m
, &reply
);
1107 r
= sd_bus_message_append_array(reply
, 'y', uuid
.bytes
, sizeof(uuid
.bytes
));
1111 return sd_bus_send(NULL
, reply
, NULL
);
1114 static int method_get_hardware_serial(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1115 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1116 _cleanup_free_
char *serial
= NULL
;
1117 Context
*c
= ASSERT_PTR(userdata
);
1122 r
= bus_verify_polkit_async(
1125 "org.freedesktop.hostname1.get-hardware-serial",
1129 &c
->polkit_registry
,
1134 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1136 r
= get_hardware_serial(&serial
);
1140 r
= sd_bus_message_new_method_return(m
, &reply
);
1144 r
= sd_bus_message_append(reply
, "s", serial
);
1148 return sd_bus_send(NULL
, reply
, NULL
);
1151 static int method_describe(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1152 _cleanup_free_
char *hn
= NULL
, *dhn
= NULL
, *in
= NULL
, *text
= NULL
,
1153 *chassis
= NULL
, *vendor
= NULL
, *model
= NULL
, *serial
= NULL
, *firmware_version
= NULL
;
1154 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1155 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1156 sd_id128_t product_uuid
= SD_ID128_NULL
;
1157 Context
*c
= ASSERT_PTR(userdata
);
1164 r
= bus_verify_polkit_async(
1167 "org.freedesktop.hostname1.get-description",
1171 &c
->polkit_registry
,
1174 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1176 /* We ignore all authentication errors here, since most data is unprivileged, the one exception being
1177 * the product ID which we'll check explicitly. */
1180 context_read_etc_hostname(c
);
1181 context_read_machine_info(c
);
1182 context_read_os_release(c
);
1183 context_determine_hostname_source(c
);
1185 r
= gethostname_strict(&hn
);
1188 return log_error_errno(r
, "Failed to read local host name: %m");
1190 hn
= get_default_hostname();
1195 dhn
= get_default_hostname();
1199 if (isempty(c
->data
[PROP_ICON_NAME
]))
1200 in
= context_fallback_icon_name(c
);
1202 chassis
= context_get_chassis(c
);
1204 assert_se(uname(&u
) >= 0);
1206 if (isempty(c
->data
[PROP_HARDWARE_VENDOR
]))
1207 (void) get_hardware_vendor(&vendor
);
1208 if (isempty(c
->data
[PROP_HARDWARE_MODEL
]))
1209 (void) get_hardware_model(&model
);
1212 /* The product UUID and hardware serial is only available to privileged clients */
1213 (void) id128_get_product(&product_uuid
);
1214 (void) get_hardware_serial(&serial
);
1216 (void) get_firmware_version(&firmware_version
);
1218 r
= json_build(&v
, JSON_BUILD_OBJECT(
1219 JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn
)),
1220 JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c
->data
[PROP_STATIC_HOSTNAME
])),
1221 JSON_BUILD_PAIR("PrettyHostname", JSON_BUILD_STRING(c
->data
[PROP_PRETTY_HOSTNAME
])),
1222 JSON_BUILD_PAIR("DefaultHostname", JSON_BUILD_STRING(dhn
)),
1223 JSON_BUILD_PAIR("HostnameSource", JSON_BUILD_STRING(hostname_source_to_string(c
->hostname_source
))),
1224 JSON_BUILD_PAIR("IconName", JSON_BUILD_STRING(in
?: c
->data
[PROP_ICON_NAME
])),
1225 JSON_BUILD_PAIR("Chassis", JSON_BUILD_STRING(chassis
)),
1226 JSON_BUILD_PAIR("Deployment", JSON_BUILD_STRING(c
->data
[PROP_DEPLOYMENT
])),
1227 JSON_BUILD_PAIR("Location", JSON_BUILD_STRING(c
->data
[PROP_LOCATION
])),
1228 JSON_BUILD_PAIR("KernelName", JSON_BUILD_STRING(u
.sysname
)),
1229 JSON_BUILD_PAIR("KernelRelease", JSON_BUILD_STRING(u
.release
)),
1230 JSON_BUILD_PAIR("KernelVersion", JSON_BUILD_STRING(u
.version
)),
1231 JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c
->data
[PROP_OS_PRETTY_NAME
])),
1232 JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c
->data
[PROP_OS_CPE_NAME
])),
1233 JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c
->data
[PROP_OS_HOME_URL
])),
1234 JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor
?: c
->data
[PROP_HARDWARE_VENDOR
])),
1235 JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model
?: c
->data
[PROP_HARDWARE_MODEL
])),
1236 JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial
)),
1237 JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version
)),
1238 JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_ID128(product_uuid
)),
1239 JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_NULL
)));
1242 return log_error_errno(r
, "Failed to build JSON data: %m");
1244 r
= json_variant_format(v
, 0, &text
);
1246 return log_error_errno(r
, "Failed to format JSON data: %m");
1248 r
= sd_bus_message_new_method_return(m
, &reply
);
1252 r
= sd_bus_message_append(reply
, "s", text
);
1256 return sd_bus_send(NULL
, reply
, NULL
);
1259 static const sd_bus_vtable hostname_vtable
[] = {
1260 SD_BUS_VTABLE_START(0),
1261 SD_BUS_PROPERTY("Hostname", "s", property_get_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1262 SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1263 SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_PRETTY_HOSTNAME
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1264 SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1265 SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1266 SD_BUS_PROPERTY("IconName", "s", property_get_icon_name
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1267 SD_BUS_PROPERTY("Chassis", "s", property_get_chassis
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1268 SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_DEPLOYMENT
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1269 SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_LOCATION
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1270 SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field
, offsetof(struct utsname
, sysname
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1271 SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field
, offsetof(struct utsname
, release
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1272 SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field
, offsetof(struct utsname
, version
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1273 SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_PRETTY_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1274 SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_CPE_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1275 SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_HOME_URL
, SD_BUS_VTABLE_PROPERTY_CONST
),
1276 SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1277 SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1278 SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1280 SD_BUS_METHOD_WITH_ARGS("SetHostname",
1281 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1283 method_set_hostname
,
1284 SD_BUS_VTABLE_UNPRIVILEGED
),
1285 SD_BUS_METHOD_WITH_ARGS("SetStaticHostname",
1286 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1288 method_set_static_hostname
,
1289 SD_BUS_VTABLE_UNPRIVILEGED
),
1290 SD_BUS_METHOD_WITH_ARGS("SetPrettyHostname",
1291 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1293 method_set_pretty_hostname
,
1294 SD_BUS_VTABLE_UNPRIVILEGED
),
1295 SD_BUS_METHOD_WITH_ARGS("SetIconName",
1296 SD_BUS_ARGS("s", icon
, "b", interactive
),
1298 method_set_icon_name
,
1299 SD_BUS_VTABLE_UNPRIVILEGED
),
1300 SD_BUS_METHOD_WITH_ARGS("SetChassis",
1301 SD_BUS_ARGS("s", chassis
, "b", interactive
),
1304 SD_BUS_VTABLE_UNPRIVILEGED
),
1305 SD_BUS_METHOD_WITH_ARGS("SetDeployment",
1306 SD_BUS_ARGS("s", deployment
, "b", interactive
),
1308 method_set_deployment
,
1309 SD_BUS_VTABLE_UNPRIVILEGED
),
1310 SD_BUS_METHOD_WITH_ARGS("SetLocation",
1311 SD_BUS_ARGS("s", location
, "b", interactive
),
1313 method_set_location
,
1314 SD_BUS_VTABLE_UNPRIVILEGED
),
1315 SD_BUS_METHOD_WITH_ARGS("GetProductUUID",
1316 SD_BUS_ARGS("b", interactive
),
1317 SD_BUS_RESULT("ay", uuid
),
1318 method_get_product_uuid
,
1319 SD_BUS_VTABLE_UNPRIVILEGED
),
1320 SD_BUS_METHOD_WITH_ARGS("GetHardwareSerial",
1322 SD_BUS_RESULT("s", serial
),
1323 method_get_hardware_serial
,
1324 SD_BUS_VTABLE_UNPRIVILEGED
),
1325 SD_BUS_METHOD_WITH_ARGS("Describe",
1327 SD_BUS_RESULT("s", json
),
1329 SD_BUS_VTABLE_UNPRIVILEGED
),
1334 static const BusObjectImplementation manager_object
= {
1335 "/org/freedesktop/hostname1",
1336 "org.freedesktop.hostname1",
1337 .vtables
= BUS_VTABLES(hostname_vtable
),
1340 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**ret
) {
1341 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1348 r
= sd_bus_default_system(&bus
);
1350 return log_error_errno(r
, "Failed to get system bus connection: %m");
1352 r
= bus_add_implementation(bus
, &manager_object
, c
);
1356 r
= bus_log_control_api_register(bus
);
1360 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.hostname1", 0, NULL
, NULL
);
1362 return log_error_errno(r
, "Failed to request name: %m");
1364 r
= sd_bus_attach_event(bus
, event
, 0);
1366 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1368 *ret
= TAKE_PTR(bus
);
1372 static int run(int argc
, char *argv
[]) {
1373 _cleanup_(context_destroy
) Context context
= {
1374 .hostname_source
= _HOSTNAME_INVALID
, /* appropriate value will be set later */
1376 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1377 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1382 r
= service_parse_argv("systemd-hostnamed.service",
1383 "Manage the system hostname and related metadata.",
1384 BUS_IMPLEMENTATIONS(&manager_object
,
1385 &log_control_object
),
1392 r
= mac_selinux_init();
1396 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1398 r
= sd_event_default(&event
);
1400 return log_error_errno(r
, "Failed to allocate event loop: %m");
1402 (void) sd_event_set_watchdog(event
, true);
1404 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1406 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1408 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1410 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1412 r
= connect_bus(&context
, event
, &bus
);
1416 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1418 return log_error_errno(r
, "Failed to run event loop: %m");
1423 DEFINE_MAIN_FUNCTION(run
);