diff options
Diffstat (limited to 'common/scripts/functions.sh')
-rwxr-xr-x | common/scripts/functions.sh | 534 |
1 files changed, 534 insertions, 0 deletions
diff --git a/common/scripts/functions.sh b/common/scripts/functions.sh new file mode 100755 index 00000000..deca8a0a --- /dev/null +++ b/common/scripts/functions.sh @@ -0,0 +1,534 @@ +#!/bin/bash +# +# Wizard +# +# Copyright (C) 2000,2001 Mandrakesoft. +# +# 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 2 +# of the License, or (at your option) any later version. +# See file LICENSE for further informations on licensing terms. +# +# 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, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# Authors: Jerome Dumonteil, Maurizio De Cecco, Enzo Maggi +# icons: Helene Durosini <ln@mandrakesoft.com> +# <corporate@mandrakesoft.com> http://www.mandrakesoft.com + +# include for a few functions used in various scripts +# +# - this should be loaded at the beginning of every wizard - +# (wiz load it for you) + + +#[ "${wiz_loaded_function}" = "yes" ] && return 0 + + +# assume not multi line values +#(sed -e :a -e '/\\$/N; s/\\\n//; ta') +#debug function +# $1 : message +echo_debug () { +([ -n "${DEBUG_VAL}" ] && echo $1 >> ${DEBUG_VAL})||: +} + + +# back up the specified file +bck_file(){ +# $1 file +fic=$1 +if [ -f ${fic} ]; then + j=3 + for i in 2 1 ; do + [ -f ${fic}.mdk_orig.$i ] && mv -f ${fic}.mdk_orig.$i ${fic}.mdk_orig.$j + j=$i + done + cp -f ${fic} ${fic}.mdk_orig.1 +fi +} + + +rm_val(){ + grep -v -E "^[[:space:]]*$1[[:space:]]*=" +} + + +# retrive a value from config file +get_val(){ +# $1 : config file +# $2 : variable +if [ ! -f "${1}" -o -z "$2" ]; then + return 1 +fi +sed -n "s£^[[:space:]]*$2[[:space:]]*=[[:space:]]*\(.*\)[[:space:]]*$£\1£p" $1 2>/dev/null\ +|sed 's£^\"\(.*\)\"$£\1£' +} + +# retrive a value from config file, other version without "=" +get_val2(){ +# $1 : config file +# $2 : variable +if [ ! -f "${1}" -o -z "$2" ]; then + return 1 +fi +sed -n "s£^[[:space:]]*$2[[:space:]]\+\(.*\)[[:space:]]*$£\1£p" $1 2>/dev/null\ +|sed 's£^\"\(.*\)\"$£\1£' +} + + + +comment_val(){ +sed "s/^[[:space:]]*$1[[:space:]]*=.*$/#&/" +} + + +mod_val(){ +sed -e '\£^[[:space:]]*'"$1"'[[:space:]]*=.*$£{ +i \ +# removed by mdk_serv script on '"$(date)"' +s££#&£ +a \ +'"$1=\"$2\""' +} +' +} + +mod_val2(){ +# same as chg_val, but without "=" symbol neither "" +sed -e '\£^[[:space:]]*'"$1"'[[:space:]]\+.*$£{ +i \ +# removed by mdk_serv script on '"$(date)"' +s££#&£ +a \ +'"$1 $2"' +} +' +} + + +chg_val(){ +# $1 : config file +# $2 : variable +# $3 : new value +# $4 : "s" : silent mode + +[ -f "$1" ] || exit 1 + +#if [ -n "`grep -E \"^\[\[:space:\]\]*$2\[\[:space:\]\]*=\" \"$1\"`" ]; then + +t=`grep -c -E "^[[:space:]]*$2[[:space:]]*=" $1` + +if [ $t -ge 1 ]; then + TMPFILE=`mktemp /tmp/temp.XXXXXX` || exit 1 + cat "$1" > ${TMPFILE} + (cat ${TMPFILE}|mod_val "$2" "$3" > "$1") && rm -f ${TMPFILE} +else + if [ "$4" = "s" ]; then + echo -e "\ +$2=\"$3\"\n\ +" >> "$1" + else + echo -e "\ +# added by mdk_serv script on $(date)\n\ +$2=\"$3\"\n\ +" >> "$1" + fi +fi +} + +chg_val2(){ +# same as chg_val, but without "=" symbol neither "" + +# $1 : config file +# $2 : variable +# $3 : new value +# $4 : "s" : silent mode + +[ -f "$1" ] || exit 1 + +t=`grep -c -E "^[[:space:]]*$2[[:space:]]+" $1` +if [ $t -ge 1 ]; then + TMPFILE=`mktemp /tmp/temp.XXXXXX` || exit 1 + cat "$1" > ${TMPFILE} + (cat ${TMPFILE}|mod_val2 "$2" "$3" > "$1") && rm -f ${TMPFILE} +else + if [ "$4" = "s" ]; then + echo -e "\ +$2 $3\n\ +" >> "$1" + else + echo -e "\ +# added by mdk_serv script on $(date)\n\ +$2 $3\n\ +" >> "$1" + fi +fi +} + + +# get_var $1 : function to retrieve stored value of wiz_variables +# - if $2 is provided, returns $2 (ok, it's a hack) +# - if the stored value is empty, returns the current one (in memory) +# - this function may return a null string if value is really not found - +get_var(){ +# $1 : name of the variable +# $2 : forcing value + +[ -z "$1" ] && return 1 + +if [ -n "$2" ]; then + echo "$2" + return 0 +fi + +file=/etc/sysconfig/mdk_serv + +case "$1" in + wiz_host_name) + t=`get_val ${file} wiz_host_name` + if [ -z "$t" ]; then + echo ${wiz_host_name} + else + echo "$t" + fi + ;; + wiz_domain_name) + t=`get_val ${file} wiz_domain_name` + if [ -z "$t" ]; then + echo ${wiz_domain_name} + else + echo "$t" + fi + ;; + wiz_device) + t=`get_val ${file} wiz_device` + if [ -z "$t" ]; then + echo ${wiz_device} + else + echo "$t" + fi + ;; + wiz_ext_dns1) + t=`get_val ${file} wiz_ext_dns1` + if [ -z "$t" ]; then + echo ${wiz_ext_dns1} + else + echo "$t" + fi + ;; + wiz_ext_dns2) + t=`get_val ${file} wiz_ext_dns2` + if [ -z "$t" ]; then + echo ${wiz_ext_dns2} + else + echo "$t" + fi + ;; + wiz_web_internal) + t=`get_val ${file} wiz_web_internal` + if [ -z "$t" ]; then + echo ${wiz_web_internal} + else + echo "$t" + fi + ;; + wiz_web_external) + t=`get_val ${file} wiz_web_external` + if [ -z "$t" ]; then + echo ${wiz_web_external} + else + echo "$t" + fi + ;; + wiz_ftp_internal) + t=`get_val ${file} wiz_ftp_internal` + if [ -z "$t" ]; then + echo ${wiz_ftp_internal} + else + echo "$t" + fi + ;; + wiz_ftp_external) + t=`get_val ${file} wiz_ftp_external` + if [ -z "$t" ]; then + echo ${wiz_ftp_external} + else + echo "$t" + fi + ;; + wiz_news_freq) + t=`get_val ${file} wiz_news_freq` + if [ -z "$t" ]; then + echo ${wiz_news_freq} + else + echo "$t" + fi + ;; + wiz_news_server) + t=`get_val ${file} wiz_news_server` + if [ -z "$t" ]; then + echo ${wiz_news_server} + else + echo "$t" + fi + ;; + wiz_banner) + t=`get_val ${file} wiz_banner` + if [ -z "$t" ]; then + echo ${wiz_banner} + else + echo "$t" + fi + ;; + wiz_workgroup) + t=`get_val ${file} wiz_workgroup` + if [ -z "$t" ]; then + echo ${wiz_workgroup} + else + echo "$t" + fi + ;; + wiz_do_printer_sharing) + t=`get_val ${file} wiz_do_printer_sharing` + if [ -z "$t" ]; then + echo ${wiz_do_printer_sharing} + else + echo "$t" + fi + ;; + wiz_do_file_sharing) + t=`get_val ${file} wiz_do_file_sharing` + if [ -z "$t" ]; then + echo ${wiz_do_file_sharing} + else + echo "$t" + fi + ;; + wiz_ip_net) + wdevice=`get_val ${file} wiz_device` + nfile="/etc/sysconfig/network-scripts/ifcfg-${wdevice}" + t=`get_val ${nfile} NETWORK` + if [ -z "$t" ]; then + echo ${wiz_ip_net} + else + echo "$t" + fi + ;; + wiz_ip_netmask) + wdevice=`get_val ${file} wiz_device` + nfile="/etc/sysconfig/network-scripts/ifcfg-${wdevice}" + t=`get_val ${nfile} NETMASK` + if [ -z "$t" ]; then + echo ${wiz_ip_netmask} + else + echo "$t" + fi + ;; + wiz_ip_server) + wdevice=`get_val ${file} wiz_device` + nfile="/etc/sysconfig/network-scripts/ifcfg-${wdevice}" + t=`get_val ${nfile} IPADDR` + if [ -z "$t" ]; then + echo ${wiz_ip_server} + else + echo "$t" + fi + ;; + wiz_ip_range1) + wdevice=`get_val ${file} wiz_device` + nfile="/etc/sysconfig/network-scripts/ifcfg-${wdevice}" + twnet=`get_val ${nfile} NETWORK` + nfile=/etc/dhcpd.conf + rnge= + if [ -f ${nfile} ]; then + rnge=`sed -n -e 's/^[[:space:]]*range[[:space:]]*\([1-9\.]*\)[[:space:]].*$/\1/p' ${nfile}` + fi + [ "${rnge%.*}" == "${twnet%.*}" ] || rnge="" + if [ -z "$rnge" ]; then + echo ${wiz_ip_range1} + else + echo "$rnge" + fi + ;; + wiz_ip_range2) + wdevice=`get_val ${file} wiz_device` + nfile="/etc/sysconfig/network-scripts/ifcfg-${wdevice}" + twnet=`get_val ${nfile} NETWORK` + nfile=/etc/dhcpd.conf + rnge= + if [ -f ${nfile} ]; then + rnge=`sed -n -e 's/^[[:space:]]*range[[:space:]]*[1-9\.]*[[:space:]]*\([1-9\.]*\)[^1-9\.].*$/\1/p' ${nfile}` + fi + [ "${rnge%.*}" == "${twnet%.*}" ] || rnge="" + if [ -z "$rnge" ]; then + echo ${wiz_ip_range2} + else + echo "$rnge" + fi + ;; + wiz_ext_device) + t=`get_val ${file} wiz_ext_device` + if [ -z "$t" ]; then + echo ${wiz_ext_device} + else + echo "$t" + fi + ;; + wiz_extn_device) + t=`get_val /etc/sysconfig/network GATEWAYDEV` + if [ -z "$t" ]; then + echo ${wiz_extn_device} + else + echo "$t" + fi + ;; + wiz_extn_gateway) + t=`get_val /etc/sysconfig/network GATEWAY` + if [ -z "$t" ]; then + echo ${wiz_extn_gateway} + else + echo "$t" + fi + ;; + wiz_firewall_level) + t=`get_val ${file} wiz_firewall_level` + if [ -z "$t" ]; then + echo ${wiz_firewall_level} + else + echo "$t" + fi + ;; + wiz_ext_mail_relay) + postconf -h relayhost + ;; + wiz_mail_masquerade) + nfile=/etc/postfix/canonical + wdname=`get_val ${file} wiz_domain_name` + t="" + if [ -f ${nfile} ]; then + t=`sed -n -e 's/^[[:space:]]*@'"${wdname}"'[[:space:]]*@\([^[:space:]]*\)[[:space:]]*$/\1/p' ${nfile}` + fi + if [ -z "$t" -o -z "wdname" ]; then + echo ${wiz_mail_masquerade} + else + echo "$t" + fi + ;; + wiz_timezone) + nfile=/etc/sysconfig/clock + t=`get_val ${nfile} ZONE` + if [ -z "$t" ]; then + echo ${wiz_timezone} + else + echo "$t" + fi + ;; + wiz_squid_port) + nfile=/etc/squid/squid.conf + t=`get_val2 ${nfile} http_port` + if [ -z "$t" ]; then + echo ${wiz_squid_port} + else + echo "$t" + fi + ;; + wiz_squid_mem) + nfile=/etc/squid/squid.conf + t=`get_val2 ${nfile} cache_mem` + if [ -z "$t" ]; then + echo ${wiz_squid_mem} + else + t1=`echo $t|awk '{print $1}'` + t2=`echo $t|awk '{print $2}'` + if [ "$t2" == "MB" ]; then + echo $t1 + else + echo ${wiz_squid_mem} + fi + fi + ;; + wiz_squid_disk) + nfile=/etc/squid/squid.conf + # nota : only one dir : wiz_squid_defdir + export wiz_squid_defdir=`get_var wiz_squid_defdir` + t=`sed -n "s£^[[:space:]]*cache_dir[[:space:]]\+[a-z]\+[[:space:]]\+$wiz_squid_defdir[[:space:]]\+\([0-9]\+\)[[:space:]]*.*$£\1£p" $nfile 2>/dev/null` + if [ -z "$t" ]; then + echo ${wiz_squid_disk} + else + echo $t + fi + ;; + wiz_squid_level) + t=`get_val ${file} wiz_squid_level` + if [ -z "$t" ]; then + echo ${wiz_squid_level} + else + echo "$t" + fi + ;; + wiz_squid_defdir) + t=`get_val ${file} wiz_squid_defdir` + if [ -z "$t" ]; then + [ -z "${wiz_squid_defdir}" ] && wiz_squid_defdir="/var/spool/squid" + echo ${wiz_squid_defdir} + else + echo "$t" + fi + ;; + wiz_squid_mynetw) + nfile=/etc/squid/squid.conf + t=`get_val2 ${nfile} "acl mynetwork src"` + if [ -z "$t" ]; then + echo `get_var wiz_ip_net`"/"`get_var wiz_ip_netmask` + else + echo $t + fi + ;; + wiz_squid_cachepeer) + nfile=/etc/squid/squid.conf + t=`get_val2 ${nfile} "cache_peer"` + if [ -z "$t" ]; then + echo ${wiz_squid_cachepeer} + else + echo $t|awk '{print $1}' + fi + ;; + wiz_squid_peerport) + nfile=/etc/squid/squid.conf + t=`get_val2 ${nfile} "cache_peer"` + if [ -z "$t" ]; then + echo ${wiz_squid_peerport} + else + echo $t|awk '{print $3}' + fi + ;; + + *) + return 1 + ;; +esac +} + + + + +export -f echo_debug +export -f rm_val +export -f get_val +export -f get_val2 +export -f mod_val +export -f mod_val2 +export -f chg_val +export -f chg_val2 +export -f bck_file + +export -f get_var +#export wiz_loaded_function=yes |