-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrvwrappers.sh
416 lines (355 loc) · 12.2 KB
/
srvwrappers.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/bin/bash
#
# Copyright (C) 2023, 2024 R. Lupu @ UPB, UNSTPB
#
# This program 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 3 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 <https://www.gnu.org/licenses/>.
#
# Contact: rlupu at elcom.pub.ro
#
# Version: 0.7 (Debian)
#
# Usage: <$0> <namespace> {setup, cleanup} <service>
#
source ./vnetenv.sh || { echo -e "Env not settled.\nQuit."; exit 1; }
VPATH=${VPATH:-"/tmp/vnet"}
function rsyslog_setup () {
#shared:/var/log/ /var/run/ /run/ /dev/; race_on:/var/run/rsyslog.pid /var/log/syslog /dev/log [ifnet];
# req_files: /dev/urandom;
L_ALIGN=${L_ALIGN:="\t\t"}; unset -v DONE_ALIGN
echo -ne "\n${L_ALIGN}Setup rsyslog wrapper on $1 ......"
if ! test -d $VPATH/$1/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/$1/ is created."
mkdir $VPATH/$1
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/var/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../var/ is created."
mkdir $VPATH/$1/var
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/dev/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../dev/ is created."
mkdir $VPATH/$1/dev
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/run/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../run/ is created."
mkdir $VPATH/$1/run
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/var/log/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../log/ is created."
mkdir $VPATH/$1/var/log
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -h $VPATH/$1/var/run ; then
echo -ne "\n${L_ALIGN}\tsymlink /var/run to /run/ is created."
ln -s $VPATH/$1/run/ $VPATH/$1/var/run
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
#second, populate with req. files + mount folders
if findmnt -rno SOURCE /var/log/|grep $VPATH > /dev/null; then
echo -ne "\n${L_ALIGN}\t${YELLOW}Warning:${RST} /var/log/ already mounted."
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
else
mount --bind --make-private $VPATH/$1/var/log/ /var/log/
fi
if findmnt -rno SOURCE /var/run/|grep $VPATH > /dev/null; then #TODO: work directly on /run
echo -ne "\n${L_ALIGN}\t\e[33mWarning:${RST} /var/run/ already mounted."
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
else
mount --bind --make-private $VPATH/$1/var/run /var/run/
fi
if findmnt -nro SOURCE /dev/|grep $VPATH > /dev/null; then
echo -ne "\n${L_ALIGN}\t${YELLOW}Warning:${RST} /dev/ already mounted."
if ! test -c /dev/urandom ; then
#umount /dev/
#cp -nr /dev/urandom $VPATH/$1/dev/
mknod -m 444 $VPATH/$1/dev/urandom c 1 9
#cp -nr /dev/null $VPATH/$1/dev/
mknod -m 666 $VPATH/$1/dev/null c 1 3
#mount --bind --make-private $VPATH/$1/dev/ /dev/
fi
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
else
if ! test -c /dev/urandom ; then
#cp -nr /dev/urandom $VPATH/$1/dev/
mknod -m 444 $VPATH/$1/dev/urandom c 1 9
cp -nr /dev/null $VPATH/$1/dev/
fi
mount --bind --make-private $VPATH/$1/dev/ /dev/ #alternatively, set Socket with
#imuxsock module within rsyslog.conf
fi
echo -ne "${DONE_ALIGN:-}done."
}
function nmap_setup () {
#shared:/dev/; race_on: none; req_files:/dev/random
L_ALIGN=${L_ALIGN:="\t\t"}; unset -v DONE_ALIGN
echo -ne "\n${L_ALIGN}Setup nmap wrapper on $1 ......"
if ! test -d $VPATH/$1/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/$1/ is created."
mkdir $VPATH/$1
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/dev/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../dev/ is created."
mkdir $VPATH/$1/dev
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
#check whether was mounted by another wrapper
if findmnt -nro SOURCE /dev/|grep $VPATH > /dev/null; then
echo -ne "\n${L_ALIGN}\t${YELLOW}Warning:${RST} /dev/ already mounted."
if ! test -c /dev/random ; then
#umount /dev/
#cp -nr /dev/random $VPATH/$1/dev/
mknod -m 444 $VPATH/$1/dev/random c 1 8
#cp -nr /dev/null $VPATH/$1/dev/
mknod -m 666 $VPATH/$1/dev/null c 1 3
#mount --bind --make-private $VPATH/$1/dev/ /dev/
fi
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
else
if ! test -c $VPATH/$1/dev/random ; then
#cp -nrf /dev/random $VPATH/$1/dev/ #just in case will be mounted
mknod -m 444 $VPATH/$1/dev/random c 1 8
cp -nr /dev/null $VPATH/$1/dev/
fi #by another wrapper
fi
echo -ne "${DONE_ALIGN:-}done."
}
function snort_setup () {
#shared:/var/log/; race_on: ifnet; req_files:/var/log/snort/alerts;
#Just a placeholder for further developments
cd .
}
function ssh_setup () {
#shared: /dev/pts; race_on: ; req_files:/dev/ptmx, /dev/pts/ptmx [mounted]
L_ALIGN=${L_ALIGN:="\t\t"}; unset -v DONE_ALIGN
echo -ne "\n${L_ALIGN}Setup ssh wrapper on $1 ......"
if ! command -v ssh-askpass > /dev/null; then
echo -ne "\n${L_ALIGN}\t${RED}Error:${RST} ssh-askpass not installed."
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
exit 1
fi
if ! test -d $VPATH/$1/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/$1/ is created."
mkdir $VPATH/$1
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/dev/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../dev/ is created."
mkdir $VPATH/$1/dev
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
#see tldp site for how to populate /dev
if ! test -c $VPATH/$1/dev/tty ; then
mknod -m 666 $VPATH/$1/dev/tty c 5 0
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -c $VPATH/$1/dev/console ; then
mknod -m 622 $VPATH/$1/dev/console c 5 1
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
#if ! test -p $VPATH/$1/dev/xconsole ; then
# mkfifo -m 644 $VPATH/$1/dev/xconsole
# DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
#fi
if ! test -c $VPATH/$1/dev/ptmx ; then
mknod -m 666 $VPATH/$1/dev/ptmx c 5 2
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/dev/pts ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../dev/pts is created."
mkdir $VPATH/$1/dev/pts
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
#check whether was mounted by another wrapper
if findmnt -rno SOURCE /dev/|grep $VPATH > /dev/null; then
echo -ne "\n${L_ALIGN}\t${YELLOW}Warning:${RST} /dev/ already mounted."
if ! findmnt -nr -o SOURCE /dev/pts|grep $VPATH > /dev/null; then
mount -t devpts $VPATH/$1/dev/pts /dev/pts
fi
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
else
mount --bind --make-private $VPATH/$1/dev/ /dev/
if ! findmnt -nr -o SOURCE /dev/pts|grep $VPATH > /dev/null; then
mount -t devpts $VPATH/$1/dev/pts /dev/pts
fi
fi
echo -ne "${DONE_ALIGN:-}done."
}
function strongswan_setup () {
#shared:/etc/ /var/ /var/run /run/; race_on:/var/lock /var/run/charon.pid /etc/ipsec.conf
# ?/var/log/? ?/dev/? ?/var/log/syslog? ?ifnet?; req_files: ?/dev/urandom?;
#Assumed: ipsec tool for IPSec system control
L_ALIGN=${L_ALIGN:="\t\t"}; unset -v DONE_ALIGN
echo -ne "\n${L_ALIGN}Setup strongswan wrapper on $1 ......"
if ! command -v ipsec > /dev/null; then
echo -ne "\n${L_ALIGN}\t${RED}Error:${RST} ipsec tool not installed."
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
exit 1
fi
#first, clone the shared folders + raced with the others wrappers
if ! test -d $VPATH/$1/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/$1/ is created."
mkdir $VPATH/$1
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/etc/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/... /etc/ is created."
mkdir $VPATH/$1/etc
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/var/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../var/ is created."
mkdir $VPATH/$1/var
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/run/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../run/ is created."
mkdir $VPATH/$1/run
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d $VPATH/$1/run/lock/ ; then
echo -ne "\n${L_ALIGN}\tfolder $VPATH/.../run/lock/ is created."
mkdir $VPATH/$1/run/lock
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -h $VPATH/$1/var/run ; then
echo -ne "\n${L_ALIGN}\tsymlink $VPATH/$1/var/run to $VPATH/$1/run/ is created."
ln -s $VPATH/$1/run/ $VPATH/$1/var/run
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -h /var/lock ; then
echo -ne "\n${L_ALIGN}\tsymlink /var/lock to $VPATH/.../var/lock is created."
ln -s $VPATH/$1/run/lock /var/lock
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if findmnt -nro SOURCE /var/run/|grep $VPATH > /dev/null; then
echo -ne "\n${L_ALIGN}\t\e[33mWarning:${RST} /var/run/ already mounted."
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
else
mount --bind --make-private $VPATH/$1/var/run /var/run/
fi
#second, populate clone folders with req_files + mount them
#check whether was mounted by another wrapper
if findmnt -nro SOURCE /etc/|grep $VPATH > /dev/null; then
echo -ne "\n${L_ALIGN}\t${YELLOW}Warning:${RST} /etc/ already mounted."
if ! test -f /etc/ipsec.conf ; then
umount /etc/
cp -nr /etc/ipsec.conf $VPATH/$1/etc/ #bring it from installation folder
mount --bind --make-private $VPATH/$1/etc/ /etc/
fi
if ! test -f /etc/ipsec.secrets ; then
umount /etc/
cp -nr /etc/ipsec.secrets $VPATH/$1/etc/ #idem
mount --bind --make-private $VPATH/$1/etc/ /etc/
fi
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
else
if ! test -d /etc/netns/ ; then
echo -ne "\n${L_ALIGN}\tfolder /etc/netns/ is created."
mkdir /etc/netns
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -d /etc/netns/$1/ ; then
echo -ne "\n${L_ALIGN}\tfolder /etc/netns/$1/ is created."
mkdir /etc/netns/$1
DONE_ALIGN=${DONE_ALIGN:="\n${L_ALIGN}"}
fi
if ! test -f /etc/netns/$1/ipsec.conf ; then
cp -nrf /etc/ipsec.conf /etc/netns/$1/
fi
if ! test -f /etc/netns/$1/ipsec.secrets ; then
cp -nr /etc/ipsec.secrets /etc/netns/$1/
fi
if ! test -f $VPATH/$1/etc/ipsec.conf ; then #just in case will be mounted
cp -nr /etc/ipsec.conf $VPATH/$1/etc/ #by another wrapper instance
fi
if ! test -f $VPATH/$1/etc/ipsec.secrets ; then #just in case will be mounted
cp -nr /etc/ipsec.secrets $VPATH/$1/etc/ #by another wrapper instance
fi
if ! test -d /etc/netns/$1/ipsec.d/ ; then
mkdir -m 755 /etc/netns/$1/ipsec.d
mkdir -m 700 /etc/netns/$1/ipsec.d/private
mkdir -m 755 /etc/netns/$1/ipsec.d/certs
mkdir -m 755 /etc/netns/$1/ipsec.d/cacerts
else
if ! test -d /etc/netns/$1/ipsec.d/private ; then
mkdir -m 700 /etc/netns/$1/ipsec.d/private
fi
if ! test -d /etc/netns/$1/ipsec.d/certs ; then
mkdir -m 700 /etc/netns/$1/ipsec.d/certs
fi
if ! test -d /etc/netns/$1/ipsec.d/cacerts ; then
mkdir -m 700 /etc/netns/$1/ipsec.d/cacerts
fi
fi
fi
echo -ne "${DONE_ALIGN:-}done."
}
#call wrappers here for setup
if [ "$2" = "setup" ]; then
case "$3" in
rsyslog)
rsyslog_setup $1
;;
nmap)
nmap_setup $1
;;
ssh)
ssh_setup $1
;;
snort)
snort_setup $1
;;
strongswan)
strongswan_setup $1
;;
?)
echo -e "${RED}Service $3 is not supported.${RST}\nQuit."
exit;;
esac
elif [ "$2" = "cleanup" ]; then
L_ALIGN=${L_ALIGN:="\t"}; unset -v DONE_ALIGN
case "$3" in
rsyslog)
echo -ne "\n${L_ALIGN}Clean rsyslog wrapper on $1 ....."
#TODO: umount /var/run /var/log/ /dev/ ??? <-- ctxt-based cleanup
#test -d $VPATH/$1/var/run/ && sudo rm -rf $VPATH/$1/var/run/*
#test -d $VPATH/$1/var/log/ && sudo rm -rf $VPATH/$1/var/log/*
echo -ne "${DONE_ALIGN:-}done."
;;
nmap)
echo -ne "\n${L_ALIGN}Clean nmap wrapper on $1 ....."
echo -ne "${DONE_ALIGN:-}done."
;;
ssh)
echo -ne "\n${L_ALIGN}Clean ssh wrapper on $1 ....."
echo -ne "${DONE_ALIGN:-}done."
;;
snort)
echo -ne "\n${L_ALIGN}Clean snort wrapper on $1 ....."
echo -ne "${DONE_ALIGN:-}done."
;;
strongswan)
echo -ne "\n${L_ALIGN}Clean strongswan wrapper on $1 ....."
echo -ne "${DONE_ALIGN:-}done."
;;
?)
echo -ne "${RED}Service $3 is not supported.${RST}\nQuit."
exit;;
esac
fi
#TODO: elif [ "$4" = "start" ]; then