| 1 |
From 07797aba88dca902da7eaf1dfe262c8896943de7 Mon Sep 17 00:00:00 2001 |
| 2 |
From: TJ Saunders <[email protected]> |
| 3 |
Date: Tue, 5 May 2026 09:56:33 -0700 |
| 4 |
Subject: [PATCH] Issue #2057: Properly escape the "name" provided when doing |
| 5 |
SQL lookups for allowed/denied client IP addresses or DNS names. |
| 6 |
|
| 7 |
--- |
| 8 |
contrib/mod_wrap2_sql.c | 49 +++++++++++++++++++++++++++++++++++++---- |
| 9 |
1 file changed, 45 insertions(+), 4 deletions(-) |
| 10 |
|
| 11 |
diff --git a/contrib/mod_wrap2_sql.c b/contrib/mod_wrap2_sql.c |
| 12 |
index eaf6ea7dc5..d4fd56e699 100644 |
| 13 |
--- a/contrib/mod_wrap2_sql.c |
| 14 |
+++ b/contrib/mod_wrap2_sql.c |
| 15 |
@@ -1,7 +1,7 @@ |
| 16 |
/* |
| 17 |
* ProFTPD: mod_wrap2_sql -- a mod_wrap2 sub-module for supplying IP-based |
| 18 |
* access control data via SQL tables |
| 19 |
- * Copyright (c) 2002-2016 TJ Saunders |
| 20 |
+ * Copyright (c) 2002-2026 TJ Saunders |
| 21 |
* |
| 22 |
* This program is free software; you can redistribute it and/or modify |
| 23 |
* it under the terms of the GNU General Public License as published by |
| 24 |
@@ -25,7 +25,7 @@ |
| 25 |
#include "mod_wrap2.h" |
| 26 |
#include "mod_sql.h" |
| 27 |
|
| 28 |
-#define MOD_WRAP2_SQL_VERSION "mod_wrap2_sql/1.0" |
| 29 |
+#define MOD_WRAP2_SQL_VERSION "mod_wrap2_sql/1.1" |
| 30 |
|
| 31 |
#define WRAP2_SQL_NSLOTS 2 |
| 32 |
#define WRAP2_SQL_CLIENT_QUERY_IDX 0 |
| 33 |
@@ -62,6 +62,41 @@ static int sqltab_close_cb(wrap2_table_t *sqltab) { |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
+static char *sqltab_get_escaped_text(pool *p, wrap2_table_t *sqltab, |
| 38 |
+ const char *text) { |
| 39 |
+ pool *tmp_pool = NULL; |
| 40 |
+ cmdtable *sql_cmdtab = NULL; |
| 41 |
+ cmd_rec *sql_cmd = NULL; |
| 42 |
+ modret_t *sql_res = NULL; |
| 43 |
+ |
| 44 |
+ /* Find the cmdtable for the sql_escapestr command, as the provided |
| 45 |
+ * name needs to be properly escaped for SQL syntax; see Issue #2057. |
| 46 |
+ */ |
| 47 |
+ sql_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "sql_escapestr", NULL, NULL, |
| 48 |
+ NULL); |
| 49 |
+ if (sql_cmdtab == NULL) { |
| 50 |
+ wrap2_log("error: unable to find SQL hook symbol 'sql_escapestr': " |
| 51 |
+ "perhaps your proftpd.conf needs 'LoadModule mod_sql.c'?"); |
| 52 |
+ return NULL; |
| 53 |
+ } |
| 54 |
+ |
| 55 |
+ sql_cmd = sql_cmd_create(tmp_pool, 1, text); |
| 56 |
+ sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd); |
| 57 |
+ if (sql_res == NULL) { |
| 58 |
+ wrap2_log("sql_escapestr '%s' returned no data; " |
| 59 |
+ "see the mod_sql.c SQLLogFile for more details", text); |
| 60 |
+ return NULL; |
| 61 |
+ } |
| 62 |
+ |
| 63 |
+ if (MODRET_ISERROR(sql_res)) { |
| 64 |
+ wrap2_log("error processing sql_escapestr '%s': " |
| 65 |
+ "check the mod_sql.c SQLLogFile for more details", text); |
| 66 |
+ return NULL; |
| 67 |
+ } |
| 68 |
+ |
| 69 |
+ return sql_res->data; |
| 70 |
+} |
| 71 |
+ |
| 72 |
static array_header *sqltab_fetch_clients_cb(wrap2_table_t *sqltab, |
| 73 |
const char *name) { |
| 74 |
register unsigned int i; |
| 75 |
@@ -70,12 +105,18 @@ static array_header *sqltab_fetch_clients_cb(wrap2_table_t *sqltab, |
| 76 |
cmd_rec *sql_cmd = NULL; |
| 77 |
modret_t *sql_res = NULL; |
| 78 |
array_header *sql_data = NULL; |
| 79 |
- char *query = NULL, **vals = NULL; |
| 80 |
+ char *escaped_name = NULL, *query = NULL, **vals = NULL; |
| 81 |
array_header *clients_list = NULL; |
| 82 |
|
| 83 |
/* Allocate a temporary pool for the duration of this read. */ |
| 84 |
tmp_pool = make_sub_pool(sqltab->tab_pool); |
| 85 |
|
| 86 |
+ escaped_name = sqltab_get_escaped_text(tmp_pool, sqltab, name); |
| 87 |
+ if (escaped_name == NULL) { |
| 88 |
+ destroy_pool(tmp_pool); |
| 89 |
+ return NULL; |
| 90 |
+ } |
| 91 |
+ |
| 92 |
query = ((char **) sqltab->tab_data)[WRAP2_SQL_CLIENT_QUERY_IDX]; |
| 93 |
|
| 94 |
/* Find the cmdtable for the sql_lookup command. */ |
| 95 |
@@ -89,7 +130,7 @@ static array_header *sqltab_fetch_clients_cb(wrap2_table_t *sqltab, |
| 96 |
} |
| 97 |
|
| 98 |
/* Prepare the SELECT query. */ |
| 99 |
- sql_cmd = sql_cmd_create(tmp_pool, 3, "sql_lookup", query, name); |
| 100 |
+ sql_cmd = sql_cmd_create(tmp_pool, 3, "sql_lookup", query, escaped_name); |
| 101 |
|
| 102 |
/* Call the handler. */ |
| 103 |
sql_res = pr_module_call(sql_cmdtab->m, sql_cmdtab->handler, sql_cmd); |