Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def from_cursor(cursor, connection=None, object_def=None, custom_validator=None):
if connection is None:
connection = cursor.connection
if object_def is None:
object_def = cursor.definition
writer = Writer(connection, object_def, attributes=cursor.attributes)
for entry in cursor.entries:
if isinstance(cursor, Reader):
entry.entry_writable(object_def, writer, custom_validator=custom_validator)
elif isinstance(cursor, Writer):
pass
else:
error_message = 'unknown cursor type %s' % str(type(cursor))
if log_enabled(ERROR):
log(ERROR, '%s', error_message)
raise LDAPCursorError(error_message)
writer.execution_time = cursor.execution_time
if log_enabled(BASIC):
log(BASIC, 'instantiated Writer Cursor <%r> from cursor <%r>', writer, cursor)
return writer
raise LDAPInvalidPortError('port must be an integer')
self.host = hostname
elif url_given and self.host.startswith('['):
hostname, sep, hostport = self.host[1:].partition(']')
if sep != ']' or not self._is_ipv6(hostname):
if log_enabled(ERROR):
log(ERROR, 'invalid IPv6 server address for <%s>', self.host)
raise LDAPInvalidServerError()
if len(hostport):
if not hostport.startswith(':'):
if log_enabled(ERROR):
log(ERROR, 'invalid URL in server name for <%s>', self.host)
raise LDAPInvalidServerError('invalid URL in server name')
if not hostport[1:].isdecimal():
if log_enabled(ERROR):
log(ERROR, 'port must be an integer for <%s>', self.host)
raise LDAPInvalidPortError('port must be an integer')
port = int(hostport[1:])
self.host = hostname
elif not url_given and self._is_ipv6(self.host):
pass
elif self.host.count(':') > 1:
if log_enabled(ERROR):
log(ERROR, 'invalid server address for <%s>', self.host)
raise LDAPInvalidServerError()
if not self.ipc:
self.host.rstrip('/')
if not use_ssl and not port:
port = 389
elif use_ssl and not port:
port = 636
if isinstance(response, int): # asynchronous strategy
return_value = response
if log_enabled(PROTOCOL):
log(PROTOCOL, 'async SEARCH response id <%s> received via <%s>', return_value, self)
else:
return_value = True if self.result['type'] == 'searchResDone' and len(response) > 0 else False
if not return_value and self.result['result'] not in [RESULT_SUCCESS] and not self.last_error:
self.last_error = self.result['description']
if log_enabled(PROTOCOL):
for entry in response:
if entry['type'] == 'searchResEntry':
log(PROTOCOL, 'SEARCH response entry <%s> received via <%s>', entry, self)
elif entry['type'] == 'searchResRef':
log(PROTOCOL, 'SEARCH response reference <%s> received via <%s>', entry, self)
if log_enabled(BASIC):
log(BASIC, 'done SEARCH operation, result <%s>', return_value)
return return_value
elif local_private_key_password:
if log_enabled(ERROR):
log(ERROR, 'cannot use local private key password, SSLContext not available')
raise LDAPSSLNotSupportedError('cannot use local private key password, SSLContext is not available')
else:
self.private_key_password = None
self.version = version
self.private_key_file = local_private_key_file
self.certificate_file = local_certificate_file
self.valid_names = valid_names
self.ciphers = ciphers
self.sni = sni
if log_enabled(BASIC):
log(BASIC, 'instantiated Tls: <%r>' % self)
with pool.pool_lock:
pool._incoming[counter] = (e, None, None)
# pool._incoming[counter] = (type(e)(str(e)), None, None)
# except LDAPOperationResult as e: # raise_exceptions has raised an exception. It must be redirected to the original connection thread
# exc = e
# with pool.pool_lock:
# if exc:
# pool._incoming[counter] = (exc, None, None)
# else:
# pool._incoming[counter] = (response, result, BaseStrategy.decode_request(message_type, request, controls))
self.worker.busy = False
pool.request_queue.task_done()
self.worker.task_counter += 1
if log_enabled(BASIC):
log(BASIC, 'thread terminated')
if self.master_connection.usage:
pool.terminated_usage += self.worker.connection.usage
self.worker.running = False
if log_enabled(PROTOCOL):
log(PROTOCOL, 'NTLM SICILY RESPONSE NTLM request sent via <%s>', self)
response = self.post_send_single_response(self.send('bindRequest', request, controls))
if not self.strategy.sync:
_, result = self.get_response(response)
else:
if log_enabled(PROTOCOL):
log(PROTOCOL, 'NTLM BIND response <%s> received via <%s>', response[0], self)
result = response[0]
else:
result = None
finally:
self.sasl_in_progress = False
if log_enabled(BASIC):
log(BASIC, 'done SASL NTLM operation, result <%s>', result)
return result
entry['attributes'] = dict(response['attributes'])
if raw:
if not include_empty:
# needed for python 2.6 compatibility
entry['raw_attributes'] = dict((key, response['raw_attributes'][key]) for key in response['raw_attributes'] if response['raw:attributes'][key])
else:
entry['raw'] = dict(response['raw_attributes'])
json_dict['entries'].append(entry)
if str is bytes: # Python 2
check_json_dict(json_dict)
json_output = json.dumps(json_dict, ensure_ascii=True, sort_keys=sort, indent=indent, check_circular=True, default=format_json, separators=(',', ': '))
if log_enabled(BASIC):
log(BASIC, 'building JSON output <%s> for <%s>', json_output, self)
if stream:
stream.write(json_output)
return json_output
Close connection
"""
if log_enabled(NETWORK):
log(NETWORK, 'closing connection for <%s>', self.connection)
if self.connection.lazy and not self.connection._executing_deferred and (self.connection._deferred_bind or self.connection._deferred_open):
self.connection.listening = False
self.connection.closed = True
if log_enabled(NETWORK):
log(NETWORK, 'deferred connection closed for <%s>', self.connection)
else:
if not self.connection.closed:
self._stop_listen()
if not self. no_real_dsa:
self._close_socket()
if log_enabled(NETWORK):
log(NETWORK, 'connection closed for <%s>', self.connection)
self.connection.bound = False
self.connection.request = None
self.connection.response = None
self.connection.tls_started = False
self._outstanding = None
self._referrals = []
if not self.connection.strategy.no_real_dsa:
self.connection.server.current_address = None
if self.connection.usage:
self.connection._usage.stop()
if log_enabled(BASIC):
log(BASIC, 'tls started for <%s>', connection)
return True
else:
if log_enabled(BASIC):
log(BASIC, 'tls not started for <%s>', connection)
return False
else:
if connection.result['description'] not in ['success']:
# startTLS failed
connection.last_error = 'startTLS failed - ' + str(connection.result['description'])
if log_enabled(ERROR):
log(ERROR, '%s for <%s>', connection.last_error, connection)
raise LDAPStartTLSError(connection.last_error)
if log_enabled(BASIC):
log(BASIC, 'tls started for <%s>', connection)
return self._start_tls(connection)
attrs_to_remove = []
# removes original empty attribute in case a range tag is returned
for attribute_type in entry['attributes']:
if ';range' in attribute_type.lower():
orig_attr, _, _ = attribute_type.partition(';')
attrs_to_remove.append(orig_attr)
for attribute_type in attrs_to_remove:
if log_enabled(PROTOCOL):
log(PROTOCOL, 'attribute type <%s> removed in response because of same attribute returned as range by the server in <%s>', attribute_type, self)
del entry['raw_attributes'][attribute_type]
del entry['attributes'][attribute_type]
request = self._outstanding.pop(message_id)
else:
if log_enabled(ERROR):
log(ERROR, 'message id not in outstanding queue for <%s>', self.connection)
raise(LDAPResponseTimeoutError('message id not in outstanding queue'))
if get_request:
return response, result, request
else:
return response, result