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