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