ud-replicate: move from lockfile(1) to flock
[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 -rf $tempdir
41   rm -f  $tempfile
42   rm -f  $tempfile2
43 }
44
45 PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
46 export PATH
47 HOST=`hostname -f`
48 SYNCHOST=`ud-config synchost`;
49 LOCALSYNCON=`ud-config localsyncon`;
50 EMAILAPPEND=`ud-config emailappend`;
51 cd /var/lib/misc
52 exec 200< "."
53 if ! flock -w 60 -e 200; then
54     log "Cannot acquire lock on `pwd`"
55     exit 1
56 fi
57
58 trap cleanup exit
59
60 case $HOST in
61 $LOCALSYNCON)
62     udhost=
63     ;;
64 *)
65     udhost="sshdist@$SYNCHOST:"
66     ;;
67 esac
68
69 tempfile=$(mktemp)
70 tempfile2=$(mktemp)
71
72 if [ -e /var/lib/misc/thishost/dns-sshfp ]; then
73   cp /var/lib/misc/thishost/dns-sshfp $tempfile
74 fi
75
76 if [ -e /var/lib/misc/thishost/dns-zone ]; then
77   cp /var/lib/misc/thishost/dns-zone $tempfile2
78 fi
79
80 rsync ${verbose} --delete-after --times -e 'ssh -i /etc/ssh/ssh_host_rsa_key -o ControlPath=/var/run/.ud-replicate.ssh.socket -o ControlMaster=auto -o ControlPersist=2h -o BatchMode=yes' -rp "${udhost}/var/cache/userdir-ldap/hosts/$HOST" .
81
82 makedb "$HOST/passwd.tdb" -o passwd.db.t
83 if [ -s "$HOST/shadow.tdb" ]
84 then
85     (umask 027 && makedb "$HOST/shadow.tdb" -o shadow.db.t)
86     chown root.shadow shadow.db.t
87     chmod 0640 shadow.db.t
88     mv -f shadow.db.t shadow.db
89 fi
90 makedb "$HOST/group.tdb" -o group.db.t
91 mv -f passwd.db.t passwd.db
92 mv -f group.db.t group.db
93 if [ -e "$HOST/ssh-rsa-shadow" ]; then
94         ln -sf $HOST/ssh-rsa-shadow .
95         ln -sf `pwd -P`/ssh-rsa-shadow /etc/ssh
96 else
97         rm -f ssh-rsa-shadow /etc/ssh/ssh-rsa-shadow
98 fi
99 ln -sf $HOST/ssh_known_hosts .
100 ln -sf `pwd -P`/ssh_known_hosts /etc/ssh
101
102 if [ -e ${HOST}/ssh-keys.tar.gz ]; then
103   export TMPDIR='/tmp/'
104   tempdir=$(mktemp -d)
105   tar -C "$tempdir" -xf ${HOST}/ssh-keys.tar.gz
106   mkdir -p userkeys
107   chmod 755 $tempdir
108   rsync -a --delete-after $tempdir/ userkeys/
109 fi
110
111 if [ -e ${HOST}/web-passwords ]; then
112         chown root:www-data ${HOST}/web-passwords
113         chmod 0640 ${HOST}/web-passwords
114 fi
115
116 if [ -d "/etc/exim4" -a -e "$HOST/bsmtp" ]; then
117         if perl -e 'exit !((stat "/etc/exim4/bsmtp")[9] < time()-3600)'; then
118                 cp "$HOST/bsmtp" /etc/exim4/bsmtp
119         fi
120 fi
121 if [ -d "/etc/postfix" -a -f "$HOST/forward-alias" ]; then
122         sed -e "s/:/@$EMAILAPPEND/" $HOST/forward-alias > /etc/postfix/debian
123         /usr/sbin/postmap hash:/etc/postfix/debian < /etc/postfix/debian || true
124 fi
125
126 rebuild_zones=0
127 if [ -e /var/lib/misc/thishost/dns-sshfp ]; then
128   if ! cmp -s /var/lib/misc/thishost/dns-sshfp "$tempfile"; then
129     rebuild_zones=1
130   fi
131 fi
132
133 if [ -e /var/lib/misc/thishost/dns-zone ]; then
134   if ! cmp -s /var/lib/misc/thishost/dns-zone "$tempfile2"; then
135     rebuild_zones=1
136   fi
137 fi
138
139 if [ "${rebuild_zones}" -gt 0 ]; then
140   sudo -u dnsadm /srv/dns.debian.org/bin/update
141 fi
142
143 if [ -d "/etc/freeradius" -a -e "$HOST/rtc-passwords" ]; then
144         if ! cmp -s "$HOST/rtc-passwords" /var/local/rtc-passwords.freerad; then
145                 install -o freerad -g freerad -m 400 "$HOST/rtc-passwords" /var/local/rtc-passwords.freerad
146                 service freeradius reload
147         fi
148 fi
149 if [ -d "/etc/reTurn" -a -e "$HOST/rtc-passwords" ]; then
150         if ! cmp -s "$HOST/rtc-passwords" /var/local/rtc-passwords.return; then
151                 install -o return -g return -m 400 "$HOST/rtc-passwords" /var/local/rtc-passwords.return
152                 service resiprocate-turn-server reload
153         fi
154 fi
155 if [ -d "/etc/prosody" -a -e "$HOST/rtc-passwords" ]; then
156         if ! cmp -s "$HOST/rtc-passwords" /var/local/rtc-passwords.prosody; then
157                 install -o prosody -g prosody -m 400 "$HOST/rtc-passwords" /var/local/rtc-passwords.prosody
158                 service prosody reload
159         fi
160 fi