Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ftp.py 102KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253
  1. # -*- test-case-name: twisted.test.test_ftp -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. An FTP protocol implementation
  6. """
  7. import errno
  8. import fnmatch
  9. # System Imports
  10. import os
  11. import re
  12. import stat
  13. import time
  14. try:
  15. import grp
  16. import pwd
  17. except ImportError:
  18. pwd = grp = None # type: ignore[assignment]
  19. from zope.interface import Interface, implementer
  20. # Twisted Imports
  21. from twisted import copyright
  22. from twisted.cred import checkers, credentials, error as cred_error, portal
  23. from twisted.internet import defer, error, interfaces, protocol, reactor
  24. from twisted.protocols import basic, policies
  25. from twisted.python import failure, filepath, log
  26. # constants
  27. # response codes
  28. RESTART_MARKER_REPLY = "100"
  29. SERVICE_READY_IN_N_MINUTES = "120"
  30. DATA_CNX_ALREADY_OPEN_START_XFR = "125"
  31. FILE_STATUS_OK_OPEN_DATA_CNX = "150"
  32. CMD_OK = "200.1"
  33. TYPE_SET_OK = "200.2"
  34. ENTERING_PORT_MODE = "200.3"
  35. CMD_NOT_IMPLMNTD_SUPERFLUOUS = "202"
  36. SYS_STATUS_OR_HELP_REPLY = "211.1"
  37. FEAT_OK = "211.2"
  38. DIR_STATUS = "212"
  39. FILE_STATUS = "213"
  40. HELP_MSG = "214"
  41. NAME_SYS_TYPE = "215"
  42. SVC_READY_FOR_NEW_USER = "220.1"
  43. WELCOME_MSG = "220.2"
  44. SVC_CLOSING_CTRL_CNX = "221.1"
  45. GOODBYE_MSG = "221.2"
  46. DATA_CNX_OPEN_NO_XFR_IN_PROGRESS = "225"
  47. CLOSING_DATA_CNX = "226.1"
  48. TXFR_COMPLETE_OK = "226.2"
  49. ENTERING_PASV_MODE = "227"
  50. ENTERING_EPSV_MODE = "229"
  51. USR_LOGGED_IN_PROCEED = "230.1" # v1 of code 230
  52. GUEST_LOGGED_IN_PROCEED = "230.2" # v2 of code 230
  53. REQ_FILE_ACTN_COMPLETED_OK = "250"
  54. PWD_REPLY = "257.1"
  55. MKD_REPLY = "257.2"
  56. USR_NAME_OK_NEED_PASS = "331.1" # v1 of Code 331
  57. GUEST_NAME_OK_NEED_EMAIL = "331.2" # v2 of code 331
  58. NEED_ACCT_FOR_LOGIN = "332"
  59. REQ_FILE_ACTN_PENDING_FURTHER_INFO = "350"
  60. SVC_NOT_AVAIL_CLOSING_CTRL_CNX = "421.1"
  61. TOO_MANY_CONNECTIONS = "421.2"
  62. CANT_OPEN_DATA_CNX = "425"
  63. CNX_CLOSED_TXFR_ABORTED = "426"
  64. REQ_ACTN_ABRTD_FILE_UNAVAIL = "450"
  65. REQ_ACTN_ABRTD_LOCAL_ERR = "451"
  66. REQ_ACTN_ABRTD_INSUFF_STORAGE = "452"
  67. SYNTAX_ERR = "500"
  68. SYNTAX_ERR_IN_ARGS = "501"
  69. CMD_NOT_IMPLMNTD = "502.1"
  70. OPTS_NOT_IMPLEMENTED = "502.2"
  71. BAD_CMD_SEQ = "503"
  72. CMD_NOT_IMPLMNTD_FOR_PARAM = "504"
  73. NOT_LOGGED_IN = "530.1" # v1 of code 530 - please log in
  74. AUTH_FAILURE = "530.2" # v2 of code 530 - authorization failure
  75. NEED_ACCT_FOR_STOR = "532"
  76. FILE_NOT_FOUND = "550.1" # no such file or directory
  77. PERMISSION_DENIED = "550.2" # permission denied
  78. ANON_USER_DENIED = "550.3" # anonymous users can't alter filesystem
  79. IS_NOT_A_DIR = "550.4" # rmd called on a path that is not a directory
  80. REQ_ACTN_NOT_TAKEN = "550.5"
  81. FILE_EXISTS = "550.6"
  82. IS_A_DIR = "550.7"
  83. PAGE_TYPE_UNK = "551"
  84. EXCEEDED_STORAGE_ALLOC = "552"
  85. FILENAME_NOT_ALLOWED = "553"
  86. RESPONSE = {
  87. # -- 100's --
  88. # TODO: this must be fixed
  89. RESTART_MARKER_REPLY: "110 MARK yyyy-mmmm",
  90. SERVICE_READY_IN_N_MINUTES: "120 service ready in %s minutes",
  91. DATA_CNX_ALREADY_OPEN_START_XFR: "125 Data connection already open, "
  92. "starting transfer",
  93. FILE_STATUS_OK_OPEN_DATA_CNX: "150 File status okay; about to open "
  94. "data connection.",
  95. # -- 200's --
  96. CMD_OK: "200 Command OK",
  97. TYPE_SET_OK: "200 Type set to %s.",
  98. ENTERING_PORT_MODE: "200 PORT OK",
  99. CMD_NOT_IMPLMNTD_SUPERFLUOUS: "202 Command not implemented, "
  100. "superfluous at this site",
  101. SYS_STATUS_OR_HELP_REPLY: "211 System status reply",
  102. FEAT_OK: ["211-Features:", "211 End"],
  103. DIR_STATUS: "212 %s",
  104. FILE_STATUS: "213 %s",
  105. HELP_MSG: "214 help: %s",
  106. NAME_SYS_TYPE: "215 UNIX Type: L8",
  107. WELCOME_MSG: "220 %s",
  108. SVC_READY_FOR_NEW_USER: "220 Service ready",
  109. SVC_CLOSING_CTRL_CNX: "221 Service closing control " "connection",
  110. GOODBYE_MSG: "221 Goodbye.",
  111. DATA_CNX_OPEN_NO_XFR_IN_PROGRESS: "225 data connection open, no "
  112. "transfer in progress",
  113. CLOSING_DATA_CNX: "226 Abort successful",
  114. TXFR_COMPLETE_OK: "226 Transfer Complete.",
  115. ENTERING_PASV_MODE: "227 Entering Passive Mode (%s).",
  116. # Where is EPSV defined in the RFCs?
  117. ENTERING_EPSV_MODE: "229 Entering Extended Passive Mode " "(|||%s|).",
  118. USR_LOGGED_IN_PROCEED: "230 User logged in, proceed",
  119. GUEST_LOGGED_IN_PROCEED: "230 Anonymous login ok, access " "restrictions apply.",
  120. # i.e. CWD completed OK
  121. REQ_FILE_ACTN_COMPLETED_OK: "250 Requested File Action Completed " "OK",
  122. PWD_REPLY: '257 "%s"',
  123. MKD_REPLY: '257 "%s" created',
  124. # -- 300's --
  125. USR_NAME_OK_NEED_PASS: "331 Password required for %s.",
  126. GUEST_NAME_OK_NEED_EMAIL: "331 Guest login ok, type your email "
  127. "address as password.",
  128. NEED_ACCT_FOR_LOGIN: "332 Need account for login.",
  129. REQ_FILE_ACTN_PENDING_FURTHER_INFO: "350 Requested file action pending "
  130. "further information.",
  131. # -- 400's --
  132. SVC_NOT_AVAIL_CLOSING_CTRL_CNX: "421 Service not available, closing "
  133. "control connection.",
  134. TOO_MANY_CONNECTIONS: "421 Too many users right now, try "
  135. "again in a few minutes.",
  136. CANT_OPEN_DATA_CNX: "425 Can't open data connection.",
  137. CNX_CLOSED_TXFR_ABORTED: "426 Transfer aborted. Data " "connection closed.",
  138. REQ_ACTN_ABRTD_FILE_UNAVAIL: "450 Requested action aborted. " "File unavailable.",
  139. REQ_ACTN_ABRTD_LOCAL_ERR: "451 Requested action aborted. "
  140. "Local error in processing.",
  141. REQ_ACTN_ABRTD_INSUFF_STORAGE: "452 Requested action aborted. "
  142. "Insufficient storage.",
  143. # -- 500's --
  144. SYNTAX_ERR: "500 Syntax error: %s",
  145. SYNTAX_ERR_IN_ARGS: "501 syntax error in argument(s) %s.",
  146. CMD_NOT_IMPLMNTD: "502 Command '%s' not implemented",
  147. OPTS_NOT_IMPLEMENTED: "502 Option '%s' not implemented.",
  148. BAD_CMD_SEQ: "503 Incorrect sequence of commands: " "%s",
  149. CMD_NOT_IMPLMNTD_FOR_PARAM: "504 Not implemented for parameter " "'%s'.",
  150. NOT_LOGGED_IN: "530 Please login with USER and PASS.",
  151. AUTH_FAILURE: "530 Sorry, Authentication failed.",
  152. NEED_ACCT_FOR_STOR: "532 Need an account for storing " "files",
  153. FILE_NOT_FOUND: "550 %s: No such file or directory.",
  154. PERMISSION_DENIED: "550 %s: Permission denied.",
  155. ANON_USER_DENIED: "550 Anonymous users are forbidden to " "change the filesystem",
  156. IS_NOT_A_DIR: "550 Cannot rmd, %s is not a " "directory",
  157. FILE_EXISTS: "550 %s: File exists",
  158. IS_A_DIR: "550 %s: is a directory",
  159. REQ_ACTN_NOT_TAKEN: "550 Requested action not taken: %s",
  160. PAGE_TYPE_UNK: "551 Page type unknown",
  161. EXCEEDED_STORAGE_ALLOC: "552 Requested file action aborted, "
  162. "exceeded file storage allocation",
  163. FILENAME_NOT_ALLOWED: "553 Requested action not taken, file " "name not allowed",
  164. }
  165. class InvalidPath(Exception):
  166. """
  167. Internal exception used to signify an error during parsing a path.
  168. """
  169. def toSegments(cwd, path):
  170. """
  171. Normalize a path, as represented by a list of strings each
  172. representing one segment of the path.
  173. """
  174. if path.startswith("/"):
  175. segs = []
  176. else:
  177. segs = cwd[:]
  178. for s in path.split("/"):
  179. if s == "." or s == "":
  180. continue
  181. elif s == "..":
  182. if segs:
  183. segs.pop()
  184. else:
  185. raise InvalidPath(cwd, path)
  186. elif "\0" in s or "/" in s:
  187. raise InvalidPath(cwd, path)
  188. else:
  189. segs.append(s)
  190. return segs
  191. def errnoToFailure(e, path):
  192. """
  193. Map C{OSError} and C{IOError} to standard FTP errors.
  194. """
  195. if e == errno.ENOENT:
  196. return defer.fail(FileNotFoundError(path))
  197. elif e == errno.EACCES or e == errno.EPERM:
  198. return defer.fail(PermissionDeniedError(path))
  199. elif e == errno.ENOTDIR:
  200. return defer.fail(IsNotADirectoryError(path))
  201. elif e == errno.EEXIST:
  202. return defer.fail(FileExistsError(path))
  203. elif e == errno.EISDIR:
  204. return defer.fail(IsADirectoryError(path))
  205. else:
  206. return defer.fail()
  207. _testTranslation = fnmatch.translate("TEST")
  208. def _isGlobbingExpression(segments=None):
  209. """
  210. Helper for checking if a FTPShell `segments` contains a wildcard Unix
  211. expression.
  212. Only filename globbing is supported.
  213. This means that wildcards can only be presents in the last element of
  214. `segments`.
  215. @type segments: C{list}
  216. @param segments: List of path elements as used by the FTP server protocol.
  217. @rtype: Boolean
  218. @return: True if `segments` contains a globbing expression.
  219. """
  220. if not segments:
  221. return False
  222. # To check that something is a glob expression, we convert it to
  223. # Regular Expression.
  224. # We compare it to the translation of a known non-glob expression.
  225. # If the result is the same as the original expression then it contains no
  226. # globbing expression.
  227. globCandidate = segments[-1]
  228. globTranslations = fnmatch.translate(globCandidate)
  229. nonGlobTranslations = _testTranslation.replace("TEST", globCandidate, 1)
  230. if nonGlobTranslations == globTranslations:
  231. return False
  232. else:
  233. return True
  234. class FTPCmdError(Exception):
  235. """
  236. Generic exception for FTP commands.
  237. """
  238. def __init__(self, *msg):
  239. Exception.__init__(self, *msg)
  240. self.errorMessage = msg
  241. def response(self):
  242. """
  243. Generate a FTP response message for this error.
  244. """
  245. return RESPONSE[self.errorCode] % self.errorMessage
  246. class FileNotFoundError(FTPCmdError):
  247. """
  248. Raised when trying to access a non existent file or directory.
  249. """
  250. errorCode = FILE_NOT_FOUND
  251. class AnonUserDeniedError(FTPCmdError):
  252. """
  253. Raised when an anonymous user issues a command that will alter the
  254. filesystem
  255. """
  256. errorCode = ANON_USER_DENIED
  257. class PermissionDeniedError(FTPCmdError):
  258. """
  259. Raised when access is attempted to a resource to which access is
  260. not allowed.
  261. """
  262. errorCode = PERMISSION_DENIED
  263. class IsNotADirectoryError(FTPCmdError):
  264. """
  265. Raised when RMD is called on a path that isn't a directory.
  266. """
  267. errorCode = IS_NOT_A_DIR
  268. class FileExistsError(FTPCmdError):
  269. """
  270. Raised when attempted to override an existing resource.
  271. """
  272. errorCode = FILE_EXISTS
  273. class IsADirectoryError(FTPCmdError):
  274. """
  275. Raised when DELE is called on a path that is a directory.
  276. """
  277. errorCode = IS_A_DIR
  278. class CmdSyntaxError(FTPCmdError):
  279. """
  280. Raised when a command syntax is wrong.
  281. """
  282. errorCode = SYNTAX_ERR
  283. class CmdArgSyntaxError(FTPCmdError):
  284. """
  285. Raised when a command is called with wrong value or a wrong number of
  286. arguments.
  287. """
  288. errorCode = SYNTAX_ERR_IN_ARGS
  289. class CmdNotImplementedError(FTPCmdError):
  290. """
  291. Raised when an unimplemented command is given to the server.
  292. """
  293. errorCode = CMD_NOT_IMPLMNTD
  294. class CmdNotImplementedForArgError(FTPCmdError):
  295. """
  296. Raised when the handling of a parameter for a command is not implemented by
  297. the server.
  298. """
  299. errorCode = CMD_NOT_IMPLMNTD_FOR_PARAM
  300. class FTPError(Exception):
  301. pass
  302. class PortConnectionError(Exception):
  303. pass
  304. class BadCmdSequenceError(FTPCmdError):
  305. """
  306. Raised when a client sends a series of commands in an illogical sequence.
  307. """
  308. errorCode = BAD_CMD_SEQ
  309. class AuthorizationError(FTPCmdError):
  310. """
  311. Raised when client authentication fails.
  312. """
  313. errorCode = AUTH_FAILURE
  314. def debugDeferred(self, *_):
  315. log.msg("debugDeferred(): %s" % str(_), debug=True)
  316. # -- DTP Protocol --
  317. _months = [
  318. None,
  319. "Jan",
  320. "Feb",
  321. "Mar",
  322. "Apr",
  323. "May",
  324. "Jun",
  325. "Jul",
  326. "Aug",
  327. "Sep",
  328. "Oct",
  329. "Nov",
  330. "Dec",
  331. ]
  332. @implementer(interfaces.IConsumer)
  333. class DTP(protocol.Protocol):
  334. isConnected = False
  335. _cons = None
  336. _onConnLost = None
  337. _buffer = None
  338. _encoding = "latin-1"
  339. def connectionMade(self):
  340. self.isConnected = True
  341. self.factory.deferred.callback(None)
  342. self._buffer = []
  343. def connectionLost(self, reason):
  344. self.isConnected = False
  345. if self._onConnLost is not None:
  346. self._onConnLost.callback(None)
  347. def sendLine(self, line):
  348. """
  349. Send a line to data channel.
  350. @param line: The line to be sent.
  351. @type line: L{bytes}
  352. """
  353. self.transport.write(line + b"\r\n")
  354. def _formatOneListResponse(
  355. self, name, size, directory, permissions, hardlinks, modified, owner, group
  356. ):
  357. """
  358. Helper method to format one entry's info into a text entry like:
  359. 'drwxrwxrwx 0 user group 0 Jan 01 1970 filename.txt'
  360. @param name: C{bytes} name of the entry (file or directory or link)
  361. @param size: C{int} size of the entry
  362. @param directory: evals to C{bool} - whether the entry is a directory
  363. @param permissions: L{twisted.python.filepath.Permissions} object
  364. representing that entry's permissions
  365. @param hardlinks: C{int} number of hardlinks
  366. @param modified: C{float} - entry's last modified time in seconds
  367. since the epoch
  368. @param owner: C{str} username of the owner
  369. @param group: C{str} group name of the owner
  370. @return: C{str} in the requisite format
  371. """
  372. def formatDate(mtime):
  373. now = time.gmtime()
  374. info = {
  375. "month": _months[mtime.tm_mon],
  376. "day": mtime.tm_mday,
  377. "year": mtime.tm_year,
  378. "hour": mtime.tm_hour,
  379. "minute": mtime.tm_min,
  380. }
  381. if now.tm_year != mtime.tm_year:
  382. return "%(month)s %(day)02d %(year)5d" % info
  383. else:
  384. return "%(month)s %(day)02d %(hour)02d:%(minute)02d" % info
  385. format = (
  386. "%(directory)s%(permissions)s%(hardlinks)4d "
  387. "%(owner)-9s %(group)-9s %(size)15d %(date)12s "
  388. )
  389. msg = (
  390. format
  391. % {
  392. "directory": directory and "d" or "-",
  393. "permissions": permissions.shorthand(),
  394. "hardlinks": hardlinks,
  395. "owner": owner[:8],
  396. "group": group[:8],
  397. "size": size,
  398. "date": formatDate(time.gmtime(modified)),
  399. }
  400. ).encode(self._encoding)
  401. return msg + name
  402. def sendListResponse(self, name, response):
  403. self.sendLine(self._formatOneListResponse(name, *response))
  404. # Proxy IConsumer to our transport
  405. def registerProducer(self, producer, streaming):
  406. return self.transport.registerProducer(producer, streaming)
  407. def unregisterProducer(self):
  408. self.transport.unregisterProducer()
  409. self.transport.loseConnection()
  410. def write(self, data):
  411. if self.isConnected:
  412. return self.transport.write(data)
  413. raise Exception("Crap damn crap damn crap damn")
  414. # Pretend to be a producer, too.
  415. def _conswrite(self, bytes):
  416. try:
  417. self._cons.write(bytes)
  418. except BaseException:
  419. self._onConnLost.errback()
  420. def dataReceived(self, bytes):
  421. if self._cons is not None:
  422. self._conswrite(bytes)
  423. else:
  424. self._buffer.append(bytes)
  425. def _unregConsumer(self, ignored):
  426. self._cons.unregisterProducer()
  427. self._cons = None
  428. del self._onConnLost
  429. return ignored
  430. def registerConsumer(self, cons):
  431. assert self._cons is None
  432. self._cons = cons
  433. self._cons.registerProducer(self, True)
  434. for chunk in self._buffer:
  435. self._conswrite(chunk)
  436. self._buffer = None
  437. if self.isConnected:
  438. self._onConnLost = d = defer.Deferred()
  439. d.addBoth(self._unregConsumer)
  440. return d
  441. else:
  442. self._cons.unregisterProducer()
  443. self._cons = None
  444. return defer.succeed(None)
  445. def resumeProducing(self):
  446. self.transport.resumeProducing()
  447. def pauseProducing(self):
  448. self.transport.pauseProducing()
  449. def stopProducing(self):
  450. self.transport.stopProducing()
  451. class DTPFactory(protocol.ClientFactory):
  452. """
  453. Client factory for I{data transfer process} protocols.
  454. @ivar peerCheck: perform checks to make sure the ftp-pi's peer is the same
  455. as the dtp's
  456. @ivar pi: a reference to this factory's protocol interpreter
  457. @ivar _state: Indicates the current state of the DTPFactory. Initially,
  458. this is L{_IN_PROGRESS}. If the connection fails or times out, it is
  459. L{_FAILED}. If the connection succeeds before the timeout, it is
  460. L{_FINISHED}.
  461. @cvar _IN_PROGRESS: Token to signal that connection is active.
  462. @type _IN_PROGRESS: L{object}.
  463. @cvar _FAILED: Token to signal that connection has failed.
  464. @type _FAILED: L{object}.
  465. @cvar _FINISHED: Token to signal that connection was successfully closed.
  466. @type _FINISHED: L{object}.
  467. """
  468. _IN_PROGRESS = object()
  469. _FAILED = object()
  470. _FINISHED = object()
  471. _state = _IN_PROGRESS
  472. # -- configuration variables --
  473. peerCheck = False
  474. # -- class variables --
  475. def __init__(self, pi, peerHost=None, reactor=None):
  476. """
  477. Constructor
  478. @param pi: this factory's protocol interpreter
  479. @param peerHost: if peerCheck is True, this is the tuple that the
  480. generated instance will use to perform security checks
  481. """
  482. self.pi = pi
  483. self.peerHost = peerHost # from FTP.transport.peerHost()
  484. # deferred will fire when instance is connected
  485. self.deferred = defer.Deferred()
  486. self.delayedCall = None
  487. if reactor is None:
  488. from twisted.internet import reactor
  489. self._reactor = reactor
  490. def buildProtocol(self, addr):
  491. log.msg("DTPFactory.buildProtocol", debug=True)
  492. if self._state is not self._IN_PROGRESS:
  493. return None
  494. self._state = self._FINISHED
  495. self.cancelTimeout()
  496. p = DTP()
  497. p.factory = self
  498. p.pi = self.pi
  499. self.pi.dtpInstance = p
  500. return p
  501. def stopFactory(self):
  502. log.msg("dtpFactory.stopFactory", debug=True)
  503. self.cancelTimeout()
  504. def timeoutFactory(self):
  505. log.msg("timed out waiting for DTP connection")
  506. if self._state is not self._IN_PROGRESS:
  507. return
  508. self._state = self._FAILED
  509. d = self.deferred
  510. self.deferred = None
  511. d.errback(PortConnectionError(defer.TimeoutError("DTPFactory timeout")))
  512. def cancelTimeout(self):
  513. if self.delayedCall is not None and self.delayedCall.active():
  514. log.msg("cancelling DTP timeout", debug=True)
  515. self.delayedCall.cancel()
  516. def setTimeout(self, seconds):
  517. log.msg("DTPFactory.setTimeout set to %s seconds" % seconds)
  518. self.delayedCall = self._reactor.callLater(seconds, self.timeoutFactory)
  519. def clientConnectionFailed(self, connector, reason):
  520. if self._state is not self._IN_PROGRESS:
  521. return
  522. self._state = self._FAILED
  523. d = self.deferred
  524. self.deferred = None
  525. d.errback(PortConnectionError(reason))
  526. # -- FTP-PI (Protocol Interpreter) --
  527. class ASCIIConsumerWrapper:
  528. def __init__(self, cons):
  529. self.cons = cons
  530. self.registerProducer = cons.registerProducer
  531. self.unregisterProducer = cons.unregisterProducer
  532. assert (
  533. os.linesep == "\r\n" or len(os.linesep) == 1
  534. ), "Unsupported platform (yea right like this even exists)"
  535. if os.linesep == "\r\n":
  536. self.write = cons.write
  537. def write(self, bytes):
  538. return self.cons.write(bytes.replace(os.linesep, "\r\n"))
  539. @implementer(interfaces.IConsumer)
  540. class FileConsumer:
  541. """
  542. A consumer for FTP input that writes data to a file.
  543. @ivar fObj: a file object opened for writing, used to write data received.
  544. @type fObj: C{file}
  545. """
  546. def __init__(self, fObj):
  547. self.fObj = fObj
  548. def registerProducer(self, producer, streaming):
  549. self.producer = producer
  550. assert streaming
  551. def unregisterProducer(self):
  552. self.producer = None
  553. self.fObj.close()
  554. def write(self, bytes):
  555. self.fObj.write(bytes)
  556. class FTPOverflowProtocol(basic.LineReceiver):
  557. """FTP mini-protocol for when there are too many connections."""
  558. _encoding = "latin-1"
  559. def connectionMade(self):
  560. self.sendLine(RESPONSE[TOO_MANY_CONNECTIONS].encode(self._encoding))
  561. self.transport.loseConnection()
  562. class FTP(basic.LineReceiver, policies.TimeoutMixin):
  563. """
  564. Protocol Interpreter for the File Transfer Protocol
  565. @ivar state: The current server state. One of L{UNAUTH},
  566. L{INAUTH}, L{AUTHED}, L{RENAMING}.
  567. @ivar shell: The connected avatar
  568. @ivar binary: The transfer mode. If false, ASCII.
  569. @ivar dtpFactory: Generates a single DTP for this session
  570. @ivar dtpPort: Port returned from listenTCP
  571. @ivar listenFactory: A callable with the signature of
  572. L{twisted.internet.interfaces.IReactorTCP.listenTCP} which will be used
  573. to create Ports for passive connections (mainly for testing).
  574. @ivar passivePortRange: iterator used as source of passive port numbers.
  575. @type passivePortRange: C{iterator}
  576. @cvar UNAUTH: Command channel is not yet authenticated.
  577. @type UNAUTH: L{int}
  578. @cvar INAUTH: Command channel is in the process of being authenticated.
  579. @type INAUTH: L{int}
  580. @cvar AUTHED: Command channel was successfully authenticated.
  581. @type AUTHED: L{int}
  582. @cvar RENAMING: Command channel is between the renaming command sequence.
  583. @type RENAMING: L{int}
  584. """
  585. disconnected = False
  586. # States an FTP can be in
  587. UNAUTH, INAUTH, AUTHED, RENAMING = range(4)
  588. # how long the DTP waits for a connection
  589. dtpTimeout = 10
  590. portal = None
  591. shell = None
  592. dtpFactory = None
  593. dtpPort = None
  594. dtpInstance = None
  595. binary = True
  596. PUBLIC_COMMANDS = ["FEAT", "QUIT"]
  597. FEATURES = ["FEAT", "MDTM", "PASV", "SIZE", "TYPE A;I"]
  598. passivePortRange = range(0, 1)
  599. listenFactory = reactor.listenTCP # type: ignore[attr-defined]
  600. _encoding = "latin-1"
  601. def reply(self, key, *args):
  602. msg = RESPONSE[key] % args
  603. self.sendLine(msg)
  604. def sendLine(self, line):
  605. """
  606. (Private) Encodes and sends a line
  607. @param line: L{bytes} or L{unicode}
  608. """
  609. if isinstance(line, str):
  610. line = line.encode(self._encoding)
  611. super().sendLine(line)
  612. def connectionMade(self):
  613. self.state = self.UNAUTH
  614. self.setTimeout(self.timeOut)
  615. self.reply(WELCOME_MSG, self.factory.welcomeMessage)
  616. def connectionLost(self, reason):
  617. # if we have a DTP protocol instance running and
  618. # we lose connection to the client's PI, kill the
  619. # DTP connection and close the port
  620. if self.dtpFactory:
  621. self.cleanupDTP()
  622. self.setTimeout(None)
  623. if hasattr(self.shell, "logout") and self.shell.logout is not None:
  624. self.shell.logout()
  625. self.shell = None
  626. self.transport = None
  627. def timeoutConnection(self):
  628. self.transport.loseConnection()
  629. def lineReceived(self, line):
  630. self.resetTimeout()
  631. self.pauseProducing()
  632. if bytes != str:
  633. line = line.decode(self._encoding)
  634. def processFailed(err):
  635. if err.check(FTPCmdError):
  636. self.sendLine(err.value.response())
  637. elif err.check(TypeError) and any(
  638. msg in err.value.args[0]
  639. for msg in ("takes exactly", "required positional argument")
  640. ):
  641. self.reply(SYNTAX_ERR, f"{cmd} requires an argument.")
  642. else:
  643. log.msg("Unexpected FTP error")
  644. log.err(err)
  645. self.reply(REQ_ACTN_NOT_TAKEN, "internal server error")
  646. def processSucceeded(result):
  647. if isinstance(result, tuple):
  648. self.reply(*result)
  649. elif result is not None:
  650. self.reply(result)
  651. def allDone(ignored):
  652. if not self.disconnected:
  653. self.resumeProducing()
  654. spaceIndex = line.find(" ")
  655. if spaceIndex != -1:
  656. cmd = line[:spaceIndex]
  657. args = (line[spaceIndex + 1 :],)
  658. else:
  659. cmd = line
  660. args = ()
  661. d = defer.maybeDeferred(self.processCommand, cmd, *args)
  662. d.addCallbacks(processSucceeded, processFailed)
  663. d.addErrback(log.err)
  664. # XXX It burnsss
  665. # LineReceiver doesn't let you resumeProducing inside
  666. # lineReceived atm
  667. from twisted.internet import reactor
  668. reactor.callLater(0, d.addBoth, allDone)
  669. def processCommand(self, cmd, *params):
  670. def call_ftp_command(command):
  671. method = getattr(self, "ftp_" + command, None)
  672. if method is not None:
  673. return method(*params)
  674. return defer.fail(CmdNotImplementedError(command))
  675. cmd = cmd.upper()
  676. if cmd in self.PUBLIC_COMMANDS:
  677. return call_ftp_command(cmd)
  678. elif self.state == self.UNAUTH:
  679. if cmd == "USER":
  680. return self.ftp_USER(*params)
  681. elif cmd == "PASS":
  682. return BAD_CMD_SEQ, "USER required before PASS"
  683. else:
  684. return NOT_LOGGED_IN
  685. elif self.state == self.INAUTH:
  686. if cmd == "PASS":
  687. return self.ftp_PASS(*params)
  688. else:
  689. return BAD_CMD_SEQ, "PASS required after USER"
  690. elif self.state == self.AUTHED:
  691. return call_ftp_command(cmd)
  692. elif self.state == self.RENAMING:
  693. if cmd == "RNTO":
  694. return self.ftp_RNTO(*params)
  695. else:
  696. return BAD_CMD_SEQ, "RNTO required after RNFR"
  697. def getDTPPort(self, factory):
  698. """
  699. Return a port for passive access, using C{self.passivePortRange}
  700. attribute.
  701. """
  702. for portn in self.passivePortRange:
  703. try:
  704. dtpPort = self.listenFactory(portn, factory)
  705. except error.CannotListenError:
  706. continue
  707. else:
  708. return dtpPort
  709. raise error.CannotListenError(
  710. "", portn, f"No port available in range {self.passivePortRange}"
  711. )
  712. def ftp_USER(self, username):
  713. """
  714. First part of login. Get the username the peer wants to
  715. authenticate as.
  716. """
  717. if not username:
  718. return defer.fail(CmdSyntaxError("USER requires an argument"))
  719. self._user = username
  720. self.state = self.INAUTH
  721. if self.factory.allowAnonymous and self._user == self.factory.userAnonymous:
  722. return GUEST_NAME_OK_NEED_EMAIL
  723. else:
  724. return (USR_NAME_OK_NEED_PASS, username)
  725. # TODO: add max auth try before timeout from ip...
  726. # TODO: need to implement minimal ABOR command
  727. def ftp_PASS(self, password):
  728. """
  729. Second part of login. Get the password the peer wants to
  730. authenticate with.
  731. """
  732. if self.factory.allowAnonymous and self._user == self.factory.userAnonymous:
  733. # anonymous login
  734. creds = credentials.Anonymous()
  735. reply = GUEST_LOGGED_IN_PROCEED
  736. else:
  737. # user login
  738. creds = credentials.UsernamePassword(self._user, password)
  739. reply = USR_LOGGED_IN_PROCEED
  740. del self._user
  741. def _cbLogin(result):
  742. (interface, avatar, logout) = result
  743. assert interface is IFTPShell, "The realm is busted, jerk."
  744. self.shell = avatar
  745. self.logout = logout
  746. self.workingDirectory = []
  747. self.state = self.AUTHED
  748. return reply
  749. def _ebLogin(failure):
  750. failure.trap(cred_error.UnauthorizedLogin, cred_error.UnhandledCredentials)
  751. self.state = self.UNAUTH
  752. raise AuthorizationError
  753. d = self.portal.login(creds, None, IFTPShell)
  754. d.addCallbacks(_cbLogin, _ebLogin)
  755. return d
  756. def ftp_PASV(self):
  757. """
  758. Request for a passive connection
  759. from the rfc::
  760. This command requests the server-DTP to \"listen\" on a data port
  761. (which is not its default data port) and to wait for a connection
  762. rather than initiate one upon receipt of a transfer command. The
  763. response to this command includes the host and port address this
  764. server is listening on.
  765. """
  766. # if we have a DTP port set up, lose it.
  767. if self.dtpFactory is not None:
  768. # cleanupDTP sets dtpFactory to none. Later we'll do
  769. # cleanup here or something.
  770. self.cleanupDTP()
  771. self.dtpFactory = DTPFactory(pi=self)
  772. self.dtpFactory.setTimeout(self.dtpTimeout)
  773. self.dtpPort = self.getDTPPort(self.dtpFactory)
  774. host = self.transport.getHost().host
  775. port = self.dtpPort.getHost().port
  776. self.reply(ENTERING_PASV_MODE, encodeHostPort(host, port))
  777. return self.dtpFactory.deferred.addCallback(lambda ign: None)
  778. def ftp_PORT(self, address):
  779. addr = tuple(map(int, address.split(",")))
  780. ip = "%d.%d.%d.%d" % tuple(addr[:4])
  781. port = addr[4] << 8 | addr[5]
  782. # if we have a DTP port set up, lose it.
  783. if self.dtpFactory is not None:
  784. self.cleanupDTP()
  785. self.dtpFactory = DTPFactory(pi=self, peerHost=self.transport.getPeer().host)
  786. self.dtpFactory.setTimeout(self.dtpTimeout)
  787. self.dtpPort = reactor.connectTCP(ip, port, self.dtpFactory)
  788. def connected(ignored):
  789. return ENTERING_PORT_MODE
  790. def connFailed(err):
  791. err.trap(PortConnectionError)
  792. return CANT_OPEN_DATA_CNX
  793. return self.dtpFactory.deferred.addCallbacks(connected, connFailed)
  794. def _encodeName(self, name):
  795. """
  796. Encode C{name} to be sent over the wire.
  797. This encodes L{unicode} objects as UTF-8 and leaves L{bytes} as-is.
  798. As described by U{RFC 3659 section
  799. 2.2<https://tools.ietf.org/html/rfc3659#section-2.2>}::
  800. Various FTP commands take pathnames as arguments, or return
  801. pathnames in responses. When the MLST command is supported, as
  802. indicated in the response to the FEAT command, pathnames are to be
  803. transferred in one of the following two formats.
  804. pathname = utf-8-name / raw
  805. utf-8-name = <a UTF-8 encoded Unicode string>
  806. raw = <any string that is not a valid UTF-8 encoding>
  807. Which format is used is at the option of the user-PI or server-PI
  808. sending the pathname.
  809. @param name: Name to be encoded.
  810. @type name: L{bytes} or L{unicode}
  811. @return: Wire format of C{name}.
  812. @rtype: L{bytes}
  813. """
  814. if isinstance(name, str):
  815. return name.encode("utf-8")
  816. return name
  817. def ftp_LIST(self, path=""):
  818. """This command causes a list to be sent from the server to the
  819. passive DTP. If the pathname specifies a directory or other
  820. group of files, the server should transfer a list of files
  821. in the specified directory. If the pathname specifies a
  822. file then the server should send current information on the
  823. file. A null argument implies the user's current working or
  824. default directory.
  825. """
  826. # XXX: why is this check different from ftp_RETR/ftp_STOR? See #4180
  827. if self.dtpInstance is None or not self.dtpInstance.isConnected:
  828. return defer.fail(BadCmdSequenceError("must send PORT or PASV before RETR"))
  829. # Various clients send flags like -L or -al etc. We just ignore them.
  830. if path.lower() in ["-a", "-l", "-la", "-al"]:
  831. path = ""
  832. def gotListing(results):
  833. self.reply(DATA_CNX_ALREADY_OPEN_START_XFR)
  834. for (name, attrs) in results:
  835. name = self._encodeName(name)
  836. self.dtpInstance.sendListResponse(name, attrs)
  837. self.dtpInstance.transport.loseConnection()
  838. return (TXFR_COMPLETE_OK,)
  839. try:
  840. segments = toSegments(self.workingDirectory, path)
  841. except InvalidPath:
  842. return defer.fail(FileNotFoundError(path))
  843. d = self.shell.list(
  844. segments,
  845. (
  846. "size",
  847. "directory",
  848. "permissions",
  849. "hardlinks",
  850. "modified",
  851. "owner",
  852. "group",
  853. ),
  854. )
  855. d.addCallback(gotListing)
  856. return d
  857. def ftp_NLST(self, path):
  858. """
  859. This command causes a directory listing to be sent from the server to
  860. the client. The pathname should specify a directory or other
  861. system-specific file group descriptor. An empty path implies the
  862. current working directory. If the path is non-existent, send nothing.
  863. If the path is to a file, send only the file name.
  864. @type path: C{str}
  865. @param path: The path for which a directory listing should be returned.
  866. @rtype: L{Deferred}
  867. @return: a L{Deferred} which will be fired when the listing request
  868. is finished.
  869. """
  870. # XXX: why is this check different from ftp_RETR/ftp_STOR? See #4180
  871. if self.dtpInstance is None or not self.dtpInstance.isConnected:
  872. return defer.fail(BadCmdSequenceError("must send PORT or PASV before RETR"))
  873. try:
  874. segments = toSegments(self.workingDirectory, path)
  875. except InvalidPath:
  876. return defer.fail(FileNotFoundError(path))
  877. def cbList(results, glob):
  878. """
  879. Send, line by line, each matching file in the directory listing,
  880. and then close the connection.
  881. @type results: A C{list} of C{tuple}. The first element of each
  882. C{tuple} is a C{str} and the second element is a C{list}.
  883. @param results: The names of the files in the directory.
  884. @param glob: A shell-style glob through which to filter results
  885. (see U{http://docs.python.org/2/library/fnmatch.html}), or
  886. L{None} for no filtering.
  887. @type glob: L{str} or L{None}
  888. @return: A C{tuple} containing the status code for a successful
  889. transfer.
  890. @rtype: C{tuple}
  891. """
  892. self.reply(DATA_CNX_ALREADY_OPEN_START_XFR)
  893. for (name, ignored) in results:
  894. if not glob or (glob and fnmatch.fnmatch(name, glob)):
  895. name = self._encodeName(name)
  896. self.dtpInstance.sendLine(name)
  897. self.dtpInstance.transport.loseConnection()
  898. return (TXFR_COMPLETE_OK,)
  899. def listErr(results):
  900. """
  901. RFC 959 specifies that an NLST request may only return directory
  902. listings. Thus, send nothing and just close the connection.
  903. @type results: L{Failure}
  904. @param results: The L{Failure} wrapping a L{FileNotFoundError} that
  905. occurred while trying to list the contents of a nonexistent
  906. directory.
  907. @returns: A C{tuple} containing the status code for a successful
  908. transfer.
  909. @rtype: C{tuple}
  910. """
  911. self.dtpInstance.transport.loseConnection()
  912. return (TXFR_COMPLETE_OK,)
  913. if _isGlobbingExpression(segments):
  914. # Remove globbing expression from path
  915. # and keep to be used for filtering.
  916. glob = segments.pop()
  917. else:
  918. glob = None
  919. d = self.shell.list(segments)
  920. d.addCallback(cbList, glob)
  921. # self.shell.list will generate an error if the path is invalid
  922. d.addErrback(listErr)
  923. return d
  924. def ftp_CWD(self, path):
  925. try:
  926. segments = toSegments(self.workingDirectory, path)
  927. except InvalidPath:
  928. # XXX Eh, what to fail with here?
  929. return defer.fail(FileNotFoundError(path))
  930. def accessGranted(result):
  931. self.workingDirectory = segments
  932. return (REQ_FILE_ACTN_COMPLETED_OK,)
  933. return self.shell.access(segments).addCallback(accessGranted)
  934. def ftp_CDUP(self):
  935. return self.ftp_CWD("..")
  936. def ftp_PWD(self):
  937. return (PWD_REPLY, "/" + "/".join(self.workingDirectory))
  938. def ftp_RETR(self, path):
  939. """
  940. This command causes the content of a file to be sent over the data
  941. transfer channel. If the path is to a folder, an error will be raised.
  942. @type path: C{str}
  943. @param path: The path to the file which should be transferred over the
  944. data transfer channel.
  945. @rtype: L{Deferred}
  946. @return: a L{Deferred} which will be fired when the transfer is done.
  947. """
  948. if self.dtpInstance is None:
  949. raise BadCmdSequenceError("PORT or PASV required before RETR")
  950. try:
  951. newsegs = toSegments(self.workingDirectory, path)
  952. except InvalidPath:
  953. return defer.fail(FileNotFoundError(path))
  954. # XXX For now, just disable the timeout. Later we'll want to
  955. # leave it active and have the DTP connection reset it
  956. # periodically.
  957. self.setTimeout(None)
  958. # Put it back later
  959. def enableTimeout(result):
  960. self.setTimeout(self.factory.timeOut)
  961. return result
  962. # And away she goes
  963. if not self.binary:
  964. cons = ASCIIConsumerWrapper(self.dtpInstance)
  965. else:
  966. cons = self.dtpInstance
  967. def cbSent(result):
  968. return (TXFR_COMPLETE_OK,)
  969. def ebSent(err):
  970. log.msg("Unexpected error attempting to transmit file to client:")
  971. log.err(err)
  972. if err.check(FTPCmdError):
  973. return err
  974. return (CNX_CLOSED_TXFR_ABORTED,)
  975. def cbOpened(file):
  976. # Tell them what to doooo
  977. if self.dtpInstance.isConnected:
  978. self.reply(DATA_CNX_ALREADY_OPEN_START_XFR)
  979. else:
  980. self.reply(FILE_STATUS_OK_OPEN_DATA_CNX)
  981. d = file.send(cons)
  982. d.addCallbacks(cbSent, ebSent)
  983. return d
  984. def ebOpened(err):
  985. if not err.check(
  986. PermissionDeniedError, FileNotFoundError, IsADirectoryError
  987. ):
  988. log.msg("Unexpected error attempting to open file for " "transmission:")
  989. log.err(err)
  990. if err.check(FTPCmdError):
  991. return (err.value.errorCode, "/".join(newsegs))
  992. return (FILE_NOT_FOUND, "/".join(newsegs))
  993. d = self.shell.openForReading(newsegs)
  994. d.addCallbacks(cbOpened, ebOpened)
  995. d.addBoth(enableTimeout)
  996. # Pass back Deferred that fires when the transfer is done
  997. return d
  998. def ftp_STOR(self, path):
  999. """
  1000. STORE (STOR)
  1001. This command causes the server-DTP to accept the data
  1002. transferred via the data connection and to store the data as
  1003. a file at the server site. If the file specified in the
  1004. pathname exists at the server site, then its contents shall
  1005. be replaced by the data being transferred. A new file is
  1006. created at the server site if the file specified in the
  1007. pathname does not already exist.
  1008. """
  1009. if self.dtpInstance is None:
  1010. raise BadCmdSequenceError("PORT or PASV required before STOR")
  1011. try:
  1012. newsegs = toSegments(self.workingDirectory, path)
  1013. except InvalidPath:
  1014. return defer.fail(FileNotFoundError(path))
  1015. # XXX For now, just disable the timeout. Later we'll want to
  1016. # leave it active and have the DTP connection reset it
  1017. # periodically.
  1018. self.setTimeout(None)
  1019. # Put it back later
  1020. def enableTimeout(result):
  1021. self.setTimeout(self.factory.timeOut)
  1022. return result
  1023. def cbOpened(file):
  1024. """
  1025. File was open for reading. Launch the data transfer channel via
  1026. the file consumer.
  1027. """
  1028. d = file.receive()
  1029. d.addCallback(cbConsumer)
  1030. d.addCallback(lambda ignored: file.close())
  1031. d.addCallbacks(cbSent, ebSent)
  1032. return d
  1033. def ebOpened(err):
  1034. """
  1035. Called when failed to open the file for reading.
  1036. For known errors, return the FTP error code.
  1037. For all other, return a file not found error.
  1038. """
  1039. if isinstance(err.value, FTPCmdError):
  1040. return (err.value.errorCode, "/".join(newsegs))
  1041. log.err(err, "Unexpected error received while opening file:")
  1042. return (FILE_NOT_FOUND, "/".join(newsegs))
  1043. def cbConsumer(cons):
  1044. """
  1045. Called after the file was opended for reading.
  1046. Prepare the data transfer channel and send the response
  1047. to the command channel.
  1048. """
  1049. if not self.binary:
  1050. cons = ASCIIConsumerWrapper(cons)
  1051. d = self.dtpInstance.registerConsumer(cons)
  1052. # Tell them what to doooo
  1053. if self.dtpInstance.isConnected:
  1054. self.reply(DATA_CNX_ALREADY_OPEN_START_XFR)
  1055. else:
  1056. self.reply(FILE_STATUS_OK_OPEN_DATA_CNX)
  1057. return d
  1058. def cbSent(result):
  1059. """
  1060. Called from data transport when transfer is done.
  1061. """
  1062. return (TXFR_COMPLETE_OK,)
  1063. def ebSent(err):
  1064. """
  1065. Called from data transport when there are errors during the
  1066. transfer.
  1067. """
  1068. log.err(err, "Unexpected error received during transfer:")
  1069. if err.check(FTPCmdError):
  1070. return err
  1071. return (CNX_CLOSED_TXFR_ABORTED,)
  1072. d = self.shell.openForWriting(newsegs)
  1073. d.addCallbacks(cbOpened, ebOpened)
  1074. d.addBoth(enableTimeout)
  1075. # Pass back Deferred that fires when the transfer is done
  1076. return d
  1077. def ftp_SIZE(self, path):
  1078. """
  1079. File SIZE
  1080. The FTP command, SIZE OF FILE (SIZE), is used to obtain the transfer
  1081. size of a file from the server-FTP process. This is the exact number
  1082. of octets (8 bit bytes) that would be transmitted over the data
  1083. connection should that file be transmitted. This value will change
  1084. depending on the current STRUcture, MODE, and TYPE of the data
  1085. connection or of a data connection that would be created were one
  1086. created now. Thus, the result of the SIZE command is dependent on
  1087. the currently established STRU, MODE, and TYPE parameters.
  1088. The SIZE command returns how many octets would be transferred if the
  1089. file were to be transferred using the current transfer structure,
  1090. mode, and type. This command is normally used in conjunction with
  1091. the RESTART (REST) command when STORing a file to a remote server in
  1092. STREAM mode, to determine the restart point. The server-PI might
  1093. need to read the partially transferred file, do any appropriate
  1094. conversion, and count the number of octets that would be generated
  1095. when sending the file in order to correctly respond to this command.
  1096. Estimates of the file transfer size MUST NOT be returned; only
  1097. precise information is acceptable.
  1098. http://tools.ietf.org/html/rfc3659
  1099. """
  1100. try:
  1101. newsegs = toSegments(self.workingDirectory, path)
  1102. except InvalidPath:
  1103. return defer.fail(FileNotFoundError(path))
  1104. def cbStat(result):
  1105. (size,) = result
  1106. return (FILE_STATUS, str(size))
  1107. return self.shell.stat(newsegs, ("size",)).addCallback(cbStat)
  1108. def ftp_MDTM(self, path):
  1109. """
  1110. File Modification Time (MDTM)
  1111. The FTP command, MODIFICATION TIME (MDTM), can be used to determine
  1112. when a file in the server NVFS was last modified. This command has
  1113. existed in many FTP servers for many years, as an adjunct to the REST
  1114. command for STREAM mode, thus is widely available. However, where
  1115. supported, the "modify" fact that can be provided in the result from
  1116. the new MLST command is recommended as a superior alternative.
  1117. http://tools.ietf.org/html/rfc3659
  1118. """
  1119. try:
  1120. newsegs = toSegments(self.workingDirectory, path)
  1121. except InvalidPath:
  1122. return defer.fail(FileNotFoundError(path))
  1123. def cbStat(result):
  1124. (modified,) = result
  1125. return (FILE_STATUS, time.strftime("%Y%m%d%H%M%S", time.gmtime(modified)))
  1126. return self.shell.stat(newsegs, ("modified",)).addCallback(cbStat)
  1127. def ftp_TYPE(self, type):
  1128. """
  1129. REPRESENTATION TYPE (TYPE)
  1130. The argument specifies the representation type as described
  1131. in the Section on Data Representation and Storage. Several
  1132. types take a second parameter. The first parameter is
  1133. denoted by a single Telnet character, as is the second
  1134. Format parameter for ASCII and EBCDIC; the second parameter
  1135. for local byte is a decimal integer to indicate Bytesize.
  1136. The parameters are separated by a <SP> (Space, ASCII code
  1137. 32).
  1138. """
  1139. p = type.upper()
  1140. if p:
  1141. f = getattr(self, "type_" + p[0], None)
  1142. if f is not None:
  1143. return f(p[1:])
  1144. return self.type_UNKNOWN(p)
  1145. return (SYNTAX_ERR,)
  1146. def type_A(self, code):
  1147. if code == "" or code == "N":
  1148. self.binary = False
  1149. return (TYPE_SET_OK, "A" + code)
  1150. else:
  1151. return defer.fail(CmdArgSyntaxError(code))
  1152. def type_I(self, code):
  1153. if code == "":
  1154. self.binary = True
  1155. return (TYPE_SET_OK, "I")
  1156. else:
  1157. return defer.fail(CmdArgSyntaxError(code))
  1158. def type_UNKNOWN(self, code):
  1159. return defer.fail(CmdNotImplementedForArgError(code))
  1160. def ftp_SYST(self):
  1161. return NAME_SYS_TYPE
  1162. def ftp_STRU(self, structure):
  1163. p = structure.upper()
  1164. if p == "F":
  1165. return (CMD_OK,)
  1166. return defer.fail(CmdNotImplementedForArgError(structure))
  1167. def ftp_MODE(self, mode):
  1168. p = mode.upper()
  1169. if p == "S":
  1170. return (CMD_OK,)
  1171. return defer.fail(CmdNotImplementedForArgError(mode))
  1172. def ftp_MKD(self, path):
  1173. try:
  1174. newsegs = toSegments(self.workingDirectory, path)
  1175. except InvalidPath:
  1176. return defer.fail(FileNotFoundError(path))
  1177. return self.shell.makeDirectory(newsegs).addCallback(
  1178. lambda ign: (MKD_REPLY, path)
  1179. )
  1180. def ftp_RMD(self, path):
  1181. try:
  1182. newsegs = toSegments(self.workingDirectory, path)
  1183. except InvalidPath:
  1184. return defer.fail(FileNotFoundError(path))
  1185. return self.shell.removeDirectory(newsegs).addCallback(
  1186. lambda ign: (REQ_FILE_ACTN_COMPLETED_OK,)
  1187. )
  1188. def ftp_DELE(self, path):
  1189. try:
  1190. newsegs = toSegments(self.workingDirectory, path)
  1191. except InvalidPath:
  1192. return defer.fail(FileNotFoundError(path))
  1193. return self.shell.removeFile(newsegs).addCallback(
  1194. lambda ign: (REQ_FILE_ACTN_COMPLETED_OK,)
  1195. )
  1196. def ftp_NOOP(self):
  1197. return (CMD_OK,)
  1198. def ftp_RNFR(self, fromName):
  1199. self._fromName = fromName
  1200. self.state = self.RENAMING
  1201. return (REQ_FILE_ACTN_PENDING_FURTHER_INFO,)
  1202. def ftp_RNTO(self, toName):
  1203. fromName = self._fromName
  1204. del self._fromName
  1205. self.state = self.AUTHED
  1206. try:
  1207. fromsegs = toSegments(self.workingDirectory, fromName)
  1208. tosegs = toSegments(self.workingDirectory, toName)
  1209. except InvalidPath:
  1210. return defer.fail(FileNotFoundError(fromName))
  1211. return self.shell.rename(fromsegs, tosegs).addCallback(
  1212. lambda ign: (REQ_FILE_ACTN_COMPLETED_OK,)
  1213. )
  1214. def ftp_FEAT(self):
  1215. """
  1216. Advertise the features supported by the server.
  1217. http://tools.ietf.org/html/rfc2389
  1218. """
  1219. self.sendLine(RESPONSE[FEAT_OK][0])
  1220. for feature in self.FEATURES:
  1221. self.sendLine(" " + feature)
  1222. self.sendLine(RESPONSE[FEAT_OK][1])
  1223. def ftp_OPTS(self, option):
  1224. """
  1225. Handle OPTS command.
  1226. http://tools.ietf.org/html/draft-ietf-ftpext-utf-8-option-00
  1227. """
  1228. return self.reply(OPTS_NOT_IMPLEMENTED, option)
  1229. def ftp_QUIT(self):
  1230. self.reply(GOODBYE_MSG)
  1231. self.transport.loseConnection()
  1232. self.disconnected = True
  1233. def cleanupDTP(self):
  1234. """
  1235. Call when DTP connection exits
  1236. """
  1237. log.msg("cleanupDTP", debug=True)
  1238. log.msg(self.dtpPort)
  1239. dtpPort, self.dtpPort = self.dtpPort, None
  1240. if interfaces.IListeningPort.providedBy(dtpPort):
  1241. dtpPort.stopListening()
  1242. elif interfaces.IConnector.providedBy(dtpPort):
  1243. dtpPort.disconnect()
  1244. else:
  1245. assert False, (
  1246. "dtpPort should be an IListeningPort or IConnector, "
  1247. "instead is %r" % (dtpPort,)
  1248. )
  1249. self.dtpFactory.stopFactory()
  1250. self.dtpFactory = None
  1251. if self.dtpInstance is not None:
  1252. self.dtpInstance = None
  1253. class FTPFactory(policies.LimitTotalConnectionsFactory):
  1254. """
  1255. A factory for producing ftp protocol instances
  1256. @ivar timeOut: the protocol interpreter's idle timeout time in seconds,
  1257. default is 600 seconds.
  1258. @ivar passivePortRange: value forwarded to C{protocol.passivePortRange}.
  1259. @type passivePortRange: C{iterator}
  1260. """
  1261. protocol = FTP
  1262. overflowProtocol = FTPOverflowProtocol
  1263. allowAnonymous = True
  1264. userAnonymous = "anonymous"
  1265. timeOut = 600
  1266. welcomeMessage = f"Twisted {copyright.version} FTP Server"
  1267. passivePortRange = range(0, 1)
  1268. def __init__(self, portal=None, userAnonymous="anonymous"):
  1269. self.portal = portal
  1270. self.userAnonymous = userAnonymous
  1271. self.instances = []
  1272. def buildProtocol(self, addr):
  1273. p = policies.LimitTotalConnectionsFactory.buildProtocol(self, addr)
  1274. if p is not None:
  1275. p.wrappedProtocol.portal = self.portal
  1276. p.wrappedProtocol.timeOut = self.timeOut
  1277. p.wrappedProtocol.passivePortRange = self.passivePortRange
  1278. return p
  1279. def stopFactory(self):
  1280. # make sure ftp instance's timeouts are set to None
  1281. # to avoid reactor complaints
  1282. [p.setTimeout(None) for p in self.instances if p.timeOut is not None]
  1283. policies.LimitTotalConnectionsFactory.stopFactory(self)
  1284. # -- Cred Objects --
  1285. class IFTPShell(Interface):
  1286. """
  1287. An abstraction of the shell commands used by the FTP protocol for
  1288. a given user account.
  1289. All path names must be absolute.
  1290. """
  1291. def makeDirectory(path):
  1292. """
  1293. Create a directory.
  1294. @param path: The path, as a list of segments, to create
  1295. @type path: C{list} of C{unicode}
  1296. @return: A Deferred which fires when the directory has been
  1297. created, or which fails if the directory cannot be created.
  1298. """
  1299. def removeDirectory(path):
  1300. """
  1301. Remove a directory.
  1302. @param path: The path, as a list of segments, to remove
  1303. @type path: C{list} of C{unicode}
  1304. @return: A Deferred which fires when the directory has been
  1305. removed, or which fails if the directory cannot be removed.
  1306. """
  1307. def removeFile(path):
  1308. """
  1309. Remove a file.
  1310. @param path: The path, as a list of segments, to remove
  1311. @type path: C{list} of C{unicode}
  1312. @return: A Deferred which fires when the file has been
  1313. removed, or which fails if the file cannot be removed.
  1314. """
  1315. def rename(fromPath, toPath):
  1316. """
  1317. Rename a file or directory.
  1318. @param fromPath: The current name of the path.
  1319. @type fromPath: C{list} of C{unicode}
  1320. @param toPath: The desired new name of the path.
  1321. @type toPath: C{list} of C{unicode}
  1322. @return: A Deferred which fires when the path has been
  1323. renamed, or which fails if the path cannot be renamed.
  1324. """
  1325. def access(path):
  1326. """
  1327. Determine whether access to the given path is allowed.
  1328. @param path: The path, as a list of segments
  1329. @return: A Deferred which fires with None if access is allowed
  1330. or which fails with a specific exception type if access is
  1331. denied.
  1332. """
  1333. def stat(path, keys=()):
  1334. """
  1335. Retrieve information about the given path.
  1336. This is like list, except it will never return results about
  1337. child paths.
  1338. """
  1339. def list(path, keys=()):
  1340. """
  1341. Retrieve information about the given path.
  1342. If the path represents a non-directory, the result list should
  1343. have only one entry with information about that non-directory.
  1344. Otherwise, the result list should have an element for each
  1345. child of the directory.
  1346. @param path: The path, as a list of segments, to list
  1347. @type path: C{list} of C{unicode} or C{bytes}
  1348. @param keys: A tuple of keys desired in the resulting
  1349. dictionaries.
  1350. @return: A Deferred which fires with a list of (name, list),
  1351. where the name is the name of the entry as a unicode string or
  1352. bytes and each list contains values corresponding to the requested
  1353. keys. The following are possible elements of keys, and the
  1354. values which should be returned for them:
  1355. - C{'size'}: size in bytes, as an integer (this is kinda required)
  1356. - C{'directory'}: boolean indicating the type of this entry
  1357. - C{'permissions'}: a bitvector (see os.stat(foo).st_mode)
  1358. - C{'hardlinks'}: Number of hard links to this entry
  1359. - C{'modified'}: number of seconds since the epoch since entry was
  1360. modified
  1361. - C{'owner'}: string indicating the user owner of this entry
  1362. - C{'group'}: string indicating the group owner of this entry
  1363. """
  1364. def openForReading(path):
  1365. """
  1366. @param path: The path, as a list of segments, to open
  1367. @type path: C{list} of C{unicode}
  1368. @rtype: C{Deferred} which will fire with L{IReadFile}
  1369. """
  1370. def openForWriting(path):
  1371. """
  1372. @param path: The path, as a list of segments, to open
  1373. @type path: C{list} of C{unicode}
  1374. @rtype: C{Deferred} which will fire with L{IWriteFile}
  1375. """
  1376. class IReadFile(Interface):
  1377. """
  1378. A file out of which bytes may be read.
  1379. """
  1380. def send(consumer):
  1381. """
  1382. Produce the contents of the given path to the given consumer. This
  1383. method may only be invoked once on each provider.
  1384. @type consumer: C{IConsumer}
  1385. @return: A Deferred which fires when the file has been
  1386. consumed completely.
  1387. """
  1388. class IWriteFile(Interface):
  1389. """
  1390. A file into which bytes may be written.
  1391. """
  1392. def receive():
  1393. """
  1394. Create a consumer which will write to this file. This method may
  1395. only be invoked once on each provider.
  1396. @rtype: C{Deferred} of C{IConsumer}
  1397. """
  1398. def close():
  1399. """
  1400. Perform any post-write work that needs to be done. This method may
  1401. only be invoked once on each provider, and will always be invoked
  1402. after receive().
  1403. @rtype: C{Deferred} of anything: the value is ignored. The FTP client
  1404. will not see their upload request complete until this Deferred has
  1405. been fired.
  1406. """
  1407. def _getgroups(uid):
  1408. """
  1409. Return the primary and supplementary groups for the given UID.
  1410. @type uid: C{int}
  1411. """
  1412. result = []
  1413. pwent = pwd.getpwuid(uid)
  1414. result.append(pwent.pw_gid)
  1415. for grent in grp.getgrall():
  1416. if pwent.pw_name in grent.gr_mem:
  1417. result.append(grent.gr_gid)
  1418. return result
  1419. def _testPermissions(uid, gid, spath, mode="r"):
  1420. """
  1421. checks to see if uid has proper permissions to access path with mode
  1422. @type uid: C{int}
  1423. @param uid: numeric user id
  1424. @type gid: C{int}
  1425. @param gid: numeric group id
  1426. @type spath: C{str}
  1427. @param spath: the path on the server to test
  1428. @type mode: C{str}
  1429. @param mode: 'r' or 'w' (read or write)
  1430. @rtype: C{bool}
  1431. @return: True if the given credentials have the specified form of
  1432. access to the given path
  1433. """
  1434. if mode == "r":
  1435. usr = stat.S_IRUSR
  1436. grp = stat.S_IRGRP
  1437. oth = stat.S_IROTH
  1438. amode = os.R_OK
  1439. elif mode == "w":
  1440. usr = stat.S_IWUSR
  1441. grp = stat.S_IWGRP
  1442. oth = stat.S_IWOTH
  1443. amode = os.W_OK
  1444. else:
  1445. raise ValueError(f"Invalid mode {mode!r}: must specify 'r' or 'w'")
  1446. access = False
  1447. if os.path.exists(spath):
  1448. if uid == 0:
  1449. access = True
  1450. else:
  1451. s = os.stat(spath)
  1452. if usr & s.st_mode and uid == s.st_uid:
  1453. access = True
  1454. elif grp & s.st_mode and gid in _getgroups(uid):
  1455. access = True
  1456. elif oth & s.st_mode:
  1457. access = True
  1458. if access:
  1459. if not os.access(spath, amode):
  1460. access = False
  1461. log.msg(
  1462. "Filesystem grants permission to UID %d but it is "
  1463. "inaccessible to me running as UID %d" % (uid, os.getuid())
  1464. )
  1465. return access
  1466. @implementer(IFTPShell)
  1467. class FTPAnonymousShell:
  1468. """
  1469. An anonymous implementation of IFTPShell
  1470. @type filesystemRoot: L{twisted.python.filepath.FilePath}
  1471. @ivar filesystemRoot: The path which is considered the root of
  1472. this shell.
  1473. """
  1474. def __init__(self, filesystemRoot):
  1475. self.filesystemRoot = filesystemRoot
  1476. def _path(self, path):
  1477. return self.filesystemRoot.descendant(path)
  1478. def makeDirectory(self, path):
  1479. return defer.fail(AnonUserDeniedError())
  1480. def removeDirectory(self, path):
  1481. return defer.fail(AnonUserDeniedError())
  1482. def removeFile(self, path):
  1483. return defer.fail(AnonUserDeniedError())
  1484. def rename(self, fromPath, toPath):
  1485. return defer.fail(AnonUserDeniedError())
  1486. def receive(self, path):
  1487. path = self._path(path)
  1488. return defer.fail(AnonUserDeniedError())
  1489. def openForReading(self, path):
  1490. """
  1491. Open C{path} for reading.
  1492. @param path: The path, as a list of segments, to open.
  1493. @type path: C{list} of C{unicode}
  1494. @return: A L{Deferred} is returned that will fire with an object
  1495. implementing L{IReadFile} if the file is successfully opened. If
  1496. C{path} is a directory, or if an exception is raised while trying
  1497. to open the file, the L{Deferred} will fire with an error.
  1498. """
  1499. p = self._path(path)
  1500. if p.isdir():
  1501. # Normally, we would only check for EISDIR in open, but win32
  1502. # returns EACCES in this case, so we check before
  1503. return defer.fail(IsADirectoryError(path))
  1504. try:
  1505. f = p.open("r")
  1506. except OSError as e:
  1507. return errnoToFailure(e.errno, path)
  1508. except BaseException:
  1509. return defer.fail()
  1510. else:
  1511. return defer.succeed(_FileReader(f))
  1512. def openForWriting(self, path):
  1513. """
  1514. Reject write attempts by anonymous users with
  1515. L{PermissionDeniedError}.
  1516. """
  1517. return defer.fail(PermissionDeniedError("STOR not allowed"))
  1518. def access(self, path):
  1519. p = self._path(path)
  1520. if not p.exists():
  1521. # Again, win32 doesn't report a sane error after, so let's fail
  1522. # early if we can
  1523. return defer.fail(FileNotFoundError(path))
  1524. # For now, just see if we can os.listdir() it
  1525. try:
  1526. p.listdir()
  1527. except OSError as e:
  1528. return errnoToFailure(e.errno, path)
  1529. except BaseException:
  1530. return defer.fail()
  1531. else:
  1532. return defer.succeed(None)
  1533. def stat(self, path, keys=()):
  1534. p = self._path(path)
  1535. if p.isdir():
  1536. try:
  1537. statResult = self._statNode(p, keys)
  1538. except OSError as e:
  1539. return errnoToFailure(e.errno, path)
  1540. except BaseException:
  1541. return defer.fail()
  1542. else:
  1543. return defer.succeed(statResult)
  1544. else:
  1545. return self.list(path, keys).addCallback(lambda res: res[0][1])
  1546. def list(self, path, keys=()):
  1547. """
  1548. Return the list of files at given C{path}, adding C{keys} stat
  1549. informations if specified.
  1550. @param path: the directory or file to check.
  1551. @type path: C{str}
  1552. @param keys: the list of desired metadata
  1553. @type keys: C{list} of C{str}
  1554. """
  1555. filePath = self._path(path)
  1556. if filePath.isdir():
  1557. entries = filePath.listdir()
  1558. fileEntries = [filePath.child(p) for p in entries]
  1559. elif filePath.isfile():
  1560. entries = [os.path.join(*filePath.segmentsFrom(self.filesystemRoot))]
  1561. fileEntries = [filePath]
  1562. else:
  1563. return defer.fail(FileNotFoundError(path))
  1564. results = []
  1565. for fileName, filePath in zip(entries, fileEntries):
  1566. ent = []
  1567. results.append((fileName, ent))
  1568. if keys:
  1569. try:
  1570. ent.extend(self._statNode(filePath, keys))
  1571. except OSError as e:
  1572. return errnoToFailure(e.errno, fileName)
  1573. except BaseException:
  1574. return defer.fail()
  1575. return defer.succeed(results)
  1576. def _statNode(self, filePath, keys):
  1577. """
  1578. Shortcut method to get stat info on a node.
  1579. @param filePath: the node to stat.
  1580. @type filePath: C{filepath.FilePath}
  1581. @param keys: the stat keys to get.
  1582. @type keys: C{iterable}
  1583. """
  1584. filePath.restat()
  1585. return [getattr(self, "_stat_" + k)(filePath) for k in keys]
  1586. def _stat_size(self, fp):
  1587. """
  1588. Get the filepath's size as an int
  1589. @param fp: L{twisted.python.filepath.FilePath}
  1590. @return: C{int} representing the size
  1591. """
  1592. return fp.getsize()
  1593. def _stat_permissions(self, fp):
  1594. """
  1595. Get the filepath's permissions object
  1596. @param fp: L{twisted.python.filepath.FilePath}
  1597. @return: L{twisted.python.filepath.Permissions} of C{fp}
  1598. """
  1599. return fp.getPermissions()
  1600. def _stat_hardlinks(self, fp):
  1601. """
  1602. Get the number of hardlinks for the filepath - if the number of
  1603. hardlinks is not yet implemented (say in Windows), just return 0 since
  1604. stat-ing a file in Windows seems to return C{st_nlink=0}.
  1605. (Reference:
  1606. U{http://stackoverflow.com/questions/5275731/os-stat-on-windows})
  1607. @param fp: L{twisted.python.filepath.FilePath}
  1608. @return: C{int} representing the number of hardlinks
  1609. """
  1610. try:
  1611. return fp.getNumberOfHardLinks()
  1612. except NotImplementedError:
  1613. return 0
  1614. def _stat_modified(self, fp):
  1615. """
  1616. Get the filepath's last modified date
  1617. @param fp: L{twisted.python.filepath.FilePath}
  1618. @return: C{int} as seconds since the epoch
  1619. """
  1620. return fp.getModificationTime()
  1621. def _stat_owner(self, fp):
  1622. """
  1623. Get the filepath's owner's username. If this is not implemented
  1624. (say in Windows) return the string "0" since stat-ing a file in
  1625. Windows seems to return C{st_uid=0}.
  1626. (Reference:
  1627. U{http://stackoverflow.com/questions/5275731/os-stat-on-windows})
  1628. @param fp: L{twisted.python.filepath.FilePath}
  1629. @return: C{str} representing the owner's username
  1630. """
  1631. try:
  1632. userID = fp.getUserID()
  1633. except NotImplementedError:
  1634. return "0"
  1635. else:
  1636. if pwd is not None:
  1637. try:
  1638. return pwd.getpwuid(userID)[0]
  1639. except KeyError:
  1640. pass
  1641. return str(userID)
  1642. def _stat_group(self, fp):
  1643. """
  1644. Get the filepath's owner's group. If this is not implemented
  1645. (say in Windows) return the string "0" since stat-ing a file in
  1646. Windows seems to return C{st_gid=0}.
  1647. (Reference:
  1648. U{http://stackoverflow.com/questions/5275731/os-stat-on-windows})
  1649. @param fp: L{twisted.python.filepath.FilePath}
  1650. @return: C{str} representing the owner's group
  1651. """
  1652. try:
  1653. groupID = fp.getGroupID()
  1654. except NotImplementedError:
  1655. return "0"
  1656. else:
  1657. if grp is not None:
  1658. try:
  1659. return grp.getgrgid(groupID)[0]
  1660. except KeyError:
  1661. pass
  1662. return str(groupID)
  1663. def _stat_directory(self, fp):
  1664. """
  1665. Get whether the filepath is a directory
  1666. @param fp: L{twisted.python.filepath.FilePath}
  1667. @return: C{bool}
  1668. """
  1669. return fp.isdir()
  1670. @implementer(IReadFile)
  1671. class _FileReader:
  1672. def __init__(self, fObj):
  1673. self.fObj = fObj
  1674. self._send = False
  1675. def _close(self, passthrough):
  1676. self._send = True
  1677. self.fObj.close()
  1678. return passthrough
  1679. def send(self, consumer):
  1680. assert not self._send, "Can only call IReadFile.send *once* per instance"
  1681. self._send = True
  1682. d = basic.FileSender().beginFileTransfer(self.fObj, consumer)
  1683. d.addBoth(self._close)
  1684. return d
  1685. class FTPShell(FTPAnonymousShell):
  1686. """
  1687. An authenticated implementation of L{IFTPShell}.
  1688. """
  1689. def makeDirectory(self, path):
  1690. p = self._path(path)
  1691. try:
  1692. p.makedirs()
  1693. except OSError as e:
  1694. return errnoToFailure(e.errno, path)
  1695. except BaseException:
  1696. return defer.fail()
  1697. else:
  1698. return defer.succeed(None)
  1699. def removeDirectory(self, path):
  1700. p = self._path(path)
  1701. if p.isfile():
  1702. # Win32 returns the wrong errno when rmdir is called on a file
  1703. # instead of a directory, so as we have the info here, let's fail
  1704. # early with a pertinent error
  1705. return defer.fail(IsNotADirectoryError(path))
  1706. try:
  1707. os.rmdir(p.path)
  1708. except OSError as e:
  1709. return errnoToFailure(e.errno, path)
  1710. except BaseException:
  1711. return defer.fail()
  1712. else:
  1713. return defer.succeed(None)
  1714. def removeFile(self, path):
  1715. p = self._path(path)
  1716. if p.isdir():
  1717. # Win32 returns the wrong errno when remove is called on a
  1718. # directory instead of a file, so as we have the info here,
  1719. # let's fail early with a pertinent error
  1720. return defer.fail(IsADirectoryError(path))
  1721. try:
  1722. p.remove()
  1723. except OSError as e:
  1724. return errnoToFailure(e.errno, path)
  1725. except BaseException:
  1726. return defer.fail()
  1727. else:
  1728. return defer.succeed(None)
  1729. def rename(self, fromPath, toPath):
  1730. fp = self._path(fromPath)
  1731. tp = self._path(toPath)
  1732. try:
  1733. os.rename(fp.path, tp.path)
  1734. except OSError as e:
  1735. return errnoToFailure(e.errno, fromPath)
  1736. except BaseException:
  1737. return defer.fail()
  1738. else:
  1739. return defer.succeed(None)
  1740. def openForWriting(self, path):
  1741. """
  1742. Open C{path} for writing.
  1743. @param path: The path, as a list of segments, to open.
  1744. @type path: C{list} of C{unicode}
  1745. @return: A L{Deferred} is returned that will fire with an object
  1746. implementing L{IWriteFile} if the file is successfully opened. If
  1747. C{path} is a directory, or if an exception is raised while trying
  1748. to open the file, the L{Deferred} will fire with an error.
  1749. """
  1750. p = self._path(path)
  1751. if p.isdir():
  1752. # Normally, we would only check for EISDIR in open, but win32
  1753. # returns EACCES in this case, so we check before
  1754. return defer.fail(IsADirectoryError(path))
  1755. try:
  1756. fObj = p.open("w")
  1757. except OSError as e:
  1758. return errnoToFailure(e.errno, path)
  1759. except BaseException:
  1760. return defer.fail()
  1761. return defer.succeed(_FileWriter(fObj))
  1762. @implementer(IWriteFile)
  1763. class _FileWriter:
  1764. def __init__(self, fObj):
  1765. self.fObj = fObj
  1766. self._receive = False
  1767. def receive(self):
  1768. assert not self._receive, "Can only call IWriteFile.receive *once* per instance"
  1769. self._receive = True
  1770. # FileConsumer will close the file object
  1771. return defer.succeed(FileConsumer(self.fObj))
  1772. def close(self):
  1773. return defer.succeed(None)
  1774. @implementer(portal.IRealm)
  1775. class BaseFTPRealm:
  1776. """
  1777. Base class for simple FTP realms which provides an easy hook for specifying
  1778. the home directory for each user.
  1779. """
  1780. def __init__(self, anonymousRoot):
  1781. self.anonymousRoot = filepath.FilePath(anonymousRoot)
  1782. def getHomeDirectory(self, avatarId):
  1783. """
  1784. Return a L{FilePath} representing the home directory of the given
  1785. avatar. Override this in a subclass.
  1786. @param avatarId: A user identifier returned from a credentials checker.
  1787. @type avatarId: C{str}
  1788. @rtype: L{FilePath}
  1789. """
  1790. raise NotImplementedError(
  1791. f"{self.__class__!r} did not override getHomeDirectory"
  1792. )
  1793. def requestAvatar(self, avatarId, mind, *interfaces):
  1794. for iface in interfaces:
  1795. if iface is IFTPShell:
  1796. if avatarId is checkers.ANONYMOUS:
  1797. avatar = FTPAnonymousShell(self.anonymousRoot)
  1798. else:
  1799. avatar = FTPShell(self.getHomeDirectory(avatarId))
  1800. return (IFTPShell, avatar, getattr(avatar, "logout", lambda: None))
  1801. raise NotImplementedError("Only IFTPShell interface is supported by this realm")
  1802. class FTPRealm(BaseFTPRealm):
  1803. """
  1804. @type anonymousRoot: L{twisted.python.filepath.FilePath}
  1805. @ivar anonymousRoot: Root of the filesystem to which anonymous
  1806. users will be granted access.
  1807. @type userHome: L{filepath.FilePath}
  1808. @ivar userHome: Root of the filesystem containing user home directories.
  1809. """
  1810. def __init__(self, anonymousRoot, userHome="/home"):
  1811. BaseFTPRealm.__init__(self, anonymousRoot)
  1812. self.userHome = filepath.FilePath(userHome)
  1813. def getHomeDirectory(self, avatarId):
  1814. """
  1815. Use C{avatarId} as a single path segment to construct a child of
  1816. C{self.userHome} and return that child.
  1817. """
  1818. return self.userHome.child(avatarId)
  1819. class SystemFTPRealm(BaseFTPRealm):
  1820. """
  1821. L{SystemFTPRealm} uses system user account information to decide what the
  1822. home directory for a particular avatarId is.
  1823. This works on POSIX but probably is not reliable on Windows.
  1824. """
  1825. def getHomeDirectory(self, avatarId):
  1826. """
  1827. Return the system-defined home directory of the system user account
  1828. with the name C{avatarId}.
  1829. """
  1830. path = os.path.expanduser("~" + avatarId)
  1831. if path.startswith("~"):
  1832. raise cred_error.UnauthorizedLogin()
  1833. return filepath.FilePath(path)
  1834. # --- FTP CLIENT -------------------------------------------------------------
  1835. ####
  1836. # And now for the client...
  1837. # Notes:
  1838. # * Reference: http://cr.yp.to/ftp.html
  1839. # * FIXME: Does not support pipelining (which is not supported by all
  1840. # servers anyway). This isn't a functionality limitation, just a
  1841. # small performance issue.
  1842. # * Only has a rudimentary understanding of FTP response codes (although
  1843. # the full response is passed to the caller if they so choose).
  1844. # * Assumes that USER and PASS should always be sent
  1845. # * Always sets TYPE I (binary mode)
  1846. # * Doesn't understand any of the weird, obscure TELNET stuff (\377...)
  1847. # * FIXME: Doesn't share any code with the FTPServer
  1848. class ConnectionLost(FTPError):
  1849. pass
  1850. class CommandFailed(FTPError):
  1851. pass
  1852. class BadResponse(FTPError):
  1853. pass
  1854. class UnexpectedResponse(FTPError):
  1855. pass
  1856. class UnexpectedData(FTPError):
  1857. pass
  1858. class FTPCommand:
  1859. def __init__(self, text=None, public=0):
  1860. self.text = text
  1861. self.deferred = defer.Deferred()
  1862. self.ready = 1
  1863. self.public = public
  1864. self.transferDeferred = None
  1865. def fail(self, failure):
  1866. if self.public:
  1867. self.deferred.errback(failure)
  1868. class ProtocolWrapper(protocol.Protocol):
  1869. def __init__(self, original, deferred):
  1870. self.original = original
  1871. self.deferred = deferred
  1872. def makeConnection(self, transport):
  1873. self.original.makeConnection(transport)
  1874. def dataReceived(self, data):
  1875. self.original.dataReceived(data)
  1876. def connectionLost(self, reason):
  1877. self.original.connectionLost(reason)
  1878. # Signal that transfer has completed
  1879. self.deferred.callback(None)
  1880. class IFinishableConsumer(interfaces.IConsumer):
  1881. """
  1882. A Consumer for producers that finish.
  1883. @since: 11.0
  1884. """
  1885. def finish():
  1886. """
  1887. The producer has finished producing.
  1888. """
  1889. @implementer(IFinishableConsumer)
  1890. class SenderProtocol(protocol.Protocol):
  1891. def __init__(self):
  1892. # Fired upon connection
  1893. self.connectedDeferred = defer.Deferred()
  1894. # Fired upon disconnection
  1895. self.deferred = defer.Deferred()
  1896. # Protocol stuff
  1897. def dataReceived(self, data):
  1898. raise UnexpectedData(
  1899. "Received data from the server on a " "send-only data-connection"
  1900. )
  1901. def makeConnection(self, transport):
  1902. protocol.Protocol.makeConnection(self, transport)
  1903. self.connectedDeferred.callback(self)
  1904. def connectionLost(self, reason):
  1905. if reason.check(error.ConnectionDone):
  1906. self.deferred.callback("connection done")
  1907. else:
  1908. self.deferred.errback(reason)
  1909. # IFinishableConsumer stuff
  1910. def write(self, data):
  1911. self.transport.write(data)
  1912. def registerProducer(self, producer, streaming):
  1913. """
  1914. Register the given producer with our transport.
  1915. """
  1916. self.transport.registerProducer(producer, streaming)
  1917. def unregisterProducer(self):
  1918. """
  1919. Unregister the previously registered producer.
  1920. """
  1921. self.transport.unregisterProducer()
  1922. def finish(self):
  1923. self.transport.loseConnection()
  1924. def decodeHostPort(line):
  1925. """
  1926. Decode an FTP response specifying a host and port.
  1927. @return: a 2-tuple of (host, port).
  1928. """
  1929. abcdef = re.sub("[^0-9, ]", "", line)
  1930. parsed = [int(p.strip()) for p in abcdef.split(",")]
  1931. for x in parsed:
  1932. if x < 0 or x > 255:
  1933. raise ValueError("Out of range", line, x)
  1934. a, b, c, d, e, f = parsed
  1935. host = f"{a}.{b}.{c}.{d}"
  1936. port = (int(e) << 8) + int(f)
  1937. return host, port
  1938. def encodeHostPort(host, port):
  1939. numbers = host.split(".") + [str(port >> 8), str(port % 256)]
  1940. return ",".join(numbers)
  1941. def _unwrapFirstError(failure):
  1942. failure.trap(defer.FirstError)
  1943. return failure.value.subFailure
  1944. class FTPDataPortFactory(protocol.ServerFactory):
  1945. """
  1946. Factory for data connections that use the PORT command
  1947. (i.e. "active" transfers)
  1948. """
  1949. noisy = False
  1950. def buildProtocol(self, addr):
  1951. # This is a bit hackish -- we already have a Protocol instance,
  1952. # so just return it instead of making a new one
  1953. # FIXME: Reject connections from the wrong address/port
  1954. # (potential security problem)
  1955. self.protocol.factory = self
  1956. self.port.loseConnection()
  1957. return self.protocol
  1958. class FTPClientBasic(basic.LineReceiver):
  1959. """
  1960. Foundations of an FTP client.
  1961. """
  1962. debug = False
  1963. _encoding = "latin-1"
  1964. def __init__(self):
  1965. self.actionQueue = []
  1966. self.greeting = None
  1967. self.nextDeferred = defer.Deferred().addCallback(self._cb_greeting)
  1968. self.nextDeferred.addErrback(self.fail)
  1969. self.response = []
  1970. self._failed = 0
  1971. def fail(self, error):
  1972. """
  1973. Give an error to any queued deferreds.
  1974. """
  1975. self._fail(error)
  1976. def _fail(self, error):
  1977. """
  1978. Errback all queued deferreds.
  1979. """
  1980. if self._failed:
  1981. # We're recursing; bail out here for simplicity
  1982. return error
  1983. self._failed = 1
  1984. if self.nextDeferred:
  1985. try:
  1986. self.nextDeferred.errback(
  1987. failure.Failure(ConnectionLost("FTP connection lost", error))
  1988. )
  1989. except defer.AlreadyCalledError:
  1990. pass
  1991. for ftpCommand in self.actionQueue:
  1992. ftpCommand.fail(
  1993. failure.Failure(ConnectionLost("FTP connection lost", error))
  1994. )
  1995. return error
  1996. def _cb_greeting(self, greeting):
  1997. self.greeting = greeting
  1998. def sendLine(self, line):
  1999. """
  2000. Sends a line, unless line is None.
  2001. @param line: Line to send
  2002. @type line: L{bytes} or L{unicode}
  2003. """
  2004. if line is None:
  2005. return
  2006. elif isinstance(line, str):
  2007. line = line.encode(self._encoding)
  2008. basic.LineReceiver.sendLine(self, line)
  2009. def sendNextCommand(self):
  2010. """
  2011. (Private) Processes the next command in the queue.
  2012. """
  2013. ftpCommand = self.popCommandQueue()
  2014. if ftpCommand is None:
  2015. self.nextDeferred = None
  2016. return
  2017. if not ftpCommand.ready:
  2018. self.actionQueue.insert(0, ftpCommand)
  2019. reactor.callLater(1.0, self.sendNextCommand)
  2020. self.nextDeferred = None
  2021. return
  2022. # FIXME: this if block doesn't belong in FTPClientBasic, it belongs in
  2023. # FTPClient.
  2024. if ftpCommand.text == "PORT":
  2025. self.generatePortCommand(ftpCommand)
  2026. if self.debug:
  2027. log.msg("<-- %s" % ftpCommand.text)
  2028. self.nextDeferred = ftpCommand.deferred
  2029. self.sendLine(ftpCommand.text)
  2030. def queueCommand(self, ftpCommand):
  2031. """
  2032. Add an FTPCommand object to the queue.
  2033. If it's the only thing in the queue, and we are connected and we aren't
  2034. waiting for a response of an earlier command, the command will be sent
  2035. immediately.
  2036. @param ftpCommand: an L{FTPCommand}
  2037. """
  2038. self.actionQueue.append(ftpCommand)
  2039. if (
  2040. len(self.actionQueue) == 1
  2041. and self.transport is not None
  2042. and self.nextDeferred is None
  2043. ):
  2044. self.sendNextCommand()
  2045. def queueStringCommand(self, command, public=1):
  2046. """
  2047. Queues a string to be issued as an FTP command
  2048. @param command: string of an FTP command to queue
  2049. @param public: a flag intended for internal use by FTPClient. Don't
  2050. change it unless you know what you're doing.
  2051. @return: a L{Deferred} that will be called when the response to the
  2052. command has been received.
  2053. """
  2054. ftpCommand = FTPCommand(command, public)
  2055. self.queueCommand(ftpCommand)
  2056. return ftpCommand.deferred
  2057. def popCommandQueue(self):
  2058. """
  2059. Return the front element of the command queue, or None if empty.
  2060. """
  2061. if self.actionQueue:
  2062. return self.actionQueue.pop(0)
  2063. else:
  2064. return None
  2065. def queueLogin(self, username, password):
  2066. """
  2067. Login: send the username, send the password.
  2068. If the password is L{None}, the PASS command won't be sent. Also, if
  2069. the response to the USER command has a response code of 230 (User
  2070. logged in), then PASS won't be sent either.
  2071. """
  2072. # Prepare the USER command
  2073. deferreds = []
  2074. userDeferred = self.queueStringCommand("USER " + username, public=0)
  2075. deferreds.append(userDeferred)
  2076. # Prepare the PASS command (if a password is given)
  2077. if password is not None:
  2078. passwordCmd = FTPCommand("PASS " + password, public=0)
  2079. self.queueCommand(passwordCmd)
  2080. deferreds.append(passwordCmd.deferred)
  2081. # Avoid sending PASS if the response to USER is 230.
  2082. # (ref: http://cr.yp.to/ftp/user.html#user)
  2083. def cancelPasswordIfNotNeeded(response):
  2084. if response[0].startswith("230"):
  2085. # No password needed!
  2086. self.actionQueue.remove(passwordCmd)
  2087. return response
  2088. userDeferred.addCallback(cancelPasswordIfNotNeeded)
  2089. # Error handling.
  2090. for deferred in deferreds:
  2091. # If something goes wrong, call fail
  2092. deferred.addErrback(self.fail)
  2093. # But also swallow the error, so we don't cause spurious errors
  2094. deferred.addErrback(lambda x: None)
  2095. def lineReceived(self, line):
  2096. """
  2097. (Private) Parses the response messages from the FTP server.
  2098. """
  2099. # Add this line to the current response
  2100. if bytes != str:
  2101. line = line.decode(self._encoding)
  2102. if self.debug:
  2103. log.msg("--> %s" % line)
  2104. self.response.append(line)
  2105. # Bail out if this isn't the last line of a response
  2106. # The last line of response starts with 3 digits followed by a space
  2107. codeIsValid = re.match(r"\d{3} ", line)
  2108. if not codeIsValid:
  2109. return
  2110. code = line[0:3]
  2111. # Ignore marks
  2112. if code[0] == "1":
  2113. return
  2114. # Check that we were expecting a response
  2115. if self.nextDeferred is None:
  2116. self.fail(UnexpectedResponse(self.response))
  2117. return
  2118. # Reset the response
  2119. response = self.response
  2120. self.response = []
  2121. # Look for a success or error code, and call the appropriate callback
  2122. if code[0] in ("2", "3"):
  2123. # Success
  2124. self.nextDeferred.callback(response)
  2125. elif code[0] in ("4", "5"):
  2126. # Failure
  2127. self.nextDeferred.errback(failure.Failure(CommandFailed(response)))
  2128. else:
  2129. # This shouldn't happen unless something screwed up.
  2130. log.msg(f"Server sent invalid response code {code}")
  2131. self.nextDeferred.errback(failure.Failure(BadResponse(response)))
  2132. # Run the next command
  2133. self.sendNextCommand()
  2134. def connectionLost(self, reason):
  2135. self._fail(reason)
  2136. class _PassiveConnectionFactory(protocol.ClientFactory):
  2137. noisy = False
  2138. def __init__(self, protoInstance):
  2139. self.protoInstance = protoInstance
  2140. def buildProtocol(self, ignored):
  2141. self.protoInstance.factory = self
  2142. return self.protoInstance
  2143. def clientConnectionFailed(self, connector, reason):
  2144. e = FTPError("Connection Failed", reason)
  2145. self.protoInstance.deferred.errback(e)
  2146. class FTPClient(FTPClientBasic):
  2147. """
  2148. L{FTPClient} is a client implementation of the FTP protocol which
  2149. exposes FTP commands as methods which return L{Deferred}s.
  2150. Each command method returns a L{Deferred} which is called back when a
  2151. successful response code (2xx or 3xx) is received from the server or
  2152. which is error backed if an error response code (4xx or 5xx) is received
  2153. from the server or if a protocol violation occurs. If an error response
  2154. code is received, the L{Deferred} fires with a L{Failure} wrapping a
  2155. L{CommandFailed} instance. The L{CommandFailed} instance is created
  2156. with a list of the response lines received from the server.
  2157. See U{RFC 959<http://www.ietf.org/rfc/rfc959.txt>} for error code
  2158. definitions.
  2159. Both active and passive transfers are supported.
  2160. @ivar passive: See description in __init__.
  2161. """
  2162. connectFactory = reactor.connectTCP # type: ignore[attr-defined]
  2163. def __init__(
  2164. self, username="anonymous", password="twisted@twistedmatrix.com", passive=1
  2165. ):
  2166. """
  2167. Constructor.
  2168. I will login as soon as I receive the welcome message from the server.
  2169. @param username: FTP username
  2170. @param password: FTP password
  2171. @param passive: flag that controls if I use active or passive data
  2172. connections. You can also change this after construction by
  2173. assigning to C{self.passive}.
  2174. """
  2175. FTPClientBasic.__init__(self)
  2176. self.queueLogin(username, password)
  2177. self.passive = passive
  2178. def fail(self, error):
  2179. """
  2180. Disconnect, and also give an error to any queued deferreds.
  2181. """
  2182. self.transport.loseConnection()
  2183. self._fail(error)
  2184. def receiveFromConnection(self, commands, protocol):
  2185. """
  2186. Retrieves a file or listing generated by the given command,
  2187. feeding it to the given protocol.
  2188. @param commands: list of strings of FTP commands to execute then
  2189. receive the results of (e.g. C{LIST}, C{RETR})
  2190. @param protocol: A L{Protocol} B{instance} e.g. an
  2191. L{FTPFileListProtocol}, or something that can be adapted to one.
  2192. Typically this will be an L{IConsumer} implementation.
  2193. @return: L{Deferred}.
  2194. """
  2195. protocol = interfaces.IProtocol(protocol)
  2196. wrapper = ProtocolWrapper(protocol, defer.Deferred())
  2197. return self._openDataConnection(commands, wrapper)
  2198. def queueLogin(self, username, password):
  2199. """
  2200. Login: send the username, send the password, and
  2201. set retrieval mode to binary
  2202. """
  2203. FTPClientBasic.queueLogin(self, username, password)
  2204. d = self.queueStringCommand("TYPE I", public=0)
  2205. # If something goes wrong, call fail
  2206. d.addErrback(self.fail)
  2207. # But also swallow the error, so we don't cause spurious errors
  2208. d.addErrback(lambda x: None)
  2209. def sendToConnection(self, commands):
  2210. """
  2211. XXX
  2212. @return: A tuple of two L{Deferred}s:
  2213. - L{Deferred} L{IFinishableConsumer}. You must call
  2214. the C{finish} method on the IFinishableConsumer when the
  2215. file is completely transferred.
  2216. - L{Deferred} list of control-connection responses.
  2217. """
  2218. s = SenderProtocol()
  2219. r = self._openDataConnection(commands, s)
  2220. return (s.connectedDeferred, r)
  2221. def _openDataConnection(self, commands, protocol):
  2222. """
  2223. This method returns a DeferredList.
  2224. """
  2225. cmds = [FTPCommand(command, public=1) for command in commands]
  2226. cmdsDeferred = defer.DeferredList(
  2227. [cmd.deferred for cmd in cmds], fireOnOneErrback=True, consumeErrors=True
  2228. )
  2229. cmdsDeferred.addErrback(_unwrapFirstError)
  2230. if self.passive:
  2231. # Hack: use a mutable object to sneak a variable out of the
  2232. # scope of doPassive
  2233. _mutable = [None]
  2234. def doPassive(response):
  2235. """Connect to the port specified in the response to PASV"""
  2236. host, port = decodeHostPort(response[-1][4:])
  2237. f = _PassiveConnectionFactory(protocol)
  2238. _mutable[0] = self.connectFactory(host, port, f)
  2239. pasvCmd = FTPCommand("PASV")
  2240. self.queueCommand(pasvCmd)
  2241. pasvCmd.deferred.addCallback(doPassive).addErrback(self.fail)
  2242. results = [cmdsDeferred, pasvCmd.deferred, protocol.deferred]
  2243. d = defer.DeferredList(results, fireOnOneErrback=True, consumeErrors=True)
  2244. d.addErrback(_unwrapFirstError)
  2245. # Ensure the connection is always closed
  2246. def close(x, m=_mutable):
  2247. m[0] and m[0].disconnect()
  2248. return x
  2249. d.addBoth(close)
  2250. else:
  2251. # We just place a marker command in the queue, and will fill in
  2252. # the host and port numbers later (see generatePortCommand)
  2253. portCmd = FTPCommand("PORT")
  2254. # Ok, now we jump through a few hoops here.
  2255. # This is the problem: a transfer is not to be trusted as complete
  2256. # until we get both the "226 Transfer complete" message on the
  2257. # control connection, and the data socket is closed. Thus, we use
  2258. # a DeferredList to make sure we only fire the callback at the
  2259. # right time.
  2260. portCmd.transferDeferred = protocol.deferred
  2261. portCmd.protocol = protocol
  2262. portCmd.deferred.addErrback(portCmd.transferDeferred.errback)
  2263. self.queueCommand(portCmd)
  2264. # Create dummy functions for the next callback to call.
  2265. # These will also be replaced with real functions in
  2266. # generatePortCommand.
  2267. portCmd.loseConnection = lambda result: result
  2268. portCmd.fail = lambda error: error
  2269. # Ensure that the connection always gets closed
  2270. cmdsDeferred.addErrback(lambda e, pc=portCmd: pc.fail(e) or e)
  2271. results = [cmdsDeferred, portCmd.deferred, portCmd.transferDeferred]
  2272. d = defer.DeferredList(results, fireOnOneErrback=True, consumeErrors=True)
  2273. d.addErrback(_unwrapFirstError)
  2274. for cmd in cmds:
  2275. self.queueCommand(cmd)
  2276. return d
  2277. def generatePortCommand(self, portCmd):
  2278. """
  2279. (Private) Generates the text of a given PORT command.
  2280. """
  2281. # The problem is that we don't create the listening port until we need
  2282. # it for various reasons, and so we have to muck about to figure out
  2283. # what interface and port it's listening on, and then finally we can
  2284. # create the text of the PORT command to send to the FTP server.
  2285. # FIXME: This method is far too ugly.
  2286. # FIXME: The best solution is probably to only create the data port
  2287. # once per FTPClient, and just recycle it for each new download.
  2288. # This should be ok, because we don't pipeline commands.
  2289. # Start listening on a port
  2290. factory = FTPDataPortFactory()
  2291. factory.protocol = portCmd.protocol
  2292. listener = reactor.listenTCP(0, factory)
  2293. factory.port = listener
  2294. # Ensure we close the listening port if something goes wrong
  2295. def listenerFail(error, listener=listener):
  2296. if listener.connected:
  2297. listener.loseConnection()
  2298. return error
  2299. portCmd.fail = listenerFail
  2300. # Construct crufty FTP magic numbers that represent host & port
  2301. host = self.transport.getHost().host
  2302. port = listener.getHost().port
  2303. portCmd.text = "PORT " + encodeHostPort(host, port)
  2304. def escapePath(self, path):
  2305. """
  2306. Returns a FTP escaped path (replace newlines with nulls).
  2307. """
  2308. # Escape newline characters
  2309. return path.replace("\n", "\0")
  2310. def retrieveFile(self, path, protocol, offset=0):
  2311. """
  2312. Retrieve a file from the given path
  2313. This method issues the 'RETR' FTP command.
  2314. The file is fed into the given Protocol instance. The data connection
  2315. will be passive if self.passive is set.
  2316. @param path: path to file that you wish to receive.
  2317. @param protocol: a L{Protocol} instance.
  2318. @param offset: offset to start downloading from
  2319. @return: L{Deferred}
  2320. """
  2321. cmds = ["RETR " + self.escapePath(path)]
  2322. if offset:
  2323. cmds.insert(0, ("REST " + str(offset)))
  2324. return self.receiveFromConnection(cmds, protocol)
  2325. retr = retrieveFile
  2326. def storeFile(self, path, offset=0):
  2327. """
  2328. Store a file at the given path.
  2329. This method issues the 'STOR' FTP command.
  2330. @return: A tuple of two L{Deferred}s:
  2331. - L{Deferred} L{IFinishableConsumer}. You must call
  2332. the C{finish} method on the IFinishableConsumer when the
  2333. file is completely transferred.
  2334. - L{Deferred} list of control-connection responses.
  2335. """
  2336. cmds = ["STOR " + self.escapePath(path)]
  2337. if offset:
  2338. cmds.insert(0, ("REST " + str(offset)))
  2339. return self.sendToConnection(cmds)
  2340. stor = storeFile
  2341. def rename(self, pathFrom, pathTo):
  2342. """
  2343. Rename a file.
  2344. This method issues the I{RNFR}/I{RNTO} command sequence to rename
  2345. C{pathFrom} to C{pathTo}.
  2346. @param pathFrom: the absolute path to the file to be renamed
  2347. @type pathFrom: C{str}
  2348. @param pathTo: the absolute path to rename the file to.
  2349. @type pathTo: C{str}
  2350. @return: A L{Deferred} which fires when the rename operation has
  2351. succeeded or failed. If it succeeds, the L{Deferred} is called
  2352. back with a two-tuple of lists. The first list contains the
  2353. responses to the I{RNFR} command. The second list contains the
  2354. responses to the I{RNTO} command. If either I{RNFR} or I{RNTO}
  2355. fails, the L{Deferred} is errbacked with L{CommandFailed} or
  2356. L{BadResponse}.
  2357. @rtype: L{Deferred}
  2358. @since: 8.2
  2359. """
  2360. renameFrom = self.queueStringCommand("RNFR " + self.escapePath(pathFrom))
  2361. renameTo = self.queueStringCommand("RNTO " + self.escapePath(pathTo))
  2362. fromResponse = []
  2363. # Use a separate Deferred for the ultimate result so that Deferred
  2364. # chaining can't interfere with its result.
  2365. result = defer.Deferred()
  2366. # Bundle up all the responses
  2367. result.addCallback(lambda toResponse: (fromResponse, toResponse))
  2368. def ebFrom(failure):
  2369. # Make sure the RNTO doesn't run if the RNFR failed.
  2370. self.popCommandQueue()
  2371. result.errback(failure)
  2372. # Save the RNFR response to pass to the result Deferred later
  2373. renameFrom.addCallbacks(fromResponse.extend, ebFrom)
  2374. # Hook up the RNTO to the result Deferred as well
  2375. renameTo.chainDeferred(result)
  2376. return result
  2377. def list(self, path, protocol):
  2378. """
  2379. Retrieve a file listing into the given protocol instance.
  2380. This method issues the 'LIST' FTP command.
  2381. @param path: path to get a file listing for.
  2382. @param protocol: a L{Protocol} instance, probably a
  2383. L{FTPFileListProtocol} instance. It can cope with most common file
  2384. listing formats.
  2385. @return: L{Deferred}
  2386. """
  2387. if path is None:
  2388. path = ""
  2389. return self.receiveFromConnection(["LIST " + self.escapePath(path)], protocol)
  2390. def nlst(self, path, protocol):
  2391. """
  2392. Retrieve a short file listing into the given protocol instance.
  2393. This method issues the 'NLST' FTP command.
  2394. NLST (should) return a list of filenames, one per line.
  2395. @param path: path to get short file listing for.
  2396. @param protocol: a L{Protocol} instance.
  2397. """
  2398. if path is None:
  2399. path = ""
  2400. return self.receiveFromConnection(["NLST " + self.escapePath(path)], protocol)
  2401. def cwd(self, path):
  2402. """
  2403. Issues the CWD (Change Working Directory) command.
  2404. @return: a L{Deferred} that will be called when done.
  2405. """
  2406. return self.queueStringCommand("CWD " + self.escapePath(path))
  2407. def makeDirectory(self, path):
  2408. """
  2409. Make a directory
  2410. This method issues the MKD command.
  2411. @param path: The path to the directory to create.
  2412. @type path: C{str}
  2413. @return: A L{Deferred} which fires when the server responds. If the
  2414. directory is created, the L{Deferred} is called back with the
  2415. server response. If the server response indicates the directory
  2416. was not created, the L{Deferred} is errbacked with a L{Failure}
  2417. wrapping L{CommandFailed} or L{BadResponse}.
  2418. @rtype: L{Deferred}
  2419. @since: 8.2
  2420. """
  2421. return self.queueStringCommand("MKD " + self.escapePath(path))
  2422. def removeFile(self, path):
  2423. """
  2424. Delete a file on the server.
  2425. L{removeFile} issues a I{DELE} command to the server to remove the
  2426. indicated file. Note that this command cannot remove a directory.
  2427. @param path: The path to the file to delete. May be relative to the
  2428. current dir.
  2429. @type path: C{str}
  2430. @return: A L{Deferred} which fires when the server responds. On error,
  2431. it is errbacked with either L{CommandFailed} or L{BadResponse}. On
  2432. success, it is called back with a list of response lines.
  2433. @rtype: L{Deferred}
  2434. @since: 8.2
  2435. """
  2436. return self.queueStringCommand("DELE " + self.escapePath(path))
  2437. def removeDirectory(self, path):
  2438. """
  2439. Delete a directory on the server.
  2440. L{removeDirectory} issues a I{RMD} command to the server to remove the
  2441. indicated directory. Described in RFC959.
  2442. @param path: The path to the directory to delete. May be relative to
  2443. the current working directory.
  2444. @type path: C{str}
  2445. @return: A L{Deferred} which fires when the server responds. On error,
  2446. it is errbacked with either L{CommandFailed} or L{BadResponse}. On
  2447. success, it is called back with a list of response lines.
  2448. @rtype: L{Deferred}
  2449. @since: 11.1
  2450. """
  2451. return self.queueStringCommand("RMD " + self.escapePath(path))
  2452. def cdup(self):
  2453. """
  2454. Issues the CDUP (Change Directory UP) command.
  2455. @return: a L{Deferred} that will be called when done.
  2456. """
  2457. return self.queueStringCommand("CDUP")
  2458. def pwd(self):
  2459. """
  2460. Issues the PWD (Print Working Directory) command.
  2461. The L{getDirectory} does the same job but automatically parses the
  2462. result.
  2463. @return: a L{Deferred} that will be called when done. It is up to the
  2464. caller to interpret the response, but the L{parsePWDResponse}
  2465. method in this module should work.
  2466. """
  2467. return self.queueStringCommand("PWD")
  2468. def getDirectory(self):
  2469. """
  2470. Returns the current remote directory.
  2471. @return: a L{Deferred} that will be called back with a C{str} giving
  2472. the remote directory or which will errback with L{CommandFailed}
  2473. if an error response is returned.
  2474. """
  2475. def cbParse(result):
  2476. try:
  2477. # The only valid code is 257
  2478. if int(result[0].split(" ", 1)[0]) != 257:
  2479. raise ValueError
  2480. except (IndexError, ValueError):
  2481. return failure.Failure(CommandFailed(result))
  2482. path = parsePWDResponse(result[0])
  2483. if path is None:
  2484. return failure.Failure(CommandFailed(result))
  2485. return path
  2486. return self.pwd().addCallback(cbParse)
  2487. def quit(self):
  2488. """
  2489. Issues the I{QUIT} command.
  2490. @return: A L{Deferred} that fires when the server acknowledges the
  2491. I{QUIT} command. The transport should not be disconnected until
  2492. this L{Deferred} fires.
  2493. """
  2494. return self.queueStringCommand("QUIT")
  2495. class FTPFileListProtocol(basic.LineReceiver):
  2496. """
  2497. Parser for standard FTP file listings
  2498. This is the evil required to match::
  2499. -rw-r--r-- 1 root other 531 Jan 29 03:26 README
  2500. If you need different evil for a wacky FTP server, you can
  2501. override either C{fileLinePattern} or C{parseDirectoryLine()}.
  2502. It populates the instance attribute self.files, which is a list containing
  2503. dicts with the following keys (examples from the above line):
  2504. - filetype: e.g. 'd' for directories, or '-' for an ordinary file
  2505. - perms: e.g. 'rw-r--r--'
  2506. - nlinks: e.g. 1
  2507. - owner: e.g. 'root'
  2508. - group: e.g. 'other'
  2509. - size: e.g. 531
  2510. - date: e.g. 'Jan 29 03:26'
  2511. - filename: e.g. 'README'
  2512. - linktarget: e.g. 'some/file'
  2513. Note that the 'date' value will be formatted differently depending on the
  2514. date. Check U{http://cr.yp.to/ftp.html} if you really want to try to parse
  2515. it.
  2516. It also matches the following::
  2517. -rw-r--r-- 1 root other 531 Jan 29 03:26 I HAVE\\ SPACE
  2518. - filename: e.g. 'I HAVE SPACE'
  2519. -rw-r--r-- 1 root other 531 Jan 29 03:26 LINK -> TARGET
  2520. - filename: e.g. 'LINK'
  2521. - linktarget: e.g. 'TARGET'
  2522. -rw-r--r-- 1 root other 531 Jan 29 03:26 N S -> L S
  2523. - filename: e.g. 'N S'
  2524. - linktarget: e.g. 'L S'
  2525. @ivar files: list of dicts describing the files in this listing
  2526. """
  2527. fileLinePattern = re.compile(
  2528. r"^(?P<filetype>.)(?P<perms>.{9})\s+(?P<nlinks>\d*)\s*"
  2529. r"(?P<owner>\S+)\s+(?P<group>\S+)\s+(?P<size>\d+)\s+"
  2530. r"(?P<date>...\s+\d+\s+[\d:]+)\s+(?P<filename>.{1,}?)"
  2531. r"( -> (?P<linktarget>[^\r]*))?\r?$"
  2532. )
  2533. delimiter = b"\n"
  2534. _encoding = "latin-1"
  2535. def __init__(self):
  2536. self.files = []
  2537. def lineReceived(self, line):
  2538. if bytes != str:
  2539. line = line.decode(self._encoding)
  2540. d = self.parseDirectoryLine(line)
  2541. if d is None:
  2542. self.unknownLine(line)
  2543. else:
  2544. self.addFile(d)
  2545. def parseDirectoryLine(self, line):
  2546. """
  2547. Return a dictionary of fields, or None if line cannot be parsed.
  2548. @param line: line of text expected to contain a directory entry
  2549. @type line: str
  2550. @return: dict
  2551. """
  2552. match = self.fileLinePattern.match(line)
  2553. if match is None:
  2554. return None
  2555. else:
  2556. d = match.groupdict()
  2557. d["filename"] = d["filename"].replace(r"\ ", " ")
  2558. d["nlinks"] = int(d["nlinks"])
  2559. d["size"] = int(d["size"])
  2560. if d["linktarget"]:
  2561. d["linktarget"] = d["linktarget"].replace(r"\ ", " ")
  2562. return d
  2563. def addFile(self, info):
  2564. """
  2565. Append file information dictionary to the list of known files.
  2566. Subclasses can override or extend this method to handle file
  2567. information differently without affecting the parsing of data
  2568. from the server.
  2569. @param info: dictionary containing the parsed representation
  2570. of the file information
  2571. @type info: dict
  2572. """
  2573. self.files.append(info)
  2574. def unknownLine(self, line):
  2575. """
  2576. Deal with received lines which could not be parsed as file
  2577. information.
  2578. Subclasses can override this to perform any special processing
  2579. needed.
  2580. @param line: unparsable line as received
  2581. @type line: str
  2582. """
  2583. pass
  2584. def parsePWDResponse(response):
  2585. """
  2586. Returns the path from a response to a PWD command.
  2587. Responses typically look like::
  2588. 257 "/home/andrew" is current directory.
  2589. For this example, I will return C{'/home/andrew'}.
  2590. If I can't find the path, I return L{None}.
  2591. """
  2592. match = re.search('"(.*)"', response)
  2593. if match:
  2594. return match.groups()[0]
  2595. else:
  2596. return None