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.

176 lines
3.4 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
  72. printf "\n" >> "$CONF_FILE"
  73. printf "[%s]\n" "$cfg" >> "$CONF_FILE"
  74. config_get accept_host "$cfg" accept_host 'localhost'
  75. config_get accept_port "$cfg" accept_port
  76. printf "accept = %s:%s\n" "$accept_host" "$accept_port" >> "$CONF_FILE"
  77. print_options "$cfg" CApath \
  78. CAfile \
  79. cert \
  80. CRLpath \
  81. CRLfile \
  82. curve \
  83. logId \
  84. debug \
  85. engineId \
  86. engineNum \
  87. failover \
  88. ident \
  89. key \
  90. local \
  91. PSKidentity \
  92. PSKsecrets \
  93. sslVersion \
  94. TIMEOUTbusy \
  95. TIMEOUTclose \
  96. TIMEOUTconnect \
  97. TIMEOUTidle \
  98. bool_delay \
  99. bool_libwrap \
  100. bool_reset \
  101. bool_requireCert \
  102. bool_verifyChain \
  103. bool_verifyPeer \
  104. bool_client
  105. print_list "$cfg" checkEmail \
  106. checkHost \
  107. checkIP \
  108. connect \
  109. options
  110. print_list_colon "$cfg" ciphers
  111. }
  112. process_config() {
  113. local alt_config_file
  114. rm -f "$CONF_FILE"
  115. # First line
  116. printf "; STunnel configuration file generated by uci\n" > "$CONF_FILE"
  117. printf "; Written %s\n\n" "$(date +'%c')" >> "$CONF_FILE"
  118. [ -f /etc/config/stunnel ] || return 0
  119. config_load stunnel
  120. global_defs
  121. # If "alt_config_file" specified, use that instead
  122. [ -n "$alt_config_file" ] && [ -f "$alt_config_file" ] && {
  123. rm -f "$CONF_FILE"
  124. # Symlink "alt_config_file" since it's a bit easier and safer
  125. ln -s "$alt_config_file" "$CONF_FILE"
  126. return 0
  127. }
  128. config_foreach service_section service
  129. }
  130. reload_service() {
  131. process_config
  132. # SIGHUP is used by stunnel to do init.d reload
  133. procd_send_signal stunnel
  134. }
  135. service_triggers() {
  136. procd_add_reload_trigger "stunnel"
  137. }
  138. start_service() {
  139. procd_open_instance
  140. procd_set_param command "$BIN"
  141. procd_append_param command "$CONF_FILE"
  142. process_config
  143. # set auto respawn behavior
  144. procd_set_param respawn
  145. procd_close_instance
  146. }