%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/modules/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyc

�
���^c@@s�dZddlmZmZmZddlZddlZddlmZyddlm	Z
Wn!ek
r�ddlm	Z
nXddl
ZddlZddlZddlZddlmZddlmZddlmZeje�ZyvddlZddlZddlZddlZddlZddl Z ddl!Z!ddl"Z"ddl#Z#e$Z%Wne&k
r�e'Z%nXd	Z(d
�Z)d�Z*ddddddddd�Z,dddddddddddddd
�
Z-e'e'd�Z.d�Z/d�Z0d�Z1d�Z2d�Z3d�Z4d�Z5e$d�Z6d�Z7d�Z8d�Z9e'd�Z:d�Z;d�Z<e'd�Z=dS(u5
Module for managing Windows Users

.. important::
    If you feel that Salt should be using this module to manage users on a
    minion, and it is using a different module (or gives an error similar to
    *'user.info' is not available*), see :ref:`here
    <module-provider-override>`.

:depends:
        - pywintypes
        - win32api
        - win32con
        - win32net
        - win32netcon
        - win32profile
        - win32security
        - win32ts
        - wmi

.. note::
    This currently only works with local user accounts, not domain accounts
i(tabsolute_importtunicode_literalstprint_functionN(tdatetime(tquote(tsix(tstring_types(tCommandExecutionErroruusercC@s0tjjj�stdfSts,tdfStS(u.
    Requires Windows and Windows Modules
    u Module win_useradd: Windows Onlyu)Module win_useradd: Missing Win32 Modules(tsalttutilstplatformt
is_windowstFalsetHAS_WIN32NET_MODSt__virtualname__(((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyt__virtual__As


cC@s6|dkst|tj�r"|Stj|d�SdS(u�
    Internal function for converting to Unicode Strings

    The NetUser* series of API calls in this module requires input parameters to
    be Unicode Strings. This function ensures the parameter is a Unicode String.
    This only seems to be an issue in Python 2. All calls to this function
    should be gated behind a ``if six.PY2`` check.

    Args:
        instr (str): String to convert

    Returns:
        str: Unicode type string
    uutf-8N(tNonet
isinstanceRt	text_type(tinstr((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyt_to_unicodeNsc		C@s�tjrlt|�}t|�}t|�}t|�}t|�}t|�}t|�}t|�}ni}	|r�||	d<ntS||	d<tj|	d<||	d<||	d<tj|	d<||	d<ytjdd|	�Wn`tj
k
rD}
tj
d	|�tj
d
|
j�tj
d|
j
�tj
d|
j�tSXtd
|d|d|d|�|ryt||�nt}|S(u
    Add a user to the minion.

    Args:
        name (str): User name

        password (str, optional): User's password in plain text.

        fullname (str, optional): The user's full name.

        description (str, optional): A brief description of the user account.

        groups (str, optional): A list of groups to add the user to.
            (see chgroups)

        home (str, optional): The path to the user's home directory.

        homedrive (str, optional): The drive letter to assign to the home
            directory. Must be the Drive Letter followed by a colon. ie: U:

        profile (str, optional): An explicit path to a profile. Can be a UNC or
            a folder on the system. If left blank, windows uses it's default
            profile directory.

        logonscript (str, optional): Path to a login script to run when the user
            logs on.

    Returns:
        bool: True if successful. False is unsuccessful.

    CLI Example:

    .. code-block:: bash

        salt '*' user.add name password
    unameupassworduprivuhome_dirucommentuflagsuscript_pathiuFailed to create user %sunbr: %suctx: %sumsg: %stnamet	homedrivetprofiletfullnameN(RtPY2RRtwin32netcontUSER_PRIV_USERt	UF_SCRIPTtwin32nett
NetUserAddRterrortlogtwinerrortfuncnametstrerrortupdatetchgroupstTrue(RtpasswordRtdescriptiontgroupsthomeRRtlogonscriptt	user_infotexctret((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytaddcsB-	






cC@s~tjrlt|�}t|�}t|�}t|�}t|�}t|�}t|�}t|�}nytjd|d�}Wn`tjk
r�}tjd|�tjd|j�tjd|j	�tjd|j
�tSX|r�||d<n|r||d<n|r!||d<n|r4||d	<n|rG||d
<n|rZ||d<n|rm||d<n|r�|d
kr�tj
|d<q�ytjjj|�}Wn!ttfk
r�dj|�SXtj|j��|d<n|	dk	r|	rd|d<qd|d<n|
dk	rS|
r<|dctjO<qS|dctjM<n|dk	r|r|dctjM<qn|dk	r�|r�|dctjO<q�|dctjM<n|
dk	r�|
r�|dctjO<q�|dctjM<nytjd|d|�Wn`tjk
ry}tjd|�tjd|j�tjd|j	�tjd|j
�tSXtS(uh
    Updates settings for the windows user. Name is the only required parameter.
    Settings will only be changed if the parameter is passed a value.

    .. versionadded:: 2015.8.0

    Args:
        name (str): The user name to update.

        password (str, optional): New user password in plain text.

        fullname (str, optional): The user's full name.

        description (str, optional): A brief description of the user account.

        home (str, optional): The path to the user's home directory.

        homedrive (str, optional): The drive letter to assign to the home
            directory. Must be the Drive Letter followed by a colon. ie: U:

        logonscript (str, optional): The path to the logon script.

        profile (str, optional): The path to the user's profile directory.

        expiration_date (date, optional): The date and time when the account
            expires. Can be a valid date/time string. To set to never expire
            pass the string 'Never'.

        expired (bool, optional): Pass `True` to expire the account. The user
            will be prompted to change their password at the next logon. Pass
            `False` to mark the account as 'not expired'. You can't use this to
            negate the expiration if the expiration was caused by the account
            expiring. You'll have to change the `expiration_date` as well.

        account_disabled (bool, optional): True disables the account. False
            enables the account.

        unlock_account (bool, optional): True unlocks a locked user account.
            False is ignored.

        password_never_expires (bool, optional): True sets the password to never
            expire. False allows the password to expire.

        disallow_change_password (bool, optional): True blocks the user from
            changing the password. False allows the user to change the password.

    Returns:
        bool: True if successful. False is unsuccessful.

    CLI Example:

    .. code-block:: bash

        salt '*' user.update bob password=secret profile=C:\Users\Bob
                 home=\server\homeshareob homedrive=U:
    iuFailed to update user %sunbr: %suctx: %sumsg: %supassworduhome_diruhome_dir_driveucommentuscript_pathu	full_nameuprofileuNeveruacct_expiresuInvalid Date/Time Format: {0}iupassword_expirediuflagsN(RRRRtNetUserGetInfoRRR R!R"R#RRt
TIMEQ_FOREVERRR	t	dateutilst	date_castt
ValueErrortRuntimeErrortformatttimetmktimet	timetupletUF_ACCOUNTDISABLEt
UF_LOCKOUTtUF_DONT_EXPIRE_PASSWDtUF_PASSWD_CANT_CHANGEtNetUserSetInfoR&(RR'RR(R*RR+Rtexpiration_datetexpiredtaccount_disabledtunlock_accounttpassword_never_expirestdisallow_change_passwordR,R-tdt_obj((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyR$�s�H	








c
C@s tjrt|�}nytjd|d�}Wn`tjk
r�}tjd|�tjd|j�tjd|j	�tjd|j
�tSXytj
�}Wn\tjk
r}tjd�tjd|j�tjd|j	�tjd|j
�nXt}d}xC|D];}tjd|dtj�|kr|d}t}qqW|r�|r�ytjtj|t�Wq�tjk
r�}tjd|�tjd|j�tjd|j	�tjd|j
�tSXq�tjd|�tSn|r�yt|�}	tj|	�Wq�tjk
r�}|j\}
}}|
d	krQq�tjd
|�tjd|j�tjd|j	�tjd|j
�tSq�Xnytjd|�Wn`tjk
r}tjd|�tjd|j�tjd|j	�tjd|j
�tSXtS(
u�
    Remove a user from the minion

    Args:
        name (str): The name of the user to delete

        purge (bool, optional): Boolean value indicating that the user profile
            should also be removed when the user account is deleted. If set to
            True the profile will be removed. Default is False.

        force (bool, optional): Boolean value indicating that the user account
            should be deleted even if the user is logged in. True will log the
            user out and delete user.

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.delete name
    iuUser not found: %sunbr: %suctx: %sumsg: %suNo logged in users foundu	SessionIduUser %s is currently logged in.iuFailed to remove profile for %suFailed to delete user %sN(RRRRR0RRR R!R"R#Rtwin32tstWTSEnumerateSessionstWTSQuerySessionInformationtWTSUserNameR&tWTSLogoffSessiontWTS_CURRENT_SERVER_HANDLEt
getUserSidtwin32profilet
DeleteProfilet
pywintypestargst
NetUserDel(
RtpurgetforceR,R-t	sess_listt	logged_int
session_idtsesstsidtnumbertcontexttmessage((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytdeleteTsr	

"

cC@s�tjrt|�}ntj�}|jd�dkrb|jd�d}|jd�d}n|j�}tj	tj
d|d|�d�S(u�
    Get the Security ID for the user

    Args:
        username (str): The user name for which to look up the SID

    Returns:
        str: The user SID

    CLI Example:

    .. code-block:: bash

        salt '*' user.getUserSid jsnuffy
    u\i����iN(RRRtwin32apitGetComputerNametfindtsplittuppert
win32securitytConvertSidToStringSidtLookupAccountNameR(tusernametdomain((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyRL�s	cC@std|d|�S(u9
    Set the user's password

    Args:
        name (str): The user name for which to set the password

        password (str): The new password

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.setpassword jsnuffy sup3rs3cr3t
    RR'(R$(RR'((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytsetpassword�scC@s�tjr$t|�}t|�}nt|�}t|�jd�jd�}t|�}|sdtS||dkrxtSdj	||�}t
d|dt�}|ddkS(uE
    Add user to a group

    Args:
        name (str): The user name to add to the group

        group (str): The name of the group to which to add the user

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.addgroup jsnuffy 'Power Users'
    u'ugroupsunet localgroup "{0}" {1} /adducmd.run_alltpython_shelluretcodei(RRRt
_cmd_quotetlstriptrstriptinfoRR&R6t__salt__(RtgrouptusertcmdR.((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytaddgroup�s	cC@s�tjr$t|�}t|�}nt|�}t|�jd�jd�}t|�}|sdtS||dkrxtSdj	||�}t
d|dt�}|ddkS(uW
    Remove user from a group

    Args:
        name (str): The user name to remove from the group

        group (str): The name of the group from which to remove the user

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.removegroup jsnuffy 'Power Users'
    u'ugroupsu net localgroup "{0}" {1} /deleteucmd.run_allRhuretcodei(RRRRiRjRkRlRR&R6Rm(RRnRoRpR.((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytremovegroups	cK@s�tjr$t|�}t|�}ntjjj|�}|jdt�}|rgtjjj	|�n|r}t
jd�nt|�}|s�tS||dkr�tSt
d|d|�s�tSt|�}|d|dkr�|d|kStS(u�
    Change the home directory of the user, pass True for persist to move files
    to the new home directory if the old home directory exist.

    Args:
        name (str): The name of the user whose home directory you wish to change

        home (str): The new location of the home directory

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.chhome foo \\fileserver\home\foo True
    upersistu6Ignoring unsupported 'persist' argument to user.chhomeuhomeRR*(RRRRR	RPtclean_kwargstpopRtinvalid_kwargsR RlR&R$(RR*tkwargstpersisttpre_infot	post_info((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytchhome7s(	cC@std|d|�S(uj
    Change the profile directory of the user

    Args:
        name (str): The name of the user whose profile you wish to change

        profile (str): The new location of the profile

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.chprofile foo \\fileserver\profiles\foo
    RR(R$(RR((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyt	chprofilegscC@std|d|�S(uR
    Change the full name of the user

    Args:
        name (str): The user name for which to change the full name

        fullname (str): The new value for the full name

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.chfullname user 'First Last'
    RR(R$(RR((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyt
chfullname|sc	C@s�tjrt|�}nt|t�r9|jd�}ng|D]}|jd�^q@}tjr�g|D]}t|�^qk}ntt|��}|t|�kr�t	St
|�}|s'xd|D]Y}t
|�jd�jd�}||kr�dj
||�}td|dt	�q�q�Wnx�|D]�}||krFq.nt
|�jd�jd�}dj
||�}td|dt	�}|dd	kr.tj|d
�tSq.Wtt|��}t||�d	kS(u�
    Change the groups this user belongs to, add append=False to make the user a
    member of only the specified groups

    Args:
        name (str): The user name for which to change groups

        groups (str, list): A single group or a list of groups to assign to the
            user. For multiple groups this can be a comma delimited string or a
            list.

        append (bool, optional): True adds the passed groups to the user's
            current groups. False sets the user's groups to the passed groups
            only. Default is True.

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.chgroups jsnuffy Administrators,Users True
    u,u *u'u net localgroup "{0}" {1} /deleteucmd.run_allRhunet localgroup "{0}" {1} /adduretcodeiustdout(RRRRRR`tstriptsettlist_groupsR&RiRjRkR6RmR RRtlen(	RR)tappendtxtugrpsRnRptouttagrps((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyR%�s8	"	"

cC@s!tjrt|�}ni}i}ytjd*|d�}Wntjk
rSnX|rg}ytjd*|�}Wntjk
r�nX|d|d<|d|d<tj	|d�|d<|d|d<|d	|d	<|d	|d
<t
|dtj@�|d<|d
|d<|d|d<|d|d<|d|d<t
jtj�j��|d}tj|�jd�|d<|ddkr�d|d<n tj|d�jd�|d<tj|d�jd�|d<|ddk|d<|dst||d�|d<n|d|d <|d!|d"<|d sI|d|d <n||d#<|dtj@dkrwt|d$<n
t|d$<|dtj@dkr�t|d%<n
t|d%<|dtj@dkr�t|d&<n
t|d&<|dtj@dkrt|d'<n
t|d'<d(|d)<|SiSd*S(+u~
    Return user information

    Args:
        name (str): Username for which to display information

    Returns:
        dict: A dictionary containing user information
            - fullname
            - username
            - SID
            - passwd (will always return None)
            - comment (same as description, left here for backwards compatibility)
            - description
            - active
            - logonscript
            - profile
            - home
            - homedrive
            - groups
            - password_changed
            - successful_logon_attempts
            - failed_logon_attempts
            - last_logon
            - account_disabled
            - account_locked
            - password_never_expires
            - disallow_change_password
            - gid

    CLI Example:

    .. code-block:: bash

        salt '*' user.info jsnuffy
    iu	full_nameufullnameunameuuser_siduuidupasswordupasswducommentudescriptionuflagsuactiveuscript_pathulogonscriptuprofileubad_pw_countufailed_logon_attemptsu
num_logonsusuccessful_logon_attemptsupassword_ageu%Y-%m-%d %H:%M:%Supassword_changedu
last_logoniuNeveruacct_expiresuexpiration_dateupassword_expirediuexpireduhome_diruhomeuhome_dir_driveu	homedriveugroupsupassword_never_expiresuaccount_disableduaccount_lockedudisallow_change_passworduugidN(RRRRR0RRtNetUserGetLocalGroupsRbRctboolRR:R7R8RtnowR9t
fromtimestamptstrftimet_get_userprofile_from_registryR<RR&R;R=(RR.titemsR)tsecs((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyRl�sr%	#















cC@s=tdddj|�d�d}tjd|||�|S(u
    In case net user doesn't return the userprofile we can get it from the
    registry

    Args:
        user (str): The user name, used in debug message

        sid (str): The sid to lookup in the registry

    Returns:
        str: Profile directory
    ureg.read_valueuHKEY_LOCAL_MACHINEu<SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{0}uProfileImagePathuvdatau.user %s with sid=%s profile is located at "%s"(RmR6R tdebug(RoRXtprofile_dir((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyR�<s

cC@s�tjrt|�}nt�}yt|�d}Wntk
rItSXx$|D]}|j|jd��qQWt	t
|��S(u
    Return a list of groups the named user belongs to

    Args:
        name (str): The user name for which to list groups

    Returns:
        list: A list of groups to which the user belongs

    CLI Example:

    .. code-block:: bash

        salt '*' user.list_groups foo
    ugroupsu *(RRRR~RltKeyErrorRR/R}tsortedtlist(RtugrpRoRn((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyRUs		

cC@s�dtkr|rtdSg}x�td�D]�}i}td|�}d|d<|d|d<|d|d<|d|d<|d	|d	<d|d
<|d|d<|j|�q/W|td<|S(uz
    Return the list of all info for all users

    Args:
        refresh (bool, optional): Refresh the cached user information. Useful
            when used from within a state function. Default is False.

    Returns:
        dict: A dictionary containing information about all users on the system

    CLI Example:

    .. code-block:: bash

        salt '*' user.getent
    uuser.getentuuser.list_usersu	user.infouugidugroupsuhomeunameupasswdushelluuid(t__context__RmR�(trefreshR.RotstuffR,((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytgetentss 


cC@s�d}g}t}ymxb|s$|ryt}tjddtj|tj�\}}}x|D]}|j|d�q[WqW|SWntj	k
r�nXdS(u�
    Return a list of all users on Windows

    Returns:
        list: A list of all users on the system

    CLI Example:

    .. code-block:: bash

        salt '*' user.list_users
    iunameN(
R&RRtNetUserEnumRRtFILTER_NORMAL_ACCOUNTtMAX_PREFERRED_LENGTHR�R(trest	user_listtdowhiletuserst_Ro((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyt
list_users�s"

cC@s�tjr$t|�}t|�}nt|�}|sNtdj|���nt|�}|rxtdj|���ntjjj	��t
jdd�}WdQXy|jd|�d}Wn&t
k
r�tdj|���nX|j|�d}|dkszidd6dd	6d
d6dd
6dd6dd6dd6dd6dd6dd6dd6}tdj||||���nt|�jd�|kS(u9
    Change the username for a named user

    Args:
        name (str): The user name to change

        new_name (str): The new name for the current user

    Returns:
        bool: True if successful, otherwise False

    CLI Example:

    .. code-block:: bash

        salt '*' user.rename jsnuffy jshmoe
    uUser '{0}' does not existuUser '{0}' already existstfind_classesiNtNameuSuccessuInstance not foundiuInstance requirediuInvalid parameteriuUser not foundiuDomain not foundiuHOperation is allowed only on the primary domain controller of the domainiu;Operation is not allowed on the last administrative accountiuROperation is not allowed on specified special groups: user, admin, local, or guestiuOther API errori	uInternal errori
u6There was an error renaming '{0}' to '{1}'. Error: {2}uname(RRRRlRR6RR	twinapitComtwmitWMItWin32_UserAccountt
IndexErrortRenametget(Rtnew_nametcurrent_infotnew_infotcRotresultt
error_dict((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytrename�sB	


cC@s�y+|rtjtj�}ntj�}Wnntjk
r�}tjd�tjd|j�tjd|j	�tjd|j
�tdd|��nX|s�td��n|S(um
    Get the username that salt-minion is running under. If salt-minion is
    running as a service it should return the Local System account. If salt is
    running from a command prompt it should return the username that started the
    command prompt.

    .. versionadded:: 2015.5.6

    Args:
        sam (bool, optional): False returns just the username without any domain
            notation. True returns the domain with the username in the SAM
            format. Ie: ``domain\username``

    Returns:
        str: Returns username

    CLI Example:

    .. code-block:: bash

        salt '*' user.current
    uFailed to get current userunbr: %suctx: %sumsg: %sRl(R]t
GetUserNameExtwin32contNameSamCompatibletGetUserNameRORR R!R"R#R(tsamt	user_nameR-((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pytcurrents
(>t__doc__t
__future__RRRtloggingR7RtshlexRRit	Exceptiontpipestsalt.utils.argsRtsalt.utils.dateutilstsalt.utils.platformtsalt.utils.winapitsalt.extRtsalt.ext.sixRtsalt.exceptionsRt	getLoggert__name__R ROR�R]R�RRRMRbRFR&R
tImportErrorRRRRRR/R$R\RLRgRqRrRzR{R|R%RlR�RR�R�R�R�(((s</usr/lib/python2.7/site-packages/salt/modules/win_useradd.pyt<module>s�



	
	O�d			%	'	0		>	m		'	!	F

Zerion Mini Shell 1.0