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.

212 lines
6.2 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. . /lib/functions/network.sh
  9. local net
  10. for net in $samba_iface; do
  11. local device
  12. network_is_up $net || continue
  13. network_get_device device "$net"
  14. echo -n "${device:-$net} "
  15. done
  16. )
  17. local workgroup description charset
  18. # we dont use netbios anymore as default and wsd/avahi is dns based
  19. local hostname="$(cat /proc/sys/kernel/hostname)"
  20. config_get workgroup $1 workgroup "WORKGROUP"
  21. config_get description $1 description "Samba on OpenWrt"
  22. config_get charset $1 charset "UTF-8"
  23. config_get_bool MACOS $1 macos 0
  24. config_get_bool DISABLE_NETBIOS $1 disable_netbios 0
  25. config_get_bool DISABLE_AD_DC $1 disable_ad_dc 0
  26. config_get_bool DISABLE_WINBIND $1 disable_winbind 0
  27. mkdir -p /var/etc
  28. sed -e "s#|NAME|#$hostname#g" \
  29. -e "s#|WORKGROUP|#$workgroup#g" \
  30. -e "s#|DESCRIPTION|#$description#g" \
  31. -e "s#|INTERFACES|#$interfaces#g" \
  32. -e "s#|CHARSET|#$charset#g" \
  33. /etc/samba/smb.conf.template > /var/etc/smb.conf
  34. echo -e "\n######### Dynamic written config options #########\n" >> /var/etc/smb.conf
  35. if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
  36. echo -e "\tdisable netbios = yes" >> /var/etc/smb.conf
  37. fi
  38. local homes
  39. config_get_bool homes $1 homes 0
  40. [ $homes -gt 0 ] && {
  41. cat <<EOT >> /var/etc/smb.conf
  42. [homes]
  43. comment = Home Directories
  44. browsable = no
  45. writable = yes
  46. read only = no
  47. create mask = 0750
  48. EOT
  49. }
  50. [ -e /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  51. if [ -f /etc/samba/smb.conf ]; then
  52. logger -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 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 guest_ok $1 guest_ok
  80. config_get guest_only $1 guest_only
  81. config_get inherit_owner $1 inherit_owner
  82. config_get vfs_objects $1 vfs_objects
  83. config_get_bool timemachine $1 timemachine 0
  84. config_get timemachine_maxsize $1 timemachine_maxsize
  85. config_get_bool force_root $1 force_root 0
  86. config_get write_list $1 write_list
  87. config_get read_list $1 read_list
  88. [ -z "$name" -o -z "$path" ] && return
  89. echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
  90. if [ "$force_root" -eq 1 ]; then
  91. echo -e "\tforce user = root" >> /var/etc/smb.conf
  92. echo -e "\tforce group = root" >> /var/etc/smb.conf
  93. else
  94. [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
  95. fi
  96. [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
  97. [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf
  98. [ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >> /var/etc/smb.conf
  99. [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf
  100. [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf
  101. [ -n "$guest_only" ] && echo -e "\tguest only = $guest_only" >> /var/etc/smb.conf
  102. [ -n "$inherit_owner" ] && echo -e "\tinherit owner = $inherit_owner" >> /var/etc/smb.conf
  103. [ -n "$write_list" ] && echo -e "\twrite list = $write_list" >> /var/etc/smb.conf
  104. [ -n "$read_list" ] && echo -e "\tread list = $read_list" >> /var/etc/smb.conf
  105. if [ "$MACOS" -eq 1 ]; then
  106. vfs_objects="catia fruit streams_xattr $vfs_objects"
  107. echo -e "\tfruit:encoding = native" >> /var/etc/smb.conf
  108. echo -e "\tfruit:metadata = stream" >> /var/etc/smb.conf
  109. echo -e "\tfruit:veto_appledouble = no" >> /var/etc/smb.conf
  110. # avoid mixed shares order for aapl
  111. if [ "$timemachine" -eq 1 ]; then
  112. echo -e "\tfruit:time machine = yes" >> /var/etc/smb.conf
  113. [ -n "$timemachine_maxsize" ] && echo -e "\tfruit:time machine max size = ${timemachine_maxsize}G" >> /var/etc/smb.conf
  114. fi
  115. fi
  116. [ -n "$vfs_objects" ] && echo -e "\tvfs objects = $vfs_objects" >> /var/etc/smb.conf
  117. }
  118. init_config() {
  119. # Create samba dirs
  120. [ -d /var/lib/samba ] || mkdir -p /var/lib/samba
  121. [ -d /var/cache/samba ] || mkdir -p /var/cache/samba
  122. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  123. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  124. [ -d /var/lock ] && chmod 0755 /var/lock || {
  125. mkdir -p /var/lock
  126. chmod 0755 /var/lock
  127. }
  128. config_load samba4
  129. config_foreach smb_header samba
  130. config_foreach smb_add_share sambashare
  131. }
  132. service_triggers() {
  133. PROCD_RELOAD_DELAY=2000
  134. procd_add_reload_trigger "dhcp" "system" "samba4"
  135. local i
  136. for i in $samba_iface; do
  137. procd_add_reload_interface_trigger $i
  138. done
  139. }
  140. start_service() {
  141. init_config
  142. # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  143. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  144. procd_open_instance
  145. procd_set_param command /usr/sbin/samba -F
  146. procd_set_param respawn
  147. procd_set_param file /var/etc/smb.conf
  148. procd_close_instance
  149. else
  150. # start fileserver daemon
  151. procd_open_instance
  152. procd_set_param command /usr/sbin/smbd -F
  153. procd_set_param respawn
  154. procd_set_param file /var/etc/smb.conf
  155. procd_close_instance
  156. # start netbios daemon
  157. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  158. procd_open_instance
  159. procd_set_param command /usr/sbin/nmbd -F
  160. procd_set_param respawn
  161. procd_set_param file /var/etc/smb.conf
  162. procd_close_instance
  163. fi
  164. # start winbind daemon
  165. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  166. procd_open_instance
  167. procd_set_param command /usr/sbin/winbindd -F
  168. procd_set_param respawn
  169. procd_set_param file /var/etc/smb.conf
  170. procd_close_instance
  171. fi
  172. fi
  173. # lower priority using renice (if found)
  174. if [ -x /usr/bin/renice ]; then
  175. [ -x /usr/sbin/samba ] && renice -n 2 $(pidof samba)
  176. [ -x /usr/sbin/smbd ] && renice -n 2 $(pidof smbd)
  177. [ -x /usr/sbin/nmbd ] && renice -n 2 $(pidof nmbd)
  178. [ -x /usr/sbin/winbindd ] && renice -n 2 $(pidof winbindd)
  179. fi
  180. }