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 int get_firmware_vendor(char **ret
) {
250 return get_hardware_firmware_data("bios_vendor", ret
);
253 static int get_firmware_date(usec_t
*ret
) {
254 _cleanup_free_
char *bios_date
= NULL
, *month
= NULL
, *day
= NULL
, *year
= NULL
;
259 r
= get_hardware_firmware_data("bios_date", &bios_date
);
263 *ret
= USEC_INFINITY
;
267 const char *p
= bios_date
;
268 r
= extract_many_words(&p
, "/", EXTRACT_DONT_COALESCE_SEPARATORS
, &month
, &day
, &year
, NULL
);
271 if (r
!= 3) /* less than three args read? */
273 if (!isempty(p
)) /* more left in the string? */
277 r
= safe_atou(month
, &m
);
284 r
= safe_atou(day
, &d
);
290 r
= safe_atou(year
, &y
);
293 if (y
< 1970 || y
> (unsigned) INT_MAX
)
302 time_t v
= timegm(&tm
);
303 if (v
== (time_t) -1)
305 if (tm
.tm_mday
!= (int) d
|| tm
.tm_mon
!= (int) m
|| tm
.tm_year
!= (int) y
)
306 return -EINVAL
; /* date was not normalized? (e.g. "30th of feb") */
308 *ret
= (usec_t
) v
* USEC_PER_SEC
;
313 static const char* valid_chassis(const char *chassis
) {
330 static bool valid_deployment(const char *deployment
) {
333 return in_charset(deployment
, VALID_DEPLOYMENT_CHARS
);
336 static const char* fallback_chassis(void) {
338 _cleanup_free_
char *type
= NULL
;
343 v
= detect_virtualization();
345 log_debug_errno(v
, "Failed to detect virtualization, ignoring: %m");
346 else if (VIRTUALIZATION_IS_VM(v
))
348 else if (VIRTUALIZATION_IS_CONTAINER(v
))
351 r
= read_one_line_file("/sys/class/dmi/id/chassis_type", &type
);
353 log_debug_errno(r
, "Failed to read DMI chassis type, ignoring: %m");
357 r
= safe_atou(type
, &t
);
359 log_debug_errno(r
, "Failed to parse DMI chassis type \"%s\", ignoring: %m", type
);
363 /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any
364 * additional guesswork on top of that.
366 * See the SMBIOS Specification 3.5.0 section 7.4.1 for details about the values listed here:
368 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.5.0.pdf
373 case 0x03: /* Desktop */
374 case 0x04: /* Low Profile Desktop */
375 case 0x06: /* Mini Tower */
376 case 0x07: /* Tower */
377 case 0x0D: /* All in one (i.e. PC built into monitor) */
378 case 0x23: /* Mini PC */
379 case 0x24: /* Stick PC */
382 case 0x8: /* Portable */
383 case 0x9: /* Laptop */
384 case 0xA: /* Notebook */
385 case 0xE: /* Sub Notebook */
388 case 0xB: /* Hand Held */
391 case 0x11: /* Main Server Chassis */
392 case 0x1C: /* Blade */
393 case 0x1D: /* Blade Enclosure */
396 case 0x1E: /* Tablet */
399 case 0x1F: /* Convertible */
400 case 0x20: /* Detachable */
401 return "convertible";
403 case 0x21: /* IoT Gateway */
404 case 0x22: /* Embedded PC */
408 log_debug("Unhandled DMI chassis type 0x%02x, ignoring.", t
);
413 r
= read_one_line_file("/sys/firmware/acpi/pm_profile", &type
);
415 log_debug_errno(r
, "Failed read ACPI PM profile, ignoring: %m");
419 r
= safe_atou(type
, &t
);
421 log_debug_errno(r
, "Failed parse ACPI PM profile \"%s\", ignoring: %m", type
);
425 /* We only list the really obvious cases here as the ACPI data is not really super reliable.
427 * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
429 * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
434 case 1: /* Desktop */
435 case 3: /* Workstation */
436 case 6: /* Appliance PC */
442 case 4: /* Enterprise Server */
443 case 5: /* SOHO Server */
444 case 7: /* Performance Server */
451 log_debug("Unhandled ACPI PM profile 0x%02x, ignoring.", t
);
456 r
= read_one_line_file("/proc/device-tree/chassis-type", &type
);
458 log_debug_errno(r
, "Failed to read device-tree chassis type, ignoring: %m");
462 /* Note that the Devicetree specification uses the very same vocabulary
463 * of chassis types as we do, hence we do not need to translate these types:
465 * https://github.com/devicetree-org/devicetree-specification/blob/master/source/chapter3-devicenodes.rst */
466 chassis
= valid_chassis(type
);
468 log_debug("Invalid device-tree chassis type \"%s\", ignoring.", type
);
472 static char* context_get_chassis(Context
*c
) {
473 const char *fallback
;
478 if (!isempty(c
->data
[PROP_CHASSIS
]))
479 return strdup(c
->data
[PROP_CHASSIS
]);
481 if (get_dmi_data("ID_CHASSIS", NULL
, &dmi
) > 0)
484 fallback
= fallback_chassis();
486 return strdup(fallback
);
491 static char* context_fallback_icon_name(Context
*c
) {
492 _cleanup_free_
char *chassis
= NULL
;
496 chassis
= context_get_chassis(c
);
498 return strjoin("computer-", chassis
);
500 return strdup("computer");
503 static int context_update_kernel_hostname(
505 const char *transient_hn
) {
507 _cleanup_free_
char *_hn_free
= NULL
;
514 /* /etc/hostname has the highest preference ... */
515 if (c
->data
[PROP_STATIC_HOSTNAME
]) {
516 hn
= c
->data
[PROP_STATIC_HOSTNAME
];
517 hns
= HOSTNAME_STATIC
;
519 /* ... the transient hostname, (ie: DHCP) comes next ... */
520 } else if (transient_hn
) {
522 hns
= HOSTNAME_TRANSIENT
;
524 /* ... and the ultimate fallback */
526 hn
= _hn_free
= get_default_hostname();
530 hns
= HOSTNAME_DEFAULT
;
533 r
= sethostname_idempotent(hn
);
535 return log_error_errno(r
, "Failed to set hostname: %m");
537 if (c
->hostname_source
!= hns
) {
538 c
->hostname_source
= hns
;
542 (void) nscd_flush_cache(STRV_MAKE("hosts"));
545 log_debug("Hostname was already set to <%s>.", hn
);
547 log_info("Hostname set to <%s> (%s)", hn
, hostname_source_to_string(hns
));
549 hostname_update_source_hint(hn
, hns
);
552 return r
; /* 0 if no change, 1 if something was done */
555 static void unset_statp(struct stat
**p
) {
559 **p
= (struct stat
) {};
562 static int context_write_data_static_hostname(Context
*c
) {
563 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
568 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
569 * already, even if we can't make it hit the disk. */
570 s
= &c
->etc_hostname_stat
;
572 if (isempty(c
->data
[PROP_STATIC_HOSTNAME
])) {
573 if (unlink("/etc/hostname") < 0 && errno
!= ENOENT
)
580 r
= write_string_file_atomic_label("/etc/hostname", c
->data
[PROP_STATIC_HOSTNAME
]);
588 static int context_write_data_machine_info(Context
*c
) {
589 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
590 static const char * const name
[_PROP_MAX
] = {
591 [PROP_PRETTY_HOSTNAME
] = "PRETTY_HOSTNAME",
592 [PROP_ICON_NAME
] = "ICON_NAME",
593 [PROP_CHASSIS
] = "CHASSIS",
594 [PROP_DEPLOYMENT
] = "DEPLOYMENT",
595 [PROP_LOCATION
] = "LOCATION",
597 _cleanup_strv_free_
char **l
= NULL
;
602 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
603 * already, even if we can't make it hit the disk. */
604 s
= &c
->etc_machine_info_stat
;
606 r
= load_env_file(NULL
, "/etc/machine-info", &l
);
607 if (r
< 0 && r
!= -ENOENT
)
610 for (int p
= PROP_PRETTY_HOSTNAME
; p
<= PROP_LOCATION
; p
++) {
613 r
= strv_env_assign(&l
, name
[p
], empty_to_null(c
->data
[p
]));
618 if (strv_isempty(l
)) {
619 if (unlink("/etc/machine-info") < 0 && errno
!= ENOENT
)
626 r
= write_env_file_label("/etc/machine-info", l
);
634 static int property_get_hardware_property(
635 sd_bus_message
*reply
,
638 int (*getter
)(char **)) {
640 _cleanup_free_
char *from_dmi
= NULL
;
644 assert(IN_SET(prop
, PROP_HARDWARE_VENDOR
, PROP_HARDWARE_MODEL
));
647 context_read_machine_info(c
);
649 if (isempty(c
->data
[prop
]))
650 (void) getter(&from_dmi
);
652 return sd_bus_message_append(reply
, "s", from_dmi
?: c
->data
[prop
]);
655 static int property_get_hardware_vendor(
658 const char *interface
,
659 const char *property
,
660 sd_bus_message
*reply
,
662 sd_bus_error
*error
) {
664 return property_get_hardware_property(reply
, userdata
, PROP_HARDWARE_VENDOR
, get_hardware_vendor
);
667 static int property_get_hardware_model(
670 const char *interface
,
671 const char *property
,
672 sd_bus_message
*reply
,
674 sd_bus_error
*error
) {
676 return property_get_hardware_property(reply
, userdata
, PROP_HARDWARE_MODEL
, get_hardware_model
);
679 static int property_get_firmware_version(
682 const char *interface
,
683 const char *property
,
684 sd_bus_message
*reply
,
686 sd_bus_error
*error
) {
688 _cleanup_free_
char *firmware_version
= NULL
;
690 (void) get_firmware_version(&firmware_version
);
692 return sd_bus_message_append(reply
, "s", firmware_version
);
695 static int property_get_firmware_vendor(
698 const char *interface
,
699 const char *property
,
700 sd_bus_message
*reply
,
702 sd_bus_error
*error
) {
704 _cleanup_free_
char *firmware_vendor
= NULL
;
706 (void) get_firmware_vendor(&firmware_vendor
);
708 return sd_bus_message_append(reply
, "s", firmware_vendor
);
711 static int property_get_firmware_date(
714 const char *interface
,
715 const char *property
,
716 sd_bus_message
*reply
,
718 sd_bus_error
*error
) {
720 usec_t firmware_date
= USEC_INFINITY
;
722 (void) get_firmware_date(&firmware_date
);
724 return sd_bus_message_append(reply
, "t", firmware_date
);
726 static int property_get_hostname(
729 const char *interface
,
730 const char *property
,
731 sd_bus_message
*reply
,
733 sd_bus_error
*error
) {
735 _cleanup_free_
char *hn
= NULL
;
738 r
= gethostname_strict(&hn
);
743 hn
= get_default_hostname();
748 return sd_bus_message_append(reply
, "s", hn
);
751 static int property_get_static_hostname(
754 const char *interface
,
755 const char *property
,
756 sd_bus_message
*reply
,
758 sd_bus_error
*error
) {
760 Context
*c
= ASSERT_PTR(userdata
);
762 context_read_etc_hostname(c
);
764 return sd_bus_message_append(reply
, "s", c
->data
[PROP_STATIC_HOSTNAME
]);
767 static int property_get_default_hostname(
770 const char *interface
,
771 const char *property
,
772 sd_bus_message
*reply
,
774 sd_bus_error
*error
) {
776 _cleanup_free_
char *hn
= NULL
;
778 hn
= get_default_hostname();
782 return sd_bus_message_append(reply
, "s", hn
);
785 static void context_determine_hostname_source(Context
*c
) {
786 _cleanup_free_
char *hostname
= NULL
;
791 if (c
->hostname_source
>= 0)
794 (void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST
, &hostname
);
796 if (streq_ptr(hostname
, c
->data
[PROP_STATIC_HOSTNAME
]))
797 c
->hostname_source
= HOSTNAME_STATIC
;
799 _cleanup_free_
char *fallback
= NULL
;
801 /* If the hostname was not set by us, try to figure out where it came from. If we set it to
802 * the default hostname, the file will tell us. We compare the string because it is possible
803 * that the hostname was set by an older version that had a different fallback, in the initrd
804 * or before we reexecuted. */
806 r
= read_one_line_file("/run/systemd/default-hostname", &fallback
);
807 if (r
< 0 && r
!= -ENOENT
)
808 log_warning_errno(r
, "Failed to read /run/systemd/default-hostname, ignoring: %m");
810 if (streq_ptr(fallback
, hostname
))
811 c
->hostname_source
= HOSTNAME_DEFAULT
;
813 c
->hostname_source
= HOSTNAME_TRANSIENT
;
817 static int property_get_hostname_source(
820 const char *interface
,
821 const char *property
,
822 sd_bus_message
*reply
,
824 sd_bus_error
*error
) {
826 Context
*c
= ASSERT_PTR(userdata
);
828 context_read_etc_hostname(c
);
829 context_determine_hostname_source(c
);
831 return sd_bus_message_append(reply
, "s", hostname_source_to_string(c
->hostname_source
));
834 static int property_get_machine_info_field(
837 const char *interface
,
838 const char *property
,
839 sd_bus_message
*reply
,
841 sd_bus_error
*error
) {
846 /* Acquire the context object without this property's userdata offset added. Explanation: we want
847 * access to two pointers here: a) the main context object we cache all properties in, and b) the
848 * pointer to the property field inside the context object that we are supposed to update and
849 * use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us
850 * from the 'userdata' pointer we supplied when the vtable was registered, with the offset we
851 * specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from
852 * the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot
853 * object is (which encapsulates the vtable registration), and then query the 'userdata' field
854 * directly off it. */
855 assert_se(slot
= sd_bus_get_current_slot(bus
));
856 assert_se(c
= sd_bus_slot_get_userdata(slot
));
858 context_read_machine_info(c
);
860 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
863 static int property_get_os_release_field(
866 const char *interface
,
867 const char *property
,
868 sd_bus_message
*reply
,
870 sd_bus_error
*error
) {
875 /* As above, acquire the current context without this property's userdata offset added. */
876 assert_se(slot
= sd_bus_get_current_slot(bus
));
877 assert_se(c
= sd_bus_slot_get_userdata(slot
));
879 context_read_os_release(c
);
881 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
884 static int property_get_icon_name(
887 const char *interface
,
888 const char *property
,
889 sd_bus_message
*reply
,
891 sd_bus_error
*error
) {
893 _cleanup_free_
char *n
= NULL
;
894 Context
*c
= userdata
;
897 context_read_machine_info(c
);
899 if (isempty(c
->data
[PROP_ICON_NAME
]))
900 name
= n
= context_fallback_icon_name(c
);
902 name
= c
->data
[PROP_ICON_NAME
];
907 return sd_bus_message_append(reply
, "s", name
);
910 static int property_get_chassis(
913 const char *interface
,
914 const char *property
,
915 sd_bus_message
*reply
,
917 sd_bus_error
*error
) {
919 _cleanup_free_
char *chassis
= NULL
;
920 Context
*c
= userdata
;
922 context_read_machine_info(c
);
924 chassis
= context_get_chassis(c
);
926 return sd_bus_message_append(reply
, "s", chassis
);
929 static int property_get_uname_field(
932 const char *interface
,
933 const char *property
,
934 sd_bus_message
*reply
,
936 sd_bus_error
*error
) {
940 assert_se(uname(&u
) >= 0);
942 return sd_bus_message_append(reply
, "s", (char*) &u
+ PTR_TO_SIZE(userdata
));
945 static int method_set_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
946 Context
*c
= ASSERT_PTR(userdata
);
952 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
956 name
= empty_to_null(name
);
958 /* We always go through with the procedure below without comparing to the current hostname, because
959 * we might want to adjust hostname source information even if the actual hostname is unchanged. */
961 if (name
&& !hostname_is_valid(name
, 0))
962 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", name
);
964 context_read_etc_hostname(c
);
966 r
= bus_verify_polkit_async(
969 "org.freedesktop.hostname1.set-hostname",
978 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
980 r
= context_update_kernel_hostname(c
, name
);
982 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
984 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
985 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
986 "Hostname", "HostnameSource", NULL
);
988 return sd_bus_reply_method_return(m
, NULL
);
991 static int method_set_static_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
992 Context
*c
= ASSERT_PTR(userdata
);
999 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
1003 name
= empty_to_null(name
);
1005 context_read_etc_hostname(c
);
1007 if (streq_ptr(name
, c
->data
[PROP_STATIC_HOSTNAME
]))
1008 return sd_bus_reply_method_return(m
, NULL
);
1010 if (name
&& !hostname_is_valid(name
, 0))
1011 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid static hostname '%s'", name
);
1013 r
= bus_verify_polkit_async(
1016 "org.freedesktop.hostname1.set-static-hostname",
1020 &c
->polkit_registry
,
1025 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1027 r
= free_and_strdup_warn(&c
->data
[PROP_STATIC_HOSTNAME
], name
);
1031 r
= context_write_data_static_hostname(c
);
1033 log_error_errno(r
, "Failed to write static hostname: %m");
1034 if (ERRNO_IS_PRIVILEGE(r
))
1035 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/hostname.");
1037 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/hostname is in a read-only filesystem.");
1038 return sd_bus_error_set_errnof(error
, r
, "Failed to set static hostname: %m");
1041 r
= context_update_kernel_hostname(c
, NULL
);
1043 log_error_errno(r
, "Failed to set hostname: %m");
1044 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
1047 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
1048 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
1049 "StaticHostname", "Hostname", "HostnameSource", NULL
);
1051 return sd_bus_reply_method_return(m
, NULL
);
1054 static int set_machine_info(Context
*c
, sd_bus_message
*m
, int prop
, sd_bus_message_handler_t cb
, sd_bus_error
*error
) {
1062 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
1066 name
= empty_to_null(name
);
1068 context_read_machine_info(c
);
1070 if (streq_ptr(name
, c
->data
[prop
]))
1071 return sd_bus_reply_method_return(m
, NULL
);
1073 if (!isempty(name
)) {
1074 /* The icon name might ultimately be used as file
1075 * name, so better be safe than sorry */
1077 if (prop
== PROP_ICON_NAME
&& !filename_is_valid(name
))
1078 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid icon name '%s'", name
);
1079 if (prop
== PROP_PRETTY_HOSTNAME
&& string_has_cc(name
, NULL
))
1080 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid pretty hostname '%s'", name
);
1081 if (prop
== PROP_CHASSIS
&& !valid_chassis(name
))
1082 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid chassis '%s'", name
);
1083 if (prop
== PROP_DEPLOYMENT
&& !valid_deployment(name
))
1084 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid deployment '%s'", name
);
1085 if (prop
== PROP_LOCATION
&& string_has_cc(name
, NULL
))
1086 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid location '%s'", name
);
1089 /* Since the pretty hostname should always be changed at the
1090 * same time as the static one, use the same policy action for
1093 r
= bus_verify_polkit_async(
1096 prop
== PROP_PRETTY_HOSTNAME
? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
1100 &c
->polkit_registry
,
1105 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1107 r
= free_and_strdup_warn(&c
->data
[prop
], name
);
1111 r
= context_write_data_machine_info(c
);
1113 log_error_errno(r
, "Failed to write machine info: %m");
1114 if (ERRNO_IS_PRIVILEGE(r
))
1115 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/machine-info.");
1117 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/machine-info is in a read-only filesystem.");
1118 return sd_bus_error_set_errnof(error
, r
, "Failed to write machine info: %m");
1121 log_info("Changed %s to '%s'",
1122 prop
== PROP_PRETTY_HOSTNAME
? "pretty hostname" :
1123 prop
== PROP_DEPLOYMENT
? "deployment" :
1124 prop
== PROP_LOCATION
? "location" :
1125 prop
== PROP_CHASSIS
? "chassis" : "icon name", strna(c
->data
[prop
]));
1127 (void) sd_bus_emit_properties_changed(
1128 sd_bus_message_get_bus(m
),
1129 "/org/freedesktop/hostname1",
1130 "org.freedesktop.hostname1",
1131 prop
== PROP_PRETTY_HOSTNAME
? "PrettyHostname" :
1132 prop
== PROP_DEPLOYMENT
? "Deployment" :
1133 prop
== PROP_LOCATION
? "Location" :
1134 prop
== PROP_CHASSIS
? "Chassis" : "IconName" , NULL
);
1136 return sd_bus_reply_method_return(m
, NULL
);
1139 static int method_set_pretty_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1140 return set_machine_info(userdata
, m
, PROP_PRETTY_HOSTNAME
, method_set_pretty_hostname
, error
);
1143 static int method_set_icon_name(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1144 return set_machine_info(userdata
, m
, PROP_ICON_NAME
, method_set_icon_name
, error
);
1147 static int method_set_chassis(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1148 return set_machine_info(userdata
, m
, PROP_CHASSIS
, method_set_chassis
, error
);
1151 static int method_set_deployment(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1152 return set_machine_info(userdata
, m
, PROP_DEPLOYMENT
, method_set_deployment
, error
);
1155 static int method_set_location(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1156 return set_machine_info(userdata
, m
, PROP_LOCATION
, method_set_location
, error
);
1159 static int method_get_product_uuid(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1160 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1161 Context
*c
= ASSERT_PTR(userdata
);
1167 r
= sd_bus_message_read(m
, "b", &interactive
);
1171 r
= bus_verify_polkit_async(
1174 "org.freedesktop.hostname1.get-product-uuid",
1178 &c
->polkit_registry
,
1183 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1185 r
= id128_get_product(&uuid
);
1187 if (r
== -EADDRNOTAVAIL
)
1188 log_debug_errno(r
, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
1190 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
1191 "Failed to read product UUID, ignoring: %m");
1193 return sd_bus_error_set(error
, BUS_ERROR_NO_PRODUCT_UUID
,
1194 "Failed to read product UUID from firmware.");
1197 r
= sd_bus_message_new_method_return(m
, &reply
);
1201 r
= sd_bus_message_append_array(reply
, 'y', uuid
.bytes
, sizeof(uuid
.bytes
));
1205 return sd_bus_send(NULL
, reply
, NULL
);
1208 static int method_get_hardware_serial(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1209 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1210 _cleanup_free_
char *serial
= NULL
;
1211 Context
*c
= ASSERT_PTR(userdata
);
1216 r
= bus_verify_polkit_async(
1219 "org.freedesktop.hostname1.get-hardware-serial",
1223 &c
->polkit_registry
,
1228 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1230 r
= get_hardware_serial(&serial
);
1234 r
= sd_bus_message_new_method_return(m
, &reply
);
1238 r
= sd_bus_message_append(reply
, "s", serial
);
1242 return sd_bus_send(NULL
, reply
, NULL
);
1245 static int method_describe(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1246 _cleanup_free_
char *hn
= NULL
, *dhn
= NULL
, *in
= NULL
, *text
= NULL
,
1247 *chassis
= NULL
, *vendor
= NULL
, *model
= NULL
, *serial
= NULL
, *firmware_version
= NULL
,
1248 *firmware_vendor
= NULL
;
1249 usec_t firmware_date
= USEC_INFINITY
;
1250 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1251 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1252 sd_id128_t product_uuid
= SD_ID128_NULL
;
1253 Context
*c
= ASSERT_PTR(userdata
);
1260 r
= bus_verify_polkit_async(
1263 "org.freedesktop.hostname1.get-description",
1267 &c
->polkit_registry
,
1270 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1272 /* We ignore all authentication errors here, since most data is unprivileged, the one exception being
1273 * the product ID which we'll check explicitly. */
1276 context_read_etc_hostname(c
);
1277 context_read_machine_info(c
);
1278 context_read_os_release(c
);
1279 context_determine_hostname_source(c
);
1281 r
= gethostname_strict(&hn
);
1284 return log_error_errno(r
, "Failed to read local host name: %m");
1286 hn
= get_default_hostname();
1291 dhn
= get_default_hostname();
1295 if (isempty(c
->data
[PROP_ICON_NAME
]))
1296 in
= context_fallback_icon_name(c
);
1298 chassis
= context_get_chassis(c
);
1300 assert_se(uname(&u
) >= 0);
1302 if (isempty(c
->data
[PROP_HARDWARE_VENDOR
]))
1303 (void) get_hardware_vendor(&vendor
);
1304 if (isempty(c
->data
[PROP_HARDWARE_MODEL
]))
1305 (void) get_hardware_model(&model
);
1308 /* The product UUID and hardware serial is only available to privileged clients */
1309 (void) id128_get_product(&product_uuid
);
1310 (void) get_hardware_serial(&serial
);
1312 (void) get_firmware_version(&firmware_version
);
1313 (void) get_firmware_vendor(&firmware_vendor
);
1314 (void) get_firmware_date(&firmware_date
);
1316 r
= json_build(&v
, JSON_BUILD_OBJECT(
1317 JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn
)),
1318 JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c
->data
[PROP_STATIC_HOSTNAME
])),
1319 JSON_BUILD_PAIR("PrettyHostname", JSON_BUILD_STRING(c
->data
[PROP_PRETTY_HOSTNAME
])),
1320 JSON_BUILD_PAIR("DefaultHostname", JSON_BUILD_STRING(dhn
)),
1321 JSON_BUILD_PAIR("HostnameSource", JSON_BUILD_STRING(hostname_source_to_string(c
->hostname_source
))),
1322 JSON_BUILD_PAIR("IconName", JSON_BUILD_STRING(in
?: c
->data
[PROP_ICON_NAME
])),
1323 JSON_BUILD_PAIR("Chassis", JSON_BUILD_STRING(chassis
)),
1324 JSON_BUILD_PAIR("Deployment", JSON_BUILD_STRING(c
->data
[PROP_DEPLOYMENT
])),
1325 JSON_BUILD_PAIR("Location", JSON_BUILD_STRING(c
->data
[PROP_LOCATION
])),
1326 JSON_BUILD_PAIR("KernelName", JSON_BUILD_STRING(u
.sysname
)),
1327 JSON_BUILD_PAIR("KernelRelease", JSON_BUILD_STRING(u
.release
)),
1328 JSON_BUILD_PAIR("KernelVersion", JSON_BUILD_STRING(u
.version
)),
1329 JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c
->data
[PROP_OS_PRETTY_NAME
])),
1330 JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c
->data
[PROP_OS_CPE_NAME
])),
1331 JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c
->data
[PROP_OS_HOME_URL
])),
1332 JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor
?: c
->data
[PROP_HARDWARE_VENDOR
])),
1333 JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model
?: c
->data
[PROP_HARDWARE_MODEL
])),
1334 JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial
)),
1335 JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version
)),
1336 JSON_BUILD_PAIR("FirmwareVendor", JSON_BUILD_STRING(firmware_vendor
)),
1337 JSON_BUILD_PAIR_FINITE_USEC("FirmwareDate", firmware_date
),
1338 JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_ID128(product_uuid
)),
1339 JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_NULL
)));
1342 return log_error_errno(r
, "Failed to build JSON data: %m");
1344 r
= json_variant_format(v
, 0, &text
);
1346 return log_error_errno(r
, "Failed to format JSON data: %m");
1348 r
= sd_bus_message_new_method_return(m
, &reply
);
1352 r
= sd_bus_message_append(reply
, "s", text
);
1356 return sd_bus_send(NULL
, reply
, NULL
);
1359 static const sd_bus_vtable hostname_vtable
[] = {
1360 SD_BUS_VTABLE_START(0),
1361 SD_BUS_PROPERTY("Hostname", "s", property_get_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1362 SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1363 SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_PRETTY_HOSTNAME
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1364 SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1365 SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1366 SD_BUS_PROPERTY("IconName", "s", property_get_icon_name
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1367 SD_BUS_PROPERTY("Chassis", "s", property_get_chassis
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1368 SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_DEPLOYMENT
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1369 SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_LOCATION
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1370 SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field
, offsetof(struct utsname
, sysname
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1371 SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field
, offsetof(struct utsname
, release
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1372 SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field
, offsetof(struct utsname
, version
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1373 SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_PRETTY_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1374 SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_CPE_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1375 SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_HOME_URL
, SD_BUS_VTABLE_PROPERTY_CONST
),
1376 SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1377 SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1378 SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1379 SD_BUS_PROPERTY("FirmwareVendor", "s", property_get_firmware_vendor
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1380 SD_BUS_PROPERTY("FirmwareDate", "t", property_get_firmware_date
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1382 SD_BUS_METHOD_WITH_ARGS("SetHostname",
1383 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1385 method_set_hostname
,
1386 SD_BUS_VTABLE_UNPRIVILEGED
),
1387 SD_BUS_METHOD_WITH_ARGS("SetStaticHostname",
1388 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1390 method_set_static_hostname
,
1391 SD_BUS_VTABLE_UNPRIVILEGED
),
1392 SD_BUS_METHOD_WITH_ARGS("SetPrettyHostname",
1393 SD_BUS_ARGS("s", hostname
, "b", interactive
),
1395 method_set_pretty_hostname
,
1396 SD_BUS_VTABLE_UNPRIVILEGED
),
1397 SD_BUS_METHOD_WITH_ARGS("SetIconName",
1398 SD_BUS_ARGS("s", icon
, "b", interactive
),
1400 method_set_icon_name
,
1401 SD_BUS_VTABLE_UNPRIVILEGED
),
1402 SD_BUS_METHOD_WITH_ARGS("SetChassis",
1403 SD_BUS_ARGS("s", chassis
, "b", interactive
),
1406 SD_BUS_VTABLE_UNPRIVILEGED
),
1407 SD_BUS_METHOD_WITH_ARGS("SetDeployment",
1408 SD_BUS_ARGS("s", deployment
, "b", interactive
),
1410 method_set_deployment
,
1411 SD_BUS_VTABLE_UNPRIVILEGED
),
1412 SD_BUS_METHOD_WITH_ARGS("SetLocation",
1413 SD_BUS_ARGS("s", location
, "b", interactive
),
1415 method_set_location
,
1416 SD_BUS_VTABLE_UNPRIVILEGED
),
1417 SD_BUS_METHOD_WITH_ARGS("GetProductUUID",
1418 SD_BUS_ARGS("b", interactive
),
1419 SD_BUS_RESULT("ay", uuid
),
1420 method_get_product_uuid
,
1421 SD_BUS_VTABLE_UNPRIVILEGED
),
1422 SD_BUS_METHOD_WITH_ARGS("GetHardwareSerial",
1424 SD_BUS_RESULT("s", serial
),
1425 method_get_hardware_serial
,
1426 SD_BUS_VTABLE_UNPRIVILEGED
),
1427 SD_BUS_METHOD_WITH_ARGS("Describe",
1429 SD_BUS_RESULT("s", json
),
1431 SD_BUS_VTABLE_UNPRIVILEGED
),
1436 static const BusObjectImplementation manager_object
= {
1437 "/org/freedesktop/hostname1",
1438 "org.freedesktop.hostname1",
1439 .vtables
= BUS_VTABLES(hostname_vtable
),
1442 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**ret
) {
1443 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1450 r
= sd_bus_default_system(&bus
);
1452 return log_error_errno(r
, "Failed to get system bus connection: %m");
1454 r
= bus_add_implementation(bus
, &manager_object
, c
);
1458 r
= bus_log_control_api_register(bus
);
1462 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.hostname1", 0, NULL
, NULL
);
1464 return log_error_errno(r
, "Failed to request name: %m");
1466 r
= sd_bus_attach_event(bus
, event
, 0);
1468 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1470 *ret
= TAKE_PTR(bus
);
1474 static int run(int argc
, char *argv
[]) {
1475 _cleanup_(context_destroy
) Context context
= {
1476 .hostname_source
= _HOSTNAME_INVALID
, /* appropriate value will be set later */
1478 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1479 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1484 r
= service_parse_argv("systemd-hostnamed.service",
1485 "Manage the system hostname and related metadata.",
1486 BUS_IMPLEMENTATIONS(&manager_object
,
1487 &log_control_object
),
1494 r
= mac_selinux_init();
1498 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1500 r
= sd_event_default(&event
);
1502 return log_error_errno(r
, "Failed to allocate event loop: %m");
1504 (void) sd_event_set_watchdog(event
, true);
1506 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1508 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1510 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1512 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1514 r
= connect_bus(&context
, event
, &bus
);
1518 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1520 return log_error_errno(r
, "Failed to run event loop: %m");
1525 DEFINE_MAIN_FUNCTION(run
);