Skip to content

Commit 797ceec

Browse files
committed
Changed the same as odbc_connect
Add condition when authentication information is null
1 parent f5a990d commit 797ceec

File tree

1 file changed

+58
-28
lines changed

1 file changed

+58
-28
lines changed

ext/pdo_odbc/odbc_driver.c

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -500,36 +500,65 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
500500

501501
use_direct = 1;
502502

503-
/* Force UID and PWD to be set in the DSN */
504-
bool is_uid_set = dbh->username && *dbh->username
505-
&& !strstr(dbh->data_source, "uid=")
506-
&& !strstr(dbh->data_source, "UID=");
507-
bool is_pwd_set = dbh->password && *dbh->password
508-
&& !strstr(dbh->data_source, "pwd=")
509-
&& !strstr(dbh->data_source, "PWD=");
510-
if (is_uid_set && is_pwd_set) {
511-
char *uid = NULL, *pwd = NULL;
512-
bool should_quote_uid = !php_odbc_connstr_is_quoted(dbh->username) && php_odbc_connstr_should_quote(dbh->username);
513-
bool should_quote_pwd = !php_odbc_connstr_is_quoted(dbh->password) && php_odbc_connstr_should_quote(dbh->password);
514-
if (should_quote_uid) {
515-
size_t estimated_length = php_odbc_connstr_estimate_quote_length(dbh->username);
516-
uid = emalloc(estimated_length);
517-
php_odbc_connstr_quote(uid, dbh->username, estimated_length);
518-
} else {
519-
uid = dbh->username;
503+
size_t db_len = strlen(dbh->data_source);
504+
bool use_uid_arg = dbh->username != NULL && !php_memnistr(dbh->data_source, "uid=", strlen("uid="), dbh->data_source + db_len);
505+
bool use_pwd_arg = dbh->password != NULL && !php_memnistr(dbh->data_source, "pwd=", strlen("pwd="), dbh->data_source + db_len);
506+
507+
if (use_uid_arg || use_pwd_arg) {
508+
char *db = (char*) emalloc(db_len + 1);
509+
strcpy(db, dbh->data_source);
510+
char *db_end = db + db_len;
511+
db_end--;
512+
if ((unsigned char)*(db_end) == ';') {
513+
*db_end = '\0';
520514
}
521-
if (should_quote_pwd) {
522-
size_t estimated_length = php_odbc_connstr_estimate_quote_length(dbh->password);
523-
pwd = emalloc(estimated_length);
524-
php_odbc_connstr_quote(pwd, dbh->password, estimated_length);
525-
} else {
526-
pwd = dbh->password;
515+
516+
char *uid = NULL, *pwd = NULL, *dsn;
517+
bool should_quote_uid, should_quote_pwd;
518+
size_t new_dsn_size;
519+
520+
if (use_uid_arg) {
521+
should_quote_uid = !php_odbc_connstr_is_quoted(dbh->username) && php_odbc_connstr_should_quote(dbh->username);
522+
if (should_quote_uid) {
523+
size_t estimated_length = php_odbc_connstr_estimate_quote_length(dbh->username);
524+
uid = emalloc(estimated_length);
525+
php_odbc_connstr_quote(uid, dbh->username, estimated_length);
526+
} else {
527+
uid = dbh->username;
528+
}
529+
530+
if (!use_pwd_arg) {
531+
new_dsn_size = strlen(db) + strlen(uid) + strlen(";UID=;") + 1;
532+
dsn = pemalloc(new_dsn_size, dbh->is_persistent);
533+
snprintf(dsn, new_dsn_size, "%s;UID=%s;", db, uid);
534+
}
535+
}
536+
537+
if (use_pwd_arg) {
538+
should_quote_pwd = !php_odbc_connstr_is_quoted(dbh->password) && php_odbc_connstr_should_quote(dbh->password);
539+
if (should_quote_pwd) {
540+
size_t estimated_length = php_odbc_connstr_estimate_quote_length(dbh->password);
541+
pwd = emalloc(estimated_length);
542+
php_odbc_connstr_quote(pwd, dbh->password, estimated_length);
543+
} else {
544+
pwd = dbh->password;
545+
}
546+
547+
if (!use_uid_arg) {
548+
new_dsn_size = strlen(db) + strlen(pwd) + strlen(";PWD=;") + 1;
549+
dsn = pemalloc(new_dsn_size, dbh->is_persistent);
550+
snprintf(dsn, new_dsn_size, "%s;PWD=%s;", db, pwd);
551+
}
527552
}
528-
size_t new_dsn_size = strlen(dbh->data_source)
529-
+ strlen(uid) + strlen(pwd)
530-
+ strlen(";UID=;PWD=") + 1;
531-
char *dsn = pemalloc(new_dsn_size, dbh->is_persistent);
532-
snprintf(dsn, new_dsn_size, "%s;UID=%s;PWD=%s", dbh->data_source, uid, pwd);
553+
554+
if (use_uid_arg && use_pwd_arg) {
555+
new_dsn_size = strlen(db)
556+
+ strlen(uid) + strlen(pwd)
557+
+ strlen(";UID=;PWD=;") + 1;
558+
dsn = pemalloc(new_dsn_size, dbh->is_persistent);
559+
snprintf(dsn, new_dsn_size, "%s;UID=%s;PWD=%s;", db, uid, pwd);
560+
}
561+
533562
pefree((char*)dbh->data_source, dbh->is_persistent);
534563
dbh->data_source = dsn;
535564
if (uid && should_quote_uid) {
@@ -538,6 +567,7 @@ static int pdo_odbc_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{
538567
if (pwd && should_quote_pwd) {
539568
efree(pwd);
540569
}
570+
efree(db);
541571
}
542572

543573
rc = SQLDriverConnect(H->dbc, NULL, (SQLCHAR *) dbh->data_source, strlen(dbh->data_source),

0 commit comments

Comments
 (0)