
    b                        d dl mZ d dlmZmZmZm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 d dlmZmZmZmZmZmZmZmZmZmZmZmZmZ d dl m!Z! d d	l"m#Z#m$Z$ d d
l%m&Z&m'Z' d dl(Z(d dl)Z)ddl*m+Z+  e,eeeed          Z- e,eeed          Z. G d de          Z/ G d de          Z0 G d de          Z1 G d de          Z2 G d de          Z3 G d de          Z4 G d de          Z5 G d de          Z6 G d d e          Z7 G d! d"e          Z8 G d# d$e          Z9 G d% d&e          Z: G d' d(e          Z;dS ))    N)CommandSuperCommandCommandErrorOption)ndr_pack
ndr_unpack)security)system_session)SamDB)ATYPE_SECURITY_GLOBAL_GROUPDS_GUID_USERS_CONTAINER"GTYPE_SECURITY_BUILTIN_LOCAL_GROUP!GTYPE_SECURITY_DOMAIN_LOCAL_GROUPGTYPE_SECURITY_GLOBAL_GROUPGTYPE_SECURITY_UNIVERSAL_GROUP%GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUPGTYPE_DISTRIBUTION_GLOBAL_GROUP"GTYPE_DISTRIBUTION_UNIVERSAL_GROUPSYSTEM_FLAG_DISALLOW_DELETE SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE"SYSTEM_FLAG_DOMAIN_DISALLOW_RENAMEUF_ACCOUNTDISABLE)defaultdict)
check_callCalledProcessError)	get_bytesnormalise_int32   )common)BuiltinDomainGlobal	Universalr!   r"   r#   c                   r   e Zd ZdZdZej        ej        ej        dZ	 e
dddedd	           e
d
de           e
ddg dd           e
ddddgd           e
dde           e
dde           e
dde           e
dde           e
dde           e
d d!d"d#$          g
Zd%gZ	 	 	 	 d(d'Zd&S ))cmd_group_adda  Creates a new AD group.

This command adds a new Active Directory group.  The groupname specified on the command is a unique sAMAccountName.

An Active Directory group may contain user and computer accounts as well as other groups.  An administrator adds a new group and adds members to that group so they can be managed as a single entity.  This helps to simplify security and system administration.

Groups may also be used to establish email distribution lists, using --group-type=Distribution.

Groups are located in domains in organizational units (OUs).  The group's scope is a characteristic of the group that designates the extent to which the group is applied within the domain tree or forest.

The group location (OU), type (security or distribution) and scope may all be specified on the samba-tool command when the group is created.

The command may be run from the root userid or another authorized userid.  The
-H or --URL= option can be used to execute the command on a remote server.

Example1:
samba-tool group add Group1 -H ldap://samba.samdom.example.com --description='Simple group'

Example1 adds a new group with the name Group1 added to the Users container on a remote LDAP server.  The -U parameter is used to pass the userid and password of a user that exists on the remote server and is authorized to issue the command on that server.  It defaults to the security type and global scope.

Example2:
sudo samba-tool group add Group2 --group-type=Distribution

Example2 adds a new distribution group to the local server.  The command is run under root using the sudo command.

Example3:
samba-tool group add Group3 --nis-domain=samdom --gid-number=12345

Example3 adds a new RFC2307 enabled group for NIS domain samdom and GID 12345 (both options are required to enable this feature).
%prog <groupname> [options]	sambaoptsversionoptscredopts-H--URL%LDB URL for database or target serverURLHhelptypemetavardestz	--groupouzpAlternative location (without domainDN counterpart) to default CN=Users in which new user object will be createdr2   r3   z--group-scopechoicer$   z)Group scope (Domain | Global | Universal))r3   choicesr2   z--group-typeSecurityDistributionz$Group type (Security | Distribution)z--descriptionzGroup's description--mail-addresszGroup's email addressz--noteszGroups's notesz--gid-numberzGroup's Unix/RFC2307 GID numberz--nis-domainzSFU30 NIS Domainz	--specialzAdd a special predefined group
store_trueF)r2   actiondefault	groupnameNc           
         |pddk    r!t                               |t                    }n t                              |t                    }||||t          d          |                                }|                    |d          }	 t          |t                      ||          }n&# t          $ r}t          d| d|          d }~ww xY w|rEd }|d}n|d	}n|	d
}n|
d}n|d}n	|d}n|d}|t          d| d          |                                st          d          d                                dt          t          j        dfi}|                    |                                          }|t          d| d          |\  }}}}	t          |          }|                                }|t          k    rKt          j        |                                 d|           }d }|s|                    |t(                    }nr|t*          k    rUt          j        d|           }t,          t.          z  t0          z  }|s$|                    d          st5          d          nt5          d|           |r(|                    |          st          d| d          |                    d|           st          d| d          ||d||	t7          |          dd }|||d!<   	 |                    |d"g#           | j                            d$| d%           n# t>          j         $ r}|j!        \  }}|t>          j"        k    r	 |#                    d&| d'd(g)          }n'# t>          j         $ r t          d| d|          w xY wtI          |          d*k    rt          d| d|          |d+                             d(d+,          }|rd-| d} nd.} t          d| d/|  d0|d+         j%         d          |t>          j&        k    r	 |#                    |t>          j'        g d12          }n[# t>          j         $ rI 	 |#                    d3| d'g d1)          }n'# t>          j         $ r t          d| d|          w xY wY nw xY wtI          |          d*k    rt          d| d|          |d+                             d(d+,          }!|!rd4|! d}"nd5}"|d+                             d6d+,          (                    d7          }#||#k    r$t          d| d8|" d0|d+         j%         d9          |d+                             d:d+,          }$|$$t          d| d;|" d0|d+         j%         d<          tS          t          j        |$          }$|$|k    r$t          d| d=|" d0|d+         j%         d>          t          d| d;|" d0|d+         j%         d?          t          d| d|          d }~ww xY wd S 	 |*                    ||||	|
|||@           n%# t          $ r}t          dA|z  |          d }~ww xY w| j                            dB|z             d S )CNr9   zcBoth --gid-number and --nis-domain have to be set for a RFC2307-enabled group. Operation cancelled.Tfallback_machineurlsession_infocredentialslpzFailed to add group ""zgroup-scopez
group-typedescriptionzmail-addressnotesz
gid-numberz
nis-domainzSuperfluous option --z specified with --specialz8Adding special groups is only permitted against the PDC!zProtected UserszaMembers of this group are afforded additional protections against authentication security threatszUnknown special group "z".-z	S-1-5-32-z
CN=Builtinz Error getting Builtin objects DNzUnknown group type zInvalid group OU "zCN=zInvalid group name "groupTRUE)dnsAMAccountNameobjectClass	groupTyperI   	objectSidisCriticalSystemObjectsystemFlagszrelax:0)controlszAdded group 
z(objectSid=)rO   )
expressionattrsr   r   idxz with name " z " - Special group already existsz at ")rO   rR   rQ   )basescoperY   z(sAMAccountName=znamed "zwith no namerQ   zutf-8z" - An object z" already exists, but it is not a security group. Rename or remove this existing object before attempting to add this special group.rR   z" - A security group zx" already exists, but it lacks a SID. Rename or remove this existing object before attempting to add this special group.z" - The security group z" already exists.z" already exists, but it has the wrong SID, and will not function as expected. Rename or remove this existing object before attempting to add this special group.)groupou	grouptyperI   mailaddressrJ   	gidnumber	nisdomainzFailed to add group "%s"zAdded group %s
)+security_groupgetr   distribution_groupr   r   get_loadparmget_credentialsr   r
   	Exceptionam_pdclowerr	   DOMAIN_RID_PROTECTED_USERSr   get_default_basedndom_sidget_domain_sidget_wellknown_dnr   r   r   r   r   	add_childRuntimeErrorr   addoutfwriteldbLdbErrorargsERR_CONSTRAINT_VIOLATIONsearchlenrN   ERR_ENTRY_ALREADY_EXISTS
SCOPE_BASEdecoder   newgroup)%selfr?   r+   r)   r*   r0   r_   group_scope
group_typerI   mail_addressrJ   
gid_number
nis_domainspecialgtyperG   credssamdbeinvalid_optionspecial_groupsspecial_groupridgroup_dn
object_sidsystem_flagsmsgnumestrresname	with_namegot_namenamedgot_group_typesids%                                        4/usr/lib/python3/dist-packages/samba/netcmd/group.pyrunzcmd_group_add.runv   s	   
 $*33"&&{4OPPEE&**;8WXXE:#9z?UZdZl   E  F  F  F##%%((d(CC	Han.>.>&+4 4 4EE 	H 	H 	HCyCCCQGGG	H  r	!N&!.'!-(!.)!/"!('!-'!-)" $?> $? $? $? @ @ @ <<>> 7" $6 7 7 7 "''))%/7J	,K	
N +..y/@/@AAM$"#JY#J#J#JKKK1>.Iuc;(//J//11H333%-++--55557 7
# O$55h6M O  OH <<<%-.?#.?.?@@
 @ B!C ;!<  Kx'9'9,'G'G K&'IJJJ"#@#@#@AAA Dx11':: D"#B#B#B#BCCC%%&7I&7&788 H"#F)#F#F#FGGG "+&'*%j11*0 C '%1M"[>		#	444t 	 <y < < <====s < WP WP WPF	T#666E#ll'BZ'B'B'B#3"4 + 6 6 < E E E*@I@@@!E E EE 3xx1}}*@I@@@!E E E q6::&6A:>>D '$:4$:$:$:		$&	&G	 G G)2G G9<QG G GH H H C888I#ll142? 2? 2? + @ @
 < 	I 	I 	II"',,+Ji+J+J+J'4 '4 '4 #/ #5 #5CC
  #| I I I". D	 D D Da#I #I II  C	I 3xx1}}*@I@@@!E E E  #1vzz*:zBBH / 5( 5 5 5 .%(VZZ45 &0 &7 &77=vg #!^33*.I . .$. .+.q69. . ./ / / a&**[a*88C{*.I . .%*. .14Q. . ./ / / ))93??*,,".!3	 !3 !327!3 !3>A!fi!3 !3 !3#4 #4 4
 #/!I	 !I !I27!I !I>A!fi!I !I !I#J #J J ''Ky'K'K'KQOOOoWPv F	JNN9g'2TY%/:  G G G G  	J 	J 	J9IEqIII	J 		*Y677777s   B- -
C7CC=L5 5XX M=<X=$N!!BX)$QXR&Q<;R&<$R  R&#X%R&&E+XXX: :
YYY)NNNNNNNNNNNNF)__name__
__module____qualname____doc__synopsisoptionsSambaOptionsVersionOptionsCredentialsOptionstakes_optiongroupsr   strinttakes_options
takes_argsr        r   r&   r&   :   s        > -H )-.  	tW#JQT3	( 	( 	({ G	 	 	 	X7X7X7X?	A 	A 	A~Hz>6R:	< 	< 	<%:EEE&=CHHHy/c:::~$ECPPP~$6SAAA{!A,`efffM$ J6:@DjnR8 R8 R8 R8 R8 R8r   r&   c                   r    e Zd ZdZdZej        ej        ej        dZ	 e
dddedd	          gZd
gZddZdS )cmd_group_deletea  Deletes an AD group.

The command deletes an existing AD group from the Active Directory domain.  The groupname specified on the command is the sAMAccountName.

Deleting a group is a permanent operation.  When a group is deleted, all permissions and rights that users in the group had inherited from the group account are deleted as well.

The command may be run from the root userid or another authorized userid.  The -H or --URL option can be used to execute the command on a remote server.

Example1:
samba-tool group delete Group1 -H ldap://samba.samdom.example.com -Uadministrator%passw0rd

Example1 shows how to delete an AD group from a remote LDAP server.  The -U parameter is used to pass the userid and password of a user that exists on the remote server and is authorized to issue the command on that server.

Example2:
sudo samba-tool group delete Group2

Example2 deletes group Group2 from the local server.  The command is run under root using the sudo command.
r'   r(   r,   r-   r.   r/   r0   r1   r?   Nc                 D   |                                 }|                    |d          }t          |t                      ||          }dt	          j        |          z  }		 |                    |                                t          j        |	dg          }
|
d         j	        }n # t          $ r t          d|z            w xY w	 |                    |           n%# t          $ r}t          d	|z  |          d }~ww xY w| j                            d
|z             d S )NTrA   rC   )(&(sAMAccountName=%s)(objectClass=group))rN   r]   r^   rX   rY   r   Unable to find group "%s"zFailed to remove group "%s"zDeleted group %s
)rg   rh   r   r
   rv   binary_encoderz   	domain_dnSCOPE_SUBTREErN   
IndexErrorr   deleteri   rt   ru   )r   r?   r+   r)   r*   r0   rG   r   r   filterr   r   r   s                r   r   zcmd_group_delete.runn  sX   ##%%((d(CC!.*:*:"'B0 0 0 >#I../	J,,EOO$5$5%(%6*0&*V  - -C 1vyHH 	J 	J 	J:iHIII	J	MLL"""" 	M 	M 	M<yH!LLL	M 		,y899999s%   #AB' 'CC 
D (C;;D NNNNr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   K  s         & -H )-.  	tW#JQT3	( 	( 	(M
 J: : : : : :r   r   c                       e Zd ZdZdZej        ej        ej        dZ	 e
dddedd	           e
d
ded           e
ddde           e
dde          gZddgZ	 	 	 	 	 	 	 	 ddZdS )cmd_group_add_membersa  Add members to an AD group.

This command adds one or more members to an existing Active Directory group. The command accepts one or more group member names separated by commas.  A group member may be a user or computer account or another Active Directory group.

When a member is added to a group the member may inherit permissions and rights from the group.  Likewise, when permission or rights of a group are changed, the changes may reflect in the members through inheritance.

The member names specified on the command must be the sAMaccountName.

Example1:
samba-tool group addmembers supergroup Group1,Group2,User1 -H ldap://samba.samdom.example.com -Uadministrator%passw0rd

Example1 shows how to add two groups, Group1 and Group2 and one user account, User1, to the existing AD group named supergroup.  The command will be run on a remote server specified with the -H.  The -U parameter is used to pass the userid and password of a user authorized to issue the command on the remote server.

Example2:
sudo samba-tool group addmembers supergroup User2

Example2 shows how to add a single user account, User2, to the supergroup AD group.  It uses the sudo command to run as root when issuing the command.
F%prog <groupname> (<listofmembers>]|--member-dn=<member-dn>) [options]r(   r,   r-   r.   r/   r0   r1   --member-dnzRDN of the new group member to be added.
The --object-types option will be ignored.appendr2   r3   r=   --object-typesComma separated list of object types.
The types are used to filter the search for the specified members.
Valid values are: user, group, computer, serviceaccount, contact and all.
Default: user,group,computeruser,group,computerr2   r>   r3   --member-base-dn:Base DN for group member search.
Default is the domain DN.r6   r?   listofmembers?Nc
           	      L   |                                 }
|                    |
d          }|%|#|                                  t          d          	 t	          |t                      ||
          }g }|||z  }|||                    d          z  }|	                    d          }||                    |          }|                    ||d||           n*# t          $ r}t          d|d|d	|          d }~ww xY w| j
                            d
|z             d S )NTrA   6Either listofmembers or --member-dn must be specified.rC   ,add_members_operationmember_typesmember_base_dnzFailed to add members z to group "z" - zAdded members to group %s
rg   rh   usager   r   r
   splitnormalize_dn_in_domainadd_remove_group_membersri   rt   ru   r   r?   listofmembersr+   r)   r*   r0   r   	member_dnobject_typesrG   r   r   groupmembersgroup_member_typesr   s                   r   r   zcmd_group_add_members.run  s    ##%%((d(CC!6JJLLLHJ J J	-an.>.>&+4 4 4EL$	)( 3 3C 8 88!-!3!3C!8!8)!&!=!=n!M!M**9lAE8J:H + J J J J  	- 	- 	-,iii , - - -	- 		5	ABBBBBs   BC 
D'C??DNNNNNNNr   r   r   r   r   r   r     s        & XH )-.  	tW#JQT3	( 	( 	(}B		  	  	 
 	4 -	 	 	 	!1	 	 	#M. /0J .(C (C (C (C (C (Cr   r   c                       e Zd ZdZdZej        ej        ej        dZ	 e
dddedd	           e
d
ded           e
ddde           e
dde          gZddgZ	 	 	 	 	 	 	 	 ddZdS )cmd_group_remove_membersa  Remove members from an AD group.

This command removes one or more members from an existing Active Directory group.  The command accepts one or more group member names separated by commas.  A group member may be a user or computer account or another Active Directory group that is a member of the group specified on the command.

When a member is removed from a group, inherited permissions and rights will no longer apply to the member.

Example1:
samba-tool group removemembers supergroup Group1 -H ldap://samba.samdom.example.com -Uadministrator%passw0rd

Example1 shows how to remove Group1 from supergroup.  The command will run on the remote server specified on the -H parameter.  The -U parameter is used to pass the userid and password of a user authorized to issue the command on the remote server.

Example2:
sudo samba-tool group removemembers supergroup User1

Example2 shows how to remove a single user account, User2, from the supergroup AD group.  It uses the sudo command to run as root when issuing the command.
r   r(   r,   r-   r.   r/   r0   r1   r   zPDN of the group member to be removed.
The --object-types option will be ignored.r   r   r   r   r   r   r   r   r6   r?   r   Nc
                 J   |                                 }
|                    |
d          }|%|#|                                  t          d          	 t	          |t                      ||
          }g }|||z  }|||                    d          z  }|	                    d          }||                    |          }|                    ||d||           n)# t          $ r}t          d|d	|d
|          d }~ww xY w| j
                            d|z             d S )NTrA   r   rC   r   Fr   zFailed to remove members z from group "rH   zRemoved members from group %s
r   r   s                   r   r   zcmd_group_remove_members.run  s    ##%%((d(CC!6JJLLLHJ J J	nan.>.>&+4 4 4EL$	)( 3 3C 8 88!-!3!3C!8!8)!&!=!=n!M!M**9+7AF8J:H	 + J J J J
  	n 	n 	n,P]P]P]_h_h_hiklmmm	n 		9IEFFFFFs   BC 
D'C>>Dr   r   r   r   r   r   r     s        " XH )-.  	tW#JQT3	( 	( 	(}B		  	  	 
 	4 -	 	 	 	!1	 	 	#M. /0J .(G (G (G (G (G (Gr   r   c            
           e Zd ZdZdZ edddedd           ed	d
dd           eddde           eddddd          gZej	        ej
        ej        dZ	 	 	 	 	 	 	 ddZdS )cmd_group_listzList all groups.%prog [options]r,   r-   r.   r/   r0   r1   z-vz	--verbosez3Verbose output, showing group type and group scope.r<   r2   r=   z-bz	--base-dnzSpecify base DN to use.r6   	--full-dnfull_dnF)Display DN instead of the sAMAccountName.r5   r>   r=   r2   r)   r+   r*   Nc           	         |                                 }|                    |d          }	t          |t                      |	|          }
dg}|r|ddgz  }|
                                }|r|
                    |          }|
                    |t          j        d|          }t          |          d	k    rd S |r;| j
                            d
           | j
                            d           |D ]}| j
                            d|                    dd	          z             t          t          d|d         z            dz            }|t          t          t                              d                              k    r| j
                            d           n|t          t          t                              d                              k    r| j
                            d           n|t          t          t                              d                              k    r| j
                            d           ng|t          t          t                              d                              k    r| j
                            d           n|t          t          t                               d                              k    r| j
                            d           n|t          t          t                               d                              k    r| j
                            d           nm|t          t          t                               d                              k    r| j
                            d           n| j
                            d           t          |                    dg                     }| j
                            d|z             d S |D ]g}|r1| j
                            d|                    d          z             5| j
                            d|                    dd	          z             hd S ) NTrA   rC   samaccountnamer`   member(objectClass=group)r^   rX   rY   r   zQGroup Name                                  Group Type      Group Scope  Members
zQ--------------------------------------------------------------------------------
z%-44srZ   z%sl    r    zSecurity         Builtin  r!   zSecurity         Domain   r"   zSecurity         Global   r#   zSecurity         UniversalzDistribution     Global   zDistribution     Domain   zDistribution     Universalz                          r>   z    %6u
%s
rN   )rg   rh   r   r
   r   r   rz   rv   r   r{   rt   ru   re   hexr   rd   rf   )r   r)   r+   r*   r0   verbosebase_dnr   rG   r   r   rY   r   r   r   hgtypenum_memberss                    r   r   zcmd_group_list.runa  s    ##%%((d(CC!.*:*:"'B0 0 0  	-k8,,EOO%%	 	>44W==Ill9C,='<!&  ( ( HHMMF 	KIOOpqqqIOOpqqq ; ;	#''2B'*J*J JKKKSK(8!899<NNOOc#n&8&8&C&C"D"DEEEEIOO$@AAAAC(:(:8(D(D$E$E F FFFIOO$@AAAAC(:(:8(D(D$E$E F FFFIOO$@AAAAC(:(:;(G(G$H$H I IIIIOO$@AAAAC(:(>(>x(H(H$I$I J JJJIOO$@AAAAC(:(>(>x(H(H$I$I J JJJIOO$@AAAAC(:(>(>{(K(K$L$L M MMMIOO$@AAAAIOO$@AAA!#''(B'"?"?@@	k 9::::); ;,  K K IOOFSWWT]]$:;;;	1Aq)I)I IJJJJK Kr   )NNNNFNF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   G  s        H 	tW#JQT3	( 	( 	(t[I"	$ 	$ 	$ 	t[-	 	 	 	{"?	A 	A 	AM  ).-  9K 9K 9K 9K 9K 9Kr   r   c            
           e Zd ZdZdZ edddedd           ed	d
dd           edddd           eddddd          gZej	        ej
        ej        dZdgZ	 	 	 	 	 	 	 ddZdS )cmd_group_list_memberszList all members of an AD group.

This command lists members from an existing Active Directory group. The command accepts one group name.

Example1:
samba-tool group listmembers "Domain Users" -H ldap://samba.samdom.example.com -Uadministrator%passw0rd
r'   r,   r-   r.   r/   r0   r1   z--hide-expiredz!Do not list expired group membersFr<   )r2   r>   r=   z--hide-disabledz"Do not list disabled group members)r>   r=   r2   r   r   r   r   r   r?   Nc	                    |                                 }	|                    |	d          }
	 t          |t                      |
|	          }dt	          j        |          z  }	 |                    |                                t          j        |dg          }|d         	                    dd          }n # t          $ r t          d	|z            w xY wt          t          j        |          }|                                \  }}d
|z  }d}|du r|                                }d|z  }d}|du rdt          j        t$          fz  }d|d|d||d}|                    |                                t          j        |ddg          }t'          |          dk    rd S |D ]}|r1| j                            d|	                    d          z             5|	                    dd          }||	                    dd          }| j                            d|z             d S # t,          $ r}t          d|d|          d }~ww xY w)NTrA   rC   z)(&(objectClass=group)(sAMAccountName=%s))rR   r   r   rZ   r   z<SID=%s>r\   z>(|(!(accountExpires=*))(accountExpires=0)(accountExpires>=%u))z(!(userAccountControl:%s:=%u))z(&(|(primaryGroupID=z)(memberOf=z))rW   samAccountNamecnr   rN   zFailed to list members of "z
" group - )rg   rh   r   r
   rv   r   rz   r   r   re   r   r   r   r	   rn   r   
get_nttimeOID_COMPARATOR_ANDr   r{   rt   ru   ri   )r   r?   r+   r)   r*   r0   hide_expiredhide_disabledr   rG   r   r   search_filterr   group_sid_binary	group_sidgroup_dom_sidr   group_sid_dnfilter_expirescurrent_nttimefilter_disabledr   r   member_namer   s                             r   r   zcmd_group_list_members.run  s    ##%%((d(CC6	/an.>.>&+4 4 4E I .y99:MNll5??#4#4C<M/<*5 # 8 8 $'q6::kq:#A#A   N N N"#>)#LMMMN #8#35EFFI#,??#4#4 ]C%3LNt##!&!1!1!3!3#& *8	#9 !O$$"B*,=F? #? \\\??NNNDF ,,u008I*0&6%=  ? ?C CA 6 6 IOOFSWWT]]$:;;;!gg&6Ag>>&"%''$A'"6"6K	 455556 6  	/ 	/ 	/, )		11 . / / /	/s8   6H* $AB8 7H* 8CCH* #BH* *
I4I		I)NNNNFFF)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s         -H 	tW#JQT3	( 	( 	(7"	$ 	$ 	$ 	 "8	: 	: 	: 	{"?	A 	A 	AM$ ).-  J B/ B/ B/ B/ B/ B/r   r   c                   x    e Zd ZdZdZ edddedd          gZd	d
gZe	j
        e	j        e	j        dZ	 	 ddZdS )cmd_group_movea  Move a group to an organizational unit/container.

    This command moves a group object into the specified organizational unit
    or container.
    The groupname specified on the command is the sAMAccountName.
    The name of the organizational unit or container can be specified as a
    full DN or without the domainDN component.

    The command may be run from the root userid or another authorized userid.

    The -H or --URL= option can be used to execute the command against a remote
    server.

    Example1:
    samba-tool group move Group1 'OU=OrgUnit,DC=samdom.DC=example,DC=com' \
        -H ldap://samba.samdom.example.com -U administrator

    Example1 shows how to move a group Group1 into the 'OrgUnit' organizational
    unit on a remote LDAP server.

    The -H parameter is used to specify the remote target server.

    Example2:
    samba-tool group move Group1 CN=Users

    Example2 shows how to move a group Group1 back into the CN=Users container
    on the local server.
    z+%prog <groupname> <new_parent_dn> [options]r,   r-   r.   r/   r0   r1   r?   new_parent_dnr   Nc                    |                                 }|                    |d          }t          |t                      ||          }	t	          j        |	|	                                          }
dt	          j        |          z  }	 |	                    |
|t          j	                  }|d         j
        }n # t          $ r t          d|z            w xY w	 |	                    |          }n,# t          $ r}t          d|d	|j                  d }~ww xY wt	          j        |	t!          |                    }|                    t%          |          d
z
             |                    |           	 |	                    ||           n%# t          $ r}t          d|z  |          d }~ww xY w| j                            d|d|d           d S )NTrA   rC   r   r]   rX   r^   r   r   zInvalid new_parent_dn "z": r   zFailed to move group "%s"zMoved group "z" into "z"
)rg   rh   r   r
   rv   Dnr   r   rz   r   rN   r   r   r   ri   messager   remove_base_componentsr{   add_baserenamert   ru   )r   r?   r  r+   r)   r*   r0   rG   r   r   r   r   r   r   full_new_parent_dnr   full_new_group_dns                    r   r   zcmd_group_move.run2  s+   ##%%((d(CC!.*:*:"'B0 0 0F5%//"3"344	=#I../	J,,I*0%(%6  8 8C 1vyHH 	J 	J 	J:iHIII	J	;!&!=!=m!L!L 	; 	; 	;, -qyy : ; ; ;	;  F5#h--8800X1BCCC""#5666	KLL#45555 	K 	K 	K:YFJJJ	K	"$6$6$68 	9 	9 	9 	9 	9s<   
/B: :CC1 1
D;DD:F 
F3F..F3r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r    s         : =H 	tW#JS	2 	2 	2M
 /J).-  FJ $!9 !9 !9 !9 !9 !9r   r  c                       e Zd ZdZdZ edddedd           ed	d
ed          gZdgZe	j
        e	j        e	j        dZ	 	 ddZdS )cmd_group_showa  Display a group AD object.

This command displays a group object and it's attributes in the Active
Directory domain.
The group name specified on the command is the sAMAccountName of the group.

The command may be run from the root userid or another authorized userid.

The -H or --URL= option can be used to execute the command against a remote
server.

Example1:
samba-tool group show Group1 -H ldap://samba.samdom.example.com \
    -U administrator --password=passw1rd

Example1 shows how to display a group's attributes in the domain against a
remote LDAP server.

The -H parameter is used to specify the remote target server.

Example2:
samba-tool group show Group2

Example2 shows how to display a group's attributes in the domain against a local
LDAP server.

Example3:
samba-tool group show Group3 --attributes=member,objectGUID

Example3 shows how to display a groups objectGUID and member attributes.
z%prog <group name> [options]r,   r-   r.   r/   r0   r1   z--attributesz:Comma separated list of attributes, which will be printed.group_attrs)r2   r3   r5   r?   r   Nc                 .   |                                 }|                    |d          }t          |t                      ||          }	d }
|r|                    d          }
dt          j        |          z  }|	                                }	 |	                    ||t
          j	        |
          }|d         j
        }n # t          $ r t          d|z            w xY w|D ]1}t          j        |	|          }| j                            |           2d S )	NTrA   rC   r   z,(&(objectCategory=group)(sAMAccountName=%s)))r]   rX   r^   rY   r   r   )rg   rh   r   r
   r   rv   r   r   rz   r   rN   r   r   r   get_ldif_for_editorrt   ru   )r   r?   r+   r)   r*   r0   r  rG   r   r   rY   r   domaindnr   user_dnr   
group_ldifs                    r   r   zcmd_group_show.run  sK    ##%%((d(CC!.*:*:"'B0 0 0  	+%%c**E@$Y//0 ??$$	J,,H%(%6e  E EC!fiGG 	J 	J 	J:iHIII	J  	( 	(C3E3??JIOOJ''''	( 	(s   0C CNNNNNr  r   r   r   r  r  V  s         > .H 	tW#JS	2 	2 	2~.m	- 	- 	-M J).-  IM $( ( ( ( ( (r   r  c                   r    e Zd ZdZdZ edddedd          gZej	        ej
        ej        d	Zd
 ZddZdS )cmd_group_statsz+Summary statistics about group memberships.r   r,   r-   r.   r/   r0   r1   r   c                 `    d}|                                 D ]\  }}||k    r||k    r||z  }|S )Nr   )items)r   	range_min	range_maxgroup_freqstotal_countmemberscounts          r   num_in_rangezcmd_group_stats.num_in_range  sL    )//11 	% 	%NGUG##9(<(<u$r   Nc                    |                                 }|                    |d          }t          |t                      ||          }|                                }|                    |t          j        dddg          }	i }
d}|	D ]R}t          |	                    d                    }t          |	                    dg 	                    }||
|<   ||z  }S|	j        }| j                            d
           | j                            d           | j                            d                    |                     | j                            d                    |                     |t          |          z  }| j                            d|z             t!          |
                                          }t!          |
                                          }|                    t)          |                    }||         }| j                            d                    |||                              |                                 |dz  }||         }|dz  dk    r|||dz
           z   dz  }| j                            d                    |                     t-          t.                    }|
                                D ]\  }}||xx         dz  cc<   dddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-|fg}| j                            d.           | j                            d           |D ]l}|d         }|d         }||k    r nS|                     |||          }|dk    r5d/                    ||          }| j                            d0||fz             m| j                            d1           d S )2NTrA   rC   r   r   r   r   r   r   zGroup membership statistics*
z2-------------------------------------------------
zTotal groups: {0}
zTotal memberships: {0}
z Average members per group: %.2f
zMax members: {0} ({1})
   r   zMedian members per group: {0}

)r   r   )r(     )   	   )
      )      )      )      )   '   )(   1   )2   ;   )<   E   )F   O   )P   Y   )Z   c   )d      )      )      )   i+  )i,  i  )i  i  )i  i  )i  i  )i  i  )i  i  )i  i  )i  i'  i'  z Members        Number of Groups
z{0}-{1}z	%13s  %u
z7
* Note this does not include nested group memberships
)rg   rh   r   r
   r   rz   rv   r   r   re   r{   r%  rt   ru   formatfloatlistkeysvaluesindexmaxsortr   r   r  r&  )r   r)   r+   r*   r0   rG   r   r   r   r   group_assignmentstotal_membershipsr   r   r   
num_groupsaveragegroup_namesgroup_membersr[   max_membersmidpointmedianr"  rL   bandsband
band_startband_endband_strs                                 r   r   zcmd_group_stats.run  s   ##%%((d(CC!.*:*:"'B0 0 0 OO%%	ll9C,='<"2H!=  ? ?
  	- 	-Csww/0011Dcgghg;;<<K&1d#,Y
	8999	MNNN	-44Z@@AAA	299:KLLMMM#eJ&7&77	;gEFFF ,113344.557788!!#m"4"455#C(	299+:Ec:JL L 	M 	M 	M?x(>Q}X\::a?F	;BB6JJKKK "#&&"3"9"9";"; 	* 	*E;$$$)$$$$ 8X8Xx88ZZZZ|\<%' 		;<<<	MNNN 
	G 
	GDaJAwHK''**:xMMJQ$++JAA	*/E EFFF	STTTTTr   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r&  r   r   r   r   r  r    s        55 H 	tW#JQT3	( 	( 	(M ).-   KU KU KU KU KU KUr   r  c                       e Zd ZdZdZ edddedd           ed	d
e          gZdgZe	j
        e	j        e	j        dZ	 	 ddZdS )cmd_group_edita  Modify Group AD object.

    This command will allow editing of a group account in the Active Directory
    domain. You will then be able to add or change attributes and their values.

    The groupname specified on the command is the sAMAccountName.

    The command may be run from the root userid or another authorized userid.

    The -H or --URL= option can be used to execute the command against a remote
    server.

    Example1:
    samba-tool group edit Group1 -H ldap://samba.samdom.example.com \
        -U administrator --password=passw1rd

    Example1 shows how to edit a groups attributes in the domain against a
    remote LDAP server.

    The -H parameter is used to specify the remote target server.

    Example2:
    samba-tool group edit Group2

    Example2 shows how to edit a groups attributes in the domain against a local
    server.

    Example3:
    samba-tool group edit Group3 --editor=nano

    Example3 shows how to edit a groups attributes in the domain against a local
    server using the 'nano' editor.
    r'   r,   r-   r.   r/   r0   r1   z--editorzQEditor to use instead of the system default, or 'vi' if no system default is set.r6   r?   r   Nc                    |                                 }|                    |d          }t          |t                      ||          }	dt	          j        |          z  }
|	                                }	 |	                    ||
t          j                  }|d         j	        }n # t          $ r t          d|z            w xY wt          |          dk    r!t          d	|t          |          fz            |d         }t          j        |	|          }|#t          j                            d
          }|d}t%          j        d          5 }|                    t+          |                     |                                 	 t/          ||j        g           n"# t2          $ r}t3          d|          d }~ww xY wt5          |j                  5 }|                                }d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |	                    |          }t;          |          d         }|	                    ||          }t          |          dk    r| j                            d           d S 	 |	                     |           n%# tB          $ r}t          d|z  |          d }~ww xY w| j                            d|z             d S )NTrA   rC   r   r
  r   r   r   z'Invalid number of results: for "%s": %dEDITORviz.tmp)suffixzERROR: zNothing to do
zFailed to modify group '%s': z!Modified group '%s' successfully
)"rg   rh   r   r
   rv   r   r   rz   r   rN   r   r   r{   r   r  osenvironre   tempfileNamedTemporaryFileru   r   flushr   r   r   openread
parse_ldifnextmsg_diffrt   modifyri   )r   r?   r+   r)   r*   r0   editorrG   r   r   r   r  r   r   r   result_ldift_filer   edited_fileedited_messagemsgs_edited
msg_editedres_msg_diffs                          r   r   zcmd_group_edit.run=  s   ##%%((d(CC!.*:*:"'B0 0 0 >#I../ ??$$	J,,H*0%(%6  8 8C 1vyHH 	J 	J 	J:iHIII	J s88q==H!*SXX6 7 8 8 8 !f0<<>Z^^H--F~(777 	46LL;//000LLNNN7FFK01111% 7 7 7(A6667fk"" 4k!,!1!1!3!34 4 4 4 4 4 4 4 4 4 4 4 4 4 4	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 &&~66+&&q)
~~c:66|!!IOO-...F	OLL&&&& 	O 	O 	O>JANNN	O 		<yHIIIIIs~   7/B' 'C7H
F! H!
G +F;;G  HG8,H8G<	<H?G<	 HHH
J   
K*J==Kr  r  r   r   r   r`  r`    s           B -H 	tW#JS	2 	2 	2z !7=@	B 	B 	BM J).-  IM7J 7J 7J 7J 7J 7Jr   r`  c                   x    e Zd ZdZdZ edddedd          gZd	d
gZe	j
        e	j        e	j        dZ	 	 ddZdS )cmd_group_add_unix_attrsa  Add RFC2307 attributes to a group.

This command adds Unix attributes to a group account in the Active
Directory domain.
The groupname specified on the command is the sAMaccountName.

Unix (RFC2307) attributes will be added to the group account.

Add 'idmap_ldb:use rfc2307 = Yes' to smb.conf to use these attributes for
UID/GID mapping.

The command may be run from the root userid or another authorized userid.
The -H or --URL= option can be used to execute the command against a
remote server.

Example1:
samba-tool group addunixattrs Group1 10000

Example1 shows how to add RFC2307 attributes to a domain enabled group
account.

The groups Unix ID will be set to '10000', provided this ID isn't already
in use.

z'%prog <groupname> <gidnumber> [options]r,   r-   r.   r/   r0   r1   r?   rb   r   Nc                    |                                 }|                    |          }t          |t                      ||          }	|	                                }
d                    t          j        |                    }|	                    |
t          j	        |          }t          |          dk    r"t          d                    |                    |d         j        }d|d         v r"t          d                    |                    d                    |          }|	                    |
t          j	        |          }t          |          dk    r"t          d	                    |                    |                    d
          s| j                            d           d                    ||          }	 |	                    |           n:# t          j        $ r(}t          d                    ||                    d }~ww xY w| j                            d                    |                     d S )NrC   z(samaccountname={}))r^   rX   r   zUnable to find group '{}'	gidNumberzGroup {} is a Unix group.z$(&(objectClass=group)(gidNumber={}))zgidNumber {} already used.zidmap_ldb:use rfc2307zYou are setting a Unix/RFC2307 GID. You may want to set 'idmap_ldb:use rfc2307 = Yes' in smb.conf to use the attributes for XID/SID-mapping.
z:
dn: {0}
changetype: modify
add: gidNumber
gidNumber: {1}
z!Failed to modify group '{0}': {1}z!Modified Group '{}' successfully
)rg   rh   r   r
   r   rI  rv   r   rz   r   r{   r   rN   re   rt   ru   modify_ldifrw   )r   r?   rb   r+   r)   r*   r0   rG   r   r   r  r   r   r   	group_modr   s                   r   r   zcmd_group_add_unix_attrs.run  sC    ##%%((,,!.*:*:"'B0 0 0 ??$$ '--c.?	.J.JKKll8!$!2&,  . . HHMM:AA)LLMMMq69#a&  :AA)LLMMM 8>>yIIll8!$!2&,  . . HHMM;BB9MMNNNvv-.. 	2IOO 1 2 2 2

 F8Y 		6i((((| 	6 	6 	6B &y! 4 46 6 6	6 		<CCINNOOOOOs   G H+#HHr   r  r   r   r   ry  ry  w  s         2 9H 	tW#JS	2 	2 	2M
 {+J ).-
 
 BF $3P 3P 3P 3P 3P 3Pr   ry  c            	           e Zd ZdZdZ edddedd           ed	d
e           eddd           edde           edde          gZdgZe	j
        e	j        e	j        dZ	 	 	 ddZdS )cmd_group_renamea  Rename a group and related attributes.

    This command allows to set the group's name related attributes. The
    group's CN will be renamed automatically.

    The group's CN will be the sAMAccountName.
    Use the --force-new-cn option to specify the new CN manually and the
    --reset-cn to reset this change.

    Use an empty attribute value to remove the specified attribute.

    The groupname specified on the command is the sAMAccountName.

    The command may be run locally from the root userid or another authorized
    userid.

    The -H or --URL= option can be used to execute the command against a remote
    server.

    Example1:
    samba-tool group rename employees --samaccountname=staff

    Example1 shows how to change the samaccountname of a group 'employees' to
    'staff'. The CN of the group employees will also be changed to 'staff',
    if the previous CN was the previous sAMAccountName.

    Example2:
    samba-tool group rename employees --mail-address='staff@company.com' \
        -H ldap://samba.samdom.example.com -U administrator

    Example2 shows how to rename the mail address of a group 'employees' to
    'staff@company.com'.
    The -H parameter is used to specify the remote target server.
    r'   r,   r-   r.   r/   r0   r1   z--force-new-cnz;Specify a new CN (RND) instead of using the sAMAccountName.r6   z
--reset-cnzqSet the CN (RDN) to the sAMAccountName. Use this option to reset the changes made with the --force-new-cn option.r<   r   r;   zNew mail addressz--samaccountnamez,New account name (sAMAccountName/logon name)r?   r   Nc
           	         |r|	rt          d          |dk    rt          d          |dk    rt          d          |                                }
|                    |
d          }t          |t	                      ||
          }t          j        ||                                          }dt          j        |          z  }	 |	                    |t
          j
        |g d	
          }|d         }|j        }n # t          $ r t          d|z            w xY w|                                }|d         d         }||}n||}n|d         }|d         }t          |          t          |          k    o0t          |          t          |          k    p|	pt          |          }t          j        |d|z            }|                    |           t          j                    }||_        |                    ||d|           |                    ||d|           t'          |          dk    }|                                 	 |r|                    |           |r|                    ||           n9# t.          $ r,}|                                 t          d|z  |          d }~ww xY w|                                 |r$| j                            d|d|d|d           |rk| j                            d|z             |                                D ];}|dk    r	| j                            |d||         r||         ndd           :d S d S )NzEIt is not allowed to specify --force-new-cn together with --reset-cn.r\   z8Failed to rename group - delete protected attribute 'CN'zDFailed to rename group - delete protected attribute 'sAMAccountName'TrA   rC   z)(&(objectClass=group)(samaccountname=%s)))rO   r   mailr   r   r   r   rO   zCN=%sr  zFailed to rename group "%s"zRenamed CN of group "z" from "z" to "z" successfully
zCFollowing attributes of group "%s" have been changed successfully:
rN   z: z	[removed]rV   )r   rg   rh   r   r
   rv   r  r   r   rz   r   rN   r   parentr   boolr  Messageprepare_attr_replacer{   transaction_startro  r  ri   transaction_canceltransaction_commitrt   ru   rL  )r   r?   r+   r)   r*   r0   r   r   force_new_cnreset_cnrG   r   r   r   r   r   	old_groupr   group_parent_dnold_cnnew_cnexpected_cnmust_change_cnnew_group_dnr  group_attributes_changedr   attrs                               r   r   zcmd_group_rename.run  sT     	<H 	<  ; < < <2  0 1 1 1R  < = = = ##%%((d(CC!.*:*:"'B0 0 0F5%//"3"344	=#I../	I,,I%(%6*0&. &. &.   C AI |HH 	I 	I 	I9YGHHH	I #//++4# #!FF'#FF/0F   01VF3 :f++[)9)99 9"9&*<&8&8 	 veWv%566o... kmm!"";	;K#1	3 	3 	3"";	6<PPP#&{#3#3a#7  	!!!	M' *[))) 5X|444 	M 	M 	M$$&&&<yH!LLL	M 	  """ 	LIOOO09		666666K L L L $ 	DIOO 69BD E E E#((** D D4<<	ddd*405BK4E4E6A5B 5B !C D D D D	D 	DD Ds$   4C< <D/I8 8
J.'J))J.)NNNNNNNNr  r   r   r   r  r    s$       ! !F -H 	tW8e#	/ 	/ 	/ 	N	 	 	 	|P"	$ 	$ 	$ 	#	 	 	 	!?	 	 	
M& J).-  IMIMXD XD XD XD XD XDr   r  c                   h   e Zd ZdZi Z e            ed<    e            ed<    e            ed<    e            ed<    e            ed<    e	            ed<    e
            ed<    e            ed	<    e            ed
<    e            ed<    e            ed<    e            ed<    e            ed<   dS )	cmd_groupzGroup management.rs   creater   edit
addmembersremovemembersrK  listmembersmoveshowstatsaddunixattrsr  N)r   r   r   r   subcommandsr&   r   r`  r   r   r   r   r  r  r  ry  r  r   r   r   r  r  p  s       K&K)MOOK,,..K(.**K 5 5 7 7K#;#;#=#=K (.**K!7!7!9!9K(.**K(.**K*?,,K":":"<"<K,,..Kr   r  )<samba.getoptgetoptr   samba.netcmdr   r   r   r   rv   	samba.ndrr   r   samba.dcerpcr	   
samba.authr
   samba.samdbr   
samba.dsdbr   r   r   r   r   r   r   r   r   r   r   r   r   collectionsr   
subprocessr   r   samba.commonr   r   re  rg  r\   r   dictrd   rf   r&   r   r   r   r   r   r  r  r  r`  ry  r  r  r   r   r   <module>r     s  &       D D D D D D D D D D D D 



 * * * * * * * * ! ! ! ! ! ! % % % % % %                                    $ # # # # # 5 5 5 5 5 5 5 5 3 3 3 3 3 3 3 3 				       "D!B!<$BD D E E T%J%D(JL L M M 
N8 N8 N8 N8 N8G N8 N8 N8b;: ;: ;: ;: ;:w ;: ;: ;:|]C ]C ]C ]C ]CG ]C ]C ]C@[G [G [G [G [Gw [G [G [G|SK SK SK SK SKW SK SK SKlf/ f/ f/ f/ f/W f/ f/ f/RM9 M9 M9 M9 M9W M9 M9 M9`L( L( L( L( L(W L( L( L(^cU cU cU cU cUg cU cU cULiJ iJ iJ iJ iJW iJ iJ iJX\P \P \P \P \Pw \P \P \P~XD XD XD XD XDw XD XD XDt/ / / / / / / / / /r   