sshserver.pl 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. # Starts sshd for use in the SCP and SFTP curl test harness tests.
  26. # Also creates the ssh configuration files needed for these tests.
  27. use strict;
  28. use warnings;
  29. use Cwd;
  30. use Cwd 'abs_path';
  31. use Digest::MD5;
  32. use Digest::MD5 'md5_hex';
  33. use Digest::SHA;
  34. use Digest::SHA 'sha256_base64';
  35. use MIME::Base64;
  36. #***************************************************************************
  37. # Variables and subs imported from sshhelp module
  38. #
  39. use sshhelp qw(
  40. $sshdexe
  41. $sshexe
  42. $sftpsrvexe
  43. $sftpexe
  44. $sshkeygenexe
  45. $sshdconfig
  46. $sshconfig
  47. $sftpconfig
  48. $knownhosts
  49. $sshdlog
  50. $sshlog
  51. $sftplog
  52. $sftpcmds
  53. $hstprvkeyf
  54. $hstpubkeyf
  55. $hstpubmd5f
  56. $hstpubsha256f
  57. $cliprvkeyf
  58. $clipubkeyf
  59. display_sshdconfig
  60. display_sshconfig
  61. display_sftpconfig
  62. display_sshdlog
  63. display_sshlog
  64. display_sftplog
  65. dump_array
  66. find_sshd
  67. find_ssh
  68. find_sftpsrv
  69. find_sftp
  70. find_sshkeygen
  71. logmsg
  72. sshversioninfo
  73. );
  74. #***************************************************************************
  75. # Subs imported from serverhelp module
  76. #
  77. use serverhelp qw(
  78. server_pidfilename
  79. server_logfilename
  80. );
  81. use pathhelp;
  82. #***************************************************************************
  83. my $verbose = 0; # set to 1 for debugging
  84. my $debugprotocol = 0; # set to 1 for protocol debugging
  85. my $port = 8999; # our default SCP/SFTP server port
  86. my $listenaddr = '127.0.0.1'; # default address on which to listen
  87. my $ipvnum = 4; # default IP version of listener address
  88. my $idnum = 1; # default ssh daemon instance number
  89. my $proto = 'ssh'; # protocol the ssh daemon speaks
  90. my $path = getcwd(); # current working directory
  91. my $logdir = $path .'/log'; # directory for log files
  92. my $username = $ENV{USER}; # default user
  93. my $pidfile; # ssh daemon pid file
  94. my $identity = 'curl_client_key'; # default identity file
  95. my $error;
  96. my @cfgarr;
  97. #***************************************************************************
  98. # Parse command line options
  99. #
  100. while(@ARGV) {
  101. if($ARGV[0] eq '--verbose') {
  102. $verbose = 1;
  103. }
  104. elsif($ARGV[0] eq '--debugprotocol') {
  105. $verbose = 1;
  106. $debugprotocol = 1;
  107. }
  108. elsif($ARGV[0] eq '--user') {
  109. if($ARGV[1]) {
  110. $username = $ARGV[1];
  111. shift @ARGV;
  112. }
  113. }
  114. elsif($ARGV[0] eq '--id') {
  115. if($ARGV[1]) {
  116. if($ARGV[1] =~ /^(\d+)$/) {
  117. $idnum = $1 if($1 > 0);
  118. shift @ARGV;
  119. }
  120. }
  121. }
  122. elsif($ARGV[0] eq '--ipv4') {
  123. $ipvnum = 4;
  124. $listenaddr = '127.0.0.1' if($listenaddr eq '::1');
  125. }
  126. elsif($ARGV[0] eq '--ipv6') {
  127. $ipvnum = 6;
  128. $listenaddr = '::1' if($listenaddr eq '127.0.0.1');
  129. }
  130. elsif($ARGV[0] eq '--addr') {
  131. if($ARGV[1]) {
  132. my $tmpstr = $ARGV[1];
  133. if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
  134. $listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
  135. shift @ARGV;
  136. }
  137. elsif($ipvnum == 6) {
  138. $listenaddr = $tmpstr;
  139. $listenaddr =~ s/^\[(.*)\]$/$1/;
  140. shift @ARGV;
  141. }
  142. }
  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 '--sshport') {
  151. if($ARGV[1]) {
  152. if($ARGV[1] =~ /^(\d+)$/) {
  153. $port = $1;
  154. shift @ARGV;
  155. }
  156. }
  157. }
  158. else {
  159. print STDERR "\nWarning: sshserver.pl unknown parameter: $ARGV[0]\n";
  160. }
  161. shift @ARGV;
  162. }
  163. #***************************************************************************
  164. # Default ssh daemon pid file name
  165. #
  166. if(!$pidfile) {
  167. $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
  168. }
  169. #***************************************************************************
  170. # ssh and sftp server log file names
  171. #
  172. $sshdlog = server_logfilename($logdir, 'ssh', $ipvnum, $idnum);
  173. $sftplog = server_logfilename($logdir, 'sftp', $ipvnum, $idnum);
  174. #***************************************************************************
  175. # Logging level for ssh server and client
  176. #
  177. my $loglevel = $debugprotocol?'DEBUG3':'DEBUG2';
  178. #***************************************************************************
  179. # Validate username
  180. #
  181. if(!$username) {
  182. $error = 'Will not run ssh server without a user name';
  183. }
  184. elsif($username eq 'root') {
  185. $error = 'Will not run ssh server as root to mitigate security risks';
  186. }
  187. if($error) {
  188. logmsg $error;
  189. exit 1;
  190. }
  191. #***************************************************************************
  192. # Find out ssh daemon canonical file name
  193. #
  194. my $sshd = find_sshd();
  195. if(!$sshd) {
  196. logmsg "cannot find $sshdexe";
  197. exit 1;
  198. }
  199. #***************************************************************************
  200. # Find out ssh daemon version info
  201. #
  202. my ($sshdid, $sshdvernum, $sshdverstr, $sshderror) = sshversioninfo($sshd);
  203. if(!$sshdid) {
  204. # Not an OpenSSH or SunSSH ssh daemon
  205. logmsg $sshderror if($verbose);
  206. logmsg 'SCP and SFTP tests require OpenSSH 2.9.9 or later';
  207. exit 1;
  208. }
  209. logmsg "ssh server found $sshd is $sshdverstr" if($verbose);
  210. #***************************************************************************
  211. # ssh daemon command line options we might use and version support
  212. #
  213. # -e: log stderr : OpenSSH 2.9.0 and later
  214. # -f: sshd config file : OpenSSH 1.2.1 and later
  215. # -D: no daemon forking : OpenSSH 2.5.0 and later
  216. # -o: command-line option : OpenSSH 3.1.0 and later
  217. # -t: test config file : OpenSSH 2.9.9 and later
  218. # -?: sshd version info : OpenSSH 1.2.1 and later
  219. #
  220. # -e: log stderr : SunSSH 1.0.0 and later
  221. # -f: sshd config file : SunSSH 1.0.0 and later
  222. # -D: no daemon forking : SunSSH 1.0.0 and later
  223. # -o: command-line option : SunSSH 1.0.0 and later
  224. # -t: test config file : SunSSH 1.0.0 and later
  225. # -?: sshd version info : SunSSH 1.0.0 and later
  226. #***************************************************************************
  227. # Verify minimum ssh daemon version
  228. #
  229. if((($sshdid =~ /OpenSSH/) && ($sshdvernum < 299)) ||
  230. (($sshdid =~ /SunSSH/) && ($sshdvernum < 100))) {
  231. logmsg 'SCP and SFTP tests require OpenSSH 2.9.9 or later';
  232. exit 1;
  233. }
  234. #***************************************************************************
  235. # Find out sftp server plugin canonical file name
  236. #
  237. my $sftpsrv = find_sftpsrv();
  238. if(!$sftpsrv) {
  239. logmsg "cannot find $sftpsrvexe";
  240. exit 1;
  241. }
  242. logmsg "sftp server plugin found $sftpsrv" if($verbose);
  243. #***************************************************************************
  244. # Find out sftp client canonical file name
  245. #
  246. my $sftp = find_sftp();
  247. if(!$sftp) {
  248. logmsg "cannot find $sftpexe";
  249. exit 1;
  250. }
  251. logmsg "sftp client found $sftp" if($verbose);
  252. #***************************************************************************
  253. # Find out ssh keygen canonical file name
  254. #
  255. my $sshkeygen = find_sshkeygen();
  256. if(!$sshkeygen) {
  257. logmsg "cannot find $sshkeygenexe";
  258. exit 1;
  259. }
  260. logmsg "ssh keygen found $sshkeygen" if($verbose);
  261. #***************************************************************************
  262. # Find out ssh client canonical file name
  263. #
  264. my $ssh = find_ssh();
  265. if(!$ssh) {
  266. logmsg "cannot find $sshexe";
  267. exit 1;
  268. }
  269. #***************************************************************************
  270. # Find out ssh client version info
  271. #
  272. my ($sshid, $sshvernum, $sshverstr, $ssherror) = sshversioninfo($ssh);
  273. if(!$sshid) {
  274. # Not an OpenSSH or SunSSH ssh client
  275. logmsg $ssherror if($verbose);
  276. logmsg 'SCP and SFTP tests require OpenSSH 2.9.9 or later';
  277. exit 1;
  278. }
  279. logmsg "ssh client found $ssh is $sshverstr" if($verbose);
  280. #***************************************************************************
  281. # ssh client command line options we might use and version support
  282. #
  283. # -D: dynamic app port forwarding : OpenSSH 2.9.9 and later
  284. # -F: ssh config file : OpenSSH 2.9.9 and later
  285. # -N: no shell/command : OpenSSH 2.1.0 and later
  286. # -p: connection port : OpenSSH 1.2.1 and later
  287. # -v: verbose messages : OpenSSH 1.2.1 and later
  288. # -vv: increase verbosity : OpenSSH 2.3.0 and later
  289. # -V: ssh version info : OpenSSH 1.2.1 and later
  290. #
  291. # -D: dynamic app port forwarding : SunSSH 1.0.0 and later
  292. # -F: ssh config file : SunSSH 1.0.0 and later
  293. # -N: no shell/command : SunSSH 1.0.0 and later
  294. # -p: connection port : SunSSH 1.0.0 and later
  295. # -v: verbose messages : SunSSH 1.0.0 and later
  296. # -vv: increase verbosity : SunSSH 1.0.0 and later
  297. # -V: ssh version info : SunSSH 1.0.0 and later
  298. #***************************************************************************
  299. # Verify minimum ssh client version
  300. #
  301. if((($sshid =~ /OpenSSH/) && ($sshvernum < 299)) ||
  302. (($sshid =~ /SunSSH/) && ($sshvernum < 100))) {
  303. logmsg 'SCP and SFTP tests require OpenSSH 2.9.9 or later';
  304. exit 1;
  305. }
  306. #***************************************************************************
  307. # ssh keygen command line options we actually use and version support
  308. #
  309. # -C: identity comment : OpenSSH 1.2.1 and later
  310. # -f: key filename : OpenSSH 1.2.1 and later
  311. # -N: new passphrase : OpenSSH 1.2.1 and later
  312. # -q: quiet keygen : OpenSSH 1.2.1 and later
  313. # -t: key type : OpenSSH 2.5.0 and later
  314. #
  315. # -C: identity comment : SunSSH 1.0.0 and later
  316. # -f: key filename : SunSSH 1.0.0 and later
  317. # -N: new passphrase : SunSSH 1.0.0 and later
  318. # -q: quiet keygen : SunSSH 1.0.0 and later
  319. # -t: key type : SunSSH 1.0.0 and later
  320. #***************************************************************************
  321. # Generate host and client key files for curl's tests
  322. #
  323. if((! -e $hstprvkeyf) || (! -s $hstprvkeyf) ||
  324. (! -e $hstpubkeyf) || (! -s $hstpubkeyf) ||
  325. (! -e $hstpubmd5f) || (! -s $hstpubmd5f) ||
  326. (! -e $hstpubsha256f) || (! -s $hstpubsha256f) ||
  327. (! -e $cliprvkeyf) || (! -s $cliprvkeyf) ||
  328. (! -e $clipubkeyf) || (! -s $clipubkeyf)) {
  329. # Make sure all files are gone so ssh-keygen doesn't complain
  330. unlink($hstprvkeyf, $hstpubkeyf, $hstpubmd5f, $hstpubsha256f,
  331. $cliprvkeyf, $clipubkeyf);
  332. logmsg 'generating host keys...' if($verbose);
  333. if(system "\"$sshkeygen\" -q -t rsa -f $hstprvkeyf -C 'curl test server' -N ''") {
  334. logmsg 'Could not generate host key';
  335. exit 1;
  336. }
  337. logmsg 'generating client keys...' if($verbose);
  338. if(system "\"$sshkeygen\" -q -t rsa -f $cliprvkeyf -C 'curl test client' -N ''") {
  339. logmsg 'Could not generate client key';
  340. exit 1;
  341. }
  342. # Make sure that permissions are restricted so openssh doesn't complain
  343. system "chmod 600 $hstprvkeyf";
  344. system "chmod 600 $cliprvkeyf";
  345. # Save md5 and sha256 hashes of public host key
  346. open(RSAKEYFILE, "<$hstpubkeyf");
  347. my @rsahostkey = do { local $/ = ' '; <RSAKEYFILE> };
  348. close(RSAKEYFILE);
  349. if(!$rsahostkey[1]) {
  350. logmsg 'Failed parsing base64 encoded RSA host key';
  351. exit 1;
  352. }
  353. open(PUBMD5FILE, ">$hstpubmd5f");
  354. print PUBMD5FILE md5_hex(decode_base64($rsahostkey[1]));
  355. close(PUBMD5FILE);
  356. if((! -e $hstpubmd5f) || (! -s $hstpubmd5f)) {
  357. logmsg 'Failed writing md5 hash of RSA host key';
  358. exit 1;
  359. }
  360. open(PUBSHA256FILE, ">$hstpubsha256f");
  361. print PUBSHA256FILE sha256_base64(decode_base64($rsahostkey[1]));
  362. close(PUBSHA256FILE);
  363. if((! -e $hstpubsha256f) || (! -s $hstpubsha256f)) {
  364. logmsg 'Failed writing sha256 hash of RSA host key';
  365. exit 1;
  366. }
  367. }
  368. #***************************************************************************
  369. # Convert paths for curl's tests running on Windows with Cygwin/Msys OpenSSH
  370. #
  371. my $clipubkeyf_config = abs_path("$path/$clipubkeyf");
  372. my $hstprvkeyf_config = abs_path("$path/$hstprvkeyf");
  373. my $pidfile_config = $pidfile;
  374. my $sftpsrv_config = $sftpsrv;
  375. if (pathhelp::os_is_win()) {
  376. # Ensure to use MinGW/Cygwin paths
  377. $clipubkeyf_config = pathhelp::build_sys_abs_path($clipubkeyf_config);
  378. $hstprvkeyf_config = pathhelp::build_sys_abs_path($hstprvkeyf_config);
  379. $pidfile_config = pathhelp::build_sys_abs_path($pidfile_config);
  380. $sftpsrv_config = "internal-sftp";
  381. }
  382. if ($sshdid =~ /OpenSSH-Windows/) {
  383. # Ensure to use native Windows paths with OpenSSH for Windows
  384. $clipubkeyf_config = pathhelp::sys_native_abs_path($clipubkeyf);
  385. $hstprvkeyf_config = pathhelp::sys_native_abs_path($hstprvkeyf);
  386. $pidfile_config = pathhelp::sys_native_abs_path($pidfile);
  387. $sftpsrv_config = pathhelp::sys_native_abs_path($sftpsrv);
  388. $sshdconfig = pathhelp::sys_native_abs_path($sshdconfig);
  389. $sshconfig = pathhelp::sys_native_abs_path($sshconfig);
  390. $sftpconfig = pathhelp::sys_native_abs_path($sftpconfig);
  391. }
  392. #***************************************************************************
  393. # ssh daemon configuration file options we might use and version support
  394. #
  395. # AFSTokenPassing : OpenSSH 1.2.1 and later [1]
  396. # AddressFamily : OpenSSH 4.0.0 and later
  397. # AllowTcpForwarding : OpenSSH 2.3.0 and later
  398. # AllowUsers : OpenSSH 1.2.1 and later
  399. # AuthorizedKeysFile : OpenSSH 2.9.9 and later
  400. # AuthorizedKeysFile2 : OpenSSH 2.9.9 and later
  401. # Banner : OpenSSH 2.5.0 and later
  402. # ChallengeResponseAuthentication : OpenSSH 2.5.0 and later
  403. # Ciphers : OpenSSH 2.1.0 and later [3]
  404. # ClientAliveCountMax : OpenSSH 2.9.0 and later
  405. # ClientAliveInterval : OpenSSH 2.9.0 and later
  406. # Compression : OpenSSH 3.3.0 and later
  407. # DenyUsers : OpenSSH 1.2.1 and later
  408. # ForceCommand : OpenSSH 4.4.0 and later [3]
  409. # GatewayPorts : OpenSSH 2.1.0 and later
  410. # GSSAPIAuthentication : OpenSSH 3.7.0 and later [1]
  411. # GSSAPICleanupCredentials : OpenSSH 3.8.0 and later [1]
  412. # GSSAPIKeyExchange : SunSSH 1.0.0 and later [1]
  413. # GSSAPIStoreDelegatedCredentials : SunSSH 1.0.0 and later [1]
  414. # GSSCleanupCreds : SunSSH 1.0.0 and later [1]
  415. # GSSUseSessionCredCache : SunSSH 1.0.0 and later [1]
  416. # HostbasedAuthentication : OpenSSH 2.9.0 and later
  417. # HostbasedUsesNameFromPacketOnly : OpenSSH 2.9.0 and later
  418. # HostKey : OpenSSH 1.2.1 and later
  419. # IgnoreRhosts : OpenSSH 1.2.1 and later
  420. # IgnoreUserKnownHosts : OpenSSH 1.2.1 and later
  421. # KbdInteractiveAuthentication : OpenSSH 2.3.0 and later
  422. # KeepAlive : OpenSSH 1.2.1 and later
  423. # KerberosAuthentication : OpenSSH 1.2.1 and later [1]
  424. # KerberosGetAFSToken : OpenSSH 3.8.0 and later [1]
  425. # KerberosOrLocalPasswd : OpenSSH 1.2.1 and later [1]
  426. # KerberosTgtPassing : OpenSSH 1.2.1 and later [1]
  427. # KerberosTicketCleanup : OpenSSH 1.2.1 and later [1]
  428. # KeyRegenerationInterval : OpenSSH 1.2.1 and later
  429. # ListenAddress : OpenSSH 1.2.1 and later
  430. # LoginGraceTime : OpenSSH 1.2.1 and later
  431. # LogLevel : OpenSSH 1.2.1 and later
  432. # LookupClientHostnames : SunSSH 1.0.0 and later
  433. # MACs : OpenSSH 2.5.0 and later [3]
  434. # Match : OpenSSH 4.4.0 and later [3]
  435. # MaxAuthTries : OpenSSH 3.9.0 and later
  436. # MaxStartups : OpenSSH 2.2.0 and later
  437. # PAMAuthenticationViaKbdInt : OpenSSH 2.9.0 and later [2]
  438. # PasswordAuthentication : OpenSSH 1.2.1 and later
  439. # PermitEmptyPasswords : OpenSSH 1.2.1 and later
  440. # PermitOpen : OpenSSH 4.4.0 and later [3]
  441. # PermitRootLogin : OpenSSH 1.2.1 and later
  442. # PermitTunnel : OpenSSH 4.3.0 and later
  443. # PermitUserEnvironment : OpenSSH 3.5.0 and later
  444. # PidFile : OpenSSH 2.1.0 and later
  445. # Port : OpenSSH 1.2.1 and later
  446. # PrintLastLog : OpenSSH 2.9.0 and later
  447. # PrintMotd : OpenSSH 1.2.1 and later
  448. # Protocol : OpenSSH 2.1.0 and later
  449. # PubkeyAuthentication : OpenSSH 2.5.0 and later
  450. # RhostsAuthentication : OpenSSH 1.2.1 and later
  451. # RhostsRSAAuthentication : OpenSSH 1.2.1 and later
  452. # RSAAuthentication : OpenSSH 1.2.1 and later
  453. # ServerKeyBits : OpenSSH 1.2.1 and later
  454. # SkeyAuthentication : OpenSSH 1.2.1 and later [1]
  455. # StrictModes : OpenSSH 1.2.1 and later
  456. # Subsystem : OpenSSH 2.2.0 and later
  457. # SyslogFacility : OpenSSH 1.2.1 and later
  458. # TCPKeepAlive : OpenSSH 3.8.0 and later
  459. # UseDNS : OpenSSH 3.7.0 and later
  460. # UseLogin : OpenSSH 1.2.1 and later
  461. # UsePAM : OpenSSH 3.7.0 and later [1][2]
  462. # UsePrivilegeSeparation : OpenSSH 3.2.2 and later
  463. # VerifyReverseMapping : OpenSSH 3.1.0 and later
  464. # X11DisplayOffset : OpenSSH 1.2.1 and later [3]
  465. # X11Forwarding : OpenSSH 1.2.1 and later
  466. # X11UseLocalhost : OpenSSH 3.1.0 and later
  467. # XAuthLocation : OpenSSH 2.1.1 and later [3]
  468. #
  469. # [1] Option only available if activated at compile time
  470. # [2] Option specific for portable versions
  471. # [3] Option not used in our ssh server config file
  472. #***************************************************************************
  473. # Initialize sshd config with options actually supported in OpenSSH 2.9.9
  474. #
  475. logmsg 'generating ssh server config file...' if($verbose);
  476. @cfgarr = ();
  477. push @cfgarr, '# This is a generated file. Do not edit.';
  478. push @cfgarr, "# $sshdverstr sshd configuration file for curl testing";
  479. push @cfgarr, '#';
  480. # AllowUsers and DenyUsers options should use lowercase on Windows
  481. # and do not support quotes around values for some unknown reason.
  482. if ($sshdid =~ /OpenSSH-Windows/) {
  483. my $username_lc = lc $username;
  484. if (exists $ENV{USERDOMAIN}) {
  485. my $userdomain_lc = lc $ENV{USERDOMAIN};
  486. $username_lc = "$userdomain_lc\\$username_lc";
  487. }
  488. $username_lc =~ s/ /\?/g; # replace space with ?
  489. push @cfgarr, "DenyUsers !$username_lc";
  490. push @cfgarr, "AllowUsers $username_lc";
  491. } else {
  492. push @cfgarr, "DenyUsers !$username";
  493. push @cfgarr, "AllowUsers $username";
  494. }
  495. push @cfgarr, "AuthorizedKeysFile $clipubkeyf_config";
  496. push @cfgarr, "AuthorizedKeysFile2 $clipubkeyf_config";
  497. push @cfgarr, "HostKey $hstprvkeyf_config";
  498. if ($sshdid !~ /OpenSSH-Windows/) {
  499. push @cfgarr, "PidFile $pidfile_config";
  500. }
  501. if(($sshdid =~ /OpenSSH/) && ($sshdvernum >= 880)) {
  502. push @cfgarr, 'HostKeyAlgorithms +ssh-rsa';
  503. push @cfgarr, 'PubkeyAcceptedKeyTypes +ssh-rsa';
  504. }
  505. push @cfgarr, '#';
  506. push @cfgarr, "Port $port";
  507. push @cfgarr, "ListenAddress $listenaddr";
  508. push @cfgarr, 'Protocol 2';
  509. push @cfgarr, '#';
  510. push @cfgarr, 'AllowTcpForwarding yes';
  511. push @cfgarr, 'Banner none';
  512. push @cfgarr, 'ChallengeResponseAuthentication no';
  513. push @cfgarr, 'ClientAliveCountMax 3';
  514. push @cfgarr, 'ClientAliveInterval 0';
  515. push @cfgarr, 'GatewayPorts no';
  516. push @cfgarr, 'HostbasedAuthentication no';
  517. push @cfgarr, 'HostbasedUsesNameFromPacketOnly no';
  518. push @cfgarr, 'IgnoreRhosts yes';
  519. push @cfgarr, 'IgnoreUserKnownHosts yes';
  520. push @cfgarr, 'KeyRegenerationInterval 0';
  521. push @cfgarr, 'LoginGraceTime 30';
  522. push @cfgarr, "LogLevel $loglevel";
  523. push @cfgarr, 'MaxStartups 5';
  524. push @cfgarr, 'PasswordAuthentication no';
  525. push @cfgarr, 'PermitEmptyPasswords no';
  526. push @cfgarr, 'PermitRootLogin no';
  527. push @cfgarr, 'PrintLastLog no';
  528. push @cfgarr, 'PrintMotd no';
  529. push @cfgarr, 'PubkeyAuthentication yes';
  530. push @cfgarr, 'RhostsRSAAuthentication no';
  531. push @cfgarr, 'RSAAuthentication no';
  532. push @cfgarr, 'ServerKeyBits 768';
  533. push @cfgarr, 'StrictModes no';
  534. push @cfgarr, "Subsystem sftp \"$sftpsrv_config\"";
  535. push @cfgarr, 'SyslogFacility AUTH';
  536. push @cfgarr, 'UseLogin no';
  537. push @cfgarr, 'X11Forwarding no';
  538. push @cfgarr, '#';
  539. #***************************************************************************
  540. # Write out initial sshd configuration file for curl's tests
  541. #
  542. $error = dump_array($sshdconfig, @cfgarr);
  543. if($error) {
  544. logmsg $error;
  545. exit 1;
  546. }
  547. #***************************************************************************
  548. # Verifies at run time if sshd supports a given configuration file option
  549. #
  550. sub sshd_supports_opt {
  551. my ($option, $value) = @_;
  552. my $err;
  553. #
  554. if((($sshdid =~ /OpenSSH/) && ($sshdvernum >= 310)) ||
  555. ($sshdid =~ /SunSSH/)) {
  556. # ssh daemon supports command line options -t -f and -o
  557. $err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/,
  558. qx("$sshd" -t -f $sshdconfig -o "$option=$value" 2>&1);
  559. return !$err;
  560. }
  561. if(($sshdid =~ /OpenSSH/) && ($sshdvernum >= 299)) {
  562. # ssh daemon supports command line options -t and -f
  563. $err = dump_array($sshdconfig, (@cfgarr, "$option $value"));
  564. if($err) {
  565. logmsg $err;
  566. return 0;
  567. }
  568. $err = grep /((Unsupported)|(Bad configuration)|(Deprecated)) option.*$option/,
  569. qx("$sshd" -t -f $sshdconfig 2>&1);
  570. unlink $sshdconfig;
  571. return !$err;
  572. }
  573. return 0;
  574. }
  575. #***************************************************************************
  576. # Kerberos Authentication support may have not been built into sshd
  577. #
  578. if(sshd_supports_opt('KerberosAuthentication','no')) {
  579. push @cfgarr, 'KerberosAuthentication no';
  580. }
  581. if(sshd_supports_opt('KerberosGetAFSToken','no')) {
  582. push @cfgarr, 'KerberosGetAFSToken no';
  583. }
  584. if(sshd_supports_opt('KerberosOrLocalPasswd','no')) {
  585. push @cfgarr, 'KerberosOrLocalPasswd no';
  586. }
  587. if(sshd_supports_opt('KerberosTgtPassing','no')) {
  588. push @cfgarr, 'KerberosTgtPassing no';
  589. }
  590. if(sshd_supports_opt('KerberosTicketCleanup','yes')) {
  591. push @cfgarr, 'KerberosTicketCleanup yes';
  592. }
  593. #***************************************************************************
  594. # Andrew File System support may have not been built into sshd
  595. #
  596. if(sshd_supports_opt('AFSTokenPassing','no')) {
  597. push @cfgarr, 'AFSTokenPassing no';
  598. }
  599. #***************************************************************************
  600. # S/Key authentication support may have not been built into sshd
  601. #
  602. if(sshd_supports_opt('SkeyAuthentication','no')) {
  603. push @cfgarr, 'SkeyAuthentication no';
  604. }
  605. #***************************************************************************
  606. # GSSAPI Authentication support may have not been built into sshd
  607. #
  608. my $sshd_builtwith_GSSAPI;
  609. if(sshd_supports_opt('GSSAPIAuthentication','no')) {
  610. push @cfgarr, 'GSSAPIAuthentication no';
  611. $sshd_builtwith_GSSAPI = 1;
  612. }
  613. if(sshd_supports_opt('GSSAPICleanupCredentials','yes')) {
  614. push @cfgarr, 'GSSAPICleanupCredentials yes';
  615. }
  616. if(sshd_supports_opt('GSSAPIKeyExchange','no')) {
  617. push @cfgarr, 'GSSAPIKeyExchange no';
  618. }
  619. if(sshd_supports_opt('GSSAPIStoreDelegatedCredentials','no')) {
  620. push @cfgarr, 'GSSAPIStoreDelegatedCredentials no';
  621. }
  622. if(sshd_supports_opt('GSSCleanupCreds','yes')) {
  623. push @cfgarr, 'GSSCleanupCreds yes';
  624. }
  625. if(sshd_supports_opt('GSSUseSessionCredCache','no')) {
  626. push @cfgarr, 'GSSUseSessionCredCache no';
  627. }
  628. push @cfgarr, '#';
  629. #***************************************************************************
  630. # Options that might be supported or not in sshd OpenSSH 2.9.9 and later
  631. #
  632. if(sshd_supports_opt('AddressFamily','any')) {
  633. # Address family must be specified before ListenAddress
  634. splice @cfgarr, 14, 0, 'AddressFamily any';
  635. }
  636. if(sshd_supports_opt('Compression','no')) {
  637. push @cfgarr, 'Compression no';
  638. }
  639. if(sshd_supports_opt('KbdInteractiveAuthentication','no')) {
  640. push @cfgarr, 'KbdInteractiveAuthentication no';
  641. }
  642. if(sshd_supports_opt('KeepAlive','no')) {
  643. push @cfgarr, 'KeepAlive no';
  644. }
  645. if(sshd_supports_opt('LookupClientHostnames','no')) {
  646. push @cfgarr, 'LookupClientHostnames no';
  647. }
  648. if(sshd_supports_opt('MaxAuthTries','10')) {
  649. push @cfgarr, 'MaxAuthTries 10';
  650. }
  651. if(sshd_supports_opt('PAMAuthenticationViaKbdInt','no')) {
  652. push @cfgarr, 'PAMAuthenticationViaKbdInt no';
  653. }
  654. if(sshd_supports_opt('PermitTunnel','no')) {
  655. push @cfgarr, 'PermitTunnel no';
  656. }
  657. if(sshd_supports_opt('PermitUserEnvironment','no')) {
  658. push @cfgarr, 'PermitUserEnvironment no';
  659. }
  660. if(sshd_supports_opt('RhostsAuthentication','no')) {
  661. push @cfgarr, 'RhostsAuthentication no';
  662. }
  663. if(sshd_supports_opt('TCPKeepAlive','no')) {
  664. push @cfgarr, 'TCPKeepAlive no';
  665. }
  666. if(sshd_supports_opt('UseDNS','no')) {
  667. push @cfgarr, 'UseDNS no';
  668. }
  669. if(sshd_supports_opt('UsePAM','no')) {
  670. push @cfgarr, 'UsePAM no';
  671. }
  672. if($sshdid =~ /OpenSSH/) {
  673. # http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=6492415
  674. if(sshd_supports_opt('UsePrivilegeSeparation','no')) {
  675. push @cfgarr, 'UsePrivilegeSeparation no';
  676. }
  677. }
  678. if(sshd_supports_opt('VerifyReverseMapping','no')) {
  679. push @cfgarr, 'VerifyReverseMapping no';
  680. }
  681. if(sshd_supports_opt('X11UseLocalhost','yes')) {
  682. push @cfgarr, 'X11UseLocalhost yes';
  683. }
  684. push @cfgarr, '#';
  685. #***************************************************************************
  686. # Write out resulting sshd configuration file for curl's tests
  687. #
  688. $error = dump_array($sshdconfig, @cfgarr);
  689. if($error) {
  690. logmsg $error;
  691. exit 1;
  692. }
  693. #***************************************************************************
  694. # Verify that sshd actually supports our generated configuration file
  695. #
  696. if(system "\"$sshd\" -t -f $sshdconfig > $sshdlog 2>&1") {
  697. logmsg "sshd configuration file $sshdconfig failed verification";
  698. display_sshdlog();
  699. display_sshdconfig();
  700. exit 1;
  701. }
  702. #***************************************************************************
  703. # Generate ssh client host key database file for curl's tests
  704. #
  705. if((! -e $knownhosts) || (! -s $knownhosts)) {
  706. logmsg 'generating ssh client known hosts file...' if($verbose);
  707. unlink($knownhosts);
  708. if(open(RSAKEYFILE, "<$hstpubkeyf")) {
  709. my @rsahostkey = do { local $/ = ' '; <RSAKEYFILE> };
  710. if(close(RSAKEYFILE)) {
  711. if(open(KNOWNHOSTS, ">$knownhosts")) {
  712. print KNOWNHOSTS "$listenaddr ssh-rsa $rsahostkey[1]\n";
  713. if(!close(KNOWNHOSTS)) {
  714. $error = "Error: cannot close file $knownhosts";
  715. }
  716. }
  717. else {
  718. $error = "Error: cannot write file $knownhosts";
  719. }
  720. }
  721. else {
  722. $error = "Error: cannot close file $hstpubkeyf";
  723. }
  724. }
  725. else {
  726. $error = "Error: cannot read file $hstpubkeyf";
  727. }
  728. if($error) {
  729. logmsg $error;
  730. exit 1;
  731. }
  732. }
  733. #***************************************************************************
  734. # Convert paths for curl's tests running on Windows using Cygwin OpenSSH
  735. #
  736. my $identity_config = abs_path("$path/$identity");
  737. my $knownhosts_config = abs_path("$path/$knownhosts");
  738. if (pathhelp::os_is_win()) {
  739. # Ensure to use MinGW/Cygwin paths
  740. $identity_config = pathhelp::build_sys_abs_path($identity_config);
  741. $knownhosts_config = pathhelp::build_sys_abs_path($knownhosts_config);
  742. }
  743. if ($sshdid =~ /OpenSSH-Windows/) {
  744. # Ensure to use native Windows paths with OpenSSH for Windows
  745. $identity_config = pathhelp::sys_native_abs_path($identity);
  746. $knownhosts_config = pathhelp::sys_native_abs_path($knownhosts);
  747. }
  748. #***************************************************************************
  749. # ssh client configuration file options we might use and version support
  750. #
  751. # AddressFamily : OpenSSH 3.7.0 and later
  752. # BatchMode : OpenSSH 1.2.1 and later
  753. # BindAddress : OpenSSH 2.9.9 and later
  754. # ChallengeResponseAuthentication : OpenSSH 2.5.0 and later
  755. # CheckHostIP : OpenSSH 1.2.1 and later
  756. # Cipher : OpenSSH 1.2.1 and later [3]
  757. # Ciphers : OpenSSH 2.1.0 and later [3]
  758. # ClearAllForwardings : OpenSSH 2.9.9 and later
  759. # Compression : OpenSSH 1.2.1 and later
  760. # CompressionLevel : OpenSSH 1.2.1 and later [3]
  761. # ConnectionAttempts : OpenSSH 1.2.1 and later
  762. # ConnectTimeout : OpenSSH 3.7.0 and later
  763. # ControlMaster : OpenSSH 3.9.0 and later
  764. # ControlPath : OpenSSH 3.9.0 and later
  765. # DisableBanner : SunSSH 1.2.0 and later
  766. # DynamicForward : OpenSSH 2.9.0 and later
  767. # EnableSSHKeysign : OpenSSH 3.6.0 and later
  768. # EscapeChar : OpenSSH 1.2.1 and later [3]
  769. # ExitOnForwardFailure : OpenSSH 4.4.0 and later
  770. # ForwardAgent : OpenSSH 1.2.1 and later
  771. # ForwardX11 : OpenSSH 1.2.1 and later
  772. # ForwardX11Trusted : OpenSSH 3.8.0 and later
  773. # GatewayPorts : OpenSSH 1.2.1 and later
  774. # GlobalKnownHostsFile : OpenSSH 1.2.1 and later
  775. # GSSAPIAuthentication : OpenSSH 3.7.0 and later [1]
  776. # GSSAPIDelegateCredentials : OpenSSH 3.7.0 and later [1]
  777. # HashKnownHosts : OpenSSH 4.0.0 and later
  778. # Host : OpenSSH 1.2.1 and later
  779. # HostbasedAuthentication : OpenSSH 2.9.0 and later
  780. # HostKeyAlgorithms : OpenSSH 2.9.0 and later [3]
  781. # HostKeyAlias : OpenSSH 2.5.0 and later [3]
  782. # HostName : OpenSSH 1.2.1 and later
  783. # IdentitiesOnly : OpenSSH 3.9.0 and later
  784. # IdentityFile : OpenSSH 1.2.1 and later
  785. # IgnoreIfUnknown : SunSSH 1.2.0 and later
  786. # KeepAlive : OpenSSH 1.2.1 and later
  787. # KbdInteractiveAuthentication : OpenSSH 2.3.0 and later
  788. # KbdInteractiveDevices : OpenSSH 2.3.0 and later [3]
  789. # LocalCommand : OpenSSH 4.3.0 and later [3]
  790. # LocalForward : OpenSSH 1.2.1 and later [3]
  791. # LogLevel : OpenSSH 1.2.1 and later
  792. # MACs : OpenSSH 2.5.0 and later [3]
  793. # NoHostAuthenticationForLocalhost : OpenSSH 3.0.0 and later
  794. # NumberOfPasswordPrompts : OpenSSH 1.2.1 and later
  795. # PasswordAuthentication : OpenSSH 1.2.1 and later
  796. # PermitLocalCommand : OpenSSH 4.3.0 and later
  797. # Port : OpenSSH 1.2.1 and later
  798. # PreferredAuthentications : OpenSSH 2.5.2 and later
  799. # Protocol : OpenSSH 2.1.0 and later
  800. # ProxyCommand : OpenSSH 1.2.1 and later [3]
  801. # PubkeyAuthentication : OpenSSH 2.5.0 and later
  802. # RekeyLimit : OpenSSH 3.7.0 and later
  803. # RemoteForward : OpenSSH 1.2.1 and later [3]
  804. # RhostsRSAAuthentication : OpenSSH 1.2.1 and later
  805. # RSAAuthentication : OpenSSH 1.2.1 and later
  806. # ServerAliveCountMax : OpenSSH 3.8.0 and later
  807. # ServerAliveInterval : OpenSSH 3.8.0 and later
  808. # SmartcardDevice : OpenSSH 2.9.9 and later [1][3]
  809. # StrictHostKeyChecking : OpenSSH 1.2.1 and later
  810. # TCPKeepAlive : OpenSSH 3.8.0 and later
  811. # Tunnel : OpenSSH 4.3.0 and later
  812. # TunnelDevice : OpenSSH 4.3.0 and later [3]
  813. # UsePAM : OpenSSH 3.7.0 and later [1][2][3]
  814. # UsePrivilegedPort : OpenSSH 1.2.1 and later
  815. # User : OpenSSH 1.2.1 and later
  816. # UserKnownHostsFile : OpenSSH 1.2.1 and later
  817. # VerifyHostKeyDNS : OpenSSH 3.8.0 and later
  818. # XAuthLocation : OpenSSH 2.1.1 and later [3]
  819. #
  820. # [1] Option only available if activated at compile time
  821. # [2] Option specific for portable versions
  822. # [3] Option not used in our ssh client config file
  823. #***************************************************************************
  824. # Initialize ssh config with options actually supported in OpenSSH 2.9.9
  825. #
  826. logmsg 'generating ssh client config file...' if($verbose);
  827. @cfgarr = ();
  828. push @cfgarr, '# This is a generated file. Do not edit.';
  829. push @cfgarr, "# $sshverstr ssh client configuration file for curl testing";
  830. push @cfgarr, '#';
  831. push @cfgarr, 'Host *';
  832. push @cfgarr, '#';
  833. push @cfgarr, "Port $port";
  834. push @cfgarr, "HostName $listenaddr";
  835. push @cfgarr, "User $username";
  836. push @cfgarr, 'Protocol 2';
  837. push @cfgarr, '#';
  838. # BindAddress option is not supported by OpenSSH for Windows
  839. if (!($sshdid =~ /OpenSSH-Windows/)) {
  840. push @cfgarr, "BindAddress $listenaddr";
  841. }
  842. push @cfgarr, '#';
  843. push @cfgarr, "IdentityFile $identity_config";
  844. push @cfgarr, "UserKnownHostsFile $knownhosts_config";
  845. push @cfgarr, '#';
  846. push @cfgarr, 'BatchMode yes';
  847. push @cfgarr, 'ChallengeResponseAuthentication no';
  848. push @cfgarr, 'CheckHostIP no';
  849. push @cfgarr, 'ClearAllForwardings no';
  850. push @cfgarr, 'Compression no';
  851. push @cfgarr, 'ConnectionAttempts 3';
  852. push @cfgarr, 'ForwardAgent no';
  853. push @cfgarr, 'ForwardX11 no';
  854. push @cfgarr, 'GatewayPorts no';
  855. push @cfgarr, 'GlobalKnownHostsFile /dev/null';
  856. push @cfgarr, 'HostbasedAuthentication no';
  857. push @cfgarr, 'KbdInteractiveAuthentication no';
  858. push @cfgarr, "LogLevel $loglevel";
  859. push @cfgarr, 'NumberOfPasswordPrompts 0';
  860. push @cfgarr, 'PasswordAuthentication no';
  861. push @cfgarr, 'PreferredAuthentications publickey';
  862. push @cfgarr, 'PubkeyAuthentication yes';
  863. # RSA authentication options are not supported by OpenSSH for Windows
  864. if (!($sshdid =~ /OpenSSH-Windows/)) {
  865. push @cfgarr, 'RhostsRSAAuthentication no';
  866. push @cfgarr, 'RSAAuthentication no';
  867. }
  868. # Disabled StrictHostKeyChecking since it makes the tests fail on my
  869. # OpenSSH_6.0p1 on Debian Linux / Daniel
  870. push @cfgarr, 'StrictHostKeyChecking no';
  871. push @cfgarr, 'UsePrivilegedPort no';
  872. push @cfgarr, '#';
  873. #***************************************************************************
  874. # Options supported in ssh client newer than OpenSSH 2.9.9
  875. #
  876. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) {
  877. push @cfgarr, 'AddressFamily any';
  878. }
  879. if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) ||
  880. (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
  881. push @cfgarr, 'ConnectTimeout 30';
  882. }
  883. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
  884. push @cfgarr, 'ControlMaster no';
  885. }
  886. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 420)) {
  887. push @cfgarr, 'ControlPath none';
  888. }
  889. if(($sshid =~ /SunSSH/) && ($sshvernum >= 120)) {
  890. push @cfgarr, 'DisableBanner yes';
  891. }
  892. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 360)) {
  893. push @cfgarr, 'EnableSSHKeysign no';
  894. }
  895. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 440)) {
  896. push @cfgarr, 'ExitOnForwardFailure yes';
  897. }
  898. if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) ||
  899. (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
  900. push @cfgarr, 'ForwardX11Trusted no';
  901. }
  902. if(($sshd_builtwith_GSSAPI) && ($sshdid eq $sshid) &&
  903. ($sshdvernum == $sshvernum)) {
  904. push @cfgarr, 'GSSAPIAuthentication no';
  905. push @cfgarr, 'GSSAPIDelegateCredentials no';
  906. if($sshid =~ /SunSSH/) {
  907. push @cfgarr, 'GSSAPIKeyExchange no';
  908. }
  909. }
  910. if((($sshid =~ /OpenSSH/) && ($sshvernum >= 400)) ||
  911. (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
  912. push @cfgarr, 'HashKnownHosts no';
  913. }
  914. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 390)) {
  915. push @cfgarr, 'IdentitiesOnly yes';
  916. }
  917. if(($sshid =~ /SunSSH/) && ($sshvernum >= 120)) {
  918. push @cfgarr, 'IgnoreIfUnknown no';
  919. }
  920. if((($sshid =~ /OpenSSH/) && ($sshvernum < 380)) ||
  921. ($sshid =~ /SunSSH/)) {
  922. push @cfgarr, 'KeepAlive no';
  923. }
  924. if((($sshid =~ /OpenSSH/) && ($sshvernum >= 300)) ||
  925. ($sshid =~ /SunSSH/)) {
  926. push @cfgarr, 'NoHostAuthenticationForLocalhost no';
  927. }
  928. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) {
  929. push @cfgarr, 'PermitLocalCommand no';
  930. }
  931. if((($sshid =~ /OpenSSH/) && ($sshvernum >= 370)) ||
  932. (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
  933. push @cfgarr, 'RekeyLimit 1G';
  934. }
  935. if((($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) ||
  936. (($sshid =~ /SunSSH/) && ($sshvernum >= 120))) {
  937. push @cfgarr, 'ServerAliveCountMax 3';
  938. push @cfgarr, 'ServerAliveInterval 0';
  939. }
  940. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) {
  941. push @cfgarr, 'TCPKeepAlive no';
  942. }
  943. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 430)) {
  944. push @cfgarr, 'Tunnel no';
  945. }
  946. if(($sshid =~ /OpenSSH/) && ($sshvernum >= 380)) {
  947. push @cfgarr, 'VerifyHostKeyDNS no';
  948. }
  949. push @cfgarr, '#';
  950. #***************************************************************************
  951. # Write out resulting ssh client configuration file for curl's tests
  952. #
  953. $error = dump_array($sshconfig, @cfgarr);
  954. if($error) {
  955. logmsg $error;
  956. exit 1;
  957. }
  958. #***************************************************************************
  959. # Initialize client sftp config with options actually supported.
  960. #
  961. logmsg 'generating sftp client config file...' if($verbose);
  962. splice @cfgarr, 1, 1, "# $sshverstr sftp client configuration file for curl testing";
  963. #
  964. for(my $i = scalar(@cfgarr) - 1; $i > 0; $i--) {
  965. if($cfgarr[$i] =~ /^DynamicForward/) {
  966. splice @cfgarr, $i, 1;
  967. next;
  968. }
  969. if($cfgarr[$i] =~ /^ClearAllForwardings/) {
  970. splice @cfgarr, $i, 1, "ClearAllForwardings yes";
  971. next;
  972. }
  973. }
  974. #***************************************************************************
  975. # Write out resulting sftp client configuration file for curl's tests
  976. #
  977. $error = dump_array($sftpconfig, @cfgarr);
  978. if($error) {
  979. logmsg $error;
  980. exit 1;
  981. }
  982. @cfgarr = ();
  983. #***************************************************************************
  984. # Generate client sftp commands batch file for sftp server verification
  985. #
  986. logmsg 'generating sftp client commands file...' if($verbose);
  987. push @cfgarr, 'pwd';
  988. push @cfgarr, 'quit';
  989. $error = dump_array($sftpcmds, @cfgarr);
  990. if($error) {
  991. logmsg $error;
  992. exit 1;
  993. }
  994. @cfgarr = ();
  995. #***************************************************************************
  996. # Prepare command line of ssh server daemon
  997. #
  998. my $cmd = "\"$sshd\" -e -D -f $sshdconfig > $sshdlog 2>&1";
  999. logmsg "SCP/SFTP server listening on port $port" if($verbose);
  1000. logmsg "RUN: $cmd" if($verbose);
  1001. #***************************************************************************
  1002. # Start the ssh server daemon on Windows without forking it
  1003. #
  1004. if ($sshdid =~ /OpenSSH-Windows/) {
  1005. # Fake pidfile for ssh server on Windows.
  1006. if(open(OUT, ">$pidfile")) {
  1007. print OUT $$ . "\n";
  1008. close(OUT);
  1009. }
  1010. # Flush output.
  1011. $| = 1;
  1012. # Put an "exec" in front of the command so that the child process
  1013. # keeps this child's process ID by being tied to the spawned shell.
  1014. exec("exec $cmd") || die "Can't exec() $cmd: $!";
  1015. # exec() will create a new process, but ties the existence of the
  1016. # new process to the parent waiting perl.exe and sh.exe processes.
  1017. # exec() should never return back here to this process. We protect
  1018. # ourselves by calling die() just in case something goes really bad.
  1019. die "error: exec() has returned";
  1020. }
  1021. #***************************************************************************
  1022. # Start the ssh server daemon without forking it
  1023. #
  1024. my $rc = system($cmd);
  1025. if($rc == -1) {
  1026. logmsg "\"$sshd\" failed with: $!";
  1027. }
  1028. elsif($rc & 127) {
  1029. logmsg sprintf("\"$sshd\" died with signal %d, and %s coredump",
  1030. ($rc & 127), ($rc & 128)?'a':'no');
  1031. }
  1032. elsif($verbose && ($rc >> 8)) {
  1033. logmsg sprintf("\"$sshd\" exited with %d", $rc >> 8);
  1034. }
  1035. #***************************************************************************
  1036. # Clean up once the server has stopped
  1037. #
  1038. unlink($hstprvkeyf, $hstpubkeyf, $hstpubmd5f, $hstpubsha256f,
  1039. $cliprvkeyf, $clipubkeyf, $knownhosts,
  1040. $sshdconfig, $sshconfig, $sftpconfig);
  1041. exit 0;