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.

222 lines
6.3 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. mkdir -p /var/etc
  32. sed -e "s#|NAME|#$hostname#g" \
  33. -e "s#|WORKGROUP|#$workgroup#g" \
  34. -e "s#|DESCRIPTION|#$description#g" \
  35. -e "s#|INTERFACES|#$interfaces#g" \
  36. -e "s#|CHARSET|#$charset#g" \
  37. /etc/samba/smb.conf.template > /var/etc/smb.conf
  38. {
  39. printf "\n######### Dynamic written config options #########\n"
  40. if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
  41. printf "\tdisable netbios = yes\n"
  42. fi
  43. if [ "$DISABLE_ASYNC_IO" -eq 1 ]; then
  44. printf "\taio read size = 0\n"
  45. printf "\taio write size = 0\n"
  46. # sendfile bug: https://bugzilla.samba.org/show_bug.cgi?id=14095
  47. printf "\tuse sendfile = no\n"
  48. fi
  49. } >> /var/etc/smb.conf
  50. [ -e /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  51. if [ ! -L /etc/samba/smb.conf ]; then
  52. logger -p daemon.warn -t 'samba4-server' "Local custom /etc/samba/smb.conf file detected, all luci/config settings are ignored!"
  53. fi
  54. }
  55. smb_add_share() {
  56. local name
  57. local path
  58. local users
  59. local create_mask
  60. local dir_mask
  61. local browseable
  62. local read_only
  63. local writeable
  64. local guest_ok
  65. local guest_only
  66. local inherit_owner
  67. local vfs_objects
  68. local timemachine
  69. local timemachine_maxsize
  70. local force_root
  71. local write_list
  72. local read_list
  73. config_get name $1 name
  74. config_get path $1 path
  75. config_get users $1 users
  76. config_get create_mask $1 create_mask
  77. config_get dir_mask $1 dir_mask
  78. config_get browseable $1 browseable
  79. config_get read_only $1 read_only
  80. config_get writeable $1 writeable
  81. config_get guest_ok $1 guest_ok
  82. config_get guest_only $1 guest_only
  83. config_get inherit_owner $1 inherit_owner
  84. config_get vfs_objects $1 vfs_objects
  85. config_get_bool timemachine $1 timemachine 0
  86. config_get timemachine_maxsize $1 timemachine_maxsize
  87. config_get_bool force_root $1 force_root 0
  88. config_get write_list $1 write_list
  89. config_get 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. [ -n "$vfs_objects" ] && printf "\tvfs objects = %s\n" "$vfs_objects"
  121. } >> /var/etc/smb.conf
  122. }
  123. init_config() {
  124. # Create samba dirs
  125. [ -d /var/lib/samba ] || mkdir -p /var/lib/samba
  126. [ -d /var/cache/samba ] || mkdir -p /var/cache/samba
  127. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  128. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  129. [ -d /var/lock ] || mkdir -p /var/lock
  130. chmod 0755 /var/lock
  131. config_load samba4
  132. config_foreach smb_header samba
  133. config_foreach smb_add_share sambashare
  134. }
  135. service_triggers() {
  136. # PROCD_RELOAD_DELAY=1000
  137. procd_add_reload_trigger "dhcp" "system" "samba4"
  138. local i
  139. for i in $SAMBA_IFACE; do
  140. procd_add_reload_interface_trigger $i
  141. done
  142. }
  143. start_service() {
  144. init_config
  145. if [ ! -e /etc/samba/smb.conf ]; then
  146. logger -p daemon.error -t 'samba4-server' "missing config /etc/samba/smb.conf!"
  147. exit 1
  148. fi
  149. local nice_value
  150. config_get nice_value extra samba_nice 0
  151. # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  152. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  153. procd_open_instance
  154. procd_set_param command /usr/sbin/samba -F
  155. procd_set_param nice $nice_value
  156. procd_set_param respawn
  157. procd_set_param file /etc/samba/smb.conf
  158. procd_set_param limits nofile=16384
  159. procd_close_instance
  160. else
  161. # start fileserver daemon
  162. procd_open_instance
  163. procd_set_param command /usr/sbin/smbd -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. # start netbios daemon
  170. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  171. procd_open_instance
  172. procd_set_param command /usr/sbin/nmbd -F
  173. procd_set_param nice $nice_value
  174. procd_set_param respawn
  175. procd_set_param file /etc/samba/smb.conf
  176. procd_close_instance
  177. fi
  178. # start winbind daemon
  179. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  180. procd_open_instance
  181. procd_set_param command /usr/sbin/winbindd -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. fi
  188. }