Skip to content

Commit 16692d5

Browse files
committedOct 22, 2015
Initial check-in of scripts.
1 parent 2228495 commit 16692d5

File tree

7 files changed

+358
-0
lines changed

7 files changed

+358
-0
lines changed
 

‎README.md

+16
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
# samba-hacking
22
A collection of utility scripts for hacking Samba.
3+
4+
## General Hacking
5+
6+
* **wafbuild.sh:** Store my usual configure parameters. Distclean, reconfigure, rebuild.
7+
8+
## Clustered Hacking
9+
10+
* **enlighten.sh:** Distribute working repository (on my laptop) to remote nodes via SSH + rsync.
11+
* **rebirth.sh:** Kill and restart Samba processes.
12+
13+
## Witness Hacking
14+
15+
* **resource-change.sh:** Trigger a Witness resource change notification.
16+
* **wisdom.sh:** Hack to allow source4 samba to read AD credentials stored in CTDB.
17+
* **witness-me.sh:** Script to monitor Samba, Witness, and CTDB.
18+

‎enlighten.sh

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/bin/bash
2+
3+
# enlighten.sh
4+
#
5+
# A clustered Samba hacking script to synchronize the code repositories on the
6+
# cluster nodes.
7+
8+
DEFAULT_NODES="ganesh buddhi riddhi siddhi"
9+
10+
get_help() {
11+
echo "USAGE: enlighten.sh [-bcf] [<node> ...]
12+
13+
A clustered Samba hacking script to synchronize the code repositories on the
14+
cluster nodes. By default, this script only tries to update the code on remote
15+
nodes via rsync. Nodes are specified as a space-separated list of names or IP
16+
address. If no nodes are specified, the following list is used:
17+
18+
$DEFAULT_NODES
19+
20+
Options:
21+
-b, --build
22+
Run a Samba build on the first node before distributing the repo to the
23+
other nodes.
24+
25+
-c, --configure
26+
Clean the repo, run configure, and do a Samba build. Implies --build.
27+
28+
-f, --force
29+
Normally, this script attempts to synchronize the repositories using an
30+
rsync batched write. This can be really finnicky if you change anything
31+
on the remote repositories. Use --force to perform a full, non-batched
32+
rsync on every node. This is much slower but will usually succeed.
33+
"
34+
}
35+
36+
BUILD=false
37+
FORCE=false
38+
CMD="make -j4"
39+
40+
41+
while [[ $# > 0 ]]; do
42+
ARG="$1"
43+
44+
if [[ ${ARG:0:1} != "-" ]]; then
45+
break
46+
fi
47+
48+
OPTS=()
49+
50+
if [[ ${#ARG} -gt 2 && ${ARG:0:2} != "--" && ${ARG:0:1} == "-" ]]; then
51+
for (( i=1; i<${#ARG}; i++ )); do
52+
OPTS+=("-${ARG:$i:1}")
53+
done
54+
else
55+
OPTS+=($ARG)
56+
fi
57+
58+
for OPT in "${OPTS[@]}"; do
59+
case $OPT in
60+
-b|--build)
61+
BUILD=true
62+
shift
63+
;;
64+
-f|--force)
65+
FORCE=true
66+
shift
67+
;;
68+
-c|--configure)
69+
BUILD=true
70+
CMD="./wafbuild.sh"
71+
shift
72+
;;
73+
*)
74+
echo "Unknown option: $OPT"
75+
get_help
76+
exit
77+
;;
78+
esac
79+
done
80+
done
81+
82+
nodes=${@:-"$DEFAULT_NODES"}
83+
84+
echo "BUILD: $BUILD"
85+
echo "FORCE: $FORCE"
86+
echo "CMD..: $CMD"
87+
echo "NODES: $nodes"
88+
89+
exit
90+
91+
rm -rf rsync-batch*; rm -rf ../samba-rsync/rsync-batch*
92+
if $FORCE; then
93+
rsync -rltvD . ../samba-rsync >/dev/null
94+
else
95+
rsync --write-batch=rsync-batch -rltvD . ../samba-rsync >/dev/null
96+
fi
97+
98+
sync_repo() {
99+
echo "Syncing $1..."
100+
ssh $1 "rm -rf rsync_batch*; rm -rf samba/rsync-batch* >/dev/null"
101+
102+
if $FORCE; then
103+
rsync -rltvD . $1:samba >/dev/null
104+
else
105+
scp -q rsync-batch* $1:
106+
ssh $1 "./rsync-batch.sh samba >/dev/null"
107+
fi
108+
}
109+
110+
if $BUILD; then
111+
head=$(echo "$nodes" | awk '{ print $1 }')
112+
others=${nodes#$head }
113+
114+
echo "Syncing $head..."
115+
sync_repo $head
116+
117+
echo "Running command [$CMD] on $head..."
118+
ssh $head "cd samba; $CMD"
119+
ret=$?
120+
121+
if [ $ret == 0 ]; then
122+
if $FORCE; then
123+
FFLAG="f"
124+
else
125+
FFLAG=""
126+
fi
127+
ssh $head "cd samba; ./enlighten.sh -${FFLAG} ${others}"
128+
fi
129+
else
130+
for n in $nodes; do
131+
sync_repo $n
132+
done
133+
fi

‎rebirth.sh

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/bash
2+
3+
# rebirth.sh
4+
#
5+
# A clustered Samba hacking script to kill and restart Samba processes on the
6+
# cluster nodes. This script should be run locally on the individual nodes.
7+
8+
TIMEOUT_SECS=0
9+
DEFAULT_PROCS="ctdbd samba smbd nmbd winbindd"
10+
11+
get_help() {
12+
echo "USAGE: rebirth.sh [<process> ...]
13+
14+
A clustered Samba hacking script to kill and restart Samba processes on the
15+
cluster nodes. This script should be run locally on the individual nodes.
16+
Processes to be killed and restarted can be specified by name as parameters on
17+
the command line. By default, the following list of processes is defined:
18+
19+
$DEFAULT_PROCS
20+
"
21+
}
22+
23+
PROCS=""
24+
RESTART_SAMBA=false
25+
RESTART_CTDB=false
26+
RESTART_WB=false
27+
28+
while [[ $# > 0 ]]; do
29+
PROG="$1"
30+
31+
case $PROG in
32+
ctdb|ctdbd)
33+
PROCS="$PROCS ctdbd"
34+
;;
35+
samba|smb|smbd)
36+
PROCS="$PROCS samba smbd nmbd"
37+
;;
38+
winbind|winbindd|wb)
39+
PROCS="$PROCS winbindd"
40+
;;
41+
all)
42+
PROCS=""
43+
;;
44+
*)
45+
echo "Unknown process: " $PROC
46+
get_help
47+
exit 1
48+
esac
49+
50+
shift
51+
done
52+
53+
PROCS=${PROCS:-"$DEFAULT_PROCS"}
54+
55+
if [[ $PROCS == *"samba"* ]] || [[ $PROCS == *"smbd"* ]] || [[ $PROCS == *"nmbd"* ]]; then
56+
RESTART_SAMBA=true
57+
fi
58+
if [[ $PROCS == *"ctdbd"* ]]; then
59+
RESTART_CTDB=true
60+
fi
61+
if [[ $PROCS == *"winbindd"* ]]; then
62+
RESTART_WB=true
63+
fi
64+
65+
echo "Killing processes [$PROCS]"
66+
killall -9 $PROCS >/dev/null 2>&1
67+
killall -9 $PROCS >/dev/null 2>&1
68+
69+
if $RESTART_SAMBA; then
70+
rm -rf /var/run/witnessd.pid
71+
fi
72+
73+
mkdir -p /var/run/samba
74+
mkdir -p /var/run/ctdb
75+
76+
if $RESTART_CTDB; then
77+
echo -n "Starting CTDB"
78+
bin/default/ctdb/ctdbd --reclock /data/lock-mnt/reclock --pidfile /var/run/ctdb/ctdbd.pid --event-script-dir ctdb/config/events.d/ --public-addresses=/etc/ctdb/public_addresses
79+
N=0
80+
STATUS=$(bin/default/ctdb/ctdb status 2>&1)
81+
NSTATUS=$(echo "$STATUS" | grep "THIS NODE" | awk '{ print $3 }')
82+
until [ "$NSTATUS" == "OK" ] || [ "$NSTATUS" == *"BANNED"* ] || [ "$STATUS" == *"Errno"* ] || [ $N -ge $TIMEOUT_SECS ]; do
83+
echo -n "."
84+
(( N++ ))
85+
sleep 1
86+
STATUS=$(bin/default/ctdb/ctdb status 2>&1)
87+
NSTATUS=$(echo "$STATUS" | grep "THIS NODE" | awk '{ print $3 }')
88+
#echo -n "$NSTATUS"
89+
done
90+
if [ "$NSTATUS" == *"BANNED"* ]; then
91+
echo -e "BANNED?!\nCTDB failed to start, check logs."
92+
exit 1
93+
fi
94+
if [ $N -ge $TIMEOUT_SECS ]; then
95+
echo -e "TIMEOUT!\nCTDB failed to start, check logs."
96+
exit 1
97+
fi
98+
if [ "$STATUS" == *"Errno"* ]; then
99+
echo -e "ERROR!!\nCTDB failed to start, check logs."
100+
echo "$STATUS"
101+
exit 1
102+
fi
103+
echo "OK!"
104+
fi
105+
106+
if $RESTART_SAMBA || $RESTART_WB; then
107+
echo "Starting Samba daemons"
108+
bin/samba >/dev/null 2>&1
109+
fi
110+
111+
echo "Done."
112+

‎resource-change.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
#!/bin/bash
4+
5+
BROADCAST=""
6+
RESOURCE=""
7+
STATE=""
8+
9+
while [[ $# > 0 ]]; do
10+
ARG="$1"
11+
12+
case $ARG in
13+
-n|--nodes)
14+
BROADCAST="/root/samba/bin/default/ctdb/onnode -p $2 "
15+
shift
16+
;;
17+
--)
18+
;;
19+
*)
20+
RESOURCE=$1
21+
STATE=$2
22+
if [[ "x$RESOURCE" == "x" ]] || [[ "x$STATE" == "x" ]]; then
23+
echo "Invalid parameters: RESOURCE[$RESOURCE] STATE[$STATE]"
24+
exit 1
25+
fi
26+
shift
27+
esac
28+
shift
29+
done
30+
31+
${BROADCAST}/root/samba/bin/smbcontrol witnessd witnessnotify change $RESOURCE $STATE

‎wafbuild.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
PDB_SHARED_MODULES="pdb_ads,pdb_tdbsam,pdb_smbpasswd,pdb_wbc_sam"
2+
IDMAP_SHARED_MODULES="idmap_ipa,idmap_rid,idmap_ad,idmap_adex,idmap_hash,idmap_tdb2,idmap_ldap"
3+
GPEXT_SHARED_MODULES="gpext_security,gpext_registry,gpext_scripts"
4+
VFS_SHARED_MODULES="vfs_glusterfs"
5+
6+
CONFIGURE_OPTS=" \
7+
--prefix=/root/samba/ \
8+
--localstatedir=/var \
9+
--sysconfdir=/etc \
10+
--with-lockdir=/var/lib/samba \
11+
--with-piddir=/var/run \
12+
--with-privatedir=/etc/samba \
13+
--with-statedir=/var/lib/samba \
14+
--with-cachedir=/var/lib/samba \
15+
--with-quotas \
16+
--with-pam \
17+
--with-shared-modules=$IDMAP_SHARED_MODULES,$PDB_SHARED_MODULES,$GPEXT_SHARED_MODULES,$VFS_SHARED_MODULES \
18+
--with-ads \
19+
--with-dnsupdate \
20+
--enable-developer \
21+
--with-aio-support \
22+
--with-logfilebase=/var/log/samba \
23+
--enable-selftest \
24+
--with-selftest-prefix=./st \
25+
--enable-fhs \
26+
--with-cluster-support \
27+
$@"
28+
# --libdir=/usr/lib64 \
29+
# --mandir=/usr/share/man \
30+
# --with-modulesdir=/usr/lib64/samba \
31+
32+
make distclean && ./buildtools/bin/waf configure $CONFIGURE_OPTS && make -j4

‎wisdom.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
bin/default/ctdb/ctdb catdb secrets.tdb | grep -v "dmaster\|rsn\|Dumped" | sed -e 's/key/{\nkey/' -e 's/^$/}/' >foodb
4+
rm -f /etc/samba/secrets.*
5+
tdbrestore /etc/samba/secrets.tdb <foodb

‎witness-me.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
SHOW_LOG=""
4+
5+
while [[ $# > 0 ]]; do
6+
OPT="$1"
7+
8+
case $OPT in
9+
-l|--log)
10+
SHOW_LOG="echo; echo '==== /var/log/samba/log.samba ====='; tail -25 /var/log/samba/log.samba"
11+
;;
12+
*)
13+
echo "Unknown opt: " $OPT
14+
exit 1
15+
esac
16+
17+
shift
18+
done
19+
20+
function WPID() {
21+
if [ -e /var/run/witnessd.pid ]; then
22+
echo "Witness PID: $(cat /var/run/witnessd.pid)"
23+
else
24+
echo "Witness PID: NONE"
25+
fi
26+
}
27+
28+
#watch -n 1 "bin/smbstatus -S; echo; netstat -4dnp | grep \":1024\" | grep samba | awk '{print \"PID: \" substr(\$7, 0, index(\$7, \"/\")-1) \" RPC (Witness) Conn: \" substr(\$4, 0, index(\$4,\":\")-1)}' | uniq; echo; bin/default/ctdb/ctdb status; bin/default/ctdb/ctdb ip; $SHOW_LOG"
29+
watch -tn 1 "bin/smbstatus -p; echo `WPID`; echo; bin/default/ctdb/ctdb status; bin/default/ctdb/ctdb ip; $SHOW_LOG"

0 commit comments

Comments
 (0)
Please sign in to comment.