
c`c           @   s   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 d e f d     YZ d S(	   s<   FirewallCommand class for command line client simplificationt   FirewallCommandiN(   t   errors(   t   FirewallError(   t   DBusException(   t   checkIPnMaskt   checkIP6nMaskt	   check_mact
   check_portt   check_single_addressc           B   s  e  Z e e d   Z d   Z d   Z d   Z d   Z d   Z d+ d  Z
 d+ d  Z d+ d  Z d+ d	 d
  Z d+ d  Z d+ d  Z d+ d+ e d  Z e d  Z e d  Z e d  Z e d  Z e d  Z d+ e d  Z e d  Z e d  Z d   Z d d  Z e d  Z d   Z d   Z d   Z d   Z d   Z  d   Z! d+ g  d   Z" d!   Z# d"   Z$ d#   Z% d$   Z& d%   Z' d&   Z( d'   Z) d(   Z* d)   Z+ d*   Z, RS(,   c         C   s(   | |  _  | |  _ t |  _ d  |  _ d  S(   N(   t   quiett   verboset   Truet'   _FirewallCommand__use_exception_handlert   Nonet   fw(   t   selfR	   R
   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   __init__#   s    			c         C   s   | |  _  d  S(   N(   R   (   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   set_fw)   s    c         C   s   | |  _  d  S(   N(   R	   (   R   t   flag(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt	   set_quiet,   s    c         C   s   |  j  S(   N(   R	   (   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt	   get_quiet/   s    c         C   s   | |  _  d  S(   N(   R
   (   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   set_verbose2   s    c         C   s   |  j  S(   N(   R
   (   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   get_verbose5   s    c         C   s1   | d  k	 r- |  j r- t j j | d  n  d  S(   Ns   
(   R   R	   t   syst   stdoutt   write(   R   t   msg(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt	   print_msg8   s    c         C   s1   | d  k	 r- |  j r- t j j | d  n  d  S(   Ns   
(   R   R	   R   t   stderrR   (   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_error_msg<   s    c         C   s=   d } d } t  j j   r, | | | } n  |  j |  d  S(   Ns   [91ms   [00m(   R   R   t   isattyR   (   R   R   t   FAILt   END(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_warning@   s
    i    c         C   s:   | d k r |  j  |  n |  j |  t j |  d  S(   Ni   (   R!   R   R   t   exit(   R   R   t	   exit_code(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_and_exitG   s    c         C   s   |  j  | d  d  S(   Ni   (   R$   (   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   failR   s    c         C   s0   | d  k	 r, |  j r, t j j | d  n  d  S(   Ns   
(   R   R
   R   R   R   (   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_if_verboseU   s    c
         C   s1  |  j  d  k	 r |  j  j   n  g  }
 d } g  } x | D] } | d  k	 r y | |  } Wq t k
 r } t j t |   } t |  d k r |  j d |  n |  j	 d | |  | | k r | j
 |  n  | d 7} q8 q Xn  |
 j
 |  q8 Wx|
 D]} g  } | d  k	 r(| | 7} n  t | t  rXt | t  rX| j
 |  n
 | | 7} | d  k	 r{| | 7} n  |  j   y | |   Wnt t f k
 r} t | t  r|  j | j    | j   } n t |  } t j |  } | t j t j t j t j g k r$d } n  t |  d k rJ|  j d |  n5 | d k rk|  j d |  d  S|  j	 d | |  | | k r| j
 |  n  | d 7} n X|  j   qW|	 s-t |  | k sd | k rd  St |  d k rt j | d  q-t |  d k r-t j t j  q-n  d  S(   Ni    i   s   Warning: %ss	   Error: %s(   R   R   t   authorizeAllt	   ExceptionR   t   get_codet   strt   lenR!   R$   t   appendt
   isinstancet   listt   tuplet   deactivate_exception_handlerR   t   fail_if_not_authorizedt   get_dbus_namet   get_dbus_messageR   t   ALREADY_ENABLEDt   NOT_ENABLEDt   ZONE_ALREADY_SETt   ALREADY_SETt   activate_exception_handlerR   R"   t   UNKNOWN_ERROR(   R   t   cmd_typet   optiont   action_methodt   query_methodt   parse_methodt   messaget
   start_argst   end_argst   no_exitt   itemst   _errorst   _error_codest   itemR   t   codet	   call_item(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   __cmd_sequenceY   sr    

 

	c      	   C   s&   |  j  d | | | | | d | d  S(   Nt   addRB   (   t   _FirewallCommand__cmd_sequence(   R   R;   R<   R=   R>   R?   RB   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   add_sequence   s    c         C   s/   |  j  d | | | | | d | g d | d  S(   NRJ   R@   RB   (   RK   (   R   t   xR;   R<   R=   R>   R?   RB   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   x_add_sequence   s    c	   	      C   s8   |  j  d | | | | | d | g d | g d | d  S(   NRJ   R@   RA   RB   (   RK   (	   R   t   zoneR;   R<   R=   R>   R?   t   timeoutRB   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   zone_add_timeout_sequence   s    c      	   C   s&   |  j  d | | | | | d | d  S(   Nt   removeRB   (   RK   (   R   R;   R<   R=   R>   R?   RB   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   remove_sequence   s    c         C   s/   |  j  d | | | | | d | g d | d  S(   NRR   R@   RB   (   RK   (   R   RM   R;   R<   R=   R>   R?   RB   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   x_remove_sequence   s    c         C   s|  g  } x | D] } | d  k	 r y | |  } Wq t k
 r }	 t |  d k rj |  j d |	  q q t j t |	   }
 |  j d |	 |
  q Xn  | j |  q Wx| D]} g  } | d  k	 r | | 7} n  t	 | t
  rt	 | t  r| j |  n
 | | 7} |  j   y | |   } Wn t k
 r}	 |  j |	 j    t j |	 j    }
 t |  d k r|  j d |	 j    q q|  j d |	 j   |
  nb t k
 r}	 t j t |	   }
 t |  d k r|  j d |	  q|  j d |	 |
  n X|  j   t |  d k rQ|  j d | | d | f  q |  j |  q W| sxt j d  n  d  S(	   Ni   s   Warning: %ss	   Error: %ss   %s: %st   not   yesi    (   RU   s   yes(   R   R(   R+   R!   R   R)   R*   R$   R,   R-   R.   R/   R0   R   R1   R2   R3   R8   R   t   print_query_resultR   R"   (   R   R;   R=   R>   R?   R@   RB   RC   RF   R   RG   RH   t   res(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   __query_sequence   sR     



"c         C   s    |  j  | | | | d | d  S(   NRB   (   t    _FirewallCommand__query_sequence(   R   R;   R=   R>   R?   RB   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   query_sequence   s    c      	   C   s)   |  j  | | | | d | g d | d  S(   NR@   RB   (   RZ   (   R   RM   R;   R=   R>   R?   RB   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   x_query_sequence   s    c         C   sf   t  |  rb t |  rb t |  rb | j d  oE t |  d k rb t t j d |   n  | S(   Ns   ipset:i   s8   '%s' is no valid IPv4, IPv6 or MAC address, nor an ipset(   R   R   R   t
   startswithR+   R   R   t   INVALID_ADDR(   R   t   value(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   parse_source   s    "	t   /c         C   s   y | j  |  \ } } Wn' t k
 rB t t j d |   n Xt |  sd t t j |   n  | d k r t t j d |   n  | | f S(   NsT   bad port (most likely missing protocol), correct syntax is portid[-portid]%sprotocolt   tcpt   udpt   sctpt   dccps'   '%s' not in {'tcp'|'udp'|'sctp'|'dccp'}(   Rb   Rc   Rd   Re   (   t   splitt
   ValueErrorR   R   t   INVALID_PORTR   t   INVALID_PROTOCOL(   R   R_   t	   separatort   portt   proto(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt
   parse_port  s    	c   
      C   sF  d  } d  } d  } d  } d } xd | | k r,| | j d d  d } | t |  d 7} d | | k r | | j d d  d }	 n
 | | }	 | 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| sHt t j d   n  | sct t j d   n  | pl| st t j d   n  t |  st t j |   n  | d k rt t j d |   n  | rt |  rt t j |   n  | r6t	 d |  r6| st	 d |  r6t t j
 |   q6n  | | | | f S(   Ni    t   =i   t   :Rk   Rl   t   toportt   toaddrt   ifs   invalid forward port arg '%s's   missing ports   missing protocols   missing destinationRb   Rc   Rd   Re   s'   '%s' not in {'tcp'|'udp'|'sctp'|'dccp'}t   ipv4t   ipv6(   s   tcps   udps   sctps   dccp(   R   Rf   R+   R   R   t   INVALID_FORWARDR   Rh   Ri   R   R^   (
   R   R_   t   compatRk   t   protocolRp   Rq   t   it   optt   val(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   parse_forward_port  sT    
						c         C   s_   | j  d  } t |  d k r/ | d d f St |  d k rE | St t j d |   d  S(   NRn   i   i    t    i   s   invalid ipset option '%s'(   Rf   R+   R   R   t   INVALID_OPTION(   R   R_   t   args(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   parse_ipset_optionH  s    	c         C   sD   d d g } | | k r@ t  t j d | d j |  f   n  | S(   NRs   Rt   s'   invalid argument: %s (choose from '%s')s   ', '(   R   R   t   INVALID_IPVt   join(   R   R_   t   ipvs(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   check_destination_ipvR  s    	c         C   sU   y | j  d d  \ } } Wn# t k
 rA t t j d   n X|  j |  | f S(   NRo   i   s(   destination syntax is ipv:address[/mask](   Rf   Rg   R   R   t   INVALID_DESTINATIONR   (   R   R_   t   ipvt   destination(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   parse_service_destinationZ  s    	c         C   sG   d d d g } | | k rC t  t j d | d j |  f   n  | S(   NRs   Rt   t   ebs'   invalid argument: %s (choose from '%s')s   ', '(   R   R   R   R   (   R   R_   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt	   check_ipvb  s    	c         C   sG   d d d g } | | k rC t  t j d | d j |  f   n  | S(   NR|   Rs   Rt   s'   invalid argument: %s (choose from '%s')s   ', '(   R   R   R   R   (   R   R_   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   check_helper_familyj  s    	c         C   sc   | j  d  s( t t j d |   n  t | j d d   d k  r_ t t j d |   n  | S(   Nt   nf_conntrack_s(   '%s' does not start with 'nf_conntrack_'R|   i   s   Module name '%s' too short(   R]   R   R   t   INVALID_MODULER+   t   replace(   R   R_   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   check_moduler  s    	c         C   s  | j    } | j   } t t | j   |   } | j   } | j   }	 | j   }
 | j   } | j	   } | j
   } | j   } | j   } | j   } | j   } | j   } g  } | d  k	 r | | k r | j d  q n  | s | r| j d  n  | r%| d d j |  } n  |  j |  |  j r`|  j d |  |  j d |  n  |  j d |  |  j d | rd	 n d
  |  j d d j |   |  j d d j |   |  j d d j t |	    |  j d d j g  |
 D] } d | d | d f ^ q  |  j d d j t |    |  j d | rVd	 n d
  |  j d d j g  | D]( \ } } } } d | | | | f ^ qt  |  j d d j g  | D] } d | d | d f ^ q  |  j d d j |   |  j d d j |   d  S(   Nt   defaultt   actives    (%s)s   , s     summary: s     description: s
     target: s     icmp-block-inversion: %sRV   RU   s     interfaces: t    s     sources: s     services: s	     ports: s   %s/%si    i   s     protocols: s     masquerade: %ss     forward-ports: s   
	s$   port=%s:proto=%s:toport=%s:toaddr=%ss     source-ports: s     icmp-blocks: s     rich rules: 
	(   t	   getTargett   getIcmpBlockInversiont   sortedt   sett   getInterfacest
   getSourcest   getServicest   getPortst   getProtocolst   getMasqueradet   getForwardPortst   getSourcePortst   getIcmpBlockst   getRichRulest   getDescriptiont   getShortR   R,   R   R   R
   (   R   RO   t   settingst   default_zonet   extra_interfacest   targett   icmp_block_inversiont
   interfacest   sourcest   servicest   portst	   protocolst
   masqueradet   forward_portst   source_portst   icmp_blockst   rulest   descriptiont   short_descriptiont
   attributesRk   Rl   Rp   Rq   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_zone_info|  sX    		 - 		7		-c      	   C   s  | j    } | j   } | j   } | j   } | j   } | j   } | j   }	 |  j |  |  j r |  j d |	  |  j d |  n  |  j d d j	 g  | D] }
 d |
 d |
 d f ^ q   |  j d d j	 |   |  j d	 d j	 g  | D] }
 d |
 d |
 d f ^ q   |  j d
 d j	 |   |  j d d j	 g  | j
   D] \ } } d | | f ^ q]  d  S(   Ns     summary: s     description: s	     ports: R   s   %s/%si    i   s     protocols: s     source-ports: s     modules: s     destination: s   %s:%s(   R   R   R   t
   getModulesR   t   getDestinationsR   R   R
   R   RC   (   R   t   serviceR   R   R   R   t   modulesR   t   destinationsR   Rk   t   kt   v(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_service_info  s*    	-		-		c         C   s   | j    } | j   } | j   } t |  d k rE d d g } n  |  j |  |  j r |  j d |  |  j d |  n  |  j d d j |   d  S(   Ni    Rs   Rt   s     summary: s     description: s     destination: R   (   R   R   R   R+   R   R
   R   (   R   t   icmptypeR   R   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_icmptype_info  s    	c   
      C   s   | j    } | j   } | j   } | j   } | j   } |  j |  |  j rw |  j d |  |  j d |  n  |  j d |  |  j d d j g  | j   D]( \ } }	 |	 r d | |	 f n | ^ q   |  j d d j |   d  S(   Ns     summary: s     description: s     type: s     options: R   s   %s=%ss     entries: (	   t   getTypet
   getOptionst
   getEntriesR   R   R   R
   R   RC   (
   R   t   ipsetR   t
   ipset_typet   optionst   entriesR   R   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_ipset_info  s    	=c   	   	   C   s   | j    } | j   } | j   } | j   } | j   } |  j |  |  j rw |  j d |  |  j d |  n  |  j d |  |  j d |  |  j d d j g  | D] } d | d | d	 f ^ q   d  S(
   Ns     summary: s     description: s
     family: s
     module: s	     ports: R   s   %s/%si    i   (   R   t	   getModulet	   getFamilyR   R   R   R
   R   (	   R   t   helperR   R   t   modulet   familyR   R   Rk   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   print_helper_info  s    	c         C   s*   | r |  j  d  n |  j  d d  d  S(   NRV   RU   i   (   R$   (   R   R_   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyRW     s    c         C   s   |  j  s   n  |  j |  t j t |   } | t j t j t j t j	 g k ri |  j
 d |  n |  j d | |  d  S(   Ns   Warning: %ss	   Error: %s(   R   R1   R   R)   R*   R   R4   R5   R6   R7   R!   R$   (   R   t   exception_messageRG   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   exception_handler  s    	c         C   s,   d | k r( d } |  j  | t j  n  d  S(   Nt   NotAuthorizedExceptions`   Authorization failed.
    Make sure polkit agent is running or run the application as superuser.(   R$   R   t   NOT_AUTHORIZED(   R   R   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyR1     s    c         C   s   t  |  _ d  S(   N(   t   FalseR   (   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyR0     s    c         C   s   t  |  _ d  S(   N(   R   R   (   R   (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyR8     s    c         C   s   g  } t    } t |  } xu | D]m } | s2 Pn  | j   } t |  d k  s" | d d k rf q" n  | | k r" | j |  | j |  q" q" W| j   | S(   Ni   i    t   #t   ;(   R   R   (   R   t   opent   stripR+   R,   RJ   t   close(   R   t   filenameR   t   entries_sett   ft   line(    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   get_ipset_entries_from_file  s    	"
N(-   t   __name__t
   __module__R   R   R   R   R   R   R   R   R   R   R!   R$   R%   R&   RK   RL   RN   RQ   RS   RT   RZ   R[   R\   R`   Rm   R{   R   R   R   R   R   R   R   R   R   R   R   RW   R   R1   R0   R8   R   (    (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyR    "   sT   					J2	2	
					
1									(   t   __doc__t   __all__R   t   firewallR   t   firewall.errorsR   t   dbus.exceptionsR   t   firewall.functionsR   R   R   R   R   t   objectR    (    (    (    s4   /usr/lib/python2.7/site-packages/firewall/command.pyt   <module>   s   	(