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