]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/keymap-util.c
license: LGPL-2.1+ -> LGPL-2.1-or-later
[thirdparty/systemd.git] / src / locale / keymap-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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_full(&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 } else if (!in_section && first_word(l, "Section")) {
275 _cleanup_strv_free_ char **a = NULL;
276
277 r = strv_split_full(&a, l, WHITESPACE, EXTRACT_UNQUOTE);
278 if (r < 0)
279 return -ENOMEM;
280
281 if (strv_length(a) == 2 && streq(a[1], "InputClass"))
282 in_section = true;
283
284 } else if (in_section && first_word(l, "EndSection"))
285 in_section = false;
286 }
287
288 return 0;
289 }
290
291 int locale_write_data(Context *c, char ***settings) {
292 _cleanup_strv_free_ char **l = NULL;
293 struct stat st;
294 int r, p;
295
296 /* Set values will be returned as strv in *settings on success. */
297
298 for (p = 0; p < _VARIABLE_LC_MAX; p++) {
299 _cleanup_free_ char *t = NULL;
300 char **u;
301 const char *name;
302
303 name = locale_variable_to_string(p);
304 assert(name);
305
306 if (isempty(c->locale[p]))
307 continue;
308
309 if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
310 return -ENOMEM;
311
312 u = strv_env_set(l, t);
313 if (!u)
314 return -ENOMEM;
315
316 strv_free_and_replace(l, u);
317 }
318
319 if (strv_isempty(l)) {
320 if (unlink("/etc/locale.conf") < 0)
321 return errno == ENOENT ? 0 : -errno;
322
323 c->locale_mtime = USEC_INFINITY;
324 return 0;
325 }
326
327 r = write_env_file_label("/etc/locale.conf", l);
328 if (r < 0)
329 return r;
330
331 *settings = TAKE_PTR(l);
332
333 if (stat("/etc/locale.conf", &st) >= 0)
334 c->locale_mtime = timespec_load(&st.st_mtim);
335
336 return 0;
337 }
338
339 int vconsole_write_data(Context *c) {
340 _cleanup_strv_free_ char **l = NULL;
341 struct stat st;
342 int r;
343
344 r = load_env_file(NULL, "/etc/vconsole.conf", &l);
345 if (r < 0 && r != -ENOENT)
346 return r;
347
348 if (isempty(c->vc_keymap))
349 l = strv_env_unset(l, "KEYMAP");
350 else {
351 _cleanup_free_ char *s = NULL;
352 char **u;
353
354 s = strjoin("KEYMAP=", c->vc_keymap);
355 if (!s)
356 return -ENOMEM;
357
358 u = strv_env_set(l, s);
359 if (!u)
360 return -ENOMEM;
361
362 strv_free_and_replace(l, u);
363 }
364
365 if (isempty(c->vc_keymap_toggle))
366 l = strv_env_unset(l, "KEYMAP_TOGGLE");
367 else {
368 _cleanup_free_ char *s = NULL;
369 char **u;
370
371 s = strjoin("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
372 if (!s)
373 return -ENOMEM;
374
375 u = strv_env_set(l, s);
376 if (!u)
377 return -ENOMEM;
378
379 strv_free_and_replace(l, u);
380 }
381
382 if (strv_isempty(l)) {
383 if (unlink("/etc/vconsole.conf") < 0)
384 return errno == ENOENT ? 0 : -errno;
385
386 c->vc_mtime = USEC_INFINITY;
387 return 0;
388 }
389
390 r = write_env_file_label("/etc/vconsole.conf", l);
391 if (r < 0)
392 return r;
393
394 if (stat("/etc/vconsole.conf", &st) >= 0)
395 c->vc_mtime = timespec_load(&st.st_mtim);
396
397 return 0;
398 }
399
400 int x11_write_data(Context *c) {
401 _cleanup_fclose_ FILE *f = NULL;
402 _cleanup_free_ char *temp_path = NULL;
403 struct stat st;
404 int r;
405
406 if (isempty(c->x11_layout) &&
407 isempty(c->x11_model) &&
408 isempty(c->x11_variant) &&
409 isempty(c->x11_options)) {
410
411 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
412 return errno == ENOENT ? 0 : -errno;
413
414 c->vc_mtime = USEC_INFINITY;
415 return 0;
416 }
417
418 (void) mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
419 r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
420 if (r < 0)
421 return r;
422
423 (void) fchmod(fileno(f), 0644);
424
425 fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
426 "# probably wise not to edit this file manually. Use localectl(1) to\n"
427 "# instruct systemd-localed to update it.\n"
428 "Section \"InputClass\"\n"
429 " Identifier \"system-keyboard\"\n"
430 " MatchIsKeyboard \"on\"\n", f);
431
432 if (!isempty(c->x11_layout))
433 fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout);
434
435 if (!isempty(c->x11_model))
436 fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model);
437
438 if (!isempty(c->x11_variant))
439 fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant);
440
441 if (!isempty(c->x11_options))
442 fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options);
443
444 fputs("EndSection\n", f);
445
446 r = fflush_sync_and_check(f);
447 if (r < 0)
448 goto fail;
449
450 if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
451 r = -errno;
452 goto fail;
453 }
454
455 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0)
456 c->x11_mtime = timespec_load(&st.st_mtim);
457
458 return 0;
459
460 fail:
461 if (temp_path)
462 (void) unlink(temp_path);
463
464 return r;
465 }
466
467 static int read_next_mapping(const char* filename,
468 unsigned min_fields, unsigned max_fields,
469 FILE *f, unsigned *n, char ***a) {
470 assert(f);
471 assert(n);
472 assert(a);
473
474 for (;;) {
475 _cleanup_free_ char *line = NULL;
476 size_t length;
477 char *l, **b;
478 int r;
479
480 r = read_line(f, LONG_LINE_MAX, &line);
481 if (r < 0)
482 return r;
483 if (r == 0)
484 break;
485
486 (*n)++;
487
488 l = strstrip(line);
489 if (IN_SET(l[0], 0, '#'))
490 continue;
491
492 r = strv_split_full(&b, l, WHITESPACE, EXTRACT_UNQUOTE);
493 if (r < 0)
494 return r;
495
496 length = strv_length(b);
497 if (length < min_fields || length > max_fields) {
498 log_error("Invalid line %s:%u, ignoring.", filename, *n);
499 strv_free(b);
500 continue;
501
502 }
503
504 *a = b;
505 return 1;
506 }
507
508 return 0;
509 }
510
511 int vconsole_convert_to_x11(Context *c) {
512 const char *map;
513 int modified = -1;
514
515 map = systemd_kbd_model_map();
516
517 if (isempty(c->vc_keymap)) {
518 modified =
519 !isempty(c->x11_layout) ||
520 !isempty(c->x11_model) ||
521 !isempty(c->x11_variant) ||
522 !isempty(c->x11_options);
523
524 context_free_x11(c);
525 } else {
526 _cleanup_fclose_ FILE *f = NULL;
527 unsigned n = 0;
528
529 f = fopen(map, "re");
530 if (!f)
531 return -errno;
532
533 for (;;) {
534 _cleanup_strv_free_ char **a = NULL;
535 int r;
536
537 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
538 if (r < 0)
539 return r;
540 if (r == 0)
541 break;
542
543 if (!streq(c->vc_keymap, a[0]))
544 continue;
545
546 if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
547 !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
548 !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
549 !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
550
551 if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
552 free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
553 free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
554 free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
555 return -ENOMEM;
556
557 modified = true;
558 }
559
560 break;
561 }
562 }
563
564 if (modified > 0)
565 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
566 strempty(c->x11_layout),
567 strempty(c->x11_model),
568 strempty(c->x11_variant),
569 strempty(c->x11_options));
570 else if (modified < 0)
571 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
572 c->vc_keymap);
573 else
574 log_debug("X11 keyboard layout did not need to be modified.");
575
576 return modified > 0;
577 }
578
579 int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) {
580 const char *dir;
581 _cleanup_free_ char *n;
582
583 if (x11_variant)
584 n = strjoin(x11_layout, "-", x11_variant);
585 else
586 n = strdup(x11_layout);
587 if (!n)
588 return -ENOMEM;
589
590 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
591 _cleanup_free_ char *p = NULL, *pz = NULL;
592 bool uncompressed;
593
594 p = strjoin(dir, "xkb/", n, ".map");
595 pz = strjoin(dir, "xkb/", n, ".map.gz");
596 if (!p || !pz)
597 return -ENOMEM;
598
599 uncompressed = access(p, F_OK) == 0;
600 if (uncompressed || access(pz, F_OK) == 0) {
601 log_debug("Found converted keymap %s at %s",
602 n, uncompressed ? p : pz);
603
604 *new_keymap = TAKE_PTR(n);
605 return 1;
606 }
607 }
608
609 return 0;
610 }
611
612 int find_legacy_keymap(Context *c, char **ret) {
613 const char *map;
614 _cleanup_fclose_ FILE *f = NULL;
615 _cleanup_free_ char *new_keymap = NULL;
616 unsigned n = 0;
617 unsigned best_matching = 0;
618 int r;
619
620 assert(!isempty(c->x11_layout));
621
622 map = systemd_kbd_model_map();
623
624 f = fopen(map, "re");
625 if (!f)
626 return -errno;
627
628 for (;;) {
629 _cleanup_strv_free_ char **a = NULL;
630 unsigned matching = 0;
631
632 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
633 if (r < 0)
634 return r;
635 if (r == 0)
636 break;
637
638 /* Determine how well matching this entry is */
639 if (streq(c->x11_layout, a[1]))
640 /* If we got an exact match, this is best */
641 matching = 10;
642 else {
643 /* We have multiple X layouts, look for an
644 * entry that matches our key with everything
645 * but the first layout stripped off. */
646 if (startswith_comma(c->x11_layout, a[1]))
647 matching = 5;
648 else {
649 _cleanup_free_ char *x = NULL;
650
651 /* If that didn't work, strip off the
652 * other layouts from the entry, too */
653 x = strndup(a[1], strcspn(a[1], ","));
654 if (startswith_comma(c->x11_layout, x))
655 matching = 1;
656 }
657 }
658
659 if (matching > 0) {
660 if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) {
661 matching++;
662
663 if (streq_ptr(c->x11_variant, a[3])) {
664 matching++;
665
666 if (streq_ptr(c->x11_options, a[4]))
667 matching++;
668 }
669 }
670 }
671
672 /* The best matching entry so far, then let's save that */
673 if (matching >= MAX(best_matching, 1u)) {
674 log_debug("Found legacy keymap %s with score %u",
675 a[0], matching);
676
677 if (matching > best_matching) {
678 best_matching = matching;
679
680 r = free_and_strdup(&new_keymap, a[0]);
681 if (r < 0)
682 return r;
683 }
684 }
685 }
686
687 if (best_matching < 10 && c->x11_layout) {
688 /* The best match is only the first part of the X11
689 * keymap. Check if we have a converted map which
690 * matches just the first layout.
691 */
692 char *l, *v = NULL, *converted;
693
694 l = strndupa(c->x11_layout, strcspn(c->x11_layout, ","));
695 if (c->x11_variant)
696 v = strndupa(c->x11_variant, strcspn(c->x11_variant, ","));
697 r = find_converted_keymap(l, v, &converted);
698 if (r < 0)
699 return r;
700 if (r > 0)
701 free_and_replace(new_keymap, converted);
702 }
703
704 *ret = TAKE_PTR(new_keymap);
705 return (bool) *ret;
706 }
707
708 int find_language_fallback(const char *lang, char **language) {
709 const char *map;
710 _cleanup_fclose_ FILE *f = NULL;
711 unsigned n = 0;
712
713 assert(lang);
714 assert(language);
715
716 map = systemd_language_fallback_map();
717
718 f = fopen(map, "re");
719 if (!f)
720 return -errno;
721
722 for (;;) {
723 _cleanup_strv_free_ char **a = NULL;
724 int r;
725
726 r = read_next_mapping(map, 2, 2, f, &n, &a);
727 if (r <= 0)
728 return r;
729
730 if (streq(lang, a[0])) {
731 assert(strv_length(a) == 2);
732 *language = TAKE_PTR(a[1]);
733 return 1;
734 }
735 }
736
737 assert_not_reached("should not be here");
738 }
739
740 int x11_convert_to_vconsole(Context *c) {
741 bool modified = false;
742
743 if (isempty(c->x11_layout)) {
744 modified =
745 !isempty(c->vc_keymap) ||
746 !isempty(c->vc_keymap_toggle);
747
748 context_free_vconsole(c);
749 } else {
750 _cleanup_free_ char *new_keymap = NULL;
751 int r;
752
753 r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap);
754 if (r < 0)
755 return r;
756 else if (r == 0) {
757 r = find_legacy_keymap(c, &new_keymap);
758 if (r < 0)
759 return r;
760 }
761 if (r == 0)
762 /* We search for layout-variant match first, but then we also look
763 * for anything which matches just the layout. So it's accurate to say
764 * that we couldn't find anything which matches the layout. */
765 log_notice("No conversion to virtual console map found for \"%s\".",
766 c->x11_layout);
767
768 if (!streq_ptr(c->vc_keymap, new_keymap)) {
769 free_and_replace(c->vc_keymap, new_keymap);
770 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
771 modified = true;
772 }
773 }
774
775 if (modified)
776 log_info("Changing virtual console keymap to '%s' toggle '%s'",
777 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
778 else
779 log_debug("Virtual console keymap was not modified.");
780
781 return modified;
782 }