]>
Commit | Line | Data |
---|---|---|
7640a5de LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
41550d40 | 4 | Copyright 2011 Lennart Poettering |
7640a5de LP |
5 | |
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
7640a5de LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
7640a5de | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
7640a5de LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
7640a5de LP |
20 | #include <errno.h> |
21 | #include <string.h> | |
f426cc5d | 22 | #include <sys/utsname.h> |
cf0fbc49 | 23 | #include <unistd.h> |
7640a5de | 24 | |
b5efdb8a | 25 | #include "alloc-util.h" |
6bedfcbb | 26 | #include "bus-util.h" |
ad740100 | 27 | #include "def.h" |
4d1a6904 | 28 | #include "env-util.h" |
6bedfcbb | 29 | #include "fileio-label.h" |
958b66ea | 30 | #include "hostname-util.h" |
6bedfcbb | 31 | #include "parse-util.h" |
bb15fafe | 32 | #include "path-util.h" |
6bedfcbb LP |
33 | #include "selinux-util.h" |
34 | #include "strv.h" | |
ee104e11 | 35 | #include "user-util.h" |
6bedfcbb LP |
36 | #include "util.h" |
37 | #include "virt.h" | |
91f9dcaf | 38 | |
799298d6 JG |
39 | #define VALID_DEPLOYMENT_CHARS (DIGITS LETTERS "-.:") |
40 | ||
7640a5de LP |
41 | enum { |
42 | PROP_HOSTNAME, | |
43 | PROP_STATIC_HOSTNAME, | |
44 | PROP_PRETTY_HOSTNAME, | |
45 | PROP_ICON_NAME, | |
7871c8e9 | 46 | PROP_CHASSIS, |
799298d6 | 47 | PROP_DEPLOYMENT, |
ce0f1493 | 48 | PROP_LOCATION, |
f426cc5d DH |
49 | PROP_KERNEL_NAME, |
50 | PROP_KERNEL_RELEASE, | |
9be3455f | 51 | PROP_KERNEL_VERSION, |
44c32988 DH |
52 | PROP_OS_PRETTY_NAME, |
53 | PROP_OS_CPE_NAME, | |
7640a5de LP |
54 | _PROP_MAX |
55 | }; | |
56 | ||
66a4c743 LP |
57 | typedef struct Context { |
58 | char *data[_PROP_MAX]; | |
59 | Hashmap *polkit_registry; | |
60 | } Context; | |
ad740100 | 61 | |
66a4c743 | 62 | static void context_reset(Context *c) { |
7640a5de LP |
63 | int p; |
64 | ||
66a4c743 LP |
65 | assert(c); |
66 | ||
1f6b4113 | 67 | for (p = 0; p < _PROP_MAX; p++) |
a1e58e8e | 68 | c->data[p] = mfree(c->data[p]); |
7640a5de LP |
69 | } |
70 | ||
36e34057 | 71 | static void context_free(Context *c) { |
66a4c743 LP |
72 | assert(c); |
73 | ||
74 | context_reset(c); | |
36e34057 | 75 | bus_verify_polkit_async_registry_free(c->polkit_registry); |
66a4c743 LP |
76 | } |
77 | ||
78 | static int context_read_data(Context *c) { | |
7640a5de | 79 | int r; |
f426cc5d | 80 | struct utsname u; |
7640a5de | 81 | |
66a4c743 LP |
82 | assert(c); |
83 | ||
84 | context_reset(c); | |
7640a5de | 85 | |
f426cc5d DH |
86 | assert_se(uname(&u) >= 0); |
87 | c->data[PROP_KERNEL_NAME] = strdup(u.sysname); | |
88 | c->data[PROP_KERNEL_RELEASE] = strdup(u.release); | |
9be3455f DH |
89 | c->data[PROP_KERNEL_VERSION] = strdup(u.version); |
90 | if (!c->data[PROP_KERNEL_NAME] || !c->data[PROP_KERNEL_RELEASE] || | |
91 | !c->data[PROP_KERNEL_VERSION]) | |
f426cc5d DH |
92 | return -ENOMEM; |
93 | ||
66a4c743 LP |
94 | c->data[PROP_HOSTNAME] = gethostname_malloc(); |
95 | if (!c->data[PROP_HOSTNAME]) | |
7640a5de LP |
96 | return -ENOMEM; |
97 | ||
139e5336 | 98 | r = read_hostname_config("/etc/hostname", &c->data[PROP_STATIC_HOSTNAME]); |
7640a5de LP |
99 | if (r < 0 && r != -ENOENT) |
100 | return r; | |
101 | ||
102 | r = parse_env_file("/etc/machine-info", NEWLINE, | |
66a4c743 LP |
103 | "PRETTY_HOSTNAME", &c->data[PROP_PRETTY_HOSTNAME], |
104 | "ICON_NAME", &c->data[PROP_ICON_NAME], | |
105 | "CHASSIS", &c->data[PROP_CHASSIS], | |
799298d6 | 106 | "DEPLOYMENT", &c->data[PROP_DEPLOYMENT], |
ce0f1493 | 107 | "LOCATION", &c->data[PROP_LOCATION], |
7640a5de LP |
108 | NULL); |
109 | if (r < 0 && r != -ENOENT) | |
110 | return r; | |
111 | ||
44c32988 DH |
112 | r = parse_env_file("/etc/os-release", NEWLINE, |
113 | "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME], | |
114 | "CPE_NAME", &c->data[PROP_OS_CPE_NAME], | |
115 | NULL); | |
ece174c5 | 116 | if (r == -ENOENT) |
5ae4d543 LP |
117 | r = parse_env_file("/usr/lib/os-release", NEWLINE, |
118 | "PRETTY_NAME", &c->data[PROP_OS_PRETTY_NAME], | |
119 | "CPE_NAME", &c->data[PROP_OS_CPE_NAME], | |
120 | NULL); | |
5ae4d543 | 121 | |
44c32988 DH |
122 | if (r < 0 && r != -ENOENT) |
123 | return r; | |
124 | ||
7640a5de LP |
125 | return 0; |
126 | } | |
127 | ||
7871c8e9 | 128 | static bool valid_chassis(const char *chassis) { |
7871c8e9 LP |
129 | assert(chassis); |
130 | ||
131 | return nulstr_contains( | |
132 | "vm\0" | |
133 | "container\0" | |
134 | "desktop\0" | |
135 | "laptop\0" | |
136 | "server\0" | |
137 | "tablet\0" | |
c49e59c1 | 138 | "handset\0" |
25fa306e LP |
139 | "watch\0" |
140 | "embedded\0", | |
7871c8e9 LP |
141 | chassis); |
142 | } | |
143 | ||
799298d6 JG |
144 | static bool valid_deployment(const char *deployment) { |
145 | assert(deployment); | |
146 | ||
c2142cf1 | 147 | return in_charset(deployment, VALID_DEPLOYMENT_CHARS); |
799298d6 JG |
148 | } |
149 | ||
7871c8e9 | 150 | static const char* fallback_chassis(void) { |
7640a5de LP |
151 | int r; |
152 | char *type; | |
153 | unsigned t; | |
248fab74 | 154 | int v; |
7871c8e9 | 155 | |
75f86906 | 156 | v = detect_virtualization(); |
7871c8e9 | 157 | |
75f86906 | 158 | if (VIRTUALIZATION_IS_VM(v)) |
7871c8e9 | 159 | return "vm"; |
75f86906 | 160 | if (VIRTUALIZATION_IS_CONTAINER(v)) |
7871c8e9 LP |
161 | return "container"; |
162 | ||
163 | r = read_one_line_file("/sys/firmware/acpi/pm_profile", &type); | |
164 | if (r < 0) | |
165 | goto try_dmi; | |
166 | ||
167 | r = safe_atou(type, &t); | |
168 | free(type); | |
169 | if (r < 0) | |
170 | goto try_dmi; | |
171 | ||
172 | /* We only list the really obvious cases here as the ACPI data | |
173 | * is not really super reliable. | |
174 | * | |
175 | * See the ACPI 5.0 Spec Section 5.2.9.1 for details: | |
176 | * | |
177 | * http://www.acpi.info/DOWNLOADS/ACPIspec50.pdf | |
178 | */ | |
179 | ||
180 | switch(t) { | |
181 | ||
182 | case 1: | |
183 | case 3: | |
184 | case 6: | |
185 | return "desktop"; | |
186 | ||
187 | case 2: | |
188 | return "laptop"; | |
7640a5de | 189 | |
7871c8e9 LP |
190 | case 4: |
191 | case 5: | |
192 | case 7: | |
193 | return "server"; | |
7640a5de | 194 | |
7871c8e9 LP |
195 | case 8: |
196 | return "tablet"; | |
197 | } | |
198 | ||
199 | try_dmi: | |
7640a5de LP |
200 | r = read_one_line_file("/sys/class/dmi/id/chassis_type", &type); |
201 | if (r < 0) | |
202 | return NULL; | |
203 | ||
204 | r = safe_atou(type, &t); | |
205 | free(type); | |
7640a5de LP |
206 | if (r < 0) |
207 | return NULL; | |
208 | ||
209 | /* We only list the really obvious cases here. The DMI data is | |
210 | unreliable enough, so let's not do any additional guesswork | |
41550d40 LP |
211 | on top of that. |
212 | ||
e5abebab | 213 | See the SMBIOS Specification 3.0 section 7.4.1 for |
41550d40 LP |
214 | details about the values listed here: |
215 | ||
f3f4f008 | 216 | https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.0.0.pdf |
41550d40 | 217 | */ |
7640a5de LP |
218 | |
219 | switch (t) { | |
220 | ||
221 | case 0x3: | |
222 | case 0x4: | |
223 | case 0x6: | |
224 | case 0x7: | |
7871c8e9 | 225 | return "desktop"; |
7640a5de | 226 | |
7871c8e9 | 227 | case 0x8: |
7640a5de LP |
228 | case 0x9: |
229 | case 0xA: | |
230 | case 0xE: | |
7871c8e9 LP |
231 | return "laptop"; |
232 | ||
233 | case 0xB: | |
234 | return "handset"; | |
7640a5de LP |
235 | |
236 | case 0x11: | |
41550d40 | 237 | case 0x1C: |
f3f4f008 | 238 | case 0x1D: |
7871c8e9 | 239 | return "server"; |
f3f4f008 LP |
240 | |
241 | case 0x1E: | |
242 | return "tablet"; | |
7640a5de LP |
243 | } |
244 | ||
7640a5de LP |
245 | return NULL; |
246 | } | |
247 | ||
66a4c743 | 248 | static char* context_fallback_icon_name(Context *c) { |
7871c8e9 LP |
249 | const char *chassis; |
250 | ||
66a4c743 LP |
251 | assert(c); |
252 | ||
253 | if (!isempty(c->data[PROP_CHASSIS])) | |
254 | return strappend("computer-", c->data[PROP_CHASSIS]); | |
7871c8e9 LP |
255 | |
256 | chassis = fallback_chassis(); | |
257 | if (chassis) | |
258 | return strappend("computer-", chassis); | |
259 | ||
260 | return strdup("computer"); | |
261 | } | |
262 | ||
799298d6 | 263 | |
c779a442 | 264 | static bool hostname_is_useful(const char *hn) { |
fecc80c1 | 265 | return !isempty(hn) && !is_localhost(hn); |
c779a442 SW |
266 | } |
267 | ||
268 | static int context_update_kernel_hostname(Context *c) { | |
269 | const char *static_hn; | |
7640a5de LP |
270 | const char *hn; |
271 | ||
66a4c743 LP |
272 | assert(c); |
273 | ||
c779a442 SW |
274 | static_hn = c->data[PROP_STATIC_HOSTNAME]; |
275 | ||
276 | /* /etc/hostname with something other than "localhost" | |
277 | * has the highest preference ... */ | |
278 | if (hostname_is_useful(static_hn)) | |
279 | hn = static_hn; | |
280 | ||
f7340ab2 | 281 | /* ... the transient host name, (ie: DHCP) comes next ... */ |
c779a442 | 282 | else if (!isempty(c->data[PROP_HOSTNAME])) |
66a4c743 | 283 | hn = c->data[PROP_HOSTNAME]; |
7640a5de | 284 | |
c779a442 SW |
285 | /* ... fallback to static "localhost.*" ignored above ... */ |
286 | else if (!isempty(static_hn)) | |
287 | hn = static_hn; | |
288 | ||
289 | /* ... and the ultimate fallback */ | |
290 | else | |
291 | hn = "localhost"; | |
292 | ||
605f81a8 | 293 | if (sethostname_idempotent(hn) < 0) |
7640a5de LP |
294 | return -errno; |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
66a4c743 | 299 | static int context_write_data_static_hostname(Context *c) { |
7640a5de | 300 | |
66a4c743 LP |
301 | assert(c); |
302 | ||
303 | if (isempty(c->data[PROP_STATIC_HOSTNAME])) { | |
7640a5de LP |
304 | |
305 | if (unlink("/etc/hostname") < 0) | |
306 | return errno == ENOENT ? 0 : -errno; | |
307 | ||
308 | return 0; | |
309 | } | |
66a4c743 | 310 | return write_string_file_atomic_label("/etc/hostname", c->data[PROP_STATIC_HOSTNAME]); |
7640a5de LP |
311 | } |
312 | ||
f200e8bb | 313 | static int context_write_data_machine_info(Context *c) { |
4f34ed54 | 314 | |
7640a5de LP |
315 | static const char * const name[_PROP_MAX] = { |
316 | [PROP_PRETTY_HOSTNAME] = "PRETTY_HOSTNAME", | |
7871c8e9 | 317 | [PROP_ICON_NAME] = "ICON_NAME", |
799298d6 JG |
318 | [PROP_CHASSIS] = "CHASSIS", |
319 | [PROP_DEPLOYMENT] = "DEPLOYMENT", | |
ce0f1493 | 320 | [PROP_LOCATION] = "LOCATION", |
7640a5de LP |
321 | }; |
322 | ||
0ccad099 | 323 | _cleanup_strv_free_ char **l = NULL; |
7640a5de LP |
324 | int r, p; |
325 | ||
66a4c743 LP |
326 | assert(c); |
327 | ||
717603e3 | 328 | r = load_env_file(NULL, "/etc/machine-info", NULL, &l); |
7640a5de LP |
329 | if (r < 0 && r != -ENOENT) |
330 | return r; | |
331 | ||
ce0f1493 | 332 | for (p = PROP_PRETTY_HOSTNAME; p <= PROP_LOCATION; p++) { |
d77ab3f7 LP |
333 | _cleanup_free_ char *t = NULL; |
334 | char **u; | |
7640a5de LP |
335 | |
336 | assert(name[p]); | |
337 | ||
66a4c743 | 338 | if (isempty(c->data[p])) { |
f8440af5 | 339 | strv_env_unset(l, name[p]); |
7640a5de LP |
340 | continue; |
341 | } | |
342 | ||
d77ab3f7 LP |
343 | t = strjoin(name[p], "=", c->data[p], NULL); |
344 | if (!t) | |
7640a5de | 345 | return -ENOMEM; |
7640a5de LP |
346 | |
347 | u = strv_env_set(l, t); | |
7640a5de LP |
348 | if (!u) |
349 | return -ENOMEM; | |
0ccad099 LP |
350 | |
351 | strv_free(l); | |
7640a5de LP |
352 | l = u; |
353 | } | |
354 | ||
355 | if (strv_isempty(l)) { | |
7640a5de LP |
356 | if (unlink("/etc/machine-info") < 0) |
357 | return errno == ENOENT ? 0 : -errno; | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
0ccad099 | 362 | return write_env_file_label("/etc/machine-info", l); |
7640a5de LP |
363 | } |
364 | ||
66a4c743 LP |
365 | static int property_get_icon_name( |
366 | sd_bus *bus, | |
367 | const char *path, | |
368 | const char *interface, | |
369 | const char *property, | |
370 | sd_bus_message *reply, | |
ebcf1f97 LP |
371 | void *userdata, |
372 | sd_bus_error *error) { | |
7640a5de | 373 | |
66a4c743 LP |
374 | _cleanup_free_ char *n = NULL; |
375 | Context *c = userdata; | |
376 | const char *name; | |
7640a5de | 377 | |
66a4c743 LP |
378 | if (isempty(c->data[PROP_ICON_NAME])) |
379 | name = n = context_fallback_icon_name(c); | |
7640a5de | 380 | else |
66a4c743 LP |
381 | name = c->data[PROP_ICON_NAME]; |
382 | ||
383 | if (!name) | |
384 | return -ENOMEM; | |
7640a5de | 385 | |
ebcf1f97 | 386 | return sd_bus_message_append(reply, "s", name); |
7640a5de LP |
387 | } |
388 | ||
66a4c743 LP |
389 | static int property_get_chassis( |
390 | sd_bus *bus, | |
391 | const char *path, | |
392 | const char *interface, | |
393 | const char *property, | |
394 | sd_bus_message *reply, | |
ebcf1f97 LP |
395 | void *userdata, |
396 | sd_bus_error *error) { | |
7871c8e9 | 397 | |
66a4c743 LP |
398 | Context *c = userdata; |
399 | const char *name; | |
7871c8e9 | 400 | |
66a4c743 | 401 | if (isempty(c->data[PROP_CHASSIS])) |
7871c8e9 LP |
402 | name = fallback_chassis(); |
403 | else | |
66a4c743 | 404 | name = c->data[PROP_CHASSIS]; |
7871c8e9 | 405 | |
ebcf1f97 | 406 | return sd_bus_message_append(reply, "s", name); |
7871c8e9 LP |
407 | } |
408 | ||
19070062 | 409 | static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
66a4c743 LP |
410 | Context *c = userdata; |
411 | const char *name; | |
102d8f81 | 412 | int interactive; |
66a4c743 LP |
413 | char *h; |
414 | int r; | |
d200735e | 415 | |
19070062 LP |
416 | assert(m); |
417 | assert(c); | |
418 | ||
66a4c743 LP |
419 | r = sd_bus_message_read(m, "sb", &name, &interactive); |
420 | if (r < 0) | |
ebcf1f97 | 421 | return r; |
d200735e | 422 | |
66a4c743 LP |
423 | if (isempty(name)) |
424 | name = c->data[PROP_STATIC_HOSTNAME]; | |
7640a5de | 425 | |
66a4c743 LP |
426 | if (isempty(name)) |
427 | name = "localhost"; | |
7640a5de | 428 | |
8fb49443 | 429 | if (!hostname_is_valid(name, false)) |
ebcf1f97 | 430 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid hostname '%s'", name); |
7640a5de | 431 | |
66a4c743 | 432 | if (streq_ptr(name, c->data[PROP_HOSTNAME])) |
df2d202e | 433 | return sd_bus_reply_method_return(m, NULL); |
7640a5de | 434 | |
c529695e LP |
435 | r = bus_verify_polkit_async( |
436 | m, | |
437 | CAP_SYS_ADMIN, | |
438 | "org.freedesktop.hostname1.set-hostname", | |
403ed0e5 | 439 | NULL, |
c529695e LP |
440 | interactive, |
441 | UID_INVALID, | |
442 | &c->polkit_registry, | |
443 | error); | |
66a4c743 | 444 | if (r < 0) |
ebcf1f97 | 445 | return r; |
66a4c743 LP |
446 | if (r == 0) |
447 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
7640a5de | 448 | |
66a4c743 LP |
449 | h = strdup(name); |
450 | if (!h) | |
ebcf1f97 | 451 | return -ENOMEM; |
7640a5de | 452 | |
66a4c743 LP |
453 | free(c->data[PROP_HOSTNAME]); |
454 | c->data[PROP_HOSTNAME] = h; | |
7640a5de | 455 | |
c779a442 | 456 | r = context_update_kernel_hostname(c); |
66a4c743 | 457 | if (r < 0) { |
da927ba9 | 458 | log_error_errno(r, "Failed to set host name: %m"); |
ebcf1f97 | 459 | return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r)); |
66a4c743 | 460 | } |
7640a5de | 461 | |
66a4c743 | 462 | log_info("Changed host name to '%s'", strna(c->data[PROP_HOSTNAME])); |
7640a5de | 463 | |
19070062 | 464 | (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "Hostname", NULL); |
7640a5de | 465 | |
df2d202e | 466 | return sd_bus_reply_method_return(m, NULL); |
66a4c743 | 467 | } |
7640a5de | 468 | |
19070062 | 469 | static int method_set_static_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
66a4c743 LP |
470 | Context *c = userdata; |
471 | const char *name; | |
102d8f81 | 472 | int interactive; |
66a4c743 | 473 | int r; |
7640a5de | 474 | |
19070062 LP |
475 | assert(m); |
476 | assert(c); | |
477 | ||
66a4c743 LP |
478 | r = sd_bus_message_read(m, "sb", &name, &interactive); |
479 | if (r < 0) | |
ebcf1f97 | 480 | return r; |
7640a5de | 481 | |
3c6f7c34 | 482 | name = empty_to_null(name); |
7640a5de | 483 | |
66a4c743 | 484 | if (streq_ptr(name, c->data[PROP_STATIC_HOSTNAME])) |
df2d202e | 485 | return sd_bus_reply_method_return(m, NULL); |
7640a5de | 486 | |
c529695e LP |
487 | r = bus_verify_polkit_async( |
488 | m, | |
489 | CAP_SYS_ADMIN, | |
490 | "org.freedesktop.hostname1.set-static-hostname", | |
403ed0e5 | 491 | NULL, |
c529695e LP |
492 | interactive, |
493 | UID_INVALID, | |
494 | &c->polkit_registry, | |
495 | error); | |
66a4c743 | 496 | if (r < 0) |
ebcf1f97 | 497 | return r; |
66a4c743 LP |
498 | if (r == 0) |
499 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
7640a5de | 500 | |
3c6f7c34 | 501 | if (isempty(name)) |
a1e58e8e | 502 | c->data[PROP_STATIC_HOSTNAME] = mfree(c->data[PROP_STATIC_HOSTNAME]); |
3c6f7c34 | 503 | else { |
66a4c743 | 504 | char *h; |
7640a5de | 505 | |
8fb49443 | 506 | if (!hostname_is_valid(name, false)) |
ebcf1f97 | 507 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid static hostname '%s'", name); |
7640a5de | 508 | |
66a4c743 LP |
509 | h = strdup(name); |
510 | if (!h) | |
ebcf1f97 | 511 | return -ENOMEM; |
7640a5de | 512 | |
66a4c743 LP |
513 | free(c->data[PROP_STATIC_HOSTNAME]); |
514 | c->data[PROP_STATIC_HOSTNAME] = h; | |
515 | } | |
7640a5de | 516 | |
c779a442 SW |
517 | r = context_update_kernel_hostname(c); |
518 | if (r < 0) { | |
da927ba9 | 519 | log_error_errno(r, "Failed to set host name: %m"); |
c779a442 SW |
520 | return sd_bus_error_set_errnof(error, r, "Failed to set hostname: %s", strerror(-r)); |
521 | } | |
522 | ||
66a4c743 LP |
523 | r = context_write_data_static_hostname(c); |
524 | if (r < 0) { | |
da927ba9 | 525 | log_error_errno(r, "Failed to write static host name: %m"); |
ebcf1f97 | 526 | return sd_bus_error_set_errnof(error, r, "Failed to set static hostname: %s", strerror(-r)); |
66a4c743 | 527 | } |
7640a5de | 528 | |
66a4c743 | 529 | log_info("Changed static host name to '%s'", strna(c->data[PROP_STATIC_HOSTNAME])); |
7640a5de | 530 | |
19070062 | 531 | (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/hostname1", "org.freedesktop.hostname1", "StaticHostname", NULL); |
7640a5de | 532 | |
df2d202e | 533 | return sd_bus_reply_method_return(m, NULL); |
66a4c743 | 534 | } |
7640a5de | 535 | |
19070062 | 536 | static int set_machine_info(Context *c, sd_bus_message *m, int prop, sd_bus_message_handler_t cb, sd_bus_error *error) { |
102d8f81 | 537 | int interactive; |
66a4c743 LP |
538 | const char *name; |
539 | int r; | |
540 | ||
541 | assert(c); | |
66a4c743 LP |
542 | assert(m); |
543 | ||
544 | r = sd_bus_message_read(m, "sb", &name, &interactive); | |
545 | if (r < 0) | |
ebcf1f97 | 546 | return r; |
66a4c743 | 547 | |
3c6f7c34 | 548 | name = empty_to_null(name); |
66a4c743 LP |
549 | |
550 | if (streq_ptr(name, c->data[prop])) | |
df2d202e | 551 | return sd_bus_reply_method_return(m, NULL); |
7640a5de | 552 | |
66a4c743 LP |
553 | /* Since the pretty hostname should always be changed at the |
554 | * same time as the static one, use the same policy action for | |
555 | * both... */ | |
556 | ||
c529695e LP |
557 | r = bus_verify_polkit_async( |
558 | m, | |
559 | CAP_SYS_ADMIN, | |
560 | prop == PROP_PRETTY_HOSTNAME ? "org.freedesktop.hostname1.set-static-hostname" : "org.freedesktop.hostname1.set-machine-info", | |
403ed0e5 | 561 | NULL, |
c529695e LP |
562 | interactive, |
563 | UID_INVALID, | |
564 | &c->polkit_registry, | |
565 | error); | |
66a4c743 | 566 | if (r < 0) |
ebcf1f97 | 567 | return r; |
66a4c743 LP |
568 | if (r == 0) |
569 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
570 | ||
3c6f7c34 | 571 | if (isempty(name)) |
a1e58e8e | 572 | c->data[prop] = mfree(c->data[prop]); |
3c6f7c34 | 573 | else { |
66a4c743 LP |
574 | char *h; |
575 | ||
576 | /* The icon name might ultimately be used as file | |
577 | * name, so better be safe than sorry */ | |
578 | ||
ae6c3cc0 | 579 | if (prop == PROP_ICON_NAME && !filename_is_valid(name)) |
ebcf1f97 | 580 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid icon name '%s'", name); |
6294aa76 | 581 | if (prop == PROP_PRETTY_HOSTNAME && string_has_cc(name, NULL)) |
ebcf1f97 | 582 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid pretty host name '%s'", name); |
66a4c743 | 583 | if (prop == PROP_CHASSIS && !valid_chassis(name)) |
ebcf1f97 | 584 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid chassis '%s'", name); |
799298d6 JG |
585 | if (prop == PROP_DEPLOYMENT && !valid_deployment(name)) |
586 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid deployment '%s'", name); | |
ce0f1493 LP |
587 | if (prop == PROP_LOCATION && string_has_cc(name, NULL)) |
588 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid location '%s'", name); | |
66a4c743 LP |
589 | |
590 | h = strdup(name); | |
591 | if (!h) | |
ebcf1f97 | 592 | return -ENOMEM; |
66a4c743 LP |
593 | |
594 | free(c->data[prop]); | |
595 | c->data[prop] = h; | |
7640a5de LP |
596 | } |
597 | ||
f200e8bb | 598 | r = context_write_data_machine_info(c); |
66a4c743 | 599 | if (r < 0) { |
da927ba9 | 600 | log_error_errno(r, "Failed to write machine info: %m"); |
ebcf1f97 | 601 | return sd_bus_error_set_errnof(error, r, "Failed to write machine info: %s", strerror(-r)); |
66a4c743 | 602 | } |
7640a5de | 603 | |
66a4c743 LP |
604 | log_info("Changed %s to '%s'", |
605 | prop == PROP_PRETTY_HOSTNAME ? "pretty host name" : | |
799298d6 | 606 | prop == PROP_DEPLOYMENT ? "deployment" : |
ce0f1493 | 607 | prop == PROP_LOCATION ? "location" : |
66a4c743 | 608 | prop == PROP_CHASSIS ? "chassis" : "icon name", strna(c->data[prop])); |
7640a5de | 609 | |
19070062 LP |
610 | (void) sd_bus_emit_properties_changed( |
611 | sd_bus_message_get_bus(m), | |
612 | "/org/freedesktop/hostname1", | |
613 | "org.freedesktop.hostname1", | |
614 | prop == PROP_PRETTY_HOSTNAME ? "PrettyHostname" : | |
615 | prop == PROP_DEPLOYMENT ? "Deployment" : | |
616 | prop == PROP_LOCATION ? "Location" : | |
617 | prop == PROP_CHASSIS ? "Chassis" : "IconName" , NULL); | |
7640a5de | 618 | |
df2d202e | 619 | return sd_bus_reply_method_return(m, NULL); |
66a4c743 | 620 | } |
7640a5de | 621 | |
19070062 LP |
622 | static int method_set_pretty_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
623 | return set_machine_info(userdata, m, PROP_PRETTY_HOSTNAME, method_set_pretty_hostname, error); | |
7640a5de LP |
624 | } |
625 | ||
19070062 LP |
626 | static int method_set_icon_name(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
627 | return set_machine_info(userdata, m, PROP_ICON_NAME, method_set_icon_name, error); | |
66a4c743 LP |
628 | } |
629 | ||
19070062 LP |
630 | static int method_set_chassis(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
631 | return set_machine_info(userdata, m, PROP_CHASSIS, method_set_chassis, error); | |
66a4c743 LP |
632 | } |
633 | ||
19070062 LP |
634 | static int method_set_deployment(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
635 | return set_machine_info(userdata, m, PROP_DEPLOYMENT, method_set_deployment, error); | |
799298d6 JG |
636 | } |
637 | ||
19070062 LP |
638 | static int method_set_location(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
639 | return set_machine_info(userdata, m, PROP_LOCATION, method_set_location, error); | |
ce0f1493 LP |
640 | } |
641 | ||
66a4c743 LP |
642 | static const sd_bus_vtable hostname_vtable[] = { |
643 | SD_BUS_VTABLE_START(0), | |
caffbef6 | 644 | SD_BUS_PROPERTY("Hostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
66a4c743 LP |
645 | SD_BUS_PROPERTY("StaticHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_STATIC_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
646 | SD_BUS_PROPERTY("PrettyHostname", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_PRETTY_HOSTNAME, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
647 | SD_BUS_PROPERTY("IconName", "s", property_get_icon_name, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
648 | SD_BUS_PROPERTY("Chassis", "s", property_get_chassis, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
ce0f1493 LP |
649 | SD_BUS_PROPERTY("Deployment", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_DEPLOYMENT, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
650 | SD_BUS_PROPERTY("Location", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_LOCATION, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
f426cc5d DH |
651 | SD_BUS_PROPERTY("KernelName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_NAME, SD_BUS_VTABLE_PROPERTY_CONST), |
652 | SD_BUS_PROPERTY("KernelRelease", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_RELEASE, SD_BUS_VTABLE_PROPERTY_CONST), | |
9be3455f | 653 | SD_BUS_PROPERTY("KernelVersion", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_KERNEL_VERSION, SD_BUS_VTABLE_PROPERTY_CONST), |
44c32988 DH |
654 | SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST), |
655 | SD_BUS_PROPERTY("OperatingSystemCPEName", "s", NULL, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST), | |
adacb957 LP |
656 | SD_BUS_METHOD("SetHostname", "sb", NULL, method_set_hostname, SD_BUS_VTABLE_UNPRIVILEGED), |
657 | SD_BUS_METHOD("SetStaticHostname", "sb", NULL, method_set_static_hostname, SD_BUS_VTABLE_UNPRIVILEGED), | |
658 | SD_BUS_METHOD("SetPrettyHostname", "sb", NULL, method_set_pretty_hostname, SD_BUS_VTABLE_UNPRIVILEGED), | |
659 | SD_BUS_METHOD("SetIconName", "sb", NULL, method_set_icon_name, SD_BUS_VTABLE_UNPRIVILEGED), | |
660 | SD_BUS_METHOD("SetChassis", "sb", NULL, method_set_chassis, SD_BUS_VTABLE_UNPRIVILEGED), | |
799298d6 | 661 | SD_BUS_METHOD("SetDeployment", "sb", NULL, method_set_deployment, SD_BUS_VTABLE_UNPRIVILEGED), |
ce0f1493 | 662 | SD_BUS_METHOD("SetLocation", "sb", NULL, method_set_location, SD_BUS_VTABLE_UNPRIVILEGED), |
66a4c743 LP |
663 | SD_BUS_VTABLE_END, |
664 | }; | |
665 | ||
666 | static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) { | |
4afd3348 | 667 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
7640a5de LP |
668 | int r; |
669 | ||
66a4c743 LP |
670 | assert(c); |
671 | assert(event); | |
d0baa06f LP |
672 | assert(_bus); |
673 | ||
76b54375 | 674 | r = sd_bus_default_system(&bus); |
23bbb0de MS |
675 | if (r < 0) |
676 | return log_error_errno(r, "Failed to get system bus connection: %m"); | |
d0baa06f | 677 | |
19befb2d | 678 | r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/hostname1", "org.freedesktop.hostname1", hostname_vtable, c); |
23bbb0de MS |
679 | if (r < 0) |
680 | return log_error_errno(r, "Failed to register object: %m"); | |
d0baa06f | 681 | |
5bb658a1 | 682 | r = sd_bus_request_name(bus, "org.freedesktop.hostname1", 0); |
23bbb0de MS |
683 | if (r < 0) |
684 | return log_error_errno(r, "Failed to register name: %m"); | |
add10b5a | 685 | |
66a4c743 | 686 | r = sd_bus_attach_event(bus, event, 0); |
23bbb0de MS |
687 | if (r < 0) |
688 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
d0baa06f | 689 | |
66a4c743 LP |
690 | *_bus = bus; |
691 | bus = NULL; | |
d0baa06f | 692 | |
66a4c743 | 693 | return 0; |
d0baa06f LP |
694 | } |
695 | ||
696 | int main(int argc, char *argv[]) { | |
66a4c743 | 697 | Context context = {}; |
4afd3348 LP |
698 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; |
699 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
d0baa06f | 700 | int r; |
d0baa06f | 701 | |
7640a5de LP |
702 | log_set_target(LOG_TARGET_AUTO); |
703 | log_parse_environment(); | |
704 | log_open(); | |
705 | ||
4c12626c | 706 | umask(0022); |
c3dacc8b | 707 | mac_selinux_init(); |
4c12626c | 708 | |
7640a5de LP |
709 | if (argc != 1) { |
710 | log_error("This program takes no arguments."); | |
711 | r = -EINVAL; | |
712 | goto finish; | |
713 | } | |
714 | ||
afc6adb5 | 715 | r = sd_event_default(&event); |
7640a5de | 716 | if (r < 0) { |
da927ba9 | 717 | log_error_errno(r, "Failed to allocate event loop: %m"); |
7640a5de LP |
718 | goto finish; |
719 | } | |
720 | ||
cde93897 LP |
721 | sd_event_set_watchdog(event, true); |
722 | ||
66a4c743 | 723 | r = connect_bus(&context, event, &bus); |
d0baa06f | 724 | if (r < 0) |
7640a5de | 725 | goto finish; |
7640a5de | 726 | |
66a4c743 LP |
727 | r = context_read_data(&context); |
728 | if (r < 0) { | |
da927ba9 | 729 | log_error_errno(r, "Failed to read hostname and machine information: %m"); |
66a4c743 LP |
730 | goto finish; |
731 | } | |
ad740100 | 732 | |
37224a5f | 733 | r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL); |
66a4c743 | 734 | if (r < 0) { |
da927ba9 | 735 | log_error_errno(r, "Failed to run event loop: %m"); |
66a4c743 | 736 | goto finish; |
ad740100 | 737 | } |
7640a5de | 738 | |
7640a5de | 739 | finish: |
36e34057 | 740 | context_free(&context); |
7640a5de | 741 | |
7640a5de LP |
742 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
743 | } |