]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/hostname/hostnamectl.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / hostname / hostnamectl.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6 ***/
7
8 #include <getopt.h>
9 #include <locale.h>
10 #include <stdbool.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "sd-bus.h"
15 #include "sd-id128.h"
16
17 #include "alloc-util.h"
18 #include "architecture.h"
19 #include "bus-error.h"
20 #include "bus-util.h"
21 #include "hostname-util.h"
22 #include "spawn-polkit-agent.h"
23 #include "util.h"
24 #include "verbs.h"
25
26 static bool arg_ask_password = true;
27 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
28 static char *arg_host = NULL;
29 static bool arg_transient = false;
30 static bool arg_pretty = false;
31 static bool arg_static = false;
32
33 typedef struct StatusInfo {
34 const char *hostname;
35 const char *static_hostname;
36 const char *pretty_hostname;
37 const char *icon_name;
38 const char *chassis;
39 const char *deployment;
40 const char *location;
41 const char *kernel_name;
42 const char *kernel_release;
43 const char *os_pretty_name;
44 const char *os_cpe_name;
45 const char *virtualization;
46 const char *architecture;
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 printf(" Operating System: %s\n", i->os_pretty_name);
92
93 if (!isempty(i->os_cpe_name))
94 printf(" CPE OS Name: %s\n", i->os_cpe_name);
95
96 if (!isempty(i->kernel_name) && !isempty(i->kernel_release))
97 printf(" Kernel: %s %s\n", i->kernel_name, i->kernel_release);
98
99 if (!isempty(i->architecture))
100 printf(" Architecture: %s\n", i->architecture);
101
102 }
103
104 static int show_one_name(sd_bus *bus, const char* attr) {
105 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
106 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
107 const char *s;
108 int r;
109
110 r = sd_bus_get_property(
111 bus,
112 "org.freedesktop.hostname1",
113 "/org/freedesktop/hostname1",
114 "org.freedesktop.hostname1",
115 attr,
116 &error, &reply, "s");
117 if (r < 0)
118 return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r));
119
120 r = sd_bus_message_read(reply, "s", &s);
121 if (r < 0)
122 return bus_log_parse_error(r);
123
124 printf("%s\n", s);
125
126 return 0;
127 }
128
129 static int show_all_names(sd_bus *bus, sd_bus_error *error) {
130 StatusInfo info = {};
131
132 static const struct bus_properties_map hostname_map[] = {
133 { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) },
134 { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) },
135 { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) },
136 { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) },
137 { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) },
138 { "Deployment", "s", NULL, offsetof(StatusInfo, deployment) },
139 { "Location", "s", NULL, offsetof(StatusInfo, location) },
140 { "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) },
141 { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) },
142 { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) },
143 { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) },
144 {}
145 };
146
147 static const struct bus_properties_map manager_map[] = {
148 { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) },
149 { "Architecture", "s", NULL, offsetof(StatusInfo, architecture) },
150 {}
151 };
152
153 _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL;
154 int r;
155
156 r = bus_map_all_properties(bus,
157 "org.freedesktop.hostname1",
158 "/org/freedesktop/hostname1",
159 hostname_map,
160 0,
161 error,
162 &host_message,
163 &info);
164 if (r < 0)
165 return r;
166
167 r = bus_map_all_properties(bus,
168 "org.freedesktop.systemd1",
169 "/org/freedesktop/systemd1",
170 manager_map,
171 0,
172 error,
173 &manager_message,
174 &info);
175
176 print_status_info(&info);
177
178 return r;
179 }
180
181 static int show_status(int argc, char **argv, void *userdata) {
182 sd_bus *bus = userdata;
183 int r;
184
185 if (arg_pretty || arg_static || arg_transient) {
186 const char *attr;
187
188 if (!!arg_static + !!arg_pretty + !!arg_transient > 1) {
189 log_error("Cannot query more than one name type at a time");
190 return -EINVAL;
191 }
192
193 attr = arg_pretty ? "PrettyHostname" :
194 arg_static ? "StaticHostname" : "Hostname";
195
196 return show_one_name(bus, attr);
197 } else {
198 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
199
200 r = show_all_names(bus, &error);
201 if (r < 0)
202 return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r));
203
204 return 0;
205 }
206 }
207
208 static int set_simple_string(sd_bus *bus, const char *method, const char *value) {
209 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
210 int r = 0;
211
212 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
213
214 r = sd_bus_call_method(
215 bus,
216 "org.freedesktop.hostname1",
217 "/org/freedesktop/hostname1",
218 "org.freedesktop.hostname1",
219 method,
220 &error, NULL,
221 "sb", value, arg_ask_password);
222 if (r < 0)
223 log_error("Could not set property: %s", bus_error_message(&error, -r));
224 return r;
225 }
226
227 static int set_hostname(int argc, char **argv, void *userdata) {
228 _cleanup_free_ char *h = NULL;
229 const char *hostname = argv[1];
230 sd_bus *bus = userdata;
231 int r;
232
233 if (!arg_pretty && !arg_static && !arg_transient)
234 arg_pretty = arg_static = arg_transient = true;
235
236 if (arg_pretty) {
237 const char *p;
238
239 /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty
240 * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic
241 * hostname. */
242 if (arg_static && hostname_is_valid(hostname, true))
243 p = ""; /* No pretty hostname (as it is redundant), just a static one */
244 else
245 p = hostname; /* Use the passed name as pretty hostname */
246
247 r = set_simple_string(bus, "SetPrettyHostname", p);
248 if (r < 0)
249 return r;
250
251 /* Now that we set the pretty hostname, let's clean up the parameter and use that as static
252 * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing
253 * dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping
254 * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are
255 * supposed to set the pretty name. If the pretty name is not being set we assume the user knows what
256 * he does and pass the name as-is. */
257 h = strdup(hostname);
258 if (!h)
259 return log_oom();
260
261 hostname = hostname_cleanup(h); /* Use the cleaned up name as static hostname */
262 }
263
264 if (arg_static) {
265 r = set_simple_string(bus, "SetStaticHostname", hostname);
266 if (r < 0)
267 return r;
268 }
269
270 if (arg_transient) {
271 r = set_simple_string(bus, "SetHostname", hostname);
272 if (r < 0)
273 return r;
274 }
275
276 return 0;
277 }
278
279 static int set_icon_name(int argc, char **argv, void *userdata) {
280 return set_simple_string(userdata, "SetIconName", argv[1]);
281 }
282
283 static int set_chassis(int argc, char **argv, void *userdata) {
284 return set_simple_string(userdata, "SetChassis", argv[1]);
285 }
286
287 static int set_deployment(int argc, char **argv, void *userdata) {
288 return set_simple_string(userdata, "SetDeployment", argv[1]);
289 }
290
291 static int set_location(int argc, char **argv, void *userdata) {
292 return set_simple_string(userdata, "SetLocation", argv[1]);
293 }
294
295 static int help(void) {
296 printf("%s [OPTIONS...] COMMAND ...\n\n"
297 "Query or change system hostname.\n\n"
298 " -h --help Show this help\n"
299 " --version Show package version\n"
300 " --no-ask-password Do not prompt for password\n"
301 " -H --host=[USER@]HOST Operate on remote host\n"
302 " -M --machine=CONTAINER Operate on local container\n"
303 " --transient Only set transient hostname\n"
304 " --static Only set static hostname\n"
305 " --pretty Only set pretty hostname\n\n"
306 "Commands:\n"
307 " status Show current hostname settings\n"
308 " set-hostname NAME Set system hostname\n"
309 " set-icon-name NAME Set icon name for host\n"
310 " set-chassis NAME Set chassis type for host\n"
311 " set-deployment NAME Set deployment environment for host\n"
312 " set-location NAME Set location for host\n"
313 , program_invocation_short_name);
314
315 return 0;
316 }
317
318 static int verb_help(int argc, char **argv, void *userdata) {
319 return help();
320 }
321
322 static int parse_argv(int argc, char *argv[]) {
323
324 enum {
325 ARG_VERSION = 0x100,
326 ARG_NO_ASK_PASSWORD,
327 ARG_TRANSIENT,
328 ARG_STATIC,
329 ARG_PRETTY
330 };
331
332 static const struct option options[] = {
333 { "help", no_argument, NULL, 'h' },
334 { "version", no_argument, NULL, ARG_VERSION },
335 { "transient", no_argument, NULL, ARG_TRANSIENT },
336 { "static", no_argument, NULL, ARG_STATIC },
337 { "pretty", no_argument, NULL, ARG_PRETTY },
338 { "host", required_argument, NULL, 'H' },
339 { "machine", required_argument, NULL, 'M' },
340 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
341 {}
342 };
343
344 int c;
345
346 assert(argc >= 0);
347 assert(argv);
348
349 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
350
351 switch (c) {
352
353 case 'h':
354 return help();
355
356 case ARG_VERSION:
357 return version();
358
359 case 'H':
360 arg_transport = BUS_TRANSPORT_REMOTE;
361 arg_host = optarg;
362 break;
363
364 case 'M':
365 arg_transport = BUS_TRANSPORT_MACHINE;
366 arg_host = optarg;
367 break;
368
369 case ARG_TRANSIENT:
370 arg_transient = true;
371 break;
372
373 case ARG_PRETTY:
374 arg_pretty = true;
375 break;
376
377 case ARG_STATIC:
378 arg_static = true;
379 break;
380
381 case ARG_NO_ASK_PASSWORD:
382 arg_ask_password = false;
383 break;
384
385 case '?':
386 return -EINVAL;
387
388 default:
389 assert_not_reached("Unhandled option");
390 }
391
392 return 1;
393 }
394
395 static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) {
396
397 static const Verb verbs[] = {
398 { "status", VERB_ANY, 1, VERB_DEFAULT, show_status },
399 { "set-hostname", 2, 2, 0, set_hostname },
400 { "set-icon-name", 2, 2, 0, set_icon_name },
401 { "set-chassis", 2, 2, 0, set_chassis },
402 { "set-deployment", 2, 2, 0, set_deployment },
403 { "set-location", 2, 2, 0, set_location },
404 { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */
405 {}
406 };
407
408 return dispatch_verb(argc, argv, verbs, bus);
409 }
410
411 int main(int argc, char *argv[]) {
412 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
413 int r;
414
415 setlocale(LC_ALL, "");
416 log_parse_environment();
417 log_open();
418
419 r = parse_argv(argc, argv);
420 if (r <= 0)
421 goto finish;
422
423 r = bus_connect_transport(arg_transport, arg_host, false, &bus);
424 if (r < 0) {
425 log_error_errno(r, "Failed to create bus connection: %m");
426 goto finish;
427 }
428
429 r = hostnamectl_main(bus, argc, argv);
430
431 finish:
432 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
433 }