1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2013 Kay Sievers
11 #include <xkbcommon/xkbcommon.h>
17 #include "alloc-util.h"
18 #include "bus-error.h"
19 #include "bus-message.h"
22 #include "keymap-util.h"
23 #include "locale-util.h"
25 #include "path-util.h"
26 #include "selinux-util.h"
27 #include "string-util.h"
29 #include "user-util.h"
31 static Hashmap
*polkit_registry
= NULL
;
33 static 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
;
36 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
37 sd_bus_error error
= SD_BUS_ERROR_NULL
;
38 unsigned c_set
, c_unset
, p
;
43 l_unset
= new0(char*, _VARIABLE_LC_MAX
);
47 l_set
= new0(char*, _VARIABLE_LC_MAX
);
51 for (p
= 0, c_set
= 0, c_unset
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
54 name
= locale_variable_to_string(p
);
57 if (isempty(c
->locale
[p
]))
58 l_unset
[c_set
++] = (char*) name
;
62 if (asprintf(&s
, "%s=%s", name
, c
->locale
[p
]) < 0)
69 assert(c_set
+ c_unset
== _VARIABLE_LC_MAX
);
70 r
= sd_bus_message_new_method_call(bus
, &m
,
71 "org.freedesktop.systemd1",
72 "/org/freedesktop/systemd1",
73 "org.freedesktop.systemd1.Manager",
74 "UnsetAndSetEnvironment");
78 r
= sd_bus_message_append_strv(m
, l_unset
);
82 r
= sd_bus_message_append_strv(m
, l_set
);
86 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
88 log_error_errno(r
, "Failed to update the manager environment, ignoring: %m");
93 static int vconsole_reload(sd_bus
*bus
) {
94 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
99 r
= sd_bus_call_method(bus
,
100 "org.freedesktop.systemd1",
101 "/org/freedesktop/systemd1",
102 "org.freedesktop.systemd1.Manager",
106 "ss", "systemd-vconsole-setup.service", "replace");
109 log_error("Failed to issue method call: %s", bus_error_message(&error
, -r
));
113 static int vconsole_convert_to_x11_and_emit(Context
*c
, sd_bus_message
*m
) {
118 r
= x11_read_data(c
, m
);
122 r
= vconsole_convert_to_x11(c
);
127 r
= x11_write_data(c
);
129 return log_error_errno(r
, "Failed to write X11 keyboard layout: %m");
131 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
132 "/org/freedesktop/locale1",
133 "org.freedesktop.locale1",
134 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL
);
139 static int x11_convert_to_vconsole_and_emit(Context
*c
, sd_bus_message
*m
) {
144 r
= vconsole_read_data(c
, m
);
148 r
= x11_convert_to_vconsole(c
);
153 r
= vconsole_write_data(c
);
155 log_error_errno(r
, "Failed to save virtual console keymap: %m");
157 sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
),
158 "/org/freedesktop/locale1",
159 "org.freedesktop.locale1",
160 "VConsoleKeymap", "VConsoleKeymapToggle", NULL
);
162 return vconsole_reload(sd_bus_message_get_bus(m
));
165 static int property_get_locale(
168 const char *interface
,
169 const char *property
,
170 sd_bus_message
*reply
,
172 sd_bus_error
*error
) {
174 Context
*c
= userdata
;
175 _cleanup_strv_free_
char **l
= NULL
;
178 r
= locale_read_data(c
, reply
);
182 l
= new0(char*, _VARIABLE_LC_MAX
+1);
186 for (p
= 0, q
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
190 name
= locale_variable_to_string(p
);
193 if (isempty(c
->locale
[p
]))
196 if (asprintf(&t
, "%s=%s", name
, c
->locale
[p
]) < 0)
202 return sd_bus_message_append_strv(reply
, l
);
205 static int property_get_vconsole(
208 const char *interface
,
209 const char *property
,
210 sd_bus_message
*reply
,
212 sd_bus_error
*error
) {
214 Context
*c
= userdata
;
217 r
= vconsole_read_data(c
, reply
);
221 if (streq(property
, "VConsoleKeymap"))
222 return sd_bus_message_append_basic(reply
, 's', c
->vc_keymap
);
223 else if (streq(property
, "VConsoleKeymapToggle"))
224 return sd_bus_message_append_basic(reply
, 's', c
->vc_keymap_toggle
);
229 static int property_get_xkb(
232 const char *interface
,
233 const char *property
,
234 sd_bus_message
*reply
,
236 sd_bus_error
*error
) {
238 Context
*c
= userdata
;
241 r
= x11_read_data(c
, reply
);
245 if (streq(property
, "X11Layout"))
246 return sd_bus_message_append_basic(reply
, 's', c
->x11_layout
);
247 else if (streq(property
, "X11Model"))
248 return sd_bus_message_append_basic(reply
, 's', c
->x11_model
);
249 else if (streq(property
, "X11Variant"))
250 return sd_bus_message_append_basic(reply
, 's', c
->x11_variant
);
251 else if (streq(property
, "X11Options"))
252 return sd_bus_message_append_basic(reply
, 's', c
->x11_options
);
257 static void locale_free(char ***l
) {
260 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
261 (*l
)[p
] = mfree((*l
)[p
]);
264 static int method_set_locale(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
265 Context
*c
= userdata
;
266 _cleanup_strv_free_
char **settings
= NULL
, **l
= NULL
;
267 char *new_locale
[_VARIABLE_LC_MAX
] = {}, **i
;
268 _cleanup_(locale_free
) _unused_
char **dummy
= new_locale
;
269 bool modified
= false;
270 int interactive
, p
, r
;
275 r
= bus_message_read_strv_extend(m
, &l
);
279 r
= sd_bus_message_read_basic(m
, 'b', &interactive
);
283 /* If single locale without variable name is provided, then we assume it is LANG=. */
284 if (strv_length(l
) == 1 && !strchr(*l
, '=')) {
285 if (!locale_is_valid(*l
))
286 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid Locale data.");
288 new_locale
[VARIABLE_LANG
] = strdup(*l
);
289 if (!new_locale
[VARIABLE_LANG
])
295 /* Check whether a variable is valid */
299 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++) {
303 name
= locale_variable_to_string(p
);
307 if (startswith(*i
, name
) &&
309 locale_is_valid((*i
) + k
+ 1)) {
312 new_locale
[p
] = strdup((*i
) + k
+ 1);
321 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid Locale data.");
324 /* If LANG was specified, but not LANGUAGE, check if we should
325 * set it based on the language fallback table. */
326 if (!isempty(new_locale
[VARIABLE_LANG
]) &&
327 isempty(new_locale
[VARIABLE_LANGUAGE
])) {
328 _cleanup_free_
char *language
= NULL
;
330 (void) find_language_fallback(new_locale
[VARIABLE_LANG
], &language
);
332 log_debug("Converted LANG=%s to LANGUAGE=%s", new_locale
[VARIABLE_LANG
], language
);
333 free_and_replace(new_locale
[VARIABLE_LANGUAGE
], language
);
337 r
= locale_read_data(c
, m
);
339 log_error_errno(r
, "Failed to read locale data: %m");
340 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Failed to read locale data");
343 /* Merge with the current settings */
344 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
345 if (!isempty(c
->locale
[p
]) && isempty(new_locale
[p
])) {
346 new_locale
[p
] = strdup(c
->locale
[p
]);
351 locale_simplify(new_locale
);
353 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
354 if (!streq_ptr(c
->locale
[p
], new_locale
[p
])) {
360 log_debug("Locale settings were not modified.");
361 return sd_bus_reply_method_return(m
, NULL
);
364 r
= bus_verify_polkit_async(
367 "org.freedesktop.locale1.set-locale",
376 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
378 for (p
= 0; p
< _VARIABLE_LC_MAX
; p
++)
379 free_and_replace(c
->locale
[p
], new_locale
[p
]);
381 r
= locale_write_data(c
, &settings
);
383 log_error_errno(r
, "Failed to set locale: %m");
384 return sd_bus_error_set_errnof(error
, r
, "Failed to set locale: %m");
387 (void) locale_update_system_manager(c
, sd_bus_message_get_bus(m
));
390 _cleanup_free_
char *line
;
392 line
= strv_join(settings
, ", ");
393 log_info("Changed locale to %s.", strnull(line
));
395 log_info("Changed locale to unset.");
397 (void) sd_bus_emit_properties_changed(
398 sd_bus_message_get_bus(m
),
399 "/org/freedesktop/locale1",
400 "org.freedesktop.locale1",
403 return sd_bus_reply_method_return(m
, NULL
);
406 static int method_set_vc_keyboard(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
407 Context
*c
= userdata
;
408 const char *keymap
, *keymap_toggle
;
409 int convert
, interactive
, r
;
414 r
= sd_bus_message_read(m
, "ssbb", &keymap
, &keymap_toggle
, &convert
, &interactive
);
418 keymap
= empty_to_null(keymap
);
419 keymap_toggle
= empty_to_null(keymap_toggle
);
421 r
= vconsole_read_data(c
, m
);
423 log_error_errno(r
, "Failed to read virtual console keymap data: %m");
424 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Failed to read virtual console keymap data");
427 if (streq_ptr(keymap
, c
->vc_keymap
) &&
428 streq_ptr(keymap_toggle
, c
->vc_keymap_toggle
))
429 return sd_bus_reply_method_return(m
, NULL
);
431 if ((keymap
&& (!filename_is_valid(keymap
) || !string_is_safe(keymap
))) ||
432 (keymap_toggle
&& (!filename_is_valid(keymap_toggle
) || !string_is_safe(keymap_toggle
))))
433 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Received invalid keymap data");
435 r
= bus_verify_polkit_async(
438 "org.freedesktop.locale1.set-keyboard",
447 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
449 if (free_and_strdup(&c
->vc_keymap
, keymap
) < 0 ||
450 free_and_strdup(&c
->vc_keymap_toggle
, keymap_toggle
) < 0)
453 r
= vconsole_write_data(c
);
455 log_error_errno(r
, "Failed to set virtual console keymap: %m");
456 return sd_bus_error_set_errnof(error
, r
, "Failed to set virtual console keymap: %m");
459 log_info("Changed virtual console keymap to '%s' toggle '%s'",
460 strempty(c
->vc_keymap
), strempty(c
->vc_keymap_toggle
));
462 r
= vconsole_reload(sd_bus_message_get_bus(m
));
464 log_error_errno(r
, "Failed to request keymap reload: %m");
466 (void) sd_bus_emit_properties_changed(
467 sd_bus_message_get_bus(m
),
468 "/org/freedesktop/locale1",
469 "org.freedesktop.locale1",
470 "VConsoleKeymap", "VConsoleKeymapToggle", NULL
);
473 r
= vconsole_convert_to_x11_and_emit(c
, m
);
475 log_error_errno(r
, "Failed to convert keymap data: %m");
478 return sd_bus_reply_method_return(m
, NULL
);
484 static void log_xkb(struct xkb_context
*ctx
, enum xkb_log_level lvl
, const char *format
, va_list args
) {
487 fmt
= strjoina("libxkbcommon: ", format
);
488 #pragma GCC diagnostic push
489 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
490 log_internalv(LOG_DEBUG
, 0, __FILE__
, __LINE__
, __func__
, fmt
, args
);
491 #pragma GCC diagnostic pop
494 #define LOAD_SYMBOL(symbol, dl, name) \
496 (symbol) = (typeof(symbol)) dlvsym((dl), (name), "V_0.5.0"); \
497 (symbol) ? 0 : -EOPNOTSUPP; \
500 static int verify_xkb_rmlvo(const char *model
, const char *layout
, const char *variant
, const char *options
) {
502 /* We dlopen() the library in order to make the dependency soft. The library (and what it pulls in) is huge
503 * after all, hence let's support XKB maps when the library is around, and refuse otherwise. The function
504 * pointers to the shared library are below: */
506 struct xkb_context
* (*symbol_xkb_context_new
)(enum xkb_context_flags flags
) = NULL
;
507 void (*symbol_xkb_context_unref
)(struct xkb_context
*context
) = NULL
;
508 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
;
509 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
;
510 void (*symbol_xkb_keymap_unref
)(struct xkb_keymap
*keymap
) = NULL
;
512 const struct xkb_rule_names rmlvo
= {
518 struct xkb_context
*ctx
= NULL
;
519 struct xkb_keymap
*km
= NULL
;
523 /* Compile keymap from RMLVO information to check out its validity */
525 dl
= dlopen("libxkbcommon.so.0", RTLD_LAZY
);
529 r
= LOAD_SYMBOL(symbol_xkb_context_new
, dl
, "xkb_context_new");
533 r
= LOAD_SYMBOL(symbol_xkb_context_unref
, dl
, "xkb_context_unref");
537 r
= LOAD_SYMBOL(symbol_xkb_context_set_log_fn
, dl
, "xkb_context_set_log_fn");
541 r
= LOAD_SYMBOL(symbol_xkb_keymap_new_from_names
, dl
, "xkb_keymap_new_from_names");
545 r
= LOAD_SYMBOL(symbol_xkb_keymap_unref
, dl
, "xkb_keymap_unref");
549 ctx
= symbol_xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES
);
555 symbol_xkb_context_set_log_fn(ctx
, log_xkb
);
557 km
= symbol_xkb_keymap_new_from_names(ctx
, &rmlvo
, XKB_KEYMAP_COMPILE_NO_FLAGS
);
566 if (symbol_xkb_keymap_unref
&& km
)
567 symbol_xkb_keymap_unref(km
);
569 if (symbol_xkb_context_unref
&& ctx
)
570 symbol_xkb_context_unref(ctx
);
578 static int verify_xkb_rmlvo(const char *model
, const char *layout
, const char *variant
, const char *options
) {
584 static int method_set_x11_keyboard(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
585 Context
*c
= userdata
;
586 const char *layout
, *model
, *variant
, *options
;
587 int convert
, interactive
, r
;
592 r
= sd_bus_message_read(m
, "ssssbb", &layout
, &model
, &variant
, &options
, &convert
, &interactive
);
596 layout
= empty_to_null(layout
);
597 model
= empty_to_null(model
);
598 variant
= empty_to_null(variant
);
599 options
= empty_to_null(options
);
601 r
= x11_read_data(c
, m
);
603 log_error_errno(r
, "Failed to read x11 keyboard layout data: %m");
604 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Failed to read x11 keyboard layout data");
607 if (streq_ptr(layout
, c
->x11_layout
) &&
608 streq_ptr(model
, c
->x11_model
) &&
609 streq_ptr(variant
, c
->x11_variant
) &&
610 streq_ptr(options
, c
->x11_options
))
611 return sd_bus_reply_method_return(m
, NULL
);
613 if ((layout
&& !string_is_safe(layout
)) ||
614 (model
&& !string_is_safe(model
)) ||
615 (variant
&& !string_is_safe(variant
)) ||
616 (options
&& !string_is_safe(options
)))
617 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Received invalid keyboard data");
619 r
= verify_xkb_rmlvo(model
, layout
, variant
, options
);
621 log_error_errno(r
, "Cannot compile XKB keymap for new x11 keyboard layout ('%s' / '%s' / '%s' / '%s'): %m",
622 strempty(model
), strempty(layout
), strempty(variant
), strempty(options
));
624 if (r
== -EOPNOTSUPP
)
625 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Local keyboard configuration not supported on this system.");
627 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified keymap cannot be compiled, refusing as invalid.");
630 r
= bus_verify_polkit_async(
633 "org.freedesktop.locale1.set-keyboard",
642 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
644 if (free_and_strdup(&c
->x11_layout
, layout
) < 0 ||
645 free_and_strdup(&c
->x11_model
, model
) < 0 ||
646 free_and_strdup(&c
->x11_variant
, variant
) < 0 ||
647 free_and_strdup(&c
->x11_options
, options
) < 0)
650 r
= x11_write_data(c
);
652 log_error_errno(r
, "Failed to set X11 keyboard layout: %m");
653 return sd_bus_error_set_errnof(error
, r
, "Failed to set X11 keyboard layout: %m");
656 log_info("Changed X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
657 strempty(c
->x11_layout
),
658 strempty(c
->x11_model
),
659 strempty(c
->x11_variant
),
660 strempty(c
->x11_options
));
662 (void) sd_bus_emit_properties_changed(
663 sd_bus_message_get_bus(m
),
664 "/org/freedesktop/locale1",
665 "org.freedesktop.locale1",
666 "X11Layout", "X11Model", "X11Variant", "X11Options", NULL
);
669 r
= x11_convert_to_vconsole_and_emit(c
, m
);
671 log_error_errno(r
, "Failed to convert keymap data: %m");
674 return sd_bus_reply_method_return(m
, NULL
);
677 static const sd_bus_vtable locale_vtable
[] = {
678 SD_BUS_VTABLE_START(0),
679 SD_BUS_PROPERTY("Locale", "as", property_get_locale
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
680 SD_BUS_PROPERTY("X11Layout", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
681 SD_BUS_PROPERTY("X11Model", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
682 SD_BUS_PROPERTY("X11Variant", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
683 SD_BUS_PROPERTY("X11Options", "s", property_get_xkb
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
684 SD_BUS_PROPERTY("VConsoleKeymap", "s", property_get_vconsole
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
685 SD_BUS_PROPERTY("VConsoleKeymapToggle", "s", property_get_vconsole
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
686 SD_BUS_METHOD("SetLocale", "asb", NULL
, method_set_locale
, SD_BUS_VTABLE_UNPRIVILEGED
),
687 SD_BUS_METHOD("SetVConsoleKeyboard", "ssbb", NULL
, method_set_vc_keyboard
, SD_BUS_VTABLE_UNPRIVILEGED
),
688 SD_BUS_METHOD("SetX11Keyboard", "ssssbb", NULL
, method_set_x11_keyboard
, SD_BUS_VTABLE_UNPRIVILEGED
),
692 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**_bus
) {
693 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
700 r
= sd_bus_default_system(&bus
);
702 return log_error_errno(r
, "Failed to get system bus connection: %m");
704 r
= sd_bus_add_object_vtable(bus
, NULL
, "/org/freedesktop/locale1", "org.freedesktop.locale1", locale_vtable
, c
);
706 return log_error_errno(r
, "Failed to register object: %m");
708 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.locale1", 0, NULL
, NULL
);
710 return log_error_errno(r
, "Failed to request name: %m");
712 r
= sd_bus_attach_event(bus
, event
, 0);
714 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
716 *_bus
= TAKE_PTR(bus
);
721 int main(int argc
, char *argv
[]) {
722 _cleanup_(context_free
) Context context
= {
723 .locale_mtime
= USEC_INFINITY
,
724 .vc_mtime
= USEC_INFINITY
,
725 .x11_mtime
= USEC_INFINITY
,
727 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
728 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
731 log_set_target(LOG_TARGET_AUTO
);
732 log_parse_environment();
739 log_error("This program takes no arguments.");
744 r
= sd_event_default(&event
);
746 log_error_errno(r
, "Failed to allocate event loop: %m");
750 sd_event_set_watchdog(event
, true);
752 r
= connect_bus(&context
, event
, &bus
);
756 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.locale1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
758 log_error_errno(r
, "Failed to run event loop: %m");
761 bus_verify_polkit_async_registry_free(polkit_registry
);
763 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;