1 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
3 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
12 if [ -z "$interface" ]; then
15 [ -z "$interface" ] && interface=lo
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"
24 ifaddr="`/sbin/ifconfig $interface | awk '/inet/{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
26 if [ -z $ifaddr ]; then
27 echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
28 CP_ECHO_RETURN="$echo_return"
33 [ -z "$echo_return" ] || echo $RET
34 CP_ECHO_RETURN="$echo_return"
37 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
39 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
48 if [ -z "$interface" ]; then
51 [ -z "$interface" ] && interface=lo
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"
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))}'`"
62 if [ -z "$ifmask" ]; then
63 echo "cp_get_ifmask: $interface: No netmask found" 1>&2
64 CP_ECHO_RETURN="$echo_return"
69 [ -z "$echo_return" ] || echo $RET
70 CP_ECHO_RETURN="$echo_return"
73 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
75 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
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 ;;
90 [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
92 [ -z "$echo_return" ] || echo $RET
93 CP_ECHO_RETURN="$echo_return"
94 [ -n "$RET" ] || return 1
97 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
99 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
108 if [ -z "$interface" ]; then
111 [ -z "$interface" ] && interface=lo
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"
120 cp_get_ifaddr "$interface"
122 if [ -z $ipaddress ]; then
123 echo "cp_get_netaddr: $interface: No IP address found" 1>&2
124 CP_ECHO_RETURN="$echo_return"
127 cp_get_ifmask "$interface"
129 if [ -z $netmask ]; then
130 echo "cp_get_netaddr: $interface: No netmask found" 1>&2
131 CP_ECHO_RETURN="$echo_return"
135 if [ "$netmask" = "255.255.255.255" ]; then
138 cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
142 [ -z "$echo_return" ] || echo $RET
143 CP_ECHO_RETURN="$echo_return"
146 cp_check_and_backup() {
147 [ "$CP_SCRIPT_DEBUG" ] && set -vx
149 cp_backup_conffile -r "$@"
152 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
153 cp_backup_conffile() {
154 [ "$CP_SCRIPT_DEBUG" ] && set -vx
156 local file_bak check did
158 if [ "x$1" = "x-r" ]; then
165 if [ ! -f "$1" ]; then
166 echo "cp_backup_conffile: $1: No such file" 1>&2
170 file_bak="/var/backups/`basename $1`.bak"
172 file_bak="/var/backups/`basename $2`.bak"
175 if [ ! -f "$file_bak" ]; then
176 cp -pf "$1" "$file_bak"
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"
186 [ -z "$check" ] && return 0
188 if [ "$check" -a ! "$did" ]; then
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
203 [ "$CP_SCRIPT_DEBUG" ] && set -vx
205 local s sedcmd grepret ret i
216 [ -e "$i" ] || continue
217 if ! egrep -q "$s" "$i"; then
221 [ -h "$i" ] && i=$(readlink -f "$i")
222 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
223 if [ $? -ne 0 ]; then
225 echo "cp_check_and_sed: Problem with sed" 1>&2
228 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
229 cp_mv "$i.dpkg-tmp" "$i"
235 [ "$ret" -eq 2 -a "$grepret" ] && ret=1 || true
239 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
241 [ "$CP_SCRIPT_DEBUG" ] && set -vx
243 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
248 CP_NOTICE="$CP_NOTICE$1"
249 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
250 CP_NOTICE="$CP_NOTICE
255 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
257 [ "$CP_SCRIPT_DEBUG" ] && set -vx
259 local pkg version quiet
260 if [ "x$1" = "x-q" ]; then
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>
270 Subject: $pkg$version package install log
271 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
275 The $pkg package has been successfully installed on your computer.
276 For your convenience, a partial output of the last $pkg installation
280 ${CP_NOTICE}----------"
282 echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
287 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
289 [ "$CP_SCRIPT_DEBUG" ] && set -vx
297 if [ -e "$old" ]; then
298 chown --reference "$old" "$new"
299 chmod --reference "$old" "$new"
304 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
306 [ "$CP_SCRIPT_DEBUG" ] && set -vx
309 [ "$prompt" ] || prompt="Are you sure?"
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
319 echo -n "$prompt (y)es/(n)o: "
332 echo "Please read the message and choose y or n"
338 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
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
347 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
359 if ! db_get "$var"; then
364 [0-9][0-9]\ ?*) # other errors
371 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
372 cp_get_ldap_suffix() {
373 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
381 if [ ! -f /etc/ldap/slapd.conf ]; then
382 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
386 RET="`awk '/^suffix/ { exit }
387 END { gsub(/"/, "", $2); print $2 }' \
388 < /etc/ldap/slapd.conf`"
390 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
392 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
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"
401 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
402 cp_get_ldap_realm() {
403 [ "$CP_SCRIPT_DEBUG" ] && set -vx
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
411 if [ ! -f /etc/ldap/slapd.conf ]; then
412 echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
416 cp_get_ldap_suffix || true
417 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
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"