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.

232 lines
6.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. 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. if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
  42. printf "\tdisable netbios = yes\n"
  43. # note: samba opens port 139 even if netbios is disabled via option above, so adjust listening ports
  44. printf "\tsmb ports = 445\n"
  45. fi
  46. if [ "$DISABLE_ASYNC_IO" -eq 1 ]; then
  47. printf "\taio read size = 0\n"
  48. printf "\taio write size = 0\n"
  49. # sendfile bug: https://bugzilla.samba.org/show_bug.cgi?id=14095
  50. printf "\tuse sendfile = no\n"
  51. fi
  52. if [ "$ALLOW_LEGACY_PROTOCOLS" -eq 1 ]; then
  53. logger -p daemon.info -t 'samba4-server' "Legacy Protocols allowed, don't use this option for secure environments!"
  54. printf "\tserver min protocol = NT1\n"
  55. printf "\tlanman auth = yes\n"
  56. printf "\tntlm auth = ntlmv1-permitted\n"
  57. fi
  58. } >> /var/etc/smb.conf
  59. [ -e /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  60. if [ ! -L /etc/samba/smb.conf ]; then
  61. logger -p daemon.warn -t 'samba4-server' "Local custom /etc/samba/smb.conf file detected, all luci/config settings are ignored!"
  62. fi
  63. }
  64. smb_add_share() {
  65. local name
  66. local path
  67. local users
  68. local create_mask
  69. local dir_mask
  70. local browseable
  71. local read_only
  72. local writeable
  73. local guest_ok
  74. local guest_only
  75. local inherit_owner
  76. local vfs_objects
  77. local timemachine
  78. local timemachine_maxsize
  79. local force_root
  80. local write_list
  81. local read_list
  82. config_get name $1 name
  83. config_get path $1 path
  84. config_get users $1 users
  85. config_get create_mask $1 create_mask
  86. config_get dir_mask $1 dir_mask
  87. config_get browseable $1 browseable
  88. config_get read_only $1 read_only
  89. config_get writeable $1 writeable
  90. config_get guest_ok $1 guest_ok
  91. config_get guest_only $1 guest_only
  92. config_get inherit_owner $1 inherit_owner
  93. config_get vfs_objects $1 vfs_objects
  94. config_get_bool timemachine $1 timemachine 0
  95. config_get timemachine_maxsize $1 timemachine_maxsize
  96. config_get_bool force_root $1 force_root 0
  97. config_get write_list $1 write_list
  98. config_get read_list $1 read_list
  99. [ -z "$name" ] || [ -z "$path" ] && return
  100. {
  101. printf "\n[$name]\n\tpath = %s\n" "$path"
  102. if [ "$force_root" -eq 1 ]; then
  103. printf "\tforce user = root\n"
  104. printf "\tforce group = root\n"
  105. else
  106. [ -n "$users" ] && printf "\tvalid users = %s\n" "$users"
  107. fi
  108. [ -n "$create_mask" ] && printf "\tcreate mask = %s\n" "$create_mask"
  109. [ -n "$dir_mask" ] && printf "\tdirectory mask = %s\n" "$dir_mask"
  110. [ -n "$browseable" ] && printf "\tbrowseable = %s\n" "$browseable"
  111. [ -n "$read_only" ] && printf "\tread only = %s\n" "$read_only"
  112. [ -n "$writeable" ] && printf "\twriteable = %s\n" "$writeable"
  113. [ -n "$guest_ok" ] && printf "\tguest ok = %s\n" "$guest_ok"
  114. [ -n "$guest_only" ] && printf "\tguest only = %s\n" "$guest_only"
  115. [ -n "$inherit_owner" ] && printf "\tinherit owner = %s\n" "$inherit_owner"
  116. [ -n "$write_list" ] && printf "\twrite list = %s\n" "$write_list"
  117. [ -n "$read_list" ] && printf "\tread list = %s\n" "$read_list"
  118. if [ "$MACOS" -eq 1 ]; then
  119. vfs_objects="catia fruit streams_xattr $vfs_objects"
  120. printf "\tfruit:encoding = native\n"
  121. printf "\tfruit:metadata = stream\n"
  122. printf "\tfruit:veto_appledouble = no\n"
  123. # avoid mixed shares order for aapl
  124. if [ "$timemachine" -eq 1 ]; then
  125. printf "\tfruit:time machine = yes\n"
  126. [ -n "$timemachine_maxsize" ] && printf "\tfruit:time machine max size = %sG\n" "${timemachine_maxsize}"
  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 -p /var/lib/samba
  135. [ -d /var/cache/samba ] || mkdir -p /var/cache/samba
  136. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  137. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  138. [ -d /var/lock ] || mkdir -p /var/lock
  139. chmod 0755 /var/lock
  140. config_load samba4
  141. config_foreach smb_header samba
  142. config_foreach smb_add_share sambashare
  143. }
  144. service_triggers() {
  145. # PROCD_RELOAD_DELAY=1000
  146. procd_add_reload_trigger "dhcp" "system" "samba4"
  147. local i
  148. for i in $SAMBA_IFACE; do
  149. procd_add_reload_interface_trigger $i
  150. done
  151. }
  152. start_service() {
  153. init_config
  154. if [ ! -e /etc/samba/smb.conf ]; then
  155. logger -p daemon.error -t 'samba4-server' "missing config /etc/samba/smb.conf!"
  156. exit 1
  157. fi
  158. local nice_value
  159. config_get 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. }