Esempio n. 1
0
 def _op_create(self, page_size=4096):
     dpb = bs([1])
     s = self.str_to_bytes(self.charset)
     dpb += bs([isc_dpb_set_db_charset, len(s)]) + s
     dpb += bs([isc_dpb_lc_ctype, len(s)]) + s
     s = self.str_to_bytes(self.user)
     dpb += bs([isc_dpb_user_name, len(s)]) + s
     if self.accept_version < PROTOCOL_VERSION13:
         enc_pass = get_crypt(self.password)
         if self.accept_version == PROTOCOL_VERSION10 or not enc_pass:
             s = self.str_to_bytes(self.password)
             dpb += bs([isc_dpb_password, len(s)]) + s
         else:
             enc_pass = self.str_to_bytes(enc_pass)
             dpb += bs([isc_dpb_password_enc, len(enc_pass)]) + enc_pass
     if self.role:
         s = self.str_to_bytes(self.role)
         dpb += bs([isc_dpb_sql_role_name, len(s)]) + s
     if self.auth_data:
         s = bytes_to_hex(self.auth_data)
         dpb += bs([isc_dpb_specific_auth_data, len(s)]) + s
     if self.timezone:
         s = self.str_to_bytes(self.timezone)
         dpb += bs([isc_dpb_session_time_zone, len(s)]) + s
     dpb += bs([isc_dpb_sql_dialect, 4]) + int_to_bytes(3, 4)
     dpb += bs([isc_dpb_force_write, 4]) + int_to_bytes(1, 4)
     dpb += bs([isc_dpb_overwrite, 4]) + int_to_bytes(1, 4)
     dpb += bs([isc_dpb_page_size, 4]) + int_to_bytes(page_size, 4)
     p = xdrlib.Packer()
     p.pack_int(self.op_create)
     p.pack_int(0)  # Database Object ID
     p.pack_bytes(self.str_to_bytes(self.filename))
     p.pack_bytes(dpb)
     self.sock.send(p.get_buffer())
Esempio n. 2
0
 def _op_attach(self):
     dpb = bs([isc_dpb_version1])
     s = self.str_to_bytes(self.charset)
     dpb += bs([isc_dpb_lc_ctype, len(s)]) + s
     s = self.str_to_bytes(self.user)
     dpb += bs([isc_dpb_user_name, len(s)]) + s
     if self.accept_version < PROTOCOL_VERSION13:
         enc_pass = get_crypt(self.password)
         if self.accept_version == PROTOCOL_VERSION10 or not enc_pass:
             s = self.str_to_bytes(self.password)
             dpb += bs([isc_dpb_password, len(s)]) + s
         else:
             enc_pass = self.str_to_bytes(enc_pass)
             dpb += bs([isc_dpb_password_enc, len(enc_pass)]) + enc_pass
     if self.role:
         s = self.str_to_bytes(self.role)
         dpb += bs([isc_dpb_sql_role_name, len(s)]) + s
     dpb += bs([isc_dpb_process_id, 4]) + int_to_bytes(os.getpid(), 4)
     s = self.str_to_bytes(sys.argv[0])
     dpb += bs([isc_dpb_process_name, len(s)]) + s
     if self.auth_data:
         s = bytes_to_hex(self.auth_data)
         dpb += bs([isc_dpb_specific_auth_data, len(s)]) + s
     if self.timezone:
         s = self.str_to_bytes(self.timezone)
         dpb += bs([isc_dpb_session_time_zone, len(s)]) + s
     p = xdrlib.Packer()
     p.pack_int(self.op_attach)
     p.pack_int(0)  # Database Object ID
     p.pack_bytes(self.str_to_bytes(self.filename))
     p.pack_bytes(dpb)
     self.sock.send(p.get_buffer())
Esempio n. 3
0
 def _op_detach(self):
     if self.db_handle is None:
         raise OperationalError('_op_detach() Invalid db handle')
     p = xdrlib.Packer()
     p.pack_int(self.op_detach)
     p.pack_int(self.db_handle)
     self.sock.send(p.get_buffer())
Esempio n. 4
0
 def _op_get_segment(self, blob_handle):
     p = xdrlib.Packer()
     p.pack_int(self.op_get_segment)
     p.pack_int(blob_handle)
     p.pack_int(self.buffer_length)
     p.pack_int(0)
     self.sock.send(p.get_buffer())
Esempio n. 5
0
 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())
Esempio n. 6
0
 def _op_info_transaction(self, trans_handle, b):
     p = xdrlib.Packer()
     p.pack_int(self.op_info_transaction)
     p.pack_int(trans_handle)
     p.pack_int(0)
     p.pack_bytes(b)
     p.pack_int(self.buffer_length)
     self.sock.send(p.get_buffer())
Esempio n. 7
0
 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())
Esempio n. 8
0
 def _op_info_sql(self, stmt_handle, vars):
     p = xdrlib.Packer()
     p.pack_int(self.op_info_sql)
     p.pack_int(stmt_handle)
     p.pack_int(0)
     p.pack_bytes(vars)
     p.pack_int(self.buffer_length)
     self.sock.send(p.get_buffer())
Esempio n. 9
0
 def _op_fetch(self, stmt_handle, blr):
     p = xdrlib.Packer()
     p.pack_int(self.op_fetch)
     p.pack_int(stmt_handle)
     p.pack_bytes(blr)
     p.pack_int(0)
     p.pack_int(400)
     self.sock.send(p.get_buffer())
Esempio n. 10
0
 def _op_create_blob2(self, trans_handle):
     p = xdrlib.Packer()
     p.pack_int(self.op_create_blob2)
     p.pack_int(0)
     p.pack_int(trans_handle)
     p.pack_int(0)
     p.pack_int(0)
     self.sock.send(p.get_buffer())
Esempio n. 11
0
 def _op_cont_auth(self, auth_data, auth_plugin_name, auth_plugin_list,
                   keys):
     p = xdrlib.Packer()
     p.pack_int(self.op_cont_auth)
     p.pack_bytes(bytes_to_hex(auth_data))
     p.pack_bytes(auth_plugin_name)
     p.pack_bytes(auth_plugin_list)
     p.pack_bytes(keys)
     self.sock.send(p.get_buffer())
Esempio n. 12
0
 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())
Esempio n. 13
0
 def _op_put_segment(self, blob_handle, seg_data):
     ln = len(seg_data)
     p = xdrlib.Packer()
     p.pack_int(self.op_put_segment)
     p.pack_int(blob_handle)
     p.pack_int(ln)
     p.pack_int(ln)
     pad_length = (4 - ln) & 3
     self.sock.send(p.get_buffer() + seg_data + bs([0]) * pad_length)
Esempio n. 14
0
 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())
Esempio n. 15
0
 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())
Esempio n. 16
0
 def _op_batch_segments(self, blob_handle, seg_data):
     ln = len(seg_data)
     p = xdrlib.Packer()
     p.pack_int(self.op_batch_segments)
     p.pack_int(blob_handle)
     p.pack_int(ln + 2)
     p.pack_int(ln + 2)
     pad_length = ((4 - (ln + 2)) & 3)
     self.sock.send(p.get_buffer() + int_to_bytes(ln, 2) + seg_data +
                    bs([0]) * pad_length)
Esempio n. 17
0
 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())
Esempio n. 18
0
    def _op_execute2(self, stmt_handle, trans_handle, params, output_blr):
        p = xdrlib.Packer()
        p.pack_int(self.op_execute2)
        p.pack_int(stmt_handle)
        p.pack_int(trans_handle)

        if len(params) == 0:
            values = b''
            p.pack_bytes(bs([]))
            p.pack_int(0)
            p.pack_int(0)
        else:
            (blr, values) = self.params_to_blr(trans_handle, params)
            p.pack_bytes(blr)
            p.pack_int(0)
            p.pack_int(1)

        q = xdrlib.Packer()
        q.pack_bytes(output_blr)
        q.pack_int(0)
        self.sock.send(p.get_buffer() + values + q.get_buffer())
Esempio n. 19
0
 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_bytes(self.str_to_bytes(query))
     p.pack_bytes(desc_items)
     p.pack_int(self.buffer_length)
     self.sock.send(p.get_buffer())
Esempio n. 20
0
 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())
Esempio n. 21
0
    def _op_execute(self, stmt_handle, trans_handle, params):
        p = xdrlib.Packer()
        p.pack_int(self.op_execute)
        p.pack_int(stmt_handle)
        p.pack_int(trans_handle)

        if len(params) == 0:
            p.pack_bytes(bs([]))
            p.pack_int(0)
            p.pack_int(0)
            self.sock.send(p.get_buffer())
        else:
            (blr, values) = self.params_to_blr(trans_handle, params)
            p.pack_bytes(blr)
            p.pack_int(0)
            p.pack_int(1)
            self.sock.send(p.get_buffer() + values)
Esempio n. 22
0
 def _op_prepare_statement(self,
                           stmt_handle,
                           trans_handle,
                           query,
                           option_items=None):
     if option_items is None:
         option_items = bs([])
     desc_items = option_items + bs([isc_info_sql_stmt_type
                                     ]) + INFO_SQL_SELECT_DESCRIBE_VARS
     p = xdrlib.Packer()
     p.pack_int(self.op_prepare_statement)
     p.pack_int(trans_handle)
     p.pack_int(stmt_handle)
     p.pack_int(3)  # dialect = 3
     p.pack_bytes(self.str_to_bytes(query))
     p.pack_bytes(desc_items)
     p.pack_int(self.buffer_length)
     self.sock.send(p.get_buffer())
Esempio n. 23
0
    def _op_connect(self, auth_plugin_name, wire_crypt):
        protocols = [
            # PROTOCOL_VERSION, Arch type (Generic=1), min, max, weight
            '0000000a00000001000000000000000500000002',  # 10, 1, 0, 5, 2
            'ffff800b00000001000000000000000500000004',  # 11, 1, 0, 5, 4
            'ffff800c00000001000000000000000500000006',  # 12, 1, 0, 5, 6
            'ffff800d00000001000000000000000500000008',  # 13, 1, 0, 5, 8
        ]
        p = xdrlib.Packer()
        p.pack_int(self.op_connect)
        p.pack_int(self.op_attach)
        p.pack_int(3)  # CONNECT_VERSION
        p.pack_int(1)  # arch_generic
        p.pack_bytes(self.str_to_bytes(self.filename if self.filename else ''))

        p.pack_int(len(protocols))
        p.pack_bytes(self.uid(auth_plugin_name, wire_crypt))
        self.sock.send(p.get_buffer() + hex_to_bytes(''.join(protocols)))
Esempio n. 24
0
 def _op_service_attach(self):
     spb = bs([2, 2])
     s = self.str_to_bytes(self.user)
     spb += bs([isc_spb_user_name, len(s)]) + s
     if self.accept_version < PROTOCOL_VERSION13:
         enc_pass = get_crypt(self.password)
         if self.accept_version == PROTOCOL_VERSION10 or not enc_pass:
             s = self.str_to_bytes(self.password)
             spb += bs([isc_dpb_password, len(s)]) + s
         else:
             enc_pass = self.str_to_bytes(enc_pass)
             spb += bs([isc_dpb_password_enc, len(enc_pass)]) + enc_pass
     if self.auth_data:
         s = self.str_to_bytes(bytes_to_hex(self.auth_data))
         spb += bs([isc_dpb_specific_auth_data, len(s)]) + s
     spb += bs(
         [isc_spb_dummy_packet_interval, 0x04, 0x78, 0x0a, 0x00, 0x00])
     p = xdrlib.Packer()
     p.pack_int(self.op_service_attach)
     p.pack_int(0)
     p.pack_bytes(b'service_mgr')
     p.pack_bytes(spb)
     self.sock.send(p.get_buffer())
Esempio n. 25
0
 def _op_open_blob(self, blob_id, trans_handle):
     p = xdrlib.Packer()
     p.pack_int(self.op_open_blob)
     p.pack_int(trans_handle)
     self.sock.send(p.get_buffer() + blob_id)
Esempio n. 26
0
 def _op_close_blob(self, blob_handle):
     p = xdrlib.Packer()
     p.pack_int(self.op_close_blob)
     p.pack_int(blob_handle)
     self.sock.send(p.get_buffer())
Esempio n. 27
0
 def _op_free_statement(self, stmt_handle, mode):
     p = xdrlib.Packer()
     p.pack_int(self.op_free_statement)
     p.pack_int(stmt_handle)
     p.pack_int(mode)
     self.sock.send(p.get_buffer())
Esempio n. 28
0
 def _op_rollback_retaining(self, trans_handle):
     p = xdrlib.Packer()
     p.pack_int(self.op_rollback_retaining)
     p.pack_int(trans_handle)
     self.sock.send(p.get_buffer())
Esempio n. 29
0
 def _op_commit(self, trans_handle):
     p = xdrlib.Packer()
     p.pack_int(self.op_commit)
     p.pack_int(trans_handle)
     self.sock.send(p.get_buffer())
Esempio n. 30
0
    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'):
                    hash_algo = {
                        b'Srp256': hashlib.sha256,
                        b'Srp': hashlib.sha1,
                    }[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:
                        # send op_cont_auth
                        self._op_cont_auth(
                            srp.long2bytes(self.client_public_key),
                            self.accept_plugin_name, self.plugin_list, b'')
                        # parse op_cont_auth
                        b = self.recv_channel(4)
                        assert bytes_to_bint(b) == self.op_cont_auth
                        ln = bytes_to_bint(self.recv_channel(4))
                        data = self.recv_channel(ln, word_alignment=True)
                        ln = bytes_to_bint(self.recv_channel(4))
                        plugin_name = self.recv_channel(ln,
                                                        word_alignment=True)
                        ln = bytes_to_bint(self.recv_channel(4))
                        plugin_list = self.recv_channel(ln,
                                                        word_alignment=True)
                        ln = bytes_to_bint(self.recv_channel(4))
                        keys = self.recv_channel(ln, word_alignment=True)

                    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))
                    session_key = b''
                else:
                    raise OperationalError('Unknown auth plugin %s' %
                                           (self.accept_plugin_name))
            else:
                auth_data = b''
                session_key = b''

            if op_code == self.op_cond_accept:
                self._op_cont_auth(auth_data, self.accept_plugin_name,
                                   self.plugin_list, b'')
                (h, oid, buf) = self._op_response()

            if self.wire_crypt and session_key:
                # op_crypt: plugin[Arc4] key[Symmetric]
                p = xdrlib.Packer()
                p.pack_int(self.op_crypt)
                p.pack_bytes(b'Arc4')
                p.pack_bytes(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