-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmpshare.sh
executable file
·99 lines (97 loc) · 2.11 KB
/
tmpshare.sh
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
#!/bin/bash -e
if [ $# == 0 ]; then
echo "Share a directory using smbd"
echo "USAGE: $0 DIR [ssh user@host] [PORT [PASSWORD [SMBD_FLAGS...]]]"
exit 1
fi
root=$1
port=${2-445}
ssh=
if [ "$port" == ssh ]; then
ssh="&"
target=$3
if [ -z "$target" ]; then
echo "Target expected after 'ssh'"
exit 1
fi
shift 2
# TODO: Can default be a Unix domain socket?
# Even if that, share would be accessible from remote at $port
# Mb we should do random password?
port=${2-4445}
fi
password=${3-0000}
shift 3 || true
name=$(basename "$root")
user=$(whoami)
tmpdir=/tmp/smb$port
conf=$tmpdir/$user.conf
share_config="
path = $root
# guest ok = yes
writable = yes
force create mode = 774
force user = $user
"
become=
test "$port" -lt 1024 && become=sudo
set -xe
$become rm -fv /tmp/smbd-smb.conf.pid
mkdir -p $tmpdir
$become rm -rfv $tmpdir/*
echo "
[global]
security = user
smbd:backgroundqueue = no
lock dir = $tmpdir
private dir = $tmpdir
pid directory = $tmpdir
state directory = $tmpdir
ncalrpc dir = $tmpdir/ncalrpc
cache directory = $tmpdir/smbdcache
client min protocol = NT1
server min protocol = NT1
bind interfaces only = yes
# interfaces = lo vboxnet0
# unix extensions = no
follow symlinks = yes
map to guest = Bad User
[t]
$share_config
[$name]
$share_config
" > $conf
if [[ "$SAMBASRC" == *4*11* ]]; then
f="--log-stdout --debuglevel=2"
else
f="--debug-stdout --debuglevel=2"
fi
if [ "$SAMBASRC" == "" ]; then
pdbedit=pdbedit
smbd=smbd
else
smbd=smbd/smbd
pdbedit=utils/pdbedit
cd "$SAMBASRC/bin/default/source3"
fi
echo "$password
$password"| tee /dev/stderr| $pdbedit -D4 -s "$conf" -t -a -u $user
ulimit -n 2048
# Enable `fg`
set -m
$become $smbd -p $port --foreground --no-process-group $f\
--configfile="$conf" "$@" &
if [ -n "$ssh" ]; then
# TODO: uid=$(whoami) should be evaluated on the remote machine
${SSH-ssh} -R $port:localhost:$port "$target" -t\
"set -x; mkdir -p $name &&"\
"sudo mount.cifs //localhost/$name $name -o"\
soft,port=$port,user=$user,password=$password',uid=$(whoami) &&'\
"bash; sudo umount $name" || true
echo "Killing smbd..."
pkill -P $$
wait
else
jobs
fg
fi