secureserver.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. if($ARGV[1] =~ /^([\w\/]+)$/) {
  112. $stunnel = $ARGV[1];
  113. }
  114. else {
  115. $stunnel = "\"". $ARGV[1] ."\"";
  116. }
  117. shift @ARGV;
  118. }
  119. }
  120. elsif($ARGV[0] eq '--srcdir') {
  121. if($ARGV[1]) {
  122. $srcdir = $ARGV[1];
  123. shift @ARGV;
  124. }
  125. }
  126. elsif($ARGV[0] eq '--certfile') {
  127. if($ARGV[1]) {
  128. $stuncert = $ARGV[1];
  129. shift @ARGV;
  130. }
  131. }
  132. elsif($ARGV[0] eq '--id') {
  133. if($ARGV[1]) {
  134. if($ARGV[1] =~ /^(\d+)$/) {
  135. $idnum = $1 if($1 > 0);
  136. shift @ARGV;
  137. }
  138. }
  139. }
  140. elsif($ARGV[0] eq '--ipv4') {
  141. $ipvnum = 4;
  142. }
  143. elsif($ARGV[0] eq '--ipv6') {
  144. $ipvnum = 6;
  145. }
  146. elsif($ARGV[0] eq '--pidfile') {
  147. if($ARGV[1]) {
  148. $pidfile = "$path/". $ARGV[1];
  149. shift @ARGV;
  150. }
  151. }
  152. elsif($ARGV[0] eq '--logfile') {
  153. if($ARGV[1]) {
  154. $logfile = "$path/". $ARGV[1];
  155. shift @ARGV;
  156. }
  157. }
  158. elsif($ARGV[0] eq '--logdir') {
  159. if($ARGV[1]) {
  160. $logdir = "$path/". $ARGV[1];
  161. shift @ARGV;
  162. }
  163. }
  164. else {
  165. print STDERR "\nWarning: secureserver.pl unknown parameter: $ARGV[0]\n";
  166. }
  167. shift @ARGV;
  168. }
  169. #***************************************************************************
  170. # Initialize command line option dependent variables
  171. #
  172. if($pidfile) {
  173. # Use our pidfile directory to store the conf files
  174. $piddir = dirname($pidfile);
  175. }
  176. else {
  177. # Use the current directory to store the conf files
  178. $piddir = $path;
  179. $pidfile = server_pidfilename($piddir, $proto, $ipvnum, $idnum);
  180. }
  181. if(!$logfile) {
  182. $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
  183. }
  184. $conffile = "$piddir/${proto}_stunnel.conf";
  185. $capath = abs_path($path);
  186. $certfile = "$srcdir/". ($stuncert?"certs/$stuncert":"stunnel.pem");
  187. $certfile = abs_path($certfile);
  188. my $ssltext = uc($proto) ." SSL/TLS:";
  189. #***************************************************************************
  190. # Find out version info for the given stunnel binary
  191. #
  192. foreach my $veropt (('-version', '-V')) {
  193. foreach my $verstr (qx($stunnel $veropt 2>&1)) {
  194. if($verstr =~ /^stunnel (\d+)\.(\d+) on /) {
  195. $ver_major = $1;
  196. $ver_minor = $2;
  197. }
  198. elsif($verstr =~ /^sslVersion.*fips *= *yes/) {
  199. # the fips option causes an error if stunnel doesn't support it
  200. $fips_support = 1;
  201. last
  202. }
  203. }
  204. last if($ver_major);
  205. }
  206. if((!$ver_major) || (!$ver_minor)) {
  207. if(-x "$stunnel" && ! -d "$stunnel") {
  208. print "$ssltext Unknown stunnel version\n";
  209. }
  210. else {
  211. print "$ssltext No stunnel\n";
  212. }
  213. exit 1;
  214. }
  215. $stunnel_version = (100*$ver_major) + $ver_minor;
  216. #***************************************************************************
  217. # Verify minimum stunnel required version
  218. #
  219. if($stunnel_version < 310) {
  220. print "$ssltext Unsupported stunnel version $ver_major.$ver_minor\n";
  221. exit 1;
  222. }
  223. #***************************************************************************
  224. # Find out if we are running on Windows using the tstunnel binary
  225. #
  226. if($stunnel =~ /tstunnel(\.exe)?"?$/) {
  227. $tstunnel_windows = 1;
  228. # convert Cygwin/MinGW paths to Win32 format
  229. $capath = pathhelp::sys_native_abs_path($capath);
  230. $certfile = pathhelp::sys_native_abs_path($certfile);
  231. }
  232. #***************************************************************************
  233. # Build command to execute for stunnel 3.X versions
  234. #
  235. if($stunnel_version < 400) {
  236. if($stunnel_version >= 319) {
  237. $socketopt = "-O a:SO_REUSEADDR=1";
  238. }
  239. $cmd = "$stunnel -p $certfile -P $pidfile ";
  240. $cmd .= "-d $accept_port -r $target_port -f -D $loglevel ";
  241. $cmd .= ($socketopt) ? "$socketopt " : "";
  242. $cmd .= ">$logfile 2>&1";
  243. if($verbose) {
  244. print uc($proto) ." server (stunnel $ver_major.$ver_minor)\n";
  245. print "cmd: $cmd\n";
  246. print "pem cert file: $certfile\n";
  247. print "pid file: $pidfile\n";
  248. print "log file: $logfile\n";
  249. print "log level: $loglevel\n";
  250. print "listen on port: $accept_port\n";
  251. print "connect to port: $target_port\n";
  252. }
  253. }
  254. #***************************************************************************
  255. # Build command to execute for stunnel 4.00 and newer
  256. #
  257. if($stunnel_version >= 400) {
  258. $socketopt = "a:SO_REUSEADDR=1";
  259. if(($stunnel_version >= 534) && $tstunnel_windows) {
  260. # SO_EXCLUSIVEADDRUSE is on by default on Vista or newer,
  261. # but does not work together with SO_REUSEADDR being on.
  262. $socketopt .= "\nsocket = a:SO_EXCLUSIVEADDRUSE=0";
  263. }
  264. $cmd = "$stunnel $conffile ";
  265. $cmd .= ">$logfile 2>&1";
  266. # setup signal handler
  267. $SIG{INT} = \&exit_signal_handler;
  268. $SIG{TERM} = \&exit_signal_handler;
  269. # stunnel configuration file
  270. if(open(my $stunconf, ">", "$conffile")) {
  271. print $stunconf "CApath = $capath\n";
  272. print $stunconf "cert = $certfile\n";
  273. print $stunconf "debug = $loglevel\n";
  274. print $stunconf "socket = $socketopt\n";
  275. if($fips_support) {
  276. # disable fips in case OpenSSL doesn't support it
  277. print $stunconf "fips = no\n";
  278. }
  279. if(!$tstunnel_windows) {
  280. # do not use Linux-specific options on Windows
  281. print $stunconf "output = $logfile\n";
  282. print $stunconf "pid = $pidfile\n";
  283. print $stunconf "foreground = yes\n";
  284. }
  285. print $stunconf "\n";
  286. print $stunconf "[curltest]\n";
  287. print $stunconf "accept = $accept_port\n";
  288. print $stunconf "connect = $target_port\n";
  289. if(!close($stunconf)) {
  290. print "$ssltext Error closing file $conffile\n";
  291. exit 1;
  292. }
  293. }
  294. else {
  295. print "$ssltext Error writing file $conffile\n";
  296. exit 1;
  297. }
  298. if($verbose) {
  299. print uc($proto) ." server (stunnel $ver_major.$ver_minor)\n";
  300. print "cmd: $cmd\n";
  301. print "CApath = $capath\n";
  302. print "cert = $certfile\n";
  303. print "debug = $loglevel\n";
  304. print "socket = $socketopt\n";
  305. if($fips_support) {
  306. print "fips = no\n";
  307. }
  308. if(!$tstunnel_windows) {
  309. print "pid = $pidfile\n";
  310. print "output = $logfile\n";
  311. print "foreground = yes\n";
  312. }
  313. print "\n";
  314. print "[curltest]\n";
  315. print "accept = $accept_port\n";
  316. print "connect = $target_port\n";
  317. }
  318. }
  319. #***************************************************************************
  320. # Set file permissions on certificate pem file.
  321. #
  322. chmod(0600, $certfile) if(-f $certfile);
  323. print STDERR "RUN: $cmd\n" if($verbose);
  324. #***************************************************************************
  325. # Run tstunnel on Windows.
  326. #
  327. if($tstunnel_windows) {
  328. # Fake pidfile for tstunnel on Windows.
  329. if(open(my $out, ">", "$pidfile")) {
  330. print $out $$ . "\n";
  331. close($out);
  332. }
  333. # Flush output.
  334. $| = 1;
  335. # Put an "exec" in front of the command so that the child process
  336. # keeps this child's process ID by being tied to the spawned shell.
  337. exec("exec $cmd") || die "Can't exec() $cmd: $!";
  338. # exec() will create a new process, but ties the existence of the
  339. # new process to the parent waiting perl.exe and sh.exe processes.
  340. # exec() should never return back here to this process. We protect
  341. # ourselves by calling die() just in case something goes really bad.
  342. die "error: exec() has returned";
  343. }
  344. #***************************************************************************
  345. # Run stunnel.
  346. #
  347. my $rc = system($cmd);
  348. $rc >>= 8;
  349. unlink($conffile) if($conffile && -f $conffile);
  350. exit $rc;