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