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 file_bak check did
164 if [ "x$1" = "x-r" ]; then
171 if [ ! -f "$1" ]; then
172 echo "cp_backup_conffile: $1: No such file" 1>&2
176 file_bak="/var/backups/`basename $1`.bak"
178 file_bak="/var/backups/`basename $2`.bak"
181 if [ ! -f "$file_bak" ]; then
182 cp -pf "$1" "$file_bak"
185 if ! cmp -s "$1" "$file_bak"; then
186 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
187 cp -pf "$1" "$file_bak"
192 [ -z "$check" ] && return 0
194 if [ "$check" -a ! "$did" ]; then
201 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
202 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
203 # A sed wrapper, to use instead of perl -pi -e
204 # - relatively safe in-place s///g
205 # - takes care of symlinks and ownership
206 # returns true if changed, false if nothing happened
209 [ "$CP_SCRIPT_DEBUG" ] && set -vx
211 local s sedcmd grepret ret i
222 [ -e "$i" ] || continue
223 if ! egrep -q "$s" "$i"; then
227 [ -h "$i" ] && i=$(readlink -f "$i")
228 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
229 if [ $? -ne 0 ]; then
231 echo "cp_check_and_sed: Problem with sed" 1>&2
234 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
235 cp_mv "$i.dpkg-tmp" "$i"
241 [ "$ret" -eq 2 -a "$grepret" ] && ret=1
245 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
247 [ "$CP_SCRIPT_DEBUG" ] && set -vx
249 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
254 CP_NOTICE="$CP_NOTICE$1"
255 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
256 CP_NOTICE="$CP_NOTICE
261 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
263 [ "$CP_SCRIPT_DEBUG" ] && set -vx
265 local pkg version quiet
266 if [ "x$1" = "x-q" ]; then
273 return 1 # must have at least the package name as argument
275 [ -n "$2" ] && version=" $2"
276 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
277 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
278 CP_NOTICE="From: $pkg postinst script <root>
280 Subject: $pkg$version package install log
281 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
285 The $pkg package has been successfully installed on your computer.
286 For your convenience, a partial output of the last $pkg installation
290 ${CP_NOTICE}----------"
292 echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
297 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
299 [ "$CP_SCRIPT_DEBUG" ] && set -vx
307 if [ -e "$old" ]; then
308 chown --reference "$old" "$new"
309 chmod --reference "$old" "$new"
314 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
316 [ "$CP_SCRIPT_DEBUG" ] && set -vx
319 [ "$prompt" ] || prompt="Are you sure?"
322 # check to prevent user from using this when debconf is active
323 # (it would break communication with its backend - uses stdin/out)
324 if [ "$DEBCONF_REDIR" ]; then
325 echo "cp_yes_no: debconf redirection detected" >&2
329 echo -n "$prompt (y)es/(n)o: "
342 echo "Please read the message and choose y or n"
348 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
350 # db_get seems to return error messages if the requested variable
351 # is not found - we do not find that desirable, and checking $? with
352 # set -e turned on seems to be a bit of a problem
353 # --> that's why we wrap the db_get call to return unset RET variable
354 # instead of some gibberish error message that our script does
357 [ "$CP_SCRIPT_DEBUG" ] && set -vx
362 # check to prevent user from using this before activating debconf
363 # confmodule needs to be loaded before accessing db_* functions
364 if [ -z "$DEBCONF_REDIR" ]; then
365 echo "cp_db_get: debconf not activated!" >&2
369 if ! db_get "$var"; then
374 [0-9][0-9]\ ?*) # other errors
381 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
382 cp_get_ldap_suffix() {
383 [ "$CP_SCRIPT_DEBUG" ] && set -vx
386 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
387 # in our own way we need to set CP_ECHO_RETURN the way we want it
388 # but preserving initial state so it could be used afterwards
391 if [ ! -f /etc/ldap/slapd.conf ]; then
392 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
396 RET="`awk '/^suffix/ { exit }
397 END { gsub(/"/, "", $2); print $2 }' \
398 < /etc/ldap/slapd.conf`"
400 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
402 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
406 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.conf" >&2
407 [ -z "$echo_return" ] || echo $RET
408 CP_ECHO_RETURN="$echo_return"
411 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
412 cp_get_ldap_realm() {
413 [ "$CP_SCRIPT_DEBUG" ] && set -vx
416 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
417 # in our own way we need to set CP_ECHO_RETURN the way we want it
418 # but preserving initial state so it could be used afterwards
421 if [ ! -f /etc/ldap/slapd.conf ]; then
422 echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
426 cp_get_ldap_suffix || true
427 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
429 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
430 [ -z "$echo_return" ] || echo $RET
431 CP_ECHO_RETURN="$echo_return"