
    gL                         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
r
S SKJr  S SKJrJr  S SK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JrJrJr   " S
 S\5      r g)    N)PopenPIPESTDOUT)Ldbdsdb)miscsecurity)
ndr_unpack)	get_bytes)CommandErrorOption   )GetPasswordCommandgpg_decryptdecrypt_samba_gpg_helpvirtual_attributes_helpc                   ,   \ rS rSrSrSr\R                  \R                  S.r	\
" SSSSS	9\
" S
S\SSS9\
" SSS\SSS9\
" SS\SSS9\
" S\\SSS9\
" S\SSSS9\
" S S!\S"S#S9\
" S$S%SSS&S9\
" S'\S(S)S*S9\
" S+S,SSS-S9\
" S.S/SSS0S9/r     S4S2 jrS3rg1)5cmd_user_syncpasswords.   a$  Sync the password of user accounts.

This syncs logon passwords for user accounts.

Note that this command should run on a single domain controller only
(typically the PDC-emulator). However the "password hash gpg key ids"
option should to be configured on all domain controllers.

The command must be run from the root user id or another authorized user id.
The '-H' or '--URL' option only supports ldapi:// and can be used to adjust the
local path.  By default, ldapi:// is used with the default path to the
privileged ldapi socket.

This command has three modes: "Cache Initialization", "Sync Loop Run" and
"Sync Loop Terminate".


Cache Initialization
====================

The first time, this command needs to be called with
'--cache-ldb-initialize' in order to initialize its cache.

The cache initialization requires '--attributes' and allows the following
optional options: '--decrypt-samba-gpg', '--script', '--filter' or
'-H/--URL'.

The '--attributes' parameter takes a comma separated list of attributes,
which will be printed or given to the script specified by '--script'. If a
specified attribute is not available on an object it will be silently omitted.
All attributes defined in the schema (e.g. the unicodePwd attribute holds
the NTHASH) and the following virtual attributes are possible (see '--help'
for supported virtual attributes in your environment):

   virtualClearTextUTF16: The raw cleartext as stored in the
                          'Primary:CLEARTEXT' (or 'Primary:SambaGPG'
                          with '--decrypt-samba-gpg') buffer inside of the
                          supplementalCredentials attribute. This typically
                          contains valid UTF-16-LE, but may contain random
                          bytes, e.g. for computer accounts.

   virtualClearTextUTF8:  As virtualClearTextUTF16, but converted to UTF-8
                          (only from valid UTF-16-LE).

   virtualSSHA:           As virtualClearTextUTF8, but a salted SHA-1
                          checksum, useful for OpenLDAP's '{SSHA}' algorithm.

   virtualCryptSHA256:    As virtualClearTextUTF8, but a salted SHA256
                          checksum, useful for OpenLDAP's '{CRYPT}' algorithm,
                          with a $5$... salt, see crypt(3) on modern systems.
                          The number of rounds used to calculate the hash can
                          also be specified. By appending ";rounds=x" to the
                          attribute name i.e. virtualCryptSHA256;rounds=10000
                          will calculate a SHA256 hash with 10,000 rounds.
                          Non numeric values for rounds are silently ignored.
                          The value is calculated as follows:
                          1) If a value exists in 'Primary:userPassword' with
                             the specified number of rounds it is returned.
                          2) If 'Primary:CLEARTEXT', or 'Primary:SambaGPG' with
                             '--decrypt-samba-gpg'. Calculate a hash with
                             the specified number of rounds
                          3) Return the first CryptSHA256 value in
                             'Primary:userPassword'.

   virtualCryptSHA512:    As virtualClearTextUTF8, but a salted SHA512
                          checksum, useful for OpenLDAP's '{CRYPT}' algorithm,
                          with a $6$... salt, see crypt(3) on modern systems.
                          The number of rounds used to calculate the hash can
                          also be specified. By appending ";rounds=x" to the
                          attribute name i.e. virtualCryptSHA512;rounds=10000
                          will calculate a SHA512 hash with 10,000 rounds.
                          Non numeric values for rounds are silently ignored.
                          The value is calculated as follows:
                          1) If a value exists in 'Primary:userPassword' with
                             the specified number of rounds it is returned.
                          2) If 'Primary:CLEARTEXT', or 'Primary:SambaGPG' with
                             '--decrypt-samba-gpg'. Calculate a hash with
                             the specified number of rounds.
                          3) Return the first CryptSHA512 value in
                             'Primary:userPassword'.

   virtualWDigestNN:      The individual hash values stored in
                          'Primary:WDigest' where NN is the hash number in
                          the range 01 to 29.
                          NOTE: As at 22-05-2017 the documentation:
                          3.1.1.8.11.3.1 WDIGEST_CREDENTIALS Construction
                        https://msdn.microsoft.com/en-us/library/cc245680.aspx
                          is incorrect.

   virtualKerberosSalt:   This results the salt string that is used to compute
                          Kerberos keys from a UTF-8 cleartext password.

   virtualSambaGPG:       The raw cleartext as stored in the
                          'Primary:SambaGPG' buffer inside of the
                          supplementalCredentials attribute.
                          See the 'password hash gpg key ids' option in
                          smb.conf.

The '--decrypt-samba-gpg' option triggers decryption of the
Primary:SambaGPG buffer. Check with '--help' if this feature is available
in your environment or not (the python-gpgme package is required).  Please
note that you might need to set the GNUPGHOME environment variable.  If the
decryption key has a passphrase you have to make sure that the GPG_AGENT_INFO
environment variable has been set correctly and the passphrase is already
known by the gpg-agent.

The '--script' option specifies a custom script that is called whenever any
of the dirsyncAttributes (see below) was changed. The script is called
without any arguments. It gets the LDIF for exactly one object on STDIN.
If the script processed the object successfully it has to respond with a
single line starting with 'DONE-EXIT: ' followed by an optional message.

Note that the script might be called without any password change, e.g. if
the account was disabled (a userAccountControl change) or the
sAMAccountName was changed. The objectGUID,isDeleted,isRecycled attributes
are always returned as unique identifier of the account. It might be useful
to also ask for non-password attributes like: objectSid, sAMAccountName,
userPrincipalName, userAccountControl, pwdLastSet and msDS-KeyVersionNumber.
Depending on the object, some attributes may not be present/available,
but you always get the current state (and not a diff).

If no '--script' option is specified, the LDIF will be printed on STDOUT or
into the logfile.

The default filter for the LDAP_SERVER_DIRSYNC_OID search is:
(&(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=512)\
    (!(sAMAccountName=krbtgt*)))
This means only normal (non-krbtgt) user
accounts are monitored.  The '--filter' can modify that, e.g. if it's
required to also sync computer accounts.


Sync Loop Run
=============

This (default) mode runs in an endless loop waiting for password related
changes in the active directory database. It makes use of the
LDAP_SERVER_DIRSYNC_OID and LDAP_SERVER_NOTIFICATION_OID controls in order
get changes in a reliable fashion. Objects are monitored for changes of the
following dirsyncAttributes:

  unicodePwd, dBCSPwd, supplementalCredentials, pwdLastSet, sAMAccountName,
  userPrincipalName and userAccountControl.

It recovers from LDAP disconnects and updates the cache in conservative way
(in single steps after each successfully processed change).  An error from
the script (specified by '--script') will result in fatal error and this
command will exit.  But the cache state should be still valid and can be
resumed in the next "Sync Loop Run".

The '--logfile' option specifies an optional (required if '--daemon' is
specified) logfile that takes all output of the command. The logfile is
automatically reopened if fstat returns st_nlink == 0.

The optional '--daemon' option will put the command into the background.

You can stop the command without the '--daemon' option, also by hitting
strg+c.

If you specify the '--no-wait' option the command skips the
LDAP_SERVER_NOTIFICATION_OID 'waiting' step and exit once
all LDAP_SERVER_DIRSYNC_OID changes are consumed.

Sync Loop Terminate
===================

In order to terminate an already running command (likely as daemon) the
'--terminate' option can be used. This also requires the '--logfile' option
to be specified.


Example1:
samba-tool user syncpasswords --cache-ldb-initialize \
    --attributes=virtualClearTextUTF8
samba-tool user syncpasswords

Example2:
samba-tool user syncpasswords --cache-ldb-initialize \
    --attributes=objectGUID,objectSID,sAMAccountName,\
    userPrincipalName,userAccountControl,pwdLastSet,\
    msDS-KeyVersionNumber,virtualCryptSHA512 \
    --script=/path/to/my-custom-syncpasswords-script.py
samba-tool user syncpasswords --daemon \
    --logfile=/var/log/samba/user-syncpasswords.log
samba-tool user syncpasswords --terminate \
    --logfile=/var/log/samba/user-syncpasswords.log

z(%prog [--cache-ldb-initialize] [options])	sambaoptsversionoptsz--cache-ldb-initializez'Initialize the cache for the first timecache_ldb_initialize
store_true)helpdestactionz--cache-ldbz-optional LDB URL user-syncpasswords-cache.ldbzCACHE-LDB-PATH	cache_ldb)r   typemetavarr   z-Hz--URLz)optional LDB URL for a local ldapi serverURLHz--filterz'optional LDAP filter to set password onzLDAP-SEARCH-FILTERfilterz--attributesATTRIBUTELIST
attributes)r   r   r   r   z--decrypt-samba-gpgFdecrypt_samba_gpg)r   r   defaultr   z--scriptz.Script that is called for each password changez/path/to/syncpasswords.scriptscriptz	--no-waitzDon't block waiting for changesnowaitz	--logfilez/The logfile to use (required in --daemon mode).z/path/to/syncpasswords.loglogfilez--daemonzdaemonize after initial setupdaemonz--terminatez5Send a SIGTERM to an already running (daemon) process	terminateNc           
      F  ^ ^^^^^"^#^$^%^&^'^(^)^*^+^,^-^. UR                  5       T l        S T l        S T l        S T l        S T l        T(       dK  Ub  [        S5      eT(       a  [        S5      eUb  [        S5      eUb  [        S5      eTb  [        S5      eO>USLa  [        S5      eU	b  [        S5      eU
SLa  [        S	5      eUSLa  [        S
5      eUSL a   U
SL a  [        S5      eUSLa  [        S5      eUSL a  U
SL a  [        S5      eU
SL a  U	c  [        S5      eUSL a  U	c  [        S5      eUbU  [        R                  R                  U5      (       d  [        SU-  5      eS[        R                  R                  U5      -  m,OS m,Um$T$c,  SS[        R                  [        R                  4-  -   S-   S-   m$/ SQm&T&/ SQ-   m#S m*T(       a  Tc;  S[        R                  R                  T R                  R                  S5      5      -  mT(       a  [         (       d  [        ["        5      eT R%                  U5      m*T* Vs/ s H  oR'                  5       PM     nnS H  nUR'                  5       U;  d  M  T*U/-  m*M!     Tb  TR'                  5       R)                  S5      (       a  [        S5      eTR'                  5       R)                  S5      (       a  [        S5      eTR'                  5       R)                  S 5      (       a  [        S!5      eTR'                  5       R)                  S"5      (       a  O[[        R                  R                  T5      (       d  T R                  R                  T5      mOT R                  R                  S#5      mS$T-  T l        U)U 4S% jm)UUUUU#U$U)U*U U,4
S& jm(U)U 4S' jm+U&U)U+U 4S( jm'U)U 4S) jnU)U 4S* jnU 4S+ jm-U 4S, jm"U 4S- jm.U"U'U)U U-U.4S. jm%U%U)U 4S/ jnU(U)U 4S0 jnT(       a.  TT l        T R-                  T R                  SS19T l        T(" 5         g U	Gb9  S2S KnUR1                  UR2                  5      S3   nUUR4                  :X  a  S4n[        R6                  " U	[        R8                  [        R:                  -  [        R<                  -  S55      nT R>                  RA                  S6U	-  5        [C        S2U5       H"  nUU:X  a  M   [        RD                  " U5        M$     [        RH                  " US25        [        RH                  " US35        [        RH                  " US75        [        RD                  " U5        T)" S8U	-  5        U	T l        T(" 5         U" U5      nU(       a  T RJ                  c	  T)" S95        g U(       d  T)" S:T RJ                  -  5        U" S 5        g T)" S;T RJ                  -  5        [        RL                  " T RJ                  [N        RP                  5        g U(       a-  [        S<[        RR                  " 5       T RJ                  4-  5      eU
SL a  U" 5         U" [        RR                  " 5       5        SnUSL a  S3nS=nUSL a  SnS2nOUnT R                  cw  US2:w  a!  T)" S>U-  5        [T        RV                  " U5        US7-  nUU:  a  UnT)" S?T R                  -  5         T R-                  T R                  S@9T l        T R                  c  Mw   U" U5        USL a  M  U" S 5        g s  snf ! [F         a     GMK  f = f! [X         a"  nS T l        T)" SAU-  5        USLa  e  S nANfS nAff = f! [        RZ                   a-  nUR\                  u  n n!S T l        T)" SBU U!4-  5         S nANS nAff = f)CNzA--attributes is only allowed together with --cache-ldb-initializezH--decrypt-samba-gpg is only allowed together with --cache-ldb-initializez=--script is only allowed together with --cache-ldb-initializez=--filter is only allowed together with --cache-ldb-initializez=-H/--URL is only allowed together with --cache-ldb-initializeFz=--no-wait is not allowed together with --cache-ldb-initializez=--logfile is not allowed together with --cache-ldb-initializez<--daemon is not allowed together with --cache-ldb-initializez?--terminate is not allowed together with --cache-ldb-initializeTz/--daemon is not allowed together with --no-waitz2--terminate is not allowed together with --no-waitz1--terminate is not allowed together with --daemonz0--daemon is only allowed together with --logfilez3--terminate is only allowed together with --logfilezscript[%s] does not exist!%sz(&(objectClass=user)z(userAccountControl:%s:=%u)z(!(sAMAccountName=krbtgt*))))
unicodePwddBCSPwdsupplementalCredentials)
pwdLastSetsAMAccountNameuserPrincipalNameuserAccountControl	isDeleted
isRecycledz
ldapi://%szldap_priv/ldapi)
objectGUIDr6   r7   zldapi://z%--cache_ldb ldapi:// is not supportedzldap://z$--cache_ldb ldap:// is not supportedzldaps://z%--cache_ldb ldaps:// is not supportedztdb://zuser-syncpasswords-cache.ldbz%s.pidc                   > TR                   b  [        R                  " S5      nUR                  S:X  a  TR                   nS Tl         T" SU-  5        [        R                  " U[        R
                  [        R                  -  [        R                  -  S5      n[        R                  " US5        [        R                  " US5        [        R                  " US5        [        R                  " U5        T" SU-  5        UTl         S[        R                  " 5       [        R                  " 5       U 4-  n TR                  R                  U 5        g )Nr   z$Closing logfile[%s] (st_nlink == 0)
  r      zReopened logfile[%s]
z%s: pid[%d]: %s)r)   osfstatst_nlinkopenO_WRONLYO_APPENDO_CREATdup2closetimectimegetpidoutfwrite)msginfor)   logfdlog_msgselfs       O/usr/lib/python3/dist-packages/samba/netcmd/user/readpasswords/syncpasswords.pyrM   +cmd_user_syncpasswords.run.<locals>.log_msg  s    ||'xx{==A%"llG#'DLCwOPGGGR[[2;;-F-SUZ[EGGE1%GGE1%GGE1%HHUO4@A#*DL#JJLIIK' C IIOOC     c                  	  >
 / SQn [        T5      Tl        [        R                  " TR                  S5      Tl        TR                  R                  TR                  [        R                  U S9n[        U5      S:X  a   [        US   S   S   5      Tl	        OS Tl	        TR                  c  T(       d  [        ST-  5      eTR                  b  T(       a  [        ST-  5      eTR                  Gc:  T
Tl	        TTl        TTl        S	S
/Tl        TTl        TTl        TTl        STR                  -  S-   S[$        R&                  " [)        TR                  5      5      R+                  S5      -  -   S[$        R&                  " [)        TR                  5      5      R+                  S5      -  -   SR-                  S TR                   5       5      -   STR                  S   -  -   SR-                  S TR                   5       5      -   nTR                   (       a  US-  nOUS-  nTR"                  b  USTR"                  -  -  nUS[        R.                  " [1        [2        R2                  " 5       5      5      -  -  nTR                  R5                  U5        S Tl        TR8                  R;                  ST-  5        TR                  R=                  U5      n[?        U5      u  pVTR                  RA                  U[        RB                  5      nTR8                  R;                  SU-  5        g [        US   S   S   5      Tl        / Tl        US   S    H'  nTR                  RE                  [        U5      5        M)     [        US   S   S   5      S
/Tl        / Tl        US   S    H'  nTR                  RE                  [        U5      5        M)     [        US   S   S   5      n	U	S;   d   eU	S :X  a  S!Tl        OS"Tl        S#US   ;   a  [        US   S#   S   5      Tl        OS Tl        S$US   ;   a  [1        US   S$   S   5      Tl        OS Tl        T" S%T-  5        g ! [         a  nS Tl	         S nAGNS nAff = f)&N)samdbUrldirsyncFilterdirsyncAttributedirsyncControlpasswordAttributedecryptSambaGPGsyncCommand
currentPidzKEY=USERSYNCPASSWORDS)basescopeattrsr   r   rS   zHcache_ldb[%s] not initialized, use --cache-ldb-initialize the first timezEcache_ldb[%s] already initialized, --cache-ldb-initialize not allowedzdirsync:1:0:0extended_dn:1:0dn: %s
zobjectClass: userSyncPasswords
zsamdbUrl:: %s
utf8zdirsyncFilter:: %s
 c              3      #    U  H6  nS [         R                  " [        U5      5      R                  S5      -  v   M8     g7f)zdirsyncAttribute:: %s
r`   Nbase64	b64encoder   decode.0as     rO   	<genexpr>Acmd_user_syncpasswords.run.<locals>.load_cache.<locals>.<genexpr>  sB       #J  wIqr#<v?O?OPYZ[P\?]?d?dek?l#l  wI   >A dirsyncControl: %s
c              3      #    U  H6  nS [         R                  " [        U5      5      R                  S5      -  v   M8     g7f)zpasswordAttribute:: %s
r`   Nrc   rg   s     rO   rj   rk     sB       #L  xKrs#=@P@PQZ[\Q]@^@e@efl@m#m  xKrl   zdecryptSambaGPG: TRUE
zdecryptSambaGPG: FALSE
syncCommand: %s
currentTime: %s
zInitialized cache_ldb[%s]
r-   rT   rU   rV   rW   rX   )TRUEFALSErq   TFrY   rZ   zUsing cache_ldb[%s]
)#r   cacheldbDncache_dnsearch
SCOPE_BASElenstr	samdb_urlKeyErrorr   dirsync_filterdirsync_attrsdirsync_controlspassword_attrsr%   sync_commandrd   re   r   rf   join
timestringintrE   add_ldifcurrent_pidrH   rI   
parse_ldifnext
write_ldifCHANGETYPE_NONEappend)cache_attrsreser   msgs
changetyperJ   ldifri   decrypt_stringr!   r   r   r%   r~   r}   rM   r   rN   r   s             rO   
load_cache.cmd_user_syncpasswords.run.<locals>.load_cache  sl   	K YDJFF4::/FGDM**##cnn*5 $ 7C3x1}*%(Q
);A)>%?DN "&~~%.B"#m#,$. / /~~).B"#j#,$. / /~~%!"&4#%2")8:K(L%&4#):&$0!%5=>,v/?/?	$..@Y/Z/a/abh/iij 2F4D4DYtObObEc4d4k4klr4sst 77  #Jvz  wI  wI  #J  J	J
 2D4I4I!4LLM 77  #Lw{  xK  xK  #L  LL )) 99H ::H$$0 3d6G6G GGH/#..TYY[AQ2RRR

##H-#' 		 = KLzz,,X6"&t*
zz,,S#2E2EF		t,4 1 '*#a&*A!*D&E#%'"Q 23A&&--c!f5 4),SV4D-Ea-H)IK\(]%&(#Q 34A''..s1v6 5!$SV,=%>q%A!B%)::;:!V+-1D*-2D* CF*(+CF=,A!,D(ED%(,D%3q6)'*3q6,+?+B'CD$'+D$/9=>A   *%)DNN*s   ?Q% %
R/Q<<Rc                   > T" STR                   < SU < S35        [        TR                   [        [        [        S9nUR	                  5       nUb   eSU-  nUR                  UR                  S5      5      S   R                  S5      nT" SU-  5        UR	                  5       nUc  UR                  5         UR                  5       nUR                  S	5      (       a  g T" S
U-  5        [        SU< SU< S35      e)NzCall Popen[z] for 
)stdinstdoutstderrr-   zutf-8r   z%s
zDONE-EXIT: zRESULT: %s
zERROR: z - )r   r   r   r   pollcommunicateencoderf   r+   wait
startswith	Exception)dnr   sync_command_pr   inputreplyrM   rN   s         rO   run_sync_command4cmd_user_syncpasswords.run.<locals>.run_sync_command  s    1B1BBGH"4#4#4)-*.*02N
 !%%'C;;DME"..W%''(**0&/ Fe$% %%'C{((* %%'C..Nc*+#u=>>rQ   c           
      @  > UR                   R                  S5      n[        [        R                  U5      nUR                   R                  S5      n[        [
        R                  U5      nUR                  5       u  pgU[
        R                  :X  a  T" SU -  5        g [        UR                  5       5       H;  nT H2  n	UR                  5       U	R                  5       :X  d  M'  X	 S/USU-  '   M4     M=     TR                  R                  U[        R                  5      n
T" SXXZ4-  5        TR!                  TR                  SU-  SU-  S	[        R"                  TR$                  TR&                  S
9nTR                  R                  U[        R                  5      nT" SXU4-  5        TR(                  c  TR*                  R-                  SU-  5        g TR*                  R-                  S[/        UR                  5       5      -  5        T" UR                   U5        g )NGUIDSIDz'# Dirsync[%d] SKIP: DOMAIN_RID_KRBTGT

zREDACTED SECRET ATTRIBUTEz# %s::z# Dirsync[%d] %s %s
%sr-   z	<GUID=%s>z(objectClass=user))usernamebasednr"   r\   r]   decryptz# Passwords[%d] %s %s
z# attrs=%s
)r   get_extended_componentr
   r   r   r	   dom_sidsplitDOMAIN_RID_KRBTGTlistkeyslowersamdbr   rt   r   get_account_attributesrx   r   r%   r   rH   rI   sorted)idxdirsync_objbinary_guidguid
binary_sidsid
domain_sidridri   hdirsync_ldifobjr   dirsync_secret_attrsrM   r   rN   s                rO   handle_object1cmd_user_syncpasswords.run.<locals>.handle_object  s   %..??GKdii5D$>>uEJX--z:C!iikOJh000CsKL+**,--AwwyAGGI-'N5P4QHqL1 . .
  ::00c>Q>QRL-C0NNO--djj7;cz5@45G5I47NN484G4G6:6L6L . NC ::((c.A.ABD-C0@@A  (		.IIOONfSXXZ.@ABSVVT*rQ   c                 t  > [         R                  nU (       d  U[         R                  -  n [         R                  " TR                  US5      Tl        Sn [        R                  " TR
                  [        R                  [        R                  -  5        SnU(       dG  [         R                   " TR
                  S5      nS Tl         [%        U5      Tl        TR"                  b  gU(       aQ  U (       aJ   [         R(                  " TR
                  S5        [         R*                  " TR
                  5        S	Tl        g [        R                  " TR
                  [        R,                  5        g! [         aK  nUR                  u  p4U[        R                  :X  a  U (       a   S nAgT" STR                  XC4-  5        e S nAff = f! [         aY  nUR                  u  p4U[        R                  :w  a,  U[        R                  :w  a  T" STR                  XC4-  5        e  S nAGNS nAff = f! [&         a  n S nAGNeS nAff = f! [         a+  n	U	R                  u  p4T" STR                  XC4-  5        e S n	A	ff = f! [         a/  n
U
R                  u  p4T" S
TR                  XC4-  5         S n
A
gS n
A
ff = f)Nr:   Fz8check_current_pid_conflict: failed to open[%s] - %s (%d)TzFcheck_current_pid_conflict: failed to get exclusive lock[%s] - %s (%d)@   r   z=check_current_pid_conflict: failed to truncate [%s] - %s (%d)zCcheck_current_pid_conflict: failed to get shared lock[%s] - %s (%d))r<   O_RDWRrB   r?   lockfilelockfdIOErrorargserrnoENOENTfcntllockfLOCK_EXLOCK_NBEACCESEAGAINreadr   r   
ValueError	ftruncaterD   LOCK_SH)r+   flagse4errrJ   got_exclusivee5bufr   e2e6rM   rN   s              rO   check_current_pid_conflict>cmd_user_syncpasswords.run.<locals>.check_current_pid_conflict$  s8   IIE#	 ggdmmUEB "MDKK)FG $ !ggdkk2.#' '*3xD$ ##/LLa0 % 3DKK7 e  WW
%,,& $R12 3  WW
%,,&3%,,+>d!]]C56 7 "   !#JS[!]]C56 7	  3WW
]12 3 3 3sy   'E0 AG 	H. 6!I  /I> 0
G:)G (G  G
H+AH&&H+.
I>I
I;&I66I;>
J7%J22J7c           	        > T
R                   S:w  Ga  Sn[        SS5       He  n [        R                  " T
R                   [        R                  [        R
                  -  5        SnU(       a    O[        R                  " S5        Mg     U(       d9  T	" SU < S	T
R                  < S
35        [        SU < S	T
R                  < S35      eU b  SU -  nOS n [        R                   " T
R                   S5        Ub*  [        R"                  " T
R                   [%        U5      5        U T
l        T
R&                  b  T	" ST
R&                  -  5        ST
R(                  -  S-   S-   nT
R&                  b  UST
R&                  -  -  nUSS[*        R,                  " [/        [        R                  " 5       5      5      -  -   -  nT
R0                  R3                  U5        g ! [         aZ  nUR                  u  pEU[        R                  :w  a-  U[        R                  :w  a  T	" SU T
R                  XT4-  5        e  S nAGNS nAff = f! [         a+  nUR                  u  pET	" ST
R                  XT4-  5        e S nAff = f)Nr   Fr      Tz:update_pid(%r): failed to get exclusive lock[%s] - %s (%d)r   zupdate_pid(z ): failed to get exclusive lock[]z] after 5 secondsz%d
zAcheck_current_pid_conflict: failed to write pid to [%s] - %s (%d)zcurrentPid: %d
r_   changetype: modify
zreplace: currentPid
replace: currentTime
rp   )r   ranger   r   r   r   r   r   r   r   r   r   rE   sleepr   r<   r   rI   r   r   rv   rt   r   r   rs   modify_ldif)pidr   ir   r   rJ   r   e3r   rM   rN   s            rO   
update_pid.cmd_user_syncpasswords.run.<locals>.update_pid_  s"   {{b  %q!A"DKK1NO(, %JJqM % % $--1 2&(+T]](< = = ? 3,CCLLa0in=  #D+*T-=-==>$60112K +1T5E5EFF3.DIIK@P1QQR RKJJ"";/S # "%&VV
%,,.3%,,3F#$`%($--$B%C D!"0  !#JS_!]]C56 7	s2   AGAH? 
H<"AH77H<?
I4	&I//I4c                   > [        U 5      S:  d   eU S   R                  S:X  d   eSU S   l        [        U S   5      S/Tl        STR
                  -  S-   S-   STR                  S   -  -   S	-   S
[        R                  " [        [        R                  " 5       5      5      -  -   nTR                  R                  U5        g )Nr   1.2.840.113556.1.4.841Tr^   r_   r   zreplace: dirsyncControl
rm   r   rp   )ry   oidcriticalrz   r   rv   rt   r   r   rE   rs   r   )res_controlsr   rN   s     rO   update_cache0cmd_user_syncpasswords.run.<locals>.update_cache  s    |$q(((?&&*BBBB'+LO$%(a%9;L$MD! %60156 1D4I4I!4LMN 3	3
 .s499;?O0PPQK JJ"";/rQ   c                   > [        U5      S:  d   eUS   R                  S:X  d   eU R                  R                  S5      n[	        [
        R                  U5      nSU-  n[        US   5      nTR                  R                  U[        R                  S[        R                  " U5      -  / S9n[        U5      S:X  a  gg	)
Nr   r   r   KEY=%sz(lastCookie=%s)r[   r\   
expressionr]   r   TF)ry   r   r   r   r
   r	   r   rz   rs   rw   rt   rx   binary_encode)r   r   r   r   r   
lastCookier   rN   s          rO   check_object0cmd_user_syncpasswords.run.<locals>.check_object  s    |$q(((?&&*BBBB$>>uEJX--z:CCB\!_-J**##3>>/@(+(9(9*(E0G*, $ .C 3x1}rQ   c                   > [        U5      S:  d   eUS   R                  S:X  d   eU R                  R                  S5      n[	        [
        R                  U5      nSU-  n[        US   5      nT
R                  R                  5          T
R                  R                  U[        R                  SS/S9n[        U5      S:X  a`  SU-  S	-   S
U-  -   S[        R                  " [        [        R                  " 5       5      5      -  -   nT
R                  R!                  U5        OeSU-  S-   S-   S
U-  -   S-   S[        R                  " [        [        R                  " 5       5      5      -  -   nT
R                  R#                  U5        T
R                  R%                  5         g ! [&         a$  n	T
R                  R)                  5          S n	A	g S n	A	ff = f)Nr   r   r   r   z(objectClass=*)r   r   r_   zobjectClass: userCookie
zlastCookie: %s
rp   r   zreplace: lastCookie
r   )ry   r   r   r   r
   r	   r   rz   rs   transaction_startrw   rt   rx   r   r   rE   r   r   transaction_commitr   transaction_cancel)r   r   r   r   r   r   r   r   r   r   rN   s             rO   update_object1cmd_user_syncpasswords.run.<locals>.update_object  s   |$q(((?&&*BBBB$>>uEJX--z:CCB\!_-JJJ((*0jj''Rs~~3D/;n ( > s8q=)R0: ;1Z@ A  3S^^C		DT5UU VH JJ''1","3"8#9"9#: #5
"C#D #;	#;
 #6s499;GW8X"X#YK JJ**;7

--/   0

--//0s   DF% %
G/GGc                    >  TR                   R                  [        TR                  5      [        R
                  TR                  TR                  S9n T" S[        U 5      -  5        SnU  H;  nT" X R                  5      nU(       d  T" X5        T	" X R                  5        US-  nM=     T" U R                  5        [        U 5      S:X  a  g M  )N)r   r\   r]   controlszdirsync_loop(): results %d
r   r   )
r   rw   rz   r}   rt   SCOPE_SUBTREEr~   r   ry   r  )
r   rirdoner   r   rM   rN   r   r  s
       rO   dirsync_loop0cmd_user_syncpasswords.run.<locals>.dirsync_loop  s    jj''3t7J7J3K.1.?.?.2.@.@151F1F ( H 6SABA'<<8D%b,%a6!GB  S\\*s8q= rQ   c           
        > / SQnSS/nT
R                   R                  S[        R                  UUSS9nU SL a	  T	" S5        OT	" S	5        T
R                  R                  S
T
R                  -  5        T
R                  R                  ST
R                  -  5        T
R                  R                  ST
R                  -  5        T" 5         U SLa  g U H  n[        U[        R                  5      (       d   T
R                  R                  SU-  5        MB  UR                  S5      S   nUR                  S5      S   nT	" SUR                  < SU< SU< S35        T" 5         M     UR                  5       ng )N)name
uSNCreated
uSNChangedobjectClassznotification:1zshow_recycled:1zobjectClass=*r   )r   r\   r]   r  timeoutTzResuming monitoring
zGetting changes
zdirsyncFilter: %s
zdirsyncControls: %r
ro   zreferral: %s
r  r   r  z	# Notify z uSNCreated[z] uSNChanged[z]
)r   search_iteratorrt   r  rH   rI   r}   r   r   
isinstanceMessagegetr   result)r   notify_attrsnotify_controlsnotify_handlerJ   createdchangedr   r
  rM   rN   s           rO   	sync_loop-cmd_user_syncpasswords.run.<locals>.sync_loop  sI   NL/1BCO JJ66/=@=N=N=I@O?A	 7 CM t|/0+,IIOO1D4G4GGHIIOO3d6K6KKLIIOO/$2C2CCDN4$!#s{{33IIOO$4s$:;'',/2'',/2'3 4  %  &&(CrQ   c                  `  > S Tl         S Tl        [        R                  " 5       n [        R                  " 5       nUS:X  aY  [        R
                  " 5         [        R                  " 5       nUS:X  a)  [        R                  " 5       nT" SX4-  5        T" 5         g [        R                  " S5        g )Nr   zDaemonized as pid %d (from %d)
)r   rs   r<   rG   forksetsid_exit)orig_pidr   r   rM   rN   s     rO   	daemonize-cmd_user_syncpasswords.run.<locals>.daemonize
  sw    DJDJyy{H'')Cax		ggi!8))+C>#PQLHHQKrQ   )urlverboser   r   i   r:   zUsing logfile[%s]
r;   zAttached to logfile[%s]
zNo process running.
z#Process %d is not running anymore.
zSending SIGTERM to process %d.
z4Exiting pid %d, command is already running as pid %diX  z Wait before connect - sleep(%d)
zConnecting to '%s'
)r%  z#Connect to samdb Exception => (%s)
zldb.LdbError(%d) => (%s)
)/get_loadparmlpr)   r{   r   rs   r   r<   pathexistsabspathrt   OID_COMPARATOR_ANDr   UF_NORMAL_ACCOUNTprivate_pathr   r   parse_attributesr   r   r   connect_for_passwordsresource	getrlimitRLIMIT_NOFILERLIM_INFINITYr?   r@   rA   rB   rH   rI   r   rD   OSErrorrC   r   killsignalSIGTERMrG   rE   r   r   LdbErrorr   )/rN   r   r   r!   r"   r$   r%   r'   r(   r)   r*   r+   r   r   xlower_attrsri   r   r   r  r#  r1  maxfdrL   fdconflictr   retry_sleep_minretry_sleep_maxretry_sleeprJ   e7enumestrr   r~   r}   r
  r   r   r   rM   r   r   r   r   r  s/   ````  `                           @@@@@@@@@@@@@rO   runcmd_user_syncpasswords.run  s    ((*

#%"#fgg "#mnn!"#bcc!"#bcc}"#bcc  U""#bcc""#bccU""#abb%"#deeT>~"#TUU%"#WXX4RSST>goQRRTUU77>>&))"#?&#HII"''//&"99LL!4<#&#9#94;Q;Q@S  SS  =	=
 !!N 
 - 0
 
 y 277??4773G3GHY3Z#[[ "#9::!22:>N.<=n779nK=>779K/"qc)N ?   ++J77"#JKK"--i88"#IJJ"--j99"#JKK"--h77ww~~i00 $ 4 4Y ?I,,-KLI 9,	*S	 S	j	?2	+ 	+>9	v1	f	"	" 	D	 	$ 	)D	  DN33<@ 4 BDJL&&x'='=>qAE...GGGR[[2;;%>%KUSEIIOO1G;<Auo;HHRL	 & GGE1GGE1GGE1HHUO/7;<"DL-i8'/0>((* +4 6$$& 'GGD$$fnn5U!yy{D,<,<Y>  > ? ? T>K299;dlO!O~-**$!#?+MNJJ{+)Ao/1"1K.?@!%!;!;!;!ODJ **$ E$5 dl@ 	4G >B  h ! !%DJBSHI4' ( << E!wwt!
4d|CDDEsH   \-\\0 ] 
\-,\-0
]:]]^ 3#^^ )rs   rv   r   r%   r~   r   r}   r   r   r)   r(  r   r   r{   r   )FNNNNNNNNNNNN)__name__
__module____qualname____firstlineno____doc__synopsisoptionsSambaOptionsVersionOptionstakes_optiongroupsr   rz   r   r   takes_optionsrE  __static_attributes__ rQ   rO   r   r   .   s>   {z :H ))-- 	'=*<	A 	}#RY\'k	;tW#NUX3	(z IPS+(	<~C+&\	; 	$*"E8K	M 	z PWZ6X	G{!B"E	B{E3)	E 	z ?"E	B}K"E	E3M< 9=/3KO(,	]	rQ   r   )!rd   r   r   r<   r7  rE   
subprocessr   r   r   rt   samba.getoptgetoptrM  sambar   r   samba.dcerpcr   r	   	samba.ndrr
   samba.commonr   samba.netcmdr   r   commonr   r   r   r   r   rS  rQ   rO   <module>r]     sM   ,    	   * * 
   '   " - @/ @rQ   