Skip to content

Commit 134fab9

Browse files
committed
Fixes bug with schemaid in header + closes gh-44
1 parent b29e149 commit 134fab9

File tree

8 files changed

+265
-237
lines changed

8 files changed

+265
-237
lines changed

src/tarantool.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,11 +812,11 @@ PHP_METHOD(tarantool_class, authenticate) {
812812
char *login; int login_len;
813813
char *passwd; int passwd_len;
814814

815-
TARANTOOL_PARSE_PARAMS(id, "ss", &login, &login_len,
815+
TARANTOOL_PARSE_PARAMS(id, "s|s", &login, &login_len,
816816
&passwd, &passwd_len);
817817
TARANTOOL_FETCH_OBJECT(obj, id);
818818
obj->login = pestrdup(login, 1);
819-
obj->passwd = estrdup(passwd);
819+
obj->passwd = (passwd ? estrdup(passwd) : NULL);
820820
TARANTOOL_CONNECT_ON_DEMAND(obj, id);
821821

822822
__tarantool_authenticate(obj);
@@ -974,7 +974,7 @@ PHP_METHOD(tarantool_class, delete) {
974974
key_new = pack_key(key, 0);
975975

976976
long sync = TARANTOOL_G(sync_counter)++;
977-
php_tp_encode_delete(obj->value, sync, space_no, index_no, key);
977+
php_tp_encode_delete(obj->value, sync, space_no, index_no, key_new);
978978
if (key != key_new) {
979979
zval_ptr_dtor(&key_new);
980980
}

src/tarantool_proto.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ static void php_tp_pack_header(smart_str *str, size_t size,
2121
php_mp_pack_long(str, sync);
2222
}
2323

24-
size_t php_tp_sizeof_auth(uint32_t sync, size_t ulen, zend_bool guest) {
24+
size_t php_tp_sizeof_auth(uint32_t sync, size_t ulen, zend_bool nopass) {
2525
size_t val = php_tp_sizeof_header(TNT_AUTH, sync) +
2626
php_mp_sizeof_hash(2) +
2727
php_mp_sizeof_long(TNT_USERNAME) +
2828
php_mp_sizeof_string(ulen) +
2929
php_mp_sizeof_long(TNT_TUPLE) +
30-
php_mp_sizeof_array((guest ? 0 : 2));
31-
if (!guest) {
30+
php_mp_sizeof_array((nopass ? 0 : 2));
31+
if (!nopass) {
3232
val += php_mp_sizeof_string(9) +
3333
php_mp_sizeof_string(PHP_SCRAMBLE_SIZE) ;
3434
}
@@ -41,17 +41,17 @@ void php_tp_encode_auth(
4141
char * const username,
4242
size_t username_len,
4343
char * const scramble) {
44-
zend_bool guest = (strncmp(username, "guest", 6) == 0);
45-
size_t packet_size = php_tp_sizeof_auth(sync, username_len, guest);
44+
zend_bool nopass = (zend_bool )(scramble != NULL);
45+
size_t packet_size = php_tp_sizeof_auth(sync, username_len, nopass);
4646
smart_str_ensure(str, packet_size + 5);
4747
php_tp_pack_header(str, packet_size, TNT_AUTH, sync);
4848

4949
php_mp_pack_hash(str, 2);
5050
php_mp_pack_long(str, TNT_USERNAME);
5151
php_mp_pack_string(str, username, username_len);
5252
php_mp_pack_long(str, TNT_TUPLE);
53-
php_mp_pack_array(str, (guest ? 0 : 2));
54-
if (!guest) {
53+
php_mp_pack_array(str, (nopass ? 0 : 2));
54+
if (!nopass) {
5555
php_mp_pack_string(str, "chap-sha1", 9);
5656
php_mp_pack_string(str, scramble, PHP_SCRAMBLE_SIZE);
5757
}

src/third_party/tp.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,8 @@ tp_scramble_prepare(void *out, const void *salt, const void *password,
12731273
static inline char *
12741274
tp_auth(struct tp *p, const char *salt_base64, const char *user, int ulen, const char *pass, int plen)
12751275
{
1276-
char guest = (strncmp(user, "guest", 6) == 0);
1276+
//char guest = (strncmp(user, "guest", 6) == 0);
1277+
char nopass = (plen == 0);
12771278
int sz = 5 +
12781279
mp_sizeof_map(2) +
12791280
mp_sizeof_uint(TP_CODE) +
@@ -1284,7 +1285,7 @@ tp_auth(struct tp *p, const char *salt_base64, const char *user, int ulen, const
12841285
mp_sizeof_uint(TP_USERNAME) +
12851286
mp_sizeof_str(ulen) +
12861287
mp_sizeof_uint(TP_TUPLE);
1287-
if (!guest)
1288+
if (!nopass)
12881289
sz += mp_sizeof_array(2) +
12891290
mp_sizeof_str(0) +
12901291
mp_sizeof_str(SCRAMBLE_SIZE);
@@ -1305,7 +1306,7 @@ tp_auth(struct tp *p, const char *salt_base64, const char *user, int ulen, const
13051306
h = mp_encode_uint(h, TP_USERNAME);
13061307
h = mp_encode_str(h, user, ulen);
13071308
h = mp_encode_uint(h, TP_TUPLE);
1308-
if (!guest) {
1309+
if (!nopass) {
13091310
h = mp_encode_array(h, 2);
13101311
h = mp_encode_str(h, 0, 0);
13111312

@@ -1698,8 +1699,6 @@ tp_reply(struct tpresponse *r, const char * const buf, size_t size)
16981699
return -1;
16991700
r->code = mp_decode_uint(&p);
17001701
break;
1701-
default:
1702-
return -1;
17031702
}
17041703
r->bitmap |= (1ULL << key);
17051704
}
@@ -1716,23 +1715,21 @@ tp_reply(struct tpresponse *r, const char * const buf, size_t size)
17161715
while (n-- > 0) {
17171716
uint32_t key = mp_decode_uint(&p);
17181717
switch (key) {
1719-
case TP_ERROR: {
1718+
case TP_ERROR:
17201719
if (mp_typeof(*p) != MP_STR)
17211720
return -1;
17221721
uint32_t elen = 0;
17231722
r->error = mp_decode_str(&p, &elen);
17241723
r->error_end = r->error + elen;
17251724
break;
1726-
}
1727-
case TP_DATA: {
1725+
case TP_DATA:
17281726
if (mp_typeof(*p) != MP_ARRAY)
17291727
return -1;
17301728
r->data = p;
17311729
mp_next(&p);
17321730
r->data_end = p;
17331731
break;
17341732
}
1735-
}
17361733
r->bitmap |= (1ULL << key);
17371734
}
17381735
if (r->data) {

test/CreateTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,25 @@ public function test_06_bad_cridentials()
9696
$this->assertTrue($c->ping());
9797
$c->authenticate('test', 'bad_password');
9898
}
99+
100+
/**
101+
* @expectedException Exception
102+
* @expectedExceptionMessage Query error
103+
*/
104+
public function test_07_bad_guest_cridentials()
105+
{
106+
$c = new Tarantool('localhost', self::$port);
107+
$c->connect();
108+
$this->assertTrue($c->ping());
109+
$c->authenticate('guest', 'guest');
110+
}
111+
112+
public function test_08_good_cridentials()
113+
{
114+
$c = new Tarantool('localhost', self::$port);
115+
$c->connect();
116+
$this->assertTrue($c->ping());
117+
$c->authenticate('guest');
118+
$this->assertTrue($c->ping());
119+
}
99120
}

0 commit comments

Comments
 (0)