secureserver.pl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at http://curl.haxx.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. #***************************************************************************
  23. # This is the HTTPS, FTPS, POP3S, IMAPS, SMTPS, server used for curl test
  24. # harness. Actually just a layer that runs stunnel properly using the
  25. # non-secure test harness servers.
  26. BEGIN {
  27. push(@INC, $ENV{'srcdir'}) if(defined $ENV{'srcdir'});
  28. push(@INC, ".");
  29. }
  30. use strict;
  31. use warnings;
  32. use Cwd;
  33. use serverhelp qw(
  34. server_pidfilename
  35. server_logfilename
  36. );
  37. my $stunnel = "stunnel";
  38. my $verbose=0; # set to 1 for debugging
  39. my $accept_port = 8991; # just our default, weird enough
  40. my $target_port = 8999; # default test http-server port
  41. my $stuncert;
  42. my $ver_major;
  43. my $ver_minor;
  44. my $fips_support;
  45. my $stunnel_version;
  46. my $socketopt;
  47. my $cmd;
  48. my $pidfile; # stunnel pid file
  49. my $logfile; # stunnel log file
  50. my $loglevel = 5; # stunnel log level
  51. my $ipvnum = 4; # default IP version of stunneled server
  52. my $idnum = 1; # dafault stunneled server instance number
  53. my $proto = 'https'; # default secure server protocol
  54. my $conffile; # stunnel configuration file
  55. my $certfile; # certificate chain PEM file
  56. #***************************************************************************
  57. # stunnel requires full path specification for several files.
  58. #
  59. my $path = getcwd();
  60. my $srcdir = $path;
  61. my $logdir = $path .'/log';
  62. #***************************************************************************
  63. # Signal handler to remove our stunnel 4.00 and newer configuration file.
  64. #
  65. sub exit_signal_handler {
  66. my $signame = shift;
  67. local $!; # preserve errno
  68. local $?; # preserve exit status
  69. unlink($conffile) if($conffile && (-f $conffile));
  70. exit;
  71. }
  72. #***************************************************************************
  73. # Process command line options
  74. #
  75. while(@ARGV) {
  76. if($ARGV[0] eq '--verbose') {
  77. $verbose = 1;
  78. }
  79. elsif($ARGV[0] eq '--proto') {
  80. if($ARGV[1]) {
  81. $proto = $ARGV[1];
  82. shift @ARGV;
  83. }
  84. }
  85. elsif($ARGV[0] eq '--accept') {
  86. if($ARGV[1]) {
  87. if($ARGV[1] =~ /^(\d+)$/) {
  88. $accept_port = $1;
  89. shift @ARGV;
  90. }
  91. }
  92. }
  93. elsif($ARGV[0] eq '--connect') {
  94. if($ARGV[1]) {
  95. if($ARGV[1] =~ /^(\d+)$/) {
  96. $target_port = $1;
  97. shift @ARGV;
  98. }
  99. }
  100. }
  101. elsif($ARGV[0] eq '--stunnel') {
  102. if($ARGV[1]) {
  103. if($ARGV[1] =~ /^([\w\/]+)$/) {
  104. $stunnel = $ARGV[1];
  105. }
  106. else {
  107. $stunnel = "\"". $ARGV[1] ."\"";
  108. }
  109. shift @ARGV;
  110. }
  111. }
  112. elsif($ARGV[0] eq '--srcdir') {
  113. if($ARGV[1]) {
  114. $srcdir = $ARGV[1];
  115. shift @ARGV;
  116. }
  117. }
  118. elsif($ARGV[0] eq '--certfile') {
  119. if($ARGV[1]) {
  120. $stuncert = $ARGV[1];
  121. shift @ARGV;
  122. }
  123. }
  124. elsif($ARGV[0] eq '--id') {
  125. if($ARGV[1]) {
  126. if($ARGV[1] =~ /^(\d+)$/) {
  127. $idnum = $1 if($1 > 0);
  128. shift @ARGV;
  129. }
  130. }
  131. }
  132. elsif($ARGV[0] eq '--ipv4') {
  133. $ipvnum = 4;
  134. }
  135. elsif($ARGV[0] eq '--ipv6') {
  136. $ipvnum = 6;
  137. }
  138. elsif($ARGV[0] eq '--pidfile') {
  139. if($ARGV[1]) {
  140. $pidfile = "$path/". $ARGV[1];
  141. shift @ARGV;
  142. }
  143. }
  144. elsif($ARGV[0] eq '--logfile') {
  145. if($ARGV[1]) {
  146. $logfile = "$path/". $ARGV[1];
  147. shift @ARGV;
  148. }
  149. }
  150. else {
  151. print STDERR "\nWarning: secureserver.pl unknown parameter: $ARGV[0]\n";
  152. }
  153. shift @ARGV;
  154. }
  155. #***************************************************************************
  156. # Initialize command line option dependant variables
  157. #
  158. if(!$pidfile) {
  159. $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
  160. }
  161. if(!$logfile) {
  162. $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
  163. }
  164. $conffile = "$path/stunnel.conf";
  165. $certfile = "$srcdir/". ($stuncert?"certs/$stuncert":"stunnel.pem");
  166. my $ssltext = uc($proto) ." SSL/TLS:";
  167. #***************************************************************************
  168. # Find out version info for the given stunnel binary
  169. #
  170. foreach my $veropt (('-version', '-V')) {
  171. foreach my $verstr (qx($stunnel $veropt 2>&1)) {
  172. if($verstr =~ /^stunnel (\d+)\.(\d+) on /) {
  173. $ver_major = $1;
  174. $ver_minor = $2;
  175. }
  176. elsif($verstr =~ /^sslVersion.*fips *= *yes/) {
  177. # the fips option causes an error if stunnel doesn't support it
  178. $fips_support = 1;
  179. last
  180. }
  181. }
  182. last if($ver_major);
  183. }
  184. if((!$ver_major) || (!$ver_minor)) {
  185. if(-x "$stunnel" && ! -d "$stunnel") {
  186. print "$ssltext Unknown stunnel version\n";
  187. }
  188. else {
  189. print "$ssltext No stunnel\n";
  190. }
  191. exit 1;
  192. }
  193. $stunnel_version = (100*$ver_major) + $ver_minor;
  194. #***************************************************************************
  195. # Verify minimum stunnel required version
  196. #
  197. if($stunnel_version < 310) {
  198. print "$ssltext Unsupported stunnel version $ver_major.$ver_minor\n";
  199. exit 1;
  200. }
  201. #***************************************************************************
  202. # Build command to execute for stunnel 3.X versions
  203. #
  204. if($stunnel_version < 400) {
  205. if($stunnel_version >= 319) {
  206. $socketopt = "-O a:SO_REUSEADDR=1";
  207. }
  208. $cmd = "$stunnel -p $certfile -P $pidfile ";
  209. $cmd .= "-d $accept_port -r $target_port -f -D $loglevel ";
  210. $cmd .= ($socketopt) ? "$socketopt " : "";
  211. $cmd .= ">$logfile 2>&1";
  212. if($verbose) {
  213. print uc($proto) ." server (stunnel $ver_major.$ver_minor)\n";
  214. print "cmd: $cmd\n";
  215. print "pem cert file: $certfile\n";
  216. print "pid file: $pidfile\n";
  217. print "log file: $logfile\n";
  218. print "log level: $loglevel\n";
  219. print "listen on port: $accept_port\n";
  220. print "connect to port: $target_port\n";
  221. }
  222. }
  223. #***************************************************************************
  224. # Build command to execute for stunnel 4.00 and newer
  225. #
  226. if($stunnel_version >= 400) {
  227. $socketopt = "a:SO_REUSEADDR=1";
  228. $cmd = "$stunnel $conffile ";
  229. $cmd .= ">$logfile 2>&1";
  230. # setup signal handler
  231. $SIG{INT} = \&exit_signal_handler;
  232. $SIG{TERM} = \&exit_signal_handler;
  233. # stunnel configuration file
  234. if(open(STUNCONF, ">$conffile")) {
  235. print STUNCONF "
  236. CApath = $path
  237. cert = $certfile
  238. debug = $loglevel
  239. socket = $socketopt";
  240. if($fips_support) {
  241. # disable fips in case OpenSSL doesn't support it
  242. print STUNCONF "
  243. fips = no";
  244. }
  245. if($stunnel !~ /tstunnel(\.exe)?"?$/) {
  246. print STUNCONF "
  247. output = $logfile
  248. pid = $pidfile
  249. foreground = yes";
  250. }
  251. print STUNCONF "
  252. [curltest]
  253. accept = $accept_port
  254. connect = $target_port";
  255. if(!close(STUNCONF)) {
  256. print "$ssltext Error closing file $conffile\n";
  257. exit 1;
  258. }
  259. }
  260. else {
  261. print "$ssltext Error writing file $conffile\n";
  262. exit 1;
  263. }
  264. if($verbose) {
  265. print uc($proto) ." server (stunnel $ver_major.$ver_minor)\n";
  266. print "cmd: $cmd\n";
  267. print "CApath = $path\n";
  268. print "cert = $certfile\n";
  269. print "pid = $pidfile\n";
  270. print "debug = $loglevel\n";
  271. print "socket = $socketopt\n";
  272. print "output = $logfile\n";
  273. print "foreground = yes\n";
  274. print "\n";
  275. print "[curltest]\n";
  276. print "accept = $accept_port\n";
  277. print "connect = $target_port\n";
  278. }
  279. }
  280. #***************************************************************************
  281. # Set file permissions on certificate pem file.
  282. #
  283. chmod(0600, $certfile) if(-f $certfile);
  284. #***************************************************************************
  285. # Run tstunnel on Windows.
  286. #
  287. if($stunnel =~ /tstunnel(\.exe)?"?$/) {
  288. # Fake pidfile for tstunnel on Windows.
  289. if(open(OUT, ">$pidfile")) {
  290. print OUT $$ . "\n";
  291. close(OUT);
  292. }
  293. # Put an "exec" in front of the command so that the child process
  294. # keeps this child's process ID.
  295. exec("exec $cmd") || die "Can't exec() $cmd: $!";
  296. # exec() should never return back here to this process. We protect
  297. # ourselves by calling die() just in case something goes really bad.
  298. die "error: exec() has returned";
  299. }
  300. #***************************************************************************
  301. # Run stunnel.
  302. #
  303. my $rc = system($cmd);
  304. $rc >>= 8;
  305. unlink($conffile) if($conffile && -f $conffile);
  306. exit $rc;