ftpserver.pl 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242
  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 https://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 a server designed for the curl test suite.
  24. #
  25. # In December 2009 we started remaking the server to support more protocols
  26. # that are similar in spirit. Like POP3, IMAP and SMTP in addition to the FTP
  27. # it already supported since a long time. Note that it still only supports one
  28. # protocol per invoke. You need to start multiple servers to support multiple
  29. # protocols simultaneously.
  30. #
  31. # It is meant to exercise curl, it is not meant to be a fully working
  32. # or even very standard compliant server.
  33. #
  34. # You may optionally specify port on the command line, otherwise it'll
  35. # default to port 8921.
  36. #
  37. # All socket/network/TCP related stuff is done by the 'sockfilt' program.
  38. #
  39. BEGIN {
  40. push(@INC, $ENV{'srcdir'}) if(defined $ENV{'srcdir'});
  41. push(@INC, ".");
  42. # sub second timestamping needs Time::HiRes
  43. eval {
  44. no warnings "all";
  45. require Time::HiRes;
  46. import Time::HiRes qw( gettimeofday );
  47. }
  48. }
  49. use strict;
  50. use warnings;
  51. use IPC::Open2;
  52. use Digest::MD5;
  53. require "getpart.pm";
  54. require "ftp.pm";
  55. require "directories.pm";
  56. use serverhelp qw(
  57. servername_str
  58. server_pidfilename
  59. server_logfilename
  60. mainsockf_pidfilename
  61. mainsockf_logfilename
  62. datasockf_pidfilename
  63. datasockf_logfilename
  64. );
  65. #**********************************************************************
  66. # global vars...
  67. #
  68. my $verbose = 0; # set to 1 for debugging
  69. my $idstr = ""; # server instance string
  70. my $idnum = 1; # server instance number
  71. my $ipvnum = 4; # server IPv number (4 or 6)
  72. my $proto = 'ftp'; # default server protocol
  73. my $srcdir; # directory where ftpserver.pl is located
  74. my $srvrname; # server name for presentation purposes
  75. my $cwd_testno; # test case numbers extracted from CWD command
  76. my $path = '.';
  77. my $logdir = $path .'/log';
  78. #**********************************************************************
  79. # global vars used for server address and primary listener port
  80. #
  81. my $port = 8921; # default primary listener port
  82. my $listenaddr = '127.0.0.1'; # default address for listener port
  83. #**********************************************************************
  84. # global vars used for file names
  85. #
  86. my $pidfile; # server pid file name
  87. my $logfile; # server log file name
  88. my $mainsockf_pidfile; # pid file for primary connection sockfilt process
  89. my $mainsockf_logfile; # log file for primary connection sockfilt process
  90. my $datasockf_pidfile; # pid file for secondary connection sockfilt process
  91. my $datasockf_logfile; # log file for secondary connection sockfilt process
  92. #**********************************************************************
  93. # global vars used for server logs advisor read lock handling
  94. #
  95. my $SERVERLOGS_LOCK = 'log/serverlogs.lock';
  96. my $serverlogslocked = 0;
  97. #**********************************************************************
  98. # global vars used for child processes PID tracking
  99. #
  100. my $sfpid; # PID for primary connection sockfilt process
  101. my $slavepid; # PID for secondary connection sockfilt process
  102. #**********************************************************************
  103. # global typeglob filehandle vars to read/write from/to sockfilters
  104. #
  105. local *SFREAD; # used to read from primary connection
  106. local *SFWRITE; # used to write to primary connection
  107. local *DREAD; # used to read from secondary connection
  108. local *DWRITE; # used to write to secondary connection
  109. my $sockfilt_timeout = 5; # default timeout for sockfilter eXsysreads
  110. #**********************************************************************
  111. # global vars which depend on server protocol selection
  112. #
  113. my %commandfunc; # protocol command specific function callbacks
  114. my %displaytext; # text returned to client before callback runs
  115. #**********************************************************************
  116. # global vars customized for each test from the server commands file
  117. #
  118. my $ctrldelay; # set if server should throttle ctrl stream
  119. my $datadelay; # set if server should throttle data stream
  120. my $retrweirdo; # set if ftp server should use RETRWEIRDO
  121. my $retrnosize; # set if ftp server should use RETRNOSIZE
  122. my $pasvbadip; # set if ftp server should use PASVBADIP
  123. my $nosave; # set if ftp server should not save uploaded data
  124. my $nodataconn; # set if ftp srvr doesn't establish or accepts data channel
  125. my $nodataconn425; # set if ftp srvr doesn't establish data ch and replies 425
  126. my $nodataconn421; # set if ftp srvr doesn't establish data ch and replies 421
  127. my $nodataconn150; # set if ftp srvr doesn't establish data ch and replies 150
  128. my @capabilities; # set if server supports capability commands
  129. my @auth_mechs; # set if server supports authentication commands
  130. my %fulltextreply; #
  131. my %commandreply; #
  132. my %customcount; #
  133. my %delayreply; #
  134. #**********************************************************************
  135. # global variables for to test ftp wildcardmatching or other test that
  136. # need flexible LIST responses.. and corresponding files.
  137. # $ftptargetdir is keeping the fake "name" of LIST directory.
  138. #
  139. my $ftplistparserstate;
  140. my $ftptargetdir="";
  141. #**********************************************************************
  142. # global variables used when running a ftp server to keep state info
  143. # relative to the secondary or data sockfilt process. Values of these
  144. # variables should only be modified using datasockf_state() sub, given
  145. # that they are closely related and relationship is a bit awkward.
  146. #
  147. my $datasockf_state = 'STOPPED'; # see datasockf_state() sub
  148. my $datasockf_mode = 'none'; # ['none','active','passive']
  149. my $datasockf_runs = 'no'; # ['no','yes']
  150. my $datasockf_conn = 'no'; # ['no','yes']
  151. #**********************************************************************
  152. # global vars used for signal handling
  153. #
  154. my $got_exit_signal = 0; # set if program should finish execution ASAP
  155. my $exit_signal; # first signal handled in exit_signal_handler
  156. #**********************************************************************
  157. # Mail related definitions
  158. #
  159. my $TEXT_USERNAME = "user";
  160. my $TEXT_PASSWORD = "secret";
  161. my $POP3_TIMESTAMP = "<1972.987654321\@curl>";
  162. #**********************************************************************
  163. # exit_signal_handler will be triggered to indicate that the program
  164. # should finish its execution in a controlled way as soon as possible.
  165. # For now, program will also terminate from within this handler.
  166. #
  167. sub exit_signal_handler {
  168. my $signame = shift;
  169. # For now, simply mimic old behavior.
  170. killsockfilters($proto, $ipvnum, $idnum, $verbose);
  171. unlink($pidfile);
  172. if($serverlogslocked) {
  173. $serverlogslocked = 0;
  174. clear_advisor_read_lock($SERVERLOGS_LOCK);
  175. }
  176. exit;
  177. }
  178. #**********************************************************************
  179. # logmsg is general message logging subroutine for our test servers.
  180. #
  181. sub logmsg {
  182. my $now;
  183. # sub second timestamping needs Time::HiRes
  184. if($Time::HiRes::VERSION) {
  185. my ($seconds, $usec) = gettimeofday();
  186. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  187. localtime($seconds);
  188. $now = sprintf("%02d:%02d:%02d.%06d ", $hour, $min, $sec, $usec);
  189. }
  190. else {
  191. my $seconds = time();
  192. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  193. localtime($seconds);
  194. $now = sprintf("%02d:%02d:%02d ", $hour, $min, $sec);
  195. }
  196. if(open(LOGFILEFH, ">>$logfile")) {
  197. print LOGFILEFH $now;
  198. print LOGFILEFH @_;
  199. close(LOGFILEFH);
  200. }
  201. }
  202. sub ftpmsg {
  203. # append to the server.input file
  204. open(INPUT, ">>log/server$idstr.input") ||
  205. logmsg "failed to open log/server$idstr.input\n";
  206. print INPUT @_;
  207. close(INPUT);
  208. # use this, open->print->close system only to make the file
  209. # open as little as possible, to make the test suite run
  210. # better on windows/cygwin
  211. }
  212. #**********************************************************************
  213. # eXsysread is a wrapper around perl's sysread() function. This will
  214. # repeat the call to sysread() until it has actually read the complete
  215. # number of requested bytes or an unrecoverable condition occurs.
  216. # On success returns a positive value, the number of bytes requested.
  217. # On failure or timeout returns zero.
  218. #
  219. sub eXsysread {
  220. my $FH = shift;
  221. my $scalar = shift;
  222. my $nbytes = shift;
  223. my $timeout = shift; # A zero timeout disables eXsysread() time limit
  224. #
  225. my $time_limited = 0;
  226. my $timeout_rest = 0;
  227. my $start_time = 0;
  228. my $nread = 0;
  229. my $rc;
  230. $$scalar = "";
  231. if((not defined $nbytes) || ($nbytes < 1)) {
  232. logmsg "Error: eXsysread() failure: " .
  233. "length argument must be positive\n";
  234. return 0;
  235. }
  236. if((not defined $timeout) || ($timeout < 0)) {
  237. logmsg "Error: eXsysread() failure: " .
  238. "timeout argument must be zero or positive\n";
  239. return 0;
  240. }
  241. if($timeout > 0) {
  242. # caller sets eXsysread() time limit
  243. $time_limited = 1;
  244. $timeout_rest = $timeout;
  245. $start_time = int(time());
  246. }
  247. while($nread < $nbytes) {
  248. if($time_limited) {
  249. eval {
  250. local $SIG{ALRM} = sub { die "alarm\n"; };
  251. alarm $timeout_rest;
  252. $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
  253. alarm 0;
  254. };
  255. $timeout_rest = $timeout - (int(time()) - $start_time);
  256. if($timeout_rest < 1) {
  257. logmsg "Error: eXsysread() failure: timed out\n";
  258. return 0;
  259. }
  260. }
  261. else {
  262. $rc = sysread($FH, $$scalar, $nbytes - $nread, $nread);
  263. }
  264. if($got_exit_signal) {
  265. logmsg "Error: eXsysread() failure: signalled to die\n";
  266. return 0;
  267. }
  268. if(not defined $rc) {
  269. if($!{EINTR}) {
  270. logmsg "Warning: retrying sysread() interrupted system call\n";
  271. next;
  272. }
  273. if($!{EAGAIN}) {
  274. logmsg "Warning: retrying sysread() due to EAGAIN\n";
  275. next;
  276. }
  277. if($!{EWOULDBLOCK}) {
  278. logmsg "Warning: retrying sysread() due to EWOULDBLOCK\n";
  279. next;
  280. }
  281. logmsg "Error: sysread() failure: $!\n";
  282. return 0;
  283. }
  284. if($rc < 0) {
  285. logmsg "Error: sysread() failure: returned negative value $rc\n";
  286. return 0;
  287. }
  288. if($rc == 0) {
  289. logmsg "Error: sysread() failure: read zero bytes\n";
  290. return 0;
  291. }
  292. $nread += $rc;
  293. }
  294. return $nread;
  295. }
  296. #**********************************************************************
  297. # read_mainsockf attempts to read the given amount of output from the
  298. # sockfilter which is in use for the main or primary connection. This
  299. # reads untranslated sockfilt lingo which may hold data read from the
  300. # main or primary socket. On success returns 1, otherwise zero.
  301. #
  302. sub read_mainsockf {
  303. my $scalar = shift;
  304. my $nbytes = shift;
  305. my $timeout = shift; # Optional argument, if zero blocks indefinitively
  306. my $FH = \*SFREAD;
  307. if(not defined $timeout) {
  308. $timeout = $sockfilt_timeout + ($nbytes >> 12);
  309. }
  310. if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
  311. my ($fcaller, $lcaller) = (caller)[1,2];
  312. logmsg "Error: read_mainsockf() failure at $fcaller " .
  313. "line $lcaller. Due to eXsysread() failure\n";
  314. return 0;
  315. }
  316. return 1;
  317. }
  318. #**********************************************************************
  319. # read_datasockf attempts to read the given amount of output from the
  320. # sockfilter which is in use for the data or secondary connection. This
  321. # reads untranslated sockfilt lingo which may hold data read from the
  322. # data or secondary socket. On success returns 1, otherwise zero.
  323. #
  324. sub read_datasockf {
  325. my $scalar = shift;
  326. my $nbytes = shift;
  327. my $timeout = shift; # Optional argument, if zero blocks indefinitively
  328. my $FH = \*DREAD;
  329. if(not defined $timeout) {
  330. $timeout = $sockfilt_timeout + ($nbytes >> 12);
  331. }
  332. if(eXsysread($FH, $scalar, $nbytes, $timeout) != $nbytes) {
  333. my ($fcaller, $lcaller) = (caller)[1,2];
  334. logmsg "Error: read_datasockf() failure at $fcaller " .
  335. "line $lcaller. Due to eXsysread() failure\n";
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. sub sysread_or_die {
  341. my $FH = shift;
  342. my $scalar = shift;
  343. my $length = shift;
  344. my $fcaller;
  345. my $lcaller;
  346. my $result;
  347. $result = sysread($$FH, $$scalar, $length);
  348. if(not defined $result) {
  349. ($fcaller, $lcaller) = (caller)[1,2];
  350. logmsg "Failed to read input\n";
  351. logmsg "Error: $srvrname server, sysread error: $!\n";
  352. logmsg "Exited from sysread_or_die() at $fcaller " .
  353. "line $lcaller. $srvrname server, sysread error: $!\n";
  354. killsockfilters($proto, $ipvnum, $idnum, $verbose);
  355. unlink($pidfile);
  356. if($serverlogslocked) {
  357. $serverlogslocked = 0;
  358. clear_advisor_read_lock($SERVERLOGS_LOCK);
  359. }
  360. exit;
  361. }
  362. elsif($result == 0) {
  363. ($fcaller, $lcaller) = (caller)[1,2];
  364. logmsg "Failed to read input\n";
  365. logmsg "Error: $srvrname server, read zero\n";
  366. logmsg "Exited from sysread_or_die() at $fcaller " .
  367. "line $lcaller. $srvrname server, read zero\n";
  368. killsockfilters($proto, $ipvnum, $idnum, $verbose);
  369. unlink($pidfile);
  370. if($serverlogslocked) {
  371. $serverlogslocked = 0;
  372. clear_advisor_read_lock($SERVERLOGS_LOCK);
  373. }
  374. exit;
  375. }
  376. return $result;
  377. }
  378. sub startsf {
  379. my $mainsockfcmd = "./server/sockfilt " .
  380. "--ipv$ipvnum --port $port " .
  381. "--pidfile \"$mainsockf_pidfile\" " .
  382. "--logfile \"$mainsockf_logfile\"";
  383. $sfpid = open2(*SFREAD, *SFWRITE, $mainsockfcmd);
  384. print STDERR "$mainsockfcmd\n" if($verbose);
  385. print SFWRITE "PING\n";
  386. my $pong;
  387. sysread_or_die(\*SFREAD, \$pong, 5);
  388. if($pong !~ /^PONG/) {
  389. logmsg "Failed sockfilt command: $mainsockfcmd\n";
  390. killsockfilters($proto, $ipvnum, $idnum, $verbose);
  391. unlink($pidfile);
  392. if($serverlogslocked) {
  393. $serverlogslocked = 0;
  394. clear_advisor_read_lock($SERVERLOGS_LOCK);
  395. }
  396. die "Failed to start sockfilt!";
  397. }
  398. }
  399. #**********************************************************************
  400. # Returns the given test's reply data
  401. #
  402. sub getreplydata {
  403. my ($testno) = @_;
  404. my $testpart = "";
  405. $testno =~ s/^([^0-9]*)//;
  406. if($testno > 10000) {
  407. $testpart = $testno % 10000;
  408. $testno = int($testno / 10000);
  409. }
  410. loadtest("$srcdir/data/test$testno");
  411. my @data = getpart("reply", "data$testpart");
  412. if((!@data) && ($testpart ne "")) {
  413. @data = getpart("reply", "data");
  414. }
  415. return @data;
  416. }
  417. sub sockfilt {
  418. my $l;
  419. foreach $l (@_) {
  420. printf SFWRITE "DATA\n%04x\n", length($l);
  421. print SFWRITE $l;
  422. }
  423. }
  424. sub sockfiltsecondary {
  425. my $l;
  426. foreach $l (@_) {
  427. printf DWRITE "DATA\n%04x\n", length($l);
  428. print DWRITE $l;
  429. }
  430. }
  431. #**********************************************************************
  432. # Send data to the client on the control stream, which happens to be plain
  433. # stdout.
  434. #
  435. sub sendcontrol {
  436. if(!$ctrldelay) {
  437. # spit it all out at once
  438. sockfilt @_;
  439. }
  440. else {
  441. my $a = join("", @_);
  442. my @a = split("", $a);
  443. for(@a) {
  444. sockfilt $_;
  445. select(undef, undef, undef, 0.01);
  446. }
  447. }
  448. my $log;
  449. foreach $log (@_) {
  450. my $l = $log;
  451. $l =~ s/\r/[CR]/g;
  452. $l =~ s/\n/[LF]/g;
  453. logmsg "> \"$l\"\n";
  454. }
  455. }
  456. #**********************************************************************
  457. # Send data to the FTP client on the data stream when data connection
  458. # is actually established. Given that this sub should only be called
  459. # when a data connection is supposed to be established, calling this
  460. # without a data connection is an indication of weak logic somewhere.
  461. #
  462. sub senddata {
  463. my $l;
  464. if($datasockf_conn eq 'no') {
  465. logmsg "WARNING: Detected data sending attempt without DATA channel\n";
  466. foreach $l (@_) {
  467. logmsg "WARNING: Data swallowed: $l\n"
  468. }
  469. return;
  470. }
  471. foreach $l (@_) {
  472. if(!$datadelay) {
  473. # spit it all out at once
  474. sockfiltsecondary $l;
  475. }
  476. else {
  477. # pause between each byte
  478. for (split(//,$l)) {
  479. sockfiltsecondary $_;
  480. select(undef, undef, undef, 0.01);
  481. }
  482. }
  483. }
  484. }
  485. #**********************************************************************
  486. # protocolsetup initializes the 'displaytext' and 'commandfunc' hashes
  487. # for the given protocol. References to protocol command callbacks are
  488. # stored in 'commandfunc' hash, and text which will be returned to the
  489. # client before the command callback runs is stored in 'displaytext'.
  490. #
  491. sub protocolsetup {
  492. my $proto = $_[0];
  493. if($proto eq 'ftp') {
  494. %commandfunc = (
  495. 'PORT' => \&PORT_ftp,
  496. 'EPRT' => \&PORT_ftp,
  497. 'LIST' => \&LIST_ftp,
  498. 'NLST' => \&NLST_ftp,
  499. 'PASV' => \&PASV_ftp,
  500. 'CWD' => \&CWD_ftp,
  501. 'PWD' => \&PWD_ftp,
  502. 'EPSV' => \&PASV_ftp,
  503. 'RETR' => \&RETR_ftp,
  504. 'SIZE' => \&SIZE_ftp,
  505. 'REST' => \&REST_ftp,
  506. 'STOR' => \&STOR_ftp,
  507. 'APPE' => \&STOR_ftp, # append looks like upload
  508. 'MDTM' => \&MDTM_ftp,
  509. );
  510. %displaytext = (
  511. 'USER' => '331 We are happy you popped in!',
  512. 'PASS' => '230 Welcome you silly person',
  513. 'PORT' => '200 You said PORT - I say FINE',
  514. 'TYPE' => '200 I modify TYPE as you wanted',
  515. 'LIST' => '150 here comes a directory',
  516. 'NLST' => '150 here comes a directory',
  517. 'CWD' => '250 CWD command successful.',
  518. 'SYST' => '215 UNIX Type: L8', # just fake something
  519. 'QUIT' => '221 bye bye baby', # just reply something
  520. 'MKD' => '257 Created your requested directory',
  521. 'REST' => '350 Yeah yeah we set it there for you',
  522. 'DELE' => '200 OK OK OK whatever you say',
  523. 'RNFR' => '350 Received your order. Please provide more',
  524. 'RNTO' => '250 Ok, thanks. File renaming completed.',
  525. 'NOOP' => '200 Yes, I\'m very good at doing nothing.',
  526. 'PBSZ' => '500 PBSZ not implemented',
  527. 'PROT' => '500 PROT not implemented',
  528. 'welcome' => join("",
  529. '220- _ _ ____ _ '."\r\n",
  530. '220- ___| | | | _ \| | '."\r\n",
  531. '220- / __| | | | |_) | | '."\r\n",
  532. '220- | (__| |_| | _ {| |___ '."\r\n",
  533. '220 \___|\___/|_| \_\_____|'."\r\n")
  534. );
  535. }
  536. elsif($proto eq 'pop3') {
  537. %commandfunc = (
  538. 'APOP' => \&APOP_pop3,
  539. 'AUTH' => \&AUTH_pop3,
  540. 'CAPA' => \&CAPA_pop3,
  541. 'DELE' => \&DELE_pop3,
  542. 'LIST' => \&LIST_pop3,
  543. 'NOOP' => \&NOOP_pop3,
  544. 'PASS' => \&PASS_pop3,
  545. 'QUIT' => \&QUIT_pop3,
  546. 'RETR' => \&RETR_pop3,
  547. 'RSET' => \&RSET_pop3,
  548. 'STAT' => \&STAT_pop3,
  549. 'TOP' => \&TOP_pop3,
  550. 'UIDL' => \&UIDL_pop3,
  551. 'USER' => \&USER_pop3,
  552. );
  553. %displaytext = (
  554. 'welcome' => join("",
  555. ' _ _ ____ _ '."\r\n",
  556. ' ___| | | | _ \| | '."\r\n",
  557. ' / __| | | | |_) | | '."\r\n",
  558. ' | (__| |_| | _ {| |___ '."\r\n",
  559. ' \___|\___/|_| \_\_____|'."\r\n",
  560. '+OK curl POP3 server ready to serve '."\r\n")
  561. );
  562. }
  563. elsif($proto eq 'imap') {
  564. %commandfunc = (
  565. 'APPEND' => \&APPEND_imap,
  566. 'CAPABILITY' => \&CAPABILITY_imap,
  567. 'CHECK' => \&CHECK_imap,
  568. 'CLOSE' => \&CLOSE_imap,
  569. 'COPY' => \&COPY_imap,
  570. 'CREATE' => \&CREATE_imap,
  571. 'DELETE' => \&DELETE_imap,
  572. 'EXAMINE' => \&EXAMINE_imap,
  573. 'EXPUNGE' => \&EXPUNGE_imap,
  574. 'FETCH' => \&FETCH_imap,
  575. 'LIST' => \&LIST_imap,
  576. 'LSUB' => \&LSUB_imap,
  577. 'LOGIN' => \&LOGIN_imap,
  578. 'LOGOUT' => \&LOGOUT_imap,
  579. 'NOOP' => \&NOOP_imap,
  580. 'RENAME' => \&RENAME_imap,
  581. 'SEARCH' => \&SEARCH_imap,
  582. 'SELECT' => \&SELECT_imap,
  583. 'STATUS' => \&STATUS_imap,
  584. 'STORE' => \&STORE_imap,
  585. 'UID' => \&UID_imap,
  586. );
  587. %displaytext = (
  588. 'welcome' => join("",
  589. ' _ _ ____ _ '."\r\n",
  590. ' ___| | | | _ \| | '."\r\n",
  591. ' / __| | | | |_) | | '."\r\n",
  592. ' | (__| |_| | _ {| |___ '."\r\n",
  593. ' \___|\___/|_| \_\_____|'."\r\n",
  594. '* OK curl IMAP server ready to serve'."\r\n")
  595. );
  596. }
  597. elsif($proto eq 'smtp') {
  598. %commandfunc = (
  599. 'DATA' => \&DATA_smtp,
  600. 'EHLO' => \&EHLO_smtp,
  601. 'EXPN' => \&EXPN_smtp,
  602. 'HELO' => \&HELO_smtp,
  603. 'HELP' => \&HELP_smtp,
  604. 'MAIL' => \&MAIL_smtp,
  605. 'NOOP' => \&NOOP_smtp,
  606. 'RSET' => \&RSET_smtp,
  607. 'RCPT' => \&RCPT_smtp,
  608. 'VRFY' => \&VRFY_smtp,
  609. 'QUIT' => \&QUIT_smtp,
  610. );
  611. %displaytext = (
  612. 'welcome' => join("",
  613. '220- _ _ ____ _ '."\r\n",
  614. '220- ___| | | | _ \| | '."\r\n",
  615. '220- / __| | | | |_) | | '."\r\n",
  616. '220- | (__| |_| | _ {| |___ '."\r\n",
  617. '220 \___|\___/|_| \_\_____|'."\r\n")
  618. );
  619. }
  620. }
  621. sub close_dataconn {
  622. my ($closed)=@_; # non-zero if already disconnected
  623. my $datapid = processexists($datasockf_pidfile);
  624. logmsg "=====> Closing $datasockf_mode DATA connection...\n";
  625. if(!$closed) {
  626. if($datapid > 0) {
  627. logmsg "Server disconnects $datasockf_mode DATA connection\n";
  628. print DWRITE "DISC\n";
  629. my $i;
  630. sysread DREAD, $i, 5;
  631. }
  632. else {
  633. logmsg "Server finds $datasockf_mode DATA connection already ".
  634. "disconnected\n";
  635. }
  636. }
  637. else {
  638. logmsg "Server knows $datasockf_mode DATA connection is already ".
  639. "disconnected\n";
  640. }
  641. if($datapid > 0) {
  642. print DWRITE "QUIT\n";
  643. waitpid($datapid, 0);
  644. unlink($datasockf_pidfile) if(-f $datasockf_pidfile);
  645. logmsg "DATA sockfilt for $datasockf_mode data channel quits ".
  646. "(pid $datapid)\n";
  647. }
  648. else {
  649. logmsg "DATA sockfilt for $datasockf_mode data channel already ".
  650. "dead\n";
  651. }
  652. logmsg "=====> Closed $datasockf_mode DATA connection\n";
  653. datasockf_state('STOPPED');
  654. }
  655. ################
  656. ################ SMTP commands
  657. ################
  658. # The type of server (SMTP or ESMTP)
  659. my $smtp_type;
  660. # The client (which normally contains the test number)
  661. my $smtp_client;
  662. sub EHLO_smtp {
  663. my ($client) = @_;
  664. my @data;
  665. # TODO: Get the IP address of the client connection to use in the
  666. # EHLO response when the client doesn't specify one but for now use
  667. # 127.0.0.1
  668. if(!$client) {
  669. $client = "[127.0.0.1]";
  670. }
  671. # Set the server type to ESMTP
  672. $smtp_type = "ESMTP";
  673. # Calculate the EHLO response
  674. push @data, "$smtp_type pingpong test server Hello $client";
  675. if((@capabilities) || (@auth_mechs)) {
  676. my $mechs;
  677. for my $c (@capabilities) {
  678. push @data, $c;
  679. }
  680. for my $am (@auth_mechs) {
  681. if(!$mechs) {
  682. $mechs = "$am";
  683. }
  684. else {
  685. $mechs .= " $am";
  686. }
  687. }
  688. if($mechs) {
  689. push @data, "AUTH $mechs";
  690. }
  691. }
  692. # Send the EHLO response
  693. for(my $i = 0; $i < @data; $i++) {
  694. my $d = $data[$i];
  695. if($i < @data - 1) {
  696. sendcontrol "250-$d\r\n";
  697. }
  698. else {
  699. sendcontrol "250 $d\r\n";
  700. }
  701. }
  702. # Store the client (as it may contain the test number)
  703. $smtp_client = $client;
  704. return 0;
  705. }
  706. sub HELO_smtp {
  707. my ($client) = @_;
  708. # TODO: Get the IP address of the client connection to use in the HELO
  709. # response when the client doesn't specify one but for now use 127.0.0.1
  710. if(!$client) {
  711. $client = "[127.0.0.1]";
  712. }
  713. # Set the server type to SMTP
  714. $smtp_type = "SMTP";
  715. # Send the HELO response
  716. sendcontrol "250 $smtp_type pingpong test server Hello $client\r\n";
  717. # Store the client (as it may contain the test number)
  718. $smtp_client = $client;
  719. return 0;
  720. }
  721. sub MAIL_smtp {
  722. my ($args) = @_;
  723. logmsg "MAIL_smtp got $args\n";
  724. if (!$args) {
  725. sendcontrol "501 Unrecognized parameter\r\n";
  726. }
  727. else {
  728. my $from;
  729. my $size;
  730. my @elements = split(/ /, $args);
  731. # Get the FROM and SIZE parameters
  732. for my $e (@elements) {
  733. if($e =~ /^FROM:(.*)$/) {
  734. $from = $1;
  735. }
  736. elsif($e =~ /^SIZE=(\d+)$/) {
  737. $size = $1;
  738. }
  739. }
  740. # Validate the from address (only <> and a valid email address inside
  741. # <> are allowed, such as <user@example.com>)
  742. if ((!$from) || (($from ne "<>") && ($from !~
  743. /^<([a-zA-Z0-9._%+-]+)\@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})>$/))) {
  744. sendcontrol "501 Invalid address\r\n";
  745. }
  746. else {
  747. my @found;
  748. my $valid = 1;
  749. # Check the capabilities for SIZE and if the specified size is
  750. # greater than the message size then reject it
  751. if (@found = grep /^SIZE (\d+)$/, @capabilities) {
  752. if ($found[0] =~ /^SIZE (\d+)$/) {
  753. if ($size > $1) {
  754. $valid = 0;
  755. }
  756. }
  757. }
  758. if(!$valid) {
  759. sendcontrol "552 Message size too large\r\n";
  760. }
  761. else {
  762. sendcontrol "250 Sender OK\r\n";
  763. }
  764. }
  765. }
  766. return 0;
  767. }
  768. sub RCPT_smtp {
  769. my ($args) = @_;
  770. logmsg "RCPT_smtp got $args\n";
  771. # Get the TO parameter
  772. if($args !~ /^TO:(.*)/) {
  773. sendcontrol "501 Unrecognized parameter\r\n";
  774. }
  775. else {
  776. my $to = $1;
  777. # Validate the to address (only a valid email address inside <> is
  778. # allowed, such as <user@example.com>)
  779. if ($to !~
  780. /^<([a-zA-Z0-9._%+-]+)\@([a-zA-Z0-9.-]+).([a-zA-Z]{2,4})>$/) {
  781. sendcontrol "501 Invalid address\r\n";
  782. }
  783. else {
  784. sendcontrol "250 Recipient OK\r\n";
  785. }
  786. }
  787. return 0;
  788. }
  789. sub DATA_smtp {
  790. my ($args) = @_;
  791. if ($args) {
  792. sendcontrol "501 Unrecognized parameter\r\n";
  793. }
  794. elsif ($smtp_client !~ /^(\d*)$/) {
  795. sendcontrol "501 Invalid arguments\r\n";
  796. }
  797. else {
  798. sendcontrol "354 Show me the mail\r\n";
  799. my $testno = $smtp_client;
  800. my $filename = "log/upload.$testno";
  801. logmsg "Store test number $testno in $filename\n";
  802. open(FILE, ">$filename") ||
  803. return 0; # failed to open output
  804. my $line;
  805. my $ulsize=0;
  806. my $disc=0;
  807. my $raw;
  808. while (5 == (sysread \*SFREAD, $line, 5)) {
  809. if($line eq "DATA\n") {
  810. my $i;
  811. my $eob;
  812. sysread \*SFREAD, $i, 5;
  813. my $size = 0;
  814. if($i =~ /^([0-9a-fA-F]{4})\n/) {
  815. $size = hex($1);
  816. }
  817. read_mainsockf(\$line, $size);
  818. $ulsize += $size;
  819. print FILE $line if(!$nosave);
  820. $raw .= $line;
  821. if($raw =~ /\x0d\x0a\x2e\x0d\x0a/) {
  822. # end of data marker!
  823. $eob = 1;
  824. }
  825. logmsg "> Appending $size bytes to file\n";
  826. if($eob) {
  827. logmsg "Found SMTP EOB marker\n";
  828. last;
  829. }
  830. }
  831. elsif($line eq "DISC\n") {
  832. # disconnect!
  833. $disc=1;
  834. last;
  835. }
  836. else {
  837. logmsg "No support for: $line";
  838. last;
  839. }
  840. }
  841. if($nosave) {
  842. print FILE "$ulsize bytes would've been stored here\n";
  843. }
  844. close(FILE);
  845. logmsg "received $ulsize bytes upload\n";
  846. sendcontrol "250 OK, data received!\r\n";
  847. }
  848. return 0;
  849. }
  850. sub NOOP_smtp {
  851. my ($args) = @_;
  852. if($args) {
  853. sendcontrol "501 Unrecognized parameter\r\n";
  854. }
  855. else {
  856. sendcontrol "250 OK\r\n";
  857. }
  858. return 0;
  859. }
  860. sub RSET_smtp {
  861. my ($args) = @_;
  862. if($args) {
  863. sendcontrol "501 Unrecognized parameter\r\n";
  864. }
  865. else {
  866. sendcontrol "250 Resetting\r\n";
  867. }
  868. return 0;
  869. }
  870. sub HELP_smtp {
  871. my ($args) = @_;
  872. # One argument is optional
  873. if($args) {
  874. logmsg "HELP_smtp got $args\n";
  875. }
  876. if($smtp_client eq "verifiedserver") {
  877. # This is the secret command that verifies that this actually is
  878. # the curl test server
  879. sendcontrol "214 WE ROOLZ: $$\r\n";
  880. if($verbose) {
  881. print STDERR "FTPD: We returned proof we are the test server\n";
  882. }
  883. logmsg "return proof we are we\n";
  884. }
  885. else {
  886. sendcontrol "214-This server supports the following commands:\r\n";
  887. if(@auth_mechs) {
  888. sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP AUTH\r\n";
  889. }
  890. else {
  891. sendcontrol "214 HELO EHLO RCPT DATA RSET MAIL VRFY EXPN QUIT HELP\r\n";
  892. }
  893. }
  894. return 0;
  895. }
  896. sub VRFY_smtp {
  897. my ($args) = @_;
  898. my ($username, $address) = split(/ /, $args, 2);
  899. logmsg "VRFY_smtp got $args\n";
  900. if($username eq "") {
  901. sendcontrol "501 Unrecognized parameter\r\n";
  902. }
  903. else {
  904. my @data = getreplydata($smtp_client);
  905. for my $d (@data) {
  906. sendcontrol $d;
  907. }
  908. }
  909. return 0;
  910. }
  911. sub EXPN_smtp {
  912. my ($list_name) = @_;
  913. logmsg "EXPN_smtp got $list_name\n";
  914. if(!$list_name) {
  915. sendcontrol "501 Unrecognized parameter\r\n";
  916. }
  917. else {
  918. my @data = getreplydata($smtp_client);
  919. for my $d (@data) {
  920. sendcontrol $d;
  921. }
  922. }
  923. return 0;
  924. }
  925. sub QUIT_smtp {
  926. sendcontrol "221 curl $smtp_type server signing off\r\n";
  927. return 0;
  928. }
  929. # What was deleted by IMAP STORE / POP3 DELE commands
  930. my @deleted;
  931. ################
  932. ################ IMAP commands
  933. ################
  934. # global to allow the command functions to read it
  935. my $cmdid;
  936. # what was picked by SELECT
  937. my $selected;
  938. # Any IMAP parameter can come in escaped and in double quotes.
  939. # This function is dumb (so far) and just removes the quotes if present.
  940. sub fix_imap_params {
  941. foreach (@_) {
  942. $_ = $1 if /^"(.*)"$/;
  943. }
  944. }
  945. sub CAPABILITY_imap {
  946. if((!@capabilities) && (!@auth_mechs)) {
  947. sendcontrol "$cmdid BAD Command\r\n";
  948. }
  949. else {
  950. my $data;
  951. # Calculate the CAPABILITY response
  952. $data = "* CAPABILITY IMAP4";
  953. for my $c (@capabilities) {
  954. $data .= " $c";
  955. }
  956. for my $am (@auth_mechs) {
  957. $data .= " AUTH=$am";
  958. }
  959. $data .= " pingpong test server\r\n";
  960. # Send the CAPABILITY response
  961. sendcontrol $data;
  962. sendcontrol "$cmdid OK CAPABILITY completed\r\n";
  963. }
  964. return 0;
  965. }
  966. sub LOGIN_imap {
  967. my ($args) = @_;
  968. my ($user, $password) = split(/ /, $args, 2);
  969. fix_imap_params($user, $password);
  970. logmsg "LOGIN_imap got $args\n";
  971. if ($user eq "") {
  972. sendcontrol "$cmdid BAD Command Argument\r\n";
  973. }
  974. elsif (($user ne $TEXT_USERNAME) || ($password ne $TEXT_PASSWORD)) {
  975. sendcontrol "$cmdid NO LOGIN failed\r\n";
  976. }
  977. else {
  978. sendcontrol "$cmdid OK LOGIN completed\r\n";
  979. }
  980. return 0;
  981. }
  982. sub SELECT_imap {
  983. my ($mailbox) = @_;
  984. fix_imap_params($mailbox);
  985. logmsg "SELECT_imap got test $mailbox\n";
  986. if($mailbox eq "") {
  987. sendcontrol "$cmdid BAD Command Argument\r\n";
  988. }
  989. else {
  990. # Example from RFC 3501, 6.3.1. SELECT Command
  991. sendcontrol "* 172 EXISTS\r\n";
  992. sendcontrol "* 1 RECENT\r\n";
  993. sendcontrol "* OK [UNSEEN 12] Message 12 is first unseen\r\n";
  994. sendcontrol "* OK [UIDVALIDITY 3857529045] UIDs valid\r\n";
  995. sendcontrol "* OK [UIDNEXT 4392] Predicted next UID\r\n";
  996. sendcontrol "* FLAGS (\\Answered \\Flagged \\Deleted \\Seen \\Draft)\r\n";
  997. sendcontrol "* OK [PERMANENTFLAGS (\\Deleted \\Seen \\*)] Limited\r\n";
  998. sendcontrol "$cmdid OK [READ-WRITE] SELECT completed\r\n";
  999. $selected = $mailbox;
  1000. }
  1001. return 0;
  1002. }
  1003. sub FETCH_imap {
  1004. my ($args) = @_;
  1005. my ($uid, $how) = split(/ /, $args, 2);
  1006. fix_imap_params($uid, $how);
  1007. logmsg "FETCH_imap got $args\n";
  1008. if ($selected eq "") {
  1009. sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
  1010. }
  1011. else {
  1012. my @data;
  1013. my $size;
  1014. if($selected eq "verifiedserver") {
  1015. # this is the secret command that verifies that this actually is
  1016. # the curl test server
  1017. my $response = "WE ROOLZ: $$\r\n";
  1018. if($verbose) {
  1019. print STDERR "FTPD: We returned proof we are the test server\n";
  1020. }
  1021. $data[0] = $response;
  1022. logmsg "return proof we are we\n";
  1023. }
  1024. else {
  1025. # send mail content
  1026. logmsg "retrieve a mail\n";
  1027. @data = getreplydata($selected);
  1028. }
  1029. for (@data) {
  1030. $size += length($_);
  1031. }
  1032. sendcontrol "* $uid FETCH ($how {$size}\r\n";
  1033. for my $d (@data) {
  1034. sendcontrol $d;
  1035. }
  1036. sendcontrol ")\r\n";
  1037. sendcontrol "$cmdid OK FETCH completed\r\n";
  1038. }
  1039. return 0;
  1040. }
  1041. sub APPEND_imap {
  1042. my ($args) = @_;
  1043. logmsg "APPEND_imap got $args\r\n";
  1044. $args =~ /^([^ ]+) [^{]*\{(\d+)\}$/;
  1045. my ($mailbox, $size) = ($1, $2);
  1046. fix_imap_params($mailbox);
  1047. if($mailbox eq "") {
  1048. sendcontrol "$cmdid BAD Command Argument\r\n";
  1049. }
  1050. else {
  1051. sendcontrol "+ Ready for literal data\r\n";
  1052. my $testno = $mailbox;
  1053. my $filename = "log/upload.$testno";
  1054. logmsg "Store test number $testno in $filename\n";
  1055. open(FILE, ">$filename") ||
  1056. return 0; # failed to open output
  1057. my $received = 0;
  1058. my $line;
  1059. while(5 == (sysread \*SFREAD, $line, 5)) {
  1060. if($line eq "DATA\n") {
  1061. sysread \*SFREAD, $line, 5;
  1062. my $chunksize = 0;
  1063. if($line =~ /^([0-9a-fA-F]{4})\n/) {
  1064. $chunksize = hex($1);
  1065. }
  1066. read_mainsockf(\$line, $chunksize);
  1067. my $left = $size - $received;
  1068. my $datasize = ($left > $chunksize) ? $chunksize : $left;
  1069. if($datasize > 0) {
  1070. logmsg "> Appending $datasize bytes to file\n";
  1071. print FILE substr($line, 0, $datasize) if(!$nosave);
  1072. $line = substr($line, $datasize);
  1073. $received += $datasize;
  1074. if($received == $size) {
  1075. logmsg "Received all data, waiting for final CRLF.\n";
  1076. }
  1077. }
  1078. if($received == $size && $line eq "\r\n") {
  1079. last;
  1080. }
  1081. }
  1082. elsif($line eq "DISC\n") {
  1083. logmsg "Unexpected disconnect!\n";
  1084. last;
  1085. }
  1086. else {
  1087. logmsg "No support for: $line";
  1088. last;
  1089. }
  1090. }
  1091. if($nosave) {
  1092. print FILE "$size bytes would've been stored here\n";
  1093. }
  1094. close(FILE);
  1095. logmsg "received $size bytes upload\n";
  1096. sendcontrol "$cmdid OK APPEND completed\r\n";
  1097. }
  1098. return 0;
  1099. }
  1100. sub STORE_imap {
  1101. my ($args) = @_;
  1102. my ($uid, $what, $value) = split(/ /, $args, 3);
  1103. fix_imap_params($uid);
  1104. logmsg "STORE_imap got $args\n";
  1105. if ($selected eq "") {
  1106. sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
  1107. }
  1108. elsif (($uid eq "") || ($what ne "+Flags") || ($value eq "")) {
  1109. sendcontrol "$cmdid BAD Command Argument\r\n";
  1110. }
  1111. else {
  1112. if($value eq "\\Deleted") {
  1113. push(@deleted, $uid);
  1114. }
  1115. sendcontrol "* $uid FETCH (FLAGS (\\Seen $value))\r\n";
  1116. sendcontrol "$cmdid OK STORE completed\r\n";
  1117. }
  1118. return 0;
  1119. }
  1120. sub LIST_imap {
  1121. my ($args) = @_;
  1122. my ($reference, $mailbox) = split(/ /, $args, 2);
  1123. fix_imap_params($reference, $mailbox);
  1124. logmsg "LIST_imap got $args\n";
  1125. if ($reference eq "") {
  1126. sendcontrol "$cmdid BAD Command Argument\r\n";
  1127. }
  1128. elsif ($reference eq "verifiedserver") {
  1129. # this is the secret command that verifies that this actually is
  1130. # the curl test server
  1131. sendcontrol "* LIST () \"/\" \"WE ROOLZ: $$\"\r\n";
  1132. sendcontrol "$cmdid OK LIST Completed\r\n";
  1133. if($verbose) {
  1134. print STDERR "FTPD: We returned proof we are the test server\n";
  1135. }
  1136. logmsg "return proof we are we\n";
  1137. }
  1138. else {
  1139. my @data = getreplydata($reference);
  1140. for my $d (@data) {
  1141. sendcontrol $d;
  1142. }
  1143. sendcontrol "$cmdid OK LIST Completed\r\n";
  1144. }
  1145. return 0;
  1146. }
  1147. sub LSUB_imap {
  1148. my ($args) = @_;
  1149. my ($reference, $mailbox) = split(/ /, $args, 2);
  1150. fix_imap_params($reference, $mailbox);
  1151. logmsg "LSUB_imap got $args\n";
  1152. if ($reference eq "") {
  1153. sendcontrol "$cmdid BAD Command Argument\r\n";
  1154. }
  1155. else {
  1156. my @data = getreplydata($reference);
  1157. for my $d (@data) {
  1158. sendcontrol $d;
  1159. }
  1160. sendcontrol "$cmdid OK LSUB Completed\r\n";
  1161. }
  1162. return 0;
  1163. }
  1164. sub EXAMINE_imap {
  1165. my ($mailbox) = @_;
  1166. fix_imap_params($mailbox);
  1167. logmsg "EXAMINE_imap got $mailbox\n";
  1168. if ($mailbox eq "") {
  1169. sendcontrol "$cmdid BAD Command Argument\r\n";
  1170. }
  1171. else {
  1172. my @data = getreplydata($mailbox);
  1173. for my $d (@data) {
  1174. sendcontrol $d;
  1175. }
  1176. sendcontrol "$cmdid OK [READ-ONLY] EXAMINE completed\r\n";
  1177. }
  1178. return 0;
  1179. }
  1180. sub STATUS_imap {
  1181. my ($args) = @_;
  1182. my ($mailbox, $what) = split(/ /, $args, 2);
  1183. fix_imap_params($mailbox);
  1184. logmsg "STATUS_imap got $args\n";
  1185. if ($mailbox eq "") {
  1186. sendcontrol "$cmdid BAD Command Argument\r\n";
  1187. }
  1188. else {
  1189. my @data = getreplydata($mailbox);
  1190. for my $d (@data) {
  1191. sendcontrol $d;
  1192. }
  1193. sendcontrol "$cmdid OK STATUS completed\r\n";
  1194. }
  1195. return 0;
  1196. }
  1197. sub SEARCH_imap {
  1198. my ($what) = @_;
  1199. fix_imap_params($what);
  1200. logmsg "SEARCH_imap got $what\n";
  1201. if ($selected eq "") {
  1202. sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
  1203. }
  1204. elsif ($what eq "") {
  1205. sendcontrol "$cmdid BAD Command Argument\r\n";
  1206. }
  1207. else {
  1208. my @data = getreplydata($selected);
  1209. for my $d (@data) {
  1210. sendcontrol $d;
  1211. }
  1212. sendcontrol "$cmdid OK SEARCH completed\r\n";
  1213. }
  1214. return 0;
  1215. }
  1216. sub CREATE_imap {
  1217. my ($args) = @_;
  1218. fix_imap_params($args);
  1219. logmsg "CREATE_imap got $args\n";
  1220. if ($args eq "") {
  1221. sendcontrol "$cmdid BAD Command Argument\r\n";
  1222. }
  1223. else {
  1224. sendcontrol "$cmdid OK CREATE completed\r\n";
  1225. }
  1226. return 0;
  1227. }
  1228. sub DELETE_imap {
  1229. my ($args) = @_;
  1230. fix_imap_params($args);
  1231. logmsg "DELETE_imap got $args\n";
  1232. if ($args eq "") {
  1233. sendcontrol "$cmdid BAD Command Argument\r\n";
  1234. }
  1235. else {
  1236. sendcontrol "$cmdid OK DELETE completed\r\n";
  1237. }
  1238. return 0;
  1239. }
  1240. sub RENAME_imap {
  1241. my ($args) = @_;
  1242. my ($from_mailbox, $to_mailbox) = split(/ /, $args, 2);
  1243. fix_imap_params($from_mailbox, $to_mailbox);
  1244. logmsg "RENAME_imap got $args\n";
  1245. if (($from_mailbox eq "") || ($to_mailbox eq "")) {
  1246. sendcontrol "$cmdid BAD Command Argument\r\n";
  1247. }
  1248. else {
  1249. sendcontrol "$cmdid OK RENAME completed\r\n";
  1250. }
  1251. return 0;
  1252. }
  1253. sub CHECK_imap {
  1254. if ($selected eq "") {
  1255. sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
  1256. }
  1257. else {
  1258. sendcontrol "$cmdid OK CHECK completed\r\n";
  1259. }
  1260. return 0;
  1261. }
  1262. sub CLOSE_imap {
  1263. if ($selected eq "") {
  1264. sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
  1265. }
  1266. elsif (!@deleted) {
  1267. sendcontrol "$cmdid BAD Command Argument\r\n";
  1268. }
  1269. else {
  1270. sendcontrol "$cmdid OK CLOSE completed\r\n";
  1271. @deleted = ();
  1272. }
  1273. return 0;
  1274. }
  1275. sub EXPUNGE_imap {
  1276. if ($selected eq "") {
  1277. sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
  1278. }
  1279. else {
  1280. if (!@deleted) {
  1281. # Report the number of existing messages as per the SELECT
  1282. # command
  1283. sendcontrol "* 172 EXISTS\r\n";
  1284. }
  1285. else {
  1286. # Report the message UIDs being deleted
  1287. for my $d (@deleted) {
  1288. sendcontrol "* $d EXPUNGE\r\n";
  1289. }
  1290. @deleted = ();
  1291. }
  1292. sendcontrol "$cmdid OK EXPUNGE completed\r\n";
  1293. }
  1294. return 0;
  1295. }
  1296. sub COPY_imap {
  1297. my ($args) = @_;
  1298. my ($uid, $mailbox) = split(/ /, $args, 2);
  1299. fix_imap_params($uid, $mailbox);
  1300. logmsg "COPY_imap got $args\n";
  1301. if (($uid eq "") || ($mailbox eq "")) {
  1302. sendcontrol "$cmdid BAD Command Argument\r\n";
  1303. }
  1304. else {
  1305. sendcontrol "$cmdid OK COPY completed\r\n";
  1306. }
  1307. return 0;
  1308. }
  1309. sub UID_imap {
  1310. my ($args) = @_;
  1311. my ($command) = split(/ /, $args, 1);
  1312. fix_imap_params($command);
  1313. logmsg "UID_imap got $args\n";
  1314. if ($selected eq "") {
  1315. sendcontrol "$cmdid BAD Command received in Invalid state\r\n";
  1316. }
  1317. elsif (($command ne "COPY") && ($command ne "FETCH") &&
  1318. ($command ne "STORE") && ($command ne "SEARCH")) {
  1319. sendcontrol "$cmdid BAD Command Argument\r\n";
  1320. }
  1321. else {
  1322. my @data = getreplydata($selected);
  1323. for my $d (@data) {
  1324. sendcontrol $d;
  1325. }
  1326. sendcontrol "$cmdid OK $command completed\r\n";
  1327. }
  1328. return 0;
  1329. }
  1330. sub NOOP_imap {
  1331. my ($args) = @_;
  1332. my @data = (
  1333. "* 22 EXPUNGE\r\n",
  1334. "* 23 EXISTS\r\n",
  1335. "* 3 RECENT\r\n",
  1336. "* 14 FETCH (FLAGS (\\Seen \\Deleted))\r\n",
  1337. );
  1338. if ($args) {
  1339. sendcontrol "$cmdid BAD Command Argument\r\n";
  1340. }
  1341. else {
  1342. for my $d (@data) {
  1343. sendcontrol $d;
  1344. }
  1345. sendcontrol "$cmdid OK NOOP completed\r\n";
  1346. }
  1347. return 0;
  1348. }
  1349. sub LOGOUT_imap {
  1350. sendcontrol "* BYE curl IMAP server signing off\r\n";
  1351. sendcontrol "$cmdid OK LOGOUT completed\r\n";
  1352. return 0;
  1353. }
  1354. ################
  1355. ################ POP3 commands
  1356. ################
  1357. # Who is attempting to log in
  1358. my $username;
  1359. sub CAPA_pop3 {
  1360. my @list = ();
  1361. my $mechs;
  1362. # Calculate the capability list based on the specified capabilities
  1363. # (except APOP) and any authentication mechanisms
  1364. for my $c (@capabilities) {
  1365. push @list, "$c\r\n" unless $c eq "APOP";
  1366. }
  1367. for my $am (@auth_mechs) {
  1368. if(!$mechs) {
  1369. $mechs = "$am";
  1370. }
  1371. else {
  1372. $mechs .= " $am";
  1373. }
  1374. }
  1375. if($mechs) {
  1376. push @list, "SASL $mechs\r\n";
  1377. }
  1378. if(!@list) {
  1379. sendcontrol "-ERR Unrecognized command\r\n";
  1380. }
  1381. else {
  1382. my @data = ();
  1383. # Calculate the CAPA response
  1384. push @data, "+OK List of capabilities follows\r\n";
  1385. for my $l (@list) {
  1386. push @data, "$l\r\n";
  1387. }
  1388. push @data, "IMPLEMENTATION POP3 pingpong test server\r\n";
  1389. # Send the CAPA response
  1390. for my $d (@data) {
  1391. sendcontrol $d;
  1392. }
  1393. # End with the magic 3-byte end of listing marker
  1394. sendcontrol ".\r\n";
  1395. }
  1396. return 0;
  1397. }
  1398. sub APOP_pop3 {
  1399. my ($args) = @_;
  1400. my ($user, $secret) = split(/ /, $args, 2);
  1401. if (!grep /^APOP$/, @capabilities) {
  1402. sendcontrol "-ERR Unrecognized command\r\n";
  1403. }
  1404. elsif (($user eq "") || ($secret eq "")) {
  1405. sendcontrol "-ERR Protocol error\r\n";
  1406. }
  1407. else {
  1408. my $digest = Digest::MD5::md5_hex($POP3_TIMESTAMP, $TEXT_PASSWORD);
  1409. if (($user ne $TEXT_USERNAME) || ($secret ne $digest)) {
  1410. sendcontrol "-ERR Login failure\r\n";
  1411. }
  1412. else {
  1413. sendcontrol "+OK Login successful\r\n";
  1414. }
  1415. }
  1416. return 0;
  1417. }
  1418. sub AUTH_pop3 {
  1419. if(!@auth_mechs) {
  1420. sendcontrol "-ERR Unrecognized command\r\n";
  1421. }
  1422. else {
  1423. my @data = ();
  1424. # Calculate the AUTH response
  1425. push @data, "+OK List of supported mechanisms follows\r\n";
  1426. for my $am (@auth_mechs) {
  1427. push @data, "$am\r\n";
  1428. }
  1429. # Send the AUTH response
  1430. for my $d (@data) {
  1431. sendcontrol $d;
  1432. }
  1433. # End with the magic 3-byte end of listing marker
  1434. sendcontrol ".\r\n";
  1435. }
  1436. return 0;
  1437. }
  1438. sub USER_pop3 {
  1439. my ($user) = @_;
  1440. logmsg "USER_pop3 got $user\n";
  1441. if (!$user) {
  1442. sendcontrol "-ERR Protocol error\r\n";
  1443. }
  1444. else {
  1445. $username = $user;
  1446. sendcontrol "+OK\r\n";
  1447. }
  1448. return 0;
  1449. }
  1450. sub PASS_pop3 {
  1451. my ($password) = @_;
  1452. logmsg "PASS_pop3 got $password\n";
  1453. if (($username ne $TEXT_USERNAME) || ($password ne $TEXT_PASSWORD)) {
  1454. sendcontrol "-ERR Login failure\r\n";
  1455. }
  1456. else {
  1457. sendcontrol "+OK Login successful\r\n";
  1458. }
  1459. return 0;
  1460. }
  1461. sub RETR_pop3 {
  1462. my ($msgid) = @_;
  1463. my @data;
  1464. if($msgid =~ /^verifiedserver$/) {
  1465. # this is the secret command that verifies that this actually is
  1466. # the curl test server
  1467. my $response = "WE ROOLZ: $$\r\n";
  1468. if($verbose) {
  1469. print STDERR "FTPD: We returned proof we are the test server\n";
  1470. }
  1471. $data[0] = $response;
  1472. logmsg "return proof we are we\n";
  1473. }
  1474. else {
  1475. # send mail content
  1476. logmsg "retrieve a mail\n";
  1477. @data = getreplydata($msgid);
  1478. }
  1479. sendcontrol "+OK Mail transfer starts\r\n";
  1480. for my $d (@data) {
  1481. sendcontrol $d;
  1482. }
  1483. # end with the magic 3-byte end of mail marker, assumes that the
  1484. # mail body ends with a CRLF!
  1485. sendcontrol ".\r\n";
  1486. return 0;
  1487. }
  1488. sub LIST_pop3 {
  1489. # This is a built-in fake-message list
  1490. my @data = (
  1491. "1 100\r\n",
  1492. "2 4294967400\r\n", # > 4 GB
  1493. "3 200\r\n",
  1494. );
  1495. logmsg "retrieve a message list\n";
  1496. sendcontrol "+OK Listing starts\r\n";
  1497. for my $d (@data) {
  1498. sendcontrol $d;
  1499. }
  1500. # End with the magic 3-byte end of listing marker
  1501. sendcontrol ".\r\n";
  1502. return 0;
  1503. }
  1504. sub DELE_pop3 {
  1505. my ($msgid) = @_;
  1506. logmsg "DELE_pop3 got $msgid\n";
  1507. if (!$msgid) {
  1508. sendcontrol "-ERR Protocol error\r\n";
  1509. }
  1510. else {
  1511. push (@deleted, $msgid);
  1512. sendcontrol "+OK\r\n";
  1513. }
  1514. return 0;
  1515. }
  1516. sub STAT_pop3 {
  1517. my ($args) = @_;
  1518. if ($args) {
  1519. sendcontrol "-ERR Protocol error\r\n";
  1520. }
  1521. else {
  1522. # Send statistics for the built-in fake message list as
  1523. # detailed in the LIST_pop3 function above
  1524. sendcontrol "+OK 3 4294967800\r\n";
  1525. }
  1526. return 0;
  1527. }
  1528. sub NOOP_pop3 {
  1529. my ($args) = @_;
  1530. if ($args) {
  1531. sendcontrol "-ERR Protocol error\r\n";
  1532. }
  1533. else {
  1534. sendcontrol "+OK\r\n";
  1535. }
  1536. return 0;
  1537. }
  1538. sub UIDL_pop3 {
  1539. # This is a built-in fake-message UID list
  1540. my @data = (
  1541. "1 1\r\n",
  1542. "2 2\r\n",
  1543. "3 4\r\n", # Note that UID 3 is a simulated "deleted" message
  1544. );
  1545. if (!grep /^UIDL$/, @capabilities) {
  1546. sendcontrol "-ERR Unrecognized command\r\n";
  1547. }
  1548. else {
  1549. logmsg "retrieve a message UID list\n";
  1550. sendcontrol "+OK Listing starts\r\n";
  1551. for my $d (@data) {
  1552. sendcontrol $d;
  1553. }
  1554. # End with the magic 3-byte end of listing marker
  1555. sendcontrol ".\r\n";
  1556. }
  1557. return 0;
  1558. }
  1559. sub TOP_pop3 {
  1560. my ($args) = @_;
  1561. my ($msgid, $lines) = split(/ /, $args, 2);
  1562. logmsg "TOP_pop3 got $args\n";
  1563. if (!grep /^TOP$/, @capabilities) {
  1564. sendcontrol "-ERR Unrecognized command\r\n";
  1565. }
  1566. elsif (($msgid eq "") || ($lines eq "")) {
  1567. sendcontrol "-ERR Protocol error\r\n";
  1568. }
  1569. else {
  1570. if ($lines == "0") {
  1571. logmsg "retrieve header of mail\n";
  1572. }
  1573. else {
  1574. logmsg "retrieve top $lines lines of mail\n";
  1575. }
  1576. my @data = getreplydata($msgid);
  1577. sendcontrol "+OK Mail transfer starts\r\n";
  1578. # Send mail content
  1579. for my $d (@data) {
  1580. sendcontrol $d;
  1581. }
  1582. # End with the magic 3-byte end of mail marker, assumes that the
  1583. # mail body ends with a CRLF!
  1584. sendcontrol ".\r\n";
  1585. }
  1586. return 0;
  1587. }
  1588. sub RSET_pop3 {
  1589. my ($args) = @_;
  1590. if ($args) {
  1591. sendcontrol "-ERR Protocol error\r\n";
  1592. }
  1593. else {
  1594. if (@deleted) {
  1595. logmsg "resetting @deleted message(s)\n";
  1596. @deleted = ();
  1597. }
  1598. sendcontrol "+OK\r\n";
  1599. }
  1600. return 0;
  1601. }
  1602. sub QUIT_pop3 {
  1603. if(@deleted) {
  1604. logmsg "deleting @deleted message(s)\n";
  1605. @deleted = ();
  1606. }
  1607. sendcontrol "+OK curl POP3 server signing off\r\n";
  1608. return 0;
  1609. }
  1610. ################
  1611. ################ FTP commands
  1612. ################
  1613. my $rest=0;
  1614. sub REST_ftp {
  1615. $rest = $_[0];
  1616. logmsg "Set REST position to $rest\n"
  1617. }
  1618. sub switch_directory_goto {
  1619. my $target_dir = $_;
  1620. if(!$ftptargetdir) {
  1621. $ftptargetdir = "/";
  1622. }
  1623. if($target_dir eq "") {
  1624. $ftptargetdir = "/";
  1625. }
  1626. elsif($target_dir eq "..") {
  1627. if($ftptargetdir eq "/") {
  1628. $ftptargetdir = "/";
  1629. }
  1630. else {
  1631. $ftptargetdir =~ s/[[:alnum:]]+\/$//;
  1632. }
  1633. }
  1634. else {
  1635. $ftptargetdir .= $target_dir . "/";
  1636. }
  1637. }
  1638. sub switch_directory {
  1639. my $target_dir = $_[0];
  1640. if($target_dir =~ /^test-(\d+)/) {
  1641. $cwd_testno = $1;
  1642. }
  1643. elsif($target_dir eq "/") {
  1644. $ftptargetdir = "/";
  1645. }
  1646. else {
  1647. my @dirs = split("/", $target_dir);
  1648. for(@dirs) {
  1649. switch_directory_goto($_);
  1650. }
  1651. }
  1652. }
  1653. sub CWD_ftp {
  1654. my ($folder, $fullcommand) = $_[0];
  1655. switch_directory($folder);
  1656. if($ftptargetdir =~ /^\/fully_simulated/) {
  1657. $ftplistparserstate = "enabled";
  1658. }
  1659. else {
  1660. undef $ftplistparserstate;
  1661. }
  1662. }
  1663. sub PWD_ftp {
  1664. my $mydir;
  1665. $mydir = $ftptargetdir ? $ftptargetdir : "/";
  1666. if($mydir ne "/") {
  1667. $mydir =~ s/\/$//;
  1668. }
  1669. sendcontrol "257 \"$mydir\" is current directory\r\n";
  1670. }
  1671. sub LIST_ftp {
  1672. # print "150 ASCII data connection for /bin/ls (193.15.23.1,59196) (0 bytes)\r\n";
  1673. # this is a built-in fake-dir ;-)
  1674. my @ftpdir=("total 20\r\n",
  1675. "drwxr-xr-x 8 98 98 512 Oct 22 13:06 .\r\n",
  1676. "drwxr-xr-x 8 98 98 512 Oct 22 13:06 ..\r\n",
  1677. "drwxr-xr-x 2 98 98 512 May 2 1996 .NeXT\r\n",
  1678. "-r--r--r-- 1 0 1 35 Jul 16 1996 README\r\n",
  1679. "lrwxrwxrwx 1 0 1 7 Dec 9 1999 bin -> usr/bin\r\n",
  1680. "dr-xr-xr-x 2 0 1 512 Oct 1 1997 dev\r\n",
  1681. "drwxrwxrwx 2 98 98 512 May 29 16:04 download.html\r\n",
  1682. "dr-xr-xr-x 2 0 1 512 Nov 30 1995 etc\r\n",
  1683. "drwxrwxrwx 2 98 1 512 Oct 30 14:33 pub\r\n",
  1684. "dr-xr-xr-x 5 0 1 512 Oct 1 1997 usr\r\n");
  1685. if($datasockf_conn eq 'no') {
  1686. if($nodataconn425) {
  1687. sendcontrol "150 Opening data connection\r\n";
  1688. sendcontrol "425 Can't open data connection\r\n";
  1689. }
  1690. elsif($nodataconn421) {
  1691. sendcontrol "150 Opening data connection\r\n";
  1692. sendcontrol "421 Connection timed out\r\n";
  1693. }
  1694. elsif($nodataconn150) {
  1695. sendcontrol "150 Opening data connection\r\n";
  1696. # client shall timeout
  1697. }
  1698. else {
  1699. # client shall timeout
  1700. }
  1701. return 0;
  1702. }
  1703. if($ftplistparserstate) {
  1704. @ftpdir = ftp_contentlist($ftptargetdir);
  1705. }
  1706. logmsg "pass LIST data on data connection\n";
  1707. if($cwd_testno) {
  1708. loadtest("$srcdir/data/test$cwd_testno");
  1709. my @data = getpart("reply", "data");
  1710. for(@data) {
  1711. my $send = $_;
  1712. # convert all \n to \r\n for ASCII transfer
  1713. $send =~ s/\r\n/\n/g;
  1714. $send =~ s/\n/\r\n/g;
  1715. logmsg "send $send as data\n";
  1716. senddata $send;
  1717. }
  1718. $cwd_testno = 0; # forget it again
  1719. }
  1720. else {
  1721. # old hard-coded style
  1722. for(@ftpdir) {
  1723. senddata $_;
  1724. }
  1725. }
  1726. close_dataconn(0);
  1727. sendcontrol "226 ASCII transfer complete\r\n";
  1728. return 0;
  1729. }
  1730. sub NLST_ftp {
  1731. my @ftpdir=("file", "with space", "fake", "..", " ..", "funny", "README");
  1732. if($datasockf_conn eq 'no') {
  1733. if($nodataconn425) {
  1734. sendcontrol "150 Opening data connection\r\n";
  1735. sendcontrol "425 Can't open data connection\r\n";
  1736. }
  1737. elsif($nodataconn421) {
  1738. sendcontrol "150 Opening data connection\r\n";
  1739. sendcontrol "421 Connection timed out\r\n";
  1740. }
  1741. elsif($nodataconn150) {
  1742. sendcontrol "150 Opening data connection\r\n";
  1743. # client shall timeout
  1744. }
  1745. else {
  1746. # client shall timeout
  1747. }
  1748. return 0;
  1749. }
  1750. logmsg "pass NLST data on data connection\n";
  1751. for(@ftpdir) {
  1752. senddata "$_\r\n";
  1753. }
  1754. close_dataconn(0);
  1755. sendcontrol "226 ASCII transfer complete\r\n";
  1756. return 0;
  1757. }
  1758. sub MDTM_ftp {
  1759. my $testno = $_[0];
  1760. my $testpart = "";
  1761. if ($testno > 10000) {
  1762. $testpart = $testno % 10000;
  1763. $testno = int($testno / 10000);
  1764. }
  1765. loadtest("$srcdir/data/test$testno");
  1766. my @data = getpart("reply", "mdtm");
  1767. my $reply = $data[0];
  1768. chomp $reply if($reply);
  1769. if($reply && ($reply =~ /^[+-]?\d+$/) && ($reply < 0)) {
  1770. sendcontrol "550 $testno: no such file.\r\n";
  1771. }
  1772. elsif($reply) {
  1773. sendcontrol "$reply\r\n";
  1774. }
  1775. else {
  1776. sendcontrol "500 MDTM: no such command.\r\n";
  1777. }
  1778. return 0;
  1779. }
  1780. sub SIZE_ftp {
  1781. my $testno = $_[0];
  1782. if($ftplistparserstate) {
  1783. my $size = wildcard_filesize($ftptargetdir, $testno);
  1784. if($size == -1) {
  1785. sendcontrol "550 $testno: No such file or directory.\r\n";
  1786. }
  1787. else {
  1788. sendcontrol "213 $size\r\n";
  1789. }
  1790. return 0;
  1791. }
  1792. if($testno =~ /^verifiedserver$/) {
  1793. my $response = "WE ROOLZ: $$\r\n";
  1794. my $size = length($response);
  1795. sendcontrol "213 $size\r\n";
  1796. return 0;
  1797. }
  1798. if($testno =~ /(\d+)\/?$/) {
  1799. $testno = $1;
  1800. }
  1801. else {
  1802. print STDERR "SIZE_ftp: invalid test number: $testno\n";
  1803. return 1;
  1804. }
  1805. my $testpart = "";
  1806. if($testno > 10000) {
  1807. $testpart = $testno % 10000;
  1808. $testno = int($testno / 10000);
  1809. }
  1810. loadtest("$srcdir/data/test$testno");
  1811. my @data = getpart("reply", "size");
  1812. my $size = $data[0];
  1813. if($size) {
  1814. if($size > -1) {
  1815. sendcontrol "213 $size\r\n";
  1816. }
  1817. else {
  1818. sendcontrol "550 $testno: No such file or directory.\r\n";
  1819. }
  1820. }
  1821. else {
  1822. $size=0;
  1823. @data = getpart("reply", "data$testpart");
  1824. for(@data) {
  1825. $size += length($_);
  1826. }
  1827. if($size) {
  1828. sendcontrol "213 $size\r\n";
  1829. }
  1830. else {
  1831. sendcontrol "550 $testno: No such file or directory.\r\n";
  1832. }
  1833. }
  1834. return 0;
  1835. }
  1836. sub RETR_ftp {
  1837. my ($testno) = @_;
  1838. if($datasockf_conn eq 'no') {
  1839. if($nodataconn425) {
  1840. sendcontrol "150 Opening data connection\r\n";
  1841. sendcontrol "425 Can't open data connection\r\n";
  1842. }
  1843. elsif($nodataconn421) {
  1844. sendcontrol "150 Opening data connection\r\n";
  1845. sendcontrol "421 Connection timed out\r\n";
  1846. }
  1847. elsif($nodataconn150) {
  1848. sendcontrol "150 Opening data connection\r\n";
  1849. # client shall timeout
  1850. }
  1851. else {
  1852. # client shall timeout
  1853. }
  1854. return 0;
  1855. }
  1856. if($ftplistparserstate) {
  1857. my @content = wildcard_getfile($ftptargetdir, $testno);
  1858. if($content[0] == -1) {
  1859. #file not found
  1860. }
  1861. else {
  1862. my $size = length $content[1];
  1863. sendcontrol "150 Binary data connection for $testno ($size bytes).\r\n",
  1864. senddata $content[1];
  1865. close_dataconn(0);
  1866. sendcontrol "226 File transfer complete\r\n";
  1867. }
  1868. return 0;
  1869. }
  1870. if($testno =~ /^verifiedserver$/) {
  1871. # this is the secret command that verifies that this actually is
  1872. # the curl test server
  1873. my $response = "WE ROOLZ: $$\r\n";
  1874. my $len = length($response);
  1875. sendcontrol "150 Binary junk ($len bytes).\r\n";
  1876. senddata "WE ROOLZ: $$\r\n";
  1877. close_dataconn(0);
  1878. sendcontrol "226 File transfer complete\r\n";
  1879. if($verbose) {
  1880. print STDERR "FTPD: We returned proof we are the test server\n";
  1881. }
  1882. return 0;
  1883. }
  1884. $testno =~ s/^([^0-9]*)//;
  1885. my $testpart = "";
  1886. if ($testno > 10000) {
  1887. $testpart = $testno % 10000;
  1888. $testno = int($testno / 10000);
  1889. }
  1890. loadtest("$srcdir/data/test$testno");
  1891. my @data = getpart("reply", "data$testpart");
  1892. my $size=0;
  1893. for(@data) {
  1894. $size += length($_);
  1895. }
  1896. my %hash = getpartattr("reply", "data$testpart");
  1897. if($size || $hash{'sendzero'}) {
  1898. if($rest) {
  1899. # move read pointer forward
  1900. $size -= $rest;
  1901. logmsg "REST $rest was removed from size, makes $size left\n";
  1902. $rest = 0; # reset REST offset again
  1903. }
  1904. if($retrweirdo) {
  1905. sendcontrol "150 Binary data connection for $testno () ($size bytes).\r\n",
  1906. "226 File transfer complete\r\n";
  1907. for(@data) {
  1908. my $send = $_;
  1909. senddata $send;
  1910. }
  1911. close_dataconn(0);
  1912. $retrweirdo=0; # switch off the weirdo again!
  1913. }
  1914. else {
  1915. my $sz = "($size bytes)";
  1916. if($retrnosize) {
  1917. $sz = "size?";
  1918. }
  1919. sendcontrol "150 Binary data connection for $testno () $sz.\r\n";
  1920. for(@data) {
  1921. my $send = $_;
  1922. senddata $send;
  1923. }
  1924. close_dataconn(0);
  1925. sendcontrol "226 File transfer complete\r\n";
  1926. }
  1927. }
  1928. else {
  1929. sendcontrol "550 $testno: No such file or directory.\r\n";
  1930. }
  1931. return 0;
  1932. }
  1933. sub STOR_ftp {
  1934. my $testno=$_[0];
  1935. my $filename = "log/upload.$testno";
  1936. if($datasockf_conn eq 'no') {
  1937. if($nodataconn425) {
  1938. sendcontrol "150 Opening data connection\r\n";
  1939. sendcontrol "425 Can't open data connection\r\n";
  1940. }
  1941. elsif($nodataconn421) {
  1942. sendcontrol "150 Opening data connection\r\n";
  1943. sendcontrol "421 Connection timed out\r\n";
  1944. }
  1945. elsif($nodataconn150) {
  1946. sendcontrol "150 Opening data connection\r\n";
  1947. # client shall timeout
  1948. }
  1949. else {
  1950. # client shall timeout
  1951. }
  1952. return 0;
  1953. }
  1954. logmsg "STOR test number $testno in $filename\n";
  1955. sendcontrol "125 Gimme gimme gimme!\r\n";
  1956. open(FILE, ">$filename") ||
  1957. return 0; # failed to open output
  1958. my $line;
  1959. my $ulsize=0;
  1960. my $disc=0;
  1961. while (5 == (sysread DREAD, $line, 5)) {
  1962. if($line eq "DATA\n") {
  1963. my $i;
  1964. sysread DREAD, $i, 5;
  1965. my $size = 0;
  1966. if($i =~ /^([0-9a-fA-F]{4})\n/) {
  1967. $size = hex($1);
  1968. }
  1969. read_datasockf(\$line, $size);
  1970. #print STDERR " GOT: $size bytes\n";
  1971. $ulsize += $size;
  1972. print FILE $line if(!$nosave);
  1973. logmsg "> Appending $size bytes to file\n";
  1974. }
  1975. elsif($line eq "DISC\n") {
  1976. # disconnect!
  1977. $disc=1;
  1978. last;
  1979. }
  1980. else {
  1981. logmsg "No support for: $line";
  1982. last;
  1983. }
  1984. }
  1985. if($nosave) {
  1986. print FILE "$ulsize bytes would've been stored here\n";
  1987. }
  1988. close(FILE);
  1989. close_dataconn($disc);
  1990. logmsg "received $ulsize bytes upload\n";
  1991. sendcontrol "226 File transfer complete\r\n";
  1992. return 0;
  1993. }
  1994. sub PASV_ftp {
  1995. my ($arg, $cmd)=@_;
  1996. my $pasvport;
  1997. my $bindonly = ($nodataconn) ? '--bindonly' : '';
  1998. # kill previous data connection sockfilt when alive
  1999. if($datasockf_runs eq 'yes') {
  2000. killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
  2001. logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
  2002. }
  2003. datasockf_state('STOPPED');
  2004. logmsg "====> Passive DATA channel requested by client\n";
  2005. logmsg "DATA sockfilt for passive data channel starting...\n";
  2006. # We fire up a new sockfilt to do the data transfer for us.
  2007. my $datasockfcmd = "./server/sockfilt " .
  2008. "--ipv$ipvnum $bindonly --port 0 " .
  2009. "--pidfile \"$datasockf_pidfile\" " .
  2010. "--logfile \"$datasockf_logfile\"";
  2011. $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
  2012. if($nodataconn) {
  2013. datasockf_state('PASSIVE_NODATACONN');
  2014. }
  2015. else {
  2016. datasockf_state('PASSIVE');
  2017. }
  2018. print STDERR "$datasockfcmd\n" if($verbose);
  2019. print DWRITE "PING\n";
  2020. my $pong;
  2021. sysread_or_die(\*DREAD, \$pong, 5);
  2022. if($pong =~ /^FAIL/) {
  2023. logmsg "DATA sockfilt said: FAIL\n";
  2024. logmsg "DATA sockfilt for passive data channel failed\n";
  2025. logmsg "DATA sockfilt not running\n";
  2026. datasockf_state('STOPPED');
  2027. sendcontrol "500 no free ports!\r\n";
  2028. return;
  2029. }
  2030. elsif($pong !~ /^PONG/) {
  2031. logmsg "DATA sockfilt unexpected response: $pong\n";
  2032. logmsg "DATA sockfilt for passive data channel failed\n";
  2033. logmsg "DATA sockfilt killed now\n";
  2034. killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
  2035. logmsg "DATA sockfilt not running\n";
  2036. datasockf_state('STOPPED');
  2037. sendcontrol "500 no free ports!\r\n";
  2038. return;
  2039. }
  2040. logmsg "DATA sockfilt for passive data channel started (pid $slavepid)\n";
  2041. # Find out on what port we listen on or have bound
  2042. my $i;
  2043. print DWRITE "PORT\n";
  2044. # READ the response code
  2045. sysread_or_die(\*DREAD, \$i, 5);
  2046. # READ the response size
  2047. sysread_or_die(\*DREAD, \$i, 5);
  2048. my $size = 0;
  2049. if($i =~ /^([0-9a-fA-F]{4})\n/) {
  2050. $size = hex($1);
  2051. }
  2052. # READ the response data
  2053. read_datasockf(\$i, $size);
  2054. # The data is in the format
  2055. # IPvX/NNN
  2056. if($i =~ /IPv(\d)\/(\d+)/) {
  2057. # FIX: deal with IP protocol version
  2058. $pasvport = $2;
  2059. }
  2060. if(!$pasvport) {
  2061. logmsg "DATA sockfilt unknown listener port\n";
  2062. logmsg "DATA sockfilt for passive data channel failed\n";
  2063. logmsg "DATA sockfilt killed now\n";
  2064. killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
  2065. logmsg "DATA sockfilt not running\n";
  2066. datasockf_state('STOPPED');
  2067. sendcontrol "500 no free ports!\r\n";
  2068. return;
  2069. }
  2070. if($nodataconn) {
  2071. my $str = nodataconn_str();
  2072. logmsg "DATA sockfilt for passive data channel ($str) bound on port ".
  2073. "$pasvport\n";
  2074. }
  2075. else {
  2076. logmsg "DATA sockfilt for passive data channel listens on port ".
  2077. "$pasvport\n";
  2078. }
  2079. if($cmd ne "EPSV") {
  2080. # PASV reply
  2081. my $p=$listenaddr;
  2082. $p =~ s/\./,/g;
  2083. if($pasvbadip) {
  2084. $p="1,2,3,4";
  2085. }
  2086. sendcontrol sprintf("227 Entering Passive Mode ($p,%d,%d)\n",
  2087. int($pasvport/256), int($pasvport%256));
  2088. }
  2089. else {
  2090. # EPSV reply
  2091. sendcontrol sprintf("229 Entering Passive Mode (|||%d|)\n", $pasvport);
  2092. }
  2093. logmsg "Client has been notified that DATA conn ".
  2094. "will be accepted on port $pasvport\n";
  2095. if($nodataconn) {
  2096. my $str = nodataconn_str();
  2097. logmsg "====> Client fooled ($str)\n";
  2098. return;
  2099. }
  2100. eval {
  2101. local $SIG{ALRM} = sub { die "alarm\n" };
  2102. # assume swift operations unless explicitly slow
  2103. alarm ($datadelay?20:10);
  2104. # Wait for 'CNCT'
  2105. my $input;
  2106. # FIX: Monitor ctrl conn for disconnect
  2107. while(sysread(DREAD, $input, 5)) {
  2108. if($input !~ /^CNCT/) {
  2109. # we wait for a connected client
  2110. logmsg "Odd, we got $input from client\n";
  2111. next;
  2112. }
  2113. logmsg "Client connects to port $pasvport\n";
  2114. last;
  2115. }
  2116. alarm 0;
  2117. };
  2118. if ($@) {
  2119. # timed out
  2120. logmsg "$srvrname server timed out awaiting data connection ".
  2121. "on port $pasvport\n";
  2122. logmsg "accept failed or connection not even attempted\n";
  2123. logmsg "DATA sockfilt killed now\n";
  2124. killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
  2125. logmsg "DATA sockfilt not running\n";
  2126. datasockf_state('STOPPED');
  2127. return;
  2128. }
  2129. else {
  2130. logmsg "====> Client established passive DATA connection ".
  2131. "on port $pasvport\n";
  2132. }
  2133. return;
  2134. }
  2135. #
  2136. # Support both PORT and EPRT here.
  2137. #
  2138. sub PORT_ftp {
  2139. my ($arg, $cmd) = @_;
  2140. my $port;
  2141. my $addr;
  2142. # kill previous data connection sockfilt when alive
  2143. if($datasockf_runs eq 'yes') {
  2144. killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
  2145. logmsg "DATA sockfilt for $datasockf_mode data channel killed\n";
  2146. }
  2147. datasockf_state('STOPPED');
  2148. logmsg "====> Active DATA channel requested by client\n";
  2149. # We always ignore the given IP and use localhost.
  2150. if($cmd eq "PORT") {
  2151. if($arg !~ /(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)/) {
  2152. logmsg "DATA sockfilt for active data channel not started ".
  2153. "(bad PORT-line: $arg)\n";
  2154. sendcontrol "500 silly you, go away\r\n";
  2155. return;
  2156. }
  2157. $port = ($5<<8)+$6;
  2158. $addr = "$1.$2.$3.$4";
  2159. }
  2160. # EPRT |2|::1|49706|
  2161. elsif($cmd eq "EPRT") {
  2162. if($arg !~ /(\d+)\|([^\|]+)\|(\d+)/) {
  2163. logmsg "DATA sockfilt for active data channel not started ".
  2164. "(bad EPRT-line: $arg)\n";
  2165. sendcontrol "500 silly you, go away\r\n";
  2166. return;
  2167. }
  2168. sendcontrol "200 Thanks for dropping by. We contact you later\r\n";
  2169. $port = $3;
  2170. $addr = $2;
  2171. }
  2172. else {
  2173. logmsg "DATA sockfilt for active data channel not started ".
  2174. "(invalid command: $cmd)\n";
  2175. sendcontrol "500 we don't like $cmd now\r\n";
  2176. return;
  2177. }
  2178. if(!$port || $port > 65535) {
  2179. logmsg "DATA sockfilt for active data channel not started ".
  2180. "(illegal PORT number: $port)\n";
  2181. return;
  2182. }
  2183. if($nodataconn) {
  2184. my $str = nodataconn_str();
  2185. logmsg "DATA sockfilt for active data channel not started ($str)\n";
  2186. datasockf_state('ACTIVE_NODATACONN');
  2187. logmsg "====> Active DATA channel not established\n";
  2188. return;
  2189. }
  2190. logmsg "DATA sockfilt for active data channel starting...\n";
  2191. # We fire up a new sockfilt to do the data transfer for us.
  2192. my $datasockfcmd = "./server/sockfilt " .
  2193. "--ipv$ipvnum --connect $port --addr \"$addr\" " .
  2194. "--pidfile \"$datasockf_pidfile\" " .
  2195. "--logfile \"$datasockf_logfile\"";
  2196. $slavepid = open2(\*DREAD, \*DWRITE, $datasockfcmd);
  2197. datasockf_state('ACTIVE');
  2198. print STDERR "$datasockfcmd\n" if($verbose);
  2199. print DWRITE "PING\n";
  2200. my $pong;
  2201. sysread_or_die(\*DREAD, \$pong, 5);
  2202. if($pong =~ /^FAIL/) {
  2203. logmsg "DATA sockfilt said: FAIL\n";
  2204. logmsg "DATA sockfilt for active data channel failed\n";
  2205. logmsg "DATA sockfilt not running\n";
  2206. datasockf_state('STOPPED');
  2207. # client shall timeout awaiting connection from server
  2208. return;
  2209. }
  2210. elsif($pong !~ /^PONG/) {
  2211. logmsg "DATA sockfilt unexpected response: $pong\n";
  2212. logmsg "DATA sockfilt for active data channel failed\n";
  2213. logmsg "DATA sockfilt killed now\n";
  2214. killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
  2215. logmsg "DATA sockfilt not running\n";
  2216. datasockf_state('STOPPED');
  2217. # client shall timeout awaiting connection from server
  2218. return;
  2219. }
  2220. logmsg "DATA sockfilt for active data channel started (pid $slavepid)\n";
  2221. logmsg "====> Active DATA channel connected to client port $port\n";
  2222. return;
  2223. }
  2224. #**********************************************************************
  2225. # datasockf_state is used to change variables that keep state info
  2226. # relative to the FTP secondary or data sockfilt process as soon as
  2227. # one of the five possible stable states is reached. Variables that
  2228. # are modified by this sub may be checked independently but should
  2229. # not be changed except by calling this sub.
  2230. #
  2231. sub datasockf_state {
  2232. my $state = $_[0];
  2233. if($state eq 'STOPPED') {
  2234. # Data sockfilter initial state, not running,
  2235. # not connected and not used.
  2236. $datasockf_state = $state;
  2237. $datasockf_mode = 'none';
  2238. $datasockf_runs = 'no';
  2239. $datasockf_conn = 'no';
  2240. }
  2241. elsif($state eq 'PASSIVE') {
  2242. # Data sockfilter accepted connection from client.
  2243. $datasockf_state = $state;
  2244. $datasockf_mode = 'passive';
  2245. $datasockf_runs = 'yes';
  2246. $datasockf_conn = 'yes';
  2247. }
  2248. elsif($state eq 'ACTIVE') {
  2249. # Data sockfilter has connected to client.
  2250. $datasockf_state = $state;
  2251. $datasockf_mode = 'active';
  2252. $datasockf_runs = 'yes';
  2253. $datasockf_conn = 'yes';
  2254. }
  2255. elsif($state eq 'PASSIVE_NODATACONN') {
  2256. # Data sockfilter bound port without listening,
  2257. # client won't be able to establish data connection.
  2258. $datasockf_state = $state;
  2259. $datasockf_mode = 'passive';
  2260. $datasockf_runs = 'yes';
  2261. $datasockf_conn = 'no';
  2262. }
  2263. elsif($state eq 'ACTIVE_NODATACONN') {
  2264. # Data sockfilter does not even run,
  2265. # client awaits data connection from server in vain.
  2266. $datasockf_state = $state;
  2267. $datasockf_mode = 'active';
  2268. $datasockf_runs = 'no';
  2269. $datasockf_conn = 'no';
  2270. }
  2271. else {
  2272. die "Internal error. Unknown datasockf state: $state!";
  2273. }
  2274. }
  2275. #**********************************************************************
  2276. # nodataconn_str returns string of efective nodataconn command. Notice
  2277. # that $nodataconn may be set alone or in addition to a $nodataconnXXX.
  2278. #
  2279. sub nodataconn_str {
  2280. my $str;
  2281. # order matters
  2282. $str = 'NODATACONN' if($nodataconn);
  2283. $str = 'NODATACONN425' if($nodataconn425);
  2284. $str = 'NODATACONN421' if($nodataconn421);
  2285. $str = 'NODATACONN150' if($nodataconn150);
  2286. return "$str";
  2287. }
  2288. #**********************************************************************
  2289. # customize configures test server operation for each curl test, reading
  2290. # configuration commands/parameters from server commands file each time
  2291. # a new client control connection is established with the test server.
  2292. # On success returns 1, otherwise zero.
  2293. #
  2294. sub customize {
  2295. $ctrldelay = 0; # default is no throttling of the ctrl stream
  2296. $datadelay = 0; # default is no throttling of the data stream
  2297. $retrweirdo = 0; # default is no use of RETRWEIRDO
  2298. $retrnosize = 0; # default is no use of RETRNOSIZE
  2299. $pasvbadip = 0; # default is no use of PASVBADIP
  2300. $nosave = 0; # default is to actually save uploaded data to file
  2301. $nodataconn = 0; # default is to establish or accept data channel
  2302. $nodataconn425 = 0; # default is to not send 425 without data channel
  2303. $nodataconn421 = 0; # default is to not send 421 without data channel
  2304. $nodataconn150 = 0; # default is to not send 150 without data channel
  2305. @capabilities = (); # default is to not support capability commands
  2306. @auth_mechs = (); # default is to not support authentication commands
  2307. %fulltextreply = ();#
  2308. %commandreply = (); #
  2309. %customcount = (); #
  2310. %delayreply = (); #
  2311. open(CUSTOM, "<log/ftpserver.cmd") ||
  2312. return 1;
  2313. logmsg "FTPD: Getting commands from log/ftpserver.cmd\n";
  2314. while(<CUSTOM>) {
  2315. if($_ =~ /REPLY \"([A-Z]+ [A-Za-z0-9+-\/=\*. ]+)\" (.*)/) {
  2316. $fulltextreply{$1}=eval "qq{$2}";
  2317. logmsg "FTPD: set custom reply for $1\n";
  2318. }
  2319. elsif($_ =~ /REPLY ([A-Za-z0-9+\/=\*]*) (.*)/) {
  2320. $commandreply{$1}=eval "qq{$2}";
  2321. if($1 eq "") {
  2322. logmsg "FTPD: set custom reply for empty command\n";
  2323. }
  2324. else {
  2325. logmsg "FTPD: set custom reply for $1 command\n";
  2326. }
  2327. }
  2328. elsif($_ =~ /COUNT ([A-Z]+) (.*)/) {
  2329. # we blank the custom reply for this command when having
  2330. # been used this number of times
  2331. $customcount{$1}=$2;
  2332. logmsg "FTPD: blank custom reply for $1 command after $2 uses\n";
  2333. }
  2334. elsif($_ =~ /DELAY ([A-Z]+) (\d*)/) {
  2335. $delayreply{$1}=$2;
  2336. logmsg "FTPD: delay reply for $1 with $2 seconds\n";
  2337. }
  2338. elsif($_ =~ /SLOWDOWN/) {
  2339. $ctrldelay=1;
  2340. $datadelay=1;
  2341. logmsg "FTPD: send response with 0.01 sec delay between each byte\n";
  2342. }
  2343. elsif($_ =~ /RETRWEIRDO/) {
  2344. logmsg "FTPD: instructed to use RETRWEIRDO\n";
  2345. $retrweirdo=1;
  2346. }
  2347. elsif($_ =~ /RETRNOSIZE/) {
  2348. logmsg "FTPD: instructed to use RETRNOSIZE\n";
  2349. $retrnosize=1;
  2350. }
  2351. elsif($_ =~ /PASVBADIP/) {
  2352. logmsg "FTPD: instructed to use PASVBADIP\n";
  2353. $pasvbadip=1;
  2354. }
  2355. elsif($_ =~ /NODATACONN425/) {
  2356. # applies to both active and passive FTP modes
  2357. logmsg "FTPD: instructed to use NODATACONN425\n";
  2358. $nodataconn425=1;
  2359. $nodataconn=1;
  2360. }
  2361. elsif($_ =~ /NODATACONN421/) {
  2362. # applies to both active and passive FTP modes
  2363. logmsg "FTPD: instructed to use NODATACONN421\n";
  2364. $nodataconn421=1;
  2365. $nodataconn=1;
  2366. }
  2367. elsif($_ =~ /NODATACONN150/) {
  2368. # applies to both active and passive FTP modes
  2369. logmsg "FTPD: instructed to use NODATACONN150\n";
  2370. $nodataconn150=1;
  2371. $nodataconn=1;
  2372. }
  2373. elsif($_ =~ /NODATACONN/) {
  2374. # applies to both active and passive FTP modes
  2375. logmsg "FTPD: instructed to use NODATACONN\n";
  2376. $nodataconn=1;
  2377. }
  2378. elsif($_ =~ /CAPA (.*)/) {
  2379. logmsg "FTPD: instructed to support CAPABILITY command\n";
  2380. @capabilities = split(/ (?!(?:[^" ]|[^"] [^"])+")/, $1);
  2381. foreach (@capabilities) {
  2382. $_ = $1 if /^"(.*)"$/;
  2383. }
  2384. }
  2385. elsif($_ =~ /AUTH (.*)/) {
  2386. logmsg "FTPD: instructed to support AUTHENTICATION command\n";
  2387. @auth_mechs = split(/ /, $1);
  2388. }
  2389. elsif($_ =~ /NOSAVE/) {
  2390. # don't actually store the file we upload - to be used when
  2391. # uploading insanely huge amounts
  2392. $nosave = 1;
  2393. logmsg "FTPD: NOSAVE prevents saving of uploaded data\n";
  2394. }
  2395. }
  2396. close(CUSTOM);
  2397. }
  2398. #----------------------------------------------------------------------
  2399. #----------------------------------------------------------------------
  2400. #--------------------------- END OF SUBS ----------------------------
  2401. #----------------------------------------------------------------------
  2402. #----------------------------------------------------------------------
  2403. #**********************************************************************
  2404. # Parse command line options
  2405. #
  2406. # Options:
  2407. #
  2408. # --verbose # verbose
  2409. # --srcdir # source directory
  2410. # --id # server instance number
  2411. # --proto # server protocol
  2412. # --pidfile # server pid file
  2413. # --logfile # server log file
  2414. # --ipv4 # server IP version 4
  2415. # --ipv6 # server IP version 6
  2416. # --port # server listener port
  2417. # --addr # server address for listener port binding
  2418. #
  2419. while(@ARGV) {
  2420. if($ARGV[0] eq '--verbose') {
  2421. $verbose = 1;
  2422. }
  2423. elsif($ARGV[0] eq '--srcdir') {
  2424. if($ARGV[1]) {
  2425. $srcdir = $ARGV[1];
  2426. shift @ARGV;
  2427. }
  2428. }
  2429. elsif($ARGV[0] eq '--id') {
  2430. if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
  2431. $idnum = $1 if($1 > 0);
  2432. shift @ARGV;
  2433. }
  2434. }
  2435. elsif($ARGV[0] eq '--proto') {
  2436. if($ARGV[1] && ($ARGV[1] =~ /^(ftp|imap|pop3|smtp)$/)) {
  2437. $proto = $1;
  2438. shift @ARGV;
  2439. }
  2440. else {
  2441. die "unsupported protocol $ARGV[1]";
  2442. }
  2443. }
  2444. elsif($ARGV[0] eq '--pidfile') {
  2445. if($ARGV[1]) {
  2446. $pidfile = $ARGV[1];
  2447. shift @ARGV;
  2448. }
  2449. }
  2450. elsif($ARGV[0] eq '--logfile') {
  2451. if($ARGV[1]) {
  2452. $logfile = $ARGV[1];
  2453. shift @ARGV;
  2454. }
  2455. }
  2456. elsif($ARGV[0] eq '--ipv4') {
  2457. $ipvnum = 4;
  2458. $listenaddr = '127.0.0.1' if($listenaddr eq '::1');
  2459. }
  2460. elsif($ARGV[0] eq '--ipv6') {
  2461. $ipvnum = 6;
  2462. $listenaddr = '::1' if($listenaddr eq '127.0.0.1');
  2463. }
  2464. elsif($ARGV[0] eq '--port') {
  2465. if($ARGV[1] && ($ARGV[1] =~ /^(\d+)$/)) {
  2466. $port = $1 if($1 > 1024);
  2467. shift @ARGV;
  2468. }
  2469. }
  2470. elsif($ARGV[0] eq '--addr') {
  2471. if($ARGV[1]) {
  2472. my $tmpstr = $ARGV[1];
  2473. if($tmpstr =~ /^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/) {
  2474. $listenaddr = "$1.$2.$3.$4" if($ipvnum == 4);
  2475. }
  2476. elsif($ipvnum == 6) {
  2477. $listenaddr = $tmpstr;
  2478. $listenaddr =~ s/^\[(.*)\]$/$1/;
  2479. }
  2480. shift @ARGV;
  2481. }
  2482. }
  2483. else {
  2484. print STDERR "\nWarning: ftpserver.pl unknown parameter: $ARGV[0]\n";
  2485. }
  2486. shift @ARGV;
  2487. }
  2488. #***************************************************************************
  2489. # Initialize command line option dependant variables
  2490. #
  2491. if(!$srcdir) {
  2492. $srcdir = $ENV{'srcdir'} || '.';
  2493. }
  2494. if(!$pidfile) {
  2495. $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
  2496. }
  2497. if(!$logfile) {
  2498. $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
  2499. }
  2500. $mainsockf_pidfile = "$path/".
  2501. mainsockf_pidfilename($proto, $ipvnum, $idnum);
  2502. $mainsockf_logfile =
  2503. mainsockf_logfilename($logdir, $proto, $ipvnum, $idnum);
  2504. if($proto eq 'ftp') {
  2505. $datasockf_pidfile = "$path/".
  2506. datasockf_pidfilename($proto, $ipvnum, $idnum);
  2507. $datasockf_logfile =
  2508. datasockf_logfilename($logdir, $proto, $ipvnum, $idnum);
  2509. }
  2510. $srvrname = servername_str($proto, $ipvnum, $idnum);
  2511. $idstr = "$idnum" if($idnum > 1);
  2512. protocolsetup($proto);
  2513. $SIG{INT} = \&exit_signal_handler;
  2514. $SIG{TERM} = \&exit_signal_handler;
  2515. startsf();
  2516. logmsg sprintf("%s server listens on port IPv${ipvnum}/${port}\n", uc($proto));
  2517. open(PID, ">$pidfile");
  2518. print PID $$."\n";
  2519. close(PID);
  2520. logmsg("logged pid $$ in $pidfile\n");
  2521. while(1) {
  2522. # kill previous data connection sockfilt when alive
  2523. if($datasockf_runs eq 'yes') {
  2524. killsockfilters($proto, $ipvnum, $idnum, $verbose, 'data');
  2525. logmsg "DATA sockfilt for $datasockf_mode data channel killed now\n";
  2526. }
  2527. datasockf_state('STOPPED');
  2528. #
  2529. # We read 'sockfilt' commands.
  2530. #
  2531. my $input;
  2532. logmsg "Awaiting input\n";
  2533. sysread_or_die(\*SFREAD, \$input, 5);
  2534. if($input !~ /^CNCT/) {
  2535. # we wait for a connected client
  2536. logmsg "MAIN sockfilt said: $input";
  2537. next;
  2538. }
  2539. logmsg "====> Client connect\n";
  2540. set_advisor_read_lock($SERVERLOGS_LOCK);
  2541. $serverlogslocked = 1;
  2542. # flush data:
  2543. $| = 1;
  2544. &customize(); # read test control instructions
  2545. my $welcome = $commandreply{"welcome"};
  2546. if(!$welcome) {
  2547. $welcome = $displaytext{"welcome"};
  2548. }
  2549. else {
  2550. # clear it after use
  2551. $commandreply{"welcome"}="";
  2552. if($welcome !~ /\r\n\z/) {
  2553. $welcome .= "\r\n";
  2554. }
  2555. }
  2556. sendcontrol $welcome;
  2557. #remove global variables from last connection
  2558. if($ftplistparserstate) {
  2559. undef $ftplistparserstate;
  2560. }
  2561. if($ftptargetdir) {
  2562. undef $ftptargetdir;
  2563. }
  2564. if($verbose) {
  2565. print STDERR "OUT: $welcome";
  2566. }
  2567. my $full = "";
  2568. while(1) {
  2569. my $i;
  2570. # Now we expect to read DATA\n[hex size]\n[prot], where the [prot]
  2571. # part only is FTP lingo.
  2572. # COMMAND
  2573. sysread_or_die(\*SFREAD, \$i, 5);
  2574. if($i !~ /^DATA/) {
  2575. logmsg "MAIN sockfilt said $i";
  2576. if($i =~ /^DISC/) {
  2577. # disconnect
  2578. last;
  2579. }
  2580. next;
  2581. }
  2582. # SIZE of data
  2583. sysread_or_die(\*SFREAD, \$i, 5);
  2584. my $size = 0;
  2585. if($i =~ /^([0-9a-fA-F]{4})\n/) {
  2586. $size = hex($1);
  2587. }
  2588. # data
  2589. read_mainsockf(\$input, $size);
  2590. ftpmsg $input;
  2591. $full .= $input;
  2592. # Loop until command completion
  2593. next unless($full =~ /\r\n$/);
  2594. # Remove trailing CRLF.
  2595. $full =~ s/[\n\r]+$//;
  2596. my $FTPCMD;
  2597. my $FTPARG;
  2598. if($proto eq "imap") {
  2599. # IMAP is different with its identifier first on the command line
  2600. if(($full =~ /^([^ ]+) ([^ ]+) (.*)/) ||
  2601. ($full =~ /^([^ ]+) ([^ ]+)/)) {
  2602. $cmdid=$1; # set the global variable
  2603. $FTPCMD=$2;
  2604. $FTPARG=$3;
  2605. }
  2606. # IMAP authentication cancellation
  2607. elsif($full =~ /^\*$/) {
  2608. # Command id has already been set
  2609. $FTPCMD="*";
  2610. $FTPARG="";
  2611. }
  2612. # IMAP long "commands" are base64 authentication data
  2613. elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
  2614. # Command id has already been set
  2615. $FTPCMD=$full;
  2616. $FTPARG="";
  2617. }
  2618. else {
  2619. sendcontrol "$full BAD Command\r\n";
  2620. last;
  2621. }
  2622. }
  2623. elsif($full =~ /^([A-Z]{3,4})(\s(.*))?$/i) {
  2624. $FTPCMD=$1;
  2625. $FTPARG=$3;
  2626. }
  2627. elsif($proto eq "pop3") {
  2628. # POP3 authentication cancellation
  2629. if($full =~ /^\*$/) {
  2630. $FTPCMD="*";
  2631. $FTPARG="";
  2632. }
  2633. # POP3 long "commands" are base64 authentication data
  2634. elsif($full =~ /^[A-Z0-9+\/]*={0,2}$/i) {
  2635. $FTPCMD=$full;
  2636. $FTPARG="";
  2637. }
  2638. else {
  2639. sendcontrol "-ERR Unrecognized command\r\n";
  2640. last;
  2641. }
  2642. }
  2643. elsif($proto eq "smtp") {
  2644. # SMTP authentication cancellation
  2645. if($full =~ /^\*$/) {
  2646. $FTPCMD="*";
  2647. $FTPARG="";
  2648. }
  2649. # SMTP long "commands" are base64 authentication data
  2650. elsif($full =~ /^[A-Z0-9+\/]{0,512}={0,2}$/i) {
  2651. $FTPCMD=$full;
  2652. $FTPARG="";
  2653. }
  2654. else {
  2655. sendcontrol "500 Unrecognized command\r\n";
  2656. last;
  2657. }
  2658. }
  2659. else {
  2660. sendcontrol "500 Unrecognized command\r\n";
  2661. last;
  2662. }
  2663. logmsg "< \"$full\"\n";
  2664. if($verbose) {
  2665. print STDERR "IN: $full\n";
  2666. }
  2667. $full = "";
  2668. my $delay = $delayreply{$FTPCMD};
  2669. if($delay) {
  2670. # just go sleep this many seconds!
  2671. logmsg("Sleep for $delay seconds\n");
  2672. my $twentieths = $delay * 20;
  2673. while($twentieths--) {
  2674. select(undef, undef, undef, 0.05) unless($got_exit_signal);
  2675. }
  2676. }
  2677. my $check = 1; # no response yet
  2678. # See if there is a custom reply for the full text
  2679. my $fulltext = $FTPARG ? $FTPCMD . " " . $FTPARG : $FTPCMD;
  2680. my $text = $fulltextreply{$fulltext};
  2681. if($text && ($text ne "")) {
  2682. sendcontrol "$text\r\n";
  2683. $check = 0;
  2684. }
  2685. else {
  2686. # See if there is a custom reply for the command
  2687. $text = $commandreply{$FTPCMD};
  2688. if($text && ($text ne "")) {
  2689. if($customcount{$FTPCMD} && (!--$customcount{$FTPCMD})) {
  2690. # used enough times so blank the custom command reply
  2691. $commandreply{$FTPCMD}="";
  2692. }
  2693. sendcontrol "$text\r\n";
  2694. $check = 0;
  2695. }
  2696. else {
  2697. # See if there is any display text for the command
  2698. $text = $displaytext{$FTPCMD};
  2699. if($text && ($text ne "")) {
  2700. if($proto eq 'imap') {
  2701. sendcontrol "$cmdid $text\r\n";
  2702. }
  2703. else {
  2704. sendcontrol "$text\r\n";
  2705. }
  2706. $check = 0;
  2707. }
  2708. # only perform this if we're not faking a reply
  2709. my $func = $commandfunc{$FTPCMD};
  2710. if($func) {
  2711. &$func($FTPARG, $FTPCMD);
  2712. $check = 0;
  2713. }
  2714. }
  2715. }
  2716. if($check) {
  2717. logmsg "$FTPCMD wasn't handled!\n";
  2718. if($proto eq 'pop3') {
  2719. sendcontrol "-ERR $FTPCMD is not dealt with!\r\n";
  2720. }
  2721. elsif($proto eq 'imap') {
  2722. sendcontrol "$cmdid BAD $FTPCMD is not dealt with!\r\n";
  2723. }
  2724. else {
  2725. sendcontrol "500 $FTPCMD is not dealt with!\r\n";
  2726. }
  2727. }
  2728. } # while(1)
  2729. logmsg "====> Client disconnected\n";
  2730. if($serverlogslocked) {
  2731. $serverlogslocked = 0;
  2732. clear_advisor_read_lock($SERVERLOGS_LOCK);
  2733. }
  2734. }
  2735. killsockfilters($proto, $ipvnum, $idnum, $verbose);
  2736. unlink($pidfile);
  2737. if($serverlogslocked) {
  2738. $serverlogslocked = 0;
  2739. clear_advisor_read_lock($SERVERLOGS_LOCK);
  2740. }
  2741. exit;