userdir-ldap.conf: add keyring.pgp to default add_keyrings
[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
36 cleanup ()
37 {
38   rm -f lock
39   rm -rf $tempdir
40 }
41
42 PATH=/sbin:/usr/sbin:/bin:/usr/bin
43 export PATH
44 HOST=`hostname -f`
45 SYNCHOST=`ud-config synchost`;
46 LOCALSYNCON=`ud-config localsyncon`;
47 EMAILAPPEND=`ud-config emailappend`;
48 cd /tmp/
49 cd /var/lib/misc || cd /var/state/glibc/ || cd /var/db/
50 lockfile -r 1 -l 3600 lock
51 trap cleanup exit
52
53 case $HOST in
54 $LOCALSYNCON)
55     udhost=
56     ;;
57 *)
58     udhost="sshdist@$SYNCHOST:"
59     ;;
60 esac
61
62 rsync ${verbose} --delete-after -e 'ssh -i /etc/ssh/ssh_host_rsa_key -o PreferredAuthentications=publickey' -rp "${udhost}/var/cache/userdir-ldap/hosts/$HOST" .
63
64 makedb "$HOST/passwd.tdb" -o passwd.db.t
65 if [ -s "$HOST/shadow.tdb" ]
66 then
67     (umask 027 && makedb "$HOST/shadow.tdb" -o shadow.db.t)
68     chown root.shadow shadow.db.t
69     chmod 0640 shadow.db.t
70     mv -f shadow.db.t shadow.db
71 fi
72 makedb "$HOST/group.tdb" -o group.db.t
73 mv -f passwd.db.t passwd.db
74 mv -f group.db.t group.db
75 if [ -e "$HOST/ssh-rsa-shadow" ]; then
76         ln -sf $HOST/ssh-rsa-shadow .
77         ln -sf `pwd -P`/ssh-rsa-shadow /etc/ssh
78 else
79         rm -f ssh-rsa-shadow /etc/ssh/ssh-rsa-shadow
80 fi
81 ln -sf $HOST/ssh_known_hosts .
82 ln -sf `pwd -P`/ssh_known_hosts /etc/ssh
83
84 if [ -e ${HOST}/ssh-keys.tar.gz ]; then
85   export TMPDIR='/tmp/'
86   tempdir=$(mktemp -d)
87   tar -C "$tempdir" -xf ${HOST}/ssh-keys.tar.gz
88   mkdir -p userkeys
89   chmod 755 $tempdir
90   rsync -a --delete-after $tempdir/ userkeys/
91 fi
92
93 CHROOTS=""
94 if [ -x /usr/bin/dchroot ]; then
95         CHROOTS=`dchroot --listpaths`
96 elif [ -x /usr/bin/dchroot-dsa ]; then
97         CHROOTS=$(dchroot-dsa -i | grep Location | awk '{print $2}')
98 fi
99 if [ -n "$CHROOTS" ]; then
100         for c in $CHROOTS; do
101                 if [ "$c" = "/" ] || [ "$c" = "" ]; then
102                         echo "$0 WTF: \$c is '' or '/' here." 2>&1
103                         exit 1
104                 fi
105                 if [ -x "$c/usr/bin/makedb" ]
106                 then
107                         mkdir -p "$c/var/lib/misc/$HOST"
108
109                         # remove extra stuff from earlier times and so
110                         find "$c/var/lib/misc/$HOST" -mindepth 1 \
111                                 ! -name group.tdb -a \
112                                 ! -name passwd.tdb -a \
113                                 ! -name ssh_known_hosts \
114                                 -print0 | xargs --no-run-if-empty -0 rm -f
115                         rsync -a ${verbose} $HOST/group.tdb $HOST/passwd.tdb $HOST/ssh_known_hosts "$c/var/lib/misc/$HOST"
116
117                         # clean up from the times we supposedly did shadow stuff in chroots
118                         rm -f "$c/var/lib/misc/shadow.db"
119
120                         # from failed makedb runs earlier.
121                         rm -f "$c/var/lib/misc/passwd.db.t" \
122                               "$c/var/lib/misc/group.db.t"
123                         # build passwd information
124                         chroot "$c" makedb "/var/lib/misc/$HOST/passwd.tdb" -o /var/lib/misc/passwd.db.t
125                         chroot "$c" makedb "/var/lib/misc/$HOST/group.tdb" -o /var/lib/misc/group.db.t
126                         mv -f "$c/var/lib/misc/passwd.db.t" "$c/var/lib/misc/passwd.db"
127                         mv -f "$c/var/lib/misc/group.db.t" "$c/var/lib/misc/group.db"
128
129                         ln -sf "$HOST/ssh_known_hosts" "$c/var/lib/misc/"
130                         if [ -d "$c/etc/ssh" ]; then
131                                 ln -sf ../../var/lib/misc/ssh_known_hosts "$c/etc/ssh"
132                         elif [ -L "$c/etc/ssh" ] && [ "`readlink \"$c/etc/ssh\"`" = "../../var/lib/misc/ssh_known_hosts" ]; then
133                                 # clean up past mistakes
134                                 rm -f "$c/etc/ssh"
135                         fi
136                 fi
137         done
138 fi
139
140 if [ -d "/etc/exim4" -a -e "$HOST/bsmtp" ]; then
141         if perl -e 'exit !((stat "/etc/exim4/bsmtp")[9] < time()-3600)'; then
142                 cp "$HOST/bsmtp" /etc/exim4/bsmtp
143         fi
144 fi
145 if [ -d "/etc/postfix" -a -f "$HOST/forward-alias" ]; then
146         sed -e "s/:/@$EMAILAPPEND/" $HOST/forward-alias > /etc/postfix/debian
147         /usr/sbin/postmap hash:/etc/postfix/debian < /etc/postfix/debian || true
148 fi