r74: kriva logika na kraju cp_get_ifdefault...
[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 || true
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   [ -n "$1" ] && pkg="$1"       # barf if no package
265   [ -n "$2" ] && version=" $2" || true
266   if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
267     [ -n "$quiet" ] || echo "Mailing upgrade output to root."
268     CP_NOTICE="From: $pkg postinst script <root>
269 To: root
270 Subject: $pkg$version package install log
271 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
272  
273 Hello!
274
275 The $pkg package has been successfully installed on your computer.
276 For your convenience, a partial output of the last $pkg installation
277 is included below.
278
279 ----------
280 ${CP_NOTICE}----------"
281   
282     echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
283     sleep 1
284   fi
285 }
286
287 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
288 cp_mv () {
289   [ "$CP_SCRIPT_DEBUG" ] && set -vx
290
291   local new old
292   if [ -z "$2" ]; then
293     return 1
294   fi
295   new="$1"
296   old="$2"
297   if [ -e "$old" ]; then
298     chown --reference "$old" "$new"
299     chmod --reference "$old" "$new"
300   fi
301   mv "$new" "$old"
302 }
303
304 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
305 cp_yes_no () {
306   [ "$CP_SCRIPT_DEBUG" ] && set -vx
307   local prompt answer
308   prompt="$1"
309   [ "$prompt" ] || prompt="Are you sure?"
310   RET=""
311
312   # check to prevent user from using this when debconf is active
313   # (it would break communication with its backend - uses stdin/out)
314   if [ "$DEBCONF_REDIR" ]; then
315     echo "cp_yes_no: debconf redirection detected" >&2
316     return 1
317   fi
318
319   echo -n "$prompt (y)es/(n)o: "
320   read answer
321   case "$answer" in
322     y*)
323       echo
324       RET=y
325       ;;
326     n*)
327       echo
328       RET=n
329       ;;
330     *)
331       echo
332       echo "Please read the message and choose y or n"
333       cp_yes_no
334       ;;
335   esac
336 }
337
338 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
339 #
340 # db_get seems to return error messages if the requested variable 
341 # is not found - we do not find that desirable, and checking $? with
342 # set -e turned on seems to be a bit of a problem
343 # --> that's why we wrap the db_get call to return unset RET variable
344 #     instead of some gibberish error message that our script does
345 #     not expect
346 cp_db_get() {
347   [ "$CP_SCRIPT_DEBUG" ] && set -vx
348
349   local var
350   var="$1"
351   
352   # check to prevent user from using this before activating debconf
353   # confmodule needs to be loaded before accessing db_* functions
354   if [ -z "$DEBCONF_REDIR" ]; then
355     echo "cp_db_get: debconf not activated!" >&2
356     return 1
357   fi
358  
359   if ! db_get "$var"; then
360     case "$RET" in
361         *doesn\'t\ exist)
362           RET=""
363           ;;
364         [0-9][0-9]\ ?*) # other errors
365           RET=""
366           ;;
367     esac
368   fi
369 }
370
371 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
372 cp_get_ldap_suffix() {
373   [ "$CP_SCRIPT_DEBUG" ] && set -vx
374
375   local echo_return
376   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
377   # in our own way we need to set CP_ECHO_RETURN the way we want it
378   # but preserving initial state so it could be used afterwards
379   CP_ECHO_RETURN=""
380
381   if [ ! -f /etc/ldap/slapd.conf ]; then
382     echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
383     return 2
384   fi
385   
386   RET="`awk  '/^suffix/      { exit }
387               END            { gsub(/"/, "", $2); print $2 }' \
388              < /etc/ldap/slapd.conf`"
389
390   if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
391     RET=""
392     echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
393     return 2
394   fi
395     
396   [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.conf" >&2
397   [ -z "$echo_return" ] || echo $RET
398   CP_ECHO_RETURN="$echo_return"
399 }
400
401 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
402 cp_get_ldap_realm() {
403   [ "$CP_SCRIPT_DEBUG" ] && set -vx
404
405   local echo_return
406   echo_return="$CP_ECHO_RETURN" # since we're making our own calls
407   # in our own way we need to set CP_ECHO_RETURN the way we want it
408   # but preserving initial state so it could be used afterwards
409   CP_ECHO_RETURN=""
410
411   if [ ! -f /etc/ldap/slapd.conf ]; then
412     echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
413     return 2
414   fi
415  
416   cp_get_ldap_suffix || true
417   RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
418
419   [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
420   [ -z "$echo_return" ] || echo $RET
421   CP_ECHO_RETURN="$echo_return"
422 }
423