|
35 |
import re |
35 |
import re |
36 |
import threading |
36 |
import threading |
37 |
import gc |
37 |
import gc |
|
|
38 |
import sys |
38 |
|
39 |
|
39 |
from univention.management.console import Translation |
40 |
from univention.management.console import Translation |
40 |
from univention.management.console.modules import UMC_OptionTypeError, UMC_OptionMissing, UMC_CommandError |
41 |
from univention.management.console.modules import UMC_OptionTypeError, UMC_OptionMissing, UMC_CommandError |
|
47 |
import univention.admin.syntax as udm_syntax |
48 |
import univention.admin.syntax as udm_syntax |
48 |
import univention.admin.uexceptions as udm_errors |
49 |
import univention.admin.uexceptions as udm_errors |
49 |
|
50 |
|
50 |
from ...config import ucr |
51 |
from univention.management.console.config import ucr |
51 |
from ...log import MODULE |
52 |
from univention.management.console.log import MODULE |
52 |
|
53 |
|
53 |
from .syntax import widget, default_value |
54 |
from .syntax import widget, default_value |
54 |
|
55 |
|
|
92 |
class LDAP_ConnectionError( Exception ): |
93 |
class LDAP_ConnectionError( Exception ): |
93 |
pass |
94 |
pass |
94 |
|
95 |
|
|
|
96 |
|
95 |
def LDAP_Connection( func ): |
97 |
def LDAP_Connection( func ): |
96 |
"""This decorator function provides an open LDAP connection that can |
98 |
"""This decorator function provides an open LDAP connection that can |
97 |
be accessed via the variable ldap_connection and a vaild position |
99 |
be accessed via the variable ldap_connection and a vaild position |
98 |
within the LDAP directory in the viariable ldap_position. It reuses |
100 |
within the LDAP directory in the viariable ldap_position. It reuses |
99 |
an already open connection or creates a new one. If the function |
101 |
an already open connection or creates a new one. |
100 |
fails with an LDAP error the decorators tries to reopen the LDAP |
|
|
101 |
connection and invokes the function again. if it still fails an |
102 |
LDAP_ConnectionError is raised. |
103 |
|
102 |
|
104 |
When using the decorator the method get to additional keyword arguments. |
103 |
When using the decorator the method get to additional keyword arguments. |
105 |
|
104 |
|
106 |
example: |
105 |
example: |
107 |
@LDAP_Connection |
106 |
@LDAP_Connection |
108 |
def do_ldap_stuff(, arg1, arg2, ldap_connection = None, ldap_positio = None ): |
107 |
def do_ldap_stuff(self, arg1, arg2, ldap_connection=None, ldap_position=None): |
109 |
... |
108 |
... |
110 |
ldap_connection.searchDn( ..., position = ldap_position ) |
109 |
ldap_connection.searchDn(..., position=ldap_position) |
111 |
... |
110 |
... |
112 |
""" |
111 |
""" |
113 |
def wrapper_func( *args, **kwargs ): |
112 |
def wrapper_func( *args, **kwargs ): |
114 |
global _ldap_connection, _ldap_position, _user_dn, _password, _licenseCheck |
113 |
global _ldap_connection, _ldap_position, _user_dn, _password, _licenseCheck |
115 |
|
114 |
|
116 |
if _ldap_connection is not None: |
115 |
if _ldap_connection is None: |
117 |
MODULE.info( 'Using open LDAP connection for user %s' % _user_dn ) |
|
|
118 |
lo = _ldap_connection |
119 |
po = _ldap_position |
120 |
else: |
121 |
MODULE.info( 'Opening LDAP connection for user %s' % _user_dn ) |
116 |
MODULE.info( 'Opening LDAP connection for user %s' % _user_dn ) |
122 |
try: |
117 |
try: |
123 |
lo = udm_uldap.access( host = ucr.get( 'ldap/master' ), base = ucr.get( 'ldap/base' ), binddn = _user_dn, bindpw = _password ) |
118 |
lo = udm_uldap.access(host=ucr.get('ldap/master'), base=ucr.get('ldap/base'), binddn=_user_dn, bindpw=_password) |
|
|
119 |
except LDAPError as exc: |
120 |
raise LDAP_ConnectionError, str(exc), sys.exc_info()[2] |
124 |
|
121 |
|
125 |
# license check (see also univention.admin.uldap.access.bind()) |
122 |
# license check (see also univention.admin.uldap.access.bind()) |
126 |
if not GPLversion: |
123 |
if not GPLversion: |
127 |
try: |
124 |
InvalidLicense = ( |
128 |
_licenseCheck = univention.admin.license.init_select(lo, 'admin') |
125 |
univention.admin.uexceptions.licenseInvalid, |
129 |
if _licenseCheck in range(1, 5) or _licenseCheck in range(6,12): |
126 |
univention.admin.uexceptions.licenseNotFound, |
130 |
lo.allow_modify = 0 |
127 |
univention.admin.uexceptions.licenseExpired, |
131 |
if _licenseCheck is not None: |
128 |
univention.admin.uexceptions.licenseWrongBaseDn, |
132 |
lo.requireLicense() |
129 |
) |
133 |
except univention.admin.uexceptions.licenseInvalid: |
130 |
try: |
|
|
131 |
_licenseCheck = univention.admin.license.init_select(lo, 'admin') |
132 |
if _licenseCheck in range(1, 5) or _licenseCheck in range(6, 12): |
134 |
lo.allow_modify = 0 |
133 |
lo.allow_modify = 0 |
|
|
134 |
if _licenseCheck is not None: |
135 |
lo.requireLicense() |
135 |
lo.requireLicense() |
136 |
except univention.admin.uexceptions.licenseNotFound: |
136 |
except InvalidLicense: |
137 |
lo.allow_modify = 0 |
137 |
lo.allow_modify = 0 |
138 |
lo.requireLicense() |
138 |
lo.requireLicense() |
139 |
except univention.admin.uexceptions.licenseExpired: |
|
|
140 |
lo.allow_modify = 0 |
141 |
lo.requireLicense() |
142 |
except univention.admin.uexceptions.licenseWrongBaseDn: |
143 |
lo.allow_modify = 0 |
144 |
lo.requireLicense() |
145 |
|
139 |
|
146 |
po = udm_uldap.position( lo.base ) |
140 |
po = udm_uldap.position(lo.base) |
147 |
except udm_errors.noObject, e: |
|
|
148 |
raise e |
149 |
except LDAPError, e: |
150 |
raise LDAP_ConnectionError( 'Opening LDAP connection failed: %s' % str( e ) ) |
151 |
|
141 |
|
152 |
kwargs[ 'ldap_connection' ] = lo |
142 |
kwargs['ldap_connection'] = lo |
153 |
kwargs[ 'ldap_position' ] = po |
143 |
kwargs['ldap_position'] = po |
154 |
try: |
144 |
ret = func(*args, **kwargs) |
155 |
ret = func( *args, **kwargs ) |
145 |
_ldap_connection = lo |
156 |
_ldap_connection = lo |
146 |
_ldap_position = po |
157 |
_ldap_position = po |
147 |
return ret |
158 |
return ret |
|
|
159 |
except (udm_errors.ldapSizelimitExceeded, udm_errors.ldapTimeout), e: |
160 |
raise e |
161 |
except ( LDAPError, udm_errors.base ), e: |
162 |
MODULE.info( 'LDAP operation for user %s has failed' % _user_dn ) |
163 |
try: |
164 |
lo = udm_uldap.access( host = ucr.get( 'ldap/master' ), base = ucr.get( 'ldap/base' ), binddn= _user_dn, bindpw = _password ) |
165 |
lo.requireLicense() |
166 |
po = udm_uldap.position( lo.base ) |
167 |
except udm_errors.noObject, e: |
168 |
raise e |
169 |
except ( LDAPError, udm_errors.base ), e: |
170 |
raise LDAP_ConnectionError( 'Opening LDAP connection failed: %s' % str( e ) ) |
171 |
|
148 |
|
172 |
kwargs[ 'ldap_connection' ] = lo |
|
|
173 |
kwargs[ 'ldap_position' ] = po |
174 |
try: |
175 |
ret = func( *args, **kwargs ) |
176 |
_ldap_connection = lo |
177 |
_ldap_position = po |
178 |
return ret |
179 |
except (udm_errors.ldapSizelimitExceeded, udm_errors.ldapTimeout), e: |
180 |
raise e |
181 |
except udm_errors.base, e: |
182 |
raise LDAP_ConnectionError( str( e ) ) |
183 |
|
184 |
return [] |
185 |
|
186 |
return wrapper_func |
149 |
return wrapper_func |
187 |
|
150 |
|
|
|
151 |
|
188 |
# exceptions |
152 |
# exceptions |
189 |
class UDM_Error( Exception ): |
153 |
class UDM_Error( Exception ): |
190 |
pass |
154 |
pass |
|
1083 |
module_search_options = {'scope' : 'base', 'container' : key} |
1047 |
module_search_options = {'scope' : 'base', 'container' : key} |
1084 |
try: |
1048 |
try: |
1085 |
return read_syntax_choices( syntax_name, {}, module_search_options ) |
1049 |
return read_syntax_choices( syntax_name, {}, module_search_options ) |
1086 |
except LDAP_ConnectionError: |
1050 |
except LDAPError: |
1087 |
# invalid DN |
1051 |
# invalid DN |
1088 |
return [] |
1052 |
return [] |
1089 |
if syn.key is not None: |
1053 |
if syn.key is not None: |