
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
³Y„jÁ^  ã               @   sf   d Z dgZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZ G dd„ deƒZdS )	z<FirewallCommand class for command line client simplificationÚFirewallCommandé    N)Úerrors)ÚFirewallError)ÚDBusException)ÚcheckIPnMaskÚcheckIP6nMaskÚ	check_macÚ
check_portÚcheck_single_addressc               @   sœ  e Zd Zd\dd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zd]dd„Z	d^dd„Z
d_dd„Zd`dd„Zdadd„Zdbdd„Zdcdd„Zdddd„Zded d!„Zdfd"d#„Zdgd$d%„Zdhd&d'„Zdid(d)„Zdjd*d+„Zdkd,d-„Zd.d/„ Zdld1d2„Zdmd3d4„Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Zd?d@„ Z dg dAfdBdC„Z!dg fdDdE„Z"dg fdFdG„Z#dHdI„ Z$dJdK„ Z%dLdM„ Z&dNdO„ Z'dPdQ„ Z(dRdS„ Z)dTdU„ Z*dVdW„ Z+dXdY„ Z,dZd[„ Z-dS )nr   Fc             C   s   || _ || _d| _d | _d S )NT)ÚquietÚverboseÚ'_FirewallCommand__use_exception_handlerÚfw)Úselfr   r   © r   ú/usr/lib/python3.6/command.pyÚ__init__#   s    zFirewallCommand.__init__c             C   s
   || _ d S )N)r   )r   r   r   r   r   Úset_fw)   s    zFirewallCommand.set_fwc             C   s
   || _ d S )N)r   )r   Úflagr   r   r   Ú	set_quiet,   s    zFirewallCommand.set_quietc             C   s   | j S )N)r   )r   r   r   r   Ú	get_quiet/   s    zFirewallCommand.get_quietc             C   s
   || _ d S )N)r   )r   r   r   r   r   Úset_verbose2   s    zFirewallCommand.set_verbosec             C   s   | j S )N)r   )r   r   r   r   Úget_verbose5   s    zFirewallCommand.get_verboseNc             C   s$   |d k	r | j  r tjj|d ƒ d S )NÚ
)r   ÚsysÚstdoutÚwrite)r   Úmsgr   r   r   Ú	print_msg8   s    zFirewallCommand.print_msgc             C   s$   |d k	r | j  r tjj|d ƒ d S )Nr   )r   r   Ústderrr   )r   r   r   r   r   Úprint_error_msg<   s    zFirewallCommand.print_error_msgc             C   s,   d}d}t jjƒ r|| | }| j|ƒ d S )Nz[91mz[00m)r   r   Úisattyr    )r   r   ZFAILZENDr   r   r   Úprint_warning@   s
    
zFirewallCommand.print_warningr   c             C   s,   |dkr| j |ƒ n
| j|ƒ tj|ƒ d S )Né   )r"   r   r   Úexit)r   r   Z	exit_coder   r   r   Úprint_and_exitG   s    
zFirewallCommand.print_and_exitc             C   s   | j |dƒ d S )Né   )r%   )r   r   r   r   r   ÚfailR   s    zFirewallCommand.failc             C   s"   |d k	r| j rtjj|d ƒ d S )Nr   )r   r   r   r   )r   r   r   r   r   Úprint_if_verboseU   s    z FirewallCommand.print_if_verbosec
             C   sŠ  | j d k	r| j jƒ  g }
d}g }x¤|D ]œ}|d k	r¸y||ƒ}W nx tk
r¶ } z\tjt|ƒƒ}t|ƒdkrz| jd| ƒ n| jd| |ƒ ||krœ|j	|ƒ |d7 }w&W Y d d }~X nX |
j	|ƒ q&W �xb|
D �]X}g }|d k	rè||7 }t
|tƒ oþt
|tƒ �r|j	|ƒ n||7 }|d k	�r(||7 }| jƒ  y||Ž  W nâ ttfk
�r } zÀt
|tƒ�rx| j|jƒ ƒ |jƒ }nt|ƒ}tj|ƒ}|tjtjtjtjgk�r¨d}t|ƒdk�rÆ| jd| ƒ n,|dk�râ| jd| ƒ d S | jd| |ƒ ||k�r|j	|ƒ |d7 }W Y d d }~X nX | jƒ  qÎW |	�s†t|ƒ|k�sJd|k�rNd S t|ƒdk�rltj|d ƒ nt|ƒdk�r†tjtjƒ d S )Nr   r#   zWarning: %sz	Error: %s)r   ZauthorizeAllÚ	Exceptionr   Úget_codeÚstrÚlenr"   r%   ÚappendÚ
isinstanceÚlistÚtupleÚdeactivate_exception_handlerr   Úfail_if_not_authorizedÚget_dbus_nameÚget_dbus_messager   ÚALREADY_ENABLEDÚNOT_ENABLEDÚZONE_ALREADY_SETÚALREADY_SETÚactivate_exception_handlerr   r$   ZUNKNOWN_ERROR)r   Zcmd_typeÚoptionÚaction_methodÚquery_methodÚparse_methodÚmessageÚ
start_argsÚend_argsÚno_exitÚitemsZ_errorsZ_error_codesÚitemr   ÚcodeÚ	call_itemr   r   r   Z__cmd_sequenceY   sr    










zFirewallCommand.__cmd_sequencec          	   C   s   | j d||||||d� d S )NÚadd)rA   )Ú_FirewallCommand__cmd_sequence)r   r:   r;   r<   r=   r>   rA   r   r   r   Úadd_sequence¤   s    zFirewallCommand.add_sequencec          
   C   s    | j d||||||g|d� d S )NrF   )r?   rA   )rG   )r   Úxr:   r;   r<   r=   r>   rA   r   r   r   Úx_add_sequence©   s    zFirewallCommand.x_add_sequencec	       	      C   s$   | j d||||||g|g|d�	 d S )NrF   )r?   r@   rA   )rG   )	r   Úzoner:   r;   r<   r=   r>   ZtimeoutrA   r   r   r   Úzone_add_timeout_sequence¯   s    z)FirewallCommand.zone_add_timeout_sequencec          	   C   s   | j d||||||d� d S )NÚremove)rA   )rG   )r   r:   r;   r<   r=   r>   rA   r   r   r   Úremove_sequence¶   s    zFirewallCommand.remove_sequencec          
   C   s    | j d||||||g|d� d S )NrM   )r?   rA   )rG   )r   rI   r:   r;   r<   r=   r>   rA   r   r   r   Úx_remove_sequence»   s    z!FirewallCommand.x_remove_sequencec             C   s  g }xŠ|D ]‚}|d k	r‚y||ƒ}W n^ t k
r€ }	 zBt|ƒdkrR| jd|	 ƒ w
ntjt|	ƒƒ}
| jd|	 |
ƒ W Y d d }	~	X nX |j|ƒ q
W �xv|D �]l}g }|d k	r²||7 }t|t	ƒ rÖt|t
ƒ rÖ|j|ƒ n||7 }| jƒ  y||Ž }W nÖ tk
�rj }	 zZ| j|	jƒ ƒ tj|	jƒ ƒ}
t|ƒdk�rF| jd|	jƒ  ƒ w˜n| jd|	jƒ  |
ƒ W Y d d }	~	X n` t k
�rÈ }	 zBtjt|	ƒƒ}
t|ƒdk�r¨| jd|	 ƒ n| jd|	 |
ƒ W Y d d }	~	X nX | jƒ  t|ƒdk�rü| jd|| d| f ƒ q˜| j|ƒ q˜W |�stjdƒ d S )	Nr#   zWarning: %sz	Error: %sz%s: %sÚnoÚyesr   )rP   rQ   )r)   r,   r"   r   r*   r+   r%   r-   r.   r/   r0   r1   r   r2   r3   r4   r9   r   Úprint_query_resultr   r$   )r   r:   r<   r=   r>   r?   rA   rB   rC   r   rD   rE   Úresr   r   r   Z__query_sequenceÂ   sR    
""z FirewallCommand.__query_sequencec             C   s   | j |||||d� d S )N)rA   )Ú _FirewallCommand__query_sequence)r   r:   r<   r=   r>   rA   r   r   r   Úquery_sequenceô   s    
zFirewallCommand.query_sequencec             C   s   | j |||||g|d� d S )N)r?   rA   )rT   )r   rI   r:   r<   r=   r>   rA   r   r   r   Úx_query_sequenceù   s    
z FirewallCommand.x_query_sequencec             C   sJ   t |ƒ rFt|ƒ rFt|ƒ rF|jdƒo2t|ƒdk rFttjd| ƒ‚|S )Nzipset:é   z8'%s' is no valid IPv4, IPv6 or MAC address, nor an ipset)r   r   r   Ú
startswithr,   r   r   ÚINVALID_ADDR)r   Úvaluer   r   r   Úparse_sourceÿ   s    

zFirewallCommand.parse_sourceú/c             C   sl   y|j |ƒ\}}W n$ tk
r6   ttjd| ƒ‚Y nX t|ƒsLttj|ƒ‚|dkrdttjd| ƒ‚||fS )NzTbad port (most likely missing protocol), correct syntax is portid[-portid]%sprotocolÚtcpÚudpÚsctpÚdccpz''%s' not in {'tcp'|'udp'|'sctp'|'dccp'})r]   r^   r_   r`   )ÚsplitÚ
ValueErrorr   r   ÚINVALID_PORTr	   ÚINVALID_PROTOCOL)r   rZ   Z	separatorÚportÚprotor   r   r   Ú
parse_port  s    zFirewallCommand.parse_portc       
      C   s¸  d }d }d }d }d}xØd||d … krì||d … j ddƒd }|t|ƒd 7 }d||d … krx||d … j ddƒd }	n||d … }	|t|	ƒd 7 }|dkr¢|	}q|dkr°|	}q|dkr¾|	}q|dkrÌ|	}q|d	krÚ|rÚqttjd
| ƒ‚qW |�s ttjdƒ‚|�sttjdƒ‚|�p|�s*ttjdƒ‚t|ƒ�s@ttj|ƒ‚|dk�rZttjd| ƒ‚|�rxt|ƒ �rxttj|ƒ‚|�r¬td|ƒ �r¬|�s td|ƒ �r¬ttj	|ƒ‚||||fS )Nr   ú=r#   ú:re   rf   ÚtoportÚtoaddrÚifzinvalid forward port arg '%s'zmissing portzmissing protocolzmissing destinationr]   r^   r_   r`   z''%s' not in {'tcp'|'udp'|'sctp'|'dccp'}Úipv4Úipv6)r]   r^   r_   r`   )
ra   r,   r   r   ZINVALID_FORWARDr	   rc   rd   r
   rY   )
r   rZ   Úcompatre   Zprotocolrj   rk   ÚiÚoptÚvalr   r   r   Úparse_forward_port  sT    

z"FirewallCommand.parse_forward_portc             C   sF   |j dƒ}t|ƒdkr"|d dfS t|ƒdkr2|S ttjd| ƒ‚d S )Nrh   r#   r   Ú r&   zinvalid ipset option '%s')ra   r,   r   r   ZINVALID_OPTION)r   rZ   Úargsr   r   r   Úparse_ipset_optionH  s    
z"FirewallCommand.parse_ipset_optionc             C   s.   ddg}||kr*t tjd|dj|ƒf ƒ‚|S )Nrm   rn   z'invalid argument: %s (choose from '%s')z', ')r   r   ÚINVALID_IPVÚjoin)r   rZ   Úipvsr   r   r   Úcheck_destination_ipvR  s    z%FirewallCommand.check_destination_ipvc             C   sD   y|j ddƒ\}}W n  tk
r4   ttjdƒ‚Y nX | j|ƒ|fS )Nri   r#   z(destination syntax is ipv:address[/mask])ra   rb   r   r   ZINVALID_DESTINATIONrz   )r   rZ   ZipvZdestinationr   r   r   Úparse_service_destinationZ  s    z)FirewallCommand.parse_service_destinationc             C   s0   dddg}||kr,t tjd|dj|ƒf ƒ‚|S )Nrm   rn   Zebz'invalid argument: %s (choose from '%s')z', ')r   r   rw   rx   )r   rZ   ry   r   r   r   Ú	check_ipvb  s    
zFirewallCommand.check_ipvc             C   s0   dddg}||kr,t tjd|dj|ƒf ƒ‚|S )Nrt   rm   rn   z'invalid argument: %s (choose from '%s')z', ')r   r   rw   rx   )r   rZ   ry   r   r   r   Úcheck_helper_familyj  s    
z#FirewallCommand.check_helper_familyc             C   sB   |j dƒsttjd| ƒ‚t|jddƒƒdk r>ttjd| ƒ‚|S )NZnf_conntrack_z('%s' does not start with 'nf_conntrack_'rt   r#   zModule name '%s' too short)rX   r   r   ZINVALID_MODULEr,   Úreplace)r   rZ   r   r   r   Úcheck_moduler  s    


zFirewallCommand.check_moduleTc             C   sÞ  |j ƒ }|jƒ }|jƒ }|jƒ }	|jƒ }
|jƒ }|jƒ }|jƒ }|jƒ }|j	ƒ }|j
ƒ }|rv|jƒ }|jƒ }|jƒ }n,|jƒ }tt|jƒ | ƒƒ}|jƒ }|jƒ }dd„ }g }|d k	rÈ||krÈ|jdƒ | rÖ|sâ|sâ|rì|rì|rì|jdƒ |�r|ddj|ƒ  }| j|ƒ | j�r2| jd| ƒ | jd| ƒ |�rJ| jd	t|ƒ ƒ | jd
| ƒ |�sv| jd|�rndnd ƒ |�r¦| jddj|ƒ ƒ | jddj|ƒ ƒ n(| jddj|ƒ ƒ | jddj|ƒ ƒ | jddjt|ƒƒ ƒ | jddjdd„ |D ƒƒ ƒ | jddjt|	ƒƒ ƒ |�s:| jd|�r2dnd ƒ | jd|
�rJdnd ƒ | jd|�rbdnd djdd„ |D ƒƒ ƒ | jddjdd„ |D ƒƒ ƒ | jd dj|ƒ ƒ | jd!|�rÀdnd djt||d"�ƒ ƒ d S )#Nc             S   sf   d}d}y| j |ƒ}W n tk
r*   Y n8X |t|ƒ7 }t| ||| |d … j dƒ … jddƒƒ}|S )Nr   z	priority=ú ú"rt   )Úindexrb   r,   Úintr~   )ZruleÚpriorityZ
search_strrp   r   r   r   Úrich_rule_sorted_key’  s    *zDFirewallCommand.print_zone_policy_info.<locals>.rich_rule_sorted_keyÚdefaultZactivez (%s)z, z  summary: z  description: z  priority: z
  target: z  icmp-block-inversion: %srQ   rP   z  ingress-zones: r€   z  egress-zones: z  interfaces: z  sources: z  services: z	  ports: c             S   s    g | ]}d |d |d f ‘qS )z%s/%sr   r#   r   )Ú.0re   r   r   r   ú
<listcomp>¹  s   z:FirewallCommand.print_zone_policy_info.<locals>.<listcomp>z  protocols: z  forward: %sz  masquerade: %sz  forward-ports: z
	rt   c             S   s$   g | ]\}}}}d ||||f ‘qS )z$port=%s:proto=%s:toport=%s:toaddr=%sr   )r‡   re   rf   rj   rk   r   r   r   rˆ   À  s   z  source-ports: c             S   s    g | ]}d |d |d f ‘qS )z%s/%sr   r#   r   )r‡   re   r   r   r   rˆ   Å  s   z  icmp-blocks: z  rich rules: )Úkey)Z	getTargetZgetServicesÚgetPortsÚgetProtocolsZgetMasqueradeZgetForwardPortsÚgetSourcePortsZgetIcmpBlocksZgetRichRulesÚgetDescriptionÚgetShortZgetIngressZonesZgetEgressZonesZgetPriorityZgetIcmpBlockInversionÚsortedÚsetZgetInterfacesZ
getSourcesZ
getForwardr-   rx   r   r   r+   )r   rK   ÚsettingsÚdefault_zoneÚextra_interfacesÚisPolicyÚtargetZservicesÚportsÚ	protocolsZ
masqueradeZforward_portsÚsource_portsZicmp_blocksZrulesÚdescriptionÚshort_descriptionZingress_zonesZegress_zonesr„   Zicmp_block_inversionZ
interfacesZsourcesZforwardr…   Z
attributesr   r   r   Úprint_zone_policy_info|  sx    





z&FirewallCommand.print_zone_policy_infoc             C   s   | j ||||dd� d S )NF)r’   r“   r”   )r›   )r   rK   r‘   r’   r“   r   r   r   Úprint_zone_infoË  s    zFirewallCommand.print_zone_infoc             C   s   | j ||||dd� d S )NT)r’   r“   r”   )r›   )r   Zpolicyr‘   r’   r“   r   r   r   Úprint_policy_infoÎ  s    z!FirewallCommand.print_policy_infoc             C   s.  |j ƒ }|jƒ }|jƒ }|jƒ }|jƒ }|jƒ }|jƒ }	|jƒ }
|jƒ }| j	|ƒ | j
rt| j	d|	 ƒ | j	d| ƒ | j	ddjdd„ |D ƒƒ ƒ | j	ddj|ƒ ƒ | j	ddjd	d„ |D ƒƒ ƒ | j	d
dj|ƒ ƒ | j	ddjdd„ |jƒ D ƒƒ ƒ | j	ddjt|
ƒƒ ƒ | j	ddjt|ƒƒ ƒ d S )Nz  summary: z  description: z	  ports: r€   c             S   s    g | ]}d |d |d f ‘qS )z%s/%sr   r#   r   )r‡   re   r   r   r   rˆ   ß  s   z6FirewallCommand.print_service_info.<locals>.<listcomp>z  protocols: z  source-ports: c             S   s    g | ]}d |d |d f ‘qS )z%s/%sr   r#   r   )r‡   re   r   r   r   rˆ   ã  s   z  modules: z  destination: c             S   s   g | ]\}}d ||f ‘qS )z%s:%sr   )r‡   ÚkÚvr   r   r   rˆ   ç  s   z  includes: z  helpers: )rŠ   r‹   rŒ   Z
getModulesr�   ÚgetDestinationsrŽ   ZgetIncludesZ
getHelpersr   r   rx   rB   r�   )r   Zservicer‘   r–   r—   r˜   Úmodulesr™   Údestinationsrš   ZincludesZhelpersr   r   r   Úprint_service_infoÑ  s2    


z"FirewallCommand.print_service_infoc             C   sp   |j ƒ }|jƒ }|jƒ }t|ƒdkr,ddg}| j|ƒ | jrX| jd| ƒ | jd| ƒ | jddj|ƒ ƒ d S )Nr   rm   rn   z  summary: z  description: z  destination: r€   )r    r�   rŽ   r,   r   r   rx   )r   Zicmptyper‘   r¢   r™   rš   r   r   r   Úprint_icmptype_infoì  s    
z#FirewallCommand.print_icmptype_infoc             C   sœ   |j ƒ }|jƒ }|jƒ }|jƒ }|jƒ }| j|ƒ | jrT| jd| ƒ | jd| ƒ | jd| ƒ | jddjdd„ |jƒ D ƒƒ ƒ | jddj|ƒ ƒ d S )	Nz  summary: z  description: z  type: z  options: r€   c             S   s$   g | ]\}}|rd ||f n|‘qS )z%s=%sr   )r‡   rž   rŸ   r   r   r   rˆ     s   z4FirewallCommand.print_ipset_info.<locals>.<listcomp>z  entries: )	ZgetTypeZ
getOptionsZ
getEntriesr�   rŽ   r   r   rx   rB   )r   Zipsetr‘   Z
ipset_typeZoptionsÚentriesr™   rš   r   r   r   Úprint_ipset_infoø  s    
z FirewallCommand.print_ipset_infoc             C   s’   |j ƒ }|jƒ }|jƒ }|jƒ }|jƒ }| j|ƒ | jrT| jd| ƒ | jd| ƒ | jd| ƒ | jd| ƒ | jddjdd„ |D ƒƒ ƒ d S )	Nz  summary: z  description: z
  family: z
  module: z	  ports: r€   c             S   s    g | ]}d |d |d f ‘qS )z%s/%sr   r#   r   )r‡   re   r   r   r   rˆ     s   z5FirewallCommand.print_helper_info.<locals>.<listcomp>)rŠ   Z	getModuleZ	getFamilyr�   rŽ   r   r   rx   )r   Úhelperr‘   r–   ÚmoduleZfamilyr™   rš   r   r   r   Úprint_helper_info  s    
z!FirewallCommand.print_helper_infoc             C   s    |r| j dƒ n| j ddƒ d S )NrQ   rP   r#   )r%   )r   rZ   r   r   r   rR     s    z"FirewallCommand.print_query_resultc             C   s\   | j s‚ | j|ƒ tjt|ƒƒ}|tjtjtjtj	gkrH| j
d| ƒ n| jd| |ƒ d S )NzWarning: %sz	Error: %s)r   r2   r   r*   r+   r   r5   r6   r7   r8   r"   r%   )r   Úexception_messagerD   r   r   r   Úexception_handler  s    

z!FirewallCommand.exception_handlerc             C   s   d|krd}| j |tjƒ d S )NZNotAuthorizedExceptionz`Authorization failed.
    Make sure polkit agent is running or run the application as superuser.)r%   r   ZNOT_AUTHORIZED)r   rª   r   r   r   r   r2   '  s    z&FirewallCommand.fail_if_not_authorizedc             C   s
   d| _ d S )NF)r   )r   r   r   r   r1   -  s    z,FirewallCommand.deactivate_exception_handlerc             C   s
   d| _ d S )NT)r   )r   r   r   r   r9   0  s    z*FirewallCommand.activate_exception_handlerc             C   sp   g }t ƒ }t|ƒ}xP|D ]H}|s"P |jƒ }t|ƒdk s|d dkrDq||kr|j|ƒ |j|ƒ qW |jƒ  |S )Nr#   r   ú#ú;)r¬   r­   )r�   ÚopenÚstripr,   r-   rF   Úclose)r   Úfilenamer¥   Zentries_setÚfÚliner   r   r   Úget_ipset_entries_from_file3  s    

z+FirewallCommand.get_ipset_entries_from_file)FF)N)N)N)Nr   )N)N)NNF)F)F)F)F)F)NF)F)F)r\   )F).Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r    r"   r%   r'   r(   rG   rH   rJ   rL   rN   rO   rT   rU   rV   r[   rg   rs   rv   rz   r{   r|   r}   r   r›   rœ   r�   r£   r¤   r¦   r©   rR   r«   r2   r1   r9   r´   r   r   r   r   r   "   sX   






 
J





2



2

O)Ú__doc__Ú__all__r   Zfirewallr   Zfirewall.errorsr   Zdbus.exceptionsr   Zfirewall.functionsr   r   r   r	   r
   Úobjectr   r   r   r   r   Ú<module>   s   