| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/stat.h> |
| 4 |
#include <sys/utsname.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-bus.h" |
| 8 |
#include "sd-device.h" |
| 9 |
#include "sd-event.h" |
| 10 |
#include "sd-json.h" |
| 11 |
|
| 12 |
#include "alloc-util.h" |
| 13 |
#include "bitfield.h" |
| 14 |
#include "bus-common-errors.h" |
| 15 |
#include "bus-get-properties.h" |
| 16 |
#include "bus-log-control-api.h" |
| 17 |
#include "bus-object.h" |
| 18 |
#include "bus-polkit.h" |
| 19 |
#include "bus-util.h" |
| 20 |
#include "constants.h" |
| 21 |
#include "daemon-util.h" |
| 22 |
#include "device-private.h" |
| 23 |
#include "device-util.h" |
| 24 |
#include "env-file.h" |
| 25 |
#include "env-util.h" |
| 26 |
#include "extract-word.h" |
| 27 |
#include "fileio.h" |
| 28 |
#include "hashmap.h" |
| 29 |
#include "hostname-setup.h" |
| 30 |
#include "hostname-util.h" |
| 31 |
#include "id128-util.h" |
| 32 |
#include "json-util.h" |
| 33 |
#include "label-util.h" |
| 34 |
#include "log.h" |
| 35 |
#include "main-func.h" |
| 36 |
#include "nulstr-util.h" |
| 37 |
#include "os-util.h" |
| 38 |
#include "parse-util.h" |
| 39 |
#include "path-util.h" |
| 40 |
#include "service-util.h" |
| 41 |
#include "socket-util.h" |
| 42 |
#include "stat-util.h" |
| 43 |
#include "string-util.h" |
| 44 |
#include "strv.h" |
| 45 |
#include "time-util.h" |
| 46 |
#include "varlink-io.systemd.Hostname.h" |
| 47 |
#include "varlink-io.systemd.service.h" |
| 48 |
#include "varlink-util.h" |
| 49 |
#include "virt.h" |
| 50 |
|
| 51 |
#define VALID_DEPLOYMENT_CHARS (ALPHANUMERICAL "-.:") |
| 52 |
|
| 53 |
/* Properties we cache are indexed by an enum, to make invalidation easy and systematic (as we can iterate |
| 54 |
* through them all, and they are uniformly strings). */ |
| 55 |
typedef enum { |
| 56 |
/* Read from /etc/hostname */ |
| 57 |
PROP_STATIC_HOSTNAME, |
| 58 |
PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS, |
| 59 |
|
| 60 |
/* Read from /etc/machine-info (with fallbacks) */ |
| 61 |
PROP_PRETTY_HOSTNAME, |
| 62 |
_PROP_MACHINE_INFO_SETTABLE_FIRST = PROP_PRETTY_HOSTNAME, |
| 63 |
PROP_ICON_NAME, |
| 64 |
PROP_CHASSIS, |
| 65 |
PROP_DEPLOYMENT, |
| 66 |
PROP_LOCATION, |
| 67 |
PROP_TAGS, |
| 68 |
_PROP_MACHINE_INFO_SETTABLE_LAST = PROP_TAGS, |
| 69 |
PROP_HARDWARE_VENDOR, |
| 70 |
PROP_HARDWARE_MODEL, |
| 71 |
PROP_HARDWARE_SKU, |
| 72 |
PROP_HARDWARE_VERSION, |
| 73 |
|
| 74 |
/* Read from /etc/os-release (or /usr/lib/os-release) */ |
| 75 |
PROP_OS_PRETTY_NAME, |
| 76 |
PROP_OS_FANCY_NAME, |
| 77 |
PROP_OS_CPE_NAME, |
| 78 |
PROP_OS_HOME_URL, |
| 79 |
PROP_OS_SUPPORT_END, |
| 80 |
PROP_OS_IMAGE_ID, |
| 81 |
PROP_OS_IMAGE_VERSION, |
| 82 |
|
| 83 |
_PROP_MAX, |
| 84 |
_PROP_INVALID = -EINVAL, |
| 85 |
} HostProperty; |
| 86 |
|
| 87 |
typedef struct Context { |
| 88 |
char *data[_PROP_MAX]; |
| 89 |
|
| 90 |
HostnameSource hostname_source; |
| 91 |
|
| 92 |
struct stat etc_hostname_stat; |
| 93 |
struct stat etc_os_release_stat; |
| 94 |
struct stat etc_machine_info_stat; |
| 95 |
|
| 96 |
sd_event *event; |
| 97 |
sd_bus *bus; |
| 98 |
sd_varlink_server *varlink_server; |
| 99 |
Hashmap *polkit_registry; |
| 100 |
sd_device *device_dmi; |
| 101 |
sd_device *device_acpi; |
| 102 |
sd_device *device_tree; |
| 103 |
} Context; |
| 104 |
|
| 105 |
static void context_reset(Context *c, uint64_t mask) { |
| 106 |
assert(c); |
| 107 |
|
| 108 |
for (HostProperty p = 0; p < _PROP_MAX; p++) { |
| 109 |
if (!BIT_SET(mask, p)) |
| 110 |
continue; |
| 111 |
|
| 112 |
c->data[p] = mfree(c->data[p]); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#define context_reset_many(c, ...) \ |
| 117 |
context_reset((c), INDEXES_TO_MASK(uint64_t, __VA_ARGS__)) |
| 118 |
|
| 119 |
static void context_destroy(Context *c) { |
| 120 |
assert(c); |
| 121 |
|
| 122 |
context_reset(c, UINT64_MAX); |
| 123 |
hashmap_free(c->polkit_registry); |
| 124 |
sd_event_unref(c->event); |
| 125 |
sd_bus_flush_close_unref(c->bus); |
| 126 |
sd_varlink_server_unref(c->varlink_server); |
| 127 |
sd_device_unref(c->device_dmi); |
| 128 |
sd_device_unref(c->device_acpi); |
| 129 |
sd_device_unref(c->device_tree); |
| 130 |
} |
| 131 |
|
| 132 |
static void context_read_etc_hostname(Context *c) { |
| 133 |
struct stat current_stat = {}; |
| 134 |
int r; |
| 135 |
|
| 136 |
assert(c); |
| 137 |
|
| 138 |
if (stat(etc_hostname(), ¤t_stat) >= 0 && |
| 139 |
stat_inode_unmodified(&c->etc_hostname_stat, ¤t_stat)) |
| 140 |
return; |
| 141 |
|
| 142 |
context_reset_many(c, PROP_STATIC_HOSTNAME, |
| 143 |
PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS); |
| 144 |
|
| 145 |
r = read_etc_hostname(/* path= */ NULL, /* substitute_wildcards= */ false, &c->data[PROP_STATIC_HOSTNAME]); |
| 146 |
if (r < 0) { |
| 147 |
if (r != -ENOENT) |
| 148 |
log_warning_errno(r, "Failed to read /etc/hostname, ignoring: %m"); |
| 149 |
} else { |
| 150 |
_cleanup_free_ char *substituted = strdup(c->data[PROP_STATIC_HOSTNAME]); |
| 151 |
if (!substituted) |
| 152 |
return (void) log_oom(); |
| 153 |
|
| 154 |
r = hostname_substitute_wildcards(substituted); |
| 155 |
if (r < 0) |
| 156 |
log_warning_errno(r, "Failed to substitute wildcards in /etc/hostname, ignoring: %m"); |
| 157 |
else |
| 158 |
c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS] = TAKE_PTR(substituted); |
| 159 |
} |
| 160 |
|
| 161 |
c->etc_hostname_stat = current_stat; |
| 162 |
} |
| 163 |
|
| 164 |
static void context_read_machine_info(Context *c) { |
| 165 |
struct stat current_stat = {}; |
| 166 |
int r; |
| 167 |
|
| 168 |
assert(c); |
| 169 |
|
| 170 |
if (stat(etc_machine_info(), ¤t_stat) >= 0 && |
| 171 |
stat_inode_unmodified(&c->etc_machine_info_stat, ¤t_stat)) |
| 172 |
return; |
| 173 |
|
| 174 |
context_reset_many(c, PROP_PRETTY_HOSTNAME, |
| 175 |
PROP_ICON_NAME, |
| 176 |
PROP_CHASSIS, |
| 177 |
PROP_DEPLOYMENT, |
| 178 |
PROP_LOCATION, |
| 179 |
PROP_TAGS, |
| 180 |
PROP_HARDWARE_VENDOR, |
| 181 |
PROP_HARDWARE_MODEL, |
| 182 |
PROP_HARDWARE_SKU, |
| 183 |
PROP_HARDWARE_VERSION); |
| 184 |
|
| 185 |
r = parse_env_file(NULL, etc_machine_info(), |
| 186 |
"PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME], |
| 187 |
"ICON_NAME", &c->data[PROP_ICON_NAME], |
| 188 |
"CHASSIS", &c->data[PROP_CHASSIS], |
| 189 |
"DEPLOYMENT", &c->data[PROP_DEPLOYMENT], |
| 190 |
"LOCATION", &c->data[PROP_LOCATION], |
| 191 |
"TAGS", &c->data[PROP_TAGS], |
| 192 |
"HARDWARE_VENDOR", &c->data[PROP_HARDWARE_VENDOR], |
| 193 |
"HARDWARE_MODEL", &c->data[PROP_HARDWARE_MODEL], |
| 194 |
"HARDWARE_SKU", &c->data[PROP_HARDWARE_SKU], |
| 195 |
"HARDWARE_VERSION", &c->data[PROP_HARDWARE_VERSION]); |
| 196 |
if (r < 0 && r != -ENOENT) |
| 197 |
log_warning_errno(r, "Failed to read /etc/machine-info, ignoring: %m"); |
| 198 |
|
| 199 |
c->etc_machine_info_stat = current_stat; |
| 200 |
} |
| 201 |
|
| 202 |
static void context_read_os_release(Context *c) { |
| 203 |
_cleanup_free_ char *os_name = NULL, *os_pretty_name = NULL, *os_fancy_name = NULL, *os_ansi_color = NULL; |
| 204 |
struct stat current_stat = {}; |
| 205 |
int r; |
| 206 |
|
| 207 |
assert(c); |
| 208 |
|
| 209 |
if ((stat("/etc/os-release", ¤t_stat) >= 0 || |
| 210 |
stat("/usr/lib/os-release", ¤t_stat) >= 0) && |
| 211 |
stat_inode_unmodified(&c->etc_os_release_stat, ¤t_stat)) |
| 212 |
return; |
| 213 |
|
| 214 |
context_reset_many(c, PROP_OS_PRETTY_NAME, |
| 215 |
PROP_OS_FANCY_NAME, |
| 216 |
PROP_OS_CPE_NAME, |
| 217 |
PROP_OS_HOME_URL, |
| 218 |
PROP_OS_SUPPORT_END, |
| 219 |
PROP_OS_IMAGE_ID, |
| 220 |
PROP_OS_IMAGE_VERSION); |
| 221 |
|
| 222 |
r = parse_os_release(NULL, |
| 223 |
"PRETTY_NAME", &os_pretty_name, |
| 224 |
"FANCY_NAME", &os_fancy_name, |
| 225 |
"ANSI_COLOR", &os_ansi_color, |
| 226 |
"NAME", &os_name, |
| 227 |
"CPE_NAME", &c->data[PROP_OS_CPE_NAME], |
| 228 |
"HOME_URL", &c->data[PROP_OS_HOME_URL], |
| 229 |
"SUPPORT_END", &c->data[PROP_OS_SUPPORT_END], |
| 230 |
"IMAGE_ID", &c->data[PROP_OS_IMAGE_ID], |
| 231 |
"IMAGE_VERSION", &c->data[PROP_OS_IMAGE_VERSION]); |
| 232 |
if (r < 0 && r != -ENOENT) |
| 233 |
log_warning_errno(r, "Failed to read os-release file, ignoring: %m"); |
| 234 |
|
| 235 |
if (free_and_strdup(&c->data[PROP_OS_PRETTY_NAME], os_release_pretty_name(os_pretty_name, os_name)) < 0) |
| 236 |
log_oom(); |
| 237 |
|
| 238 |
unescape_fancy_name(&os_fancy_name); |
| 239 |
|
| 240 |
if (isempty(os_fancy_name)) { |
| 241 |
free(os_fancy_name); /* free if empty string */ |
| 242 |
|
| 243 |
if (isempty(os_ansi_color)) |
| 244 |
os_fancy_name = strdup(c->data[PROP_OS_PRETTY_NAME]); |
| 245 |
else |
| 246 |
os_fancy_name = strjoin("\x1B[", os_ansi_color, "m", c->data[PROP_OS_PRETTY_NAME]); |
| 247 |
if (!os_fancy_name) |
| 248 |
log_oom(); |
| 249 |
} |
| 250 |
|
| 251 |
free_and_replace(c->data[PROP_OS_FANCY_NAME], os_fancy_name); |
| 252 |
|
| 253 |
c->etc_os_release_stat = current_stat; |
| 254 |
} |
| 255 |
|
| 256 |
static bool use_dmi_data(void) { |
| 257 |
int r; |
| 258 |
|
| 259 |
r = getenv_bool("SYSTEMD_HOSTNAME_FORCE_DMI"); |
| 260 |
if (r >= 0) { |
| 261 |
log_debug("Honouring $SYSTEMD_HOSTNAME_FORCE_DMI override: %s", yes_no(r)); |
| 262 |
return r; |
| 263 |
} |
| 264 |
if (r != -ENXIO) |
| 265 |
log_debug_errno(r, "Failed to parse $SYSTEMD_HOSTNAME_FORCE_DMI, ignoring: %m"); |
| 266 |
|
| 267 |
if (detect_container() > 0) { |
| 268 |
log_debug("Running in a container, not using DMI hardware data."); |
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
static int context_acquire_dmi_device(Context *c) { |
| 276 |
int r; |
| 277 |
|
| 278 |
assert(c); |
| 279 |
assert(!c->device_dmi); |
| 280 |
|
| 281 |
if (!use_dmi_data()) |
| 282 |
return 0; |
| 283 |
|
| 284 |
r = sd_device_new_from_syspath(&c->device_dmi, "/sys/class/dmi/id/"); |
| 285 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 286 |
log_debug_errno(r, "Failed to open /sys/class/dmi/id/ device, ignoring: %m"); |
| 287 |
return 0; |
| 288 |
} |
| 289 |
if (r < 0) |
| 290 |
return log_error_errno(r, "Failed to open /sys/class/dmi/id/ device: %m"); |
| 291 |
|
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
static int context_acquire_acpi_device(Context *c) { |
| 296 |
int r; |
| 297 |
|
| 298 |
assert(c); |
| 299 |
assert(!c->device_acpi); |
| 300 |
|
| 301 |
r = sd_device_new_from_syspath(&c->device_acpi, "/sys/firmware/acpi/"); |
| 302 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 303 |
log_debug_errno(r, "Failed to open /sys/firmware/acpi/ device, ignoring: %m"); |
| 304 |
return 0; |
| 305 |
} |
| 306 |
if (r < 0) |
| 307 |
return log_error_errno(r, "Failed to open /sys/firmware/acpi/ device: %m"); |
| 308 |
|
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
static int context_acquire_device_tree(Context *c) { |
| 313 |
int r; |
| 314 |
|
| 315 |
assert(c); |
| 316 |
assert(!c->device_tree); |
| 317 |
|
| 318 |
r = sd_device_new_from_path(&c->device_tree, "/proc/device-tree/"); |
| 319 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 320 |
log_debug_errno(r, "Failed to open /proc/device-tree/ device, ignoring: %m"); |
| 321 |
return 0; |
| 322 |
} |
| 323 |
if (r < 0) |
| 324 |
return log_error_errno(r, "Failed to open /proc/device-tree/ device: %m"); |
| 325 |
|
| 326 |
return 1; |
| 327 |
} |
| 328 |
|
| 329 |
static int get_dmi_property(Context *c, const char *key, char **ret) { |
| 330 |
const char *s; |
| 331 |
int r; |
| 332 |
|
| 333 |
assert(c); |
| 334 |
assert(key); |
| 335 |
assert(ret); |
| 336 |
|
| 337 |
if (!c->device_dmi) |
| 338 |
return -ENODEV; |
| 339 |
|
| 340 |
r = sd_device_get_property_value(c->device_dmi, key, &s); |
| 341 |
if (r < 0) |
| 342 |
return r; |
| 343 |
|
| 344 |
if (!string_is_safe(s, STRING_ALLOW_EMPTY|STRING_ALLOW_BACKSLASHES|STRING_ALLOW_QUOTES|STRING_ALLOW_GLOBS)) |
| 345 |
return -ENXIO; |
| 346 |
|
| 347 |
return strdup_to(ret, s); |
| 348 |
} |
| 349 |
|
| 350 |
static int get_dmi_properties(Context *c, const char * const * keys, char **ret) { |
| 351 |
int r = -ENOENT; |
| 352 |
|
| 353 |
assert(c); |
| 354 |
assert(ret); |
| 355 |
|
| 356 |
STRV_FOREACH(k, keys) { |
| 357 |
r = get_dmi_property(c, *k, ret); |
| 358 |
if (r >= 0 || !ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 359 |
return r; |
| 360 |
} |
| 361 |
|
| 362 |
return r; |
| 363 |
} |
| 364 |
|
| 365 |
static int get_chassis_asset_tag(Context *c, char **ret) { |
| 366 |
return get_dmi_property(c, "ID_CHASSIS_ASSET_TAG", ret); |
| 367 |
} |
| 368 |
|
| 369 |
static int get_hardware_vendor(Context *c, char **ret) { |
| 370 |
return get_dmi_properties(c, STRV_MAKE_CONST("ID_VENDOR_FROM_DATABASE", "ID_VENDOR"), ret); |
| 371 |
} |
| 372 |
|
| 373 |
static int get_hardware_model(Context *c, char **ret) { |
| 374 |
return get_dmi_properties(c, STRV_MAKE_CONST("ID_MODEL_FROM_DATABASE", "ID_MODEL"), ret); |
| 375 |
} |
| 376 |
|
| 377 |
static int get_hardware_sku(Context *c, char **ret) { |
| 378 |
_cleanup_free_ char *model = NULL, *sku = NULL; |
| 379 |
int r; |
| 380 |
|
| 381 |
assert(ret); |
| 382 |
|
| 383 |
r = get_dmi_property(c, "ID_SKU", &sku); |
| 384 |
if (r < 0) |
| 385 |
return r; |
| 386 |
|
| 387 |
/* Suppress reporting the SKU field, if it's the same string as the |
| 388 |
* model field, which it appears to be on various systems */ |
| 389 |
r = get_hardware_model(c, &model); |
| 390 |
if (r < 0) { |
| 391 |
if (r != -ENOENT) |
| 392 |
return r; |
| 393 |
} else if (streq_ptr(sku, model)) |
| 394 |
return -ENOENT; |
| 395 |
|
| 396 |
*ret = TAKE_PTR(sku); |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
static int get_hardware_version(Context *c, char **ret) { |
| 401 |
_cleanup_free_ char *version = NULL; |
| 402 |
int r; |
| 403 |
|
| 404 |
assert(ret); |
| 405 |
|
| 406 |
r = get_dmi_property(c, "ID_HARDWARE_VERSION", &version); |
| 407 |
if (r < 0) |
| 408 |
return r; |
| 409 |
|
| 410 |
/* Suppress reporting the version field, if it's the same string as the |
| 411 |
* model or sku field, which it appears to be on various systems */ |
| 412 |
int (*getter)(Context *c, char **ret); |
| 413 |
FOREACH_ARGUMENT(getter, get_hardware_model, get_hardware_sku) { |
| 414 |
_cleanup_free_ char *value = NULL; |
| 415 |
|
| 416 |
r = getter(c, &value); |
| 417 |
if (r == -ENOENT) |
| 418 |
continue; |
| 419 |
if (r < 0) |
| 420 |
return r; |
| 421 |
if (streq_ptr(version, value)) |
| 422 |
return -ENOENT; |
| 423 |
} |
| 424 |
|
| 425 |
*ret = TAKE_PTR(version); |
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
static int get_sysattr(sd_device *device, const char *key, char **ret) { |
| 430 |
const char *s; |
| 431 |
int r; |
| 432 |
|
| 433 |
assert(key); |
| 434 |
assert(ret); |
| 435 |
|
| 436 |
if (!device) |
| 437 |
return -ENODEV; |
| 438 |
|
| 439 |
r = device_get_sysattr_safe_string(device, key, &s); |
| 440 |
if (r < 0) |
| 441 |
return log_device_debug_errno(device, r, "Failed to read '%s' attribute: %m", key); |
| 442 |
|
| 443 |
if (!string_is_safe(s, STRING_ALLOW_EMPTY|STRING_ALLOW_BACKSLASHES|STRING_ALLOW_QUOTES|STRING_ALLOW_GLOBS)) |
| 444 |
return log_device_debug_errno(device, SYNTHETIC_ERRNO(ENXIO), |
| 445 |
"'%s' attribute is not safe for exposing through DBus: %s", |
| 446 |
key, s); |
| 447 |
|
| 448 |
return strdup_to(ret, empty_to_null(s)); |
| 449 |
} |
| 450 |
|
| 451 |
static int get_dmi_sysattr(Context *c, const char *key, char **ret) { |
| 452 |
return get_sysattr(ASSERT_PTR(c)->device_dmi, key, ret); |
| 453 |
} |
| 454 |
|
| 455 |
static int get_device_tree_sysattr(Context *c, const char *key, char **ret) { |
| 456 |
return get_sysattr(ASSERT_PTR(c)->device_tree, key, ret); |
| 457 |
} |
| 458 |
|
| 459 |
static int get_hardware_serial(Context *c, char **ret) { |
| 460 |
int r; |
| 461 |
|
| 462 |
assert(c); |
| 463 |
assert(ret); |
| 464 |
|
| 465 |
FOREACH_STRING(attr, "product_serial", "board_serial") { |
| 466 |
r = get_dmi_sysattr(c, attr, ret); |
| 467 |
if (r >= 0 || !ERRNO_IS_NEG_DEVICE_ABSENT(r)) |
| 468 |
return r; |
| 469 |
} |
| 470 |
|
| 471 |
return get_device_tree_sysattr(c, "serial-number", ret); |
| 472 |
} |
| 473 |
|
| 474 |
static int get_firmware_version(Context *c, char **ret) { |
| 475 |
return get_dmi_sysattr(c, "bios_version", ret); |
| 476 |
} |
| 477 |
|
| 478 |
static int get_firmware_vendor(Context *c, char **ret) { |
| 479 |
return get_dmi_sysattr(c, "bios_vendor", ret); |
| 480 |
} |
| 481 |
|
| 482 |
static int get_firmware_date(Context *c, usec_t *ret) { |
| 483 |
_cleanup_free_ char *bios_date = NULL, *month = NULL, *day = NULL, *year = NULL; |
| 484 |
int r; |
| 485 |
|
| 486 |
assert(c); |
| 487 |
assert(ret); |
| 488 |
|
| 489 |
r = get_dmi_sysattr(c, "bios_date", &bios_date); |
| 490 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(r)) { |
| 491 |
*ret = USEC_INFINITY; |
| 492 |
return 0; |
| 493 |
} |
| 494 |
if (r < 0) |
| 495 |
return r; |
| 496 |
|
| 497 |
const char *p = bios_date; |
| 498 |
r = extract_many_words(&p, "/", EXTRACT_DONT_COALESCE_SEPARATORS, &month, &day, &year); |
| 499 |
if (r < 0) |
| 500 |
return r; |
| 501 |
if (r != 3) /* less than three args read? */ |
| 502 |
return -EINVAL; |
| 503 |
if (!isempty(p)) /* more left in the string? */ |
| 504 |
return -EINVAL; |
| 505 |
|
| 506 |
unsigned m, d, y; |
| 507 |
r = safe_atou_full(month, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &m); |
| 508 |
if (r < 0) |
| 509 |
return r; |
| 510 |
if (m < 1 || m > 12) |
| 511 |
return -EINVAL; |
| 512 |
m -= 1; |
| 513 |
|
| 514 |
r = safe_atou_full(day, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &d); |
| 515 |
if (r < 0) |
| 516 |
return r; |
| 517 |
if (d < 1 || d > 31) |
| 518 |
return -EINVAL; |
| 519 |
|
| 520 |
r = safe_atou_full(year, 10 | SAFE_ATO_REFUSE_PLUS_MINUS | SAFE_ATO_REFUSE_LEADING_WHITESPACE, &y); |
| 521 |
if (r < 0) |
| 522 |
return r; |
| 523 |
if (y < 1970 || y > (unsigned) INT_MAX) |
| 524 |
return -EINVAL; |
| 525 |
y -= 1900; |
| 526 |
|
| 527 |
struct tm tm = { |
| 528 |
.tm_mday = d, |
| 529 |
.tm_mon = m, |
| 530 |
.tm_year = y, |
| 531 |
}; |
| 532 |
|
| 533 |
usec_t v; |
| 534 |
r = mktime_or_timegm_usec(&tm, /* utc= */ true, &v); |
| 535 |
if (r < 0) |
| 536 |
return r; |
| 537 |
if (tm.tm_mday != (int) d || tm.tm_mon != (int) m || tm.tm_year != (int) y) |
| 538 |
return -EINVAL; /* date was not normalized? (e.g. "30th of feb") */ |
| 539 |
|
| 540 |
*ret = v; |
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
static const char* valid_chassis(const char *chassis) { |
| 545 |
assert(chassis); |
| 546 |
|
| 547 |
return nulstr_get( |
| 548 |
"vm\0" |
| 549 |
"container\0" |
| 550 |
"desktop\0" |
| 551 |
"laptop\0" |
| 552 |
"convertible\0" |
| 553 |
"server\0" |
| 554 |
"tablet\0" |
| 555 |
"handset\0" |
| 556 |
"watch\0" |
| 557 |
"embedded\0", |
| 558 |
chassis); |
| 559 |
} |
| 560 |
|
| 561 |
static bool valid_deployment(const char *deployment) { |
| 562 |
assert(deployment); |
| 563 |
|
| 564 |
return in_charset(deployment, VALID_DEPLOYMENT_CHARS); |
| 565 |
} |
| 566 |
|
| 567 |
static const char* fallback_chassis_by_virtualization(void) { |
| 568 |
Virtualization v = detect_virtualization(); |
| 569 |
if (v < 0) { |
| 570 |
log_debug_errno(v, "Failed to detect virtualization, ignoring: %m"); |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
|
| 574 |
if (VIRTUALIZATION_IS_VM(v)) |
| 575 |
return "vm"; |
| 576 |
if (VIRTUALIZATION_IS_CONTAINER(v)) |
| 577 |
return "container"; |
| 578 |
|
| 579 |
return NULL; |
| 580 |
} |
| 581 |
|
| 582 |
static const char* fallback_chassis_by_dmi(Context *c) { |
| 583 |
unsigned t; |
| 584 |
int r; |
| 585 |
|
| 586 |
assert(c); |
| 587 |
|
| 588 |
if (!c->device_dmi) |
| 589 |
return NULL; |
| 590 |
|
| 591 |
r = device_get_sysattr_unsigned(c->device_dmi, "chassis_type", &t); |
| 592 |
if (r < 0) { |
| 593 |
log_debug_errno(r, "Failed to read/parse DMI chassis type, ignoring: %m"); |
| 594 |
return NULL; |
| 595 |
} |
| 596 |
|
| 597 |
/* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any |
| 598 |
* additional guesswork on top of that. |
| 599 |
* |
| 600 |
* See the SMBIOS Specification 3.5.0 section 7.4.1 for details about the values listed here: |
| 601 |
* |
| 602 |
* https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.5.0.pdf |
| 603 |
*/ |
| 604 |
|
| 605 |
switch (t) { |
| 606 |
|
| 607 |
case 0x03: /* Desktop */ |
| 608 |
case 0x04: /* Low Profile Desktop */ |
| 609 |
case 0x06: /* Mini Tower */ |
| 610 |
case 0x07: /* Tower */ |
| 611 |
case 0x0D: /* All in one (i.e. PC built into monitor) */ |
| 612 |
case 0x23: /* Mini PC */ |
| 613 |
case 0x24: /* Stick PC */ |
| 614 |
return "desktop"; |
| 615 |
|
| 616 |
case 0x8: /* Portable */ |
| 617 |
case 0x9: /* Laptop */ |
| 618 |
case 0xA: /* Notebook */ |
| 619 |
case 0xE: /* Sub Notebook */ |
| 620 |
return "laptop"; |
| 621 |
|
| 622 |
case 0xB: /* Hand Held */ |
| 623 |
return "handset"; |
| 624 |
|
| 625 |
case 0x11: /* Main Server Chassis */ |
| 626 |
case 0x1C: /* Blade */ |
| 627 |
case 0x1D: /* Blade Enclosure */ |
| 628 |
return "server"; |
| 629 |
|
| 630 |
case 0x1E: /* Tablet */ |
| 631 |
return "tablet"; |
| 632 |
|
| 633 |
case 0x1F: /* Convertible */ |
| 634 |
case 0x20: /* Detachable */ |
| 635 |
return "convertible"; |
| 636 |
|
| 637 |
case 0x21: /* IoT Gateway */ |
| 638 |
case 0x22: /* Embedded PC */ |
| 639 |
return "embedded"; |
| 640 |
|
| 641 |
default: |
| 642 |
log_debug("Unhandled DMI chassis type 0x%02x, ignoring.", t); |
| 643 |
return NULL; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
static const char* fallback_chassis_by_acpi(Context *c) { |
| 648 |
unsigned t; |
| 649 |
int r; |
| 650 |
|
| 651 |
assert(c); |
| 652 |
|
| 653 |
if (!c->device_acpi) |
| 654 |
return NULL; |
| 655 |
|
| 656 |
r = device_get_sysattr_unsigned(c->device_acpi, "pm_profile", &t); |
| 657 |
if (r < 0) { |
| 658 |
log_debug_errno(r, "Failed to read/parse ACPI PM profile, ignoring: %m"); |
| 659 |
return NULL; |
| 660 |
} |
| 661 |
|
| 662 |
/* We only list the really obvious cases here as the ACPI data is not really super reliable. |
| 663 |
* |
| 664 |
* See the ACPI 5.0 Spec Section 5.2.9.1 for details: |
| 665 |
* |
| 666 |
* http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf |
| 667 |
*/ |
| 668 |
|
| 669 |
switch (t) { |
| 670 |
|
| 671 |
case 1: /* Desktop */ |
| 672 |
case 3: /* Workstation */ |
| 673 |
case 6: /* Appliance PC */ |
| 674 |
return "desktop"; |
| 675 |
|
| 676 |
case 2: /* Mobile */ |
| 677 |
return "laptop"; |
| 678 |
|
| 679 |
case 4: /* Enterprise Server */ |
| 680 |
case 5: /* SOHO Server */ |
| 681 |
case 7: /* Performance Server */ |
| 682 |
return "server"; |
| 683 |
|
| 684 |
case 8: /* Tablet */ |
| 685 |
return "tablet"; |
| 686 |
|
| 687 |
default: |
| 688 |
log_debug("Unhandled ACPI PM profile 0x%02x, ignoring.", t); |
| 689 |
return NULL; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
static const char* fallback_chassis_by_device_tree(Context *c) { |
| 694 |
const char *type, *chassis; |
| 695 |
int r; |
| 696 |
|
| 697 |
assert(c); |
| 698 |
|
| 699 |
if (!c->device_tree) |
| 700 |
return NULL; |
| 701 |
|
| 702 |
r = device_get_sysattr_safe_string(c->device_tree, "chassis-type", &type); |
| 703 |
if (r < 0) { |
| 704 |
log_debug_errno(r, "Failed to read device-tree chassis type, ignoring: %m"); |
| 705 |
return NULL; |
| 706 |
} |
| 707 |
|
| 708 |
/* Note that the DeviceTree specification uses the very same vocabulary |
| 709 |
* of chassis types as we do, hence we do not need to translate these types: |
| 710 |
* |
| 711 |
* https://github.com/devicetree-org/devicetree-specification/blob/master/source/chapter3-devicenodes.rst */ |
| 712 |
|
| 713 |
chassis = valid_chassis(type); |
| 714 |
if (!chassis) |
| 715 |
log_debug("Invalid device-tree chassis type \"%s\", ignoring.", type); |
| 716 |
return chassis; |
| 717 |
} |
| 718 |
|
| 719 |
static const char* fallback_chassis(Context *c) { |
| 720 |
assert(c); |
| 721 |
|
| 722 |
return |
| 723 |
fallback_chassis_by_virtualization() ?: |
| 724 |
fallback_chassis_by_dmi(c) ?: |
| 725 |
fallback_chassis_by_acpi(c) ?: |
| 726 |
fallback_chassis_by_device_tree(c); |
| 727 |
} |
| 728 |
|
| 729 |
static char* context_get_chassis(Context *c) { |
| 730 |
const char *fallback; |
| 731 |
char *dmi; |
| 732 |
|
| 733 |
assert(c); |
| 734 |
|
| 735 |
if (!isempty(c->data[PROP_CHASSIS])) |
| 736 |
return strdup(c->data[PROP_CHASSIS]); |
| 737 |
|
| 738 |
if (get_dmi_property(c, "ID_CHASSIS", &dmi) >= 0) |
| 739 |
return dmi; |
| 740 |
|
| 741 |
fallback = fallback_chassis(c); |
| 742 |
if (fallback) |
| 743 |
return strdup(fallback); |
| 744 |
|
| 745 |
return NULL; |
| 746 |
} |
| 747 |
|
| 748 |
static char* context_fallback_icon_name(Context *c) { |
| 749 |
_cleanup_free_ char *chassis = NULL; |
| 750 |
|
| 751 |
assert(c); |
| 752 |
|
| 753 |
chassis = context_get_chassis(c); |
| 754 |
if (chassis) |
| 755 |
return strjoin("computer-", chassis); |
| 756 |
|
| 757 |
return strdup("computer"); |
| 758 |
} |
| 759 |
|
| 760 |
static int context_update_kernel_hostname( |
| 761 |
Context *c, |
| 762 |
const char *transient_hostname) { |
| 763 |
|
| 764 |
_cleanup_free_ char *_hn_free = NULL; |
| 765 |
const char *hn; |
| 766 |
HostnameSource hns; |
| 767 |
int r; |
| 768 |
|
| 769 |
assert(c); |
| 770 |
|
| 771 |
/* /etc/hostname has the highest preference ... */ |
| 772 |
if (c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS]) { |
| 773 |
hn = c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS]; |
| 774 |
hns = HOSTNAME_STATIC; |
| 775 |
|
| 776 |
/* ... the transient hostname, (ie: DHCP) comes next ... */ |
| 777 |
} else if (transient_hostname) { |
| 778 |
hn = transient_hostname; |
| 779 |
hns = HOSTNAME_TRANSIENT; |
| 780 |
|
| 781 |
/* ... and the ultimate fallback */ |
| 782 |
} else { |
| 783 |
hn = _hn_free = get_default_hostname(); |
| 784 |
if (!hn) |
| 785 |
return log_oom(); |
| 786 |
|
| 787 |
hns = HOSTNAME_DEFAULT; |
| 788 |
} |
| 789 |
|
| 790 |
r = sethostname_idempotent(hn); |
| 791 |
if (r < 0) |
| 792 |
return log_error_errno(r, "Failed to set hostname: %m"); |
| 793 |
|
| 794 |
if (c->hostname_source != hns) { |
| 795 |
c->hostname_source = hns; |
| 796 |
r = 1; |
| 797 |
} |
| 798 |
|
| 799 |
if (r == 0) |
| 800 |
log_debug("Hostname was already set to <%s>.", hn); |
| 801 |
else { |
| 802 |
log_info("Hostname set to <%s> (%s)", hn, hostname_source_to_string(hns)); |
| 803 |
|
| 804 |
hostname_update_source_hint(hn, hns); |
| 805 |
} |
| 806 |
|
| 807 |
return r; /* 0 if no change, 1 if something was done */ |
| 808 |
} |
| 809 |
|
| 810 |
static void unset_statp(struct stat **p) { |
| 811 |
assert(p); |
| 812 |
|
| 813 |
if (!*p) |
| 814 |
return; |
| 815 |
|
| 816 |
**p = (struct stat) {}; |
| 817 |
} |
| 818 |
|
| 819 |
static int context_write_data_static_hostname(Context *c) { |
| 820 |
_cleanup_(unset_statp) struct stat *s = NULL; |
| 821 |
int r; |
| 822 |
|
| 823 |
assert(c); |
| 824 |
|
| 825 |
/* Make sure that if we fail here, we invalidate the cached information, since it was updated |
| 826 |
* already, even if we can't make it hit the disk. */ |
| 827 |
s = &c->etc_hostname_stat; |
| 828 |
|
| 829 |
if (isempty(c->data[PROP_STATIC_HOSTNAME])) { |
| 830 |
if (unlink(etc_hostname()) < 0 && errno != ENOENT) |
| 831 |
return -errno; |
| 832 |
|
| 833 |
TAKE_PTR(s); |
| 834 |
return 0; |
| 835 |
} |
| 836 |
|
| 837 |
r = write_string_file(etc_hostname(), c->data[PROP_STATIC_HOSTNAME], WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL); |
| 838 |
if (r < 0) |
| 839 |
return r; |
| 840 |
|
| 841 |
TAKE_PTR(s); |
| 842 |
return 0; |
| 843 |
} |
| 844 |
|
| 845 |
static int context_write_data_machine_info(Context *c) { |
| 846 |
_cleanup_(unset_statp) struct stat *s = NULL; |
| 847 |
static const char * const name[_PROP_MAX] = { |
| 848 |
[PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME", |
| 849 |
[PROP_ICON_NAME] = "ICON_NAME", |
| 850 |
[PROP_CHASSIS] = "CHASSIS", |
| 851 |
[PROP_DEPLOYMENT] = "DEPLOYMENT", |
| 852 |
[PROP_LOCATION] = "LOCATION", |
| 853 |
[PROP_TAGS] = "TAGS", |
| 854 |
}; |
| 855 |
_cleanup_strv_free_ char **l = NULL; |
| 856 |
int r; |
| 857 |
|
| 858 |
assert(c); |
| 859 |
|
| 860 |
/* Make sure that if we fail here, we invalidate the cached information, since it was updated |
| 861 |
* already, even if we can't make it hit the disk. */ |
| 862 |
s = &c->etc_machine_info_stat; |
| 863 |
|
| 864 |
r = load_env_file(NULL, etc_machine_info(), &l); |
| 865 |
if (r < 0 && r != -ENOENT) |
| 866 |
return r; |
| 867 |
|
| 868 |
for (HostProperty p = _PROP_MACHINE_INFO_SETTABLE_FIRST; p <= _PROP_MACHINE_INFO_SETTABLE_LAST; p++) { |
| 869 |
assert(name[p]); |
| 870 |
|
| 871 |
r = strv_env_assign(&l, name[p], empty_to_null(c->data[p])); |
| 872 |
if (r < 0) |
| 873 |
return r; |
| 874 |
} |
| 875 |
|
| 876 |
if (strv_isempty(l)) { |
| 877 |
if (unlink(etc_machine_info()) < 0 && errno != ENOENT) |
| 878 |
return -errno; |
| 879 |
|
| 880 |
TAKE_PTR(s); |
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
r = write_env_file( |
| 885 |
AT_FDCWD, |
| 886 |
etc_machine_info(), |
| 887 |
/* headers= */ NULL, |
| 888 |
l, |
| 889 |
WRITE_ENV_FILE_LABEL); |
| 890 |
if (r < 0) |
| 891 |
return r; |
| 892 |
|
| 893 |
TAKE_PTR(s); |
| 894 |
return 0; |
| 895 |
} |
| 896 |
|
| 897 |
static int property_get_hardware_property( |
| 898 |
sd_bus_message *reply, |
| 899 |
Context *c, |
| 900 |
HostProperty prop, |
| 901 |
int (*getter)(Context *c, char **ret)) { |
| 902 |
|
| 903 |
_cleanup_free_ char *from_dmi = NULL; |
| 904 |
|
| 905 |
assert(reply); |
| 906 |
assert(c); |
| 907 |
assert(IN_SET(prop, PROP_HARDWARE_VENDOR, PROP_HARDWARE_MODEL, |
| 908 |
PROP_HARDWARE_SKU, PROP_HARDWARE_VERSION)); |
| 909 |
assert(getter); |
| 910 |
|
| 911 |
context_read_machine_info(c); |
| 912 |
|
| 913 |
if (isempty(c->data[prop])) |
| 914 |
(void) getter(c, &from_dmi); |
| 915 |
|
| 916 |
return sd_bus_message_append(reply, "s", from_dmi ?: c->data[prop]); |
| 917 |
} |
| 918 |
|
| 919 |
static int property_get_hardware_vendor( |
| 920 |
sd_bus *bus, |
| 921 |
const char *path, |
| 922 |
const char *interface, |
| 923 |
const char *property, |
| 924 |
sd_bus_message *reply, |
| 925 |
void *userdata, |
| 926 |
sd_bus_error *error) { |
| 927 |
|
| 928 |
return property_get_hardware_property(reply, userdata, PROP_HARDWARE_VENDOR, get_hardware_vendor); |
| 929 |
} |
| 930 |
|
| 931 |
static int property_get_hardware_model( |
| 932 |
sd_bus *bus, |
| 933 |
const char *path, |
| 934 |
const char *interface, |
| 935 |
const char *property, |
| 936 |
sd_bus_message *reply, |
| 937 |
void *userdata, |
| 938 |
sd_bus_error *error) { |
| 939 |
|
| 940 |
return property_get_hardware_property(reply, userdata, PROP_HARDWARE_MODEL, get_hardware_model); |
| 941 |
} |
| 942 |
|
| 943 |
static int property_get_hardware_sku( |
| 944 |
sd_bus *bus, |
| 945 |
const char *path, |
| 946 |
const char *interface, |
| 947 |
const char *property, |
| 948 |
sd_bus_message *reply, |
| 949 |
void *userdata, |
| 950 |
sd_bus_error *error) { |
| 951 |
|
| 952 |
return property_get_hardware_property(reply, userdata, PROP_HARDWARE_SKU, get_hardware_sku); |
| 953 |
} |
| 954 |
|
| 955 |
static int property_get_hardware_version( |
| 956 |
sd_bus *bus, |
| 957 |
const char *path, |
| 958 |
const char *interface, |
| 959 |
const char *property, |
| 960 |
sd_bus_message *reply, |
| 961 |
void *userdata, |
| 962 |
sd_bus_error *error) { |
| 963 |
|
| 964 |
return property_get_hardware_property(reply, userdata, PROP_HARDWARE_VERSION, get_hardware_version); |
| 965 |
} |
| 966 |
|
| 967 |
static int property_get_firmware_version( |
| 968 |
sd_bus *bus, |
| 969 |
const char *path, |
| 970 |
const char *interface, |
| 971 |
const char *property, |
| 972 |
sd_bus_message *reply, |
| 973 |
void *userdata, |
| 974 |
sd_bus_error *error) { |
| 975 |
|
| 976 |
_cleanup_free_ char *firmware_version = NULL; |
| 977 |
Context *c = ASSERT_PTR(userdata); |
| 978 |
|
| 979 |
(void) get_firmware_version(c, &firmware_version); |
| 980 |
|
| 981 |
return sd_bus_message_append(reply, "s", firmware_version); |
| 982 |
} |
| 983 |
|
| 984 |
static int property_get_firmware_vendor( |
| 985 |
sd_bus *bus, |
| 986 |
const char *path, |
| 987 |
const char *interface, |
| 988 |
const char *property, |
| 989 |
sd_bus_message *reply, |
| 990 |
void *userdata, |
| 991 |
sd_bus_error *error) { |
| 992 |
|
| 993 |
_cleanup_free_ char *firmware_vendor = NULL; |
| 994 |
Context *c = ASSERT_PTR(userdata); |
| 995 |
|
| 996 |
(void) get_firmware_vendor(c, &firmware_vendor); |
| 997 |
|
| 998 |
return sd_bus_message_append(reply, "s", firmware_vendor); |
| 999 |
} |
| 1000 |
|
| 1001 |
static int property_get_firmware_date( |
| 1002 |
sd_bus *bus, |
| 1003 |
const char *path, |
| 1004 |
const char *interface, |
| 1005 |
const char *property, |
| 1006 |
sd_bus_message *reply, |
| 1007 |
void *userdata, |
| 1008 |
sd_bus_error *error) { |
| 1009 |
|
| 1010 |
usec_t firmware_date = USEC_INFINITY; |
| 1011 |
Context *c = ASSERT_PTR(userdata); |
| 1012 |
|
| 1013 |
(void) get_firmware_date(c, &firmware_date); |
| 1014 |
|
| 1015 |
return sd_bus_message_append(reply, "t", firmware_date); |
| 1016 |
} |
| 1017 |
|
| 1018 |
static int property_get_chassis_asset_tag( |
| 1019 |
sd_bus *bus, |
| 1020 |
const char *path, |
| 1021 |
const char *interface, |
| 1022 |
const char *property, |
| 1023 |
sd_bus_message *reply, |
| 1024 |
void *userdata, |
| 1025 |
sd_bus_error *error) { |
| 1026 |
|
| 1027 |
_cleanup_free_ char *chassis_asset_tag = NULL; |
| 1028 |
Context *c = ASSERT_PTR(userdata); |
| 1029 |
|
| 1030 |
(void) get_chassis_asset_tag(c, &chassis_asset_tag); |
| 1031 |
|
| 1032 |
return sd_bus_message_append(reply, "s", chassis_asset_tag); |
| 1033 |
} |
| 1034 |
|
| 1035 |
static int property_get_hostname( |
| 1036 |
sd_bus *bus, |
| 1037 |
const char *path, |
| 1038 |
const char *interface, |
| 1039 |
const char *property, |
| 1040 |
sd_bus_message *reply, |
| 1041 |
void *userdata, |
| 1042 |
sd_bus_error *error) { |
| 1043 |
|
| 1044 |
_cleanup_free_ char *hn = NULL; |
| 1045 |
int r; |
| 1046 |
|
| 1047 |
r = gethostname_strict(&hn); |
| 1048 |
if (r < 0) { |
| 1049 |
if (r != -ENXIO) |
| 1050 |
return r; |
| 1051 |
|
| 1052 |
hn = get_default_hostname(); |
| 1053 |
if (!hn) |
| 1054 |
return -ENOMEM; |
| 1055 |
} |
| 1056 |
|
| 1057 |
return sd_bus_message_append(reply, "s", hn); |
| 1058 |
} |
| 1059 |
|
| 1060 |
static int property_get_static_hostname( |
| 1061 |
sd_bus *bus, |
| 1062 |
const char *path, |
| 1063 |
const char *interface, |
| 1064 |
const char *property, |
| 1065 |
sd_bus_message *reply, |
| 1066 |
void *userdata, |
| 1067 |
sd_bus_error *error) { |
| 1068 |
|
| 1069 |
Context *c = ASSERT_PTR(userdata); |
| 1070 |
|
| 1071 |
context_read_etc_hostname(c); |
| 1072 |
|
| 1073 |
return sd_bus_message_append(reply, "s", c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS]); |
| 1074 |
} |
| 1075 |
|
| 1076 |
static int property_get_default_hostname( |
| 1077 |
sd_bus *bus, |
| 1078 |
const char *path, |
| 1079 |
const char *interface, |
| 1080 |
const char *property, |
| 1081 |
sd_bus_message *reply, |
| 1082 |
void *userdata, |
| 1083 |
sd_bus_error *error) { |
| 1084 |
|
| 1085 |
_cleanup_free_ char *hn = NULL; |
| 1086 |
|
| 1087 |
hn = get_default_hostname(); |
| 1088 |
if (!hn) |
| 1089 |
return log_oom(); |
| 1090 |
|
| 1091 |
return sd_bus_message_append(reply, "s", hn); |
| 1092 |
} |
| 1093 |
|
| 1094 |
static void context_determine_hostname_source(Context *c) { |
| 1095 |
_cleanup_free_ char *hostname = NULL; |
| 1096 |
int r; |
| 1097 |
|
| 1098 |
assert(c); |
| 1099 |
|
| 1100 |
if (c->hostname_source >= 0) |
| 1101 |
return; |
| 1102 |
|
| 1103 |
(void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST, &hostname); |
| 1104 |
|
| 1105 |
if (streq_ptr(hostname, c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS])) |
| 1106 |
c->hostname_source = HOSTNAME_STATIC; |
| 1107 |
else { |
| 1108 |
_cleanup_free_ char *fallback = NULL; |
| 1109 |
|
| 1110 |
/* If the hostname was not set by us, try to figure out where it came from. If we set it to |
| 1111 |
* the default hostname, the file will tell us. We compare the string because it is possible |
| 1112 |
* that the hostname was set by an older version that had a different fallback, in the initrd |
| 1113 |
* or before we reexecuted. */ |
| 1114 |
|
| 1115 |
r = read_one_line_file("/run/systemd/default-hostname", &fallback); |
| 1116 |
if (r < 0 && r != -ENOENT) |
| 1117 |
log_warning_errno(r, "Failed to read /run/systemd/default-hostname, ignoring: %m"); |
| 1118 |
|
| 1119 |
if (streq_ptr(fallback, hostname)) |
| 1120 |
c->hostname_source = HOSTNAME_DEFAULT; |
| 1121 |
else |
| 1122 |
c->hostname_source = HOSTNAME_TRANSIENT; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
static int property_get_hostname_source( |
| 1127 |
sd_bus *bus, |
| 1128 |
const char *path, |
| 1129 |
const char *interface, |
| 1130 |
const char *property, |
| 1131 |
sd_bus_message *reply, |
| 1132 |
void *userdata, |
| 1133 |
sd_bus_error *error) { |
| 1134 |
|
| 1135 |
Context *c = ASSERT_PTR(userdata); |
| 1136 |
|
| 1137 |
context_read_etc_hostname(c); |
| 1138 |
context_determine_hostname_source(c); |
| 1139 |
|
| 1140 |
return sd_bus_message_append(reply, "s", hostname_source_to_string(c->hostname_source)); |
| 1141 |
} |
| 1142 |
|
| 1143 |
static int property_get_machine_info_field( |
| 1144 |
sd_bus *bus, |
| 1145 |
const char *path, |
| 1146 |
const char *interface, |
| 1147 |
const char *property, |
| 1148 |
sd_bus_message *reply, |
| 1149 |
void *userdata, |
| 1150 |
sd_bus_error *error) { |
| 1151 |
|
| 1152 |
sd_bus_slot *slot; |
| 1153 |
Context *c; |
| 1154 |
|
| 1155 |
/* Acquire the context object without this property's userdata offset added. Explanation: we want |
| 1156 |
* access to two pointers here: a) the main context object we cache all properties in, and b) the |
| 1157 |
* pointer to the property field inside the context object that we are supposed to update and |
| 1158 |
* use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us |
| 1159 |
* from the 'userdata' pointer we supplied when the vtable was registered, with the offset we |
| 1160 |
* specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from |
| 1161 |
* the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot |
| 1162 |
* object is (which encapsulates the vtable registration), and then query the 'userdata' field |
| 1163 |
* directly off it. */ |
| 1164 |
assert_se(slot = sd_bus_get_current_slot(bus)); |
| 1165 |
assert_se(c = sd_bus_slot_get_userdata(slot)); |
| 1166 |
|
| 1167 |
context_read_machine_info(c); |
| 1168 |
|
| 1169 |
return sd_bus_message_append(reply, "s", *(char**) userdata); |
| 1170 |
} |
| 1171 |
|
| 1172 |
static int property_get_tags( |
| 1173 |
sd_bus *bus, |
| 1174 |
const char *path, |
| 1175 |
const char *interface, |
| 1176 |
const char *property, |
| 1177 |
sd_bus_message *reply, |
| 1178 |
void *userdata, |
| 1179 |
sd_bus_error *error) { |
| 1180 |
|
| 1181 |
Context *c = ASSERT_PTR(userdata); |
| 1182 |
int r; |
| 1183 |
|
| 1184 |
context_read_machine_info(c); |
| 1185 |
|
| 1186 |
/* Silently drop any invalid tags that might have been written into the file by hand */ |
| 1187 |
_cleanup_strv_free_ char **l = NULL; |
| 1188 |
r = machine_tags_from_string(c->data[PROP_TAGS], /* graceful= */ true, &l); |
| 1189 |
if (r < 0) |
| 1190 |
log_warning_errno(r, "Failed to parse machine tags '%s', ignoring: %m", strnull(c->data[PROP_TAGS])); |
| 1191 |
|
| 1192 |
return sd_bus_message_append_strv(reply, l); |
| 1193 |
} |
| 1194 |
|
| 1195 |
static int property_get_os_release_field( |
| 1196 |
sd_bus *bus, |
| 1197 |
const char *path, |
| 1198 |
const char *interface, |
| 1199 |
const char *property, |
| 1200 |
sd_bus_message *reply, |
| 1201 |
void *userdata, |
| 1202 |
sd_bus_error *error) { |
| 1203 |
|
| 1204 |
sd_bus_slot *slot; |
| 1205 |
Context *c; |
| 1206 |
|
| 1207 |
/* As above, acquire the current context without this property's userdata offset added. */ |
| 1208 |
assert_se(slot = sd_bus_get_current_slot(bus)); |
| 1209 |
assert_se(c = sd_bus_slot_get_userdata(slot)); |
| 1210 |
|
| 1211 |
context_read_os_release(c); |
| 1212 |
|
| 1213 |
return sd_bus_message_append(reply, "s", *(char**) userdata); |
| 1214 |
} |
| 1215 |
|
| 1216 |
static int property_get_os_support_end( |
| 1217 |
sd_bus *bus, |
| 1218 |
const char *path, |
| 1219 |
const char *interface, |
| 1220 |
const char *property, |
| 1221 |
sd_bus_message *reply, |
| 1222 |
void *userdata, |
| 1223 |
sd_bus_error *error) { |
| 1224 |
|
| 1225 |
Context *c = userdata; |
| 1226 |
usec_t eol = USEC_INFINITY; |
| 1227 |
|
| 1228 |
context_read_os_release(c); |
| 1229 |
|
| 1230 |
if (c->data[PROP_OS_SUPPORT_END]) |
| 1231 |
(void) os_release_support_ended(c->data[PROP_OS_SUPPORT_END], /* quiet= */ false, &eol); |
| 1232 |
|
| 1233 |
return sd_bus_message_append(reply, "t", eol); |
| 1234 |
} |
| 1235 |
|
| 1236 |
static int property_get_icon_name( |
| 1237 |
sd_bus *bus, |
| 1238 |
const char *path, |
| 1239 |
const char *interface, |
| 1240 |
const char *property, |
| 1241 |
sd_bus_message *reply, |
| 1242 |
void *userdata, |
| 1243 |
sd_bus_error *error) { |
| 1244 |
|
| 1245 |
_cleanup_free_ char *n = NULL; |
| 1246 |
Context *c = userdata; |
| 1247 |
const char *name; |
| 1248 |
|
| 1249 |
context_read_machine_info(c); |
| 1250 |
|
| 1251 |
if (isempty(c->data[PROP_ICON_NAME])) |
| 1252 |
name = n = context_fallback_icon_name(c); |
| 1253 |
else |
| 1254 |
name = c->data[PROP_ICON_NAME]; |
| 1255 |
|
| 1256 |
if (!name) |
| 1257 |
return -ENOMEM; |
| 1258 |
|
| 1259 |
return sd_bus_message_append(reply, "s", name); |
| 1260 |
} |
| 1261 |
|
| 1262 |
static int property_get_chassis( |
| 1263 |
sd_bus *bus, |
| 1264 |
const char *path, |
| 1265 |
const char *interface, |
| 1266 |
const char *property, |
| 1267 |
sd_bus_message *reply, |
| 1268 |
void *userdata, |
| 1269 |
sd_bus_error *error) { |
| 1270 |
|
| 1271 |
_cleanup_free_ char *chassis = NULL; |
| 1272 |
Context *c = userdata; |
| 1273 |
|
| 1274 |
context_read_machine_info(c); |
| 1275 |
|
| 1276 |
chassis = context_get_chassis(c); |
| 1277 |
|
| 1278 |
return sd_bus_message_append(reply, "s", chassis); |
| 1279 |
} |
| 1280 |
|
| 1281 |
static int property_get_uname_field( |
| 1282 |
sd_bus *bus, |
| 1283 |
const char *path, |
| 1284 |
const char *interface, |
| 1285 |
const char *property, |
| 1286 |
sd_bus_message *reply, |
| 1287 |
void *userdata, |
| 1288 |
sd_bus_error *error) { |
| 1289 |
|
| 1290 |
struct utsname u; |
| 1291 |
|
| 1292 |
assert_se(uname(&u) >= 0); |
| 1293 |
|
| 1294 |
return sd_bus_message_append(reply, "s", (char*) &u + PTR_TO_SIZE(userdata)); |
| 1295 |
} |
| 1296 |
|
| 1297 |
static int property_get_machine_id( |
| 1298 |
sd_bus *bus, |
| 1299 |
const char *path, |
| 1300 |
const char *interface, |
| 1301 |
const char *property, |
| 1302 |
sd_bus_message *reply, |
| 1303 |
void *userdata, |
| 1304 |
sd_bus_error *error) { |
| 1305 |
|
| 1306 |
sd_id128_t id; |
| 1307 |
int r; |
| 1308 |
|
| 1309 |
r = sd_id128_get_machine(&id); |
| 1310 |
if (r < 0) |
| 1311 |
return r; |
| 1312 |
|
| 1313 |
return bus_property_get_id128(bus, path, interface, property, reply, &id, error); |
| 1314 |
} |
| 1315 |
|
| 1316 |
static int property_get_boot_id( |
| 1317 |
sd_bus *bus, |
| 1318 |
const char *path, |
| 1319 |
const char *interface, |
| 1320 |
const char *property, |
| 1321 |
sd_bus_message *reply, |
| 1322 |
void *userdata, |
| 1323 |
sd_bus_error *error) { |
| 1324 |
|
| 1325 |
sd_id128_t id; |
| 1326 |
int r; |
| 1327 |
|
| 1328 |
r = sd_id128_get_boot(&id); |
| 1329 |
if (r < 0) |
| 1330 |
return r; |
| 1331 |
|
| 1332 |
return bus_property_get_id128(bus, path, interface, property, reply, &id, error); |
| 1333 |
} |
| 1334 |
|
| 1335 |
static int property_get_vsock_cid( |
| 1336 |
sd_bus *bus, |
| 1337 |
const char *path, |
| 1338 |
const char *interface, |
| 1339 |
const char *property, |
| 1340 |
sd_bus_message *reply, |
| 1341 |
void *userdata, |
| 1342 |
sd_bus_error *error) { |
| 1343 |
|
| 1344 |
unsigned local_cid = VMADDR_CID_ANY; |
| 1345 |
|
| 1346 |
(void) vsock_get_local_cid(&local_cid); |
| 1347 |
|
| 1348 |
return sd_bus_message_append(reply, "u", (uint32_t) local_cid); |
| 1349 |
} |
| 1350 |
|
| 1351 |
static int validate_and_substitute_hostname(const char *name, char **ret_substituted, sd_bus_error *error) { |
| 1352 |
int r; |
| 1353 |
|
| 1354 |
assert(ret_substituted); |
| 1355 |
|
| 1356 |
if (!name) { |
| 1357 |
*ret_substituted = NULL; |
| 1358 |
return 0; |
| 1359 |
} |
| 1360 |
|
| 1361 |
_cleanup_free_ char *substituted = strdup(name); |
| 1362 |
if (!substituted) |
| 1363 |
return log_oom(); |
| 1364 |
|
| 1365 |
r = hostname_substitute_wildcards(substituted); |
| 1366 |
if (r < 0) |
| 1367 |
return log_error_errno(r, "Failed to substitute wildcards in hostname: %m"); |
| 1368 |
|
| 1369 |
if (!hostname_is_valid(substituted, 0)) |
| 1370 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name); |
| 1371 |
|
| 1372 |
*ret_substituted = TAKE_PTR(substituted); |
| 1373 |
return 1; |
| 1374 |
} |
| 1375 |
|
| 1376 |
static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1377 |
Context *c = ASSERT_PTR(userdata); |
| 1378 |
const char *name; |
| 1379 |
int interactive, r; |
| 1380 |
|
| 1381 |
assert(m); |
| 1382 |
|
| 1383 |
r = sd_bus_message_read(m, "sb", &name, &interactive); |
| 1384 |
if (r < 0) |
| 1385 |
return r; |
| 1386 |
|
| 1387 |
name = empty_to_null(name); |
| 1388 |
|
| 1389 |
/* We always go through with the procedure below without comparing to the current hostname, because |
| 1390 |
* we might want to adjust hostname source information even if the actual hostname is unchanged. */ |
| 1391 |
|
| 1392 |
_cleanup_free_ char *substituted = NULL; |
| 1393 |
r = validate_and_substitute_hostname(name, &substituted, error); |
| 1394 |
if (r < 0) |
| 1395 |
return r; |
| 1396 |
|
| 1397 |
name = substituted; |
| 1398 |
|
| 1399 |
r = bus_verify_polkit_async_full( |
| 1400 |
m, |
| 1401 |
"org.freedesktop.hostname1.set-hostname", |
| 1402 |
/* details= */ NULL, |
| 1403 |
/* good_user= */ UID_INVALID, |
| 1404 |
interactive ? POLKIT_ALLOW_INTERACTIVE : 0, |
| 1405 |
&c->polkit_registry, |
| 1406 |
error); |
| 1407 |
if (r < 0) |
| 1408 |
return r; |
| 1409 |
if (r == 0) |
| 1410 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1411 |
|
| 1412 |
context_read_etc_hostname(c); |
| 1413 |
|
| 1414 |
r = context_update_kernel_hostname(c, name); |
| 1415 |
if (r < 0) |
| 1416 |
return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m"); |
| 1417 |
else if (r > 0) |
| 1418 |
(void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), |
| 1419 |
"/org/freedesktop/hostname1", "org.freedesktop.hostname1", |
| 1420 |
"Hostname", "HostnameSource", NULL); |
| 1421 |
|
| 1422 |
return sd_bus_reply_method_return(m, NULL); |
| 1423 |
} |
| 1424 |
|
| 1425 |
static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1426 |
Context *c = ASSERT_PTR(userdata); |
| 1427 |
const char *name; |
| 1428 |
int interactive, r; |
| 1429 |
|
| 1430 |
assert(m); |
| 1431 |
|
| 1432 |
r = sd_bus_message_read(m, "sb", &name, &interactive); |
| 1433 |
if (r < 0) |
| 1434 |
return r; |
| 1435 |
|
| 1436 |
name = empty_to_null(name); |
| 1437 |
|
| 1438 |
context_read_etc_hostname(c); |
| 1439 |
|
| 1440 |
if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME])) |
| 1441 |
return sd_bus_reply_method_return(m, NULL); |
| 1442 |
|
| 1443 |
_cleanup_free_ char *substituted = NULL; |
| 1444 |
r = validate_and_substitute_hostname(name, &substituted, error); |
| 1445 |
if (r < 0) |
| 1446 |
return r; |
| 1447 |
|
| 1448 |
r = bus_verify_polkit_async_full( |
| 1449 |
m, |
| 1450 |
"org.freedesktop.hostname1.set-static-hostname", |
| 1451 |
/* details= */ NULL, |
| 1452 |
/* good_user= */ UID_INVALID, |
| 1453 |
interactive ? POLKIT_ALLOW_INTERACTIVE : 0, |
| 1454 |
&c->polkit_registry, |
| 1455 |
error); |
| 1456 |
if (r < 0) |
| 1457 |
return r; |
| 1458 |
if (r == 0) |
| 1459 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1460 |
|
| 1461 |
r = free_and_strdup_warn(&c->data[PROP_STATIC_HOSTNAME], name); |
| 1462 |
if (r < 0) |
| 1463 |
return r; |
| 1464 |
|
| 1465 |
free_and_replace(c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS], substituted); |
| 1466 |
|
| 1467 |
r = context_write_data_static_hostname(c); |
| 1468 |
if (r < 0) { |
| 1469 |
log_error_errno(r, "Failed to write static hostname: %m"); |
| 1470 |
if (ERRNO_IS_PRIVILEGE(r)) |
| 1471 |
return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/hostname."); |
| 1472 |
if (r == -EROFS) |
| 1473 |
return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/hostname is in a read-only filesystem."); |
| 1474 |
return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %m"); |
| 1475 |
} |
| 1476 |
|
| 1477 |
r = context_update_kernel_hostname(c, /* transient_hostname= */ NULL); |
| 1478 |
if (r < 0) { |
| 1479 |
log_error_errno(r, "Failed to set hostname: %m"); |
| 1480 |
return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m"); |
| 1481 |
} |
| 1482 |
|
| 1483 |
(void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), |
| 1484 |
"/org/freedesktop/hostname1", "org.freedesktop.hostname1", |
| 1485 |
"StaticHostname", "Hostname", "HostnameSource", NULL); |
| 1486 |
|
| 1487 |
return sd_bus_reply_method_return(m, NULL); |
| 1488 |
} |
| 1489 |
|
| 1490 |
static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_message_handler_t cb, sd_bus_error *error) { |
| 1491 |
int interactive; |
| 1492 |
const char *name; |
| 1493 |
int r; |
| 1494 |
|
| 1495 |
assert(c); |
| 1496 |
assert(m); |
| 1497 |
|
| 1498 |
r = sd_bus_message_read(m, "sb", &name, &interactive); |
| 1499 |
if (r < 0) |
| 1500 |
return r; |
| 1501 |
|
| 1502 |
name = empty_to_null(name); |
| 1503 |
|
| 1504 |
context_read_machine_info(c); |
| 1505 |
|
| 1506 |
if (streq_ptr(name, c->data[prop])) |
| 1507 |
return sd_bus_reply_method_return(m, NULL); |
| 1508 |
|
| 1509 |
if (!isempty(name)) { |
| 1510 |
/* The icon name might ultimately be used as file |
| 1511 |
* name, so better be safe than sorry */ |
| 1512 |
|
| 1513 |
if (prop == PROP_ICON_NAME && !filename_is_valid(name)) |
| 1514 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name); |
| 1515 |
if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL)) |
| 1516 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty hostname '%s'", name); |
| 1517 |
if (prop == PROP_CHASSIS && !valid_chassis(name)) |
| 1518 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name); |
| 1519 |
if (prop == PROP_DEPLOYMENT && !valid_deployment(name)) |
| 1520 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name); |
| 1521 |
if (prop == PROP_LOCATION && string_has_cc(name, NULL)) |
| 1522 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name); |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* Since the pretty hostname should always be changed at the same time as the static one, use the |
| 1526 |
* same policy action for both... */ |
| 1527 |
|
| 1528 |
r = bus_verify_polkit_async_full( |
| 1529 |
m, |
| 1530 |
prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info", |
| 1531 |
/* details= */ NULL, |
| 1532 |
/* good_user= */ UID_INVALID, |
| 1533 |
interactive ? POLKIT_ALLOW_INTERACTIVE : 0, |
| 1534 |
&c->polkit_registry, |
| 1535 |
error); |
| 1536 |
if (r < 0) |
| 1537 |
return r; |
| 1538 |
if (r == 0) |
| 1539 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1540 |
|
| 1541 |
r = free_and_strdup_warn(&c->data[prop], name); |
| 1542 |
if (r < 0) |
| 1543 |
return r; |
| 1544 |
|
| 1545 |
r = context_write_data_machine_info(c); |
| 1546 |
if (r < 0) { |
| 1547 |
log_error_errno(r, "Failed to write machine info: %m"); |
| 1548 |
if (ERRNO_IS_PRIVILEGE(r)) |
| 1549 |
return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/machine-info."); |
| 1550 |
if (r == -EROFS) |
| 1551 |
return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/machine-info is in a read-only filesystem."); |
| 1552 |
return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %m"); |
| 1553 |
} |
| 1554 |
|
| 1555 |
log_info("Changed %s to '%s'", |
| 1556 |
prop == PROP_PRETTY_HOSTNAME ? "pretty hostname" : |
| 1557 |
prop == PROP_DEPLOYMENT ? "deployment" : |
| 1558 |
prop == PROP_LOCATION ? "location" : |
| 1559 |
prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop])); |
| 1560 |
|
| 1561 |
(void) sd_bus_emit_properties_changed( |
| 1562 |
sd_bus_message_get_bus(m), |
| 1563 |
"/org/freedesktop/hostname1", |
| 1564 |
"org.freedesktop.hostname1", |
| 1565 |
prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" : |
| 1566 |
prop == PROP_DEPLOYMENT ? "Deployment" : |
| 1567 |
prop == PROP_LOCATION ? "Location" : |
| 1568 |
prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL); |
| 1569 |
|
| 1570 |
return sd_bus_reply_method_return(m, NULL); |
| 1571 |
} |
| 1572 |
|
| 1573 |
static int method_set_pretty_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1574 |
return set_machine_info(userdata, m, PROP_PRETTY_HOSTNAME, method_set_pretty_hostname, error); |
| 1575 |
} |
| 1576 |
|
| 1577 |
static int method_set_icon_name(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1578 |
return set_machine_info(userdata, m, PROP_ICON_NAME, method_set_icon_name, error); |
| 1579 |
} |
| 1580 |
|
| 1581 |
static int method_set_chassis(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1582 |
return set_machine_info(userdata, m, PROP_CHASSIS, method_set_chassis, error); |
| 1583 |
} |
| 1584 |
|
| 1585 |
static int method_set_deployment(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1586 |
return set_machine_info(userdata, m, PROP_DEPLOYMENT, method_set_deployment, error); |
| 1587 |
} |
| 1588 |
|
| 1589 |
static int method_set_location(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1590 |
return set_machine_info(userdata, m, PROP_LOCATION, method_set_location, error); |
| 1591 |
} |
| 1592 |
|
| 1593 |
static int method_set_tags(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1594 |
Context *c = ASSERT_PTR(userdata); |
| 1595 |
int r; |
| 1596 |
|
| 1597 |
assert(m); |
| 1598 |
|
| 1599 |
_cleanup_strv_free_ char **tags = NULL; |
| 1600 |
r = sd_bus_message_read_strv(m, &tags); |
| 1601 |
if (r < 0) |
| 1602 |
return r; |
| 1603 |
|
| 1604 |
strv_sort_uniq(tags); |
| 1605 |
|
| 1606 |
if (strv_length(tags) > MACHINE_TAGS_MAX) |
| 1607 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Too many machine tags specified."); |
| 1608 |
|
| 1609 |
_cleanup_free_ char *j = strv_join(tags, ":"); |
| 1610 |
if (!j) |
| 1611 |
return log_oom(); |
| 1612 |
|
| 1613 |
if (!machine_tag_list_is_valid(tags)) |
| 1614 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid tags '%s'", j); |
| 1615 |
|
| 1616 |
context_read_machine_info(c); |
| 1617 |
|
| 1618 |
if (streq_ptr(empty_to_null(j), empty_to_null(c->data[PROP_TAGS]))) |
| 1619 |
return sd_bus_reply_method_return(m, NULL); |
| 1620 |
|
| 1621 |
r = bus_verify_polkit_async_full( |
| 1622 |
m, |
| 1623 |
"org.freedesktop.hostname1.set-machine-info", |
| 1624 |
/* details= */ NULL, |
| 1625 |
/* good_user= */ UID_INVALID, |
| 1626 |
/* flags= */ 0, |
| 1627 |
&c->polkit_registry, |
| 1628 |
error); |
| 1629 |
if (r < 0) |
| 1630 |
return r; |
| 1631 |
if (r == 0) |
| 1632 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1633 |
|
| 1634 |
if (strv_isempty(tags)) |
| 1635 |
c->data[PROP_TAGS] = mfree(c->data[PROP_TAGS]); |
| 1636 |
else |
| 1637 |
free_and_replace(c->data[PROP_TAGS], j); |
| 1638 |
|
| 1639 |
r = context_write_data_machine_info(c); |
| 1640 |
if (r < 0) { |
| 1641 |
log_error_errno(r, "Failed to write machine info: %m"); |
| 1642 |
if (ERRNO_IS_PRIVILEGE(r)) |
| 1643 |
return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/machine-info."); |
| 1644 |
if (r == -EROFS) |
| 1645 |
return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/machine-info is in a read-only filesystem."); |
| 1646 |
return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %m"); |
| 1647 |
} |
| 1648 |
|
| 1649 |
log_info("Changed tags to '%s'", strempty(c->data[PROP_TAGS])); |
| 1650 |
|
| 1651 |
(void) sd_bus_emit_properties_changed( |
| 1652 |
sd_bus_message_get_bus(m), |
| 1653 |
"/org/freedesktop/hostname1", |
| 1654 |
"org.freedesktop.hostname1", |
| 1655 |
"Tags", |
| 1656 |
NULL); |
| 1657 |
|
| 1658 |
return sd_bus_reply_method_return(m, NULL); |
| 1659 |
} |
| 1660 |
|
| 1661 |
static int method_get_product_uuid(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1662 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
| 1663 |
Context *c = ASSERT_PTR(userdata); |
| 1664 |
int interactive, r; |
| 1665 |
sd_id128_t uuid; |
| 1666 |
|
| 1667 |
assert(m); |
| 1668 |
|
| 1669 |
r = sd_bus_message_read(m, "b", &interactive); |
| 1670 |
if (r < 0) |
| 1671 |
return r; |
| 1672 |
|
| 1673 |
r = bus_verify_polkit_async_full( |
| 1674 |
m, |
| 1675 |
"org.freedesktop.hostname1.get-product-uuid", |
| 1676 |
/* details= */ NULL, |
| 1677 |
/* good_user= */ UID_INVALID, |
| 1678 |
interactive ? POLKIT_ALLOW_INTERACTIVE : 0, |
| 1679 |
&c->polkit_registry, |
| 1680 |
error); |
| 1681 |
if (r < 0) |
| 1682 |
return r; |
| 1683 |
if (r == 0) |
| 1684 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1685 |
|
| 1686 |
r = id128_get_product(&uuid); |
| 1687 |
if (r < 0) { |
| 1688 |
if (r == -EADDRNOTAVAIL) |
| 1689 |
log_debug_errno(r, "DMI product UUID is all 0x00 or all 0xFF, ignoring."); |
| 1690 |
else |
| 1691 |
log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, |
| 1692 |
"Failed to read product UUID, ignoring: %m"); |
| 1693 |
|
| 1694 |
return sd_bus_error_set(error, BUS_ERROR_NO_PRODUCT_UUID, |
| 1695 |
"Failed to read product UUID from firmware."); |
| 1696 |
} |
| 1697 |
|
| 1698 |
r = sd_bus_message_new_method_return(m, &reply); |
| 1699 |
if (r < 0) |
| 1700 |
return r; |
| 1701 |
|
| 1702 |
r = sd_bus_message_append_array(reply, 'y', uuid.bytes, sizeof(uuid.bytes)); |
| 1703 |
if (r < 0) |
| 1704 |
return r; |
| 1705 |
|
| 1706 |
return sd_bus_message_send(reply); |
| 1707 |
} |
| 1708 |
|
| 1709 |
static int method_get_hardware_serial(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1710 |
_cleanup_free_ char *serial = NULL; |
| 1711 |
Context *c = ASSERT_PTR(userdata); |
| 1712 |
int r; |
| 1713 |
|
| 1714 |
assert(m); |
| 1715 |
|
| 1716 |
r = bus_verify_polkit_async( |
| 1717 |
m, |
| 1718 |
"org.freedesktop.hostname1.get-hardware-serial", |
| 1719 |
/* details= */ NULL, |
| 1720 |
&c->polkit_registry, |
| 1721 |
error); |
| 1722 |
if (r < 0) |
| 1723 |
return r; |
| 1724 |
if (r == 0) |
| 1725 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1726 |
|
| 1727 |
r = get_hardware_serial(c, &serial); |
| 1728 |
if (r < 0) |
| 1729 |
return sd_bus_error_set(error, BUS_ERROR_NO_HARDWARE_SERIAL, |
| 1730 |
"Failed to read hardware serial from firmware."); |
| 1731 |
|
| 1732 |
return sd_bus_reply_method_return(m, "s", serial); |
| 1733 |
} |
| 1734 |
|
| 1735 |
static int method_get_machine_info(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1736 |
static const struct { |
| 1737 |
const char *name; |
| 1738 |
HostProperty prop; |
| 1739 |
} field_table[] = { |
| 1740 |
{ "PRETTY_HOSTNAME", PROP_PRETTY_HOSTNAME }, |
| 1741 |
{ "ICON_NAME", PROP_ICON_NAME }, |
| 1742 |
{ "CHASSIS", PROP_CHASSIS }, |
| 1743 |
{ "DEPLOYMENT", PROP_DEPLOYMENT }, |
| 1744 |
{ "LOCATION", PROP_LOCATION }, |
| 1745 |
{ "TAGS", PROP_TAGS }, |
| 1746 |
{ "HARDWARE_VENDOR", PROP_HARDWARE_VENDOR }, |
| 1747 |
{ "HARDWARE_MODEL", PROP_HARDWARE_MODEL }, |
| 1748 |
{ "HARDWARE_SKU", PROP_HARDWARE_SKU }, |
| 1749 |
{ "HARDWARE_VERSION", PROP_HARDWARE_VERSION }, |
| 1750 |
}; |
| 1751 |
|
| 1752 |
Context *c = ASSERT_PTR(userdata); |
| 1753 |
const char *field; |
| 1754 |
int r; |
| 1755 |
|
| 1756 |
assert(m); |
| 1757 |
|
| 1758 |
r = sd_bus_message_read(m, "s", &field); |
| 1759 |
if (r < 0) |
| 1760 |
return r; |
| 1761 |
|
| 1762 |
if (isempty(field)) |
| 1763 |
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Field name must not be empty."); |
| 1764 |
|
| 1765 |
if (!env_name_is_valid(field)) |
| 1766 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid field name '%s'.", field); |
| 1767 |
|
| 1768 |
FOREACH_ELEMENT(e, field_table) |
| 1769 |
if (streq(field, e->name)) { |
| 1770 |
/* For fields that are also exposed as D-Bus properties, use the same Context cache as the |
| 1771 |
* property getters. Note that this returns the raw /etc/machine-info value only: property-level |
| 1772 |
* fallback logic (e.g. DMI/chassis-based synthesis) is not applied here. For custom/unknown |
| 1773 |
* fields, fall back to reading the file directly. */ |
| 1774 |
context_read_machine_info(c); |
| 1775 |
|
| 1776 |
if (isempty(c->data[e->prop])) |
| 1777 |
return sd_bus_error_setf(error, BUS_ERROR_FIELD_NOT_SET, "Field '%s' is not set or empty in /etc/machine-info.", field); |
| 1778 |
|
| 1779 |
return sd_bus_reply_method_return(m, "s", c->data[e->prop]); |
| 1780 |
} |
| 1781 |
|
| 1782 |
_cleanup_free_ char *value = NULL; |
| 1783 |
|
| 1784 |
r = parse_env_file(NULL, etc_machine_info(), |
| 1785 |
field, &value); |
| 1786 |
if (r < 0 && r != -ENOENT) |
| 1787 |
return sd_bus_error_set_errnof(error, r, "Failed to read /etc/machine-info: %m"); |
| 1788 |
|
| 1789 |
if (isempty(value)) |
| 1790 |
return sd_bus_error_setf(error, BUS_ERROR_FIELD_NOT_SET, "Field '%s' is not set or empty in /etc/machine-info.", field); |
| 1791 |
|
| 1792 |
return sd_bus_reply_method_return(m, "s", value); |
| 1793 |
} |
| 1794 |
|
| 1795 |
static int build_describe_response(Context *c, bool privileged, sd_json_variant **ret) { |
| 1796 |
_cleanup_free_ char *hn = NULL, *dhn = NULL, *in = NULL, |
| 1797 |
*chassis = NULL, *vendor = NULL, *model = NULL, *serial = NULL, *firmware_version = NULL, |
| 1798 |
*firmware_vendor = NULL, *chassis_asset_tag = NULL, *sku = NULL, *hardware_version = NULL; |
| 1799 |
_cleanup_strv_free_ char **os_release_pairs = NULL, **machine_info_pairs = NULL; |
| 1800 |
usec_t firmware_date = USEC_INFINITY, eol = USEC_INFINITY; |
| 1801 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1802 |
sd_id128_t machine_id, boot_id, product_uuid = SD_ID128_NULL; |
| 1803 |
unsigned local_cid = VMADDR_CID_ANY; |
| 1804 |
struct utsname u; |
| 1805 |
int r; |
| 1806 |
|
| 1807 |
assert(c); |
| 1808 |
assert(ret); |
| 1809 |
|
| 1810 |
context_read_etc_hostname(c); |
| 1811 |
context_read_machine_info(c); |
| 1812 |
context_read_os_release(c); |
| 1813 |
context_determine_hostname_source(c); |
| 1814 |
|
| 1815 |
dhn = get_default_hostname(); |
| 1816 |
if (!dhn) |
| 1817 |
return log_oom(); |
| 1818 |
|
| 1819 |
r = gethostname_strict(&hn); |
| 1820 |
if (r < 0 && r != -ENXIO) |
| 1821 |
return log_error_errno(r, "Failed to read local host name: %m"); |
| 1822 |
|
| 1823 |
if (isempty(c->data[PROP_ICON_NAME])) |
| 1824 |
in = context_fallback_icon_name(c); |
| 1825 |
|
| 1826 |
chassis = context_get_chassis(c); |
| 1827 |
|
| 1828 |
assert_se(uname(&u) >= 0); |
| 1829 |
|
| 1830 |
if (isempty(c->data[PROP_HARDWARE_VENDOR])) |
| 1831 |
(void) get_hardware_vendor(c, &vendor); |
| 1832 |
if (isempty(c->data[PROP_HARDWARE_MODEL])) |
| 1833 |
(void) get_hardware_model(c, &model); |
| 1834 |
if (isempty(c->data[PROP_HARDWARE_SKU])) |
| 1835 |
(void) get_hardware_sku(c, &sku); |
| 1836 |
if (isempty(c->data[PROP_HARDWARE_VERSION])) |
| 1837 |
(void) get_hardware_version(c, &hardware_version); |
| 1838 |
|
| 1839 |
if (privileged) { |
| 1840 |
/* The product UUID and hardware serial is only available to privileged clients */ |
| 1841 |
(void) id128_get_product(&product_uuid); |
| 1842 |
(void) get_hardware_serial(c, &serial); |
| 1843 |
} |
| 1844 |
(void) get_firmware_version(c, &firmware_version); |
| 1845 |
(void) get_firmware_vendor(c, &firmware_vendor); |
| 1846 |
(void) get_firmware_date(c, &firmware_date); |
| 1847 |
(void) get_chassis_asset_tag(c, &chassis_asset_tag); |
| 1848 |
|
| 1849 |
if (c->data[PROP_OS_SUPPORT_END]) |
| 1850 |
(void) os_release_support_ended(c->data[PROP_OS_SUPPORT_END], /* quiet= */ false, &eol); |
| 1851 |
|
| 1852 |
r = sd_id128_get_machine(&machine_id); |
| 1853 |
if (r < 0) |
| 1854 |
return log_error_errno(r, "Failed to get machine ID: %m"); |
| 1855 |
|
| 1856 |
r = sd_id128_get_boot(&boot_id); |
| 1857 |
if (r < 0) |
| 1858 |
return log_error_errno(r, "Failed to get boot ID: %m"); |
| 1859 |
|
| 1860 |
(void) vsock_get_local_cid(&local_cid); |
| 1861 |
|
| 1862 |
(void) load_os_release_pairs(/* root= */ NULL, &os_release_pairs); |
| 1863 |
(void) load_env_file_pairs(/* f= */ NULL, etc_machine_info(), &machine_info_pairs); |
| 1864 |
|
| 1865 |
r = sd_json_buildo( |
| 1866 |
&v, |
| 1867 |
SD_JSON_BUILD_PAIR_STRING("Hostname", hn ?: dhn), |
| 1868 |
SD_JSON_BUILD_PAIR_STRING("StaticHostname", c->data[PROP_STATIC_HOSTNAME_SUBSTITUTED_WILDCARDS]), |
| 1869 |
SD_JSON_BUILD_PAIR_STRING("PrettyHostname", c->data[PROP_PRETTY_HOSTNAME]), |
| 1870 |
SD_JSON_BUILD_PAIR_STRING("DefaultHostname", dhn), |
| 1871 |
SD_JSON_BUILD_PAIR_STRING("HostnameSource", hostname_source_to_string(c->hostname_source)), |
| 1872 |
SD_JSON_BUILD_PAIR_STRING("IconName", in ?: c->data[PROP_ICON_NAME]), |
| 1873 |
SD_JSON_BUILD_PAIR_STRING("Chassis", chassis), |
| 1874 |
SD_JSON_BUILD_PAIR_STRING("ChassisAssetTag", chassis_asset_tag), |
| 1875 |
SD_JSON_BUILD_PAIR_STRING("Deployment", c->data[PROP_DEPLOYMENT]), |
| 1876 |
SD_JSON_BUILD_PAIR_STRING("Location", c->data[PROP_LOCATION]), |
| 1877 |
SD_JSON_BUILD_PAIR_STRING("KernelName", u.sysname), |
| 1878 |
SD_JSON_BUILD_PAIR_STRING("KernelRelease", u.release), |
| 1879 |
SD_JSON_BUILD_PAIR_STRING("KernelVersion", u.version), |
| 1880 |
SD_JSON_BUILD_PAIR_STRING("OperatingSystemPrettyName", c->data[PROP_OS_PRETTY_NAME]), |
| 1881 |
SD_JSON_BUILD_PAIR_STRING("OperatingSystemFancyName", c->data[PROP_OS_FANCY_NAME]), |
| 1882 |
SD_JSON_BUILD_PAIR_STRING("OperatingSystemCPEName", c->data[PROP_OS_CPE_NAME]), |
| 1883 |
SD_JSON_BUILD_PAIR_STRING("OperatingSystemHomeURL", c->data[PROP_OS_HOME_URL]), |
| 1884 |
JSON_BUILD_PAIR_FINITE_USEC("OperatingSystemSupportEnd", eol), |
| 1885 |
SD_JSON_BUILD_PAIR("OperatingSystemReleaseData", JSON_BUILD_STRV_ENV_PAIR(os_release_pairs)), |
| 1886 |
SD_JSON_BUILD_PAIR_STRING("OperatingSystemImageID", c->data[PROP_OS_IMAGE_ID]), |
| 1887 |
SD_JSON_BUILD_PAIR_STRING("OperatingSystemImageVersion", c->data[PROP_OS_IMAGE_VERSION]), |
| 1888 |
SD_JSON_BUILD_PAIR("MachineInformationData", JSON_BUILD_STRV_ENV_PAIR(machine_info_pairs)), |
| 1889 |
SD_JSON_BUILD_PAIR_STRING("HardwareVendor", vendor ?: c->data[PROP_HARDWARE_VENDOR]), |
| 1890 |
SD_JSON_BUILD_PAIR_STRING("HardwareModel", model ?: c->data[PROP_HARDWARE_MODEL]), |
| 1891 |
SD_JSON_BUILD_PAIR_STRING("HardwareSerial", serial), |
| 1892 |
SD_JSON_BUILD_PAIR_STRING("HardwareSKU", sku ?: c->data[PROP_HARDWARE_SKU]), |
| 1893 |
SD_JSON_BUILD_PAIR_STRING("HardwareVersion", hardware_version ?: c->data[PROP_HARDWARE_VERSION]), |
| 1894 |
SD_JSON_BUILD_PAIR_STRING("FirmwareVersion", firmware_version), |
| 1895 |
SD_JSON_BUILD_PAIR_STRING("FirmwareVendor", firmware_vendor), |
| 1896 |
JSON_BUILD_PAIR_FINITE_USEC("FirmwareDate", firmware_date), |
| 1897 |
SD_JSON_BUILD_PAIR_ID128("MachineID", machine_id), |
| 1898 |
SD_JSON_BUILD_PAIR_ID128("BootID", boot_id), |
| 1899 |
SD_JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid), "ProductUUID", SD_JSON_BUILD_ID128(product_uuid)), |
| 1900 |
SD_JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid), "ProductUUID", SD_JSON_BUILD_NULL), |
| 1901 |
SD_JSON_BUILD_PAIR_CONDITION(local_cid != VMADDR_CID_ANY, "VSockCID", SD_JSON_BUILD_UNSIGNED(local_cid)), |
| 1902 |
SD_JSON_BUILD_PAIR_CONDITION(local_cid == VMADDR_CID_ANY, "VSockCID", SD_JSON_BUILD_NULL)); |
| 1903 |
if (r < 0) |
| 1904 |
return log_error_errno(r, "Failed to build JSON data: %m"); |
| 1905 |
|
| 1906 |
*ret = TAKE_PTR(v); |
| 1907 |
return 0; |
| 1908 |
} |
| 1909 |
|
| 1910 |
static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
| 1911 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1912 |
Context *c = ASSERT_PTR(userdata); |
| 1913 |
_cleanup_free_ char *text = NULL; |
| 1914 |
bool privileged; |
| 1915 |
int r; |
| 1916 |
|
| 1917 |
assert(m); |
| 1918 |
|
| 1919 |
r = bus_verify_polkit_async( |
| 1920 |
m, |
| 1921 |
"org.freedesktop.hostname1.get-description", |
| 1922 |
/* details= */ NULL, |
| 1923 |
&c->polkit_registry, |
| 1924 |
error); |
| 1925 |
if (r == 0) |
| 1926 |
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 1927 |
|
| 1928 |
/* We ignore all authentication errors here, since most data is unprivileged, the one exception being |
| 1929 |
* the product ID which we'll check explicitly. */ |
| 1930 |
privileged = r > 0; |
| 1931 |
|
| 1932 |
r = build_describe_response(c, privileged, &v); |
| 1933 |
if (r < 0) |
| 1934 |
return r; |
| 1935 |
|
| 1936 |
r = sd_json_variant_format(v, 0, &text); |
| 1937 |
if (r < 0) |
| 1938 |
return log_error_errno(r, "Failed to format JSON data: %m"); |
| 1939 |
|
| 1940 |
return sd_bus_reply_method_return(m, "s", text); |
| 1941 |
} |
| 1942 |
|
| 1943 |
static const sd_bus_vtable hostname_vtable[] = { |
| 1944 |
SD_BUS_VTABLE_START(0), |
| 1945 |
SD_BUS_PROPERTY("Hostname", "s", property_get_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1946 |
SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1947 |
SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field, offsetof(Context, data[PROP_PRETTY_HOSTNAME]), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1948 |
SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1949 |
SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1950 |
SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1951 |
SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1952 |
SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field, offsetof(Context, data[PROP_DEPLOYMENT]), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1953 |
SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field, offsetof(Context, data[PROP_LOCATION]), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1954 |
SD_BUS_PROPERTY("Tags", "as", property_get_tags, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 1955 |
SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field, offsetof(struct utsname, sysname), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST), |
| 1956 |
SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field, offsetof(struct utsname, release), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST), |
| 1957 |
SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field, offsetof(struct utsname, version), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST), |
| 1958 |
SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_PRETTY_NAME]), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1959 |
SD_BUS_PROPERTY("OperatingSystemFancyName", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_FANCY_NAME]), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1960 |
SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_CPE_NAME]), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1961 |
SD_BUS_PROPERTY("OperatingSystemSupportEnd", "t", property_get_os_support_end, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1962 |
SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_HOME_URL]), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1963 |
SD_BUS_PROPERTY("OperatingSystemImageID", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_IMAGE_ID]), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1964 |
SD_BUS_PROPERTY("OperatingSystemImageVersion", "s", property_get_os_release_field, offsetof(Context, data[PROP_OS_IMAGE_VERSION]), SD_BUS_VTABLE_PROPERTY_CONST), |
| 1965 |
SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1966 |
SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1967 |
SD_BUS_PROPERTY("HardwareSKU", "s", property_get_hardware_sku, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1968 |
SD_BUS_PROPERTY("HardwareVersion", "s", property_get_hardware_version, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1969 |
SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1970 |
SD_BUS_PROPERTY("FirmwareVendor", "s", property_get_firmware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1971 |
SD_BUS_PROPERTY("FirmwareDate", "t", property_get_firmware_date, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1972 |
SD_BUS_PROPERTY("MachineID", "ay", property_get_machine_id, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1973 |
SD_BUS_PROPERTY("BootID", "ay", property_get_boot_id, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1974 |
SD_BUS_PROPERTY("VSockCID", "u", property_get_vsock_cid, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1975 |
SD_BUS_PROPERTY("ChassisAssetTag", "s", property_get_chassis_asset_tag, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 1976 |
|
| 1977 |
SD_BUS_METHOD_WITH_ARGS("SetHostname", |
| 1978 |
SD_BUS_ARGS("s", hostname, "b", interactive), |
| 1979 |
SD_BUS_NO_RESULT, |
| 1980 |
method_set_hostname, |
| 1981 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1982 |
SD_BUS_METHOD_WITH_ARGS("SetStaticHostname", |
| 1983 |
SD_BUS_ARGS("s", hostname, "b", interactive), |
| 1984 |
SD_BUS_NO_RESULT, |
| 1985 |
method_set_static_hostname, |
| 1986 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1987 |
SD_BUS_METHOD_WITH_ARGS("SetPrettyHostname", |
| 1988 |
SD_BUS_ARGS("s", hostname, "b", interactive), |
| 1989 |
SD_BUS_NO_RESULT, |
| 1990 |
method_set_pretty_hostname, |
| 1991 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1992 |
SD_BUS_METHOD_WITH_ARGS("SetIconName", |
| 1993 |
SD_BUS_ARGS("s", icon, "b", interactive), |
| 1994 |
SD_BUS_NO_RESULT, |
| 1995 |
method_set_icon_name, |
| 1996 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 1997 |
SD_BUS_METHOD_WITH_ARGS("SetChassis", |
| 1998 |
SD_BUS_ARGS("s", chassis, "b", interactive), |
| 1999 |
SD_BUS_NO_RESULT, |
| 2000 |
method_set_chassis, |
| 2001 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2002 |
SD_BUS_METHOD_WITH_ARGS("SetDeployment", |
| 2003 |
SD_BUS_ARGS("s", deployment, "b", interactive), |
| 2004 |
SD_BUS_NO_RESULT, |
| 2005 |
method_set_deployment, |
| 2006 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2007 |
SD_BUS_METHOD_WITH_ARGS("SetLocation", |
| 2008 |
SD_BUS_ARGS("s", location, "b", interactive), |
| 2009 |
SD_BUS_NO_RESULT, |
| 2010 |
method_set_location, |
| 2011 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2012 |
SD_BUS_METHOD_WITH_ARGS("SetTags", |
| 2013 |
SD_BUS_ARGS("as", tags), |
| 2014 |
SD_BUS_NO_RESULT, |
| 2015 |
method_set_tags, |
| 2016 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2017 |
SD_BUS_METHOD_WITH_ARGS("GetProductUUID", |
| 2018 |
SD_BUS_ARGS("b", interactive), |
| 2019 |
SD_BUS_RESULT("ay", uuid), |
| 2020 |
method_get_product_uuid, |
| 2021 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2022 |
SD_BUS_METHOD_WITH_ARGS("GetHardwareSerial", |
| 2023 |
SD_BUS_NO_ARGS, |
| 2024 |
SD_BUS_RESULT("s", serial), |
| 2025 |
method_get_hardware_serial, |
| 2026 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2027 |
SD_BUS_METHOD_WITH_ARGS("Describe", |
| 2028 |
SD_BUS_NO_ARGS, |
| 2029 |
SD_BUS_RESULT("s", json), |
| 2030 |
method_describe, |
| 2031 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2032 |
SD_BUS_METHOD_WITH_ARGS("GetMachineInfo", |
| 2033 |
SD_BUS_ARGS("s", field), |
| 2034 |
SD_BUS_RESULT("s", value), |
| 2035 |
method_get_machine_info, |
| 2036 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 2037 |
|
| 2038 |
SD_BUS_VTABLE_END, |
| 2039 |
}; |
| 2040 |
|
| 2041 |
static const BusObjectImplementation manager_object = { |
| 2042 |
"/org/freedesktop/hostname1", |
| 2043 |
"org.freedesktop.hostname1", |
| 2044 |
.vtables = BUS_VTABLES(hostname_vtable), |
| 2045 |
}; |
| 2046 |
|
| 2047 |
static int connect_bus(Context *c) { |
| 2048 |
int r; |
| 2049 |
|
| 2050 |
assert(c); |
| 2051 |
assert(c->event); |
| 2052 |
assert(!c->bus); |
| 2053 |
|
| 2054 |
r = bus_open_system_watch_bind_with_description(&c->bus, "bus-api-hostname"); |
| 2055 |
if (r < 0) |
| 2056 |
return log_error_errno(r, "Failed to get system bus connection: %m"); |
| 2057 |
|
| 2058 |
r = bus_add_implementation(c->bus, &manager_object, c); |
| 2059 |
if (r < 0) |
| 2060 |
return r; |
| 2061 |
|
| 2062 |
r = bus_log_control_api_register(c->bus); |
| 2063 |
if (r < 0) |
| 2064 |
return r; |
| 2065 |
|
| 2066 |
r = sd_bus_request_name_async(c->bus, NULL, "org.freedesktop.hostname1", 0, NULL, NULL); |
| 2067 |
if (r < 0) |
| 2068 |
return log_error_errno(r, "Failed to request name: %m"); |
| 2069 |
|
| 2070 |
r = sd_bus_attach_event(c->bus, c->event, 0); |
| 2071 |
if (r < 0) |
| 2072 |
return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
| 2073 |
|
| 2074 |
return 0; |
| 2075 |
} |
| 2076 |
|
| 2077 |
static int vl_method_describe(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 2078 |
Context *c = ASSERT_PTR(userdata); |
| 2079 |
bool privileged; |
| 2080 |
int r; |
| 2081 |
|
| 2082 |
assert(link); |
| 2083 |
assert(parameters); |
| 2084 |
|
| 2085 |
r = sd_varlink_dispatch(link, parameters, dispatch_table_polkit_only, /* userdata= */ NULL); |
| 2086 |
if (r != 0) |
| 2087 |
return r; |
| 2088 |
|
| 2089 |
r = varlink_verify_polkit_async_full( |
| 2090 |
link, |
| 2091 |
c->bus, |
| 2092 |
"org.freedesktop.hostname1.get-hardware-serial", |
| 2093 |
/* details= */ NULL, |
| 2094 |
UID_INVALID, |
| 2095 |
POLKIT_DONT_REPLY, |
| 2096 |
&c->polkit_registry); |
| 2097 |
if (r == 0) |
| 2098 |
return 0; /* No authorization for now, but the async polkit stuff will call us again when it has it */ |
| 2099 |
|
| 2100 |
/* We ignore all authentication errors here, since most data is unprivileged, the one exception being |
| 2101 |
* the product ID which we'll check explicitly. */ |
| 2102 |
privileged = r > 0; |
| 2103 |
|
| 2104 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 2105 |
r = build_describe_response(c, privileged, &v); |
| 2106 |
if (r < 0) |
| 2107 |
return r; |
| 2108 |
|
| 2109 |
return sd_varlink_reply(link, v); |
| 2110 |
} |
| 2111 |
|
| 2112 |
static int connect_varlink(Context *c) { |
| 2113 |
int r; |
| 2114 |
|
| 2115 |
assert(c); |
| 2116 |
assert(c->event); |
| 2117 |
assert(!c->varlink_server); |
| 2118 |
|
| 2119 |
r = varlink_server_new( |
| 2120 |
&c->varlink_server, |
| 2121 |
SD_VARLINK_SERVER_ACCOUNT_UID|SD_VARLINK_SERVER_INHERIT_USERDATA, |
| 2122 |
c); |
| 2123 |
if (r < 0) |
| 2124 |
return log_error_errno(r, "Failed to allocate Varlink server: %m"); |
| 2125 |
|
| 2126 |
r = sd_varlink_server_add_interface_many( |
| 2127 |
c->varlink_server, |
| 2128 |
&vl_interface_io_systemd_Hostname, |
| 2129 |
&vl_interface_io_systemd_service); |
| 2130 |
if (r < 0) |
| 2131 |
return log_error_errno(r, "Failed to add Hostname interface to Varlink server: %m"); |
| 2132 |
|
| 2133 |
r = sd_varlink_server_bind_method_many( |
| 2134 |
c->varlink_server, |
| 2135 |
"io.systemd.Hostname.Describe", vl_method_describe, |
| 2136 |
"io.systemd.service.Ping", varlink_method_ping, |
| 2137 |
"io.systemd.service.SetLogLevel", varlink_method_set_log_level, |
| 2138 |
"io.systemd.service.GetEnvironment", varlink_method_get_environment); |
| 2139 |
if (r < 0) |
| 2140 |
return log_error_errno(r, "Failed to bind Varlink method calls: %m"); |
| 2141 |
|
| 2142 |
r = sd_varlink_server_attach_event(c->varlink_server, c->event, SD_EVENT_PRIORITY_NORMAL); |
| 2143 |
if (r < 0) |
| 2144 |
return log_error_errno(r, "Failed to attach Varlink server to event loop: %m"); |
| 2145 |
|
| 2146 |
r = sd_varlink_server_listen_auto(c->varlink_server); |
| 2147 |
if (r < 0) |
| 2148 |
return log_error_errno(r, "Failed to bind to passed Varlink sockets: %m"); |
| 2149 |
if (r == 0) { |
| 2150 |
r = sd_varlink_server_listen_address(c->varlink_server, "/run/systemd/io.systemd.Hostname", 0666); |
| 2151 |
if (r < 0) |
| 2152 |
return log_error_errno(r, "Failed to bind to Varlink socket: %m"); |
| 2153 |
} |
| 2154 |
|
| 2155 |
return 0; |
| 2156 |
} |
| 2157 |
|
| 2158 |
static bool context_check_idle(void *userdata) { |
| 2159 |
Context *c = ASSERT_PTR(userdata); |
| 2160 |
|
| 2161 |
return sd_varlink_server_current_connections(c->varlink_server) == 0 && |
| 2162 |
hashmap_isempty(c->polkit_registry); |
| 2163 |
} |
| 2164 |
|
| 2165 |
static int run(int argc, char *argv[]) { |
| 2166 |
_cleanup_(context_destroy) Context context = { |
| 2167 |
.hostname_source = _HOSTNAME_INVALID, /* appropriate value will be set later */ |
| 2168 |
}; |
| 2169 |
int r; |
| 2170 |
|
| 2171 |
log_setup(); |
| 2172 |
|
| 2173 |
r = service_parse_argv("systemd-hostnamed.service", |
| 2174 |
"Manage the system hostname and related metadata.", |
| 2175 |
BUS_IMPLEMENTATIONS(&manager_object, |
| 2176 |
&log_control_object), |
| 2177 |
/* runtime_scope= */ NULL, |
| 2178 |
argc, argv); |
| 2179 |
if (r <= 0) |
| 2180 |
return r; |
| 2181 |
|
| 2182 |
umask(0022); |
| 2183 |
|
| 2184 |
r = mac_init(); |
| 2185 |
if (r < 0) |
| 2186 |
return r; |
| 2187 |
|
| 2188 |
r = context_acquire_dmi_device(&context); |
| 2189 |
if (r < 0) |
| 2190 |
return r; |
| 2191 |
|
| 2192 |
r = context_acquire_acpi_device(&context); |
| 2193 |
if (r < 0) |
| 2194 |
return r; |
| 2195 |
|
| 2196 |
r = context_acquire_device_tree(&context); |
| 2197 |
if (r < 0) |
| 2198 |
return r; |
| 2199 |
|
| 2200 |
r = sd_event_default(&context.event); |
| 2201 |
if (r < 0) |
| 2202 |
return log_error_errno(r, "Failed to allocate event loop: %m"); |
| 2203 |
|
| 2204 |
(void) sd_event_set_watchdog(context.event, true); |
| 2205 |
|
| 2206 |
r = sd_event_set_signal_exit(context.event, true); |
| 2207 |
if (r < 0) |
| 2208 |
return log_error_errno(r, "Failed to install SIGINT/SIGTERM handlers: %m"); |
| 2209 |
|
| 2210 |
r = connect_bus(&context); |
| 2211 |
if (r < 0) |
| 2212 |
return r; |
| 2213 |
|
| 2214 |
r = connect_varlink(&context); |
| 2215 |
if (r < 0) |
| 2216 |
return r; |
| 2217 |
|
| 2218 |
r = sd_notify(false, NOTIFY_READY_MESSAGE); |
| 2219 |
if (r < 0) |
| 2220 |
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m"); |
| 2221 |
|
| 2222 |
r = bus_event_loop_with_idle( |
| 2223 |
context.event, |
| 2224 |
context.bus, |
| 2225 |
"org.freedesktop.hostname1", |
| 2226 |
DEFAULT_EXIT_USEC, |
| 2227 |
context_check_idle, |
| 2228 |
&context); |
| 2229 |
if (r < 0) |
| 2230 |
return log_error_errno(r, "Failed to run event loop: %m"); |
| 2231 |
|
| 2232 |
return 0; |
| 2233 |
} |
| 2234 |
|
| 2235 |
DEFINE_MAIN_FUNCTION(run); |