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