* Ispravak za cp_mail: pokusava se pozvati sendmail wrapper cak i kad ne
[carnet-tools-cn.git] / functions.sh
index 75014f0..24c7cf9 100644 (file)
@@ -1,6 +1,13 @@
+# Initialize "public" CP_ECHO_RETURN variable once
+if [ -z "$_CP_INIT_ECHO_RETURN" ]; then
+  _CP_INIT_ECHO_RETURN=1
+  CP_ECHO_RETURN=""
+fi
+
 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
 cp_get_ifaddr() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local ifaddr interface echo_return
   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
   # in our own way we need to set CP_ECHO_RETURN the way we want it
@@ -36,6 +43,7 @@ cp_get_ifaddr() {
 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
 cp_get_ifmask() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local ifmask interface echo_return
   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
   # in our own way we need to set CP_ECHO_RETURN the way we want it
@@ -70,28 +78,42 @@ cp_get_ifmask() {
 
 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
 cp_get_ifdefault() {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local echo_return
   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
   # in our own way we need to set CP_ECHO_RETURN the way we want it
   # but preserving initial state so it could be used afterwards
   CP_ECHO_RETURN=""
 
-  [ "$CP_SCRIPT_DEBUG" ] && set -vx
   RET=""
   case $1 in
-    dev)  RET=`route -n | awk '/^0.0.0.0/{print $8}'` ;;
-    addr) RET=`route -n | awk '/^0.0.0.0/{print $2}'` ;;
-    *)    echo "cp_get_ifdefault: Argument required (dev or addr)" >&2 ;;
+    dev)
+      RET=`route -n | awk 'BEGIN {m = 2^32}
+                           /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $8; m = $5}}
+                             END {print r}'`
+      ;;
+    addr)
+      RET=`route -n | awk 'BEGIN {m = 2^32}
+                           /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $2; m = $5}}
+                                    END {print r}'`
+      ;;
+    *)
+      echo "cp_get_ifdefault: Argument required (dev or addr)" >&2
+      ;;
   esac
+
   [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
+
   [ -z "$echo_return" ] || echo $RET
   CP_ECHO_RETURN="$echo_return"
-  [ -z "$RET" ] && return 1
+  [ -n "$RET" ] || return 1
 }
 
 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
 cp_get_netaddr() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local netmask ipaddress interface cidr echo_return
   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
   # in our own way we need to set CP_ECHO_RETURN the way we want it
@@ -129,7 +151,7 @@ cp_get_netaddr() {
   if [ "$netmask" = "255.255.255.255" ]; then
     cidr="$ipaddress/32"
   else 
-    cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
+    cidr="`ipcalc -nb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
   fi
   
   RET="$cidr"
@@ -138,17 +160,59 @@ cp_get_netaddr() {
 }
 
 cp_check_and_backup() {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   cp_backup_conffile -r "$@"
 }
 
 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
 cp_backup_conffile() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
-  local file_bak check did
+
+  local dir ext file_bak check did opt_p opt_d
   
-  if [ "x$1" = "x-r" ]; then
-    check=1
-    shift
+  dir=/var/backups
+  ext=.bak
+  
+  while echo "x$1" |grep -q '^x-'; do
+    case "$1" in
+    -r)
+      shift
+      check=1
+      ;;
+    -p)
+      shift
+      opt_p=1
+      ;;
+    -d)
+      shift
+      opt_d=1
+      # Is argument to -d full path or relative?
+      if echo "$1" |grep -q '^/'; then
+        dir="$1"
+      else
+        dir="$dir/$1"
+      fi
+      shift
+      ;;
+    -n)
+      shift
+      ext=
+      ;;
+    esac
+  done
+  if [ "$opt_d" ]; then
+    if [ ! -e "$dir" -a "$opt_p" ]; then
+      mkdir "$dir"
+    fi
+    if [ $? -ne 0 ]; then
+      echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
+      return 3
+    fi
+  fi
+  if [ ! -d "$dir" ]; then
+    echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
+    return 3
   fi
   if [ -z "$1" ]; then
     return 1
@@ -158,9 +222,9 @@ cp_backup_conffile() {
     return 2
   fi
   if [ -z "$2" ]; then
-    file_bak="/var/backups/`basename $1`.bak"
+    file_bak="$dir/`basename $1`$ext"
   else
-    file_bak="/var/backups/`basename $2`.bak"
+    file_bak="$dir/`basename $2`$ext"
   fi
 
   if [ ! -f "$file_bak" ]; then
@@ -173,15 +237,18 @@ cp_backup_conffile() {
       did=1
     fi
   fi
-  
-  if [ "$check" -a "$did" ]; then
-    return 0
-  else
+
+  [ -z "$check" ] && return 0
+
+  if [ "$check" -a ! "$did" ]; then
     return 3
+  else
+    return 0
   fi
 }
 
 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
+# modified by ico, Mon,  6 Jun 2005 11:58:08 +0200
 # A sed wrapper, to use instead of perl -pi -e
 #  - relatively safe in-place s///g
 #  - takes care of symlinks and ownership
@@ -189,7 +256,11 @@ cp_backup_conffile() {
 #
 cp_check_and_sed() {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local s sedcmd grepret ret i
+  if [ -z "$3" ]; then
+    return 1
+  fi
   s="$1"
   shift
   sedcmd="$1"
@@ -204,6 +275,11 @@ cp_check_and_sed() {
     fi
     [ -h "$i" ]        && i=$(readlink -f "$i")
     sed "$sedcmd" "$i" > "$i.dpkg-tmp"
+    if [ $? -ne 0 ]; then
+      rm "$i.dpkg-tmp"
+      echo "cp_check_and_sed: Problem with sed" 1>&2
+      return 5
+    fi
     if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
       cp_mv "$i.dpkg-tmp" "$i"
     else
@@ -211,13 +287,14 @@ cp_check_and_sed() {
     fi
     ret=0
   done
-  [ "$ret" -eq 2 -a "$grepret"] && ret=1 || true
+  [ "$ret" -eq 2 -a "$grepret" ] && ret=1
   return $ret
 }
 
 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
 cp_echo () {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
     shift
   else   
@@ -233,20 +310,25 @@ cp_echo () {
 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
 cp_mail () {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local pkg version quiet
   if [ "x$1" = "x-q" ]; then
     quiet=1
     shift
   fi
-  [ -n "$1" ] && pkg="$1"       # barf if no package
-  [ -n "$2" ] && version=" $2" || true
+  if [ -n "$1" ]; then
+    pkg="$1"
+  else
+    return 1 # must have at least the package name as argument
+  fi
+  [ -n "$2" ] && version=" $2"
   if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
     [ -n "$quiet" ] || echo "Mailing upgrade output to root."
     CP_NOTICE="From: $pkg postinst script <root>
 To: root
 Subject: $pkg$version package install log
-Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
+Date: $(LC_ALL=C /bin/date --rfc-2822)
+
 Hello!
 
 The $pkg package has been successfully installed on your computer.
@@ -255,15 +337,18 @@ is included below.
 
 ----------
 ${CP_NOTICE}----------"
-  
-    echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
-    sleep 1
+
+    if [ -x /usr/sbin/sendmail ]; then
+      echo "$CP_NOTICE" | /usr/sbin/sendmail -t -oi &
+      sleep 1
+    fi
   fi
 }
 
 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
 cp_mv () {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local new old
   if [ -z "$2" ]; then
     return 1
@@ -273,9 +358,11 @@ cp_mv () {
   if [ -e "$old" ]; then
     chown --reference "$old" "$new"
     chmod --reference "$old" "$new"
+    cp -pf "$old" "$old.cn-old"
   fi
   mv "$new" "$old"
 }
+
 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
 cp_yes_no () {
   [ "$CP_SCRIPT_DEBUG" ] && set -vx
@@ -294,6 +381,14 @@ cp_yes_no () {
   echo -n "$prompt (y)es/(n)o: "
   read answer
   case "$answer" in
+    Y*)
+      echo
+      RET=y
+      ;;
+    N*)
+      echo
+      RET=n
+      ;;
     y*)
       echo
       RET=y
@@ -305,7 +400,7 @@ cp_yes_no () {
     *)
       echo
       echo "Please read the message and choose y or n"
-      cp_yes_no
+      cp_yes_no "$prompt"
       ;;
   esac
 }
@@ -319,6 +414,8 @@ cp_yes_no () {
 #     instead of some gibberish error message that our script does
 #     not expect
 cp_db_get() {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local var
   var="$1"
   
@@ -343,14 +440,14 @@ cp_db_get() {
 
 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
 cp_get_ldap_suffix() {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local echo_return
   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
   # in our own way we need to set CP_ECHO_RETURN the way we want it
   # but preserving initial state so it could be used afterwards
   CP_ECHO_RETURN=""
 
-  [ "$CP_SCRIPT_DEBUG" ] && set -vx
-
   if [ ! -f /etc/ldap/slapd.conf ]; then
     echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
     return 2
@@ -373,14 +470,14 @@ cp_get_ldap_suffix() {
 
 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
 cp_get_ldap_realm() {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
   local echo_return
   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
   # in our own way we need to set CP_ECHO_RETURN the way we want it
   # but preserving initial state so it could be used afterwards
   CP_ECHO_RETURN=""
 
-  [ "$CP_SCRIPT_DEBUG" ] && set -vx
-
   if [ ! -f /etc/ldap/slapd.conf ]; then
     echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
     return 2
@@ -390,6 +487,83 @@ cp_get_ldap_realm() {
   RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
 
   [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
-  [ -z "$echo_return" ] || echo $RET
-  CP_ECHO_RETURN="$echo_return"
+}
+
+# if fqdn is name.dom3.dom2.dom1.hr, check if this host is MX for
+# either dom3.dom2.dom1.hr, dom2.dom1.hr or dom1.hr and dump highest level
+# domain on stdout
+cp_get_mx_domain() {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+
+  local domains d host
+
+  host=$(hostname -f)
+  RET="$host"
+  if ! echo "$host" | grep -q '\.'; then
+    return
+  fi
+  if [ ! -x /usr/bin/host ]; then
+    # no host command
+    return
+  fi
+  domains=$(/bin/hostname -f | awk -F. '
+    {
+      for (i=2; i<NF; i++) { 
+        for (j=i; j<NF; j++) {
+          printf "%s", $(j)"."
+        };
+        print $NF
+      }
+    }'   )
+  for d in $domains
+  do
+    mxes=$(/usr/bin/host -t mx $d)
+    # handle output of both /usr/bin/host providers
+    mxes=$(echo "$mxes"|\
+           awk '/mail is handled by/ || /MX/ {print $NF}'|sed 's/\.$//')
+    if echo "$mxes" |egrep -q "^$host$"; then
+      RET="$d"
+    fi
+  done
+
+  [ -z "$CP_ECHO_RETURN" ] || echo "$RET"
+}
+
+_cp_customize() {
+  [ "$CP_SCRIPT_DEBUG" ] && set -vx
+  # < input functions > output_functions
+  # new-prefix list-of-functions
+  perl -e '
+    $prefix = shift; %keep = (); $output = "";
+    $keep{$_} = 1 for @ARGV;
+    # add dependent functions
+    %deps = (
+      get_ifaddr       => [ "get_ifdefault" ],
+      get_netaddr      => [ "get_ifdefault", "get_ifaddr", "get_ifmask" ],
+      check_and_backup => [ "backup_conffile" ],
+      check_and_sed    => [ "mv" ],
+      get_ldap_realm   => [ "get_ldap_suffix" ],
+    );
+    foreach my $f (keys(%keep)) {
+      if (exists($deps{$f})) {
+        $keep{$_} = 1 for (@{$deps{$f}});
+      }
+    }
+    while (<STDIN>) {
+      if (/^_?cp_(\w+) *\(\) *\{/) {
+        if (! exists($keep{$1})) {
+          while (<STDIN>) { last if /^\}/; } # skip it
+          next;
+        } else {
+          $output .= "\n";
+        }
+      }
+      next if /^\s*(\#|$)/;
+      $output .= $_;
+    }
+    $output =~ s/\b(_?)cp_/$1${prefix}_/sg;
+    $prefix = uc($prefix);
+    $output =~ s/\b(_?)CP_/$1${prefix}_/sg;
+    print $output;
+  ' -- $*
 }