]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
dbc4fbae | 2 | |
dbc4fbae | 3 | #include <getopt.h> |
a9cdc94f | 4 | #include <locale.h> |
3f6fd1ba LP |
5 | #include <stdbool.h> |
6 | #include <stdlib.h> | |
dbc4fbae | 7 | #include <string.h> |
dbc4fbae | 8 | |
b028f3e4 | 9 | #include "sd-bus.h" |
958b66ea | 10 | #include "sd-id128.h" |
3f6fd1ba | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
3f6fd1ba | 13 | #include "architecture.h" |
b028f3e4 | 14 | #include "bus-error.h" |
3f6fd1ba LP |
15 | #include "bus-util.h" |
16 | #include "hostname-util.h" | |
dbc4fbae | 17 | #include "spawn-polkit-agent.h" |
87adb0db | 18 | #include "terminal-util.h" |
3f6fd1ba | 19 | #include "util.h" |
f46bc484 | 20 | #include "verbs.h" |
dbc4fbae | 21 | |
dbc4fbae | 22 | static bool arg_ask_password = true; |
b028f3e4 | 23 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
7085053a | 24 | static char *arg_host = NULL; |
960787ae ZJS |
25 | static bool arg_transient = false; |
26 | static bool arg_pretty = false; | |
27 | static bool arg_static = false; | |
dbc4fbae | 28 | |
dbc4fbae | 29 | typedef struct StatusInfo { |
f37f8a61 YW |
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; | |
87adb0db | 43 | const char *home_url; |
dbc4fbae LP |
44 | } StatusInfo; |
45 | ||
46 | static void print_status_info(StatusInfo *i) { | |
39883f62 | 47 | sd_id128_t mid = {}, bid = {}; |
dbc4fbae | 48 | int r; |
dbc4fbae LP |
49 | |
50 | assert(i); | |
51 | ||
e9a2e453 | 52 | printf(" Static hostname: %s\n", strna(i->static_hostname)); |
dbc4fbae | 53 | |
c0b21b96 LP |
54 | if (!isempty(i->pretty_hostname) && |
55 | !streq_ptr(i->pretty_hostname, i->static_hostname)) | |
e9a2e453 | 56 | printf(" Pretty hostname: %s\n", i->pretty_hostname); |
c0b21b96 LP |
57 | |
58 | if (!isempty(i->hostname) && | |
59 | !streq_ptr(i->hostname, i->static_hostname)) | |
e9a2e453 | 60 | printf("Transient hostname: %s\n", i->hostname); |
dbc4fbae | 61 | |
41414fed LP |
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); | |
dbc4fbae LP |
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 | ||
e9a2e453 LP |
84 | if (!isempty(i->virtualization)) |
85 | printf(" Virtualization: %s\n", i->virtualization); | |
fe29f9d2 | 86 | |
87adb0db LP |
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 | } | |
fe29f9d2 | 98 | |
3448456b DH |
99 | if (!isempty(i->os_cpe_name)) |
100 | printf(" CPE OS Name: %s\n", i->os_cpe_name); | |
fe29f9d2 | 101 | |
fa4f8f9b DH |
102 | if (!isempty(i->kernel_name) && !isempty(i->kernel_release)) |
103 | printf(" Kernel: %s %s\n", i->kernel_name, i->kernel_release); | |
e9a2e453 LP |
104 | |
105 | if (!isempty(i->architecture)) | |
106 | printf(" Architecture: %s\n", i->architecture); | |
fe29f9d2 | 107 | |
dbc4fbae LP |
108 | } |
109 | ||
b028f3e4 | 110 | static int show_one_name(sd_bus *bus, const char* attr) { |
4afd3348 LP |
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; | |
b028f3e4 | 113 | const char *s; |
960787ae ZJS |
114 | int r; |
115 | ||
b028f3e4 | 116 | r = sd_bus_get_property( |
960787ae ZJS |
117 | bus, |
118 | "org.freedesktop.hostname1", | |
119 | "/org/freedesktop/hostname1", | |
b028f3e4 SP |
120 | "org.freedesktop.hostname1", |
121 | attr, | |
122 | &error, &reply, "s"); | |
f9e0eefc LP |
123 | if (r < 0) |
124 | return log_error_errno(r, "Could not get property: %s", bus_error_message(&error, r)); | |
960787ae | 125 | |
b028f3e4 SP |
126 | r = sd_bus_message_read(reply, "s", &s); |
127 | if (r < 0) | |
5b30bef8 | 128 | return bus_log_parse_error(r); |
960787ae | 129 | |
960787ae ZJS |
130 | printf("%s\n", s); |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
f9e0eefc | 135 | static int show_all_names(sd_bus *bus, sd_bus_error *error) { |
b92bea5d | 136 | StatusInfo info = {}; |
e9a2e453 LP |
137 | |
138 | static const struct bus_properties_map hostname_map[] = { | |
41414fed LP |
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) }, | |
87adb0db | 150 | { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) }, |
b028f3e4 SP |
151 | {} |
152 | }; | |
e9a2e453 LP |
153 | |
154 | static const struct bus_properties_map manager_map[] = { | |
41414fed LP |
155 | { "Virtualization", "s", NULL, offsetof(StatusInfo, virtualization) }, |
156 | { "Architecture", "s", NULL, offsetof(StatusInfo, architecture) }, | |
e9a2e453 LP |
157 | {} |
158 | }; | |
159 | ||
f37f8a61 | 160 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *host_message = NULL, *manager_message = NULL; |
b028f3e4 | 161 | int r; |
dbc4fbae | 162 | |
b028f3e4 SP |
163 | r = bus_map_all_properties(bus, |
164 | "org.freedesktop.hostname1", | |
165 | "/org/freedesktop/hostname1", | |
e9a2e453 | 166 | hostname_map, |
a7e4861c | 167 | 0, |
f9e0eefc | 168 | error, |
f37f8a61 | 169 | &host_message, |
9f6eb1cd | 170 | &info); |
dbc4fbae | 171 | if (r < 0) |
f37f8a61 | 172 | return r; |
dbc4fbae | 173 | |
f37f8a61 YW |
174 | r = bus_map_all_properties(bus, |
175 | "org.freedesktop.systemd1", | |
176 | "/org/freedesktop/systemd1", | |
177 | manager_map, | |
a7e4861c | 178 | 0, |
f37f8a61 YW |
179 | error, |
180 | &manager_message, | |
181 | &info); | |
e9a2e453 | 182 | |
dbc4fbae | 183 | print_status_info(&info); |
b028f3e4 | 184 | |
e9a2e453 | 185 | return r; |
dbc4fbae LP |
186 | } |
187 | ||
f46bc484 YW |
188 | static int show_status(int argc, char **argv, void *userdata) { |
189 | sd_bus *bus = userdata; | |
f9e0eefc LP |
190 | int r; |
191 | ||
960787ae ZJS |
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); | |
f9e0eefc LP |
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 | } | |
960787ae ZJS |
213 | } |
214 | ||
b028f3e4 | 215 | static int set_simple_string(sd_bus *bus, const char *method, const char *value) { |
4afd3348 | 216 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
b028f3e4 SP |
217 | int r = 0; |
218 | ||
8a4b13c5 | 219 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
b028f3e4 SP |
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) | |
4ae25393 YW |
230 | return log_error_errno(r, "Could not set property: %s", bus_error_message(&error, -r)); |
231 | ||
232 | return 0; | |
b028f3e4 SP |
233 | } |
234 | ||
f46bc484 | 235 | static int set_hostname(int argc, char **argv, void *userdata) { |
dbc4fbae | 236 | _cleanup_free_ char *h = NULL; |
f46bc484 YW |
237 | const char *hostname = argv[1]; |
238 | sd_bus *bus = userdata; | |
dbc4fbae LP |
239 | int r; |
240 | ||
960787ae ZJS |
241 | if (!arg_pretty && !arg_static && !arg_transient) |
242 | arg_pretty = arg_static = arg_transient = true; | |
243 | ||
244 | if (arg_pretty) { | |
fda2c5d2 LP |
245 | const char *p; |
246 | ||
2ae0858e LP |
247 | /* If the passed hostname is already valid, then assume the user doesn't know anything about pretty |
248 | * hostnames, so let's unset the pretty hostname, and just set the passed hostname as static/dynamic | |
fda2c5d2 | 249 | * hostname. */ |
2ae0858e LP |
250 | if (arg_static && hostname_is_valid(hostname, true)) |
251 | p = ""; /* No pretty hostname (as it is redundant), just a static one */ | |
252 | else | |
253 | p = hostname; /* Use the passed name as pretty hostname */ | |
fda2c5d2 | 254 | |
b028f3e4 | 255 | r = set_simple_string(bus, "SetPrettyHostname", p); |
dbc4fbae LP |
256 | if (r < 0) |
257 | return r; | |
2ae0858e LP |
258 | |
259 | /* Now that we set the pretty hostname, let's clean up the parameter and use that as static | |
260 | * hostname. If the hostname was already valid as static hostname, this will only chop off the trailing | |
261 | * dot if there is one. If it was not valid, then it will be made fully valid by truncating, dropping | |
629ff674 | 262 | * multiple dots, and dropping weird chars. Note that we clean the name up only if we also are |
2ae0858e LP |
263 | * supposed to set the pretty name. If the pretty name is not being set we assume the user knows what |
264 | * he does and pass the name as-is. */ | |
265 | h = strdup(hostname); | |
266 | if (!h) | |
267 | return log_oom(); | |
268 | ||
269 | hostname = hostname_cleanup(h); /* Use the cleaned up name as static hostname */ | |
dbc4fbae LP |
270 | } |
271 | ||
960787ae | 272 | if (arg_static) { |
b028f3e4 | 273 | r = set_simple_string(bus, "SetStaticHostname", hostname); |
dbc4fbae LP |
274 | if (r < 0) |
275 | return r; | |
276 | } | |
277 | ||
960787ae | 278 | if (arg_transient) { |
b028f3e4 | 279 | r = set_simple_string(bus, "SetHostname", hostname); |
dbc4fbae LP |
280 | if (r < 0) |
281 | return r; | |
282 | } | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
f46bc484 YW |
287 | static int set_icon_name(int argc, char **argv, void *userdata) { |
288 | return set_simple_string(userdata, "SetIconName", argv[1]); | |
dbc4fbae LP |
289 | } |
290 | ||
f46bc484 YW |
291 | static int set_chassis(int argc, char **argv, void *userdata) { |
292 | return set_simple_string(userdata, "SetChassis", argv[1]); | |
7871c8e9 LP |
293 | } |
294 | ||
f46bc484 YW |
295 | static int set_deployment(int argc, char **argv, void *userdata) { |
296 | return set_simple_string(userdata, "SetDeployment", argv[1]); | |
799298d6 JG |
297 | } |
298 | ||
f46bc484 YW |
299 | static int set_location(int argc, char **argv, void *userdata) { |
300 | return set_simple_string(userdata, "SetLocation", argv[1]); | |
41414fed LP |
301 | } |
302 | ||
f46bc484 | 303 | static int help(void) { |
7591abd4 LP |
304 | printf("%s [OPTIONS...] COMMAND ...\n\n" |
305 | "Query or change system hostname.\n\n" | |
dbc4fbae LP |
306 | " -h --help Show this help\n" |
307 | " --version Show package version\n" | |
7591abd4 | 308 | " --no-ask-password Do not prompt for password\n" |
b028f3e4 | 309 | " -H --host=[USER@]HOST Operate on remote host\n" |
a86a47ce LP |
310 | " -M --machine=CONTAINER Operate on local container\n" |
311 | " --transient Only set transient hostname\n" | |
312 | " --static Only set static hostname\n" | |
313 | " --pretty Only set pretty hostname\n\n" | |
dbc4fbae | 314 | "Commands:\n" |
7591abd4 LP |
315 | " status Show current hostname settings\n" |
316 | " set-hostname NAME Set system hostname\n" | |
7871c8e9 | 317 | " set-icon-name NAME Set icon name for host\n" |
799298d6 | 318 | " set-chassis NAME Set chassis type for host\n" |
601185b4 | 319 | " set-deployment NAME Set deployment environment for host\n" |
41414fed | 320 | " set-location NAME Set location for host\n" |
601185b4 | 321 | , program_invocation_short_name); |
f46bc484 YW |
322 | |
323 | return 0; | |
324 | } | |
325 | ||
326 | static int verb_help(int argc, char **argv, void *userdata) { | |
327 | return help(); | |
dbc4fbae LP |
328 | } |
329 | ||
330 | static int parse_argv(int argc, char *argv[]) { | |
331 | ||
332 | enum { | |
333 | ARG_VERSION = 0x100, | |
334 | ARG_NO_ASK_PASSWORD, | |
960787ae ZJS |
335 | ARG_TRANSIENT, |
336 | ARG_STATIC, | |
337 | ARG_PRETTY | |
dbc4fbae LP |
338 | }; |
339 | ||
340 | static const struct option options[] = { | |
341 | { "help", no_argument, NULL, 'h' }, | |
342 | { "version", no_argument, NULL, ARG_VERSION }, | |
eb9da376 LP |
343 | { "transient", no_argument, NULL, ARG_TRANSIENT }, |
344 | { "static", no_argument, NULL, ARG_STATIC }, | |
345 | { "pretty", no_argument, NULL, ARG_PRETTY }, | |
dbc4fbae | 346 | { "host", required_argument, NULL, 'H' }, |
b028f3e4 | 347 | { "machine", required_argument, NULL, 'M' }, |
dbc4fbae | 348 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, |
eb9da376 | 349 | {} |
dbc4fbae LP |
350 | }; |
351 | ||
352 | int c; | |
353 | ||
354 | assert(argc >= 0); | |
355 | assert(argv); | |
356 | ||
601185b4 | 357 | while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) |
dbc4fbae LP |
358 | |
359 | switch (c) { | |
360 | ||
361 | case 'h': | |
f46bc484 | 362 | return help(); |
dbc4fbae LP |
363 | |
364 | case ARG_VERSION: | |
3f6fd1ba | 365 | return version(); |
dbc4fbae | 366 | |
b028f3e4 SP |
367 | case 'H': |
368 | arg_transport = BUS_TRANSPORT_REMOTE; | |
369 | arg_host = optarg; | |
dbc4fbae LP |
370 | break; |
371 | ||
b028f3e4 | 372 | case 'M': |
de33fc62 | 373 | arg_transport = BUS_TRANSPORT_MACHINE; |
b028f3e4 | 374 | arg_host = optarg; |
dbc4fbae LP |
375 | break; |
376 | ||
960787ae ZJS |
377 | case ARG_TRANSIENT: |
378 | arg_transient = true; | |
dbc4fbae LP |
379 | break; |
380 | ||
960787ae ZJS |
381 | case ARG_PRETTY: |
382 | arg_pretty = true; | |
dbc4fbae LP |
383 | break; |
384 | ||
960787ae ZJS |
385 | case ARG_STATIC: |
386 | arg_static = true; | |
dbc4fbae LP |
387 | break; |
388 | ||
59f432ea LP |
389 | case ARG_NO_ASK_PASSWORD: |
390 | arg_ask_password = false; | |
391 | break; | |
392 | ||
dbc4fbae LP |
393 | case '?': |
394 | return -EINVAL; | |
395 | ||
396 | default: | |
eb9da376 | 397 | assert_not_reached("Unhandled option"); |
dbc4fbae | 398 | } |
dbc4fbae | 399 | |
dbc4fbae LP |
400 | return 1; |
401 | } | |
402 | ||
b028f3e4 | 403 | static int hostnamectl_main(sd_bus *bus, int argc, char *argv[]) { |
dbc4fbae | 404 | |
f46bc484 YW |
405 | static const Verb verbs[] = { |
406 | { "status", VERB_ANY, 1, VERB_DEFAULT, show_status }, | |
407 | { "set-hostname", 2, 2, 0, set_hostname }, | |
408 | { "set-icon-name", 2, 2, 0, set_icon_name }, | |
409 | { "set-chassis", 2, 2, 0, set_chassis }, | |
410 | { "set-deployment", 2, 2, 0, set_deployment }, | |
411 | { "set-location", 2, 2, 0, set_location }, | |
412 | { "help", VERB_ANY, VERB_ANY, 0, verb_help }, /* Not documented, but supported since it is created. */ | |
413 | {} | |
dbc4fbae LP |
414 | }; |
415 | ||
f46bc484 | 416 | return dispatch_verb(argc, argv, verbs, bus); |
dbc4fbae LP |
417 | } |
418 | ||
419 | int main(int argc, char *argv[]) { | |
4afd3348 | 420 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
84f6181c | 421 | int r; |
dbc4fbae | 422 | |
a9cdc94f | 423 | setlocale(LC_ALL, ""); |
dbc4fbae LP |
424 | log_parse_environment(); |
425 | log_open(); | |
426 | ||
427 | r = parse_argv(argc, argv); | |
b028f3e4 | 428 | if (r <= 0) |
dbc4fbae | 429 | goto finish; |
b028f3e4 | 430 | |
266f3e26 | 431 | r = bus_connect_transport(arg_transport, arg_host, false, &bus); |
b028f3e4 | 432 | if (r < 0) { |
da927ba9 | 433 | log_error_errno(r, "Failed to create bus connection: %m"); |
dbc4fbae LP |
434 | goto finish; |
435 | } | |
436 | ||
b028f3e4 | 437 | r = hostnamectl_main(bus, argc, argv); |
dbc4fbae LP |
438 | |
439 | finish: | |
5567fafb | 440 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
dbc4fbae | 441 | } |