3 1˜g]8 ã @ sD d dl Z d dlT d dlZd dlZdZG dd„ dƒZG dd„ dƒZdS )é N)Ú*z0.1c @ s e Zd ZdZdd„ Zdd„ ZdS )ÚSchemaValidatorz+Libnftables JSON validator using jsonschemac C sJ t jjt jjtƒdƒ}t|dƒ�}tj|ƒ| _W d Q R X dd l }|| _ d S )Nzschema.jsonÚrr ) ÚosÚpathÚjoinÚdirnameÚ__file__ÚopenÚjsonÚloadÚschemaÚ jsonschema)ÚselfZschema_pathZschema_filer © r ú/usr/lib/python3.6/nftables.pyÚ__init__ s zSchemaValidator.__init__c C s | j j|| jd� d S )N)Úinstancer )r Úvalidater )r r r r r r " s zSchemaValidator.validateN)Ú__name__Ú __module__Ú__qualname__Ú__doc__r r r r r r r s r c @ sP e Zd ZdZdddddddd œZdWdXdYdZd[d\d]d^d_d`dadbdœZdZdcdd„Zdd„ Zdd„ Z dd„ Z dd„ Zdd „ Zd!d"„ Z d#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Zd?d@„ ZdAdB„ ZdCdD„ ZdEdF„ ZdGdH„ Z dIdJ„ Z!dKdL„ Z"dMdN„ Z#dOdP„ Z$dQdR„ Z%dSdT„ Z&dUdV„ Z'dS )dÚNftablesz*A class representing libnftables interfaceé é é é é é é@ )ÚscannerÚparserÚevalZnetlinkZmnlz proto-ctxZsegtreer é é é é é é é )Ú reversednsÚserviceÚ statelessÚhandler ÚechoÚguidÚ numeric_protoÚnumeric_prioÚnumeric_symbolÚnumeric_timeÚterseNúlibnftables.so.1.1.0c C s> t j|ƒ}|j| _t| j_tg| j_|j| _t| j_tg| j_|j | _ ttg| j _|j | _ t| j _tg| j _|j| _ttg| j_|j| _t| j_tg| j_|j | _ t| j _tg| j _|j| _t| j_tg| j_|j| _t| j_tg| j_|j| _t| j_ttg| j_|j| _tg|j_| jdƒ| _| j| jƒ | j| jƒ dS )al Instantiate a new Nftables class object. Accepts a shared object file to open, by default standard search path is searched for a file named 'libnftables.so'. After loading the library using ctypes module, a new nftables context is requested from the library and buffering of output and error streams is turned on. r N)ZcdllZLoadLibraryZnft_ctx_newZc_void_pZrestypeZc_intZargtypesÚnft_ctx_output_get_flagsZc_uintÚnft_ctx_output_set_flagsÚnft_ctx_output_get_debugÚnft_ctx_output_set_debugZnft_ctx_buffer_outputÚnft_ctx_get_output_bufferZc_char_pZnft_ctx_buffer_errorÚnft_ctx_get_error_bufferÚnft_run_cmd_from_bufferÚnft_ctx_freeÚ_Nftables__ctx)r ZsofileÚlibr r r r C sD zNftables.__init__c C s | j | jƒ d S )N)r> r? )r r r r Ú__del__ s zNftables.__del__c C s | j | }| j| jƒ|@ S )N)Úoutput_flagsr7 r? )r ÚnameÚflagr r r Z__get_output_flag‚ s zNftables.__get_output_flagc C sD | j | }| j| jƒ}|r$||B }n || @ }| j| j|ƒ ||@ S )N)rB r7 r? r8 )r rC ÚvalrD ÚflagsZ new_flagsr r r Z__set_output_flag† s zNftables.__set_output_flagc C s | j dƒS )z©Get the current state of reverse DNS output. Returns a boolean indicating whether reverse DNS lookups are performed for IP addresses in output. r+ )Ú_Nftables__get_output_flag)r r r r Úget_reversedns_output� s zNftables.get_reversedns_outputc C s | j d|ƒS )zŸEnable or disable reverse DNS output. Accepts a boolean turning reverse DNS lookups in output on or off. Returns the previous value. r+ )Ú_Nftables__set_output_flag)r rE r r r Úset_reversedns_output˜ s zNftables.set_reversedns_outputc C s | j dƒS )z¦Get the current state of service name output. Returns a boolean indicating whether service names are used for port numbers in output or not. r, )rG )r r r r Úget_service_output¡ s zNftables.get_service_outputc C s | j d|ƒS )z³Enable or disable service name output. Accepts a boolean turning service names for port numbers in output on or off. Returns the previous value. r, )rI )r rE r r r Úset_service_output© s zNftables.set_service_outputc C s | j dƒS )z„Get the current state of stateless output. Returns a boolean indicating whether stateless output is active or not. r- )rG )r r r r Úget_stateless_output³ s zNftables.get_stateless_outputc C s | j d|ƒS )z—Enable or disable stateless output. Accepts a boolean turning stateless output either on or off. Returns the previous value. r- )rI )r rE r r r Úset_stateless_outputº s zNftables.set_stateless_outputc C s | j dƒS )z~Get the current state of handle output. Returns a boolean indicating whether handle output is active or not. r. )rG )r r r r Úget_handle_outputà s zNftables.get_handle_outputc C s | j d|ƒS )zŠEnable or disable handle output. Accepts a boolean turning handle output on or off. Returns the previous value. r. )rI )r rE r r r Úset_handle_outputÊ s zNftables.set_handle_outputc C s | j dƒS )zzGet the current state of JSON output. Returns a boolean indicating whether JSON output is active or not. r )rG )r r r r Úget_json_outputÓ s zNftables.get_json_outputc C s | j d|ƒS )z�Enable or disable JSON output. Accepts a boolean turning JSON output either on or off. Returns the previous value. r )rI )r rE r r r Úset_json_outputÚ s zNftables.set_json_outputc C s | j dƒS )zzGet the current state of echo output. Returns a boolean indicating whether echo output is active or not. r/ )rG )r r r r Úget_echo_outputã s zNftables.get_echo_outputc C s | j d|ƒS )z†Enable or disable echo output. Accepts a boolean turning echo output on or off. Returns the previous value. r/ )rI )r rE r r r Úset_echo_outputê s zNftables.set_echo_outputc C s | j dƒS )z›Get the current state of GID/UID output. Returns a boolean indicating whether names for group/user IDs are used in output or not. r0 )rG )r r r r Úget_guid_outputó s zNftables.get_guid_outputc C s | j d|ƒS )z–Enable or disable GID/UID output. Accepts a boolean turning names for group/user IDs on or off. Returns the previous value. r0 )rI )r rE r r r Úset_guid_outputû s zNftables.set_guid_outputc C s | j dƒS )ztGet current status of numeric protocol output flag. Returns a boolean value indicating the status. r1 )rG )r r r r Úget_numeric_proto_output s z!Nftables.get_numeric_proto_outputc C s | j d|ƒS )zœSet numeric protocol output flag. Accepts a boolean turning numeric protocol output either on or off. Returns the previous value. r1 )rI )r rE r r r Úset_numeric_proto_output s z!Nftables.set_numeric_proto_outputc C s | j dƒS )zzGet current status of numeric chain priority output flag. Returns a boolean value indicating the status. r2 )rG )r r r r Úget_numeric_prio_output s z Nftables.get_numeric_prio_outputc C s | j d|ƒS )z°Set numeric chain priority output flag. Accepts a boolean turning numeric chain priority output either on or off. Returns the previous value. r2 )rI )r rE r r r Úset_numeric_prio_output s z Nftables.set_numeric_prio_outputc C s | j dƒS )zsGet current status of numeric symbols output flag. Returns a boolean value indicating the status. r3 )rG )r r r r Úget_numeric_symbol_output% s z"Nftables.get_numeric_symbol_outputc C s | j d|ƒS )zÂSet numeric symbols output flag. Accepts a boolean turning numeric representation of symbolic constants in output either on or off. Returns the previous value. r3 )rI )r rE r r r Úset_numeric_symbol_output, s z"Nftables.set_numeric_symbol_outputc C s | j dƒS )zqGet current status of numeric times output flag. Returns a boolean value indicating the status. r4 )rG )r r r r Úget_numeric_time_output6 s z Nftables.get_numeric_time_outputc C s | j d|ƒS )z¹Set numeric times output flag. Accepts a boolean turning numeric representation of time values in output either on or off. Returns the previous value. r4 )rI )r rE r r r Úset_numeric_time_output= s z Nftables.set_numeric_time_outputc C s | j dƒS )z|Get the current state of terse output. Returns a boolean indicating whether terse output is active or not. r5 )rG )r r r r Úget_terse_outputG s zNftables.get_terse_outputc C s | j d|ƒS )z�Enable or disable terse output. Accepts a boolean turning terse output either on or off. Returns the previous value. r5 )rI )r rE r r r Úset_terse_outputN s zNftables.set_terse_outputc C sV | j | jƒ}g }x2| jjƒ D ]$\}}||@ r|j|ƒ || M }qW |rR|j|ƒ |S )zmGet currently active debug flags. Returns a set of flag names. See set_debug() for details. )r9 r? Údebug_flagsÚitemsÚappend)r rE ÚnamesÚnÚvr r r Ú get_debugW s zNftables.get_debugc C s` | j ƒ }t|ƒttgkr|g}d}x*|D ]"}t|ƒtkrB| j| }||O }q(W | j| j|ƒ |S )a Set debug output flags. Accepts either a single flag or a set of flags. Each flag might be given either as string or integer value as shown in the following table: Name | Value (hex) ----------------------- scanner | 0x1 parser | 0x2 eval | 0x4 netlink | 0x8 mnl | 0x10 proto-ctx | 0x20 segtree | 0x40 Returns a set of previously active debug flags, as returned by get_debug() method. r )rg ÚtypeÚstrÚintra r: r? )r ÚvaluesÚoldrE rf r r r Ú set_debugh s zNftables.set_debugc C sd d}t |tƒsd}|jdƒ}| j| j|ƒ}| j| jƒ}| j| jƒ}|rZ|jdƒ}|jdƒ}|||fS )aÁ Run a simple nftables command via libnftables. Accepts a string containing an nftables command just like what one would enter into an interactive nftables (nft -i) session. Returns a tuple (rc, output, error): rc -- return code as returned by nft_run_cmd_from_buffer() fuction output -- a string containing output written to stdout error -- a string containing output written to stderr FTzutf-8)Ú isinstanceÚbytesÚencoder= r? r; r<