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.

200 lines
5.8 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. [ -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. service_triggers() {
  124. PROCD_RELOAD_DELAY=2000
  125. procd_add_reload_trigger "dhcp" "system" "samba4"
  126. local i
  127. for i in $samba_iface; do
  128. procd_add_reload_interface_trigger $i
  129. done
  130. }
  131. start_service() {
  132. init_config
  133. # start main AD-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  134. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  135. procd_open_instance
  136. procd_set_param command /usr/sbin/samba -F
  137. procd_set_param respawn
  138. procd_set_param file /var/etc/smb.conf
  139. procd_close_instance
  140. else
  141. # start fileserver daemon
  142. procd_open_instance
  143. procd_set_param command /usr/sbin/smbd -F
  144. procd_set_param respawn
  145. procd_set_param file /var/etc/smb.conf
  146. procd_close_instance
  147. # start netbios daemon
  148. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  149. procd_open_instance
  150. procd_set_param command /usr/sbin/nmbd -F
  151. procd_set_param respawn
  152. procd_set_param file /var/etc/smb.conf
  153. procd_close_instance
  154. fi
  155. # start winbind daemon
  156. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  157. procd_open_instance
  158. procd_set_param command /usr/sbin/winbindd -F
  159. procd_set_param respawn
  160. procd_set_param file /var/etc/smb.conf
  161. procd_close_instance
  162. fi
  163. fi
  164. # lower priority using renice (if found)
  165. if [ -x /usr/bin/renice ]; then
  166. [ -x /usr/sbin/samba ] && renice -n 2 $(pidof samba)
  167. [ -x /usr/sbin/smbd ] && renice -n 2 $(pidof smbd)
  168. [ -x /usr/sbin/nmbd ] && renice -n 2 $(pidof nmbd)
  169. [ -x /usr/sbin/winbindd ] && renice -n 2 $(pidof winbindd)
  170. fi
  171. }