]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hostname/hostnamectl.c
Highlight the synopsis and summary in --help
[thirdparty/systemd.git] / src / hostname / hostnamectl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
dbc4fbae 2
dbc4fbae 3#include <getopt.h>
a9cdc94f 4#include <locale.h>
3f6fd1ba
LP
5#include <stdbool.h>
6#include <stdlib.h>
dbc4fbae 7#include <string.h>
dbc4fbae 8
b028f3e4 9#include "sd-bus.h"
958b66ea 10#include "sd-id128.h"
3f6fd1ba 11
b5efdb8a 12#include "alloc-util.h"
3f6fd1ba 13#include "architecture.h"
b028f3e4 14#include "bus-error.h"
3f6fd1ba
LP
15#include "bus-util.h"
16#include "hostname-util.h"
5e332028 17#include "main-func.h"
294bf0c3 18#include "pretty-print.h"
dbc4fbae 19#include "spawn-polkit-agent.h"
ce2529b4 20#include "terminal-util.h"
3f6fd1ba 21#include "util.h"
f46bc484 22#include "verbs.h"
dbc4fbae 23
dbc4fbae 24static bool arg_ask_password = true;
b028f3e4 25static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
7085053a 26static char *arg_host = NULL;
960787ae
ZJS
27static bool arg_transient = false;
28static bool arg_pretty = false;
29static bool arg_static = false;
dbc4fbae 30
dbc4fbae 31typedef struct StatusInfo {
f37f8a61
YW
32 const char *hostname;
33 const char *static_hostname;
34 const char *pretty_hostname;
35 const char *icon_name;
36 const char *chassis;
37 const char *deployment;
38 const char *location;
39 const char *kernel_name;
40 const char *kernel_release;
41 const char *os_pretty_name;
42 const char *os_cpe_name;
43 const char *virtualization;
44 const char *architecture;
87adb0db 45 const char *home_url;
dbc4fbae
LP
46} StatusInfo;
47
48static void print_status_info(StatusInfo *i) {
39883f62 49 sd_id128_t mid = {}, bid = {};
dbc4fbae 50 int r;
dbc4fbae
LP
51
52 assert(i);
53
e9a2e453 54 printf(" Static hostname: %s\n", strna(i->static_hostname));
dbc4fbae 55
c0b21b96
LP
56 if (!isempty(i->pretty_hostname) &&
57 !streq_ptr(i->pretty_hostname, i->static_hostname))
e9a2e453 58 printf(" Pretty hostname: %s\n", i->pretty_hostname);
c0b21b96
LP
59
60 if (!isempty(i->hostname) &&
61 !streq_ptr(i->hostname, i->static_hostname))
e9a2e453 62 printf("Transient hostname: %s\n", i->hostname);
dbc4fbae 63
41414fed
LP
64 if (!isempty(i->icon_name))
65 printf(" Icon name: %s\n",
66 strna(i->icon_name));
67
68 if (!isempty(i->chassis))
69 printf(" Chassis: %s\n",
70 strna(i->chassis));
71
72 if (!isempty(i->deployment))
73 printf(" Deployment: %s\n", i->deployment);
74
75 if (!isempty(i->location))
76 printf(" Location: %s\n", i->location);
dbc4fbae
LP
77
78 r = sd_id128_get_machine(&mid);
79 if (r >= 0)
80 printf(" Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(mid));
81
82 r = sd_id128_get_boot(&bid);
83 if (r >= 0)
84 printf(" Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(bid));
85
e9a2e453
LP
86 if (!isempty(i->virtualization))
87 printf(" Virtualization: %s\n", i->virtualization);
fe29f9d2 88
87adb0db
LP
89 if (!isempty(i->os_pretty_name)) {
90 _cleanup_free_ char *formatted = NULL;
91 const char *t = i->os_pretty_name;
92
93 if (i->home_url) {
94 if (terminal_urlify(i->home_url, i->os_pretty_name, &formatted) >= 0)
95 t = formatted;
96 }
97
98 printf(" Operating System: %s\n", t);
99 }
fe29f9d2 100
3448456b
DH
101 if (!isempty(i->os_cpe_name))
102 printf(" CPE OS Name: %s\n", i->os_cpe_name);
fe29f9d2 103
fa4f8f9b
DH
104 if (!isempty(i->kernel_name) && !isempty(i->kernel_release))
105 printf(" Kernel: %s %s\n", i->kernel_name, i->kernel_release);
e9a2e453
LP
106
107 if (!isempty(i->architecture))
108 printf(" Architecture: %s\n", i->architecture);
fe29f9d2 109
dbc4fbae
LP
110}
111
b028f3e4 112static int show_one_name(sd_bus *bus, const char* attr) {
4afd3348
LP
113 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
114 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
b028f3e4 115 const char *s;
960787ae
ZJS
116 int r;
117
b028f3e4 118 r = sd_bus_get_property(
960787ae
ZJS
119 bus,
120 "org.freedesktop.hostname1",
121 "/org/freedesktop/hostname1",
b028f3e4
SP
122 "org.freedesktop.hostname1",
123 attr,
124 &error, &reply, "s");
f9e0eefc
LP
125 if (r < 0)
126 return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r));
960787ae 127
b028f3e4
SP
128 r = sd_bus_message_read(reply, "s", &s);
129 if (r < 0)
5b30bef8 130 return bus_log_parse_error(r);
960787ae 131
960787ae
ZJS
132 printf("%s\n", s);
133
134 return 0;
135}
136
f9e0eefc 137static int show_all_names(sd_bus *bus, sd_bus_error *error) {
b92bea5d 138 StatusInfo info = {};
e9a2e453
LP
139
140 static const struct bus_properties_map hostname_map[] = {
41414fed
LP
141 { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) },
142 { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) },
143 { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) },
144 { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) },
145 { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) },
146 { "Deployment", "s", NULL, offsetof(StatusInfo, deployment) },
147 { "Location", "s", NULL, offsetof(StatusInfo, location) },
148 { "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) },
149 { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
150 { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
151 { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
87adb0db 152 { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) },
b028f3e4
SP
153 {}
154 };
e9a2e453
LP
155
156 static const struct bus_properties_map manager_map[] = {
41414fed
LP
157 { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
158 { "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
e9a2e453
LP
159 {}
160 };
161
f37f8a61 162 _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL;
b028f3e4 163 int r;
dbc4fbae 164
b028f3e4
SP
165 r = bus_map_all_properties(bus,
166 "org.freedesktop.hostname1",
167 "/org/freedesktop/hostname1",
e9a2e453 168 hostname_map,
a7e4861c 169 0,
f9e0eefc 170 error,
f37f8a61 171 &host_message,
9f6eb1cd 172 &info);
dbc4fbae 173 if (r < 0)
f37f8a61 174 return r;
dbc4fbae 175
f37f8a61
YW
176 r = bus_map_all_properties(bus,
177 "org.freedesktop.systemd1",
178 "/org/freedesktop/systemd1",
179 manager_map,
a7e4861c 180 0,
f37f8a61
YW
181 error,
182 &manager_message,
183 &info);
e9a2e453 184
dbc4fbae 185 print_status_info(&info);
b028f3e4 186
e9a2e453 187 return r;
dbc4fbae
LP
188}
189
f46bc484
YW
190static int show_status(int argc, char **argv, void *userdata) {
191 sd_bus *bus = userdata;
f9e0eefc
LP
192 int r;
193
960787ae
ZJS
194 if (arg_pretty || arg_static || arg_transient) {
195 const char *attr;
196
197 if (!!arg_static + !!arg_pretty + !!arg_transient > 1) {
198 log_error("Cannot query more than one name type at a time");
199 return -EINVAL;
200 }
201
202 attr = arg_pretty ? "PrettyHostname" :
203 arg_static ? "StaticHostname" : "Hostname";
204
205 return show_one_name(bus, attr);
f9e0eefc
LP
206 } else {
207 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
208
209 r = show_all_names(bus, &error);
210 if (r < 0)
211 return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r));
212
213 return 0;
214 }
960787ae
ZJS
215}
216
b028f3e4 217static int set_simple_string(sd_bus *bus, const char *method, const char *value) {
4afd3348 218 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
b028f3e4
SP
219 int r = 0;
220
8a4b13c5 221 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
b028f3e4
SP
222
223 r = sd_bus_call_method(
224 bus,
225 "org.freedesktop.hostname1",
226 "/org/freedesktop/hostname1",
227 "org.freedesktop.hostname1",
228 method,
229 &error, NULL,
230 "sb", value, arg_ask_password);
231 if (r < 0)
4ae25393
YW
232 return log_error_errno(r, "Could not set property: %s", bus_error_message(&error, -r));
233
234 return 0;
b028f3e4
SP
235}
236
f46bc484 237static int set_hostname(int argc, char **argv, void *userdata) {
dbc4fbae 238 _cleanup_free_ char *h = NULL;
f46bc484
YW
239 const char *hostname = argv[1];
240 sd_bus *bus = userdata;
dbc4fbae
LP
241 int r;
242
960787ae
ZJS
243 if (!arg_pretty && !arg_static && !arg_transient)
244 arg_pretty = arg_static = arg_transient = true;
245
246 if (arg_pretty) {
fda2c5d2
LP
247 const char *p;
248
2ae0858e
LP
249 /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty
250 * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic
fda2c5d2 251 * hostname. */
2ae0858e
LP
252 if (arg_static && hostname_is_valid(hostname, true))
253 p = ""; /* No pretty hostname (as it is redundant), just a static one */
254 else
255 p = hostname; /* Use the passed name as pretty hostname */
fda2c5d2 256
b028f3e4 257 r = set_simple_string(bus, "SetPrettyHostname", p);
dbc4fbae
LP
258 if (r < 0)
259 return r;
2ae0858e
LP
260
261 /* Now that we set the pretty hostname, let's clean up the parameter and use that as static
262 * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing
263 * dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping
629ff674 264 * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are
2ae0858e
LP
265 * supposed to set the pretty name. If the pretty name is not being set we assume the user knows what
266 * he does and pass the name as-is. */
267 h = strdup(hostname);
268 if (!h)
269 return log_oom();
270
271 hostname = hostname_cleanup(h); /* Use the cleaned up name as static hostname */
dbc4fbae
LP
272 }
273
960787ae 274 if (arg_static) {
b028f3e4 275 r = set_simple_string(bus, "SetStaticHostname", hostname);
dbc4fbae
LP
276 if (r < 0)
277 return r;
278 }
279
960787ae 280 if (arg_transient) {
b028f3e4 281 r = set_simple_string(bus, "SetHostname", hostname);
dbc4fbae
LP
282 if (r < 0)
283 return r;
284 }
285
286 return 0;
287}
288
f46bc484
YW
289static int set_icon_name(int argc, char **argv, void *userdata) {
290 return set_simple_string(userdata, "SetIconName", argv[1]);
dbc4fbae
LP
291}
292
f46bc484
YW
293static int set_chassis(int argc, char **argv, void *userdata) {
294 return set_simple_string(userdata, "SetChassis", argv[1]);
7871c8e9
LP
295}
296
f46bc484
YW
297static int set_deployment(int argc, char **argv, void *userdata) {
298 return set_simple_string(userdata, "SetDeployment", argv[1]);
799298d6
JG
299}
300
f46bc484
YW
301static int set_location(int argc, char **argv, void *userdata) {
302 return set_simple_string(userdata, "SetLocation", argv[1]);
41414fed
LP
303}
304
f46bc484 305static int help(void) {
37ec0fdd
LP
306 _cleanup_free_ char *link = NULL;
307 int r;
308
309 r = terminal_urlify_man("hostnamectl", "1", &link);
310 if (r < 0)
311 return log_oom();
312
ce2529b4
ZJS
313 printf("%s%s [OPTIONS...] COMMAND ...\n\n"
314 "Query or change system hostname.%s\n"
e1fac8a6
ZJS
315 "\nCommands:\n"
316 " status Show current hostname settings\n"
317 " set-hostname NAME Set system hostname\n"
318 " set-icon-name NAME Set icon name for host\n"
319 " set-chassis NAME Set chassis type for host\n"
320 " set-deployment NAME Set deployment environment for host\n"
321 " set-location NAME Set location for host\n"
322 "\nOptions:\n"
dbc4fbae
LP
323 " -h --help Show this help\n"
324 " --version Show package version\n"
7591abd4 325 " --no-ask-password Do not prompt for password\n"
b028f3e4 326 " -H --host=[USER@]HOST Operate on remote host\n"
a86a47ce
LP
327 " -M --machine=CONTAINER Operate on local container\n"
328 " --transient Only set transient hostname\n"
329 " --static Only set static hostname\n"
e1fac8a6 330 " --pretty Only set pretty hostname\n"
37ec0fdd 331 "\nSee the %s for details.\n"
ce2529b4 332 , ansi_highlight()
37ec0fdd 333 , program_invocation_short_name
ce2529b4 334 , ansi_normal()
37ec0fdd
LP
335 , link
336 );
f46bc484
YW
337
338 return 0;
339}
340
341static int verb_help(int argc, char **argv, void *userdata) {
342 return help();
dbc4fbae
LP
343}
344
345static int parse_argv(int argc, char *argv[]) {
346
347 enum {
348 ARG_VERSION = 0x100,
349 ARG_NO_ASK_PASSWORD,
960787ae
ZJS
350 ARG_TRANSIENT,
351 ARG_STATIC,
352 ARG_PRETTY
dbc4fbae
LP
353 };
354
355 static const struct option options[] = {
356 { "help", no_argument, NULL, 'h' },
357 { "version", no_argument, NULL, ARG_VERSION },
eb9da376
LP
358 { "transient", no_argument, NULL, ARG_TRANSIENT },
359 { "static", no_argument, NULL, ARG_STATIC },
360 { "pretty", no_argument, NULL, ARG_PRETTY },
dbc4fbae 361 { "host", required_argument, NULL, 'H' },
b028f3e4 362 { "machine", required_argument, NULL, 'M' },
dbc4fbae 363 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
eb9da376 364 {}
dbc4fbae
LP
365 };
366
367 int c;
368
369 assert(argc >= 0);
370 assert(argv);
371
601185b4 372 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
dbc4fbae
LP
373
374 switch (c) {
375
376 case 'h':
f46bc484 377 return help();
dbc4fbae
LP
378
379 case ARG_VERSION:
3f6fd1ba 380 return version();
dbc4fbae 381
b028f3e4
SP
382 case 'H':
383 arg_transport = BUS_TRANSPORT_REMOTE;
384 arg_host = optarg;
dbc4fbae
LP
385 break;
386
b028f3e4 387 case 'M':
de33fc62 388 arg_transport = BUS_TRANSPORT_MACHINE;
b028f3e4 389 arg_host = optarg;
dbc4fbae
LP
390 break;
391
960787ae
ZJS
392 case ARG_TRANSIENT:
393 arg_transient = true;
dbc4fbae
LP
394 break;
395
960787ae
ZJS
396 case ARG_PRETTY:
397 arg_pretty = true;
dbc4fbae
LP
398 break;
399
960787ae
ZJS
400 case ARG_STATIC:
401 arg_static = true;
dbc4fbae
LP
402 break;
403
59f432ea
LP
404 case ARG_NO_ASK_PASSWORD:
405 arg_ask_password = false;
406 break;
407
dbc4fbae
LP
408 case '?':
409 return -EINVAL;
410
411 default:
eb9da376 412 assert_not_reached("Unhandled option");
dbc4fbae 413 }
dbc4fbae 414
dbc4fbae
LP
415 return 1;
416}
417
b028f3e4 418static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
dbc4fbae 419
f46bc484
YW
420 static const Verb verbs[] = {
421 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
422 { "set-hostname", 2, 2, 0, set_hostname },
423 { "set-icon-name", 2, 2, 0, set_icon_name },
424 { "set-chassis", 2, 2, 0, set_chassis },
425 { "set-deployment", 2, 2, 0, set_deployment },
426 { "set-location", 2, 2, 0, set_location },
427 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
428 {}
dbc4fbae
LP
429 };
430
f46bc484 431 return dispatch_verb(argc, argv, verbs, bus);
dbc4fbae
LP
432}
433
43ead411 434static int run(int argc, char *argv[]) {
4afd3348 435 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
84f6181c 436 int r;
dbc4fbae 437
a9cdc94f 438 setlocale(LC_ALL, "");
1a043959 439 log_show_color(true);
dbc4fbae
LP
440 log_parse_environment();
441 log_open();
442
443 r = parse_argv(argc, argv);
b028f3e4 444 if (r <= 0)
43ead411 445 return r;
b028f3e4 446
266f3e26 447 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
43ead411
ZJS
448 if (r < 0)
449 return log_error_errno(r, "Failed to create bus connection: %m");
dbc4fbae 450
43ead411 451 return hostnamectl_main(bus, argc, argv);
dbc4fbae 452}
43ead411
ZJS
453
454DEFINE_MAIN_FUNCTION(run);