ftpserver.pl 91 KB

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