1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Wez Furlong <wez@php.net> |
14 | Frank M. Kromann <frank@kromann.info> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "php_ini.h"
24 #include "ext/standard/info.h"
25 #include "ext/pdo/php_pdo.h"
26 #include "ext/pdo/php_pdo_driver.h"
27 #include "php_pdo_dblib.h"
28 #include "php_pdo_dblib_int.h"
29 #include "zend_exceptions.h"
30
31 /* Cache of the server supported datatypes, initialized in handle_factory */
32 zval* pdo_dblib_datatypes;
33
dblib_fetch_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)34 static void dblib_fetch_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
35 {
36 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
37 pdo_dblib_err *einfo = &H->err;
38 pdo_dblib_stmt *S = NULL;
39 char *message;
40 char *msg;
41
42 if (stmt) {
43 S = (pdo_dblib_stmt*)stmt->driver_data;
44 einfo = &S->err;
45 }
46
47 if (einfo->lastmsg) {
48 msg = einfo->lastmsg;
49 } else if (DBLIB_G(err).lastmsg) {
50 msg = DBLIB_G(err).lastmsg;
51 DBLIB_G(err).lastmsg = NULL;
52 } else {
53 msg = einfo->dberrstr;
54 }
55
56 /* don't return anything if there's nothing to return */
57 if (msg == NULL && einfo->dberr == 0 && einfo->oserr == 0 && einfo->severity == 0) {
58 return;
59 }
60
61 spprintf(&message, 0, "%s [%d] (severity %d) [%s]",
62 msg, einfo->dberr, einfo->severity, stmt ? ZSTR_VAL(stmt->active_query_string) : "");
63
64 add_next_index_long(info, einfo->dberr);
65 add_next_index_string(info, message);
66 efree(message);
67 add_next_index_long(info, einfo->oserr);
68 add_next_index_long(info, einfo->severity);
69 if (einfo->oserrstr) {
70 add_next_index_string(info, einfo->oserrstr);
71 }
72 }
73
74
dblib_handle_closer(pdo_dbh_t * dbh)75 static void dblib_handle_closer(pdo_dbh_t *dbh)
76 {
77 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
78
79 if (H) {
80 pdo_dblib_err_dtor(&H->err);
81 if (H->link) {
82 dbclose(H->link);
83 H->link = NULL;
84 }
85 if (H->login) {
86 dbfreelogin(H->login);
87 H->login = NULL;
88 }
89 pefree(H, dbh->is_persistent);
90 dbh->driver_data = NULL;
91 }
92 }
93
dblib_handle_preparer(pdo_dbh_t * dbh,zend_string * sql,pdo_stmt_t * stmt,zval * driver_options)94 static bool dblib_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
95 {
96 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
97 pdo_dblib_stmt *S = ecalloc(1, sizeof(*S));
98
99 S->H = H;
100 stmt->driver_data = S;
101 stmt->methods = &dblib_stmt_methods;
102 stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
103 S->computed_column_name_count = 0;
104 S->err.sqlstate = stmt->error_code;
105
106 return true;
107 }
108
dblib_handle_doer(pdo_dbh_t * dbh,const zend_string * sql)109 static zend_long dblib_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
110 {
111 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
112 RETCODE ret, resret;
113
114 dbsetuserdata(H->link, (BYTE*)&H->err);
115
116 if (FAIL == dbcmd(H->link, ZSTR_VAL(sql))) {
117 return -1;
118 }
119
120 if (FAIL == dbsqlexec(H->link)) {
121 return -1;
122 }
123
124 resret = dbresults(H->link);
125
126 if (resret == FAIL) {
127 return -1;
128 }
129
130 ret = dbnextrow(H->link);
131 if (ret == FAIL) {
132 return -1;
133 }
134
135 if (dbnumcols(H->link) <= 0) {
136 return DBCOUNT(H->link);
137 }
138
139 /* throw away any rows it might have returned */
140 dbcanquery(H->link);
141
142 return DBCOUNT(H->link);
143 }
144
dblib_handle_quoter(pdo_dbh_t * dbh,const zend_string * unquoted,enum pdo_param_type paramtype)145 static zend_string* dblib_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype)
146 {
147 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
148 bool use_national_character_set = 0;
149 size_t i;
150 char *q;
151 size_t quotedlen = 0;
152 zend_string *quoted_str;
153
154 if (H->assume_national_character_set_strings) {
155 use_national_character_set = 1;
156 }
157 if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
158 use_national_character_set = 1;
159 }
160 if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
161 use_national_character_set = 0;
162 }
163
164 /* Detect quoted length, adding extra char for doubled single quotes */
165 for (i = 0; i < ZSTR_LEN(unquoted); i++) {
166 if (ZSTR_VAL(unquoted)[i] == '\'') ++quotedlen;
167 ++quotedlen;
168 }
169
170 quotedlen += 2; /* +2 for opening, closing quotes */
171 if (use_national_character_set) {
172 ++quotedlen; /* N prefix */
173 }
174 quoted_str = zend_string_alloc(quotedlen, 0);
175 q = ZSTR_VAL(quoted_str);
176 if (use_national_character_set) {
177 *q++ = 'N';
178 }
179 *q++ = '\'';
180
181 for (i = 0; i < ZSTR_LEN(unquoted); i++) {
182 if (ZSTR_VAL(unquoted)[i] == '\'') {
183 *q++ = '\'';
184 *q++ = '\'';
185 } else {
186 *q++ = ZSTR_VAL(unquoted)[i];
187 }
188 }
189 *q++ = '\'';
190 *q = '\0';
191
192 return quoted_str;
193 }
194
pdo_dblib_transaction_cmd(const char * cmd,pdo_dbh_t * dbh)195 static bool pdo_dblib_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
196 {
197 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
198
199 if (FAIL == dbcmd(H->link, cmd)) {
200 return false;
201 }
202
203 if (FAIL == dbsqlexec(H->link)) {
204 return false;
205 }
206
207 return true;
208 }
209
dblib_handle_begin(pdo_dbh_t * dbh)210 static bool dblib_handle_begin(pdo_dbh_t *dbh)
211 {
212 return pdo_dblib_transaction_cmd("BEGIN TRANSACTION", dbh);
213 }
214
dblib_handle_commit(pdo_dbh_t * dbh)215 static bool dblib_handle_commit(pdo_dbh_t *dbh)
216 {
217 return pdo_dblib_transaction_cmd("COMMIT TRANSACTION", dbh);
218 }
219
dblib_handle_rollback(pdo_dbh_t * dbh)220 static bool dblib_handle_rollback(pdo_dbh_t *dbh)
221 {
222 return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
223 }
224
dblib_handle_last_id(pdo_dbh_t * dbh,const zend_string * name)225 zend_string *dblib_handle_last_id(pdo_dbh_t *dbh, const zend_string *name)
226 {
227 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
228
229 RETCODE ret;
230 char *id = NULL;
231 size_t len;
232 zend_string *ret_id;
233
234 /*
235 * Would use scope_identity() but it's not implemented on Sybase
236 */
237
238 if (FAIL == dbcmd(H->link, "SELECT @@IDENTITY")) {
239 return NULL;
240 }
241
242 if (FAIL == dbsqlexec(H->link)) {
243 return NULL;
244 }
245
246 ret = dbresults(H->link);
247 if (ret == FAIL || ret == NO_MORE_RESULTS) {
248 dbcancel(H->link);
249 return NULL;
250 }
251
252 ret = dbnextrow(H->link);
253
254 if (ret == FAIL || ret == NO_MORE_ROWS) {
255 dbcancel(H->link);
256 return NULL;
257 }
258
259 if (dbdatlen(H->link, 1) == 0) {
260 dbcancel(H->link);
261 return NULL;
262 }
263
264 id = emalloc(32);
265 len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
266 dbcancel(H->link);
267
268 ret_id = zend_string_init(id, len, 0);
269 efree(id);
270 return ret_id;
271 }
272
dblib_set_attr(pdo_dbh_t * dbh,zend_long attr,zval * val)273 static bool dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
274 {
275 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
276 zend_long lval;
277 bool bval;
278
279 switch(attr) {
280 case PDO_ATTR_DEFAULT_STR_PARAM:
281 if (!pdo_get_long_param(&lval, val)) {
282 return false;
283 }
284 H->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL ? 1 : 0;
285 return true;
286 case PDO_ATTR_TIMEOUT:
287 case PDO_DBLIB_ATTR_QUERY_TIMEOUT:
288 if (!pdo_get_long_param(&lval, val)) {
289 return false;
290 }
291 return SUCCEED == dbsettime(lval);
292 case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
293 if (!pdo_get_bool_param(&bval, val)) {
294 return false;
295 }
296 H->stringify_uniqueidentifier = bval;
297 return true;
298 case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
299 if (!pdo_get_bool_param(&bval, val)) {
300 return false;
301 }
302 H->skip_empty_rowsets = bval;
303 return true;
304 case PDO_DBLIB_ATTR_DATETIME_CONVERT:
305 if (!pdo_get_bool_param(&bval, val)) {
306 return false;
307 }
308 H->datetime_convert = bval;
309 return true;
310 default:
311 return false;
312 }
313 }
314
dblib_get_tds_version(zval * return_value,int tds)315 static void dblib_get_tds_version(zval *return_value, int tds)
316 {
317 switch (tds) {
318 case DBTDS_2_0:
319 ZVAL_STRING(return_value, "2.0");
320 break;
321
322 case DBTDS_3_4:
323 ZVAL_STRING(return_value, "3.4");
324 break;
325
326 case DBTDS_4_0:
327 ZVAL_STRING(return_value, "4.0");
328 break;
329
330 case DBTDS_4_2:
331 ZVAL_STRING(return_value, "4.2");
332 break;
333
334 case DBTDS_4_6:
335 ZVAL_STRING(return_value, "4.6");
336 break;
337
338 case DBTDS_4_9_5:
339 ZVAL_STRING(return_value, "4.9.5");
340 break;
341
342 case DBTDS_5_0:
343 ZVAL_STRING(return_value, "5.0");
344 break;
345
346 #ifdef DBTDS_7_0
347 case DBTDS_7_0:
348 ZVAL_STRING(return_value, "7.0");
349 break;
350 #endif
351
352 #ifdef DBTDS_7_1
353 case DBTDS_7_1:
354 ZVAL_STRING(return_value, "7.1");
355 break;
356 #endif
357
358 #ifdef DBTDS_7_2
359 case DBTDS_7_2:
360 ZVAL_STRING(return_value, "7.2");
361 break;
362 #endif
363
364 #ifdef DBTDS_7_3
365 case DBTDS_7_3:
366 ZVAL_STRING(return_value, "7.3");
367 break;
368 #endif
369
370 #ifdef DBTDS_7_4
371 case DBTDS_7_4:
372 ZVAL_STRING(return_value, "7.4");
373 break;
374 #endif
375
376 default:
377 ZVAL_FALSE(return_value);
378 break;
379 }
380 }
381
dblib_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)382 static int dblib_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
383 {
384 pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
385
386 switch (attr) {
387 case PDO_ATTR_DEFAULT_STR_PARAM:
388 ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
389 break;
390
391 case PDO_ATTR_EMULATE_PREPARES:
392 /* this is the only option available, but expose it so common tests and whatever else can introspect */
393 ZVAL_TRUE(return_value);
394 break;
395
396 case PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER:
397 ZVAL_BOOL(return_value, H->stringify_uniqueidentifier);
398 break;
399
400 case PDO_DBLIB_ATTR_VERSION:
401 ZVAL_STRING(return_value, dbversion());
402 break;
403
404 case PDO_DBLIB_ATTR_TDS_VERSION:
405 dblib_get_tds_version(return_value, dbtds(H->link));
406 break;
407
408 case PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS:
409 ZVAL_BOOL(return_value, H->skip_empty_rowsets);
410 break;
411
412 case PDO_DBLIB_ATTR_DATETIME_CONVERT:
413 ZVAL_BOOL(return_value, H->datetime_convert);
414 break;
415
416 default:
417 return 0;
418 }
419
420 return 1;
421 }
422
423 static const struct pdo_dbh_methods dblib_methods = {
424 dblib_handle_closer,
425 dblib_handle_preparer,
426 dblib_handle_doer,
427 dblib_handle_quoter,
428 dblib_handle_begin, /* begin */
429 dblib_handle_commit, /* commit */
430 dblib_handle_rollback, /* rollback */
431 dblib_set_attr, /*set attr */
432 dblib_handle_last_id, /* last insert id */
433 dblib_fetch_error, /* fetch error */
434 dblib_get_attribute, /* get attr */
435 NULL, /* check liveness */
436 NULL, /* get driver methods */
437 NULL, /* request shutdown */
438 NULL, /* in transaction, use PDO's internal tracking mechanism */
439 NULL, /* get gc */
440 NULL /* scanner */
441 };
442
pdo_dblib_handle_factory(pdo_dbh_t * dbh,zval * driver_options)443 static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
444 {
445 pdo_dblib_db_handle *H;
446 int i, nvars, nvers, ret = 0;
447
448 const pdo_dblib_keyval tdsver[] = {
449 {"4.2",DBVERSION_42}
450 ,{"4.6",DBVERSION_46}
451 ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
452 ,{"6.0",DBVERSION_70}
453 ,{"7.0",DBVERSION_70}
454 #ifdef DBVERSION_71
455 ,{"7.1",DBVERSION_71}
456 #endif
457 #ifdef DBVERSION_72
458 ,{"7.2",DBVERSION_72}
459 ,{"8.0",DBVERSION_72}
460 #endif
461 #ifdef DBVERSION_73
462 ,{"7.3",DBVERSION_73}
463 #endif
464 #ifdef DBVERSION_74
465 ,{"7.4",DBVERSION_74}
466 #endif
467 ,{"10.0",DBVERSION_100}
468 ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
469
470 };
471
472 struct pdo_data_src_parser vars[] = {
473 { "charset", NULL, 0 }
474 ,{ "appname", "PHP " PDO_DBLIB_FLAVOUR, 0 }
475 ,{ "host", "127.0.0.1", 0 }
476 ,{ "dbname", NULL, 0 }
477 ,{ "secure", NULL, 0 } /* DBSETLSECURE */
478 ,{ "version", NULL, 0 } /* DBSETLVERSION */
479 ,{ "user", NULL, 0 }
480 ,{ "password", NULL, 0 }
481 };
482
483 nvars = sizeof(vars)/sizeof(vars[0]);
484 nvers = sizeof(tdsver)/sizeof(tdsver[0]);
485
486 php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars);
487
488 H = pecalloc(1, sizeof(*H), dbh->is_persistent);
489 H->login = dblogin();
490 H->err.sqlstate = dbh->error_code;
491 H->assume_national_character_set_strings = 0;
492 H->stringify_uniqueidentifier = 0;
493 H->skip_empty_rowsets = 0;
494 H->datetime_convert = 0;
495
496 if (!H->login) {
497 goto cleanup;
498 }
499
500 if (driver_options) {
501 int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1);
502 int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1);
503 int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
504
505 if (connect_timeout == -1) {
506 connect_timeout = timeout;
507 }
508 if (query_timeout == -1) {
509 query_timeout = timeout;
510 }
511
512 dbsetlogintime(connect_timeout); /* Connection/Login Timeout */
513 dbsettime(query_timeout); /* Statement Timeout */
514
515 H->assume_national_character_set_strings = pdo_attr_lval(driver_options, PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0;
516 H->stringify_uniqueidentifier = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, 0);
517 H->skip_empty_rowsets = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS, 0);
518 H->datetime_convert = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_DATETIME_CONVERT, 0);
519 }
520
521 DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler);
522 DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler);
523
524 if(vars[5].optval) {
525 for(i=0;i<nvers;i++) {
526 if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
527 if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
528 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string.");
529 goto cleanup;
530 }
531 break;
532 }
533 }
534
535 if (i==nvers) {
536 printf("Invalid version '%s'\n", vars[5].optval);
537 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string.");
538 goto cleanup; /* unknown version specified */
539 }
540 }
541
542 if (!dbh->username && vars[6].optval) {
543 dbh->username = pestrdup(vars[6].optval, dbh->is_persistent);
544 }
545
546 if (dbh->username) {
547 if(FAIL == DBSETLUSER(H->login, dbh->username)) {
548 goto cleanup;
549 }
550 }
551
552 if (!dbh->password && vars[7].optval) {
553 dbh->password = pestrdup(vars[7].optval, dbh->is_persistent);
554 }
555
556 if (dbh->password) {
557 if(FAIL == DBSETLPWD(H->login, dbh->password)) {
558 goto cleanup;
559 }
560 }
561
562 #ifndef PHP_DBLIB_IS_MSSQL
563 if (vars[0].optval) {
564 DBSETLCHARSET(H->login, vars[0].optval);
565 }
566 #endif
567
568 DBSETLAPP(H->login, vars[1].optval);
569
570 /* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
571 #ifdef DBSETLDBNAME
572 if (vars[3].optval) {
573 if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
574 }
575 #endif
576
577 H->link = dbopen(H->login, vars[2].optval);
578
579 if (!H->link) {
580 goto cleanup;
581 }
582
583 /*
584 * FreeTDS < 0.92 does not support the DBSETLDBNAME option
585 * Send use database here after login (Will not work with SQL Azure)
586 */
587 #ifndef DBSETLDBNAME
588 if (vars[3].optval) {
589 if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
590 }
591 #endif
592
593 #ifdef PHP_DBLIB_IS_MSSQL
594 /* dblib do not return more than this length from text/image */
595 DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
596 #endif
597
598 /* limit text/image from network */
599 DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
600
601 /* allow double quoted indentifiers */
602 DBSETOPT(H->link, DBQUOTEDIDENT, "1");
603
604 ret = 1;
605 dbh->max_escaped_char_length = 2;
606 dbh->alloc_own_columns = 1;
607
608 cleanup:
609 for (i = 0; i < nvars; i++) {
610 if (vars[i].freeme) {
611 efree(vars[i].optval);
612 }
613 }
614
615 dbh->methods = &dblib_methods;
616 dbh->driver_data = H;
617
618 if (!ret) {
619 zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr,
620 "SQLSTATE[%s] %s (severity %d)",
621 DBLIB_G(err).sqlstate,
622 DBLIB_G(err).dberrstr,
623 DBLIB_G(err).severity);
624 }
625
626 return ret;
627 }
628
629 const pdo_driver_t pdo_dblib_driver = {
630 #ifdef PDO_DBLIB_IS_MSSQL
631 PDO_DRIVER_HEADER(mssql),
632 #elif defined(PHP_WIN32)
633 #define PDO_DBLIB_IS_SYBASE
634 PDO_DRIVER_HEADER(sybase),
635 #else
636 PDO_DRIVER_HEADER(dblib),
637 #endif
638 pdo_dblib_handle_factory
639 };
640