virt-login-shell-helper.c 11.1 KB
Newer Older
D
Dan Walsh 已提交
1
/*
2
 * virt-login-shell-helper.c: a shell to connect to a container
D
Dan Walsh 已提交
3
 *
4
 * Copyright (C) 2013-2014 Red Hat, Inc.
D
Dan Walsh 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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 <fnmatch.h>
E
Eric Blake 已提交
23 24 25
#include <getopt.h>
#include <signal.h>
#include <stdarg.h>
M
Michal Privoznik 已提交
26
#include <unistd.h>
D
Dan Walsh 已提交
27 28 29 30 31 32 33 34 35 36 37

#include "internal.h"
#include "virerror.h"
#include "virconf.h"
#include "virutil.h"
#include "virfile.h"
#include "virprocess.h"
#include "configmake.h"
#include "virstring.h"
#include "viralloc.h"
#include "vircommand.h"
38
#include "virgettext.h"
D
Dan Walsh 已提交
39 40 41 42 43 44
#define VIR_FROM_THIS VIR_FROM_NONE

static const char *conf_file = SYSCONFDIR "/libvirt/virt-login-shell.conf";

static int virLoginShellAllowedUser(virConfPtr conf,
                                    const char *name,
45 46
                                    gid_t *groups,
                                    size_t ngroups)
D
Dan Walsh 已提交
47 48 49 50
{
    int ret = -1;
    size_t i;
    char *gname = NULL;
51
    char **users = NULL, **entries;
D
Dan Walsh 已提交
52

53 54
    if (virConfGetValueStringList(conf, "allowed_users", false, &users) < 0)
        goto cleanup;
D
Dan Walsh 已提交
55

56

57
    for (entries = users; entries && *entries; entries++) {
58 59 60 61 62 63 64 65 66 67 68
        char *entry = *entries;
        /*
          If string begins with a % this indicates a linux group.
          Check to see if the user is in the Linux Group.
        */
        if (entry[0] == '%') {
            entry++;
            if (!*entry)
                continue;
            for (i = 0; i < ngroups; i++) {
                if (!(gname = virGetGroupName(groups[i])))
D
Dan Walsh 已提交
69
                    continue;
70
                if (fnmatch(entry, gname, 0) == 0) {
D
Dan Walsh 已提交
71 72 73
                    ret = 0;
                    goto cleanup;
                }
74 75 76 77 78 79
                VIR_FREE(gname);
            }
        } else {
            if (fnmatch(entry, name, 0) == 0) {
                ret = 0;
                goto cleanup;
D
Dan Walsh 已提交
80 81 82
            }
        }
    }
83 84 85
    virReportSystemError(EPERM,
                         _("%s not matched against 'allowed_users' in %s"),
                         name, conf_file);
86
 cleanup:
D
Dan Walsh 已提交
87
    VIR_FREE(gname);
88
    virStringListFree(users);
D
Dan Walsh 已提交
89 90 91
    return ret;
}

92

93
static int virLoginShellGetShellArgv(virConfPtr conf,
94 95
                                     char ***shargv,
                                     size_t *shargvlen)
D
Dan Walsh 已提交
96
{
97 98 99
    int rv;

    if ((rv = virConfGetValueStringList(conf, "shell", true, shargv)) < 0)
100
        return -1;
101

102
    if (rv == 0) {
103 104 105 106 107
        if (VIR_ALLOC_N(*shargv, 2) < 0)
            return -1;
        if (VIR_STRDUP((*shargv)[0], "/bin/sh") < 0) {
            VIR_FREE(*shargv);
            return -1;
D
Dan Walsh 已提交
108
        }
109 110 111
        *shargvlen = 1;
    } else {
        *shargvlen = virStringListLength((const char *const *)shargv);
D
Dan Walsh 已提交
112
    }
113
    return 0;
D
Dan Walsh 已提交
114 115 116 117 118 119 120 121 122 123
}

static char *progname;

/*
 * Print usage
 */
static void
usage(void)
{
124 125 126
    fprintf(stdout,
            _("\n"
              "Usage:\n"
E
Eric Blake 已提交
127
              "  %s [option]\n\n"
128
              "Options:\n"
E
Eric Blake 已提交
129 130
              "  -h | --help            Display program help\n"
              "  -V | --version         Display program version\n"
131
              "  -c CMD                 Run CMD via shell\n"
132 133 134
              "\n"
              "libvirt login shell\n"),
            progname);
D
Dan Walsh 已提交
135 136 137
    return;
}

138 139 140 141 142 143 144 145
/* Display version information. */
static void
show_version(void)
{
    printf("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
}


146 147 148 149 150 151
static void
hideErrorFunc(void *opaque ATTRIBUTE_UNUSED,
              virErrorPtr err ATTRIBUTE_UNUSED)
{
}

D
Dan Walsh 已提交
152 153 154 155 156
int
main(int argc, char **argv)
{
    virConfPtr conf = NULL;
    const char *login_shell_path = conf_file;
E
Eric Blake 已提交
157 158
    pid_t cpid = -1;
    int ret = EXIT_CANCELED;
D
Dan Walsh 已提交
159 160 161 162 163
    int status;
    uid_t uid = getuid();
    gid_t gid = getgid();
    char *name = NULL;
    char **shargv = NULL;
164
    size_t shargvlen = 0;
165
    char *shcmd = NULL;
D
Dan Walsh 已提交
166 167 168 169 170 171 172 173 174
    virSecurityModelPtr secmodel = NULL;
    virSecurityLabelPtr seclabel = NULL;
    virDomainPtr dom = NULL;
    virConnectPtr conn = NULL;
    char *homedir = NULL;
    int arg;
    int longindex = -1;
    int ngroups;
    gid_t *groups = NULL;
175 176 177 178
    ssize_t nfdlist = 0;
    int *fdlist = NULL;
    int openmax;
    size_t i;
179
    const char *cmdstr = NULL;
180
    char *tmp;
181
    char *term = NULL;
182
    virErrorPtr saved_err = NULL;
183
    bool autoshell = false;
D
Dan Walsh 已提交
184 185 186

    struct option opt[] = {
        {"help", no_argument, NULL, 'h'},
187
        {"version", optional_argument, NULL, 'V'},
D
Dan Walsh 已提交
188 189 190
        {NULL, 0, NULL, 0}
    };
    if (virInitialize() < 0) {
E
Eric Blake 已提交
191 192
        fprintf(stderr, _("Failed to initialize libvirt error handling"));
        return EXIT_CANCELED;
D
Dan Walsh 已提交
193 194
    }

195
    virSetErrorFunc(NULL, hideErrorFunc);
D
Dan Walsh 已提交
196 197 198
    virSetErrorLogPriorityFunc(NULL);

    progname = argv[0];
199
    if (virGettextInitialize() < 0)
D
Dan Walsh 已提交
200 201
        return ret;

202
    while ((arg = getopt_long(argc, argv, "hVc:", opt, &longindex)) != -1) {
D
Dan Walsh 已提交
203 204 205 206
        switch (arg) {
        case 'h':
            usage();
            exit(EXIT_SUCCESS);
207 208 209 210 211

        case 'V':
            show_version();
            exit(EXIT_SUCCESS);

212 213 214 215
        case 'c':
            cmdstr = optarg;
            break;

216 217 218
        case '?':
        default:
            usage();
E
Eric Blake 已提交
219
            exit(EXIT_CANCELED);
D
Dan Walsh 已提交
220 221 222 223 224 225 226 227 228
        }
    }

    if (argc > optind) {
        virReportSystemError(EINVAL, _("%s takes no options"), progname);
        goto cleanup;
    }

    if (uid == 0) {
229 230
        virReportSystemError(EPERM, _("%s must be run by non root users"),
                             progname);
D
Dan Walsh 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        goto cleanup;
    }

    name = virGetUserName(uid);
    if (!name)
        goto cleanup;

    homedir = virGetUserDirectoryByUID(uid);
    if (!homedir)
        goto cleanup;

    if (!(conf = virConfReadFile(login_shell_path, 0)))
        goto cleanup;

    if ((ngroups = virGetGroupList(uid, gid, &groups)) < 0)
        goto cleanup;

248
    if (virLoginShellAllowedUser(conf, name, groups, ngroups) < 0)
D
Dan Walsh 已提交
249 250
        goto cleanup;

251
    if (virLoginShellGetShellArgv(conf, &shargv, &shargvlen) < 0)
D
Dan Walsh 已提交
252 253
        goto cleanup;

254
    if (virConfGetValueBool(conf, "auto_shell", &autoshell) < 0)
255 256
        goto cleanup;

257
    conn = virConnectOpen("lxc:///system");
D
Dan Walsh 已提交
258 259 260 261 262 263 264
    if (!conn)
        goto cleanup;

    dom = virDomainLookupByName(conn, name);
    if (!dom)
        goto cleanup;

J
John Ferlan 已提交
265
    if (!virDomainIsActive(dom) && virDomainCreate(dom) < 0) {
D
Dan Walsh 已提交
266 267 268
        virErrorPtr last_error;
        last_error = virGetLastError();
        if (last_error->code != VIR_ERR_OPERATION_INVALID) {
269 270 271
            virReportSystemError(last_error->code,
                                 _("Can't create %s container: %s"),
                                 name, last_error->message);
D
Dan Walsh 已提交
272 273 274 275
            goto cleanup;
        }
    }

276 277 278 279 280 281 282
    openmax = sysconf(_SC_OPEN_MAX);
    if (openmax < 0) {
        virReportSystemError(errno,  "%s",
                             _("sysconf(_SC_OPEN_MAX) failed"));
        goto cleanup;
    }

D
Dan Walsh 已提交
283 284 285 286 287 288 289 290 291 292
    if ((nfdlist = virDomainLxcOpenNamespace(dom, &fdlist, 0)) < 0)
        goto cleanup;
    if (VIR_ALLOC(secmodel) < 0)
        goto cleanup;
    if (VIR_ALLOC(seclabel) < 0)
        goto cleanup;
    if (virNodeGetSecurityModel(conn, secmodel) < 0)
        goto cleanup;
    if (virDomainGetSecurityLabel(dom, seclabel) < 0)
        goto cleanup;
293 294 295 296
    if (virSetUIDGID(0, 0, NULL, 0) < 0)
        goto cleanup;
    if (virDomainLxcEnterSecurityLabel(secmodel, seclabel, NULL, 0) < 0)
        goto cleanup;
297 298
    if (virDomainLxcEnterCGroup(dom, 0) < 0)
        goto cleanup;
299 300 301 302 303 304 305 306 307
    if (nfdlist > 0 &&
        virDomainLxcEnterNamespace(dom, nfdlist, fdlist, NULL, NULL, 0) < 0)
        goto cleanup;
    if (virSetUIDGID(uid, gid, groups, ngroups) < 0)
        goto cleanup;
    if (chdir(homedir) < 0) {
        virReportSystemError(errno, _("Unable to chdir(%s)"), homedir);
        goto cleanup;
    }
D
Dan Walsh 已提交
308

309 310 311
    if (autoshell) {
        tmp = virGetUserShell(uid);
        if (tmp) {
312
            virStringListFree(shargv);
313 314 315 316 317 318 319 320 321 322
            shargvlen = 1;
            if (VIR_ALLOC_N(shargv[0], shargvlen + 1) < 0) {
                VIR_FREE(tmp);
                goto cleanup;
            }
            shargv[0] = tmp;
            shargv[1] = NULL;
        }
    }

323 324 325 326 327 328 329 330 331 332
    if (cmdstr) {
        if (VIR_REALLOC_N(shargv, shargvlen + 3) < 0)
            goto cleanup;
        if (VIR_STRDUP(shargv[shargvlen++], "-c") < 0)
            goto cleanup;
        if (VIR_STRDUP(shargv[shargvlen++], cmdstr) < 0)
            goto cleanup;
        shargv[shargvlen] = NULL;
    }

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    /* We need to modify the first elementin shargv
     * so that it has the relative filename and has
     * a leading '-' to indicate it is a login shell
     */
    shcmd = shargv[0];
    if (shcmd[0] != '/') {
        virReportSystemError(errno,
                             _("Shell '%s' should have absolute path"),
                             shcmd);
        goto cleanup;
    }
    tmp = strrchr(shcmd, '/');
    if (VIR_STRDUP(shargv[0], tmp) < 0)
        goto cleanup;
    shargv[0][0] = '-';

349 350 351 352 353 354
    /* We're duping the string because the clearenv()
     * call will shortly release the pointer we get
     * back from virGetEnvAllowSUID() right here */
    if (VIR_STRDUP(term, virGetEnvAllowSUID("TERM")) < 0)
        goto cleanup;

355
    /* A fork is required to create new process in correct pid namespace.  */
E
Eric Blake 已提交
356
    if ((cpid = virFork()) < 0)
D
Dan Walsh 已提交
357 358 359
        goto cleanup;

    if (cpid == 0) {
360
        int tmpfd;
361

362 363
        for (i = 3; i < openmax; i++) {
            tmpfd = i;
364 365
            VIR_MASS_CLOSE(tmpfd);
        }
366 367 368 369 370 371 372 373 374 375

        clearenv();
        setenv("PATH", "/bin:/usr/bin", 1);
        setenv("SHELL", shcmd, 1);
        setenv("USER", name, 1);
        setenv("LOGNAME", name, 1);
        setenv("HOME", homedir, 1);
        if (term)
            setenv("TERM", term, 1);

376
        if (execv(shcmd, (char *const*) shargv) < 0) {
377
            virReportSystemError(errno, _("Unable to exec shell %s"),
378
                                 shcmd);
E
Eric Blake 已提交
379 380
            virDispatchError(NULL);
            return errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE;
D
Dan Walsh 已提交
381 382
        }
    }
383

E
Eric Blake 已提交
384 385
    /* At this point, the parent is now waiting for the child to exit,
     * but as that may take a long time, we release resources now.  */
386
 cleanup:
387 388
    saved_err = virSaveLastError();

389 390 391
    if (nfdlist > 0)
        for (i = 0; i < nfdlist; i++)
            VIR_FORCE_CLOSE(fdlist[i]);
392
    VIR_FREE(fdlist);
D
Dan Walsh 已提交
393
    virConfFree(conf);
394 395 396 397
    if (dom)
        virDomainFree(dom);
    if (conn)
        virConnectClose(conn);
398
    virStringListFree(shargv);
399
    VIR_FREE(shcmd);
400
    VIR_FREE(term);
D
Dan Walsh 已提交
401 402 403 404 405
    VIR_FREE(name);
    VIR_FREE(homedir);
    VIR_FREE(seclabel);
    VIR_FREE(secmodel);
    VIR_FREE(groups);
E
Eric Blake 已提交
406 407 408 409

    if (virProcessWait(cpid, &status, true) == 0)
        virProcessExitWithStatus(status);

410 411
    if (saved_err) {
        virSetError(saved_err);
412
        fprintf(stderr, "%s: %s\n", argv[0], virGetLastErrorMessage());
413
    }
D
Dan Walsh 已提交
414 415
    return ret;
}