<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
3
YÉj  ã               @   s   d Z ddddddddgZd	d
lZd	dlmZ yejddƒ W n ek
rT   dZY n8X yd	dlmZ dZW n  e	eej
fk
rŠ   dZY nX d
ad	dlmZ d	dlmZ d	dlmZ d	d
lZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d„ Zd"d„ Zd#d„ Zd
S )$z(Functions for NetworkManager interactionÚcheck_nm_importedÚnm_is_importedÚnm_get_zone_of_connectionÚnm_set_zone_of_connectionÚnm_get_connectionsÚnm_get_connection_of_interfaceÚnm_get_bus_nameÚnm_get_dbus_interfaceé    N)ÚGLibÚNMz1.0F)r   T)Úerrors)ÚFirewallError)Úlogc               C   s   t sttjdƒ‚dS )zNCheck function to raise a MISSING_IMPORT error if the import of NM failed
    zgi.repository.NM = 1.0N)Ú_nm_importedr   r   ZMISSING_IMPORT© r   r   ú/usr/lib/python3.6/fw_nm.pyr   0   s    c               C   s   t S )znReturns true if NM has been properly imported
    @return True if import was successful, False otherwirse
    )r   r   r   r   r   r   6   s    c               C   s   t stjjdƒa t S )zŒReturns the NM client object or None if the import of NM failed
    @return NM.Client instance if import was successful, None otherwise
    N)Ú
_nm_clientr   ZClientÚnewr   r   r   r   Únm_get_client<   s    r   c             C   sŒ   t ƒ  tƒ j| ƒ}|dkrdS |jƒ }|dkr2dS y |jƒ tjjtjjB @ rPdS W n  t	k
rr   |j
ƒ rndS Y nX |jƒ }|dkrˆd}|S )z¤Get zone of connection from NM
    @param connection name
    @return zone string setting of connection, empty string if not set, None if connection is unknown
    NÚ )r   r   Úget_connection_by_uuidÚget_setting_connectionÚ	get_flagsr   ÚSettingsConnectionFlagsÚNM_GENERATEDÚNM_VOLATILEÚAttributeErrorÚget_unsavedZget_zone)Ú
connectionÚconÚsetting_conÚzoner   r   r   r   E   s$    
c             C   sV   t ƒ  tƒ j|ƒ}|dkrdS |jƒ }|dkr2dS | dkr>d} |jd| ƒ |jddƒS )zSet the zone for a connection
    @param zone name
    @param connection name
    @return True if zone was set, else False
    NFr   r!   T)r   r   r   r   Zset_propertyZcommit_changes)r!   r   r   r    r   r   r   r   c   s    c       	      C   s~   | j ƒ  |j ƒ  tƒ  tƒ jƒ }xX|D ]P}|jƒ r4q&|jƒ }|jƒ }|jƒ }|||< x |D ]}|jƒ }|rZ|| |< qZW q&W dS )znGet active connections from NM
    @param connections return dict
    @param connections_name return dict
    N)	Úclearr   r   Úget_active_connectionsÚget_vpnZget_idÚget_uuidÚget_devicesÚget_ip_iface)	ZconnectionsZconnections_nameZactive_connectionsÚ
active_conÚnameZuuidZdevicesÚdevÚip_ifacer   r   r   r   x   s    


c              C   sš   t ƒ  g } xŠtƒ jƒ D ]|}|jƒ r$qy&|jƒ }|jƒ tjjtjj	B @ rHwW n t
k
rh   |jƒ rdwY nX x&|jƒ D ]}|jƒ }|rt| j|ƒ qtW qW | S )zGGet active interfaces from NM
    @returns list of interface names
    )r   r   r#   r$   Úget_connectionr   r   r   r   r   r   r   r&   r'   Úappend)Zactive_interfacesr(   r   r*   r+   r   r   r   Únm_get_interfaces”   s$    r.   c             C   s6   g }x,t ƒ D ]"}t|ƒ}| t|ƒkr|j|ƒ qW |S )N)r.   r   r   r-   )r!   Z
interfacesÚ	interfaceZconnr   r   r   Únm_get_interfaces_in_zone´   s    r0   c             C   s<   t ƒ  x0tƒ jƒ D ]"}|jƒ }|dkr(q|| kr|S qW dS )zzGet device from NM which has the given IP interface
    @param interface name
    @returns NM.Device instance or None
    N)r   r   r&   r'   )r/   Údevicer+   r   r   r   Únm_get_device_by_ip_iface½   s    r2   c             C   sx   t ƒ  t| ƒ}|dkrdS |jƒ }|dkr.dS y |jƒ }|jƒ tjj@ rLdS W n  tk
rn   |j	ƒ rjdS Y nX |j
ƒ S )z‰Get connection from NM that is using the interface
    @param interface name
    @returns connection that is using interface or None
    N)r   r2   Zget_active_connectionr,   r   r   r   r   r   r   r%   )r/   r1   r(   r   r   r   r   r   Í   s    
c              C   sR   t sd S y&tjƒ } | jtjtjƒ}|j}~~ |S  tk
rL   t	j
dƒ Y nX d S )Nz(Failed to get bus name of NetworkManager)r   ÚdbusZ	SystemBusZ
get_objectr   ÚDBUS_INTERFACEZ	DBUS_PATHZbus_nameÚ	Exceptionr   Zdebug2)ZbusÚobjr)   r   r   r   r   è   s    c               C   s   t sdS tjS )Nr   )r   r   r4   r   r   r   r   r   õ   s    )Ú__doc__Ú__all__ZgiZgi.repositoryr
   Zrequire_versionÚ
ValueErrorr   r   ÚImportErrorÚErrorr   Zfirewallr   Zfirewall.errorsr   Zfirewall.core.loggerr   r3   r   r   r   r   r   r   r.   r0   r2   r   r   r   r   r   r   r   Ú<module>   s@   

	 	