U e5d@s^dZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z mZddddd gZejdZd ZGd d d Zead ZdZdZd%ddZGdddejZGdddejZGdddeZGdddeZGdd d eZGdddZ ddZ!e"dkrZe!Z#e#j$Z$de$krpe$%dZ&e'e$de&e(e)dgZ*e$e&ddZ$nddl+Z*e,e*e$Z-e-e#j.e#j/fe#j0e#j1fe#j2e#j3dZ4e#j5r6z ddl6Z6Wn.e7k re8d ej9d!e:dYnXe6;d"d#ZWne?k rXYnXdS)&aAn RFC 5321 smtp proxy with optional RFC 1870 and RFC 6531 extensions. Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] Options: --nosetuid -n This program generally tries to setuid `nobody', unless this flag is set. The setuid call will fail if this program is not run as root (in which case, use this flag). --version -V Print the version number and exit. --class classname -c classname Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by default. --size limit -s limit Restrict the total size of the incoming message to "limit" number of bytes via the RFC 1870 SIZE extension. Defaults to 33554432 bytes. --smtputf8 -u Enable the SMTPUTF8 extension and behave as an RFC 6531 smtp proxy. --debug -d Turn on debugging prints. --help -h Print this message and exit. Version: %(__version__)s If localhost is not given then `localhost' is used, and if localport is not given then 8025 is used. If remotehost is not given then `localhost' is used, and if remoteport is not given, then 25 is used. N)warn) get_addr_specget_angle_addr SMTPChannel SMTPServerDebuggingServer PureProxy MailmanProxyzPython SMTP proxy version 0.3c@seZdZddZddZdS)DevnullcCsdSNselfmsgr r /usr/lib64/python3.8/smtpd.pywritefz Devnull.writecCsdSr r rr r rflushgrz Devnull.flushN)__name__ __module__ __qualname__rrr r r rr esr  z, icCs4ttttjd|r&t|tjdt|dS)Nfile)print__doc__globalssysstderrexit)coderr r rusagepsr#c@seZdZdZdZdZeefddZe ddZ e dd d fd d Z d d Z ddZe ddZejddZe ddZejddZe ddZejddZe ddZejddZe ddZejddZe dd Zejd!d Ze d"d#Zejd$d#Ze d%d&Zejd'd&Ze d(d)Zejd*d)Ze d+d,Zejd-d,Ze d.d/Zejd0d/Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<Z d=d>Z!d?d@Z"dAdBZ#dCdDZ$dEdFZ%dGdHZ&dIdJZ'dKdLZ(dMdNZ)dOdPZ*dQdRZ+dS)SrricCs|Sr r )xr r r|rzSMTPChannel.cCs0zt|jWStk r*|jYSXdSr )maxcommand_size_limitsvalues ValueErrorcommand_size_limitrr r rmax_command_size_limit~sz"SMTPChannel.max_command_size_limitNFc Cs&tjj|||d||_||_||_||_||_||_|rF|rFt d|rdd|_ d|_ d|_ t |_nd|_ d|_ td|_ d |_|d|_d |_|jt|_z||_WnBtk r}z$||jd tjkrWYdSd}~XYnXtd t |jt!d |"d|jt#fdS)NmapFdecode_data and enable_SMTPUTF8 cannot be set to True at the same timer .r . FrzPeer:rz 220 %s %s)$asynchat async_chat__init__ smtp_serverconnaddrdata_size_limitenable_SMTPUTF8 _decode_datar* _emptystring_linesep_dotsepNEWLINE_newlineord_set_rset_state seen_greeting extended_smtpr(clearsocketZgetfqdnfqdnZ getpeernamepeerOSErrorcloseargserrnoZENOTCONNrrepr DEBUGSTREAMpush __version__) rZserverr9r:r;r.r< decode_dataerrr r rr7s@   zSMTPChannel.__init__cCs.|j|_d|_g|_d|_d|_|ddS)z/Reset state variables to their post-DATA state.NFrr2)COMMAND smtp_statemailfromrcpttosrequire_SMTPUTF8 num_bytesset_terminatorrr r r_set_post_data_states z SMTPChannel._set_post_data_statecCs|d|_g|_dS)z.Reset all state variables except the greeting.rN)r\ received_datareceived_linesrr r rrDszSMTPChannel._set_rset_statecCstdtd|jS)NzTAccess to __server attribute on SMTPChannel is deprecated, use 'smtp_server' insteadrDeprecationWarningr8rr r r__servers zSMTPChannel.__servercCstdtd||_dS)NzRSetting __server attribute on SMTPChannel is deprecated, set 'smtp_server' insteadr_r`rvaluer r rrbs cCstdtd|jS)NzUAccess to __line attribute on SMTPChannel is deprecated, use 'received_lines' insteadr_rrar^rr r r__lines zSMTPChannel.__linecCstdtd||_dS)NzSSetting __line attribute on SMTPChannel is deprecated, set 'received_lines' insteadr_rercr r rrfs cCstdtd|jS)NzRAccess to __state attribute on SMTPChannel is deprecated, use 'smtp_state' insteadr_rrarVrr r r__states zSMTPChannel.__statecCstdtd||_dS)NzPSetting __state attribute on SMTPChannel is deprecated, set 'smtp_state' insteadr_rgrcr r rrhs cCstdtd|jS)NzXAccess to __greeting attribute on SMTPChannel is deprecated, use 'seen_greeting' insteadr_rrarErr r r __greetings zSMTPChannel.__greetingcCstdtd||_dS)NzVSetting __greeting attribute on SMTPChannel is deprecated, set 'seen_greeting' insteadr_rircr r rrjs cCstdtd|jS)NzSAccess to __mailfrom attribute on SMTPChannel is deprecated, use 'mailfrom' insteadr_rrarWrr r r __mailfroms zSMTPChannel.__mailfromcCstdtd||_dS)NzQSetting __mailfrom attribute on SMTPChannel is deprecated, set 'mailfrom' insteadr_rkrcr r rrls cCstdtd|jS)NzQAccess to __rcpttos attribute on SMTPChannel is deprecated, use 'rcpttos' insteadr_rrarXrr r r __rcpttoss zSMTPChannel.__rcpttoscCstdtd||_dS)NzOSetting __rcpttos attribute on SMTPChannel is deprecated, set 'rcpttos' insteadr_rmrcr r rrns cCstdtd|jS)NzTAccess to __data attribute on SMTPChannel is deprecated, use 'received_data' insteadr_rrar]rr r r__datas zSMTPChannel.__datacCstdtd||_dS)NzRSetting __data attribute on SMTPChannel is deprecated, set 'received_data' insteadr_rorcr r rrps cCstdtd|jS)NzKAccess to __fqdn attribute on SMTPChannel is deprecated, use 'fqdn' insteadr_rrarIrr r r__fqdn s zSMTPChannel.__fqdncCstdtd||_dS)NzISetting __fqdn attribute on SMTPChannel is deprecated, set 'fqdn' insteadr_rqrcr r rrrs cCstdtd|jS)NzKAccess to __peer attribute on SMTPChannel is deprecated, use 'peer' insteadr_rrarJrr r r__peers zSMTPChannel.__peercCstdtd||_dS)NzISetting __peer attribute on SMTPChannel is deprecated, set 'peer' insteadr_rsrcr r rrts cCstdtd|jS)NzKAccess to __conn attribute on SMTPChannel is deprecated, use 'conn' insteadr_rrar9rr r r__conn s zSMTPChannel.__conncCstdtd||_dS)NzISetting __conn attribute on SMTPChannel is deprecated, set 'conn' insteadr_rurcr r rrv%s cCstdtd|jS)NzKAccess to __addr attribute on SMTPChannel is deprecated, use 'addr' insteadr_rrar:rr r r__addr+s zSMTPChannel.__addrcCstdtd||_dS)NzISetting __addr attribute on SMTPChannel is deprecated, set 'addr' insteadr_rwrcr r rrx0s cCs&tj|t|d|jrdnddS)Nr0utf-8ascii)r5r6rQbytesrYr r r rrQ7s  zSMTPChannel.pushcCs|d}|j|jkr|j}n|j|jkr*|j}|r<|j|krjoinr^rrOrPrVrUrZrQr=rfindupperstriprFr(r+getattrr|r;splitr?r@r~rBr]rJrWrXrrr8process_messager\) rlineZszicommandargZmax_szmethodrtextrMkwargsZstatusr r rfound_terminatorLsl        zSMTPChannel.found_terminatorcCsH|s|ddS|jr&|ddS|||_|d|jdS)Nz501 Syntax: HELO hostname503 Duplicate HELO/EHLOz250 %s)rQrErDrIrrr r r smtp_HELOs  zSMTPChannel.smtp_HELOcCs|s|ddS|jr&|ddS|||_d|_|d|j|jrr|d|j|jdd7<|js|d|jr|d |jdd 7<|d dS) Nz501 Syntax: EHLO hostnamerTz250-%sz 250-SIZE %sMAILz 250-8BITMIMEz 250-SMTPUTF8 z250 HELP) rQrErDrFrIr;r(r=r<rr r r smtp_EHLOs&    zSMTPChannel.smtp_EHLOcCs|r|dn |ddS)Nz501 Syntax: NOOPrrQrr r r smtp_NOOPs zSMTPChannel.smtp_NOOPcCs|d|dS)Nz221 Bye)rQZclose_when_donerr r r smtp_QUITs zSMTPChannel.smtp_QUITcCs0t|}|d||kr,||dSdS)Nr)r}rr)rkeywordrZkeylenr r r_strip_command_keywordsz"SMTPChannel._strip_command_keywordcCsF|sdS|dr$t|\}}n t|\}}|s<||fS|j|fS)N)rr<)lstrip startswithrrZ addr_spec)rraddressrestr r r_getaddrs zSMTPChannel._getaddrcCsHi}|D]:}|d\}}}|r,|r2|s2dS|r:|nd||<q|S)N=T) partitionisalnum)rparamsresultZparameqrdr r r _getparamsszSMTPChannel._getparamscCs|rd}|}|dkr$|dq|dkr8|dq|dkr^d}|jrR||7}||q|dkrd }|jrx||7}||q|d kr|d q|d kr|d q|dkr|dq|dkr|dq|dkr|dq|dn |ddS)N [SP ]ZEHLOz250 Syntax: EHLO hostnameZHELOz250 Syntax: HELO hostnamerz 250 Syntax: MAIL FROM:
ZRCPTz250 Syntax: RCPT TO:
r|z250 Syntax: DATAZRSETz250 Syntax: RSETZNOOPz250 Syntax: NOOPZQUITz250 Syntax: QUITZVRFYz250 Syntax: VRFY
zD501 Supported commands: EHLO HELO MAIL RCPT DATA RSET NOOP QUIT VRFYzD250 Supported commands: EHLO HELO MAIL RCPT DATA RSET NOOP QUIT VRFY)rrQrF)rrZextendedZlc_argrr r r smtp_HELPs:          zSMTPChannel.smtp_HELPcCs@|r2||\}}|r"|dq<|d|n |ddS)NzB252 Cannot VRFY user, but will accept message and attempt deliveryz502 Could not VRFY %sz501 Syntax: VRFY
)rrQ)rrrrr r r smtp_VRFYs  zSMTPChannel.smtp_VRFYcCs|js|ddStd|tdd}|jr4|d7}|dkrJ||dS|d|}||\}}|sv||dS|js|r||dS|jr|ddS| |_ | |j }|dkr||dS|j s| dd }|d kr|d dS|jr8| d d }|dkr d|_n|d k r8|ddS| dd}|r|sb||dS|jrt||jkr|ddSt|dkr|ddS||_td|jtd|ddS)N503 Error: send HELO firstz ===> MAILrz 501 Syntax: MAIL FROM:
rzFROM:z503 Error: nested MAIL commandZBODY7BIT)rZ8BITMIMEz1501 Error: BODY can only be one of 7BIT, 8BITMIMEZSMTPUTF8FTz&501 Error: SMTPUTF8 takes no argumentsZSIZEz:552 Error: message size exceeds fixed maximum message sizerz:555 MAIL FROM parameters not recognized or not implementedzsender:r)rErQrrPrFrrrWrrrrr=popr<rYisdigitr;intr}keys)rr syntaxerrrrZbodysmtputf8sizer r r smtp_MAILsh                    zSMTPChannel.smtp_MAILcCs|js|ddStd|td|js6|ddSd}|jrH|d7}|dkr^||dS|d|}||\}}|s||dS|js|r||dS| |_ | |j }|dkr||dSt | dkr|d dS|j|td |jtd|d dS) Nrz ===> RCPTrz503 Error: need MAIL commandz501 Syntax: RCPT TO:
rzTO:rz8555 RCPT TO parameters not recognized or not implementedzrecips:r)rErQrrPrWrFrrrrrrr}rrXr~)rrrrrr r r smtp_RCPT7s@           zSMTPChannel.smtp_RCPTcCs(|r|ddS||ddS)Nz501 Syntax: RSETr)rQrDrr r r smtp_RSETZs  zSMTPChannel.smtp_RSETcCsZ|js|ddS|js(|ddS|r:|ddS|j|_|d|ddS)Nrz503 Error: need RCPT commandz501 Syntax: DATAs . z#354 End data with .)rErQrXr|rVr[rr r r smtp_DATAas    zSMTPChannel.smtp_DATAcCs|ddS)Nz502 EXPN not implementedrrr r r smtp_EXPNpszSMTPChannel.smtp_EXPN),rrrrUr|r+ collections defaultdictr(propertyr,DATA_SIZE_DEFAULTr7r\rDZ_SMTPChannel__serversetterZ_SMTPChannel__lineZ_SMTPChannel__stateZ_SMTPChannel__greetingZ_SMTPChannel__mailfromZ_SMTPChannel__rcpttosZ_SMTPChannel__dataZ_SMTPChannel__fqdnZ_SMTPChannel__peerZ_SMTPChannel__connZ_SMTPChannel__addrrQrrrrrrrrrrrrrrrrr r r rrws  '                       >   # 6#c@s2eZdZeZedddfddZddZddZdS) rNFcCs||_||_||_||_||_|r.|r.tdtjj||dzNt j |dt j i}| |dd|dd| |||dWn|Yn(Xtd|jjtt||ftddS) Nr/r-typerr$z0%s started at %s Local addr: %s Remote addr:%sr)Z _localaddr _remoteaddrr;r<r=r*asyncore dispatcherr7rHZ getaddrinfoZ SOCK_STREAMZ create_socketZset_reuse_addrZbindZlistenrLr __class__rtimectimerP)rZ localaddrZ remoteaddrr;r.r<rSZ gai_resultsr r rr7xs6  zSMTPServer.__init__c Cs6tdt|td|||||j|j|j|j}dS)NzIncoming connection from %sr)rrOrP channel_classr;_mapr<r=)rr9r:Zchannelr r rhandle_acceptedszSMTPServer.handle_acceptedcKstdS)aOverride this abstract method to handle messages from the client. peer is a tuple containing (ipaddr, port) of the client that made the socket connection to our smtp port. mailfrom is the raw address the client claims the message is coming from. rcpttos is a list of raw addresses the client wishes to deliver the message to. data is a string containing the entire full text of the message, headers (if supplied) and all. It has been `de-transparencied' according to RFC 821, Section 4.5.2. In other words, a line containing a `.' followed by other text has had the leading dot removed. kwargs is a dictionary containing additional information. It is empty if decode_data=True was given as init parameter, otherwise it will contain the following keys: 'mail_options': list of parameters to the mail command. All elements are uppercase strings. Example: ['BODY=8BITMIME', 'SMTPUTF8']. 'rcpt_options': same, for the rcpt command. This function should return None for a normal `250 Ok' response; otherwise, it should return the desired response string in RFC 821 format. N)NotImplementedErrorrrJrWrXrrr r rrszSMTPServer.process_message) rrrrrrr7rrr r r rrts  c@seZdZddZddZdS)rcCsld}|}|D]V}|rL|sLd|d}t|ts@t|d}t|d}t|ts^t|}t|qdS)Nr$zX-Peer: rry) splitlines isinstancerrOencoder)rrJrZ inheaderslinesrZ peerheaderr r r_print_message_contents   z&DebuggingServer._print_message_contentcKsXtd|r@|dr&td|d|dr@td|d|||tddS)Nz%---------- MESSAGE FOLLOWS ----------rzmail options: %srzrcpt options: %s z%------------ END MESSAGE ------------)rgetrrr r rrs   zDebuggingServer.process_messageN)rrrrrr r r rrscs,eZdZfddZddZddZZS)rcs.d|kr|drtdtt|j||dS)Nr<z$PureProxy does not support SMTPUTF8.r*superrr7rrMrrr rr7szPureProxy.__init__c Csf|d}d}|D]}|sq(|d7}q||d|dt|}||||}td|tddS)Nrrr$z X-Peer: %szwe got some refusals:r)rinsertrAr_deliverrrP) rrJrWrXrrrrrefusedr r rrs   zPureProxy.process_messagec Csddl}i}zB|}||jd|jdz||||}W5|XWn|jk r}ztdtd|j }W5d}~XYnft |j fk r}zBtd|j tdt |dd}t |dd } |D]} || f|| <qW5d}~XYnX|S) Nrr$zgot SMTPRecipientsRefusedrZgotZ smtp_codeZ smtp_errorignore)smtplibZSMTPZconnectrquitZsendmailZSMTPRecipientsRefusedrrPZ recipientsrKZ SMTPExceptionrr) rrWrXrrrseZerrcodeerrmsgrr r rrs$    zPureProxy._deliver)rrrr7rr __classcell__r r rrrs cs$eZdZfddZddZZS)r cs.d|kr|drtdtt|j||dS)Nr<z'MailmanProxy does not support SMTPUTF8.rrrr rr7 szMailmanProxy.__init__cCs*ddlm}ddlm}ddlm}ddlm}g} |D]t} | dd} | d} t| dkrfq8| d} t| dkr| d }nd }| | r8|d krq8| | | |fq8| D]\} } }| | qt d d |td|r||||}t d|tdi}||}||}|ds&||d<|dsDtt|d<| D]\} } }t d| td|| }|s|j| dd}||| <|d kr|j|d dn|dkr|j|d dnh|dkr|j|d dnN|dkr|j|d dn4|dkrH|dkr d|d <nd!|d <|j|d dqHdS)"Nr)StringIO)Utils)Message)MailList@-r_r$r)radminownerrequestrleavezforwarding recips:rrzwe got refusals:fromZFromdateZDatezsending message to)lock)tolistr)Ztoadminr)Ztoownerr)Z torequest)rrrZ subscribeZSubjectZ unsubscribe)iorZMailmanrrrlowerrr}Z list_existsr~removerrrPrrrrZEnqueue)rrJrWrXrrrrrZ listnamesZrcptZlocalpartsZlistnamerrZmlistsrrZmlistr r rrsb                    zMailmanProxy.process_message)rrrr7rrr r rrr s c@seZdZdZdZdZdZdS)OptionsTrNF)rrrsetuid classname size_limitr<r r r rr_src Cspz.ttjdddddddddd g\}}Wn.tjk r\}ztd|W5d}~XYnXt}|D]\}}|d krtd qh|d krtttd qh|d krd|_ qh|dkr||_ qh|dkrtj a qh|dkrd|_ qh|dkrhzt|}||_Wqhtd|tj dtdYqhXqht|dkrsN   M-SB