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
8 # modified by irako, Mon, 02 Jul 2018 21:46:29 +0200
10 [ "$CP_SCRIPT_DEBUG" ] && set -vx
12 local ifaddr interface echo_return
13 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
14 # in our own way we need to set CP_ECHO_RETURN the way we want it
15 # but preserving initial state so it could be used afterwards
19 if [ -z "$interface" ]; then
22 [ -z "$interface" ] && interface=lo
25 #if ! ifconfig $interface 2> /dev/null >> /dev/null; then
26 if ! ip addr show $interface 2> /dev/null >> /dev/null; then
27 echo "cp_get_ifaddr: $interface: No such interface" 1>&2
28 CP_ECHO_RETURN="$echo_return"
32 #ifaddr="`/sbin/ifconfig $interface | awk '/inet /{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
33 #ifaddr="`/sbin/ifconfig $interface | awk '/inet /{ printf($2) }'`"
34 ifaddr="`ip -o -4 addr show $interface scope global | awk '{print $4;}' | cut -d/ -f 1 | head -1`"
36 if [ -z $ifaddr ]; then
37 echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
38 CP_ECHO_RETURN="$echo_return"
43 [ -z "$echo_return" ] || echo $RET
44 CP_ECHO_RETURN="$echo_return"
47 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
48 # modified by irako, Mon, 02 Jul 2018 21:46:29 +0200
50 [ "$CP_SCRIPT_DEBUG" ] && set -vx
52 local ifmask interface echo_return cidr
53 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
54 # in our own way we need to set CP_ECHO_RETURN the way we want it
55 # but preserving initial state so it could be used afterwards
59 if [ -z "$interface" ]; then
62 [ -z "$interface" ] && interface=lo
65 #if ! ifconfig $interface 2> /dev/null >> /dev/null; then
66 if ! ip addr show $interface 2> /dev/null >> /dev/null; then
67 echo "cp_get_ifmask: $interface: No such interface" 1>&2
68 CP_ECHO_RETURN="$echo_return"
72 #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))}'`"
73 #ifmask="`/sbin/ifconfig $interface | awk '/netmask /{ printf($4) }'`"
74 cidr="`ip -o -4 addr show $interface scope global | awk '{print $4;}'`"
75 ifmask="`ipcalc -nb 161.53.0.188/28 | awk '/^Netmask:/{print $2}'`"
77 if [ -z "$ifmask" ]; then
78 echo "cp_get_ifmask: $interface: No netmask found" 1>&2
79 CP_ECHO_RETURN="$echo_return"
84 [ -z "$echo_return" ] || echo $RET
85 CP_ECHO_RETURN="$echo_return"
88 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
89 # modified by irako, Mon, 02 Jul 2018 21:46:29 +0200
91 [ "$CP_SCRIPT_DEBUG" ] && set -vx
94 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
95 # in our own way we need to set CP_ECHO_RETURN the way we want it
96 # but preserving initial state so it could be used afterwards
102 #RET=`route -n | awk 'BEGIN {m = 2^32}
103 # /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $8; m = $5}}
105 RET=`ip route | grep ^default | awk '{print $5}'`
108 #RET=`route -n | awk 'BEGIN {m = 2^32}
109 # /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $2; m = $5}}
111 RET=`ip route | grep ^default | awk '{print $3}'`
114 echo "cp_get_ifdefault: Argument required (dev or addr)" >&2
118 [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
120 [ -z "$echo_return" ] || echo $RET
121 CP_ECHO_RETURN="$echo_return"
122 [ -n "$RET" ] || return 1
125 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
126 # modified by irako, Mon, 02 Jul 2018 21:46:29 +0200
128 [ "$CP_SCRIPT_DEBUG" ] && set -vx
130 local netmask ipaddress interface cidr echo_return
131 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
132 # in our own way we need to set CP_ECHO_RETURN the way we want it
133 # but preserving initial state so it could be used afterwards
137 if [ -z "$interface" ]; then
140 [ -z "$interface" ] && interface=lo
143 #if ! ifconfig $interface 2> /dev/null >> /dev/null; then
144 if ! ip addr show $interface 2> /dev/null >> /dev/null; then
145 echo "cp_get_netaddr: $interface: No such interface" 1>&2
146 CP_ECHO_RETURN="$echo_return"
150 cp_get_ifaddr "$interface"
152 if [ -z $ipaddress ]; then
153 echo "cp_get_netaddr: $interface: No IP address found" 1>&2
154 CP_ECHO_RETURN="$echo_return"
157 cp_get_ifmask "$interface"
159 if [ -z $netmask ]; then
160 echo "cp_get_netaddr: $interface: No netmask found" 1>&2
161 CP_ECHO_RETURN="$echo_return"
165 if [ "$netmask" = "255.255.255.255" ]; then
168 cidr="`ipcalc -nb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
172 [ -z "$echo_return" ] || echo $RET
173 CP_ECHO_RETURN="$echo_return"
176 cp_check_and_backup() {
177 [ "$CP_SCRIPT_DEBUG" ] && set -vx
179 cp_backup_conffile -r "$@"
182 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
183 cp_backup_conffile() {
184 [ "$CP_SCRIPT_DEBUG" ] && set -vx
186 local dir ext file_bak check did opt_p opt_d
191 while echo "x$1" |grep -q '^x-'; do
204 # Is argument to -d full path or relative?
205 if echo "$1" |grep -q '^/'; then
218 if [ "$opt_d" ]; then
219 if [ ! -e "$dir" -a "$opt_p" ]; then
222 if [ $? -ne 0 ]; then
223 echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
227 if [ ! -d "$dir" ]; then
228 echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
234 if [ ! -f "$1" ]; then
235 echo "cp_backup_conffile: $1: No such file" 1>&2
239 file_bak="$dir/`basename $1`$ext"
241 file_bak="$dir/`basename $2`$ext"
244 if [ ! -f "$file_bak" ]; then
245 cp -pf "$1" "$file_bak"
248 if ! cmp -s "$1" "$file_bak"; then
249 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
250 cp -pf "$1" "$file_bak"
255 [ -z "$check" ] && return 0
257 if [ "$check" -a ! "$did" ]; then
264 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
265 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
266 # A sed wrapper, to use instead of perl -pi -e
267 # - relatively safe in-place s///g
268 # - takes care of symlinks and ownership
269 # returns true if changed, false if nothing happened
272 [ "$CP_SCRIPT_DEBUG" ] && set -vx
274 local s sedcmd grepret ret i
285 [ -e "$i" ] || continue
286 if ! egrep -q "$s" "$i"; then
290 [ -h "$i" ] && i=$(readlink -f "$i")
291 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
292 if [ $? -ne 0 ]; then
294 echo "cp_check_and_sed: Problem with sed" 1>&2
297 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
298 cp_mv "$i.dpkg-tmp" "$i"
304 [ "$ret" -eq 2 -a "$grepret" ] && ret=1
308 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
310 [ "$CP_SCRIPT_DEBUG" ] && set -vx
312 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
317 CP_NOTICE="$CP_NOTICE$1"
318 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
319 CP_NOTICE="$CP_NOTICE
324 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
326 [ "$CP_SCRIPT_DEBUG" ] && set -vx
328 local pkg version quiet
329 if [ "x$1" = "x-q" ]; then
336 return 1 # must have at least the package name as argument
338 [ -n "$2" ] && version=" $2"
339 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
340 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
341 CP_NOTICE="From: $pkg postinst script <root>
343 Subject: $pkg$version package install log
344 Date: $(LC_ALL=C /bin/date --rfc-2822)
348 The $pkg package has been successfully installed on your computer.
349 For your convenience, a partial output of the last $pkg installation
353 ${CP_NOTICE}----------"
355 if [ -x /usr/sbin/sendmail ]; then
356 echo "$CP_NOTICE" | /usr/sbin/sendmail -t -oi &
362 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
364 [ "$CP_SCRIPT_DEBUG" ] && set -vx
372 if [ -e "$old" ]; then
373 chown --reference "$old" "$new"
374 chmod --reference "$old" "$new"
375 cp -pf "$old" "$old.cn-old"
380 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
382 [ "$CP_SCRIPT_DEBUG" ] && set -vx
385 [ "$prompt" ] || prompt="Are you sure?"
388 # check to prevent user from using this when debconf is active
389 # (it would break communication with its backend - uses stdin/out)
390 if [ "$DEBCONF_REDIR" ]; then
391 echo "cp_yes_no: debconf redirection detected" >&2
395 echo -n "$prompt (y)es/(n)o: "
416 echo "Please read the message and choose y or n"
422 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
424 # db_get seems to return error messages if the requested variable
425 # is not found - we do not find that desirable, and checking $? with
426 # set -e turned on seems to be a bit of a problem
427 # --> that's why we wrap the db_get call to return unset RET variable
428 # instead of some gibberish error message that our script does
431 [ "$CP_SCRIPT_DEBUG" ] && set -vx
436 # check to prevent user from using this before activating debconf
437 # confmodule needs to be loaded before accessing db_* functions
438 if [ -z "$DEBCONF_REDIR" ]; then
439 echo "cp_db_get: debconf not activated!" >&2
443 if ! db_get "$var"; then
448 [0-9][0-9]\ ?*) # other errors
455 # by ico, Wed, 07 Dec 2011 13:30:02 +0100
456 cp_get_ldap_suffix() {
457 [ "$CP_SCRIPT_DEBUG" ] && set -vx
460 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
461 # in our own way we need to set CP_ECHO_RETURN the way we want it
462 # but preserving initial state so it could be used afterwards
467 if [ -d /etc/ldap/slapd.d/ ]; then
468 RET="`grep -r olcSuffix: /etc/ldap/slapd.d/ | awk '{print $2}' | head -1`"
469 elif [ -f /etc/ldap/slapd.conf ]; then
470 RET="`awk '/^suffix/ { exit }
471 END { gsub(/"/, "", $2); print $2 }' \
472 < /etc/ldap/slapd.conf`"
475 echo "cp_get_ldap_suffix: /etc/ldap/slapd.{d/,conf}: No such file or directory" >&2
479 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
481 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.d/" >&2
485 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.{d/,conf}" >&2
486 [ -z "$echo_return" ] || echo $RET
487 CP_ECHO_RETURN="$echo_return"
490 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
491 cp_get_ldap_realm() {
492 [ "$CP_SCRIPT_DEBUG" ] && set -vx
495 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
496 # in our own way we need to set CP_ECHO_RETURN the way we want it
497 # but preserving initial state so it could be used afterwards
502 if [ ! -d /etc/ldap/slapd.d/ -a ! -f /etc/ldap/slapd.conf ]; then
503 echo "cp_get_ldap_realm: /etc/ldap/slapd.{d/,conf}: No such file or directory" >&2
507 cp_get_ldap_suffix || true
508 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
510 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.{d/,conf}" >&2
511 [ -z "$echo_return" ] || echo $RET
512 CP_ECHO_RETURN="$echo_return"
515 # if fqdn is name.dom3.dom2.dom1.hr, check if this host is MX for
516 # either dom3.dom2.dom1.hr, dom2.dom1.hr or dom1.hr and dump highest level
519 [ "$CP_SCRIPT_DEBUG" ] && set -vx
525 if ! echo "$host" | grep -q '\.'; then
528 if [ ! -x /usr/bin/host ]; then
532 domains=$(/bin/hostname -f | awk -F. '
534 for (i=2; i<NF; i++) {
535 for (j=i; j<NF; j++) {
543 mxes=$(/usr/bin/host -t mx $d)
544 # handle output of both /usr/bin/host providers
545 mxes=$(echo "$mxes"|\
546 awk '/mail is handled by/ || /MX/ {print $NF}'|sed 's/\.$//')
547 if echo "$mxes" |egrep -q "^$host$"; then
552 [ -z "$CP_ECHO_RETURN" ] || echo "$RET"
556 [ "$CP_SCRIPT_DEBUG" ] && set -vx
557 # < input functions > output_functions
558 # new-prefix list-of-functions
560 $prefix = shift; %keep = (); $output = "";
561 $keep{$_} = 1 for @ARGV;
562 # add dependent functions
564 get_ifaddr => [ "get_ifdefault" ],
565 get_netaddr => [ "get_ifdefault", "get_ifaddr", "get_ifmask" ],
566 check_and_backup => [ "backup_conffile" ],
567 check_and_sed => [ "mv" ],
568 get_ldap_realm => [ "get_ldap_suffix" ],
570 foreach my $f (keys(%keep)) {
571 if (exists($deps{$f})) {
572 $keep{$_} = 1 for (@{$deps{$f}});
576 if (/^_?cp_(\w+) *\(\) *\{/) {
577 if (! exists($keep{$1})) {
578 while (<STDIN>) { last if /^\}/; } # skip it
584 next if /^\s*(\#|$)/;
587 $output =~ s/\b(_?)cp_/$1${prefix}_/sg;
588 $prefix = uc($prefix);
589 $output =~ s/\b(_?)CP_/$1${prefix}_/sg;