1
0
mirror of https://github.com/systemd/systemd synced 2025-10-03 18:54:45 +02:00

Compare commits

..

7 Commits

Author SHA1 Message Date
Luca Boccassi
5d5b6442a2
Merge pull request #18875 from keszybz/localed-error
localed: return error when setting a non-installed keymap
2021-03-05 22:50:41 +00:00
ChopperRob
39815435d0
Update 60-sensor.hwdb (#18884)
added support for the Lenovo IdeaPad D330-10IGM screen orientation
2021-03-06 06:11:26 +09:00
Zbigniew Jędrzejewski-Szmek
73d0806abb shared/kbd-util: simplify suffix stripping
It only came to me now that this can be prettified.
2021-03-04 11:52:25 +01:00
Zbigniew Jędrzejewski-Szmek
9ef70c06c6 localed: refuse to set a keymap which is not installed
In https://bugzilla.redhat.com/show_bug.cgi?id=1933873 a keymap was set without
the package that provides it being installed (it2 is in kbd-legacy, which is
not installed by default). Setting a non-installed keymap is problematic,
because it results in nasty failures afterward (*). So let's to the same as
e.g. for locale data, and refuse a setting if the definition doesn't exists in
the filesystem.

The implementation using nftw() is not the most efficient, but I think it's OK
in this case. This is definitely not in any kind of hot path, and I prefer not
to duplicate the filename manipulation logic in a second function.

(*) If the keymap is not found, vconsole-setup.service will fail.
dracut-cmdline-ask.service has Requires=vconsole-setup.service, so it will also
fail, and this breaks boot. dracut-cmdline-ask.service having a hard dependency
is appropriate though: we sadly don't display what the keymap is, and with a wrong
keymap, any attempts to enter a password are likely to fail.
2021-03-04 11:44:20 +01:00
Zbigniew Jędrzejewski-Szmek
1d230090c3 shared/kbd-util: return error on resource errors
I guess we should still not fail on failure to access a directory and such.
2021-03-04 11:44:13 +01:00
Zbigniew Jędrzejewski-Szmek
3864b4b038 shared/kbd-util: fix return value confusion with nftw()
We would return a real error sometimes from the callback, and FTW_STOP other
times. Because of FTW_ACTIONRETVAL, everything except FTW_STOP would be
ignored. I don't think using FTW_ACTIONRETVAL is useful.

nftw() can only be used meaningfully with errno. Even if we return a proper
value ourselves from the callback, it will be propagated as a return value from
nftw(), but there is no way to distinguish this from a value generated by
nftw() itself, which is -1/-EPERM on error. So let's set errno ourselves so the
caller can at least look at that.

The code still ignores all errors.
2021-03-04 11:44:07 +01:00
Zbigniew Jędrzejewski-Szmek
315edc2c11 Move basic/kbd-util to shared/
It is (or should be used) in localectl, localed, and a few other places,
no reason to keep it in basic/.
2021-03-04 09:39:29 +01:00
8 changed files with 154 additions and 118 deletions

2
TODO
View File

@ -7,6 +7,8 @@ Bugfixes:
* userdbctl: "Password OK: yes" is shown even when there are no passwords
or the password is locked.
* Get rid of nftw(). We should refuse to use such useless APIs on principle.
External:
* Fedora: add an rpmlint check that verifies that all unit files in the RPM are listed in %systemd_post macros.

View File

@ -497,8 +497,9 @@ sensor:modalias:acpi:BMA250E*:dmi:bvnLENOVO:*:pvrLenovoMIIX3-1030:*
sensor:modalias:acpi:SMO8500*:dmi:bvnLENOVO:*:pvrLenovoMIIX3-830:*
ACCEL_MOUNT_MATRIX=-1, 0, 0; 0, 1, 0; 0, 0, 1
# IdeaPad D330
# IdeaPad D330 and D330-10IGM
sensor:modalias:acpi:BOSC0200*:dmi:*:svnLENOVO:pn81H3:*
sensor:modalias:acpi:BOSC0200*:dmi:*:svnLENOVO:*:cvrLenovoideapadD330-10IGM:*
ACCEL_MOUNT_MATRIX=0, 1, 0; -1, 0, 0; 0, 0, 1
# IdeaPad Miix 300

View File

@ -1,109 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <ftw.h>
#include "kbd-util.h"
#include "log.h"
#include "nulstr-util.h"
#include "path-util.h"
#include "set.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
static thread_local Set *keymaps = NULL;
static int nftw_cb(
const char *fpath,
const struct stat *sb,
int tflag,
struct FTW *ftwbuf) {
_cleanup_free_ char *p = NULL;
char *e;
int r;
if (tflag != FTW_F)
return 0;
if (!endswith(fpath, ".map") &&
!endswith(fpath, ".map.gz"))
return 0;
p = strdup(basename(fpath));
if (!p)
return FTW_STOP;
e = endswith(p, ".map");
if (e)
*e = 0;
e = endswith(p, ".map.gz");
if (e)
*e = 0;
if (!keymap_is_valid(p))
return 0;
r = set_consume(keymaps, TAKE_PTR(p));
if (r < 0 && r != -EEXIST)
return r;
return 0;
}
int get_keymaps(char ***ret) {
_cleanup_strv_free_ char **l = NULL;
const char *dir;
int r;
keymaps = set_new(&string_hash_ops);
if (!keymaps)
return -ENOMEM;
NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
r = nftw(dir, nftw_cb, 20, FTW_PHYS|FTW_ACTIONRETVAL);
if (r == FTW_STOP)
log_debug("Directory not found %s", dir);
else if (r < 0)
log_debug_errno(r, "Can't add keymap: %m");
}
l = set_get_strv(keymaps);
if (!l) {
set_free_free(keymaps);
return -ENOMEM;
}
set_free(keymaps);
if (strv_isempty(l))
return -ENOENT;
strv_sort(l);
*ret = TAKE_PTR(l);
return 0;
}
bool keymap_is_valid(const char *name) {
if (isempty(name))
return false;
if (strlen(name) >= 128)
return false;
if (!utf8_is_valid(name))
return false;
if (!filename_is_valid(name))
return false;
if (!string_is_safe(name))
return false;
return true;
}

View File

@ -83,8 +83,6 @@ basic_sources = files('''
io-util.c
io-util.h
ioprio.h
kbd-util.c
kbd-util.h
khash.c
khash.h
label.c

View File

@ -19,6 +19,7 @@
#include "bus-polkit.h"
#include "def.h"
#include "dlfcn-util.h"
#include "kbd-util.h"
#include "keymap-util.h"
#include "locale-util.h"
#include "macro.h"
@ -474,7 +475,7 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_error *error) {
Context *c = userdata;
const char *keymap, *keymap_toggle;
const char *name, *keymap, *keymap_toggle;
int convert, interactive, r;
assert(m);
@ -490,17 +491,23 @@ static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_erro
r = vconsole_read_data(c, m);
if (r < 0) {
log_error_errno(r, "Failed to read virtual console keymap data: %m");
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Failed to read virtual console keymap data");
return sd_bus_error_set_errnof(error, r, "Failed to read virtual console keymap data: %m");
}
FOREACH_STRING(name, keymap ?: keymap_toggle, keymap ? keymap_toggle : NULL) {
r = keymap_exists(name); /* This also verifies that the keymap name is kosher. */
if (r < 0) {
log_error_errno(r, "Failed to check keymap %s: %m", name);
return sd_bus_error_set_errnof(error, r, "Failed to check keymap %s: %m", name);
}
if (r == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED, "Keymap %s is not installed.", name);
}
if (streq_ptr(keymap, c->vc_keymap) &&
streq_ptr(keymap_toggle, c->vc_keymap_toggle))
return sd_bus_reply_method_return(m, NULL);
if ((keymap && (!filename_is_valid(keymap) || !string_is_safe(keymap))) ||
(keymap_toggle && (!filename_is_valid(keymap_toggle) || !string_is_safe(keymap_toggle))))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Received invalid keymap data");
r = bus_verify_polkit_async(
m,
CAP_SYS_ADMIN,

134
src/shared/kbd-util.c Normal file
View File

@ -0,0 +1,134 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <ftw.h>
#include "errno-util.h"
#include "kbd-util.h"
#include "log.h"
#include "nulstr-util.h"
#include "path-util.h"
#include "set.h"
#include "string-util.h"
#include "strv.h"
#include "utf8.h"
static thread_local const char *keymap_name = NULL;
static thread_local Set *keymaps = NULL;
static int nftw_cb(
const char *fpath,
const struct stat *sb,
int tflag,
struct FTW *ftwbuf) {
_cleanup_free_ char *p = NULL;
int r;
/* If keymap_name is non-null, return true if keymap keymap_name is found.
* Otherwise, add all keymaps to keymaps. */
if (tflag != FTW_F)
return 0;
fpath = basename(fpath);
const char *e = endswith(fpath, ".map") ?: endswith(fpath, ".map.gz");
if (!e)
return 0;
p = strndup(fpath, e - fpath);
if (!p) {
errno = ENOMEM;
return -1;
}
if (keymap_name)
return streq(p, keymap_name);
if (!keymap_is_valid(p))
return 0;
r = set_consume(keymaps, TAKE_PTR(p));
if (r < 0 && r != -EEXIST) {
errno = -r;
return -1;
}
return 0;
}
int get_keymaps(char ***ret) {
keymaps = set_new(&string_hash_ops);
if (!keymaps)
return -ENOMEM;
const char *dir;
NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS)
if (nftw(dir, nftw_cb, 20, FTW_PHYS) < 0) {
if (errno == ENOENT)
continue;
if (ERRNO_IS_RESOURCE(errno)) {
keymaps = set_free_free(keymaps);
return log_warning_errno(errno, "Failed to read keymap list from %s: %m", dir);
}
log_debug_errno(errno, "Failed to read keymap list from %s, ignoring: %m", dir);
}
_cleanup_strv_free_ char **l = set_get_strv(keymaps);
if (!l) {
keymaps = set_free_free(keymaps);
return -ENOMEM;
}
keymaps = set_free(keymaps);
if (strv_isempty(l))
return -ENOENT;
strv_sort(l);
*ret = TAKE_PTR(l);
return 0;
}
bool keymap_is_valid(const char *name) {
if (isempty(name))
return false;
if (strlen(name) >= 128)
return false;
if (!utf8_is_valid(name))
return false;
if (!filename_is_valid(name))
return false;
if (!string_is_safe(name))
return false;
return true;
}
int keymap_exists(const char *name) {
int r = 0;
if (!keymap_is_valid(name))
return -EINVAL;
keymap_name = name;
const char *dir;
NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
r = nftw(dir, nftw_cb, 20, FTW_PHYS);
if (r > 0)
break;
if (r < 0 && errno != ENOENT)
log_debug_errno(errno, "Failed to read keymap list from %s, ignoring: %m", dir);
}
keymap_name = NULL;
return r > 0;
}

View File

@ -18,3 +18,4 @@
int get_keymaps(char ***l);
bool keymap_is_valid(const char *name);
int keymap_exists(const char *name);

View File

@ -149,6 +149,8 @@ shared_sources = files('''
json-internal.h
json.c
json.h
kbd-util.c
kbd-util.h
killall.c
killall.h
libcrypt-util.c