ó =OXc@sdZddlZddlZddlZddlmZmZddlmZddl m Z m Z m Z m Z mZddlmZddlmZddlmZdd lmZmZmZmZdd lmZdd l mZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ied 6ed 6ZAde efd„ƒYZBddlCmDZDdS(s Server-mode SFTP support. iÿÿÿÿN(tmd5tsha1(tutil(tBaseSFTPtMessaget SFTP_FAILUREtSFTP_PERMISSION_DENIEDtSFTP_NO_SUCH_FILE(tSFTPServerInterface(tSFTPAttributes(tDEBUG(tlongt string_typest bytes_typestb(tSubsystemHandler(%t CMD_HANDLEt SFTP_DESCt CMD_STATUStSFTP_EOFtCMD_NAMEtSFTP_BAD_MESSAGEtCMD_EXTENDED_REPLYtSFTP_FLAG_READtSFTP_FLAG_WRITEtSFTP_FLAG_APPENDtSFTP_FLAG_CREATEtSFTP_FLAG_TRUNCtSFTP_FLAG_EXCLt CMD_NAMEStCMD_OPENt CMD_CLOSEtSFTP_OKtCMD_READtCMD_DATAt CMD_WRITEt CMD_REMOVEt CMD_RENAMEt CMD_MKDIRt CMD_RMDIRt CMD_OPENDIRt CMD_READDIRtCMD_STATt CMD_ATTRSt CMD_LSTATt CMD_FSTATt CMD_SETSTATt CMD_FSETSTATt CMD_READLINKt CMD_SYMLINKt CMD_REALPATHt CMD_EXTENDEDtSFTP_OP_UNSUPPORTEDRRt SFTPServercBs¡eZdZed„Zd„Zd„Zd„Zed„ƒZ ed„ƒZ d„Z e d„Z dd „Zd „Zd „Zd „Zd „Zd„ZRS(så Server-side SFTP subsystem support. Since this is a `.SubsystemHandler`, it can be (and is meant to be) set as the handler for ``"sftp"`` requests. Use `.Transport.set_subsystem_handler` to activate this class. cOsŽtj|ƒtj||||ƒ|jƒ}tj|jƒdƒ|_|jƒ|_ d|_ i|_ i|_ ||||Ž|_ dS(s“ The constructor for SFTPServer is meant to be called from within the `.Transport` as a subsystem handler. ``server`` and any additional parameters or keyword parameters are passed from the original call to `.Transport.set_subsystem_handler`. :param .Channel channel: channel passed from the `.Transport`. :param str name: name of the requested subsystem. :param .ServerInterface server: the server object associated with this channel and subsystem :param class sftp_si: a subclass of `.SFTPServerInterface` to use for handling individual requests. s.sftpiN(Rt__init__Rt get_transportRt get_loggertget_log_channeltloggert get_hexdumpt ultra_debugt next_handlet file_tablet folder_tabletserver(tselftchanneltnameR@tsftp_sitlargstkwargst transport((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyR6<s     cCs‰tt|ƒtƒrWxm|D]4}tt|ƒj|d|jjƒd|ƒqWn.tt|ƒj|d|jjƒd|ƒdS(Ns[chan s] (t issubclassttypetlisttsuperR5t_logtsocktget_name(RAtleveltmsgtm((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyRLVs 5c Cs\||_|jtdt|ƒƒ|jƒ|jjƒxtrWy|jƒ\}}Wnet k r}|jtdƒdSt k rÀ}|jtdt |ƒƒ|jtt j ƒƒdSXt|ƒ}|jƒ}y|j|||ƒWq=t k rS}|jtdt |ƒƒ|jtt j ƒƒy|j|tƒWqTqTXq=Xq=WdS(Ns!Started sftp server on channel %ssEOF -- end of sessionsException on channel: s Exception in server processing: (RMRLR treprt_send_server_versionR@tsession_startedtTruet _read_packettEOFErrort ExceptiontstrRt tb_stringsRtget_intt_processt _send_statusR( RARCRGRBtttdatateRPtrequest_number((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pytstart_subsystem]s2       cCs~|jjƒtt|ƒjƒx!|jjƒD]}|jƒq0Wx!|jjƒD]}|jƒqTWi|_i|_dS(N( R@t session_endedRKR5tfinish_subsystemR>tvaluestcloseR?(RAtf((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyRdys  cCs=|tjkrtS|tjks1|tjkr5tStSdS(su Convert an errno value (as from an ``OSError`` or ``IOError``) into a standard SFTP result code. This is a convenience function for trapping exceptions in server code and returning an appropriate result. :param int e: an errno code, as from ``OSError.errno``. :return: an `int` SFTP error code like ``SFTP_NO_SUCH_FILE``. N(terrnotEACCESRtENOENTtENOTDIRRR(R`((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt convert_errno„s cCsÒtjdkrd|j|j@r5tj||jƒn|j|j@rdtj||j |j ƒqdn|j|j @r“tj ||j |jfƒn|j|j@rÎt|dƒ}|j|jƒWdQXndS(s: Change a file's attributes on the local filesystem. The contents of ``attr`` are used to change the permissions, owner, group ownership, and/or modification & access time of the file, depending on which attributes are present in ``attr``. This is meant to be a handy helper function for translating SFTP file requests into local file operations. :param str filename: name of the file to alter (should usually be an absolute path). :param .SFTPAttributes attr: attributes to change. twin32sw+N(tsystplatformt_flagstFLAG_PERMISSIONStostchmodtst_modet FLAG_UIDGIDtchowntst_uidtst_gidt FLAG_AMTIMEtutimetst_atimetst_mtimet FLAG_SIZEtopenttruncatetst_size(tfilenametattrRg((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt set_file_attr—scGsêtƒ}|j|ƒx½|D]µ}t|tƒrB|j|ƒqt|tƒra|j|ƒqt|ttfƒr†|j|ƒqt |ƒt kr¨|j |ƒqt dt |ƒdt t |ƒƒƒ‚qW|j||ƒdS(Nsunknown type for s type (Rtadd_intt isinstanceR t add_int64tintR R t add_stringRIR t_packRXRRt _send_packet(RARaR^targRPtitem((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt _response´s   .cCsžtt|ƒtƒs)|j||ƒdS|jtd|jƒƒ|jd7_|rn||j|jƒRR(RARathandletfolder((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt_send_handle_responseÄscCsT|dkr7yt|}Wq7tk r3d}q7Xn|j|t||dƒdS(NtUnknownt(tNoneRt IndexErrorRR(RARatcodetdesc((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyR]Ñs    cCsh|jj|ƒ}tt|ƒtƒrTtƒ}|j|ƒ|j||tƒdS|j ||ƒdS(N( R@t list_folderRHRIRJRŽt _set_filesR“RUR](RARatpathtrespR’((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt _open_folderÚs  cCsª|jƒ}t|ƒdkr2|j|tƒdStƒ}|j|ƒ|jt|ƒƒx8|D]0}|j|jƒ|j|ƒ|j|ƒqbW|j t |ƒdS(Ni( t_get_next_filestlenR]RRR„RˆRR‰RŠR(RARaR’tflistRPR‚((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt _read_folderås     cCsq|jƒ}|jƒ}|jƒ}|jƒ}|jƒ}||jkrb|j|tdƒdS|j|}xE|D]&} | tkrv| } t| } PqvqvW|j|tdƒdS|dkr |j ƒ} t t | ƒt ƒsû|j|| dƒdS| j |}n|dkr |}n|dkrC|j|tdƒdStƒ} |}xË|||krt||||ƒ}t|dƒ}d}| ƒ}xo||kr |j||ƒ}t|tƒsá|j||dƒdS|j|ƒ|t|ƒ7}||7}qW| |jƒ7} qUWtƒ}|j|ƒ|jd ƒ|j| ƒ|j| ƒ|jt|ƒdS( NsInvalid handlesNo supported hash types foundisUnable to stat fileisBlock size too smallisUnable to hash files check-file(t get_binarytget_listt get_int64R[R>R]Rt _hash_classRtstatRHRIR R€tbytestmintreadR…R tupdateR tdigestRR„Rˆt add_bytesRŠR(RARaRPR‘talg_listtstarttlengtht block_sizeRgtxtalgnametalgtsttsum_outtoffsettblocklentchunklentcountthash_objR_((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt _check_fileósb                      cCs«|t@r |t@r tj}n|t@r6tj}n tj}|t@rY|tjO}n|t@rs|tj O}n|t @r|tj O}n|t @r§|tj O}n|S(s;convert SFTP-style open() flags to Python's os.open() flags(RRRrtO_RDWRtO_WRONLYtO_RDONLYRtO_APPENDRtO_CREATRtO_TRUNCRtO_EXCL(RAtpflagstflags((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyt_convert_pfl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R,tlstatR-R.tchattrR/R0treadlinkRR1tsymlinkR2t canonicalizeR3R¼R4(RAR^RaRPRœRÅR‚R‘R·R°R_toldpathtnewpathR’Rt target_pathtrpathttag((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyR\@sè  %            &     "  "               "  #  "   "  "   N(t__name__t __module__t__doc__RR6RLRbRdt staticmethodRlRƒRtFalseR“R–R]RžR¢R¼RÆR\(((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyR55s       ; (RŽ(ERÚRrRhRnthashlibRRtparamikoRt paramiko.sftpRRRRRtparamiko.sftp_siRtparamiko.sftp_attrR tparamiko.commonR tparamiko.py3compatR R R Rtparamiko.serverRRRRRRRRRRRRRRRRRR R!R"R#R$R%R&R'R(R)R*R+R,R-R.R/R0R1R2R3R4R¦R5tparamiko.sftp_handleRŽ(((s8/usr/lib/python2.7/site-packages/paramiko/sftp_server.pyts$   ("è ÿŒ