diff --git a/functions.inc.php b/functions.inc.php index 6b1ac14d..45c3d5a9 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -1779,13 +1779,13 @@ function db_query(string $sql, array $values = array(), bool $ignore_errors = fa * @param string $additionalwhere (default ''). * @return int|mixed rows deleted. */ -function db_delete(string $table, string $where, string $delete, string $additionalwhere = '') +function db_delete(string $table, string $where, string $delete, string $additionalwhere = '', array $additional_params = []) { $table = table_by_key($table); $query = "DELETE FROM $table WHERE $where = ? $additionalwhere"; - return db_execute($query, array($delete)); + return db_execute($query, array_merge(array($delete), $additional_params)); } @@ -1924,10 +1924,18 @@ function db_log(string $domain, string $action, string $data): bool * @param array $values * @return string */ -function db_in_clause(string $field, array $values): string +function db_in_clause(string $field, array $values, array &$params = []): string { - $v = array_map('escape_string', array_values($values)); - return " $field IN ('" . implode("','", $v) . "') "; + if (empty($values)) { + return " 1=0 "; // empty IN clause is invalid SQL, return false predicate + } + $placeholders = []; + foreach (array_values($values) as $i => $value) { + $key = '_in_' . count($params) . '_' . $i; + $placeholders[] = ':' . $key; + $params[$key] = $value; + } + return " $field IN (" . implode(",", $placeholders) . ") "; } /** @@ -1942,7 +1950,7 @@ function db_in_clause(string $field, array $values): string * Note: the $searchmode operator will only be used if a $condition for that field is set. * This also means you'll need to set a (dummy) condition for NULL and NOTNULL. */ -function db_where_clause(array $condition, array $struct, $additional_raw_where = '', array $searchmode = array()) +function db_where_clause(array $condition, array $struct, $additional_raw_where = '', array $searchmode = array(), array &$params = []) { if (count($condition) == 0 && trim($additional_raw_where) == '') { throw new Exception("db_where_cond: parameter is an empty array!"); @@ -1977,11 +1985,13 @@ function db_where_clause(array $condition, array $struct, $additional_raw_where } elseif ($operator == "NOTNULL") { $querypart = $field . ' IS NOT NULL'; } else { - $querypart = $field . $operator . "'" . escape_string($value) . "'"; - // might need other types adding here. if (db_pgsql() && isset($struct[$field]) && in_array($struct[$field]['type'], array('ts', 'num')) && $value === '') { $querypart = $field . $operator . " NULL"; + } else { + $param_key = '_wh_' . preg_replace('/[^a-zA-Z0-9_]/', '_', $field); + $querypart = $field . $operator . " :" . $param_key; + $params[$param_key] = $value; } } diff --git a/model/AdminHandler.php b/model/AdminHandler.php index 74235fb7..7a5d14d5 100644 --- a/model/AdminHandler.php +++ b/model/AdminHandler.php @@ -170,8 +170,9 @@ class AdminHandler extends PFAHandler 'username' => $this->id, 'domain' => 'ALL', ); - $where = db_where_clause(array('username' => $this->id, 'domain' => 'ALL'), $this->struct); - $result = db_query_one("SELECT username from " . table_by_key('domain_admins') . " " . $where); + $params = []; + $where = db_where_clause(array('username' => $this->id, 'domain' => 'ALL'), $this->struct, '', array(), $params); + $result = db_query_one("SELECT username from " . table_by_key('domain_admins') . " " . $where, $params); if (empty($result)) { db_insert('domain_admins', $values, array('created')); # TODO: check for errors diff --git a/model/AliasHandler.php b/model/AliasHandler.php index 880575d3..0f6ddb5d 100644 --- a/model/AliasHandler.php +++ b/model/AliasHandler.php @@ -78,12 +78,14 @@ class AliasHandler extends PFAHandler ' FROM ' . table_by_key('mailbox') . ' WHERE username IS NOT NULL '; + // Note: extrafrom is a raw SQL string stored in struct with no mechanism to attach params. + // Values here are internal (allowed_domains from session), not user input, so escape_string is acceptable. if (isset($condition['domain']) && !isset($searchmode['domain']) && in_array($condition['domain'], $this->allowed_domains)) { - # listing for a specific domain, so restrict subquery to that domain - $extrafrom .= ' AND ' . db_in_clause($this->domain_field, array($condition['domain'])); + $v = array_map('escape_string', array($condition['domain'])); + $extrafrom .= " AND $this->domain_field IN ('" . implode("','", $v) . "') "; } else { - # restrict subquery to all domains accessible to this admin - $extrafrom .= ' AND ' . db_in_clause($this->domain_field, $this->allowed_domains); + $v = array_map('escape_string', $this->allowed_domains); + $extrafrom .= " AND $this->domain_field IN ('" . implode("','", $v) . "') "; } $extrafrom .= ' ) AS __mailbox ON __mailbox_username = address'; diff --git a/model/DomainHandler.php b/model/DomainHandler.php index b7a09600..f950f644 100644 --- a/model/DomainHandler.php +++ b/model/DomainHandler.php @@ -219,7 +219,7 @@ class DomainHandler extends PFAHandler if (Config::bool('alias_domain')) { # check if this domain is an alias domain target - if yes, do not allow to delete it $handler = new AliasdomainHandler(0, $this->admin_username); - $handler->getList("target_domain = '" . escape_string($this->id) . "'"); + $handler->getList(array('target_domain' => $this->id)); $aliasdomains = $handler->result(); if (count($aliasdomains) > 0) { @@ -234,17 +234,17 @@ class DomainHandler extends PFAHandler # cleaning up all tables doesn't hurt, even if vacation or displaying the quota is disabled # some tables don't have a domain field, so we need a workaround - $like_domain = "LIKE '" . escape_string('%@' . $this->id) . "'"; + $like_domain_value = '%@' . $this->id; db_delete('domain_admins', 'domain', $this->id); db_delete('alias', 'domain', $this->id); db_delete('mailbox', 'domain', $this->id); db_delete('alias_domain', 'alias_domain', $this->id); db_delete('vacation', 'domain', $this->id); - db_delete('vacation_notification', 'on_vacation', $this->id, "OR on_vacation $like_domain"); - db_delete('quota', 'username', $this->id, "OR username $like_domain"); - db_delete('quota2', 'username', $this->id, "OR username $like_domain"); - db_delete('fetchmail', 'mailbox', $this->id, "OR mailbox $like_domain"); + db_delete('vacation_notification', 'on_vacation', $this->id, "OR on_vacation LIKE ?", [$like_domain_value]); + db_delete('quota', 'username', $this->id, "OR username LIKE ?", [$like_domain_value]); + db_delete('quota2', 'username', $this->id, "OR username LIKE ?", [$like_domain_value]); + db_delete('fetchmail', 'mailbox', $this->id, "OR mailbox LIKE ?", [$like_domain_value]); db_delete('log', 'domain', $this->id); # TODO: should we really delete the log? # finally delete the domain diff --git a/model/PFAHandler.php b/model/PFAHandler.php index a80641de..4460f142 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -711,25 +711,29 @@ abstract class PFAHandler $table = table_by_key($this->db_table); $additional_where = ''; + $params = []; if ($this->domain_field != "") { - $additional_where .= " AND " . db_in_clause($this->domain_field, $this->allowed_domains); + $additional_where .= " AND " . db_in_clause($this->domain_field, $this->allowed_domains, $params); } # if logged in as user, restrict to the items the user is allowed to see if ((!$this->is_admin) && $this->user_field != '') { - $additional_where .= " AND " . $this->user_field . " = '" . escape_string($this->username) . "' "; + $additional_where .= " AND " . $this->user_field . " = :_user_field "; + $params['_user_field'] = $this->username; } if (is_array($condition)) { if (isset($condition['_']) && count($this->searchfields) > 0) { $simple_search = array(); foreach ($this->searchfields as $field) { - $simple_search[] = "$field LIKE '%" . escape_string($condition['_']) . "%'"; + $param_key = '_search_' . preg_replace('/[^a-zA-Z0-9_]/', '_', $field); + $simple_search[] = "$field LIKE :$param_key"; + $params[$param_key] = '%' . $condition['_'] . '%'; } $additional_where .= " AND ( " . join(" OR ", $simple_search) . " ) "; unset($condition['_']); } - $where = db_where_clause($condition, $this->struct, $additional_where, $searchmode); + $where = db_where_clause($condition, $this->struct, $additional_where, $searchmode, $params); } else { if ($condition == "") { $condition = '1=1'; @@ -740,6 +744,7 @@ abstract class PFAHandler return array( 'select_cols' => " SELECT $cols ", 'from_where_order' => " FROM $table $extrafrom $where ORDER BY " . $this->order_by, + 'params' => $params, ); } @@ -753,7 +758,7 @@ abstract class PFAHandler public function getPagebrowser($condition, $searchmode) { $queryparts = $this->build_select_query($condition, $searchmode); - return create_page_browser($this->label_field, $queryparts['from_where_order']); + return create_page_browser($this->label_field, $queryparts['from_where_order'], $queryparts['params'] ?? []); } /** @@ -784,8 +789,9 @@ abstract class PFAHandler $db_result = array(); + $params = $queryparts['params'] ?? []; - $result = db_query_all($query); + $result = db_query_all($query, $params); foreach ($result as $row) { $db_result[$row[$this->id_field]] = $row; diff --git a/public/broadcast-message.php b/public/broadcast-message.php index f2ca21a4..3a031157 100644 --- a/public/broadcast-message.php +++ b/public/broadcast-message.php @@ -60,13 +60,18 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") { $table_mailbox = table_by_key('mailbox'); $table_alias = table_by_key('alias'); - $q = "SELECT username from $table_mailbox WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains); - if (intval(safepost('mailboxes_only')) == 0) { - $q .= " UNION SELECT goto FROM $table_alias WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains) . " AND goto NOT IN ($q)"; - } - $result = db_query_all($q); + $params = []; + $q = "SELECT username from $table_mailbox WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains, $params); + $result = db_query_all($q, $params); $recipients = array_column($result, 'username'); + if (intval(safepost('mailboxes_only')) == 0) { + $params2 = []; + $q2 = "SELECT goto FROM $table_alias WHERE active='" . db_get_boolean(true) . "' AND " . db_in_clause("domain", $wanted_domains, $params2); + $result2 = db_query_all($q2, $params2); + $recipients = array_merge($recipients, array_column($result2, 'goto')); + } + $recipients = array_unique($recipients); if (count($recipients) > 0) { diff --git a/public/list-virtual.php b/public/list-virtual.php index f00515ea..b0304943 100644 --- a/public/list-virtual.php +++ b/public/list-virtual.php @@ -65,7 +65,7 @@ if (count($list_domains) == 0) { } if (empty($fDomain)) { - $fDomain = escape_string($list_domains[0]); + $fDomain = $list_domains[0]; } @@ -195,8 +195,8 @@ if (count($search) == 0 || !isset($search['_'])) { $sql_where .= " $table_mailbox.domain= :domain "; $sql_params['domain'] = $fDomain; } else { - $searchterm = escape_string($search['_']); - $sql_where .= db_in_clause("$table_mailbox.domain", $list_domains) . " "; + $searchterm = $search['_']; + $sql_where .= db_in_clause("$table_mailbox.domain", $list_domains, $sql_params) . " "; $sql_where .= " AND ( $table_mailbox.username LIKE :searchterm OR $table_mailbox.name LIKE :searchterm "; $sql_params['searchterm'] = "%$searchterm%"; diff --git a/public/main.php b/public/main.php index 1065d98b..de4e5750 100644 --- a/public/main.php +++ b/public/main.php @@ -43,15 +43,16 @@ $smarty->assign('q', ''); // do not run this search stuff for an admin who has no domains associated. if (!empty($q) && !empty($list_domains)) { - $domain_filter = db_in_clause('domain', $list_domains); + $params = ['q' => "%$q%"]; + $domain_filter = db_in_clause('domain', $list_domains, $params); $table_alias = table_by_key('alias'); $table_domain = table_by_key('domain'); $table_mailbox = table_by_key('mailbox'); - $aliases = db_query_all("SELECT * FROM $table_alias WHERE address LIKE :q AND $domain_filter ORDER BY address ASC LIMIT 15", ['q' => "%$q%"]); - $mailboxes = db_query_all("SELECT * FROM $table_mailbox WHERE username LIKE :q AND $domain_filter ORDER BY username ASC LIMIT 15", ['q' => "%$q%"]); - $domains = db_query_all("SELECT * FROM $table_domain WHERE domain LIKE :q AND $domain_filter AND domain != 'ALL' ORDER BY domain ASC LIMIT 15", ['q' => "%$q%"]); + $aliases = db_query_all("SELECT * FROM $table_alias WHERE address LIKE :q AND $domain_filter ORDER BY address ASC LIMIT 15", $params); + $mailboxes = db_query_all("SELECT * FROM $table_mailbox WHERE username LIKE :q AND $domain_filter ORDER BY username ASC LIMIT 15", $params); + $domains = db_query_all("SELECT * FROM $table_domain WHERE domain LIKE :q AND $domain_filter AND domain != 'ALL' ORDER BY domain ASC LIMIT 15", $params); $smarty->assign('q', $q); $smarty->assign('mailboxes', $mailboxes); diff --git a/public/users/password-recover.php b/public/users/password-recover.php index 77d304be..dc73693b 100644 --- a/public/users/password-recover.php +++ b/public/users/password-recover.php @@ -75,12 +75,10 @@ if ($_SERVER['REQUEST_METHOD'] === "POST") { throw new \InvalidArgumentException("fUsername field required"); } - $tUsername = escape_string($username); - $table = $context === 'admin' ? 'admin' : 'mailbox'; $login = new Login($table); - $token = $login->generatePasswordRecoveryCode($tUsername); + $token = $login->generatePasswordRecoveryCode($username); if ($token !== false) { $table = table_by_key($context === 'users' ? 'mailbox' : 'admin'); @@ -92,15 +90,15 @@ if ($_SERVER['REQUEST_METHOD'] === "POST") { $phone = isset($row['phone']) ? trim($row['phone']) : null; if ($email_other) { - sendCodeByEmail($email_other, $tUsername, $token); + sendCodeByEmail($email_other, $username, $token); } elseif ($phone) { - sendCodeBySMS($phone, $tUsername, $token); + sendCodeBySMS($phone, $username, $token); } else { error_log(__FILE__ . " - No mechanism configured for password-recovery."); } if ($email_other || $phone) { - header("Location: password-change.php?username=" . $tUsername); + header("Location: password-change.php?username=" . urlencode($username)); exit(0); } } diff --git a/public/viewlog.php b/public/viewlog.php index af1c11f9..e6121113 100644 --- a/public/viewlog.php +++ b/public/viewlog.php @@ -48,7 +48,7 @@ $error = 0; if ($_SERVER['REQUEST_METHOD'] == "GET") { if (isset($_GET['page']) && $_GET['fDomain']) { - $fDomain_aux = escape_string($_GET['fDomain']); + $fDomain_aux = $_GET['fDomain']; $flag_fDomain = 0; if ((is_array($list_domains) and sizeof($list_domains) > 0)) { foreach ($list_domains as $domain) { @@ -76,8 +76,8 @@ if ($_SERVER['REQUEST_METHOD'] == "GET") { } } elseif ($_SERVER['REQUEST_METHOD'] == "POST") { $page_number = 1; - if (isset($_POST['fDomain'])) { - $fDomain = escape_string($_POST['fDomain']); + if (isset($_POST['fDomain']) && in_array($_POST['fDomain'], $list_domains)) { + $fDomain = $_POST['fDomain']; } } else { throw new InvalidArgumentException('Unsupported request method');