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 "pdo/php_pdo.h"
26 #include "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_long_param(&lval, val)) {
294 return false;
295 }
296 H->stringify_uniqueidentifier = lval;
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_long_param(&lval, val)) {
306 return false;
307 }
308 H->datetime_convert = lval;
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 };
441
pdo_dblib_handle_factory(pdo_dbh_t * dbh,zval * driver_options)442 static int pdo_dblib_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
443 {
444 pdo_dblib_db_handle *H;
445 int i, nvars, nvers, ret = 0;
446
447 const pdo_dblib_keyval tdsver[] = {
448 {"4.2",DBVERSION_42}
449 ,{"4.6",DBVERSION_46}
450 ,{"5.0",DBVERSION_70} /* FIXME: This does not work with Sybase, but environ will */
451 ,{"6.0",DBVERSION_70}
452 ,{"7.0",DBVERSION_70}
453 #ifdef DBVERSION_71
454 ,{"7.1",DBVERSION_71}
455 #endif
456 #ifdef DBVERSION_72
457 ,{"7.2",DBVERSION_72}
458 ,{"8.0",DBVERSION_72}
459 #endif
460 #ifdef DBVERSION_73
461 ,{"7.3",DBVERSION_73}
462 #endif
463 #ifdef DBVERSION_74
464 ,{"7.4",DBVERSION_74}
465 #endif
466 ,{"10.0",DBVERSION_100}
467 ,{"auto",0} /* Only works with FreeTDS. Other drivers will bork */
468
469 };
470
471 struct pdo_data_src_parser vars[] = {
472 { "charset", NULL, 0 }
473 ,{ "appname", "PHP " PDO_DBLIB_FLAVOUR, 0 }
474 ,{ "host", "127.0.0.1", 0 }
475 ,{ "dbname", NULL, 0 }
476 ,{ "secure", NULL, 0 } /* DBSETLSECURE */
477 ,{ "version", NULL, 0 } /* DBSETLVERSION */
478 ,{ "user", NULL, 0 }
479 ,{ "password", NULL, 0 }
480 };
481
482 nvars = sizeof(vars)/sizeof(vars[0]);
483 nvers = sizeof(tdsver)/sizeof(tdsver[0]);
484
485 php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, nvars);
486
487 H = pecalloc(1, sizeof(*H), dbh->is_persistent);
488 H->login = dblogin();
489 H->err.sqlstate = dbh->error_code;
490 H->assume_national_character_set_strings = 0;
491 H->stringify_uniqueidentifier = 0;
492 H->skip_empty_rowsets = 0;
493 H->datetime_convert = 0;
494
495 if (!H->login) {
496 goto cleanup;
497 }
498
499 if (driver_options) {
500 int connect_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_CONNECTION_TIMEOUT, -1);
501 int query_timeout = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_QUERY_TIMEOUT, -1);
502 int timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
503
504 if (connect_timeout == -1) {
505 connect_timeout = timeout;
506 }
507 if (query_timeout == -1) {
508 query_timeout = timeout;
509 }
510
511 dbsetlogintime(connect_timeout); /* Connection/Login Timeout */
512 dbsettime(query_timeout); /* Statement Timeout */
513
514 H->assume_national_character_set_strings = pdo_attr_lval(driver_options, PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0;
515 H->stringify_uniqueidentifier = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER, 0);
516 H->skip_empty_rowsets = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS, 0);
517 H->datetime_convert = pdo_attr_lval(driver_options, PDO_DBLIB_ATTR_DATETIME_CONVERT, 0);
518 }
519
520 DBERRHANDLE(H->login, (EHANDLEFUNC) pdo_dblib_error_handler);
521 DBMSGHANDLE(H->login, (MHANDLEFUNC) pdo_dblib_msg_handler);
522
523 if(vars[5].optval) {
524 for(i=0;i<nvers;i++) {
525 if(strcmp(vars[5].optval,tdsver[i].key) == 0) {
526 if(FAIL==dbsetlversion(H->login, tdsver[i].value)) {
527 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Failed to set version specified in connection string.");
528 goto cleanup;
529 }
530 break;
531 }
532 }
533
534 if (i==nvers) {
535 printf("Invalid version '%s'\n", vars[5].optval);
536 pdo_raise_impl_error(dbh, NULL, "HY000", "PDO_DBLIB: Invalid version specified in connection string.");
537 goto cleanup; /* unknown version specified */
538 }
539 }
540
541 if (!dbh->username && vars[6].optval) {
542 dbh->username = pestrdup(vars[6].optval, dbh->is_persistent);
543 }
544
545 if (dbh->username) {
546 if(FAIL == DBSETLUSER(H->login, dbh->username)) {
547 goto cleanup;
548 }
549 }
550
551 if (!dbh->password && vars[7].optval) {
552 dbh->password = pestrdup(vars[7].optval, dbh->is_persistent);
553 }
554
555 if (dbh->password) {
556 if(FAIL == DBSETLPWD(H->login, dbh->password)) {
557 goto cleanup;
558 }
559 }
560
561 #ifndef PHP_DBLIB_IS_MSSQL
562 if (vars[0].optval) {
563 DBSETLCHARSET(H->login, vars[0].optval);
564 }
565 #endif
566
567 DBSETLAPP(H->login, vars[1].optval);
568
569 /* DBSETLDBNAME is only available in FreeTDS 0.92 or above */
570 #ifdef DBSETLDBNAME
571 if (vars[3].optval) {
572 if(FAIL == DBSETLDBNAME(H->login, vars[3].optval)) goto cleanup;
573 }
574 #endif
575
576 H->link = dbopen(H->login, vars[2].optval);
577
578 if (!H->link) {
579 goto cleanup;
580 }
581
582 /*
583 * FreeTDS < 0.92 does not support the DBSETLDBNAME option
584 * Send use database here after login (Will not work with SQL Azure)
585 */
586 #ifndef DBSETLDBNAME
587 if (vars[3].optval) {
588 if(FAIL == dbuse(H->link, vars[3].optval)) goto cleanup;
589 }
590 #endif
591
592 #ifdef PHP_DBLIB_IS_MSSQL
593 /* dblib do not return more than this length from text/image */
594 DBSETOPT(H->link, DBTEXTLIMIT, "2147483647");
595 #endif
596
597 /* limit text/image from network */
598 DBSETOPT(H->link, DBTEXTSIZE, "2147483647");
599
600 /* allow double quoted indentifiers */
601 DBSETOPT(H->link, DBQUOTEDIDENT, "1");
602
603 ret = 1;
604 dbh->max_escaped_char_length = 2;
605 dbh->alloc_own_columns = 1;
606
607 cleanup:
608 for (i = 0; i < nvars; i++) {
609 if (vars[i].freeme) {
610 efree(vars[i].optval);
611 }
612 }
613
614 dbh->methods = &dblib_methods;
615 dbh->driver_data = H;
616
617 if (!ret) {
618 zend_throw_exception_ex(php_pdo_get_exception(), DBLIB_G(err).dberr,
619 "SQLSTATE[%s] %s (severity %d)",
620 DBLIB_G(err).sqlstate,
621 DBLIB_G(err).dberrstr,
622 DBLIB_G(err).severity);
623 }
624
625 return ret;
626 }
627
628 const pdo_driver_t pdo_dblib_driver = {
629 #ifdef PDO_DBLIB_IS_MSSQL
630 PDO_DRIVER_HEADER(mssql),
631 #elif defined(PHP_WIN32)
632 #define PDO_DBLIB_IS_SYBASE
633 PDO_DRIVER_HEADER(sybase),
634 #else
635 PDO_DRIVER_HEADER(dblib),
636 #endif
637 pdo_dblib_handle_factory
638 };
639