1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
15 #include "alloc-util.h"
16 #include "architecture.h"
17 #include "bus-error.h"
19 #include "hostname-util.h"
20 #include "spawn-polkit-agent.h"
21 #include "terminal-util.h"
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;
32 typedef struct StatusInfo
{
34 const char *static_hostname
;
35 const char *pretty_hostname
;
36 const char *icon_name
;
38 const char *deployment
;
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
;
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 _cleanup_free_
char *formatted
= NULL
;
92 const char *t
= i
->os_pretty_name
;
95 if (terminal_urlify(i
->home_url
, i
->os_pretty_name
, &formatted
) >= 0)
99 printf(" Operating System: %s\n", t
);
102 if (!isempty(i
->os_cpe_name
))
103 printf(" CPE OS Name: %s\n", i
->os_cpe_name
);
105 if (!isempty(i
->kernel_name
) && !isempty(i
->kernel_release
))
106 printf(" Kernel: %s %s\n", i
->kernel_name
, i
->kernel_release
);
108 if (!isempty(i
->architecture
))
109 printf(" Architecture: %s\n", i
->architecture
);
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
;
119 r
= sd_bus_get_property(
121 "org.freedesktop.hostname1",
122 "/org/freedesktop/hostname1",
123 "org.freedesktop.hostname1",
125 &error
, &reply
, "s");
127 return log_error_errno(r
, "Could not get property: %s", bus_error_message(&error
, r
));
129 r
= sd_bus_message_read(reply
, "s", &s
);
131 return bus_log_parse_error(r
);
138 static int show_all_names(sd_bus
*bus
, sd_bus_error
*error
) {
139 StatusInfo info
= {};
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
) },
157 static const struct bus_properties_map manager_map
[] = {
158 { "Virtualization", "s", NULL
, offsetof(StatusInfo
, virtualization
) },
159 { "Architecture", "s", NULL
, offsetof(StatusInfo
, architecture
) },
163 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*host_message
= NULL
, *manager_message
= NULL
;
166 r
= bus_map_all_properties(bus
,
167 "org.freedesktop.hostname1",
168 "/org/freedesktop/hostname1",
177 r
= bus_map_all_properties(bus
,
178 "org.freedesktop.systemd1",
179 "/org/freedesktop/systemd1",
186 print_status_info(&info
);
191 static int show_status(int argc
, char **argv
, void *userdata
) {
192 sd_bus
*bus
= userdata
;
195 if (arg_pretty
|| arg_static
|| arg_transient
) {
198 if (!!arg_static
+ !!arg_pretty
+ !!arg_transient
> 1) {
199 log_error("Cannot query more than one name type at a time");
203 attr
= arg_pretty
? "PrettyHostname" :
204 arg_static
? "StaticHostname" : "Hostname";
206 return show_one_name(bus
, attr
);
208 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
210 r
= show_all_names(bus
, &error
);
212 return log_error_errno(r
, "Failed to query system properties: %s", bus_error_message(&error
, r
));
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
;
222 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
224 r
= sd_bus_call_method(
226 "org.freedesktop.hostname1",
227 "/org/freedesktop/hostname1",
228 "org.freedesktop.hostname1",
231 "sb", value
, arg_ask_password
);
233 log_error("Could not set property: %s", bus_error_message(&error
, -r
));
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
;
243 if (!arg_pretty
&& !arg_static
&& !arg_transient
)
244 arg_pretty
= arg_static
= arg_transient
= true;
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
252 if (arg_static
&& hostname_is_valid(hostname
, true))
253 p
= ""; /* No pretty hostname (as it is redundant), just a static one */
255 p
= hostname
; /* Use the passed name as pretty hostname */
257 r
= set_simple_string(bus
, "SetPrettyHostname", p
);
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
);
271 hostname
= hostname_cleanup(h
); /* Use the cleaned up name as static hostname */
275 r
= set_simple_string(bus
, "SetStaticHostname", hostname
);
281 r
= set_simple_string(bus
, "SetHostname", hostname
);
289 static int set_icon_name(int argc
, char **argv
, void *userdata
) {
290 return set_simple_string(userdata
, "SetIconName", argv
[1]);
293 static int set_chassis(int argc
, char **argv
, void *userdata
) {
294 return set_simple_string(userdata
, "SetChassis", argv
[1]);
297 static int set_deployment(int argc
, char **argv
, void *userdata
) {
298 return set_simple_string(userdata
, "SetDeployment", argv
[1]);
301 static int set_location(int argc
, char **argv
, void *userdata
) {
302 return set_simple_string(userdata
, "SetLocation", argv
[1]);
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"
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
);
328 static int verb_help(int argc
, char **argv
, void *userdata
) {
332 static int parse_argv(int argc
, char *argv
[]) {
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
},
359 while ((c
= getopt_long(argc
, argv
, "hH:M:", options
, NULL
)) >= 0)
370 arg_transport
= BUS_TRANSPORT_REMOTE
;
375 arg_transport
= BUS_TRANSPORT_MACHINE
;
380 arg_transient
= true;
391 case ARG_NO_ASK_PASSWORD
:
392 arg_ask_password
= false;
399 assert_not_reached("Unhandled option");
405 static int hostnamectl_main(sd_bus
*bus
, int argc
, char *argv
[]) {
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. */
418 return dispatch_verb(argc
, argv
, verbs
, bus
);
421 int main(int argc
, char *argv
[]) {
422 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
425 setlocale(LC_ALL
, "");
426 log_parse_environment();
429 r
= parse_argv(argc
, argv
);
433 r
= bus_connect_transport(arg_transport
, arg_host
, false, &bus
);
435 log_error_errno(r
, "Failed to create bus connection: %m");
439 r
= hostnamectl_main(bus
, argc
, argv
);
442 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;