miniupnpd.init 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2014 OpenWrt.org
  3. START=94
  4. STOP=15
  5. SERVICE_USE_PID=1
  6. upnpd_get_port_range() {
  7. local _var="$1"; shift
  8. local _val
  9. config_get _val "$@"
  10. case "$_val" in
  11. [0-9]*[:-][0-9]*)
  12. export -n -- "${_var}_start=${_val%%[:-]*}"
  13. export -n -- "${_var}_end=${_val##*[:-]}"
  14. ;;
  15. [0-9]*)
  16. export -n -- "${_var}_start=$_val"
  17. export -n -- "${_var}_end="
  18. ;;
  19. esac
  20. }
  21. conf_rule_add() {
  22. local cfg="$1"
  23. local tmpconf="$2"
  24. local action external_port_start external_port_end int_addr
  25. local internal_port_start internal_port_end
  26. config_get action "$cfg" action "deny" # allow or deny
  27. upnpd_get_port_range "ext" "$cfg" ext_ports "0-65535" # external ports: x, x-y, x:y
  28. config_get int_addr "$cfg" int_addr "0.0.0.0/0" # ip or network and subnet mask (internal)
  29. upnpd_get_port_range "int" "$cfg" int_ports "0-65535" # internal ports: x, x-y, x:y or range
  30. # Make a single IP IP/32 so that miniupnpd.conf can use it.
  31. case "$int_addr" in
  32. */*) ;;
  33. *) int_addr="$int_addr/32" ;;
  34. esac
  35. echo "${action} ${ext_start}${ext_end:+-}${ext_end} ${int_addr} ${int_start}${int_end:+-}${int_end}" >>$tmpconf
  36. }
  37. upnpd_write_bool() {
  38. local opt="$1"
  39. local def="${2:-0}"
  40. local alt="$3"
  41. local val
  42. config_get_bool val config "$opt" "$def"
  43. if [ "$val" -eq 0 ]; then
  44. echo "${alt:-$opt}=no" >> $tmpconf
  45. else
  46. echo "${alt:-$opt}=yes" >> $tmpconf
  47. fi
  48. }
  49. boot() {
  50. return
  51. }
  52. start() {
  53. config_load "upnpd"
  54. local extiface intiface upload download logging secure enabled natpmp
  55. local extip port usesysuptime conffile serial_number model_number
  56. local uuid notify_interval presentation_url enable_upnp
  57. local upnp_lease_file clean_ruleset_threshold clean_ruleset_interval
  58. local ipv6_listening_ip enabled
  59. config_get_bool enabled config enabled 1
  60. [ "$enabled" -gt 0 ] || return 1
  61. config_get extiface config external_iface
  62. config_get extzone config external_zone
  63. config_get intiface config internal_iface
  64. config_get extip config external_ip
  65. config_get port config port 5000
  66. config_get upload config upload
  67. config_get download config download
  68. config_get_bool logging config log_output 0
  69. config_get conffile config config_file
  70. config_get serial_number config serial_number
  71. config_get model_number config model_number
  72. config_get uuid config uuid
  73. config_get notify_interval config notify_interval
  74. config_get presentation_url config presentation_url
  75. config_get upnp_lease_file config upnp_lease_file
  76. config_get clean_ruleset_threshold config clean_ruleset_threshold
  77. config_get clean_ruleset_interval config clean_ruleset_interval
  78. config_get ipv6_listening_ip config ipv6_listening_ip
  79. local args
  80. . /lib/functions/network.sh
  81. local ifname
  82. # manual external interface overrides everything
  83. if [ -z "$extiface" ] ; then
  84. # manual external zone (if dynamically find interfaces
  85. # belonging to it) overrides network_find_wan*
  86. if [ -n "$extzone" ] ; then
  87. ifname=$(fw3 -q zone $extzone | head -1)
  88. fi
  89. [ -n "$extiface" ] || network_find_wan extiface
  90. [ -n "$extiface" ] || network_find_wan6 extiface
  91. fi
  92. [ -n "$ifname" ] || network_get_device ifname ${extiface}
  93. if [ -n "$conffile" ]; then
  94. args="-f $conffile"
  95. else
  96. local tmpconf="/var/etc/miniupnpd.conf"
  97. args="-f $tmpconf"
  98. mkdir -p /var/etc
  99. echo "ext_ifname=$ifname" >$tmpconf
  100. [ -n "$extip" ] && \
  101. echo "ext_ip=$extip" >>$tmpconf
  102. local iface
  103. for iface in ${intiface:-lan}; do
  104. local device
  105. network_get_device device "$iface" && {
  106. echo "listening_ip=$device" >>$tmpconf
  107. }
  108. done
  109. [ "$port" != "auto" ] && \
  110. echo "port=$port" >>$tmpconf
  111. config_load "upnpd"
  112. upnpd_write_bool enable_natpmp 1
  113. upnpd_write_bool enable_upnp 1
  114. upnpd_write_bool secure_mode 1
  115. upnpd_write_bool pcp_allow_thirdparty 0
  116. upnpd_write_bool system_uptime 1
  117. [ -n "$upnp_lease_file" ] && \
  118. echo "lease_file=$upnp_lease_file" >>$tmpconf
  119. [ -n "$upload" -a -n "$download" ] && {
  120. echo "bitrate_down=$(($download * 1024 * 8))" >>$tmpconf
  121. echo "bitrate_up=$(($upload * 1024 * 8))" >>$tmpconf
  122. }
  123. [ -n "${presentation_url}" ] && \
  124. echo "presentation_url=${presentation_url}" >>$tmpconf
  125. [ -n "${notify_interval}" ] && \
  126. echo "notify_interval=${notify_interval}" >>$tmpconf
  127. [ -n "${clean_ruleset_threshold}" ] && \
  128. echo "clean_ruleset_threshold=${clean_ruleset_threshold}" >>$tmpconf
  129. [ -n "${clean_ruleset_interval}" ] && \
  130. echo "clean_ruleset_interval=${clean_ruleset_interval}" >>$tmpconf
  131. [ -n "${ipv6_listening_ip}" ] && \
  132. echo "ipv6_listening_ip=${ipv6_listening_ip}" >>$tmpconf
  133. [ -z "$uuid" ] && {
  134. uuid="$(cat /proc/sys/kernel/random/uuid)"
  135. uci set upnpd.config.uuid=$uuid
  136. uci commit upnpd
  137. }
  138. [ "$uuid" = "nocli" ] || \
  139. echo "uuid=$uuid" >>$tmpconf
  140. [ -n "${serial_number}" ] && \
  141. echo "serial=${serial_number}" >>$tmpconf
  142. [ -n "${model_number}" ] && \
  143. echo "model_number=${model_number}" >>$tmpconf
  144. config_foreach conf_rule_add perm_rule "$tmpconf"
  145. fi
  146. if [ -n "$ifname" ]; then
  147. # start firewall
  148. iptables -L MINIUPNPD >/dev/null 2>/dev/null || fw3 reload
  149. if [ "$logging" = "1" ]; then
  150. SERVICE_DAEMONIZE=1 \
  151. service_start /usr/sbin/miniupnpd $args -d
  152. else
  153. SERVICE_DAEMONIZE= \
  154. service_start /usr/sbin/miniupnpd $args
  155. fi
  156. else
  157. logger -t "upnp daemon" "external interface not found, not starting"
  158. fi
  159. }
  160. stop() {
  161. service_stop /usr/sbin/miniupnpd
  162. iptables -t nat -F MINIUPNPD 2>/dev/null
  163. iptables -t filter -F MINIUPNPD 2>/dev/null
  164. [ -x /usr/sbin/ip6tables ] && {
  165. ip6tables -t filter -F MINIUPNPD 2>/dev/null
  166. }
  167. }