make ud-replicate work slightly more accurately for zones
[mirror/userdir-ldap.git] / ud-replicate
1 #! /bin/bash
2
3 #   Copyright (c) 1999-2001  Jason Gunthorpe <jgg@debian.org>
4 #   Copyright (c) 2002-2003,2006  Ryan Murray <rmurray@debian.org>
5 #   Copyright (c) 2004-2005  Joey Schulze <joey@infodrom.org>
6 #   Copyright (c) 2008 Peter Palfrader <peter@palfrader.org>
7 #   Copyright (c) 2008 Stephen Gran <sgran@debian.org>
8 #
9 #   This program is free software; you can redistribute it and/or modify
10 #   it under the terms of the GNU General Public License as published by
11 #   the Free Software Foundation; either version 2 of the License, or
12 #   (at your option) any later version.
13 #
14 #   This program is distributed in the hope that it will be useful,
15 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #   GNU General Public License for more details.
18 #
19 #   You should have received a copy of the GNU General Public License
20 #   along with this program; if not, write to the Free Software
21 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 set -e
24
25 # Without effect on the commandline
26 if [ -z "$TERM" -o "$TERM" = "dumb" ]
27 then
28     exec > /dev/null 2>&1
29     sleep $(( $RANDOM % 120 ))
30 else
31     verbose=-v
32 fi
33
34 tempdir=''
35 tempfile=''
36 tempfile2=''
37
38 cleanup ()
39 {
40   rm -f lock
41   rm -rf $tempdir
42   rm -f  $tempfile
43   rm -f  $tempfile2
44 }
45
46 PATH=/sbin:/usr/sbin:/bin:/usr/bin
47 export PATH
48 HOST=`hostname -f`
49 SYNCHOST=`ud-config synchost`;
50 LOCALSYNCON=`ud-config localsyncon`;
51 EMAILAPPEND=`ud-config emailappend`;
52 cd /tmp/
53 cd /var/lib/misc || cd /var/state/glibc/ || cd /var/db/
54 lockfile -r 1 -l 3600 lock
55 trap cleanup exit
56
57 case $HOST in
58 $LOCALSYNCON)
59     udhost=
60     ;;
61 *)
62     udhost="sshdist@$SYNCHOST:"
63     ;;
64 esac
65
66 tempfile=$(mktemp)
67 tempfile2=$(mktemp)
68
69 if [ -e /var/lib/misc/thishost/dns-sshfp ]; then
70   cp /var/lib/misc/thishost/dns-sshfp $tempfile
71 fi
72
73 if [ -e /var/lib/misc/thishost/dns-zone ]; then
74   cp /var/lib/misc/thishost/dns-zone $tempfile2
75 fi
76
77 rsync ${verbose} --delete-after -e 'ssh -i /etc/ssh/ssh_host_rsa_key -o PreferredAuthentications=publickey' -rp "${udhost}/var/cache/userdir-ldap/hosts/$HOST" .
78
79 makedb "$HOST/passwd.tdb" -o passwd.db.t
80 if [ -s "$HOST/shadow.tdb" ]
81 then
82     (umask 027 && makedb "$HOST/shadow.tdb" -o shadow.db.t)
83     chown root.shadow shadow.db.t
84     chmod 0640 shadow.db.t
85     mv -f shadow.db.t shadow.db
86 fi
87 makedb "$HOST/group.tdb" -o group.db.t
88 mv -f passwd.db.t passwd.db
89 mv -f group.db.t group.db
90 if [ -e "$HOST/ssh-rsa-shadow" ]; then
91         ln -sf $HOST/ssh-rsa-shadow .
92         ln -sf `pwd -P`/ssh-rsa-shadow /etc/ssh
93 else
94         rm -f ssh-rsa-shadow /etc/ssh/ssh-rsa-shadow
95 fi
96 ln -sf $HOST/ssh_known_hosts .
97 ln -sf `pwd -P`/ssh_known_hosts /etc/ssh
98
99 if [ -e ${HOST}/ssh-keys.tar.gz ]; then
100   export TMPDIR='/tmp/'
101   tempdir=$(mktemp -d)
102   tar -C "$tempdir" -xf ${HOST}/ssh-keys.tar.gz
103   mkdir -p userkeys
104   chmod 755 $tempdir
105   rsync -a --delete-after $tempdir/ userkeys/
106 fi
107
108 CHROOTS=""
109 if [ -x /usr/bin/dchroot ]; then
110         CHROOTS=`dchroot --listpaths`
111 elif [ -x /usr/bin/dchroot-dsa ]; then
112         CHROOTS=$(dchroot-dsa -i | grep Location | awk '{print $2}')
113 fi
114 if [ -n "$CHROOTS" ]; then
115         for c in $CHROOTS; do
116                 if [ "$c" = "/" ] || [ "$c" = "" ]; then
117                         echo "$0 WTF: \$c is '' or '/' here." 2>&1
118                         exit 1
119                 fi
120                 if [ -x "$c/usr/bin/makedb" ]
121                 then
122                         mkdir -p "$c/var/lib/misc/$HOST"
123
124                         # remove extra stuff from earlier times and so
125                         find "$c/var/lib/misc/$HOST" -mindepth 1 \
126                                 ! -name group.tdb -a \
127                                 ! -name passwd.tdb -a \
128                                 ! -name ssh_known_hosts \
129                                 -print0 | xargs --no-run-if-empty -0 rm -f
130                         rsync -a ${verbose} $HOST/group.tdb $HOST/passwd.tdb $HOST/ssh_known_hosts "$c/var/lib/misc/$HOST"
131
132                         # clean up from the times we supposedly did shadow stuff in chroots
133                         rm -f "$c/var/lib/misc/shadow.db"
134
135                         # from failed makedb runs earlier.
136                         rm -f "$c/var/lib/misc/passwd.db.t" \
137                               "$c/var/lib/misc/group.db.t"
138                         # build passwd information
139                         chroot "$c" makedb "/var/lib/misc/$HOST/passwd.tdb" -o /var/lib/misc/passwd.db.t
140                         chroot "$c" makedb "/var/lib/misc/$HOST/group.tdb" -o /var/lib/misc/group.db.t
141                         mv -f "$c/var/lib/misc/passwd.db.t" "$c/var/lib/misc/passwd.db"
142                         mv -f "$c/var/lib/misc/group.db.t" "$c/var/lib/misc/group.db"
143
144                         ln -sf "$HOST/ssh_known_hosts" "$c/var/lib/misc/"
145                         if [ -d "$c/etc/ssh" ]; then
146                                 ln -sf ../../var/lib/misc/ssh_known_hosts "$c/etc/ssh"
147                         elif [ -L "$c/etc/ssh" ] && [ "`readlink \"$c/etc/ssh\"`" = "../../var/lib/misc/ssh_known_hosts" ]; then
148                                 # clean up past mistakes
149                                 rm -f "$c/etc/ssh"
150                         fi
151                 fi
152         done
153 fi
154
155 if [ -d "/etc/exim4" -a -e "$HOST/bsmtp" ]; then
156         if perl -e 'exit !((stat "/etc/exim4/bsmtp")[9] < time()-3600)'; then
157                 cp "$HOST/bsmtp" /etc/exim4/bsmtp
158         fi
159 fi
160 if [ -d "/etc/postfix" -a -f "$HOST/forward-alias" ]; then
161         sed -e "s/:/@$EMAILAPPEND/" $HOST/forward-alias > /etc/postfix/debian
162         /usr/sbin/postmap hash:/etc/postfix/debian < /etc/postfix/debian || true
163 fi
164
165 rndc_reload=0
166 if [ -e /var/lib/misc/thishost/dns-sshfp ]; then
167   if ! cmp -s /var/lib/misc/thishost/dns-sshfp $tempfile2; then
168     /git/HOOKS/write_zonefile debian.org
169     rndc_reload=1
170   fi
171 fi
172
173 if [ -e /var/lib/misc/thishost/dns-zone ]; then
174   if ! cmp -s /var/lib/misc/thishost/dns-zone $tempfile2; then
175     /git/HOOKS/write_zonefile debian.net
176     rndc_reload=1
177   fi
178 fi
179
180 if [ "${rndc_reload}" -gt 0 ]; then
181   rndc reload
182 fi