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.

213 lines
6.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=98
  3. USE_PROCD=1
  4. smb_header() {
  5. config_get samba_iface $1 interface "loopback lan"
  6. # resolve interfaces
  7. local interfaces
  8. interfaces=$(
  9. . /lib/functions/network.sh
  10. local net
  11. for net in $samba_iface; do
  12. local device
  13. network_is_up $net || continue
  14. network_get_device device "$net"
  15. printf "%s " "${device:-$net}"
  16. done
  17. )
  18. local workgroup description charset
  19. # we dont use netbios anymore as default and wsd/avahi is dns based
  20. local hostname
  21. hostname="$(cat /proc/sys/kernel/hostname)"
  22. config_get workgroup $1 workgroup "WORKGROUP"
  23. config_get description $1 description "Samba on OpenWrt"
  24. config_get 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. mkdir -p /var/etc
  31. sed -e "s#|NAME|#$hostname#g" \
  32. -e "s#|WORKGROUP|#$workgroup#g" \
  33. -e "s#|DESCRIPTION|#$description#g" \
  34. -e "s#|INTERFACES|#$interfaces#g" \
  35. -e "s#|CHARSET|#$charset#g" \
  36. /etc/samba/smb.conf.template > /var/etc/smb.conf
  37. {
  38. printf "\n######### Dynamic written config options #########\n"
  39. if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
  40. printf "\tdisable netbios = yes\n"
  41. fi
  42. if [ "$DISABLE_ASYNC_IO" -eq 1 ]; then
  43. printf "\taio read size = 0\n"
  44. printf "\taio write size = 0\n"
  45. # sendfile bug: https://bugzilla.samba.org/show_bug.cgi?id=14095
  46. printf "\tuse sendfile = no\n"
  47. fi
  48. } >> /var/etc/smb.conf
  49. [ -e /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  50. if ! [ -L /etc/samba/smb.conf ]; then
  51. logger -t 'samba4-server' "Local custom /etc/samba/smb.conf file detected, all luci/config settings are ignored!"
  52. fi
  53. }
  54. smb_add_share() {
  55. local name
  56. local path
  57. local users
  58. local create_mask
  59. local dir_mask
  60. local browseable
  61. local read_only
  62. local writeable
  63. local guest_ok
  64. local guest_only
  65. local inherit_owner
  66. local vfs_objects
  67. local timemachine
  68. local timemachine_maxsize
  69. local force_root
  70. local write_list
  71. local read_list
  72. config_get name $1 name
  73. config_get path $1 path
  74. config_get users $1 users
  75. config_get create_mask $1 create_mask
  76. config_get dir_mask $1 dir_mask
  77. config_get browseable $1 browseable
  78. config_get read_only $1 read_only
  79. config_get writeable $1 writeable
  80. config_get guest_ok $1 guest_ok
  81. config_get guest_only $1 guest_only
  82. config_get inherit_owner $1 inherit_owner
  83. config_get vfs_objects $1 vfs_objects
  84. config_get_bool timemachine $1 timemachine 0
  85. config_get timemachine_maxsize $1 timemachine_maxsize
  86. config_get_bool force_root $1 force_root 0
  87. config_get write_list $1 write_list
  88. config_get 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. [ -n "$vfs_objects" ] && printf "\tvfs objects = %s\n" "$vfs_objects"
  120. } >> /var/etc/smb.conf
  121. }
  122. init_config() {
  123. # Create samba dirs
  124. [ -d /var/lib/samba ] || mkdir -p /var/lib/samba
  125. [ -d /var/cache/samba ] || mkdir -p /var/cache/samba
  126. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  127. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  128. [ -d /var/lock ] || mkdir -p /var/lock
  129. chmod 0755 /var/lock
  130. config_load samba4
  131. config_foreach smb_header samba
  132. config_foreach smb_add_share sambashare
  133. }
  134. service_triggers() {
  135. PROCD_RELOAD_DELAY=2000
  136. procd_add_reload_trigger "dhcp" "system" "samba4"
  137. local i
  138. for i in $samba_iface; do
  139. procd_add_reload_interface_trigger $i
  140. done
  141. }
  142. start_service() {
  143. init_config
  144. # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  145. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  146. procd_open_instance
  147. procd_set_param command /usr/sbin/samba -F
  148. procd_set_param respawn
  149. procd_set_param file /var/etc/smb.conf
  150. procd_close_instance
  151. else
  152. # start fileserver daemon
  153. procd_open_instance
  154. procd_set_param command /usr/sbin/smbd -F
  155. procd_set_param respawn
  156. procd_set_param file /var/etc/smb.conf
  157. procd_close_instance
  158. # start netbios daemon
  159. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  160. procd_open_instance
  161. procd_set_param command /usr/sbin/nmbd -F
  162. procd_set_param respawn
  163. procd_set_param file /var/etc/smb.conf
  164. procd_close_instance
  165. fi
  166. # start winbind daemon
  167. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  168. procd_open_instance
  169. procd_set_param command /usr/sbin/winbindd -F
  170. procd_set_param respawn
  171. procd_set_param file /var/etc/smb.conf
  172. procd_close_instance
  173. fi
  174. fi
  175. # lower priority using renice (if found)
  176. if [ -x /usr/bin/renice ]; then
  177. [ -x /usr/sbin/samba ] && renice -n 2 $(pidof samba)
  178. [ -x /usr/sbin/smbd ] && renice -n 2 $(pidof smbd)
  179. [ -x /usr/sbin/nmbd ] && renice -n 2 $(pidof nmbd)
  180. [ -x /usr/sbin/winbindd ] && renice -n 2 $(pidof winbindd)
  181. fi
  182. }