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.

183 lines
4.7 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_bool hide_dot_files $1 hide_dot_files 0
  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. [ "$hide_dot_files" -eq 1 ] && printf "\thide dot files = %s\n" "yes"
  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. modprobe cifsd 2> /dev/null
  125. if [ ! -e /sys/module/cifsd ]; then
  126. logger -t 'cifsd' "modprobe of cifsd module failed, can\'t start cifsd!"
  127. exit 1
  128. fi
  129. logger -t 'cifsd' "Starting CIFS/SMB userspace service."
  130. procd_open_instance
  131. procd_set_param command /usr/sbin/cifsd --n
  132. procd_close_instance
  133. }
  134. stop_service()
  135. {
  136. logger -t 'cifsd' "Stopping CIFSD userspace service."
  137. killall cifsd > /dev/null 2>&1
  138. sleep 1
  139. [ -e /sys/class/cifsd-control/kill_server ] && echo hard > /sys/class/cifsd-control/kill_server
  140. sleep 2
  141. [ -e /sys/module/cifsd ] && rmmod cifsd > /dev/null 2>&1
  142. # With open smb connections rmmod takes longer
  143. if [ -e /sys/module/cifsd ]; then
  144. sleep 5
  145. rmmod cifsd > /dev/null 2>&1
  146. fi
  147. [ -f /tmp/cifsd.lock ] && rm /tmp/cifsd.lock
  148. }
  149. reload_service() {
  150. stop_service "$@"
  151. sleep 1
  152. start_service "$@"
  153. }