-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc.subr
464 lines (395 loc) · 10.8 KB
/
nc.subr
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
if [ ! "$_CBSD_NC_SUBR" ]; then
_CBSD_NC_SUBR=1
###
# transparent remote command routing
if [ -n "${CBSD_RNODE}" ]; then
rnode="${CBSD_RNODE}"
unset CBSD_RNODE
cmd=$( /usr/bin/basename $0 )
exec /usr/local/bin/cbsd rexe node=${rnode} /usr/local/bin/cbsd ${cmd} $@
fi
# common functions used by various rc scripts
# jname must be set
# export myjid as JID
# return 0 if jail not exist
# return 1 if jail exist
get_jid() {
local _res
myjid="0"
[ -z "$jname" -a -z "${path}" ] && return 0
_res=$( cbsdjls | while read _jid _name; do
[ "${_name}" = "${jname}" ] && echo ${_jid}
done)
[ -z "${_res}" ] && return 0
myjid=${_res}
return 1
}
# fatal error. Print message then quit with exitval
err() {
exitval=$1
shift
${ECHO} "$*"
# 1>&2
[ "${pause_after_err}" = "1" ] && echo "Press any key" && read p
exit $exitval
}
# print $MYARG and exit
usage() {
err 1 "usage: $0 ${MYARG}"
}
# parser/initialization for MYARG, OPTARG
init() {
local MYNUMTEST=0 _i
local bin_version=
[ -z "${double_args}" ] && double_args=1
bin_version=$( /usr/local/bin/cbsd -c version 2>/dev/null )
[ "${bin_version}" != "${myversion}" ] && ${ECHO} "${MAGENTA}Warning: CBSD is ${bin_version} while workdir initializated for ${myversion}. Please re-run: ${GREEN}cbsd initenv${NORMAL}"
for _i in ${MYARG} ${MYOPTARG}; do
MYNUMTEST=$(( MYNUMTEST + 1 ))
done
DEBLOG="${tmpdir}/`/usr/bin/basename $0`-log.$$"
trap "/bin/rm -f ${DEBLOG}" HUP INT ABRT BUS TERM EXIT
# disable history of execution cbsd command inside cbsd command
export NO_CBSD_HISTORY=yes
if [ "$1" = "--help" ]; then
[ -z "${CBSDMODULE}" ] && CBSDMODULE="sys"
${ECHO} "[${BOLD}${CBSDMODULE}${NORMAL}] ${GREEN}${MYDESC}${NORMAL}"
${ECHO} "${BOLD}require: ${GREENL}${MYARG}${NORMAL}"
${ECHO} "${BOLD}opt: ${GREEN}${MYOPTARG}${NORMAL}"
[ -n "${ADDHELP}" ] && printf "${GREEN}${ADDHELP}${NORMAL}"
[ -f "/usr/local/share/doc/cbsd/currentver.html" ] && cbsddocsrc="/usr/local/share/doc/cbsd"
[ -n "${MANPAGE}" ] && printf "${BOLD}Check out manual page: ${GREEN}${MANPAGE}${NORMAL}\n"
[ -n "${EXTHELP}" -a -n "${cbsddocsrc}" ] && printf "${BOLD}External help: ${GREEN}${cbsddocsrc}/${EXTHELP}${NORMAL}\n"
exit 0
fi
if [ "$1" = "--args" ]; then
echo "${greeting} ${MYNUMTEST}"
for argword in ${MYARG} ${MYOPTARG}; do
echo "${argword}"
done
err 0 ""
fi
[ -f "${localcbsdconf}" ] || err 1 "no such local conf file"
. ${localcbsdconf}
# example
#MYARG="src mk"
#MYOPTARG="arch"
while [ -n "$1" -a $MYNUMTEST -gt 0 ]; do
ARG="${1%%=*}" #todo: value with "world1 world2"
VAL="${1##*=}"
for arg in ${MYARG} ${MYOPTARG}; do
case "$arg" in
"${ARG}")
eval T="\$$ARG"
[ ${double_args} -eq 0 -a -n "${T}" ] && continue # value already set, skip
eval "${ARG}='${VAL}'"; MYNUMTEST=$((MYNUMTEST - 1));
break
;;
esac
done
shift
done
for i in $MYARG; do
unset T
eval T="\$$i"
[ -z "${T}" ] && err 1 "${MAGENTA}Please set: ${GREEN}$i${NORMAL}"
done
return 0
}
# getyesno var
# Show $1 strings and get answer in infinity loop
# return 0 when YES/1 and 1 when not
# return 3 if not interactive ($inter=0) (or INTER=0 env exist)
# if ALWAYS_YES=1 and inter=0, always return 0
# if ALWAYS_NO=1 and inter=0, always return 1
getyesno()
{
if [ "${inter}" = "0" -o "${INTER}" = "0" ]; then
[ "${ALWAYS_YES}" = "1" ] && return 0
[ "${ALWAYS_NO}" = "1" ] && return 1
return 3
fi
if [ -z "${DIALOG}" ]; then
${ECHO} "${BOLD}${1}${NORMAL}"
answ=
while [ x$answ = x ]; do
${ECHO} "[${GREEN}yes(1) or no(0)${NORMAL}]"
read reply leftover
case $reply in
# "yes", "true", "on", or "1"
Y*|y*|1)
return 0
;;
# "no", "false", "off", or "0"
N*|n*|0)
return 1
;;
esac
done
else
. ${dialog}
local msg_yes="yes"
local msg_no="no"
f_dialog_title " Yes and No "
f_dialog_yesno "${1}" "${hline}"
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
fi
}
# value="YES" or "NO"
checkyesno()
{
eval _value=\$${1}
case $_value in
# "yes", "true", "on", or "1"
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
return 0
;;
# "no", "false", "off", or "0"
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
return 1
;;
*)
# echo "\$${1} is not set properly - see ${rcvar_manpage}."
return 1
;;
esac
}
# cut prefix from x.x.x.x/pref into IWM variable
ipwmask()
{
IWM=${1%%/*}
}
# parse ip4_addr, check ip, remove commas
# detect when IP6 in list and set flags for sleep
# export variables:
# ipv4_first_public
# ipv4_first_private
# ipv4_first
# ipv6_first_public
# ipv6_first_private
# ipv6_first
geniplist()
{
local I ST a
[ -z "${1}" ] && return 0
[ ! -f "${distdir}/tools.subr" ] && err 1 "No such tools.subr"
. ${distdir}/tools.subr
local private_net="10.0.0.0/8 172.16.0.0/12 192.168.0.0/16"
local pnet NC_RANGE INNET res
# init global exported variables
ipv4_first_public=
ipv4_first_private=
ipv4_first=
ipv6_first_public=
ipv6_first_private=
ipv6_first=
HAVE_IPV6=0
multiips=$@
I=$( echo ${multiips} |/usr/bin/tr "," " " )
IPS=""
ST=0
for a in ${I}; do
# Identify type {inet,inet6}.
iptype ${a}
case $? in
0)
echo "IP ${a} not identified"
continue
;;
1)
[ -z "${ipv4_first}" ] && ipv4_first="${IWM}" # IWM from iptype
INNET=0 # count of coincidence in private network
if [ -z "${ipv4_first_private}" -o -z "${ip4_first_public}" ]; then
for pnet in ${private_net}; do
NC_RANGE=$( /bin/echo ${pnet} |/usr/bin/tr "/" " " )
netmask ${NC_RANGE} ${IWM} > /dev/null 2>&1
res=$?
if [ ${res} -eq 1 ]; then
INNET=$(( INNET + 1 ))
fi
done
fi
[ -z "${ipv4_first_private}" -a ${INNET} -ne 0 ] && ipv4_first_private="${IWM}"
[ -z "${ipv4_first_public}" -a ${INNET} -eq 0 ] && ipv4_first_public="${IWM}"
;;
2)
[ -z "${ipv6_first}" ] && ipv6_first="${IWM}" # IWM from iptype
HAVE_IPV6=1
;;
esac
# this is ip
ST=$((ST + 1))
if [ ${ST} -eq 1 ]; then
IPS="${a}"
else
IPS="${IPS} ${a}"
fi
done
}
# print "." for progress activity
dot()
{
if [ $? -ne 0 ]; then
${ECHO} "${MAGENTA}Error in $1${NORMAL}"
if [ -f ${DEBLOG} ]; then
${ECHO} "${MAGENTA}Guru meditaion: ${NORMAL}"
cat ${DEBLOG}
fi
exit 1
fi
[ "$1" = "0" ] && return 0
printf "${MAGENTA}.${NORMAL}"
}
# check&make lockfile $1
# $2 - additional action for trap
# $3 - safe, for force
makelock()
{
[ -f "${LOCKFILE}" -a "${3}" = "safe" ] && /bin/rm -f ${LOCKFILE}
[ -f "${LOCKFILE}" ] && err 1 "Locked: ${LOCKFILE}. Try later"
TRAPS="/bin/rm -f ${LOCKFILE}; exit"
if [ -n "$2" ]; then
TRAPS="$2; ${TRAPS}"
fi
trap "${TRAPS}" HUP KILL INT ABRT BUS TERM EXIT
[ "$3" = "safe" ] || /usr/bin/touch $LOCKFILE
}
# search for slow jails and wait him before exit
# $1 - which file lookup: stop or start
# if $2 then timeout=$2
wait_for_fpid()
{
local ECHO="echo -e"
local A PID
local parallel=0
[ -z "${1}" ] && return 0
local myaction="${1}"
local myfile="j${myaction}" # jstop and jstart file
[ -n "$2" ] && parallel=${2}
while [ 1 ]; do
A=$( /usr/bin/find ${ftmpdir} -type f -depth 1 -maxdepth 1 -name ${myfile}\.\*\.\* |/usr/bin/head -n1 )
[ -z "${A}" ] && break
PID=$( /bin/cat ${A} )
[ -z "${PID}" ] && break
/bin/ps -p ${PID} > /dev/null 2>&1
[ $? -ne 0 ] && break # No such pid here
${ECHO} "${MAGENTA}Waiting for ${myaction}ing jail who have pid: ${GREEN}${PID}${NORMAL}"
cbsd_pwait --pid=${PID} --timeout=${parallel}
done
}
# wrapper for including config in $etcdir. $1 is name of the file
# At the first we try to include $etcdir/defaults/$1, in second: $etcdir/$1
# third: ${moduledir}/etc/$1
# finaly: in jailsysdir/jname/etc/
readconf()
{
[ -z "${1}" ] && return 1
[ -r "${etcdir}/defaults/$1" ] && . ${etcdir}/defaults/$1
[ -r "${etcdir}/$1" ] && . ${etcdir}/$1
if [ -n "${CBSDMODULE}" ]; then
[ -r "${moduledir}/${CBSDMODULE}.d/etc/$1" ] && . ${moduledir}/${CBSDMODULE}.d/etc/$1
fi
#finnaly, search conf in jails-system/$jname/etc
if [ -n "${jname}" ]; then
[ -r "${jailsysdir}/${jname}/etc/${1}" ] && . ${jailsysdir}/${jname}/etc/${1}
fi
return 0
}
# wrapper for copy config from $etcdir to $2 location
# local copy of config win, $etcdir/defaults - is last (reverse order to readconf)
storeconf()
{
[ -z "${1}" -o -z "${2}" ] && return 1
# per-jail etc dir - first and high priority finnaly
if [ -n "${jname}" ]; then
[ -r "${jailsysdir}/${jname}/etc/${1}" ] && /bin/cp ${jailsysdir}/${jname}/etc/${1} ${2} && return 0
fi
[ -r "${etcdir}/$1" ] && /bin/cp ${etcdir}/$1 ${2} && return 0
[ -r "${etcdir}/defaults/$1" ] && /bin/cp ${etcdir}/defaults/$1 ${2} && return 0
# no conf here
return 1
}
validate_jname()
{
local _res
[ -z "$1" ] && return 1
_res=$( echo ${1} |${GREP_CMD} -E "^[0-9]|[-E%^&\(\)+\/?><:\{\}=\.]" 2>&1 )
return $?
}
# echo ${_device} |grep -sq ^/
# concat for workdir when $1 not beginning with /
# data=$( normalize_path ${data} )
normalize_path()
{
local _res prefix
prefix=$( substr --pos=0 --len=1 --str="${1}" )
if [ "${prefix}" != "/" ]; then
_res="${workdir}/${1}"
else
_res="${1}"
fi
echo "${_res}"
}
# we need support for relative paths (when $relative_path not set to 0)
# init path from jconf or rcconf
init_jail_path()
{
[ "${relative_path}" = "0" ] && return 0
path=$( normalize_path ${path} )
mount_fstab=$( normalize_path ${mount_fstab} )
data=$( normalize_path ${data} )
rcconf=$( normalize_path ${rcconf} )
jailskeldir=$( normalize_path ${jailskeldir} )
}
get_help()
{
local _fetch=0
local _myhtml=
local _myhelp=
local TRAP=""
local _res
[ -z "${EXTHELP}" ] && return 0
# verification that we have a local copy documentation.
# - if not, try to use the online documentation.
_myhtml="/usr/local/share/doc/cbsd/${EXTHELP}"
if [ ! -f "${_myhtml}" ]; then
_myhtml="/tmp/myhtml.$$"
_fetch=1
else
_fetch=0
fi
_myhelp="/tmp/myhelp.$$"
if [ ${_fetch} -eq 1 ]; then
f_dialog_info "You have no local documentation. Try to obtain docs from: ${cbsddocsrc}. Please wait..."
_res=$( /usr/bin/su -m nobody -c "/usr/bin/fetch -q -o ${_myhtml} ${cbsddocsrc}/${EXTHELP}" 2>&1 )
if [ $? -ne 0 -o ! -f "${_myhtml}" ]; then
local msg_ok="It's a pity"
f_dialog_msgbox "${_res}"
return 0
fi
TRAP="${TRAP} /bin/rm -f ${_myhtml};"
trap "${TRAP}" HUP INT ABRT BUS TERM EXIT
fi
# need for help in translate quality
/bin/cat > ${_myhelp} << EOF
Attention! This is automatic translation of this text.
You can improve the quality and help CBSD by sending a more correct version
of the text or fix html pages via GITHUB repository: https://github.com/olevole/cbsd-wwwdoc
EOF
/usr/bin/sed -e 's/<[^>]*>//g' ${_myhtml} |/usr/bin/tr -s "[:space:]"| /usr/bin/fold -s -w 100 >> ${_myhelp}
if [ ${_fetch} -eq 1 ]; then
trap "" HUP INT ABRT BUS TERM EXIT
/bin/rm -f "${_myhtml}"
fi
[ ! -f "${_myhelp}" ] && return 0
trap "/bin/rm -f ${_myhelp}" HUP INT ABRT BUS TERM EXIT
f_show_help "${_myhelp}"
/bin/rm -f ${_myhelp}
trap "" HUP INT ABRT BUS TERM EXIT
}
###
fi