Uncommit a change from aba after a little discussion on irc
[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 cd /tmp/
48 cd /var/lib/misc || cd /var/state/glibc/ || cd /var/db/
49 lockfile -r 1 -l 3600 lock
50 trap cleanup exit
51
52 case $HOST in
53 $LOCALSYNCON)
54     udhost=
55     ;;
56 *)
57     udhost="sshdist@$SYNCHOST:"
58     ;;
59 esac
60
61 rsync ${verbose} -e 'ssh -i /etc/ssh/ssh_host_rsa_key -o PreferredAuthentications=publickey' -rp "${udhost}/var/cache/userdir-ldap/hosts/$HOST" .
62
63 makedb "$HOST/passwd.tdb" -o passwd.db.t
64 if [ -s "$HOST/shadow.tdb" ]
65 then
66     (umask 027 && makedb "$HOST/shadow.tdb" -o shadow.db.t)
67     chown root.shadow shadow.db.t
68     chmod 0640 shadow.db.t
69     mv -f shadow.db.t shadow.db
70 fi
71 makedb "$HOST/group.tdb" -o group.db.t
72 mv -f passwd.db.t passwd.db
73 mv -f group.db.t group.db
74 for a in $HOST/ssh-rsa-shadow $HOST/ssh_known_hosts; do
75         ln -sf $a .
76 done
77 ln -sf `pwd -P`/ssh-rsa-shadow /etc/ssh
78 ln -sf `pwd -P`/ssh_known_hosts /etc/ssh
79
80 if [ -e ${HOST}/ssh-keys.tar.gz ]; then
81   export TMPDIR='/tmp/'
82   tempdir=$(mktemp -d)
83   tar -C "$tempdir" -xf ${HOST}/ssh-keys.tar.gz
84   [ -d userkeys ] || mkdir userkeys
85   chmod 755 $tempdir
86   rsync -a --delete-after $tempdir/ userkeys/
87 fi
88
89 CHROOTS=""
90 if [ -x /usr/bin/dchroot ]; then
91         CHROOTS=`dchroot --listpaths`
92 elif [ -x /usr/bin/dchroot-dsa ]; then
93         CHROOTS=$(dchroot-dsa -i | grep Location | awk '{print $2}')
94 fi
95 if [ -n "$CHROOTS" ]; then
96         for c in $CHROOTS; do
97                 if [ -x "$c/usr/bin/makedb" ]
98                 then
99
100                         test ! -d "$c/var/lib/misc/$HOST" || mkdir -p "$c/var/lib/misc/$HOST"
101
102                         rsync -a ${verbose} $HOST/group.tdb $HOST/passwd.tdb $HOST/ssh* "$c/var/lib/misc/$HOST"
103
104                         test ! -f "$c/var/lib/misc/$HOST/shadow.tdb" || rm -f "$c/var/lib/misc/$HOST/shadow.tdb"
105                         test ! -f "$c/var/lib/misc/shadow.db" || rm -f "$c/var/lib/misc/shadow.db"
106
107                         chroot "$c" makedb "/var/lib/misc/$HOST/passwd.tdb" -o /var/lib/misc/passwd.db.t
108                         chroot "$c" makedb "/var/lib/misc/$HOST/group.tdb" -o /var/lib/misc/group.db.t
109                         mv -f "$c/var/lib/misc/passwd.db.t" "$c/var/lib/misc/passwd.db"
110                         mv -f "$c/var/lib/misc/group.db.t" "$c/var/lib/misc/group.db"
111                         ln -sf "$HOST/ssh_known_hosts" "$c/var/lib/misc/"
112                         ln -sf ../../var/lib/misc/ssh_known_hosts "$c/etc/ssh"
113                 fi
114         done
115 fi
116
117 if [ -d "/etc/exim4" -a -e "$HOST/bsmtp" ]; then
118         if perl -e 'exit !((stat "/etc/exim4/bsmtp")[9] < time()-3600)'; then
119                 cp "$HOST/bsmtp" /etc/exim4/bsmtp
120         fi
121 fi
122 if [ -d "/etc/postfix" -a -f "$HOST/forward-alias" ]; then
123         sed -e 's/:/@debian.org/' $HOST/forward-alias > /etc/postfix/debian
124         /usr/sbin/postmap hash:/etc/postfix/debian < /etc/postfix/debian || true
125 fi