secureserver.pl 11 KB

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