#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2014 IPFire Network Development Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ############################################################################### settings_read() { local file="${1}" assert isset file shift local valid_keys local ignore_superfluous_settings="false" local arg while read -r arg; do case "${arg}" in --ignore-superfluous-settings) ignore_superfluous_settings="true" ;; *) list_append valid_keys "${arg}" ;; esac done <<< "$(args "$@")" if [ -d "${file}" ]; then error "Not a configuration file: '${file}'" return ${EXIT_ERROR} fi # Exit if the file cannot be read. [ -r "${file}" ] || return ${EXIT_ERROR} local line key val while read -r line; do case "${line}" in *=*) key=$(cli_get_key ${line}) # If valid keys is set, key must be in the list. if [ -n "${valid_keys}" ]; then list_match ${key} ${valid_keys} || continue fi val=$(cli_get_val "${line}") val=$(settings_strip ${val}) # Assign variable. printf -v ${key} "%s" "${val}" ;; *) log DEBUG "Invalid line in configuration file: ${line}" ;; esac done < ${file} } settings_read_array() { local file=${1} assert isset file shift local array=${1} assert isset array shift local valid_keys=$@ # Exit if the file cannot be read. [ -r "${file}" ] || return ${EXIT_ERROR} local line key val while read -r line; do case "${line}" in *=*) key=$(cli_get_key ${line}) # If valid_keys is set, key must be in the list. if [ -n "${valid_keys}" ]; then if ! list_match ${key} ${valid_keys}; then log DEBUG "Ignoring configuration setting: ${key}" continue fi fi val=$(cli_get_val "${line}") val=$(settings_strip ${val}) # Assign variable. printf -v "${array}["${key}"]" "%s" "${val}" ;; *) log DEBUG "Invalid line in configuration file: ${line}" ;; esac done < ${file} } # Strip leading and trailing "s. settings_strip() { local var="$@" # Do nothing for strings that contain spaces. #if contains_spaces ${var}; then # print "${var}" # return ${EXIT_OK} #fi unquote "${var}" } settings_write() { local settings_file="${1}" assert isset settings_file shift local check_func local arg while read arg; do case "${arg}" in --check=*) check_func="$(cli_get_val "${arg}")" ;; # Stop argument processing when reaching the first # configuration parameter *) break ;; esac shift done <<< "$(args "$@")" # Check if all values to be written are sane if isset check_func && ! settings_check "${check_func}"; then return ${EXIT_ERROR} fi if ! make_parent_directory "${settings_file}"; then return ${EXIT_ERROR} fi log DEBUG "Writing settings file '${settings_file}'" local param for param in $(list_sort "$@"); do echo "${param}=\"${!param}\"" done > ${settings_file} } settings_remove() { local settings_file="${1}" local abspath="$(readlink -e "${settings_file}")" if [ "${settings_file}" != "${abspath}" ]; then log ERROR "Can only handle absolute paths" return ${EXIT_ERROR} fi file_delete "${settings_file}" } settings_print() { local param for param in $(list_sort "$@"); do printf "%-32s = %s\n" "${param}" "${!param}" done } settings_check() { local check_func="${1}" # Execute the check function "${check_func}" local ret="${?}" case "${ret}" in # OK ${EXIT_OK}|${EXIT_TRUE}) log DEBUG "Configuration check succeeded." return ${EXIT_TRUE} ;; # Error ${EXIT_ERROR}|${EXIT_FALSE}) log CRITICAL "Configuration check failed. No settings have been written." return ${EXIT_FALSE} ;; # Command not found ${EXIT_COMMAND_NOT_FOUND}) log CRITICAL "Configuration check function '${check_func}' was not found." return ${EXIT_FALSE} ;; esac log CRITICAL "Unhandled exit code for '${check_func}': ${ret}" return ${EXIT_ERROR} } settings_set() { while [ $# -gt 0 ]; do case "${1}" in *=*) local key=$(cli_get_key "${1}") local val=$(cli_get_val "${1}") log INFO "Setting configuration option '${key}=${val}'". printf -v ${key} "%s" "${val}" ;; *) warning "Invalid parameter given: ${1}" ;; esac shift done } network_settings_read() { local options="${NETWORK_SETTINGS_FILE_PARAMS}" # If the DEBUG variable has already been set, # don't overwrite it. if [ -n "${DEBUG}" ]; then list_remove options DEBUG fi settings_read "${NETWORK_SETTINGS_FILE}" ${options} } network_settings_write() { settings_write "${NETWORK_SETTINGS_FILE}" ${NETWORK_SETTINGS_FILE_PARAMS} } network_settings_set() { # Process any settings that require immediate actin while [ $# -gt 0 ]; do local arg=${1} shift case "${arg}" in *=*) local key=$(cli_get_key "${arg}") local val=$(cli_get_val "${arg}") case "${key}" in DNS_RANDOMIZE|DNS_SEARCH_DOMAIN|DNS_USE_LOCAL_RESOLVER) dns_generate_resolvconf ;; WIRELESS_REGULATORY_DOMAIN) if ! wireless_valid_reg_domain "${val}"; then warning "Ignoring invalid wireless regulatory domain: ${val}" continue fi if ! wireless_set_reg_domain "${val}"; then error "Error setting wireless regulatory domain: ${val}" fi ;; esac ;; esac # Save setting settings_set ${arg} done return ${EXIT_OK} } network_settings_print() { settings_print ${NETWORK_SETTINGS_FILE_PARAMS} } network_settings_list() { print "${NETWORK_SETTINGS_FILE_PARAMS}" } firewall_settings_read() { settings_read "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS[*]}" } firewall_settings_write() { settings_write "${FIREWALL_SETTINGS_FILE}" "${FIREWALL_SETTINGS[*]}" } firewall_settings_print() { settings_print "${FIREWALL_SETTINGS[*]}" }