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.

235 lines
7.8 KiB

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