X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fhostname%2Fhostnamed.c;h=c7f738a62c037b4ab890a223039d4676d7f09bbf;hp=5c767cde9722ed26650725aa24716c279a7c1a33;hb=b5af2aca120f1bf13cffc270803c2232918dd967;hpb=9be3455f55a5aa445ae8ae69301c54d19111d2e4 diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 5c767cde9..c7f738a62 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -22,8 +22,8 @@ #include #include #include -#include #include +#include #include "util.h" #include "strv.h" @@ -35,12 +35,16 @@ #include "bus-util.h" #include "event-util.h" +#define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:") + enum { PROP_HOSTNAME, PROP_STATIC_HOSTNAME, PROP_PRETTY_HOSTNAME, PROP_ICON_NAME, PROP_CHASSIS, + PROP_DEPLOYMENT, + PROP_LOCATION, PROP_KERNEL_NAME, PROP_KERNEL_RELEASE, PROP_KERNEL_VERSION, @@ -65,11 +69,11 @@ static void context_reset(Context *c) { } } -static void context_free(Context *c, sd_bus *bus) { +static void context_free(Context *c) { assert(c); context_reset(c); - bus_verify_polkit_async_registry_free(bus, c->polkit_registry); + bus_verify_polkit_async_registry_free(c->polkit_registry); } static int context_read_data(Context *c) { @@ -100,6 +104,8 @@ static int context_read_data(Context *c) { "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME], "ICON_NAME", &c->data[PROP_ICON_NAME], "CHASSIS", &c->data[PROP_CHASSIS], + "DEPLOYMENT", &c->data[PROP_DEPLOYMENT], + "LOCATION", &c->data[PROP_LOCATION], NULL); if (r < 0 && r != -ENOENT) return r; @@ -108,26 +114,20 @@ static int context_read_data(Context *c) { "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME], "CPE_NAME", &c->data[PROP_OS_CPE_NAME], NULL); + if (r == -ENOENT) { + r = parse_env_file("/usr/lib/os-release", NEWLINE, + "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME], + "CPE_NAME", &c->data[PROP_OS_CPE_NAME], + NULL); + } + if (r < 0 && r != -ENOENT) return r; return 0; } -static bool check_nss(void) { - void *dl; - - dl = dlopen("libnss_myhostname.so.2", RTLD_LAZY); - if (dl) { - dlclose(dl); - return true; - } - - return false; -} - static bool valid_chassis(const char *chassis) { - assert(chassis); return nulstr_contains( @@ -137,10 +137,17 @@ static bool valid_chassis(const char *chassis) { "laptop\0" "server\0" "tablet\0" - "handset\0", + "handset\0" + "watch\0", chassis); } +static bool valid_deployment(const char *deployment) { + assert(deployment); + + return in_charset(deployment, VALID_DEPLOYMENT_CHARS); +} + static const char* fallback_chassis(void) { int r; char *type; @@ -250,16 +257,36 @@ static char* context_fallback_icon_name(Context *c) { return strdup("computer"); } -static int context_write_data_hostname(Context *c) { + +static bool hostname_is_useful(const char *hn) { + return !isempty(hn) && !is_localhost(hn); +} + +static int context_update_kernel_hostname(Context *c) { + const char *static_hn; const char *hn; assert(c); - if (isempty(c->data[PROP_HOSTNAME])) - hn = "localhost"; - else + static_hn = c->data[PROP_STATIC_HOSTNAME]; + + /* /etc/hostname with something other than "localhost" + * has the highest preference ... */ + if (hostname_is_useful(static_hn)) + hn = static_hn; + + /* ... the transient host name, (ie: DHCP) comes next ...*/ + else if (!isempty(c->data[PROP_HOSTNAME])) hn = c->data[PROP_HOSTNAME]; + /* ... fallback to static "localhost.*" ignored above ... */ + else if (!isempty(static_hn)) + hn = static_hn; + + /* ... and the ultimate fallback */ + else + hn = "localhost"; + if (sethostname(hn, strlen(hn)) < 0) return -errno; @@ -285,7 +312,9 @@ static int context_write_data_machine_info(Context *c) { static const char * const name[_PROP_MAX] = { [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME", [PROP_ICON_NAME] = "ICON_NAME", - [PROP_CHASSIS] = "CHASSIS" + [PROP_CHASSIS] = "CHASSIS", + [PROP_DEPLOYMENT] = "DEPLOYMENT", + [PROP_LOCATION] = "LOCATION", }; _cleanup_strv_free_ char **l = NULL; @@ -293,12 +322,13 @@ static int context_write_data_machine_info(Context *c) { assert(c); - r = load_env_file("/etc/machine-info", NULL, &l); + r = load_env_file(NULL, "/etc/machine-info", NULL, &l); if (r < 0 && r != -ENOENT) return r; - for (p = PROP_PRETTY_HOSTNAME; p <= PROP_CHASSIS; p++) { - char *t, **u; + for (p = PROP_PRETTY_HOSTNAME; p <= PROP_LOCATION; p++) { + _cleanup_free_ char *t = NULL; + char **u; assert(name[p]); @@ -307,12 +337,11 @@ static int context_write_data_machine_info(Context *c) { continue; } - if (asprintf(&t, "%s=%s", name[p], strempty(c->data[p])) < 0) + t = strjoin(name[p], "=", c->data[p], NULL); + if (!t) return -ENOMEM; u = strv_env_set(l, t); - free(t); - if (!u) return -ENOMEM; @@ -321,7 +350,6 @@ static int context_write_data_machine_info(Context *c) { } if (strv_isempty(l)) { - if (unlink("/etc/machine-info") < 0) return errno == ENOENT ? 0 : -errno; @@ -398,7 +426,7 @@ static int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, s if (streq_ptr(name, c->data[PROP_HOSTNAME])) return sd_bus_reply_method_return(m, NULL); - r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-hostname", interactive, error, method_set_hostname, c); + r = bus_verify_polkit_async(bus, &c->polkit_registry, m, CAP_SYS_ADMIN, "org.freedesktop.hostname1.set-hostname", interactive, error, method_set_hostname, c); if (r < 0) return r; if (r == 0) @@ -411,7 +439,7 @@ static int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, s free(c->data[PROP_HOSTNAME]); c->data[PROP_HOSTNAME] = h; - r = context_write_data_hostname(c); + r = context_update_kernel_hostname(c); if (r < 0) { log_error("Failed to set host name: %s", strerror(-r)); return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r)); @@ -440,7 +468,7 @@ static int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *user if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME])) return sd_bus_reply_method_return(m, NULL); - r = bus_verify_polkit_async(bus, &c->polkit_registry, m, "org.freedesktop.hostname1.set-static-hostname", interactive, error, method_set_static_hostname, c); + r = bus_verify_polkit_async(bus, &c->polkit_registry, m, CAP_SYS_ADMIN, "org.freedesktop.hostname1.set-static-hostname", interactive, error, method_set_static_hostname, c); if (r < 0) return r; if (r == 0) @@ -463,6 +491,12 @@ static int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *user c->data[PROP_STATIC_HOSTNAME] = h; } + r = context_update_kernel_hostname(c); + if (r < 0) { + log_error("Failed to set host name: %s", strerror(-r)); + return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r)); + } + r = context_write_data_static_hostname(c); if (r < 0) { log_error("Failed to write static host name: %s", strerror(-r)); @@ -499,9 +533,10 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop * same time as the static one, use the same policy action for * both... */ - r = bus_verify_polkit_async(bus, &c->polkit_registry, m, prop == PROP_PRETTY_HOSTNAME ? - "org.freedesktop.hostname1.set-static-hostname" : - "org.freedesktop.hostname1.set-machine-info", interactive, error, cb, c); + r = bus_verify_polkit_async(bus, &c->polkit_registry, m, CAP_SYS_ADMIN, + prop == PROP_PRETTY_HOSTNAME ? + "org.freedesktop.hostname1.set-static-hostname" : + "org.freedesktop.hostname1.set-machine-info", interactive, error, cb, c); if (r < 0) return r; if (r == 0) @@ -518,11 +553,14 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop if (prop == PROP_ICON_NAME && !filename_is_safe(name)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name); - if (prop == PROP_PRETTY_HOSTNAME && - (string_has_cc(name) || chars_intersect(name, "\t"))) + if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name); if (prop == PROP_CHASSIS && !valid_chassis(name)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name); + if (prop == PROP_DEPLOYMENT && !valid_deployment(name)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name); + if (prop == PROP_LOCATION && string_has_cc(name, NULL)) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name); h = strdup(name); if (!h) @@ -540,11 +578,15 @@ static int set_machine_info(Context *c, sd_bus *bus, sd_bus_message *m, int prop log_info("Changed %s to '%s'", prop == PROP_PRETTY_HOSTNAME ? "pretty host name" : + prop == PROP_DEPLOYMENT ? "deployment" : + prop == PROP_LOCATION ? "location" : prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop])); sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" : - prop == PROP_CHASSIS ? "Chassis" : "IconName", NULL); + prop == PROP_DEPLOYMENT ? "Deployment" : + prop == PROP_LOCATION ? "Location" : + prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL); return sd_bus_reply_method_return(m, NULL); } @@ -561,6 +603,14 @@ static int method_set_chassis(sd_bus *bus, sd_bus_message *m, void *userdata, sd return set_machine_info(userdata, bus, m, PROP_CHASSIS, method_set_chassis, error); } +static int method_set_deployment(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) { + return set_machine_info(userdata, bus, m, PROP_DEPLOYMENT, method_set_deployment, error); +} + +static int method_set_location(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) { + return set_machine_info(userdata, bus, m, PROP_LOCATION, method_set_location, error); +} + static const sd_bus_vtable hostname_vtable[] = { SD_BUS_VTABLE_START(0), SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, 0), @@ -568,6 +618,8 @@ static const sd_bus_vtable hostname_vtable[] = { SD_BUS_PROPERTY("PrettyHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("Deployment", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_DEPLOYMENT, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), + SD_BUS_PROPERTY("Location", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_LOCATION, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), SD_BUS_PROPERTY("KernelName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_NAME, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("KernelRelease", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_RELEASE, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("KernelVersion", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_VERSION, SD_BUS_VTABLE_PROPERTY_CONST), @@ -578,11 +630,13 @@ static const sd_bus_vtable hostname_vtable[] = { SD_BUS_METHOD("SetPrettyHostname", "sb", NULL, method_set_pretty_hostname, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("SetIconName", "sb", NULL, method_set_icon_name, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("SetChassis", "sb", NULL, method_set_chassis, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("SetDeployment", "sb", NULL, method_set_deployment, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("SetLocation", "sb", NULL, method_set_location, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_VTABLE_END, }; static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; assert(c); @@ -621,9 +675,8 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { int main(int argc, char *argv[]) { Context context = {}; - _cleanup_event_unref_ sd_event *event = NULL; - _cleanup_bus_unref_ sd_bus *bus = NULL; + _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; log_set_target(LOG_TARGET_AUTO); @@ -639,9 +692,6 @@ int main(int argc, char *argv[]) { goto finish; } - if (!check_nss()) - log_warning("Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname!"); - if (argc != 1) { log_error("This program takes no arguments."); r = -EINVAL; @@ -673,7 +723,7 @@ int main(int argc, char *argv[]) { } finish: - context_free(&context, bus); + context_free(&context); return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }