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.

248 lines
7.7 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. fi
  52. if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
  53. printf "\tdisable netbios = yes\n"
  54. # note: samba opens port 139 even if netbios is disabled via option above, so adjust listening ports
  55. printf "\tsmb ports = 445\n"
  56. fi
  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. if [ "$ALLOW_LEGACY_PROTOCOLS" -eq 1 ]; then
  64. logger -p daemon.info -t 'samba4-server' "Legacy Protocols allowed, don't use this option for secure environments!"
  65. printf "\tserver min protocol = NT1\n"
  66. printf "\tlanman auth = yes\n"
  67. printf "\tntlm auth = ntlmv1-permitted\n"
  68. fi
  69. } >> /var/etc/smb.conf
  70. [ -e /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  71. if [ ! -L /etc/samba/smb.conf ]; then
  72. logger -p daemon.warn -t 'samba4-server' "Local custom /etc/samba/smb.conf file detected, all luci/config settings are ignored!"
  73. fi
  74. }
  75. smb_add_share() {
  76. local name
  77. local path
  78. local users
  79. local create_mask
  80. local dir_mask
  81. local browseable
  82. local read_only
  83. local writeable
  84. local guest_ok
  85. local guest_only
  86. local inherit_owner
  87. local vfs_objects
  88. local timemachine
  89. local timemachine_maxsize
  90. local force_root
  91. local write_list
  92. local read_list
  93. config_get name $1 name
  94. config_get path $1 path
  95. config_get users $1 users
  96. config_get create_mask $1 create_mask
  97. config_get dir_mask $1 dir_mask
  98. config_get browseable $1 browseable
  99. config_get read_only $1 read_only
  100. config_get writeable $1 writeable
  101. config_get guest_ok $1 guest_ok
  102. config_get guest_only $1 guest_only
  103. config_get inherit_owner $1 inherit_owner
  104. config_get vfs_objects $1 vfs_objects
  105. config_get_bool timemachine $1 timemachine 0
  106. config_get timemachine_maxsize $1 timemachine_maxsize
  107. config_get_bool force_root $1 force_root 0
  108. config_get write_list $1 write_list
  109. config_get read_list $1 read_list
  110. [ -z "$name" ] || [ -z "$path" ] && return
  111. {
  112. printf "\n[$name]\n\tpath = %s\n" "$path"
  113. if [ "$force_root" -eq 1 ]; then
  114. printf "\tforce user = root\n"
  115. printf "\tforce group = root\n"
  116. else
  117. [ -n "$users" ] && printf "\tvalid users = %s\n" "$users"
  118. fi
  119. [ -n "$create_mask" ] && printf "\tcreate mask = %s\n" "$create_mask"
  120. [ -n "$dir_mask" ] && printf "\tdirectory mask = %s\n" "$dir_mask"
  121. [ -n "$browseable" ] && printf "\tbrowseable = %s\n" "$browseable"
  122. [ -n "$read_only" ] && printf "\tread only = %s\n" "$read_only"
  123. [ -n "$writeable" ] && printf "\twriteable = %s\n" "$writeable"
  124. [ -n "$guest_ok" ] && printf "\tguest ok = %s\n" "$guest_ok"
  125. [ -n "$guest_only" ] && printf "\tguest only = %s\n" "$guest_only"
  126. [ -n "$inherit_owner" ] && printf "\tinherit owner = %s\n" "$inherit_owner"
  127. [ -n "$write_list" ] && printf "\twrite list = %s\n" "$write_list"
  128. [ -n "$read_list" ] && printf "\tread list = %s\n" "$read_list"
  129. if [ "$MACOS" -eq 1 ]; then
  130. vfs_objects="catia fruit streams_xattr $vfs_objects"
  131. printf "\tfruit:encoding = native\n"
  132. printf "\tfruit:metadata = stream\n"
  133. printf "\tfruit:veto_appledouble = no\n"
  134. # avoid mixed shares order for aapl
  135. if [ "$timemachine" -eq 1 ]; then
  136. printf "\tfruit:time machine = yes\n"
  137. [ -n "$timemachine_maxsize" ] && printf "\tfruit:time machine max size = %sG\n" "${timemachine_maxsize}"
  138. fi
  139. fi
  140. [ -n "$vfs_objects" ] && printf "\tvfs objects = %s\n" "$vfs_objects"
  141. } >> /var/etc/smb.conf
  142. }
  143. init_config() {
  144. # Create samba dirs
  145. [ -d /var/lib/samba ] || mkdir -m 755 -p /var/lib/samba
  146. [ -d /var/cache/samba ] || mkdir -m 755 -p /var/cache/samba
  147. [ -d /var/lock ] || mkdir -m 755 -p /var/lock
  148. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  149. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  150. chmod 0755 /var/lock
  151. chmod 0755 /var/lib/samba
  152. chmod 0755 /var/cache/samba
  153. config_load samba4
  154. config_foreach smb_header samba
  155. config_foreach smb_add_share sambashare
  156. }
  157. service_triggers() {
  158. # PROCD_RELOAD_DELAY=1000
  159. procd_add_reload_trigger "dhcp" "system" "samba4"
  160. local i
  161. for i in $SAMBA_IFACE; do
  162. procd_add_reload_interface_trigger $i
  163. done
  164. }
  165. start_service() {
  166. init_config
  167. if [ ! -e /etc/samba/smb.conf ]; then
  168. logger -p daemon.error -t 'samba4-server' "missing config /etc/samba/smb.conf!"
  169. exit 1
  170. fi
  171. local nice_value
  172. config_get nice_value extra samba_nice 0
  173. # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  174. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  175. procd_open_instance
  176. procd_set_param command /usr/sbin/samba -F
  177. procd_set_param nice $nice_value
  178. procd_set_param respawn
  179. procd_set_param file /etc/samba/smb.conf
  180. procd_set_param limits nofile=16384
  181. procd_close_instance
  182. else
  183. # start fileserver daemon
  184. procd_open_instance
  185. procd_set_param command /usr/sbin/smbd -F
  186. procd_set_param nice $nice_value
  187. procd_set_param respawn
  188. procd_set_param file /etc/samba/smb.conf
  189. procd_set_param limits nofile=16384
  190. procd_close_instance
  191. # start netbios daemon
  192. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  193. procd_open_instance
  194. procd_set_param command /usr/sbin/nmbd -F
  195. procd_set_param nice $nice_value
  196. procd_set_param respawn
  197. procd_set_param file /etc/samba/smb.conf
  198. procd_close_instance
  199. fi
  200. # start winbind daemon
  201. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  202. procd_open_instance
  203. procd_set_param command /usr/sbin/winbindd -F
  204. procd_set_param nice $nice_value
  205. procd_set_param respawn
  206. procd_set_param file /etc/samba/smb.conf
  207. procd_close_instance
  208. fi
  209. fi
  210. }