Skip to content

Commit c4e90be

Browse files
committed
Optimized the handling of authentication information for odbc_sqlconnect.
1 parent 2e8cdd8 commit c4e90be

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

ext/odbc/php_odbc.c

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,32 +2095,62 @@ int odbc_sqlconnect(odbc_connection **conn, char *db, char *uid, char *pwd, int
20952095
/* a connection string may have = but not ; - i.e. "DSN=PHP" */
20962096
if (strstr((char*)db, "=")) {
20972097
direct = 1;
2098+
2099+
/* There are many character comparison patterns, so convert them to lower case. */
2100+
size_t db_len = strlen(db);
2101+
char *lower_db;
2102+
lower_db = (char*) emalloc(db_len);
2103+
strcpy(lower_db, db);
2104+
2105+
unsigned char *c = (unsigned char *) lower_db;
2106+
const unsigned char *e = c + db_len;
2107+
while (c < e) {
2108+
*c = tolower(*c);
2109+
c++;
2110+
}
2111+
2112+
bool use_uid_arg = !strstr(lower_db, "uid=");
2113+
bool use_pwd_arg = !strstr(lower_db, "pwd=");
2114+
efree(lower_db);
2115+
20982116
/* Force UID and PWD to be set in the DSN */
2099-
bool is_uid_set = uid && *uid
2100-
&& !strstr(db, "uid=")
2101-
&& !strstr(db, "UID=");
2102-
bool is_pwd_set = pwd && *pwd
2103-
&& !strstr(db, "pwd=")
2104-
&& !strstr(db, "PWD=");
2105-
if (is_uid_set && is_pwd_set) {
2117+
if (use_uid_arg || use_pwd_arg) {
21062118
char *uid_quoted = NULL, *pwd_quoted = NULL;
2107-
bool should_quote_uid = !php_odbc_connstr_is_quoted(uid) && php_odbc_connstr_should_quote(uid);
2108-
bool should_quote_pwd = !php_odbc_connstr_is_quoted(pwd) && php_odbc_connstr_should_quote(pwd);
2109-
if (should_quote_uid) {
2110-
size_t estimated_length = php_odbc_connstr_estimate_quote_length(uid);
2111-
uid_quoted = emalloc(estimated_length);
2112-
php_odbc_connstr_quote(uid_quoted, uid, estimated_length);
2113-
} else {
2114-
uid_quoted = uid;
2119+
bool should_quote_uid, should_quote_pwd;
2120+
if (use_uid_arg) {
2121+
should_quote_uid = !php_odbc_connstr_is_quoted(uid) && php_odbc_connstr_should_quote(uid);
2122+
if (should_quote_uid) {
2123+
size_t estimated_length = php_odbc_connstr_estimate_quote_length(uid);
2124+
uid_quoted = emalloc(estimated_length);
2125+
php_odbc_connstr_quote(uid_quoted, uid, estimated_length);
2126+
} else {
2127+
uid_quoted = uid;
2128+
}
2129+
2130+
if (!use_pwd_arg) {
2131+
spprintf(&ldb, 0, "%s;UID=%s", db, uid_quoted);
2132+
}
21152133
}
2116-
if (should_quote_pwd) {
2117-
size_t estimated_length = php_odbc_connstr_estimate_quote_length(pwd);
2118-
pwd_quoted = emalloc(estimated_length);
2119-
php_odbc_connstr_quote(pwd_quoted, pwd, estimated_length);
2120-
} else {
2121-
pwd_quoted = pwd;
2134+
2135+
if (use_pwd_arg) {
2136+
should_quote_pwd = !php_odbc_connstr_is_quoted(pwd) && php_odbc_connstr_should_quote(pwd);
2137+
if (should_quote_pwd) {
2138+
size_t estimated_length = php_odbc_connstr_estimate_quote_length(pwd);
2139+
pwd_quoted = emalloc(estimated_length);
2140+
php_odbc_connstr_quote(pwd_quoted, pwd, estimated_length);
2141+
} else {
2142+
pwd_quoted = pwd;
2143+
}
2144+
2145+
if (!use_uid_arg) {
2146+
spprintf(&ldb, 0, "%s;PWD=%s", db, pwd_quoted);
2147+
}
2148+
}
2149+
2150+
if (use_uid_arg && use_pwd_arg) {
2151+
spprintf(&ldb, 0, "%s;UID=%s;PWD=%s", db, uid_quoted, pwd_quoted);
21222152
}
2123-
spprintf(&ldb, 0, "%s;UID=%s;PWD=%s", db, uid_quoted, pwd_quoted);
2153+
21242154
if (uid_quoted && should_quote_uid) {
21252155
efree(uid_quoted);
21262156
}

0 commit comments

Comments
 (0)