r91: Pazi na slucaj cudne mrezne konfiguracije -- visestrukih
[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
32   if [ -z $ifaddr ]; then
33     echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
34     CP_ECHO_RETURN="$echo_return"
35     return 1
36   fi
37
38   RET="$ifaddr"
39   [ -z "$echo_return" ] || echo $RET
40   CP_ECHO_RETURN="$echo_return"
41 }
42
43 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
44 cp_get_ifmask() {
45   [ "$CP_SCRIPT_DEBUG" ] && set -vx
46
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
51   CP_ECHO_RETURN=""
52
53   interface="$1"
54   if [ -z "$interface" ]; then
55     cp_get_ifdefault dev
56     interface="$RET"
57     [ -z "$interface" ] && interface=lo
58   fi
59
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"
63     return 1
64   fi
65
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))}'`"
67
68   if [ -z "$ifmask" ]; then
69     echo "cp_get_ifmask: $interface: No netmask found" 1>&2
70     CP_ECHO_RETURN="$echo_return"
71     return 1
72   fi
73
74   RET="$ifmask"
75   [ -z "$echo_return" ] || echo $RET
76   CP_ECHO_RETURN="$echo_return"
77 }
78
79 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
80 cp_get_ifdefault() {
81   [ "$CP_SCRIPT_DEBUG" ] && set -vx
82
83   local echo_return
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
87   CP_ECHO_RETURN=""
88
89   RET=""
90   case $1 in
91     dev)  RET=`route -n | \
92                awk 'BEGIN       {m = 2^32}
93                     /^0.0.0.0\b/{if ($5 < m) {r = $8; m = $5}}
94                     END         {print r}'`
95     addr) RET=`route -n | \
96                awk 'BEGIN       {m = 2^32}
97                     /^0.0.0.0\b/{if ($5 < m) {r = $2; m = $5}}
98                     END         {print r}'`
99     *)    echo "cp_get_ifdefault: Argument required (dev or addr)" >&2 ;;
100   esac
101
102   [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
103
104   [ -z "$echo_return" ] || echo $RET
105   CP_ECHO_RETURN="$echo_return"
106   [ -n "$RET" ] || return 1
107 }
108
109 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
110 cp_get_netaddr() {
111   [ "$CP_SCRIPT_DEBUG" ] && set -vx
112
113   local netmask ipaddress interface cidr echo_return
114   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
115   # in our own way we need to set CP_ECHO_RETURN the way we want it
116   # but preserving initial state so it could be used afterwards
117   CP_ECHO_RETURN=""
118
119   interface="$1"
120   if [ -z "$interface" ]; then
121     cp_get_ifdefault dev
122     interface="$RET"
123     [ -z "$interface" ] && interface=lo
124   fi
125
126   if ! ifconfig $interface 2> /dev/null >> /dev/null; then
127     echo "cp_get_netaddr: $interface: No such interface" 1>&2
128     CP_ECHO_RETURN="$echo_return"
129     return 1
130   fi
131
132   cp_get_ifaddr "$interface"
133   ipaddress="$RET"
134   if [ -z $ipaddress ]; then
135     echo "cp_get_netaddr: $interface: No IP address found" 1>&2
136     CP_ECHO_RETURN="$echo_return"
137     return 1
138   fi
139   cp_get_ifmask "$interface"
140   netmask="$RET"
141   if [ -z $netmask ]; then
142     echo "cp_get_netaddr: $interface: No netmask found" 1>&2
143     CP_ECHO_RETURN="$echo_return"
144     return 1
145   fi
146
147   if [ "$netmask" = "255.255.255.255" ]; then
148     cidr="$ipaddress/32"
149   else 
150     cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
151   fi
152   
153   RET="$cidr"
154   [ -z "$echo_return" ] || echo $RET
155   CP_ECHO_RETURN="$echo_return"
156 }
157
158 cp_check_and_backup() {
159   [ "$CP_SCRIPT_DEBUG" ] && set -vx
160
161   cp_backup_conffile -r "$@"
162 }
163
164 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
165 cp_backup_conffile() {
166   [ "$CP_SCRIPT_DEBUG" ] && set -vx
167
168   local dir ext file_bak check did opt_p opt_d
169   
170   dir=/var/backups
171   ext=.bak
172   
173   while echo "x$1" |grep -q '^x-'; do
174     case "$1" in
175     -r)
176       shift
177       check=1
178       ;;
179     -p)
180       shift
181       opt_p=1
182       ;;
183     -d)
184       shift
185       opt_d=1
186       # Is argument to -d full path or relative?
187       if echo "$1" |grep -q '^/'; then
188         dir="$1"
189       else
190         dir="$dir/$1"
191       fi
192       shift
193       ;;
194     -n)
195       shift
196       ext=
197       ;;
198     esac
199   done
200   if [ "$opt_d" ]; then
201     if [ ! -e "$dir" -a "$opt_p" ]; then
202       mkdir "$dir"
203     fi
204     if [ $? -ne 0 ]; then
205       echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
206       return 3
207     fi
208   fi
209   if [ ! -d "$dir" ]; then
210     echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
211     return 3
212   fi
213   if [ -z "$1" ]; then
214     return 1
215   fi
216   if [ ! -f "$1" ]; then
217     echo "cp_backup_conffile: $1: No such file" 1>&2
218     return 2
219   fi
220   if [ -z "$2" ]; then
221     file_bak="$dir/`basename $1`$ext"
222   else
223     file_bak="$dir/`basename $2`$ext"
224   fi
225
226   if [ ! -f "$file_bak" ]; then
227     cp -pf "$1" "$file_bak"
228     did=1
229   else
230     if ! cmp -s "$1" "$file_bak"; then
231       /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
232       cp -pf "$1" "$file_bak"
233       did=1
234     fi
235   fi
236
237   [ -z "$check" ] && return 0
238
239   if [ "$check" -a ! "$did" ]; then
240     return 3
241   else
242     return 0
243   fi
244 }
245
246 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
247 # modified by ico, Mon,  6 Jun 2005 11:58:08 +0200
248 # A sed wrapper, to use instead of perl -pi -e
249 #  - relatively safe in-place s///g
250 #  - takes care of symlinks and ownership
251 # returns true if changed, false if nothing happened
252 #
253 cp_check_and_sed() {
254   [ "$CP_SCRIPT_DEBUG" ] && set -vx
255
256   local s sedcmd grepret ret i
257   if [ -z "$3" ]; then
258     return 1
259   fi
260   s="$1"
261   shift
262   sedcmd="$1"
263   shift
264   ret=2
265   for i in "$@"
266   do
267     [ -e "$i" ]        || continue
268     if ! egrep -q "$s" "$i"; then
269       grepret=1
270       continue
271     fi
272     [ -h "$i" ]        && i=$(readlink -f "$i")
273     sed "$sedcmd" "$i" > "$i.dpkg-tmp"
274     if [ $? -ne 0 ]; then
275       rm "$i.dpkg-tmp"
276       echo "cp_check_and_sed: Problem with sed" 1>&2
277       return 5
278     fi
279     if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
280       cp_mv "$i.dpkg-tmp" "$i"
281     else
282       rm "$i.dpkg-tmp"
283     fi
284     ret=0
285   done
286   [ "$ret" -eq 2 -a "$grepret" ] && ret=1
287   return $ret
288 }
289
290 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
291 cp_echo () {
292   [ "$CP_SCRIPT_DEBUG" ] && set -vx
293
294   if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
295     shift
296   else   
297     echo "$*"
298   fi
299   CP_NOTICE="$CP_NOTICE$1"
300   if [ `echo -n "$*" | wc -l` -eq 0 ]; then
301     CP_NOTICE="$CP_NOTICE
302 "
303   fi
304 }   
305
306 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
307 cp_mail () {
308   [ "$CP_SCRIPT_DEBUG" ] && set -vx
309
310   local pkg version quiet
311   if [ "x$1" = "x-q" ]; then
312     quiet=1
313     shift
314   fi
315   if [ -n "$1" ]; then
316     pkg="$1"
317   else
318     return 1 # must have at least the package name as argument
319   fi
320   [ -n "$2" ] && version=" $2"
321   if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
322     [ -n "$quiet" ] || echo "Mailing upgrade output to root."
323     CP_NOTICE="From: $pkg postinst script <root>
324 To: root
325 Subject: $pkg$version package install log
326 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
327  
328 Hello!
329
330 The $pkg package has been successfully installed on your computer.
331 For your convenience, a partial output of the last $pkg installation
332 is included below.
333
334 ----------
335 ${CP_NOTICE}----------"
336   
337     echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
338     sleep 1
339   fi
340 }
341
342 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
343 cp_mv () {
344   [ "$CP_SCRIPT_DEBUG" ] && set -vx
345
346   local new old
347   if [ -z "$2" ]; then
348     return 1
349   fi
350   new="$1"
351   old="$2"
352   if [ -e "$old" ]; then
353     chown --reference "$old" "$new"
354     chmod --reference "$old" "$new"
355   fi
356   mv "$new" "$old"
357 }
358
359 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
360 cp_yes_no () {
361   [ "$CP_SCRIPT_DEBUG" ] && set -vx
362   local prompt answer
363   prompt="$1"
364   [ "$prompt" ] || prompt="Are you sure?"
365   RET=""
366
367   # check to prevent user from using this when debconf is active
368   # (it would break communication with its backend - uses stdin/out)
369   if [ "$DEBCONF_REDIR" ]; then
370     echo "cp_yes_no: debconf redirection detected" >&2
371     return 1
372   fi
373
374   echo -n "$prompt (y)es/(n)o: "
375   read answer
376   case "$answer" in
377     y*)
378       echo
379       RET=y
380       ;;
381     n*)
382       echo
383       RET=n
384       ;;
385     *)
386       echo
387       echo "Please read the message and choose y or n"
388       cp_yes_no
389       ;;
390   esac
391 }
392
393 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
394 #
395 # db_get seems to return error messages if the requested variable 
396 # is not found - we do not find that desirable, and checking $? with
397 # set -e turned on seems to be a bit of a problem
398 # --> that's why we wrap the db_get call to return unset RET variable
399 #     instead of some gibberish error message that our script does
400 #     not expect
401 cp_db_get() {
402   [ "$CP_SCRIPT_DEBUG" ] && set -vx
403
404   local var
405   var="$1"
406   
407   # check to prevent user from using this before activating debconf
408   # confmodule needs to be loaded before accessing db_* functions
409   if [ -z "$DEBCONF_REDIR" ]; then
410     echo "cp_db_get: debconf not activated!" >&2
411     return 1
412   fi
413  
414   if ! db_get "$var"; then
415     case "$RET" in
416         *doesn\'t\ exist)
417           RET=""
418           ;;
419         [0-9][0-9]\ ?*) # other errors
420           RET=""
421           ;;
422     esac
423   fi
424 }
425
426 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
427 cp_get_ldap_suffix() {
428   [ "$CP_SCRIPT_DEBUG" ] && set -vx
429
430   local echo_return
431   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
432   # in our own way we need to set CP_ECHO_RETURN the way we want it
433   # but preserving initial state so it could be used afterwards
434   CP_ECHO_RETURN=""
435
436   if [ ! -f /etc/ldap/slapd.conf ]; then
437     echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
438     return 2
439   fi
440   
441   RET="`awk  '/^suffix/      { exit }
442               END            { gsub(/"/, "", $2); print $2 }' \
443              < /etc/ldap/slapd.conf`"
444
445   if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
446     RET=""
447     echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
448     return 2
449   fi
450     
451   [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.conf" >&2
452   [ -z "$echo_return" ] || echo $RET
453   CP_ECHO_RETURN="$echo_return"
454 }
455
456 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
457 cp_get_ldap_realm() {
458   [ "$CP_SCRIPT_DEBUG" ] && set -vx
459
460   local echo_return
461   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
462   # in our own way we need to set CP_ECHO_RETURN the way we want it
463   # but preserving initial state so it could be used afterwards
464   CP_ECHO_RETURN=""
465
466   if [ ! -f /etc/ldap/slapd.conf ]; then
467     echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
468     return 2
469   fi
470  
471   cp_get_ldap_suffix || true
472   RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
473
474   [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
475   [ -z "$echo_return" ] || echo $RET
476   CP_ECHO_RETURN="$echo_return"
477 }
478