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