-
Notifications
You must be signed in to change notification settings - Fork 0
/
nc.subr
619 lines (536 loc) · 14.2 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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
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}
}
# Same as err but also dup message to syslog with WARNING verbosity level
log_err() {
local _msg
exitval=$1
shift
${ECHO} "$*"
# remove ANSII
_msg=$( ${ECHO} "$*" | ${SED_CMD} "s,$(printf '\033')\\[[0-9;]*[a-zA-Z],,g" )
cbsdlogger WARNING ${CBSD_APP}: ${_msg}
exit ${exitval}
}
# print $MYARG and exit
usage() {
err 1 "usage: $0 ${MYARG}"
}
# parser/initialization for MYARG, OPTARG
# if CBSD_INIT_SAVE2FILE variables exist, use first <arg> as filename
# and save all settings into <arg> filename
init() {
local MYNUMTEST=0 _i
local bin_version= _pos _arg_len _pref
local _tofile=0
local IFS OIFS
[ -n "${CBSD_INIT_SAVE2FILE}" ] && _tofile=1
. ${strings}
[ -z "${double_args}" ] && double_args=1
bin_version=$( /usr/local/bin/cbsd -c version 2>/dev/null )
if [ "${bin_version}" != "${myversion}" ]; then
OIFS="${IFS}"
IFS=" "
${ECHO} "${W1_COLOR}Warning${N1_COLOR}: CBSD is ${bin_version} while workdir initializated for ${myversion}. Please re-run: ${N2_COLOR}cbsd initenv${N0_COLOR}" 1>&2
IFS="${OIFS}"
fi
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
IFS=" "
[ -z "${CBSDMODULE}" ] && CBSDMODULE="sys"
${ECHO} "[${BOLD}${CBSDMODULE}${N0_COLOR}] ${N2_COLOR}${MYDESC}${N0_COLOR}"
${ECHO} "${BOLD}require: ${GREENL}${MYARG}${N0_COLOR}"
${ECHO} "${BOLD}opt: ${N2_COLOR}${MYOPTARG}${N0_COLOR}"
[ -n "${ADDHELP}" ] && printf "${N2_COLOR}${ADDHELP}${N0_COLOR}"
[ -f "/usr/local/share/doc/cbsd/currentver.html" ] && cbsddocsrc="/usr/local/share/doc/cbsd"
[ -n "${MANPAGE}" ] && printf "${BOLD}Check out manual page: ${N2_COLOR}${MANPAGE}${N0_COLOR}\n"
[ -n "${EXTHELP}" -a -n "${cbsddocsrc}" ] && printf "${BOLD}External help: ${N2_COLOR}${cbsddocsrc}/${EXTHELP}_ssi.html${N0_COLOR}\n"
exit 0
fi
if [ "$1" = "--args" ]; then
IFS=" "
echo "${greeting} ${MYNUMTEST}"
for argword in ${MYARG} ${MYOPTARG}; do
echo "${argword}"
done
err 0 ""
fi
[ ! -r "${localcbsdconf}" ] && err 1 "no such local conf file: ${localcbsdconf}"
. ${localcbsdconf}
# example
#MYARG="src mk"
#MYOPTARG="arch"
i=0
while [ -n "${1}" ]; do
IFS=
strpos --str="${1}" --search="="
_pos=$?
if [ ${_pos} -eq 0 ]; then
# not params=value form
IFS=" "
shift
continue
fi
_arg_len=$( strlen ${1} )
_pref=$(( _arg_len - _pos ))
ARG=$( substr --pos=0 --len=${_pos} --str="${1}" )
VAL=$( substr --pos=$(( ${_pos} +2 )) --len=${_pref} --str="${1}" )
IFS=" "
VAL=$( echo ${VAL} | ${TR_CMD} -d '"' ) # strip extra quotes
#echo "V[$VAL]"
# todo: rewrite to substr
#if [ ${double_args} -eq 0 ]; then
# shift
# if [ -n "${T}" ]; then
# continue # value already set, skip
# else
# eval T="\$$ARG"
# fi
#fi
i=$(( i + 1 ))
eval "${ARG}='${VAL}'";
[ ${_tofile} -eq 1 ] && ${CAT_CMD} >> ${CBSD_INIT_SAVE2FILE} <<EOF
${ARG}='${VAL}'
EOF
shift
done
IFS=" "
for i in ${MYARG}; do
unset T
eval T="\$$i"
[ -z "${T}" ] && err 1 "${N1_COLOR}Please set ${CBSD_APP}: ${N2_COLOR}${i}${N0_COLOR}"
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
# $1 - pre-defined return: 1 - yes, 0 - no
getyesno()
{
if [ -n "${2}" ]; then
case "${2}" in
1)
return 0
;;
0)
return 1
;;
esac
fi
if [ "${inter}" = "0" -o -n "${NOINTER}" ]; then
[ "${ALWAYS_YES}" = "1" ] && return 0
[ "${ALWAYS_NO}" = "1" ] && return 1
return 3
fi
if [ -z "${DIALOG}" ]; then
${ECHO} "${BOLD}${1}${N0_COLOR}"
answ=
while [ x$answ = x ]; do
${ECHO} "[${N2_COLOR}yes(1) or no(0)${N0_COLOR}]"
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
}
# getyesnocancel var
# Show $1 strings and get answer (or cancel) in infinity loop
# return 0 when YES/1 and 1 when not
# return 2 when cancel
# 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
# $1 - pre-defined return: 1 - yes, 0 - no
getyesnocancel()
{
if [ -n "${2}" ]; then
case "${2}" in
1)
return 0
;;
0)
return 1
;;
esac
fi
if [ "${inter}" = "0" -o -n "${NOINTER}" ]; then
[ "${ALWAYS_YES}" = "1" ] && return 0
[ "${ALWAYS_NO}" = "1" ] && return 1
return 3
fi
if [ -z "${DIALOG}" ]; then
${ECHO} "${BOLD}${1}${N0_COLOR}"
answ=
while [ x$answ = x ]; do
${ECHO} "[${N2_COLOR}yes(1) or no(0) or cancel(2)${N0_COLOR}]"
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
;;
C*|c*|2)
return 2
;;
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()
{
unset IWM
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} | ${TR_CMD} "," " " )
IPS=""
ST=0
for a in ${I}; do
a=$( is_special_ip ${a} )
# Identify type {inet,inet6}.
iptype ${a}
case $? in
0)
case ${a} in
[Dd][Hh][Cc][Pp])
;;
[Rr][Ee][Aa][Ll][Dd][Hh][Cc][Pp])
;;
*)
${ECHO} "${N1_COLOR}ip not identified: ${N2_COLOR}${a}${N0_COLOR}"
;;
esac
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} | ${TR_CMD} "/" " " )
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
# Update Redis
if [ "${mod_cbsd_redis_enabled}" = "YES" -a -z "${MOD_CBSD_REDIS_DISABLED}" -a -n "${CBSD_APP}" ]; then
end_time=$( ${DATE_CMD} +%s )
diff_time=$(( end_time - st_time ))
cbsdredis publish cbsd_events '{"cmd":"'${CBSD_APP}'", "node":"'${nodename}'", "dest":"'${node}'", "jail":"'${jname}'", "status":2, "msg":"'$1'", "duration":'${diff_time}',"code":'$?'}'
fi
${ECHO} "${N1_COLOR}Error in $1${N0_COLOR}"
if [ -f ${DEBLOG} ]; then
${ECHO} "${N1_COLOR}Guru meditaion: ${N0_COLOR}"
cat ${DEBLOG}
fi
exit 1
fi
[ "$1" = "0" ] && return 0
printf "${N1_COLOR}.${N0_COLOR}"
}
# 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
# -a - what action to observe: 'stop' or 'start'
# -i - timeout interval for cbsd_pwait (default: 1)
# -t - global loop timeout (default: 90)
wait_for_fpid()
{
local ECHO="echo -e"
local A PID
local interval=1
local myaction=
local timeout=90
local ltimeout=0
while getopts "a:i:t:" opt; do
case "${opt}" in
a) myaction="${OPTARG}" ;;
i) interval="${OPTARG}" ;;
t) timeout="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${myaction}" ] && return 0
local myfile="j${myaction}" # jstop and jstart file
while [ 1 ]; do
A=$( /usr/bin/find ${ftmpdir} -type f -depth 1 -maxdepth 1 -name ${myfile}\.\*\.\* | /usr/bin/head -n1 )
[ -z "${A}" ] && break
PID=$( ${CAT_CMD} ${A} )
[ -z "${PID}" ] && break
/bin/ps -p ${PID} > /dev/null 2>&1
[ $? -ne 0 ] && break # No such pid here
${ECHO} "${N1_COLOR}Waiting for ${myaction}ing jail who have pid: ${N2_COLOR}${PID}${N0_COLOR}"
cbsd_pwait --pid=${PID} --timeout=${interval}
ltimeout=$(( ltimeout + interval ))
[ ${ltimeout} -gt ${timeout} ] && break
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()
{
[ -z "$1" ] && return 0 # empty name is valid
echo -n "$1" | ${AWK_CMD} '!/^[a-zA-Z0-9_@%:][-a-zA-Z0-9_@%:]*$/{exit 1}' RS='^$'
}
# 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="${tmpdir}/myhtml.$$"
_fetch=1
else
_fetch=0
fi
_myhelp="${tmpdir}/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}.html" 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
${CAT_CMD} > ${_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
${SED_CMD} -e 's/<[^>]*>//g' ${_myhtml} | ${TR_CMD} -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
}
# global config here:
readconf logger.conf
###
fi