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