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