ó
 c‰`c           @   sç   d  d d d d d d d d d	 d
 g Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z e j d k  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d d „ Z d „  Z d S(   t   command_of_pidt   pid_of_sendert   uid_of_sendert   user_of_uidt   context_of_sendert   command_of_sendert   user_of_sendert   dbus_to_pythont   dbus_signaturet%   dbus_introspection_prepare_propertiest!   dbus_introspection_add_propertiesiÿÿÿÿN(   t   minidom(   t   logt   3c         C   s[   yB t  d |  d ƒ ) } | j ƒ  d j d d ƒ j ƒ  } Wd QXWn t k
 rV d SX| S(   s     Get command for pid from /proc s   /proc/%d/cmdlinet   ri    t    t    N(   t   opent	   readlinest   replacet   stript	   Exceptiont   None(   t   pidt   ft   cmd(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR    %   s    ,c         C   sV   |  j  d d ƒ } t j | d ƒ } y t | j | ƒ ƒ } Wn t k
 rQ d SX| S(   sW    Get pid from sender string using 
    org.freedesktop.DBus.GetConnectionUnixProcessID s   org.freedesktop.DBuss   /org/freedesktop/DBusN(   t
   get_objectt   dbust	   Interfacet   intt   GetConnectionUnixProcessIDt
   ValueErrorR   (   t   bust   sendert   dbus_objt
   dbus_ifaceR   (    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   .   s    c         C   sV   |  j  d d ƒ } t j | d ƒ } y t | j | ƒ ƒ } Wn t k
 rQ d SX| S(   sV    Get user id from sender string using 
    org.freedesktop.DBus.GetConnectionUnixUser s   org.freedesktop.DBuss   /org/freedesktop/DBusN(   R   R   R   R   t   GetConnectionUnixUserR   R   (   R    R!   R"   R#   t   uid(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   ;   s    c         C   s0   y t  j |  ƒ } Wn t k
 r' d SX| d S(   s    Get user for uid from pwd i    N(   t   pwdt   getpwuidR   R   (   R%   t   pws(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   H   s
    c         C   sh   |  j  d d ƒ } t j | d ƒ } y | j | ƒ } Wn t k
 rK d SXd j t t t	 | ƒ ƒ ƒ S(   sl    Get SELinux context from sender string using 
    org.freedesktop.DBus.GetConnectionSELinuxSecurityContext s   org.freedesktop.DBuss   /org/freedesktop/DBust    N(
   R   R   R   t#   GetConnectionSELinuxSecurityContextR   R   t   joint   mapt   chrR   (   R    R!   R"   R#   t   context(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   Q   s    c         C   s   t  t |  | ƒ ƒ S(   s     Return command of D-Bus sender (   R    R   (   R    R!   (    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   _   s    c         C   s   t  t |  | ƒ ƒ S(   N(   R   R   (   R    R!   (    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   d   s    c          C   s‹  |  d  k r |  } n}t |  t j ƒ r6 t |  ƒ } n\t |  t j ƒ rl t r] |  j d ƒ n	 t |  ƒ } n&t r“ t |  t j	 ƒ r“ t |  ƒ } nÿt |  t j
 ƒ r´ t |  ƒ } nÞt |  t j ƒ s2t |  t j ƒ s2t |  t j ƒ s2t |  t j ƒ s2t |  t j ƒ s2t |  t j ƒ s2t |  t j ƒ rAt |  ƒ } nQt |  t j ƒ rbt |  ƒ } n0t |  t j ƒ r–g  |  D] } t | ƒ ^ q{} nü t |  t j ƒ rÐt g  |  D] } t | ƒ ^ q²ƒ } nÂ t |  t j ƒ rûd „  |  j ƒ  Dƒ } n— t |  t ƒ sst |  t ƒ sst |  t ƒ sst |  t ƒ sst |  t ƒ sst |  t ƒ sst |  t ƒ sst |  t ƒ r||  } n t d t |  ƒ ƒ ‚ | d  k	 r‡| t k rºt | t ƒ sb| t k rÖt | t ƒ sb| t k ròt | t ƒ sb| t k rt | t ƒ sb| t k r*t | t ƒ sb| t k rFt | t ƒ sb| t k r‡t | t ƒ r‡t d | t  | ƒ | f ƒ ‚ q‡n  | S(   Ns   utf-8c         S   s+   i  |  ]! \ } } t  | ƒ t  | ƒ “ q S(    (   R   (   t   .0t   kt   v(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pys
   <dictcomp>   s   	 s   Unhandled %ss   %s is %s, expected %s(!   R   t
   isinstanceR   t   Booleant   boolt   Stringt   PY2t   encodet   strt
   UTF8Stringt
   ObjectPatht   Bytet   Int16t   Int32t   Int64t   UInt16t   UInt32t   UInt64R   t   Doublet   floatt   ArrayR   t   Structt   tuplet
   Dictionaryt   itemst   bytest   listt   dictt	   TypeErrort   reprt   type(   t   objt   expected_typet
   python_objt   x(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   g   sV    	$"(	%c         C   s¢  t  |  t j ƒ r d St  |  t j ƒ r, d St  |  t j ƒ rB d St  |  t j ƒ rX d St  |  t j ƒ rn d St  |  t j ƒ r„ d St  |  t j ƒ rš d St  |  t j	 ƒ r° d St  |  t j
 ƒ rÆ d	 St  |  t j ƒ rÜ d
 St  |  t j ƒ rò d St  |  t j ƒ r2t |  j ƒ d k r$d |  j Sd |  j Snl t  |  t j ƒ rOd |  j St  |  t j ƒ rld |  j St rˆt  |  t j ƒ rˆd St d t |  ƒ ƒ ‚ d  S(   Nt   bt   st   ot   yt   nt   iRR   t   qt   ut   tt   di   s   a(%s)s   a%ss   (%s)s   a{%s}s   Unhandled %s(   R2   R   R3   R5   R:   R;   R<   R=   R>   R?   R@   RA   RB   RD   t   lent	   signatureRE   RG   R6   R9   RL   RM   (   RO   (    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR   —   sB    c         C   sð   | d  k r i  } n  t |  d ƒ s7 t |  d i  ƒ n  t |  d ƒ } i  | | <y |  j | ƒ } Wn t k
 r| i  } n Xxl | j ƒ  D]^ \ } } i t | ƒ d 6| | | <| | k rÖ | | | | | d <qŠ d | | | d <qŠ Wd  S(   Nt   _fw_dbus_propertiesRN   t   accesst   read(   R   t   hasattrt   setattrt   getattrt   GetAllR   RH   R   (   RO   t	   interfaceR`   t   dipt   _dictt   keyt   value(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR	   ¼   s    	

c   
      C   s:  t  j | ƒ } t |  d ƒ rxì | j d ƒ D]Ø } | j d ƒ r. | j d ƒ | k r. i  } t |  d ƒ r t |  d ƒ } n  | | k rxu | | j ƒ  D]` \ } } | j d ƒ } | j	 d | ƒ | j	 d | d ƒ | j	 d | d ƒ | j
 | ƒ qœ Wqq. q. Wn  t j | j ƒ  ƒ | j ƒ  }	 | j ƒ  |	 S(   NR_   Rf   t   namet   propertyRN   R`   (   R   t   parseStringRb   t   getElementsByTagNamet   hasAttributet   getAttributeRd   RH   t   createElementt   setAttributet   appendChildR   t   debug10t   toxmlt   unlink(
   RO   t   dataRf   t   doct   nodeRg   Ri   Rj   t   propt   new_data(    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyR
   Ð   s&    
(   t   __all__R   R&   t   syst   xml.domR   t   firewall.core.loggerR   t   versionR6   R    R   R   R   R   R   R   R   R   R   R	   R
   (    (    (    s7   /usr/lib/python2.7/site-packages/firewall/dbus_utils.pyt   <module>   s*   											0	%