samba.init 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2008-2012 OpenWrt.org
  3. START=60
  4. USE_PROCD=1
  5. smb_header() {
  6. local interface
  7. config_get interface $1 interface "loopback lan"
  8. # resolve interfaces
  9. local interfaces=$(
  10. . /lib/functions/network.sh
  11. local net
  12. for net in $interface; do
  13. local device
  14. network_get_device device "$net" && {
  15. local subnet
  16. network_get_subnet subnet "$net" && echo -n "$subnet "
  17. network_get_subnet6 subnet "$net" && echo -n "$subnet "
  18. }
  19. echo -n "${device:-$net} "
  20. done
  21. )
  22. local name workgroup description charset
  23. local hostname="$(uci_get system.@system[0].hostname)"
  24. config_get name $1 name "${hostname:-OpenWrt}"
  25. config_get workgroup $1 workgroup "${hostname:-OpenWrt}"
  26. config_get description $1 description "Samba on ${hostname:-OpenWrt}"
  27. config_get charset $1 charset "UTF-8"
  28. mkdir -p /var/etc
  29. sed -e "s#|NAME|#$name#g" \
  30. -e "s#|WORKGROUP|#$workgroup#g" \
  31. -e "s#|DESCRIPTION|#$description#g" \
  32. -e "s#|INTERFACES|#$interfaces#g" \
  33. -e "s#|CHARSET|#$charset#g" \
  34. /etc/samba/smb.conf.template > /var/etc/smb.conf
  35. local homes
  36. config_get_bool homes $1 homes 0
  37. [ $homes -gt 0 ] && {
  38. cat <<EOT >> /var/etc/smb.conf
  39. [homes]
  40. comment = Home Directories
  41. browsable = no
  42. read only = no
  43. create mode = 0750
  44. EOT
  45. }
  46. [ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
  47. }
  48. smb_add_share() {
  49. local name
  50. local path
  51. local users
  52. local read_only
  53. local guest_ok
  54. local create_mask
  55. local dir_mask
  56. local browseable
  57. config_get name $1 name
  58. config_get path $1 path
  59. config_get users $1 users
  60. config_get read_only $1 read_only
  61. config_get guest_ok $1 guest_ok
  62. config_get create_mask $1 create_mask
  63. config_get dir_mask $1 dir_mask
  64. config_get browseable $1 browseable
  65. [ -z "$name" -o -z "$path" ] && return
  66. echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
  67. [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
  68. [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf
  69. [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf
  70. [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
  71. [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf
  72. [ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >> /var/etc/smb.conf
  73. }
  74. init_config() {
  75. config_load samba
  76. config_foreach smb_header samba
  77. config_foreach smb_add_share sambashare
  78. }
  79. reload_service() {
  80. init_config
  81. killall -HUP smbd
  82. }
  83. service_triggers() {
  84. procd_add_reload_trigger samba
  85. }
  86. start_service() {
  87. init_config
  88. procd_open_instance
  89. procd_set_param command /usr/sbin/smbd -F
  90. procd_set_param respawn
  91. procd_close_instance
  92. procd_open_instance
  93. procd_set_param command /usr/sbin/nmbd -F
  94. procd_set_param respawn
  95. procd_close_instance
  96. }