]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hostname/hostnamectl.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / hostname / hostnamectl.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <getopt.h>
21 #include <locale.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "sd-bus.h"
27 #include "sd-id128.h"
28
29 #include "alloc-util.h"
30 #include "architecture.h"
31 #include "bus-error.h"
32 #include "bus-util.h"
33 #include "hostname-util.h"
34 #include "spawn-polkit-agent.h"
35 #include "util.h"
36
37 static bool arg_ask_password = true;
38 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
39 static char *arg_host = NULL;
40 static bool arg_transient = false;
41 static bool arg_pretty = false;
42 static bool arg_static = false;
43
44 typedef struct StatusInfo {
45 char *hostname;
46 char *static_hostname;
47 char *pretty_hostname;
48 char *icon_name;
49 char *chassis;
50 char *deployment;
51 char *location;
52 char *kernel_name;
53 char *kernel_release;
54 char *os_pretty_name;
55 char *os_cpe_name;
56 char *virtualization;
57 char *architecture;
58 } StatusInfo;
59
60 static void print_status_info(StatusInfo *i) {
61 sd_id128_t mid = {}, bid = {};
62 int r;
63
64 assert(i);
65
66 printf(" Static hostname: %s\n", strna(i->static_hostname));
67
68 if (!isempty(i->pretty_hostname) &&
69 !streq_ptr(i->pretty_hostname, i->static_hostname))
70 printf(" Pretty hostname: %s\n", i->pretty_hostname);
71
72 if (!isempty(i->hostname) &&
73 !streq_ptr(i->hostname, i->static_hostname))
74 printf("Transient hostname: %s\n", i->hostname);
75
76 if (!isempty(i->icon_name))
77 printf(" Icon name: %s\n",
78 strna(i->icon_name));
79
80 if (!isempty(i->chassis))
81 printf(" Chassis: %s\n",
82 strna(i->chassis));
83
84 if (!isempty(i->deployment))
85 printf(" Deployment: %s\n", i->deployment);
86
87 if (!isempty(i->location))
88 printf(" Location: %s\n", i->location);
89
90 r = sd_id128_get_machine(&mid);
91 if (r >= 0)
92 printf(" Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(mid));
93
94 r = sd_id128_get_boot(&bid);
95 if (r >= 0)
96 printf(" Boot ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(bid));
97
98 if (!isempty(i->virtualization))
99 printf(" Virtualization: %s\n", i->virtualization);
100
101 if (!isempty(i->os_pretty_name))
102 printf(" Operating System: %s\n", i->os_pretty_name);
103
104 if (!isempty(i->os_cpe_name))
105 printf(" CPE OS Name: %s\n", i->os_cpe_name);
106
107 if (!isempty(i->kernel_name) && !isempty(i->kernel_release))
108 printf(" Kernel: %s %s\n", i->kernel_name, i->kernel_release);
109
110 if (!isempty(i->architecture))
111 printf(" Architecture: %s\n", i->architecture);
112
113 }
114
115 static int show_one_name(sd_bus *bus, const char* attr) {
116 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
117 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
118 const char *s;
119 int r;
120
121 r = sd_bus_get_property(
122 bus,
123 "org.freedesktop.hostname1",
124 "/org/freedesktop/hostname1",
125 "org.freedesktop.hostname1",
126 attr,
127 &error, &reply, "s");
128 if (r < 0)
129 return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r));
130
131 r = sd_bus_message_read(reply, "s", &s);
132 if (r < 0)
133 return bus_log_parse_error(r);
134
135 printf("%s\n", s);
136
137 return 0;
138 }
139
140 static int show_all_names(sd_bus *bus, sd_bus_error *error) {
141 StatusInfo info = {};
142
143 static const struct bus_properties_map hostname_map[] = {
144 { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) },
145 { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) },
146 { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) },
147 { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) },
148 { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) },
149 { "Deployment", "s", NULL, offsetof(StatusInfo, deployment) },
150 { "Location", "s", NULL, offsetof(StatusInfo, location) },
151 { "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) },
152 { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
153 { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
154 { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
155 {}
156 };
157
158 static const struct bus_properties_map manager_map[] = {
159 { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
160 { "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
161 {}
162 };
163
164 int r;
165
166 r = bus_map_all_properties(bus,
167 "org.freedesktop.hostname1",
168 "/org/freedesktop/hostname1",
169 hostname_map,
170 error,
171 &info);
172 if (r < 0)
173 goto fail;
174
175 bus_map_all_properties(bus,
176 "org.freedesktop.systemd1",
177 "/org/freedesktop/systemd1",
178 manager_map,
179 error,
180 &info);
181
182 print_status_info(&info);
183
184 fail:
185 free(info.hostname);
186 free(info.static_hostname);
187 free(info.pretty_hostname);
188 free(info.icon_name);
189 free(info.chassis);
190 free(info.deployment);
191 free(info.location);
192 free(info.kernel_name);
193 free(info.kernel_release);
194 free(info.os_pretty_name);
195 free(info.os_cpe_name);
196 free(info.virtualization);
197 free(info.architecture);
198
199 return r;
200 }
201
202 static int show_status(sd_bus *bus, char **args, unsigned n) {
203 int r;
204
205 assert(args);
206
207 if (arg_pretty || arg_static || arg_transient) {
208 const char *attr;
209
210 if (!!arg_static + !!arg_pretty + !!arg_transient > 1) {
211 log_error("Cannot query more than one name type at a time");
212 return -EINVAL;
213 }
214
215 attr = arg_pretty ? "PrettyHostname" :
216 arg_static ? "StaticHostname" : "Hostname";
217
218 return show_one_name(bus, attr);
219 } else {
220 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
221
222 r = show_all_names(bus, &error);
223 if (r < 0)
224 return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r));
225
226 return 0;
227 }
228 }
229
230 static int set_simple_string(sd_bus *bus, const char *method, const char *value) {
231 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
232 int r = 0;
233
234 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
235
236 r = sd_bus_call_method(
237 bus,
238 "org.freedesktop.hostname1",
239 "/org/freedesktop/hostname1",
240 "org.freedesktop.hostname1",
241 method,
242 &error, NULL,
243 "sb", value, arg_ask_password);
244 if (r < 0)
245 log_error("Could not set property: %s", bus_error_message(&error, -r));
246 return r;
247 }
248
249 static int set_hostname(sd_bus *bus, char **args, unsigned n) {
250 _cleanup_free_ char *h = NULL;
251 const char *hostname = args[1];
252 int r;
253
254 assert(args);
255 assert(n == 2);
256
257 if (!arg_pretty && !arg_static && !arg_transient)
258 arg_pretty = arg_static = arg_transient = true;
259
260 if (arg_pretty) {
261 const char *p;
262
263 /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty
264 * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic
265 * hostname. */
266 if (arg_static && hostname_is_valid(hostname, true))
267 p = ""; /* No pretty hostname (as it is redundant), just a static one */
268 else
269 p = hostname; /* Use the passed name as pretty hostname */
270
271 r = set_simple_string(bus, "SetPrettyHostname", p);
272 if (r < 0)
273 return r;
274
275 /* Now that we set the pretty hostname, let's clean up the parameter and use that as static
276 * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing
277 * dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping
278 * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are
279 * supposed to set the pretty name. If the pretty name is not being set we assume the user knows what
280 * he does and pass the name as-is. */
281 h = strdup(hostname);
282 if (!h)
283 return log_oom();
284
285 hostname = hostname_cleanup(h); /* Use the cleaned up name as static hostname */
286 }
287
288 if (arg_static) {
289 r = set_simple_string(bus, "SetStaticHostname", hostname);
290 if (r < 0)
291 return r;
292 }
293
294 if (arg_transient) {
295 r = set_simple_string(bus, "SetHostname", hostname);
296 if (r < 0)
297 return r;
298 }
299
300 return 0;
301 }
302
303 static int set_icon_name(sd_bus *bus, char **args, unsigned n) {
304 assert(args);
305 assert(n == 2);
306
307 return set_simple_string(bus, "SetIconName", args[1]);
308 }
309
310 static int set_chassis(sd_bus *bus, char **args, unsigned n) {
311 assert(args);
312 assert(n == 2);
313
314 return set_simple_string(bus, "SetChassis", args[1]);
315 }
316
317 static int set_deployment(sd_bus *bus, char **args, unsigned n) {
318 assert(args);
319 assert(n == 2);
320
321 return set_simple_string(bus, "SetDeployment", args[1]);
322 }
323
324 static int set_location(sd_bus *bus, char **args, unsigned n) {
325 assert(args);
326 assert(n == 2);
327
328 return set_simple_string(bus, "SetLocation", args[1]);
329 }
330
331 static void help(void) {
332 printf("%s [OPTIONS...] COMMAND ...\n\n"
333 "Query or change system hostname.\n\n"
334 " -h --help Show this help\n"
335 " --version Show package version\n"
336 " --no-ask-password Do not prompt for password\n"
337 " -H --host=[USER@]HOST Operate on remote host\n"
338 " -M --machine=CONTAINER Operate on local container\n"
339 " --transient Only set transient hostname\n"
340 " --static Only set static hostname\n"
341 " --pretty Only set pretty hostname\n\n"
342 "Commands:\n"
343 " status Show current hostname settings\n"
344 " set-hostname NAME Set system hostname\n"
345 " set-icon-name NAME Set icon name for host\n"
346 " set-chassis NAME Set chassis type for host\n"
347 " set-deployment NAME Set deployment environment for host\n"
348 " set-location NAME Set location for host\n"
349 , program_invocation_short_name);
350 }
351
352 static int parse_argv(int argc, char *argv[]) {
353
354 enum {
355 ARG_VERSION = 0x100,
356 ARG_NO_ASK_PASSWORD,
357 ARG_TRANSIENT,
358 ARG_STATIC,
359 ARG_PRETTY
360 };
361
362 static const struct option options[] = {
363 { "help", no_argument, NULL, 'h' },
364 { "version", no_argument, NULL, ARG_VERSION },
365 { "transient", no_argument, NULL, ARG_TRANSIENT },
366 { "static", no_argument, NULL, ARG_STATIC },
367 { "pretty", no_argument, NULL, ARG_PRETTY },
368 { "host", required_argument, NULL, 'H' },
369 { "machine", required_argument, NULL, 'M' },
370 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
371 {}
372 };
373
374 int c;
375
376 assert(argc >= 0);
377 assert(argv);
378
379 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
380
381 switch (c) {
382
383 case 'h':
384 help();
385 return 0;
386
387 case ARG_VERSION:
388 return version();
389
390 case 'H':
391 arg_transport = BUS_TRANSPORT_REMOTE;
392 arg_host = optarg;
393 break;
394
395 case 'M':
396 arg_transport = BUS_TRANSPORT_MACHINE;
397 arg_host = optarg;
398 break;
399
400 case ARG_TRANSIENT:
401 arg_transient = true;
402 break;
403
404 case ARG_PRETTY:
405 arg_pretty = true;
406 break;
407
408 case ARG_STATIC:
409 arg_static = true;
410 break;
411
412 case ARG_NO_ASK_PASSWORD:
413 arg_ask_password = false;
414 break;
415
416 case '?':
417 return -EINVAL;
418
419 default:
420 assert_not_reached("Unhandled option");
421 }
422
423 return 1;
424 }
425
426 static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
427
428 static const struct {
429 const char* verb;
430 const enum {
431 MORE,
432 LESS,
433 EQUAL
434 } argc_cmp;
435 const int argc;
436 int (* const dispatch)(sd_bus *bus, char **args, unsigned n);
437 } verbs[] = {
438 { "status", LESS, 1, show_status },
439 { "set-hostname", EQUAL, 2, set_hostname },
440 { "set-icon-name", EQUAL, 2, set_icon_name },
441 { "set-chassis", EQUAL, 2, set_chassis },
442 { "set-deployment", EQUAL, 2, set_deployment },
443 { "set-location", EQUAL, 2, set_location },
444 };
445
446 int left;
447 unsigned i;
448
449 assert(argc >= 0);
450 assert(argv);
451
452 left = argc - optind;
453
454 if (left <= 0)
455 /* Special rule: no arguments means "status" */
456 i = 0;
457 else {
458 if (streq(argv[optind], "help")) {
459 help();
460 return 0;
461 }
462
463 for (i = 0; i < ELEMENTSOF(verbs); i++)
464 if (streq(argv[optind], verbs[i].verb))
465 break;
466
467 if (i >= ELEMENTSOF(verbs)) {
468 log_error("Unknown operation %s", argv[optind]);
469 return -EINVAL;
470 }
471 }
472
473 switch (verbs[i].argc_cmp) {
474
475 case EQUAL:
476 if (left != verbs[i].argc) {
477 log_error("Invalid number of arguments.");
478 return -EINVAL;
479 }
480
481 break;
482
483 case MORE:
484 if (left < verbs[i].argc) {
485 log_error("Too few arguments.");
486 return -EINVAL;
487 }
488
489 break;
490
491 case LESS:
492 if (left > verbs[i].argc) {
493 log_error("Too many arguments.");
494 return -EINVAL;
495 }
496
497 break;
498
499 default:
500 assert_not_reached("Unknown comparison operator.");
501 }
502
503 return verbs[i].dispatch(bus, argv + optind, left);
504 }
505
506 int main(int argc, char *argv[]) {
507 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
508 int r;
509
510 setlocale(LC_ALL, "");
511 log_parse_environment();
512 log_open();
513
514 r = parse_argv(argc, argv);
515 if (r <= 0)
516 goto finish;
517
518 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
519 if (r < 0) {
520 log_error_errno(r, "Failed to create bus connection: %m");
521 goto finish;
522 }
523
524 r = hostnamectl_main(bus, argc, argv);
525
526 finish:
527 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
528 }