
    qi                     v    S r SSKrSSKrSSKrSSKJr  SSKJr  Sr\R                  " \
5      r " S S\5      rg)a
  
Implementation of a domain controller that allows users to authenticate against
a Windows NT domain or a local computer.

Used by HTTPAuthenticator. Only available on linux and macOS.

See also https://wsgidav.readthedocs.io/en/latest/user_guide_configure.html

Purpose
-------

Usage::

   from wsgidav.dc.nt_dc import NTDomainController
   domain_controller = NTDomainController(wsgidav_app, config)

where:

+ domain_controller object corresponds to that in ``wsgidav.yaml`` or
  as input into ``wsgidav.http_authenticator.HTTPAuthenticator``.

+ preset_domain allows the admin to specify a domain to be used (instead of any domain that
  may come as part of the user_name in domain\user). This is useful only if there
  is one domain to be authenticated against and you want to spare users from typing the
  domain name

+ preset_server allows the admin to specify the NETBIOS name of the domain controller to
  be used (complete with the preceding \\). if absent, it will look for trusted
  domain controllers on the localhost.

This class allows the user to authenticate against a Windows NT domain or a local computer,
requires NT or beyond (2000, XP, 2003, etc).

This class requires Mark Hammond's Win32 extensions for Python at here_ or sourceforge_

.. _here : http://starship.python.net/crew/mhammond/win32/Downloads.html
.. _sourceforge : http://sourceforge.net/projects/pywin32/

Information on Win32 network authentication was from the following resources:

+ http://ejabberd.jabber.ru/node/55

+ http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81402


Testability and caveats
-----------------------

**Digest Authentication**
   Digest authentication requires the password to be retrieve from the system to compute
   the correct digest for comparison. This is so far impossible (and indeed would be a
   big security loophole if it was allowed), so digest authentication WILL not work
   with this class.

   Highly recommend basic authentication over SSL support.

**User Login**
   Authentication will count as a user login attempt, so any security in place for
   invalid password attempts may be triggered.

   Also note that, even though the user is logged in, the application does not impersonate
   the user - the application will continue to run under the account and permissions it
   started with. The user has the read/write permissions to the share of the running account
   and not his own account.

**Using on a local computer**
   This class has been tested on a local computer (Windows XP). Leave domain as None and
   do not specify domain when entering user_name in this case.

**Using for a network domain**
   This class is being tested for a network domain (I'm setting one up to test).

    N)util)BaseDomainControllerreStructuredTextc                   ^   ^  \ rS rSrU 4S jrS rS rS rS rS r	S r
S	 rS
 rS rSrU =r$ )NTDomainControllerX   c                    > [         TU ]  X5        [        R                  " USSS9nUR	                  S5      U l        UR	                  S5      U l        g )Nnt_dcT)as_dictpreset_domainpreset_server)super__init__r   get_dict_valuegetr   r   )selfwsgidav_appconfigdc_conf	__class__s       E/home/kali/flask_env/lib/python3.13/site-packages/wsgidav/dc/nt_dc.pyr   NTDomainController.__init__Y   sF    -%%fgtD$[[9$[[9    c                 l    U R                   R                   SU R                  < SU R                  < S3$ )N(, ))r   __name__r   r   r   s    r   __str__NTDomainController.__str__a   s7    ~~&&'q););(>bASAS@VVWX	
r   c                     g)NzWindows Domain Authentication )r   	path_infoenvirons      r   get_domain_realm#NTDomainController.get_domain_realmf   s    .r   c                     g)NTr#   )r   realmr%   s      r   require_authentication)NTDomainController.require_authenticationi   s    r   c                 n    U R                  U5      u  pVU R                  U5      nU R                  XcXW5      $ N)_get_domain_username_get_domain_controller_name
_auth_user)r   r)   	user_namepasswordr%   domainuserdc_names           r   basic_auth_user"NTDomainController.basic_auth_userl   s5    00;226:tv??r   c                     g)NFr#   r   s    r   supports_http_digest_auth,NTDomainController.supports_http_digest_authq   s    r   c                     UR                  SS5      n[        U5      S:X  a  S nUS   nO
US   nUS   nU R                  b  U R                  nX44$ )N\   r   )splitlenr   )r   r1   	user_datar3   r4   s        r   r.   'NTDomainController._get_domain_username   s]    OOD!,	y>QFQ<Dq\FQ<D)''F~r   c                     U R                   b  U R                   $  [        R                  " S U5      nU$ ! [         a    S n U$ f = fr-   )r   win32netNetGetAnyDCName	Exception)r   r3   pdcs      r   r/   .NTDomainController._get_domain_controller_name   sS    )%%%	**48C 
  	C
	s   4 AAc                 2   SnU(       a  US:X  a  Sn [         R                  " US[        R                  S5      u  pVn[        R
                  " U5      R                  5       nU HR  nUR                  S5      n	U	(       d   e[        R                  " U	5      (       d   eXxS   R                  5       :X  d  MR    g   U(       a  M  [        R                  SU< SU< 35        g! [         R                   a"  n
[        R                  SU
 35         S n
A
gS n
A
ff = f)	Ninitr   nameTzNetUserEnum: FUser z not found on server )rC   NetUserEnumwin32netconFILTER_NORMAL_ACCOUNTr   to_strlowerr   is_strerror_logger	exceptioninfo)r   r1   r3   serverresumeusers_totalunuserinfouinamees              r   _is_userNTDomainController._is_user   s    (0(<(<A{@@!)%v [[+113 %H%\\&1F!M6;;v....f-3355# !& f$ 	uYM)>vjIJ	 >> !!M!"56s$   BC  5C  7C   D4DDc           	      t   U R                  XU5      (       d  gS n [        R                  " UUU[        R                  [        R                  5      nU(       d8  [
        R                  SU< SU< S35         U(       a  UR                  5         gg U(       a  UR                  5         [
        R                  SU< S35        g! [        R                   aB  n[
        R                  SU< SU< SU 35         S nAU(       a  UR                  5         ggS nAff = f! U(       a  UR                  5         f f = f)	NFz
LogonUser(r   z, '***') failed.z, '***') failed: rK   z logged on.T)
r^   win32security	LogonUserLOGON32_LOGON_NETWORKLOGON32_PROVIDER_DEFAULTrS   warningCloserR   debug)r   r1   r2   r3   rV   htokenerrs          r   r0   NTDomainController._auth_user   s    }}Y77	",,3366F *YMF:EU VW    i]+67 "" 	OOYMF:5FseL  	  s*   AC D D8D DD D7)r   r   )r   
__module____qualname____firstlineno__r   r    r&   r*   r6   r9   r.   r/   r^   r0   __static_attributes____classcell__)r   s   @r   r   r   X   s;    :

/@
2
0 r   r   )__doc__rC   rM   ra   wsgidavr   wsgidav.dc.base_dcr   __docformat__get_module_loggerr   rS   r   r#   r   r   <module>ru      sA   HR     3"

 
 
*C- Cr   