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.

192 lines
5.3 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 DISABLE_NETBIOS $1 disable_netbios 0
  24. config_get_bool DISABLE_AD_DC $1 disable_ad_dc 0
  25. config_get_bool DISABLE_WINBIND $1 disable_winbind 0
  26. mkdir -p /var/etc
  27. sed -e "s#|NAME|#$name#g" \
  28. -e "s#|WORKGROUP|#$workgroup#g" \
  29. -e "s#|DESCRIPTION|#$description#g" \
  30. -e "s#|INTERFACES|#$interfaces#g" \
  31. -e "s#|CHARSET|#$charset#g" \
  32. /etc/samba/smb.conf.template > /var/etc/smb.conf
  33. echo -e "\n######### Dynamic written config options #########\n" >> /var/etc/smb.conf
  34. if [ "$DISABLE_NETBIOS" -eq 1 ] || [ ! -x /usr/sbin/nmbd ]; then
  35. echo -e "\tdisable netbios = yes" >> /var/etc/smb.conf
  36. fi
  37. local homes
  38. config_get_bool homes $1 homes 0
  39. [ $homes -gt 0 ] && {
  40. cat <<EOT >> /var/etc/smb.conf
  41. [homes]
  42. comment = Home Directories
  43. browsable = no
  44. writable = yes
  45. read only = no
  46. create mask = 0750
  47. EOT
  48. }
  49. [ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  50. }
  51. smb_add_share() {
  52. local name
  53. local path
  54. local users
  55. local public
  56. local writable
  57. local printable
  58. local create_mask
  59. local browseable
  60. local read_only
  61. local guest_ok
  62. local guest_only
  63. local inherit_owner
  64. local vfs_objects
  65. config_get name $1 name
  66. config_get path $1 path
  67. config_get users $1 users
  68. config_get public $1 public
  69. config_get writable $1 writable
  70. config_get printable $1 printable
  71. config_get create_mask $1 create_mask
  72. config_get dir_mask $1 dir_mask
  73. config_get browseable $1 browseable
  74. config_get read_only $1 read_only
  75. config_get guest_ok $1 guest_ok
  76. config_get guest_only $1 guest_only
  77. config_get inherit_owner $1 inherit_owner
  78. config_get vfs_objects $1 vfs_objects
  79. [ -z "$name" -o -z "$path" ] && return
  80. echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
  81. [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
  82. [ -n "$public" ] && echo -e "\tpublic = $public" >> /var/etc/smb.conf
  83. [ -n "$writable" ] && echo -e "\twritable = $writable" >> /var/etc/smb.conf
  84. [ -n "$printable" ] && echo -e "\tprintable = $printable" >> /var/etc/smb.conf
  85. [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
  86. [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf
  87. [ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >> /var/etc/smb.conf
  88. [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf
  89. [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf
  90. [ -n "$guest_only" ] && echo -e "\tguest only = $guest_only" >> /var/etc/smb.conf
  91. [ -n "$inherit_owner" ] && echo -e "\tinherit owner = $inherit_owner" >> /var/etc/smb.conf
  92. [ -n "$vfs_objects" ] && echo -e "\tvfs objects = $vfs_objects" >> /var/etc/smb.conf
  93. }
  94. init_config() {
  95. # Create samba dirs
  96. [ -d /var/lib/samba ] || mkdir -p /var/lib/samba
  97. [ -d /var/cache/samba ] || mkdir -p /var/cache/samba
  98. [ -d /var/run/samba ] || mkdir -p /var/run/samba
  99. [ -d /var/log/samba ] || mkdir -p /var/log/samba
  100. [ -d /var/lock ] && chmod 0755 /var/lock || {
  101. mkdir -p /var/lock
  102. chmod 0755 /var/lock
  103. }
  104. config_load samba4
  105. config_foreach smb_header samba
  106. config_foreach smb_add_share sambashare
  107. }
  108. reload_service() {
  109. init_config
  110. killall -HUP samba
  111. killall -HUP smbd
  112. killall -HUP nmbd
  113. killall -HUP winbindd
  114. }
  115. service_triggers() {
  116. procd_add_reload_trigger samba4
  117. local i
  118. for i in $samba_iface; do
  119. procd_add_reload_interface_trigger $i
  120. done
  121. }
  122. start_service() {
  123. init_config
  124. # start main AC-DC daemon, will spawn (smbd,nmbd,winbindd) as needed/configured.
  125. if [ "$DISABLE_AD_DC" -ne 1 ] && [ -x /usr/sbin/samba ]; then
  126. procd_open_instance
  127. procd_set_param command /usr/sbin/samba -F
  128. procd_set_param respawn
  129. procd_set_param file /var/etc/smb.conf
  130. procd_close_instance
  131. else
  132. # start fileserver daemon
  133. procd_open_instance
  134. procd_set_param command /usr/sbin/smbd -F
  135. procd_set_param respawn
  136. procd_set_param file /var/etc/smb.conf
  137. procd_close_instance
  138. # start netbios daemon
  139. if [ "$DISABLE_NETBIOS" -ne 1 ] && [ -x /usr/sbin/nmbd ]; then
  140. procd_open_instance
  141. procd_set_param command /usr/sbin/nmbd -F
  142. procd_set_param respawn
  143. procd_set_param file /var/etc/smb.conf
  144. procd_close_instance
  145. fi
  146. # start winbind daemon
  147. if [ "$DISABLE_WINBIND" -ne 1 ] && [ -x /usr/sbin/winbindd ]; then
  148. procd_open_instance
  149. procd_set_param command /usr/sbin/winbindd -F
  150. procd_set_param respawn
  151. procd_set_param file /var/etc/smb.conf
  152. procd_close_instance
  153. fi
  154. fi
  155. # lower priority using renice (if found)
  156. if [ -x /usr/bin/renice ]; then
  157. [ -x /usr/sbin/samba ] && renice -n 2 $(pidof samba)
  158. [ -x /usr/sbin/smbd ] && renice -n 2 $(pidof smbd)
  159. [ -x /usr/sbin/nmbd ] && renice -n 2 $(pidof nmbd)
  160. [ -x /usr/sbin/winbindd ] && renice -n 2 $(pidof winbindd)
  161. fi
  162. }