blob: b849a1cd370d182f93f4ad1656642bbcc8410deb [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh /etc/rc.common
2
3START=98
4STOP=05
5USE_PROCD=1
6
7SAMBA_IFACE=""
8
9config_get_sane() {
10 config_get "$@"
11 set -- "$(echo "$1" | tr -d '<>[]{};%?=#\n')"
12}
13
14smb_header() {
15 config_get_sane SAMBA_IFACE "$1" interface "lan"
16
17 # resolve interfaces
18 interfaces=$(
19 . /lib/functions/network.sh
20
21 for net in $SAMBA_IFACE; do
22 network_is_up "$net" || continue
23 network_get_device device "$net"
24 printf "%s " "${device:-$net}"
25 done
26 )
27
28 # we dont use netbios anymore as default and wsd/avahi is dns based
29 hostname="$(sed 's/\..*//' /proc/sys/kernel/hostname | tr -d '{};%?=#\n')"
30
31 config_get_sane workgroup "$1" workgroup "WORKGROUP"
32 config_get_sane description "$1" description "Samba on OpenWrt"
33 config_get_sane charset "$1" charset "UTF-8"
34
35 config_get_bool MACOS "$1" macos 0
36 config_get_bool DISABLE_NETBIOS "$1" disable_netbios 0
37 config_get_bool DISABLE_AD_DC "$1" disable_ad_dc 0
38 config_get_bool DISABLE_WINBIND "$1" disable_winbind 0
39 config_get_bool DISABLE_ASYNC_IO "$1" disable_async_io 0
40 config_get_bool ALLOW_LEGACY_PROTOCOLS "$1" allow_legacy_protocols 0
41 config_get_bool ENABLE_EXTRA_TUNING "$1" enable_extra_tuning 0
42
43 mkdir -p /var/etc
44 sed -e "s#|NAME|#$hostname#g" \
45 -e "s#|WORKGROUP|#$workgroup#g" \
46 -e "s#|DESCRIPTION|#$description#g" \
47 -e "s#|INTERFACES|#$interfaces#g" \
48 -e "s#|CHARSET|#$charset#g" \
49 /etc/samba/smb.conf.template > /var/etc/smb.conf
50
51 {
52 printf "\n######### Dynamic written config options #########\n"
53
54 # extra tuning options by community feedback (kinda try&error)
55 if [ "$ENABLE_EXTRA_TUNING" -eq 1 ]; then
56 socket_opt="$(grep -i 'socket options' /etc/samba/smb.conf.template | awk -F'=' '{print $2}' | tr -d '\n')"
57 [ -n "$socket_opt" ] && printf "\tsocket options =%s SO_KEEPALIVE\n" "$socket_opt" # add keepalive, maybe larger buffer? SO_RCVBUF=65536 SO_SNDBUF=65536
58
59 printf "\tmax xmit = 131072\n" # increase smb1 transmit size
60 printf "\tmin receivefile size = 131072\n" # allows zero-copy writes via fs
61 printf "\tfake oplocks = Yes\n" # may corrupt files for simultanous writes to the same files by multiple clients, but might also see big speed boost
62 printf "\tuse sendfile = Yes\n" # enable sendfile?
63 fi
64
65 if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
66 printf "\tdisable netbios = yes\n"
67 # note: samba opens port 139 even if netbios is disabled via option above, so adjust listening ports
68 printf "\tsmb ports = 445\n"
69 fi
70
71 if [ "$DISABLE_ASYNC_IO" -eq 1 ]; then
72 printf "\taio read size = 0\n"
73 printf "\taio write size = 0\n"
74 fi
75
76 if [ "$ALLOW_LEGACY_PROTOCOLS" -eq 1 ]; then
77 logger -p daemon.info -t 'samba4-server' "Legacy Protocols allowed, don't use this option for secure environments!"
78 printf "\tserver min protocol = NT1\n"
79 printf "\tlanman auth = yes\n"
80 printf "\tntlm auth = ntlmv1-permitted\n"
81 fi
82 } >> /var/etc/smb.conf
83
84 [ -e /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
85
86 if [ ! -L /etc/samba/smb.conf ]; then
87 logger -p daemon.warn -t 'samba4-server' "Local custom /etc/samba/smb.conf file detected, all luci/config settings are ignored!"
88 fi
89}
90
91smb_add_share() {
92 config_get_sane name "$1" name
93 config_get_sane path "$1" path
94 config_get_sane users "$1" users
95 config_get_sane create_mask "$1" create_mask
96 config_get_sane dir_mask "$1" dir_mask
97 config_get_sane browseable "$1" browseable
98 config_get_sane read_only "$1" read_only
99 config_get_sane writeable "$1" writeable
100 config_get_sane guest_ok "$1" guest_ok
101 config_get_sane guest_only "$1" guest_only
102 config_get_sane inherit_owner "$1" inherit_owner
103 config_get_sane vfs_objects "$1" vfs_objects
104 config_get_bool timemachine "$1" timemachine 0
105 config_get_sane timemachine_maxsize "$1" timemachine_maxsize
106 config_get_bool force_root "$1" force_root 0
107 config_get_sane write_list "$1" write_list
108 config_get_sane read_list "$1" read_list
109
110 [ -z "$name" ] || [ -z "$path" ] && return
111
112 {
113 printf "\n[$name]\n\tpath = %s\n" "$path"
114
115 if [ "$force_root" -eq 1 ]; then
116 printf "\tforce user = root\n"
117 printf "\tforce group = root\n"
118 else
119 [ -n "$users" ] && printf "\tvalid users = %s\n" "$users"
120 fi
121
122 [ -n "$create_mask" ] && printf "\tcreate mask = %s\n" "$create_mask"
123 [ -n "$dir_mask" ] && printf "\tdirectory mask = %s\n" "$dir_mask"
124
125 [ -n "$browseable" ] && printf "\tbrowseable = %s\n" "$browseable"
126 [ -n "$read_only" ] && printf "\tread only = %s\n" "$read_only"
127 [ -n "$writeable" ] && printf "\twriteable = %s\n" "$writeable"
128 [ -n "$guest_ok" ] && printf "\tguest ok = %s\n" "$guest_ok"
129 [ -n "$guest_only" ] && printf "\tguest only = %s\n" "$guest_only"
130 [ -n "$inherit_owner" ] && printf "\tinherit owner = %s\n" "$inherit_owner"
131
132 [ -n "$write_list" ] && printf "\twrite list = %s\n" "$write_list"
133 [ -n "$read_list" ] && printf "\tread list = %s\n" "$read_list"
134
135 if [ "$MACOS" -eq 1 ]; then
136 vfs_objects="catia fruit streams_xattr $vfs_objects"
137 printf "\tfruit:encoding = native\n"
138 printf "\tfruit:metadata = stream\n"
139 printf "\tfruit:veto_appledouble = no\n"
140 # avoid mixed shares order for aapl
141 if [ "$timemachine" -eq 1 ]; then
142 printf "\tfruit:time machine = yes\n"
143 [ -n "$timemachine_maxsize" ] && printf "\tfruit:time machine max size = %sG\n" "${timemachine_maxsize}"
144 fi
145 fi
146
147 # always enable io_uring if we can ("should" fail silently via samba module load if no kernel support)
148 if [ "$DISABLE_ASYNC_IO" -ne 1 ] && [ -e /usr/lib/samba/vfs/io_uring.so ] ; then
149 logger -p daemon.info -t 'samba4-server' "io_uring module found, enabling VFS io_uring. (also needs Kernel 5.4+ Support)"
150 # make sure its last in list
151 if [ -n "$vfs_objects" ]; then
152 vfs_objects="$vfs_objects io_uring"
153 else
154 vfs_objects="io_uring"
155 fi
156 fi
157
158 [ -n "$vfs_objects" ] && printf "\tvfs objects = %s\n" "$vfs_objects"
159 } >> /var/etc/smb.conf
160}
161
162init_config() {
163 # Create samba dirs
164 [ -d /var/lib/samba ] || mkdir -m 755 -p /var/lib/samba
165 [ -d /var/cache/samba ] || mkdir -m 755 -p /var/cache/samba
166 [ -d /var/lock ] || mkdir -m 755 -p /var/lock
167 [ -d /var/run/samba ] || mkdir -p /var/run/samba
168 [ -d /var/log/samba ] || mkdir -p /var/log/samba
169 chmod 0755 /var/lock
170 chmod 0755 /var/lib/samba
171 chmod 0755 /var/cache/samba
172
173 config_load samba4
174 config_foreach smb_header samba
175 config_foreach smb_add_share sambashare
176}
177
178service_triggers() {
179 # PROCD_RELOAD_DELAY=1000
180
181 procd_add_reload_trigger "dhcp" "system" "samba4"
182
183 for i in $SAMBA_IFACE; do
184 procd_add_reload_interface_trigger "$i"
185 done
186}
187
188start_service() {
189 init_config
190
191 if [ ! -e /etc/samba/smb.conf ]; then
192 logger -p daemon.error -t 'samba4-server' "missing config /etc/samba/smb.conf!"
193 exit 1
194 fi
195
196 config_get_sane nice_value extra samba_nice 0
197
198 # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
199 if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
200 procd_open_instance
201 procd_set_param command /usr/sbin/samba -F
202 procd_set_param nice "$nice_value"
203 procd_set_param respawn
204 procd_set_param file /etc/samba/smb.conf
205 procd_set_param limits nofile=16384
206 procd_close_instance
207 else
208 # start fileserver daemon
209 procd_open_instance
210 procd_set_param command /usr/sbin/smbd -F
211 procd_set_param nice "$nice_value"
212 procd_set_param respawn
213 procd_set_param file /etc/samba/smb.conf
214 procd_set_param limits nofile=16384
215 procd_close_instance
216
217 # start netbios daemon
218 if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
219 procd_open_instance
220 procd_set_param command /usr/sbin/nmbd -F
221 procd_set_param nice "$nice_value"
222 procd_set_param respawn
223 procd_set_param file /etc/samba/smb.conf
224 procd_close_instance
225 fi
226 # start winbind daemon
227 if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
228 procd_open_instance
229 procd_set_param command /usr/sbin/winbindd -F
230 procd_set_param nice "$nice_value"
231 procd_set_param respawn
232 procd_set_param file /etc/samba/smb.conf
233 procd_close_instance
234 fi
235 fi
236}