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 opt_p opt_d
167 while echo "x$1" |grep -q '^x-'; do
179 # Is argument to -d full path or relative?
180 if echo "$1" |grep -q '^/'; then
192 if [ "$opt_d" ]; then
193 if [ ! -e "$dir" -a "$opt_p" ]; then
195 if [ $? -ne 0 ]; then
196 echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
200 if [ ! -d "$dir" ]; then
201 echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
207 if [ ! -f "$1" ]; then
208 echo "cp_backup_conffile: $1: No such file" 1>&2
212 file_bak="$dir/`basename $1`$ext"
214 file_bak="$dir/`basename $2`$ext"
217 if [ ! -f "$file_bak" ]; then
218 cp -pf "$1" "$file_bak"
221 if ! cmp -s "$1" "$file_bak"; then
222 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
223 cp -pf "$1" "$file_bak"
228 [ -z "$check" ] && return 0
230 if [ "$check" -a ! "$did" ]; then
237 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
238 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
239 # A sed wrapper, to use instead of perl -pi -e
240 # - relatively safe in-place s///g
241 # - takes care of symlinks and ownership
242 # returns true if changed, false if nothing happened
245 [ "$CP_SCRIPT_DEBUG" ] && set -vx
247 local s sedcmd grepret ret i
258 [ -e "$i" ] || continue
259 if ! egrep -q "$s" "$i"; then
263 [ -h "$i" ] && i=$(readlink -f "$i")
264 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
265 if [ $? -ne 0 ]; then
267 echo "cp_check_and_sed: Problem with sed" 1>&2
270 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
271 cp_mv "$i.dpkg-tmp" "$i"
277 [ "$ret" -eq 2 -a "$grepret" ] && ret=1
281 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
283 [ "$CP_SCRIPT_DEBUG" ] && set -vx
285 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
290 CP_NOTICE="$CP_NOTICE$1"
291 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
292 CP_NOTICE="$CP_NOTICE
297 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
299 [ "$CP_SCRIPT_DEBUG" ] && set -vx
301 local pkg version quiet
302 if [ "x$1" = "x-q" ]; then
309 return 1 # must have at least the package name as argument
311 [ -n "$2" ] && version=" $2"
312 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
313 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
314 CP_NOTICE="From: $pkg postinst script <root>
316 Subject: $pkg$version package install log
317 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
321 The $pkg package has been successfully installed on your computer.
322 For your convenience, a partial output of the last $pkg installation
326 ${CP_NOTICE}----------"
328 echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
333 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
335 [ "$CP_SCRIPT_DEBUG" ] && set -vx
343 if [ -e "$old" ]; then
344 chown --reference "$old" "$new"
345 chmod --reference "$old" "$new"
350 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
352 [ "$CP_SCRIPT_DEBUG" ] && set -vx
355 [ "$prompt" ] || prompt="Are you sure?"
358 # check to prevent user from using this when debconf is active
359 # (it would break communication with its backend - uses stdin/out)
360 if [ "$DEBCONF_REDIR" ]; then
361 echo "cp_yes_no: debconf redirection detected" >&2
365 echo -n "$prompt (y)es/(n)o: "
378 echo "Please read the message and choose y or n"
384 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
386 # db_get seems to return error messages if the requested variable
387 # is not found - we do not find that desirable, and checking $? with
388 # set -e turned on seems to be a bit of a problem
389 # --> that's why we wrap the db_get call to return unset RET variable
390 # instead of some gibberish error message that our script does
393 [ "$CP_SCRIPT_DEBUG" ] && set -vx
398 # check to prevent user from using this before activating debconf
399 # confmodule needs to be loaded before accessing db_* functions
400 if [ -z "$DEBCONF_REDIR" ]; then
401 echo "cp_db_get: debconf not activated!" >&2
405 if ! db_get "$var"; then
410 [0-9][0-9]\ ?*) # other errors
417 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
418 cp_get_ldap_suffix() {
419 [ "$CP_SCRIPT_DEBUG" ] && set -vx
422 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
423 # in our own way we need to set CP_ECHO_RETURN the way we want it
424 # but preserving initial state so it could be used afterwards
427 if [ ! -f /etc/ldap/slapd.conf ]; then
428 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
432 RET="`awk '/^suffix/ { exit }
433 END { gsub(/"/, "", $2); print $2 }' \
434 < /etc/ldap/slapd.conf`"
436 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
438 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
442 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.conf" >&2
443 [ -z "$echo_return" ] || echo $RET
444 CP_ECHO_RETURN="$echo_return"
447 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
448 cp_get_ldap_realm() {
449 [ "$CP_SCRIPT_DEBUG" ] && set -vx
452 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
453 # in our own way we need to set CP_ECHO_RETURN the way we want it
454 # but preserving initial state so it could be used afterwards
457 if [ ! -f /etc/ldap/slapd.conf ]; then
458 echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
462 cp_get_ldap_suffix || true
463 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
465 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
466 [ -z "$echo_return" ] || echo $RET
467 CP_ECHO_RETURN="$echo_return"