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