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