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.

219 lines
6.0 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. mkdir -p /var/etc
  30. sed -e "s#|NAME|#$hostname#g" \
  31. -e "s#|WORKGROUP|#$workgroup#g" \
  32. -e "s#|DESCRIPTION|#$description#g" \
  33. -e "s#|INTERFACES|#$interfaces#g" \
  34. -e "s#|CHARSET|#$charset#g" \
  35. /etc/samba/smb.conf.template > /var/etc/smb.conf
  36. {
  37. printf "\n######### Dynamic written config options #########\n"
  38. if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
  39. printf "\tdisable netbios = yes\n"
  40. fi
  41. local homes
  42. config_get_bool homes $1 homes 0
  43. [ $homes -gt 0 ] && {
  44. cat <<EOT
  45. [homes]
  46. comment = Home Directories
  47. browsable = no
  48. writable = yes
  49. read only = no
  50. create mask = 0750
  51. EOT
  52. }
  53. } >> /var/etc/smb.conf
  54. [ -e /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  55. if ! [ -L /etc/samba/smb.conf ]; then
  56. logger -t 'samba4-server' "Local custom /etc/samba/smb.conf file detected, all luci/config settings are ignored!"
  57. fi
  58. }
  59. smb_add_share() {
  60. local name
  61. local path
  62. local users
  63. local create_mask
  64. local dir_mask
  65. local browseable
  66. local read_only
  67. local writeable
  68. local guest_ok
  69. local guest_only
  70. local inherit_owner
  71. local vfs_objects
  72. local timemachine
  73. local timemachine_maxsize
  74. local force_root
  75. local write_list
  76. local read_list
  77. config_get name $1 name
  78. config_get path $1 path
  79. config_get users $1 users
  80. config_get create_mask $1 create_mask
  81. config_get dir_mask $1 dir_mask
  82. config_get browseable $1 browseable
  83. config_get read_only $1 read_only
  84. config_get writeable $1 writeable
  85. config_get guest_ok $1 guest_ok
  86. config_get guest_only $1 guest_only
  87. config_get inherit_owner $1 inherit_owner
  88. config_get vfs_objects $1 vfs_objects
  89. config_get_bool timemachine $1 timemachine 0
  90. config_get timemachine_maxsize $1 timemachine_maxsize
  91. config_get_bool force_root $1 force_root 0
  92. config_get write_list $1 write_list
  93. config_get read_list $1 read_list
  94. [ -z "$name" ] || [ -z "$path" ] && return
  95. {
  96. printf "\n[$name]\n\tpath = %s\n" "$path"
  97. if [ "$force_root" -eq 1 ]; then
  98. printf "\tforce user = root\n"
  99. printf "\tforce group = root\n"
  100. else
  101. [ -n "$users" ] && printf "\tvalid users = %s\n" "$users"
  102. fi
  103. [ -n "$create_mask" ] && printf "\tcreate mask = %s\n" "$create_mask"
  104. [ -n "$dir_mask" ] && printf "\tdirectory mask = %s\n" "$dir_mask"
  105. [ -n "$browseable" ] && printf "\tbrowseable = %s\n" "$browseable"
  106. [ -n "$read_only" ] && printf "\tread only = %s\n" "$read_only"
  107. [ -n "$writeable" ] && printf "\twriteable = %s\n" "$writeable"
  108. [ -n "$guest_ok" ] && printf "\tguest ok = %s\n" "$guest_ok"
  109. [ -n "$guest_only" ] && printf "\tguest only = %s\n" "$guest_only"
  110. [ -n "$inherit_owner" ] && printf "\tinherit owner = %s\n" "$inherit_owner"
  111. [ -n "$write_list" ] && printf "\twrite list = %s\n" "$write_list"
  112. [ -n "$read_list" ] && printf "\tread list = %s\n" "$read_list"
  113. if [ "$MACOS" -eq 1 ]; then
  114. vfs_objects="catia fruit streams_xattr $vfs_objects"
  115. printf "\tfruit:encoding = native\n"
  116. printf "\tfruit:metadata = stream\n"
  117. printf "\tfruit:veto_appledouble = no\n"
  118. # avoid mixed shares order for aapl
  119. if [ "$timemachine" -eq 1 ]; then
  120. printf "\tfruit:time machine = yes\n"
  121. [ -n "$timemachine_maxsize" ] && printf "\tfruit:time machine max size = %sG\n" "${timemachine_maxsize}"
  122. fi
  123. fi
  124. [ -n "$vfs_objects" ] && printf "\tvfs objects = %s\n" "$vfs_objects"
  125. } >> /var/etc/smb.conf
  126. }
  127. init_config() {
  128. # Create samba dirs
  129. [ -d /var/lib/samba ] || mkdir -p /var/lib/samba
  130. [ -d /var/cache/samba ] || mkdir -p /var/cache/samba
  131. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  132. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  133. [ -d /var/lock ] || mkdir -p /var/lock
  134. chmod 0755 /var/lock
  135. config_load samba4
  136. config_foreach smb_header samba
  137. config_foreach smb_add_share sambashare
  138. }
  139. service_triggers() {
  140. PROCD_RELOAD_DELAY=2000
  141. procd_add_reload_trigger "dhcp" "system" "samba4"
  142. local i
  143. for i in $samba_iface; do
  144. procd_add_reload_interface_trigger $i
  145. done
  146. }
  147. start_service() {
  148. init_config
  149. # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  150. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  151. procd_open_instance
  152. procd_set_param command /usr/sbin/samba -F
  153. procd_set_param respawn
  154. procd_set_param file /var/etc/smb.conf
  155. procd_close_instance
  156. else
  157. # start fileserver daemon
  158. procd_open_instance
  159. procd_set_param command /usr/sbin/smbd -F
  160. procd_set_param respawn
  161. procd_set_param file /var/etc/smb.conf
  162. procd_close_instance
  163. # start netbios daemon
  164. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  165. procd_open_instance
  166. procd_set_param command /usr/sbin/nmbd -F
  167. procd_set_param respawn
  168. procd_set_param file /var/etc/smb.conf
  169. procd_close_instance
  170. fi
  171. # start winbind daemon
  172. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  173. procd_open_instance
  174. procd_set_param command /usr/sbin/winbindd -F
  175. procd_set_param respawn
  176. procd_set_param file /var/etc/smb.conf
  177. procd_close_instance
  178. fi
  179. fi
  180. # lower priority using renice (if found)
  181. if [ -x /usr/bin/renice ]; then
  182. [ -x /usr/sbin/samba ] && renice -n 2 $(pidof samba)
  183. [ -x /usr/sbin/smbd ] && renice -n 2 $(pidof smbd)
  184. [ -x /usr/sbin/nmbd ] && renice -n 2 $(pidof nmbd)
  185. [ -x /usr/sbin/winbindd ] && renice -n 2 $(pidof winbindd)
  186. fi
  187. }