Package easyLDAP :: Module easyLDAP_bind
[frames] | no frames]

Source Code for Module easyLDAP.easyLDAP_bind

  1  # -*- coding: utf-8 -*- 
  2   
  3  #    easyLDAP - a python library that makes LDAP management easier than before... 
  4  #    Copyright (C) 2004-2010,2020  Mike Gabriel <m.gabriel@das-netzwerkteam.de> 
  5  # 
  6  #    This program is free software: you can redistribute it and/or modify 
  7  #    it under the terms of the GNU General Public License as published by 
  8  #    the Free Software Foundation, either version 3 of the License, or 
  9  #    (at your option) any later version. 
 10  # 
 11  #    This program is distributed in the hope that it will be useful, 
 12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #    GNU General Public License for more details. 
 15  # 
 16  #    You should have received a copy of the GNU General Public License 
 17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18  # 
 19   
 20  from easyLDAP_defaults import * 
 21  import types, getpass 
 22   
23 -def easyLDAP_setDefaultCredentials(bind_dn,bind_pw=None, config_defaults=EASY_LDAP):
24 """ 25 easyLDAP_setDefaultCredentials('myDN', 'myPW') 26 27 sets the easyLDAP default value dictionary entries as 28 follows: 29 30 EASY_LDAP['BindDN'] = 'myDN' 31 EASY_LDAP['BindPW'] = 'myPW' 32 33 Now you can call the functions 34 35 easyLDAPdefaultBind(obj) 36 easyLDAPdefaultBind_norefresh(obj) 37 38 and the bind process will be performed automagically 39 with the default values. 40 41 If 'myPW' is not specified, you will be prompted for it 42 on STDOUT/STDIN. 43 """ 44 config_defaults['BindDN'] = str(bind_dn) 45 if bind_pw != None: 46 config_defaults['BindPW'] = str(bind_pw) 47 else: 48 usePW = getpass.getpass('Enter ldap password (%s): ' % (bind_dn)) 49 config_defaults['BindPW'] = usePW 50 return None
51
52 -def easyLDAP_setDefaultBindCredentials(bind_pw=None, config_defaults=EASY_LDAP):
53 """ 54 easyLDAP_setDefaultBindCredentials('myPW') 55 56 sets the easyLDAP default value dictionary entries as 57 follows: 58 59 EASY_LDAP['BindDN'] = 'myDN' 60 EASY_LDAP['BindPW'] = 'myPW' 61 62 Now you can call the functions 63 64 easyLDAPdefaultBind(obj) 65 easyLDAPdefaultBind_norefresh(obj) 66 67 and the bind process will be performed automagically 68 with the default values. 69 70 If 'myPW' is not specified, you will be prompted for it 71 on STDOUT/STDIN. 72 """ 73 if bind_pw is None: 74 bind_pw = config_defaults['BindPW'] 75 easyLDAPsetDefaultCredentials(config_defaults['BindDN'],bind_pw=bind_pw, config_defaults=config_defaults) 76 return None
77 78
79 -def easyLDAP_setDefaultAdminCredentials(bind_pw=None, config_defaults=EASY_LDAP):
80 """ 81 easyLDAP_setDefaultAdminCredentials('myAdminPW') 82 83 sets the easyLDAP default value dictionary entries as 84 follows: 85 86 EASY_LDAP['BindDN'] = config_defaults['AdminRDN']+','+ 87 config_defaults['BaseDN'] 88 EASY_LDAP['BindPW'] = 'myAdminPW' 89 90 Now you can call the functions 91 92 easyLDAPdefaultBind(my_easyLDAPobject) 93 easyLDAPdefaultBind_norefresh(my_easyLDAPobject) 94 95 and the administrative bind process will be performed 96 automagically with these default values. 97 98 If 'myAdminPW' is not specified and EASY_LDAP['BindPW'] 99 contains an empty string, a password dialog will be 100 initiated on STDIN/STDOUT. This dialog can be forced 101 by passing a non-string as 'myAdminPW' (e.g. explicitly: 102 None). 103 """ 104 admindn = config_defaults['AdminRDN'] + ',' + config_defaults['BaseDN'] 105 config_defaults['BindDN'] = admindn 106 if (bind_pw != '') and (type(bind_pw) is types.StringType): 107 config_defaults['BindPW'] = str(bind_pw) 108 else: 109 if (config_defaults['BindPW'] == '') or (type(bind_pw) is not types.StringType): 110 config_defaults['BindPW'] = getpass.getpass('Enter ldap password (%s): ' % (admindn))
111 112
113 -def easyLDAP_defaultBind(easyLDAP_instance, config_defaults=EASY_LDAP):
114 """ 115 easyLDAP_defaultBind(INSTANCE) 116 117 binds the given object to the LDAP server. This helper function 118 requires the dictionary entry EASY_LDAP['BindDN'] to be set. 119 120 If so, it will check the dict entry EASY_LDAP['BindPW'] for a 121 non empty string. If so, it will bind the given easyLDAP object 122 with the standard credentials, defined by the named dictionary 123 entries. If the bind password is not globally specified, the 124 function will ask for the password at the command line. 125 126 This function uses refreshes the easyLDAP object's cache. 127 The result of the binding method will be returned by this 128 function. 129 """ 130 if config_defaults['BindDN'] != '': 131 useDN = config_defaults['BindDN'] 132 if config_defaults['BindPW'] == '': 133 usePW = getpass.getpass('Enter ldap password (%s): ' % (useDN)) 134 else: 135 usePW = config_defaults['BindPW'] 136 return easyLDAP_instance.bind(useDN,usePW) 137 return easyLDAP_instance.bind('','')
138 139
140 -def easyLDAP_defaultBind_norefresh(easyLDAP_instance, config_defaults=EASY_LDAP):
141 """ 142 easyLDAP_defaultBind_norefresh(INSTANCE) 143 144 binds the given object to the LDAP server. This helper function 145 requires the dictionary entry EASY_LDAP['BindDN'] to be set. 146 147 If so, it will check the dict entry EASY_LDAP['BindPW'] for a 148 non-empty string. If so, it will bind the given easyLDAP object 149 with the standard credentials, defined by the named dictionary 150 entries. If the bind password is not globally specified, the 151 function will ask for the password at the command line. 152 153 This function does _NOT_ refresh the easyLDAP object's cache. 154 Some values of the original LDAP object might be missing in 155 the easyLDAP object cache as a consequence of restrictions 156 of the previously used credentials. 157 158 The result of the binding method will be returned by this 159 function. 160 """ 161 if config_defaults['BindDN'] != '': 162 useDN = config_defaults['BindDN'] 163 if config_defaults['BindPW'] == '': 164 usePW = getpass.getpass('Enter ldap password (%s): ' % (useDN)) 165 else: 166 usePW = config_defaults['BindPW'] 167 168 return easyLDAP_instance.bind_norefresh(useDN,usePW) 169 return easyLDAP_instance.bind_norefresh('','')
170 171 # for compatibility 172 easyLDAPsetDefaultCredentials = easyLDAP_setDefaultCredentials 173 easyLDAPsetDefaultBindCredentials = easyLDAP_setDefaultBindCredentials 174 easyLDAPsetDefaultAdminCredentials = easyLDAP_setDefaultAdminCredentials 175 easyLDAPdefaultBind = easyLDAP_defaultBind 176 easyLDAPdefaultBind_norefresh = easyLDAP_defaultBind_norefresh 177 178 if __name__=='__main__': 179 pass 180