hostnamed.c revision f200e8bb34d08616dbcb03d6503d0610f4e7e5bc
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2011 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringtypedef struct Context {
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering for (p = 0; p < _PROP_MAX; p++) {
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersenstatic void context_free(Context *c, sd_bus *bus) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus_verify_polkit_async_registry_free(bus, c->polkit_registry);
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poetteringstatic int context_read_data(Context *c) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->data[PROP_HOSTNAME] = gethostname_malloc();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = read_one_line_file("/etc/hostname", &c->data[PROP_STATIC_HOSTNAME]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0 && r != -ENOENT)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = parse_env_file("/etc/machine-info", NEWLINE,
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (r < 0 && r != -ENOENT)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic bool check_nss(void) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering dl = dlopen("libnss_myhostname.so.2", RTLD_LAZY);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic bool valid_chassis(const char *chassis) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic const char* fallback_chassis(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return "container";
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* We only list the really obvious cases here as the ACPI data
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * is not really super reliable.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return "desktop";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return "laptop";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return "server";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return "tablet";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering /* We only list the really obvious cases here. The DMI data is
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering unreliable enough, so let's not do any additional guesswork
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering on top of that.
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering See the SMBIOS Specification 2.7.1 section 7.4.1 for
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering details about the values listed here:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen return "desktop";
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return "laptop";
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return "handset";
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return "server";
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic char* context_fallback_icon_name(Context *c) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return strappend("computer-", c->data[PROP_CHASSIS]);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poetteringstatic int context_write_data_hostname(Context *c) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int context_write_data_static_hostname(Context *c) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (isempty(c->data[PROP_STATIC_HOSTNAME])) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int context_write_data_machine_info(Context *c) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering static const char * const name[_PROP_MAX] = {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = load_env_file("/etc/machine-info", NULL, &l);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (r < 0 && r != -ENOENT)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering for (p = PROP_PRETTY_HOSTNAME; p <= PROP_CHASSIS; p++) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (asprintf(&t, "%s=%s", name[p], strempty(c->data[p])) < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return write_env_file_label("/etc/machine-info", l);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering name = n = context_fallback_icon_name(c);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return sd_bus_message_append(reply, "s", name);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return sd_bus_message_append(reply, "s", name);
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poetteringstatic int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = sd_bus_message_read(m, "sb", &name, &interactive);
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (streq_ptr(name, c->data[PROP_HOSTNAME]))
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return sd_bus_reply_method_return(m, NULL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-hostname", interactive, error, method_set_hostname, c);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_error("Failed to set host name: %s", strerror(-r));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_info("Changed host name to '%s'", strna(c->data[PROP_HOSTNAME]));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "Hostname", NULL);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return sd_bus_reply_method_return(m, NULL);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_message_read(m, "sb", &name, &interactive);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return sd_bus_reply_method_return(m, NULL);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-static-hostname", interactive, error, method_set_static_hostname, c);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = context_write_data_static_hostname(c);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_error("Failed to write static host name: %s", strerror(-r));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %s", strerror(-r));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_info("Changed static host name to '%s'", strna(c->data[PROP_STATIC_HOSTNAME]));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "StaticHostname", NULL);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return sd_bus_reply_method_return(m, NULL);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poetteringstatic int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop, sd_bus_message_handler_t cb, sd_bus_error *error) {
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_message_read(m, "sb", &name, &interactive);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return sd_bus_reply_method_return(m, NULL);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering /* Since the pretty hostname should always be changed at the
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering * same time as the static one, use the same policy action for
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = bus_verify_polkit_async(bus, &c->polkit_registry, m, prop == PROP_PRETTY_HOSTNAME ?
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "org.freedesktop.hostname1.set-static-hostname" :
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering "org.freedesktop.hostname1.set-machine-info", interactive, error, cb, c);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers /* The icon name might ultimately be used as file
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * name, so better be safe than sorry */
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (prop == PROP_ICON_NAME && !filename_is_safe(name))
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (string_has_cc(name) || chars_intersect(name, "\t")))
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (prop == PROP_CHASSIS && !valid_chassis(name))
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to write machine info: %s", strerror(-r));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %s", strerror(-r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering prop == PROP_PRETTY_HOSTNAME ? "pretty host name" :
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop]));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" :
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering prop == PROP_CHASSIS ? "Chassis" : "IconName", NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return sd_bus_reply_method_return(m, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int method_set_pretty_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return set_machine_info(userdata, bus, m, PROP_PRETTY_HOSTNAME, method_set_pretty_hostname, error);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int method_set_icon_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return set_machine_info(userdata, bus, m, PROP_ICON_NAME, method_set_icon_name, error);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic int method_set_chassis(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return set_machine_info(userdata, bus, m, PROP_CHASSIS, method_set_chassis, error);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const sd_bus_vtable hostname_vtable[] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, 0),
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SD_BUS_PROPERTY("StaticHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_STATIC_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SD_BUS_PROPERTY("PrettyHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering SD_BUS_METHOD("SetHostname", "sb", NULL, method_set_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering SD_BUS_METHOD("SetStaticHostname", "sb", NULL, method_set_static_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering SD_BUS_METHOD("SetPrettyHostname", "sb", NULL, method_set_pretty_hostname, SD_BUS_VTABLE_UNPRIVILEGED),
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering SD_BUS_METHOD("SetIconName", "sb", NULL, method_set_icon_name, SD_BUS_VTABLE_UNPRIVILEGED),
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering SD_BUS_METHOD("SetChassis", "sb", NULL, method_set_chassis, SD_BUS_VTABLE_UNPRIVILEGED),
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_error("Failed to get system bus connection: %s", strerror(-r));
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = sd_bus_add_object_vtable(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_error("Failed to register object: %s", strerror(-r));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_request_name(bus, "org.freedesktop.hostname1", 0);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering log_error("Failed to register name: %s", strerror(-r));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering log_error("Failed to attach bus to event loop: %s", strerror(-r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek log_error("This program takes no arguments.");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_warning("Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname!");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_error("This program takes no arguments.");
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering log_error("Failed to allocate event loop: %s", strerror(-r));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen log_error("Failed to read hostname and machine information: %s", strerror(-r));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to run event loop: %s", strerror(-r));