o w[e]@s`dZddlZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z m Z gdZ e deddddlZddlZejdZd ZGd d d Zead Zd ZdZd(ddZGdddejZGdddejZGdddeZGdddeZGdddeZ GdddZ!ddZ"e#d kr.e"Z$e$j%Z%d!e%vre%&d!Z'e(e%de'e)e*dgZ+e%e'd"dZ%nddl,Z+e-e+e%Z.e.e$j/e$j0fe$j1e$j2fe$j3e$j4d#Z5e$j6rzddl7Z7Wne8ye9d$ej:d%e;d"Ynwe7ye9d'ej:d%e;d"Ynwze?WdSe@y-YdSwdS))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 MailmanProxyzThe smtpd module is deprecated and unmaintained and will be removed in Python 3.12. Please see aiosmtpd (https://aiosmtpd.readthedocs.io/) for the recommended replacement.) stacklevelzPython SMTP proxy version 0.3c@eZdZddZddZdS)DevnullcCdSNselfmsgrr/usr/lib/python3.10/smtpd.pywriterz Devnull.writecCrrrrrrrflushsrz Devnull.flushN)__name__ __module__ __qualname__rrrrrrr qs r  z, icCs4ttttjd|rt|tjdt|dS)Nfile)print__doc__globalssysstderrexit)coderrrrusage|sr'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|Srr)xrrrrzSMTPChannel.cCs*zt|jWSty|jYSwr)maxcommand_size_limitsvalues ValueErrorcommand_size_limitrrrrmax_command_size_limits   z"SMTPChannel.max_command_size_limitNFc Cs tjj|||d||_||_||_||_||_||_|r#|r#t d|r2d|_ d|_ d|_ t |_nd|_ d|_ td|_ d |_|d|_d |_|jt|_z||_Wntyy}z||jtjkrnWYd}~dSd}~wwtd t|jt d |!d |jt"fdS)NmapFdecode_data and enable_SMTPUTF8 cannot be set to True at the same timer . . FzPeer: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,clearsocketgetfqdnfqdn getpeernamepeerOSErrorcloseerrnoENOTCONNr repr DEBUGSTREAMpush __version__) rserverr>r?r@r2rA decode_dataerrrrrr<sD    zSMTPChannel.__init__cCs.|j|_d|_g|_d|_d|_|ddS)z/Reset state variables to their post-DATA state.NFrr7)COMMAND smtp_statemailfromrcpttosrequire_SMTPUTF8 num_bytesset_terminatorrrrr_set_post_data_states z SMTPChannel._set_post_data_statecCs|d|_g|_dS)z.Reset all state variables except the greeting.rN)rd received_datareceived_linesrrrrrIs zSMTPChannel._set_rset_statecCtdtd|jS)NzTAccess to __server attribute on SMTPChannel is deprecated, use 'smtp_server' insteadr rDeprecationWarningr=rrrr__serverzSMTPChannel.__servercCtdtd||_dS)NzRSetting __server attribute on SMTPChannel is deprecated, set 'smtp_server' insteadr rhrvaluerrrrj cCrg)NzUAccess to __line attribute on SMTPChannel is deprecated, use 'received_lines' insteadr rrirfrrrr__linerkzSMTPChannel.__linecCrl)NzSSetting __line attribute on SMTPChannel is deprecated, set 'received_lines' insteadr rprmrrrrqrocCrg)NzRAccess to __state attribute on SMTPChannel is deprecated, use 'smtp_state' insteadr rrir^rrrr__staterkzSMTPChannel.__statecCrl)NzPSetting __state attribute on SMTPChannel is deprecated, set 'smtp_state' insteadr rrrmrrrrsrocCrg)NzXAccess to __greeting attribute on SMTPChannel is deprecated, use 'seen_greeting' insteadr rrirJrrrr __greetingrkzSMTPChannel.__greetingcCrl)NzVSetting __greeting attribute on SMTPChannel is deprecated, set 'seen_greeting' insteadr rtrmrrrrurocCrg)NzSAccess to __mailfrom attribute on SMTPChannel is deprecated, use 'mailfrom' insteadr rrir_rrrr __mailfromrkzSMTPChannel.__mailfromcCrl)NzQSetting __mailfrom attribute on SMTPChannel is deprecated, set 'mailfrom' insteadr rvrmrrrrwrocCrg)NzQAccess to __rcpttos attribute on SMTPChannel is deprecated, use 'rcpttos' insteadr rrir`rrrr __rcpttosrkzSMTPChannel.__rcpttoscCrl)NzOSetting __rcpttos attribute on SMTPChannel is deprecated, set 'rcpttos' insteadr rxrmrrrryrocCrg)NzTAccess to __data attribute on SMTPChannel is deprecated, use 'received_data' insteadr rrirerrrr__data rkzSMTPChannel.__datacCrl)NzRSetting __data attribute on SMTPChannel is deprecated, set 'received_data' insteadr rzrmrrrr{rocCrg)NzKAccess to __fqdn attribute on SMTPChannel is deprecated, use 'fqdn' insteadr rrirOrrrr__fqdnrkzSMTPChannel.__fqdncCrl)NzISetting __fqdn attribute on SMTPChannel is deprecated, set 'fqdn' insteadr r|rmrrrr}rocCrg)NzKAccess to __peer attribute on SMTPChannel is deprecated, use 'peer' insteadr rrirQrrrr__peer!rkzSMTPChannel.__peercCrl)NzISetting __peer attribute on SMTPChannel is deprecated, set 'peer' insteadr r~rmrrrr&rocCrg)NzKAccess to __conn attribute on SMTPChannel is deprecated, use 'conn' insteadr rrir>rrrr__conn,rkzSMTPChannel.__conncCrl)NzISetting __conn attribute on SMTPChannel is deprecated, set 'conn' insteadr rrmrrrr1rocCrg)NzKAccess to __addr attribute on SMTPChannel is deprecated, use 'addr' insteadr rrir?rrrr__addr7rkzSMTPChannel.__addrcCrl)NzISetting __addr attribute on SMTPChannel is deprecated, set 'addr' insteadr rrmrrrr<rocCs&tj|t|d|jr dnddS)Nr4utf-8ascii)r:r;rXbytesrarrrrrXCs  zSMTPChannel.pushcCs~d}|j|jkr |j}n |j|jkr|j}|r|j|krdS|r)|jt|7_|jr7|j t |ddS|j |dS)Nr) r^r]r0DATAr@rblenrBrfappendstr)rdatalimitrrrcollect_incoming_dataHs  z!SMTPChannel.collect_incoming_datac Cs|j|j}tdt|tdg|_|j|jkr|jd}|_|s)| ddS|j s1t |d}| d}|dkrA| }d}n|d| }||dd}|jr[|j|n|j}||kri| ddSt|d |d}|s|| d |dS||dS|j|jkr| d d|_dS|jr|j|jkr| d d|_dSg}||jD]} | r| d|jkr|| ddq|| q|j||_|j|j|j|jf} i} |j s|j|jd } |j j!| i| } |"| s| ddS| | dS)NzData:rrz500 Error: bad syntaxr r(z500 Error: line too longsmtp_z&500 Error: command "%s" not recognizedz451 Internal confusionz552 Error: Too much mail data) mail_options rcpt_options250 OK)#rCjoinrfr rVrWr^r]rbrXrBrfindupperstriprKr,r/getattrrr@splitrDrErrGrerQr_r`rrr=process_messagerd) rlineszicommandargmax_szmethodrtextargskwargsstatusrrrfound_terminatorXsl          zSMTPChannel.found_terminatorcCsH|s |ddS|jr|ddS|||_|d|jdS)Nz501 Syntax: HELO hostname503 Duplicate HELO/EHLOz250 %s)rXrJrIrOrrrrr smtp_HELOs  zSMTPChannel.smtp_HELOcCs|s |ddS|jr|ddS|||_d|_|d|j|jr9|d|j|jdd7<|jsA|d|jrR|d |jdd 7<|d dS) Nz501 Syntax: EHLO hostnamerTz250-%sz 250-SIZE %sMAILz 250-8BITMIMEz 250-SMTPUTF8 z250 HELP) rXrJrIrKrOr@r,rBrArrrr smtp_EHLOs&    zSMTPChannel.smtp_EHLOcCs |r |ddS|ddS)Nz501 Syntax: NOOPrrXrrrr smtp_NOOPszSMTPChannel.smtp_NOOPcCs|d|dS)Nz221 Bye)rXclose_when_donerrrr smtp_QUITs  zSMTPChannel.smtp_QUITcCs0t|}|d||kr||dSdS)Nr)rrr)rkeywordrkeylenrrr_strip_command_keywordsz"SMTPChannel._strip_command_keywordcCsF|sdS|drt|\}}nt|\}}|s||fS|j|fS)N)rr<)lstrip startswithrr addr_spec)rraddressrestrrr_getaddrs  zSMTPChannel._getaddrcCsHi}|D]}|d\}}}|r|r|sdS|r|nd||<q|S)N=T) partitionisalnum)rparamsresultparameqrnrrr _getparamsszSMTPChannel._getparamscCs|rd}|}|dkr|ddS|dkr|ddS|dkr2d}|jr+||7}||dS|dkrFd }|jr?||7}||dS|d krQ|d dS|d kr\|d dS|dkrg|ddS|dkrr|ddS|dkr}|ddS|ddS|ddS)N [SP ]EHLOz250 Syntax: EHLO hostnameHELOz250 Syntax: HELO hostnamerz 250 Syntax: MAIL FROM:
RCPTz250 Syntax: RCPT TO:
rz250 Syntax: DATARSETz250 Syntax: RSETNOOPz250 Syntax: NOOPQUITz250 Syntax: QUITVRFYz250 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)rrXrK)rrextendedlc_argrrrr smtp_HELPs:zSMTPChannel.smtp_HELPcCsD|r||\}}|r|ddS|d|dS|ddS)NzB252 Cannot VRFY user, but will accept message and attempt deliveryz502 Could not VRFY %sz501 Syntax: VRFY
)rrX)rrrrrrr smtp_VRFYs zSMTPChannel.smtp_VRFYcCs|js |ddStd|tdd}|jr|d7}|dur%||dS|d|}||\}}|s;||dS|jsG|rG||dS|jrQ|ddS| |_ | |j }|duri||dS|j s}| dd }|d vr}|d dS|jr| d d }|durd|_n |d ur|ddS| dd}|r|s||dS|jrt||jkr|ddSt|dkr|ddS||_td|jtd|ddS)N503 Error: send HELO firstz ===> MAILrz 501 Syntax: MAIL FROM:
rzFROM:z503 Error: nested MAIL commandBODY7BIT)r8BITMIMEz1501 Error: BODY can only be one of 7BIT, 8BITMIMESMTPUTF8FTz&501 Error: SMTPUTF8 takes no argumentsSIZEz:552 Error: message size exceeds fixed maximum message sizerz:555 MAIL FROM parameters not recognized or not implementedzsender:r)rJrXr rWrKrrr_rrrrrBpoprAraisdigitr@intrkeys)rr syntaxerrrrbodysmtputf8sizerrr smtp_MAIL sh                 zSMTPChannel.smtp_MAILcCs|js |ddStd|td|js|ddSd}|jr$|d7}|dur/||dS|d|}||\}}|sE||dS|jsQ|rQ||dS| |_ | |j }|duri||dSt | dkrx|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)rJrXr rWr_rKrrrrrrrrr`r)rrrrrrrr smtp_RCPTCs@           zSMTPChannel.smtp_RCPTcCs(|r |ddS||ddS)Nz501 Syntax: RSETr)rXrIrrrr smtp_RSETfs  zSMTPChannel.smtp_RSETcCsZ|js |ddS|js|ddS|r|ddS|j|_|d|ddS)Nrz503 Error: need RCPT commandz501 Syntax: DATAs . z#354 End data with .)rJrXr`rr^rcrrrr smtp_DATAms    zSMTPChannel.smtp_DATAcCs|ddS)Nz502 EXPN not implementedrrrrr smtp_EXPN|szSMTPChannel.smtp_EXPN),rrrr]rr/ collections defaultdictr,propertyr0DATA_SIZE_DEFAULTr<rdrI_SMTPChannel__serversetter_SMTPChannel__line_SMTPChannel__state_SMTPChannel__greeting_SMTPChannel__mailfrom_SMTPChannel__rcpttos_SMTPChannel__data_SMTPChannel__fqdn_SMTPChannel__peer_SMTPChannel__conn_SMTPChannel__addrrXrrrrrrrrrrrrrrrrrrrrrs  '                       >   # 6# rc@s2eZdZeZedddfddZddZddZdS) rNFcCs||_||_||_||_||_|r|rtdtjj||dz't j |dt j i}| |dd|dd| |||dWn|td|jjtt||ftddS) Nr3r1typerr(z0%s started at %s Local addr: %s Remote addr:%sr) _localaddr _remoteaddrr@rArBr.asyncore dispatcherr<rM getaddrinfo SOCK_STREAM create_socketset_reuse_addrbindlistenrSr __class__rtimectimerW)r localaddr remoteaddrr@r2rAr[ gai_resultsrrrr<s2  zSMTPServer.__init__c Cs6tdt|td|||||j|j|j|j}dS)NzIncoming connection from %sr)r rVrW channel_classr@_maprArB)rr>r?channelrrrhandle_acceptedszSMTPServer.handle_acceptedcKst)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. )NotImplementedErrorrrQr_r`rrrrrrszSMTPServer.process_message) rrrrrrr<rrrrrrrs  rc@r )rcCsld}|}|D]+}|r&|s&d|d}t|ts t|d}t|d}t|ts/t|}t|qdS)Nr(zX-Peer: rr) splitlines isinstancerrVencoder )rrQr inheaderslinesr peerheaderrrr_print_message_contents    z&DebuggingServer._print_message_contentcKsXtd|r |drtd|d|dr td|d|||tddS)Nz%---------- MESSAGE FOLLOWS ----------rzmail options: %srzrcpt options: %s z%------------ END MESSAGE ------------)r getrr rrrrs    zDebuggingServer.process_messageN)rrrrrrrrrrs rcs,eZdZfddZddZddZZS)rcs2d|vr |dr tdtt|j|i|dS)NrAz$PureProxy does not support SMTPUTF8.)r.superrr<rrrrrrr<szPureProxy.__init__c Csf|d}d}|D] }|sn|d7}q ||d|dt|}||||}td|tddS)Nrrr(z X-Peer: %szwe got some refusals:r)rinsertrFr_deliverr rW) rrQr_r`rrrrrefusedrrrrs   zPureProxy.process_messagec Csddl}i}z%|}||jd|jdz||||}W|W|S|w|jyH}ztdtd|j }WYd}~|Sd}~wt |j fy|}z&td|j tdt |dd}t |dd } |D]} || f|| <qhWYd}~|Sd}~ww) Nrr(zgot SMTPRecipientsRefusedrgot smtp_code smtp_errorignore)smtplibSMTPconnectrsendmailquitSMTPRecipientsRefusedr rW recipientsrR SMTPExceptionrr) rr_r`rrrseerrcodeerrmsgrrrrrs2       zPureProxy._deliver)rrrr<rr __classcell__rrrrrs rcs$eZdZfddZddZZS)r cs>tdtdd|vr|drtdtt|j|i|dS)Nz8MailmanProxy is deprecated and will be removed in futurer rAz'MailmanProxy does not support SMTPUTF8.)rrir.rrr<rrrrr<s zMailmanProxy.__init__cCsddlm}ddlm}ddlm}ddlm}g} |D]:} | dd} | d} t| dkr3q| d} t| dkrB| d }nd }| | rM|d vrNq| | | |fq| D] \} } }| | qYt d d |td|r~||||}t d|tdi}||}||}|ds||d<|dstt|d<| D]g\} } }t d| td|| }|s|j| dd}||| <|d kr|j|d dq|dkr|j|d dq|dkr|j|d dq|dkr|j|d dq|dvr |dkrd|d <nd!|d <|j|d dqdS)"Nr)StringIO)Utils)Message)MailList@-r r(r)radminownerrequestrleavezforwarding recips:rrzwe got refusals:fromFromdateDatezsending message to)lock)tolistr1)toadminr2)toownerr3) torequest)rr4r subscribeSubject unsubscribe)ior+Mailmanr,r-r.lowerrr list_existsrremover rrWrrrrEnqueue)rrQr_r`rr+r,r-r. listnamesrcptlocalpartslistnamerrmlistsr%rmlistrrrr sf               zMailmanProxy.process_message)rrrr<rr*rrrrr s r c@seZdZdZdZdZdZdS)OptionsTrNF)rrrsetuid classname size_limitrArrrrrNms rNc CsXzttjdddgd\}}Wntjy*}z td|WYd}~nd}~wwt}|D]\\}}|dvr=tdq0|dvrKtttdq0|dvrSd|_ q0|d vr[||_ q0|d vrctj a q0|d vrkd |_ q0|d vrz t|}||_Wq0td|tj dtdYq0q0t|dkrd}d}n&t|dkr|d}d}nt|dkr|d}|d}n tddt||d} | dkrtdd||d| |_z t|| dd|_Wntytdd|Ynw|d} | dkrtdd||d| |_zt|| dd|_W|Sty+tdd|Y|Sw)Nr(z nVhc:s:du)zclass=nosetuidversionhelpzsize=debugr)z-hz--helpr)z-Vz --version)z-nz --nosetuidF)z-cz--class)z-dz--debug)z-uz --smtputf8T)z-sz--sizezInvalid size: rzlocalhost:8025z localhost:25r zInvalid arguments: %s:zBad local spec: %szBad local port: %szBad remote spec: %szBad remote port: %s)getoptr#argverrorr'rNr rYr%rOrPr$rWrArrQr COMMASPACErr localhost localportr. remotehost remoteport) optsrr&optionsoptrint_size localspec remotespecrrrr parseargsts|             rf__main__r5r()rAz7Cannot import module "pwd"; try running with -n option.rnobodyz3Cannot setuid "nobody"; try running with -n option.)r)Ar!r#osrTrXrrMrwarningsremail._header_value_parserrr__all__rirr:rYprogramrYr rWrFr[rr'r;rrrrrr rNrfrrarPrfindlastdot __import__r"localsmodrgrclass_r\r]r^r_rQrAproxyrOpwd ImportErrorr r$r%getpwnamrhPermissionErrorloopKeyboardInterruptrrrrsN    M-U B