]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/locale/localed.c
headers: remove unneeded includes from util.h
[thirdparty/systemd.git] / src / locale / localed.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
1822350d 2
1822350d
KS
3#include <errno.h>
4#include <string.h>
ca78ad1d
ZJS
5#include <sys/stat.h>
6#include <sys/types.h>
1822350d
KS
7#include <unistd.h>
8
349cc4a5 9#if HAVE_XKBCOMMON
3ffd4af2 10#include <xkbcommon/xkbcommon.h>
5de34470 11#include <dlfcn.h>
3ffd4af2
LP
12#endif
13
8d451309
KS
14#include "sd-bus.h"
15
b5efdb8a 16#include "alloc-util.h"
8d451309
KS
17#include "bus-error.h"
18#include "bus-message.h"
bb15fafe
LP
19#include "bus-util.h"
20#include "def.h"
4897d1dc 21#include "keymap-util.h"
75683450 22#include "locale-util.h"
4897d1dc 23#include "macro.h"
c6f09e6a 24#include "main-func.h"
36dd5ffd 25#include "missing_capability.h"
bb15fafe 26#include "path-util.h"
d7b8eec7 27#include "selinux-util.h"
50008ae4 28#include "signal-util.h"
4897d1dc 29#include "string-util.h"
bb15fafe 30#include "strv.h"
ee104e11 31#include "user-util.h"
d4f5a1f4 32
8d451309
KS
33static int locale_update_system_manager(Context *c, sd_bus *bus) {
34 _cleanup_free_ char **l_unset = NULL;
35 _cleanup_strv_free_ char **l_set = NULL;
4afd3348 36 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
8d451309 37 sd_bus_error error = SD_BUS_ERROR_NULL;
608eea8e
LP
38 size_t c_set, c_unset;
39 LocaleVariable p;
8d451309 40 int r;
1822350d
KS
41
42 assert(bus);
43
78c97cbe 44 l_unset = new0(char*, _VARIABLE_LC_MAX);
8d451309 45 if (!l_unset)
46e8b947 46 return log_oom();
1822350d 47
78c97cbe 48 l_set = new0(char*, _VARIABLE_LC_MAX);
8d451309 49 if (!l_set)
46e8b947 50 return log_oom();
8d451309 51
78c97cbe
ZJS
52 for (p = 0, c_set = 0, c_unset = 0; p < _VARIABLE_LC_MAX; p++) {
53 const char *name;
54
55 name = locale_variable_to_string(p);
56 assert(name);
1822350d 57
8d451309 58 if (isempty(c->locale[p]))
78c97cbe 59 l_unset[c_set++] = (char*) name;
1822350d
KS
60 else {
61 char *s;
62
46e8b947
LP
63 s = strjoin(name, "=", c->locale[p]);
64 if (!s)
65 return log_oom();
1822350d
KS
66
67 l_set[c_unset++] = s;
68 }
69 }
70
78c97cbe 71 assert(c_set + c_unset == _VARIABLE_LC_MAX);
151b9b96 72 r = sd_bus_message_new_method_call(bus, &m,
8d451309
KS
73 "org.freedesktop.systemd1",
74 "/org/freedesktop/systemd1",
75 "org.freedesktop.systemd1.Manager",
151b9b96 76 "UnsetAndSetEnvironment");
8d451309 77 if (r < 0)
46e8b947 78 return bus_log_create_error(r);
1822350d 79
8d451309
KS
80 r = sd_bus_message_append_strv(m, l_unset);
81 if (r < 0)
46e8b947 82 return bus_log_create_error(r);
1822350d 83
8d451309
KS
84 r = sd_bus_message_append_strv(m, l_set);
85 if (r < 0)
46e8b947 86 return bus_log_create_error(r);
1822350d 87
c49b30a2 88 r = sd_bus_call(bus, m, 0, &error, NULL);
8d451309 89 if (r < 0)
936d1136 90 return log_error_errno(r, "Failed to update the manager environment: %s", bus_error_message(&error, r));
1822350d 91
8d451309 92 return 0;
1822350d
KS
93}
94
8d451309 95static int vconsole_reload(sd_bus *bus) {
4afd3348 96 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1822350d 97 int r;
1822350d
KS
98
99 assert(bus);
100
8d451309 101 r = sd_bus_call_method(bus,
1822350d
KS
102 "org.freedesktop.systemd1",
103 "/org/freedesktop/systemd1",
104 "org.freedesktop.systemd1.Manager",
8d451309
KS
105 "RestartUnit",
106 &error,
107 NULL,
108 "ss", "systemd-vconsole-setup.service", "replace");
1822350d 109
8d451309 110 if (r < 0)
26a93376 111 return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
4ae25393
YW
112
113 return 0;
1822350d
KS
114}
115
df4fd2c7 116static int vconsole_convert_to_x11_and_emit(Context *c, sd_bus_message *m) {
78bd12a0 117 int r;
0732ef7a 118
df4fd2c7
YW
119 assert(m);
120
121 r = x11_read_data(c, m);
122 if (r < 0)
123 return r;
4e829d21 124
4897d1dc
ZJS
125 r = vconsole_convert_to_x11(c);
126 if (r <= 0)
127 return r;
4e829d21 128
4897d1dc
ZJS
129 /* modified */
130 r = x11_write_data(c);
131 if (r < 0)
132 return log_error_errno(r, "Failed to write X11 keyboard layout: %m");
4e829d21 133
df4fd2c7 134 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
4897d1dc
ZJS
135 "/org/freedesktop/locale1",
136 "org.freedesktop.locale1",
137 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
4e829d21 138
4897d1dc 139 return 1;
4e829d21
ZJS
140}
141
df4fd2c7 142static int x11_convert_to_vconsole_and_emit(Context *c, sd_bus_message *m) {
0732ef7a 143 int r;
1822350d 144
df4fd2c7
YW
145 assert(m);
146
147 r = vconsole_read_data(c, m);
148 if (r < 0)
149 return r;
1822350d 150
4897d1dc
ZJS
151 r = x11_convert_to_vconsole(c);
152 if (r <= 0)
153 return r;
502f9614 154
4897d1dc
ZJS
155 /* modified */
156 r = vconsole_write_data(c);
157 if (r < 0)
158 log_error_errno(r, "Failed to save virtual console keymap: %m");
1822350d 159
df4fd2c7 160 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m),
4897d1dc
ZJS
161 "/org/freedesktop/locale1",
162 "org.freedesktop.locale1",
163 "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
1822350d 164
df4fd2c7 165 return vconsole_reload(sd_bus_message_get_bus(m));
1822350d
KS
166}
167
ebcf1f97
LP
168static int property_get_locale(
169 sd_bus *bus,
170 const char *path,
171 const char *interface,
172 const char *property,
173 sd_bus_message *reply,
174 void *userdata,
175 sd_bus_error *error) {
176
8d451309 177 Context *c = userdata;
b47d419c 178 _cleanup_strv_free_ char **l = NULL;
df4fd2c7
YW
179 int p, q, r;
180
181 r = locale_read_data(c, reply);
182 if (r < 0)
183 return r;
1822350d 184
78c97cbe 185 l = new0(char*, _VARIABLE_LC_MAX+1);
1822350d
KS
186 if (!l)
187 return -ENOMEM;
188
78c97cbe 189 for (p = 0, q = 0; p < _VARIABLE_LC_MAX; p++) {
1822350d 190 char *t;
78c97cbe
ZJS
191 const char *name;
192
193 name = locale_variable_to_string(p);
194 assert(name);
1822350d 195
8d451309 196 if (isempty(c->locale[p]))
1822350d
KS
197 continue;
198
78c97cbe 199 if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
1822350d 200 return -ENOMEM;
1822350d 201
8d451309 202 l[q++] = t;
1822350d
KS
203 }
204
8d451309 205 return sd_bus_message_append_strv(reply, l);
1822350d
KS
206}
207
df4fd2c7
YW
208static int property_get_vconsole(
209 sd_bus *bus,
210 const char *path,
211 const char *interface,
212 const char *property,
213 sd_bus_message *reply,
214 void *userdata,
215 sd_bus_error *error) {
216
217 Context *c = userdata;
218 int r;
219
220 r = vconsole_read_data(c, reply);
221 if (r < 0)
222 return r;
223
224 if (streq(property, "VConsoleKeymap"))
225 return sd_bus_message_append_basic(reply, 's', c->vc_keymap);
226 else if (streq(property, "VConsoleKeymapToggle"))
227 return sd_bus_message_append_basic(reply, 's', c->vc_keymap_toggle);
228
229 return -EINVAL;
230}
231
232static int property_get_xkb(
233 sd_bus *bus,
234 const char *path,
235 const char *interface,
236 const char *property,
237 sd_bus_message *reply,
238 void *userdata,
239 sd_bus_error *error) {
240
241 Context *c = userdata;
242 int r;
243
244 r = x11_read_data(c, reply);
245 if (r < 0)
246 return r;
247
248 if (streq(property, "X11Layout"))
249 return sd_bus_message_append_basic(reply, 's', c->x11_layout);
250 else if (streq(property, "X11Model"))
251 return sd_bus_message_append_basic(reply, 's', c->x11_model);
252 else if (streq(property, "X11Variant"))
253 return sd_bus_message_append_basic(reply, 's', c->x11_variant);
254 else if (streq(property, "X11Options"))
255 return sd_bus_message_append_basic(reply, 's', c->x11_options);
256
257 return -EINVAL;
258}
259
19070062 260static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *error) {
e6755a33 261 _cleanup_(locale_variables_freep) char *new_locale[_VARIABLE_LC_MAX] = {};
df4fd2c7 262 _cleanup_strv_free_ char **settings = NULL, **l = NULL;
e6755a33 263 Context *c = userdata;
8d451309 264 bool modified = false;
df4fd2c7 265 int interactive, p, r;
e6755a33 266 char **i;
1822350d 267
19070062
LP
268 assert(m);
269 assert(c);
270
8d451309
KS
271 r = bus_message_read_strv_extend(m, &l);
272 if (r < 0)
ebcf1f97 273 return r;
1822350d 274
8d451309
KS
275 r = sd_bus_message_read_basic(m, 'b', &interactive);
276 if (r < 0)
ebcf1f97 277 return r;
1822350d 278
4156e767
YW
279 /* If single locale without variable name is provided, then we assume it is LANG=. */
280 if (strv_length(l) == 1 && !strchr(*l, '=')) {
281 if (!locale_is_valid(*l))
282 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Locale data.");
283
284 new_locale[VARIABLE_LANG] = strdup(*l);
285 if (!new_locale[VARIABLE_LANG])
286 return -ENOMEM;
287
288 l = strv_free(l);
289 }
290
df4fd2c7 291 /* Check whether a variable is valid */
8d451309
KS
292 STRV_FOREACH(i, l) {
293 bool valid = false;
1822350d 294
78c97cbe 295 for (p = 0; p < _VARIABLE_LC_MAX; p++) {
8d451309 296 size_t k;
78c97cbe 297 const char *name;
1822350d 298
78c97cbe
ZJS
299 name = locale_variable_to_string(p);
300 assert(name);
301
302 k = strlen(name);
303 if (startswith(*i, name) &&
8d451309 304 (*i)[k] == '=' &&
75683450 305 locale_is_valid((*i) + k + 1)) {
8d451309 306 valid = true;
1822350d 307
df4fd2c7
YW
308 new_locale[p] = strdup((*i) + k + 1);
309 if (!new_locale[p])
310 return -ENOMEM;
1822350d 311
8d451309
KS
312 break;
313 }
1822350d
KS
314 }
315
8d451309 316 if (!valid)
ebcf1f97 317 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid Locale data.");
8d451309 318 }
1822350d 319
4e829d21
ZJS
320 /* If LANG was specified, but not LANGUAGE, check if we should
321 * set it based on the language fallback table. */
df4fd2c7
YW
322 if (!isempty(new_locale[VARIABLE_LANG]) &&
323 isempty(new_locale[VARIABLE_LANGUAGE])) {
4e829d21
ZJS
324 _cleanup_free_ char *language = NULL;
325
df4fd2c7 326 (void) find_language_fallback(new_locale[VARIABLE_LANG], &language);
4e829d21 327 if (language) {
df4fd2c7
YW
328 log_debug("Converted LANG=%s to LANGUAGE=%s", new_locale[VARIABLE_LANG], language);
329 free_and_replace(new_locale[VARIABLE_LANGUAGE], language);
4e829d21
ZJS
330 }
331 }
332
df4fd2c7
YW
333 r = locale_read_data(c, m);
334 if (r < 0) {
335 log_error_errno(r, "Failed to read locale data: %m");
336 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Failed to read locale data");
337 }
8d451309 338
df4fd2c7
YW
339 /* Merge with the current settings */
340 for (p = 0; p < _VARIABLE_LC_MAX; p++)
341 if (!isempty(c->locale[p]) && isempty(new_locale[p])) {
342 new_locale[p] = strdup(c->locale[p]);
343 if (!new_locale[p])
344 return -ENOMEM;
345 }
1822350d 346
df4fd2c7 347 locale_simplify(new_locale);
1822350d 348
df4fd2c7
YW
349 for (p = 0; p < _VARIABLE_LC_MAX; p++)
350 if (!streq_ptr(c->locale[p], new_locale[p])) {
351 modified = true;
352 break;
8d451309 353 }
1822350d 354
df4fd2c7
YW
355 if (!modified) {
356 log_debug("Locale settings were not modified.");
357 return sd_bus_reply_method_return(m, NULL);
358 }
1822350d 359
df4fd2c7
YW
360 r = bus_verify_polkit_async(
361 m,
362 CAP_SYS_ADMIN,
363 "org.freedesktop.locale1.set-locale",
364 NULL,
365 interactive,
366 UID_INVALID,
af7865c1 367 &c->polkit_registry,
df4fd2c7
YW
368 error);
369 if (r < 0)
370 return r;
371 if (r == 0)
372 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1822350d 373
df4fd2c7
YW
374 for (p = 0; p < _VARIABLE_LC_MAX; p++)
375 free_and_replace(c->locale[p], new_locale[p]);
1822350d 376
df4fd2c7
YW
377 r = locale_write_data(c, &settings);
378 if (r < 0) {
379 log_error_errno(r, "Failed to set locale: %m");
380 return sd_bus_error_set_errnof(error, r, "Failed to set locale: %m");
381 }
502f9614 382
df4fd2c7 383 (void) locale_update_system_manager(c, sd_bus_message_get_bus(m));
1822350d 384
df4fd2c7
YW
385 if (settings) {
386 _cleanup_free_ char *line;
387
388 line = strv_join(settings, ", ");
389 log_info("Changed locale to %s.", strnull(line));
502f9614 390 } else
df4fd2c7
YW
391 log_info("Changed locale to unset.");
392
393 (void) sd_bus_emit_properties_changed(
394 sd_bus_message_get_bus(m),
395 "/org/freedesktop/locale1",
396 "org.freedesktop.locale1",
397 "Locale", NULL);
502f9614 398
df2d202e 399 return sd_bus_reply_method_return(m, NULL);
8d451309 400}
1822350d 401
19070062 402static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
8d451309 403 Context *c = userdata;
8d451309 404 const char *keymap, *keymap_toggle;
b47ff73b 405 int convert, interactive, r;
f2cc3753 406
19070062
LP
407 assert(m);
408 assert(c);
409
8d451309
KS
410 r = sd_bus_message_read(m, "ssbb", &keymap, &keymap_toggle, &convert, &interactive);
411 if (r < 0)
ebcf1f97 412 return r;
1822350d 413
3c6f7c34
LP
414 keymap = empty_to_null(keymap);
415 keymap_toggle = empty_to_null(keymap_toggle);
1822350d 416
df4fd2c7
YW
417 r = vconsole_read_data(c, m);
418 if (r < 0) {
419 log_error_errno(r, "Failed to read virtual console keymap data: %m");
420 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Failed to read virtual console keymap data");
421 }
422
b47ff73b
YW
423 if (streq_ptr(keymap, c->vc_keymap) &&
424 streq_ptr(keymap_toggle, c->vc_keymap_toggle))
425 return sd_bus_reply_method_return(m, NULL);
1822350d 426
b47ff73b
YW
427 if ((keymap && (!filename_is_valid(keymap) || !string_is_safe(keymap))) ||
428 (keymap_toggle && (!filename_is_valid(keymap_toggle) || !string_is_safe(keymap_toggle))))
429 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keymap data");
1822350d 430
b47ff73b
YW
431 r = bus_verify_polkit_async(
432 m,
433 CAP_SYS_ADMIN,
434 "org.freedesktop.locale1.set-keyboard",
435 NULL,
436 interactive,
437 UID_INVALID,
af7865c1 438 &c->polkit_registry,
b47ff73b
YW
439 error);
440 if (r < 0)
441 return r;
442 if (r == 0)
443 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1822350d 444
b47ff73b
YW
445 if (free_and_strdup(&c->vc_keymap, keymap) < 0 ||
446 free_and_strdup(&c->vc_keymap_toggle, keymap_toggle) < 0)
447 return -ENOMEM;
0b507b17 448
b47ff73b
YW
449 r = vconsole_write_data(c);
450 if (r < 0) {
451 log_error_errno(r, "Failed to set virtual console keymap: %m");
452 return sd_bus_error_set_errnof(error, r, "Failed to set virtual console keymap: %m");
453 }
1822350d 454
b47ff73b
YW
455 log_info("Changed virtual console keymap to '%s' toggle '%s'",
456 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
1822350d 457
26a93376 458 (void) vconsole_reload(sd_bus_message_get_bus(m));
1822350d 459
b47ff73b
YW
460 (void) sd_bus_emit_properties_changed(
461 sd_bus_message_get_bus(m),
462 "/org/freedesktop/locale1",
463 "org.freedesktop.locale1",
464 "VConsoleKeymap", "VConsoleKeymapToggle", NULL);
1822350d 465
b47ff73b 466 if (convert) {
df4fd2c7 467 r = vconsole_convert_to_x11_and_emit(c, m);
b47ff73b
YW
468 if (r < 0)
469 log_error_errno(r, "Failed to convert keymap data: %m");
8d451309 470 }
1822350d 471
df2d202e 472 return sd_bus_reply_method_return(m, NULL);
8d451309 473}
1822350d 474
349cc4a5 475#if HAVE_XKBCOMMON
5de34470 476
3cb063fd 477_printf_(3, 0)
d4f5a1f4 478static void log_xkb(struct xkb_context *ctx, enum xkb_log_level lvl, const char *format, va_list args) {
8433e339
JS
479 const char *fmt;
480
63c372cb 481 fmt = strjoina("libxkbcommon: ", format);
032b7541
ZJS
482#pragma GCC diagnostic push
483#pragma GCC diagnostic ignored "-Wformat-nonliteral"
8433e339 484 log_internalv(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, fmt, args);
032b7541 485#pragma GCC diagnostic pop
d4f5a1f4
DH
486}
487
5de34470
LP
488#define LOAD_SYMBOL(symbol, dl, name) \
489 ({ \
490 (symbol) = (typeof(symbol)) dlvsym((dl), (name), "V_0.5.0"); \
491 (symbol) ? 0 : -EOPNOTSUPP; \
492 })
493
d4f5a1f4 494static int verify_xkb_rmlvo(const char *model, const char *layout, const char *variant, const char *options) {
5de34470
LP
495
496 /* We dlopen() the library in order to make the dependency soft. The library (and what it pulls in) is huge
497 * after all, hence let's support XKB maps when the library is around, and refuse otherwise. The function
498 * pointers to the shared library are below: */
499
500 struct xkb_context* (*symbol_xkb_context_new)(enum xkb_context_flags flags) = NULL;
501 void (*symbol_xkb_context_unref)(struct xkb_context *context) = NULL;
502 void (*symbol_xkb_context_set_log_fn)(struct xkb_context *context, void (*log_fn)(struct xkb_context *context, enum xkb_log_level level, const char *format, va_list args)) = NULL;
503 struct xkb_keymap* (*symbol_xkb_keymap_new_from_names)(struct xkb_context *context, const struct xkb_rule_names *names, enum xkb_keymap_compile_flags flags) = NULL;
504 void (*symbol_xkb_keymap_unref)(struct xkb_keymap *keymap) = NULL;
505
d4f5a1f4
DH
506 const struct xkb_rule_names rmlvo = {
507 .model = model,
508 .layout = layout,
509 .variant = variant,
510 .options = options,
511 };
512 struct xkb_context *ctx = NULL;
513 struct xkb_keymap *km = NULL;
5de34470 514 void *dl;
d4f5a1f4
DH
515 int r;
516
5de34470
LP
517 /* Compile keymap from RMLVO information to check out its validity */
518
519 dl = dlopen("libxkbcommon.so.0", RTLD_LAZY);
520 if (!dl)
521 return -EOPNOTSUPP;
522
523 r = LOAD_SYMBOL(symbol_xkb_context_new, dl, "xkb_context_new");
524 if (r < 0)
525 goto finish;
526
527 r = LOAD_SYMBOL(symbol_xkb_context_unref, dl, "xkb_context_unref");
528 if (r < 0)
529 goto finish;
530
531 r = LOAD_SYMBOL(symbol_xkb_context_set_log_fn, dl, "xkb_context_set_log_fn");
532 if (r < 0)
533 goto finish;
d4f5a1f4 534
5de34470
LP
535 r = LOAD_SYMBOL(symbol_xkb_keymap_new_from_names, dl, "xkb_keymap_new_from_names");
536 if (r < 0)
537 goto finish;
538
539 r = LOAD_SYMBOL(symbol_xkb_keymap_unref, dl, "xkb_keymap_unref");
540 if (r < 0)
541 goto finish;
542
543 ctx = symbol_xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
d4f5a1f4
DH
544 if (!ctx) {
545 r = -ENOMEM;
5de34470 546 goto finish;
d4f5a1f4
DH
547 }
548
5de34470 549 symbol_xkb_context_set_log_fn(ctx, log_xkb);
d4f5a1f4 550
5de34470 551 km = symbol_xkb_keymap_new_from_names(ctx, &rmlvo, XKB_KEYMAP_COMPILE_NO_FLAGS);
d4f5a1f4
DH
552 if (!km) {
553 r = -EINVAL;
5de34470 554 goto finish;
d4f5a1f4
DH
555 }
556
557 r = 0;
558
5de34470
LP
559finish:
560 if (symbol_xkb_keymap_unref && km)
561 symbol_xkb_keymap_unref(km);
562
563 if (symbol_xkb_context_unref && ctx)
564 symbol_xkb_context_unref(ctx);
565
566 (void) dlclose(dl);
d4f5a1f4
DH
567 return r;
568}
5de34470 569
d4f5a1f4 570#else
5de34470 571
d4f5a1f4
DH
572static int verify_xkb_rmlvo(const char *model, const char *layout, const char *variant, const char *options) {
573 return 0;
574}
5de34470 575
d4f5a1f4
DH
576#endif
577
19070062 578static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
8d451309 579 Context *c = userdata;
8d451309 580 const char *layout, *model, *variant, *options;
b47ff73b 581 int convert, interactive, r;
1822350d 582
19070062
LP
583 assert(m);
584 assert(c);
585
8d451309
KS
586 r = sd_bus_message_read(m, "ssssbb", &layout, &model, &variant, &options, &convert, &interactive);
587 if (r < 0)
ebcf1f97 588 return r;
1822350d 589
3c6f7c34
LP
590 layout = empty_to_null(layout);
591 model = empty_to_null(model);
592 variant = empty_to_null(variant);
593 options = empty_to_null(options);
1822350d 594
df4fd2c7
YW
595 r = x11_read_data(c, m);
596 if (r < 0) {
597 log_error_errno(r, "Failed to read x11 keyboard layout data: %m");
598 return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Failed to read x11 keyboard layout data");
599 }
600
b47ff73b
YW
601 if (streq_ptr(layout, c->x11_layout) &&
602 streq_ptr(model, c->x11_model) &&
603 streq_ptr(variant, c->x11_variant) &&
604 streq_ptr(options, c->x11_options))
605 return sd_bus_reply_method_return(m, NULL);
606
607 if ((layout && !string_is_safe(layout)) ||
608 (model && !string_is_safe(model)) ||
609 (variant && !string_is_safe(variant)) ||
610 (options && !string_is_safe(options)))
611 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keyboard data");
612
fe28d887
YW
613 r = verify_xkb_rmlvo(model, layout, variant, options);
614 if (r < 0) {
615 log_error_errno(r, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m",
616 strempty(model), strempty(layout), strempty(variant), strempty(options));
617
618 if (r == -EOPNOTSUPP)
619 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Local keyboard configuration not supported on this system.");
620
621 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Specified keymap cannot be compiled, refusing as invalid.");
622 }
623
b47ff73b
YW
624 r = bus_verify_polkit_async(
625 m,
626 CAP_SYS_ADMIN,
627 "org.freedesktop.locale1.set-keyboard",
628 NULL,
629 interactive,
630 UID_INVALID,
af7865c1 631 &c->polkit_registry,
b47ff73b
YW
632 error);
633 if (r < 0)
634 return r;
635 if (r == 0)
636 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1822350d 637
b47ff73b
YW
638 if (free_and_strdup(&c->x11_layout, layout) < 0 ||
639 free_and_strdup(&c->x11_model, model) < 0 ||
640 free_and_strdup(&c->x11_variant, variant) < 0 ||
641 free_and_strdup(&c->x11_options, options) < 0)
642 return -ENOMEM;
1822350d 643
b47ff73b
YW
644 r = x11_write_data(c);
645 if (r < 0) {
646 log_error_errno(r, "Failed to set X11 keyboard layout: %m");
647 return sd_bus_error_set_errnof(error, r, "Failed to set X11 keyboard layout: %m");
648 }
1822350d 649
b47ff73b
YW
650 log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
651 strempty(c->x11_layout),
652 strempty(c->x11_model),
653 strempty(c->x11_variant),
654 strempty(c->x11_options));
1822350d 655
b47ff73b
YW
656 (void) sd_bus_emit_properties_changed(
657 sd_bus_message_get_bus(m),
658 "/org/freedesktop/locale1",
659 "org.freedesktop.locale1",
660 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL);
1822350d 661
b47ff73b 662 if (convert) {
df4fd2c7 663 r = x11_convert_to_vconsole_and_emit(c, m);
b47ff73b
YW
664 if (r < 0)
665 log_error_errno(r, "Failed to convert keymap data: %m");
1822350d
KS
666 }
667
df2d202e 668 return sd_bus_reply_method_return(m, NULL);
1822350d
KS
669}
670
8d451309
KS
671static const sd_bus_vtable locale_vtable[] = {
672 SD_BUS_VTABLE_START(0),
6d1bd3b2 673 SD_BUS_PROPERTY("Locale", "as", property_get_locale, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
df4fd2c7
YW
674 SD_BUS_PROPERTY("X11Layout", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
675 SD_BUS_PROPERTY("X11Model", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
676 SD_BUS_PROPERTY("X11Variant", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
677 SD_BUS_PROPERTY("X11Options", "s", property_get_xkb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
678 SD_BUS_PROPERTY("VConsoleKeymap", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
679 SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", property_get_vconsole, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
adacb957
LP
680 SD_BUS_METHOD("SetLocale", "asb", NULL, method_set_locale, SD_BUS_VTABLE_UNPRIVILEGED),
681 SD_BUS_METHOD("SetVConsoleKeyboard", "ssbb", NULL, method_set_vc_keyboard, SD_BUS_VTABLE_UNPRIVILEGED),
682 SD_BUS_METHOD("SetX11Keyboard", "ssssbb", NULL, method_set_x11_keyboard, SD_BUS_VTABLE_UNPRIVILEGED),
8d451309
KS
683 SD_BUS_VTABLE_END
684};
685
686static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
4afd3348 687 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1822350d
KS
688 int r;
689
8d451309
KS
690 assert(c);
691 assert(event);
1822350d
KS
692 assert(_bus);
693
76b54375 694 r = sd_bus_default_system(&bus);
f647962d
MS
695 if (r < 0)
696 return log_error_errno(r, "Failed to get system bus connection: %m");
1822350d 697
19befb2d 698 r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/locale1", "org.freedesktop.locale1", locale_vtable, c);
f647962d
MS
699 if (r < 0)
700 return log_error_errno(r, "Failed to register object: %m");
1822350d 701
0c0b9306 702 r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.locale1", 0, NULL, NULL);
f647962d 703 if (r < 0)
0c0b9306 704 return log_error_errno(r, "Failed to request name: %m");
1822350d 705
8d451309 706 r = sd_bus_attach_event(bus, event, 0);
f647962d
MS
707 if (r < 0)
708 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1822350d 709
1cc6c93a 710 *_bus = TAKE_PTR(bus);
1822350d 711
8d451309 712 return 0;
1822350d
KS
713}
714
c6f09e6a 715static int run(int argc, char *argv[]) {
6804d7a8 716 _cleanup_(context_clear) Context context = {
df4fd2c7
YW
717 .locale_mtime = USEC_INFINITY,
718 .vc_mtime = USEC_INFINITY,
719 .x11_mtime = USEC_INFINITY,
720 };
4afd3348
LP
721 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
722 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1822350d 723 int r;
1822350d 724
6bf3c61c 725 log_setup_service();
8d451309 726
1822350d 727 umask(0022);
c3dacc8b 728 mac_selinux_init();
1822350d 729
c6f09e6a
YW
730 if (argc != 1)
731 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
1822350d 732
50008ae4
YW
733 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
734
afc6adb5 735 r = sd_event_default(&event);
c6f09e6a
YW
736 if (r < 0)
737 return log_error_errno(r, "Failed to allocate event loop: %m");
1822350d 738
50008ae4
YW
739 (void) sd_event_set_watchdog(event, true);
740
741 r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
c6f09e6a
YW
742 if (r < 0)
743 return log_error_errno(r, "Failed to install SIGINT handler: %m");
50008ae4
YW
744
745 r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
c6f09e6a
YW
746 if (r < 0)
747 return log_error_errno(r, "Failed to install SIGTERM handler: %m");
cde93897 748
8d451309 749 r = connect_bus(&context, event, &bus);
1822350d 750 if (r < 0)
c6f09e6a 751 return r;
1822350d 752
37224a5f 753 r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
4897d1dc 754 if (r < 0)
c6f09e6a 755 return log_error_errno(r, "Failed to run event loop: %m");
1822350d 756
c6f09e6a 757 return 0;
1822350d 758}
c6f09e6a
YW
759
760DEFINE_MAIN_FUNCTION(run);