hostnamed.c revision f200e8bb34d08616dbcb03d6503d0610f4e7e5bc
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2011 Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart 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
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
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/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
8bdbb8d9cbe1d35708385573d70984ab4533812dLennart Poettering#include <errno.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <string.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <dlfcn.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "strv.h"
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering#include "def.h"
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include "virt.h"
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering#include "env-util.h"
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering#include "fileio-label.h"
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering#include "label.h"
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering#include "bus-util.h"
d538bfc71331d2e48363283decd77a61599021dbCristian Rodríguez#include "event-util.h"
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringenum {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering PROP_HOSTNAME,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering PROP_STATIC_HOSTNAME,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering PROP_PRETTY_HOSTNAME,
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering PROP_ICON_NAME,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen PROP_CHASSIS,
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen _PROP_MAX
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringtypedef struct Context {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering char *data[_PROP_MAX];
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering Hashmap *polkit_registry;
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering} Context;
04d39279245834494baccfdb9349db8bf80abd13Lennart Poettering
023fb90b83871a15ef7f57e8cd126e3426f99b9eLennart Poetteringstatic void context_reset(Context *c) {
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering int p;
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
f2cbe59e113f08549949a76ac5b9b3972df4cc30Lennart Poettering assert(c);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering for (p = 0; p < _PROP_MAX; p++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(c->data[p]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->data[p] = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersenstatic void context_free(Context *c, sd_bus *bus) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert(c);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering context_reset(c);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering bus_verify_polkit_async_registry_free(bus, c->polkit_registry);
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering}
785890acf6d629ff881a1f065f431df1b7fc8c7aLennart Poettering
d8f52ed25a9edce75fda5251c977b7898e33887eLennart Poetteringstatic int context_read_data(Context *c) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering int r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering assert(c);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering context_reset(c);
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->data[PROP_HOSTNAME] = gethostname_malloc();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (!c->data[PROP_HOSTNAME])
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -ENOMEM;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = read_one_line_file("/etc/hostname", &c->data[PROP_STATIC_HOSTNAME]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0 && r != -ENOENT)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = parse_env_file("/etc/machine-info", NEWLINE,
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME],
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "ICON_NAME", &c->data[PROP_ICON_NAME],
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering "CHASSIS", &c->data[PROP_CHASSIS],
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering NULL);
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering if (r < 0 && r != -ENOENT)
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return r;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering return 0;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering}
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poetteringstatic bool check_nss(void) {
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering void *dl;
acf97e213e69a97e63ab8f7fad7ecd53608c757aLennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering dl = dlopen("libnss_myhostname.so.2", RTLD_LAZY);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (dl) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering dlclose(dl);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return true;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return false;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering}
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringstatic bool valid_chassis(const char *chassis) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering assert(chassis);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return nulstr_contains(
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "vm\0"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "container\0"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "desktop\0"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "laptop\0"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "server\0"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "tablet\0"
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering "handset\0",
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering chassis);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering}
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersenstatic const char* fallback_chassis(void) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering int r;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering char *type;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen unsigned t;
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering int v;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering v = detect_virtualization(NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (v == VIRTUALIZATION_VM)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return "vm";
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (v == VIRTUALIZATION_CONTAINER)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return "container";
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto try_dmi;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = safe_atou(type, &t);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen free(type);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r < 0)
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering goto try_dmi;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen /* We only list the really obvious cases here as the ACPI data
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * is not really super reliable.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering *
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * See the ACPI 5.0 Spec Section 5.2.9.1 for details:
3d7415f43f0fe6a821d7bc4a341ba371e8a30ef3Lennart Poettering *
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen switch(t) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 1:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 3:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 6:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return "desktop";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 2:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return "laptop";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 4:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 5:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 7:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return "server";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 8:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return "tablet";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering }
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poetteringtry_dmi:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type);
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering if (r < 0)
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering return NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen r = safe_atou(type, &t);
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering free(type);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen if (r < 0)
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen return NULL;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
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
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering See the SMBIOS Specification 2.7.1 section 7.4.1 for
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering details about the values listed here:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering */
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering switch (t) {
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 0x3:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 0x4:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 0x6:
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering case 0x7:
e56056e93d33619a3acf13e483900b4f8938228fThomas Hindoe Paaboel Andersen return "desktop";
0b63e2789f984e84f40bf6e49f5da15c87298cedLennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0x8:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0x9:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0xA:
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering case 0xE:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return "laptop";
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering case 0xB:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return "handset";
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering case 0x11:
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering case 0x1C:
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return "server";
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return NULL;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic char* context_fallback_icon_name(Context *c) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *chassis;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(c);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering if (!isempty(c->data[PROP_CHASSIS]))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return strappend("computer-", c->data[PROP_CHASSIS]);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering chassis = fallback_chassis();
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (chassis)
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering return strappend("computer-", chassis);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return strdup("computer");
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
56159e0d918e9a9be07988133bb2847779325de0Lennart Poetteringstatic int context_write_data_hostname(Context *c) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *hn;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(c);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (isempty(c->data[PROP_HOSTNAME]))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering hn = "localhost";
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering else
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering hn = c->data[PROP_HOSTNAME];
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (sethostname(hn, strlen(hn)) < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return -errno;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int context_write_data_static_hostname(Context *c) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(c);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (isempty(c->data[PROP_STATIC_HOSTNAME])) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (unlink("/etc/hostname") < 0)
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return errno == ENOENT ? 0 : -errno;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering}
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poetteringstatic int context_write_data_machine_info(Context *c) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering static const char * const name[_PROP_MAX] = {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME",
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering [PROP_ICON_NAME] = "ICON_NAME",
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering [PROP_CHASSIS] = "CHASSIS"
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering };
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering _cleanup_strv_free_ char **l = NULL;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering int r, p;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering assert(c);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering r = load_env_file("/etc/machine-info", NULL, &l);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (r < 0 && r != -ENOENT)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return r;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering for (p = PROP_PRETTY_HOSTNAME; p <= PROP_CHASSIS; p++) {
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering char *t, **u;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering assert(name[p]);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering if (isempty(c->data[p])) {
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering strv_env_unset(l, name[p]);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering continue;
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (asprintf(&t, "%s=%s", name[p], strempty(c->data[p])) < 0)
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering return -ENOMEM;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering u = strv_env_set(l, t);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering free(t);
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering if (!u)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return -ENOMEM;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering strv_free(l);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering l = u;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering }
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (strv_isempty(l)) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (unlink("/etc/machine-info") < 0)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return errno == ENOENT ? 0 : -errno;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return 0;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering }
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering return write_env_file_label("/etc/machine-info", l);
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering}
c19de71113f956809995fc68817e055e9f61f607Lennart Poettering
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poetteringstatic int property_get_icon_name(
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering sd_bus *bus,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *path,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering const char *interface,
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering const char *property,
10f9c75519671e7c7ab8993b54fe22da7c2d0c38Lennart Poettering sd_bus_message *reply,
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering void *userdata,
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering sd_bus_error *error) {
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
8937e7b68940d0fa0d0aab90eb7425fa7dccebc9Lennart Poettering _cleanup_free_ char *n = NULL;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering Context *c = userdata;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering const char *name;
b6b1849830f5e4a6065c3b0c993668e500c954d3Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (isempty(c->data[PROP_ICON_NAME]))
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering name = n = context_fallback_icon_name(c);
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering else
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering name = c->data[PROP_ICON_NAME];
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering if (!name)
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering return -ENOMEM;
cd61c3bfd718fb398cc53ced906266a9297782c9Lennart Poettering
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return sd_bus_message_append(reply, "s", name);
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen}
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poetteringstatic int property_get_chassis(
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering sd_bus *bus,
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering const char *path,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *interface,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *property,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering sd_bus_message *reply,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering void *userdata,
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering sd_bus_error *error) {
d21ed1ead18d16d35c30299a69d3366847f8a039Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering Context *c = userdata;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering const char *name;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (isempty(c->data[PROP_CHASSIS]))
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering name = fallback_chassis();
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering else
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering name = c->data[PROP_CHASSIS];
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return sd_bus_message_append(reply, "s", name);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering}
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poetteringstatic int method_set_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering Context *c = userdata;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen const char *name;
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering int interactive;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering char *h;
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen int r;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering r = sd_bus_message_read(m, "sb", &name, &interactive);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r < 0)
a7893c6b28772edbc7e1fea3c209caa54d465648Lennart Poettering return r;
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering
5b30bef856e89a571df57b7b953e9a1409d9acedLennart Poettering if (isempty(name))
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering name = c->data[PROP_STATIC_HOSTNAME];
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (isempty(name))
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering name = "localhost";
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering if (!hostname_is_valid(name))
9d12709626bccc0cae677a7035f62efe6aabb4abLennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (streq_ptr(name, c->data[PROP_HOSTNAME]))
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return sd_bus_reply_method_return(m, NULL);
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
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 if (r < 0)
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return r;
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering if (r == 0)
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
aa1936ea1a89c2bb968ba33e3274898a4eeae771Lennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering h = strdup(name);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!h)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return -ENOMEM;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering free(c->data[PROP_HOSTNAME]);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering c->data[PROP_HOSTNAME] = h;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = context_write_data_hostname(c);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0) {
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 }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_info("Changed host name to '%s'", strna(c->data[PROP_HOSTNAME]));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "Hostname", NULL);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return sd_bus_reply_method_return(m, NULL);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering}
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic int method_set_static_hostname(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering Context *c = userdata;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering const char *name;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering int interactive;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering int r;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = sd_bus_message_read(m, "sb", &name, &interactive);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering if (isempty(name))
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering name = NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME]))
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return sd_bus_reply_method_return(m, NULL);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
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);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return r;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r == 0)
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (isempty(name)) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering free(c->data[PROP_STATIC_HOSTNAME]);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering c->data[PROP_STATIC_HOSTNAME] = NULL;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering } else {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering char *h;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!hostname_is_valid(name))
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering h = strdup(name);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (!h)
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return -ENOMEM;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering free(c->data[PROP_STATIC_HOSTNAME]);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering c->data[PROP_STATIC_HOSTNAME] = h;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering r = context_write_data_static_hostname(c);
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering if (r < 0) {
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 }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering log_info("Changed static host name to '%s'", strna(c->data[PROP_STATIC_HOSTNAME]));
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering sd_bus_emit_properties_changed(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "StaticHostname", NULL);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return sd_bus_reply_method_return(m, NULL);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering}
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
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 int interactive;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering const char *name;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering int r;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(c);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(bus);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering assert(m);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_message_read(m, "sb", &name, &interactive);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return r;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (isempty(name))
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering name = NULL;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (streq_ptr(name, c->data[prop]))
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return sd_bus_reply_method_return(m, NULL);
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
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 * both... */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
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 if (r < 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return r;
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering if (r == 0)
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (isempty(name)) {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers free(c->data[prop]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->data[prop] = NULL;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers } else {
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers char *h;
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers /* The icon name might ultimately be used as file
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * name, so better be safe than sorry */
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
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 if (prop == PROP_PRETTY_HOSTNAME &&
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);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering h = strdup(name);
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering if (!h)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return -ENOMEM;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering free(c->data[prop]);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering c->data[prop] = h;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = context_write_data_machine_info(c);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
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));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_info("Changed %s to '%s'",
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering prop == PROP_PRETTY_HOSTNAME ? "pretty host name" :
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop]));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
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
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return sd_bus_reply_method_return(m, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
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 Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
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 Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
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 Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic const sd_bus_vtable hostname_vtable[] = {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering SD_BUS_VTABLE_START(0),
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 Poettering SD_BUS_VTABLE_END,
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering};
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poetteringstatic int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering _cleanup_bus_unref_ sd_bus *bus = NULL;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering int r;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(c);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(event);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering assert(_bus);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = sd_bus_default_system(&bus);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r < 0) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_error("Failed to get system bus connection: %s", strerror(-r));
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering return r;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = sd_bus_add_object_vtable(bus, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (r < 0) {
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering log_error("Failed to register object: %s", strerror(-r));
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering return r;
878cd7e95ca303f9851d227a22d2022bd49944b0Lennart Poettering }
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering
717603e391b52983ca1fd218e7333a1b9dfc5c05Lennart Poettering r = sd_bus_request_name(bus, "org.freedesktop.hostname1", 0);
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering if (r < 0) {
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering log_error("Failed to register name: %s", strerror(-r));
89f7c8465cd1ab37347dd0c15920bce31e8225dfLennart Poettering return r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = sd_bus_attach_event(bus, event, 0);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r < 0) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering log_error("Failed to attach bus to event loop: %s", strerror(-r));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return r;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering *_bus = bus;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering bus = NULL;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering return 0;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering}
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poetteringint main(int argc, char *argv[]) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering Context context = {};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_event_unref_ sd_event *event = NULL;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering _cleanup_bus_unref_ sd_bus *bus = NULL;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering int r;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_set_target(LOG_TARGET_AUTO);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_parse_environment();
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_open();
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering umask(0022);
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering label_init("/etc");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (argc != 1) {
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek log_error("This program takes no arguments.");
e7e9b6bb0b0bc5b1eb256a44f8afec6b634f26efZbigniew Jędrzejewski-Szmek r = -EINVAL;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering goto finish;
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering }
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (!check_nss())
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_warning("Warning: nss-myhostname is not installed. Changing the local hostname might make it unresolveable. Please install nss-myhostname!");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering if (argc != 1) {
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering log_error("This program takes no arguments.");
f48e75cb9a8112d35855c44a156934f2ee0edb2eLennart Poettering r = -EINVAL;
fefdc04b38725457a91651218feb7000f6ccc1f4Lennart Poettering goto finish;
a6c616024db23fef34152c1432892824a07799ccLennart Poettering }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = sd_event_default(&event);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r < 0) {
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering log_error("Failed to allocate event loop: %s", strerror(-r));
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering goto finish;
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering }
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering sd_event_set_watchdog(event, true);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering r = connect_bus(&context, event, &bus);
8b0cc9a36c8f92f010f2e8465942d2cd7c580d78Lennart Poettering if (r < 0)
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers goto finish;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
a6c616024db23fef34152c1432892824a07799ccLennart Poettering r = context_read_data(&context);
a6c616024db23fef34152c1432892824a07799ccLennart Poettering if (r < 0) {
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen log_error("Failed to read hostname and machine information: %s", strerror(-r));
a1da85830bfaa77b9eb9c54693e5573559c97e50Tom Gundersen goto finish;
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering }
56159e0d918e9a9be07988133bb2847779325de0Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (r < 0) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_error("Failed to run event loop: %s", strerror(-r));
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers goto finish;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers }
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sieversfinish:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers context_free(&context, bus);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering