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)) }'`"
32 if [ -z $ifaddr ]; then
33 echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
34 CP_ECHO_RETURN="$echo_return"
39 [ -z "$echo_return" ] || echo $RET
40 CP_ECHO_RETURN="$echo_return"
43 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
45 [ "$CP_SCRIPT_DEBUG" ] && set -vx
47 local ifmask interface echo_return
48 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
49 # in our own way we need to set CP_ECHO_RETURN the way we want it
50 # but preserving initial state so it could be used afterwards
54 if [ -z "$interface" ]; then
57 [ -z "$interface" ] && interface=lo
60 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
61 echo "cp_get_ifmask: $interface: No such interface" 1>&2
62 CP_ECHO_RETURN="$echo_return"
66 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 if [ -z "$ifmask" ]; then
69 echo "cp_get_ifmask: $interface: No netmask found" 1>&2
70 CP_ECHO_RETURN="$echo_return"
75 [ -z "$echo_return" ] || echo $RET
76 CP_ECHO_RETURN="$echo_return"
79 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
81 [ "$CP_SCRIPT_DEBUG" ] && set -vx
84 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
85 # in our own way we need to set CP_ECHO_RETURN the way we want it
86 # but preserving initial state so it could be used afterwards
92 RET=`route -n | awk 'BEGIN {m = 2^32}
93 /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $8; m = $5}}
97 RET=`route -n | awk 'BEGIN {m = 2^32}
98 /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $2; m = $5}}
102 echo "cp_get_ifdefault: Argument required (dev or addr)" >&2
106 [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
108 [ -z "$echo_return" ] || echo $RET
109 CP_ECHO_RETURN="$echo_return"
110 [ -n "$RET" ] || return 1
113 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
115 [ "$CP_SCRIPT_DEBUG" ] && set -vx
117 local netmask ipaddress interface cidr echo_return
118 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
119 # in our own way we need to set CP_ECHO_RETURN the way we want it
120 # but preserving initial state so it could be used afterwards
124 if [ -z "$interface" ]; then
127 [ -z "$interface" ] && interface=lo
130 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
131 echo "cp_get_netaddr: $interface: No such interface" 1>&2
132 CP_ECHO_RETURN="$echo_return"
136 cp_get_ifaddr "$interface"
138 if [ -z $ipaddress ]; then
139 echo "cp_get_netaddr: $interface: No IP address found" 1>&2
140 CP_ECHO_RETURN="$echo_return"
143 cp_get_ifmask "$interface"
145 if [ -z $netmask ]; then
146 echo "cp_get_netaddr: $interface: No netmask found" 1>&2
147 CP_ECHO_RETURN="$echo_return"
151 if [ "$netmask" = "255.255.255.255" ]; then
154 cidr="`ipcalc -nb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
158 [ -z "$echo_return" ] || echo $RET
159 CP_ECHO_RETURN="$echo_return"
162 cp_check_and_backup() {
163 [ "$CP_SCRIPT_DEBUG" ] && set -vx
165 cp_backup_conffile -r "$@"
168 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
169 cp_backup_conffile() {
170 [ "$CP_SCRIPT_DEBUG" ] && set -vx
172 local dir ext file_bak check did opt_p opt_d
177 while echo "x$1" |grep -q '^x-'; do
190 # Is argument to -d full path or relative?
191 if echo "$1" |grep -q '^/'; then
204 if [ "$opt_d" ]; then
205 if [ ! -e "$dir" -a "$opt_p" ]; then
208 if [ $? -ne 0 ]; then
209 echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
213 if [ ! -d "$dir" ]; then
214 echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
220 if [ ! -f "$1" ]; then
221 echo "cp_backup_conffile: $1: No such file" 1>&2
225 file_bak="$dir/`basename $1`$ext"
227 file_bak="$dir/`basename $2`$ext"
230 if [ ! -f "$file_bak" ]; then
231 cp -pf "$1" "$file_bak"
234 if ! cmp -s "$1" "$file_bak"; then
235 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
236 cp -pf "$1" "$file_bak"
241 [ -z "$check" ] && return 0
243 if [ "$check" -a ! "$did" ]; then
250 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
251 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
252 # A sed wrapper, to use instead of perl -pi -e
253 # - relatively safe in-place s///g
254 # - takes care of symlinks and ownership
255 # returns true if changed, false if nothing happened
258 [ "$CP_SCRIPT_DEBUG" ] && set -vx
260 local s sedcmd grepret ret i
271 [ -e "$i" ] || continue
272 if ! egrep -q "$s" "$i"; then
276 [ -h "$i" ] && i=$(readlink -f "$i")
277 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
278 if [ $? -ne 0 ]; then
280 echo "cp_check_and_sed: Problem with sed" 1>&2
283 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
284 cp_mv "$i.dpkg-tmp" "$i"
290 [ "$ret" -eq 2 -a "$grepret" ] && ret=1
294 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
296 [ "$CP_SCRIPT_DEBUG" ] && set -vx
298 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
303 CP_NOTICE="$CP_NOTICE$1"
304 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
305 CP_NOTICE="$CP_NOTICE
310 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
312 [ "$CP_SCRIPT_DEBUG" ] && set -vx
314 local pkg version quiet
315 if [ "x$1" = "x-q" ]; then
322 return 1 # must have at least the package name as argument
324 [ -n "$2" ] && version=" $2"
325 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
326 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
327 CP_NOTICE="From: $pkg postinst script <root>
329 Subject: $pkg$version package install log
330 Date: $(LC_ALL=C /bin/date --rfc-2822)
334 The $pkg package has been successfully installed on your computer.
335 For your convenience, a partial output of the last $pkg installation
339 ${CP_NOTICE}----------"
341 if [ -x /usr/sbin/sendmail ]; then
342 echo "$CP_NOTICE" | /usr/sbin/sendmail -t -oi &
348 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
350 [ "$CP_SCRIPT_DEBUG" ] && set -vx
358 if [ -e "$old" ]; then
359 chown --reference "$old" "$new"
360 chmod --reference "$old" "$new"
361 cp -pf "$old" "$old.cn-old"
366 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
368 [ "$CP_SCRIPT_DEBUG" ] && set -vx
371 [ "$prompt" ] || prompt="Are you sure?"
374 # check to prevent user from using this when debconf is active
375 # (it would break communication with its backend - uses stdin/out)
376 if [ "$DEBCONF_REDIR" ]; then
377 echo "cp_yes_no: debconf redirection detected" >&2
381 echo -n "$prompt (y)es/(n)o: "
402 echo "Please read the message and choose y or n"
408 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
410 # db_get seems to return error messages if the requested variable
411 # is not found - we do not find that desirable, and checking $? with
412 # set -e turned on seems to be a bit of a problem
413 # --> that's why we wrap the db_get call to return unset RET variable
414 # instead of some gibberish error message that our script does
417 [ "$CP_SCRIPT_DEBUG" ] && set -vx
422 # check to prevent user from using this before activating debconf
423 # confmodule needs to be loaded before accessing db_* functions
424 if [ -z "$DEBCONF_REDIR" ]; then
425 echo "cp_db_get: debconf not activated!" >&2
429 if ! db_get "$var"; then
434 [0-9][0-9]\ ?*) # other errors
441 # by ico, Wed, 07 Dec 2011 13:30:02 +0100
442 cp_get_ldap_suffix() {
443 [ "$CP_SCRIPT_DEBUG" ] && set -vx
446 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
447 # in our own way we need to set CP_ECHO_RETURN the way we want it
448 # but preserving initial state so it could be used afterwards
452 if [ -d /etc/ldap/slapd.d/ ]; then
453 RET="`grep -r olcSuffix /etc/ldap/slapd.d/|awk '{print $2}'`"
454 elif [ -f /etc/ldap/slapd.conf ]; then
455 RET="`awk '/^suffix/ { exit }
456 END { gsub(/"/, "", $2); print $2 }' \
457 < /etc/ldap/slapd.conf`"
459 if [ ! -d /etc/ldap/slapd.d/ ]; then
460 echo "cp_get_ldap_suffix: /etc/ldap/slapd.d/: No such directory" >&2
462 elif [ ! -f /etc/ldap/slapd.conf ]; then
463 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
469 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
471 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.d/" >&2
475 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.d/ or /etc/ldap/slapd.conf" >&2
476 [ -z "$echo_return" ] || echo $RET
477 CP_ECHO_RETURN="$echo_return"
480 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
481 cp_get_ldap_realm() {
482 [ "$CP_SCRIPT_DEBUG" ] && set -vx
485 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
486 # in our own way we need to set CP_ECHO_RETURN the way we want it
487 # but preserving initial state so it could be used afterwards
490 if [ ! -d /etc/ldap/slapd.d/ ]; then
491 echo "cp_get_ldap_suffix: /etc/ldap/slapd.d/: No such directory" >&2
493 elif [ ! -f /etc/ldap/slapd.conf ]; then
494 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
498 cp_get_ldap_suffix || true
499 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
501 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.d/" >&2
502 [ -z "$echo_return" ] || echo $RET
503 CP_ECHO_RETURN="$echo_return"
506 # if fqdn is name.dom3.dom2.dom1.hr, check if this host is MX for
507 # either dom3.dom2.dom1.hr, dom2.dom1.hr or dom1.hr and dump highest level
510 [ "$CP_SCRIPT_DEBUG" ] && set -vx
516 if ! echo "$host" | grep -q '\.'; then
519 if [ ! -x /usr/bin/host ]; then
523 domains=$(/bin/hostname -f | awk -F. '
525 for (i=2; i<NF; i++) {
526 for (j=i; j<NF; j++) {
534 mxes=$(/usr/bin/host -t mx $d)
535 # handle output of both /usr/bin/host providers
536 mxes=$(echo "$mxes"|\
537 awk '/mail is handled by/ || /MX/ {print $NF}'|sed 's/\.$//')
538 if echo "$mxes" |egrep -q "^$host$"; then
543 [ -z "$CP_ECHO_RETURN" ] || echo "$RET"
547 [ "$CP_SCRIPT_DEBUG" ] && set -vx
548 # < input functions > output_functions
549 # new-prefix list-of-functions
551 $prefix = shift; %keep = (); $output = "";
552 $keep{$_} = 1 for @ARGV;
553 # add dependent functions
555 get_ifaddr => [ "get_ifdefault" ],
556 get_netaddr => [ "get_ifdefault", "get_ifaddr", "get_ifmask" ],
557 check_and_backup => [ "backup_conffile" ],
558 check_and_sed => [ "mv" ],
559 get_ldap_realm => [ "get_ldap_suffix" ],
561 foreach my $f (keys(%keep)) {
562 if (exists($deps{$f})) {
563 $keep{$_} = 1 for (@{$deps{$f}});
567 if (/^_?cp_(\w+) *\(\) *\{/) {
568 if (! exists($keep{$1})) {
569 while (<STDIN>) { last if /^\}/; } # skip it
575 next if /^\s*(\#|$)/;
578 $output =~ s/\b(_?)cp_/$1${prefix}_/sg;
579 $prefix = uc($prefix);
580 $output =~ s/\b(_?)CP_/$1${prefix}_/sg;