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