mirror of https://github.com/xemu-project/xemu.git
os-posix.c: create and export os_set_runas()
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru> Reviewed-by: Eric Blake <eblake@redhat.com> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Message-ID: <20230901101302.3618955-3-mjt@tls.msk.ru> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
b21bdbb51a
commit
22d0251570
|
@ -50,6 +50,7 @@ void os_setup_signal_handling(void);
|
||||||
int os_set_daemonize(bool d);
|
int os_set_daemonize(bool d);
|
||||||
bool is_daemonized(void);
|
bool is_daemonized(void);
|
||||||
void os_daemonize(void);
|
void os_daemonize(void);
|
||||||
|
bool os_set_runas(const char *optarg);
|
||||||
void os_setup_post(void);
|
void os_setup_post(void);
|
||||||
int os_mlock(void);
|
int os_mlock(void);
|
||||||
|
|
||||||
|
|
23
os-posix.c
23
os-posix.c
|
@ -102,8 +102,14 @@ void os_set_proc_name(const char *s)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
static bool os_parse_runas_uid_gid(const char *optarg)
|
* Prepare to change user ID. optarg can be one of 3 forms:
|
||||||
|
* - a username, in which case user ID will be changed to its uid,
|
||||||
|
* with primary and supplementary groups set up too;
|
||||||
|
* - a numeric uid, in which case only the uid will be set;
|
||||||
|
* - a pair of numeric uid:gid.
|
||||||
|
*/
|
||||||
|
bool os_set_runas(const char *optarg)
|
||||||
{
|
{
|
||||||
unsigned long lv;
|
unsigned long lv;
|
||||||
const char *ep;
|
const char *ep;
|
||||||
|
@ -111,6 +117,13 @@ static bool os_parse_runas_uid_gid(const char *optarg)
|
||||||
gid_t got_gid;
|
gid_t got_gid;
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
|
user_pwd = getpwnam(optarg);
|
||||||
|
if (user_pwd) {
|
||||||
|
user_uid = -1;
|
||||||
|
user_gid = -1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
rc = qemu_strtoul(optarg, &ep, 0, &lv);
|
rc = qemu_strtoul(optarg, &ep, 0, &lv);
|
||||||
got_uid = lv; /* overflow here is ID in C99 */
|
got_uid = lv; /* overflow here is ID in C99 */
|
||||||
if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
|
if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
|
||||||
|
@ -137,11 +150,7 @@ int os_parse_cmd_args(int index, const char *optarg)
|
||||||
{
|
{
|
||||||
switch (index) {
|
switch (index) {
|
||||||
case QEMU_OPTION_runas:
|
case QEMU_OPTION_runas:
|
||||||
user_pwd = getpwnam(optarg);
|
if (!os_set_runas(optarg)) {
|
||||||
if (user_pwd) {
|
|
||||||
user_uid = -1;
|
|
||||||
user_gid = -1;
|
|
||||||
} else if (!os_parse_runas_uid_gid(optarg)) {
|
|
||||||
error_report("User \"%s\" doesn't exist"
|
error_report("User \"%s\" doesn't exist"
|
||||||
" (and is not <uid>:<gid>)",
|
" (and is not <uid>:<gid>)",
|
||||||
optarg);
|
optarg);
|
||||||
|
|
Loading…
Reference in New Issue