1 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
3 [ "$CP_SCRIPT_DEBUG" ] && set -vx
4 local ifaddr interface echo_return
5 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
6 # in our own way we need to set CP_ECHO_RETURN the way we want it
7 # but preserving initial state so it could be used afterwards
11 if [ -z "$interface" ]; then
14 [ -z "$interface" ] && interface=lo
17 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
18 echo "cp_get_ifaddr: $interface: No such interface" 1>&2
19 CP_ECHO_RETURN="$echo_return"
23 ifaddr="`/sbin/ifconfig $interface | awk '/inet/{ printf("%s\n",substr($2,index($2,":")+1)) }'`"
25 if [ -z $ifaddr ]; then
26 echo "cp_get_ifaddr: $interface: No ip address found" 1>&2
27 CP_ECHO_RETURN="$echo_return"
32 [ -z "$echo_return" ] || echo $RET
33 CP_ECHO_RETURN="$echo_return"
36 # by ddzeko & ico, Fri, 18 Mar 2005 14:44:08 +0100
38 [ "$CP_SCRIPT_DEBUG" ] && set -vx
39 local ifmask interface echo_return
40 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
41 # in our own way we need to set CP_ECHO_RETURN the way we want it
42 # but preserving initial state so it could be used afterwards
46 if [ -z "$interface" ]; then
49 [ -z "$interface" ] && interface=lo
52 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
53 echo "cp_get_ifmask: $interface: No such interface" 1>&2
54 CP_ECHO_RETURN="$echo_return"
58 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))}'`"
60 if [ -z "$ifmask" ]; then
61 echo "cp_get_ifmask: $interface: No netmask found" 1>&2
62 CP_ECHO_RETURN="$echo_return"
67 [ -z "$echo_return" ] || echo $RET
68 CP_ECHO_RETURN="$echo_return"
71 # by ddzeko, Mon, 21 Mar 2005 07:00:22 +0100
74 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
75 # in our own way we need to set CP_ECHO_RETURN the way we want it
76 # but preserving initial state so it could be used afterwards
79 [ "$CP_SCRIPT_DEBUG" ] && set -vx
82 dev) RET=`route -n | awk '/^0.0.0.0/{print $8}'` ;;
83 addr) RET=`route -n | awk '/^0.0.0.0/{print $2}'` ;;
84 *) echo "cp_get_ifdefault: Argument required (dev or addr)" >&2 ;;
86 [ -z "$RET" -a "$1" ] && echo "cp_get_ifdefault: No default route" >&2
87 [ -z "$echo_return" ] || echo $RET
88 CP_ECHO_RETURN="$echo_return"
89 [ -z "$RET" ] && return 1
92 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
94 [ "$CP_SCRIPT_DEBUG" ] && set -vx
95 local netmask ipaddress interface cidr echo_return
96 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
97 # in our own way we need to set CP_ECHO_RETURN the way we want it
98 # but preserving initial state so it could be used afterwards
102 if [ -z "$interface" ]; then
105 [ -z "$interface" ] && interface=lo
108 if ! ifconfig $interface 2> /dev/null >> /dev/null; then
109 echo "cp_get_netaddr: $interface: No such interface" 1>&2
110 CP_ECHO_RETURN="$echo_return"
114 cp_get_ifaddr "$interface"
116 if [ -z $ipaddress ]; then
117 echo "cp_get_netaddr: $interface: No IP address found" 1>&2
118 CP_ECHO_RETURN="$echo_return"
121 cp_get_ifmask "$interface"
123 if [ -z $netmask ]; then
124 echo "cp_get_netaddr: $interface: No netmask found" 1>&2
125 CP_ECHO_RETURN="$echo_return"
129 if [ "$netmask" = "255.255.255.255" ]; then
132 cidr="`ipcalc -ncb $ipaddress $netmask | awk '/^Network:/{print $2}'`"
136 [ -z "$echo_return" ] || echo $RET
137 CP_ECHO_RETURN="$echo_return"
140 cp_check_and_backup() {
141 cp_backup_conffile -r "$@"
144 # by ico, Tue, 15 Mar 2005 14:04:21 +0100
145 cp_backup_conffile() {
146 [ "$CP_SCRIPT_DEBUG" ] && set -vx
147 local file_bak check did
149 if [ "x$1" = "x-r" ]; then
156 if [ ! -f "$1" ]; then
157 echo "cp_backup_conffile: $1: No such file" 1>&2
161 file_bak="/var/backups/`basename $1`.bak"
163 file_bak="/var/backups/`basename $2`.bak"
166 if [ ! -f "$file_bak" ]; then
167 cp -pf "$1" "$file_bak"
170 if ! cmp -s "$1" "$file_bak"; then
171 /usr/bin/savelog -p -c 7 "$file_bak" > /dev/null 2> /dev/null
172 cp -pf "$1" "$file_bak"
177 [ -z "$check" ] && return 0
179 if [ "$check" -a ! "$did" ]; then
186 # by jelly, Tue, 15 Mar 2005 14:04:21 +0100
187 # modified by ico, Mon, 6 Jun 2005 11:58:08 +0200
188 # A sed wrapper, to use instead of perl -pi -e
189 # - relatively safe in-place s///g
190 # - takes care of symlinks and ownership
191 # returns true if changed, false if nothing happened
194 [ "$CP_SCRIPT_DEBUG" ] && set -vx
195 local s sedcmd grepret ret i
206 [ -e "$i" ] || continue
207 if ! egrep -q "$s" "$i"; then
211 [ -h "$i" ] && i=$(readlink -f "$i")
212 sed "$sedcmd" "$i" > "$i.dpkg-tmp"
213 if [ $? -ne 0 ]; then
215 echo "cp_check_and_sed: Problem with sed" 1>&2
218 if ! cmp -s "$i" "$i.dpkg-tmp" 2>&1 >/dev/null; then
219 cp_mv "$i.dpkg-tmp" "$i"
225 [ "$ret" -eq 2 -a "$grepret" ] && ret=1 || true
229 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
231 [ "$CP_SCRIPT_DEBUG" ] && set -vx
232 if [ "x$1" = "x-mailonly" -o "x$1" = "x-m" ]; then
237 CP_NOTICE="$CP_NOTICE$1"
238 if [ `echo -n "$*" | wc -l` -eq 0 ]; then
239 CP_NOTICE="$CP_NOTICE
244 # by jelly, Sun, 20 Mar 2005 20:12:19 +0100
246 [ "$CP_SCRIPT_DEBUG" ] && set -vx
247 local pkg version quiet
248 if [ "x$1" = "x-q" ]; then
252 [ -n "$1" ] && pkg="$1" # barf if no package
253 [ -n "$2" ] && version=" $2" || true
254 if [ "$(echo $CP_NOTICE|wc -w)" -gt 0 ]; then
255 [ -n "$quiet" ] || echo "Mailing upgrade output to root."
256 CP_NOTICE="From: $pkg postinst script <root>
258 Subject: $pkg$version package install log
259 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
263 The $pkg package has been successfully installed on your computer.
264 For your convenience, a partial output of the last $pkg installation
268 ${CP_NOTICE}----------"
270 echo "$CP_NOTICE" | /usr/sbin/sendmail -t &
275 # by ddzeko, Mon, 21 Mar 2005 11:31:59 +0100
277 [ "$CP_SCRIPT_DEBUG" ] && set -vx
284 if [ -e "$old" ]; then
285 chown --reference "$old" "$new"
286 chmod --reference "$old" "$new"
291 # by ddzeko, Mon, 21 Mar 2005 13:35:42 +0100
293 [ "$CP_SCRIPT_DEBUG" ] && set -vx
296 [ "$prompt" ] || prompt="Are you sure?"
299 # check to prevent user from using this when debconf is active
300 # (it would break communication with its backend - uses stdin/out)
301 if [ "$DEBCONF_REDIR" ]; then
302 echo "cp_yes_no: debconf redirection detected" >&2
306 echo -n "$prompt (y)es/(n)o: "
319 echo "Please read the message and choose y or n"
325 # by ddzeko, Tue, 29 Mar 2005 17:37:52 +0200
327 # db_get seems to return error messages if the requested variable
328 # is not found - we do not find that desirable, and checking $? with
329 # set -e turned on seems to be a bit of a problem
330 # --> that's why we wrap the db_get call to return unset RET variable
331 # instead of some gibberish error message that our script does
337 # check to prevent user from using this before activating debconf
338 # confmodule needs to be loaded before accessing db_* functions
339 if [ -z "$DEBCONF_REDIR" ]; then
340 echo "cp_db_get: debconf not activated!" >&2
344 if ! db_get "$var"; then
349 [0-9][0-9]\ ?*) # other errors
356 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
357 cp_get_ldap_suffix() {
359 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
360 # in our own way we need to set CP_ECHO_RETURN the way we want it
361 # but preserving initial state so it could be used afterwards
364 [ "$CP_SCRIPT_DEBUG" ] && set -vx
366 if [ ! -f /etc/ldap/slapd.conf ]; then
367 echo "cp_get_ldap_suffix: /etc/ldap/slapd.conf: No such file" >&2
371 RET="`awk '/^suffix/ { exit }
372 END { gsub(/"/, "", $2); print $2 }' \
373 < /etc/ldap/slapd.conf`"
375 if [ "`echo $RET | sed 's/,/ /g' | wc -w`" -gt 2 ]; then
377 echo "cp_get_ldap_suffix: Invalid LDAP suffix in /etc/ldap/slapd.conf" >&2
381 [ -z "$RET" ] && echo "cp_get_ldap_suffix: No LDAP suffix in /etc/ldap/slapd.conf" >&2
382 [ -z "$echo_return" ] || echo $RET
383 CP_ECHO_RETURN="$echo_return"
386 # by ico, Wed, 20 Apr 2005 21:09:54 +0200
387 cp_get_ldap_realm() {
389 echo_return="$CP_ECHO_RETURN" # since we're making our own calls
390 # in our own way we need to set CP_ECHO_RETURN the way we want it
391 # but preserving initial state so it could be used afterwards
394 [ "$CP_SCRIPT_DEBUG" ] && set -vx
396 if [ ! -f /etc/ldap/slapd.conf ]; then
397 echo "cp_get_ldap_realm: /etc/ldap/slapd.conf: No such file" >&2
401 cp_get_ldap_suffix || true
402 RET="`echo $RET | awk -F, '{print $1}' | awk -F= '{print $2}'`"
404 [ -z "$RET" ] && echo "cp_get_ldap_realm: No LDAP REALM in /etc/ldap/slapd.conf" >&2
405 [ -z "$echo_return" ] || echo $RET
406 CP_ECHO_RETURN="$echo_return"