-
Notifications
You must be signed in to change notification settings - Fork 486
/
startjail.sh
executable file
·559 lines (484 loc) · 13.9 KB
/
startjail.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#/bin/sh
# Script to startup a jail
# Args $1 = jail-name
#######################################################################
# Source our functions
PROGDIR="/usr/local/share/warden"
# Source our variables
. ${PROGDIR}/scripts/backend/functions.sh
start_jail_vimage()
{
BRIDGE=
# See if we need to create a new bridge, or use an existing one
_bridges=`get_bridge_interfaces`
if [ -n "${_bridges}" ] ; then
for _bridge in ${_bridges}
do
_members=`get_bridge_members ${_bridge}`
for _member in ${_members}
do
if [ "${_member}" = "${IFACE}" ] ; then
BRIDGE=${_bridge}
break
fi
done
if [ -n "${BRIDGE}" ] ; then
break
fi
done
fi
if [ -z "${BRIDGE}" ] ; then
BRIDGE=`ifconfig bridge create mtu ${MTU}`
fi
if [ -n "${IFACE}" ] ; then
if ! is_bridge_member "${BRIDGE}" "${IFACE}" ; then
ifconfig ${BRIDGE} addm ${IFACE}
fi
fi
# create epair for vimage jail
EPAIRA=`ifconfig epair create mtu ${MTU}`
ifconfig ${EPAIRA} up
EPAIRB=`echo ${EPAIRA}|sed -E "s/([0-9])a$/\1b/g"`
ifconfig ${BRIDGE} addm ${EPAIRA} up
# If no bridge specified, and IP4 is enabled, lets suggest one
if [ -z "$BRIDGEIP4" -a -n "$IP4" -a "${NATENABLE}" = "YES" ] ; then
BRIDGEIP4="`echo $IP4 | cut -d '.' -f 1-3`.254"
fi
if [ -n "${BRIDGEIP4}" ] ; then
if ! ipv4_configured "${BRIDGE}" ; then
ifconfig ${BRIDGE} inet "${BRIDGEIP4}"
elif ! ipv4_address_configured "${BRIDGE}" "${BRIDGEIP4}" ; then
ifconfig ${BRIDGE} inet alias "${BRIDGEIP4}"
fi
fi
if [ -n "${BRIDGEIPS4}" ] ; then
for _ip in ${BRIDGEIPS4}
do
if ! ipv4_address_configured "${BRIDGE}" "${_ip}" ; then
ifconfig ${BRIDGE} inet alias "${_ip}"
fi
done
fi
if [ -n "${BRIDGEIP6}" ] ; then
if ! ipv6_configured "${BRIDGE}" ; then
ifconfig ${BRIDGE} inet6 "${BRIDGEIP6}"
elif ! ipv6_address_configured "${BRIDGE}" "${BRIDGEIP6}" ; then
ifconfig ${BRIDGE} inet6 alias "${BRIDGEIP6}"
fi
fi
if [ -n "${BRIDGEIPS6}" ] ; then
for _ip in ${BRIDGEIPS6}
do
if ! ipv6_address_configured "${BRIDGE}" "${_ip}" ; then
ifconfig ${BRIDGE} inet6 alias "${_ip}"
fi
done
fi
# Start the jail now
warden_print "jail -c path=${JAILDIR} host.hostname=${HOST} ${jFlags} persist vnet"
jail -c path=${JAILDIR} host.hostname=${HOST} ${jFlags} persist vnet
if [ $? -ne 0 ] ; then
echo "ERROR: Failed starting jail with above command..."
umountjailxfs "${JAILNAME}"
exit 1
fi
JID="`jls | grep ${JAILDIR}$ | tr -s " " | cut -d " " -f 2`"
# Move epairb into jail
ifconfig ${EPAIRB} vnet ${JID}
# Configure the IPv4 addresses
if [ -n "${IP4}" ] ; then
warden_print "Setting IP4 address: ${IP4}"
jexec ${JID} ifconfig ${EPAIRB} inet "${IP4}"
fi
for ip4 in ${IPS4}
do
ipv4_configured ${EPAIRB} ${JID}
if [ "$?" = "0" ] ; then
if ! ipv4_address_configured "${EPAIRB}" "${ip4}" "${JID}" ; then
jexec ${JID} ifconfig ${EPAIRB} inet alias ${ip4}
fi
else
jexec ${JID} ifconfig ${EPAIRB} inet ${ip4}
fi
done
# Configure the IPv6 addresses
if [ -n "${IP6}" ] ; then
warden_print "Setting IP6 address: ${IP6}"
jexec ${JID} ifconfig ${EPAIRB} inet6 "${IP4}"
fi
for ip6 in ${IPS6}
do
ipv6_configured ${EPAIRB} ${JID}
if [ "$?" = "0" ] ; then
if ! ipv6_address_configured "${EPAIRB}" "${ip6}" "${JID}" ; then
jexec ${JID} ifconfig ${EPAIRB} inet6 alias ${ip6}
fi
else
jexec ${JID} ifconfig ${EPAIRB} inet6 ${ip6}
fi
done
#
# Configure default IPv4 gateway
#
if [ -n "${GATEWAY4}" ] ; then
jexec ${JID} route add -inet default ${GATEWAY4}
#
# No defaultrouter configured for IPv4, so if bridge IP address was
# configured, we set the default router to that IP.
#
elif [ -n "${BRIDGEIP4}" ] ; then
get_ip_and_netmask "${BRIDGEIP4}"
jexec ${JID} route add -inet default ${JIP}
fi
#
# Configure default IPv6 gateway
#
if [ -n "${GATEWAY6}" ] ; then
jexec ${JID} route add -inet6 default ${GATEWAY6}
#
# No defaultrouter configured for IPv6, so if bridge IP address was
# configured, we set the default router to that IP.
#
elif [ -n "${BRIDGEIP6}" ] ; then
get_ip_and_netmask "${BRIDGEIP6}"
jexec ${JID} route add -inet6 default ${JIP}
fi
#
# Configure lo0 interface
#
jexec ${JID} ifconfig lo0 up
#
# If NAT is not enabled, return now
#
if [ "${NATENABLE}" = "NO" ] ; then
if [ -z "${GATEWAY4}" ] ; then
GATEWAY4="$(get_default_route)"
fi
if [ -n "${GATEWAY4}" ] ; then
jexec ${JID} route add -inet default ${GATEWAY4}
fi
return 0
fi
#
# Set ourself to be a jail router with NAT. Don't
# use PF since it will panic the box when used
# with VIMAGE.
#
ip_forwarding=`sysctl -n net.inet.ip.forwarding`
if [ "${ip_forwarding}" = "0" ] ; then
sysctl net.inet.ip.forwarding=1
fi
ip6_forwarding=`sysctl -n net.inet6.ip6.forwarding`
if [ "${ip6_forwarding}" = "0" ] ; then
sysctl net.inet6.ip6.forwarding=1
fi
firewall_enable=`egrep '^firewall_enable' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
firewall_type=`egrep '^firewall_type' /etc/rc.conf|cut -f2 -d'='|sed 's|"||g'`
if [ "${firewall_enable}" != "YES" -o "${firewall_type}" != "open" ] ; then
tmp_rcconf=`mktemp /tmp/.wdn.XXXXXX`
egrep -v '^firewall_(enable|type)' /etc/rc.conf >> "${tmp_rcconf}"
cat<<__EOF__>>"${tmp_rcconf}"
firewall_enable="YES"
firewall_type="open"
__EOF__
if [ -s "${tmp_rcconf}" ] ; then
cp /etc/rc.conf /var/tmp/rc.conf.bak
mv "${tmp_rcconf}" /etc/rc.conf
if [ "$?" != "0" ] ; then
mv /var/tmp/rc.conf.bak /etc/rc.conf
fi
fi
/sbin/ipfw -f flush
warden_run ipfw add allow all from any to any via lo0
fi
prioroty=0
instance=`get_ipfw_nat_instance "${IFACE}"`
if [ -z "${instance}" ] ; then
priority=`get_ipfw_nat_priority`
instance=`get_ipfw_nat_instance`
else
priority=`get_ipfw_nat_priority "${IFACE}"`
instance=`get_ipfw_nat_instance "${IFACE}"`
fi
ext_ip4=`get_interface_ipv4_address "${IFACE}"`
ext_ip6=`get_interface_ipv6_address "${IFACE}"`
warden_run ipfw nat "${instance}" config if "${IFACE}" reset same_ports unreg_only log
if [ -n "${ext_ip4}" ] ; then
ipfw list | grep -q "from any to ${ext_ip4} in recv ${IFACE}"
if [ "$?" != "0" ] ; then
warden_run ipfw add nat "${instance}" \
all from any to ${ext_ip4} in recv ${IFACE}
fi
fi
if [ -n "${ext_ip6}" ] ; then
ipfw list | grep -q "from any to ${ext_ip6} in recv ${IFACE}"
if [ "$?" != "0" ] ; then
warden_run ipfw add nat "${instance}" \
all from any to ${ext_ip6} in recv ${IFACE}
fi
fi
if [ -n "${IP4}" ] ; then
get_ip_and_netmask "${IP4}"
warden_run ipfw add nat "${instance}" \
all from ${JIP} to any out xmit ${IFACE}
fi
for ip4 in ${IPS4}
do
get_ip_and_netmask "${ip4}"
warden_run ipfw add nat "${instance}" \
all from ${JIP} to any out xmit ${IFACE}
done
if [ -n "${IP6}" ] ; then
get_ip_and_netmask "${IP6}"
warden_run ipfw add nat "${instance}" \
all from ${JIP} to any out xmit ${IFACE}
fi
for ip6 in ${IPS6}
do
get_ip_and_netmask "${ip6}"
warden_run ipfw add nat "${instance}" \
all from ${JIP} to any out xmit ${IFACE}
done
# End of jail VIMAGE startup function
}
# Function to start a jail up the normal way
start_jail_standard()
{
# Check for primary IPV4 / IPV6
if [ -n "$IP4" ] ; then
_ipflags="ip4.addr=${IP4}"
ifconfig $IFACE inet alias ${IP4}
fi
if [ -n "$IP6" ] ; then
_ipflags="${_ipflags} ip6.addr=${IP6}"
ifconfig $IFACE inet6 alias ${IP6}
fi
# Setup the extra IP4s for this jail
for _ip in $IPS4
do
ifconfig $IFACE inet alias ${_ip}
_ipflags="${_ipflags} ip4.addr=${_ip}"
done
# Setup the extra IP6s for this jail
for _ip in $IPS6
do
ifconfig $IFACE inet6 alias ${_ip}
_ipflags="${_ipflags} ip6.addr=${_ip}"
done
warden_print "jail -c path=${JAILDIR} ${_ipflags} host.hostname=${HOST} ${jFlags} persist"
jail -c path=${JAILDIR} ${_ipflags} host.hostname=${HOST} ${jFlags} persist
if [ $? -ne 0 ] ; then
warden_error "Failed starting jail with above command..."
umountjailxfs "${JAILNAME}"
exit 1
fi
JID="`jls | grep ${JAILDIR}$ | tr -s " " | cut -d " " -f 2`"
}
JAILNAME="${1}"
export JAILNAME
if [ -z "${JAILNAME}" ]
then
warden_error "No jail specified to start!"
exit 5
fi
if [ -z "${JDIR}" ]
then
warden_error "JDIR is unset!!!!"
exit 5
fi
JAILDIR="${JDIR}/${JAILNAME}"
if [ ! -d "${JAILDIR}" ]
then
warden_error "No jail located at ${JAILDIR}"
exit 5
fi
# Make sure the jail is NOT already running
jls | grep ${JAILDIR}$ >/dev/null 2>/dev/null
if [ "$?" = "0" ]
then
warden_error "Jail appears to be running already!"
exit 6
fi
# pre-start hooks
if [ -x "${JMETADIR}/jail-pre-start" ] ; then
"${JMETADIR}/jail-pre-start"
fi
IFACE=
DEFAULT=0
# Make sure jail uses special interface if specified
if [ -e "${JMETADIR}/iface" ] ; then
IFACE=`cat "${JMETADIR}/iface"`
fi
if [ -z "${IFACE}" ] ; then
IFACE=`get_default_interface`
DEFAULT=1
fi
if [ -z "${IFACE}" ] ; then
warden_error "no interface specified and a default doesn't exist!"
exit 6
fi
MTU=`ifconfig ${IFACE} | head -1 | sed -E 's/.*mtu ([0-9]+)/\1/g'`
GATEWAY4=
if [ -e "${JMETADIR}/defaultrouter-ipv4" ] ; then
GATEWAY4=`cat "${JMETADIR}/defaultrouter-ipv4"`
fi
GATEWAY6=
if [ -e "${JMETADIR}/defaultrouter-ipv6" ] ; then
GATEWAY6=`cat "${JMETADIR}/defaultrouter-ipv6"`
fi
BRIDGEIP4=
if [ -e "${JMETADIR}/bridge-ipv4" ] ; then
BRIDGEIP4=`cat "${JMETADIR}/bridge-ipv4"`
fi
BRIDGEIPS4=
if [ -e "${JMETADIR}/alias-bridge-ipv4" ] ; then
while read line
do
BRIDGEIPS4="${BRIDGEIPS4} $line"
done < ${JMETADIR}/alias-bridge-ipv4
fi
BRIDGEIP6=
if [ -e "${JMETADIR}/bridge-ipv6" ] ; then
BRIDGEIP6=`cat "${JMETADIR}/bridge-ipv6"`
fi
BRIDGEIPS6=
if [ -e "${JMETADIR}/alias-bridge-ipv6" ] ; then
while read line
do
BRIDGEIPS6="${BRIDGEIPS6} $line"
done < ${JMETADIR}/alias-bridge-ipv6
fi
# Check if we need to enable vnet
VIMAGEENABLE="NO"
if [ -e "${JMETADIR}/vnet" ] ; then
VIMAGEENABLE="YES"
fi
# Check if we need to enable NAT
NATENABLE="NO"
if [ -e "${JMETADIR}/nat" ] ; then
NATENABLE="YES"
fi
set_warden_metadir
if [ -e "${JMETADIR}/jail-linux" ] ; then
LINUXJAIL="YES"
fi
HOST="`cat ${JMETADIR}/host`"
if is_symlinked_mountpoint ${JAILDIR}/dev; then
warden_print "${JAILDIR}/dev has symlink as parent, not mounting"
else
mount -t devfs devfs "${JAILDIR}/dev"
fi
if [ "$LINUXJAIL" = "YES" ] ; then
# Linux Jail
if is_symlinked_mountpoint ${JAILDIR}/proc; then
warden_print "${JAILDIR}/proc has symlink as parent, not mounting"
else
mount -t linprocfs linproc "${JAILDIR}/proc"
fi
if is_symlinked_mountpoint ${JAILDIR}/dev/fd; then
warden_print "${JAILDIR}/dev/fd has symlink as parent, not mounting"
else
mount -t fdescfs null "${JAILDIR}/dev/fd"
fi
if is_symlinked_mountpoint ${JAILDIR}/sys; then
warden_print "${JAILDIR}/sys has symlink as parent, not mounting"
else
mount -t linsysfs linsys "${JAILDIR}/sys"
fi
if [ -e "${JAILDIR}/lib/init/rw" ] ; then
if is_symlinked_mountpoint ${JAILDIR}/lib/init/rw; then
warden_print "${JAILDIR}/lib/init/rw has symlink as parent, not mounting"
else
mount -t tmpfs tmpfs "${JAILDIR}/lib/init/rw"
fi
fi
else
# FreeBSD Jail
if is_symlinked_mountpoint ${JAILDIR}/proc; then
warden_print "${JAILDIR}/proc has symlink as parent, not mounting"
else
mount -t procfs proc "${JAILDIR}/proc"
fi
if [ -e "${JMETADIR}/jail-portjail" ] ; then mountjailxfs ${JAILNAME} ; fi
fi
# Check for user-supplied mounts
if [ -e "${JMETADIR}/fstab" ] ; then
warden_print "Mounting user-supplied file-systems"
cp ${JMETADIR}/fstab /tmp/.wardenfstab.$$
sed -i '' "s|%%JAILDIR%%|${JAILDIR}|g" /tmp/.wardenfstab.$$
mount -a -F /tmp/.wardenfstab.$$
rm /tmp/.wardenfstab.$$
fi
IP4=
if [ -e "${JMETADIR}/ipv4" ] ; then
IP4=`cat "${JMETADIR}/ipv4"`
# Check if somebody snuck in a IP without / on it
echo $IP4 | grep -q '/'
if [ $? -ne 0 ] ; then
IP4="${IP4}/24"
fi
fi
IPS4=
if [ -e "${JMETADIR}/alias-ipv4" ] ; then
while read line
do
IPS4="${IPS4} $line"
done < ${JMETADIR}/alias-ipv4
fi
IP6=
if [ -e "${JMETADIR}/ipv6" ] ; then
IP6=`cat "${JMETADIR}/ipv6"`
# Check if somebody snuck in a IP without / on it
echo $IP6 | grep -q '/'
if [ $? -ne 0 ] ; then
IP6="${IP6}/64"
fi
fi
IPS6=
if [ -e "${JMETADIR}/alias-ipv6" ] ; then
while read line
do
IPS6="${IPS6} $line"
done < ${JMETADIR}/alias-ipv6
fi
jFlags=""
# Grab any additional jail flags
if [ -e "${JMETADIR}/jail-flags" ] ; then
jFlags=`cat ${JMETADIR}/jail-flags`
fi
# Are we using VIMAGE, if so start it up!
if [ "$VIMAGEENABLE" = "YES" ] ; then
start_jail_vimage
else
# Using a standard jail configuration
start_jail_standard
fi
if [ "$LINUXJAIL" = "YES" ] ; then
# If we have a custom start script
if [ -e "${JMETADIR}/jail-start" ] ; then
sCmd=`cat ${JMETADIR}/jail-start`
warden_print "Starting jail with: ${sCmd}"
jexec ${JID} ${sCmd} 2>&1
else
# Check for different init styles
if [ -e "${JAILDIR}/etc/init.d/rc" ] ; then
jexec ${JID} /bin/sh /etc/init.d/rc 3 2>&1
elif [ -e "${JAILDIR}/etc/rc" ] ; then
jexec ${JID} /bin/sh /etc/rc 3 2>&1
fi
fi
else
# If we have a custom start script
if [ -e "${JMETADIR}/jail-start" ] ; then
sCmd=`cat ${JMETADIR}/jail-start`
warden_print "Starting jail with: ${sCmd}"
jexec ${JID} ${sCmd} 2>&1
else
warden_print "Starting jail with: /etc/rc"
jexec ${JID} /bin/sh /etc/rc >/dev/tty 2>&1
fi
fi
# post-start hooks
if [ -x "${JMETADIR}/jail-post-start" ] ; then
"${JMETADIR}/jail-post-start"
fi