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.

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