-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprivate-profile.sh
executable file
·167 lines (138 loc) · 2.88 KB
/
private-profile.sh
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
private=0
privlib=0
use_systemd=0
use_firejail=0
name=""
copy=0
netns=""
rmprof=0
to_copy=()
evvars=()
exitm()
{
echo "$1"
rmprof
exit 1
}
rmprof()
{
if [[ "$rmprof" -eq 1 && -n "${profile+x}" ]]
then
rm -r "${profile}"
fi
}
set -eu
while getopts "p:tcn:" arg
do
case ${arg} in
p)
profile="${OPTARG}"
name=$(basename "$profile")
;;
t)
private=1
;;
c)
copy=1
;;
n)
netns="${OPTARG}"
;;
*)
exit 1
;;
esac
done
shift $((OPTIND-1))
varfile="$1"
. "$varfile"
shift
if [[ -z "${progname:+x}" || -z "${profiledir:+x}" ]]
then
exitm '$progname and $profiledir must be specified and cannot be empty strings!'
fi
vpncmd()
{
systemctl -q is-active openvpn@us3-TCP-chaanakya && netns="" || netns="$netns"
}
firejail="firejail"
fjargs=( "--nowhitelist=${profiledir}" )
# private-lib generation if enabled
if [ "$privlib" -eq 1 ]
then
if [[ -z "${genlib+x}" || -z "${libdir+x}" ]]
then
exitm '$genlib and $libdir must all be set for $privlib!'
fi
. "$genlib"
libs=$(compile_list "${libdir}" "${extralibs:-}")
fjargs+=( "--private-lib=$libs" )
fi
# Deal with creating a private profile if requested
if [ "$private" -eq 1 ]
then
nprofile=$(mktemp -d -p "${profiledir}")
name=$(basename "$nprofile")
if [ "${destdir:=}" != "" ]
then
mkdir "${nprofile}"/"${destdir}"
fi
rmprof=1
if [ "$copy" -eq 1 ]
then
if [[ -z "${profile+x}" ]]
then
exitm 'A profile must be specified on the command-line if copying is enabled!'
fi
for i in "${tocopy[@]}"
do
cp -R "${profile}"/"${i}" "${nprofile}"/"${destdir}"/"${i}"
done
fi
profile="$nprofile"
fi
if [[ -z "${profile+x}" ]]
then
exitm 'Either $profile must be specified on the command-line or a temporary profile must be requested!'
fi
sprogname=$(basename "${progname}")
fjargs+=( "--whitelist=${profile}" "--name=${sprogname}-${name}" )
vpncmd
if [ "$netns" != "" ]
then
fjargs+=( "--net=${netns}" )
fi
for i in "${envvars[@]}"
do
fjargs+=( "--env=${i}" )
done
progargs="${progargs:-}"
rprogargs="${rprogargs:-}"
cmd="${progname} $(eval echo "${progargs[@]}")"
rcmd="${progname} $(eval echo "${rprogargs[@]}")"
fjcmd="${firejail} ${fjargs[*]} --"
systemdcmd="systemd-run --wait --user --unit=${sprogname}-${name}.service --description=${sprogname}-${name}"
if [ "$use_firejail" -eq 1 ]
then
cmd="${fjcmd} ${cmd}"
rcmd="${fjcmd} ${rcmd}"
else
cmd="/usr/bin/env ${envvars[*]} ${cmd}"
rcmd="/usr/bin/env ${envvars[*]} ${rcmd}"
fi
if [ "$use_systemd" -eq 1 ]
then
running=$(systemctl --user --quiet is-active "${sprogname}-${name}".service; echo $?)
cmd="${systemdcmd} ${cmd}"
else
running=$(pgrep -f "${progname} $(eval echo "${progargs[@]}")" > /dev/null; echo $?)
fi
if [ "$running" -eq 0 ]
then
$rcmd
else
$cmd
fi
# Remove profile if asked
rmprof