]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hostname/hostnamectl.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / hostname / hostnamectl.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4 #include <locale.h>
5 #include <stdbool.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "sd-bus.h"
10 #include "sd-id128.h"
11
12 #include "alloc-util.h"
13 #include "architecture.h"
14 #include "bus-error.h"
15 #include "bus-map-properties.h"
16 #include "hostname-util.h"
17 #include "main-func.h"
18 #include "pretty-print.h"
19 #include "spawn-polkit-agent.h"
20 #include "terminal-util.h"
21 #include "util.h"
22 #include "verbs.h"
23
24 static bool arg_ask_password = true;
25 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
26 static char *arg_host = NULL;
27 static bool arg_transient = false;
28 static bool arg_pretty = false;
29 static bool arg_static = false;
30
31 typedef struct StatusInfo {
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;
45 const char *home_url;
46 } StatusInfo;
47
48 static void print_status_info(StatusInfo *i) {
49 sd_id128_t mid = {}, bid = {};
50 int r;
51
52 assert(i);
53
54 printf(" Static hostname: %s\n", strna(i->static_hostname));
55
56 if (!isempty(i->pretty_hostname) &&
57 !streq_ptr(i->pretty_hostname, i->static_hostname))
58 printf(" Pretty hostname: %s\n", i->pretty_hostname);
59
60 if (!isempty(i->hostname) &&
61 !streq_ptr(i->hostname, i->static_hostname))
62 printf("Transient hostname: %s\n", i->hostname);
63
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);
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
86 if (!isempty(i->virtualization))
87 printf(" Virtualization: %s\n", i->virtualization);
88
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 }
100
101 if (!isempty(i->os_cpe_name))
102 printf(" CPE OS Name: %s\n", i->os_cpe_name);
103
104 if (!isempty(i->kernel_name) && !isempty(i->kernel_release))
105 printf(" Kernel: %s %s\n", i->kernel_name, i->kernel_release);
106
107 if (!isempty(i->architecture))
108 printf(" Architecture: %s\n", i->architecture);
109
110 }
111
112 static int show_one_name(sd_bus *bus, const char* attr) {
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;
115 const char *s;
116 int r;
117
118 r = sd_bus_get_property(
119 bus,
120 "org.freedesktop.hostname1",
121 "/org/freedesktop/hostname1",
122 "org.freedesktop.hostname1",
123 attr,
124 &error, &reply, "s");
125 if (r < 0)
126 return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r));
127
128 r = sd_bus_message_read(reply, "s", &s);
129 if (r < 0)
130 return bus_log_parse_error(r);
131
132 printf("%s\n", s);
133
134 return 0;
135 }
136
137 static int show_all_names(sd_bus *bus, sd_bus_error *error) {
138 StatusInfo info = {};
139
140 static const struct bus_properties_map hostname_map[] = {
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) },
152 { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) },
153 {}
154 };
155
156 static const struct bus_properties_map manager_map[] = {
157 { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
158 { "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
159 {}
160 };
161
162 _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL;
163 int r;
164
165 r = bus_map_all_properties(bus,
166 "org.freedesktop.hostname1",
167 "/org/freedesktop/hostname1",
168 hostname_map,
169 0,
170 error,
171 &host_message,
172 &info);
173 if (r < 0)
174 return r;
175
176 r = bus_map_all_properties(bus,
177 "org.freedesktop.systemd1",
178 "/org/freedesktop/systemd1",
179 manager_map,
180 0,
181 error,
182 &manager_message,
183 &info);
184
185 print_status_info(&info);
186
187 return r;
188 }
189
190 static int show_status(int argc, char **argv, void *userdata) {
191 sd_bus *bus = userdata;
192 int r;
193
194 if (arg_pretty || arg_static || arg_transient) {
195 const char *attr;
196
197 if (!!arg_static + !!arg_pretty + !!arg_transient > 1)
198 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
199 "Cannot query more than one name type at a time");
200
201 attr = arg_pretty ? "PrettyHostname" :
202 arg_static ? "StaticHostname" : "Hostname";
203
204 return show_one_name(bus, attr);
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 }
214 }
215
216 static int set_simple_string(sd_bus *bus, const char *method, const char *value) {
217 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
218 int r = 0;
219
220 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
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)
231 return log_error_errno(r, "Could not set property: %s", bus_error_message(&error, r));
232
233 return 0;
234 }
235
236 static int set_hostname(int argc, char **argv, void *userdata) {
237 _cleanup_free_ char *h = NULL;
238 const char *hostname = argv[1];
239 sd_bus *bus = userdata;
240 int r;
241
242 if (!arg_pretty && !arg_static && !arg_transient)
243 arg_pretty = arg_static = arg_transient = true;
244
245 if (arg_pretty) {
246 const char *p;
247
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
250 * hostname. */
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 */
255
256 r = set_simple_string(bus, "SetPrettyHostname", p);
257 if (r < 0)
258 return r;
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
263 * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are
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 */
271 }
272
273 if (arg_static) {
274 r = set_simple_string(bus, "SetStaticHostname", hostname);
275 if (r < 0)
276 return r;
277 }
278
279 if (arg_transient) {
280 r = set_simple_string(bus, "SetHostname", hostname);
281 if (r < 0)
282 return r;
283 }
284
285 return 0;
286 }
287
288 static int set_icon_name(int argc, char **argv, void *userdata) {
289 return set_simple_string(userdata, "SetIconName", argv[1]);
290 }
291
292 static int set_chassis(int argc, char **argv, void *userdata) {
293 return set_simple_string(userdata, "SetChassis", argv[1]);
294 }
295
296 static int set_deployment(int argc, char **argv, void *userdata) {
297 return set_simple_string(userdata, "SetDeployment", argv[1]);
298 }
299
300 static int set_location(int argc, char **argv, void *userdata) {
301 return set_simple_string(userdata, "SetLocation", argv[1]);
302 }
303
304 static int help(void) {
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
312 printf("%s [OPTIONS...] COMMAND ...\n\n"
313 "%sQuery or change system hostname.%s\n"
314 "\nCommands:\n"
315 " status Show current hostname settings\n"
316 " set-hostname NAME Set system hostname\n"
317 " set-icon-name NAME Set icon name for host\n"
318 " set-chassis NAME Set chassis type for host\n"
319 " set-deployment NAME Set deployment environment for host\n"
320 " set-location NAME Set location for host\n"
321 "\nOptions:\n"
322 " -h --help Show this help\n"
323 " --version Show package version\n"
324 " --no-ask-password Do not prompt for password\n"
325 " -H --host=[USER@]HOST Operate on remote host\n"
326 " -M --machine=CONTAINER Operate on local container\n"
327 " --transient Only set transient hostname\n"
328 " --static Only set static hostname\n"
329 " --pretty Only set pretty hostname\n"
330 "\nSee the %s for details.\n"
331 , program_invocation_short_name
332 , ansi_highlight()
333 , ansi_normal()
334 , link
335 );
336
337 return 0;
338 }
339
340 static int verb_help(int argc, char **argv, void *userdata) {
341 return help();
342 }
343
344 static int parse_argv(int argc, char *argv[]) {
345
346 enum {
347 ARG_VERSION = 0x100,
348 ARG_NO_ASK_PASSWORD,
349 ARG_TRANSIENT,
350 ARG_STATIC,
351 ARG_PRETTY
352 };
353
354 static const struct option options[] = {
355 { "help", no_argument, NULL, 'h' },
356 { "version", no_argument, NULL, ARG_VERSION },
357 { "transient", no_argument, NULL, ARG_TRANSIENT },
358 { "static", no_argument, NULL, ARG_STATIC },
359 { "pretty", no_argument, NULL, ARG_PRETTY },
360 { "host", required_argument, NULL, 'H' },
361 { "machine", required_argument, NULL, 'M' },
362 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
363 {}
364 };
365
366 int c;
367
368 assert(argc >= 0);
369 assert(argv);
370
371 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
372
373 switch (c) {
374
375 case 'h':
376 return help();
377
378 case ARG_VERSION:
379 return version();
380
381 case 'H':
382 arg_transport = BUS_TRANSPORT_REMOTE;
383 arg_host = optarg;
384 break;
385
386 case 'M':
387 arg_transport = BUS_TRANSPORT_MACHINE;
388 arg_host = optarg;
389 break;
390
391 case ARG_TRANSIENT:
392 arg_transient = true;
393 break;
394
395 case ARG_PRETTY:
396 arg_pretty = true;
397 break;
398
399 case ARG_STATIC:
400 arg_static = true;
401 break;
402
403 case ARG_NO_ASK_PASSWORD:
404 arg_ask_password = false;
405 break;
406
407 case '?':
408 return -EINVAL;
409
410 default:
411 assert_not_reached("Unhandled option");
412 }
413
414 return 1;
415 }
416
417 static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
418
419 static const Verb verbs[] = {
420 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
421 { "set-hostname", 2, 2, 0, set_hostname },
422 { "set-icon-name", 2, 2, 0, set_icon_name },
423 { "set-chassis", 2, 2, 0, set_chassis },
424 { "set-deployment", 2, 2, 0, set_deployment },
425 { "set-location", 2, 2, 0, set_location },
426 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
427 {}
428 };
429
430 return dispatch_verb(argc, argv, verbs, bus);
431 }
432
433 static int run(int argc, char *argv[]) {
434 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
435 int r;
436
437 setlocale(LC_ALL, "");
438 log_setup_cli();
439
440 r = parse_argv(argc, argv);
441 if (r <= 0)
442 return r;
443
444 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
445 if (r < 0)
446 return bus_log_connect_error(r);
447
448 return hostnamectl_main(bus, argc, argv);
449 }
450
451 DEFINE_MAIN_FUNCTION(run);