]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hostname/hostnamed.c
Merge pull request #23126 from keszybz/clone3-prohibit
[thirdparty/systemd.git] / src / hostname / hostnamed.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <sys/utsname.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8
9 #include "alloc-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-get-properties.h"
12 #include "bus-log-control-api.h"
13 #include "bus-polkit.h"
14 #include "def.h"
15 #include "env-file-label.h"
16 #include "env-file.h"
17 #include "env-util.h"
18 #include "fileio-label.h"
19 #include "fileio.h"
20 #include "hostname-setup.h"
21 #include "hostname-util.h"
22 #include "id128-util.h"
23 #include "json.h"
24 #include "main-func.h"
25 #include "missing_capability.h"
26 #include "nscd-flush.h"
27 #include "nulstr-util.h"
28 #include "os-util.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "sd-device.h"
32 #include "selinux-util.h"
33 #include "service-util.h"
34 #include "signal-util.h"
35 #include "stat-util.h"
36 #include "string-table.h"
37 #include "strv.h"
38 #include "user-util.h"
39 #include "util.h"
40 #include "virt.h"
41
42 #define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:")
43
44 /* Properties we cache are indexed by an enum, to make invalidation easy and systematic (as we can iterate
45 * through them all, and they are uniformly strings). */
46 typedef enum {
47 /* Read from /etc/hostname */
48 PROP_STATIC_HOSTNAME,
49
50 /* Read from /etc/machine-info */
51 PROP_PRETTY_HOSTNAME,
52 PROP_ICON_NAME,
53 PROP_CHASSIS,
54 PROP_DEPLOYMENT,
55 PROP_LOCATION,
56 PROP_HARDWARE_VENDOR,
57 PROP_HARDWARE_MODEL,
58
59 /* Read from /etc/os-release (or /usr/lib/os-release) */
60 PROP_OS_PRETTY_NAME,
61 PROP_OS_CPE_NAME,
62 PROP_OS_HOME_URL,
63 _PROP_MAX,
64 _PROP_INVALID = -EINVAL,
65 } HostProperty;
66
67 typedef struct Context {
68 char *data[_PROP_MAX];
69
70 HostnameSource hostname_source;
71
72 struct stat etc_hostname_stat;
73 struct stat etc_os_release_stat;
74 struct stat etc_machine_info_stat;
75
76 Hashmap *polkit_registry;
77 } Context;
78
79 static void context_reset(Context *c, uint64_t mask) {
80 assert(c);
81
82 for (int p = 0; p < _PROP_MAX; p++) {
83 if (!FLAGS_SET(mask, UINT64_C(1) << p))
84 continue;
85
86 c->data[p] = mfree(c->data[p]);
87 }
88 }
89
90 static void context_destroy(Context *c) {
91 assert(c);
92
93 context_reset(c, UINT64_MAX);
94 bus_verify_polkit_async_registry_free(c->polkit_registry);
95 }
96
97 static void context_read_etc_hostname(Context *c) {
98 struct stat current_stat = {};
99 int r;
100
101 assert(c);
102
103 if (stat("/etc/hostname", &current_stat) >= 0 &&
104 stat_inode_unmodified(&c->etc_hostname_stat, &current_stat))
105 return;
106
107 context_reset(c, UINT64_C(1) << PROP_STATIC_HOSTNAME);
108
109 r = read_etc_hostname(NULL, &c->data[PROP_STATIC_HOSTNAME]);
110 if (r < 0 && r != -ENOENT)
111 log_warning_errno(r, "Failed to read /etc/hostname, ignoring: %m");
112
113 c->etc_hostname_stat = current_stat;
114 }
115
116 static void context_read_machine_info(Context *c) {
117 struct stat current_stat = {};
118 int r;
119
120 assert(c);
121
122 if (stat("/etc/machine-info", &current_stat) >= 0 &&
123 stat_inode_unmodified(&c->etc_machine_info_stat, &current_stat))
124 return;
125
126 context_reset(c,
127 (UINT64_C(1) << PROP_PRETTY_HOSTNAME) |
128 (UINT64_C(1) << PROP_ICON_NAME) |
129 (UINT64_C(1) << PROP_CHASSIS) |
130 (UINT64_C(1) << PROP_DEPLOYMENT) |
131 (UINT64_C(1) << PROP_LOCATION) |
132 (UINT64_C(1) << PROP_HARDWARE_VENDOR) |
133 (UINT64_C(1) << PROP_HARDWARE_MODEL));
134
135 r = parse_env_file(NULL, "/etc/machine-info",
136 "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
137 "ICON_NAME", &c->data[PROP_ICON_NAME],
138 "CHASSIS", &c->data[PROP_CHASSIS],
139 "DEPLOYMENT", &c->data[PROP_DEPLOYMENT],
140 "LOCATION", &c->data[PROP_LOCATION],
141 "HARDWARE_VENDOR", &c->data[PROP_HARDWARE_VENDOR],
142 "HARDWARE_MODEL", &c->data[PROP_HARDWARE_MODEL]);
143 if (r < 0 && r != -ENOENT)
144 log_warning_errno(r, "Failed to read /etc/machine-info, ignoring: %m");
145
146 c->etc_machine_info_stat = current_stat;
147 }
148
149 static void context_read_os_release(Context *c) {
150 struct stat current_stat = {};
151 int r;
152
153 assert(c);
154
155 if ((stat("/etc/os-release", &current_stat) >= 0 ||
156 stat("/usr/lib/os-release", &current_stat) >= 0) &&
157 stat_inode_unmodified(&c->etc_os_release_stat, &current_stat))
158 return;
159
160 context_reset(c,
161 (UINT64_C(1) << PROP_OS_PRETTY_NAME) |
162 (UINT64_C(1) << PROP_OS_CPE_NAME) |
163 (UINT64_C(1) << PROP_OS_HOME_URL));
164
165 r = parse_os_release(NULL,
166 "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME],
167 "CPE_NAME", &c->data[PROP_OS_CPE_NAME],
168 "HOME_URL", &c->data[PROP_OS_HOME_URL]);
169 if (r < 0 && r != -ENOENT)
170 log_warning_errno(r, "Failed to read os-release file, ignoring: %m");
171
172 c->etc_os_release_stat = current_stat;
173 }
174
175 static int get_dmi_data(const char *database_key, const char *regular_key, char **ret) {
176 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
177 _cleanup_free_ char *b = NULL;
178 const char *s = NULL;
179 int r;
180
181 r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
182 if (r < 0)
183 return log_debug_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
184
185 if (database_key)
186 (void) sd_device_get_property_value(device, database_key, &s);
187 if (!s && regular_key)
188 (void) sd_device_get_property_value(device, regular_key, &s);
189
190 if (!ret)
191 return !!s;
192
193 if (s) {
194 b = strdup(s);
195 if (!b)
196 return -ENOMEM;
197 }
198
199 *ret = TAKE_PTR(b);
200 return !!s;
201 }
202
203 static int get_hardware_vendor(char **ret) {
204 return get_dmi_data("ID_VENDOR_FROM_DATABASE", "ID_VENDOR", ret);
205 }
206
207 static int get_hardware_model(char **ret) {
208 return get_dmi_data("ID_MODEL_FROM_DATABASE", "ID_MODEL", ret);
209 }
210
211 static int get_hardware_firmware_data(const char *sysattr, char **ret) {
212 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
213 _cleanup_free_ char *b = NULL;
214 const char *s = NULL;
215 int r;
216
217 assert(sysattr);
218
219 r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
220 if (r < 0)
221 return log_debug_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
222
223 (void) sd_device_get_sysattr_value(device, sysattr, &s);
224 if (!isempty(s)) {
225 b = strdup(s);
226 if (!b)
227 return -ENOMEM;
228 }
229
230 if (ret)
231 *ret = TAKE_PTR(b);
232
233 return !isempty(s);
234 }
235
236 static int get_hardware_serial(char **ret) {
237 int r;
238
239 r = get_hardware_firmware_data("product_serial", ret);
240 if (r <= 0)
241 return get_hardware_firmware_data("board_serial", ret);
242
243 return r;
244 }
245
246 static int get_firmware_version(char **ret) {
247 return get_hardware_firmware_data("bios_version", ret);
248 }
249
250 static const char* valid_chassis(const char *chassis) {
251 assert(chassis);
252
253 return nulstr_get(
254 "vm\0"
255 "container\0"
256 "desktop\0"
257 "laptop\0"
258 "convertible\0"
259 "server\0"
260 "tablet\0"
261 "handset\0"
262 "watch\0"
263 "embedded\0",
264 chassis);
265 }
266
267 static bool valid_deployment(const char *deployment) {
268 assert(deployment);
269
270 return in_charset(deployment, VALID_DEPLOYMENT_CHARS);
271 }
272
273 static const char* fallback_chassis(void) {
274 const char *chassis;
275 _cleanup_free_ char *type = NULL;
276 Virtualization v;
277 unsigned t;
278 int r;
279
280 v = detect_virtualization();
281 if (v < 0)
282 log_debug_errno(v, "Failed to detect virtualization, ignoring: %m");
283 else if (VIRTUALIZATION_IS_VM(v))
284 return "vm";
285 else if (VIRTUALIZATION_IS_CONTAINER(v))
286 return "container";
287
288 r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
289 if (r < 0) {
290 log_debug_errno(r, "Failed to read DMI chassis type, ignoring: %m");
291 goto try_acpi;
292 }
293
294 r = safe_atou(type, &t);
295 if (r < 0) {
296 log_debug_errno(r, "Failed to parse DMI chassis type \"%s\", ignoring: %m", type);
297 goto try_acpi;
298 }
299
300 /* We only list the really obvious cases here. The DMI data is unreliable enough, so let's not do any
301 * additional guesswork on top of that.
302 *
303 * See the SMBIOS Specification 3.5.0 section 7.4.1 for details about the values listed here:
304 *
305 * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.5.0.pdf
306 */
307
308 switch (t) {
309
310 case 0x03: /* Desktop */
311 case 0x04: /* Low Profile Desktop */
312 case 0x06: /* Mini Tower */
313 case 0x07: /* Tower */
314 case 0x0D: /* All in one (i.e. PC built into monitor) */
315 case 0x23: /* Mini PC */
316 case 0x24: /* Stick PC */
317 return "desktop";
318
319 case 0x8: /* Portable */
320 case 0x9: /* Laptop */
321 case 0xA: /* Notebook */
322 case 0xE: /* Sub Notebook */
323 return "laptop";
324
325 case 0xB: /* Hand Held */
326 return "handset";
327
328 case 0x11: /* Main Server Chassis */
329 case 0x1C: /* Blade */
330 case 0x1D: /* Blade Enclosure */
331 return "server";
332
333 case 0x1E: /* Tablet */
334 return "tablet";
335
336 case 0x1F: /* Convertible */
337 case 0x20: /* Detachable */
338 return "convertible";
339
340 case 0x21: /* IoT Gateway */
341 case 0x22: /* Embedded PC */
342 return "embedded";
343
344 default:
345 log_debug("Unhandled DMI chassis type 0x%02x, ignoring.", t);
346 }
347
348 try_acpi:
349 type = mfree(type);
350 r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
351 if (r < 0) {
352 log_debug_errno(r, "Failed read ACPI PM profile, ignoring: %m");
353 goto try_devicetree;
354 }
355
356 r = safe_atou(type, &t);
357 if (r < 0) {
358 log_debug_errno(r, "Failed parse ACPI PM profile \"%s\", ignoring: %m", type);
359 goto try_devicetree;
360 }
361
362 /* We only list the really obvious cases here as the ACPI data is not really super reliable.
363 *
364 * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
365 *
366 * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
367 */
368
369 switch (t) {
370
371 case 1: /* Desktop */
372 case 3: /* Workstation */
373 case 6: /* Appliance PC */
374 return "desktop";
375
376 case 2: /* Mobile */
377 return "laptop";
378
379 case 4: /* Enterprise Server */
380 case 5: /* SOHO Server */
381 case 7: /* Performance Server */
382 return "server";
383
384 case 8: /* Tablet */
385 return "tablet";
386
387 default:
388 log_debug("Unhandled ACPI PM profile 0x%02x, ignoring.", t);
389 }
390
391 try_devicetree:
392 type = mfree(type);
393 r = read_one_line_file("/proc/device-tree/chassis-type", &type);
394 if (r < 0) {
395 log_debug_errno(r, "Failed to read device-tree chassis type, ignoring: %m");
396 return NULL;
397 }
398
399 /* Note that the Devicetree specification uses the very same vocabulary
400 * of chassis types as we do, hence we do not need to translate these types:
401 *
402 * https://github.com/devicetree-org/devicetree-specification/blob/master/source/chapter3-devicenodes.rst */
403 chassis = valid_chassis(type);
404 if (!chassis)
405 log_debug("Invalid device-tree chassis type \"%s\", ignoring.", type);
406 return chassis;
407 }
408
409 static char* context_get_chassis(Context *c) {
410 const char *fallback;
411 char *dmi;
412
413 assert(c);
414
415 if (!isempty(c->data[PROP_CHASSIS]))
416 return strdup(c->data[PROP_CHASSIS]);
417
418 if (get_dmi_data("ID_CHASSIS", NULL, &dmi) >= 0)
419 return dmi;
420
421 fallback = fallback_chassis();
422 if (fallback)
423 return strdup(fallback);
424
425 return NULL;
426 }
427
428 static char* context_fallback_icon_name(Context *c) {
429 _cleanup_free_ char *chassis = NULL;
430
431 assert(c);
432
433 chassis = context_get_chassis(c);
434 if (chassis)
435 return strjoin("computer-", chassis);
436
437 return strdup("computer");
438 }
439
440 static int context_update_kernel_hostname(
441 Context *c,
442 const char *transient_hn) {
443
444 _cleanup_free_ char *_hn_free = NULL;
445 const char *hn;
446 HostnameSource hns;
447 int r;
448
449 assert(c);
450
451 /* /etc/hostname has the highest preference ... */
452 if (c->data[PROP_STATIC_HOSTNAME]) {
453 hn = c->data[PROP_STATIC_HOSTNAME];
454 hns = HOSTNAME_STATIC;
455
456 /* ... the transient hostname, (ie: DHCP) comes next ... */
457 } else if (transient_hn) {
458 hn = transient_hn;
459 hns = HOSTNAME_TRANSIENT;
460
461 /* ... and the ultimate fallback */
462 } else {
463 hn = _hn_free = get_default_hostname();
464 if (!hn)
465 return log_oom();
466
467 hns = HOSTNAME_DEFAULT;
468 }
469
470 r = sethostname_idempotent(hn);
471 if (r < 0)
472 return log_error_errno(r, "Failed to set hostname: %m");
473
474 if (c->hostname_source != hns) {
475 c->hostname_source = hns;
476 r = 1;
477 }
478
479 (void) nscd_flush_cache(STRV_MAKE("hosts"));
480
481 if (r == 0)
482 log_debug("Hostname was already set to <%s>.", hn);
483 else {
484 log_info("Hostname set to <%s> (%s)", hn, hostname_source_to_string(hns));
485
486 hostname_update_source_hint(hn, hns);
487 }
488
489 return r; /* 0 if no change, 1 if something was done */
490 }
491
492 static void unset_statp(struct stat **p) {
493 if (!*p)
494 return;
495
496 **p = (struct stat) {};
497 }
498
499 static int context_write_data_static_hostname(Context *c) {
500 _cleanup_(unset_statp) struct stat *s = NULL;
501 int r;
502
503 assert(c);
504
505 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
506 * already, even if we can't make it hit the disk. */
507 s = &c->etc_hostname_stat;
508
509 if (isempty(c->data[PROP_STATIC_HOSTNAME])) {
510 if (unlink("/etc/hostname") < 0 && errno != ENOENT)
511 return -errno;
512
513 TAKE_PTR(s);
514 return 0;
515 }
516
517 r = write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]);
518 if (r < 0)
519 return r;
520
521 TAKE_PTR(s);
522 return 0;
523 }
524
525 static int context_write_data_machine_info(Context *c) {
526 _cleanup_(unset_statp) struct stat *s = NULL;
527 static const char * const name[_PROP_MAX] = {
528 [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
529 [PROP_ICON_NAME] = "ICON_NAME",
530 [PROP_CHASSIS] = "CHASSIS",
531 [PROP_DEPLOYMENT] = "DEPLOYMENT",
532 [PROP_LOCATION] = "LOCATION",
533 };
534 _cleanup_strv_free_ char **l = NULL;
535 int r;
536
537 assert(c);
538
539 /* Make sure that if we fail here, we invalidate the cached information, since it was updated
540 * already, even if we can't make it hit the disk. */
541 s = &c->etc_machine_info_stat;
542
543 r = load_env_file(NULL, "/etc/machine-info", &l);
544 if (r < 0 && r != -ENOENT)
545 return r;
546
547 for (int p = PROP_PRETTY_HOSTNAME; p <= PROP_LOCATION; p++) {
548 assert(name[p]);
549
550 r = strv_env_assign(&l, name[p], empty_to_null(c->data[p]));
551 if (r < 0)
552 return r;
553 }
554
555 if (strv_isempty(l)) {
556 if (unlink("/etc/machine-info") < 0 && errno != ENOENT)
557 return -errno;
558
559 TAKE_PTR(s);
560 return 0;
561 }
562
563 r = write_env_file_label("/etc/machine-info", l);
564 if (r < 0)
565 return r;
566
567 TAKE_PTR(s);
568 return 0;
569 }
570
571 static int property_get_hardware_property(
572 sd_bus_message *reply,
573 Context *c,
574 HostProperty prop,
575 int (*getter)(char **)) {
576
577 _cleanup_free_ char *from_dmi = NULL;
578
579 assert(reply);
580 assert(c);
581 assert(IN_SET(prop, PROP_HARDWARE_VENDOR, PROP_HARDWARE_MODEL));
582 assert(getter);
583
584 context_read_machine_info(c);
585
586 if (isempty(c->data[prop]))
587 (void) getter(&from_dmi);
588
589 return sd_bus_message_append(reply, "s", from_dmi ?: c->data[prop]);
590 }
591
592 static int property_get_hardware_vendor(
593 sd_bus *bus,
594 const char *path,
595 const char *interface,
596 const char *property,
597 sd_bus_message *reply,
598 void *userdata,
599 sd_bus_error *error) {
600
601 return property_get_hardware_property(reply, userdata, PROP_HARDWARE_VENDOR, get_hardware_vendor);
602 }
603
604 static int property_get_hardware_model(
605 sd_bus *bus,
606 const char *path,
607 const char *interface,
608 const char *property,
609 sd_bus_message *reply,
610 void *userdata,
611 sd_bus_error *error) {
612
613 return property_get_hardware_property(reply, userdata, PROP_HARDWARE_MODEL, get_hardware_model);
614 }
615
616 static int property_get_firmware_version(
617 sd_bus *bus,
618 const char *path,
619 const char *interface,
620 const char *property,
621 sd_bus_message *reply,
622 void *userdata,
623 sd_bus_error *error) {
624
625 _cleanup_free_ char *firmware_version = NULL;
626
627 (void) get_firmware_version(&firmware_version);
628
629 return sd_bus_message_append(reply, "s", firmware_version);
630 }
631
632 static int property_get_hostname(
633 sd_bus *bus,
634 const char *path,
635 const char *interface,
636 const char *property,
637 sd_bus_message *reply,
638 void *userdata,
639 sd_bus_error *error) {
640
641 _cleanup_free_ char *hn = NULL;
642 int r;
643
644 r = gethostname_strict(&hn);
645 if (r < 0) {
646 if (r != -ENXIO)
647 return r;
648
649 hn = get_default_hostname();
650 if (!hn)
651 return -ENOMEM;
652 }
653
654 return sd_bus_message_append(reply, "s", hn);
655 }
656
657 static int property_get_static_hostname(
658 sd_bus *bus,
659 const char *path,
660 const char *interface,
661 const char *property,
662 sd_bus_message *reply,
663 void *userdata,
664 sd_bus_error *error) {
665
666 Context *c = userdata;
667 assert(c);
668
669 context_read_etc_hostname(c);
670
671 return sd_bus_message_append(reply, "s", c->data[PROP_STATIC_HOSTNAME]);
672 }
673
674 static int property_get_default_hostname(
675 sd_bus *bus,
676 const char *path,
677 const char *interface,
678 const char *property,
679 sd_bus_message *reply,
680 void *userdata,
681 sd_bus_error *error) {
682
683 _cleanup_free_ char *hn = NULL;
684
685 hn = get_default_hostname();
686 if (!hn)
687 return log_oom();
688
689 return sd_bus_message_append(reply, "s", hn);
690 }
691
692 static void context_determine_hostname_source(Context *c) {
693 _cleanup_free_ char *hostname = NULL;
694 int r;
695
696 assert(c);
697
698 if (c->hostname_source >= 0)
699 return;
700
701 (void) gethostname_full(GET_HOSTNAME_ALLOW_LOCALHOST, &hostname);
702
703 if (streq_ptr(hostname, c->data[PROP_STATIC_HOSTNAME]))
704 c->hostname_source = HOSTNAME_STATIC;
705 else {
706 _cleanup_free_ char *fallback = NULL;
707
708 /* If the hostname was not set by us, try to figure out where it came from. If we set it to
709 * the default hostname, the file will tell us. We compare the string because it is possible
710 * that the hostname was set by an older version that had a different fallback, in the
711 * initramfs or before we reexecuted. */
712
713 r = read_one_line_file("/run/systemd/default-hostname", &fallback);
714 if (r < 0 && r != -ENOENT)
715 log_warning_errno(r, "Failed to read /run/systemd/default-hostname, ignoring: %m");
716
717 if (streq_ptr(fallback, hostname))
718 c->hostname_source = HOSTNAME_DEFAULT;
719 else
720 c->hostname_source = HOSTNAME_TRANSIENT;
721 }
722 }
723
724 static int property_get_hostname_source(
725 sd_bus *bus,
726 const char *path,
727 const char *interface,
728 const char *property,
729 sd_bus_message *reply,
730 void *userdata,
731 sd_bus_error *error) {
732
733 Context *c = userdata;
734 assert(c);
735
736 context_read_etc_hostname(c);
737 context_determine_hostname_source(c);
738
739 return sd_bus_message_append(reply, "s", hostname_source_to_string(c->hostname_source));
740 }
741
742 static int property_get_machine_info_field(
743 sd_bus *bus,
744 const char *path,
745 const char *interface,
746 const char *property,
747 sd_bus_message *reply,
748 void *userdata,
749 sd_bus_error *error) {
750
751 sd_bus_slot *slot;
752 Context *c;
753
754 /* Acquire the context object without this property's userdata offset added. Explanation: we want
755 * access to two pointers here: a) the main context object we cache all properties in, and b) the
756 * pointer to the property field inside the context object that we are supposed to update and
757 * use. The latter (b) we get in the 'userdata' function parameter, and sd-bus calculates that for us
758 * from the 'userdata' pointer we supplied when the vtable was registered, with the offset we
759 * specified in the vtable added on top. To get the former (a) we need the 'userdata' pointer from
760 * the vtable registration directly, without the offset added. Hence we ask sd-bus what the slot
761 * object is (which encapsulates the vtable registration), and then query the 'userdata' field
762 * directly off it. */
763 assert_se(slot = sd_bus_get_current_slot(bus));
764 assert_se(c = sd_bus_slot_get_userdata(slot));
765
766 context_read_machine_info(c);
767
768 return sd_bus_message_append(reply, "s", *(char**) userdata);
769 }
770
771 static int property_get_os_release_field(
772 sd_bus *bus,
773 const char *path,
774 const char *interface,
775 const char *property,
776 sd_bus_message *reply,
777 void *userdata,
778 sd_bus_error *error) {
779
780 sd_bus_slot *slot;
781 Context *c;
782
783 /* As above, acquire the current context without this property's userdata offset added. */
784 assert_se(slot = sd_bus_get_current_slot(bus));
785 assert_se(c = sd_bus_slot_get_userdata(slot));
786
787 context_read_os_release(c);
788
789 return sd_bus_message_append(reply, "s", *(char**) userdata);
790 }
791
792 static int property_get_icon_name(
793 sd_bus *bus,
794 const char *path,
795 const char *interface,
796 const char *property,
797 sd_bus_message *reply,
798 void *userdata,
799 sd_bus_error *error) {
800
801 _cleanup_free_ char *n = NULL;
802 Context *c = userdata;
803 const char *name;
804
805 context_read_machine_info(c);
806
807 if (isempty(c->data[PROP_ICON_NAME]))
808 name = n = context_fallback_icon_name(c);
809 else
810 name = c->data[PROP_ICON_NAME];
811
812 if (!name)
813 return -ENOMEM;
814
815 return sd_bus_message_append(reply, "s", name);
816 }
817
818 static int property_get_chassis(
819 sd_bus *bus,
820 const char *path,
821 const char *interface,
822 const char *property,
823 sd_bus_message *reply,
824 void *userdata,
825 sd_bus_error *error) {
826
827 _cleanup_free_ char *chassis = NULL;
828 Context *c = userdata;
829
830 context_read_machine_info(c);
831
832 chassis = context_get_chassis(c);
833
834 return sd_bus_message_append(reply, "s", chassis);
835 }
836
837 static int property_get_uname_field(
838 sd_bus *bus,
839 const char *path,
840 const char *interface,
841 const char *property,
842 sd_bus_message *reply,
843 void *userdata,
844 sd_bus_error *error) {
845
846 struct utsname u;
847
848 assert_se(uname(&u) >= 0);
849
850 return sd_bus_message_append(reply, "s", (char*) &u + PTR_TO_SIZE(userdata));
851 }
852
853 static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
854 Context *c = userdata;
855 const char *name;
856 int interactive, r;
857
858 assert(m);
859 assert(c);
860
861 r = sd_bus_message_read(m, "sb", &name, &interactive);
862 if (r < 0)
863 return r;
864
865 name = empty_to_null(name);
866
867 /* We always go through with the procedure below without comparing to the current hostname, because
868 * we might want to adjust hostname source information even if the actual hostname is unchanged. */
869
870 if (name && !hostname_is_valid(name, 0))
871 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
872
873 context_read_etc_hostname(c);
874
875 r = bus_verify_polkit_async(
876 m,
877 CAP_SYS_ADMIN,
878 "org.freedesktop.hostname1.set-hostname",
879 NULL,
880 interactive,
881 UID_INVALID,
882 &c->polkit_registry,
883 error);
884 if (r < 0)
885 return r;
886 if (r == 0)
887 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
888
889 r = context_update_kernel_hostname(c, name);
890 if (r < 0)
891 return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m");
892 else if (r > 0)
893 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
894 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
895 "Hostname", "HostnameSource", NULL);
896
897 return sd_bus_reply_method_return(m, NULL);
898 }
899
900 static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
901 Context *c = userdata;
902 const char *name;
903 int interactive;
904 int r;
905
906 assert(m);
907 assert(c);
908
909 r = sd_bus_message_read(m, "sb", &name, &interactive);
910 if (r < 0)
911 return r;
912
913 name = empty_to_null(name);
914
915 context_read_etc_hostname(c);
916
917 if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
918 return sd_bus_reply_method_return(m, NULL);
919
920 if (name && !hostname_is_valid(name, 0))
921 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
922
923 r = bus_verify_polkit_async(
924 m,
925 CAP_SYS_ADMIN,
926 "org.freedesktop.hostname1.set-static-hostname",
927 NULL,
928 interactive,
929 UID_INVALID,
930 &c->polkit_registry,
931 error);
932 if (r < 0)
933 return r;
934 if (r == 0)
935 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
936
937 r = free_and_strdup_warn(&c->data[PROP_STATIC_HOSTNAME], name);
938 if (r < 0)
939 return r;
940
941 r = context_write_data_static_hostname(c);
942 if (r < 0) {
943 log_error_errno(r, "Failed to write static hostname: %m");
944 if (ERRNO_IS_PRIVILEGE(r))
945 return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/hostname.");
946 if (r == -EROFS)
947 return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/hostname is in a read-only filesystem.");
948 return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %m");
949 }
950
951 r = context_update_kernel_hostname(c, NULL);
952 if (r < 0) {
953 log_error_errno(r, "Failed to set hostname: %m");
954 return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %m");
955 }
956
957 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
958 "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
959 "StaticHostname", "Hostname", "HostnameSource", NULL);
960
961 return sd_bus_reply_method_return(m, NULL);
962 }
963
964 static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_message_handler_t cb, sd_bus_error *error) {
965 int interactive;
966 const char *name;
967 int r;
968
969 assert(c);
970 assert(m);
971
972 r = sd_bus_message_read(m, "sb", &name, &interactive);
973 if (r < 0)
974 return r;
975
976 name = empty_to_null(name);
977
978 context_read_machine_info(c);
979
980 if (streq_ptr(name, c->data[prop]))
981 return sd_bus_reply_method_return(m, NULL);
982
983 if (!isempty(name)) {
984 /* The icon name might ultimately be used as file
985 * name, so better be safe than sorry */
986
987 if (prop == PROP_ICON_NAME && !filename_is_valid(name))
988 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
989 if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL))
990 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty hostname '%s'", name);
991 if (prop == PROP_CHASSIS && !valid_chassis(name))
992 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name);
993 if (prop == PROP_DEPLOYMENT && !valid_deployment(name))
994 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name);
995 if (prop == PROP_LOCATION && string_has_cc(name, NULL))
996 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name);
997 }
998
999 /* Since the pretty hostname should always be changed at the
1000 * same time as the static one, use the same policy action for
1001 * both... */
1002
1003 r = bus_verify_polkit_async(
1004 m,
1005 CAP_SYS_ADMIN,
1006 prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info",
1007 NULL,
1008 interactive,
1009 UID_INVALID,
1010 &c->polkit_registry,
1011 error);
1012 if (r < 0)
1013 return r;
1014 if (r == 0)
1015 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1016
1017 r = free_and_strdup_warn(&c->data[prop], name);
1018 if (r < 0)
1019 return r;
1020
1021 r = context_write_data_machine_info(c);
1022 if (r < 0) {
1023 log_error_errno(r, "Failed to write machine info: %m");
1024 if (ERRNO_IS_PRIVILEGE(r))
1025 return sd_bus_error_set(error, BUS_ERROR_FILE_IS_PROTECTED, "Not allowed to update /etc/machine-info.");
1026 if (r == -EROFS)
1027 return sd_bus_error_set(error, BUS_ERROR_READ_ONLY_FILESYSTEM, "/etc/machine-info is in a read-only filesystem.");
1028 return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %m");
1029 }
1030
1031 log_info("Changed %s to '%s'",
1032 prop == PROP_PRETTY_HOSTNAME ? "pretty hostname" :
1033 prop == PROP_DEPLOYMENT ? "deployment" :
1034 prop == PROP_LOCATION ? "location" :
1035 prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop]));
1036
1037 (void) sd_bus_emit_properties_changed(
1038 sd_bus_message_get_bus(m),
1039 "/org/freedesktop/hostname1",
1040 "org.freedesktop.hostname1",
1041 prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" :
1042 prop == PROP_DEPLOYMENT ? "Deployment" :
1043 prop == PROP_LOCATION ? "Location" :
1044 prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL);
1045
1046 return sd_bus_reply_method_return(m, NULL);
1047 }
1048
1049 static int method_set_pretty_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1050 return set_machine_info(userdata, m, PROP_PRETTY_HOSTNAME, method_set_pretty_hostname, error);
1051 }
1052
1053 static int method_set_icon_name(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1054 return set_machine_info(userdata, m, PROP_ICON_NAME, method_set_icon_name, error);
1055 }
1056
1057 static int method_set_chassis(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1058 return set_machine_info(userdata, m, PROP_CHASSIS, method_set_chassis, error);
1059 }
1060
1061 static int method_set_deployment(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1062 return set_machine_info(userdata, m, PROP_DEPLOYMENT, method_set_deployment, error);
1063 }
1064
1065 static int method_set_location(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1066 return set_machine_info(userdata, m, PROP_LOCATION, method_set_location, error);
1067 }
1068
1069 static int method_get_product_uuid(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1070 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1071 Context *c = userdata;
1072 int interactive, r;
1073 sd_id128_t uuid;
1074
1075 assert(m);
1076 assert(c);
1077
1078 r = sd_bus_message_read(m, "b", &interactive);
1079 if (r < 0)
1080 return r;
1081
1082 r = bus_verify_polkit_async(
1083 m,
1084 CAP_SYS_ADMIN,
1085 "org.freedesktop.hostname1.get-product-uuid",
1086 NULL,
1087 interactive,
1088 UID_INVALID,
1089 &c->polkit_registry,
1090 error);
1091 if (r < 0)
1092 return r;
1093 if (r == 0)
1094 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1095
1096 r = id128_get_product(&uuid);
1097 if (r < 0) {
1098 if (r == -EADDRNOTAVAIL)
1099 log_debug_errno(r, "DMI product UUID is all 0x00 or all 0xFF, ignoring.");
1100 else
1101 log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
1102 "Failed to read product UUID, ignoring: %m");
1103
1104 return sd_bus_error_set(error, BUS_ERROR_NO_PRODUCT_UUID,
1105 "Failed to read product UUID from firmware.");
1106 }
1107
1108 r = sd_bus_message_new_method_return(m, &reply);
1109 if (r < 0)
1110 return r;
1111
1112 r = sd_bus_message_append_array(reply, 'y', uuid.bytes, sizeof(uuid.bytes));
1113 if (r < 0)
1114 return r;
1115
1116 return sd_bus_send(NULL, reply, NULL);
1117 }
1118
1119 static int method_get_hardware_serial(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1120 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1121 _cleanup_free_ char *serial = NULL;
1122 Context *c = userdata;
1123 int r;
1124
1125 assert(m);
1126 assert(c);
1127
1128 r = bus_verify_polkit_async(
1129 m,
1130 CAP_SYS_ADMIN,
1131 "org.freedesktop.hostname1.get-hardware-serial",
1132 NULL,
1133 false,
1134 UID_INVALID,
1135 &c->polkit_registry,
1136 error);
1137 if (r < 0)
1138 return r;
1139 if (r == 0)
1140 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1141
1142 r = get_hardware_serial(&serial);
1143 if (r < 0)
1144 return r;
1145
1146 r = sd_bus_message_new_method_return(m, &reply);
1147 if (r < 0)
1148 return r;
1149
1150 r = sd_bus_message_append(reply, "s", serial);
1151 if (r < 0)
1152 return r;
1153
1154 return sd_bus_send(NULL, reply, NULL);
1155 }
1156
1157 static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1158 _cleanup_free_ char *hn = NULL, *dhn = NULL, *in = NULL, *text = NULL,
1159 *chassis = NULL, *vendor = NULL, *model = NULL, *serial = NULL, *firmware_version = NULL;
1160 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1161 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
1162 sd_id128_t product_uuid = SD_ID128_NULL;
1163 Context *c = userdata;
1164 bool privileged;
1165 struct utsname u;
1166 int r;
1167
1168 assert(m);
1169 assert(c);
1170
1171 r = bus_verify_polkit_async(
1172 m,
1173 CAP_SYS_ADMIN,
1174 "org.freedesktop.hostname1.get-description",
1175 NULL,
1176 false,
1177 UID_INVALID,
1178 &c->polkit_registry,
1179 NULL);
1180 if (r == 0)
1181 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1182
1183 /* We ignore all authentication errors here, since most data is unprivileged, the one exception being
1184 * the product ID which we'll check explicitly. */
1185 privileged = r > 0;
1186
1187 context_read_etc_hostname(c);
1188 context_read_machine_info(c);
1189 context_read_os_release(c);
1190 context_determine_hostname_source(c);
1191
1192 r = gethostname_strict(&hn);
1193 if (r < 0) {
1194 if (r != -ENXIO)
1195 return log_error_errno(r, "Failed to read local host name: %m");
1196
1197 hn = get_default_hostname();
1198 if (!hn)
1199 return log_oom();
1200 }
1201
1202 dhn = get_default_hostname();
1203 if (!dhn)
1204 return log_oom();
1205
1206 if (isempty(c->data[PROP_ICON_NAME]))
1207 in = context_fallback_icon_name(c);
1208
1209 chassis = context_get_chassis(c);
1210
1211 assert_se(uname(&u) >= 0);
1212
1213 if (isempty(c->data[PROP_HARDWARE_VENDOR]))
1214 (void) get_hardware_vendor(&vendor);
1215 if (isempty(c->data[PROP_HARDWARE_MODEL]))
1216 (void) get_hardware_model(&model);
1217
1218 if (privileged) {
1219 /* The product UUID and hardware serial is only available to privileged clients */
1220 (void) id128_get_product(&product_uuid);
1221 (void) get_hardware_serial(&serial);
1222 }
1223 (void) get_firmware_version(&firmware_version);
1224
1225 r = json_build(&v, JSON_BUILD_OBJECT(
1226 JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn)),
1227 JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c->data[PROP_STATIC_HOSTNAME])),
1228 JSON_BUILD_PAIR("PrettyHostname", JSON_BUILD_STRING(c->data[PROP_PRETTY_HOSTNAME])),
1229 JSON_BUILD_PAIR("DefaultHostname", JSON_BUILD_STRING(dhn)),
1230 JSON_BUILD_PAIR("HostnameSource", JSON_BUILD_STRING(hostname_source_to_string(c->hostname_source))),
1231 JSON_BUILD_PAIR("IconName", JSON_BUILD_STRING(in ?: c->data[PROP_ICON_NAME])),
1232 JSON_BUILD_PAIR("Chassis", JSON_BUILD_STRING(chassis)),
1233 JSON_BUILD_PAIR("Deployment", JSON_BUILD_STRING(c->data[PROP_DEPLOYMENT])),
1234 JSON_BUILD_PAIR("Location", JSON_BUILD_STRING(c->data[PROP_LOCATION])),
1235 JSON_BUILD_PAIR("KernelName", JSON_BUILD_STRING(u.sysname)),
1236 JSON_BUILD_PAIR("KernelRelease", JSON_BUILD_STRING(u.release)),
1237 JSON_BUILD_PAIR("KernelVersion", JSON_BUILD_STRING(u.version)),
1238 JSON_BUILD_PAIR("OperatingSystemPrettyName", JSON_BUILD_STRING(c->data[PROP_OS_PRETTY_NAME])),
1239 JSON_BUILD_PAIR("OperatingSystemCPEName", JSON_BUILD_STRING(c->data[PROP_OS_CPE_NAME])),
1240 JSON_BUILD_PAIR("OperatingSystemHomeURL", JSON_BUILD_STRING(c->data[PROP_OS_HOME_URL])),
1241 JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor ?: c->data[PROP_HARDWARE_VENDOR])),
1242 JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model ?: c->data[PROP_HARDWARE_MODEL])),
1243 JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial)),
1244 JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version)),
1245 JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_ID128(product_uuid)),
1246 JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_NULL)));
1247
1248 if (r < 0)
1249 return log_error_errno(r, "Failed to build JSON data: %m");
1250
1251 r = json_variant_format(v, 0, &text);
1252 if (r < 0)
1253 return log_error_errno(r, "Failed to format JSON data: %m");
1254
1255 r = sd_bus_message_new_method_return(m, &reply);
1256 if (r < 0)
1257 return r;
1258
1259 r = sd_bus_message_append(reply, "s", text);
1260 if (r < 0)
1261 return r;
1262
1263 return sd_bus_send(NULL, reply, NULL);
1264 }
1265
1266 static const sd_bus_vtable hostname_vtable[] = {
1267 SD_BUS_VTABLE_START(0),
1268 SD_BUS_PROPERTY("Hostname", "s", property_get_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1269 SD_BUS_PROPERTY("StaticHostname", "s", property_get_static_hostname, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1270 SD_BUS_PROPERTY("PrettyHostname", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1271 SD_BUS_PROPERTY("DefaultHostname", "s", property_get_default_hostname, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1272 SD_BUS_PROPERTY("HostnameSource", "s", property_get_hostname_source, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1273 SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1274 SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1275 SD_BUS_PROPERTY("Deployment", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_DEPLOYMENT, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1276 SD_BUS_PROPERTY("Location", "s", property_get_machine_info_field, offsetof(Context, data) + sizeof(char*) * PROP_LOCATION, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1277 SD_BUS_PROPERTY("KernelName", "s", property_get_uname_field, offsetof(struct utsname, sysname), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
1278 SD_BUS_PROPERTY("KernelRelease", "s", property_get_uname_field, offsetof(struct utsname, release), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
1279 SD_BUS_PROPERTY("KernelVersion", "s", property_get_uname_field, offsetof(struct utsname, version), SD_BUS_VTABLE_ABSOLUTE_OFFSET|SD_BUS_VTABLE_PROPERTY_CONST),
1280 SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
1281 SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
1282 SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_HOME_URL, SD_BUS_VTABLE_PROPERTY_CONST),
1283 SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1284 SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1285 SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1286
1287 SD_BUS_METHOD_WITH_NAMES("SetHostname",
1288 "sb",
1289 SD_BUS_PARAM(hostname)
1290 SD_BUS_PARAM(interactive),
1291 NULL,,
1292 method_set_hostname,
1293 SD_BUS_VTABLE_UNPRIVILEGED),
1294 SD_BUS_METHOD_WITH_NAMES("SetStaticHostname",
1295 "sb",
1296 SD_BUS_PARAM(hostname)
1297 SD_BUS_PARAM(interactive),
1298 NULL,,
1299 method_set_static_hostname,
1300 SD_BUS_VTABLE_UNPRIVILEGED),
1301 SD_BUS_METHOD_WITH_NAMES("SetPrettyHostname",
1302 "sb",
1303 SD_BUS_PARAM(hostname)
1304 SD_BUS_PARAM(interactive),
1305 NULL,,
1306 method_set_pretty_hostname,
1307 SD_BUS_VTABLE_UNPRIVILEGED),
1308 SD_BUS_METHOD_WITH_NAMES("SetIconName",
1309 "sb",
1310 SD_BUS_PARAM(icon)
1311 SD_BUS_PARAM(interactive),
1312 NULL,,
1313 method_set_icon_name,
1314 SD_BUS_VTABLE_UNPRIVILEGED),
1315 SD_BUS_METHOD_WITH_NAMES("SetChassis",
1316 "sb",
1317 SD_BUS_PARAM(chassis)
1318 SD_BUS_PARAM(interactive),
1319 NULL,,
1320 method_set_chassis,
1321 SD_BUS_VTABLE_UNPRIVILEGED),
1322 SD_BUS_METHOD_WITH_NAMES("SetDeployment",
1323 "sb",
1324 SD_BUS_PARAM(deployment)
1325 SD_BUS_PARAM(interactive),
1326 NULL,,
1327 method_set_deployment,
1328 SD_BUS_VTABLE_UNPRIVILEGED),
1329 SD_BUS_METHOD_WITH_NAMES("SetLocation",
1330 "sb",
1331 SD_BUS_PARAM(location)
1332 SD_BUS_PARAM(interactive),
1333 NULL,,
1334 method_set_location,
1335 SD_BUS_VTABLE_UNPRIVILEGED),
1336 SD_BUS_METHOD_WITH_NAMES("GetProductUUID",
1337 "b",
1338 SD_BUS_PARAM(interactive),
1339 "ay",
1340 SD_BUS_PARAM(uuid),
1341 method_get_product_uuid,
1342 SD_BUS_VTABLE_UNPRIVILEGED),
1343 SD_BUS_METHOD_WITH_NAMES("GetHardwareSerial",
1344 NULL,,
1345 "s",
1346 SD_BUS_PARAM(serial),
1347 method_get_hardware_serial,
1348 SD_BUS_VTABLE_UNPRIVILEGED),
1349 SD_BUS_METHOD_WITH_ARGS("Describe",
1350 SD_BUS_NO_ARGS,
1351 SD_BUS_RESULT("s", json),
1352 method_describe,
1353 SD_BUS_VTABLE_UNPRIVILEGED),
1354
1355 SD_BUS_VTABLE_END,
1356 };
1357
1358 static const BusObjectImplementation manager_object = {
1359 "/org/freedesktop/hostname1",
1360 "org.freedesktop.hostname1",
1361 .vtables = BUS_VTABLES(hostname_vtable),
1362 };
1363
1364 static int connect_bus(Context *c, sd_event *event, sd_bus **ret) {
1365 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1366 int r;
1367
1368 assert(c);
1369 assert(event);
1370 assert(ret);
1371
1372 r = sd_bus_default_system(&bus);
1373 if (r < 0)
1374 return log_error_errno(r, "Failed to get system bus connection: %m");
1375
1376 r = bus_add_implementation(bus, &manager_object, c);
1377 if (r < 0)
1378 return r;
1379
1380 r = bus_log_control_api_register(bus);
1381 if (r < 0)
1382 return r;
1383
1384 r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.hostname1", 0, NULL, NULL);
1385 if (r < 0)
1386 return log_error_errno(r, "Failed to request name: %m");
1387
1388 r = sd_bus_attach_event(bus, event, 0);
1389 if (r < 0)
1390 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1391
1392 *ret = TAKE_PTR(bus);
1393 return 0;
1394 }
1395
1396 static int run(int argc, char *argv[]) {
1397 _cleanup_(context_destroy) Context context = {
1398 .hostname_source = _HOSTNAME_INVALID, /* appropriate value will be set later */
1399 };
1400 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
1401 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1402 int r;
1403
1404 log_setup();
1405
1406 r = service_parse_argv("systemd-hostnamed.service",
1407 "Manage the system hostname and related metadata.",
1408 BUS_IMPLEMENTATIONS(&manager_object,
1409 &log_control_object),
1410 argc, argv);
1411 if (r <= 0)
1412 return r;
1413
1414 umask(0022);
1415
1416 r = mac_selinux_init();
1417 if (r < 0)
1418 return r;
1419
1420 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
1421
1422 r = sd_event_default(&event);
1423 if (r < 0)
1424 return log_error_errno(r, "Failed to allocate event loop: %m");
1425
1426 (void) sd_event_set_watchdog(event, true);
1427
1428 r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
1429 if (r < 0)
1430 return log_error_errno(r, "Failed to install SIGINT handler: %m");
1431
1432 r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
1433 if (r < 0)
1434 return log_error_errno(r, "Failed to install SIGTERM handler: %m");
1435
1436 r = connect_bus(&context, event, &bus);
1437 if (r < 0)
1438 return r;
1439
1440 r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL);
1441 if (r < 0)
1442 return log_error_errno(r, "Failed to run event loop: %m");
1443
1444 return 0;
1445 }
1446
1447 DEFINE_MAIN_FUNCTION(run);