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