secureserver.pl 11 KB

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