ahem, we need to actually look in the host subdir
[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 #
8 #   This program is free software; you can redistribute it and/or modify
9 #   it under the terms of the GNU General Public License as published by
10 #   the Free Software Foundation; either version 2 of the License, or
11 #   (at your option) any later version.
12 #
13 #   This program is distributed in the hope that it will be useful,
14 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #   GNU General Public License for more details.
17 #
18 #   You should have received a copy of the GNU General Public License
19 #   along with this program; if not, write to the Free Software
20 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 set -e
23
24 # Without effect on the commandline
25 if [ -z "$TERM" -o "$TERM" = "dumb" ]
26 then
27     exec > /dev/null 2>&1
28     sleep $(( $RANDOM % 120 ))
29 else
30     verbose=-v
31 fi
32
33 tempdir=''
34
35 cleanup ()
36 {
37   rm -f lock
38   rm -rf $tempdir
39 }
40
41 PATH=/sbin:/usr/sbin:/bin:/usr/bin
42 export PATH
43 HOST=`hostname -f`
44 SYNCHOST=`ud-config synchost`;
45 LOCALSYNCON=`ud-config localsyncon`;
46 cd /tmp/
47 cd /var/lib/misc || cd /var/state/glibc/ || cd /var/db/
48 lockfile -r 1 -l 3600 lock
49 trap cleanup exit
50
51 case $HOST in
52 $LOCALSYNCON)
53     udhost=
54     ;;
55 *)
56     udhost="sshdist@$SYNCHOST:"
57     ;;
58 esac
59
60 rsync ${verbose} -e 'ssh -i /etc/ssh/ssh_host_rsa_key -o PreferredAuthentications=publickey' -rp "${udhost}/var/cache/userdir-ldap/hosts/$HOST" .
61
62 makedb "$HOST/passwd.tdb" -o passwd.db.t
63 if [ -s "$HOST/shadow.tdb" ]
64 then
65     (umask 027 && makedb "$HOST/shadow.tdb" -o shadow.db.t)
66     chown root.shadow shadow.db.t
67     chmod 0640 shadow.db.t
68     mv -f shadow.db.t shadow.db
69 fi
70 makedb "$HOST/group.tdb" -o group.db.t
71 mv -f passwd.db.t passwd.db
72 mv -f group.db.t group.db
73 for a in $HOST/ssh-rsa-shadow $HOST/ssh_known_hosts; do
74         ln -sf $a .
75 done
76 ln -sf `pwd -P`/ssh-rsa-shadow /etc/ssh
77 ln -sf `pwd -P`/ssh_known_hosts /etc/ssh
78
79 if [ -e ${HOST}/ssh-keys.tar.gz ]; then
80   export TMPDIR='/tmp/' 
81   tempdir=$(mktemp -d)
82   old=$(pwd -P)
83   cd $tempdir && tar -xvf ${old}/${HOST}/ssh-keys.tar.gz
84   cd old
85   mkdir userkeys 2> /dev/null || true
86   chmod 755 $tempdir
87   rsync -av --delete-after $tempdir/ userkeys/
88 fi
89
90 if [ -x /usr/bin/dchroot ]; then
91         CHROOTS=`dchroot --listpaths`
92         for c in $CHROOTS; do
93                 if [ -x "$c/usr/bin/makedb" ]
94                 then
95
96                         test ! -d "$c/var/lib/misc/$HOST" || mkdir -p "$c/var/lib/misc/$HOST"
97
98                         rsync -a ${verbose} $HOST/group.tdb $HOST/passwd.tdb $HOST/ssh* "$c/var/lib/misc/$HOST"
99
100                         test ! -f "$c/var/lib/misc/$HOST/shadow.tdb" || rm -f "$c/var/lib/misc/$HOST/shadow.tdb"
101                         test ! -f "$c/var/lib/misc/shadow.db" || rm -f "$c/var/lib/misc/shadow.db"
102
103                         chroot "$c" makedb "/var/lib/misc/$HOST/passwd.tdb" -o /var/lib/misc/passwd.db.t
104                         chroot "$c" makedb "/var/lib/misc/$HOST/group.tdb" -o /var/lib/misc/group.db.t
105                         mv -f "$c/var/lib/misc/passwd.db.t" "$c/var/lib/misc/passwd.db"
106                         mv -f "$c/var/lib/misc/group.db.t" "$c/var/lib/misc/group.db"
107                         ln -sf "$HOST/ssh_known_hosts" "$c/var/lib/misc/"
108                         ln -sf ../../var/lib/misc/ssh_known_hosts "$c/etc/ssh"
109                 fi
110         done
111 fi
112
113 if [ -d "/etc/exim4" -a -e "$HOST/bsmtp" ]; then
114         if perl -e 'exit !((stat "/etc/exim4/bsmtp")[9] < time()-3600)'; then
115                 cp "$HOST/bsmtp" /etc/exim4/bsmtp
116         fi
117 fi
118 if [ -d "/etc/postfix" -a -f "$HOST/forward-alias" ]; then
119         sed -e 's/:/@debian.org/' $HOST/forward-alias > /etc/postfix/debian
120         /usr/sbin/postmap hash:/etc/postfix/debian < /etc/postfix/debian || true
121 fi