]>
git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.util
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 # A simple print statement
26 printf -- "${fmt}\n" "$@"
40 # The args() function takes a number of arguments like
41 # var1="abc d" var2="abc" var3="abcd e"
42 # and splits them into several arguments, devided by newline
44 echo "$@" |
xargs printf "%s\n"
50 if [ "${var:0:1}" = "\"" ]; then
54 local last
=$
(( ${#var} - 1 ))
55 if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
69 # remove leading whitespace characters
70 value="${value#"${value%%[![:space:]]*}"}"
72 # remove trailing whitespace characters
73 value="${value%"${value##*[![:space:]]}"}"
78 # Print a pretty error message
80 echo -e " ${CLR_RED_B}ERROR
${CLR_RESET} : $@
" >&2
87 # Print a pretty warn message
89 echo -e " ${CLR_YELLOW_B}WARNING
${CLR_RESET}: $@
" >&2
96 # Speedup function to avoid a call of the basename binary
110 printf -v "${key}" "${format}" "$@
"
120 for unit in ${units}; do
124 if [ ${m} -gt 0 ]; then
125 ret="${m}${unit} ${ret}"
150 if ! isinteger arg; then
154 # Convert hours and minutes into seconds
157 arg=$(( ${arg} * 3600 ))
160 arg=$(( ${arg} * 60 ))
165 ret=$(( ${ret} + ${arg} ))
176 format "${key}" "%s
" "$@
"
183 [ -r "${file}" ] || return ${EXIT_ERROR}
193 if [ -e "${file}" ] && [ ! -w "${file}" ]; then
194 log ERROR "${file}: No such
file"
198 print "%s
" "$@
" >> ${file} 2>/dev/null
204 [ -e "${file}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
207 file_is_newer_than
() {
211 local age1
=$
(file_get_age
"${file1}")
212 local age2
=$
(file_get_age
"${file2}")
214 if [ ${age1} -gt ${age2} ]; then
224 if [ -e "${file}" ]; then
225 stat
--format="%Y" "${file}"
235 local dirname="$(dirname "${path}")"
236 mkdir
-p "${dirname}"
242 list_match
"${!param}" yes on true
1
246 local b
="$(random 12)"
248 # Remove multicast bit
249 # and set address is software assigned
250 local first_byte
=$
(( 0x
${b:0:2} & 0xfe ))
251 first_byte
=$
(( ${first_byte} |
0x02 ))
254 printf -v output
"%02x" "${first_byte}"
256 output
="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}"
258 # Check if output is valid
259 assert mac_is_valid
"${output}"
268 # Remove all colons and make the rest lowercase.
273 if [ "${#mac}" = "12" ]; then
274 # Add colons (:) to mac address
277 for i
in 2 4 6 8 10; do
278 output
="${output}:${mac:${i}:2}"
284 assert mac_is_valid
${output}
292 [[ ${mac} =~ ^
([0-9a-f]{2}\
:){5}[0-9a-f]{2}$
]]
296 echo $
(</proc
/sys
/kernel
/random
/uuid
)
302 if [ ${val} -lt 0 ]; then
315 local length
="${1:-8}"
318 while [ ${#random} -lt ${length} ]; do
319 random
="${random}$(rand)"
322 echo "${random:0:${length}}"
335 list_match
"${var}" "$@"
341 isoneof
${var} 0 1 no
yes on off true false
347 [[ ${var} =~ ^
[0-9]+$
]]
368 [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ]
371 [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ]
374 error
"${proto} is not a valid proto"
384 error_log
"Backtrace (most recent call in first line):"
387 for i
in $
(seq ${start} ${#BASH_SOURCE[*]}); do
388 [ -z "${FUNCNAME[${i}]}" ] && continue
390 # Print called binary with arguments.
391 if [ "${FUNCNAME[${i}]}" == "main" ]; then
392 local args
="$(list_reverse ${BASH_ARGV[*]})"
393 printf -v source "%20s" "$0"
394 error_log
" ${source} ${args}"
398 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
399 error_log
" $(printf "%20s
" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
406 if ! ${assertion}; then
407 error_log "Assertion '${assertion}' failed."
409 exit ${EXIT_ERROR_ASSERT}
415 # This function checks, if the given argument is an assert error
416 # exit code. If this is the case, the script will halt immediately.
417 assert_check_retval() {
420 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
421 exit ${EXIT_ERROR_ASSERT}
427 # This function executes the given command and inverses the return code
431 ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
437 log DEBUG "Running command: ${cmd}"
440 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
441 LOG_FACILITY="${LOG_FACILITY}" \
445 #log DEBUG "Returned with code '${ret}'"
447 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
448 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
449 exit ${EXIT_ERROR_ASSERT}
458 log DEBUG "Running command: ${cmd}"
463 log DEBUG "Returned with code '${ret}'"
477 log DEBUG "Exec'ing
command: ${cmd}"
481 log ERROR "Could not exec-ute
: ${cmd}"
485 cmd_not_implemented() {
486 assert false "not implemented
"
489 # Runs a command in a clean environment so that no confidential information
490 # is leaked to any untrusted commands.
491 cmd_clean_environment() {
494 log DEBUG "Running
command in a clean environment
: ${cmd}"
498 log DEBUG "Returned with code
'${ret}'"
502 # Executes the given command in background
507 # Prints the PID of the process that was started last
508 cmd_background_get_pid() {
512 cmd_background_result() {
518 # Increase security of the read command
524 if [ $# -eq 2 ]; then
525 eval echo {${1}..${2}}
526 elif [ $# -eq 3 ]; then
527 eval echo {${1}..${3}..${2}}
532 eval echo {0..$(( ${1} - 1 ))}
549 # Prints the number of seconds since epoch.
559 for unit in s m h d w; do
572 [ ${value} -lt ${limit} ] && break
574 value=$(( ${value} / ${limit} ))
577 echo "${value}${unit}"
585 for unit in B k M G T; do
586 [ ${value} -lt ${limit} ] && break
587 value=$(( ${value} / ${limit} ))
590 echo "${value}${unit}"
596 if ! grep -q "^
${module}" /proc/modules; then
597 log DEBUG "Loading module
'${module}'.
"
605 if [ -n "$
(type -p ${binary})" ]; then
613 local function="${1}"
615 if [ "$
(type -t "${function}")" = "function" ]; then
625 if ! isinteger process; then
626 process=$(pidof ${process})
631 for pid in ${process}; do
633 [ -d "/proc
/${pid}" ] || break
644 if [ "${hex:0:2}" != "0x
" ]; then
648 printf "%d
\n" "${hex}"
654 [ ${char} -lt 256 ] || return ${EXIT_ERROR}
656 printf "\\$
(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n"
660 LC_CTYPE="C
" printf "%d
\n" "'${1}"
667 network_is_running() {
668 # Check, if the network service is running.
669 service_is_active network
676 local var2=${var// /}
678 if [ ${#var} -ne ${#var2} ]; then
689 [[ ${string} =~ ${match} ]] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
696 while [ ${pos} -lt ${#string} ]; do
697 print "${string:${pos}:1}"
698 pos=$(( ${pos} + 1 ))
709 while [ ${pos} -lt ${#string} ]; do
710 output="${string:${pos}:1}${output}"
711 pos=$(( ${pos} + 1 ))
724 for i in 7 6 5 4 3 2 1; do
727 if [ $(( ${number} / ${div} )) -eq 1 ]; then
732 number="$(( ${number} % ${div} ))"
735 if [ $(( ${number} % 2 )) -eq 1 ]; then
749 while [ ${pos} -lt ${#string} ]; do
750 char="${string:${pos}:1}"
751 pos=$(( ${pos} + 1 ))
753 number=$(( ${number} << 1 ))
758 number=$(( ${number} + 1 ))
761 assert false "Invalid character: ${char}"
771 local dec="$(ord "${1}")"
777 local dec="$(bin2dec "$@")"
783 local dec="$(bin2dec "$@")"
789 local dec="$(hex2dec "$@")"
797 # Prepend 0x if necessary.
798 [ "${hex:0:2}" = "0x" ] || hex="0x${hex}"
800 printf "%d\n" "${hex}"
804 printf "%02x\n" "${1}"
807 # This function just copy config files
814 # Check if we can read from the source
815 if [ ! -r "${src}" ]; then
816 log ERROR "Cannot read ${src}"
820 # Check if ${dst} is a directory
821 if [ -d "${dst}" ]; then
822 log ERROR "${dst} is a directory"
826 if ! fread "${src}" > "${dst}"; then
827 log ERROR "Could not copy data from ${src} to ${dst}"