]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/hostname/hostnamectl.c
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / hostname / hostnamectl.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
dbc4fbae 2/***
dbc4fbae 3 Copyright 2012 Lennart Poettering
dbc4fbae
LP
4***/
5
dbc4fbae 6#include <getopt.h>
a9cdc94f 7#include <locale.h>
3f6fd1ba
LP
8#include <stdbool.h>
9#include <stdlib.h>
dbc4fbae 10#include <string.h>
dbc4fbae 11
b028f3e4 12#include "sd-bus.h"
958b66ea 13#include "sd-id128.h"
3f6fd1ba 14
b5efdb8a 15#include "alloc-util.h"
3f6fd1ba 16#include "architecture.h"
b028f3e4 17#include "bus-error.h"
3f6fd1ba
LP
18#include "bus-util.h"
19#include "hostname-util.h"
dbc4fbae 20#include "spawn-polkit-agent.h"
87adb0db 21#include "terminal-util.h"
3f6fd1ba 22#include "util.h"
f46bc484 23#include "verbs.h"
dbc4fbae 24
dbc4fbae 25static bool arg_ask_password = true;
b028f3e4 26static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
7085053a 27static char *arg_host = NULL;
960787ae
ZJS
28static bool arg_transient = false;
29static bool arg_pretty = false;
30static bool arg_static = false;
dbc4fbae 31
dbc4fbae 32typedef struct StatusInfo {
f37f8a61
YW
33 const char *hostname;
34 const char *static_hostname;
35 const char *pretty_hostname;
36 const char *icon_name;
37 const char *chassis;
38 const char *deployment;
39 const char *location;
40 const char *kernel_name;
41 const char *kernel_release;
42 const char *os_pretty_name;
43 const char *os_cpe_name;
44 const char *virtualization;
45 const char *architecture;
87adb0db 46 const char *home_url;
dbc4fbae
LP
47} StatusInfo;
48
49static void print_status_info(StatusInfo *i) {
39883f62 50 sd_id128_t mid = {}, bid = {};
dbc4fbae 51 int r;
dbc4fbae
LP
52
53 assert(i);
54
e9a2e453 55 printf(" Static hostname: %s\n", strna(i->static_hostname));
dbc4fbae 56
c0b21b96
LP
57 if (!isempty(i->pretty_hostname) &&
58 !streq_ptr(i->pretty_hostname, i->static_hostname))
e9a2e453 59 printf(" Pretty hostname: %s\n", i->pretty_hostname);
c0b21b96
LP
60
61 if (!isempty(i->hostname) &&
62 !streq_ptr(i->hostname, i->static_hostname))
e9a2e453 63 printf("Transient hostname: %s\n", i->hostname);
dbc4fbae 64
41414fed
LP
65 if (!isempty(i->icon_name))
66 printf(" Icon name: %s\n",
67 strna(i->icon_name));
68
69 if (!isempty(i->chassis))
70 printf(" Chassis: %s\n",
71 strna(i->chassis));
72
73 if (!isempty(i->deployment))
74 printf(" Deployment: %s\n", i->deployment);
75
76 if (!isempty(i->location))
77 printf(" Location: %s\n", i->location);
dbc4fbae
LP
78
79 r = sd_id128_get_machine(&mid);
80 if (r >= 0)
81 printf(" Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(mid));
82
83 r = sd_id128_get_boot(&bid);
84 if (r >= 0)
85 printf(" Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(bid));
86
e9a2e453
LP
87 if (!isempty(i->virtualization))
88 printf(" Virtualization: %s\n", i->virtualization);
fe29f9d2 89
87adb0db
LP
90 if (!isempty(i->os_pretty_name)) {
91 _cleanup_free_ char *formatted = NULL;
92 const char *t = i->os_pretty_name;
93
94 if (i->home_url) {
95 if (terminal_urlify(i->home_url, i->os_pretty_name, &formatted) >= 0)
96 t = formatted;
97 }
98
99 printf(" Operating System: %s\n", t);
100 }
fe29f9d2 101
3448456b
DH
102 if (!isempty(i->os_cpe_name))
103 printf(" CPE OS Name: %s\n", i->os_cpe_name);
fe29f9d2 104
fa4f8f9b
DH
105 if (!isempty(i->kernel_name) && !isempty(i->kernel_release))
106 printf(" Kernel: %s %s\n", i->kernel_name, i->kernel_release);
e9a2e453
LP
107
108 if (!isempty(i->architecture))
109 printf(" Architecture: %s\n", i->architecture);
fe29f9d2 110
dbc4fbae
LP
111}
112
b028f3e4 113static int show_one_name(sd_bus *bus, const char* attr) {
4afd3348
LP
114 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
115 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
b028f3e4 116 const char *s;
960787ae
ZJS
117 int r;
118
b028f3e4 119 r = sd_bus_get_property(
960787ae
ZJS
120 bus,
121 "org.freedesktop.hostname1",
122 "/org/freedesktop/hostname1",
b028f3e4
SP
123 "org.freedesktop.hostname1",
124 attr,
125 &error, &reply, "s");
f9e0eefc
LP
126 if (r < 0)
127 return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r));
960787ae 128
b028f3e4
SP
129 r = sd_bus_message_read(reply, "s", &s);
130 if (r < 0)
5b30bef8 131 return bus_log_parse_error(r);
960787ae 132
960787ae
ZJS
133 printf("%s\n", s);
134
135 return 0;
136}
137
f9e0eefc 138static int show_all_names(sd_bus *bus, sd_bus_error *error) {
b92bea5d 139 StatusInfo info = {};
e9a2e453
LP
140
141 static const struct bus_properties_map hostname_map[] = {
41414fed
LP
142 { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) },
143 { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) },
144 { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) },
145 { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) },
146 { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) },
147 { "Deployment", "s", NULL, offsetof(StatusInfo, deployment) },
148 { "Location", "s", NULL, offsetof(StatusInfo, location) },
149 { "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) },
150 { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
151 { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
152 { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
87adb0db 153 { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) },
b028f3e4
SP
154 {}
155 };
e9a2e453
LP
156
157 static const struct bus_properties_map manager_map[] = {
41414fed
LP
158 { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
159 { "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
e9a2e453
LP
160 {}
161 };
162
f37f8a61 163 _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL;
b028f3e4 164 int r;
dbc4fbae 165
b028f3e4
SP
166 r = bus_map_all_properties(bus,
167 "org.freedesktop.hostname1",
168 "/org/freedesktop/hostname1",
e9a2e453 169 hostname_map,
a7e4861c 170 0,
f9e0eefc 171 error,
f37f8a61 172 &host_message,
9f6eb1cd 173 &info);
dbc4fbae 174 if (r < 0)
f37f8a61 175 return r;
dbc4fbae 176
f37f8a61
YW
177 r = bus_map_all_properties(bus,
178 "org.freedesktop.systemd1",
179 "/org/freedesktop/systemd1",
180 manager_map,
a7e4861c 181 0,
f37f8a61
YW
182 error,
183 &manager_message,
184 &info);
e9a2e453 185
dbc4fbae 186 print_status_info(&info);
b028f3e4 187
e9a2e453 188 return r;
dbc4fbae
LP
189}
190
f46bc484
YW
191static int show_status(int argc, char **argv, void *userdata) {
192 sd_bus *bus = userdata;
f9e0eefc
LP
193 int r;
194
960787ae
ZJS
195 if (arg_pretty || arg_static || arg_transient) {
196 const char *attr;
197
198 if (!!arg_static + !!arg_pretty + !!arg_transient > 1) {
199 log_error("Cannot query more than one name type at a time");
200 return -EINVAL;
201 }
202
203 attr = arg_pretty ? "PrettyHostname" :
204 arg_static ? "StaticHostname" : "Hostname";
205
206 return show_one_name(bus, attr);
f9e0eefc
LP
207 } else {
208 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
209
210 r = show_all_names(bus, &error);
211 if (r < 0)
212 return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r));
213
214 return 0;
215 }
960787ae
ZJS
216}
217
b028f3e4 218static int set_simple_string(sd_bus *bus, const char *method, const char *value) {
4afd3348 219 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
b028f3e4
SP
220 int r = 0;
221
8a4b13c5 222 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
b028f3e4
SP
223
224 r = sd_bus_call_method(
225 bus,
226 "org.freedesktop.hostname1",
227 "/org/freedesktop/hostname1",
228 "org.freedesktop.hostname1",
229 method,
230 &error, NULL,
231 "sb", value, arg_ask_password);
232 if (r < 0)
233 log_error("Could not set property: %s", bus_error_message(&error, -r));
234 return r;
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) {
7591abd4
LP
306 printf("%s [OPTIONS...] COMMAND ...\n\n"
307 "Query or change system hostname.\n\n"
dbc4fbae
LP
308 " -h --help Show this help\n"
309 " --version Show package version\n"
7591abd4 310 " --no-ask-password Do not prompt for password\n"
b028f3e4 311 " -H --host=[USER@]HOST Operate on remote host\n"
a86a47ce
LP
312 " -M --machine=CONTAINER Operate on local container\n"
313 " --transient Only set transient hostname\n"
314 " --static Only set static hostname\n"
315 " --pretty Only set pretty hostname\n\n"
dbc4fbae 316 "Commands:\n"
7591abd4
LP
317 " status Show current hostname settings\n"
318 " set-hostname NAME Set system hostname\n"
7871c8e9 319 " set-icon-name NAME Set icon name for host\n"
799298d6 320 " set-chassis NAME Set chassis type for host\n"
601185b4 321 " set-deployment NAME Set deployment environment for host\n"
41414fed 322 " set-location NAME Set location for host\n"
601185b4 323 , program_invocation_short_name);
f46bc484
YW
324
325 return 0;
326}
327
328static int verb_help(int argc, char **argv, void *userdata) {
329 return help();
dbc4fbae
LP
330}
331
332static int parse_argv(int argc, char *argv[]) {
333
334 enum {
335 ARG_VERSION = 0x100,
336 ARG_NO_ASK_PASSWORD,
960787ae
ZJS
337 ARG_TRANSIENT,
338 ARG_STATIC,
339 ARG_PRETTY
dbc4fbae
LP
340 };
341
342 static const struct option options[] = {
343 { "help", no_argument, NULL, 'h' },
344 { "version", no_argument, NULL, ARG_VERSION },
eb9da376
LP
345 { "transient", no_argument, NULL, ARG_TRANSIENT },
346 { "static", no_argument, NULL, ARG_STATIC },
347 { "pretty", no_argument, NULL, ARG_PRETTY },
dbc4fbae 348 { "host", required_argument, NULL, 'H' },
b028f3e4 349 { "machine", required_argument, NULL, 'M' },
dbc4fbae 350 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
eb9da376 351 {}
dbc4fbae
LP
352 };
353
354 int c;
355
356 assert(argc >= 0);
357 assert(argv);
358
601185b4 359 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
dbc4fbae
LP
360
361 switch (c) {
362
363 case 'h':
f46bc484 364 return help();
dbc4fbae
LP
365
366 case ARG_VERSION:
3f6fd1ba 367 return version();
dbc4fbae 368
b028f3e4
SP
369 case 'H':
370 arg_transport = BUS_TRANSPORT_REMOTE;
371 arg_host = optarg;
dbc4fbae
LP
372 break;
373
b028f3e4 374 case 'M':
de33fc62 375 arg_transport = BUS_TRANSPORT_MACHINE;
b028f3e4 376 arg_host = optarg;
dbc4fbae
LP
377 break;
378
960787ae
ZJS
379 case ARG_TRANSIENT:
380 arg_transient = true;
dbc4fbae
LP
381 break;
382
960787ae
ZJS
383 case ARG_PRETTY:
384 arg_pretty = true;
dbc4fbae
LP
385 break;
386
960787ae
ZJS
387 case ARG_STATIC:
388 arg_static = true;
dbc4fbae
LP
389 break;
390
59f432ea
LP
391 case ARG_NO_ASK_PASSWORD:
392 arg_ask_password = false;
393 break;
394
dbc4fbae
LP
395 case '?':
396 return -EINVAL;
397
398 default:
eb9da376 399 assert_not_reached("Unhandled option");
dbc4fbae 400 }
dbc4fbae 401
dbc4fbae
LP
402 return 1;
403}
404
b028f3e4 405static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
dbc4fbae 406
f46bc484
YW
407 static const Verb verbs[] = {
408 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
409 { "set-hostname", 2, 2, 0, set_hostname },
410 { "set-icon-name", 2, 2, 0, set_icon_name },
411 { "set-chassis", 2, 2, 0, set_chassis },
412 { "set-deployment", 2, 2, 0, set_deployment },
413 { "set-location", 2, 2, 0, set_location },
414 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
415 {}
dbc4fbae
LP
416 };
417
f46bc484 418 return dispatch_verb(argc, argv, verbs, bus);
dbc4fbae
LP
419}
420
421int main(int argc, char *argv[]) {
4afd3348 422 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
84f6181c 423 int r;
dbc4fbae 424
a9cdc94f 425 setlocale(LC_ALL, "");
dbc4fbae
LP
426 log_parse_environment();
427 log_open();
428
429 r = parse_argv(argc, argv);
b028f3e4 430 if (r <= 0)
dbc4fbae 431 goto finish;
b028f3e4 432
266f3e26 433 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
b028f3e4 434 if (r < 0) {
da927ba9 435 log_error_errno(r, "Failed to create bus connection: %m");
dbc4fbae
LP
436 goto finish;
437 }
438
b028f3e4 439 r = hostnamectl_main(bus, argc, argv);
dbc4fbae
LP
440
441finish:
5567fafb 442 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
dbc4fbae 443}