]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/keymap-util.c
shared: split out polkit stuff from bus-util.c → bus-polkit.c
[thirdparty/systemd.git] / src / locale / keymap-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7
8 #include "bus-polkit.h"
9 #include "env-file-label.h"
10 #include "env-file.h"
11 #include "env-util.h"
12 #include "fd-util.h"
13 #include "fileio-label.h"
14 #include "fileio.h"
15 #include "kbd-util.h"
16 #include "keymap-util.h"
17 #include "locale-util.h"
18 #include "macro.h"
19 #include "mkdir.h"
20 #include "nulstr-util.h"
21 #include "string-util.h"
22 #include "strv.h"
23 #include "tmpfile-util.h"
24
25 static bool startswith_comma(const char *s, const char *prefix) {
26 s = startswith(s, prefix);
27 if (!s)
28 return false;
29
30 return IN_SET(*s, ',', '\0');
31 }
32
33 static const char* systemd_kbd_model_map(void) {
34 const char* s;
35
36 s = getenv("SYSTEMD_KBD_MODEL_MAP");
37 if (s)
38 return s;
39
40 return SYSTEMD_KBD_MODEL_MAP;
41 }
42
43 static const char* systemd_language_fallback_map(void) {
44 const char* s;
45
46 s = getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP");
47 if (s)
48 return s;
49
50 return SYSTEMD_LANGUAGE_FALLBACK_MAP;
51 }
52
53 static void context_free_x11(Context *c) {
54 c->x11_layout = mfree(c->x11_layout);
55 c->x11_options = mfree(c->x11_options);
56 c->x11_model = mfree(c->x11_model);
57 c->x11_variant = mfree(c->x11_variant);
58 }
59
60 static void context_free_vconsole(Context *c) {
61 c->vc_keymap = mfree(c->vc_keymap);
62 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
63 }
64
65 static void context_free_locale(Context *c) {
66 int p;
67
68 for (p = 0; p < _VARIABLE_LC_MAX; p++)
69 c->locale[p] = mfree(c->locale[p]);
70 }
71
72 void context_clear(Context *c) {
73 context_free_locale(c);
74 context_free_x11(c);
75 context_free_vconsole(c);
76
77 sd_bus_message_unref(c->locale_cache);
78 sd_bus_message_unref(c->x11_cache);
79 sd_bus_message_unref(c->vc_cache);
80
81 bus_verify_polkit_async_registry_free(c->polkit_registry);
82 };
83
84 void locale_simplify(char *locale[_VARIABLE_LC_MAX]) {
85 int p;
86
87 for (p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++)
88 if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p]))
89 locale[p] = mfree(locale[p]);
90 }
91
92 int locale_read_data(Context *c, sd_bus_message *m) {
93 struct stat st;
94 int r;
95
96 /* Do not try to re-read the file within single bus operation. */
97 if (m) {
98 if (m == c->locale_cache)
99 return 0;
100
101 sd_bus_message_unref(c->locale_cache);
102 c->locale_cache = sd_bus_message_ref(m);
103 }
104
105 r = stat("/etc/locale.conf", &st);
106 if (r < 0 && errno != ENOENT)
107 return -errno;
108
109 if (r >= 0) {
110 usec_t t;
111
112 /* If mtime is not changed, then we do not need to re-read the file. */
113 t = timespec_load(&st.st_mtim);
114 if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime)
115 return 0;
116
117 c->locale_mtime = t;
118 context_free_locale(c);
119
120 r = parse_env_file(NULL, "/etc/locale.conf",
121 "LANG", &c->locale[VARIABLE_LANG],
122 "LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
123 "LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
124 "LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
125 "LC_TIME", &c->locale[VARIABLE_LC_TIME],
126 "LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
127 "LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
128 "LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
129 "LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
130 "LC_NAME", &c->locale[VARIABLE_LC_NAME],
131 "LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
132 "LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE],
133 "LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT],
134 "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
135 if (r < 0)
136 return r;
137 } else {
138 int p;
139
140 c->locale_mtime = USEC_INFINITY;
141 context_free_locale(c);
142
143 /* Fill in what we got passed from systemd. */
144 for (p = 0; p < _VARIABLE_LC_MAX; p++) {
145 const char *name;
146
147 name = locale_variable_to_string(p);
148 assert(name);
149
150 r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name)));
151 if (r < 0)
152 return r;
153 }
154 }
155
156 locale_simplify(c->locale);
157 return 0;
158 }
159
160 int vconsole_read_data(Context *c, sd_bus_message *m) {
161 struct stat st;
162 usec_t t;
163 int r;
164
165 /* Do not try to re-read the file within single bus operation. */
166 if (m) {
167 if (m == c->vc_cache)
168 return 0;
169
170 sd_bus_message_unref(c->vc_cache);
171 c->vc_cache = sd_bus_message_ref(m);
172 }
173
174 if (stat("/etc/vconsole.conf", &st) < 0) {
175 if (errno != ENOENT)
176 return -errno;
177
178 c->vc_mtime = USEC_INFINITY;
179 context_free_vconsole(c);
180 return 0;
181 }
182
183 /* If mtime is not changed, then we do not need to re-read */
184 t = timespec_load(&st.st_mtim);
185 if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime)
186 return 0;
187
188 c->vc_mtime = t;
189 context_free_vconsole(c);
190
191 r = parse_env_file(NULL, "/etc/vconsole.conf",
192 "KEYMAP", &c->vc_keymap,
193 "KEYMAP_TOGGLE", &c->vc_keymap_toggle);
194 if (r < 0)
195 return r;
196
197 return 0;
198 }
199
200 int x11_read_data(Context *c, sd_bus_message *m) {
201 _cleanup_fclose_ FILE *f = NULL;
202 bool in_section = false;
203 struct stat st;
204 usec_t t;
205 int r;
206
207 /* Do not try to re-read the file within single bus operation. */
208 if (m) {
209 if (m == c->x11_cache)
210 return 0;
211
212 sd_bus_message_unref(c->x11_cache);
213 c->x11_cache = sd_bus_message_ref(m);
214 }
215
216 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) {
217 if (errno != ENOENT)
218 return -errno;
219
220 c->x11_mtime = USEC_INFINITY;
221 context_free_x11(c);
222 return 0;
223 }
224
225 /* If mtime is not changed, then we do not need to re-read */
226 t = timespec_load(&st.st_mtim);
227 if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime)
228 return 0;
229
230 c->x11_mtime = t;
231 context_free_x11(c);
232
233 f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
234 if (!f)
235 return -errno;
236
237 for (;;) {
238 _cleanup_free_ char *line = NULL;
239 char *l;
240
241 r = read_line(f, LONG_LINE_MAX, &line);
242 if (r < 0)
243 return r;
244 if (r == 0)
245 break;
246
247 l = strstrip(line);
248 if (IN_SET(l[0], 0, '#'))
249 continue;
250
251 if (in_section && first_word(l, "Option")) {
252 _cleanup_strv_free_ char **a = NULL;
253
254 r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_UNQUOTE);
255 if (r < 0)
256 return r;
257
258 if (strv_length(a) == 3) {
259 char **p = NULL;
260
261 if (streq(a[1], "XkbLayout"))
262 p = &c->x11_layout;
263 else if (streq(a[1], "XkbModel"))
264 p = &c->x11_model;
265 else if (streq(a[1], "XkbVariant"))
266 p = &c->x11_variant;
267 else if (streq(a[1], "XkbOptions"))
268 p = &c->x11_options;
269
270 if (p) {
271 free_and_replace(*p, a[2]);
272 }
273 }
274
275 } else if (!in_section && first_word(l, "Section")) {
276 _cleanup_strv_free_ char **a = NULL;
277
278 r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_UNQUOTE);
279 if (r < 0)
280 return -ENOMEM;
281
282 if (strv_length(a) == 2 && streq(a[1], "InputClass"))
283 in_section = true;
284
285 } else if (in_section && first_word(l, "EndSection"))
286 in_section = false;
287 }
288
289 return 0;
290 }
291
292 int locale_write_data(Context *c, char ***settings) {
293 _cleanup_strv_free_ char **l = NULL;
294 struct stat st;
295 int r, p;
296
297 /* Set values will be returned as strv in *settings on success. */
298
299 for (p = 0; p < _VARIABLE_LC_MAX; p++) {
300 _cleanup_free_ char *t = NULL;
301 char **u;
302 const char *name;
303
304 name = locale_variable_to_string(p);
305 assert(name);
306
307 if (isempty(c->locale[p]))
308 continue;
309
310 if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
311 return -ENOMEM;
312
313 u = strv_env_set(l, t);
314 if (!u)
315 return -ENOMEM;
316
317 strv_free_and_replace(l, u);
318 }
319
320 if (strv_isempty(l)) {
321 if (unlink("/etc/locale.conf") < 0)
322 return errno == ENOENT ? 0 : -errno;
323
324 c->locale_mtime = USEC_INFINITY;
325 return 0;
326 }
327
328 r = write_env_file_label("/etc/locale.conf", l);
329 if (r < 0)
330 return r;
331
332 *settings = TAKE_PTR(l);
333
334 if (stat("/etc/locale.conf", &st) >= 0)
335 c->locale_mtime = timespec_load(&st.st_mtim);
336
337 return 0;
338 }
339
340 int vconsole_write_data(Context *c) {
341 _cleanup_strv_free_ char **l = NULL;
342 struct stat st;
343 int r;
344
345 r = load_env_file(NULL, "/etc/vconsole.conf", &l);
346 if (r < 0 && r != -ENOENT)
347 return r;
348
349 if (isempty(c->vc_keymap))
350 l = strv_env_unset(l, "KEYMAP");
351 else {
352 _cleanup_free_ char *s = NULL;
353 char **u;
354
355 s = strjoin("KEYMAP=", c->vc_keymap);
356 if (!s)
357 return -ENOMEM;
358
359 u = strv_env_set(l, s);
360 if (!u)
361 return -ENOMEM;
362
363 strv_free_and_replace(l, u);
364 }
365
366 if (isempty(c->vc_keymap_toggle))
367 l = strv_env_unset(l, "KEYMAP_TOGGLE");
368 else {
369 _cleanup_free_ char *s = NULL;
370 char **u;
371
372 s = strjoin("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
373 if (!s)
374 return -ENOMEM;
375
376 u = strv_env_set(l, s);
377 if (!u)
378 return -ENOMEM;
379
380 strv_free_and_replace(l, u);
381 }
382
383 if (strv_isempty(l)) {
384 if (unlink("/etc/vconsole.conf") < 0)
385 return errno == ENOENT ? 0 : -errno;
386
387 c->vc_mtime = USEC_INFINITY;
388 return 0;
389 }
390
391 r = write_env_file_label("/etc/vconsole.conf", l);
392 if (r < 0)
393 return r;
394
395 if (stat("/etc/vconsole.conf", &st) >= 0)
396 c->vc_mtime = timespec_load(&st.st_mtim);
397
398 return 0;
399 }
400
401 int x11_write_data(Context *c) {
402 _cleanup_fclose_ FILE *f = NULL;
403 _cleanup_free_ char *temp_path = NULL;
404 struct stat st;
405 int r;
406
407 if (isempty(c->x11_layout) &&
408 isempty(c->x11_model) &&
409 isempty(c->x11_variant) &&
410 isempty(c->x11_options)) {
411
412 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
413 return errno == ENOENT ? 0 : -errno;
414
415 c->vc_mtime = USEC_INFINITY;
416 return 0;
417 }
418
419 (void) mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
420 r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
421 if (r < 0)
422 return r;
423
424 (void) fchmod(fileno(f), 0644);
425
426 fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
427 "# probably wise not to edit this file manually. Use localectl(1) to\n"
428 "# instruct systemd-localed to update it.\n"
429 "Section \"InputClass\"\n"
430 " Identifier \"system-keyboard\"\n"
431 " MatchIsKeyboard \"on\"\n", f);
432
433 if (!isempty(c->x11_layout))
434 fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout);
435
436 if (!isempty(c->x11_model))
437 fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model);
438
439 if (!isempty(c->x11_variant))
440 fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant);
441
442 if (!isempty(c->x11_options))
443 fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options);
444
445 fputs("EndSection\n", f);
446
447 r = fflush_sync_and_check(f);
448 if (r < 0)
449 goto fail;
450
451 if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
452 r = -errno;
453 goto fail;
454 }
455
456 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0)
457 c->x11_mtime = timespec_load(&st.st_mtim);
458
459 return 0;
460
461 fail:
462 if (temp_path)
463 (void) unlink(temp_path);
464
465 return r;
466 }
467
468 static int read_next_mapping(const char* filename,
469 unsigned min_fields, unsigned max_fields,
470 FILE *f, unsigned *n, char ***a) {
471 assert(f);
472 assert(n);
473 assert(a);
474
475 for (;;) {
476 _cleanup_free_ char *line = NULL;
477 size_t length;
478 char *l, **b;
479 int r;
480
481 r = read_line(f, LONG_LINE_MAX, &line);
482 if (r < 0)
483 return r;
484 if (r == 0)
485 break;
486
487 (*n)++;
488
489 l = strstrip(line);
490 if (IN_SET(l[0], 0, '#'))
491 continue;
492
493 r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_UNQUOTE);
494 if (r < 0)
495 return r;
496
497 length = strv_length(b);
498 if (length < min_fields || length > max_fields) {
499 log_error("Invalid line %s:%u, ignoring.", filename, *n);
500 strv_free(b);
501 continue;
502
503 }
504
505 *a = b;
506 return 1;
507 }
508
509 return 0;
510 }
511
512 int vconsole_convert_to_x11(Context *c) {
513 const char *map;
514 int modified = -1;
515
516 map = systemd_kbd_model_map();
517
518 if (isempty(c->vc_keymap)) {
519 modified =
520 !isempty(c->x11_layout) ||
521 !isempty(c->x11_model) ||
522 !isempty(c->x11_variant) ||
523 !isempty(c->x11_options);
524
525 context_free_x11(c);
526 } else {
527 _cleanup_fclose_ FILE *f = NULL;
528 unsigned n = 0;
529
530 f = fopen(map, "re");
531 if (!f)
532 return -errno;
533
534 for (;;) {
535 _cleanup_strv_free_ char **a = NULL;
536 int r;
537
538 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
539 if (r < 0)
540 return r;
541 if (r == 0)
542 break;
543
544 if (!streq(c->vc_keymap, a[0]))
545 continue;
546
547 if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
548 !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
549 !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
550 !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
551
552 if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
553 free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
554 free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
555 free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
556 return -ENOMEM;
557
558 modified = true;
559 }
560
561 break;
562 }
563 }
564
565 if (modified > 0)
566 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
567 strempty(c->x11_layout),
568 strempty(c->x11_model),
569 strempty(c->x11_variant),
570 strempty(c->x11_options));
571 else if (modified < 0)
572 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
573 c->vc_keymap);
574 else
575 log_debug("X11 keyboard layout did not need to be modified.");
576
577 return modified > 0;
578 }
579
580 int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) {
581 const char *dir;
582 _cleanup_free_ char *n;
583
584 if (x11_variant)
585 n = strjoin(x11_layout, "-", x11_variant);
586 else
587 n = strdup(x11_layout);
588 if (!n)
589 return -ENOMEM;
590
591 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
592 _cleanup_free_ char *p = NULL, *pz = NULL;
593 bool uncompressed;
594
595 p = strjoin(dir, "xkb/", n, ".map");
596 pz = strjoin(dir, "xkb/", n, ".map.gz");
597 if (!p || !pz)
598 return -ENOMEM;
599
600 uncompressed = access(p, F_OK) == 0;
601 if (uncompressed || access(pz, F_OK) == 0) {
602 log_debug("Found converted keymap %s at %s",
603 n, uncompressed ? p : pz);
604
605 *new_keymap = TAKE_PTR(n);
606 return 1;
607 }
608 }
609
610 return 0;
611 }
612
613 int find_legacy_keymap(Context *c, char **ret) {
614 const char *map;
615 _cleanup_fclose_ FILE *f = NULL;
616 _cleanup_free_ char *new_keymap = NULL;
617 unsigned n = 0;
618 unsigned best_matching = 0;
619 int r;
620
621 assert(!isempty(c->x11_layout));
622
623 map = systemd_kbd_model_map();
624
625 f = fopen(map, "re");
626 if (!f)
627 return -errno;
628
629 for (;;) {
630 _cleanup_strv_free_ char **a = NULL;
631 unsigned matching = 0;
632
633 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
634 if (r < 0)
635 return r;
636 if (r == 0)
637 break;
638
639 /* Determine how well matching this entry is */
640 if (streq(c->x11_layout, a[1]))
641 /* If we got an exact match, this is best */
642 matching = 10;
643 else {
644 /* We have multiple X layouts, look for an
645 * entry that matches our key with everything
646 * but the first layout stripped off. */
647 if (startswith_comma(c->x11_layout, a[1]))
648 matching = 5;
649 else {
650 _cleanup_free_ char *x = NULL;
651
652 /* If that didn't work, strip off the
653 * other layouts from the entry, too */
654 x = strndup(a[1], strcspn(a[1], ","));
655 if (startswith_comma(c->x11_layout, x))
656 matching = 1;
657 }
658 }
659
660 if (matching > 0) {
661 if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) {
662 matching++;
663
664 if (streq_ptr(c->x11_variant, a[3])) {
665 matching++;
666
667 if (streq_ptr(c->x11_options, a[4]))
668 matching++;
669 }
670 }
671 }
672
673 /* The best matching entry so far, then let's save that */
674 if (matching >= MAX(best_matching, 1u)) {
675 log_debug("Found legacy keymap %s with score %u",
676 a[0], matching);
677
678 if (matching > best_matching) {
679 best_matching = matching;
680
681 r = free_and_strdup(&new_keymap, a[0]);
682 if (r < 0)
683 return r;
684 }
685 }
686 }
687
688 if (best_matching < 10 && c->x11_layout) {
689 /* The best match is only the first part of the X11
690 * keymap. Check if we have a converted map which
691 * matches just the first layout.
692 */
693 char *l, *v = NULL, *converted;
694
695 l = strndupa(c->x11_layout, strcspn(c->x11_layout, ","));
696 if (c->x11_variant)
697 v = strndupa(c->x11_variant, strcspn(c->x11_variant, ","));
698 r = find_converted_keymap(l, v, &converted);
699 if (r < 0)
700 return r;
701 if (r > 0)
702 free_and_replace(new_keymap, converted);
703 }
704
705 *ret = TAKE_PTR(new_keymap);
706 return (bool) *ret;
707 }
708
709 int find_language_fallback(const char *lang, char **language) {
710 const char *map;
711 _cleanup_fclose_ FILE *f = NULL;
712 unsigned n = 0;
713
714 assert(lang);
715 assert(language);
716
717 map = systemd_language_fallback_map();
718
719 f = fopen(map, "re");
720 if (!f)
721 return -errno;
722
723 for (;;) {
724 _cleanup_strv_free_ char **a = NULL;
725 int r;
726
727 r = read_next_mapping(map, 2, 2, f, &n, &a);
728 if (r <= 0)
729 return r;
730
731 if (streq(lang, a[0])) {
732 assert(strv_length(a) == 2);
733 *language = TAKE_PTR(a[1]);
734 return 1;
735 }
736 }
737
738 assert_not_reached("should not be here");
739 }
740
741 int x11_convert_to_vconsole(Context *c) {
742 bool modified = false;
743
744 if (isempty(c->x11_layout)) {
745 modified =
746 !isempty(c->vc_keymap) ||
747 !isempty(c->vc_keymap_toggle);
748
749 context_free_vconsole(c);
750 } else {
751 _cleanup_free_ char *new_keymap = NULL;
752 int r;
753
754 r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap);
755 if (r < 0)
756 return r;
757 else if (r == 0) {
758 r = find_legacy_keymap(c, &new_keymap);
759 if (r < 0)
760 return r;
761 }
762 if (r == 0)
763 /* We search for layout-variant match first, but then we also look
764 * for anything which matches just the layout. So it's accurate to say
765 * that we couldn't find anything which matches the layout. */
766 log_notice("No conversion to virtual console map found for \"%s\".",
767 c->x11_layout);
768
769 if (!streq_ptr(c->vc_keymap, new_keymap)) {
770 free_and_replace(c->vc_keymap, new_keymap);
771 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
772 modified = true;
773 }
774 }
775
776 if (modified)
777 log_info("Changing virtual console keymap to '%s' toggle '%s'",
778 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
779 else
780 log_debug("Virtual console keymap was not modified.");
781
782 return modified;
783 }