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.

207 lines
5.9 KiB

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