]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hostname/hostnamectl.c
Merge pull request #32993 from poettering/cryptenroll-no-pcr
[thirdparty/systemd.git] / src / hostname / hostnamectl.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4 #include <locale.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "sd-bus.h"
11 #include "sd-id128.h"
12
13 #include "alloc-util.h"
14 #include "architecture.h"
15 #include "build.h"
16 #include "bus-common-errors.h"
17 #include "bus-error.h"
18 #include "bus-locator.h"
19 #include "bus-map-properties.h"
20 #include "format-table.h"
21 #include "hostname-setup.h"
22 #include "hostname-util.h"
23 #include "json.h"
24 #include "main-func.h"
25 #include "parse-argument.h"
26 #include "pretty-print.h"
27 #include "socket-util.h"
28 #include "spawn-polkit-agent.h"
29 #include "terminal-util.h"
30 #include "verbs.h"
31
32 static bool arg_ask_password = true;
33 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
34 static char *arg_host = NULL;
35 static bool arg_transient = false;
36 static bool arg_pretty = false;
37 static bool arg_static = false;
38 static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
39
40 typedef struct StatusInfo {
41 const char *hostname;
42 const char *static_hostname;
43 const char *pretty_hostname;
44 const char *icon_name;
45 const char *chassis;
46 const char *deployment;
47 const char *location;
48 const char *kernel_name;
49 const char *kernel_release;
50 const char *os_pretty_name;
51 const char *os_cpe_name;
52 usec_t os_support_end;
53 const char *virtualization;
54 const char *architecture;
55 const char *home_url;
56 const char *hardware_vendor;
57 const char *hardware_model;
58 const char *firmware_version;
59 usec_t firmware_date;
60 sd_id128_t machine_id;
61 sd_id128_t boot_id;
62 const char *hardware_serial;
63 sd_id128_t product_uuid;
64 uint32_t vsock_cid;
65 } StatusInfo;
66
67 static const char* chassis_string_to_glyph(const char *chassis) {
68 if (streq_ptr(chassis, "laptop"))
69 return u8"💻"; /* Personal Computer */
70 if (streq_ptr(chassis, "desktop"))
71 return u8"🖥️"; /* Desktop Computer */
72 if (streq_ptr(chassis, "server"))
73 return u8"🖳"; /* Old Personal Computer */
74 if (streq_ptr(chassis, "tablet"))
75 return u8"具"; /* Ideograph tool, implement; draw up, write, looks vaguely tabletty */
76 if (streq_ptr(chassis, "watch"))
77 return u8"⌚"; /* Watch */
78 if (streq_ptr(chassis, "handset"))
79 return u8"🕻"; /* Left Hand Telephone Receiver */
80 if (streq_ptr(chassis, "vm"))
81 return u8"🖴"; /* Hard disk */
82 if (streq_ptr(chassis, "container"))
83 return u8"☐"; /* Ballot Box */
84 return NULL;
85 }
86
87 static const char *os_support_end_color(usec_t n, usec_t eol) {
88 usec_t left;
89
90 /* If the end of support is over, color output in red. If only a month is left, color output in
91 * yellow. If more than a year is left, color green. In between just show in regular color. */
92
93 if (n >= eol)
94 return ANSI_HIGHLIGHT_RED;
95 left = eol - n;
96 if (left < USEC_PER_MONTH)
97 return ANSI_HIGHLIGHT_YELLOW;
98 if (left > USEC_PER_YEAR)
99 return ANSI_HIGHLIGHT_GREEN;
100
101 return NULL;
102 }
103
104 static int print_status_info(StatusInfo *i) {
105 _cleanup_(table_unrefp) Table *table = NULL;
106 TableCell *cell;
107 int r;
108
109 assert(i);
110
111 table = table_new_vertical();
112 if (!table)
113 return log_oom();
114
115 assert_se(cell = table_get_cell(table, 0, 0));
116 (void) table_set_ellipsize_percent(table, cell, 100);
117
118 table_set_ersatz_string(table, TABLE_ERSATZ_UNSET);
119
120 r = table_add_many(table,
121 TABLE_FIELD, "Static hostname",
122 TABLE_STRING, i->static_hostname);
123 if (r < 0)
124 return table_log_add_error(r);
125
126 if (!isempty(i->pretty_hostname) &&
127 !streq_ptr(i->pretty_hostname, i->static_hostname)) {
128 r = table_add_many(table,
129 TABLE_FIELD, "Pretty hostname",
130 TABLE_STRING, i->pretty_hostname);
131 if (r < 0)
132 return table_log_add_error(r);
133 }
134
135 if (!isempty(i->hostname) &&
136 !streq_ptr(i->hostname, i->static_hostname)) {
137 r = table_add_many(table,
138 TABLE_FIELD, "Transient hostname",
139 TABLE_STRING, i->hostname);
140 if (r < 0)
141 return table_log_add_error(r);
142 }
143
144 if (!isempty(i->icon_name)) {
145 r = table_add_many(table,
146 TABLE_FIELD, "Icon name",
147 TABLE_STRING, i->icon_name);
148 if (r < 0)
149 return table_log_add_error(r);
150 }
151
152 if (!isempty(i->chassis)) {
153 /* Possibly add a pretty symbol. Let's not bother with non-unicode fallbacks, because this is
154 * just a prettification and we can't really express this with ASCII anyway. */
155 const char *v = chassis_string_to_glyph(i->chassis);
156 if (v)
157 v = strjoina(i->chassis, " ", v);
158
159 r = table_add_many(table,
160 TABLE_FIELD, "Chassis",
161 TABLE_STRING, v ?: i->chassis);
162 if (r < 0)
163 return table_log_add_error(r);
164 }
165
166 if (!isempty(i->deployment)) {
167 r = table_add_many(table,
168 TABLE_FIELD, "Deployment",
169 TABLE_STRING, i->deployment);
170 if (r < 0)
171 return table_log_add_error(r);
172 }
173
174 if (!isempty(i->location)) {
175 r = table_add_many(table,
176 TABLE_FIELD, "Location",
177 TABLE_STRING, i->location);
178 if (r < 0)
179 return table_log_add_error(r);
180 }
181
182 if (!sd_id128_is_null(i->machine_id)) {
183 r = table_add_many(table,
184 TABLE_FIELD, "Machine ID",
185 TABLE_ID128, i->machine_id);
186 if (r < 0)
187 return table_log_add_error(r);
188 }
189
190 if (!sd_id128_is_null(i->boot_id)) {
191 r = table_add_many(table,
192 TABLE_FIELD, "Boot ID",
193 TABLE_ID128, i->boot_id);
194 if (r < 0)
195 return table_log_add_error(r);
196 }
197
198 if (!sd_id128_is_null(i->product_uuid)) {
199 r = table_add_many(table,
200 TABLE_FIELD, "Product UUID",
201 TABLE_UUID, i->product_uuid);
202 if (r < 0)
203 return table_log_add_error(r);
204 }
205
206 if (i->vsock_cid != VMADDR_CID_ANY) {
207 r = table_add_many(table,
208 TABLE_FIELD, "AF_VSOCK CID",
209 TABLE_UINT32, i->vsock_cid);
210 if (r < 0)
211 return table_log_add_error(r);
212 }
213
214 if (!isempty(i->virtualization)) {
215 r = table_add_many(table,
216 TABLE_FIELD, "Virtualization",
217 TABLE_STRING, i->virtualization);
218 if (r < 0)
219 return table_log_add_error(r);
220 }
221
222 if (!isempty(i->os_pretty_name)) {
223 r = table_add_many(table,
224 TABLE_FIELD, "Operating System",
225 TABLE_STRING, i->os_pretty_name,
226 TABLE_SET_URL, i->home_url);
227 if (r < 0)
228 return table_log_add_error(r);
229 }
230
231 if (!isempty(i->os_cpe_name)) {
232 r = table_add_many(table,
233 TABLE_FIELD, "CPE OS Name",
234 TABLE_STRING, i->os_cpe_name);
235 if (r < 0)
236 return table_log_add_error(r);
237 }
238
239 if (timestamp_is_set(i->os_support_end)) {
240 usec_t n = now(CLOCK_REALTIME);
241
242 r = table_add_many(table,
243 TABLE_FIELD, "OS Support End",
244 TABLE_TIMESTAMP_DATE, i->os_support_end,
245 TABLE_FIELD, n < i->os_support_end ? "OS Support Remaining" : "OS Support Expired",
246 TABLE_TIMESPAN_DAY, n < i->os_support_end ? i->os_support_end - n : n - i->os_support_end,
247 TABLE_SET_COLOR, os_support_end_color(n, i->os_support_end));
248 if (r < 0)
249 return table_log_add_error(r);
250 }
251
252 if (!isempty(i->kernel_name) && !isempty(i->kernel_release)) {
253 const char *v;
254
255 v = strjoina(i->kernel_name, " ", i->kernel_release);
256 r = table_add_many(table,
257 TABLE_FIELD, "Kernel",
258 TABLE_STRING, v);
259 if (r < 0)
260 return table_log_add_error(r);
261 }
262
263 if (!isempty(i->architecture)) {
264 r = table_add_many(table,
265 TABLE_FIELD, "Architecture",
266 TABLE_STRING, i->architecture);
267 if (r < 0)
268 return table_log_add_error(r);
269 }
270
271 if (!isempty(i->hardware_vendor)) {
272 r = table_add_many(table,
273 TABLE_FIELD, "Hardware Vendor",
274 TABLE_STRING, i->hardware_vendor);
275 if (r < 0)
276 return table_log_add_error(r);
277 }
278
279 if (!isempty(i->hardware_model)) {
280 r = table_add_many(table,
281 TABLE_FIELD, "Hardware Model",
282 TABLE_STRING, i->hardware_model);
283 if (r < 0)
284 return table_log_add_error(r);
285 }
286
287 if (!isempty(i->hardware_serial)) {
288 r = table_add_many(table,
289 TABLE_FIELD, "Hardware Serial",
290 TABLE_STRING, i->hardware_serial);
291 if (r < 0)
292 return table_log_add_error(r);
293 }
294
295 if (!isempty(i->firmware_version)) {
296 r = table_add_many(table,
297 TABLE_FIELD, "Firmware Version",
298 TABLE_STRING, i->firmware_version);
299 if (r < 0)
300 return table_log_add_error(r);
301 }
302
303 if (timestamp_is_set(i->firmware_date)) {
304 usec_t n = now(CLOCK_REALTIME);
305
306 r = table_add_many(table,
307 TABLE_FIELD, "Firmware Date",
308 TABLE_TIMESTAMP_DATE, i->firmware_date);
309 if (r < 0)
310 return table_log_add_error(r);
311
312 if (i->firmware_date < n) {
313 r = table_add_many(table,
314 TABLE_FIELD, "Firmware Age",
315 TABLE_TIMESPAN_DAY, n - i->firmware_date,
316 TABLE_SET_COLOR, n - i->firmware_date > USEC_PER_YEAR*2 ? ANSI_HIGHLIGHT_YELLOW : NULL);
317 if (r < 0)
318 return table_log_add_error(r);
319 }
320 }
321
322 r = table_print(table, NULL);
323 if (r < 0)
324 return table_log_print_error(r);
325
326 return 0;
327 }
328
329 static int get_one_name(sd_bus *bus, const char* attr, char **ret) {
330 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
331 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
332 const char *s;
333 int r;
334
335 assert(bus);
336 assert(attr);
337
338 /* This obtains one string property, and copy it if 'ret' is set, or print it otherwise. */
339
340 r = bus_get_property(bus, bus_hostname, attr, &error, &reply, "s");
341 if (r < 0)
342 return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r));
343
344 r = sd_bus_message_read(reply, "s", &s);
345 if (r < 0)
346 return bus_log_parse_error(r);
347
348 if (ret) {
349 char *str;
350
351 str = strdup(s);
352 if (!str)
353 return log_oom();
354
355 *ret = str;
356 } else
357 printf("%s\n", s);
358
359 return 0;
360 }
361
362 static int show_all_names(sd_bus *bus) {
363 StatusInfo info = {
364 .vsock_cid = VMADDR_CID_ANY,
365 .os_support_end = USEC_INFINITY,
366 .firmware_date = USEC_INFINITY,
367 };
368
369 static const struct bus_properties_map hostname_map[] = {
370 { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) },
371 { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) },
372 { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) },
373 { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) },
374 { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) },
375 { "Deployment", "s", NULL, offsetof(StatusInfo, deployment) },
376 { "Location", "s", NULL, offsetof(StatusInfo, location) },
377 { "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) },
378 { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
379 { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
380 { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
381 { "OperatingSystemSupportEnd", "t", NULL, offsetof(StatusInfo, os_support_end) },
382 { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) },
383 { "HardwareVendor", "s", NULL, offsetof(StatusInfo, hardware_vendor) },
384 { "HardwareModel", "s", NULL, offsetof(StatusInfo, hardware_model) },
385 { "FirmwareVersion", "s", NULL, offsetof(StatusInfo, firmware_version) },
386 { "FirmwareDate", "t", NULL, offsetof(StatusInfo, firmware_date) },
387 { "MachineID", "ay", bus_map_id128, offsetof(StatusInfo, machine_id) },
388 { "BootID", "ay", bus_map_id128, offsetof(StatusInfo, boot_id) },
389 { "VSockCID", "u", NULL, offsetof(StatusInfo, vsock_cid) },
390 {}
391 }, manager_map[] = {
392 { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
393 { "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
394 {}
395 };
396
397 _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL;
398 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
399 int r;
400
401 r = bus_map_all_properties(bus,
402 "org.freedesktop.hostname1",
403 "/org/freedesktop/hostname1",
404 hostname_map,
405 0,
406 &error,
407 &host_message,
408 &info);
409 if (r < 0)
410 return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r));
411
412 r = bus_map_all_properties(bus,
413 "org.freedesktop.systemd1",
414 "/org/freedesktop/systemd1",
415 manager_map,
416 0,
417 &error,
418 &manager_message,
419 &info);
420 if (r < 0)
421 return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r));
422
423 _cleanup_(sd_bus_message_unrefp) sd_bus_message *product_uuid_reply = NULL;
424 r = bus_call_method(bus,
425 bus_hostname,
426 "GetProductUUID",
427 &error,
428 &product_uuid_reply,
429 "b",
430 false);
431 if (r < 0) {
432 log_full_errno(sd_bus_error_has_names(
433 &error,
434 BUS_ERROR_NO_PRODUCT_UUID,
435 SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED,
436 SD_BUS_ERROR_UNKNOWN_METHOD) ? LOG_DEBUG : LOG_WARNING,
437 r, "Failed to query product UUID, ignoring: %s", bus_error_message(&error, r));
438 sd_bus_error_free(&error);
439 } else {
440 r = bus_message_read_id128(product_uuid_reply, &info.product_uuid);
441 if (r < 0)
442 return bus_log_parse_error(r);
443 }
444
445 _cleanup_(sd_bus_message_unrefp) sd_bus_message *hardware_serial_reply = NULL;
446 r = bus_call_method(bus,
447 bus_hostname,
448 "GetHardwareSerial",
449 &error,
450 &hardware_serial_reply,
451 NULL);
452 if (r < 0)
453 log_full_errno(sd_bus_error_has_names(
454 &error,
455 BUS_ERROR_NO_HARDWARE_SERIAL,
456 SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED,
457 SD_BUS_ERROR_UNKNOWN_METHOD) ||
458 ERRNO_IS_DEVICE_ABSENT(r) ? LOG_DEBUG : LOG_WARNING, /* old hostnamed used to send ENOENT/ENODEV back to client as is, handle that gracefully */
459 r, "Failed to query hardware serial, ignoring: %s", bus_error_message(&error, r));
460 else {
461 r = sd_bus_message_read_basic(hardware_serial_reply, 's', &info.hardware_serial);
462 if (r < 0)
463 return bus_log_parse_error(r);
464 }
465
466 /* For older version of hostnamed. */
467 if (!arg_host) {
468 if (sd_id128_is_null(info.machine_id))
469 (void) sd_id128_get_machine(&info.machine_id);
470 if (sd_id128_is_null(info.boot_id))
471 (void) sd_id128_get_boot(&info.boot_id);
472 }
473
474 return print_status_info(&info);
475 }
476
477 static int get_hostname_based_on_flag(sd_bus *bus) {
478 const char *attr;
479
480 if (!!arg_static + !!arg_pretty + !!arg_transient > 1)
481 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
482 "Cannot query more than one name type at a time");
483
484 attr = arg_pretty ? "PrettyHostname" :
485 arg_static ? "StaticHostname" : "Hostname";
486
487 return get_one_name(bus, attr, NULL);
488 }
489
490 static int show_status(int argc, char **argv, void *userdata) {
491 sd_bus *bus = userdata;
492 int r;
493
494 if (arg_json_format_flags != JSON_FORMAT_OFF) {
495 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
496 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
497 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
498 const char *text = NULL;
499
500 r = bus_call_method(bus, bus_hostname, "Describe", &error, &reply, NULL);
501 if (r < 0)
502 return log_error_errno(r, "Could not get description: %s", bus_error_message(&error, r));
503
504 r = sd_bus_message_read(reply, "s", &text);
505 if (r < 0)
506 return bus_log_parse_error(r);
507
508 r = json_parse(text, 0, &v, NULL, NULL);
509 if (r < 0)
510 return log_error_errno(r, "Failed to parse JSON: %m");
511
512 json_variant_dump(v, arg_json_format_flags, NULL, NULL);
513 return 0;
514 }
515
516 if (arg_pretty || arg_static || arg_transient)
517 return get_hostname_based_on_flag(bus);
518
519 return show_all_names(bus);
520 }
521
522
523 static int set_simple_string_internal(sd_bus *bus, sd_bus_error *error, const char *target, const char *method, const char *value) {
524 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
525 int r;
526
527 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
528
529 if (!error)
530 error = &e;
531
532 r = bus_call_method(bus, bus_hostname, method, error, NULL, "sb", value, arg_ask_password);
533 if (r < 0)
534 return log_error_errno(r, "Could not set %s: %s", target, bus_error_message(error, r));
535
536 return 0;
537 }
538
539 static int set_simple_string(sd_bus *bus, const char *target, const char *method, const char *value) {
540 return set_simple_string_internal(bus, NULL, target, method, value);
541 }
542
543 static int set_hostname(int argc, char **argv, void *userdata) {
544 _cleanup_free_ char *h = NULL;
545 const char *hostname = argv[1];
546 sd_bus *bus = userdata;
547 bool implicit = false, show_hint = false;
548 int r, ret = 0;
549
550 if (!arg_pretty && !arg_static && !arg_transient)
551 arg_pretty = arg_static = arg_transient = implicit = true;
552
553 if (!implicit && !arg_static && arg_transient) {
554 _cleanup_free_ char *source = NULL;
555
556 r = get_one_name(bus, "HostnameSource", &source);
557 if (r < 0)
558 return r;
559
560 if (hostname_source_from_string(source) == HOSTNAME_STATIC)
561 log_info("Hint: static hostname is already set, so the specified transient hostname will not be used.");
562 }
563
564 if (arg_pretty) {
565 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
566 const char *p;
567
568 /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty
569 * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic
570 * hostname. */
571 if (implicit && hostname_is_valid(hostname, VALID_HOSTNAME_TRAILING_DOT))
572 p = ""; /* No pretty hostname (as it is redundant), just a static one */
573 else
574 p = hostname; /* Use the passed name as pretty hostname */
575
576 r = set_simple_string_internal(bus, &error, "pretty hostname", "SetPrettyHostname", p);
577 if (r < 0) {
578 if (implicit &&
579 sd_bus_error_has_names(&error,
580 BUS_ERROR_FILE_IS_PROTECTED,
581 BUS_ERROR_READ_ONLY_FILESYSTEM)) {
582 show_hint = true;
583 ret = r;
584 } else
585 return r;
586 }
587
588 /* Now that we set the pretty hostname, let's clean up the parameter and use that as static
589 * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing
590 * dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping
591 * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are
592 * supposed to set the pretty name. If the pretty name is not being set we assume the user knows what
593 * they are doing and pass the name as-is. */
594 h = strdup(hostname);
595 if (!h)
596 return log_oom();
597
598 hostname = hostname_cleanup(h); /* Use the cleaned up name as static hostname */
599 }
600
601 if (arg_static) {
602 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
603
604 r = set_simple_string_internal(bus, &error, "static hostname", "SetStaticHostname", hostname);
605 if (r < 0) {
606 if (implicit &&
607 sd_bus_error_has_names(&error,
608 BUS_ERROR_FILE_IS_PROTECTED,
609 BUS_ERROR_READ_ONLY_FILESYSTEM)) {
610 show_hint = true;
611 ret = r;
612 } else
613 return r;
614 }
615 }
616
617 if (arg_transient) {
618 r = set_simple_string(bus, "transient hostname", "SetHostname", hostname);
619 if (r < 0)
620 return r;
621 }
622
623 if (show_hint)
624 log_info("Hint: use --transient option when /etc/machine-info or /etc/hostname cannot be modified (e.g. located in read-only filesystem).");
625
626 return ret;
627 }
628
629 static int get_or_set_hostname(int argc, char **argv, void *userdata) {
630 return argc == 1 ? get_hostname_based_on_flag(userdata) :
631 set_hostname(argc, argv, userdata);
632 }
633
634 static int get_or_set_icon_name(int argc, char **argv, void *userdata) {
635 return argc == 1 ? get_one_name(userdata, "IconName", NULL) :
636 set_simple_string(userdata, "icon", "SetIconName", argv[1]);
637 }
638
639 static int get_or_set_chassis(int argc, char **argv, void *userdata) {
640 return argc == 1 ? get_one_name(userdata, "Chassis", NULL) :
641 set_simple_string(userdata, "chassis", "SetChassis", argv[1]);
642 }
643
644 static int get_or_set_deployment(int argc, char **argv, void *userdata) {
645 return argc == 1 ? get_one_name(userdata, "Deployment", NULL) :
646 set_simple_string(userdata, "deployment", "SetDeployment", argv[1]);
647 }
648
649 static int get_or_set_location(int argc, char **argv, void *userdata) {
650 return argc == 1 ? get_one_name(userdata, "Location", NULL) :
651 set_simple_string(userdata, "location", "SetLocation", argv[1]);
652 }
653
654 static int help(void) {
655 _cleanup_free_ char *link = NULL;
656 int r;
657
658 r = terminal_urlify_man("hostnamectl", "1", &link);
659 if (r < 0)
660 return log_oom();
661
662 printf("%s [OPTIONS...] COMMAND ...\n\n"
663 "%sQuery or change system hostname.%s\n"
664 "\nCommands:\n"
665 " status Show current hostname settings\n"
666 " hostname [NAME] Get/set system hostname\n"
667 " icon-name [NAME] Get/set icon name for host\n"
668 " chassis [NAME] Get/set chassis type for host\n"
669 " deployment [NAME] Get/set deployment environment for host\n"
670 " location [NAME] Get/set location for host\n"
671 "\nOptions:\n"
672 " -h --help Show this help\n"
673 " --version Show package version\n"
674 " --no-ask-password Do not prompt for password\n"
675 " -H --host=[USER@]HOST Operate on remote host\n"
676 " -M --machine=CONTAINER Operate on local container\n"
677 " --transient Only set transient hostname\n"
678 " --static Only set static hostname\n"
679 " --pretty Only set pretty hostname\n"
680 " --json=pretty|short|off\n"
681 " Generate JSON output\n"
682 " -j Same as --json=pretty on tty, --json=short otherwise\n"
683 "\nSee the %s for details.\n",
684 program_invocation_short_name,
685 ansi_highlight(),
686 ansi_normal(),
687 link);
688
689 return 0;
690 }
691
692 static int verb_help(int argc, char **argv, void *userdata) {
693 return help();
694 }
695
696 static int parse_argv(int argc, char *argv[]) {
697
698 enum {
699 ARG_VERSION = 0x100,
700 ARG_NO_ASK_PASSWORD,
701 ARG_TRANSIENT,
702 ARG_STATIC,
703 ARG_PRETTY,
704 ARG_JSON,
705 };
706
707 static const struct option options[] = {
708 { "help", no_argument, NULL, 'h' },
709 { "version", no_argument, NULL, ARG_VERSION },
710 { "transient", no_argument, NULL, ARG_TRANSIENT },
711 { "static", no_argument, NULL, ARG_STATIC },
712 { "pretty", no_argument, NULL, ARG_PRETTY },
713 { "host", required_argument, NULL, 'H' },
714 { "machine", required_argument, NULL, 'M' },
715 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
716 { "json", required_argument, NULL, ARG_JSON },
717 {}
718 };
719
720 int c, r;
721
722 assert(argc >= 0);
723 assert(argv);
724
725 while ((c = getopt_long(argc, argv, "hH:M:j", options, NULL)) >= 0)
726
727 switch (c) {
728
729 case 'h':
730 return help();
731
732 case ARG_VERSION:
733 return version();
734
735 case 'H':
736 arg_transport = BUS_TRANSPORT_REMOTE;
737 arg_host = optarg;
738 break;
739
740 case 'M':
741 arg_transport = BUS_TRANSPORT_MACHINE;
742 arg_host = optarg;
743 break;
744
745 case ARG_TRANSIENT:
746 arg_transient = true;
747 break;
748
749 case ARG_PRETTY:
750 arg_pretty = true;
751 break;
752
753 case ARG_STATIC:
754 arg_static = true;
755 break;
756
757 case ARG_NO_ASK_PASSWORD:
758 arg_ask_password = false;
759 break;
760
761 case ARG_JSON:
762 r = parse_json_argument(optarg, &arg_json_format_flags);
763 if (r <= 0)
764 return r;
765
766 break;
767
768 case 'j':
769 arg_json_format_flags = JSON_FORMAT_PRETTY_AUTO|JSON_FORMAT_COLOR_AUTO;
770 break;
771
772 case '?':
773 return -EINVAL;
774
775 default:
776 assert_not_reached();
777 }
778
779 return 1;
780 }
781
782 static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
783
784 static const Verb verbs[] = {
785 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
786 { "hostname", VERB_ANY, 2, 0, get_or_set_hostname },
787 { "set-hostname", 2, 2, 0, get_or_set_hostname }, /* obsolete */
788 { "icon-name", VERB_ANY, 2, 0, get_or_set_icon_name },
789 { "set-icon-name", 2, 2, 0, get_or_set_icon_name }, /* obsolete */
790 { "chassis", VERB_ANY, 2, 0, get_or_set_chassis },
791 { "set-chassis", 2, 2, 0, get_or_set_chassis }, /* obsolete */
792 { "deployment", VERB_ANY, 2, 0, get_or_set_deployment },
793 { "set-deployment", 2, 2, 0, get_or_set_deployment }, /* obsolete */
794 { "location", VERB_ANY, 2, 0, get_or_set_location },
795 { "set-location", 2, 2, 0, get_or_set_location }, /* obsolete */
796 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
797 {}
798 };
799
800 return dispatch_verb(argc, argv, verbs, bus);
801 }
802
803 static int run(int argc, char *argv[]) {
804 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
805 int r;
806
807 setlocale(LC_ALL, "");
808 log_setup();
809
810 r = parse_argv(argc, argv);
811 if (r <= 0)
812 return r;
813
814 r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
815 if (r < 0)
816 return bus_log_connect_error(r, arg_transport);
817
818 return hostnamectl_main(bus, argc, argv);
819 }
820
821 DEFINE_MAIN_FUNCTION(run);