
    g5B                        S SK r S SKrS SKrS SKrS SKrS SKrS SKJrJrJ	r	  S SKJ
r
  S SKJr  S SKJrJr  S SKJr  S SKJr  S SKJr  S	S
KJr   " S S\R0                  5      r " S S\5      r " S S\5      r " S S\5      rg)    N)ERR_INVALID_CREDENTIALSERR_INSUFFICIENT_ACCESS_RIGHTSLdbError)colour)system_session)OptionOptionParser)get_samba_logger)SamDB)SDDLValueError   )JSONEncoderc                   (    \ rS rSrSrSS jrS rSrg)PlainHelpFormatter%   z>This help formatter does text wrapping and preserves newlines.c           
          U R                   U R                  -
  nSU R                  -  nUR                  S5      nU Vs/ s H  n[        R                  " UUUUS9PM     nnSR                  U5      S-   nU$ s  snf )N 
)initial_indentsubsequent_indent)widthcurrent_indentsplittextwrapfilljoin)selfdescription
desc_widthindent
paragraphspwrapped_paragraphsresults           7/usr/lib/python3/dist-packages/samba/netcmd/__init__.pyformat_description%PlainHelpFormatter.format_description(   s    ZZ$"5"55
t*** &&t,
  !
  	 MM!$)/,24   	 ! -.5!s   !A7c                 "    U(       a  SU-   S-   $ g)Nr     )r   epilogs     r%   format_epilog PlainHelpFormatter.format_epilog5   s    &=4''    r*   N)r)   )__name__
__module____qualname____firstlineno____doc__r&   r,   __static_attributes__r*   r.   r%   r   r   %   s    Hr.   r   c                   8   \ rS rSrSrS r\" \5      rS r\" \5      r	S r
\" \
5      rSrSrSr0 rSrS	rSrSrSrSrSrSS
 jr\R2                  \R4                  4S jrSS jrSS jrS rS rS r SS jr!S r"SS jr#S r$SSS.S jr%S r&S r'SS jr(S r)Sr*g)Command<   zA samba-tool command.c                 Z    U R                   R                  5       S   R                  S5      $ )Nr   r   )r3   
splitlinesrstripr   s    r%   _get_short_descriptionCommand._get_short_description?   s%    ||&&(+22488r.   c                     U R                   R                  S5      nUS   S-   [        R                  " SR	                  USS  5      5      -   $ )Nr   r   r   )r3   r   r   dedentr   )r   liness     r%   _get_full_descriptionCommand._get_full_descriptionD   sA    ""4(Qx$591E!FFFr.   c                 h    U R                   R                  nUR                  S5      (       a  USS  $ U$ )Ncmd_   )	__class__r/   
startswith)r   names     r%   	_get_nameCommand._get_nameJ   s0    ~~&&??6""8Or.   Nr*   FTc                 *    Ub  Xl         Ub  X l        g g Noutferrfr   rN   rO   s      r%   
_set_filesCommand._set_filesb   s    II r.   c                 &    U R                  X5        g rL   )rQ   rP   s      r%   __init__Command.__init__h   s    #r.   c                 p    U R                  U=(       d    U R                  5      u  p#UR                  5         g rL   )_create_parsercommand_nameprint_usage)r   progparser_s       r%   usageCommand.usagek   s+    ''(A0A0AB	r.   c                 6   U R                   S:X  a+  Uc  SnOU SU 3nUb  SU0nO0 nU R                  " X!40 UD6  g [        R                  " S5      nUc  SOSU S3nUc  [	        U U S	U 3U R
                  S
9  g [	        U U S	U SU 3U R
                  S
9  g )Njsonr   z - zerror classERRORr)   (): file)preferred_output_formatprint_json_statusr   
c_DARK_REDprintrO   )r   msgevalueklasskwargserrs         r%   _print_errorCommand._print_erroro   s    ''61~S) '/""69&9(m1UG1>SE%3%(tyy9SE%3%s6(3$))Dr.   c                    UR                   u  p#pE[        [        R                  " S5       SU S3U R                  S9  [        SU 3U R                  S9   [        UR                  5       S U R                  5       5      n[        [        R                  " S5      SUS-    3 U R                  S9  [        SU 3U R                  S9  g ! [         a     NTf = f)	Nra   rd   r   re   r   ^>   )
argsrj   r   ri   rO   lenencodedecode
ValueErrorc_DARK_YELLOW)r   egeneric_msgspecific_msgpositionsddls         r%   _print_sddl_value_errorCommand._print_sddl_value_error   s    45FF18""7+,B{m2>99	$jtyy)	DKKM)84<<>?H
 	%%c*1X\N*:;499M, tyy1  		s   *C 
CCc                     UR                  5       nUR                  U5      n[        UR                  U[	        U5      US9$ )z:Helper to connect to Ldb database using command line opts.)credentialssession_infolp)get_loadparmget_credentialsr   Hr   )r   hostopts	sambaoptscredoptsr   credss         r%   ldb_connectCommand.ldb_connect   s?    ##%((,XZZU"0"4= 	=r.   c                     [         R                  " XR                  [        SSS9  U R                  R	                  S5        g)zPrint json on the screen using consistent formatting and sorting.

A custom JSONEncoder class is used to help with serializing unknown
objects such as Dn for example.
ru   T)clsr    	sort_keysr   N)r`   dumprN   r   write)r   datas     r%   
print_jsonCommand.print_json   s*     			$		{1M		r.   c                     0 nU(       a  SUS'   USLa  [        U5      US'   OSUS'   Ub  X$S'   UR                  U5        U R                  U5        g)a  For commands that really have nothing to say when they succeed
        (`samba-tool foo delete --json`), we can still emit
        '{"status": "OK"}
'. And if they fail they can say:
        '{"status": "error"}
'.
        This function hopes to keep things consistent.

        If error is true-ish but not True, it is stringified and added
        as a message. For example, if error is an LdbError with an
        OBJECT_NOT_FOUND code, self.print_json_status(error) results
        in this:

            '{"status": "error", "message": "object not found"}
'

        unless an explicit message is added, in which case that is
        used. A message can be provided on success, like this:

            '{"status": "OK", "message": "thanks for asking!"}
'

        Extra keywords can be added too.

        In summary, you might go:

            try:
                samdb.delete(dn)
            except Exception as e:
                print_json_status(e)
                return
            print_json_status()
        errorstatusTmessageOKN)strupdater   )r   r   r   rn   r   s        r%   rh   Command.print_json_status   sY    < $DND "%e*Y!DN%OFr.   c                    [        U[        5      (       a*  UR                  u  p#nUR                  nUR                  nSnO[
        R                  " 5       u  p#nUnSnSn[        U[        R                  5      (       a'  [        X0R                  S9  U R                  5         SnGO[        U[        5      (       a  UR                  u  pU[        :X  a  [        SU R                  S9  SnGOeU	S:X  a  [        SU R                  S9  SnGOGU	R                  S5      (       a  U R!                  XiS	5        SnGOU["        :X  a  U R!                  S
5        SnOU	S:X  a  U R!                  U	5        SnOU R!                  XiS	5        O[        U[$        5      (       a  U R'                  U5        SnO[        U[(        5      (       a  U R!                  USS9  SnO[        U[*        5      (       a  U R!                  XcS5        OW[-        U5      [.        L a  U R!                  XcS5        SnO0Uc  U R!                  U5        OU R!                  Xc[1        U5      5        U(       d  [2        R4                  " 5       S:  a  [6        R8                  " X@R                  S9  gg)zdisplay a command errorFzuncaught exceptionTre   zInvalid username or passwordz9LDAP client internal error: NT_STATUS_NETWORK_UNREACHABLEzCould not reach remote serverzUnable to open tdb ldbz#User has insufficient access rightsz,Operation unavailable without authenticationassert)rm   runtime	exceptionN   )
isinstanceCommandErrorexception_infoinner_exceptionr   sysexc_infooptparseOptParseErrorrj   rO   r]   r   rv   r   rG   rp   r   r   r   AssertionErrorRuntimeErrortype	Exceptionr   sambaget_debug_level	tracebackprint_tb)
r   r|   etyperl   
etracebackr   r   force_traceback	ldb_ecodeldb_emsgs
             r%   show_command_errorCommand.show_command_error   s   a&&*+*:*:'UJ//OiiG#O*-,,.'UJO*G"Oa//00&yy)JJL#O22$3$8$8!Y334499E"'XX5DIIF"'$$%:;;!!'U;"'<<!!"GH"'KK!!(+"'!!'U;88((9#O88gX6"O66gy9/"i/g{;"O$g&gs5z:e335:z		: ;r.   c           	         [        U R                  U R                  [        5       UU[        S9nUR                  U R                  5        0 n[        U R                  R                  5       5       H/  nU R                  U   nU" U5      XE'   UR                  XE   5        M1     U R                  (       a  UR                  SSSSS9  X44$ )N)r]   r   	formatterrZ   r+   option_classz--colorz'use colour if available (default: auto)zalways|never|autoauto)helpmetavardefault)r	   synopsisfull_descriptionr   r   add_optionstakes_optionssortedtakes_optiongroupskeysadd_option_group
use_colour
add_option)r   rZ   r+   r[   optiongroupsrH   optiongroups          r%   rW   Command._create_parser  s    ----(*! 	4--.422779:D11$7K!,V!4L##L$67 ; ??i#L&9&,  .
 ##r.   c                 @    U R                   R                  US-   5        g )Nr   )rN   r   )r   texts     r%   r   Command.message(  s    		t$r.   rM   c                6    U R                  X#5        Xl        X4$ )z8This is a leaf node, the command that will actually run.)rQ   rX   )r   pathrN   rO   argvs        r%   _resolveCommand._resolve+  s    # |r.   c                 *   U R                  U R                  5      u  p# UR                  [        U5      5      u  pE[        UR                  5      nUR                   HC  nUR                   H0  n	U	R                  c  M  U	R                  U;   d  M$  XyR                  	 M2     ME     UR                  U5        UR                  S5      S:X  a  SU l        OS U l        U R                  (       a!  U R                  UR!                  SS5      5        Sn
SnSn[#        U R$                  5       H7  u  pUS   S:w  a  US   S	:w  a  U
S
-  n
US   S:X  d	  US   S	:X  a  SnM2  US
-  nM9     ['        U5      U
:  d  U(       d   ['        U5      U:  a  UR)                  5         g[        U5      U l        XPl        Xpl         U R0                  " U0 UD6$ ! [         a  nU R                  U5         S nAgS nAff = f! [         a  nU R                  U5         S nAgS nAff = f)Noutput_formatr`   colorr   r   F?*r   +T)rW   rX   
parse_argslistr   r   dict__dict__option_groupsoption_listdestr   getrg   r   apply_colour_choicepop	enumerate
takes_argsrw   rY   raw_argvraw_args
raw_kwargsrun)r   r   r[   r   optsrv   r|   rn   option_groupoptionmin_argsmax_argsundetermined_max_argsiargs                  r%   _runCommand._run1  s   #2243D3DE	**4:6JD dmm$"00L&22;;*v{{f/D{{+ 3 1 	l#::o&&0+1D( ,0D(??$$VZZ%@A  %0FA2w#~#b'S.A2w#~RC(,%A 1 I *?CIPXDX T
 	88T,V,,[  	##A&	\  	##A&	s/   G 3G- 
G*G%%G*-
H7HHc                 4    [        SU R                   S35      e)z=Run the command. This should be overridden by all subclasses.'z' run method not implemented)NotImplementedErrorrX   )r   rv   rn   s      r%   r   Command.runi  s    !Ad&7&7%88T"UVVr.   c                 Z    [        SU=(       d    U R                  U R                  X#S.UD6$ )zGet a logger object.)rH   streamverbosequietr*   )r
   rH   rO   )r   rH   r   r  rn   s        r%   
get_loggerCommand.get_loggerm  s3     "499  	r.   c                     Xl          [        R                  " U R                  U R                  US9  g! [
         a  n[        SU S35      eSnAff = f)zHeuristics to work out whether the user wants colour output, from a
--color=yes|no|auto option. This alters the ANSI 16 bit colour
"constants" in the colour module to be either real colours or empty
strings.
)hintzUnknown --color option: z% please choose from always|never|autoN)requested_colourr   colour_if_wantedrN   rO   rz   r   )r   	requestedr|   s      r%   r   Command.apply_colour_choicet  se     !*	G##DII$(II)24  	G!9) EF  F G G	Gs   *3 
AAA)rX   rO   rN   rg   r   r   r   r  )NNrL   )r)   FF)+r/   r0   r1   r2   r3   r<   propertyshort_descriptionrA   r   rI   rH   r   r   r   r   hiddenr   r  r   r   r   rg   rQ   r   stdoutstderrrT   r]   rp   r   r   r   rh   r   rW   r   r   r   r   r  r   r4   r*   r.   r%   r6   r6   <   s    9 !!78G   56 ID HJMFJHHJ"  JJSZZ $E,2.=*X8;t$,% *.D 6pWGr.   r6   c                   6    \ rS rSrSrSr0 rSSS.S jrS rSr	g)	SuperCommandi  z&A samba-tool command with subcommands.z%prog <subcommand>NrM   c                   Xl         U R                  X#5        / n[        U5       H  u  pgXpR                  ;   aA  XFS-   S [	        U5      -   nU SU 3n	U R                  U   n
U
R
                  " U	/UQ7X#S.6s  $ US;   a  US:X  a  SnUR                  U5        Mv  [        U< SU< S	3U R                  S
9  U / 4s  $    [        SU-  U R                  S
9  X4$ )zThis is an internal node. We need to consume one of the args and
find the relevant child, returning an instance of that Command.

If there are no children, this SuperCommand will be returned
and its _run() will do a --help like thing.
r   Nr   rM   )--helpr   Nz-hz-Vz	--versionr   r  z: no such subcommand: r   re   z%s: missing subcommand
)	rX   rQ   r   subcommandstupler   appendrj   rN   )r   r   rN   rO   rv   deferred_argsr   asub_argssub_pathsubs              r%   r   SuperCommand._resolve  s     !#
 dODA$$$A<%*>>"V1QC=&&q)||HNxNdNNGG; A$$Q' D!<499M":# $( 	(D1		B$$r.   c                    Sn[        U R                  R                  5       5      n[        U Vs/ s H  n[	        U5      PM     snSS9nU H:  nU R                  U   nUR
                  (       a  M%  USU* XgR                  4-  -  nM<     USU R                   S3-  nU R                  U R                  US9u  pUR                  [        U5      5      u  pUR                  5         gs  snf )	Nz
Available subcommands:
r   )r   z  %*s  - %s
z6
For more help on a specific subcommand, please type: z <subcommand> (-h|--help)
)r+   r   )r   r  r   maxrw   r  r  rX   rW   r   r   
print_help)r   r   r+   subcmdsc
max_lengthcmd_namecmdr[   r   r   rv   s               r%   r   SuperCommand._run  s    -))..01'2'Q#a&'2A>
H""8,CzzoX'<'<)> > >F	   	L''((CE 	F  $2243D3DV2T&&tDz2

 	% 3s   C&)rX   )
r/   r0   r1   r2   r3   r   r  r   r   r4   r*   r.   r%   r  r    s    0#HK)-D $%Lr.   r  c                   (    \ rS rSrSrSS jrS rSrg)r   i  z1An exception class for samba-tool Command errors.Nc                 P    Xl         X l        [        R                  " 5       U l        g rL   )r   r   r   r   r   )r   r   r   s      r%   rT   CommandError.__init__  s    .!llnr.   c                      SU R                   -  $ )NzCommandError(%s))r   r;   s    r%   __repr__CommandError.__repr__  s    !DLL00r.   )r   r   r   rL   )r/   r0   r1   r2   r3   rT   r)  r4   r*   r.   r%   r   r     s    ;-
1r.   r   )r`   r   r   r   r   r   r   r   r   r   r   
samba.authr   samba.getoptr   r	   samba.loggerr
   samba.samdbr   samba.dcerpc.securityr   encodersr   IndentedHelpFormatterr   objectr6   r  r   r   r*   r.   r%   <module>r3     sr   &   
    Q Q  % - )  0 !77 .EGf EGP
C7 CL	19 	1r.   