security fix in master branch
[openemr.git] / library / authentication / privDB.php
blob5daac271637e79e29955f4dba6ba722a113224e2
1 <?php
2 /**
3 * To support an optional higher level of security, queries that access password
4 * related information use these functions instead of the standard functions
5 * provided by sql.inc.
7 * By default, the privQuery and privStatement calls pass-through to
8 * the existing ADODB instance initialized by sql.inc.
10 * If an additional configuration file is created (secure_sqlconf.php) and saved
11 * in the sites/<sitename> directory (e.g. sites/default). The MySQL login
12 * information defined in that file as $secure_* will be used to create an ADODB
13 * instance specifically for querying privileged information.
15 * By configuring a server in this way, the default MySQL user can be denied access
16 * to sensitive tables (currently only "users_secure" would qualify). Thus
17 * the likelyhood of unintended modification can be reduced (e.g. through SQL Injection).
19 * Details on how to set this up are included in Documentation/privileged_db/priv_db_HOWTO
21 * The trade off for this additional security is extra complexity in configuration and
22 * maintenance of the database, hence it is not enabled at install time and must be
23 * done manually.
25 * Copyright (C) 2013 Kevin Yeh <kevin.y@integralemr.com> and OEMR <www.oemr.org>
27 * LICENSE: This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License
29 * as published by the Free Software Foundation; either version 3
30 * of the License, or (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
38 * @package OpenEMR
39 * @author Kevin Yeh <kevin.y@integralemr.com>
40 * @link http://www.open-emr.org
44 define("PRIV_DB", "PRIV_DB");
45 function getPrivDB()
47 if (!isset($GLOBALS[PRIV_DB])) {
48 $secure_config=$GLOBALS['OE_SITE_DIR'] . "/secure_sqlconf.php";
49 if (file_exists($secure_config)) {
50 require_once($secure_config);
51 $GLOBALS[PRIV_DB]=NewADOConnection("mysql_log");
52 $GLOBALS[PRIV_DB]->PConnect($secure_host.":".$secure_port, $secure_login, $secure_pass, $secure_dbase);
53 } else {
54 $GLOBALS[PRIV_DB]=$GLOBALS['adodb']['db'];
58 return $GLOBALS[PRIV_DB];
61 /**
62 * mechanism to use "super user" for SQL queries related to password operations
64 * @param type $sql
65 * @param type $params
66 * @return type
68 function privStatement($sql, $params = null)
70 if (is_array($params)) {
71 $recordset = getPrivDB()->Execute($sql, $params);
72 } else {
73 $recordset = getPrivDB()->Execute($sql);
76 if ($recordset === false) {
77 // These error messages are explictly NOT run through xl() because we still
78 // need them if there is a database problem.
79 echo "Failure during database access! Check server error log.";
80 $backtrace=debug_backtrace();
82 error_log("Executing as user:" .getPrivDB()->user." Statement failed:".$sql.":". $GLOBALS['last_mysql_error']
83 ."==>".$backtrace[1]["file"]." at ".$backtrace[1]["line"].":".$backtrace[1]["function"]);
84 exit;
87 return $recordset;
88 return sqlStatement($sql, $params);
91 /**
93 * Wrapper for privStatement that just returns the first row of a query or FALSE
94 * if there were no results.
96 * @param type $sql
97 * @param type $params
98 * @return boolean
100 function privQuery($sql, $params = null)
102 $recordset=privStatement($sql, $params);
103 if ($recordset->EOF) {
104 return false;
107 $rez = $recordset->FetchRow();
108 if ($rez == false) {
109 return false;
112 return $rez;