def _op_drop_database(self): if self.db_handle is None: raise OperationalError('_op_drop_database() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_drop_database) p.pack_int(self.db_handle) self.sock.send(p.get_buffer())
def _op_service_detach(self): if self.db_handle is None: raise OperationalError('_op_service_detach() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_service_detach) p.pack_int(self.db_handle) self.sock.send(p.get_buffer())
def _op_transaction(self, tpb): if self.db_handle is None: raise OperationalError('_op_transaction() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_transaction) p.pack_int(self.db_handle) p.pack_bytes(tpb) self.sock.send(p.get_buffer())
def _op_allocate_statement(self): if self.db_handle is None: raise OperationalError( '_op_allocate_statement() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_allocate_statement) p.pack_int(self.db_handle) self.sock.send(p.get_buffer())
def _op_cancel_events(self, event_id): if self.db_handle is None: raise OperationalError('_op_cancel_events() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_cancel_events) p.pack_int(self.db_handle) p.pack_int(event_id) self.sock.send(p.get_buffer())
def _op_service_start(self, param): if self.db_handle is None: raise OperationalError('_op_service_start() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_service_start) p.pack_int(self.db_handle) p.pack_int(0) p.pack_bytes(param) self.sock.send(p.get_buffer())
def _op_connect_request(self): if self.db_handle is None: raise OperationalError('_op_connect_request() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_connect_request) p.pack_int(1) # async p.pack_int(self.db_handle) p.pack_int(0) self.sock.send(p.get_buffer())
def _op_info_database(self, b): if self.db_handle is None: raise OperationalError('_op_info_database() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_info_database) p.pack_int(self.db_handle) p.pack_int(0) p.pack_bytes(b) p.pack_int(self.buffer_length) self.sock.send(p.get_buffer())
def _op_service_info(self, param, item, buffer_length=512): if self.db_handle is None: raise OperationalError('_op_service_info() Invalid db handle') p = xdrlib.Packer() p.pack_int(self.op_service_info) p.pack_int(self.db_handle) p.pack_int(0) p.pack_bytes(param) p.pack_bytes(item) p.pack_int(buffer_length) self.sock.send(p.get_buffer())
def _parse_op_response(self): b = self.recv_channel(16) h = bytes_to_bint(b[0:4]) # Object handle oid = b[4:12] # Object ID buf_len = bytes_to_bint(b[12:]) # buffer length buf = self.recv_channel(buf_len, word_alignment=True) (gds_codes, sql_code, message) = self._parse_status_vector() if sql_code or message: raise OperationalError(message, gds_codes, sql_code) return (h, oid, buf)
def _execute(self, stmt_handle, params): cooked_params = self._convert_params(params) self.transaction.connection._op_execute(stmt_handle, self.transaction.trans_handle, cooked_params) try: (h, oid, buf) = self.transaction.connection._op_response() except OperationalError: e = sys.exc_info()[1] if 335544665 in e.gds_codes: raise IntegrityError(e._message, e.gds_codes, e.sql_code) raise OperationalError(e._message, e.gds_codes, e.sql_code)
def _op_exec_immediate(self, trans_handle, query): if self.db_handle is None: raise OperationalError('_op_exec_immediate() Invalid db handle') desc_items = bs([]) p = xdrlib.Packer() p.pack_int(self.op_exec_immediate) p.pack_int(trans_handle) p.pack_int(self.db_handle) p.pack_int(3) # dialect = 3 p.pack_string(self.str_to_bytes(query)) p.pack_bytes(desc_items) p.pack_int(self.buffer_length) self.sock.send(p.get_buffer())
def _op_accept(self): b = self.recv_channel(4) while bytes_to_bint(b) == self.op_dummy: b = self.recv_channel(4) if bytes_to_bint(b) == self.op_reject: raise OperationalError('Connection is rejected', None, None) assert bytes_to_bint(b) == self.op_accept b = self.recv_channel(12) up = xdrlib.Unpacker(b) assert up.unpack_int() == 10 assert up.unpack_int() == 1 assert up.unpack_int() == 3 up.done()
def uid(self, auth_plugin_name, wire_crypt): def pack_cnct_param(k, v): if k != CNCT_specific_data: return bs([k] + [len(v)]) + v # specific_data split per 254 bytes b = b'' i = 0 while len(v) > 254: b += bs([k, 255, i]) + v[:254] v = v[254:] i += 1 b += bs([k, len(v) + 1, i]) + v return b auth_plugin_list = ('Srp256', 'Srp', 'Legacy_Auth') # get and calculate CNCT_xxxx values if sys.platform == 'win32': user = os.environ['USERNAME'] hostname = os.environ['COMPUTERNAME'] else: user = os.environ.get('USER', '') hostname = socket.gethostname() if auth_plugin_name in ('Srp256', 'Srp'): self.client_public_key, self.client_private_key = srp.client_seed() specific_data = bytes_to_hex(srp.long2bytes( self.client_public_key)) elif auth_plugin_name == 'Legacy_Auth': assert crypt, "Legacy_Auth needs crypt module" specific_data = self.str_to_bytes(get_crypt(self.password)) else: raise OperationalError("Unknown auth plugin name '%s'" % (auth_plugin_name, )) self.plugin_name = auth_plugin_name self.plugin_list = b','.join( [s.encode('utf-8') for s in auth_plugin_list]) client_crypt = b'\x01\x00\x00\x00' if wire_crypt else b'\x00\x00\x00\x00' # set CNCT_xxxx values r = b'' r += pack_cnct_param(CNCT_login, self.str_to_bytes(self.user)) r += pack_cnct_param(CNCT_plugin_name, self.str_to_bytes(self.plugin_name)) r += pack_cnct_param(CNCT_plugin_list, self.plugin_list) r += pack_cnct_param(CNCT_specific_data, specific_data) r += pack_cnct_param(CNCT_client_crypt, client_crypt) r += pack_cnct_param(CNCT_user, self.str_to_bytes(user)) r += pack_cnct_param(CNCT_host, self.str_to_bytes(hostname)) r += pack_cnct_param(CNCT_user_verification, b'') return r
def _op_response(self): b = self.recv_channel(4) while bytes_to_bint(b) == self.op_dummy: b = self.recv_channel(4) op_code = bytes_to_bint(b) while op_code == self.op_response and self.lazy_response_count: self.lazy_response_count -= 1 h, oid, buf = self._parse_op_response() b = self.recv_channel(4) if op_code == self.op_cont_auth: raise OperationalError('Unauthorized') elif op_code != self.op_response: raise InternalError("_op_response:op_code = %d" % (op_code, )) return self._parse_op_response()
def _op_que_events(self, event_names, event_id): if self.db_handle is None: raise OperationalError('_op_que_events() Invalid db handle') params = bs([1]) for name, n in event_names.items(): params += bs([len(name)]) params += self.str_to_bytes(name) params += int_to_bytes(n, 4) p = xdrlib.Packer() p.pack_int(self.op_que_events) p.pack_int(self.db_handle) p.pack_bytes(params) p.pack_int(0) # ast p.pack_int(0) # args p.pack_int(event_id) self.sock.send(p.get_buffer())
def recv_channel(self, nbytes, word_alignment=False): n = nbytes if word_alignment and (n % 4): n += 4 - nbytes % 4 # 4 bytes word alignment r = bytes([]) while n: if (os.name != 'java' and self.timeout is not None and select.select([self.sock], [], [], self.timeout)[0] == []): break b = self.sock.recv(n) if not b: break r += b n -= len(b) if len(r) < nbytes: raise OperationalError('Can not recv() packets', None, None) return r[:nbytes]
def _parse_op_response(self): b = self.recv_channel(16) h = bytes_to_bint(b[0:4]) # Object handle oid = b[4:12] # Object ID buf_len = bytes_to_bint(b[12:]) # buffer length buf = self.recv_channel(buf_len, word_alignment=True) (gds_codes, sql_code, message) = self._parse_status_vector() if gds_codes.intersection([ 335544838, 335544879, 335544880, 335544466, 335544665, 335544347, 335544558 ]): raise IntegrityError(message, gds_codes, sql_code) elif gds_codes.intersection([335544321]): warnings.warn(message) elif (sql_code or message) and not gds_codes.intersection([335544434]): raise OperationalError(message, gds_codes, sql_code) return (h, oid, buf)
def execute(self, query, params=[]): DEBUG_OUTPUT("Cursor::execute()", query, params) self.transaction.check_trans_handle() stmt = self._get_stmt(query) cooked_params = self._convert_params(params) if stmt.stmt_type == isc_info_sql_stmt_exec_procedure: self.transaction.connection._op_execute2( stmt.handle, self.transaction.trans_handle, cooked_params, calc_blr(stmt.xsqlda)) self._callproc_result = self.transaction.connection._op_sql_response(stmt.xsqlda) self.transaction.connection._op_response() self._fetch_records = None else: DEBUG_OUTPUT( "Cursor::execute() _op_execute()", stmt.handle, self.transaction.trans_handle) self.transaction.connection._op_execute( stmt.handle, self.transaction.trans_handle, cooked_params) try: (h, oid, buf) = self.transaction.connection._op_response() except OperationalError as e: self._fetch_records = None self._callproc_result = None if e.gds_codes.intersection([335544665, 335544466, 335544838, 335544347]): raise IntegrityError(e._message, e.gds_codes, e.sql_code) if e.sql_code == -303: warnings.warn(e._message) return self else: raise OperationalError(e._message, e.gds_codes, e.sql_code) if stmt.stmt_type == isc_info_sql_stmt_select: self._fetch_records = _fetch_generator(stmt) stmt.open() else: self._fetch_records = None self._callproc_result = None self.transaction.is_dirty = True return self
def _parse_connect_response(self): # want and treat op_accept or op_cond_accept or op_accept_data b = self.recv_channel(4) while bytes_to_bint(b) == self.op_dummy: b = self.recv_channel(4) if bytes_to_bint(b) == self.op_reject: raise OperationalError('Connection is rejected') op_code = bytes_to_bint(b) if op_code == self.op_response: return self._parse_op_response() # error occured b = self.recv_channel(12) self.accept_version = byte_to_int(b[3]) self.accept_architecture = bytes_to_bint(b[4:8]) self.accept_type = bytes_to_bint(b[8:]) self.lazy_response_count = 0 if op_code == self.op_cond_accept or op_code == self.op_accept_data: ln = bytes_to_bint(self.recv_channel(4)) data = self.recv_channel(ln, word_alignment=True) ln = bytes_to_bint(self.recv_channel(4)) self.accept_plugin_name = self.recv_channel(ln, word_alignment=True) is_authenticated = bytes_to_bint(self.recv_channel(4)) ln = bytes_to_bint(self.recv_channel(4)) self.recv_channel(ln, word_alignment=True) # keys if is_authenticated == 0: if self.accept_plugin_name in (b'Srp256', b'Srp'): if self.accept_plugin_name == b'Srp256': hash_algo = hashlib.sha256 elif self.accept_plugin_name == b'Srp': hash_algo = hashlib.sha1 else: raise OperationalError('Unknown auth plugin %s' % (self.accept_plugin_name)) user = self.user if len(user) > 2 and user[0] == user[-1] == '"': user = user[1:-1] user = user.replace('""', '"') else: user = user.upper() if len(data) == 0: raise OperationalError('Unauthorized') ln = bytes_to_int(data[:2]) server_salt = data[2:ln + 2] server_public_key = srp.bytes2long( hex_to_bytes(data[4 + ln:])) auth_data, session_key = srp.client_proof( self.str_to_bytes(user), self.str_to_bytes(self.password), server_salt, self.client_public_key, server_public_key, self.client_private_key, hash_algo) elif self.accept_plugin_name == b'Legacy_Auth': auth_data = self.str_to_bytes(get_crypt(self.password)) else: raise OperationalError('Unauthorized') if self.wire_crypt: # send op_cont_auth p = xdrlib.Packer() p.pack_int(self.op_cont_auth) p.pack_string(bytes_to_hex(auth_data)) p.pack_bytes(self.accept_plugin_name) p.pack_bytes(self.plugin_list) p.pack_bytes(b'') self.sock.send(p.get_buffer()) (h, oid, buf) = self._op_response() # op_crypt: plugin[Arc4] key[Symmetric] p = xdrlib.Packer() p.pack_int(self.op_crypt) p.pack_string(b'Arc4') p.pack_string(b'Symmetric') self.sock.send(p.get_buffer()) self.sock.set_translator(ARC4.new(session_key), ARC4.new(session_key)) (h, oid, buf) = self._op_response() else: # use later _op_attach() and _op_create() self.auth_data = auth_data else: assert op_code == self.op_accept
def _op_accept(self): b = self.recv_channel(4) while bytes_to_bint(b) == self.op_dummy: b = self.recv_channel(4) if bytes_to_bint(b) == self.op_reject: raise OperationalError('Connection is rejected') op_code = bytes_to_bint(b) if op_code == self.op_response: return self._parse_op_response() # error occured b = self.recv_channel(12) self.accept_version = byte_to_int(b[3]) self.accept_architecture = bytes_to_bint(b[4:8]) self.accept_type = bytes_to_bint(b[8:]) self.lazy_response_count = 0 if op_code == self.op_cond_accept or op_code == self.op_accept_data: read_length = 0 ln = bytes_to_bint(self.recv_channel(4)) data = self.recv_channel(ln, word_alignment=True) ln = bytes_to_bint(self.recv_channel(4)) self.accept_plugin_name = self.recv_channel(ln, word_alignment=True) is_authenticated = bytes_to_bint(self.recv_channel(4)) read_length += 4 ln = bytes_to_bint(self.recv_channel(4)) self.recv_channel(ln, word_alignment=True) # keys if is_authenticated == 0: if self.accept_plugin_name == b'Srp': ln = bytes_to_int(data[:2]) server_salt = data[2:ln + 2] server_public_key = srp.bytes2long( hex_to_bytes(data[4 + ln:])) auth_data, session_key = srp.client_proof( self.str_to_bytes(self.user.upper()), self.str_to_bytes(self.password), server_salt, self.client_public_key, server_public_key, self.client_private_key) elif self.accept_plugin_name == b'Legacy_Auth': auth_data = get_crypt(self.password) else: raise OperationalError('Unauthorized') if self.wire_crypt: # send op_cont_auth p = xdrlib.Packer() p.pack_int(self.op_cont_auth) p.pack_string(bytes_to_hex(auth_data)) p.pack_bytes(self.accept_plugin_name) p.pack_bytes(self.plugin_list) p.pack_bytes(b'') self.sock.send(p.get_buffer()) (h, oid, buf) = self._op_response() # op_crypt: plugin[Arc4] key[Symmetric] p = xdrlib.Packer() p.pack_int(self.op_crypt) p.pack_string(b'Arc4') p.pack_string(b'Symmetric') self.sock.send(p.get_buffer()) self.sock.set_translator(ARC4.new(session_key), ARC4.new(session_key)) (h, oid, buf) = self._op_response() else: # use later _op_attach() and _op_create() self.auth_data = auth_data else: assert op_code == self.op_accept