gvalkov

ksmbd-init 3.4.2 fix

Nov 29th, 2021
989
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh /etc/rc.common
  2.  
  3. START=98
  4. USE_PROCD=1
  5.  
  6. SMBD_IFACE=""
  7.  
  8. config_get_sane()
  9. {
  10.     config_get "$@"
  11.     set -- "$(echo "$1" | tr -d '<>[]{};%?=#\n')"
  12. }
  13.  
  14. smb_header()
  15. {
  16.     config_get_sane SMBD_IFACE "$1" interface "lan"
  17.  
  18.     # resolve interfaces
  19.     interfaces=$(
  20.         . /lib/functions/network.sh
  21.  
  22.         for net in $SMBD_IFACE; do
  23.             network_is_up "$net" || continue
  24.             network_get_device device "$net"
  25.             printf "%s " "${device:-$net}"
  26.         done
  27.     )
  28.  
  29.     # we dont use netbios anymore as default and wsd/avahi is dns based
  30.     hostname="$(sed 's/\..*//' /proc/sys/kernel/hostname | tr -d '{};%?=#\n')"
  31.  
  32.     config_get_sane workgroup "$1" workgroup "WORKGROUP"
  33.     config_get_sane description "$1" description "Ksmbd on OpenWrt"
  34.     config_get_bool ALLOW_LEGACY_PROTOCOLS "$1" allow_legacy_protocols 0
  35.  
  36.     sed -e "s#|NAME|#$hostname#g" \
  37.         -e "s#|WORKGROUP|#$workgroup#g" \
  38.         -e "s#|DESCRIPTION|#$description#g" \
  39.         -e "s#|INTERFACES|#$interfaces#g" \
  40.         /etc/ksmbd/smb.conf.template > /var/etc/ksmbd/smb.conf
  41.        
  42.     {
  43.         printf "\n######### Dynamic written config options #########\n"
  44.  
  45.         if [ "$ALLOW_LEGACY_PROTOCOLS" -eq 1 ]; then
  46.             logger -p daemon.info -t 'ksmbd' "Legacy Protocols allowed, don't use this option for secure environments!"
  47.             printf "\tserver min protocol = NT1\n"
  48.             printf "\tserver signing = disabled\n"
  49.         fi
  50.     } >> /var/etc/ksmbd/smb.conf
  51.  
  52.     [ -e /etc/ksmbd/smb.conf ] || ln -nsf /var/etc/ksmbd/smb.conf /etc/ksmbd/smb.conf
  53.  
  54.     if [ ! -L /etc/ksmbd/smb.conf ]; then
  55.         logger -p daemon.warn -t 'ksmbd' "Local custom /etc/ksmbd/smb.conf file detected, all UCI/Luci config settings are ignored!"
  56.     fi
  57. }
  58.  
  59. smb_add_share()
  60. {
  61.     config_get_sane name "$1" name
  62.     config_get_sane path "$1" path
  63.     config_get_sane comment "$1" comment
  64.     config_get_sane users "$1" users
  65.     config_get_sane create_mask "$1" create_mask
  66.     config_get_sane dir_mask "$1" dir_mask
  67.     config_get_sane browseable "$1" browseable
  68.     config_get_sane read_only "$1" read_only
  69.     config_get_sane writeable "$1" writeable
  70.     config_get_sane guest_ok "$1" guest_ok
  71.     config_get_bool force_root "$1" force_root 0
  72.     config_get_sane write_list "$1" write_list
  73.     config_get_sane read_list "$1" read_list
  74.     config_get_sane hide_dot_files "$1" hide_dot_files
  75.     config_get_sane veto_files "$1" veto_files
  76.     config_get_sane inherit_owner "$1" inherit_owner
  77.     config_get_sane force_create_mode "$1" force_create_mode
  78.     config_get_sane force_directory_mode "$1" force_directory_mode
  79.  
  80.     [ -z "$name" ] || [ -z "$path" ] && return
  81.  
  82.     {
  83.         printf "\n[%s]\n\tpath = %s\n" "$name" "$path"
  84.         [ -n "$comment" ] && printf "\tcomment = %s\n" "$comment"
  85.  
  86.         if [ "$force_root" -eq 1 ]; then
  87.             printf "\tforce user = %s\n" "root"
  88.             printf "\tforce group = %s\n" "root"
  89.         else
  90.             [ -n "$users" ] && printf "\tvalid users = %s\n" "$users"
  91.         fi
  92.  
  93.         [ -n "$create_mask" ] && printf "\tcreate mask = %s\n" "$create_mask"
  94.         [ -n "$dir_mask" ] && printf "\tdirectory mask = %s\n" "$dir_mask"
  95.         [ -n "$force_create_mode" ] && printf "\tforce create mode = %s\n" "$force_create_mode"
  96.         [ -n "$force_directory_mode" ] && printf "\tforce directory mode = %s\n" "$force_directory_mode"
  97.  
  98.         [ -n "$browseable" ] && printf "\tbrowseable = %s\n" "$browseable"
  99.         [ -n "$read_only" ] && printf "\tread only = %s\n" "$read_only"
  100.         [ -n "$writeable" ] && printf "\twriteable = %s\n" "$writeable"
  101.  
  102.         [ -n "$guest_ok" ] && printf "\tguest ok = %s\n" "$guest_ok"
  103.         [ -n "$inherit_owner" ] && printf "\tinherit owner = %s\n" "$inherit_owner"
  104.  
  105.         [ -n "$write_list" ] && printf "\twrite list = %s\n" "$write_list"
  106.         [ -n "$read_list" ] && printf "\tread list = %s\n" "$read_list"
  107.  
  108.         [ -n "$hide_dot_files" ] && printf "\thide dot files = %s\n" "$hide_dot_files"
  109.         [ -n "$veto_files" ] && printf "\tveto files = %s\n" "$veto_files"
  110.     } >> /var/etc/ksmbd/smb.conf
  111. }
  112.  
  113. init_config()
  114. {
  115.     mkdir -p /var/etc/ksmbd
  116.  
  117.     config_load ksmbd
  118.     config_foreach smb_header globals
  119.     config_foreach smb_add_share share
  120. }
  121.  
  122. service_triggers()
  123. {
  124.     # PROCD_RELOAD_DELAY=1000
  125.  
  126.     procd_add_reload_trigger "dhcp" "system" "ksmbd"
  127.  
  128.     for i in $SMBD_IFACE; do
  129.         procd_add_reload_interface_trigger "$i"
  130.     done
  131. }
  132.  
  133. kill_server()
  134. {
  135.     if [ -e /sys/module/ksmbd ]; then
  136.         if [ -e /sys/class/ksmbd-control/kill_server ]; then
  137.             logger -p daemon.info -t 'ksmbd' "triggering kill_server"
  138.             echo hard > /sys/class/ksmbd-control/kill_server
  139.         fi
  140.     fi
  141. }
  142.  
  143. start_service()
  144. {
  145.     init_config
  146.  
  147.     if [ ! -e /etc/ksmbd/smb.conf ]; then
  148.         logger -p daemon.error -t 'ksmbd' "missing config /etc/ksmbd/smb.conf!"
  149.         exit 1
  150.     fi
  151.    
  152.     # NOTE: We don't do a soft-reload via signal, since [global] smb.conf setting changes will be ignored, so always reset hard.
  153.     [ -e /sys/module/ksmbd ] && stop_service
  154.    
  155.     [ ! -e /sys/module/ksmbd ] && modprobe ksmbd 2> /dev/null
  156.     if [ ! -e /sys/module/ksmbd ]; then
  157.         logger -p daemon.error -t 'ksmbd' "modprobe of ksmbd module failed, can\'t start ksmbd!"
  158.         exit 1
  159.     fi
  160.  
  161.     logger -p daemon.notice -t 'ksmbd' "Starting Ksmbd userspace service."
  162.     procd_open_instance
  163.     procd_add_mdns "smb" "tcp" "445" "daemon=ksmbd"
  164.     procd_set_param command /usr/sbin/ksmbd.mountd --n
  165.     procd_set_param file /etc/ksmbd/smb.conf
  166.     procd_set_param limits nofile=16384
  167.     procd_close_instance
  168. }
  169.  
  170. stop_service()
  171. {
  172.     logger -p daemon.notice -t 'ksmbd' "Stopping Ksmbd userspace service."
  173.     killall ksmbd.mountd > /dev/null 2>&1
  174.    
  175.     [ -e /sys/module/ksmbd ] && rmmod ksmbd > /dev/null 2>&1
  176.     # kill server if we cant rmmod
  177.     [ -e /sys/module/ksmbd ] && kill_server
  178.     # next try
  179.     [ -e /sys/module/ksmbd ] && rmmod ksmbd > /dev/null 2>&1
  180.    
  181.     if [ -e /sys/module/ksmbd ]; then
  182.         logger -p daemon.error -t 'ksmbd' "module still loaded after kill_server?"
  183.     fi
  184.     [ -f /tmp/ksmbd.lock ] && rm /tmp/ksmbd.lock
  185. }
  186.  
  187. # reload_service() {
  188.     # restart "$@"
  189. # }
  190.  
RAW Paste Data