<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import os , sys <EOL> import contextlib <EOL> import socket <EOL> import ntsecuritycon <EOL> import pywintypes <EOL> import win32con <EOL> import win32security <EOL> import win32api <EOL> import win32cred <EOL> import win32event <EOL> import win32net <EOL> import win32netcon <EOL> import winerror <EOL> from winsys . _compat import * <EOL> from winsys import constants , core , exc , utils <EOL> from winsys import _advapi32 <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> LOGON = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = win32security ) <EOL> LOGON . doc ( "<STR_LIT>" ) <EOL> EXTENDED_NAME = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = win32con ) <EOL> EXTENDED_NAME . doc ( "<STR_LIT>" ) <EOL> WELL_KNOWN_SID = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = win32security ) <EOL> WELL_KNOWN_SID . doc ( "<STR_LIT>" ) <EOL> USER_PRIV = constants . Constants . from_list ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , pattern = "<STR_LIT>" , namespace = win32netcon ) <EOL> USER_PRIV . doc ( "<STR_LIT>" ) <EOL> UF = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = win32netcon ) <EOL> UF . doc ( "<STR_LIT>" ) <EOL> SID_NAME_USE = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = ntsecuritycon ) <EOL> SID_NAME_USE . doc ( "<STR_LIT>" ) <EOL> FILTER = constants . Constants . from_pattern ( "<STR_LIT>" , namespace = win32netcon ) <EOL> FILTER . doc ( "<STR_LIT>" ) <EOL> PySID = pywintypes . SIDType <EOL> class x_accounts ( exc . x_winsys ) : <EOL> "<STR_LIT>" <EOL> WINERROR_MAP = { <EOL> winerror . ERROR_NONE_MAPPED : exc . x_not_found <EOL> } <EOL> wrapped = exc . wrapper ( WINERROR_MAP , x_accounts ) <EOL> def _win32net_enum ( win32_fn , system_or_domain ) : <EOL> resume = <NUM_LIT:0> <EOL> while True : <EOL> items , total , resume = wrapped ( win32_fn , system_or_domain , <NUM_LIT:0> , resume ) <EOL> for item in items : <EOL> yield item <EOL> if resume == <NUM_LIT:0> : break <EOL> def principal ( principal , cls = core . UNSET ) : <EOL> """<STR_LIT>""" <EOL> cls = Principal if cls is core . UNSET else cls <EOL> if principal is None : <EOL> return None <EOL> elif type ( principal ) == PySID : <EOL> return cls . from_sid ( principal ) <EOL> elif isinstance ( principal , int ) : <EOL> return cls . from_well_known ( principal ) <EOL> elif isinstance ( principal , cls ) : <EOL> return principal <EOL> else : <EOL> return cls . from_string ( unicode ( principal ) ) <EOL> def user ( name ) : <EOL> """<STR_LIT>""" <EOL> return principal ( name , cls = User ) <EOL> def group ( name ) : <EOL> """<STR_LIT>""" <EOL> return principal ( name , cls = Group ) <EOL> def local_group ( name ) : <EOL> """<STR_LIT>""" <EOL> return principal ( name , cls = LocalGroup ) <EOL> def global_group ( name ) : <EOL> """<STR_LIT>""" <EOL> return principal ( name , cls = GlobalGroup ) <EOL> def me ( ) : <EOL> """<STR_LIT>""" <EOL> return Principal . me ( ) <EOL> _domain = None <EOL> def domain ( system = None ) : <EOL> global _domain <EOL> if _domain is None : <EOL> _domain = wrapped ( win32net . NetWkstaGetInfo , system , <NUM_LIT:100> ) [ '<STR_LIT>' ] <EOL> return _domain <EOL> def domain_controller ( domain = None ) : <EOL> return wrapped ( win32net . NetGetAnyDCName , None , domain ) <EOL> def users ( system = None ) : <EOL> """<STR_LIT>""" <EOL> return iter ( _LocalUsers ( system ) ) <EOL> class Principal ( core . _WinSysObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sid , system = None ) : <EOL> """<STR_LIT>""" <EOL> core . _WinSysObject . __init__ ( self ) <EOL> self . sid = sid <EOL> self . system = system <EOL> try : <EOL> self . name , self . domain , self . type = wrapped ( win32security . LookupAccountSid , self . system , self . sid ) <EOL> except exc . x_not_found : <EOL> self . name = str ( self . sid ) <EOL> self . domain = self . type = None <EOL> def __hash__ ( self ) : <EOL> return hash ( str ( self . sid ) ) <EOL> def __eq__ ( self , other ) : <EOL> return self . sid == principal ( other ) . sid <EOL> def __lt__ ( self , other ) : <EOL> return self . sid < principal ( other ) . sid <EOL> def pyobject ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . sid <EOL> def as_string ( self ) : <EOL> if self . domain : <EOL> return "<STR_LIT>" % ( self . domain , self . name ) <EOL> else : <EOL> return self . name or str ( self . sid ) <EOL> def dumped ( self , level ) : <EOL> return utils . dumped ( "<STR_LIT>" % ( <EOL> self . as_string ( ) , <EOL> wrapped ( win32security . ConvertSidToStringSid , self . sid ) <EOL> ) , level ) <EOL> def logon ( self , password = core . UNSET , logon_type = core . UNSET ) : <EOL> """<STR_LIT>""" <EOL> if logon_type is core . UNSET : <EOL> logon_type = LOGON . LOGON_NETWORK <EOL> else : <EOL> logon_type = LOGON . constant ( logon_type ) <EOL> hUser = wrapped ( <EOL> win32security . LogonUser , <EOL> self . name , <EOL> self . domain , <EOL> password , <EOL> logon_type , <EOL> LOGON . PROVIDER_DEFAULT <EOL> ) <EOL> return hUser <EOL> @ classmethod <EOL> def from_string ( cls , string , system = None ) : <EOL> r"""<STR_LIT>""" <EOL> if string == "<STR_LIT>" : <EOL> string = wrapped ( win32api . GetUserNameEx , win32con . NameSamCompatible ) <EOL> sid , domain , type = wrapped ( <EOL> win32security . LookupAccountName , <EOL> None if system is None else unicode ( system ) , <EOL> unicode ( string ) <EOL> ) <EOL> cls = cls . SID_NAME_USE_MAP . get ( type , cls ) <EOL> return cls ( sid , None if system is None else unicode ( system ) ) <EOL> @ classmethod <EOL> def from_sid ( cls , sid , system = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> name , domain , type = wrapped ( <EOL> win32security . LookupAccountSid , <EOL> None if system is None else unicode ( system ) , <EOL> sid <EOL> ) <EOL> except exc . x_not_found : <EOL> name = domain = type = core . UNSET <EOL> cls = cls . SID_NAME_USE_MAP . get ( type , cls ) <EOL> return cls ( sid , None if system is None else unicode ( system ) ) <EOL> @ classmethod <EOL> def from_well_known ( cls , well_known , domain = None ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_sid ( wrapped ( win32security . CreateWellKnownSid , well_known , principal ( domain ) ) ) <EOL> @ classmethod <EOL> def me ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_string ( wrapped ( win32api . GetUserNameEx , EXTENDED_NAME . SamCompatible ) ) <EOL> @ contextlib . contextmanager <EOL> def impersonate ( self , password = core . UNSET , logon_type = core . UNSET ) : <EOL> """<STR_LIT>""" <EOL> hLogon = self . logon ( password , logon_type ) <EOL> wrapped ( win32security . ImpersonateLoggedOnUser , hLogon ) <EOL> yield hLogon <EOL> wrapped ( win32security . RevertToSelf ) <EOL> def __enter__ ( self ) : <EOL> wrapped ( win32security . ImpersonateLoggedOnUser , self . logon ( logon_type = LOGON . LOGON_INTERACTIVE ) ) <EOL> def __exit__ ( self , * exc_info ) : <EOL> wrapped ( win32security . RevertToSelf ) <EOL> class User ( Principal ) : <EOL> @ classmethod <EOL> def create ( cls , username , password , system = None ) : <EOL> """<STR_LIT>""" <EOL> user_info = dict ( <EOL> name = username , <EOL> password = password , <EOL> priv = USER_PRIV . USER , <EOL> home_dir = None , <EOL> comment = None , <EOL> flags = UF . SCRIPT , <EOL> script_path = None <EOL> ) <EOL> wrapped ( win32net . NetUserAdd , system , <NUM_LIT:1> , user_info ) <EOL> return cls . from_string ( username , system ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> wrapped ( win32net . NetUserDel , self . system , self . name ) <EOL> def groups ( self ) : <EOL> """<STR_LIT>""" <EOL> for group_name , attributes in wrapped ( win32net . NetUserGetGroups , self . system , self . name ) : <EOL> yield group ( group_name ) <EOL> for group_name in wrapped ( win32net . NetUserGetLocalGroups , self . system , self . name ) : <EOL> yield group ( group_name ) <EOL> def join ( self , other_group ) : <EOL> """<STR_LIT>""" <EOL> return group ( other_group ) . add ( self ) <EOL> def leave ( self , other_group ) : <EOL> """<STR_LIT>""" <EOL> return group ( other_group ) . remove ( self ) <EOL> def runas ( self , command_line , password = core . UNSET , load_profile = False ) : <EOL> """<STR_LIT>""" <EOL> logon_flags = <NUM_LIT:0> <EOL> if load_profile : logon_flags |= _advapi32 . LOGON_FLAGS . WITH_PROFILE <EOL> process_info = _advapi32 . CreateProcessWithLogonW ( <EOL> username = self . name , <EOL> domain = self . domain , <EOL> password = password , <EOL> command_line = command_line , <EOL> logon_flags = logon_flags <EOL> ) <EOL> class Group ( Principal ) : <EOL> SID_NAME_USE_MAP = { } <EOL> def __contains__ ( self , member ) : <EOL> """<STR_LIT>""" <EOL> member = principal ( member ) <EOL> return any ( member == m for m in self ) <EOL> class GlobalGroup ( Group ) : <EOL> _enumerator = win32net . NetGroupEnum <EOL> @ classmethod <EOL> def create ( cls , groupname , domain = None ) : <EOL> """<STR_LIT>""" <EOL> system = domain_controller ( domain ) <EOL> wrapped ( win32net . NetGroupAdd , system , <NUM_LIT:0> , dict ( name = groupname ) ) <EOL> return cls . from_string ( groupname , system ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> wrapped ( win32net . NetGroupDel , self . system , self . name ) <EOL> def add ( self , member ) : <EOL> """<STR_LIT>""" <EOL> member = principal ( member ) <EOL> wrapped ( win32net . NetGroupAddUser , self . system , self . name , r"<STR_LIT>" % ( member . domain , member . name ) ) <EOL> return member <EOL> def remove ( self , member ) : <EOL> """<STR_LIT>""" <EOL> member = principal ( member ) <EOL> wrapped ( win32net . NetGroupDelUser , self . system , self . name , r"<STR_LIT>" % ( member . domain , member . name ) ) <EOL> return member <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> resume = <NUM_LIT:0> <EOL> while True : <EOL> members , total , resume = wrapped ( win32net . NetGroupGetUsers , self . system , self . name , <NUM_LIT:1> , resume ) <EOL> for member in members : <EOL> yield principal ( member [ '<STR_LIT:name>' ] ) <EOL> if resume == <NUM_LIT:0> : break <EOL> class LocalGroup ( Group ) : <EOL> @ classmethod <EOL> def create ( cls , groupname , system = None ) : <EOL> """<STR_LIT>""" <EOL> wrapped ( win32net . NetLocalGroupAdd , system , <NUM_LIT:0> , dict ( name = groupname ) ) <EOL> return cls . from_string ( groupname , system ) <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> wrapped ( win32net . NetLocalGroupDel , self . system , self . name ) <EOL> def add ( self , member ) : <EOL> """<STR_LIT>""" <EOL> member = principal ( member ) <EOL> wrapped ( win32net . NetLocalGroupAddMembers , self . system , self . name , <NUM_LIT:0> , [ dict ( sid = member . sid ) ] ) <EOL> return member <EOL> def remove ( self , member ) : <EOL> """<STR_LIT>""" <EOL> member = principal ( member ) <EOL> wrapped ( win32net . NetLocalGroupDelMembers , self . system , self . name , [ "<STR_LIT>" % ( member . domain , member . name ) ] ) <EOL> return member <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> resume = <NUM_LIT:0> <EOL> while True : <EOL> members , total , resume = wrapped ( win32net . NetLocalGroupGetMembers , self . system , self . name , resume ) <EOL> for member in members : <EOL> yield principal ( member [ '<STR_LIT>' ] ) <EOL> if resume == <NUM_LIT:0> : break <EOL> Principal . SID_NAME_USE_MAP = { <EOL> SID_NAME_USE . User : User , <EOL> SID_NAME_USE . Group : Group , <EOL> SID_NAME_USE . WellKnownGroup : Group <EOL> } <EOL> def local_groups ( system = None ) : <EOL> """<STR_LIT>""" <EOL> for group in _win32net_enum ( win32net . NetLocalGroupEnum , system ) : <EOL> yield LocalGroup . from_string ( group [ '<STR_LIT:name>' ] ) <EOL> def global_groups ( domain = None ) : <EOL> """<STR_LIT>""" <EOL> for group in _win32net_enum ( win32net . NetGroupEnum , domain_controller ( domain ) ) : <EOL> yield GlobalGroup . from_string ( group [ '<STR_LIT:name>' ] ) <EOL> class _LocalUsers ( object ) : <EOL> def __init__ ( self , system = None ) : <EOL> self . system = system <EOL> def __iter__ ( self ) : <EOL> resume = <NUM_LIT:0> <EOL> while True : <EOL> users , total , resume = wrapped ( win32net . NetUserEnum , self . system , <NUM_LIT:0> , FILTER . NORMAL_ACCOUNT , resume ) <EOL> for user in users : <EOL> yield User . from_string ( user [ '<STR_LIT:name>' ] ) <EOL> if resume == <NUM_LIT:0> : break <EOL> def add ( self , username , password ) : <EOL> return User . create ( username , password ) <EOL> def remove ( self , local_user ) : <EOL> return user ( local_user ) . delete ( ) </s>
