
    IdC6                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ dd	lmZ dd
lmZ  G d de      Z G d dej0                        Z G d de      Z G d de      Z G d de      Zy)    N)ERR_INVALID_CREDENTIALSLdbError)colour)system_session)SambaOptionOptionError)get_samba_logger)SamDB   )JSONEncoder)ValidationErrorc                   \     e Zd Zej                  dgz   Zej                  Zd Z fdZ xZ	S )Option
validatorsc                 ~    t        | d      xs g }|D ]  }	  |||        y# t        $ r}t        |      d}~ww xY w)zRuns the list of validators on the current option.

        If the validator raises ValidationError, turn that into CommandError
        which gives nicer output.
        r   N)getattrr   CommandError)selfoptvaluer   	validatores         7/usr/lib/python3/dist-packages/samba/netcmd/__init__.pyrun_validatorszOption.run_validators)   sO     T<06B
# 	&I&#u%	& # &"1o%&s   	#	<7<c                 L    t         |   ||      }| j                  ||       |S )zOverride convert_value to run validators just after.

        This can also be done in process() but there we would have to
        replace the entire method.
        )superconvert_valuer   )r   r   r   	__class__s      r   r   zOption.convert_value7   s+     %c51C'    )
__name__
__module____qualname__r   ATTRSoptparseSUPPRESS_HELPr   r   __classcell__)r   s   @r   r   r   %   s/    .E**M& r   r   c                       e Zd ZddZd Zy)PlainHelpFormatterc           	          | j                   | j                  z
  }d| j                  z  }|j                  d      }|D cg c]  }t        j                  ||||       }}dj                  |      dz   }|S c c}w )N 
)initial_indentsubsequent_indent)widthcurrent_indentsplittextwrapfilljoin)r   description
desc_widthindent
paragraphspwrapped_paragraphsresults           r   format_descriptionz%PlainHelpFormatter.format_descriptionF   s    ZZ$"5"55
t*** &&t,
  !
 	 MM!$)/,24! ! -.5!s    A6c                     |rd|z   dz   S y)Nr+     )r   epilogs     r   format_epilogz PlainHelpFormatter.format_epilogS   s    &=4''r   N)r=   )r    r!   r"   r;   r@   r>   r   r   r(   r(   E   s    r   r(   c                      e Zd ZdZd Z ee      Zd Z ee      Zd Z	 ee	      Z
dZg Zg Zi ZdZdZdZdZdZdZddZej.                  ej0                  fd	Zdd
ZddZd Zd Zd ZddZd Z ddddZ!d Z"d Z#ddZ$d Z%y)CommandzA samba-tool command.c                 Z    | j                   j                         d   j                  d      S )Nr   r+   )__doc__
splitlinesrstripr   s    r   _get_short_descriptionzCommand._get_short_description]   s%    ||&&(+22488r   c                     | j                   j                  d      }|d   dz   t        j                  dj	                  |dd              z   S )Nr+   r   r   )rD   r0   r1   dedentr3   )r   liness     r   _get_full_descriptionzCommand._get_full_descriptionb   sA    ""4(Qx$591E!FFFr   c                 ^    | j                   j                  }|j                  d      r|dd  S |S )Ncmd_   )r   r    
startswith)r   names     r   	_get_namezCommand._get_nameh   s-    ~~&&??6"8Or   NFTc                 *    ||| _         ||| _        y y Noutferrfr   rV   rW   s      r   
_set_fileszCommand._set_files   s!    DIDI r   c                 (    | j                  ||       y rT   )rY   rX   s      r   __init__zCommand.__init__   s    d#r   c                 L    | j                  |      \  }}|j                          y rT   )_create_parserprint_usage)r   progparser_s       r   usagezCommand.usage   s"    ''-	r   c                     t        j                  d      }|dnd| d}|t        | | d| | j                         y t        | | d| d| | j                         y )NERRORr=   ()z: filez - )r   
c_DARK_REDprintrW   )r   msgevalueklasserrs        r   _print_errorzCommand._print_error   sj    (m1UG1>SE%3%(tyy9SE%3%s6(3$))Dr   c                 t    |j                         }|j                  |      }t        ||t        |      |      S )z:Helper to connect to Ldb database using command line opts.)credentialssession_infolp)get_loadparmget_credentialsr
   r   )r   ldap_url	sambaoptscredoptsrs   credss         r   ldb_connectzCommand.ldb_connect   s;    ##%((,X5"0"4= 	=r   c                     t        j                  || j                  t        dd       | j                  j	                  d       y)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.
           T)clsr6   	sort_keysr+   N)jsondumprV   r   write)r   datas     r   
print_jsonzCommand.print_json   s,     			$		{1M		r   c                    t        |t              r+|j                  \  }}}|j                  }|j                  }d}nt        j                         \  }}}|}d}d}t        |t              r+t        || j                         | j                          d}n=t        |t              r|j                  \  }}	|t        k(  rt        d| j                         d}n|	dk(  rt        d| j                         d}n|	j                  d      r| j                  ||	d	       d}n| j                  ||	d	       nt        |t               r| j                  |d
       d}n{t        |t"              r| j                  ||d       nWt%        |      t&        u r| j                  ||d       d}n0|| j                  |       n| j                  ||t)        |             |st+        j,                         dk\  r"t/        j0                  || j                         yy)zdisplay a command errorFzuncaught exceptionTrg   zInvalid username or passwordz9LDAP client internal error: NT_STATUS_NETWORK_UNREACHABLEzCould not reach remote serverzUnable to open tdb ldbassert)rm   runtime	exceptionN   )
isinstancer   exception_infoinner_exceptionmessagesysexc_infor   rj   rW   rb   r   argsr   rP   ro   AssertionErrorRuntimeErrortype	Exceptionstrsambaget_debug_level	tracebackprint_tb)
r   r   etyperl   
etracebackr   r   force_traceback	ldb_ecodeldb_emsgs
             r   show_command_errorzCommand.show_command_error   s   a&*+*:*:'UFJ//OiiG#O*-,,.'UFJO*G"Oa%&tyy)JJL#O2$3$8$8!Y334499E"'XX5DIIF"'$$%:;!!'8U;"'!!'8U;8gX6"O6gvy9/"i/gv{;"O$g&gvs5z:e335:z		: ;r   c                    t        j                  | j                  | j                  t	               ||      }|j                  | j                         i }t        | j                  j                               D ]0  }| j                  |   } ||      ||<   |j                  ||          2 | j                  r|j                  dddd       ||fS )N)rb   r4   	formatterr_   r?   z--colorz'use colour if available (default: auto)zalways|never|autoauto)helpmetavardefault)r$   OptionParsersynopsisfull_descriptionr(   add_optionstakes_optionssortedtakes_optiongroupskeysadd_option_group
use_colour
add_option)r   r_   r?   r`   optiongroupsrQ   optiongroups          r   r]   zCommand._create_parser   s    &&----(*f	&
 	4--.422779: 	8D11$7K!,V!4L##L$67	8 ??i#L&9&,  .
 |##r   c                 @    | j                   j                  |dz          y )Nr+   )rV   r   )r   texts     r   r   zCommand.message   s    		t$r   rU   c                <    | j                  ||       || _        | |fS )z8This is a leaf node, the command that will actually run.)rY   command_name)r   pathrV   rW   argvs        r   _resolvezCommand._resolve   s"    d# d|r   c                 D   | j                  | j                        \  }}|j                  t        |            \  }}t	        |j
                        }|j                  D ]-  }|j                  D ]  }|j                  ||j                  =  / |j                  |       | j                  r!| j                  |j                  dd             d}	d}
d}t        | j                        D ]2  \  }}|d   dk7  r|d   dk7  r|	dz  }	|d   d	k(  s|d   dk(  rd
}.|
dz  }
4 t        |      |	k  s|st        |      |
kD  r|j!                          yt        |      | _        || _        || _        	  | j(                  |i |S # t*        $ r}| j-                  |       Y d }~yd }~ww xY w)Ncolorr   r   F?*r   +T)r]   r   
parse_argslistdict__dict__option_groupsoption_listdestupdater   apply_colour_choicepop	enumerate
takes_argslenr^   raw_argvraw_args
raw_kwargsrunr   r   )r   r   r`   r   optsr   kwargsoption_groupoptionmin_argsmax_argsundetermined_max_argsiargr   s                  r   _runzCommand._run   s   #2243D3DE&&tDz2
ddmm$"00 	,L&22 ,;;*v{{+,	, 	l#??$$VZZ%@A  %0 	FAs2w#~#b'S.A2w#~RC(,%A	 I *?CIPXDX T
 	488T,V,, 	##A&	s   )E; ;	FFFc                 4    t        d| j                   d      )z=Run the command. This should be overridden by all subclasses.'z' run method not implemented)NotImplementedErrorr   )r   r   r   s      r   r   zCommand.run  s    !Ad&7&7%88T"UVVr   c                 R    t        d|xs | j                  | j                  ||d|S )zGet a logger object.)rQ   streamverbosequietr>   )r	   rQ   rW   )r   rQ   r   r   r   s        r   
get_loggerzCommand.get_logger   s5     "4995  	r   c                     || _         	 t        j                  | j                  | j                  |       y# t
        $ r}t        d| d      d}~ww xY w)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_wantedrV   rW   
ValueErrorr   )r   	requestedr   s      r   r   zCommand.apply_colour_choice'  sg     !*	G##DII$(II)24  	G!9) EF  F G G	Gs   ,6 	AAA)NNrT   )r=   FF)&r    r!   r"   rD   rH   propertyshort_descriptionrL   r   rR   rQ   r   r   r   r   hiddenr   r   r   r   r   rY   r   stdoutstderrr[   rb   ro   rz   r   r   r]   r   r   r   r   r   r   r>   r   r   rB   rB   Z   s    9 !!78G   56 ID HJMFJHHJ  JJSZZ $E=.;`$(% *.D (TWGr   rB   c                   ,    e Zd ZdZdZi ZddddZd Zy)SuperCommandz&A samba-tool command with subcommands.z%prog <subcommand>NrU   c                   || _         | j                  ||       g }t        |      D ]  \  }}|| j                  v rC||dz   d t	        |      z   }| d| }	| j                  |   }
 |
j
                  |	g|||dc S |dv r|dk(  rd}|j                  |       tt        |d|d	| j                  
       | g fc S  t        d|z  | j                  
       | |fS )a  This 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*   rU   )--helpr   Nz-hz-Vz	--versionr   r   z: no such subcommand: r+   rg   z%s: missing subcommand
)	r   rY   r   subcommandstupler   appendrj   rV   )r   r   rV   rW   r   deferred_argsr   asub_argssub_pathsubs              r   r   zSuperCommand._resolve>  s    !d#
 dO 	DAqD$$$A<%*>>"V1QC=&&q)#s||HNxNdNNII; A$$Q' D!<499M":#	( 	(D1		Bm$$r   c                    d}t        | j                  j                               }t        |D cg c]  }t	        |       c}      }|D ]4  }| j                  |   }|j
                  r|d| ||j                  fz  z  }6 |d| j                   dz  }| j                  | j                  |      \  }}	|j                  t        |            \  }
}|j                          yc c}w )Nz
Available subcommands:
z  %*s  - %s
z5For more help on a specific subcommand, please type: z <subcommand> (-h|--help)
)r?   r   )r   r   r   maxr   r   r   r   r]   r   r   
print_help)r   r   r?   subcmdsc
max_lengthcmd_namecmdr`   r   r   r   s               r   r   zSuperCommand._rund  s    -))..01'2Q#a&23
 	>H""8,CzzoXs'<'<)> > >F		> 	J''((CE 	F  $2243D3DV2T&&tDz2
d
 	% 3s   C")r    r!   r"   rD   r   r   r   r   r>   r   r   r   r   7  s    0#HK)-D $%Lr   r   c                       e Zd ZdZddZd Zy)r   z1An exception class for samba-tool Command errors.Nc                 R    || _         || _        t        j                         | _        y rT   )r   r   r   r   r   )r   r   r   s      r   r[   zCommandError.__init__  s    .!llnr   c                      d| j                   z  S )NzCommandError(%s))r   rG   s    r   __repr__zCommandError.__repr__  s    !DLL00r   rT   )r    r!   r"   rD   r[   r  r>   r   r   r   r   }  s    ;-
1r   r   )r   r$   r   r1   r   r   r   r   r   r   
samba.authr   samba.getoptr   r   samba.loggerr	   samba.samdbr
   encodersr   r   r   r   IndentedHelpFormatterr(   objectrB   r   r   r   r>   r   r   <module>r     s|   &   
    1  % 1 )  ! '[ @77 *ZGf ZGzC7 CL	19 	1r   