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