Try to make key acceptance logic clearer
[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,2011 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:/usr/local/sbin:/usr/local/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 --times -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 if [ -e ${HOST}/web-passwords ]; then
109         chown root:www-data ${HOST}/web-passwords
110         chmod 0640 ${HOST}/web-passwords
111 fi
112
113 CHROOTS=""
114 if [ -x /usr/bin/dchroot ]; then
115         CHROOTS=`dchroot --listpaths`
116 elif [ -x /usr/bin/dchroot-dsa ]; then
117         CHROOTS=$(dchroot-dsa -i | grep Location | awk '{print $2}')
118 fi
119 if [ -n "$CHROOTS" ]; then
120         for c in $CHROOTS; do
121                 if [ "$c" = "/" ] || [ "$c" = "" ]; then
122                         echo "$0 WTF: \$c is '' or '/' here." 2>&1
123                         exit 1
124                 fi
125                 if [ -x "$c/usr/bin/makedb" ]
126                 then
127                         mkdir -p "$c/var/lib/misc/$HOST"
128
129                         # remove extra stuff from earlier times and so
130                         find "$c/var/lib/misc/$HOST" -mindepth 1 \
131                                 ! -name group.tdb -a \
132                                 ! -name passwd.tdb -a \
133                                 ! -name ssh_known_hosts \
134                                 -print0 | xargs --no-run-if-empty -0 rm -f
135                         rsync -a ${verbose} $HOST/group.tdb $HOST/passwd.tdb $HOST/ssh_known_hosts "$c/var/lib/misc/$HOST"
136
137                         # clean up from the times we supposedly did shadow stuff in chroots
138                         rm -f "$c/var/lib/misc/shadow.db"
139
140                         # from failed makedb runs earlier.
141                         rm -f "$c/var/lib/misc/passwd.db.t" \
142                               "$c/var/lib/misc/group.db.t"
143                         # build passwd information
144                         chroot "$c" makedb "/var/lib/misc/$HOST/passwd.tdb" -o /var/lib/misc/passwd.db.t
145                         chroot "$c" makedb "/var/lib/misc/$HOST/group.tdb" -o /var/lib/misc/group.db.t
146                         mv -f "$c/var/lib/misc/passwd.db.t" "$c/var/lib/misc/passwd.db"
147                         mv -f "$c/var/lib/misc/group.db.t" "$c/var/lib/misc/group.db"
148
149                         ln -sf "$HOST/ssh_known_hosts" "$c/var/lib/misc/"
150                         if [ -d "$c/etc/ssh" ]; then
151                                 ln -sf ../../var/lib/misc/ssh_known_hosts "$c/etc/ssh"
152                         elif [ -L "$c/etc/ssh" ] && [ "`readlink \"$c/etc/ssh\"`" = "../../var/lib/misc/ssh_known_hosts" ]; then
153                                 # clean up past mistakes
154                                 rm -f "$c/etc/ssh"
155                         fi
156                 fi
157         done
158 fi
159
160 if [ -d "/etc/exim4" -a -e "$HOST/bsmtp" ]; then
161         if perl -e 'exit !((stat "/etc/exim4/bsmtp")[9] < time()-3600)'; then
162                 cp "$HOST/bsmtp" /etc/exim4/bsmtp
163         fi
164 fi
165 if [ -d "/etc/postfix" -a -f "$HOST/forward-alias" ]; then
166         sed -e "s/:/@$EMAILAPPEND/" $HOST/forward-alias > /etc/postfix/debian
167         /usr/sbin/postmap hash:/etc/postfix/debian < /etc/postfix/debian || true
168 fi
169
170 rebuild_zones=0
171 if [ -e /var/lib/misc/thishost/dns-sshfp ]; then
172   if ! cmp -s /var/lib/misc/thishost/dns-sshfp "$tempfile"; then
173     rebuild_zones=1
174   fi
175 fi
176
177 if [ -e /var/lib/misc/thishost/dns-zone ]; then
178   if ! cmp -s /var/lib/misc/thishost/dns-zone "$tempfile2"; then
179     rebuild_zones=1
180   fi
181 fi
182
183 if [ "${rebuild_zones}" -gt 0 ]; then
184   sudo -u dnsadm /srv/dns.debian.org/bin/update
185 fi
186
187 if [ -d "/etc/freeradius" -a -e "$HOST/rtc-passwords" ]; then
188         install -o freerad -g freerad -m 400 $HOST/rtc-passwords /var/local/rtc-passwords.freerad
189         service freeradius reload
190 fi
191 if [ -d "/etc/reTurn" -a -e "$HOST/rtc-passwords" ]; then
192         install -o return -g return -m 400 $HOST/rtc-passwords /var/local/rtc-passwords.return
193         service resiprocate-turn-server reload
194 fi