HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/dnsutil.cpython-310.pyc
o

�N�g�-�@s�dZddlZddlZddlZddlZddlZddlZe�e	�Z
dd�Zd#dd�Zd$dd	�Z
d#d
d�Zd%dd
�Zdd�Zdd�Zdd�Zd&dd�Zd&dd�Zd'dd�Zd(dd�Zd)dd�Zd*d!d"�ZdS)+z|
Compendium of generic DNS utilities.

.. note::

    Some functions in the ``dnsutil`` execution module depend on ``dig``.
�NcCsdS)z�
    Generic, should work on any platform (including Windows). Functionality
    which requires dependencies outside of Python do not belong in this module.
    T�rrr�H/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/dnsutil.py�__virtual__sr�
/etc/hostscCs�|s0z#tjj�|d��}tjj�|���}Wd�n1swYWn
ty/YdSwi}|��D]"}|s;q6|�	d�rAq6|�
�}|d}|dd�}|�|g��|�q6|S)zr
    Parse /etc/hosts file.

    CLI Example:

    .. code-block:: bash

        salt '*' dnsutil.parse_hosts
    �rNzError: hosts data was not found�#r�)
�salt�utils�files�fopen�stringutils�
to_unicode�read�	Exception�
splitlines�
startswith�split�
setdefault�extend)�	hostsfile�hosts�fp_Z	hostsdict�line�comps�ip�aliasesrrr�parse_hostss(
���
rcCs�|�d�}t|d�}||vr|D]
}|||vr|�|�q|s%d|��Sd�|d�|��}tjj�|d��}|�	tjj
�|��Wd�n1sKwYd|�d	|��S)
z�
    Append a single line to the /etc/hosts file.

    CLI Example:

    .. code-block:: bash

        salt '*' dnsutil.hosts_append /etc/hosts 127.0.0.1 ad1.yuk.co,ad2.yuk.co
    �,)rz"No additional hosts were added to z
{} {}� �aNz The following line was added to �:)rr�remove�format�joinr	r
rr�writer
�to_str)r�ip_addr�entries�	host_listr�hostZappend_linerrrr�hosts_append;s



�
�r+c		Cs$tjj�|d��}tjj�|���}Wd�n1swY|�d�}tjj�|d��Z}|��D]L}|r>|�	��
d�rL|�tjj�|�d���q3|��}|D]}||dd�vra|�
|�qRt|�dkr|�tjj�d�|���|�tjj�d��q3Wd�dS1s�wYdS)	a�
    Remove a host from the /etc/hosts file. If doing so will leave a line
    containing only an IP address, then the line will be deleted. This function
    will leave comments and blank lines intact.

    CLI Examples:

    .. code-block:: bash

        salt '*' dnsutil.hosts_remove /etc/hosts ad1.yuk.co
        salt '*' dnsutil.hosts_remove /etc/hosts ad2.yuk.co,ad1.yuk.co
    rNr�wr�
rr)r	r
rrr
rrrr�striprr%r&r"�lenr$)	rr(rrr)�out_filerrr*rrr�hosts_removeVs(
�

���"�r1cCs�|r/z#tjj�|d��}tjj�|���}Wd�n1swYWn	ty.Ynw|s3dSi}d}|��D�]5}|�	d�}|d�
�}|sLq;|�	�}|�d�rb|d||d�dd	�<q;d
|vrnd|vrnd}d	}|dkr�|d
|��7}d|vr�d}|�d
d	��dd	�}nq;d|vr�|�d|d��	�}n|�	�}d|vr�|ddkr�|�
d�|d|d<|d|d<|d|d<|d�ddd�|d<|d|d<t|d�|d<t|d�|d<t|d�|d<t|d �|d!<q;|ddkr�|�d|d�|d�d��sd"|v�rd#�|d|d�|d<|d$d"k�r+|�d"g��|d�q;|d$d%k�rHd%|v�rG|�d%g��|d|dd&��q;|dd'v�rc|d|dd(�|�|di�|d<q;|d|�|d$i�|d<q;|S))z�
    Parses a zone file. Can be passed raw zone data on the API level.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.parse_zone /var/lib/named/example.com.zone
    rNzError: Zone data was not foundZsingle�;r�$r��(�)�multirZORIGIN�@ZSOA�INZNETWORK�ZSOURCE��.ZCONTACT�ZSERIAL�ZREFRESH�ZRETRY�ZEXPIRE�	ZMINTTL�NSz{}.{}��MX)�priorityr*)�A�AAAA)�TARGETZTTL)r	r
rrr
rrrrrr.r�replace�pop�_to_seconds�insert�endswithr#r�append)Zzonefile�zonerZzonedict�moderrr7rrr�
parse_zoneus�
���



���rQcCs�|��}d|vrt|�dd��d}n(d|vr"t|�dd��d}nd|vr)d}nzt|�}Wnty:d}Ynw|dkrAd}|S)z�
    Converts a time value to seconds.

    As per RFC1035 (page 45), max time is 1 week, so anything longer (or
    unreadable) will be set to one week (604800 seconds).
    �Hr4i�Di�Q�Wi�:	)�upper�intrI�
ValueError)Ztimestr�secondsrrrrK�s�rKcCstjj�d�duS)z�
    The dig-specific functions have been moved into their own module, but
    because they are also DNS utilities, a compatibility layer exists. This
    function helps add that layer.
    �digN)r	r
�path�whichrrrr�_has_dig�sr\cCst�r	td|�SdS)z�
    Check that string ip_addr is a valid IP

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.check_ip 127.0.0.1
    zdig.check_ip�<This function requires dig, which is not currently available�r\�__salt__)r'rrr�check_ip�s
r`cC�ft�r
td||�S|dur1zdd�t�|dtjdtj�D�}|WStjy0d|��YSwdS)z�
    Return the A record(s) for ``host``.

    Always returns a list.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.A www.google.com
    zdig.ANcS�g|]}|dd�qS�r;rr��.0�sockrrr�
<listcomp>�
��zA.<locals>.<listcomp>r�Unable to resolve r])r\r_�socket�getaddrinfo�AF_INET�SOCK_RAW�gaierror�r*�
nameserver�	addressesrrrrF�s���rFcCra)z�
    Return the AAAA record(s) for ``host``.

    Always returns a list.

    .. versionadded:: 2014.7.5

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.AAAA www.google.com
    zdig.AAAANcSrbrcrrdrrrrg&rhzAAAA.<locals>.<listcomp>rrir])r\r_rjrk�AF_INET6rmrnrorrrrGs���rGTcC�t�rtd|||�SdS)z�
    Return a list of IPs of the nameservers for ``domain``

    If 'resolve' is False, don't resolve names.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.NS google.com

    zdig.NSr]r^��domain�resolverprrrrB3s
rB�SPFcCrs)a^
    Return the allowed IPv4 ranges in the SPF record for ``domain``.

    If record is ``SPF`` and the SPF record is empty, the TXT record will be
    searched automatically. If you know the domain uses TXT and not SPF,
    specifying that will save a lookup.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.SPF google.com
    zdig.SPFr]r^)ru�recordrprrrrwFsFcCrs)a
    Return a list of lists for the MX of ``domain``.

    If the 'resolve' argument is True, resolve IPs for the servers.

    It's limited to one IP, because although in practice it's very rarely a
    round robin, it is an acceptable configuration and pulling just one IP lets
    the data be similar to the non-resolved version. If you think an MX has
    multiple IPs, don't use the resolver here, resolve them in a separate step.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.MX google.com
    zdig.MXr]r^rtrrrrDZsrDr4cCsti}d}|r
|d|��7}td|d�}t�d�}|s|p|S|r.||kr.tt|�d�}n|}td||d�|S)	a#
    Return, store and update a dns serial for your zone files.

    zone: a keyword for a specific zone

    update: store an updated version of the serial in a grain

    If ``update`` is False, the function will retrieve an existing serial or
    return the current date if no serial is stored. Nothing will be stored

    If ``update`` is True, the function will set the serial to the current date
    if none exist or if the existing serial is for a previous date. If a serial
    for greater than the current date is already stored, the function will
    increment it.

    This module stores the serial in a grain, you can explicitly set the
    stored value as a grain named ``dnsserial_<zone_name>``.

    CLI Example:

    .. code-block:: bash

        salt ns1 dnsutil.serial example.com
    Z	dnsserial�_z
grains.get)�keyz%Y%m%d01rz
grains.setval)rz�val)r_�time�strftime�strrV)rO�updateZgrainsrzZstoredZpresent�currentrrr�serialqs
r�)rN)rNN)NN)N)TN)rwN)FN)r4F)�__doc__�loggingrjr|Zsalt.utils.filesr	Zsalt.utils.pathZsalt.utils.stringutils�	getLogger�__name__�logrrr+r1rQrKr\r`rFrGrBrwrDr�rrrr�<module>s,




O