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