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