File: virt-login-shell.c

package info (click to toggle)
libvirt 11.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,168 kB
  • sloc: ansic: 537,214; xml: 335,516; python: 12,041; perl: 2,626; sh: 2,175; makefile: 448; javascript: 126; cpp: 22
file content (95 lines) | stat: -rw-r--r-- 2,943 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * virt-login-shell.c: a setuid shell to connect to a container
 *
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <unistd.h>
#include <sys/types.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "configmake.h"

#define G_N_ELEMENTS(Array) (sizeof(Array) / sizeof(*(Array)))
#define VIR_INT64_STR_BUFLEN 21

int main(int argc, char **argv) {
    char uidstr[VIR_INT64_STR_BUFLEN];
    char gidstr[VIR_INT64_STR_BUFLEN];
    const char * newargv[6];
    size_t nargs = 0;
    char *newenv[] = {
        NULL,
        NULL,
    };
    char *term = getenv("TERM");

    if (getuid() == 0 || getgid() == 0) {
        fprintf(stderr, "%s: must not be run as root\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (geteuid() != 0) {
        fprintf(stderr, "%s: must be run as setuid root\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    newargv[nargs++] = LIBEXECDIR "/virt-login-shell-helper";
    if (argc == 3) {
        if (strcmp(argv[1], "-c") != 0) {
            fprintf(stderr, "%s: syntax: %s [-c CMDSTR]\n", argv[0], argv[0]);
            exit(EXIT_FAILURE);
        }
        newargv[nargs++] = argv[1];
        newargv[nargs++] = argv[2];
    } else if (argc != 1) {
        fprintf(stderr, "%s: syntax: %s [-c CMDSTR]\n", argv[0], argv[0]);
        exit(EXIT_FAILURE);
    }
    newargv[nargs++] = uidstr;
    newargv[nargs++] = gidstr;
    newargv[nargs++] = NULL;

    assert(nargs <= G_N_ELEMENTS(newargv));

    if (term &&
        asprintf(&(newenv[0]), "TERM=%s", term) < 0) {
        fprintf(stderr, "%s: cannot set TERM env variable: %s\n",
                argv[0], strerror(errno));
        exit(EXIT_FAILURE);
    }

    assert(snprintf(uidstr, sizeof(uidstr), "%d", getuid()) < sizeof(uidstr));
    assert(snprintf(gidstr, sizeof(gidstr), "%d", getgid()) < sizeof(gidstr));

    if (setuid(0) < 0) {
        fprintf(stderr, "%s: unable to set real UID to root: %s\n",
                argv[0], strerror(errno));
        exit(EXIT_FAILURE);
    }

    execve(newargv[0], (char *const*)newargv, newenv);
    fprintf(stderr, "%s: failed to run %s/virt-login-shell-helper: %s\n",
            argv[0], LIBEXECDIR, strerror(errno));
    exit(EXIT_FAILURE);
}