Minimalne izmjene u paketu za Debian stretch
[carnet-tools-cn.git] / functions.sh
1 # Initialize "public" CP_ECHO_RETURN variable once
2 if [ -z "$_CP_INIT_ECHO_RETURN" ]; then
3   _CP_INIT_ECHO_RETURN=1
4   CP_ECHO_RETURN=""
5 fi
6
7 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
8 cp_get_ifaddr() {
9   [ "$CP_SCRIPT_DEBUG" ] && set -vx
10
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
15   CP_ECHO_RETURN=""
16
17   interface="$1"
18   if [ -z "$interface" ]; then
19     cp_get_ifdefault dev
20     interface="$RET"
21     [ -z "$interface" ] && interface=lo
22   fi
23
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"
27     return 2
28   fi
29
30   #ifaddr="`/sbin/ifconfig $interface | awk '/inet /{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
31   ifaddr="`/sbin/ifconfig $interface | awk '/inet /{ printf($2) }'`"
32
33   if [ -z $ifaddr ]; then
34     echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
35     CP_ECHO_RETURN="$echo_return"
36     return 1
37   fi
38
39   RET="$ifaddr"
40   [ -z "$echo_return" ] || echo $RET
41   CP_ECHO_RETURN="$echo_return"
42 }
43
44 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
45 cp_get_ifmask() {
46   [ "$CP_SCRIPT_DEBUG" ] && set -vx
47
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
52   CP_ECHO_RETURN=""
53
54   interface="$1"
55   if [ -z "$interface" ]; then
56     cp_get_ifdefault dev
57     interface="$RET"
58     [ -z "$interface" ] && interface=lo
59   fi
60
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"
64     return 1
65   fi
66
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) }'`"
69
70   if [ -z "$ifmask" ]; then
71     echo "cp_get_ifmask: $interface: No netmask found" 1>&2
72     CP_ECHO_RETURN="$echo_return"
73     return 1
74   fi
75
76   RET="$ifmask"
77   [ -z "$echo_return" ] || echo $RET
78   CP_ECHO_RETURN="$echo_return"
79 }
80
81 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
82 cp_get_ifdefault() {
83   [ "$CP_SCRIPT_DEBUG" ] && set -vx
84
85   local echo_return
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
89   CP_ECHO_RETURN=""
90
91   RET=""
92   case $1 in
93     dev)
94       RET=`route -n | awk 'BEGIN {m = 2^32}
95                     /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $8; m = $5}}
96                              END {print r}'`
97       ;;
98     addr)
99       RET=`route -n | awk 'BEGIN {m = 2^32}
100                     /^0\.0\.0\.0[ \t]/ {if ($5 < m) {r = $2; m = $5}}
101                              END {print r}'`
102       ;;
103     *)
104       echo "cp_get_ifdefault: Argument required (dev or addr)" >&2
105       ;;
106   esac
107
108   [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
109
110   [ -z "$echo_return" ] || echo $RET
111   CP_ECHO_RETURN="$echo_return"
112   [ -n "$RET" ] || return 1
113 }
114
115 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
116 cp_get_netaddr() {
117   [ "$CP_SCRIPT_DEBUG" ] && set -vx
118
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
123   CP_ECHO_RETURN=""
124
125   interface="$1"
126   if [ -z "$interface" ]; then
127     cp_get_ifdefault dev
128     interface="$RET"
129     [ -z "$interface" ] && interface=lo
130   fi
131
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"
135     return 1
136   fi
137
138   cp_get_ifaddr "$interface"
139   ipaddress="$RET"
140   if [ -z $ipaddress ]; then
141     echo "cp_get_netaddr: $interface: No IP address found" 1>&2
142     CP_ECHO_RETURN="$echo_return"
143     return 1
144   fi
145   cp_get_ifmask "$interface"
146   netmask="$RET"
147   if [ -z $netmask ]; then
148     echo "cp_get_netaddr: $interface: No netmask found" 1>&2
149     CP_ECHO_RETURN="$echo_return"
150     return 1
151   fi
152
153   if [ "$netmask" = "255.255.255.255" ]; then
154     cidr="$ipaddress/32"
155   else 
156     cidr="`ipcalc -nb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
157   fi
158   
159   RET="$cidr"
160   [ -z "$echo_return" ] || echo $RET
161   CP_ECHO_RETURN="$echo_return"
162 }
163
164 cp_check_and_backup() {
165   [ "$CP_SCRIPT_DEBUG" ] && set -vx
166
167   cp_backup_conffile -r "$@"
168 }
169
170 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
171 cp_backup_conffile() {
172   [ "$CP_SCRIPT_DEBUG" ] && set -vx
173
174   local dir ext file_bak check did opt_p opt_d
175   
176   dir=/var/backups
177   ext=.bak
178   
179   while echo "x$1" |grep -q '^x-'; do
180     case "$1" in
181     -r)
182       shift
183       check=1
184       ;;
185     -p)
186       shift
187       opt_p=1
188       ;;
189     -d)
190       shift
191       opt_d=1
192       # Is argument to -d full path or relative?
193       if echo "$1" |grep -q '^/'; then
194         dir="$1"
195       else
196         dir="$dir/$1"
197       fi
198       shift
199       ;;
200     -n)
201       shift
202       ext=
203       ;;
204     esac
205   done
206   if [ "$opt_d" ]; then
207     if [ ! -e "$dir" -a "$opt_p" ]; then
208       mkdir "$dir"
209     fi
210     if [ $? -ne 0 ]; then
211       echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
212       return 3
213     fi
214   fi
215   if [ ! -d "$dir" ]; then
216     echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
217     return 3
218   fi
219   if [ -z "$1" ]; then
220     return 1
221   fi
222   if [ ! -f "$1" ]; then
223     echo "cp_backup_conffile: $1: No such file" 1>&2
224     return 2
225   fi
226   if [ -z "$2" ]; then
227     file_bak="$dir/`basename $1`$ext"
228   else
229     file_bak="$dir/`basename $2`$ext"
230   fi
231
232   if [ ! -f "$file_bak" ]; then
233     cp -pf "$1" "$file_bak"
234     did=1
235   else
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"
239       did=1
240     fi
241   fi
242
243   [ -z "$check" ] && return 0
244
245   if [ "$check" -a ! "$did" ]; then
246     return 3
247   else
248     return 0
249   fi
250 }
251
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
258 #
259 cp_check_and_sed() {
260   [ "$CP_SCRIPT_DEBUG" ] && set -vx
261
262   local s sedcmd grepret ret i
263   if [ -z "$3" ]; then
264     return 1
265   fi
266   s="$1"
267   shift
268   sedcmd="$1"
269   shift
270   ret=2
271   for i in "$@"
272   do
273     [ -e "$i" ]        || continue
274     if ! egrep -q "$s" "$i"; then
275       grepret=1
276       continue
277     fi
278     [ -h "$i" ]        && i=$(readlink -f "$i")
279     sed "$sedcmd" "$i" > "$i.dpkg-tmp"
280     if [ $? -ne 0 ]; then
281       rm "$i.dpkg-tmp"
282       echo "cp_check_and_sed: Problem with sed" 1>&2
283       return 5
284     fi
285     if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
286       cp_mv "$i.dpkg-tmp" "$i"
287     else
288       rm "$i.dpkg-tmp"
289     fi
290     ret=0
291   done
292   [ "$ret" -eq 2 -a "$grepret" ] && ret=1
293   return $ret
294 }
295
296 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
297 cp_echo () {
298   [ "$CP_SCRIPT_DEBUG" ] && set -vx
299
300   if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
301     shift
302   else   
303     echo "$*"
304   fi
305   CP_NOTICE="$CP_NOTICE$1"
306   if [ `echo -n "$*" | wc -l` -eq 0 ]; then
307     CP_NOTICE="$CP_NOTICE
308 "
309   fi
310 }   
311
312 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
313 cp_mail () {
314   [ "$CP_SCRIPT_DEBUG" ] && set -vx
315
316   local pkg version quiet
317   if [ "x$1" = "x-q" ]; then
318     quiet=1
319     shift
320   fi
321   if [ -n "$1" ]; then
322     pkg="$1"
323   else
324     return 1 # must have at least the package name as argument
325   fi
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>
330 To: root
331 Subject: $pkg$version package install log
332 Date: $(LC_ALL=C /bin/date --rfc-2822)
333
334 Hello!
335
336 The $pkg package has been successfully installed on your computer.
337 For your convenience, a partial output of the last $pkg installation
338 is included below.
339
340 ----------
341 ${CP_NOTICE}----------"
342
343     if [ -x /usr/sbin/sendmail ]; then
344       echo "$CP_NOTICE" | /usr/sbin/sendmail -t -oi &
345       sleep 1
346     fi
347   fi
348 }
349
350 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
351 cp_mv () {
352   [ "$CP_SCRIPT_DEBUG" ] && set -vx
353
354   local new old
355   if [ -z "$2" ]; then
356     return 1
357   fi
358   new="$1"
359   old="$2"
360   if [ -e "$old" ]; then
361     chown --reference "$old" "$new"
362     chmod --reference "$old" "$new"
363     cp -pf "$old" "$old.cn-old"
364   fi
365   mv "$new" "$old"
366 }
367
368 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
369 cp_yes_no () {
370   [ "$CP_SCRIPT_DEBUG" ] && set -vx
371   local prompt answer
372   prompt="$1"
373   [ "$prompt" ] || prompt="Are you sure?"
374   RET=""
375
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
380     return 1
381   fi
382
383   echo -n "$prompt (y)es/(n)o: "
384   read answer
385   case "$answer" in
386     Y*)
387       echo
388       RET=y
389       ;;
390     N*)
391       echo
392       RET=n
393       ;;
394     y*)
395       echo
396       RET=y
397       ;;
398     n*)
399       echo
400       RET=n
401       ;;
402     *)
403       echo
404       echo "Please read the message and choose y or n"
405       cp_yes_no "$prompt"
406       ;;
407   esac
408 }
409
410 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
411 #
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
417 #     not expect
418 cp_db_get() {
419   [ "$CP_SCRIPT_DEBUG" ] && set -vx
420
421   local var
422   var="$1"
423   
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
428     return 1
429   fi
430  
431   if ! db_get "$var"; then
432     case "$RET" in
433         *doesn\'t\ exist)
434           RET=""
435           ;;
436         [0-9][0-9]\ ?*) # other errors
437           RET=""
438           ;;
439     esac
440   fi
441 }
442
443 # by ico, Wed, 07 Dec 2011 13:30:02 +0100
444 cp_get_ldap_suffix() {
445   [ "$CP_SCRIPT_DEBUG" ] && set -vx
446
447   local echo_return
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
451   CP_ECHO_RETURN=""
452
453   RET=""
454  
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`"
461   else
462     RET=""
463     echo "cp_get_ldap_suffix: /etc/ldap/slapd.{d/,conf}: No such file or directory" >&2
464     return 2
465   fi
466
467   if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
468     RET=""
469     echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.d/" >&2
470     return 2
471   fi
472     
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"
476 }
477
478 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
479 cp_get_ldap_realm() {
480   [ "$CP_SCRIPT_DEBUG" ] && set -vx
481
482   local echo_return
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
486   CP_ECHO_RETURN=""
487
488   RET=""
489
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
492     return 2
493   fi
494
495   cp_get_ldap_suffix || true
496   RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
497
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"
501 }
502
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
505 # domain on stdout
506 cp_get_mx_domain() {
507   [ "$CP_SCRIPT_DEBUG" ] && set -vx
508
509   local domains d host
510
511   host=$(hostname -f)
512   RET="$host"
513   if ! echo "$host" | grep -q '\.'; then
514     return
515   fi
516   if [ ! -x /usr/bin/host ]; then
517     # no host command
518     return
519   fi
520   domains=$(/bin/hostname -f | awk -F. '
521     {
522       for (i=2; i<NF; i++) { 
523         for (j=i; j<NF; j++) {
524           printf "%s", $(j)"."
525         };
526         print $NF
527       }
528     }'    )
529   for d in $domains
530   do
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
536       RET="$d"
537     fi
538   done
539
540   [ -z "$CP_ECHO_RETURN" ] || echo "$RET"
541 }
542
543 _cp_customize() {
544   [ "$CP_SCRIPT_DEBUG" ] && set -vx
545   # < input functions > output_functions
546   # new-prefix list-of-functions
547   perl -e '
548     $prefix = shift; %keep = (); $output = "";
549     $keep{$_} = 1 for @ARGV;
550     # add dependent functions
551     %deps = (
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" ],
557     );
558     foreach my $f (keys(%keep)) {
559       if (exists($deps{$f})) {
560         $keep{$_} = 1 for (@{$deps{$f}});
561       }
562     }
563     while (<STDIN>) {
564       if (/^_?cp_(\w+) *\(\) *\{/) {
565         if (! exists($keep{$1})) {
566           while (<STDIN>) { last if /^\}/; } # skip it
567           next;
568         } else {
569           $output .= "\n";
570         }
571       }
572       next if /^\s*(\#|$)/;
573       $output .= $_;
574     }
575     $output =~ s/\b(_?)cp_/$1${prefix}_/sg;
576     $prefix = uc($prefix);
577     $output =~ s/\b(_?)CP_/$1${prefix}_/sg;
578     print $output;
579   ' -- $*
580 }