This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
pbime.c
113 lines (101 loc) · 3.68 KB
/
pbime.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*-
* Copyright (c) 2003 Mike Barcroft <mike@FreeBSD.org>
* Copyright (c) 2007 Bill Moran/Collaborative Fusion
* Copyright (c) 2013 Kris Moore/PC-BSD Software <kris@pcbsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/jail.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <login_cap.h>
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <unistd.h>
#include <string.h>
static void usage(void);
int
main(int argc, char *argv[])
{
char newchroot[2048];
char mountscript[4096];
int jid, ngroups;
uid_t huid;
struct passwd *husername, *jusername;
gid_t groups[NGROUPS];
login_cap_t *lcap;
if (argc < 4)
usage();
/* Get the current user ID and user name in the host system */
huid = getuid();
husername = getpwuid(huid);
/* Run the mount script */
if ( (strlen("/usr/pbi/.pbimount") + strlen(argv[1]) + strlen(argv[2])) > 4090)
err(1, "Max length exceeded for pbidir / mntdir");
if ( strlen("argv[2]") + strlen("/virtbase") > 2040)
err(1, "Max length exceeded for virtbase");
strcpy(newchroot, argv[1]);
strcat(newchroot, "/virtbase");
strcpy(mountscript, "/usr/pbi/.pbimount ");
strcat(mountscript, argv[1]);
strcat(mountscript, " ");
strcat(mountscript, argv[2]);
//printf( "mountscript: %s \n", mountscript);
if ( system(mountscript) != 0 )
err(1, "Failed mounting PBI");
if (chdir(newchroot) == -1 || chroot(".") == -1)
err(1, "Could not chroot to: %s", newchroot);
/* Get the user name in the jail */
jusername = getpwuid(huid);
if (jusername == NULL || strcmp(husername->pw_name, jusername->pw_name) != 0)
err(1, "Username mapping failed");
//if (chdir("/") == -1)
// err(1, "chdir(): /");
lcap = login_getpwclass(jusername);
if (lcap = NULL)
err(1, "getpwclass: %s", jusername);
ngroups = NGROUPS;
if (getgrouplist(jusername->pw_name, jusername->pw_gid, groups, &ngroups) != 0)
err(1, "getgrouplist: %s", jusername->pw_name);
if (setgroups(ngroups, groups) != 0)
err(1, "setgroups");
if (setgid(jusername->pw_gid) != 0)
err(1, "setgid");
if (setusercontext(lcap, jusername, jusername->pw_uid,
LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN) != 0)
err(1, "setusercontext");
login_close(lcap);
if (execvp(argv[3], argv + 3) == -1)
err(1, "execvp(): %s", argv[3]);
exit(0);
}
static void
usage(void)
{
fprintf(stderr, "usage: pbime mntdir pbidir command [...]\n");
exit(1);
}