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