
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
³Y„jJ  ã               @   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d „ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zddd„Zdd„ Zddd	„Zdd
„ ZdS )Úcommand_of_pidÚpid_of_senderÚuid_of_senderÚuser_of_uidÚcontext_of_senderÚcommand_of_senderÚuser_of_senderÚdbus_to_pythonÚdbus_signatureÚ%dbus_introspection_prepare_propertiesÚ!dbus_introspection_add_propertiesé    N)Úminidom)ÚlogÚ3c             C   sP   y6t d|  dƒ�}|jƒ d jddƒjƒ }W dQ R X W n tk
rJ   dS X |S )z  Get command for pid from /proc z/proc/%d/cmdlineÚrr   ú ú N)ÚopenÚ	readlinesÚreplaceÚstripÚ	Exception)ÚpidÚfÚcmd© r   ú /usr/lib/python3.6/dbus_utils.pyr   %   s    &c             C   sD   | j ddƒ}tj|dƒ}yt|j|ƒƒ}W n tk
r>   dS X |S )zW Get pid from sender string using 
    org.freedesktop.DBus.GetConnectionUnixProcessID zorg.freedesktop.DBusz/org/freedesktop/DBusN)Ú
get_objectÚdbusÚ	InterfaceÚintZGetConnectionUnixProcessIDÚ
ValueError)ÚbusÚsenderÚdbus_objÚ
dbus_ifacer   r   r   r   r   .   s    c             C   sD   | j ddƒ}tj|dƒ}yt|j|ƒƒ}W n tk
r>   dS X |S )zV Get user id from sender string using 
    org.freedesktop.DBus.GetConnectionUnixUser zorg.freedesktop.DBusz/org/freedesktop/DBusN)r   r   r   r    ZGetConnectionUnixUserr!   )r"   r#   r$   r%   Úuidr   r   r   r   ;   s    c             C   s,   yt j| ƒ}W n tk
r"   dS X |d S )z Get user for uid from pwd Nr   )ÚpwdÚgetpwuidr   )r&   Zpwsr   r   r   r   H   s
    c             C   sP   | j ddƒ}tj|dƒ}y|j|ƒ}W n tk
r:   dS X djttt|ƒƒƒS )zl Get SELinux context from sender string using 
    org.freedesktop.DBus.GetConnectionSELinuxSecurityContext zorg.freedesktop.DBusz/org/freedesktop/DBusNÚ )	r   r   r   Z#GetConnectionSELinuxSecurityContextr   ÚjoinÚmapÚchrr   )r"   r#   r$   r%   Úcontextr   r   r   r   Q   s    c             C   s   t t| |ƒƒS )z  Return command of D-Bus sender )r   r   )r"   r#   r   r   r   r   _   s    c             C   s   t t| |ƒƒS )N)r   r   )r"   r#   r   r   r   r   d   s    c             C   sš  | d kr| }�n¾t | tjƒr(t| ƒ}�n¦t | tjƒrNtrB| jdƒnt| ƒ}�n€trjt | tjƒrjt| ƒ}�ndt | tj	ƒr‚t| ƒ}�nLt | tj
ƒsÖt | tjƒsÖt | tjƒsÖt | tjƒsÖt | tjƒsÖt | tjƒsÖt | tjƒràt| ƒ}nît | tjƒröt| ƒ}nØt | tjƒ�rdd„ | D ƒ}nºt | tjƒ�r6tdd„ | D ƒƒ}n˜t | tjƒ�rXdd„ | jƒ D ƒ}nvt | tƒ�s¸t | tƒ�s¸t | tƒ�s¸t | tƒ�s¸t | tƒ�s¸t | tƒ�s¸t | tƒ�s¸t | tƒ�r¾| }ntdt| ƒ ƒ‚|d k	�r–|tk�rðt |tƒ �s€|tk�rt |tƒ �s€|tk�r t |tƒ �s€|tk�r8t |tƒ �s€|tk�rPt |tƒ �s€|tk�rht |tƒ �s€|tk�r–t |tƒ �r–td|t|ƒ|f ƒ‚|S )	Nzutf-8c             S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0Úxr   r   r   ú
<listcomp>}   s    z"dbus_to_python.<locals>.<listcomp>c             S   s   g | ]}t |ƒ‘qS r   )r   )r.   r/   r   r   r   r0      s    c             S   s   i | ]\}}t |ƒt |ƒ“qS r   )r   )r.   ÚkÚvr   r   r   ú
<dictcomp>�   s    z"dbus_to_python.<locals>.<dictcomp>zUnhandled %sz%s is %s, expected %s)Ú
isinstancer   ÚBooleanÚboolÚStringÚPY2ÚencodeÚstrÚ
UTF8StringÚ
ObjectPathÚByteÚInt16ÚInt32ÚInt64ÚUInt16ÚUInt32ÚUInt64r    ÚDoubleÚfloatÚArrayÚStructÚtupleÚ
DictionaryÚitemsÚbytesÚlistÚdictÚ	TypeErrorÚreprÚtype)ÚobjZexpected_typeZ
python_objr   r   r   r   g   sV    


c             C   s>  t | tjƒrdS t | tjƒr dS t | tjƒr0dS t | tjƒr@dS t | tjƒrPdS t | tjƒr`dS t | tjƒrpdS t | tj	ƒr€dS t | tj
ƒr�d	S t | tjƒr d
S t | tjƒr°dS t | tjƒ� rât| jƒdkrÖd| j S d| j S nXt | tjƒ� rúd| j S t | tjƒ�rd| j S t�r*t | tjƒ�r*dS tdt| ƒ ƒ‚d S )NÚbÚsÚoÚyÚnÚir/   ÚqÚuÚtÚdé   za(%s)za%sz(%s)za{%s}zUnhandled %s)r4   r   r5   r7   r<   r=   r>   r?   r@   rA   rB   rC   rD   rF   ÚlenZ	signaturerG   rI   r8   r;   rN   rO   )rQ   r   r   r   r	   —   sB    


c             C   s¸   |d kri }t | dƒs"t| di ƒ t| dƒ}i ||< y| j|ƒ}W n tk
rZ   i }Y nX xV|jƒ D ]J\}}dt|ƒi|| |< ||kr || || | d< qfd|| | d< qfW d S )NÚ_fw_dbus_propertiesrP   ÚaccessÚread)ÚhasattrÚsetattrÚgetattrZGetAllr   rJ   r	   )rQ   Ú	interfacer_   ÚdipZ_dictÚkeyÚvaluer   r   r   r
   ¼   s    


c       
      C   sÜ   t j|ƒ}t| dƒrºx¤|jdƒD ]–}|jdƒr |jdƒ|kr i }t| dƒrTt| dƒ}||kr xX|| jƒ D ]H\}}|jdƒ}|j	d|ƒ |j	d|d ƒ |j	d|d ƒ |j
|ƒ qjW q W tj|jƒ ƒ |jƒ }	|jƒ  |	S )Nr^   rd   ÚnameÚpropertyrP   r_   )r   ZparseStringra   ZgetElementsByTagNameZhasAttributeZgetAttributerc   rJ   ZcreateElementZsetAttributeZappendChildr   Zdebug10ZtoxmlÚunlink)
rQ   Údatard   ÚdocZnodere   rf   rg   ZpropZnew_datar   r   r   r   Ð   s&    





)N)N)Ú__all__r   r'   ÚsysZxml.domr   Zfirewall.core.loggerr   Úversionr8   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   Ú<module>   s*   
		
0%
