3 YÉjÁ^ã@sfdZdgZddlZddlmZddlmZddlmZddl m Z m Z m Z m Z mZGdd„deƒZdS) z„Zd?d@„Z dgdAfdBdC„Z!dgfdDdE„Z"dgfdFdG„Z#dHdI„Z$dJdK„Z%dLdM„Z&dNdO„Z'dPdQ„Z(dRdS„Z)dTdU„Z*dVdW„Z+dXdY„Z,dZd[„Z-dS)nrFcCs||_||_d|_d|_dS)NT)ÚquietÚverboseÚ'_FirewallCommand__use_exception_handlerÚfw)Úselfr r ©rú/usr/lib/python3.6/command.pyÚ__init__#szFirewallCommand.__init__cCs ||_dS)N)r)rrrrrÚset_fw)szFirewallCommand.set_fwcCs ||_dS)N)r )rÚflagrrrÚ set_quiet,szFirewallCommand.set_quietcCs|jS)N)r )rrrrÚ get_quiet/szFirewallCommand.get_quietcCs ||_dS)N)r )rrrrrÚ set_verbose2szFirewallCommand.set_verbosecCs|jS)N)r )rrrrÚ get_verbose5szFirewallCommand.get_verboseNcCs$|dk r |j r tjj|dƒdS)NÚ )r ÚsysÚstdoutÚwrite)rÚmsgrrrÚ print_msg8szFirewallCommand.print_msgcCs$|dk r |j r tjj|dƒdS)Nr)r rÚstderrr)rrrrrÚprint_error_msg<szFirewallCommand.print_error_msgcCs,d}d}tjjƒr|||}|j|ƒdS)Nzz)rrÚisattyr )rrZFAILZENDrrrÚ print_warning@s   zFirewallCommand.print_warningrcCs,|dkr|j|ƒn |j|ƒtj|ƒdS)Né)r"rrÚexit)rrZ exit_coderrrÚprint_and_exitGs  zFirewallCommand.print_and_exitcCs|j|dƒdS)Né)r%)rrrrrÚfailRszFirewallCommand.failcCs"|dk r|jrtjj|dƒdS)Nr)r rrr)rrrrrÚprint_if_verboseUsz FirewallCommand.print_if_verbosec CsŠ|jdk r|jjƒg} d} g} x¤|D]œ} |dk r¸y || ƒ} Wnxtk r¶}z\tjt|ƒƒ}t|ƒdkrz|jd|ƒn|jd||ƒ|| krœ| j |ƒ| d7} w&WYdd}~XnX| j | ƒq&W�xb| D�]X} g}|dk rè||7}t | t ƒ oþt | t ƒ �r|j | ƒn|| 7}|dk �r(||7}|j ƒy ||ŽWnâttfk �r}zÀt |tƒ�rx|j|jƒƒ|jƒ}nt|ƒ}tj|ƒ}|tjtjtjtjgk�r¨d}t|ƒdk�rÆ|jd|ƒn,|dk�râ|jd|ƒdS|jd||ƒ|| k�r| j |ƒ| d7} WYdd}~XnX|jƒqÎW| �s†t|ƒ| k�sJd| k�rNdSt| ƒdk�rltj| dƒnt| ƒdk�r†tjtjƒdS)Nrr#z Warning: %sz Error: %s)rZ authorizeAllÚ ExceptionrÚget_codeÚstrÚlenr"r%ÚappendÚ isinstanceÚlistÚtupleÚdeactivate_exception_handlerrÚfail_if_not_authorizedÚ get_dbus_nameÚget_dbus_messagerÚALREADY_ENABLEDÚ NOT_ENABLEDÚZONE_ALREADY_SETÚ ALREADY_SETÚactivate_exception_handlerrr$Z UNKNOWN_ERROR)rZcmd_typeÚoptionÚ action_methodÚ query_methodÚ parse_methodÚmessageÚ start_argsÚend_argsÚno_exitÚitemsZ_errorsZ _error_codesÚitemrÚcodeÚ call_itemrrrZ__cmd_sequenceYsr                 zFirewallCommand.__cmd_sequencec Cs|jd||||||d�dS)NÚadd)rA)Ú_FirewallCommand__cmd_sequence)rr:r;r<r=r>rArrrÚ add_sequence¤s zFirewallCommand.add_sequencec Cs |jd||||||g|d�dS)NrF)r?rA)rG)rÚxr:r;r<r=r>rArrrÚx_add_sequence©s zFirewallCommand.x_add_sequencec Cs$|jd||||||g|g|d� dS)NrF)r?r@rA)rG) rÚzoner:r;r<r=r>ZtimeoutrArrrÚzone_add_timeout_sequence¯s z)FirewallCommand.zone_add_timeout_sequencec Cs|jd||||||d�dS)NÚremove)rA)rG)rr:r;r<r=r>rArrrÚremove_sequence¶s zFirewallCommand.remove_sequencec Cs |jd||||||g|d�dS)NrM)r?rA)rG)rrIr:r;r<r=r>rArrrÚx_remove_sequence»s z!FirewallCommand.x_remove_sequencec Csg}xŠ|D]‚}|dk r‚y ||ƒ}Wn^tk r€} zBt|ƒdkrR|jd| ƒw ntjt| ƒƒ} |jd| | ƒWYdd} ~ XnX|j|ƒq W�xv|D�]l}g} |dk r²| |7} t|t ƒ rÖt|t ƒ rÖ| j|ƒn| |7} |j ƒy || Ž} WnÖt k �rj} zZ|j | jƒƒtj| jƒƒ} t|ƒdk�rF|jd| jƒƒw˜n|jd| jƒ| ƒWYdd} ~ Xn`tk �rÈ} zBtjt| ƒƒ} t|ƒdk�r¨|jd| ƒn|jd| | ƒWYdd} ~ XnX|jƒt|ƒdk�rü|jd||d| fƒq˜|j| ƒq˜W|�stjdƒdS) Nr#z Warning: %sz Error: %sz%s: %sÚnoÚyesr)rPrQ)r)r,r"rr*r+r%r-r.r/r0r1rr2r3r4r9rÚprint_query_resultrr$) rr:r<r=r>r?rArBrCrrDrEÚresrrrZ__query_sequenceÂsR   "  "z FirewallCommand.__query_sequencecCs|j|||||d�dS)N)rA)Ú _FirewallCommand__query_sequence)rr:r<r=r>rArrrÚquery_sequenceôs zFirewallCommand.query_sequencecCs|j|||||g|d�dS)N)r?rA)rT)rrIr:r<r=r>rArrrÚx_query_sequenceùs z FirewallCommand.x_query_sequencecCsJt|ƒ rFt|ƒ rFt|ƒ rF|jdƒo2t|ƒdk rFttjd|ƒ‚|S)Nzipset:éz8'%s' is no valid IPv4, IPv6 or MAC address, nor an ipset)rrrÚ startswithr,rrÚ INVALID_ADDR)rÚvaluerrrÚ parse_sourceÿs   zFirewallCommand.parse_sourceú/c Csly|j|ƒ\}}Wn$tk r6ttjd|ƒ‚YnXt|ƒsLttj|ƒ‚|dkrdttjd|ƒ‚||fS)NzTbad port (most likely missing protocol), correct syntax is portid[-portid]%sprotocolÚtcpÚudpÚsctpÚdccpz''%s' not in {'tcp'|'udp'|'sctp'|'dccp'})r]r^r_r`)ÚsplitÚ ValueErrorrrÚ INVALID_PORTr ÚINVALID_PROTOCOL)rrZZ separatorÚportÚprotorrrÚ parse_ports zFirewallCommand.parse_portc Cs¸d}d}d}d}d}xØd||d…krì||d…jddƒd}|t|ƒd7}d||d…krx||d…jddƒd} n ||d…} |t| ƒd7}|dkr¢| }q|dkr°| }q|dkr¾| }q|dkrÌ| }q|d krÚ|rÚqttjd |ƒ‚qW|�sttjd ƒ‚|�sttjd ƒ‚|�p|�s*ttjd ƒ‚t|ƒ�s@ttj|ƒ‚|dk�rZttjd|ƒ‚|�rxt|ƒ �rxttj|ƒ‚|�r¬td|ƒ �r¬|�s td|ƒ �r¬ttj |ƒ‚||||fS)Nrú=r#ú:rerfÚtoportÚtoaddrÚifzinvalid forward port arg '%s'z missing portzmissing protocolzmissing destinationr]r^r_r`z''%s' not in {'tcp'|'udp'|'sctp'|'dccp'}Úipv4Úipv6)r]r^r_r`) rar,rrZINVALID_FORWARDr rcrdr rY) rrZÚcompatreZprotocolrjrkÚiÚoptÚvalrrrÚparse_forward_portsT           z"FirewallCommand.parse_forward_portcCsF|jdƒ}t|ƒdkr"|ddfSt|ƒdkr2|Sttjd|ƒ‚dS)Nrhr#rÚr&zinvalid ipset option '%s')rar,rrZINVALID_OPTION)rrZÚargsrrrÚparse_ipset_optionHs    z"FirewallCommand.parse_ipset_optioncCs.ddg}||kr*ttjd|dj|ƒfƒ‚|S)Nrmrnz'invalid argument: %s (choose from '%s')z', ')rrÚ INVALID_IPVÚjoin)rrZÚipvsrrrÚcheck_destination_ipvRs z%FirewallCommand.check_destination_ipvc CsDy|jddƒ\}}Wn tk r4ttjdƒ‚YnX|j|ƒ|fS)Nrir#z(destination syntax is ipv:address[/mask])rarbrrZINVALID_DESTINATIONrz)rrZZipvZ destinationrrrÚparse_service_destinationZs  z)FirewallCommand.parse_service_destinationcCs0dddg}||kr,ttjd|dj|ƒfƒ‚|S)NrmrnZebz'invalid argument: %s (choose from '%s')z', ')rrrwrx)rrZryrrrÚ check_ipvbs  zFirewallCommand.check_ipvcCs0dddg}||kr,ttjd|dj|ƒfƒ‚|S)Nrtrmrnz'invalid argument: %s (choose from '%s')z', ')rrrwrx)rrZryrrrÚcheck_helper_familyjs  z#FirewallCommand.check_helper_familycCsB|jdƒsttjd|ƒ‚t|jddƒƒdkr>ttjd|ƒ‚|S)NZ nf_conntrack_z('%s' does not start with 'nf_conntrack_'rtr#zModule name '%s' too short)rXrrZINVALID_MODULEr,Úreplace)rrZrrrÚ check_modulers   zFirewallCommand.check_moduleTcCsÞ|jƒ}|jƒ}|jƒ}|jƒ} |jƒ} |jƒ} |jƒ} |jƒ} |jƒ}|j ƒ}|j ƒ}|rv|j ƒ}|j ƒ}|j ƒ}n,|jƒ}tt|jƒ|ƒƒ}|jƒ}|jƒ}dd„}g}|dk rÈ||krÈ|jdƒ| rÖ|sâ|sâ|rì|rì|rì|jdƒ|�r|ddj|ƒ}|j|ƒ|j�r2|jd|ƒ|jd|ƒ|�rJ|jd t|ƒƒ|jd |ƒ|�sv|jd |�rnd nd ƒ|�r¦|jddj|ƒƒ|jddj|ƒƒn(|jddj|ƒƒ|jddj|ƒƒ|jddjt|ƒƒƒ|jddjdd„|Dƒƒƒ|jddjt| ƒƒƒ|�s:|jd|�r2d nd ƒ|jd| �rJd nd ƒ|jd| �rbdnddjdd„| Dƒƒƒ|jddjdd„| Dƒƒƒ|jd dj| ƒƒ|jd!|�rÀdnddjt||d"�ƒƒdS)#NcSsfd}d}y|j|ƒ}Wntk r*Yn8X|t|ƒ7}t|||||d…jdƒ…jddƒƒ}|S)Nrz priority=ú ú"rt)Úindexrbr,Úintr~)ZruleÚpriorityZ search_strrprrrÚrich_rule_sorted_key’s *zDFirewallCommand.print_zone_policy_info..rich_rule_sorted_keyÚdefaultZactivez (%s)z, z summary: z description: z priority: z target: z icmp-block-inversion: %srQrPz ingress-zones: r€z egress-zones: z interfaces: z sources: z services: z ports: cSs g|]}d|d|df‘qS)z%s/%srr#r)Ú.0rerrrú ¹sz:FirewallCommand.print_zone_policy_info..z protocols: z forward: %sz masquerade: %sz forward-ports: z rtcSs$g|]\}}}}d||||f‘qS)z$port=%s:proto=%s:toport=%s:toaddr=%sr)r‡rerfrjrkrrrrˆÀsz source-ports: cSs g|]}d|d|df‘qS)z%s/%srr#r)r‡rerrrrˆÅsz icmp-blocks: z rich rules: )Úkey)Z getTargetZ getServicesÚgetPortsÚ getProtocolsZ getMasqueradeZgetForwardPortsÚgetSourcePortsZ getIcmpBlocksZ getRichRulesÚgetDescriptionÚgetShortZgetIngressZonesZgetEgressZonesZ getPriorityZgetIcmpBlockInversionÚsortedÚsetZ getInterfacesZ getSourcesZ getForwardr-rxrr r+)rrKÚsettingsÚ default_zoneÚextra_interfacesÚisPolicyÚtargetZservicesÚportsÚ protocolsZ masqueradeZ forward_portsÚ source_portsZ icmp_blocksZrulesÚ descriptionÚshort_descriptionZ ingress_zonesZ egress_zonesr„Zicmp_block_inversionZ interfacesZsourcesZforwardr…Z attributesrrrÚprint_zone_policy_info|sx        z&FirewallCommand.print_zone_policy_infocCs|j||||dd�dS)NF)r’r“r”)r›)rrKr‘r’r“rrrÚprint_zone_infoËszFirewallCommand.print_zone_infocCs|j||||dd�dS)NT)r’r“r”)r›)rZpolicyr‘r’r“rrrÚprint_policy_infoÎsz!FirewallCommand.print_policy_infoc Cs.|jƒ}|jƒ}|jƒ}|jƒ}|jƒ}|jƒ}|jƒ} |jƒ} |jƒ} |j |ƒ|j rt|j d| ƒ|j d|ƒ|j ddj dd„|Dƒƒƒ|j ddj |ƒƒ|j ddj d d„|Dƒƒƒ|j d dj |ƒƒ|j d dj d d„|j ƒDƒƒƒ|j d dj t | ƒƒƒ|j ddj t | ƒƒƒdS)Nz summary: z description: z ports: r€cSs g|]}d|d|df‘qS)z%s/%srr#r)r‡rerrrrˆßsz6FirewallCommand.print_service_info..z protocols: z source-ports: cSs g|]}d|d|df‘qS)z%s/%srr#r)r‡rerrrrˆãsz modules: z destination: cSsg|]\}}d||f‘qS)z%s:%sr)r‡ÚkÚvrrrrˆçsz includes: z helpers: )rŠr‹rŒZ getModulesr�ÚgetDestinationsrŽZ getIncludesZ getHelpersrr rxrBr�) rZservicer‘r–r—r˜Úmodulesr™Ú destinationsršZincludesZhelpersrrrÚprint_service_infoÑs2   z"FirewallCommand.print_service_infocCsp|jƒ}|jƒ}|jƒ}t|ƒdkr,ddg}|j|ƒ|jrX|jd|ƒ|jd|ƒ|jddj|ƒƒdS)Nrrmrnz summary: z description: z destination: r€)r r�rŽr,rr rx)rZicmptyper‘r¢r™ršrrrÚprint_icmptype_infoìs  z#FirewallCommand.print_icmptype_infocCsœ|jƒ}|jƒ}|jƒ}|jƒ}|jƒ}|j|ƒ|jrT|jd|ƒ|jd|ƒ|jd|ƒ|jddjdd„|jƒDƒƒƒ|jddj|ƒƒdS) Nz summary: z description: z type: z options: r€cSs$g|]\}}|rd||fn|‘qS)z%s=%sr)r‡ržrŸrrrrˆsz4FirewallCommand.print_ipset_info..z entries: ) ZgetTypeZ getOptionsZ getEntriesr�rŽrr rxrB)rZipsetr‘Z ipset_typeZoptionsÚentriesr™ršrrrÚprint_ipset_infoøs z FirewallCommand.print_ipset_infocCs’|jƒ}|jƒ}|jƒ}|jƒ}|jƒ}|j|ƒ|jrT|jd|ƒ|jd|ƒ|jd|ƒ|jd|ƒ|jddjdd„|DƒƒƒdS) Nz summary: z description: z family: z module: z ports: r€cSs g|]}d|d|df‘qS)z%s/%srr#r)r‡rerrrrˆsz5FirewallCommand.print_helper_info..)rŠZ getModuleZ getFamilyr�rŽrr rx)rÚhelperr‘r–ÚmoduleZfamilyr™ršrrrÚprint_helper_infos z!FirewallCommand.print_helper_infocCs |r|jdƒn |jddƒdS)NrQrPr#)r%)rrZrrrrRs z"FirewallCommand.print_query_resultcCs\|js‚|j|ƒtjt|ƒƒ}|tjtjtjtj gkrH|j d|ƒn|j d||ƒdS)Nz Warning: %sz Error: %s) r r2rr*r+rr5r6r7r8r"r%)rÚexception_messagerDrrrÚexception_handlers  z!FirewallCommand.exception_handlercCsd|krd}|j|tjƒdS)NZNotAuthorizedExceptionz`Authorization failed. Make sure polkit agent is running or run the application as superuser.)r%rZNOT_AUTHORIZED)rrªrrrrr2'sz&FirewallCommand.fail_if_not_authorizedcCs d|_dS)NF)r )rrrrr1-sz,FirewallCommand.deactivate_exception_handlercCs d|_dS)NT)r )rrrrr90sz*FirewallCommand.activate_exception_handlercCspg}tƒ}t|ƒ}xP|D]H}|s"P|jƒ}t|ƒdks|ddkrDq||kr|j|ƒ|j|ƒqW|jƒ|S)Nr#rú#ú;)r¬r­)r�ÚopenÚstripr,r-rFÚclose)rÚfilenamer¥Z entries_setÚfÚlinerrrÚget_ipset_entries_from_file3s  z+FirewallCommand.get_ipset_entries_from_file)FF)N)N)N)Nr)N)N)NNF)F)F)F)F)F)NF)F)F)r\)F).Ú__name__Ú __module__Ú __qualname__rrrrrrrr r"r%r'r(rGrHrJrLrNrOrTrUrVr[rgrsrvrzr{r|r}rr›rœr�r£r¤r¦r©rRr«r2r1r9r´rrrrr"sX       J      2    2  O  )Ú__doc__Ú__all__rZfirewallrZfirewall.errorsrZdbus.exceptionsrZfirewall.functionsrrrr r ÚobjectrrrrrÚs