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 | \
93 /^0.0.0.0\b/{if ($5 < m) {r = $8; m = $5}}
95 addr) RET=`route -n | \
97 /^0.0.0.0\b/{if ($5 < m) {r = $2; m = $5}}
99 *) echo "cp_get_ifdefault: Argument required (dev or addr)" >&2 ;;
102 [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
104 [ -z "$echo_return" ] || echo $RET
105 CP_ECHO_RETURN="$echo_return"
106 [ -n "$RET" ] || return 1
109 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
111 [ "$CP_SCRIPT_DEBUG" ] && set -vx
113 local netmask ipaddress interface cidr echo_return
114 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
115 # in our own way we need to set CP_ECHO_RETURN the way we want it
116 # but preserving initial state so it could be used afterwards
120 if [ -z "$interface" ]; then
123 [ -z "$interface" ] && interface=lo
126 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
127 echo "cp_get_netaddr: $interface: No such interface" 1>&2
128 CP_ECHO_RETURN="$echo_return"
132 cp_get_ifaddr "$interface"
134 if [ -z $ipaddress ]; then
135 echo "cp_get_netaddr: $interface: No IP address found" 1>&2
136 CP_ECHO_RETURN="$echo_return"
139 cp_get_ifmask "$interface"
141 if [ -z $netmask ]; then
142 echo "cp_get_netaddr: $interface: No netmask found" 1>&2
143 CP_ECHO_RETURN="$echo_return"
147 if [ "$netmask" = "255.255.255.255" ]; then
150 cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
154 [ -z "$echo_return" ] || echo $RET
155 CP_ECHO_RETURN="$echo_return"
158 cp_check_and_backup() {
159 [ "$CP_SCRIPT_DEBUG" ] && set -vx
161 cp_backup_conffile -r "$@"
164 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
165 cp_backup_conffile() {
166 [ "$CP_SCRIPT_DEBUG" ] && set -vx
168 local dir ext file_bak check did opt_p opt_d
173 while echo "x$1" |grep -q '^x-'; do
186 # Is argument to -d full path or relative?
187 if echo "$1" |grep -q '^/'; then
200 if [ "$opt_d" ]; then
201 if [ ! -e "$dir" -a "$opt_p" ]; then
204 if [ $? -ne 0 ]; then
205 echo "cp_backup_conffile: Error creating backup directory $dir" 1>&2
209 if [ ! -d "$dir" ]; then
210 echo "cp_backup_conffile: Invalid backup directory $dir" 1>&2
216 if [ ! -f "$1" ]; then
217 echo "cp_backup_conffile: $1: No such file" 1>&2
221 file_bak="$dir/`basename $1`$ext"
223 file_bak="$dir/`basename $2`$ext"
226 if [ ! -f "$file_bak" ]; then
227 cp -pf "$1" "$file_bak"
230 if ! cmp -s "$1" "$file_bak"; then
231 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
232 cp -pf "$1" "$file_bak"
237 [ -z "$check" ] && return 0
239 if [ "$check" -a ! "$did" ]; then
246 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
247 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
248 # A sed wrapper, to use instead of perl -pi -e
249 # - relatively safe in-place s///g
250 # - takes care of symlinks and ownership
251 # returns true if changed, false if nothing happened
254 [ "$CP_SCRIPT_DEBUG" ] && set -vx
256 local s sedcmd grepret ret i
267 [ -e "$i" ] || continue
268 if ! egrep -q "$s" "$i"; then
272 [ -h "$i" ] && i=$(readlink -f "$i")
273 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
274 if [ $? -ne 0 ]; then
276 echo "cp_check_and_sed: Problem with sed" 1>&2
279 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
280 cp_mv "$i.dpkg-tmp" "$i"
286 [ "$ret" -eq 2 -a "$grepret" ] && ret=1
290 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
292 [ "$CP_SCRIPT_DEBUG" ] && set -vx
294 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
299 CP_NOTICE="$CP_NOTICE$1"
300 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
301 CP_NOTICE="$CP_NOTICE
306 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
308 [ "$CP_SCRIPT_DEBUG" ] && set -vx
310 local pkg version quiet
311 if [ "x$1" = "x-q" ]; then
318 return 1 # must have at least the package name as argument
320 [ -n "$2" ] && version=" $2"
321 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
322 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
323 CP_NOTICE="From: $pkg postinst script <root>
325 Subject: $pkg$version package install log
326 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
330 The $pkg package has been successfully installed on your computer.
331 For your convenience, a partial output of the last $pkg installation
335 ${CP_NOTICE}----------"
337 echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
342 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
344 [ "$CP_SCRIPT_DEBUG" ] && set -vx
352 if [ -e "$old" ]; then
353 chown --reference "$old" "$new"
354 chmod --reference "$old" "$new"
359 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
361 [ "$CP_SCRIPT_DEBUG" ] && set -vx
364 [ "$prompt" ] || prompt="Are you sure?"
367 # check to prevent user from using this when debconf is active
368 # (it would break communication with its backend - uses stdin/out)
369 if [ "$DEBCONF_REDIR" ]; then
370 echo "cp_yes_no: debconf redirection detected" >&2
374 echo -n "$prompt (y)es/(n)o: "
387 echo "Please read the message and choose y or n"
393 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
395 # db_get seems to return error messages if the requested variable
396 # is not found - we do not find that desirable, and checking $? with
397 # set -e turned on seems to be a bit of a problem
398 # --> that's why we wrap the db_get call to return unset RET variable
399 # instead of some gibberish error message that our script does
402 [ "$CP_SCRIPT_DEBUG" ] && set -vx
407 # check to prevent user from using this before activating debconf
408 # confmodule needs to be loaded before accessing db_* functions
409 if [ -z "$DEBCONF_REDIR" ]; then
410 echo "cp_db_get: debconf not activated!" >&2
414 if ! db_get "$var"; then
419 [0-9][0-9]\ ?*) # other errors
426 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
427 cp_get_ldap_suffix() {
428 [ "$CP_SCRIPT_DEBUG" ] && set -vx
431 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
432 # in our own way we need to set CP_ECHO_RETURN the way we want it
433 # but preserving initial state so it could be used afterwards
436 if [ ! -f /etc/ldap/slapd.conf ]; then
437 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
441 RET="`awk '/^suffix/ { exit }
442 END { gsub(/"/, "", $2); print $2 }' \
443 < /etc/ldap/slapd.conf`"
445 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
447 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
451 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.conf" >&2
452 [ -z "$echo_return" ] || echo $RET
453 CP_ECHO_RETURN="$echo_return"
456 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
457 cp_get_ldap_realm() {
458 [ "$CP_SCRIPT_DEBUG" ] && set -vx
461 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
462 # in our own way we need to set CP_ECHO_RETURN the way we want it
463 # but preserving initial state so it could be used afterwards
466 if [ ! -f /etc/ldap/slapd.conf ]; then
467 echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
471 cp_get_ldap_suffix || true
472 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
474 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
475 [ -z "$echo_return" ] || echo $RET
476 CP_ECHO_RETURN="$echo_return"