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