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"
14 #include "constants.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"
41 #define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:")
43 /* Properties we cache are indexed by an enum, to make invalidation easy and systematic (as we can iterate
44 * through them all, and they are uniformly strings). */
46 /* Read from /etc/hostname */
49 /* Read from /etc/machine-info */
58 /* Read from /etc/os-release (or /usr/lib/os-release) */
63 _PROP_INVALID
= -EINVAL
,
66 typedef struct Context
{
67 char *data
[_PROP_MAX
];
69 HostnameSource hostname_source
;
71 struct stat etc_hostname_stat
;
72 struct stat etc_os_release_stat
;
73 struct stat etc_machine_info_stat
;
75 Hashmap
*polkit_registry
;
78 static void context_reset(Context
*c
, uint64_t mask
) {
81 for (int p
= 0; p
< _PROP_MAX
; p
++) {
82 if (!FLAGS_SET(mask
, UINT64_C(1) << p
))
85 c
->data
[p
] = mfree(c
->data
[p
]);
89 static void context_destroy(Context
*c
) {
92 context_reset(c
, UINT64_MAX
);
93 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
96 static void context_read_etc_hostname(Context
*c
) {
97 struct stat current_stat
= {};
102 if (stat("/etc/hostname", ¤t_stat
) >= 0 &&
103 stat_inode_unmodified(&c
->etc_hostname_stat
, ¤t_stat
))
106 context_reset(c
, UINT64_C(1) << PROP_STATIC_HOSTNAME
);
108 r
= read_etc_hostname(NULL
, &c
->data
[PROP_STATIC_HOSTNAME
]);
109 if (r
< 0 && r
!= -ENOENT
)
110 log_warning_errno(r
, "Failed to read /etc/hostname, ignoring: %m");
112 c
->etc_hostname_stat
= current_stat
;
115 static void context_read_machine_info(Context
*c
) {
116 struct stat current_stat
= {};
121 if (stat("/etc/machine-info", ¤t_stat
) >= 0 &&
122 stat_inode_unmodified(&c
->etc_machine_info_stat
, ¤t_stat
))
126 (UINT64_C(1) << PROP_PRETTY_HOSTNAME
) |
127 (UINT64_C(1) << PROP_ICON_NAME
) |
128 (UINT64_C(1) << PROP_CHASSIS
) |
129 (UINT64_C(1) << PROP_DEPLOYMENT
) |
130 (UINT64_C(1) << PROP_LOCATION
) |
131 (UINT64_C(1) << PROP_HARDWARE_VENDOR
) |
132 (UINT64_C(1) << PROP_HARDWARE_MODEL
));
134 r
= parse_env_file(NULL
, "/etc/machine-info",
135 "PRETTY_HOSTNAME", &c
->data
[PROP_PRETTY_HOSTNAME
],
136 "ICON_NAME", &c
->data
[PROP_ICON_NAME
],
137 "CHASSIS", &c
->data
[PROP_CHASSIS
],
138 "DEPLOYMENT", &c
->data
[PROP_DEPLOYMENT
],
139 "LOCATION", &c
->data
[PROP_LOCATION
],
140 "HARDWARE_VENDOR", &c
->data
[PROP_HARDWARE_VENDOR
],
141 "HARDWARE_MODEL", &c
->data
[PROP_HARDWARE_MODEL
]);
142 if (r
< 0 && r
!= -ENOENT
)
143 log_warning_errno(r
, "Failed to read /etc/machine-info, ignoring: %m");
145 c
->etc_machine_info_stat
= current_stat
;
148 static void context_read_os_release(Context
*c
) {
149 struct stat current_stat
= {};
154 if ((stat("/etc/os-release", ¤t_stat
) >= 0 ||
155 stat("/usr/lib/os-release", ¤t_stat
) >= 0) &&
156 stat_inode_unmodified(&c
->etc_os_release_stat
, ¤t_stat
))
160 (UINT64_C(1) << PROP_OS_PRETTY_NAME
) |
161 (UINT64_C(1) << PROP_OS_CPE_NAME
) |
162 (UINT64_C(1) << PROP_OS_HOME_URL
));
164 r
= parse_os_release(NULL
,
165 "PRETTY_NAME", &c
->data
[PROP_OS_PRETTY_NAME
],
166 "CPE_NAME", &c
->data
[PROP_OS_CPE_NAME
],
167 "HOME_URL", &c
->data
[PROP_OS_HOME_URL
]);
168 if (r
< 0 && r
!= -ENOENT
)
169 log_warning_errno(r
, "Failed to read os-release file, ignoring: %m");
171 c
->etc_os_release_stat
= current_stat
;
174 static int get_dmi_data(const char *database_key
, const char *regular_key
, char **ret
) {
175 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
176 _cleanup_free_
char *b
= NULL
;
177 const char *s
= NULL
;
180 r
= sd_device_new_from_syspath(&device
, "/sys/class/dmi/id");
182 return log_debug_errno(r
, "Failed to open /sys/class/dmi/id device, ignoring: %m");
185 (void) sd_device_get_property_value(device
, database_key
, &s
);
186 if (!s
&& regular_key
)
187 (void) sd_device_get_property_value(device
, regular_key
, &s
);
202 static int get_hardware_vendor(char **ret
) {
203 return get_dmi_data("ID_VENDOR_FROM_DATABASE", "ID_VENDOR", ret
);
206 static int get_hardware_model(char **ret
) {
207 return get_dmi_data("ID_MODEL_FROM_DATABASE", "ID_MODEL", ret
);
210 static int get_hardware_firmware_data(const char *sysattr
, char **ret
) {
211 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
212 _cleanup_free_
char *b
= NULL
;
213 const char *s
= NULL
;
218 r
= sd_device_new_from_syspath(&device
, "/sys/class/dmi/id");
220 return log_debug_errno(r
, "Failed to open /sys/class/dmi/id device, ignoring: %m");
222 (void) sd_device_get_sysattr_value(device
, sysattr
, &s
);
235 static int get_hardware_serial(char **ret
) {
238 r
= get_hardware_firmware_data("product_serial", ret
);
240 return get_hardware_firmware_data("board_serial", ret
);
245 static int get_firmware_version(char **ret
) {
246 return get_hardware_firmware_data("bios_version", ret
);
249 static const char* valid_chassis(const char *chassis
) {
266 static bool valid_deployment(const char *deployment
) {
269 return in_charset(deployment
, VALID_DEPLOYMENT_CHARS
);
272 static const char* fallback_chassis(void) {
274 _cleanup_free_
char *type
= NULL
;
279 v
= detect_virtualization();
281 log_debug_errno(v
, "Failed to detect virtualization, ignoring: %m");
282 else if (VIRTUALIZATION_IS_VM(v
))
284 else if (VIRTUALIZATION_IS_CONTAINER(v
))
287 r
= read_one_line_file("/sys/class/dmi/id/chassis_type", &type
);
289 log_debug_errno(r
, "Failed to read DMI chassis type, ignoring: %m");
293 r
= safe_atou(type
, &t
);
295 log_debug_errno(r
, "Failed to parse DMI chassis type \"%s\", ignoring: %m", type
);
299 /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any
300 * additional guesswork on top of that.
302 * See the SMBIOS Specification 3.5.0 section 7.4.1 for details about the values listed here:
304 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.5.0.pdf
309 case 0x03: /* Desktop */
310 case 0x04: /* Low Profile Desktop */
311 case 0x06: /* Mini Tower */
312 case 0x07: /* Tower */
313 case 0x0D: /* All in one (i.e. PC built into monitor) */
314 case 0x23: /* Mini PC */
315 case 0x24: /* Stick PC */
318 case 0x8: /* Portable */
319 case 0x9: /* Laptop */
320 case 0xA: /* Notebook */
321 case 0xE: /* Sub Notebook */
324 case 0xB: /* Hand Held */
327 case 0x11: /* Main Server Chassis */
328 case 0x1C: /* Blade */
329 case 0x1D: /* Blade Enclosure */
332 case 0x1E: /* Tablet */
335 case 0x1F: /* Convertible */
336 case 0x20: /* Detachable */
337 return "convertible";
339 case 0x21: /* IoT Gateway */
340 case 0x22: /* Embedded PC */
344 log_debug("Unhandled DMI chassis type 0x%02x, ignoring.", t
);
349 r
= read_one_line_file("/sys/firmware/acpi/pm_profile", &type
);
351 log_debug_errno(r
, "Failed read ACPI PM profile, ignoring: %m");
355 r
= safe_atou(type
, &t
);
357 log_debug_errno(r
, "Failed parse ACPI PM profile \"%s\", ignoring: %m", type
);
361 /* We only list the really obvious cases here as the ACPI data is not really super reliable.
363 * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
365 * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
370 case 1: /* Desktop */
371 case 3: /* Workstation */
372 case 6: /* Appliance PC */
378 case 4: /* Enterprise Server */
379 case 5: /* SOHO Server */
380 case 7: /* Performance Server */
387 log_debug("Unhandled ACPI PM profile 0x%02x, ignoring.", t
);
392 r
= read_one_line_file("/proc/device-tree/chassis-type", &type
);
394 log_debug_errno(r
, "Failed to read device-tree chassis type, ignoring: %m");
398 /* Note that the Devicetree specification uses the very same vocabulary
399 * of chassis types as we do, hence we do not need to translate these types:
401 * https://github.com/devicetree-org/devicetree-specification/blob/master/source/chapter3-devicenodes.rst */
402 chassis
= valid_chassis(type
);
404 log_debug("Invalid device-tree chassis type \"%s\", ignoring.", type
);
408 static char* context_get_chassis(Context
*c
) {
409 const char *fallback
;
414 if (!isempty(c
->data
[PROP_CHASSIS
]))
415 return strdup(c
->data
[PROP_CHASSIS
]);
417 if (get_dmi_data("ID_CHASSIS", NULL
, &dmi
) > 0)
420 fallback
= fallback_chassis();
422 return strdup(fallback
);
427 static char* context_fallback_icon_name(Context
*c
) {
428 _cleanup_free_
char *chassis
= NULL
;
432 chassis
= context_get_chassis(c
);
434 return strjoin("computer-", chassis
);
436 return strdup("computer");
439 static int context_update_kernel_hostname(
441 const char *transient_hn
) {
443 _cleanup_free_
char *_hn_free
= NULL
;
450 /* /etc/hostname has the highest preference ... */
451 if (c
->data
[PROP_STATIC_HOSTNAME
]) {
452 hn
= c
->data
[PROP_STATIC_HOSTNAME
];
453 hns
= HOSTNAME_STATIC
;
455 /* ... the transient hostname, (ie: DHCP) comes next ... */
456 } else if (transient_hn
) {
458 hns
= HOSTNAME_TRANSIENT
;
460 /* ... and the ultimate fallback */
462 hn
= _hn_free
= get_default_hostname();
466 hns
= HOSTNAME_DEFAULT
;
469 r
= sethostname_idempotent(hn
);
471 return log_error_errno(r
, "Failed to set hostname: %m");
473 if (c
->hostname_source
!= hns
) {
474 c
->hostname_source
= hns
;
478 (void) nscd_flush_cache(STRV_MAKE("hosts"));
481 log_debug("Hostname was already set to <%s>.", hn
);
483 log_info("Hostname set to <%s> (%s)", hn
, hostname_source_to_string(hns
));
485 hostname_update_source_hint(hn
, hns
);
488 return r
; /* 0 if no change, 1 if something was done */
491 static void unset_statp(struct stat
**p
) {
495 **p
= (struct stat
) {};
498 static int context_write_data_static_hostname(Context
*c
) {
499 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
504 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
505 * already, even if we can't make it hit the disk. */
506 s
= &c
->etc_hostname_stat
;
508 if (isempty(c
->data
[PROP_STATIC_HOSTNAME
])) {
509 if (unlink("/etc/hostname") < 0 && errno
!= ENOENT
)
516 r
= write_string_file_atomic_label("/etc/hostname", c
->data
[PROP_STATIC_HOSTNAME
]);
524 static int context_write_data_machine_info(Context
*c
) {
525 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
526 static const char * const name
[_PROP_MAX
] = {
527 [PROP_PRETTY_HOSTNAME
] = "PRETTY_HOSTNAME",
528 [PROP_ICON_NAME
] = "ICON_NAME",
529 [PROP_CHASSIS
] = "CHASSIS",
530 [PROP_DEPLOYMENT
] = "DEPLOYMENT",
531 [PROP_LOCATION
] = "LOCATION",
533 _cleanup_strv_free_
char **l
= NULL
;
538 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
539 * already, even if we can't make it hit the disk. */
540 s
= &c
->etc_machine_info_stat
;
542 r
= load_env_file(NULL
, "/etc/machine-info", &l
);
543 if (r
< 0 && r
!= -ENOENT
)
546 for (int p
= PROP_PRETTY_HOSTNAME
; p
<= PROP_LOCATION
; p
++) {
549 r
= strv_env_assign(&l
, name
[p
], empty_to_null(c
->data
[p
]));
554 if (strv_isempty(l
)) {
555 if (unlink("/etc/machine-info") < 0 && errno
!= ENOENT
)
562 r
= write_env_file_label("/etc/machine-info", l
);
570 static int property_get_hardware_property(
571 sd_bus_message
*reply
,
574 int (*getter
)(char **)) {
576 _cleanup_free_
char *from_dmi
= NULL
;
580 assert(IN_SET(prop
, PROP_HARDWARE_VENDOR
, PROP_HARDWARE_MODEL
));
583 context_read_machine_info(c
);
585 if (isempty(c
->data
[prop
]))
586 (void) getter(&from_dmi
);
588 return sd_bus_message_append(reply
, "s", from_dmi
?: c
->data
[prop
]);
591 static int property_get_hardware_vendor(
594 const char *interface
,
595 const char *property
,
596 sd_bus_message
*reply
,
598 sd_bus_error
*error
) {
600 return property_get_hardware_property(reply
, userdata
, PROP_HARDWARE_VENDOR
, get_hardware_vendor
);
603 static int property_get_hardware_model(
606 const char *interface
,
607 const char *property
,
608 sd_bus_message
*reply
,
610 sd_bus_error
*error
) {
612 return property_get_hardware_property(reply
, userdata
, PROP_HARDWARE_MODEL
, get_hardware_model
);
615 static int property_get_firmware_version(
618 const char *interface
,
619 const char *property
,
620 sd_bus_message
*reply
,
622 sd_bus_error
*error
) {
624 _cleanup_free_
char *firmware_version
= NULL
;
626 (void) get_firmware_version(&firmware_version
);
628 return sd_bus_message_append(reply
, "s", firmware_version
);
631 static int property_get_hostname(
634 const char *interface
,
635 const char *property
,
636 sd_bus_message
*reply
,
638 sd_bus_error
*error
) {
640 _cleanup_free_
char *hn
= NULL
;
643 r
= gethostname_strict(&hn
);
648 hn
= get_default_hostname();
653 return sd_bus_message_append(reply
, "s", hn
);
656 static int property_get_static_hostname(
659 const char *interface
,
660 const char *property
,
661 sd_bus_message
*reply
,
663 sd_bus_error
*error
) {
665 Context
*c
= ASSERT_PTR(userdata
);
667 context_read_etc_hostname(c
);
669 return sd_bus_message_append(reply
, "s", c
->data
[PROP_STATIC_HOSTNAME
]);
672 static int property_get_default_hostname(
675 const char *interface
,
676 const char *property
,
677 sd_bus_message
*reply
,
679 sd_bus_error
*error
) {
681 _cleanup_free_
char *hn
= NULL
;
683 hn
= get_default_hostname();
687 return sd_bus_message_append(reply
, "s", hn
);
690 static void context_determine_hostname_source(Context
*c
) {
691 _cleanup_free_
char *hostname
= NULL
;
696 if (c
->hostname_source
>= 0)
699 (void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST
, &hostname
);
701 if (streq_ptr(hostname
, c
->data
[PROP_STATIC_HOSTNAME
]))
702 c
->hostname_source
= HOSTNAME_STATIC
;
704 _cleanup_free_
char *fallback
= NULL
;
706 /* If the hostname was not set by us, try to figure out where it came from. If we set it to
707 * the default hostname, the file will tell us. We compare the string because it is possible
708 * that the hostname was set by an older version that had a different fallback, in the initrd
709 * or before we reexecuted. */
711 r
= read_one_line_file("/run/systemd/default-hostname", &fallback
);
712 if (r
< 0 && r
!= -ENOENT
)
713 log_warning_errno(r
, "Failed to read /run/systemd/default-hostname, ignoring: %m");
715 if (streq_ptr(fallback
, hostname
))
716 c
->hostname_source
= HOSTNAME_DEFAULT
;
718 c
->hostname_source
= HOSTNAME_TRANSIENT
;
722 static int property_get_hostname_source(
725 const char *interface
,
726 const char *property
,
727 sd_bus_message
*reply
,
729 sd_bus_error
*error
) {
731 Context
*c
= ASSERT_PTR(userdata
);
733 context_read_etc_hostname(c
);
734 context_determine_hostname_source(c
);
736 return sd_bus_message_append(reply
, "s", hostname_source_to_string(c
->hostname_source
));
739 static int property_get_machine_info_field(
742 const char *interface
,
743 const char *property
,
744 sd_bus_message
*reply
,
746 sd_bus_error
*error
) {
751 /* Acquire the context object without this property's userdata offset added. Explanation: we want
752 * access to two pointers here: a) the main context object we cache all properties in, and b) the
753 * pointer to the property field inside the context object that we are supposed to update and
754 * use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us
755 * from the 'userdata' pointer we supplied when the vtable was registered, with the offset we
756 * specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from
757 * the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot
758 * object is (which encapsulates the vtable registration), and then query the 'userdata' field
759 * directly off it. */
760 assert_se(slot
= sd_bus_get_current_slot(bus
));
761 assert_se(c
= sd_bus_slot_get_userdata(slot
));
763 context_read_machine_info(c
);
765 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
768 static int property_get_os_release_field(
771 const char *interface
,
772 const char *property
,
773 sd_bus_message
*reply
,
775 sd_bus_error
*error
) {
780 /* As above, acquire the current context without this property's userdata offset added. */
781 assert_se(slot
= sd_bus_get_current_slot(bus
));
782 assert_se(c
= sd_bus_slot_get_userdata(slot
));
784 context_read_os_release(c
);
786 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
789 static int property_get_icon_name(
792 const char *interface
,
793 const char *property
,
794 sd_bus_message
*reply
,
796 sd_bus_error
*error
) {
798 _cleanup_free_
char *n
= NULL
;
799 Context
*c
= userdata
;
802 context_read_machine_info(c
);
804 if (isempty(c
->data
[PROP_ICON_NAME
]))
805 name
= n
= context_fallback_icon_name(c
);
807 name
= c
->data
[PROP_ICON_NAME
];
812 return sd_bus_message_append(reply
, "s", name
);
815 static int property_get_chassis(
818 const char *interface
,
819 const char *property
,
820 sd_bus_message
*reply
,
822 sd_bus_error
*error
) {
824 _cleanup_free_
char *chassis
= NULL
;
825 Context
*c
= userdata
;
827 context_read_machine_info(c
);
829 chassis
= context_get_chassis(c
);
831 return sd_bus_message_append(reply
, "s", chassis
);
834 static int property_get_uname_field(
837 const char *interface
,
838 const char *property
,
839 sd_bus_message
*reply
,
841 sd_bus_error
*error
) {
845 assert_se(uname(&u
) >= 0);
847 return sd_bus_message_append(reply
, "s", (char*) &u
+ PTR_TO_SIZE(userdata
));
850 static int method_set_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
851 Context
*c
= ASSERT_PTR(userdata
);
857 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
861 name
= empty_to_null(name
);
863 /* We always go through with the procedure below without comparing to the current hostname, because
864 * we might want to adjust hostname source information even if the actual hostname is unchanged. */
866 if (name
&& !hostname_is_valid(name
, 0))
867 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", name
);
869 context_read_etc_hostname(c
);
871 r
= bus_verify_polkit_async(
874 "org.freedesktop.hostname1.set-hostname",
883 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
885 r
= context_update_kernel_hostname(c
, name
);
887 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
889 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
890 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
891 "Hostname", "HostnameSource", NULL
);
893 return sd_bus_reply_method_return(m
, NULL
);
896 static int method_set_static_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
897 Context
*c
= ASSERT_PTR(userdata
);
904 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
908 name
= empty_to_null(name
);
910 context_read_etc_hostname(c
);
912 if (streq_ptr(name
, c
->data
[PROP_STATIC_HOSTNAME
]))
913 return sd_bus_reply_method_return(m
, NULL
);
915 if (name
&& !hostname_is_valid(name
, 0))
916 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid static hostname '%s'", name
);
918 r
= bus_verify_polkit_async(
921 "org.freedesktop.hostname1.set-static-hostname",
930 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
932 r
= free_and_strdup_warn(&c
->data
[PROP_STATIC_HOSTNAME
], name
);
936 r
= context_write_data_static_hostname(c
);
938 log_error_errno(r
, "Failed to write static hostname: %m");
939 if (ERRNO_IS_PRIVILEGE(r
))
940 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/hostname.");
942 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/hostname is in a read-only filesystem.");
943 return sd_bus_error_set_errnof(error
, r
, "Failed to set static hostname: %m");
946 r
= context_update_kernel_hostname(c
, NULL
);
948 log_error_errno(r
, "Failed to set hostname: %m");
949 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
952 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
953 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
954 "StaticHostname", "Hostname", "HostnameSource", NULL
);
956 return sd_bus_reply_method_return(m
, NULL
);
959 static int set_machine_info(Context
*c
, sd_bus_message
*m
, int prop
, sd_bus_message_handler_t cb
, sd_bus_error
*error
) {
967 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
971 name
= empty_to_null(name
);
973 context_read_machine_info(c
);
975 if (streq_ptr(name
, c
->data
[prop
]))
976 return sd_bus_reply_method_return(m
, NULL
);
978 if (!isempty(name
)) {
979 /* The icon name might ultimately be used as file
980 * name, so better be safe than sorry */
982 if (prop
== PROP_ICON_NAME
&& !filename_is_valid(name
))
983 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid icon name '%s'", name
);
984 if (prop
== PROP_PRETTY_HOSTNAME
&& string_has_cc(name
, NULL
))
985 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid pretty hostname '%s'", name
);
986 if (prop
== PROP_CHASSIS
&& !valid_chassis(name
))
987 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid chassis '%s'", name
);
988 if (prop
== PROP_DEPLOYMENT
&& !valid_deployment(name
))
989 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid deployment '%s'", name
);
990 if (prop
== PROP_LOCATION
&& string_has_cc(name
, NULL
))
991 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid location '%s'", name
);
994 /* Since the pretty hostname should always be changed at the
995 * same time as the static one, use the same policy action for
998 r
= bus_verify_polkit_async(
1001 prop
== PROP_PRETTY_HOSTNAME
? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
1005 &c
->polkit_registry
,
1010 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1012 r
= free_and_strdup_warn(&c
->data
[prop
], name
);
1016 r
= context_write_data_machine_info(c
);
1018 log_error_errno(r
, "Failed to write machine info: %m");
1019 if (ERRNO_IS_PRIVILEGE(r
))
1020 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/machine-info.");
1022 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/machine-info is in a read-only filesystem.");
1023 return sd_bus_error_set_errnof(error
, r
, "Failed to write machine info: %m");
1026 log_info("Changed %s to '%s'",
1027 prop
== PROP_PRETTY_HOSTNAME
? "pretty hostname" :
1028 prop
== PROP_DEPLOYMENT
? "deployment" :
1029 prop
== PROP_LOCATION
? "location" :
1030 prop
== PROP_CHASSIS
? "chassis" : "icon name", strna(c
->data
[prop
]));
1032 (void) sd_bus_emit_properties_changed(
1033 sd_bus_message_get_bus(m
),
1034 "/org/freedesktop/hostname1",
1035 "org.freedesktop.hostname1",
1036 prop
== PROP_PRETTY_HOSTNAME
? "PrettyHostname" :
1037 prop
== PROP_DEPLOYMENT
? "Deployment" :
1038 prop
== PROP_LOCATION
? "Location" :
1039 prop
== PROP_CHASSIS
? "Chassis" : "IconName" , NULL
);
1041 return sd_bus_reply_method_return(m
, NULL
);
1044 static int method_set_pretty_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1045 return set_machine_info(userdata
, m
, PROP_PRETTY_HOSTNAME
, method_set_pretty_hostname
, error
);
1048 static int method_set_icon_name(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1049 return set_machine_info(userdata
, m
, PROP_ICON_NAME
, method_set_icon_name
, error
);
1052 static int method_set_chassis(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1053 return set_machine_info(userdata
, m
, PROP_CHASSIS
, method_set_chassis
, error
);
1056 static int method_set_deployment(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1057 return set_machine_info(userdata
, m
, PROP_DEPLOYMENT
, method_set_deployment
, error
);
1060 static int method_set_location(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1061 return set_machine_info(userdata
, m
, PROP_LOCATION
, method_set_location
, error
);
1064 static int method_get_product_uuid(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1065 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1066 Context
*c
= ASSERT_PTR(userdata
);
1072 r
= sd_bus_message_read(m
, "b", &interactive
);
1076 r
= bus_verify_polkit_async(
1079 "org.freedesktop.hostname1.get-product-uuid",
1083 &c
->polkit_registry
,
1088 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1090 r
= id128_get_product(&uuid
);
1092 if (r
== -EADDRNOTAVAIL
)
1093 log_debug_errno(r
, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
1095 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
1096 "Failed to read product UUID, ignoring: %m");
1098 return sd_bus_error_set(error
, BUS_ERROR_NO_PRODUCT_UUID
,
1099 "Failed to read product UUID from firmware.");
1102 r
= sd_bus_message_new_method_return(m
, &reply
);
1106 r
= sd_bus_message_append_array(reply
, 'y', uuid
.bytes
, sizeof(uuid
.bytes
));
1110 return sd_bus_send(NULL
, reply
, NULL
);
1113 static int method_get_hardware_serial(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1114 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1115 _cleanup_free_
char *serial
= NULL
;
1116 Context
*c
= ASSERT_PTR(userdata
);
1121 r
= bus_verify_polkit_async(
1124 "org.freedesktop.hostname1.get-hardware-serial",
1128 &c
->polkit_registry
,
1133 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1135 r
= get_hardware_serial(&serial
);
1139 r
= sd_bus_message_new_method_return(m
, &reply
);
1143 r
= sd_bus_message_append(reply
, "s", serial
);
1147 return sd_bus_send(NULL
, reply
, NULL
);
1150 static int method_describe(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1151 _cleanup_free_
char *hn
= NULL
, *dhn
= NULL
, *in
= NULL
, *text
= NULL
,
1152 *chassis
= NULL
, *vendor
= NULL
, *model
= NULL
, *serial
= NULL
, *firmware_version
= NULL
;
1153 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1154 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1155 sd_id128_t product_uuid
= SD_ID128_NULL
;
1156 Context
*c
= ASSERT_PTR(userdata
);
1163 r
= bus_verify_polkit_async(
1166 "org.freedesktop.hostname1.get-description",
1170 &c
->polkit_registry
,
1173 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1175 /* We ignore all authentication errors here, since most data is unprivileged, the one exception being
1176 * the product ID which we'll check explicitly. */
1179 context_read_etc_hostname(c
);
1180 context_read_machine_info(c
);
1181 context_read_os_release(c
);
1182 context_determine_hostname_source(c
);
1184 r
= gethostname_strict(&hn
);
1187 return log_error_errno(r
, "Failed to read local host name: %m");
1189 hn
= get_default_hostname();
1194 dhn
= get_default_hostname();
1198 if (isempty(c
->data
[PROP_ICON_NAME
]))
1199 in
= context_fallback_icon_name(c
);
1201 chassis
= context_get_chassis(c
);
1203 assert_se(uname(&u
) >= 0);
1205 if (isempty(c
->data
[PROP_HARDWARE_VENDOR
]))
1206 (void) get_hardware_vendor(&vendor
);
1207 if (isempty(c
->data
[PROP_HARDWARE_MODEL
]))
1208 (void) get_hardware_model(&model
);
1211 /* The product UUID and hardware serial is only available to privileged clients */
1212 (void) id128_get_product(&product_uuid
);
1213 (void) get_hardware_serial(&serial
);
1215 (void) get_firmware_version(&firmware_version
);
1217 r
= json_build(&v
, JSON_BUILD_OBJECT(
1218 JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn
)),
1219 JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c
->data
[PROP_STATIC_HOSTNAME
])),
1220 JSON_BUILD_PAIR("PrettyHostname", JSON_BUILD_STRING(c
->data
[PROP_PRETTY_HOSTNAME
])),
1221 JSON_BUILD_PAIR("DefaultHostname", JSON_BUILD_STRING(dhn
)),
1222 JSON_BUILD_PAIR("HostnameSource", JSON_BUILD_STRING(hostname_source_to_string(c
->hostname_source
))),
1223 JSON_BUILD_PAIR("IconName", JSON_BUILD_STRING(in
?: c
->data
[PROP_ICON_NAME
])),
1224 JSON_BUILD_PAIR("Chassis", JSON_BUILD_STRING(chassis
)),
1225 JSON_BUILD_PAIR("Deployment", JSON_BUILD_STRING(c
->data
[PROP_DEPLOYMENT
])),
1226 JSON_BUILD_PAIR("Location", JSON_BUILD_STRING(c
->data
[PROP_LOCATION
])),
1227 JSON_BUILD_PAIR("KernelName", JSON_BUILD_STRING(u
.sysname
)),
1228 JSON_BUILD_PAIR("KernelRelease", JSON_BUILD_STRING(u
.release
)),
1229 JSON_BUILD_PAIR("KernelVersion", JSON_BUILD_STRING(u
.version
)),
1230 JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c
->data
[PROP_OS_PRETTY_NAME
])),
1231 JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c
->data
[PROP_OS_CPE_NAME
])),
1232 JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c
->data
[PROP_OS_HOME_URL
])),
1233 JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor
?: c
->data
[PROP_HARDWARE_VENDOR
])),
1234 JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model
?: c
->data
[PROP_HARDWARE_MODEL
])),
1235 JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial
)),
1236 JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version
)),
1237 JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_ID128(product_uuid
)),
1238 JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_NULL
)));
1241 return log_error_errno(r
, "Failed to build JSON data: %m");
1243 r
= json_variant_format(v
, 0, &text
);
1245 return log_error_errno(r
, "Failed to format JSON data: %m");
1247 r
= sd_bus_message_new_method_return(m
, &reply
);
1251 r
= sd_bus_message_append(reply
, "s", text
);
1255 return sd_bus_send(NULL
, reply
, NULL
);
1258 static const sd_bus_vtable hostname_vtable
[] = {
1259 SD_BUS_VTABLE_START(0),
1260 SD_BUS_PROPERTY("Hostname", "s", property_get_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1261 SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1262 SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_PRETTY_HOSTNAME
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1263 SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1264 SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1265 SD_BUS_PROPERTY("IconName", "s", property_get_icon_name
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1266 SD_BUS_PROPERTY("Chassis", "s", property_get_chassis
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1267 SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_DEPLOYMENT
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1268 SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_LOCATION
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1269 SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field
, offsetof(struct utsname
, sysname
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1270 SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field
, offsetof(struct utsname
, release
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1271 SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field
, offsetof(struct utsname
, version
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1272 SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_PRETTY_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1273 SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_CPE_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1274 SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_HOME_URL
, SD_BUS_VTABLE_PROPERTY_CONST
),
1275 SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1276 SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1277 SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1279 SD_BUS_METHOD_WITH_ARGS("SetHostname",
1280 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1282 method_set_hostname
,
1283 SD_BUS_VTABLE_UNPRIVILEGED
),
1284 SD_BUS_METHOD_WITH_ARGS("SetStaticHostname",
1285 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1287 method_set_static_hostname
,
1288 SD_BUS_VTABLE_UNPRIVILEGED
),
1289 SD_BUS_METHOD_WITH_ARGS("SetPrettyHostname",
1290 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1292 method_set_pretty_hostname
,
1293 SD_BUS_VTABLE_UNPRIVILEGED
),
1294 SD_BUS_METHOD_WITH_ARGS("SetIconName",
1295 SD_BUS_ARGS("s", icon
, "b", interactive
),
1297 method_set_icon_name
,
1298 SD_BUS_VTABLE_UNPRIVILEGED
),
1299 SD_BUS_METHOD_WITH_ARGS("SetChassis",
1300 SD_BUS_ARGS("s", chassis
, "b", interactive
),
1303 SD_BUS_VTABLE_UNPRIVILEGED
),
1304 SD_BUS_METHOD_WITH_ARGS("SetDeployment",
1305 SD_BUS_ARGS("s", deployment
, "b", interactive
),
1307 method_set_deployment
,
1308 SD_BUS_VTABLE_UNPRIVILEGED
),
1309 SD_BUS_METHOD_WITH_ARGS("SetLocation",
1310 SD_BUS_ARGS("s", location
, "b", interactive
),
1312 method_set_location
,
1313 SD_BUS_VTABLE_UNPRIVILEGED
),
1314 SD_BUS_METHOD_WITH_ARGS("GetProductUUID",
1315 SD_BUS_ARGS("b", interactive
),
1316 SD_BUS_RESULT("ay", uuid
),
1317 method_get_product_uuid
,
1318 SD_BUS_VTABLE_UNPRIVILEGED
),
1319 SD_BUS_METHOD_WITH_ARGS("GetHardwareSerial",
1321 SD_BUS_RESULT("s", serial
),
1322 method_get_hardware_serial
,
1323 SD_BUS_VTABLE_UNPRIVILEGED
),
1324 SD_BUS_METHOD_WITH_ARGS("Describe",
1326 SD_BUS_RESULT("s", json
),
1328 SD_BUS_VTABLE_UNPRIVILEGED
),
1333 static const BusObjectImplementation manager_object
= {
1334 "/org/freedesktop/hostname1",
1335 "org.freedesktop.hostname1",
1336 .vtables
= BUS_VTABLES(hostname_vtable
),
1339 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**ret
) {
1340 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1347 r
= sd_bus_default_system(&bus
);
1349 return log_error_errno(r
, "Failed to get system bus connection: %m");
1351 r
= bus_add_implementation(bus
, &manager_object
, c
);
1355 r
= bus_log_control_api_register(bus
);
1359 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.hostname1", 0, NULL
, NULL
);
1361 return log_error_errno(r
, "Failed to request name: %m");
1363 r
= sd_bus_attach_event(bus
, event
, 0);
1365 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1367 *ret
= TAKE_PTR(bus
);
1371 static int run(int argc
, char *argv
[]) {
1372 _cleanup_(context_destroy
) Context context
= {
1373 .hostname_source
= _HOSTNAME_INVALID
, /* appropriate value will be set later */
1375 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1376 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1381 r
= service_parse_argv("systemd-hostnamed.service",
1382 "Manage the system hostname and related metadata.",
1383 BUS_IMPLEMENTATIONS(&manager_object
,
1384 &log_control_object
),
1391 r
= mac_selinux_init();
1395 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1397 r
= sd_event_default(&event
);
1399 return log_error_errno(r
, "Failed to allocate event loop: %m");
1401 (void) sd_event_set_watchdog(event
, true);
1403 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1405 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1407 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1409 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1411 r
= connect_bus(&context
, event
, &bus
);
1415 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1417 return log_error_errno(r
, "Failed to run event loop: %m");
1422 DEFINE_MAIN_FUNCTION(run
);