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