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