a
    Wa[                     @   s  d dl mZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ d dlmZmZmZmZ d d	lmZ d
dlmZ G dd deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#dS )    N)
check_callCalledProcessError)
attrgetter)system_session)SamDB)credentialsdsdb)Net)CommandCommandErrorSuperCommandOption)	get_bytes   )commonc                   @   s   e Zd ZdZdZedddeddded	d
ededdededdededdededdededdededdededdededdededdededdeded d!eded"d#eded$d%edgZd&gZe	j
e	je	jd'Zd+d)d*Zd(S ),
cmd_createa  Create a new contact.

    This command creates a new contact in the Active Directory domain.

    The name of the new contact can be specified by the first argument
    'contactname' or the --given-name, --initial and --surname arguments.
    If no 'contactname' is given, contact's name will be made up of the given
    arguments by combining the given-name, initials and surname. Each argument
    is optional. A dot ('.') will be appended to the initials automatically.

    Example1:
    samba-tool contact create "James T. Kirk" --job-title=Captain \
        -H ldap://samba.samdom.example.com -UAdministrator%Passw1rd

    The example shows how to create a new contact in the domain against a remote
    LDAP server.

    Example2:
    samba-tool contact create --given-name=James --initials=T --surname=Kirk

    The example shows how to create a new contact in the domain against a local
    server. The resulting name is "James T. Kirk".
    z%prog [contactname] [options]-H--URL%LDB URL for database or target serverURLHhelptypemetavardestz--ouzDN of alternative location (with or without domainDN counterpart) in which the new contact will be created. E.g. 'OU=<OU name>'. Default is the domain base.r   r   z	--surnamezContact's surnamez--given-namezContact's given namez
--initialszContact's initialsz--display-namezContact's display namez--job-titlezContact's job titlez--departmentzContact's departmentz	--companyzContact's companyz--descriptionzContact's descriptionz--mail-addresszContact's email addressz--internet-addresszContact's home pagez--telephone-numberzContact's phone numberz--mobile-numberzContact's mobile phone numberz--physical-delivery-officezContact's office locationzfullcontactname?	sambaoptscredoptsversionoptsNc                 C   s   |  }||}z>t|t ||d}|j|||||	|
|||||||||d}W n. ty~ } ztd|W Y d }~n
d }~0 0 | jd|  d S )NZurlZsession_infor   lp)fullcontactnameousurnameZ	givennameinitialsZdisplaynameZjobtitle
departmentcompanydescriptionZmailaddressZinternetaddressZtelephonenumberZmobilenumberZphysicaldeliveryofficezFailed to create contactz"Contact '%s' created successfully
)	get_loadparmget_credentialsr   r   Z
newcontact	Exceptionr   outfwrite)selfr#   r   r   r    r   r$   r%   Z
given_namer&   Zdisplay_nameZ	job_titler'   r(   r)   Zmail_addressZinternet_addressZtelephone_numberZmobile_numberZphysical_delivery_officer"   credssamdbZret_namee r3   6/usr/lib/python3/dist-packages/samba/netcmd/contact.pyrunj   s8    

 zcmd_create.run)NNNNNNNNNNNNNNNNNNN__name__
__module____qualname____doc__synopsisr   strtakes_optionsZ
takes_argsoptionsSambaOptionsCredentialsOptionsVersionOptionstakes_optiongroupsr5   r3   r3   r3   r4   r   +   sl                      r   c                   @   sL   e Zd ZdZdZedddedddgZd	gZe	j
e	je	jd
ZdddZdS )
cmd_deletea4  Delete a contact.

    This command deletes a contact object from the Active Directory domain.

    The contactname specified on the command is the common name or the
    distinguished name of the contact object. The distinguished name of the
    contact can be specified with or without the domainDN component.

    Example:
    samba-tool contact delete Contact1 \
        -H ldap://samba.samdom.example.com \
        --username=Administrator --password=Passw1rd

    The example shows how to delete a contact in the domain against a remote
    LDAP server.
    %prog <contactname> [options]r   r   r   r   r   r   contactnamer   Nc              
   C   s~  |  }|j|dd}t|t ||d}| }	tj}
dt| }| 	drd}tj
}
z||}	W n4 ty } ztd||f W Y d }~n
d }~0 0 z"|j|	|
|dgd	}|d
 j}W n ty   td| Y n0 t|dkr&t|tddD ]}| jd|j  q td| z|| W n4 tyh } ztd| |W Y d }~n
d }~0 0 | jd|  d S )NTZfallback_machiner!   !(&(objectClass=contact)(name=%s))CN=(objectClass=contact)Invalid dn "%s": %sdnbasescope
expressionattrsr   Unable to find contact "%s"r   key
found: %s
FMultiple results for contact '%s'
Please specify the contact's full DNzFailed to remove contact "%s"zDeleted contact %s
)r*   r+   r   r   	domain_dnldbSCOPE_SUBTREEbinary_encodeupper
startswith
SCOPE_BASEnormalize_dn_in_domainr,   r   searchrK   
IndexErrorlensortedr   r-   r.   delete)r/   rE   r   r   r    r   r"   r0   r1   base_dnrN   filterr2   res
contact_dnmsgr3   r3   r4   r5      sR    $zcmd_delete.run)NNNNr6   r3   r3   r3   r4   rC      s(   	    rC   c                	   @   sd   e Zd ZdZdZedddeddded	d
dededdddddgZej	ej
ejdZdddZdS )cmd_listzList all contacts.
    z%prog [options]r   r   r   r   r   r   z-bz	--base-dnzSpecify base DN to use.r   z	--full-dnfull_dnF
store_truez.Display contact's full DN instead of the name.)r   defaultactionr   r   Nc                 C   s   |  }|j|dd}t|t ||d}	|	 }
|r>|	|}
|	j|
tjddgd}t	|dkrdd S |rt
|tdd	D ]}| jd
|j  qxd S |D ]"}|jddd}| jd
|  qd S )NTrF   r!   rI   name)rN   rO   rP   r   rK   rR   z%s
)idx)r*   r+   r   r   rV   r]   r^   rW   rX   r`   ra   r   r-   r.   rK   get)r/   r   r   r    r   rc   ri   r"   r0   r1   Z	search_dnre   rg   Zcontact_namer3   r3   r4   r5     s0    
zcmd_list.run)NNNNNF)r7   r8   r9   r:   r;   r   r<   r=   r>   r?   r@   rA   rB   r5   r3   r3   r3   r4   rh      s>         rh   c                   @   sX   e Zd ZdZdZedddeddded	d
edgZdgZe	j
e	je	jdZdddZdS )cmd_editaH  Modify a contact.

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

    The contactname specified on the command is the common name or the
    distinguished name of the contact object. The distinguished name of the
    contact can be specified with 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 contact edit Contact1 -H ldap://samba.samdom.example.com \
        -U Administrator --password=Passw1rd

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

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

    Example2:
    samba-tool contact edit CN=Contact2,OU=people,DC=samdom,DC=example,DC=com

    Example2 shows how to edit a contact's attributes in the domain against a
    local server. The contact, which is located in the 'people' OU,
    is specified by the full distinguished name.

    Example3:
    samba-tool contact edit Contact3 --editor=nano

    Example3 shows how to edit a contact's attributes in the domain against a
    local server using the 'nano' editor.
    rD   r   r   r   r   r   r   z--editorzQEditor to use instead of the system default, or 'vi' if no system default is set.r   rE   r   Nc                 C   s  |  }|j|dd}t|t ||d}	|	 }
tj}dt| }| 	drd}tj
}z|	|}
W n4 ty } ztd||f W Y d }~n
d }~0 0 z|	j|
||d}|d	 j}W n ty   td
| Y n0 t|dkr t|tddD ]}| jd|j  qtd| |D ]}t|	|}|d u rXtjd}|d u rXd}tjdd}|t| |  zt||j g W n0 t!y } zt!d|W Y d }~n
d }~0 0 t"|j }|# }W d    n1 s0    Y  W d    n1 s
0    Y  q$|	$|}t%|d }|	&||}t|d	krX| jd d S z|	'| W n4 ty } ztd| |W Y d }~n
d }~0 0 | jd|  d S )NTrF   r!   rG   rH   rI   rJ   )rM   rN   rO   r   rQ   r   rK   rR   rT   rU   ZEDITORZviz.tmp)suffixzERROR: zNothing to do
zFailed to modify contact '%s': z#Modified contact '%s' successfully
)(r*   r+   r   r   rV   rW   rX   rY   rZ   r[   r\   r]   r,   r   r^   rK   r_   r`   ra   r   r-   r.   r   get_ldif_for_editorosenvironro   tempfileZNamedTemporaryFiler   flushr   rm   r   openreadZ
parse_ldifnextZmsg_diffZmodify)r/   rE   r   r   r    r   Zeditorr"   r0   r1   rc   rN   rd   r2   re   rf   rg   Zresult_ldifZt_fileZedited_fileZedited_messageZmsgs_editedZ
msg_editedZres_msg_diffr3   r3   r4   r5   q  sz    

 L
zcmd_edit.run)NNNNNr6   r3   r3   r3   r4   rp   6  s2   $     rp   c                   @   sZ   e Zd ZdZdZedddeddded	d
eddgZdgZe	j
e	je	jdZdddZdS )cmd_showa  Display a contact.

    This command displays a contact object with it's attributes in the Active
    Directory domain.

    The contactname specified on the command is the common name or the
    distinguished name of the contact object. The distinguished name of the
    contact can be specified with 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 contact show Contact1 -H ldap://samba.samdom.example.com \
        -U Administrator --password=Passw1rd

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

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

    Example2:
    samba-tool contact show CN=Contact2,OU=people,DC=samdom,DC=example,DC=com

    Example2 shows how to display a contact's attributes in the domain against
    a local server. The contact, which is located in the 'people' OU, is
    specified by the full distinguished name.

    Example3:
    samba-tool contact show Contact3 --attributes=mail,mobile

    Example3 shows how to display a contact's mail and mobile attributes.
    rD   r   r   r   r   r   r   z--attributesz:Comma separated list of attributes, which will be printed.contact_attrs)r   r   r   rE   r   Nc              
   C   s^  |  }|j|dd}t|t ||d}	|	 }
tj}d }|rH|d}dt| }|	 
drd}tj}z|	|}
W n4 ty } ztd||f W Y d }~n
d }~0 0 z |	j|
|||d	}|d
 j}W n ty   td| Y n0 t|dkr6t|tddD ]}| jd|j  qtd| |D ]}t|	|}| j| q:d S )NTrF   r!   ,rG   rH   rI   rJ   )rM   rO   rN   rP   r   rQ   r   rK   rR   rT   zAMultiple results for contact '%s'
Please specify the contact's DN)r*   r+   r   r   rV   rW   rX   splitrY   rZ   r[   r\   r]   r,   r   r^   rK   r_   r`   ra   r   r-   r.   r   rr   )r/   rE   r   r   r    r   r{   r"   r0   r1   rc   rN   rP   rd   r2   re   rf   rg   Zcontact_ldifr3   r3   r4   r5     sT    
zcmd_show.run)NNNNNr6   r3   r3   r3   r4   rz     s4   #     rz   c                   @   sN   e Zd ZdZdZedddedddgZd	d
gZe	j
e	je	jdZdddZdS )cmd_movea]  Move a contact object to an organizational unit or container.

    The contactname specified on the command is the common name or the
    distinguished name of the contact object. The distinguished name of the
    contact can be specified with or without the domainDN component.

    The name of the organizational unit or container can be specified as the
    distinguished name, with 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 contact move Contact1 'OU=people' \
        -H ldap://samba.samdom.example.com -U Administrator

    Example1 shows how to move a contact Contact1 into the 'people'
    organizational unit on a remote LDAP server.

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

    Example2:
    samba-tool contact move Contact1 OU=Contacts,DC=samdom,DC=example,DC=com

    Example2 shows how to move a contact Contact1 into the OU=Contacts
    organizational unit on the local server.
    z-%prog <contactname> <new_parent_dn> [options]r   r   r   r   r   r   rE   new_parent_dnr   Nc              
   C   s  |  }|j|dd}t|t ||d}	|	 }
tj}dt| }| 	drd}tj
}z|	|}
W n4 ty } ztd||f W Y d }~n
d }~0 0 z"|	j|
||dgd	}|d
 j}W n ty   td| Y n0 t|dkr&t|tddD ]}| jd|j  q td| z|	|}W n6 tyj } ztd||f W Y d }~n
d }~0 0 t|	t|}|t|d  || z|	|| W n4 ty } ztd| |W Y d }~n
d }~0 0 | jd||f  d S )NTrF   r!   rG   rH   rI   rJ   rK   rL   r   rQ   r   rR   rT   rU   zInvalid new_parent_dn "%s": %szFailed to move contact "%s"zMoved contact "%s" into "%s"
)r*   r+   r   r   rV   rW   rX   rY   rZ   r[   r\   r]   r,   r   r^   rK   r_   r`   ra   r   r-   r.   ZDnr<   Zremove_base_componentsZadd_baserename)r/   rE   r   r   r   r    r   r"   r0   r1   rc   rN   rd   r2   re   rf   rg   Zfull_new_parent_dnZfull_new_contact_dnr3   r3   r4   r5   b  sh    
$zcmd_move.run)NNNNr6   r3   r3   r3   r4   r~   1  s(   		    r~   c                   @   sP   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< dS )	cmd_contactzContact management.Zcreaterb   ZeditlistZmoveZshowN)r7   r8   r9   r:   Zsubcommandsr   rC   rp   rh   r~   rz   r3   r3   r3   r4   r     s   




r   )$Zsamba.getoptZgetoptr>   rW   rs   ru   
subprocessr   r   operatorr   Z
samba.authr   Zsamba.samdbr   Zsambar   r   Z	samba.netr	   Zsamba.netcmdr
   r   r   r   Zsamba.compatr    r   r   rC   rh   rp   rz   r~   r   r3   r3   r3   r4   <module>   s(   rWB 
rp