This file is indexed.

/usr/share/cluster/smb.sh is in resource-agents 1:3.9.2-5ubuntu4.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#!/bin/bash

#
# Script to manage a Samba file-sharing service component.
# Unline NFS, this should be placed at the top level of a service
# because it will try to gather information necessary to run the
# smbd/nmbd daemons at run-time from the service structure.
#
# Copyright (C) 1997-2003 Sistina Software, Inc.  All rights reserved.
# Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author(s):
#  Lon Hohberger (lhh at redhat.com)
#  Tim Burke (tburke at redhat.com)
#

LC_ALL=C
LANG=C
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export LC_ALL LANG PATH

#
# Definitions!
#
declare SAMBA_CONFIG_DIR=/etc/samba
declare SMBD_COMMAND=/usr/sbin/smbd
declare NMBD_COMMAND=/usr/sbin/nmbd
declare KILLALL_COMMAND=/usr/bin/killall
declare SAMBA_PID_DIR=/var/run/samba
declare SAMBA_LOCK_DIR=/var/cache/samba

#
# gross globals
#
declare -a ipkeys
declare -a fskeys

# Don't change please :)
_FAIL=255

. $(dirname $0)/ocf-shellfuncs

meta_data()
{
    cat <<EOT
<?xml version="1.0"?>
<resource-agent version="rgmanager 2.0" name="smb">
    <version>1.0</version>

    <longdesc lang="en">
    	Dynamic smbd/nmbd resource agent
    </longdesc>
    <shortdesc lang="en">
    	Dynamic smbd/nmbd resource agent
    </shortdesc>

    <parameters>
        <parameter name="name" unique="1" primary="1">
            <longdesc lang="en">
                Samba Symbolic Name.  This name will
		correspond to /etc/samba/smb.conf.NAME
            </longdesc>
            <shortdesc lang="en">
                Samba Name
            </shortdesc>
	    <content type="string"/>
        </parameter>

        <parameter name="workgroup">
            <longdesc lang="en">
	    	Workgroup name
            </longdesc>
            <shortdesc lang="en">
	    	Workgroup name
            </shortdesc>
	    <content type="string" default="LINUXCLUSTER"/>
        </parameter>

        <parameter name="service_name" inherit="service%name">
            <longdesc lang="en">
	    	Inherit the service name.  We need to know
		the service name in order to determine file
		systems and IPs for this smb service.
            </longdesc>
            <shortdesc lang="en">
	    	Inherit the service name.
            </shortdesc>
	    <content type="string"/>
        </parameter>
    </parameters>

    <actions>
        <action name="start" timeout="0"/>
        <action name="stop" timeout="0"/>

	<!-- This is just a wrapper for LSB init scripts, so monitor
	     and status can't have a timeout, nor do they do any extra
	     work regardless of the depth -->
        <action name="status" interval="30s" timeout="0"/>
        <action name="monitor" interval="30s" timeout="0"/>

        <action name="meta-data" timeout="0"/>
        <action name="validate-all" timeout="0"/>
    </actions>
</resource-agent>
EOT
}


#
# Usage: ccs_get key
#
ccs_get()
{
	declare outp
	declare key

	[ -n "$1" ] || return $_FAIL

	key="$*"

	outp=$(ccs_tool query "$key" 2>&1)
	if [ $? -ne 0 ]; then
		if [ "$outp" = "${outp/No data available/}" ] || [ "$outp" = "${outp/Operation not permitted/}" ]; then
			ocf_log err "$outp ($key)"
			return $_FAIL
		fi

		# no real error, just no data available
		return 0
	fi

	echo $outp

	return 0
}


#
# Build a list of service IP keys; traverse refs if necessary
#
get_service_ip_keys()
{
	declare svc=$1
	declare -i x y=0
	declare outp
	declare key

	#
	# Find service-local IP keys
	#
	x=1
	while : ; do
		key="/cluster/rm/service[@name=\"$svc\"]/ip[$x]"

		#
		# Try direct method
		#
		outp=$(ccs_get "$key/@address")
		if [ $? -ne 0 ]; then
			return 1
		fi

		#
		# Try by reference
		#
		if [ -z "$outp" ]; then
			outp=$(ccs_get "$key/@ref")
			if [ $? -ne 0 ]; then
				return 1
			fi
			key="/cluster/rm/resources/ip[@address=\"$outp\"]"
		fi

		if [ -z "$outp" ]; then
			break
		fi

		#ocf_log debug "IP $outp found @ $key"

		ipkeys[$y]="$key"

		((y++))
		((x++))
	done

	ocf_log debug "$y IP addresses found for $svc/$OCF_RESKEY_name"

	return 0
}


#
# Build a list of service fs keys, traverse refs if necessary
#
get_service_fs_keys()
{
	declare svc=$1
	declare -i x y=0
	declare outp
	declare key

	#
	# Find service-local IP keys
	#
	x=1
	while : ; do
		key="/cluster/rm/service[@name=\"$svc\"]/fs[$x]"

		#
		# Try direct method
		#
		outp=$(ccs_get "$key/@name")
		if [ $? -ne 0 ]; then
			return 1
		fi

		#
		# Try by reference
		#
		if [ -z "$outp" ]; then
			outp=$(ccs_get "$key/@ref")
			if [ $? -ne 0 ]; then
				return 1
			fi
			key="/cluster/rm/resources/fs[@name=\"$outp\"]"
		fi

		if [ -z "$outp" ]; then
			break
		fi

		#ocf_log debug "filesystem $outp found @ $key"

		fskeys[$y]="$key"

		((y++))
		((x++))
	done

	ocf_log debug "$y filesystems found for $svc/$OCF_RESKEY_name"

	return 0
}


build_ip_list()
{
	declare ipaddrs ipaddr
	declare -i x=0

	while [ -n "${ipkeys[$x]}" ]; do
		ipaddr=$(ccs_get "${ipkeys[$x]}/@address")
		if [ -z "$ipaddr" ]; then
			break
		fi

		ipaddrs="$ipaddrs $ipaddr"

		((x++))
	done

	echo $ipaddrs
}


add_sha1()
{
	declare sha1line="# rgmanager-sha1 $(sha1sum "$1")"
	echo $sha1line >> "$1"
}


verify_sha1()
{
	declare tmpfile="$(mktemp /tmp/smb-$OCF_RESKEY_name.tmp.XXXXXX)"
	declare current exp

	exp=$(grep "^# rgmanager-sha1.*$1" "$1" | head -1)
	if [ -z "$exp" ]; then
		# No sha1 line.  We're done.
		ocf_log debug "No SHA1 info in $1"
		return 1
	fi

	#
	# Find expected sha1 and expected file name
	#
	exp=${exp/*sha1 /}
	exp=${exp/ */}

	grep -v "^# rgmanager-sha1" "$1" > "$tmpfile"
	current=$(sha1sum "$tmpfile")
	current=${current/ */}

	rm -f "$tmpfile"

	if [ "$current" = "$exp" ]; then
		ocf_log debug "SHA1 sum matches for $1"
		return 0
	fi
	ocf_log debug "SHA1 sum does not match for $1"
	return 1
}


add_fs_entries()
{
	declare conf="$1"
	declare sharename
	declare sharepath key

	declare -i x=0

	while [ -n "${fskeys[$x]}" ]; do
		key="${fskeys[$x]}/@name"

		sharename=$(ccs_get "$key")
		if [ -z "$sharename" ]; then
			break
		fi

		key="${fskeys[$x]}/@mountpoint"
		sharepath=$(ccs_get "$key")
		if [ -z "$sharepath" ]; then
			break
		fi

		cat >> "$conf" <<EODEV
[$sharename]
	comment = Auto-generated $sharename share
	# Hide the secret cluster files
	veto files = /.clumanager/.rgmanager/
	browsable = yes
	writable = no
	public = yes
	path = $sharepath

EODEV

		((x++))
	done
}


#
# Generate the samba configuration if neede for this service.
#
gen_smb_conf()
{
	declare conf="$1"
	declare lvl="debug"

	if [ -f "$conf" ]; then
		verify_sha1 "$conf"
		if [ $? -ne 0 ]; then
			ocf_log debug "Config file changed; skipping"
			return 0
		fi
	else
		lvl="info"
	fi

	ocf_log $lvl "Creating $conf"

	get_service_ip_keys "$OCF_RESKEY_service_name"
	get_service_fs_keys "$OCF_RESKEY_service_name"

	cat > "$conf" <<EOT
#
# "$conf"
#
# This template configuration wass automatically generated, and will
# be automatically regenerated if removed.  Please modify this file to
# speficy subdirectories and/or client access permissions.
#
# Once this file has been altered, automatic re-generation will stop.
# Remember to copy this file to all other cluster members after making
# changes, or your SMB service will not operate correctly.
#
# From a cluster perspective, the key fields are:
#     lock directory - must be unique per samba service.
#     bind interfaces only - must be present set to yes.
#     interfaces - must be set to service floating IP address.
#     path - must be the service mountpoint or subdirectory thereof.
#

[global]
        workgroup = $OCF_RESKEY_workgroup
        pid directory = /var/run/samba/$OCF_RESKEY_name
        lock directory = /var/cache/samba/$OCF_RESKEY_name
	log file = /var/log/samba/%m.log
	#private dir = /var/
	encrypt passwords = yes
	bind interfaces only = yes
	netbios name = ${OCF_RESKEY_name/ /_}

	#
	# Interfaces are based on ip resources at the top level of
	# "$OCF_RESKEY_service_name"; IPv6 addresses may or may not
	# work correctly.
	#
	interfaces = $(build_ip_list)

#
# Shares based on fs resources at the top level of "$OCF_RESKEY_service_name"
#
EOT
	add_fs_entries "$conf"
	add_sha1 "$conf"

	return 0
}


#
# Kill off the specified PID
# (from clumanager 1.0.x/1.2.x)
#
# Killing off the samba daemons was miserable to implement, merely
# because killall doesn't distinguish by program commandline.
# Consequently I had to implement these routines to selectively pick 'em off.
#
# Kills of either the {smbd|nmbd} which is running and was started with
# the specified argument.  Can't use `killall` to do this because it
# doesn't allow you to distinguish which process to kill based on any
# of the program arguments.
#
# This routine is also called on "status" checks.  In this case it doesn't
# actually kill anything.
#
# Parameters:
#       daemonName - daemon name, can be either smbd or nmbd
#       command - [stop|start|status]
#       arg     - argument passed to daemon.  In this case its not the
#                 full set of program args, rather its really just the
#                 samba config file.
#
# Returns: 0 - success (or the daemon isn't currently running)
#          1 - failure
#
kill_daemon_by_arg()
{
    	declare daemonName=$1
	declare action=$2
	declare arg=$3
	# Create a unique temporary file to stash off intermediate results
	declare tmpfile_str=/tmp/sambapids.XXXXXX
	declare tmpfile
	declare ret

	tmpfile=$(mktemp $tmpfile_str); ret_val=$?

	if [ -z "$tmpfile" ]; then
		ocf_log err "kill_daemon_by_arg: Can't create tmp file"
		return $_FAIL
	fi

	# Mumble, need to strip off the /etc/samba portion, otherwise the
	# grep pattern matching will fail.
	declare confFile="$(basename $arg)"

	# First generate a list of candidate pids.
	pidof $daemonName > $tmpfile
	if [ $? -ne 0 ]; then
		ocf_log debug "kill_daemon_by_arg: no pids for $daemonName"
		rm -f $tmpfile
		case "$action" in
		'stop')
			return 0
		;;
		'status')
			return $_FAIL
		;;
		esac
		return 0
	fi

	# If you don't find any matching daemons for a "stop" operation, thats
	# considered success; whereas for "status" inquiries its a failure.
	case "$action" in
	'stop')
		ret=0
	;;
	'status')
		ret=$_FAIL
	;;
	esac
	#
	# At this point tmpfile contains a set of pids for the corresponding
	# {smbd|nmbd}.  Now look though this candidate set of pids and compare
	# the program arguments (samba config file name).  This distinguishes
	# which ones should be killed off.
	#
	declare daemonPid=""
	for daemonPid in $(cat $tmpfile); do
		declare commandLine=$(cat /proc/$daemonPid/cmdline)
		declare confBase="$(basename $commandLine)"
		if [ "$confBase" = "$confFile" ]; then
			case "$action" in
			'status')
				rm -f $tmpfile
				return 0
			;;
			esac
			kill_daemon_pid $daemonPid
			if [ $? -ne 0 ]; then
				ret=$_FAIL
				ocf_log err \
				"kill_daemon_by_arg: kill_daemon_pid $daemonPid failed"
			else
				ocf_log debug \
				 "kill_daemon_by_arg: kill_daemon_pid $daemonPid success"
			fi
		fi
	done
	rm -f $tmpfile
	return $ret
}


#
# Kill off the specified PID
# (from clumanager 1.0.x/1.2.x)
#
kill_daemon_pid()
{
	declare pid=$1
	declare retval=0


	kill -TERM $pid
	if [ $? -eq 0 ]; then
		ocf_log debug "Samba: successfully killed $pid"
	else
		ocf_log debug "Samba: failed to kill $pid"
		retval=$_FAIL
	fi
	return $retval
}


share_start_stop()
{
	declare command=$1
	declare conf="$SAMBA_CONFIG_DIR/smb.conf.$OCF_RESKEY_name"
	declare smbd_command
	declare nmbd_command
	declare netbios_name
	
	#
	# Specify daemon options
	# -D = spawn off as separate daemon
	# -s = the following arg specifies the config file
	#
	declare smbd_options="-D -s"
	declare nmbd_options="-D -s"

	if [ "$command" = "start" ]; then
		gen_smb_conf "$conf"
	else 
		if ! [ -f "$conf" ]; then
			ocf_log warn "\"$conf\" missing during $command"
		fi
	fi

	#
	# On clusters with multiple samba shares, we need to ensure (as much
	# as possible) that each service is advertised as a separate netbios
	# name.
	#
	# Generally, the admin sets this in smb.conf.NAME - but since
	# it is not required, we need another option.  Consequently, we use
	# smb instance name (which must be unique)
	#
	if [ -f "$conf" ]; then
		grep -qe "^\([[:space:]]\+n\|n\)etbios[[:space:]]\+name[[:space:]]*=[[:space:]]*[[:alnum:]]\+" "$conf"
		if [ $? -ne 0 ]; then

			netbios_name=$OCF_RESKEY_name

			ocf_log notice "Using $netbios_name as NetBIOS name (service $OCF_RESKEY_service_name)"
			nmbd_options=" -n $netbios_name $nmbd_options"
		fi
	fi

	case $command in
	start)
		ocf_log info "Starting Samba instance \"$OCF_RESKEY_name\""
		mkdir -p "$SAMBA_PID_DIR/$OCF_RESKEY_name"
		mkdir -p "$SAMBA_LOCK_DIR/$OCF_RESKEY_name"

		[ -f "$SMBD_COMMAND" ] || exit $OCF_ERR_INSTALLED
		[ -f "$NMBD_COMMAND" ] || exit $OCF_ERR_INSTALLED

		# Kick off the per-service smbd
		$SMBD_COMMAND $smbd_options "$conf"
		ret_val=$?
		if [ $ret_val -ne 0 ]; then
			ocf_log err "Samba service failed: $SMBD_COMMAND $smbd_options \"$conf\""
			return $_FAIL
		fi
		ocf_log debug "Samba service succeeded: $SMBD_COMMAND $smbd_options \"$conf\""

		# Kick off the per-service nmbd
		$NMBD_COMMAND $nmbd_options "$conf"
		ret_val=$?
		if [ $ret_val -ne 0 ]; then
			ocf_log err "Samba service failed: $NMBD_COMMAND $nmbd_options \"$conf\""
			return $_FAIL
		fi
		ocf_log debug "Samba service succeeded: $NMBD_COMMAND $nmbd_options \"$conf\""
	;;
	stop)
		ocf_log info "Stopping Samba instance \"$OCF_RESKEY_name\""

       		kill_daemon_by_arg "nmbd" $command "$conf"
       		kill_daemon_by_arg "smbd" $command "$conf"
       		if [ "$SAMBA_PID_DIR/$OCF_RESKEY_name" != "/" ]; then
       			pushd "$SAMBA_PID_DIR" &> /dev/null
       			rm -rf "$OCF_RESKEY_name"
	       		popd &> /dev/null
		fi
		if [ "$SAMBA_LOCK_DIR/$OCF_RESKEY_name" != "/" ]; then
     			pushd "$SAMBA_LOCK_DIR" &> /dev/null
			rm -rf "$OCF_RESKEY_name"
			popd &> /dev/null
		fi
	;;
	status)
		ocf_log debug "Checking Samba instance \"$OCF_RESKEY_name\""
		kill_daemon_by_arg "nmbd" $command "$conf"
		if [ $? -ne 0 ]; then
			ocf_log err \
			"share_start_stop: nmbd for service $svc_name died!"
			return $_FAIL
		fi
		kill_daemon_by_arg "smbd" $command "$conf"
		if [ $? -ne 0 ]; then
			ocf_log err \
		"share_start_stop: nmbd for service $svc_name died!"
			return $_FAIL
		fi
	;;
	esac
}


verify_all()
{
	[ -z "$OCF_RESKEY_workgroup" ] && export OCF_RESKEY_workgroup="LINUXCLUSTER"
	[ -n "${OCF_RESKEY_name}" ] || exit $OCF_ERR_ARGS      # Invalid Argument
	if [ -z "${OCF_RESKEY_service_name}" ]; then
		ocf_log ERR "Samba service ${OCF_RESKEY_name} is not the child of a service"
		exit $OCF_ERR_ARGS
	fi
}

case $1 in
	meta-data)
		meta_data
		exit 0
	;;
	start|stop)
		verify_all
		share_start_stop $1
		exit $?
	;;
	status|monitor)
		verify_all
		share_start_stop status
		exit $?
	;;
	validate-all)
		verify_all
		echo "Yer radio's workin', driver!"
		exit 0
	;;
	*)
		echo "usage: $0 {start|stop|status|monitor|meta-data|validate-all}"
		exit $OCF_ERR_UNIMPLEMENTED
	;;
esac