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.

179 lines
3.5 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2008 OpenWrt.org
  3. START=90
  4. USE_PROCD=1
  5. PID_FILE="/var/run/stunnel.pid"
  6. CONF_FILE="/tmp/stunnel.conf"
  7. BIN="/usr/bin/stunnel"
  8. global_defs() {
  9. local debug compression
  10. config_get alt_config_file 'globals' alt_config_file
  11. [ -z "$alt_config_file" ] || return 0
  12. # Set default settings
  13. printf "foreground = yes\n" >> "$CONF_FILE"
  14. printf "pid = %s\n" "$PID_FILE" >> "$CONF_FILE"
  15. printf "syslog = yes\n" >> "$CONF_FILE"
  16. config_get debug 'globals' debug '5'
  17. printf "debug = %s\n" "$debug" >> "$CONF_FILE"
  18. config_get compression 'globals' compression
  19. [ -z "$compression" ] || printf "compression = %s\n" "$compression" >> "$CONF_FILE"
  20. }
  21. print_options() {
  22. local config=$1
  23. shift
  24. for opt in "$@"; do
  25. local $opt
  26. local value
  27. local is_boolean=0
  28. if [ "${opt:0:5}" == "bool_" ]; then
  29. opt="${opt:5}"
  30. is_boolean=1
  31. fi
  32. config_get "value" "$config" "$opt"
  33. [ -z "$value" ] || {
  34. if [ "$value" = '1' ] && [ "$is_boolean" -eq "1" ]; then
  35. value="yes"
  36. elif [ "$value" = '0' ] && [ "$is_boolean" -eq "1" ] ; then
  37. value="no"
  38. fi
  39. printf "%s = %s\n" "$opt" "$value" >> "$CONF_FILE"
  40. }
  41. done
  42. }
  43. print_list() {
  44. local config=$1
  45. shift
  46. for opt in "$@"; do
  47. local $opt
  48. local elements
  49. config_get "elements" "$config" "$opt"
  50. for element in $elements; do
  51. printf "%s = %s\n" "$opt" "$element" >> "$CONF_FILE"
  52. done
  53. done
  54. }
  55. print_list_colon() {
  56. local config=$1
  57. local value
  58. shift
  59. for opt in "$@"; do
  60. local $opt
  61. local elements
  62. config_get "elements" "$config" "$opt"
  63. for element in $elements; do
  64. value="${value}:${element}"
  65. done
  66. printf "%s = %s\n" "$opt" "${value#*:}" >> "$CONF_FILE"
  67. done
  68. }
  69. service_section() {
  70. local cfg="$1"
  71. local accept_host accept_port enabled
  72. config_get_bool enabled "$cfg" 'enabled' '1'
  73. [ ${enabled} -gt 0 ] || return 0
  74. printf "\n" >> "$CONF_FILE"
  75. printf "[%s]\n" "$cfg" >> "$CONF_FILE"
  76. config_get accept_host "$cfg" accept_host 'localhost'
  77. config_get accept_port "$cfg" accept_port
  78. printf "accept = %s:%s\n" "$accept_host" "$accept_port" >> "$CONF_FILE"
  79. print_options "$cfg" CApath \
  80. CAfile \
  81. cert \
  82. CRLpath \
  83. CRLfile \
  84. curve \
  85. logId \
  86. debug \
  87. engineId \
  88. engineNum \
  89. failover \
  90. ident \
  91. key \
  92. local \
  93. PSKidentity \
  94. PSKsecrets \
  95. sslVersion \
  96. TIMEOUTbusy \
  97. TIMEOUTclose \
  98. TIMEOUTconnect \
  99. TIMEOUTidle \
  100. bool_delay \
  101. bool_libwrap \
  102. bool_reset \
  103. bool_requireCert \
  104. bool_verifyChain \
  105. bool_verifyPeer \
  106. bool_client
  107. print_list "$cfg" checkEmail \
  108. checkHost \
  109. checkIP \
  110. connect \
  111. options
  112. print_list_colon "$cfg" ciphers
  113. }
  114. process_config() {
  115. local alt_config_file
  116. rm -f "$CONF_FILE"
  117. # First line
  118. printf "; STunnel configuration file generated by uci\n" > "$CONF_FILE"
  119. printf "; Written %s\n\n" "$(date +'%c')" >> "$CONF_FILE"
  120. [ -f /etc/config/stunnel ] || return 0
  121. config_load stunnel
  122. global_defs
  123. # If "alt_config_file" specified, use that instead
  124. [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
  125. rm -f "$CONF_FILE"
  126. # Symlink "alt_config_file" since it's a bit easier and safer
  127. ln -s "$alt_config_file" "$CONF_FILE"
  128. return 0
  129. }
  130. config_foreach service_section service
  131. }
  132. reload_service() {
  133. process_config
  134. # SIGHUP is used by stunnel to do init.d reload
  135. procd_send_signal stunnel
  136. }
  137. service_triggers() {
  138. procd_add_reload_trigger "stunnel"
  139. }
  140. start_service() {
  141. procd_open_instance
  142. procd_set_param command "$BIN"
  143. procd_append_param command "$CONF_FILE"
  144. process_config
  145. # set auto respawn behavior
  146. procd_set_param respawn
  147. procd_close_instance
  148. }