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.

236 lines
7.8 KiB

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