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
));
133 r
= parse_env_file(NULL
, "/etc/machine-info",
134 "PRETTY_HOSTNAME", &c
->data
[PROP_PRETTY_HOSTNAME
],
135 "ICON_NAME", &c
->data
[PROP_ICON_NAME
],
136 "CHASSIS", &c
->data
[PROP_CHASSIS
],
137 "DEPLOYMENT", &c
->data
[PROP_DEPLOYMENT
],
138 "LOCATION", &c
->data
[PROP_LOCATION
],
139 "VENDOR", &c
->data
[PROP_VENDOR
],
140 "MODEL", &c
->data
[PROP_MODEL
]);
141 if (r
< 0 && r
!= -ENOENT
)
142 log_warning_errno(r
, "Failed to read /etc/machine-info, ignoring: %m");
144 c
->etc_machine_info_stat
= current_stat
;
147 static void context_read_os_release(Context
*c
) {
148 struct stat current_stat
= {};
153 if ((stat("/etc/os-release", ¤t_stat
) >= 0 ||
154 stat("/usr/lib/os-release", ¤t_stat
) >= 0) &&
155 stat_inode_unmodified(&c
->etc_os_release_stat
, ¤t_stat
))
159 (UINT64_C(1) << PROP_OS_PRETTY_NAME
) |
160 (UINT64_C(1) << PROP_OS_CPE_NAME
) |
161 (UINT64_C(1) << PROP_OS_HOME_URL
));
163 r
= parse_os_release(NULL
,
164 "PRETTY_NAME", &c
->data
[PROP_OS_PRETTY_NAME
],
165 "CPE_NAME", &c
->data
[PROP_OS_CPE_NAME
],
166 "HOME_URL", &c
->data
[PROP_OS_HOME_URL
]);
167 if (r
< 0 && r
!= -ENOENT
)
168 log_warning_errno(r
, "Failed to read os-release file, ignoring: %m");
170 c
->etc_os_release_stat
= current_stat
;
173 static int get_dmi_data(const char *database_key
, const char *regular_key
, char **ret
) {
174 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
175 _cleanup_free_
char *b
= NULL
;
176 const char *s
= NULL
;
179 r
= sd_device_new_from_syspath(&device
, "/sys/class/dmi/id");
181 return log_debug_errno(r
, "Failed to open /sys/class/dmi/id device, ignoring: %m");
184 (void) sd_device_get_property_value(device
, database_key
, &s
);
185 if (!s
&& regular_key
)
186 (void) sd_device_get_property_value(device
, regular_key
, &s
);
201 static int get_hardware_vendor(char **ret
) {
202 return get_dmi_data("ID_VENDOR_FROM_DATABASE", "ID_VENDOR", ret
);
205 static int get_hardware_model(char **ret
) {
206 return get_dmi_data("ID_MODEL_FROM_DATABASE", "ID_MODEL", ret
);
209 static int get_hardware_serial(char **ret
) {
210 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
211 _cleanup_free_
char *b
= NULL
;
212 const char *s
= NULL
;
215 r
= sd_device_new_from_syspath(&device
, "/sys/class/dmi/id");
217 return log_debug_errno(r
, "Failed to open /sys/class/dmi/id device, ignoring: %m");
219 (void) sd_device_get_sysattr_value(device
, "product_serial", &s
);
221 /* Fallback to board serial */
222 (void) sd_device_get_sysattr_value(device
, "board_serial", &s
);
236 static const char* valid_chassis(const char *chassis
) {
253 static bool valid_deployment(const char *deployment
) {
256 return in_charset(deployment
, VALID_DEPLOYMENT_CHARS
);
259 static const char* fallback_chassis(void) {
261 _cleanup_free_
char *type
= NULL
;
265 v
= detect_virtualization();
267 log_debug_errno(v
, "Failed to detect virtualization, ignoring: %m");
268 else if (VIRTUALIZATION_IS_VM(v
))
270 else if (VIRTUALIZATION_IS_CONTAINER(v
))
273 r
= read_one_line_file("/sys/class/dmi/id/chassis_type", &type
);
275 log_debug_errno(r
, "Failed to read DMI chassis type, ignoring: %m");
279 r
= safe_atou(type
, &t
);
281 log_debug_errno(r
, "Failed to parse DMI chassis type \"%s\", ignoring: %m", type
);
285 /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any
286 * additional guesswork on top of that.
288 * See the SMBIOS Specification 3.0 section 7.4.1 for details about the values listed here:
290 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.0.0.pdf
295 case 0x3: /* Desktop */
296 case 0x4: /* Low Profile Desktop */
297 case 0x6: /* Mini Tower */
298 case 0x7: /* Tower */
299 case 0xD: /* All in one (i.e. PC built into monitor) */
302 case 0x8: /* Portable */
303 case 0x9: /* Laptop */
304 case 0xA: /* Notebook */
305 case 0xE: /* Sub Notebook */
308 case 0xB: /* Hand Held */
311 case 0x11: /* Main Server Chassis */
312 case 0x1C: /* Blade */
313 case 0x1D: /* Blade Enclosure */
316 case 0x1E: /* Tablet */
319 case 0x1F: /* Convertible */
320 case 0x20: /* Detachable */
321 return "convertible";
324 log_debug("Unhandled DMI chassis type 0x%02x, ignoring.", t
);
329 r
= read_one_line_file("/sys/firmware/acpi/pm_profile", &type
);
331 log_debug_errno(r
, "Failed read ACPI PM profile, ignoring: %m");
335 r
= safe_atou(type
, &t
);
337 log_debug_errno(r
, "Failed parse ACPI PM profile \"%s\", ignoring: %m", type
);
341 /* We only list the really obvious cases here as the ACPI data is not really super reliable.
343 * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
345 * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
350 case 1: /* Desktop */
351 case 3: /* Workstation */
352 case 6: /* Appliance PC */
358 case 4: /* Enterprise Server */
359 case 5: /* SOHO Server */
360 case 7: /* Performance Server */
367 log_debug("Unhandled ACPI PM profile 0x%02x, ignoring.", t
);
372 r
= read_one_line_file("/proc/device-tree/chassis-type", &type
);
374 log_debug_errno(r
, "Failed to read device-tree chassis type, ignoring: %m");
378 /* Note that the Devicetree specification uses the very same vocabulary
379 * of chassis types as we do, hence we do not need to translate these types:
381 * https://github.com/devicetree-org/devicetree-specification/blob/master/source/chapter3-devicenodes.rst */
382 chassis
= valid_chassis(type
);
384 log_debug("Invalid device-tree chassis type \"%s\", ignoring.", type
);
388 static char* context_get_chassis(Context
*c
) {
389 const char *fallback
;
394 if (!isempty(c
->data
[PROP_CHASSIS
]))
395 return strdup(c
->data
[PROP_CHASSIS
]);
397 if (get_dmi_data("ID_CHASSIS", NULL
, &dmi
) >= 0)
400 fallback
= fallback_chassis();
402 return strdup(fallback
);
407 static char* context_fallback_icon_name(Context
*c
) {
408 _cleanup_free_
char *chassis
= NULL
;
412 chassis
= context_get_chassis(c
);
414 return strjoin("computer-", chassis
);
416 return strdup("computer");
419 static int context_update_kernel_hostname(
421 const char *transient_hn
) {
423 _cleanup_free_
char *_hn_free
= NULL
;
430 /* /etc/hostname has the highest preference ... */
431 if (c
->data
[PROP_STATIC_HOSTNAME
]) {
432 hn
= c
->data
[PROP_STATIC_HOSTNAME
];
433 hns
= HOSTNAME_STATIC
;
435 /* ... the transient hostname, (ie: DHCP) comes next ... */
436 } else if (transient_hn
) {
438 hns
= HOSTNAME_TRANSIENT
;
440 /* ... and the ultimate fallback */
442 hn
= _hn_free
= get_default_hostname();
446 hns
= HOSTNAME_DEFAULT
;
449 r
= sethostname_idempotent(hn
);
451 return log_error_errno(r
, "Failed to set hostname: %m");
453 if (c
->hostname_source
!= hns
) {
454 c
->hostname_source
= hns
;
458 (void) nscd_flush_cache(STRV_MAKE("hosts"));
461 log_debug("Hostname was already set to <%s>.", hn
);
463 log_info("Hostname set to <%s> (%s)", hn
, hostname_source_to_string(hns
));
465 hostname_update_source_hint(hn
, hns
);
468 return r
; /* 0 if no change, 1 if something was done */
471 static void unset_statp(struct stat
**p
) {
475 **p
= (struct stat
) {};
478 static int context_write_data_static_hostname(Context
*c
) {
479 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
484 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
485 * already, even if we can't make it hit the disk. */
486 s
= &c
->etc_hostname_stat
;
488 if (isempty(c
->data
[PROP_STATIC_HOSTNAME
])) {
489 if (unlink("/etc/hostname") < 0 && errno
!= ENOENT
)
496 r
= write_string_file_atomic_label("/etc/hostname", c
->data
[PROP_STATIC_HOSTNAME
]);
504 static int context_write_data_machine_info(Context
*c
) {
505 _cleanup_(unset_statp
) struct stat
*s
= NULL
;
506 static const char * const name
[_PROP_MAX
] = {
507 [PROP_PRETTY_HOSTNAME
] = "PRETTY_HOSTNAME",
508 [PROP_ICON_NAME
] = "ICON_NAME",
509 [PROP_CHASSIS
] = "CHASSIS",
510 [PROP_DEPLOYMENT
] = "DEPLOYMENT",
511 [PROP_LOCATION
] = "LOCATION",
513 _cleanup_strv_free_
char **l
= NULL
;
518 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
519 * already, even if we can't make it hit the disk. */
520 s
= &c
->etc_machine_info_stat
;
522 r
= load_env_file(NULL
, "/etc/machine-info", &l
);
523 if (r
< 0 && r
!= -ENOENT
)
526 for (int p
= PROP_PRETTY_HOSTNAME
; p
<= PROP_LOCATION
; p
++) {
529 r
= strv_env_assign(&l
, name
[p
], empty_to_null(c
->data
[p
]));
534 if (strv_isempty(l
)) {
535 if (unlink("/etc/machine-info") < 0 && errno
!= ENOENT
)
542 r
= write_env_file_label("/etc/machine-info", l
);
550 static int property_get_hardware_property(
551 sd_bus_message
*reply
,
554 int (*getter
)(char **)) {
556 _cleanup_free_
char *from_dmi
= NULL
;
560 assert(IN_SET(prop
, PROP_VENDOR
, PROP_MODEL
));
563 context_read_machine_info(c
);
565 if (isempty(c
->data
[prop
]))
566 (void) getter(&from_dmi
);
568 return sd_bus_message_append(reply
, "s", from_dmi
?: c
->data
[prop
]);
571 static int property_get_hardware_vendor(
574 const char *interface
,
575 const char *property
,
576 sd_bus_message
*reply
,
578 sd_bus_error
*error
) {
580 return property_get_hardware_property(reply
, userdata
, PROP_VENDOR
, get_hardware_vendor
);
583 static int property_get_hardware_model(
586 const char *interface
,
587 const char *property
,
588 sd_bus_message
*reply
,
590 sd_bus_error
*error
) {
592 return property_get_hardware_property(reply
, userdata
, PROP_MODEL
, get_hardware_model
);
595 static int property_get_hostname(
598 const char *interface
,
599 const char *property
,
600 sd_bus_message
*reply
,
602 sd_bus_error
*error
) {
604 _cleanup_free_
char *hn
= NULL
;
607 r
= gethostname_strict(&hn
);
612 hn
= get_default_hostname();
617 return sd_bus_message_append(reply
, "s", hn
);
620 static int property_get_static_hostname(
623 const char *interface
,
624 const char *property
,
625 sd_bus_message
*reply
,
627 sd_bus_error
*error
) {
629 Context
*c
= userdata
;
632 context_read_etc_hostname(c
);
634 return sd_bus_message_append(reply
, "s", c
->data
[PROP_STATIC_HOSTNAME
]);
637 static int property_get_default_hostname(
640 const char *interface
,
641 const char *property
,
642 sd_bus_message
*reply
,
644 sd_bus_error
*error
) {
646 _cleanup_free_
char *hn
= NULL
;
648 hn
= get_default_hostname();
652 return sd_bus_message_append(reply
, "s", hn
);
655 static void context_determine_hostname_source(Context
*c
) {
656 _cleanup_free_
char *hostname
= NULL
;
661 if (c
->hostname_source
>= 0)
664 (void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST
, &hostname
);
666 if (streq_ptr(hostname
, c
->data
[PROP_STATIC_HOSTNAME
]))
667 c
->hostname_source
= HOSTNAME_STATIC
;
669 _cleanup_free_
char *fallback
= NULL
;
671 /* If the hostname was not set by us, try to figure out where it came from. If we set it to
672 * the default hostname, the file will tell us. We compare the string because it is possible
673 * that the hostname was set by an older version that had a different fallback, in the
674 * initramfs or before we reexecuted. */
676 r
= read_one_line_file("/run/systemd/default-hostname", &fallback
);
677 if (r
< 0 && r
!= -ENOENT
)
678 log_warning_errno(r
, "Failed to read /run/systemd/default-hostname, ignoring: %m");
680 if (streq_ptr(fallback
, hostname
))
681 c
->hostname_source
= HOSTNAME_DEFAULT
;
683 c
->hostname_source
= HOSTNAME_TRANSIENT
;
687 static int property_get_hostname_source(
690 const char *interface
,
691 const char *property
,
692 sd_bus_message
*reply
,
694 sd_bus_error
*error
) {
696 Context
*c
= userdata
;
699 context_read_etc_hostname(c
);
700 context_determine_hostname_source(c
);
702 return sd_bus_message_append(reply
, "s", hostname_source_to_string(c
->hostname_source
));
705 static int property_get_machine_info_field(
708 const char *interface
,
709 const char *property
,
710 sd_bus_message
*reply
,
712 sd_bus_error
*error
) {
717 /* Acquire the context object without this property's userdata offset added. Explanation: we want
718 * access to two pointers here: a) the main context object we cache all properties in, and b) the
719 * pointer to the property field inside the context object that we are supposed to update and
720 * use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us
721 * from the 'userdata' pointer we supplied when the vtable was registered, with the offset we
722 * specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from
723 * the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot
724 * object is (which encapsulates the vtable registration), and then query the 'userdata' field
725 * directly off it. */
726 assert_se(slot
= sd_bus_get_current_slot(bus
));
727 assert_se(c
= sd_bus_slot_get_userdata(slot
));
729 context_read_machine_info(c
);
731 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
734 static int property_get_os_release_field(
737 const char *interface
,
738 const char *property
,
739 sd_bus_message
*reply
,
741 sd_bus_error
*error
) {
746 /* As above, acquire the current context without this property's userdata offset added. */
747 assert_se(slot
= sd_bus_get_current_slot(bus
));
748 assert_se(c
= sd_bus_slot_get_userdata(slot
));
750 context_read_os_release(c
);
752 return sd_bus_message_append(reply
, "s", *(char**) userdata
);
755 static int property_get_icon_name(
758 const char *interface
,
759 const char *property
,
760 sd_bus_message
*reply
,
762 sd_bus_error
*error
) {
764 _cleanup_free_
char *n
= NULL
;
765 Context
*c
= userdata
;
768 context_read_machine_info(c
);
770 if (isempty(c
->data
[PROP_ICON_NAME
]))
771 name
= n
= context_fallback_icon_name(c
);
773 name
= c
->data
[PROP_ICON_NAME
];
778 return sd_bus_message_append(reply
, "s", name
);
781 static int property_get_chassis(
784 const char *interface
,
785 const char *property
,
786 sd_bus_message
*reply
,
788 sd_bus_error
*error
) {
790 _cleanup_free_
char *chassis
= NULL
;
791 Context
*c
= userdata
;
793 context_read_machine_info(c
);
795 chassis
= context_get_chassis(c
);
797 return sd_bus_message_append(reply
, "s", chassis
);
800 static int property_get_uname_field(
803 const char *interface
,
804 const char *property
,
805 sd_bus_message
*reply
,
807 sd_bus_error
*error
) {
811 assert_se(uname(&u
) >= 0);
813 return sd_bus_message_append(reply
, "s", (char*) &u
+ PTR_TO_SIZE(userdata
));
816 static int method_set_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
817 Context
*c
= userdata
;
824 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
828 name
= empty_to_null(name
);
830 /* We always go through with the procedure below without comparing to the current hostname, because
831 * we might want to adjust hostname source information even if the actual hostname is unchanged. */
833 if (name
&& !hostname_is_valid(name
, 0))
834 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid hostname '%s'", name
);
836 context_read_etc_hostname(c
);
838 r
= bus_verify_polkit_async(
841 "org.freedesktop.hostname1.set-hostname",
850 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
852 r
= context_update_kernel_hostname(c
, name
);
854 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
856 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
857 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
858 "Hostname", "HostnameSource", NULL
);
860 return sd_bus_reply_method_return(m
, NULL
);
863 static int method_set_static_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
864 Context
*c
= userdata
;
872 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
876 name
= empty_to_null(name
);
878 context_read_etc_hostname(c
);
880 if (streq_ptr(name
, c
->data
[PROP_STATIC_HOSTNAME
]))
881 return sd_bus_reply_method_return(m
, NULL
);
883 if (name
&& !hostname_is_valid(name
, 0))
884 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid static hostname '%s'", name
);
886 r
= bus_verify_polkit_async(
889 "org.freedesktop.hostname1.set-static-hostname",
898 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
900 r
= free_and_strdup_warn(&c
->data
[PROP_STATIC_HOSTNAME
], name
);
904 r
= context_write_data_static_hostname(c
);
906 log_error_errno(r
, "Failed to write static hostname: %m");
907 if (ERRNO_IS_PRIVILEGE(r
))
908 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/hostname.");
910 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/hostname is in a read-only filesystem.");
911 return sd_bus_error_set_errnof(error
, r
, "Failed to set static hostname: %m");
914 r
= context_update_kernel_hostname(c
, NULL
);
916 log_error_errno(r
, "Failed to set hostname: %m");
917 return sd_bus_error_set_errnof(error
, r
, "Failed to set hostname: %m");
920 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
921 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
922 "StaticHostname", "Hostname", "HostnameSource", NULL
);
924 return sd_bus_reply_method_return(m
, NULL
);
927 static int set_machine_info(Context
*c
, sd_bus_message
*m
, int prop
, sd_bus_message_handler_t cb
, sd_bus_error
*error
) {
935 r
= sd_bus_message_read(m
, "sb", &name
, &interactive
);
939 name
= empty_to_null(name
);
941 context_read_machine_info(c
);
943 if (streq_ptr(name
, c
->data
[prop
]))
944 return sd_bus_reply_method_return(m
, NULL
);
946 if (!isempty(name
)) {
947 /* The icon name might ultimately be used as file
948 * name, so better be safe than sorry */
950 if (prop
== PROP_ICON_NAME
&& !filename_is_valid(name
))
951 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid icon name '%s'", name
);
952 if (prop
== PROP_PRETTY_HOSTNAME
&& string_has_cc(name
, NULL
))
953 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid pretty hostname '%s'", name
);
954 if (prop
== PROP_CHASSIS
&& !valid_chassis(name
))
955 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid chassis '%s'", name
);
956 if (prop
== PROP_DEPLOYMENT
&& !valid_deployment(name
))
957 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid deployment '%s'", name
);
958 if (prop
== PROP_LOCATION
&& string_has_cc(name
, NULL
))
959 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid location '%s'", name
);
962 /* Since the pretty hostname should always be changed at the
963 * same time as the static one, use the same policy action for
966 r
= bus_verify_polkit_async(
969 prop
== PROP_PRETTY_HOSTNAME
? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
978 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
980 r
= free_and_strdup_warn(&c
->data
[prop
], name
);
984 r
= context_write_data_machine_info(c
);
986 log_error_errno(r
, "Failed to write machine info: %m");
987 if (ERRNO_IS_PRIVILEGE(r
))
988 return sd_bus_error_set(error
, BUS_ERROR_FILE_IS_PROTECTED
, "Not allowed to update /etc/machine-info.");
990 return sd_bus_error_set(error
, BUS_ERROR_READ_ONLY_FILESYSTEM
, "/etc/machine-info is in a read-only filesystem.");
991 return sd_bus_error_set_errnof(error
, r
, "Failed to write machine info: %m");
994 log_info("Changed %s to '%s'",
995 prop
== PROP_PRETTY_HOSTNAME
? "pretty hostname" :
996 prop
== PROP_DEPLOYMENT
? "deployment" :
997 prop
== PROP_LOCATION
? "location" :
998 prop
== PROP_CHASSIS
? "chassis" : "icon name", strna(c
->data
[prop
]));
1000 (void) sd_bus_emit_properties_changed(
1001 sd_bus_message_get_bus(m
),
1002 "/org/freedesktop/hostname1",
1003 "org.freedesktop.hostname1",
1004 prop
== PROP_PRETTY_HOSTNAME
? "PrettyHostname" :
1005 prop
== PROP_DEPLOYMENT
? "Deployment" :
1006 prop
== PROP_LOCATION
? "Location" :
1007 prop
== PROP_CHASSIS
? "Chassis" : "IconName" , NULL
);
1009 return sd_bus_reply_method_return(m
, NULL
);
1012 static int method_set_pretty_hostname(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1013 return set_machine_info(userdata
, m
, PROP_PRETTY_HOSTNAME
, method_set_pretty_hostname
, error
);
1016 static int method_set_icon_name(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1017 return set_machine_info(userdata
, m
, PROP_ICON_NAME
, method_set_icon_name
, error
);
1020 static int method_set_chassis(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1021 return set_machine_info(userdata
, m
, PROP_CHASSIS
, method_set_chassis
, error
);
1024 static int method_set_deployment(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1025 return set_machine_info(userdata
, m
, PROP_DEPLOYMENT
, method_set_deployment
, error
);
1028 static int method_set_location(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1029 return set_machine_info(userdata
, m
, PROP_LOCATION
, method_set_location
, error
);
1032 static int method_get_product_uuid(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1033 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1034 Context
*c
= userdata
;
1041 r
= sd_bus_message_read(m
, "b", &interactive
);
1045 r
= bus_verify_polkit_async(
1048 "org.freedesktop.hostname1.get-product-uuid",
1052 &c
->polkit_registry
,
1057 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1059 r
= id128_get_product(&uuid
);
1061 if (r
== -EADDRNOTAVAIL
)
1062 log_debug_errno(r
, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
1064 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
1065 "Failed to read product UUID, ignoring: %m");
1067 return sd_bus_error_set(error
, BUS_ERROR_NO_PRODUCT_UUID
,
1068 "Failed to read product UUID from firmware.");
1071 r
= sd_bus_message_new_method_return(m
, &reply
);
1075 r
= sd_bus_message_append_array(reply
, 'y', uuid
.bytes
, sizeof(uuid
.bytes
));
1079 return sd_bus_send(NULL
, reply
, NULL
);
1082 static int method_get_hardware_serial(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1083 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1084 _cleanup_free_
char *serial
= NULL
;
1085 Context
*c
= userdata
;
1091 r
= bus_verify_polkit_async(
1094 "org.freedesktop.hostname1.get-hardware-serial",
1098 &c
->polkit_registry
,
1103 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1105 r
= get_hardware_serial(&serial
);
1109 r
= sd_bus_message_new_method_return(m
, &reply
);
1113 r
= sd_bus_message_append(reply
, "s", serial
);
1117 return sd_bus_send(NULL
, reply
, NULL
);
1120 static int method_describe(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1121 _cleanup_free_
char *hn
= NULL
, *dhn
= NULL
, *in
= NULL
, *text
= NULL
,
1122 *chassis
= NULL
, *vendor
= NULL
, *model
= NULL
, *serial
= NULL
;
1123 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1124 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1125 sd_id128_t product_uuid
= SD_ID128_NULL
;
1126 Context
*c
= userdata
;
1134 r
= bus_verify_polkit_async(
1137 "org.freedesktop.hostname1.get-description",
1141 &c
->polkit_registry
,
1144 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1146 /* We ignore all authentication errors here, since most data is unprivileged, the one exception being
1147 * the product ID which we'll check explicitly. */
1150 context_read_etc_hostname(c
);
1151 context_read_machine_info(c
);
1152 context_read_os_release(c
);
1153 context_determine_hostname_source(c
);
1155 r
= gethostname_strict(&hn
);
1158 return log_error_errno(r
, "Failed to read local host name: %m");
1160 hn
= get_default_hostname();
1165 dhn
= get_default_hostname();
1169 if (isempty(c
->data
[PROP_ICON_NAME
]))
1170 in
= context_fallback_icon_name(c
);
1172 chassis
= context_get_chassis(c
);
1174 assert_se(uname(&u
) >= 0);
1176 if (isempty(c
->data
[PROP_VENDOR
]))
1177 (void) get_hardware_vendor(&vendor
);
1178 if (isempty(c
->data
[PROP_MODEL
]))
1179 (void) get_hardware_model(&model
);
1182 /* The product UUID and hardware serial is only available to privileged clients */
1183 (void) id128_get_product(&product_uuid
);
1184 (void) get_hardware_serial(&serial
);
1187 r
= json_build(&v
, JSON_BUILD_OBJECT(
1188 JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn
)),
1189 JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c
->data
[PROP_STATIC_HOSTNAME
])),
1190 JSON_BUILD_PAIR("PrettyHostname", JSON_BUILD_STRING(c
->data
[PROP_PRETTY_HOSTNAME
])),
1191 JSON_BUILD_PAIR("DefaultHostname", JSON_BUILD_STRING(dhn
)),
1192 JSON_BUILD_PAIR("HostnameSource", JSON_BUILD_STRING(hostname_source_to_string(c
->hostname_source
))),
1193 JSON_BUILD_PAIR("IconName", JSON_BUILD_STRING(in
?: c
->data
[PROP_ICON_NAME
])),
1194 JSON_BUILD_PAIR("Chassis", JSON_BUILD_STRING(chassis
)),
1195 JSON_BUILD_PAIR("Deployment", JSON_BUILD_STRING(c
->data
[PROP_DEPLOYMENT
])),
1196 JSON_BUILD_PAIR("Location", JSON_BUILD_STRING(c
->data
[PROP_LOCATION
])),
1197 JSON_BUILD_PAIR("KernelName", JSON_BUILD_STRING(u
.sysname
)),
1198 JSON_BUILD_PAIR("KernelRelease", JSON_BUILD_STRING(u
.release
)),
1199 JSON_BUILD_PAIR("KernelVersion", JSON_BUILD_STRING(u
.version
)),
1200 JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c
->data
[PROP_OS_PRETTY_NAME
])),
1201 JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c
->data
[PROP_OS_CPE_NAME
])),
1202 JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c
->data
[PROP_OS_HOME_URL
])),
1203 JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor
?: c
->data
[PROP_VENDOR
])),
1204 JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model
?: c
->data
[PROP_MODEL
])),
1205 JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial
)),
1206 JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_ID128(product_uuid
)),
1207 JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid
), "ProductUUID", JSON_BUILD_NULL
)));
1210 return log_error_errno(r
, "Failed to build JSON data: %m");
1212 r
= json_variant_format(v
, 0, &text
);
1214 return log_error_errno(r
, "Failed to format JSON data: %m");
1216 r
= sd_bus_message_new_method_return(m
, &reply
);
1220 r
= sd_bus_message_append(reply
, "s", text
);
1224 return sd_bus_send(NULL
, reply
, NULL
);
1227 static const sd_bus_vtable hostname_vtable
[] = {
1228 SD_BUS_VTABLE_START(0),
1229 SD_BUS_PROPERTY("Hostname", "s", property_get_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1230 SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1231 SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_PRETTY_HOSTNAME
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1232 SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1233 SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1234 SD_BUS_PROPERTY("IconName", "s", property_get_icon_name
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1235 SD_BUS_PROPERTY("Chassis", "s", property_get_chassis
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1236 SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_DEPLOYMENT
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1237 SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_LOCATION
, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
1238 SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field
, offsetof(struct utsname
, sysname
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1239 SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field
, offsetof(struct utsname
, release
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1240 SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field
, offsetof(struct utsname
, version
), SD_BUS_VTABLE_ABSOLUTE_OFFSET
|SD_BUS_VTABLE_PROPERTY_CONST
),
1241 SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_PRETTY_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1242 SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_CPE_NAME
, SD_BUS_VTABLE_PROPERTY_CONST
),
1243 SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field
, offsetof(Context
, data
) + sizeof(char*) * PROP_OS_HOME_URL
, SD_BUS_VTABLE_PROPERTY_CONST
),
1244 SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1245 SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1247 SD_BUS_METHOD_WITH_NAMES("SetHostname",
1249 SD_BUS_PARAM(hostname
)
1250 SD_BUS_PARAM(interactive
),
1252 method_set_hostname
,
1253 SD_BUS_VTABLE_UNPRIVILEGED
),
1254 SD_BUS_METHOD_WITH_NAMES("SetStaticHostname",
1256 SD_BUS_PARAM(hostname
)
1257 SD_BUS_PARAM(interactive
),
1259 method_set_static_hostname
,
1260 SD_BUS_VTABLE_UNPRIVILEGED
),
1261 SD_BUS_METHOD_WITH_NAMES("SetPrettyHostname",
1263 SD_BUS_PARAM(hostname
)
1264 SD_BUS_PARAM(interactive
),
1266 method_set_pretty_hostname
,
1267 SD_BUS_VTABLE_UNPRIVILEGED
),
1268 SD_BUS_METHOD_WITH_NAMES("SetIconName",
1271 SD_BUS_PARAM(interactive
),
1273 method_set_icon_name
,
1274 SD_BUS_VTABLE_UNPRIVILEGED
),
1275 SD_BUS_METHOD_WITH_NAMES("SetChassis",
1277 SD_BUS_PARAM(chassis
)
1278 SD_BUS_PARAM(interactive
),
1281 SD_BUS_VTABLE_UNPRIVILEGED
),
1282 SD_BUS_METHOD_WITH_NAMES("SetDeployment",
1284 SD_BUS_PARAM(deployment
)
1285 SD_BUS_PARAM(interactive
),
1287 method_set_deployment
,
1288 SD_BUS_VTABLE_UNPRIVILEGED
),
1289 SD_BUS_METHOD_WITH_NAMES("SetLocation",
1291 SD_BUS_PARAM(location
)
1292 SD_BUS_PARAM(interactive
),
1294 method_set_location
,
1295 SD_BUS_VTABLE_UNPRIVILEGED
),
1296 SD_BUS_METHOD_WITH_NAMES("GetProductUUID",
1298 SD_BUS_PARAM(interactive
),
1301 method_get_product_uuid
,
1302 SD_BUS_VTABLE_UNPRIVILEGED
),
1303 SD_BUS_METHOD_WITH_NAMES("GetHardwareSerial",
1306 SD_BUS_PARAM(serial
),
1307 method_get_hardware_serial
,
1308 SD_BUS_VTABLE_UNPRIVILEGED
),
1309 SD_BUS_METHOD_WITH_ARGS("Describe",
1311 SD_BUS_RESULT("s", json
),
1313 SD_BUS_VTABLE_UNPRIVILEGED
),
1318 static const BusObjectImplementation manager_object
= {
1319 "/org/freedesktop/hostname1",
1320 "org.freedesktop.hostname1",
1321 .vtables
= BUS_VTABLES(hostname_vtable
),
1324 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**ret
) {
1325 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1332 r
= sd_bus_default_system(&bus
);
1334 return log_error_errno(r
, "Failed to get system bus connection: %m");
1336 r
= bus_add_implementation(bus
, &manager_object
, c
);
1340 r
= bus_log_control_api_register(bus
);
1344 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.hostname1", 0, NULL
, NULL
);
1346 return log_error_errno(r
, "Failed to request name: %m");
1348 r
= sd_bus_attach_event(bus
, event
, 0);
1350 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1352 *ret
= TAKE_PTR(bus
);
1356 static int run(int argc
, char *argv
[]) {
1357 _cleanup_(context_destroy
) Context context
= {
1358 .hostname_source
= _HOSTNAME_INVALID
, /* appropriate value will be set later */
1360 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
1361 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1366 r
= service_parse_argv("systemd-hostnamed.service",
1367 "Manage the system hostname and related metadata.",
1368 BUS_IMPLEMENTATIONS(&manager_object
,
1369 &log_control_object
),
1376 r
= mac_selinux_init();
1380 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
1382 r
= sd_event_default(&event
);
1384 return log_error_errno(r
, "Failed to allocate event loop: %m");
1386 (void) sd_event_set_watchdog(event
, true);
1388 r
= sd_event_add_signal(event
, NULL
, SIGINT
, NULL
, NULL
);
1390 return log_error_errno(r
, "Failed to install SIGINT handler: %m");
1392 r
= sd_event_add_signal(event
, NULL
, SIGTERM
, NULL
, NULL
);
1394 return log_error_errno(r
, "Failed to install SIGTERM handler: %m");
1396 r
= connect_bus(&context
, event
, &bus
);
1400 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
1402 return log_error_errno(r
, "Failed to run event loop: %m");
1407 DEFINE_MAIN_FUNCTION(run
);