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