a �DOg��@sNddlZddlZddlZddlZddlZddlZddlZddlZddlZddl m Z ddl m Z m Z gd�ZejdZdZGdd�d�Ze�adZd Zd Zd%d d �ZGdd�dej�ZGdd�dej�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�d�Zdd�Z e!dk�rJe �Z"e"j#Z#de#v�rfe#�$d�Z%e&e#de%�e'�e(�d g�Z)e#e%dd�Z#nddl*Z)e+e)e#�Z,e,e"j-e"j.fe"j/e"j0fe"j1e"j2d�Z3e"j4�r(z ddl5Z5Wn,e6�y�e7d ej8d!�e�9d�Yn0e5�:d"�d#Z;ze�4e;�Wn,e<�y&e7d$ej8d!�e�9d�Yn0z e�=�Wne>�yHYn0dS)&�N)�warn)� get_addr_spec�get_angle_addr)� SMTPChannel� SMTPServer�DebuggingServer� PureProxy� MailmanProxyzPython SMTP proxy version 0.3c@seZdZdd�Zdd�ZdS)�DevnullcCsdS�N���self�msgr r �/usr/lib64/python3.9/smtpd.py�writef�z Devnull.writecCsdSr r �rr r r�flushgrz Devnull.flushN)�__name__� __module__� __qualname__rrr r r rr esr � z, i�cCs4ttt�tjd�|r&t|tjd�t�|�dS)N��file)�print�__doc__�globals�sys�stderr�exit)�coderr r r�usagepsr#c@s�eZdZdZdZdZe�efdd��Ze dd��Z e dd d fd d �Z d d �Z dd�Ze dd��Zejdd��Ze dd��Zejdd��Ze dd��Zejdd��Ze dd��Zejdd��Ze dd��Zejdd��Ze 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/��Zd1d2�Zd3d4�Zd5d6�Zd7d8�Zd9d:�Zd;d<�Z d=d>�Z!d?d@�Z"dAdB�Z#dCdD�Z$dEdF�Z%dGdH�Z&dIdJ�Z'dKdL�Z(dMdN�Z)dOdP�Z*dQdR�Z+dS)Srr�icCs|Sr r )�xr r r�|rzSMTPChannel.cCs.zt|j���WSty(|jYS0dSr )�max�command_size_limits�values� ValueError�command_size_limitrr r r�max_command_size_limit~s z"SMTPChannel.max_command_size_limitNFc Cs&tjj|||d�||_||_||_||_||_||_|rF|rFt d��|rdd|_ d|_ d|_ t |_nd|_ d|_ td�|_ d |_|��d|_d |_|j��t��|_z|��|_WnBt�y�}z(|��|jd tjkr�WYd}~dSd}~00td t |j�t!d �|�"d|jt#f�dS)N��map�Fdecode_data and enable_SMTPUTF8 cannot be set to True at the same timer� �.r� �.� FrzPeer:rz 220 %s %s)$�asynchat� async_chat�__init__� smtp_server�conn�addr�data_size_limit�enable_SMTPUTF8� _decode_datar*� _emptystring�_linesep�_dotsep�NEWLINE�_newline�ord�_set_rset_state� seen_greeting� extended_smtpr(�clear�socket�getfqdn�fqdn� getpeername�peer�OSError�close�args�errnoZENOTCONNr�repr� DEBUGSTREAM�push� __version__) rZserverr9r:r;r.r<� decode_data�errr r rr7�s@   zSMTPChannel.__init__cCs.|j|_d|_g|_d|_d|_|�d�dS)NFrr2)�COMMAND� smtp_state�mailfrom�rcpttos�require_SMTPUTF8� num_bytes�set_terminatorrr r r�_set_post_data_state�s z SMTPChannel._set_post_data_statecCs|��d|_g|_dS�Nr)r^� received_data�received_linesrr r rrD�szSMTPChannel._set_rset_statecCstdtd�|jS)NzTAccess to __server attribute on SMTPChannel is deprecated, use 'smtp_server' instead��r�DeprecationWarningr8rr r r�__server�s�zSMTPChannel.__servercCstdtd�||_dS)NzRSetting __server attribute on SMTPChannel is deprecated, set 'smtp_server' insteadrbrc�r�valuer r rre�s�cCstdtd�|jS)NzUAccess to __line attribute on SMTPChannel is deprecated, use 'received_lines' insteadrb�rrdrarr r r�__line�s�zSMTPChannel.__linecCstdtd�||_dS)NzSSetting __line attribute on SMTPChannel is deprecated, set 'received_lines' insteadrbrhrfr r rri�s�cCstdtd�|jS)NzRAccess to __state attribute on SMTPChannel is deprecated, use 'smtp_state' insteadrb�rrdrXrr r r�__state�s�zSMTPChannel.__statecCstdtd�||_dS)NzPSetting __state attribute on SMTPChannel is deprecated, set 'smtp_state' insteadrbrjrfr r rrk�s�cCstdtd�|jS)NzXAccess to __greeting attribute on SMTPChannel is deprecated, use 'seen_greeting' insteadrb�rrdrErr r r� __greeting�s�zSMTPChannel.__greetingcCstdtd�||_dS)NzVSetting __greeting attribute on SMTPChannel is deprecated, set 'seen_greeting' insteadrbrlrfr r rrm�s�cCstdtd�|jS)NzSAccess to __mailfrom attribute on SMTPChannel is deprecated, use 'mailfrom' insteadrb�rrdrYrr r r� __mailfrom�s�zSMTPChannel.__mailfromcCstdtd�||_dS)NzQSetting __mailfrom attribute on SMTPChannel is deprecated, set 'mailfrom' insteadrbrnrfr r rro�s�cCstdtd�|jS)NzQAccess to __rcpttos attribute on SMTPChannel is deprecated, use 'rcpttos' insteadrb�rrdrZrr r r� __rcpttos�s�zSMTPChannel.__rcpttoscCstdtd�||_dS)NzOSetting __rcpttos attribute on SMTPChannel is deprecated, set 'rcpttos' insteadrbrprfr r rrq�s�cCstdtd�|jS)NzTAccess to __data attribute on SMTPChannel is deprecated, use 'received_data' insteadrb�rrdr`rr r r�__data�s�zSMTPChannel.__datacCstdtd�||_dS)NzRSetting __data attribute on SMTPChannel is deprecated, set 'received_data' insteadrbrrrfr r rrss�cCstdtd�|jS)NzKAccess to __fqdn attribute on SMTPChannel is deprecated, use 'fqdn' insteadrb�rrdrJrr r r�__fqdn s�zSMTPChannel.__fqdncCstdtd�||_dS)NzISetting __fqdn attribute on SMTPChannel is deprecated, set 'fqdn' insteadrbrtrfr r rrus�cCstdtd�|jS)NzKAccess to __peer attribute on SMTPChannel is deprecated, use 'peer' insteadrb�rrdrLrr r r�__peers�zSMTPChannel.__peercCstdtd�||_dS)NzISetting __peer attribute on SMTPChannel is deprecated, set 'peer' insteadrbrvrfr r rrws�cCstdtd�|jS)NzKAccess to __conn attribute on SMTPChannel is deprecated, use 'conn' insteadrb�rrdr9rr r r�__conn s�zSMTPChannel.__conncCstdtd�||_dS)NzISetting __conn attribute on SMTPChannel is deprecated, set 'conn' insteadrbrxrfr r rry%s�cCstdtd�|jS)NzKAccess to __addr attribute on SMTPChannel is deprecated, use 'addr' insteadrb�rrdr:rr r r�__addr+s�zSMTPChannel.__addrcCstdtd�||_dS)NzISetting __addr attribute on SMTPChannel is deprecated, set 'addr' insteadrbrzrfr r rr{0s�cCs&tj�|t|d|jrdnd��dS)Nr0�utf-8�ascii)r5r6rS�bytesr[r r r rrS7s �zSMTPChannel.pushcCs|d}|j|jkr|j}n|j|jkr*|j}|r<|j|kr�joinrarrQrRrXrWr\rSr=r��find�upper�striprFr(r+�getattrrr;�splitr?r@r�rBr`rLrYrZr�r�r8�process_messager^) r�lineZsz�i�command�argZmax_sz�methodr��textrO�kwargs�statusr r r�found_terminatorLsl   � �   � zSMTPChannel.found_terminatorcCsH|s|�d�dS|jr&|�d�dS|��||_|�d|j�dS)Nz501 Syntax: HELO hostname�503 Duplicate HELO/EHLOz250 %s)rSrErDrJ�rr�r r r� smtp_HELO�s  zSMTPChannel.smtp_HELOcCs�|s|�d�dS|jr&|�d�dS|��||_d|_|�d|j�|jrr|�d|j�|jdd7<|js�|�d�|jr�|�d �|jdd 7<|�d �dS) Nz501 Syntax: EHLO hostnamer�Tz250-%sz 250-SIZE %s�MAIL�z 250-8BITMIMEz 250-SMTPUTF8� z250 HELP) rSrErDrFrJr;r(r=r<r�r r r� smtp_EHLO�s&    zSMTPChannel.smtp_EHLOcCs|r|�d�n |�d�dS)Nz501 Syntax: NOOPr��rSr�r r r� smtp_NOOP�s zSMTPChannel.smtp_NOOPcCs|�d�|��dS)Nz221 Bye)rSZclose_when_doner�r r r� smtp_QUIT�s zSMTPChannel.smtp_QUITcCs0t|�}|d|���|kr,||d���SdSr_)r�r�r�)r�keywordr�Zkeylenr r r�_strip_command_keyword�sz"SMTPChannel._strip_command_keywordcCsF|sdS|���d�r$t|�\}}n t|�\}}|s<||fS|j|fS)N)rr�<)�lstrip� startswithrrZ addr_spec)rr��address�restr r r�_getaddr�s zSMTPChannel._getaddrcCsHi}|D]:}|�d�\}}}|��r,|r2|s2dS|r:|nd||<q|S)N�=T)� partition�isalnum)r�params�resultZparam�eqrgr r r� _getparams�szSMTPChannel._getparamscCs|r�d}|��}|dkr$|�d�q�|dkr8|�d�q�|dkr^d}|jrR||7}|�|�q�|dkr�d }|jrx||7}|�|�q�|d kr�|�d �q�|d kr�|�d �q�|dkr�|�d�q�|dkr�|�d�q�|dkr�|�d�q�|�d�n |�d�dS)N� [SP ]ZEHLOz250 Syntax: EHLO hostnameZHELOz250 Syntax: HELO hostnamer�z 250 Syntax: MAIL FROM:
ZRCPTz250 Syntax: RCPT TO:
rz250 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)r�rSrF)rr�ZextendedZlc_argrr r r� smtp_HELP�s:          zSMTPChannel.smtp_HELPcCs@|r2|�|�\}}|r"|�d�q<|�d|�n |�d�dS)NzB252 Cannot VRFY user, but will accept message and attempt deliveryz502 Could not VRFY %sz501 Syntax: VRFY
)r�rS)rr�r�r�r r r� smtp_VRFY�s  zSMTPChannel.smtp_VRFYcCs�|js|�d�dStd|td�d}|jr4|d7}|durJ|�|�dS|�d|�}|�|�\}}|sv|�|�dS|js�|r�|�|�dS|jr�|�d�dS|��� �|_ |� |j �}|dur�|�|�dS|j s�|� dd �}|d vr�|�d �dS|j�r8|� d d �}|du�r d|_n|d u�r8|�d�dS|� dd�}|�r�|���sb|�|�dS|j�r�t|�|jk�r�|�d�dSt|���dk�r�|�d�dS||_td|jtd�|�d�dS)N�503 Error: send HELO firstz ===> MAILrz 501 Syntax: MAIL FROM:
r�zFROM:z503 Error: nested MAIL commandZBODY�7BIT)r�Z8BITMIMEz1501 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�)rErSrrRrFr�r�rYr�r�r�r�r=�popr<r[�isdigitr;�intr��keys)rr�� syntaxerrr�r��body�smtputf8�sizer r r� smtp_MAILsh                    zSMTPChannel.smtp_MAILcCs|js|�d�dStd|td�|js6|�d�dSd}|jrH|d7}|dur^|�|�dS|�d|�}|�|�\}}|s�|�|�dS|js�|r�|�|�dS|��� �|_ |� |j �}|dur�|�|�dSt |� ��dkr�|�d �dS|j�|�td |jtd�|�d �dS) Nr�z ===> RCPTrz503 Error: need MAIL commandz501 Syntax: RCPT TO:
r�zTO:rz8555 RCPT TO parameters not recognized or not implementedzrecips:r�)rErSrrRrYrFr�r�r�r�r�r�r�r�rZr�)rr�r�r�r�r r r� smtp_RCPT7s@           zSMTPChannel.smtp_RCPTcCs(|r|�d�dS|��|�d�dS)Nz501 Syntax: RSETr�)rSrDr�r r r� smtp_RSETZs  zSMTPChannel.smtp_RSETcCsZ|js|�d�dS|js(|�d�dS|r:|�d�dS|j|_|�d�|�d�dS)Nr�z503 Error: need RCPT commandz501 Syntax: DATAs . z#354 End data with .)rErSrZrrXr]r�r r r� smtp_DATAas    zSMTPChannel.smtp_DATAcCs|�d�dS)Nz502 EXPN not implementedr�r�r r r� smtp_EXPNpszSMTPChannel.smtp_EXPN),rrrrWrr+� collections� defaultdictr(�propertyr,�DATA_SIZE_DEFAULTr7r^rDZ_SMTPChannel__server�setterZ_SMTPChannel__lineZ_SMTPChannel__stateZ_SMTPChannel__greetingZ_SMTPChannel__mailfromZ_SMTPChannel__rcpttosZ_SMTPChannel__dataZ_SMTPChannel__fqdnZ_SMTPChannel__peerZ_SMTPChannel__connZ_SMTPChannel__addrrSr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r r r rrws� � '                       >   # 6#rc@s2eZdZeZedddfdd�Zdd�Zdd�ZdS) rNFcCs�||_||_||_||_||_|r.|r.td��tjj||d�zNt j |dt j i�}|� |dd|dd�|� �|�|�|�d�Wn|���Yn(0td|jjt�t���||ftd�dS) Nr/r-�typerr$�z0%s started at %s Local addr: %s Remote addr:%sr)Z _localaddr� _remoteaddrr;r<r=r*�asyncore� dispatcherr7rH� getaddrinfo� SOCK_STREAMZ create_socketZset_reuse_addr�bind�listenrNr� __class__r�time�ctimerR)rZ localaddrZ remoteaddrr;r.r<rUZ gai_resultsr r rr7xs2� ��zSMTPServer.__init__c Cs6tdt|�td�|�||||j|j|j|j�}dS)NzIncoming connection from %sr)rrQrR� channel_classr;�_mapr<r=)rr9r:Zchannelr r r�handle_accepted�s�zSMTPServer.handle_acceptedcKst�dSr )�NotImplementedError�rrLrYrZr�r�r r rr��szSMTPServer.process_message) rrrrr�r�r7r�r�r r r rrts �  rc@seZdZdd�Zdd�ZdS)rcCsld}|��}|D]V}|rL|sLd|d}t|t�s@t|�d��}t|�d}t|t�s^t|�}t|�qdS)Nr$zX-Peer: rr|)� splitlines� isinstancer�rQ�encoder)rrLr�Z inheaders�linesr�Z peerheaderr r r�_print_message_content�s   z&DebuggingServer._print_message_contentcKsXtd�|r@|�d�r&td|d�|�d�r@td|d�|�||�td�dS)Nz%---------- MESSAGE FOLLOWS ----------r�zmail options: %sr�zrcpt options: %s z%------------ END MESSAGE ------------)r�getr�r�r r rr��s   zDebuggingServer.process_messageN)rrrr�r�r r r rr�srcs,eZdZ�fdd�Zdd�Zdd�Z�ZS)rcs2d|vr|drtd��tt|�j|i|��dS)Nr<z$PureProxy does not support SMTPUTF8.)r*�superrr7�rrOr��r�r rr7�szPureProxy.__init__c Csf|�d�}d}|D]}|sq(|d7}q|�|d|d�t�|�}|�|||�}td|td�dS)Nrrr$z X-Peer: %szwe got some refusals:r)r��insertrAr��_deliverrrR) rrLrYrZr�r�r�r��refusedr r rr��s   zPureProxy.process_messagec Cs�ddl}i}zJ|��}|�|jd|jd�z|�|||�}W|��n |��0Wn�|jy�}ztdtd�|j }WYd}~npd}~0t |j fy�}zJtd|j td�t |dd�}t |dd �} |D]} || f|| <q�WYd}~n d}~00|S) Nrr$zgot SMTPRecipientsRefusedrZgotZ smtp_code�����Z smtp_error�ignore)�smtplibZSMTP�connectr�Zsendmail�quitZSMTPRecipientsRefusedrrRZ recipientsrMZ SMTPExceptionr�r�) rrYrZr�r�r��s�eZerrcode�errmsg�rr r rr��s$   $zPureProxy._deliver)rrrr7r�r�� __classcell__r r r�rr�s rcs$eZdZ�fdd�Zdd�Z�ZS)r cs>tdtd�d|vr$|dr$td��tt|�j|i|��dS)Nz8MailmanProxy is deprecated and will be removed in futurerbr<z'MailmanProxy does not support SMTPUTF8.)rrdr*r�rr7r�r�r rr7 s �zMailmanProxy.__init__cCs*ddlm}ddlm}ddlm}ddlm}g} |D]t} | ���d�d} | �d�} t| �dkrfq8| d} t| �dkr�| d }nd }|� | �r8|d vr�q8| � | | |f�q8| D]\} } }|� | �q�t d d � |�td�|r�|�|||�}t d|td�i}||�}|�|�}|�d��s&||d<|�d��sDt�t���|d<| D]�\} } }t d| td�|�| �}|�s�|j| dd�}||| <|d k�r�|j|d d�n�|dk�r�|j|d d�nh|dk�r�|j|d d�nN|dk�r�|j|d d�n4|dv�rH|dk�r d|d <nd!|d <|j|d d��qHdS)"Nr)�StringIO)�Utils)�Message)�MailList�@�-rbr$r)r�admin�owner�requestr��leavezforwarding recips:r�rzwe got refusals:�fromZFrom�dateZDatezsending message to)�lock)�tolistr�)Ztoadminr�)Ztoownerr�)Z torequest)r�r�r�Z subscribeZSubjectZ unsubscribe)�ior�ZMailmanr�r�r��lowerr�r�Z list_existsr��removerr�rRr�r�r�r�ZEnqueue)rrLrYrZr�r�r�r�r�Z listnamesZrcpt�local�partsZlistnamer�r�Zmlistsr�rZmlistr r rr�sb                    zMailmanProxy.process_message)rrrr7r�r�r r r�rr s r c@seZdZdZdZdZdZdS)�OptionsTrNF)rrr�setuid� classname� size_limitr<r r r rrasrc Csdz$t�tjdd�dgd��\}}Wn0tjyT}ztd|�WYd}~n d}~00t�}|D]�\}}|dvrztd�q`|dvr�tt�t�d�q`|dvr�d|_ q`|d vr�||_ q`|d vr�tj a q`|d vr�d |_ q`|d vr`zt|�}||_Wq`td|tj d�t�d�Yq`0q`t|�dk�r4d}d}nPt|�dk�rP|d}d}n4t|�dk�rp|d}|d}ntddt�|��|�d�} | dk�r�tdd|�|d| �|_zt|| dd��|_Wn"t�y�tdd|�Yn0|�d�} | dk�rtdd|�|d| �|_zt|| dd��|_Wn"t�y^tdd|�Yn0|S)Nr$z nVhc:s:du)zclass=Znosetuid�version�helpzsize=�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:25rb�zInvalid arguments: %s�:zBad local spec: %szBad local port: %szBad remote spec: %szBad remote port: %s)�getoptr�argv�errorr#rrrTr!rrr rRr<r�rr�� COMMASPACEr�r�� localhost� localportr*� remotehost� remoteport) ZoptsrOr��options�optr�Zint_sizeZ localspecZ remotespecr�r r r� parseargshsp�           r�__main__r1r$)r<z7Cannot import module "pwd"; try running with -n option.r�nobodyrbz3Cannot setuid "nobody"; try running with -n option.)r)?r�osrPr r�rHr�r5r��warningsrZemail._header_value_parserrr�__all__rZprogramrTr rRrArr�r#r6rr�rrrr rrrrr�rfindZlastdot� __import__r�locals�modrr��class_rrrrrr<�proxyr�pwd� ImportErrorrr r!�getpwnamr�PermissionErrorZloop�KeyboardInterruptr r r r�Psv   M-UB      �