secureserver.pl 9.2 KB

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