bSg@sdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z ddl m Z m Z ddlZGdddZdS)z&backend.py: interface for ufw backendsN)errorwarndebug)UFWErrorUFWRulec@soeZdZdZdddZddZddZd d Zd d d dZddZ ddZ ddZ ddZ ddZ ddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:Zd;d<ZdS)= UFWBackendzInterface for backendsNc Csd|_||_||_g|_g|_itjjtj j dd6tjjtj j dd6tjjtj j dd6|_ |dkr|j j |nidd6d d 6d d 6d d6dd6|_ tj j|_|j|j|jtjj|j d|_tjjtj jd|_tjjtj jd|_tjjtj jd|_tjjtj jd|_ytjj|j|_Wn*tk rtd}t|YnXd|_ dS)Nz default/ufwdefaultsz ufw/ufw.confconfzufw/applications.dappsroffdlowZmediumi,Zhighifulliptablesziptables-restore ip6tableszip6tables-restorez#Couldn't determine iptables version)!rnamedryrunrulesrules6ospathjoinufwZcommonZ config_dirfilesupdate loglevels do_checks _do_checks _get_defaultsZ _read_rules applicationsZ get_profilesprofilesZ iptables_dirrZiptables_restorerZip6tables_restoreutilZget_iptables_versionZiptables_versionOSError_rcaps)selfrrZ extra_fileserr_msgr(-/usr/lib/python3/dist-packages/ufw/backend.py__init__sF                 zUFWBackend.__init__cCs|jdkrdSi|_i|jd| D]6} | j | j r|j | q|j | qW|}d}q/|j rL|j |q/|j |q/W|r||_||_|t d|7}y|j d|j dWqtk rt d} t| YqXn||fS) zUpdate rule for profile in place. Returns result string and bool on whether or not the profile is used in the current ruleset. r7Fr~rrTzRules updated for profile '%s'z!Couldn't update application rules)rrrr get_app_tuplerrrrZ normalizev6rPr$Z _write_rulesrZr) r&rcZ updated_rulesZupdated_rules6Z last_tupler@Zupdated_profilertuplrZ new_app_rulesZnew_rr'r(r(r)update_app_rulesN               zUFWBackend.update_app_rulecCs||jkr|Sd}d}xHt|jjD]1}|j|jkr5|}|d7}q5q5Wd||f}t||dkr|S|dkrtd|}ntd|}t|dS)z2Find the application profile name for profile_namer7rrfz'%d' matches for '%s'z>Found multiple matches for '%s'. Please use exact profile namez&Could not find a profile matching '%s'N)r!rQrrkrr$r)r&Z profile_namertZmatchesnZ debug_msgr'r(r(r)find_application_names$      z UFWBackend.find_application_namec Cs|r'|t|jkr'tn| rO|t|jkrOtn|dkrgtng}|r|j}n |j}i}d}xt|D]r\}}||krPnd}|jdks|jdkr|j}||kr|d7}qd||  <              r)rr_rrLrXrTZufw.utilrrrrZ ufw.commonrrZufw.applicationsrr(r(r(r)s