<s> import base64 <EOL> import logging <EOL> import os <EOL> import re <EOL> import tempfile <EOL> from django . forms import FileField <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from dojango import forms <EOL> from freenasUI . common . forms import ModelForm <EOL> from freenasUI . common . freenasldap import ( <EOL> FreeNAS_ActiveDirectory , <EOL> FreeNAS_LDAP , <EOL> FreeNAS_ActiveDirectory_Exception , <EOL> ) <EOL> from freenasUI . common . pipesubr import run <EOL> from freenasUI . common . ssl import get_certificateauthority_path <EOL> from freenasUI . common . system import ( <EOL> validate_netbios_name , <EOL> validate_netbios_names , <EOL> compare_netbios_names <EOL> ) <EOL> from freenasUI . directoryservice import models <EOL> from freenasUI . middleware . notifier import notifier <EOL> from freenasUI . services . exceptions import ServiceFailed <EOL> from freenasUI . services . models import CIFS <EOL> log = logging . getLogger ( '<STR_LIT>' ) <EOL> class idmap_ad_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_ad <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_adex_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_adex <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_autorid_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_autorid <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_hash_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_hash <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_ldap_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_ldap <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_nss_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_nss <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_rfc2307_Form ( ModelForm ) : <EOL> idmap_rfc2307_ldap_user_dn_password2 = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . widgets . PasswordInput ( ) , <EOL> required = False <EOL> ) <EOL> class Meta : <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> model = models . idmap_rfc2307 <EOL> widgets = { <EOL> '<STR_LIT>' : <EOL> forms . widgets . PasswordInput ( render_value = False ) <EOL> } <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( idmap_rfc2307_Form , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance . idmap_rfc2307_ldap_user_dn_password : <EOL> self . fields [ '<STR_LIT>' ] . required = False <EOL> if self . _api is True : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> def clean_idmap_rfc2307_ldap_user_dn_password2 ( self ) : <EOL> password1 = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> password2 = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if password1 != password2 : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) <EOL> ) <EOL> return password2 <EOL> def clean ( self ) : <EOL> cdata = self . cleaned_data <EOL> if not cdata . get ( "<STR_LIT>" ) : <EOL> cdata [ '<STR_LIT>' ] = self . instance . idmap_rfc2307_ldap_user_dn_password <EOL> return cdata <EOL> class idmap_rid_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_rid <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_tdb_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_tdb <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class idmap_tdb2_Form ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . idmap_tdb2 <EOL> exclude = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class NT4Form ( ModelForm ) : <EOL> nt4_adminpw2 = forms . CharField ( <EOL> max_length = <NUM_LIT:50> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . widgets . PasswordInput ( ) , <EOL> required = False , <EOL> ) <EOL> advanced_fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class Meta : <EOL> model = models . NT4 <EOL> widgets = { <EOL> '<STR_LIT>' : forms . widgets . PasswordInput ( render_value = False ) , <EOL> } <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( NT4Form , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance . nt4_adminpw : <EOL> self . fields [ '<STR_LIT>' ] . required = False <EOL> if self . _api is True : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> self . instance . _original_nt4_idmap_backend = self . instance . nt4_idmap_backend <EOL> self . fields [ "<STR_LIT>" ] . widget . attrs [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" <EOL> ) <EOL> def clean_nt4_adminpw2 ( self ) : <EOL> password1 = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> password2 = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if password1 != password2 : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) <EOL> ) <EOL> return password2 <EOL> def clean_nt4_idmap_backend ( self ) : <EOL> nt4_idmap_backend = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if not nt4_idmap_backend : <EOL> nt4_idmap_backend = None <EOL> return nt4_idmap_backend <EOL> def clean_nt4_workgroup ( self ) : <EOL> workgroup = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> try : <EOL> validate_netbios_name ( workgroup ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( _ ( "<STR_LIT>" % e ) ) <EOL> return workgroup <EOL> def clean ( self ) : <EOL> cdata = self . cleaned_data <EOL> if not cdata . get ( "<STR_LIT>" ) : <EOL> cdata [ '<STR_LIT>' ] = self . instance . nt4_adminpw <EOL> return cdata <EOL> def save ( self ) : <EOL> enable = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> started = notifier ( ) . started ( "<STR_LIT>" ) <EOL> if enable : <EOL> if started is True : <EOL> started = notifier ( ) . restart ( "<STR_LIT>" ) <EOL> if started is False : <EOL> started = notifier ( ) . start ( "<STR_LIT>" ) <EOL> if started is False : <EOL> self . instance . ad_enable = False <EOL> super ( NT4Form , self ) . save ( ) <EOL> raise ServiceFailed ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> if started is True : <EOL> started = notifier ( ) . stop ( "<STR_LIT>" ) <EOL> obj = super ( NT4Form , self ) . save ( ) <EOL> return obj <EOL> class ActiveDirectoryForm ( ModelForm ) : <EOL> ad_netbiosname_a = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> ) <EOL> ad_netbiosname_b = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> ) <EOL> ad_netbiosalias = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> ) <EOL> advanced_fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> exclude = [ '<STR_LIT>' ] <EOL> model = models . ActiveDirectory <EOL> widgets = { <EOL> '<STR_LIT>' : forms . widgets . PasswordInput ( render_value = True ) , <EOL> } <EOL> def __original_save ( self ) : <EOL> for name in ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) : <EOL> setattr ( <EOL> self . instance , <EOL> "<STR_LIT>" % name , <EOL> getattr ( self . instance , name ) <EOL> ) <EOL> for name in ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) : <EOL> setattr ( <EOL> self . cifs , <EOL> "<STR_LIT>" % name , <EOL> getattr ( self . cifs , name ) , <EOL> ) <EOL> def __original_changed ( self ) : <EOL> if ( <EOL> self . instance . _original_ad_domainname != self . instance . ad_domainname or <EOL> self . cifs . _original_cifs_srv_netbiosname != self . cifs . cifs_srv_netbiosname or <EOL> self . cifs . _original_cifs_srv_netbiosname_b != self . cifs . cifs_srv_netbiosname_b or <EOL> self . cifs . _original_cifs_srv_netbiosalias != self . cifs . cifs_srv_netbiosalias or <EOL> self . instance . _original_ad_allow_trusted_doms != self . instance . ad_allow_trusted_doms or <EOL> self . instance . _original_ad_use_default_domain != self . instance . ad_use_default_domain or <EOL> self . instance . _original_ad_unix_extensions != self . instance . ad_unix_extensions or <EOL> self . instance . _original_ad_verbose_logging != self . instance . ad_verbose_logging or <EOL> self . instance . _original_ad_bindname != self . instance . ad_bindname or <EOL> self . instance . _original_ad_bindpw != self . instance . ad_bindpw <EOL> ) : <EOL> return True <EOL> return False <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ActiveDirectoryForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . instance . ad_bindpw : <EOL> self . fields [ '<STR_LIT>' ] . required = False <EOL> self . cifs = CIFS . objects . latest ( '<STR_LIT:id>' ) <EOL> self . __original_save ( ) <EOL> self . fields [ "<STR_LIT>" ] . widget . attrs [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" <EOL> ) <EOL> self . fields [ "<STR_LIT>" ] . widget . attrs [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" <EOL> ) <EOL> if self . cifs : <EOL> self . fields [ '<STR_LIT>' ] . initial = self . cifs . cifs_srv_netbiosname <EOL> self . fields [ '<STR_LIT>' ] . initial = self . cifs . cifs_srv_netbiosname_b <EOL> self . fields [ '<STR_LIT>' ] . initial = self . cifs . cifs_srv_netbiosalias <EOL> _n = notifier ( ) <EOL> if not _n . is_freenas ( ) : <EOL> if _n . failover_licensed ( ) : <EOL> from freenasUI . failover . utils import node_label_field <EOL> node_label_field ( <EOL> _n . failover_node ( ) , <EOL> self . fields [ '<STR_LIT>' ] , <EOL> self . fields [ '<STR_LIT>' ] , <EOL> ) <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> def clean_ad_dcname ( self ) : <EOL> ad_dcname = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> ad_dcport = <NUM_LIT> <EOL> ad_ssl = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if ad_ssl == '<STR_LIT>' : <EOL> ad_dcport = <NUM_LIT> <EOL> if not ad_dcname : <EOL> return None <EOL> parts = ad_dcname . split ( '<STR_LIT::>' ) <EOL> ad_dcname = parts [ <NUM_LIT:0> ] <EOL> if len ( parts ) > <NUM_LIT:1> and parts [ <NUM_LIT:1> ] . isdigit ( ) : <EOL> ad_dcport = long ( parts [ <NUM_LIT:1> ] ) <EOL> errors = [ ] <EOL> try : <EOL> ret = FreeNAS_ActiveDirectory . port_is_listening ( <EOL> host = ad_dcname , port = ad_dcport , errors = errors <EOL> ) <EOL> if ret is False : <EOL> raise Exception ( <EOL> '<STR_LIT>' % errors [ <NUM_LIT:0> ] <EOL> ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( '<STR_LIT>' % e ) <EOL> return self . cleaned_data . get ( '<STR_LIT>' ) <EOL> def clean_ad_gcname ( self ) : <EOL> ad_gcname = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> ad_gcport = <NUM_LIT> <EOL> ad_ssl = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if ad_ssl == '<STR_LIT>' : <EOL> ad_gcport = <NUM_LIT> <EOL> if not ad_gcname : <EOL> return None <EOL> parts = ad_gcname . split ( '<STR_LIT::>' ) <EOL> ad_gcname = parts [ <NUM_LIT:0> ] <EOL> if len ( parts ) > <NUM_LIT:1> and parts [ <NUM_LIT:1> ] . isdigit ( ) : <EOL> ad_gcport = long ( parts [ <NUM_LIT:1> ] ) <EOL> errors = [ ] <EOL> try : <EOL> ret = FreeNAS_ActiveDirectory . port_is_listening ( <EOL> host = ad_gcname , port = ad_gcport , errors = errors <EOL> ) <EOL> if ret is False : <EOL> raise Exception ( <EOL> '<STR_LIT>' % errors [ <NUM_LIT:0> ] <EOL> ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( '<STR_LIT>' % e ) <EOL> return self . cleaned_data . get ( '<STR_LIT>' ) <EOL> def clean_ad_netbiosname_a ( self ) : <EOL> netbiosname = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> try : <EOL> validate_netbios_names ( netbiosname ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( e ) <EOL> return netbiosname <EOL> def clean_ad_netbiosname_b ( self ) : <EOL> netbiosname_a = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> netbiosname = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if not netbiosname : <EOL> return netbiosname <EOL> if netbiosname_a and netbiosname_a == netbiosname : <EOL> raise forms . ValidationError ( _ ( <EOL> '<STR_LIT>' <EOL> ) ) <EOL> try : <EOL> validate_netbios_names ( netbiosname ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( e ) <EOL> return netbiosname <EOL> def clean ( self ) : <EOL> cdata = self . cleaned_data <EOL> domain = cdata . get ( "<STR_LIT>" ) <EOL> bindname = cdata . get ( "<STR_LIT>" ) <EOL> binddn = "<STR_LIT>" % ( bindname , domain ) <EOL> bindpw = cdata . get ( "<STR_LIT>" ) <EOL> site = cdata . get ( "<STR_LIT>" ) <EOL> netbiosname = cdata . get ( "<STR_LIT>" ) <EOL> netbiosname_b = cdata . get ( "<STR_LIT>" ) <EOL> ssl = cdata . get ( "<STR_LIT>" ) <EOL> certificate = cdata [ "<STR_LIT>" ] <EOL> ad_kerberos_principal = cdata [ "<STR_LIT>" ] <EOL> workgroup = None <EOL> if certificate : <EOL> certificate = certificate . get_certificate_path ( ) <EOL> args = { <EOL> '<STR_LIT>' : domain , <EOL> '<STR_LIT>' : site , <EOL> '<STR_LIT>' : ssl , <EOL> '<STR_LIT>' : certificate <EOL> } <EOL> if not cdata . get ( "<STR_LIT>" ) : <EOL> bindpw = self . instance . ad_bindpw <EOL> cdata [ '<STR_LIT>' ] = bindpw <EOL> if not ad_kerberos_principal : <EOL> if not bindname : <EOL> raise forms . ValidationError ( "<STR_LIT>" ) <EOL> if not bindpw : <EOL> raise forms . ValidationError ( "<STR_LIT>" ) <EOL> errors = [ ] <EOL> try : <EOL> ret = FreeNAS_ActiveDirectory . validate_credentials ( <EOL> domain , site = site , ssl = ssl , certfile = certificate , <EOL> binddn = binddn , bindpw = bindpw , errors = errors <EOL> ) <EOL> if ret is False : <EOL> raise forms . ValidationError ( "<STR_LIT>" % errors [ <NUM_LIT:0> ] ) <EOL> except FreeNAS_ActiveDirectory_Exception , e : <EOL> raise forms . ValidationError ( '<STR_LIT>' % e ) <EOL> args [ '<STR_LIT>' ] = binddn <EOL> args [ '<STR_LIT>' ] = bindpw <EOL> else : <EOL> args [ '<STR_LIT>' ] = ad_kerberos_principal . principal_name <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> workgroup = FreeNAS_ActiveDirectory . get_workgroup_name ( ** args ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( e ) <EOL> if workgroup : <EOL> if compare_netbios_names ( netbiosname , workgroup , None ) : <EOL> raise forms . ValidationError ( _ ( <EOL> "<STR_LIT>" <EOL> ) ) <EOL> if netbiosname_b : <EOL> if compare_netbios_names ( netbiosname_b , workgroup , None ) : <EOL> raise forms . ValidationError ( _ ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) ) <EOL> else : <EOL> log . warn ( "<STR_LIT>" ) <EOL> if ssl in ( "<STR_LIT>" , None ) : <EOL> return cdata <EOL> if not certificate : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" ) <EOL> return cdata <EOL> def save ( self ) : <EOL> enable = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if self . __original_changed ( ) : <EOL> notifier ( ) . _clear_activedirectory_config ( ) <EOL> started = notifier ( ) . started ( "<STR_LIT>" ) <EOL> obj = super ( ActiveDirectoryForm , self ) . save ( ) <EOL> self . cifs . cifs_srv_netbiosname = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> self . cifs . cifs_srv_netbiosname_b = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> self . cifs . cifs_srv_netbiosalias = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> self . cifs . save ( ) <EOL> if enable : <EOL> if started is True : <EOL> started = notifier ( ) . restart ( "<STR_LIT>" ) <EOL> if started is False : <EOL> started = notifier ( ) . start ( "<STR_LIT>" ) <EOL> if started is False : <EOL> self . instance . ad_enable = False <EOL> super ( ActiveDirectoryForm , self ) . save ( ) <EOL> raise ServiceFailed ( <EOL> "<STR_LIT>" , <EOL> _ ( "<STR_LIT>" ) , <EOL> ) <EOL> else : <EOL> if started is True : <EOL> started = notifier ( ) . stop ( "<STR_LIT>" ) <EOL> return obj <EOL> class NISForm ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . NIS <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( NISForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ "<STR_LIT>" ] . widget . attrs [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" <EOL> ) <EOL> def save ( self ) : <EOL> enable = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> started = notifier ( ) . started ( "<STR_LIT>" ) <EOL> super ( NISForm , self ) . save ( ) <EOL> if enable : <EOL> if started is True : <EOL> started = notifier ( ) . restart ( "<STR_LIT>" ) <EOL> if started is False : <EOL> started = notifier ( ) . start ( "<STR_LIT>" ) <EOL> if started is False : <EOL> self . instance . ad_enable = False <EOL> super ( NISForm , self ) . save ( ) <EOL> raise ServiceFailed ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> if started is True : <EOL> started = notifier ( ) . stop ( "<STR_LIT>" ) <EOL> class LDAPForm ( ModelForm ) : <EOL> ldap_netbiosname_a = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> ) <EOL> ldap_netbiosname_b = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> ) <EOL> ldap_netbiosalias = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False , <EOL> ) <EOL> advanced_fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> exclude = [ '<STR_LIT>' ] <EOL> model = models . LDAP <EOL> widgets = { <EOL> '<STR_LIT>' : forms . widgets . PasswordInput ( render_value = True ) , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( LDAPForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ "<STR_LIT>" ] . widget . attrs [ "<STR_LIT>" ] = ( <EOL> "<STR_LIT>" <EOL> ) <EOL> self . cifs = CIFS . objects . latest ( '<STR_LIT:id>' ) <EOL> if self . cifs : <EOL> self . fields [ '<STR_LIT>' ] . initial = self . cifs . cifs_srv_netbiosname <EOL> self . fields [ '<STR_LIT>' ] . initial = self . cifs . cifs_srv_netbiosname_b <EOL> self . fields [ '<STR_LIT>' ] . initial = self . cifs . cifs_srv_netbiosalias <EOL> _n = notifier ( ) <EOL> if not _n . is_freenas ( ) : <EOL> if _n . failover_licensed ( ) : <EOL> from freenasUI . failover . utils import node_label_field <EOL> node_label_field ( <EOL> _n . failover_node ( ) , <EOL> self . fields [ '<STR_LIT>' ] , <EOL> self . fields [ '<STR_LIT>' ] , <EOL> ) <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> else : <EOL> del self . fields [ '<STR_LIT>' ] <EOL> def clean_ldap_bindpw ( self ) : <EOL> cdata = self . cleaned_data <EOL> if not cdata . get ( "<STR_LIT>" ) : <EOL> cdata [ "<STR_LIT>" ] = self . instance . ldap_bindpw <EOL> binddn = cdata . get ( "<STR_LIT>" ) <EOL> bindpw = cdata . get ( "<STR_LIT>" ) <EOL> basedn = cdata . get ( "<STR_LIT>" ) <EOL> hostname = cdata . get ( "<STR_LIT>" ) <EOL> ssl = cdata . get ( "<STR_LIT>" ) <EOL> port = <NUM_LIT> <EOL> if ssl == "<STR_LIT>" : <EOL> port = <NUM_LIT> <EOL> if hostname : <EOL> parts = hostname . split ( '<STR_LIT::>' ) <EOL> hostname = parts [ <NUM_LIT:0> ] <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> port = int ( parts [ <NUM_LIT:1> ] ) <EOL> errors = [ ] <EOL> certfile = None <EOL> if ssl in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> certificate = cdata [ "<STR_LIT>" ] <EOL> certfile = get_certificateauthority_path ( certificate ) <EOL> ret = FreeNAS_LDAP . validate_credentials ( <EOL> hostname , binddn = binddn , bindpw = bindpw , basedn = basedn , <EOL> port = port , certfile = certfile , ssl = ssl , errors = errors <EOL> ) <EOL> if ret is False : <EOL> raise forms . ValidationError ( "<STR_LIT>" % errors [ <NUM_LIT:0> ] ) <EOL> return bindpw <EOL> def check_for_samba_schema ( self ) : <EOL> self . clean_bindpw ( ) <EOL> cdata = self . cleaned_data <EOL> binddn = cdata . get ( "<STR_LIT>" ) <EOL> bindpw = cdata . get ( "<STR_LIT>" ) <EOL> basedn = cdata . get ( "<STR_LIT>" ) <EOL> hostname = cdata . get ( "<STR_LIT>" ) <EOL> certfile = None <EOL> ssl = cdata . get ( "<STR_LIT>" ) <EOL> if ssl in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> certificate = cdata [ "<STR_LIT>" ] <EOL> certfile = get_certificateauthority_path ( certificate ) <EOL> fl = FreeNAS_LDAP ( <EOL> host = hostname , <EOL> binddn = binddn , <EOL> bindpw = bindpw , <EOL> basedn = basedn , <EOL> certfile = certfile , <EOL> ssl = ssl <EOL> ) <EOL> if fl . has_samba_schema ( ) : <EOL> self . instance . ldap_has_samba_schema = True <EOL> else : <EOL> self . instance . ldap_has_samba_schema = False <EOL> def clean_ldap_netbiosname_a ( self ) : <EOL> netbiosname = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> try : <EOL> validate_netbios_names ( netbiosname ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( e ) <EOL> return netbiosname <EOL> def clean_ldap_netbiosname_b ( self ) : <EOL> netbiosname_a = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> netbiosname = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if not netbiosname : <EOL> return netbiosname <EOL> if netbiosname_a and netbiosname_a == netbiosname : <EOL> raise forms . ValidationError ( _ ( <EOL> '<STR_LIT>' <EOL> ) ) <EOL> try : <EOL> validate_netbios_names ( netbiosname ) <EOL> except Exception as e : <EOL> raise forms . ValidationError ( e ) <EOL> return netbiosname <EOL> def clean ( self ) : <EOL> cdata = self . cleaned_data <EOL> ssl = cdata . get ( "<STR_LIT>" ) <EOL> if ssl in ( "<STR_LIT>" , None ) : <EOL> return cdata <EOL> certificate = cdata [ "<STR_LIT>" ] <EOL> if not certificate : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" ) <EOL> return cdata <EOL> def save ( self ) : <EOL> enable = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> started = notifier ( ) . started ( "<STR_LIT>" ) <EOL> obj = super ( LDAPForm , self ) . save ( ) <EOL> self . cifs . cifs_srv_netbiosname = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> self . cifs . cifs_srv_netbiosname_b = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> self . cifs . cifs_srv_netbiosalias = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> self . cifs . save ( ) <EOL> if enable : <EOL> if started is True : <EOL> started = notifier ( ) . restart ( "<STR_LIT>" ) <EOL> if started is False : <EOL> started = notifier ( ) . start ( "<STR_LIT>" ) <EOL> if started is False : <EOL> self . instance . ldap_enable = False <EOL> super ( LDAPForm , self ) . save ( ) <EOL> raise ServiceFailed ( "<STR_LIT>" , _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> if started is True : <EOL> started = notifier ( ) . stop ( "<STR_LIT>" ) <EOL> return obj <EOL> def done ( self , request , events ) : <EOL> events . append ( "<STR_LIT>" ) <EOL> super ( LDAPForm , self ) . done ( request , events ) <EOL> class KerberosRealmForm ( ModelForm ) : <EOL> advanced_fields = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . KerberosRealm <EOL> def clean_krb_realm ( self ) : <EOL> krb_realm = self . cleaned_data . get ( "<STR_LIT>" , None ) <EOL> if krb_realm : <EOL> krb_realm = krb_realm . upper ( ) <EOL> return krb_realm <EOL> def save ( self ) : <EOL> super ( KerberosRealmForm , self ) . save ( ) <EOL> notifier ( ) . start ( "<STR_LIT>" ) <EOL> class KerberosKeytabCreateForm ( ModelForm ) : <EOL> keytab_file = FileField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = False <EOL> ) <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . KerberosKeytab <EOL> def clean_keytab_file ( self ) : <EOL> keytab_file = self . cleaned_data . get ( "<STR_LIT>" , None ) <EOL> if not keytab_file : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) <EOL> ) <EOL> encoded = None <EOL> if hasattr ( keytab_file , '<STR_LIT>' ) : <EOL> filename = keytab_file . temporary_file_path ( ) <EOL> with open ( filename , "<STR_LIT:r>" ) as f : <EOL> keytab_contents = f . read ( ) <EOL> encoded = base64 . b64encode ( keytab_contents ) <EOL> f . close ( ) <EOL> else : <EOL> filename = tempfile . mktemp ( dir = '<STR_LIT>' ) <EOL> with open ( filename , '<STR_LIT>' ) as f : <EOL> for c in keytab_file . chunks ( ) : <EOL> f . write ( c ) <EOL> f . close ( ) <EOL> with open ( filename , "<STR_LIT:r>" ) as f : <EOL> keytab_contents = f . read ( ) <EOL> encoded = base64 . b64encode ( keytab_contents ) <EOL> f . close ( ) <EOL> os . unlink ( filename ) <EOL> return encoded <EOL> def save_principals ( self , keytab ) : <EOL> if not keytab : <EOL> return False <EOL> keytab_file = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> regex = re . compile ( <EOL> '<STR_LIT>' <EOL> ) <EOL> tmpfile = tempfile . mktemp ( dir = "<STR_LIT>" ) <EOL> with open ( tmpfile , '<STR_LIT:w>' ) as f : <EOL> decoded = base64 . b64decode ( keytab_file ) <EOL> f . write ( decoded ) <EOL> f . close ( ) <EOL> ( res , out , err ) = run ( "<STR_LIT>" % tmpfile ) <EOL> if res != <NUM_LIT:0> : <EOL> log . debug ( "<STR_LIT>" , err ) <EOL> os . unlink ( tmpfile ) <EOL> return False <EOL> os . unlink ( tmpfile ) <EOL> ret = False <EOL> out = out . splitlines ( ) <EOL> if not out : <EOL> return False <EOL> for line in out : <EOL> line = line . strip ( ) <EOL> if not line : <EOL> continue <EOL> m = regex . match ( line ) <EOL> if m : <EOL> try : <EOL> kp = models . KerberosPrincipal ( ) <EOL> kp . principal_keytab = keytab <EOL> kp . principal_version = int ( m . group ( <NUM_LIT:1> ) ) <EOL> kp . principal_encryption = m . group ( <NUM_LIT:2> ) <EOL> kp . principal_name = m . group ( <NUM_LIT:4> ) <EOL> kp . principal_timestamp = m . group ( <NUM_LIT:5> ) <EOL> kp . save ( ) <EOL> ret = True <EOL> except Exception as e : <EOL> log . debug ( "<STR_LIT>" , e ) <EOL> ret = False <EOL> return ret <EOL> def save ( self ) : <EOL> obj = super ( KerberosKeytabCreateForm , self ) . save ( ) <EOL> if not self . save_principals ( obj ) : <EOL> obj . delete ( ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> notifier ( ) . start ( "<STR_LIT>" ) <EOL> class KerberosKeytabEditForm ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> exclude = [ '<STR_LIT>' ] <EOL> model = models . KerberosKeytab <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( KerberosKeytabEditForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT>' ] = True <EOL> self . fields [ '<STR_LIT>' ] . widget . attrs [ '<STR_LIT:class>' ] = ( <EOL> '<STR_LIT>' <EOL> ) <EOL> def save ( self ) : <EOL> super ( KerberosKeytabEditForm , self ) . save ( ) <EOL> notifier ( ) . start ( "<STR_LIT>" ) <EOL> class KerberosSettingsForm ( ModelForm ) : <EOL> class Meta : <EOL> fields = '<STR_LIT>' <EOL> model = models . KerberosSettings <EOL> def save ( self ) : <EOL> super ( KerberosSettingsForm , self ) . save ( ) <EOL> notifier ( ) . start ( "<STR_LIT>" ) </s>
