509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3
W„j^8  ã               @   sD   d dl Z d dlT d dlZd dlZdZG dd„ dƒZG dd„ dƒZdS )é    N)Ú*z0.1c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSchemaValidatorz+Libnftables JSON validator using jsonschemac          	   C   sJ   t jjt jjtƒdƒ}t|dƒ�}tj|ƒ| _W d Q R X dd l	}|| _	d S )Nzschema.jsonÚrr   )
ÚosÚpathÚjoinÚdirnameÚ__file__ÚopenÚjsonÚloadÚschemaÚ
jsonschema)ÚselfZschema_pathZschema_filer   © r   ú/usr/lib/python3.6/nftables.pyÚ__init__   s
    zSchemaValidator.__init__c             C   s   | j j|| jd� d S )N)Úinstancer   )r   Úvalidater   )r   r   r   r   r   r   "   s    zSchemaValidator.validateN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   r   c               @   sP  e Zd ZdZdddddddd	œZdWdXdYdZd[d\d]d^d_d`dadbdœZdZdcd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-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Zd=d>„ Zd?d@„ ZdAdB„ ZdCdD„ ZdEdF„ ZdGdH„ Z dIdJ„ Z!dKdL„ Z"dMdN„ Z#dOdP„ Z$dQdR„ Z%dSdT„ Z&dUdV„ Z'dS )dÚNftablesz*A class representing libnftables interfaceé   é   é   é   é   é    é@   )ÚscannerÚparserÚevalZnetlinkZmnlz	proto-ctxZsegtreer   é   é   é   é   é	   é
   é   )Ú
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_tg| j_|j| _t| j_tg| j_|j	| _	ttg| j	_|j
| _
t| j
_tg| j
_|j| _ttg| j_|j| _t| j_tg| j_|j| _t| j_tg| j_|j| _t| j_tg| j_|j| _t| j_tg| j_|j| _t| j_ttg| j_|j| _tg|j_| jdƒ| _| j| jƒ | j| jƒ dS )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)ZcdllZLoadLibraryZnft_ctx_newZc_void_pZrestypeZc_intZargtypesÚnft_ctx_output_get_flagsZc_uintÚnft_ctx_output_set_flagsÚnft_ctx_output_get_debugÚnft_ctx_output_set_debugZnft_ctx_buffer_outputÚnft_ctx_get_output_bufferZc_char_pZnft_ctx_buffer_errorÚnft_ctx_get_error_bufferÚnft_run_cmd_from_bufferÚnft_ctx_freeÚ_Nftables__ctx)r   ZsofileÚlibr   r   r   r   C   sD    









zNftables.__init__c             C   s   | j | jƒ d S )N)r>   r?   )r   r   r   r   Ú__del__   s    zNftables.__del__c             C   s   | j | }| j| jƒ|@ S )N)Úoutput_flagsr7   r?   )r   ÚnameÚflagr   r   r   Z__get_output_flag‚   s    
zNftables.__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    


zNftables.__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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.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    zNftables.set_terse_outputc             C   sV   | j | jƒ}g }x2| jjƒ D ]$\}}||@ r|j|ƒ || M }qW |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    

zNftables.get_debugc             C   s`   | j ƒ }t|ƒttgkr|g}d}x*|D ]"}t|ƒtkrB| 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    

zNftables.set_debugc             C   sd   d}t |tƒsd}|jdƒ}| j| j|ƒ}| j| jƒ}| j| jƒ}|rZ|jdƒ}|jdƒ}|||fS )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
        FTzutf-8)Ú
isinstanceÚbytesÚencoder=   r?   r;   r<   Údecode)r   ZcmdlineZcmdline_is_unicodeÚrcÚoutputÚerrorr   r   r   Úcmd‹  s    



zNftables.cmdc             C   sJ   | j dƒ}| jtj|ƒƒ\}}}|s.| j |ƒ t|ƒr@tj|ƒ}|||fS )ai  Run an nftables command in JSON syntax via libnftables.

        Accepts a hash object as input.

        Returns a tuple (rc, output, error):
        rc     -- return code as returned by nft_run_cmd_from_buffer() function
        output -- a hash object containing library standard output
        error  -- a string containing output written to stderr
        T)rR   ru   r   ÚdumpsÚlenÚloads)r   Ú	json_rootZjson_out_oldrr   rs   rt   r   r   r   Újson_cmd£  s    



zNftables.json_cmdc             C   s   | j stƒ | _ | j j|ƒ dS )z©Validate JSON object against libnftables schema.

        Accepts a hash object as input.

        Returns True if JSON is valid, raises an exception otherwise.
        T)Ú	validatorr   r   )r   ry   r   r   r   Újson_validateµ  s    zNftables.json_validater   r   r   r   r   r   r    é€   é   i   i   i   )r6   )(r   r   r   r   ra   rB   r{   r   rA   rG   rI   rH   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   rg   rm   ru   rz   r|   r   r   r   r   r   %   sl   
<
	
						


	#r   )r   ZctypesÚsysr   ZNFTABLES_VERSIONr   r   r   r   r   r   Ú<module>   s   