]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.util
pppoe-server: Remove line to enable kernel mode
[people/ms/network.git] / src / functions / functions.util
index d1e2a92b283490d5eb46854cc447704171808b16..58afa938a7249deb33dccf83a2f117802537d53c 100644 (file)
@@ -134,7 +134,7 @@ parse_time() {
        local ret=0
 
        local arg
-       for arg in $@; do
+       for arg in "$@"; do
                local unit
 
                case "${arg}" in
@@ -248,11 +248,27 @@ file_get_age() {
        return ${EXIT_ERROR}
 }
 
-make_parent_dir() {
+make_directory() {
        local path="${1}"
 
-       local dirname="$(dirname "${path}")"
-       mkdir -p "${dirname}"
+       # Exit silently when directory already exists
+       if [ -d "${path}" ]; then
+               return ${EXIT_OK}
+       fi
+
+       if ! mkdir -p "${path}"; then
+               log ERROR "Could not create directory ${path}"
+               return ${EXIT_ERROR}
+       fi
+
+       log DEBUG "Created directory ${path}"
+       return ${EXIT_OK}
+}
+
+make_parent_directory() {
+       local path="${1}"
+
+       make_directory "$(dirname "${path}")"
 }
 
 enabled() {
@@ -261,6 +277,12 @@ enabled() {
        list_match "${!param}" yes on true 1
 }
 
+disabled() {
+       local param="${1}"
+
+       list_match "${!param}" no off false 0
+}
+
 mac_generate() {
        local b="$(random 12)"
 
@@ -311,6 +333,17 @@ mac_is_valid() {
        [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
 }
 
+# Converts the given string to lowercase and returns true if it is a valid FQDN
+fqdn_is_valid() {
+       local fqdn="${1}"
+
+       if grep -qP "^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$" <<< "${fqdn,,}"; then
+               return ${EXIT_TRUE}
+       fi
+
+       return ${EXIT_FALSE}
+}
+
 uuid() {
        echo $(</proc/sys/kernel/random/uuid)
 }
@@ -383,7 +416,7 @@ mtu_is_valid() {
        local mtu=${2}
 
        case ${proto} in
-               ipv4)
+               ethernet|ipv4)
                        [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ]
                        ;;
                ipv6)
@@ -476,18 +509,26 @@ cmd() {
 
        log DEBUG "Running command: ${cmd}"
 
-       if ! ${cmd}; then
-               local ret=$?
-
-               log DEBUG "Returned with code '${ret}'"
-               return ${ret}
-       fi
+       env -i -- \
+               HOME="${HOME}" \
+               PATH="${PATH}" \
+               TERM="${TERM}" \
+               ${cmd}
+       local ret=$?
 
-       return ${EXIT_OK}
+       case "${ret}" in
+               ${EXIT_OK})
+                       return ${EXIT_OK}
+                       ;;
+               *)
+                       log DEBUG "Returned with code '${ret}'"
+                       return ${ret}
+                       ;;
+       esac
 }
 
 cmd_quiet() {
-       cmd $@ &>/dev/null
+       cmd "$@" &>/dev/null
 }
 
 cmd_exec() {
@@ -505,22 +546,9 @@ cmd_not_implemented() {
        assert false "not implemented"
 }
 
-# Runs a command in a clean environment so that no confidential information
-# is leaked to any untrusted commands.
-cmd_clean_environment() {
-       local cmd=$@
-
-       log DEBUG "Running command in a clean environment: ${cmd}"
-       env -i -- ${cmd}
-       local ret=${?}
-
-       log DEBUG "Returned with code '${ret}'"
-       return ${ret}
-}
-
 # Executes the given command in background
 cmd_background() {
-       cmd_quiet $@ &
+       cmd_quiet "$@" &
 }
 
 # Prints the PID of the process that was started last
@@ -536,7 +564,7 @@ cmd_background_result() {
 
 # Increase security of the read command
 read() {
-       builtin read -r $@
+       builtin read -r "$@"
 }
 
 seq() {
@@ -562,7 +590,7 @@ count() {
 }
 
 which() {
-       type -P $@
+       type -P "$@"
 }
 
 # Prints the number of seconds since epoch.
@@ -612,10 +640,13 @@ beautify_bytes() {
 module_load() {
        local module=${1}
 
-       if ! grep -q "^${module}" /proc/modules; then
-               log DEBUG "Loading module '${module}'."
-               modprobe ${module}
+       # Do nothing if the module is already loaded
+       if [ -d "/sys/module/${module//-/_}" ]; then
+               return ${EXIT_OK}
        fi
+
+       log DEBUG "Loading kernel module ${module}"
+       modprobe "${module}"
 }
 
 binary_exists() {
@@ -842,8 +873,34 @@ copy() {
                return ${EXIT_ERROR}
        fi
 
+       # Create destination directory if it doesn't exist, yet
+       if ! make_parent_directory "${dst}"; then
+               return ${EXIT_ERROR}
+       fi
+
        if ! fread "${src}" > "${dst}"; then
                log ERROR "Could not copy data from ${src} to ${dst}"
                return ${EXIT_ERROR}
        fi
 }
+
+normalize() {
+       local string="$@"
+
+       tr -sc [:alnum:] "-" < <(printf "%s" "${string,,}")
+}
+
+get_driver_from_path() {
+       local path="${1}"
+
+       if file_exists "${path}"; then
+               # Resolve symlink
+               local driver="$(readlink "${path}")"
+
+               # Print module name
+               basename "${driver}"
+               return ${EXIT_OK}
+       fi
+
+       return ${EXIT_ERROR}
+}