1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
17 #include "alloc-util.h"
18 #include "architecture.h"
19 #include "bus-error.h"
21 #include "hostname-util.h"
22 #include "spawn-polkit-agent.h"
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;
33 typedef struct StatusInfo
{
35 const char *static_hostname
;
36 const char *pretty_hostname
;
37 const char *icon_name
;
39 const char *deployment
;
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
;
49 static void print_status_info(StatusInfo
*i
) {
50 sd_id128_t mid
= {}, bid
= {};
55 printf(" Static hostname: %s\n", strna(i
->static_hostname
));
57 if (!isempty(i
->pretty_hostname
) &&
58 !streq_ptr(i
->pretty_hostname
, i
->static_hostname
))
59 printf(" Pretty hostname: %s\n", i
->pretty_hostname
);
61 if (!isempty(i
->hostname
) &&
62 !streq_ptr(i
->hostname
, i
->static_hostname
))
63 printf("Transient hostname: %s\n", i
->hostname
);
65 if (!isempty(i
->icon_name
))
66 printf(" Icon name: %s\n",
69 if (!isempty(i
->chassis
))
70 printf(" Chassis: %s\n",
73 if (!isempty(i
->deployment
))
74 printf(" Deployment: %s\n", i
->deployment
);
76 if (!isempty(i
->location
))
77 printf(" Location: %s\n", i
->location
);
79 r
= sd_id128_get_machine(&mid
);
81 printf(" Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(mid
));
83 r
= sd_id128_get_boot(&bid
);
85 printf(" Boot ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(bid
));
87 if (!isempty(i
->virtualization
))
88 printf(" Virtualization: %s\n", i
->virtualization
);
90 if (!isempty(i
->os_pretty_name
))
91 printf(" Operating System: %s\n", i
->os_pretty_name
);
93 if (!isempty(i
->os_cpe_name
))
94 printf(" CPE OS Name: %s\n", i
->os_cpe_name
);
96 if (!isempty(i
->kernel_name
) && !isempty(i
->kernel_release
))
97 printf(" Kernel: %s %s\n", i
->kernel_name
, i
->kernel_release
);
99 if (!isempty(i
->architecture
))
100 printf(" Architecture: %s\n", i
->architecture
);
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
;
110 r
= sd_bus_get_property(
112 "org.freedesktop.hostname1",
113 "/org/freedesktop/hostname1",
114 "org.freedesktop.hostname1",
116 &error
, &reply
, "s");
118 return log_error_errno(r
, "Could not get property: %s", bus_error_message(&error
, r
));
120 r
= sd_bus_message_read(reply
, "s", &s
);
122 return bus_log_parse_error(r
);
129 static int show_all_names(sd_bus
*bus
, sd_bus_error
*error
) {
130 StatusInfo info
= {};
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
) },
147 static const struct bus_properties_map manager_map
[] = {
148 { "Virtualization", "s", NULL
, offsetof(StatusInfo
, virtualization
) },
149 { "Architecture", "s", NULL
, offsetof(StatusInfo
, architecture
) },
153 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*host_message
= NULL
, *manager_message
= NULL
;
156 r
= bus_map_all_properties(bus
,
157 "org.freedesktop.hostname1",
158 "/org/freedesktop/hostname1",
167 r
= bus_map_all_properties(bus
,
168 "org.freedesktop.systemd1",
169 "/org/freedesktop/systemd1",
176 print_status_info(&info
);
181 static int show_status(int argc
, char **argv
, void *userdata
) {
182 sd_bus
*bus
= userdata
;
185 if (arg_pretty
|| arg_static
|| arg_transient
) {
188 if (!!arg_static
+ !!arg_pretty
+ !!arg_transient
> 1) {
189 log_error("Cannot query more than one name type at a time");
193 attr
= arg_pretty
? "PrettyHostname" :
194 arg_static
? "StaticHostname" : "Hostname";
196 return show_one_name(bus
, attr
);
198 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
200 r
= show_all_names(bus
, &error
);
202 return log_error_errno(r
, "Failed to query system properties: %s", bus_error_message(&error
, r
));
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
;
212 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
214 r
= sd_bus_call_method(
216 "org.freedesktop.hostname1",
217 "/org/freedesktop/hostname1",
218 "org.freedesktop.hostname1",
221 "sb", value
, arg_ask_password
);
223 log_error("Could not set property: %s", bus_error_message(&error
, -r
));
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
;
233 if (!arg_pretty
&& !arg_static
&& !arg_transient
)
234 arg_pretty
= arg_static
= arg_transient
= true;
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
242 if (arg_static
&& hostname_is_valid(hostname
, true))
243 p
= ""; /* No pretty hostname (as it is redundant), just a static one */
245 p
= hostname
; /* Use the passed name as pretty hostname */
247 r
= set_simple_string(bus
, "SetPrettyHostname", p
);
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
);
261 hostname
= hostname_cleanup(h
); /* Use the cleaned up name as static hostname */
265 r
= set_simple_string(bus
, "SetStaticHostname", hostname
);
271 r
= set_simple_string(bus
, "SetHostname", hostname
);
279 static int set_icon_name(int argc
, char **argv
, void *userdata
) {
280 return set_simple_string(userdata
, "SetIconName", argv
[1]);
283 static int set_chassis(int argc
, char **argv
, void *userdata
) {
284 return set_simple_string(userdata
, "SetChassis", argv
[1]);
287 static int set_deployment(int argc
, char **argv
, void *userdata
) {
288 return set_simple_string(userdata
, "SetDeployment", argv
[1]);
291 static int set_location(int argc
, char **argv
, void *userdata
) {
292 return set_simple_string(userdata
, "SetLocation", argv
[1]);
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"
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
);
318 static int verb_help(int argc
, char **argv
, void *userdata
) {
322 static int parse_argv(int argc
, char *argv
[]) {
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
},
349 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
360 arg_transport
= BUS_TRANSPORT_REMOTE
;
365 arg_transport
= BUS_TRANSPORT_MACHINE
;
370 arg_transient
= true;
381 case ARG_NO_ASK_PASSWORD
:
382 arg_ask_password
= false;
389 assert_not_reached("Unhandled option");
395 static int hostnamectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
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. */
408 return dispatch_verb(argc
, argv
, verbs
, bus
);
411 int main(int argc
, char *argv
[]) {
412 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
415 setlocale(LC_ALL
, "");
416 log_parse_environment();
419 r
= parse_argv(argc
, argv
);
423 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
425 log_error_errno(r
, "Failed to create bus connection: %m");
429 r
= hostnamectl_main(bus
, argc
, argv
);
432 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;