secureserver.pl 9.6 KB

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