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.

205 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. . /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. config_get name $1 name
  71. config_get path $1 path
  72. config_get users $1 users
  73. config_get create_mask $1 create_mask
  74. config_get dir_mask $1 dir_mask
  75. config_get browseable $1 browseable
  76. config_get read_only $1 read_only
  77. config_get guest_ok $1 guest_ok
  78. config_get guest_only $1 guest_only
  79. config_get inherit_owner $1 inherit_owner
  80. config_get vfs_objects $1 vfs_objects
  81. config_get_bool timemachine $1 timemachine 0
  82. config_get timemachine_maxsize $1 timemachine_maxsize
  83. config_get_bool force_root $1 force_root 0
  84. [ -z "$name" -o -z "$path" ] && return
  85. echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
  86. if [ "$force_root" -eq 1 ]; then
  87. echo -e "\tforce user = root" >> /var/etc/smb.conf
  88. echo -e "\tforce group = root" >> /var/etc/smb.conf
  89. else
  90. [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
  91. fi
  92. [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
  93. [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf
  94. [ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >> /var/etc/smb.conf
  95. [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf
  96. [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf
  97. [ -n "$guest_only" ] && echo -e "\tguest only = $guest_only" >> /var/etc/smb.conf
  98. [ -n "$inherit_owner" ] && echo -e "\tinherit owner = $inherit_owner" >> /var/etc/smb.conf
  99. if [ "$MACOS" -eq 1 ]; then
  100. vfs_objects="catia fruit streams_xattr $vfs_objects"
  101. echo -e "\tfruit:encoding = native" >> /var/etc/smb.conf
  102. echo -e "\tfruit:metadata = stream" >> /var/etc/smb.conf
  103. echo -e "\tfruit:veto_appledouble = no" >> /var/etc/smb.conf
  104. # avoid mixed shares order for aapl
  105. if [ "$timemachine" -eq 1 ]; then
  106. echo -e "\tfruit:time machine = yes" >> /var/etc/smb.conf
  107. [ -n "$timemachine_maxsize" ] && echo -e "\tfruit:time machine max size = ${timemachine_maxsize}G" >> /var/etc/smb.conf
  108. fi
  109. fi
  110. [ -n "$vfs_objects" ] && echo -e "\tvfs objects = $vfs_objects" >> /var/etc/smb.conf
  111. }
  112. init_config() {
  113. # Create samba dirs
  114. [ -d /var/lib/samba ] || mkdir -p /var/lib/samba
  115. [ -d /var/cache/samba ] || mkdir -p /var/cache/samba
  116. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  117. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  118. [ -d /var/lock ] && chmod 0755 /var/lock || {
  119. mkdir -p /var/lock
  120. chmod 0755 /var/lock
  121. }
  122. config_load samba4
  123. config_foreach smb_header samba
  124. config_foreach smb_add_share sambashare
  125. }
  126. service_triggers() {
  127. PROCD_RELOAD_DELAY=2000
  128. procd_add_reload_trigger "dhcp" "system" "samba4"
  129. local i
  130. for i in $samba_iface; do
  131. procd_add_reload_interface_trigger $i
  132. done
  133. }
  134. start_service() {
  135. init_config
  136. # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  137. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  138. procd_open_instance
  139. procd_set_param command /usr/sbin/samba -F
  140. procd_set_param respawn
  141. procd_set_param file /var/etc/smb.conf
  142. procd_close_instance
  143. else
  144. # start fileserver daemon
  145. procd_open_instance
  146. procd_set_param command /usr/sbin/smbd -F
  147. procd_set_param respawn
  148. procd_set_param file /var/etc/smb.conf
  149. procd_close_instance
  150. # start netbios daemon
  151. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  152. procd_open_instance
  153. procd_set_param command /usr/sbin/nmbd -F
  154. procd_set_param respawn
  155. procd_set_param file /var/etc/smb.conf
  156. procd_close_instance
  157. fi
  158. # start winbind daemon
  159. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  160. procd_open_instance
  161. procd_set_param command /usr/sbin/winbindd -F
  162. procd_set_param respawn
  163. procd_set_param file /var/etc/smb.conf
  164. procd_close_instance
  165. fi
  166. fi
  167. # lower priority using renice (if found)
  168. if [ -x /usr/bin/renice ]; then
  169. [ -x /usr/sbin/samba ] && renice -n 2 $(pidof samba)
  170. [ -x /usr/sbin/smbd ] && renice -n 2 $(pidof smbd)
  171. [ -x /usr/sbin/nmbd ] && renice -n 2 $(pidof nmbd)
  172. [ -x /usr/sbin/winbindd ] && renice -n 2 $(pidof winbindd)
  173. fi
  174. }