5338c927e012cd1a76eccd05b92fef16cc2a6ba5
[sahlberg/ctdb.git] / config / events.d / 50.samba
1 #!/bin/sh
2 # ctdb event script for Samba
3
4 . $CTDB_BASE/functions
5
6 detect_init_style
7
8 case $CTDB_INIT_STYLE in
9         suse)
10                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
11                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
12                 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
13                 ;;
14         debian)
15                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
16                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
17                 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
18                 ;;
19         *)
20                 # should not happen, but for now use redhat style as default:
21                 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
22                 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
23                 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
24                 ;;
25 esac
26
27 service_name="samba"
28 service_start="start_samba"
29 service_stop="stop_samba"
30
31 loadconfig
32
33 start_samba() {
34         # create the state directory for samba
35         /bin/mkdir -p $CTDB_VARDIR/state/samba
36
37         # make sure samba is not already started
38         [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
39                 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
40                 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
41                 killall -0 -q smbd && {
42                     sleep 1
43                     # make absolutely sure samba is dead
44                     killall -q -9 smbd
45                 }
46
47                 killall -0 -q nmbd && {
48                     sleep 1
49                     # make absolutely sure samba is dead
50                     killall -q -9 nmbd
51                 }
52         }
53
54         # make sure winbind is not already started
55         check_ctdb_manages_winbind
56         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
57                 service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
58                 killall -0 -q winbindd && {
59                     sleep 1
60                     # make absolutely sure winbindd is dead
61                     killall -q -9 winbindd
62                 }
63
64         }
65
66         /usr/bin/net serverid wipe
67
68         # start the winbind service
69         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
70                 service "$CTDB_SERVICE_WINBIND" start
71         }
72
73         # start Samba service. Start it reniced, as under very heavy load 
74         # the number of smbd processes will mean that it leaves few cycles for
75         # anything else
76         [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
77                 nice_service "$CTDB_SERVICE_NMB" start
78                 nice_service "$CTDB_SERVICE_SMB" start
79         }
80 }
81
82 stop_samba() {
83         # shutdown Samba when ctdb goes down
84         [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
85                 service "$CTDB_SERVICE_SMB" stop
86                 service "$CTDB_SERVICE_NMB" stop
87         }
88
89         # stop the winbind service
90         check_ctdb_manages_winbind
91         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
92                 service "$CTDB_SERVICE_WINBIND" stop
93         }
94 }
95
96 # set default samba cleanup period - in minutes
97 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
98     SAMBA_CLEANUP_PERIOD=10
99 }
100
101 # we keep a cached copy of smb.conf here
102 smbconf_cache="$CTDB_VARDIR/state/samba/smb.conf.cache"
103
104
105 #############################################
106 # update the smb.conf cache in the foreground
107 testparm_foreground_update() {
108     mkdir -p "$CTDB_VARDIR/state/samba" || exit 1
109     testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
110 }
111
112 #############################################
113 # update the smb.conf cache in the background
114 testparm_background_update() {
115     # if the cache doesn't exist, then update in the foreground
116     [ -f $smbconf_cache ] || {
117         testparm_foreground_update
118     }
119     # otherwise do a background update
120     (
121         tmpfile="${smbconf_cache}.$$"
122         testparm -s > $tmpfile 2> /dev/null &
123         # remember the pid of the teamparm process
124         pid="$!"
125         # give it 10 seconds to run
126         timeleft=10
127         while [ $timeleft -gt 0 ]; do
128             timeleft=$(($timeleft - 1))
129             # see if the process still exists
130             /bin/kill -0 $pid > /dev/null 2>&1 || {
131                 # it doesn't exist, grab its exit status
132                 wait $pid
133                 [ $? = 0 ] || {
134                     echo "50.samba: smb.conf background update exited with status $?"
135                     rm -f "${tmpfile}"
136                     exit 1
137                 }               
138                 # put the new smb.conf contents in the cache (atomic rename)
139                 # make sure we remove references to the registry while doing 
140                 # this to ensure that running testparm on the cache does
141                 # not use the registry
142                 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
143                 rm -f "$tmpfile"
144                 mv -f "${tmpfile}.2" "$smbconf_cache" || {
145                     echo "50.samba: failed to update background cache"
146                     rm -f "${tmpfile}.2"
147                     exit 1
148                 }
149                 exit 0
150             }
151             # keep waiting for testparm to finish
152             sleep 1
153         done
154         # it took more than 10 seconds - kill it off
155         rm -f "${tmpfile}"
156         /bin/kill -9 "$pid" > /dev/null 2>&1
157         echo "50.samba: timed out updating smbconf cache in background"
158         exit 1
159     ) &
160 }
161
162 ##################################################
163 # show the testparm output using a cached smb.conf 
164 # to avoid registry access
165 testparm_cat() {
166     [ -f $smbconf_cache ] || {
167         testparm_foreground_update
168     }
169     testparm -s "$smbconf_cache" "$@" 2>/dev/null
170 }
171
172 # function to see if ctdb manages winbind
173 check_ctdb_manages_winbind() {
174   [ -z "$CTDB_MANAGES_WINBIND" ] && {
175     secmode=`testparm_cat --parameter-name=security`
176     case $secmode in
177         ADS|DOMAIN)
178             CTDB_MANAGES_WINBIND="yes";
179             ;;
180         *)
181             CTDB_MANAGES_WINBIND="no";
182             ;;
183     esac
184   }
185 }
186
187 list_samba_shares ()
188 {
189     testparm_cat |
190     sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
191     sed -e 's/"//g'
192 }
193
194
195 ###########################
196 # periodic cleanup function
197 periodic_cleanup() {
198     # running smbstatus scrubs any dead entries from the connections
199     # and sessionid database
200     # echo "Running periodic cleanup of samba databases"
201     smbstatus -np > /dev/null 2>&1 &
202 }
203
204 ###########################
205
206 ctdb_start_stop_service
207 ctdb_start_stop_service "windbind"
208
209 is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
210
211 ###########################
212
213 case "$1" in 
214      startup)
215         ctdb_service_start
216         ;;
217         
218      shutdown)
219         ctdb_service_stop
220         ;;
221
222      monitor)
223         # Create a dummy file to track when we need to do periodic cleanup
224         # of samba databases
225         [ -f $CTDB_VARDIR/state/samba/periodic_cleanup ] || {
226                 touch $CTDB_VARDIR/state/samba/periodic_cleanup
227         }
228         [ `/usr/bin/find $CTDB_VARDIR/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
229                 # Cleanup the databases
230                 periodic_cleanup
231                 touch $CTDB_VARDIR/state/samba/periodic_cleanup
232         }
233
234         [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
235                 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
236                         testparm_background_update
237
238                         testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
239                             testparm_foreground_update
240                             testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
241                                 echo "ERROR: testparm shows smb.conf is not clean"
242                                 exit 1
243                             }
244                         }
245                         
246                         list_samba_shares |
247                         ctdb_check_directories_probe || {
248                             testparm_foreground_update
249                             list_samba_shares |
250                             ctdb_check_directories
251                         } || exit $?
252                 }
253
254                 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
255                 [ -z "$smb_ports" ] && {
256                         smb_ports=`testparm_cat --parameter-name="smb ports"`
257                 }
258                 ctdb_check_tcp_ports $smb_ports || exit $?
259         }
260
261         # check winbind is OK
262         check_ctdb_manages_winbind
263         [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
264                 ctdb_check_command "winbind" "wbinfo -p"
265         }
266         ;;
267
268     *)
269         ctdb_standard_event_handler "$@"
270         ;;
271 esac
272
273 exit 0