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