You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
7.9 KiB

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