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.

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