1 # Initialize "public" CP_ECHO_RETURN variable once
2 if [ -z "$_CP_INIT_ECHO_RETURN" ]; then
7 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
9 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
18 if [ -z "$interface" ]; then
21 [ -z "$interface" ] && interface=lo
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"
30 ifaddr="`/sbin/ifconfig $interface | awk '/inet/{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
32 if [ -z $ifaddr ]; then
33 echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
34 CP_ECHO_RETURN="$echo_return"
39 [ -z "$echo_return" ] || echo $RET
40 CP_ECHO_RETURN="$echo_return"
43 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
45 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
54 if [ -z "$interface" ]; then
57 [ -z "$interface" ] && interface=lo
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"
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))}'`"
68 if [ -z "$ifmask" ]; then
69 echo "cp_get_ifmask: $interface: No netmask found" 1>&2
70 CP_ECHO_RETURN="$echo_return"
75 [ -z "$echo_return" ] || echo $RET
76 CP_ECHO_RETURN="$echo_return"
79 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
81 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
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 ;;
96 [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
98 [ -z "$echo_return" ] || echo $RET
99 CP_ECHO_RETURN="$echo_return"
100 [ -n "$RET" ] || return 1
103 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
105 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
114 if [ -z "$interface" ]; then
117 [ -z "$interface" ] && interface=lo
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"
126 cp_get_ifaddr "$interface"
128 if [ -z $ipaddress ]; then
129 echo "cp_get_netaddr: $interface: No IP address found" 1>&2
130 CP_ECHO_RETURN="$echo_return"
133 cp_get_ifmask "$interface"
135 if [ -z $netmask ]; then
136 echo "cp_get_netaddr: $interface: No netmask found" 1>&2
137 CP_ECHO_RETURN="$echo_return"
141 if [ "$netmask" = "255.255.255.255" ]; then
144 cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
148 [ -z "$echo_return" ] || echo $RET
149 CP_ECHO_RETURN="$echo_return"
152 cp_check_and_backup() {
153 [ "$CP_SCRIPT_DEBUG" ] && set -vx
155 cp_backup_conffile -r "$@"
158 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
159 cp_backup_conffile() {
160 [ "$CP_SCRIPT_DEBUG" ] && set -vx
162 local dir ext file_bak check did
167 while echo "x$1" |grep -q '^x-'; do
175 # is argument to -d full path or relative?
176 if echo "$1" |grep -q '^/'; then
182 if [ ! -d "$dir" ]; then
183 echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
195 if [ ! -f "$1" ]; then
196 echo "cp_backup_conffile: $1: No such file" 1>&2
200 file_bak="$dir/`basename $1`$ext"
202 file_bak="$dir/`basename $2`$ext"
205 if [ ! -f "$file_bak" ]; then
206 cp -pf "$1" "$file_bak"
209 if ! cmp -s "$1" "$file_bak"; then
210 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
211 cp -pf "$1" "$file_bak"
216 [ -z "$check" ] && return 0
218 if [ "$check" -a ! "$did" ]; then
225 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
226 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
227 # A sed wrapper, to use instead of perl -pi -e
228 # - relatively safe in-place s///g
229 # - takes care of symlinks and ownership
230 # returns true if changed, false if nothing happened
233 [ "$CP_SCRIPT_DEBUG" ] && set -vx
235 local s sedcmd grepret ret i
246 [ -e "$i" ] || continue
247 if ! egrep -q "$s" "$i"; then
251 [ -h "$i" ] && i=$(readlink -f "$i")
252 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
253 if [ $? -ne 0 ]; then
255 echo "cp_check_and_sed: Problem with sed" 1>&2
258 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
259 cp_mv "$i.dpkg-tmp" "$i"
265 [ "$ret" -eq 2 -a "$grepret" ] && ret=1
269 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
271 [ "$CP_SCRIPT_DEBUG" ] && set -vx
273 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
278 CP_NOTICE="$CP_NOTICE$1"
279 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
280 CP_NOTICE="$CP_NOTICE
285 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
287 [ "$CP_SCRIPT_DEBUG" ] && set -vx
289 local pkg version quiet
290 if [ "x$1" = "x-q" ]; then
297 return 1 # must have at least the package name as argument
299 [ -n "$2" ] && version=" $2"
300 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
301 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
302 CP_NOTICE="From: $pkg postinst script <root>
304 Subject: $pkg$version package install log
305 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
309 The $pkg package has been successfully installed on your computer.
310 For your convenience, a partial output of the last $pkg installation
314 ${CP_NOTICE}----------"
316 echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
321 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
323 [ "$CP_SCRIPT_DEBUG" ] && set -vx
331 if [ -e "$old" ]; then
332 chown --reference "$old" "$new"
333 chmod --reference "$old" "$new"
338 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
340 [ "$CP_SCRIPT_DEBUG" ] && set -vx
343 [ "$prompt" ] || prompt="Are you sure?"
346 # check to prevent user from using this when debconf is active
347 # (it would break communication with its backend - uses stdin/out)
348 if [ "$DEBCONF_REDIR" ]; then
349 echo "cp_yes_no: debconf redirection detected" >&2
353 echo -n "$prompt (y)es/(n)o: "
366 echo "Please read the message and choose y or n"
372 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
374 # db_get seems to return error messages if the requested variable
375 # is not found - we do not find that desirable, and checking $? with
376 # set -e turned on seems to be a bit of a problem
377 # --> that's why we wrap the db_get call to return unset RET variable
378 # instead of some gibberish error message that our script does
381 [ "$CP_SCRIPT_DEBUG" ] && set -vx
386 # check to prevent user from using this before activating debconf
387 # confmodule needs to be loaded before accessing db_* functions
388 if [ -z "$DEBCONF_REDIR" ]; then
389 echo "cp_db_get: debconf not activated!" >&2
393 if ! db_get "$var"; then
398 [0-9][0-9]\ ?*) # other errors
405 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
406 cp_get_ldap_suffix() {
407 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
415 if [ ! -f /etc/ldap/slapd.conf ]; then
416 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
420 RET="`awk '/^suffix/ { exit }
421 END { gsub(/"/, "", $2); print $2 }' \
422 < /etc/ldap/slapd.conf`"
424 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
426 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
430 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.conf" >&2
431 [ -z "$echo_return" ] || echo $RET
432 CP_ECHO_RETURN="$echo_return"
435 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
436 cp_get_ldap_realm() {
437 [ "$CP_SCRIPT_DEBUG" ] && set -vx
440 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
441 # in our own way we need to set CP_ECHO_RETURN the way we want it
442 # but preserving initial state so it could be used afterwards
445 if [ ! -f /etc/ldap/slapd.conf ]; then
446 echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
450 cp_get_ldap_suffix || true
451 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
453 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
454 [ -z "$echo_return" ] || echo $RET
455 CP_ECHO_RETURN="$echo_return"