File: userbindmount.c

package info (click to toggle)
userbindmount 0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: ansic: 242; makefile: 19
file content (142 lines) | stat: -rw-r--r-- 4,060 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* 
 * userbindmount: bindmount as a user (using user-namespaces).
 * Copyright (C) 2016  Renzo Davoli, University of Bologna
 * 
 * userbindmount is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; If not, see <http://www.gnu.org/licenses/>. 
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <libgen.h>
#include <sys/mount.h>

#include "userbindmount.h"

char *progname;
int sysadmin_flag = 0;
int verbose_flag = 0;
int newnamespace_flag = 0;

#define errExit(msg)    do {\
	perror(msg); \
	exit(EXIT_FAILURE); \
} while(0);

void usage_n_exit(void) {
	fprintf(stderr, 
			"Usage: %s OPTIONS [source target [source target [...]]] [-- [cmd [args]]] \n"
			"OPTIONS:\n"
			"\t-n | --newns   : create a new namespace\n"
			"\t-s | --sysadm  : set cap_sys_admin ambient capability\n"
			"\t-v | --verbose : verbose mode\n" 
			"\t-h | --help    : print this short help message\n" 
			"a new user_namespace is created if -- arg is present\n"
			"it runs $SHELL if cmd omitted\n"
			"the contents of the mounted file is read from stdin if source is '-'\n\n",
			progname);
	exit(EXIT_SUCCESS);
}

void userbindmount_mount(const char *source, const char *target) {
	int retval;
	if (*source == 0 || strcmp(source, "-") == 0)
		retval = userbindmount_fd(0, target, 0600);
	else {
		int len = strlen(source);
		if (source[0] == '"' && len > 1 && source[len - 1] == '"') {
			char string[len];
			snprintf(string, len, "%*.*s", len - 2, len - 2, source + 1);
			retval = userbindmount_string(string, target, 0600);
		} else
			retval = mount(source, target, "", MS_BIND, NULL);
	}
	if (retval < 0)
		errExit("mount");
}

static int there_is_dash_dash(char *argv[]) {
	for (; *argv != NULL; argv++) {
		if (strcmp(argv[0], "--") == 0)
			return 1;
	}
	return 0;
}

int main(int argc, char *argv[]) {
	static char *short_options = "+shvn";
	static struct option long_options[] = {
		{"help", no_argument, 0, 'h'},
		{"newns", no_argument, 0, 'n'},
		{"sysadm", no_argument, 0, 's'},
		{"verbose", no_argument, 0, 'v'},
		{0, 0, 0, 0}};
	progname = basename(argv[0]);
	while (1) {
		int c;
		if ((c = getopt_long(argc, argv, short_options, long_options, NULL)) == -1)
			break;
		switch (c) {
			case 's': sysadmin_flag = 1; break;
			case 'v': verbose_flag = 1; break;
			case 'n': newnamespace_flag = 1; break;
			case '?':
			case 'h': 
			default: usage_n_exit();
		}
	}
	if (strcmp(argv[optind - 1], "--") == 0)
		optind--;
	argc -= optind;
	argv += optind;
	if (there_is_dash_dash(argv))
		newnamespace_flag = 1;
	if (newnamespace_flag) {
		if (verbose_flag)
			fprintf(stderr, "creating a user_namespace\n");
		if (userbindmount_unshare() < 0)
			errExit("unshare");
	}
	if (sysadmin_flag) {
		if (verbose_flag)  
			fprintf(stderr, "setting sysadm ambient capability\n");   
		if (userbindmount_set_cap_sysadm() < 0)
			errExit("set_cap_sysadm");
	}
	while (argc >= 2 && strcmp(argv[0],"--") != 0) {
		if (verbose_flag) 
			fprintf(stderr, "mounting %s on %s\n", argv[0], argv[1]);
		userbindmount_mount(argv[0], argv[1]);
		argc -= 2;
		argv += 2;
	}
	if (newnamespace_flag || argc > 0) {
		char **cmdargv;
		char *argvsh[]={getenv("SHELL"),NULL};
		if (argc > 0 && strcmp(argv[0],"--") == 0) {
			argc--;
			argv++;
		}
		cmdargv = (argc == 0) ? argvsh : argv;
		if (cmdargv[0] == NULL)
			errExit("$SHELL env variable not set\n");
		if (verbose_flag) 
			fprintf(stderr, "starting %s\n", cmdargv[0]);   
		execvp(cmdargv[0], cmdargv);
	}
}