3 YÉjã@s dZddddddddgZd d lZd d lmZyejd d ƒWnek rTdZYn8Xyd dlmZdZWn e eej fk rŠdZYnXd a d dl m Z d dlmZd dlmZd d lZdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d„Zd"d„Zd#d„Zd 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ÚNMz1.0F)r T)Úerrors)Ú FirewallError)ÚlogcCststtjdƒ‚dS)zNCheck function to raise a MISSING_IMPORT error if the import of NM failed zgi.repository.NM = 1.0N)Ú _nm_importedr r ZMISSING_IMPORT©rrú/usr/lib/python3.6/fw_nm.pyr0scCstS)znReturns true if NM has been properly imported @return True if import was successful, False otherwirse )rrrrrr6scCststjjdƒatS)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 ZClientÚnewrrrrÚ nm_get_client<s rc CsŒtƒtƒj|ƒ}|dkrdS|jƒ}|dkr2dSy |jƒtjjtjjB@rPdSWn t k rr|j ƒrndSYnX|j ƒ}|dkrˆ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Ú) rrÚget_connection_by_uuidÚget_setting_connectionÚ get_flagsr ÚSettingsConnectionFlagsÚ NM_GENERATEDÚ NM_VOLATILEÚAttributeErrorÚ get_unsavedZget_zone)Ú connectionÚconÚ setting_conÚzonerrrrEs$    cCsVtƒtƒj|ƒ}|dkrdS|jƒ}|dkr2dS|dkr>d}|jd|ƒ|jddƒS)zSet the zone for a connection @param zone name @param connection name @return True if zone was set, else False NFrr!T)rrrrZ set_propertyZcommit_changes)r!rrr rrrrcs  c Cs~|jƒ|jƒtƒtƒjƒ}xX|D]P}|jƒr4q&|jƒ}|jƒ}|jƒ}|||<x |D]}|jƒ}|rZ|||<qZWq&WdS)znGet active connections from NM @param connections return dict @param connections_name return dict N) ÚclearrrÚget_active_connectionsÚget_vpnZget_idÚget_uuidÚ get_devicesÚ get_ip_iface) Z connectionsZconnections_nameZactive_connectionsÚ active_conÚnameZuuidZdevicesÚdevÚip_ifacerrrrxs   c Csštƒg}xŠtƒjƒD]|}|jƒr$qy&|jƒ}|jƒtjjtjj B@rHwWnt k rh|j ƒrdwYnXx&|j ƒD]}|j ƒ}|rt|j|ƒqtWqW|S)zGGet active interfaces from NM @returns list of interface names )rrr#r$Úget_connectionrr rrrrrr&r'Úappend)Zactive_interfacesr(rr*r+rrrÚnm_get_interfaces”s$  r.cCs6g}x,tƒD]"}t|ƒ}|t|ƒkr |j|ƒq W|S)N)r.rrr-)r!Z interfacesÚ interfaceZconnrrrÚnm_get_interfaces_in_zone´s   r0cCs<tƒx0tƒjƒD]"}|jƒ}|dkr(q||kr|SqWdS)zzGet device from NM which has the given IP interface @param interface name @returns NM.Device instance or None N)rrr&r')r/Údevicer+rrrÚnm_get_device_by_ip_iface½sr2c Csxtƒt|ƒ}|dkrdS|jƒ}|dkr.dSy |jƒ}|jƒtjj@rLdSWn tk rn|j ƒrjdSYnX|j ƒS)z‰Get connection from NM that is using the interface @param interface name @returns connection that is using interface or None N) rr2Zget_active_connectionr,rr rrrrr%)r/r1r(rrrrrÍs c CsRtsdSy&tjƒ}|jtjtjƒ}|j}~~|Stk rLt j dƒYnXdS)Nz(Failed to get bus name of NetworkManager) rÚdbusZ SystemBusZ get_objectr ÚDBUS_INTERFACEZ DBUS_PATHZbus_nameÚ ExceptionrZdebug2)ZbusÚobjr)rrrrèscCstsdStjS)Nr)rr r4rrrrrõs)Ú__doc__Ú__all__ZgiZ gi.repositoryr Zrequire_versionÚ ValueErrorrr Ú ImportErrorÚErrorrZfirewallr Zfirewall.errorsr Zfirewall.core.loggerrr3rrrrrrr.r0r2rrrrrrrÚs@