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