HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/mssql.cpython-310.pyc
o

�N�g9�@sdZddlZzddlZdZWneydZYnwddddddd	�Zd
d�Zdd
�ZGdd�dej	j
j�Zdd�Z
dd�Zdd�Zdd�Zd3dd�Zdd�Zdd�Zdd �Zd4d!d"�Zd#d$�Zd5d%d&�Z				d6d'd(�Zd)d*�Zd7d+d,�Zd-d.�Z	d8d/d0�Zd1d2�ZdS)9a�
Module to provide MS SQL Server compatibility to salt.

:depends:   - FreeTDS
            - pymssql Python module

:configuration: In order to connect to MS SQL Server, certain configuration is
    required in minion configs/pillars on the relevant minions. Some sample
    pillars might look like::

        mssql.server: 'localhost'
        mssql.port:   1433
        mssql.user:   'sysdba'
        mssql.password:   'Some preferable complex password'
        mssql.database: ''

    The default for the port is '1433' and for the database is '' (empty string);
    in most cases they can be left at the default setting.
    Options that are directly passed into functions will overwrite options from
    configs or pillars.
�NTFZ	localhosti�Zsysdba��Zserver�port�user�password�database�as_dictcCstrdSdS)zC
    Only load this module if all imports succeeded bin exists
    T)FzYThe mssql execution module cannot be loaded: the pymssql python library is not available.)�HAS_ALL_IMPORTS�r
r
�F/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/mssql.py�__virtual__+srcKsRi}dD]}||vr||||<qtdd|t�|d��||<qtjdi|��S)Nrz
config.optionzmssql.r
)Z__salt__�	_DEFAULTS�get�pymssql�connect)�kwargsZconnection_args�argr
r
r�_get_connection8s
�rc@seZdZdd�ZdS)�
_MssqlEncodercCst|�S)N)�str)�self�or
r
r�defaultFsz_MssqlEncoder.defaultN)�__name__�
__module__�__qualname__rr
r
r
rrDsrc
Kstz tdi|����}|�|�tjj�t��d|�	�i��dWSt
y9}z
dt|�ffWYd}~Sd}~ww)a
    Run a SQL query and return query result as list of tuples, or a list of dictionaries if as_dict was passed, or an empty list if no data is available.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.tsql_query 'SELECT @@version as version' as_dict=True
    Z	resultset)zCould not run the queryNr
)r�cursor�execute�salt�utils�json�loadsr�encodeZfetchall�	Exceptionr)�queryr�cur�errr
r
r�
tsql_queryJs

����r'cKstdi|��S)z
    Return the version of a MS SQL server.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.version
    �SELECT @@versionN)r(�r'�rr
r
r�version`s
r+cKsdd�tdddi|��D�S)z�
    Return the database list created on a MS SQL server.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.db_list
    cS�g|]}|d�qS�rr
��.0�rowr
r
r�
<listcomp>w���zdb_list.<locals>.<listcomp>�SELECT name FROM sys.databasesrFN)r3r)r*r
r
r�db_listms
�r4cKsttd�|�fi|���dkS)z�
    Find if a specific database exists on the MS SQL server.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.db_exists database_name='DBNAME'
    z5SELECT database_id FROM sys.databases WHERE NAME='{}'�)�lenr'�format)�
database_namerr
r
r�	db_exists}s������r9�NONEc
Ks�|dvrdSd|�d|�d�}|r|dd�|�7}d}zHztdi|��}|�d	�|���|�Wn$tyV}zd
|��WYd}~W|rQ|�d�|��SSd}~wwW|rc|�d�|��d	S|rq|�d�|��ww)
z�
    Creates a new database.
    Does not update options of existing databases.
    new_database_options can only be a list of strings

    CLI Example:

    .. code-block:: bash

        salt minion mssql.db_create DB_NAME
    )r:ZPARTIALz*CONTAINMENT can be one of NONE and PARTIALzCREATE DATABASE [z] CONTAINMENT = � � WITH �, NTzCould not create the database: Fr
)�joinr�
autocommitrrr#�close)rZcontainmentZnew_database_optionsr�sql�conn�er
r
r�	db_create�s6


����
�

�rDc
Ks�z;t|fi|��r9|dvr9tdi|��}|�d�|��}|�d�|��|�d|���|�d�|��WdSWdStyR}zd|��WYd}~Sd}~ww)	z�
    Drops a specific database from the MS SQL server.
    It will not drop any of 'master', 'model', 'msdb' or 'tempdb'.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.db_remove database_name='DBNAME'
    )ZmasterZmodelZmsdbZtempdbTz9ALTER DATABASE {} SET SINGLE_USER WITH ROLLBACK IMMEDIATEzDROP DATABASE FzCould not find the database: Nr
)r9rr?rrr7r@r#)r8rrBr%rCr
r
r�	db_remove�s&
��
��rEcKstdddd�|��S)zp
    Lists database roles.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.role_list
    Zsp_helproleT�r$rNr
r)r*r
r
r�	role_list�s
rGcKs$ttdd|�d�dd�|���dkS)z~
    Checks if a role exists.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.role_exists db_owner
    z
sp_helprole "�"TrFr5Nr
�r6r')�rolerr
r
r�role_exists�s$rKc
Ks�|sg}d|��}|r|d|��7}d}z[z(td
i|��}|�d�|���|�|D]}|���d|�d|�d��q+Wn$tya}zd|��WYd}~W|r\|�d	�|��SSd}~wwW|rn|�d	�|��dS|r||�d	�|��ww)a�
    Creates a new database role.
    If no owner is specified, the role will be owned by the user that
    executes CREATE ROLE, which is the user argument or mssql.user option.
    grants is list of strings.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.role_create role=product01 owner=sysdba grants='["SELECT", "INSERT", "UPDATE", "DELETE", "EXECUTE"]'
    zCREATE ROLE z AUTHORIZATION NTzGRANT z TO [�]zCould not create the role: Fr
)rr?rrr#r@)rJ�ownerZgrantsrrArBZgrantrCr
r
r�role_create�s<


�

����
�

�rNc
Ksxz$tdi|��}|�d�|��}|�d|���|�d�|��WdSty;}zd|��WYd}~Sd}~ww)z�
    Remove a database role.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.role_create role=test_role01
    Tz
DROP ROLE zCould not remove the role: Nr
�rr?rrr@r#)rJrrBr%rCr
r
r�role_removes


��rPc
Ksd|r	|�d|��}zttddd�|�i|���dkWSty1}zd|��WYd}~Sd}~ww)z�
    Find if a login exists in the MS SQL server.
    domain, if provided, will be prepended to login

    CLI Example:

    .. code-block:: bash

        salt minion mssql.login_exists 'LOGIN'
    �\r$z.SELECT name FROM sys.syslogins WHERE name='{}'r5zCould not find the login: Nr
)r6r'r7r#)�login�domainrrCr
r
r�login_exists0s&��������rTc

Ks�t|�t|�kr
dSt||fi|��rdS|r|�d|��}|s"g}|s&g}d|�d�}|r3|d7}nt|t�rD|�dd|d�d	��n
|�dd
|�d��|rY|dd
�|�7}d}z[z(tdi|��}|�d�|���	|�|D]}|���	d|�d|�d��qrWn$t
y�}	zd|	��WYd}	~	W|r�|�d�|��SSd}	~	wwW|r�|�d�|��dS|r�|�d�|��ww)aK
    Creates a new login.  Does not update password of existing logins.  For
    Windows authentication, provide ``new_login_domain``.  For SQL Server
    authentication, prvide ``new_login_password``.  Since hashed passwords are
    *varbinary* values, if the ``new_login_password`` is 'int / long', it will
    be considered to be HASHED.

    new_login_roles
        a list of SERVER roles

    new_login_options
        a list of strings

    CLI Example:

    .. code-block:: bash

        salt minion mssql.login_create LOGIN_NAME database=DBNAME [new_login_password=PASSWORD]
    FrQzCREATE LOGIN [�] z FROM WINDOWS rzPASSWORD=0x�xz HASHEDzPASSWORD=N'�'r<r=NTzALTER SERVER ROLE [�] ADD MEMBER [rLzCould not create the login: r
)�boolrT�
isinstance�int�insertr>rr?rrr#r@)
rRZnew_login_passwordZnew_login_domainZnew_login_rolesZnew_login_optionsrrArBrJrCr
r
r�login_createOsV


�

����
�

�r]c
Kszz%tdi|��}|�d�|��}|�d|�d��|�d�|��WdSty<}zd|��WYd}~Sd}~ww)zy
    Removes an login.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.login_remove LOGINNAME
    TzDROP LOGIN [rLFzCould not remove the login: Nr
rO)rRrrBr%rCr
r
r�login_remove�s


��r^cKs@|r	|�d|��}|r||d<ttddd|�d�i|���dkS)	z�
    Find if an user exists in a specific database on the MS SQL server.
    domain, if provided, will be prepended to username

    CLI Example:

    .. code-block:: bash

        salt minion mssql.user_exists 'USERNAME' [database='DBNAME']
    rQrr$z&SELECT name FROM sysusers WHERE name='rWr5Nr
rI)�usernamerSrrr
r
r�user_exists�s
�����r`cKsdd�t	dddi|��D�S)z�
    Get the user list for a specific database on the MS SQL server.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.user_list [database='DBNAME']
    cSr,r-r
r.r
r
rr1�r2zuser_list.<locals>.<listcomp>�9SELECT name FROM sysusers where issqluser=1 or isntuser=1rFN)rar)r*r
r
r�	user_list�s
����rbc
Ks�|r|sdSt||fi|��rd|�d�S|r)|�d|��}|r'|�d|��n|}|r/||d<|s3g}|s7g}d|�d�}|rH|d|�d	�7}n|d
7}|rW|dd�|�7}d
}z[z(tdi|��}|�d�|���|�|D]}	|���d|	�d|�d	��qpWn$ty�}
zd|
��WYd
}
~
W|r�|�d�|��SSd
}
~
wwW|r�|�d�|��dS|r�|�d�|��ww)a2
    Creates a new user.  If login is not specified, the user will be created
    without a login.  domain, if provided, will be prepended to username.
    options can only be a list of strings

    CLI Example:

    .. code-block:: bash

        salt minion mssql.user_create USERNAME database=DBNAME
    z"domain cannot be set without loginzUser z already existsrQrz
CREATE USER [rUz FOR LOGIN [rLz WITHOUT LOGINr<r=NTzALTER ROLE [rXzCould not create the user: Fr
)r`r>rr?rrr#r@)r_rRrSrZroles�optionsrrArBrJrCr
r
r�user_create�sX
�

����
�

�rdc
Ks�d|vrdSz$tdi|��}|�d�|��}|�d|���|�d�|��WdStyA}zd|��WYd}~Sd}~ww)z�
    Removes an user.

    CLI Example:

    .. code-block:: bash

        salt minion mssql.user_remove USERNAME database=DBNAME
    rFTz
DROP USER zCould not remove the user: Nr
rO)r_rrBr%rCr
r
r�user_remove
s

��re)r:N)NN)r)NrNN)rN)NrNNN)�__doc__Zsalt.utils.jsonrrr	�ImportErrorr
rrrr �JSONEncoderrr'r+r4r9rDrErGrKrNrPrTr]r^r`rbrdrer
r
r
r�<module>sP��



!$
%
!
�B

�8