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