HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/hostname/hostnamed.c
Git blob: 70adccae80d3626caa853adba05a973b4b21e9d2
Lines 1001–2000 of 2,746
return sd_bus_message_append(reply, "s", firmware_version);
}
static int property_get_firmware_vendor(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *firmware_vendor = NULL;
Context *c = ASSERT_PTR(userdata);
(void) get_firmware_vendor(c, &firmware_vendor);
return sd_bus_message_append(reply, "s", firmware_vendor);
}
static int property_get_firmware_date(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
usec_t firmware_date = USEC_INFINITY;
Context *c = ASSERT_PTR(userdata);
(void) get_firmware_date(c, &firmware_date);
return sd_bus_message_append(reply, "t", firmware_date);
}
static int property_get_chassis_asset_tag(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *chassis_asset_tag = NULL;
Context *c = ASSERT_PTR(userdata);
(void) get_chassis_asset_tag(c, &chassis_asset_tag);
return sd_bus_message_append(reply, "s", chassis_asset_tag);
}
static int property_get_hostname(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *hn = NULL;
int r;
r = gethostname_strict(&hn);
if (r < 0) {
if (r != -ENXIO)
return r;
hn = get_default_hostname_or_fallback();
if (!hn)
return -ENOMEM;
}
return sd_bus_message_append(reply, "s", hn);
}
static int property_get_static_hostname(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Context *c = ASSERT_PTR(userdata);
context_read_etc_hostname(c);
return sd_bus_message_append(reply, "s", c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS]);
}
static int property_get_default_hostname(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *hn = NULL;
hn = get_default_hostname_or_fallback();
if (!hn)
return log_oom();
return sd_bus_message_append(reply, "s", hn);
}
static void context_determine_hostname_source(Context *c) {
_cleanup_free_ char *hostname = NULL;
int r;
assert(c);
if (c->hostname_source >= 0)
return;
(void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST, &hostname);
if (streq_ptr(hostname, c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS])) {
c->hostname_source = HOSTNAME_STATIC;
return;
}
r = hostname_matches_default(hostname);
if (r < 0)
log_warning_errno(r, "Failed to check whether hostname is the recorded default, assuming not: %m");
c->hostname_source = r > 0 ? HOSTNAME_DEFAULT : HOSTNAME_TRANSIENT;
}
static int property_get_hostname_source(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Context *c = ASSERT_PTR(userdata);
context_read_etc_hostname(c);
context_determine_hostname_source(c);
return sd_bus_message_append(reply, "s", hostname_source_to_string(c->hostname_source));
}
static int property_get_machine_info_field(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
sd_bus_slot *slot;
Context *c;
/* Acquire the context object without this property's userdata offset added. Explanation: we want
* access to two pointers here: a) the main context object we cache all properties in, and b) the
* pointer to the property field inside the context object that we are supposed to update and
* use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us
* from the 'userdata' pointer we supplied when the vtable was registered, with the offset we
* specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from
* the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot
* object is (which encapsulates the vtable registration), and then query the 'userdata' field
* directly off it. */
assert_se(slot = sd_bus_get_current_slot(bus));
assert_se(c = sd_bus_slot_get_userdata(slot));
context_read_machine_info(c);
return sd_bus_message_append(reply, "s", *(char**) userdata);
}
static int property_get_tags(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Context *c = ASSERT_PTR(userdata);
int r;
context_read_machine_info(c);
/* Silently drop any invalid tags that might have been written into the file by hand */
_cleanup_strv_free_ char **l = NULL;
r = machine_tags_from_string(c->data[PROP_TAGS], /* graceful= */ true, &l);
if (r < 0)
log_warning_errno(r, "Failed to parse machine tags '%s', ignoring: %m", strnull(c->data[PROP_TAGS]));
return sd_bus_message_append_strv(reply, l);
}
static int property_get_os_release_field(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
sd_bus_slot *slot;
Context *c;
/* As above, acquire the current context without this property's userdata offset added. */
assert_se(slot = sd_bus_get_current_slot(bus));
assert_se(c = sd_bus_slot_get_userdata(slot));
context_read_os_release(c);
return sd_bus_message_append(reply, "s", *(char**) userdata);
}
static int property_get_os_support_end(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
Context *c = userdata;
usec_t eol = USEC_INFINITY;
context_read_os_release(c);
if (c->data[PROP_OS_SUPPORT_END])
(void) os_release_support_ended(c->data[PROP_OS_SUPPORT_END], /* quiet= */ false, &eol);
return sd_bus_message_append(reply, "t", eol);
}
static int property_get_icon_name(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *n = NULL;
Context *c = userdata;
const char *name;
context_read_machine_info(c);
if (isempty(c->data[PROP_ICON_NAME]))
name = n = context_fallback_icon_name(c);
else
name = c->data[PROP_ICON_NAME];
if (!name)
return -ENOMEM;
return sd_bus_message_append(reply, "s", name);
}
static int property_get_chassis(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_free_ char *chassis = NULL;
Context *c = userdata;
context_read_machine_info(c);
chassis = context_get_chassis(c);
return sd_bus_message_append(reply, "s", chassis);
}
static int property_get_uname_field(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
struct utsname u;
assert_se(uname(&u) >= 0);
return sd_bus_message_append(reply, "s", (char*) &u + PTR_TO_SIZE(userdata));
}
static int property_get_machine_id(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
sd_id128_t id;
int r;
r = sd_id128_get_machine(&id);
if (r < 0)
return r;
return bus_property_get_id128(bus, path, interface, property, reply, &id, error);
}
static int property_get_boot_id(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
sd_id128_t id;
int r;
r = sd_id128_get_boot(&id);
if (r < 0)
return r;
return bus_property_get_id128(bus, path, interface, property, reply, &id, error);
}
static int property_get_vsock_cid(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
unsigned local_cid = VMADDR_CID_ANY;
(void) vsock_get_local_cid(&local_cid);
return sd_bus_message_append(reply, "u", (uint32_t) local_cid);
}
static int validate_and_substitute_hostname(const char *name, char **ret_substituted) {
int r;
assert(ret_substituted);
if (!name) {
*ret_substituted = NULL;
return 0;
}
_cleanup_free_ char *substituted = NULL;
r = hostname_substitute_wildcards(name, &substituted);
if (r < 0)
return log_error_errno(r, "Failed to substitute wildcards in hostname: %m");
if (!hostname_is_valid(substituted, 0))
return -EUCLEAN;
*ret_substituted = TAKE_PTR(substituted);
return 1;
}
static int bus_validate_and_substitute_hostname(const char *name, char **ret_substituted, sd_bus_error *error) {
int r;
assert(ret_substituted);
r = validate_and_substitute_hostname(name, ret_substituted);
if (r == -EUCLEAN)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
return r;
}
static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
Context *c = ASSERT_PTR(userdata);
const char *name;
int interactive, r;
assert(m);
r = sd_bus_message_read(m, "sb", &name, &interactive);
if (r < 0)
return r;
name = empty_to_null(name);
/* We always go through with the procedure below without comparing to the current hostname, because
* we might want to adjust hostname source information even if the actual hostname is unchanged. */
_cleanup_free_ char *substituted = NULL;
r = bus_validate_and_substitute_hostname(name, &substituted, error);
if (r < 0)
return r;
name = substituted;
r = bus_verify_polkit_async_full(
m,
"org.freedesktop.hostname1.set-hostname",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
&c->polkit_registry,
/* ret_admin= */ NULL,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
context_read_etc_hostname(c);
r = context_update_kernel_hostname(c, name);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m");
else if (r > 0)
(void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
"/org/freedesktop/hostname1", "org.freedesktop.hostname1",
"Hostname", "HostnameSource", NULL);
return sd_bus_reply_method_return(m, NULL);
}
static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
Context *c = ASSERT_PTR(userdata);
const char *name;
int interactive, r;
assert(m);
r = sd_bus_message_read(m, "sb", &name, &interactive);
if (r < 0)
return r;
name = empty_to_null(name);
context_read_etc_hostname(c);
if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
return sd_bus_reply_method_return(m, NULL);
_cleanup_free_ char *substituted = NULL;
r = bus_validate_and_substitute_hostname(name, &substituted, error);
if (r < 0)
return r;
r = bus_verify_polkit_async_full(
m,
"org.freedesktop.hostname1.set-static-hostname",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
&c->polkit_registry,
/* ret_admin= */ NULL,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = free_and_strdup_warn(&c->data[PROP_STATIC_HOSTNAME], name);
if (r < 0)
return r;
free_and_replace(c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS], substituted);
r = context_write_data_static_hostname(c);
if (r < 0) {
if (ERRNO_IS_PRIVILEGE(r))
return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/hostname.");
if (r == -EROFS)
return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/hostname is in a read-only filesystem.");
return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %m");
}
r = context_update_kernel_hostname(c, /* transient_hostname= */ NULL);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m");
(void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
"/org/freedesktop/hostname1", "org.freedesktop.hostname1",
"StaticHostname", "Hostname", "HostnameSource", NULL);
return sd_bus_reply_method_return(m, NULL);
}
static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_message_handler_t cb, sd_bus_error *error) {
int interactive;
const char *name;
int r;
assert(c);
assert(m);
r = sd_bus_message_read(m, "sb", &name, &interactive);
if (r < 0)
return r;
name = empty_to_null(name);
context_read_machine_info(c);
if (streq_ptr(name, c->data[prop]))
return sd_bus_reply_method_return(m, NULL);
if (!isempty(name)) {
/* The icon name might ultimately be used as file
* name, so better be safe than sorry */
if (prop == PROP_ICON_NAME && !icon_name_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
if (prop == PROP_PRETTY_HOSTNAME && !pretty_hostname_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty hostname '%s'", name);
if (prop == PROP_CHASSIS && !chassis_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name);
if (prop == PROP_DEPLOYMENT && !deployment_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name);
if (prop == PROP_LOCATION && !location_is_valid(name))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name);
}
/* Since the pretty hostname should always be changed at the same time as the static one, use the
* same policy action for both... */
r = bus_verify_polkit_async_full(
m,
prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
&c->polkit_registry,
/* ret_admin= */ NULL,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = free_and_strdup_warn(&c->data[prop], name);
if (r < 0)
return r;
r = context_write_data_machine_info(c);
if (r < 0) {
if (ERRNO_IS_PRIVILEGE(r))
return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/machine-info.");
if (r == -EROFS)
return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/machine-info is in a read-only filesystem.");
return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %m");
}
log_info("Changed %s to '%s'",
prop == PROP_PRETTY_HOSTNAME ? "pretty hostname" :
prop == PROP_DEPLOYMENT ? "deployment" :
prop == PROP_LOCATION ? "location" :
prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop]));
(void) sd_bus_emit_properties_changed(
sd_bus_message_get_bus(m),
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" :
prop == PROP_DEPLOYMENT ? "Deployment" :
prop == PROP_LOCATION ? "Location" :
prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL);
return sd_bus_reply_method_return(m, NULL);
}
static int method_set_pretty_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
return set_machine_info(userdata, m, PROP_PRETTY_HOSTNAME, method_set_pretty_hostname, error);
}
static int method_set_icon_name(sd_bus_message *m, void *userdata, sd_bus_error *error) {
return set_machine_info(userdata, m, PROP_ICON_NAME, method_set_icon_name, error);
}
static int method_set_chassis(sd_bus_message *m, void *userdata, sd_bus_error *error) {
return set_machine_info(userdata, m, PROP_CHASSIS, method_set_chassis, error);
}
static int method_set_deployment(sd_bus_message *m, void *userdata, sd_bus_error *error) {
return set_machine_info(userdata, m, PROP_DEPLOYMENT, method_set_deployment, error);
}
static int method_set_location(sd_bus_message *m, void *userdata, sd_bus_error *error) {
return set_machine_info(userdata, m, PROP_LOCATION, method_set_location, error);
}
static int context_store_tags(Context *c, char **tags) {
int r;
assert(c);
/* Persists the given machine tags (which must already be validated, sorted and deduplicated) to
* /etc/machine-info and emits a PropertiesChanged signal on the Tags property. */
if (strv_isempty(tags))
c->data[PROP_TAGS] = mfree(c->data[PROP_TAGS]);
else {
_cleanup_free_ char *j = strv_join(tags, ":");
if (!j)
return log_oom();
free_and_replace(c->data[PROP_TAGS], j);
}
r = context_write_data_machine_info(c);
if (r < 0)
return r;
log_info("Changed tags to '%s'", strempty(c->data[PROP_TAGS]));
(void) sd_bus_emit_properties_changed(
c->bus,
"/org/freedesktop/hostname1",
"org.freedesktop.hostname1",
"Tags",
NULL);
return 0;
}
static int bus_error_from_tags_write(sd_bus_error *error, int r) {
assert(error);
assert(r < 0);
log_error_errno(r, "Failed to write machine info: %m");
if (ERRNO_IS_PRIVILEGE(r))
return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/machine-info.");
if (r == -EROFS)
return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/machine-info is in a read-only filesystem.");
return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %m");
}
static int method_set_tags(sd_bus_message *m, void *userdata, sd_bus_error *error) {
Context *c = ASSERT_PTR(userdata);
int r;
assert(m);
_cleanup_strv_free_ char **tags = NULL;
r = sd_bus_message_read_strv(m, &tags);
if (r < 0)
return r;
strv_sort_uniq(tags);
if (strv_length(tags) > MACHINE_TAGS_MAX)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Too many machine tags specified.");
_cleanup_free_ char *j = strv_join(tags, ":");
if (!j)
return log_oom();
if (!machine_tag_list_is_valid(tags))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid tags '%s'", j);
context_read_machine_info(c);
_cleanup_strv_free_ char **current = NULL;
r = machine_tags_from_string(c->data[PROP_TAGS], /* graceful= */ true, ¤t);
if (r < 0)
return log_error_errno(r, "Failed to parse current machine tags: %m");
if (strv_equal(current, tags))
return sd_bus_reply_method_return(m, NULL);
r = bus_verify_polkit_async_full(
m,
"org.freedesktop.hostname1.set-machine-info",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
/* flags= */ 0,
&c->polkit_registry,
/* ret_admin= */ NULL,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = context_store_tags(c, tags);
if (r < 0)
return bus_error_from_tags_write(error, r);
return sd_bus_reply_method_return(m, NULL);
}
static int machine_tags_add_remove(char * const *base, char * const *add, char * const *remove, char ***ret) {
int r;
assert(ret);
/* Computes the resulting machine tag list when adding 'add' to and removing 'remove' from the 'base'
* list, i.e. (base ∪ add) \ remove, sorted and deduplicated. Shared by the D-Bus AddAndRemoveTags()
* and Varlink SetTags() implementations. */
_cleanup_strv_free_ char **tags = strv_copy(base);
if (!tags)
return -ENOMEM;
r = strv_extend_strv(&tags, add, /* filter_duplicates= */ true);
if (r < 0)
return r;
strv_remove_strv(tags, remove);
strv_sort_uniq(tags);
if (strv_length(tags) > MACHINE_TAGS_MAX)
return -E2BIG;
*ret = TAKE_PTR(tags);
return 0;
}
static int method_add_and_remove_tags(sd_bus_message *m, void *userdata, sd_bus_error *error) {
Context *c = ASSERT_PTR(userdata);
int r;
assert(m);
_cleanup_strv_free_ char **add = NULL, **remove = NULL;
r = sd_bus_message_read_strv(m, &add);
if (r < 0)
return r;
r = sd_bus_message_read_strv(m, &remove);
if (r < 0)
return r;
if (!machine_tag_list_is_valid(add)) {
_cleanup_free_ char *j = strv_join(add, ":");
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid tags to add '%s'", strna(j));
}
if (!machine_tag_list_is_valid(remove)) {
_cleanup_free_ char *j = strv_join(remove, ":");
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid tags to remove '%s'", strna(j));
}
context_read_machine_info(c);
/* Start from the current tags, add the requested ones, then drop the ones to be removed. */
_cleanup_strv_free_ char **current = NULL;
r = machine_tags_from_string(c->data[PROP_TAGS], /* graceful= */ true, ¤t);
if (r < 0)
return log_error_errno(r, "Failed to parse current machine tags: %m");
_cleanup_strv_free_ char **tags = NULL;
r = machine_tags_add_remove(current, add, remove, &tags);
if (r == -E2BIG)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Too many machine tags specified.");
if (r < 0)
return log_oom();
if (strv_equal(current, tags))
return sd_bus_reply_method_return(m, NULL);
r = bus_verify_polkit_async_full(
m,
"org.freedesktop.hostname1.set-machine-info",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
/* flags= */ 0,
&c->polkit_registry,
/* ret_admin= */ NULL,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = context_store_tags(c, tags);
if (r < 0)
return bus_error_from_tags_write(error, r);
return sd_bus_reply_method_return(m, NULL);
}
static int method_get_product_uuid(sd_bus_message *m, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
Context *c = ASSERT_PTR(userdata);
int interactive, r;
sd_id128_t uuid;
assert(m);
r = sd_bus_message_read(m, "b", &interactive);
if (r < 0)
return r;
r = bus_verify_polkit_async_full(
m,
"org.freedesktop.hostname1.get-product-uuid",
/* details= */ NULL,
/* good_user= */ UID_INVALID,
interactive ? POLKIT_ALLOW_INTERACTIVE : 0,
&c->polkit_registry,
/* ret_admin= */ NULL,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = id128_get_product(&uuid);
if (r < 0) {
if (r == -EADDRNOTAVAIL)
log_debug_errno(r, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
else
log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
"Failed to read product UUID, ignoring: %m");
return sd_bus_error_set(error, BUS_ERROR_NO_PRODUCT_UUID,
"Failed to read product UUID from firmware.");
}
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
r = sd_bus_message_append_array(reply, 'y', uuid.bytes, sizeof(uuid.bytes));
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static int method_get_hardware_serial(sd_bus_message *m, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *serial = NULL;
Context *c = ASSERT_PTR(userdata);
int r;
assert(m);
r = bus_verify_polkit_async(
m,
"org.freedesktop.hostname1.get-hardware-serial",
/* details= */ NULL,
&c->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
r = get_hardware_serial(c, &serial);
if (r < 0)
return sd_bus_error_set(error, BUS_ERROR_NO_HARDWARE_SERIAL,
"Failed to read hardware serial from firmware.");
return sd_bus_reply_method_return(m, "s", serial);
}
static int method_get_machine_info(sd_bus_message *m, void *userdata, sd_bus_error *error) {
static const struct {
const char *name;
HostProperty prop;
} field_table[] = {
{ "PRETTY_HOSTNAME", PROP_PRETTY_HOSTNAME },
{ "ICON_NAME", PROP_ICON_NAME },
{ "CHASSIS", PROP_CHASSIS },
{ "DEPLOYMENT", PROP_DEPLOYMENT },
{ "LOCATION", PROP_LOCATION },
{ "TAGS", PROP_TAGS },
{ "HARDWARE_VENDOR", PROP_HARDWARE_VENDOR },
{ "HARDWARE_MODEL", PROP_HARDWARE_MODEL },
{ "HARDWARE_SKU", PROP_HARDWARE_SKU },
{ "HARDWARE_VERSION", PROP_HARDWARE_VERSION },
};
Context *c = ASSERT_PTR(userdata);
const char *field;
int r;
assert(m);
r = sd_bus_message_read(m, "s", &field);
if (r < 0)
return r;
if (isempty(field))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Field name must not be empty.");
if (!env_name_is_valid(field))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid field name '%s'.", field);
FOREACH_ELEMENT(e, field_table)
if (streq(field, e->name)) {
/* For fields that are also exposed as D-Bus properties, use the same Context cache as the
* property getters. Note that this returns the raw /etc/machine-info value only: property-level
* fallback logic (e.g. DMI/chassis-based synthesis) is not applied here. For custom/unknown
* fields, fall back to reading the file directly. */
context_read_machine_info(c);
if (isempty(c->data[e->prop]))
return sd_bus_error_setf(error, BUS_ERROR_FIELD_NOT_SET, "Field '%s' is not set or empty in /etc/machine-info.", field);
return sd_bus_reply_method_return(m, "s", c->data[e->prop]);
}
_cleanup_free_ char *value = NULL;
r = parse_env_file(NULL, etc_machine_info(),
field, &value);
if (r < 0 && r != -ENOENT)
return sd_bus_error_set_errnof(error, r, "Failed to read /etc/machine-info: %m");
if (isempty(value))
return sd_bus_error_setf(error, BUS_ERROR_FIELD_NOT_SET, "Field '%s' is not set or empty in /etc/machine-info.", field);
return sd_bus_reply_method_return(m, "s", value);
}
static int build_describe_response(Context *c, bool privileged, sd_json_variant **ret) {
_cleanup_free_ char *hn = NULL, *dhn = NULL, *in = NULL,
*chassis = NULL, *vendor = NULL, *model = NULL, *serial = NULL, *firmware_version = NULL,
*firmware_vendor = NULL, *chassis_asset_tag = NULL, *sku = NULL, *hardware_version = NULL;
_cleanup_strv_free_ char **os_release_pairs = NULL, **machine_info_pairs = NULL;
usec_t firmware_date = USEC_INFINITY, eol = USEC_INFINITY;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
sd_id128_t machine_id, boot_id, product_uuid = SD_ID128_NULL;
unsigned local_cid = VMADDR_CID_ANY;
struct utsname u;
int r;
assert(c);
assert(ret);
context_read_etc_hostname(c);
context_read_machine_info(c);
context_read_os_release(c);
context_determine_hostname_source(c);
dhn = get_default_hostname_or_fallback();
if (!dhn)
return log_oom();
r = gethostname_strict(&hn);
if (r < 0 && r != -ENXIO)
return log_error_errno(r, "Failed to read local host name: %m");
if (isempty(c->data[PROP_ICON_NAME]))
in = context_fallback_icon_name(c);
chassis = context_get_chassis(c);
assert_se(uname(&u) >= 0);
if (isempty(c->data[PROP_HARDWARE_VENDOR]))
(void) get_hardware_vendor(c, &vendor);
if (isempty(c->data[PROP_HARDWARE_MODEL]))
(void) get_hardware_model(c, &model);
if (isempty(c->data[PROP_HARDWARE_SKU]))
(void) get_hardware_sku(c, &sku);
if (isempty(c->data[PROP_HARDWARE_VERSION]))
(void) get_hardware_version(c, &hardware_version);
if (privileged) {
/* The product UUID and hardware serial is only available to privileged clients */
(void) id128_get_product(&product_uuid);
(void) get_hardware_serial(c, &serial);
}
(void) get_firmware_version(c, &firmware_version);
(void) get_firmware_vendor(c, &firmware_vendor);
(void) get_firmware_date(c, &firmware_date);
(void) get_chassis_asset_tag(c, &chassis_asset_tag);
if (c->data[PROP_OS_SUPPORT_END])
(void) os_release_support_ended(c->data[PROP_OS_SUPPORT_END], /* quiet= */ false, &eol);
r = sd_id128_get_machine(&machine_id);
if (r < 0)
return log_error_errno(r, "Failed to get machine ID: %m");
r = sd_id128_get_boot(&boot_id);
if (r < 0)