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