1 # Initialize "public" CP_ECHO_RETURN variable once
2 if [ -z "$_CP_INIT_ECHO_RETURN" ]; then
7 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
9 [ "$CP_SCRIPT_DEBUG" ] && set -vx
11 local ifaddr interface echo_return
12 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
13 # in our own way we need to set CP_ECHO_RETURN the way we want it
14 # but preserving initial state so it could be used afterwards
18 if [ -z "$interface" ]; then
21 [ -z "$interface" ] && interface=lo
24 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
25 echo "cp_get_ifaddr: $interface: No such interface" 1>&2
26 CP_ECHO_RETURN="$echo_return"
30 #ifaddr="`/sbin/ifconfig $interface | awk '/inet /{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
31 ifaddr="`/sbin/ifconfig $interface | awk '/inet /{ printf($2) }'`"
33 if [ -z $ifaddr ]; then
34 echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
35 CP_ECHO_RETURN="$echo_return"
40 [ -z "$echo_return" ] || echo $RET
41 CP_ECHO_RETURN="$echo_return"
44 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
46 [ "$CP_SCRIPT_DEBUG" ] && set -vx
48 local ifmask interface echo_return
49 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
50 # in our own way we need to set CP_ECHO_RETURN the way we want it
51 # but preserving initial state so it could be used afterwards
55 if [ -z "$interface" ]; then
58 [ -z "$interface" ] && interface=lo
61 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
62 echo "cp_get_ifmask: $interface: No such interface" 1>&2
63 CP_ECHO_RETURN="$echo_return"
67 #ifmask="`/sbin/ifconfig $interface | awk '/Mask/{if($3~/Mask/)a=$3;else if ($4~/Mask/)a=$4;printf ("%s\n", substr(a,index(a,":")+1))}'`"
68 ifmask="`/sbin/ifconfig $interface | awk '/netmask /{ printf($4) }'`"
70 if [ -z "$ifmask" ]; then
71 echo "cp_get_ifmask: $interface: No netmask found" 1>&2
72 CP_ECHO_RETURN="$echo_return"
77 [ -z "$echo_return" ] || echo $RET
78 CP_ECHO_RETURN="$echo_return"
81 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
83 [ "$CP_SCRIPT_DEBUG" ] && set -vx
86 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
87 # in our own way we need to set CP_ECHO_RETURN the way we want it
88 # but preserving initial state so it could be used afterwards
94 RET=`route -n | awk 'BEGIN {m = 2^32}
95 /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $8; m = $5}}
99 RET=`route -n | awk 'BEGIN {m = 2^32}
100 /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $2; m = $5}}
104 echo "cp_get_ifdefault: Argument required (dev or addr)" >&2
108 [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
110 [ -z "$echo_return" ] || echo $RET
111 CP_ECHO_RETURN="$echo_return"
112 [ -n "$RET" ] || return 1
115 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
117 [ "$CP_SCRIPT_DEBUG" ] && set -vx
119 local netmask ipaddress interface cidr echo_return
120 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
121 # in our own way we need to set CP_ECHO_RETURN the way we want it
122 # but preserving initial state so it could be used afterwards
126 if [ -z "$interface" ]; then
129 [ -z "$interface" ] && interface=lo
132 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
133 echo "cp_get_netaddr: $interface: No such interface" 1>&2
134 CP_ECHO_RETURN="$echo_return"
138 cp_get_ifaddr "$interface"
140 if [ -z $ipaddress ]; then
141 echo "cp_get_netaddr: $interface: No IP address found" 1>&2
142 CP_ECHO_RETURN="$echo_return"
145 cp_get_ifmask "$interface"
147 if [ -z $netmask ]; then
148 echo "cp_get_netaddr: $interface: No netmask found" 1>&2
149 CP_ECHO_RETURN="$echo_return"
153 if [ "$netmask" = "255.255.255.255" ]; then
156 cidr="`ipcalc -nb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
160 [ -z "$echo_return" ] || echo $RET
161 CP_ECHO_RETURN="$echo_return"
164 cp_check_and_backup() {
165 [ "$CP_SCRIPT_DEBUG" ] && set -vx
167 cp_backup_conffile -r "$@"
170 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
171 cp_backup_conffile() {
172 [ "$CP_SCRIPT_DEBUG" ] && set -vx
174 local dir ext file_bak check did opt_p opt_d
179 while echo "x$1" |grep -q '^x-'; do
192 # Is argument to -d full path or relative?
193 if echo "$1" |grep -q '^/'; then
206 if [ "$opt_d" ]; then
207 if [ ! -e "$dir" -a "$opt_p" ]; then
210 if [ $? -ne 0 ]; then
211 echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
215 if [ ! -d "$dir" ]; then
216 echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
222 if [ ! -f "$1" ]; then
223 echo "cp_backup_conffile: $1: No such file" 1>&2
227 file_bak="$dir/`basename $1`$ext"
229 file_bak="$dir/`basename $2`$ext"
232 if [ ! -f "$file_bak" ]; then
233 cp -pf "$1" "$file_bak"
236 if ! cmp -s "$1" "$file_bak"; then
237 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
238 cp -pf "$1" "$file_bak"
243 [ -z "$check" ] && return 0
245 if [ "$check" -a ! "$did" ]; then
252 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
253 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
254 # A sed wrapper, to use instead of perl -pi -e
255 # - relatively safe in-place s///g
256 # - takes care of symlinks and ownership
257 # returns true if changed, false if nothing happened
260 [ "$CP_SCRIPT_DEBUG" ] && set -vx
262 local s sedcmd grepret ret i
273 [ -e "$i" ] || continue
274 if ! egrep -q "$s" "$i"; then
278 [ -h "$i" ] && i=$(readlink -f "$i")
279 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
280 if [ $? -ne 0 ]; then
282 echo "cp_check_and_sed: Problem with sed" 1>&2
285 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
286 cp_mv "$i.dpkg-tmp" "$i"
292 [ "$ret" -eq 2 -a "$grepret" ] && ret=1
296 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
298 [ "$CP_SCRIPT_DEBUG" ] && set -vx
300 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
305 CP_NOTICE="$CP_NOTICE$1"
306 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
307 CP_NOTICE="$CP_NOTICE
312 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
314 [ "$CP_SCRIPT_DEBUG" ] && set -vx
316 local pkg version quiet
317 if [ "x$1" = "x-q" ]; then
324 return 1 # must have at least the package name as argument
326 [ -n "$2" ] && version=" $2"
327 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
328 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
329 CP_NOTICE="From: $pkg postinst script <root>
331 Subject: $pkg$version package install log
332 Date: $(LC_ALL=C /bin/date --rfc-2822)
336 The $pkg package has been successfully installed on your computer.
337 For your convenience, a partial output of the last $pkg installation
341 ${CP_NOTICE}----------"
343 if [ -x /usr/sbin/sendmail ]; then
344 echo "$CP_NOTICE" | /usr/sbin/sendmail -t -oi &
350 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
352 [ "$CP_SCRIPT_DEBUG" ] && set -vx
360 if [ -e "$old" ]; then
361 chown --reference "$old" "$new"
362 chmod --reference "$old" "$new"
363 cp -pf "$old" "$old.cn-old"
368 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
370 [ "$CP_SCRIPT_DEBUG" ] && set -vx
373 [ "$prompt" ] || prompt="Are you sure?"
376 # check to prevent user from using this when debconf is active
377 # (it would break communication with its backend - uses stdin/out)
378 if [ "$DEBCONF_REDIR" ]; then
379 echo "cp_yes_no: debconf redirection detected" >&2
383 echo -n "$prompt (y)es/(n)o: "
404 echo "Please read the message and choose y or n"
410 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
412 # db_get seems to return error messages if the requested variable
413 # is not found - we do not find that desirable, and checking $? with
414 # set -e turned on seems to be a bit of a problem
415 # --> that's why we wrap the db_get call to return unset RET variable
416 # instead of some gibberish error message that our script does
419 [ "$CP_SCRIPT_DEBUG" ] && set -vx
424 # check to prevent user from using this before activating debconf
425 # confmodule needs to be loaded before accessing db_* functions
426 if [ -z "$DEBCONF_REDIR" ]; then
427 echo "cp_db_get: debconf not activated!" >&2
431 if ! db_get "$var"; then
436 [0-9][0-9]\ ?*) # other errors
443 # by ico, Wed, 07 Dec 2011 13:30:02 +0100
444 cp_get_ldap_suffix() {
445 [ "$CP_SCRIPT_DEBUG" ] && set -vx
448 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
449 # in our own way we need to set CP_ECHO_RETURN the way we want it
450 # but preserving initial state so it could be used afterwards
455 if [ -d /etc/ldap/slapd.d/ ]; then
456 RET="`grep -r olcSuffix: /etc/ldap/slapd.d/ | awk '{print $2}' | head -1`"
457 elif [ -f /etc/ldap/slapd.conf ]; then
458 RET="`awk '/^suffix/ { exit }
459 END { gsub(/"/, "", $2); print $2 }' \
460 < /etc/ldap/slapd.conf`"
463 echo "cp_get_ldap_suffix: /etc/ldap/slapd.{d/,conf}: No such file or directory" >&2
467 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
469 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.d/" >&2
473 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.{d/,conf}" >&2
474 [ -z "$echo_return" ] || echo $RET
475 CP_ECHO_RETURN="$echo_return"
478 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
479 cp_get_ldap_realm() {
480 [ "$CP_SCRIPT_DEBUG" ] && set -vx
483 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
484 # in our own way we need to set CP_ECHO_RETURN the way we want it
485 # but preserving initial state so it could be used afterwards
490 if [ ! -d /etc/ldap/slapd.d/ -a ! -f /etc/ldap/slapd.conf ]; then
491 echo "cp_get_ldap_realm: /etc/ldap/slapd.{d/,conf}: No such file or directory" >&2
495 cp_get_ldap_suffix || true
496 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
498 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.{d/,conf}" >&2
499 [ -z "$echo_return" ] || echo $RET
500 CP_ECHO_RETURN="$echo_return"
503 # if fqdn is name.dom3.dom2.dom1.hr, check if this host is MX for
504 # either dom3.dom2.dom1.hr, dom2.dom1.hr or dom1.hr and dump highest level
507 [ "$CP_SCRIPT_DEBUG" ] && set -vx
513 if ! echo "$host" | grep -q '\.'; then
516 if [ ! -x /usr/bin/host ]; then
520 domains=$(/bin/hostname -f | awk -F. '
522 for (i=2; i<NF; i++) {
523 for (j=i; j<NF; j++) {
531 mxes=$(/usr/bin/host -t mx $d)
532 # handle output of both /usr/bin/host providers
533 mxes=$(echo "$mxes"|\
534 awk '/mail is handled by/ || /MX/ {print $NF}'|sed 's/\.$//')
535 if echo "$mxes" |egrep -q "^$host$"; then
540 [ -z "$CP_ECHO_RETURN" ] || echo "$RET"
544 [ "$CP_SCRIPT_DEBUG" ] && set -vx
545 # < input functions > output_functions
546 # new-prefix list-of-functions
548 $prefix = shift; %keep = (); $output = "";
549 $keep{$_} = 1 for @ARGV;
550 # add dependent functions
552 get_ifaddr => [ "get_ifdefault" ],
553 get_netaddr => [ "get_ifdefault", "get_ifaddr", "get_ifmask" ],
554 check_and_backup => [ "backup_conffile" ],
555 check_and_sed => [ "mv" ],
556 get_ldap_realm => [ "get_ldap_suffix" ],
558 foreach my $f (keys(%keep)) {
559 if (exists($deps{$f})) {
560 $keep{$_} = 1 for (@{$deps{$f}});
564 if (/^_?cp_(\w+) *\(\) *\{/) {
565 if (! exists($keep{$1})) {
566 while (<STDIN>) { last if /^\}/; } # skip it
572 next if /^\s*(\#|$)/;
575 $output =~ s/\b(_?)cp_/$1${prefix}_/sg;
576 $prefix = uc($prefix);
577 $output =~ s/\b(_?)CP_/$1${prefix}_/sg;