Skip to content

Commit e103c68

Browse files
authored
Merge pull request #19 from php/check-pw-for-persistent
Include password in persistent hash key
2 parents 276e7c0 + 4ba5961 commit e103c68

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

ibm_db2.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,21 @@ static void _php_db2_set_symbol(char * varname, zval *var)
351351
}
352352
/* }}} */
353353

354+
/* {{{ Murmur hash implementation (for persistent key hash)
355+
*/
354356

357+
static unsigned int _php_db2_MurmurOAAT32 (const char * key)
358+
{
359+
unsigned int h = 3323198485;
360+
for (;*key;++key) {
361+
h ^= *key;
362+
h *= 0x5bd1e995;
363+
h ^= h >> 15;
364+
}
365+
return h;
366+
}
355367

368+
/* }}} */
356369

357370
#ifdef PASE /* IBM i meta change ""->NULL */
358371
static void _php_db2_meta_helper(SQLCHAR **qualifier, size_t *qualifier_len,
@@ -2313,6 +2326,7 @@ static int _php_db2_connect_helper( INTERNAL_FUNCTION_PARAMETERS, conn_handle **
23132326
int reused = 0;
23142327
int hKeyLen = 0;
23152328
char *hKey = NULL;
2329+
unsigned int password_hashed;
23162330
char server[2048];
23172331
int attr = SQL_TRUE;
23182332
size_t database_len;
@@ -2354,10 +2368,13 @@ static int _php_db2_connect_helper( INTERNAL_FUNCTION_PARAMETERS, conn_handle **
23542368
/* Check if we already have a connection for this userID & database combination */
23552369
if (isPersistent) {
23562370
zend_resource *entry;
2357-
hKeyLen = strlen(database) + strlen(uid) + 8;
2371+
hKeyLen = strlen(database) + strlen(uid) +
2372+
sizeof("__db2_..FFFFFFFF"); /* constant part; includes null */
23582373
hKey = (char *) ecalloc(1, hKeyLen);
23592374

2360-
sprintf(hKey, "__db2_%s.%s", uid, database);
2375+
/* XXX: How do we include the options (array) in here too? */
2376+
password_hashed = _php_db2_MurmurOAAT32(password);
2377+
snprintf(hKey, hKeyLen, "__db2_%s.%s.%08x", uid, database, password_hashed);
23612378
temp = zend_hash_str_find_ptr(&EG(persistent_list), hKey, hKeyLen );
23622379
if ( temp && temp->type == le_pconn_struct) {
23632380
conn_res = *pconn_res = (conn_handle *)temp->ptr;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
IBM-DB2: db2_pconnect() - test persistent connection won't be reused with bad password
3+
--SKIPIF--
4+
<?php require_once('skipif.inc'); ?>
5+
--FILE--
6+
<?php
7+
8+
require_once('connection.inc');
9+
10+
/*
11+
* Use blatantly incorrect password, to make sure password is part of the
12+
* internal persistent connection hash.
13+
*/
14+
$conn1 = db2_pconnect($database, $user, $password);
15+
if ($conn1) {
16+
$conn2 = db2_pconnect($database, $user, "wrongbad");
17+
if ($conn2) {
18+
echo "A bad password was accepted.\n";
19+
db2_close($conn2);
20+
} else {
21+
echo "OK\n";
22+
}
23+
db2_close($conn1);
24+
}
25+
else {
26+
echo "Connection failed.\n";
27+
}
28+
29+
?>
30+
--EXPECT--
31+
OK

0 commit comments

Comments
 (0)