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.5 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=98
  3. USE_PROCD=1
  4. CIFSD_IFACE=""
  5. smb_header()
  6. {
  7. config_get CIFSD_IFACE $1 interface "lan"
  8. # resolve interfaces
  9. local interfaces
  10. interfaces=$(
  11. . /lib/functions/network.sh
  12. local net
  13. for net in $CIFSD_IFACE; do
  14. local device
  15. network_is_up $net || continue
  16. network_get_device device "$net"
  17. echo -n "${device:-$net} "
  18. done
  19. )
  20. local workgroup description
  21. local hostname
  22. hostname="$(cat /proc/sys/kernel/hostname)"
  23. config_get workgroup $1 workgroup "WORKGROUP"
  24. config_get description $1 description "Cifsd on OpenWrt"
  25. sed -e "s#|NAME|#$hostname#g" \
  26. -e "s#|WORKGROUP|#$workgroup#g" \
  27. -e "s#|DESCRIPTION|#$description#g" \
  28. -e "s#|INTERFACES|#$interfaces#g" \
  29. /etc/cifs/smb.conf.template > /var/etc/cifs/smb.conf
  30. [ -e /etc/cifs/smb.conf ] || ln -nsf /var/etc/cifs/smb.conf /etc/cifs/smb.conf
  31. if [ ! -L /etc/cifs/smb.conf ]; then
  32. logger -t 'cifsd' "Local custom /etc/cifs/smb.conf file detected, all UCI/Luci config settings are ignored!"
  33. fi
  34. }
  35. smb_add_share()
  36. {
  37. local name
  38. local path
  39. local comment
  40. local users
  41. local create_mask
  42. local dir_mask
  43. local browseable
  44. local read_only
  45. local writeable
  46. local guest_ok
  47. local force_root
  48. local write_list
  49. local read_list
  50. local hide_dot_files
  51. local veto_files
  52. local inherit_owner
  53. local force_create_mode
  54. local force_directory_mode
  55. config_get name $1 name
  56. config_get path $1 path
  57. config_get comment $1 comment
  58. config_get users $1 users
  59. config_get create_mask $1 create_mask
  60. config_get dir_mask $1 dir_mask
  61. config_get browseable $1 browseable
  62. config_get read_only $1 read_only
  63. config_get writeable $1 writeable
  64. config_get guest_ok $1 guest_ok
  65. config_get_bool force_root $1 force_root 0
  66. config_get write_list $1 write_list
  67. config_get read_list $1 read_list
  68. config_get hide_dot_files $1 hide_dot_files
  69. config_get veto_files $1 veto_files
  70. config_get inherit_owner $1 inherit_owner
  71. config_get force_create_mode $1 force_create_mode
  72. config_get force_directory_mode $1 force_directory_mode
  73. [ -z "$name" ] || [ -z "$path" ] && return
  74. {
  75. printf "\n[%s]\n\tpath = %s\n" "$name" "$path"
  76. [ -n "$comment" ] && printf "\tcomment = %s\n" "$comment"
  77. if [ "$force_root" -eq 1 ]; then
  78. printf "\tforce user = %s\n" "root"
  79. printf "\tforce group = %s\n" "root"
  80. else
  81. [ -n "$users" ] && printf "\tvalid users = %s\n" "$users"
  82. fi
  83. [ -n "$create_mask" ] && printf "\tcreate mask = %s\n" "$create_mask"
  84. [ -n "$dir_mask" ] && printf "\tdirectory mask = %s\n" "$dir_mask"
  85. [ -n "$force_create_mode" ] && printf "\tforce create mode = %s\n" "$force_create_mode"
  86. [ -n "$force_directory_mode" ] && printf "\tforce directory mode = %s\n" "$force_directory_mode"
  87. [ -n "$browseable" ] && printf "\tbrowseable = %s\n" "$browseable"
  88. [ -n "$read_only" ] && printf "\tread only = %s\n" "$read_only"
  89. [ -n "$writeable" ] && printf "\twriteable = %s\n" "$writeable"
  90. [ -n "$guest_ok" ] && printf "\tguest ok = %s\n" "$guest_ok"
  91. [ -n "$inherit_owner" ] && printf "\tinherit owner = %s\n" "$inherit_owner"
  92. [ -n "$write_list" ] && printf "\twrite list = %s\n" "$write_list"
  93. [ -n "$read_list" ] && printf "\tread list = %s\n" "$read_list"
  94. [ -n "$hide_dot_files" ] && printf "\thide dot files = %s\n" "$hide_dot_files"
  95. [ -n "$veto_files" ] && printf "\tveto files = %s\n" "$veto_files"
  96. } >> /var/etc/cifs/smb.conf
  97. }
  98. init_config()
  99. {
  100. mkdir -p /var/etc/cifs
  101. config_load cifsd
  102. # allow copy&paste from samba UCI configs (we dont have a cifsd wiki yet)
  103. config_foreach smb_header globals
  104. config_foreach smb_header samba
  105. config_foreach smb_add_share share
  106. config_foreach smb_add_share sambashare
  107. }
  108. service_triggers()
  109. {
  110. PROCD_RELOAD_DELAY=2000
  111. procd_add_reload_trigger "dhcp" "system" "cifsd"
  112. local i
  113. for i in $CIFSD_IFACE; do
  114. procd_add_reload_interface_trigger $i
  115. done
  116. }
  117. start_service()
  118. {
  119. init_config
  120. if [ ! -e /etc/cifs/smb.conf ]; then
  121. logger -t 'cifsd' "missing config /etc/cifs/smb.conf, needs to-be created manually!"
  122. exit 1
  123. fi
  124. if [ -e /sys/module/cifsd ]; then
  125. if [ -e /sys/class/cifsd-control/kill_server ]; then
  126. # upstream "BUG": ensure changes in smb.conf are reflected on a running kernel-server
  127. echo hard > /sys/class/cifsd-control/kill_server
  128. # we need a extra timeout for the reset
  129. sleep 5
  130. fi
  131. fi
  132. modprobe cifsd 2> /dev/null
  133. if [ ! -e /sys/module/cifsd ]; then
  134. logger -t 'cifsd' "modprobe of cifsd module failed, can\'t start cifsd!"
  135. exit 1
  136. fi
  137. logger -t 'cifsd' "Starting CIFS/SMB userspace service."
  138. procd_open_instance
  139. procd_set_param command /usr/sbin/cifsd --n
  140. procd_set_param file /var/etc/cifs/smb.conf
  141. procd_close_instance
  142. }
  143. stop_service()
  144. {
  145. logger -t 'cifsd' "Stopping CIFSD userspace service."
  146. killall cifsd > /dev/null 2>&1
  147. [ -e /sys/module/cifsd ] && rmmod cifsd > /dev/null 2>&1
  148. # With open smb connections rmmod is not possible, without waiting for the long 'ipc timeout', so we use 'kill_server'!
  149. if [ -e /sys/module/cifsd ]; then
  150. logger -t 'cifsd' "triggering kill_server"
  151. if [ -e /sys/class/cifsd-control/kill_server ]; then
  152. echo hard > /sys/class/cifsd-control/kill_server
  153. # we need a extra timeout for the reset
  154. sleep 5
  155. fi
  156. fi
  157. # next try
  158. [ -e /sys/module/cifsd ] && rmmod cifsd > /dev/null 2>&1
  159. # check again
  160. if [ -e /sys/module/cifsd ]; then
  161. # wait more...
  162. sleep 3
  163. fi
  164. # last try
  165. [ -e /sys/module/cifsd ] && rmmod cifsd > /dev/null 2>&1
  166. if [ -e /sys/module/cifsd ]; then
  167. logger -t 'cifsd' "module still loaded after 8s timeout"
  168. fi
  169. [ -f /tmp/cifsd.lock ] && rm /tmp/cifsd.lock
  170. }
  171. # reload_service() {
  172. # restart "$@"
  173. # }