ftpserver.pl 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391
  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 disconnect handshake with sockfilt on the secondary connection
  618. # (the only connection we actively disconnect).
  619. # This involves waiting for the disconnect acknowledgment 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. logmsg "> Throwing away $size bytes on closed connection\n";
  636. read_datasockf(\$line, $size);
  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;