
    bl                     B   d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dl	m
Z
 d dl	mZ d dlmZmZ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mZm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$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+  eddde,dd           eddde,dd           eddddd           edd d!"           ed#d$g d%&           ed'd( e- ej.                              &           ed)d*d+d!d,-           ed.d/d0d!d,-           ed1d2d3d4d56          g	Z/ ed7d8dd9d           ed:d;dd<d          gZ0d=Z1 G d> d?e          Z2d@ Z3dA Z4dB Z5 G dC dDe2          Z6 G dE dFe7          Z8 G dG dHe2          Z9 G dI dJe2          Z: G dK dLe          Z;dS )M    N)defaultdict)dsdb)nttime2unix)CommandSuperCommandCommandErrorOption)SamDB)	dot_graph)distance_matrixCOLOUR_SETS)full_matrix)
SCOPE_BASESCOPE_SUBTREELdbError)KCCldif_import_export)KCCError)get_partition_mapsget_partitionget_own_cursorget_utdvget_utdv_edgesget_utdv_distancesget_utdv_max_distanceget_kcc_and_dsasz-Hz--URLz%LDB URL for database or target serverURLH)helptypemetavardestz-oz--outputzwrite here (default stdout)FILE)r   r    r!   defaultz
--distancez&Distance matrix graph output (default)formatdistancestore_const)r   r"   constactionz--utf8zUse utf-8 Unicode characters
store_true)r   r)   z--colorzuse color (yes, no, auto))yesnoauto)r   choicesz--color-schemez,use this colour scheme (implies --color=yes)z-Sz--shorten-namesz don't print long common suffixesF)r   r)   r$   z-rz--talk-to-remotezquery other DCs' databasesz--no-keyzomit the explanatory keystore_falseTkey)r   r)   r$   r"   z--dotzGraphviz dot outputdotz--xdotzattempt to call Graphviz xdotxdot__temp__c                   n    e Zd ZdZdZej        ej        ej        dZ	e
ez   ZdZd ZddZd	 Zd
 Zd ZdS )GraphCommandz Base class for graphing commandsz%prog [options])	sambaoptsversionoptscredopts c                     |                                 }|                    |d          }t          |||          }|S )NTfallback_machine)urlcredentialslp)get_loadparmget_credentialsr
   )selfr   r6   r8   r?   credssamdbs          8/usr/lib/python3/dist-packages/samba/netcmd/visualize.pyget_dbzGraphCommand.get_db_   sD    ##%%((d(CC!2666    N.dotc                 F   ||dk    rt          || j                   dS |t          u r>t          j        d|          \  }}t          |d          }t          j        |           nt          |d          }|                    |           |                                 |S )a  Decide whether we're dealing with a filename, a tempfile, or
        stdout, and write accordingly.

        :param s: the string to write
        :param fn: a destination
        :param suffix: suffix, if destination is a tempfile

        If fn is None or "-", write to stdout.
        If fn is visualize.TEMP_FILE, write to a temporary file
        Otherwise fn should be a filename to write to.
        N-filesamba-tool-visualise)prefixsuffixw)	printoutf	TEMP_FILEtempfilemkstempopenosclosewrite)rB   sfnrO   fdfs         rE   rY   zGraphCommand.writee   s     :s!$)$$$$F??%-C-35 5 5FBRAHRLLLLRA	


					rG   c                 t    |s-|r)|                                                     d          rdS dS |dk    rdS |S )z5Heuristics to work out what output format was wanted.rH   r1   r&   r2   )lowerendswith)rB   r%   outputs      rE   calc_output_formatzGraphCommand.calc_output_format   sN     	" "&,,..11&99 "u!zV5rG   c                    ||                      |t                    }n|                      ||          }t          j                            dd          }t          j        ||g           t          j        |           d S )NSAMBA_TOOL_XDOT_PATHz/usr/bin/xdot)rY   rS   rW   environget
subprocesscallremove)rB   rZ   ra   r[   r2   s        rE   	call_xdotzGraphCommand.call_xdot   sj    >Ay))BBAv&&Bz~~4oFFr
###
	"rG   c                    |dk    rdS |dk    rOt          |t                    r|dk    rdS t          | j        d          sdS | j                                        sdS |&dt
          j                            dd          v rd	S d
S |S )zHeuristics to work out the colour scheme for distance matrices.
        Returning None means no colour, otherwise it sould be a colour
        from graph.COLOUR_SETSr,   Nr-   rJ   isatty256colorTERM zxterm-256color-heatmapansi)
isinstancestrhasattrrR   rl   rW   re   rf   )rB   colorcolor_schemera   s       rE   calc_distance_color_schemez'GraphCommand.calc_distance_color_scheme   s     D==4F??&#&& 6S==t49h// t9##%% tRZ^^FB7777//6rG   )NrH   )__name__
__module____qualname____doc__synopsisoptionsSambaOptionsVersionOptionsCredentialsOptionstakes_optiongroupsCOMMON_OPTIONSDOT_OPTIONStakes_options
takes_argsrF   rY   rb   rj   rv   r9   rG   rE   r5   r5   S   s        ** H)-. 
 #[0MJ     :        rG   r5   c                 ^    t          j        d|           }|r|                    d          S | S )zFHelper function for sorting and grouping DNs by site, if
    possible.z$CN=Servers,CN=\s*([^,]+)\s*,CN=Sites   )researchgroup)dnms     rE   get_dnstr_siter      s3     		92>>A wwqzzIrG   c                 ,    t          | d                   S )z\Helper function for sorting and grouping lists of (DN, ...) tuples
    by site, if possible.r   )r   )ts    rE   get_dnstrlist_siter      s     !A$rG   c                     ddl m} t          |           }t          |t                    r|                    d          }t           ||                                          dd         d          dz  }d	|z  S )
zQGenerate a randomish but consistent darkish colour based on the
    given object.r   )md5utf8N      )basei z#%06x)hashlibr   rr   rq   encodeint	hexdigest)xr   tmp_strcs       rE   colour_hashr      s     !ffG'3 )..((CCLL""$$RaR(r222X=AQ;rG   c                   P    e Zd ZdZeez    edddd          gz   Z	 	 	 	 	 dd
ZdS )cmd_repszrepsFrom/repsTo from every DSA-p--partitionrestrict to this partitionNr   r$   FTrB   c                 T   |                                 }|                    |d          }t          |||          \  }}|j        }t	          |j        |
          }
t          d           }i }|D ]}t          |d          }|r|j                            |t          dg          }t          |d         d         d                   }t          d|d	|d
t          j                   	 |                    d|z  ||           n9# t          $ r,}t          d|d	|d
t          j                   Y d }~d }~ww xY w|                    |||           n0|                    |||           |                    ||||           t#          |                                          }||k    rt          dt          j                   ||z
  D ] }t          d|z  t          j                   !t          d|z  t          j                   ||z
  D ] }t          d|z  t          j                   !|D ]}|	dk    r||k    r|	dk    r||k    r|                    d|z             }|                    |           ||t          |j                  <   |                                \  } }!|                                 D ]0\  }"}#|
|"|
k    r#||"         d                             ||#f           1|!                                D ]0\  }"}#|
|"|
k    r#||"         d                             ||#f           1g g dg g dd}$t3          |j                  \  }%}&|                                D ]\  }'}"|$                                D ]\  }(})|"|(         D ]\  }}#|&                    |'|'          }*|#j        D ]8}+|)d                             ||t          |+j                           |*f           9|#j        D ]8}+|)d                             |t          |+j                           ||*f           9|                     ||          dk    r|                     |||          }ddd},|$                                D ]\  }(})|)                                D ]\  }-}.t          t@                    }/|.D ]#\  }0}1}"|/|"                             |0|1f           $|/                                D ]G\  }"}2tC          d |2||||tD                    }3d |,|-         |"z  d |3}3| #                    |3|           Hd S g }4g }5g }6t#                      }7i }8t#                      }9|$                                D ]\  }(}:|:                                D ]\  }-}.|.D ]\  }0}1}"|8$                    |"tK          |"|-f                    };|(dk    rd!nd"}<|-dk    rd#nd$}=|7&                    |0           |7&                    |1           |6                    |0|1f           |4                    |;           d%|<d&|=}>|5                    |>           |9&                    |"d'|-'                                z   |;|>f           g }?|rdtQ          |9          D ]*\  }"}-};}<|?                    d(d)|;d*|<|"d+|-f           +|?                    d,           |?                    d-           tS          |7|6d|4|5||?.          }3|d/k    r| *                    |3|           d S | #                    |3|           d S )0NTr;   c                  *    t          t                    S )N)r   listr9   rG   rE   <lambda>zcmd_reps.run.<locals>.<lambda>   s    k$&7&7 rG   )readonlydNSHostNamescopeattrsr   zAttempting to contact ldap:// ()rK   	ldap://%sCould not contact ldap://)forced_local_dsazfound extra DSAs:z   %sz(missing DSAs (known locally, not by %s):othersrB   CN=NTDS Settings,currentneeded)tofrom)r   r   r   r   r&   zRepsFrom objects for %szRepsTo objects for %s)r   r   )r   colourshorten_namesgenerate_keygrouping_function
dottedsolidrV   emptyzstyle="z"; arrowhead=repsFzcolor="z";  )Fz style="dotted"; arrowhead="open"zrepsFromTo is needed)Fzstyle="solid"; arrowhead="open"zrepsFromTo currently exists)directededge_colorsedge_stylesr   	key_itemsr2   )+r@   rA   r   unix_nowr   rD   r   r   r   r   rr   rQ   sysstderr
load_samdbr   runset	list_dsasget_dsatranslate_ntdsconndsa_guidget_rep_tablesitemsappendr   rf   rep_repsFromsource_dsa_obj_guid
rep_repsTorb   rv   r   r   r   rY   
setdefaultr   addtitlesortedr   rj   )@rB   r   ra   r   r0   talk_to_remoter6   r8   r7   mode	partitionrt   ru   r   r%   r2   r?   rC   	local_kccdsasr   nc_repsguid_to_dnstrdsa_dnkccresdns_nameedsas_from_heredsa	remote_dn
remote_dsar   npartrep	all_edgesshort_partitionslong_partitionspartnamestate	edgelists
short_namerheader_strings	directionr   
part_edgessrcr"   edgesrZ   edge_coloursr   	dot_edgesdot_verticesused_colourskey_setedgelistr   	linestylearrowstyler   s@                                                                   rE   r   zcmd_reps.run   s4	    ##%%((d(CC*1b%88	4%!)/9==	
 7788
  1	F 1	FFh...C ?o,,V3=4A? - D D s1vm4Q788xx):' ' ' 'NN;#92uEEEE   E!!!L"z+ + + +HHHH
 2u%%%%q"e,,,2uv>>> 11N~%%)
;;;;*T1 : :C'C-cj99999@6I:' ' ' ' >1 : :C'C-cj99999+ F F	8##	V(;(;V^^	V(;(; [[)<y)HII
&&z222:Cc*"5667 "00221!" G GID# (DI,=,=i077FFF!" F FID# (DI,=,=h/66}EEEFF& ')"55')2 6 68 8	 -?y,O,O)/%mmoo 	* 	*NHd$-OO$5$5 * * y#'; * *KFC!0!4!4Xx!H!HJ - * *!&)00#*3q/D+E+EF')* * * * !^ * *!$..*3q/D+E+EF#')* * * ****" ""6622j@@::5;G;AC CL 2- N %.OO$5$5 . . y(1(9(9 . .$Iu!,T!2!2J+0 = =T4"4(//d<<<<'1'7'7'9'9 	. 	.e+D%153?:G9<>LN N N +9*Cd*J*J*JAAN

1f----	.	. F	uu%%(00 	1 	1OE8$,NN$4$4 1 1 	5', 1 1OCt)44d5@$BKBM 6N 6NO OF -2X,=,=7I&/4&7&7FFWE $$S))) $$T***$$c4[111 ''////:C))UUKE&&u---KKv	0A0A'A!'!0 1 1 1 111  	 
	>6<Woo @ @2i  %%6<ffii"H-1TT99"="? @ @ @ @  6 7 7 7  = > > > lI#"."-$1 )+ + + VNN1f%%%%%JJq&!!!!!s   -D
D>"D99D>)NNFTFNNNrB   NNNNNF)	rw   rx   ry   rz   r   r   r	   r   r   r9   rG   rE   r   r      sw        $$"[0t])E	 	 	4 M
 6;%*7;BF).	i" i" i" i" i" i"rG   r   c                       e Zd ZdZd Zd ZdS )NTDSConnzXCollects observation counts for NTDS connections, so we know
    whether all DSAs agree.c                 L    d| _         d| _        d| _        || _        || _        d S )Nr   F)observationssrc_attestsdest_attestsr   r"   )rB   r   r"   s      rE   __init__zNTDSConn.__init__  s,     !			rG   c                 r    | xj         dz  c_         || j        k    rd| _        || j        k    r	d| _        d S d S )Nr   T)r  r   r  r"   r  )rB   attesters     rE   attestzNTDSConn.attest  sN    Qtx#Dty   $D ! rG   N)rw   rx   ry   rz   r	  r  r9   rG   rE   r  r    s<           % % % % %rG   r  c                   V    e Zd ZdZeez    eddd          gz   Zd Z	 	 	 	 	 	 d
d	Z	dS )cmd_ntdsconnzDraw the NTDSConnection graphz--importldifz#graph from samba_kcc generated ldifNr   c                     t          j        d          }t          j                            |d          }|| _        t          j        |||          }|S )NrM   )rN   zimported.ldb)rT   mkdtemprW   pathjoin_tmp_fn_to_deleter   ldif_to_samdb)rB   ldifr?   dr[   rD   s         rE   import_ldif_dbzcmd_ntdsconn.import_ldif_db  sL    $:;;;W\\!^,,!#"0R>>	rG   FTc                 :   |                                 }||                    |d          }nd }|                     ||          }t          |||          \  }}|j                            dd          d         }t                      }g }|D ]}|r|j                            |t          dg          }|d         d         d         }	 | 
                    d|z  ||          }n9# t          $ r,}t          d	|d
|dt          j                   Y d }~d }~ww xY w|                                }|                                }n| 
                    |||          }d|z   }|}|                    |t          dg          }|d         d         d         dk    }|                    ||rdndf           |                    |t$          ddgdg          }|D ]g}t'          |j                  }||                    d          dz   d          } |                    t'          |d         d                   | |f           h|rP|| j        k    rEt1          j        |           t1          j        t0          j                            |                     i }!|D ]:\  }"}#}$|"|#f}%|%|!v r	|!|%         }nt;          |% }||!|%<   |                    |$           ;t?          tA          |           \  }}&| !                    ||          dk    r| "                    |	|
|          }
tF          |
         }'|'$                    dd          }(|'$                    dd          })g }*d|&v r|*                    d           |s|!%                                }+d|z  },n+g }+g }-g }.g }/|!&                                D ]p\  }}0|0j'        r2|+                    |           |0j(        s|-                    |           >|0j(        r|.                    |           [|/                    |           qd},|/r2|*                    d           |/D ]}|*                    d|z             |.r2|*                    d           |.D ]}|*                    d|z             |-r2|*                    d           |-D ]}|*                    d|z             tS          ||+||
||tT          |&           }1d+                    |*          }*|*rd!|(d"|)d!|*}*| ,                    d!|,d#|1d!|*|           d S g }2g }3g }4g }5t[          |          }6tA          |!&                                          D ]\  }%}|2                    |%           |j.        |6k    s|s+|3                    d$           |4                    d           R|j'        rH|4                    d           |j(        r|3                    d%           |3                    d&           |j(        r+|3                    d'           |4                    d(           |3                    d'           |4                    d)           g }7|rc|7                    d*           d+D ]$\  }8}9|8|3v r|7                    d,d-|8z  |9f           %d.D ]$\  }:}9|:|4v r|7                    d,d/|:z  |9f           %|rd0},nd|z  },t_          tA          |          |2d|,|3|5|4||71	  	        }1|d2k    r| 0                    |1|           d S | ,                    |1|           d S )3NTr;   ,r   r   r   r   r   r   r   r   rK   r   zmsDS-isRODCTRUERODCro   z(objectClass=nTDSConnection)
fromServerzsearch_options:0:2)r   
expressionr   controlsr&   headerresetz/No outbound connections are expected from RODCszNTDS Connections known to %sz-NTDS Connections known to each destination DCzTThe following connections are alleged by DCs other than the source and destination:
z  %s -> %s
zbThe following connections are alleged by DCs other than the destination but including the source:
zRThe following connections (included in the chart) are not known to the source DC:
)r   r   r   r   r   row_commentsr   NOTES

z#000000#0000ff#cc00ffz#ff0000style=dashedstyle=dotted)Fzcolor="#000000"zNTDS Connection))r$  zmissing from some DCs)r%  zmissing from source DCFz
color="%s"))r&  zunknown to destination)r'  z!unknown to source and destinationzcolor="#ff0000; %s"zNTDS Connections)r   r   r   edge_labelsr   r   r   r2   )1r@   rA   r  r   my_dsa_dnstrsplitr   rD   r   r   rF   r   rQ   r   r   get_dsServiceName	domain_dnr   r   rr   r   indexr   r  rW   ri   rmdirr  dirnamer  r  zipr   rb   rv   r   rf   keysr   r  r  r   r   r  rY   lenr  r   rj   );rB   r   ra   r   r0   r   r6   r8   r7   rt   ru   r   r%   
importldifr2   r?   rC   r   r   local_dsa_dnverticesattested_edgesr   r   r   rD   r   ntds_dnr   is_rodcmsgmsgdndest_dnr   r   r"   r  krodc_statuscoloursc_headerc_resetepiloggraph_edgesr   source_deniesdest_denies	both_denyconnrZ   r   r   r   r(  	n_serversr   r   descr  s;                                                              rE   r   zcmd_ntdsconn.run  s    ##%%,,R$,GGEEE##J33A*1b%88	4 -33C;;A>55 *	: *	:F o,,V3=4A? - D D q6-03 KKh(>	(02 2EE   E!!!L"z+ + + +HHHH
  1133__&&Ay(;;-6,,w%/&3_  6 6C !f]+A.&8GLL'W#<66"=>>>,,r%2*H&2^ *>(>  ! !C  : :CFC 0 01 4 5 56%%s3|+<Q+?'@'@'.'9 : : : ::  	)!t555IaLLLHRW__Q''((( #1 	 	CxdAEzz!HaLaHHX #VH%5%5 6+""6622j@@::5;G;AC CL ",/G{{8R00Hkk'2..GF$$OPPP! 4: $jjll6E ! " 	${{}} , ,GAt( ,#**1---#/ 4)00333) ,#**1----!((++++G :MM #3 4 4 4 ' : :nq&89999 :MM #< = = = ) : :nq&89999  :MM #F G G G + : :nq&89999+%)'3.;-02D-8: : :A WWV__F 6 6.6hh.5gg.4f6 JJJ555+,11+1634:< < < F	II	5;;==)) 	3 	3DAqQ~**.*##I...""2&&&& 3""2&&&= 3 ''	2222 ''	2222 3##I..."">2222##I..."">2222	 	- 1 2 2 2!H K K\))$$e\F-BD%IJJJ G - -t K''$$e&;e&C&*&, - - -  	B&EE2\AEfX&&	#!"."-"-$1 )+ + + VNN1f%%%%%JJq&!!!!!s   C&&
D0"DD)NNFTFNNNNNNNNF)
rw   rx   ry   rz   r   r   r	   r   r  r   r9   rG   rE   r  r    s        ##"[0~$I	 	 	4 M
   6;%*7;%)/3^" ^" ^" ^" ^" ^"rG   r  c                   h    e Zd ZdZe edddd           edded	
          gz   Z	 	 	 	 	 	 ddZdS )cmd_uptodatenesszvisualize uptodateness vectorsr   r   r   Nr   z--max-digits   z,display this many digits of out-of-date-ness)r$   r    r   FTc                    |st          d| j                   d S |                                }|                    |d          }t	          |||          \  }}|j        | _        t          | j        |          }t          | j                  \  }}|                     |	|
|          }
|	                                D ]\  }}||d fvrt          |||||          }t          ||          }t          |          }t          |t          t          |                              }|dk     rd}d|z  }t!          |||
||t"          ||dd	
  
        }|                     d
|d||           d S )Nz>this won't work without talking to the remote servers (use -r)rK   Tr;   r   
   DCzout-of-date-ness)	r   r   r   r   r   colour_scaledigitsylabelxlabelr   r#  )rQ   rR   r@   rA   r   rD   r   r   rv   r   r   r   r   minr2  rr   r   r   rY   )rB   r   ra   r   r0   r   r6   r8   r7   rt   ru   r   r%   r3  r2   r   
max_digitsr?   rC   r   r   r   r   	part_namepart_dn
utdv_edges	distancesmax_distancerP  c_scalerZ   s                                  rE   r   zcmd_uptodateness.run  s     	 #'9. . . .F ##%%((d(CC*1b%88	4_
!$*i88	,>tz,J,J)/66u7C7=? ? #3"8"8":": 	> 	>Iw$//'	4"eLLJ*:t<<I0;;LS\):):%;%;<<FzzFlGI!%#/*7),.<)0#)#'#5	7 	7 	7A JJJyyy!!4f====5	> 	>rG   )NNFTFNNNNNFNNFNrK  )	rw   rx   ry   rz   r   r	   r   r   r   r9   rG   rE   rJ  rJ    s        (("t])E	 	 	~qsB	D 	D 	D& M 6;%*7;%)04342> 2> 2> 2> 2> 2>rG   rJ  c                       e Zd ZdZi Z e                                            D ]/\  ZZe	                    d          r e            eedd         <   0dS )cmd_visualizez:Produces graphical representations of Samba network state.cmd_   N)
rw   rx   ry   rz   subcommandsglobalsr   r<  v
startswithr9   rG   rE   r\  r\    so        DDK		!! % %1<< 	%!"K!""% %rG   r\  )<rW   r   collectionsr   rg   rT   samba.getoptgetoptr|   sambar   r   samba.netcmdr   r   r   r	   samba.samdbr
   samba.graphr   r   r   r   ldbr   r   r   timer   	samba.kccr   r   samba.kcc.kcc_utilsr   samba.uptodatenessr   r   r   r   r   r   r   r   rr   r   r1  r   r   rS   r5   r   r   r   r   objectr  r  rJ  r\  r9   rG   rE   <module>rp     s  ( 
			 



 # # # # # #                        D D D D D D D D D D D D       ! ! ! ! ! ! 4 4 4 4 4 4 4 4 # # # # # # 3 3 3 3 3 3 3 3 3 3  				 - - - - - - - - ( ( ( ( ( (	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 F4FU. . .
F4"?VT3 3 3
F<F
=B B B
F88     
F96(((* * *
F $;4((**++- - - F4"2/ / / F4#*F/ / /
F:6E; ; ;'0 F7.X}. . .
F89/ / / 	[ [ [ [ [7 [ [ [|         q" q" q" q" q"| q" q" q"h% % % % %v % % %$l" l" l" l" l"< l" l" l"^<> <> <> <> <>| <> <> <>~% % % % %L % % % % %rG   