]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Pass fr_sql_query_t to sql_error functions
authorNick Porter <nick@portercomputing.co.uk>
Mon, 13 May 2024 14:48:07 +0000 (15:48 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 7 Jun 2024 02:26:58 +0000 (22:26 -0400)
12 files changed:
src/modules/rlm_sql/drivers/rlm_sql_cassandra/rlm_sql_cassandra.c
src/modules/rlm_sql/drivers/rlm_sql_db2/rlm_sql_db2.c
src/modules/rlm_sql/drivers/rlm_sql_firebird/rlm_sql_firebird.c
src/modules/rlm_sql/drivers/rlm_sql_freetds/rlm_sql_freetds.c
src/modules/rlm_sql/drivers/rlm_sql_mysql/rlm_sql_mysql.c
src/modules/rlm_sql/drivers/rlm_sql_null/rlm_sql_null.c
src/modules/rlm_sql/drivers/rlm_sql_oracle/rlm_sql_oracle.c
src/modules/rlm_sql/drivers/rlm_sql_postgresql/rlm_sql_postgresql.c
src/modules/rlm_sql/drivers/rlm_sql_sqlite/rlm_sql_sqlite.c
src/modules/rlm_sql/drivers/rlm_sql_unixodbc/rlm_sql_unixodbc.c
src/modules/rlm_sql/rlm_sql.h
src/modules/rlm_sql/sql.c

index a08f598dd9d67c5a51f02b49dcea79659bf2d540..84c8981d088d1ea7a6bcf4fc6c025055965f18ca 100644 (file)
@@ -674,9 +674,9 @@ static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_con
 }
 
 static size_t sql_error(UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
-                       rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
-       rlm_sql_cassandra_conn_t *conn = handle->conn;
+       rlm_sql_cassandra_conn_t *conn = query_ctx->handle->conn;
 
        if (conn->last_error.msg && (outlen >= 1)) {
                out[0].msg = conn->last_error.msg;
index 00f7830d7affc7abca94bfebe8354b135ba3208e..bfd6fb2bf6702849cff10ab2f1b409429b492510 100644 (file)
@@ -232,25 +232,25 @@ static sql_rcode_t sql_free_result(fr_sql_query_t *query_, UNUSED rlm_sql_config
        return RLM_SQL_OK;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller will free any memory allocated in ctx.
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], NDEBUG_UNUSED size_t outlen,
-                       rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
        char                    state[6];
        char                    errbuff[1024];
        SQLINTEGER              err;
        SQLSMALLINT             rl;
-       rlm_sql_db2_conn_t      *conn = handle->conn;
+       rlm_sql_db2_conn_t      *conn = query_ctx->handle->conn;
 
        fr_assert(conn);
        fr_assert(outlen > 0);
index f53af3ba9a2443a3201fa620a34f4e5a8a7f6206..302b73e64ebdfda065c4965c6b5da11c8b58e9b1 100644 (file)
@@ -250,21 +250,21 @@ static sql_rcode_t sql_free_result(UNUSED fr_sql_query_t *query_ctx, UNUSED rlm_
        return 0;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller will free any memory allocated in ctx.
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], NDEBUG_UNUSED size_t outlen,
-                       rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
-       rlm_sql_firebird_conn_t *conn = handle->conn;
+       rlm_sql_firebird_conn_t *conn = query_ctx->handle->conn;
 
        fr_assert(conn);
        fr_assert(outlen > 0);
index 55c7881a02e2039bf3989f4715bc118d5ec70902..8b070a22a89fcbabc228c99855d1c0fe5488eec3 100644 (file)
@@ -400,21 +400,21 @@ static sql_rcode_t sql_fields(char const **out[], rlm_sql_handle_t *handle, UNUS
        return RLM_SQL_OK;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller will free any memory allocated in ctx.
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], NDEBUG_UNUSED size_t outlen,
-                       rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
-       rlm_sql_freetds_conn_t *conn = handle->conn;
+       rlm_sql_freetds_conn_t *conn = query_ctx->handle->conn;
 
        fr_assert(conn && conn->db);
        fr_assert(outlen > 0);
@@ -808,6 +808,7 @@ static sql_rcode_t sql_socket_init(rlm_sql_handle_t *handle, rlm_sql_config_t co
                MEM(query_ctx = fr_sql_query_alloc(NULL, handle->inst, NULL, handle, NULL, database, SQL_QUERY_OTHER));
                if ((sql_query(&p_result, NULL, NULL, query_ctx) == UNLANG_ACTION_CALCULATE_RESULT) &&
                    (query_ctx->rcode != RLM_SQL_OK)) {
+                       talloc_free(query_ctx);
                        goto error;
                }
                talloc_free(query_ctx);
@@ -819,7 +820,7 @@ error:
        if (conn->context) {
                sql_log_entry_t error;
 
-               if (sql_error(NULL, &error, 1, handle, config) > 0) ERROR("%s", error.msg);
+               if (sql_error(NULL, &error, 1, &(fr_sql_query_t){ .handle = handle }, config) > 0) ERROR("%s", error.msg);
        }
 
        return RLM_SQL_ERROR;
index 4b926f5715d8fff274fd6e6e8c4737da87c05ae2..645d10db741cbd7b3b6fcdda7a642697db3d31fc 100644 (file)
@@ -573,17 +573,15 @@ static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_con
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param conn MySQL connection the query was run on.
  * @param config rlm_sql config.
  * @return
  *     - Number of errors written to the #sql_log_entry_t array.
  *     - -1 on failure.
  */
 static size_t sql_warnings(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
-                          rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                          rlm_sql_mysql_conn_t *conn, UNUSED rlm_sql_config_t const *config)
 {
-       rlm_sql_mysql_conn_t    *conn = talloc_get_type_abort(handle->conn, rlm_sql_mysql_conn_t);
-
        MYSQL_RES               *result;
        MYSQL_ROW               row;
        unsigned int            num_fields;
@@ -633,22 +631,22 @@ static size_t sql_warnings(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen
        return i;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller should free any memory allocated in ctx (talloc_free_children()).
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
-                       rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
 {
-       rlm_sql_mysql_t         *inst = talloc_get_type_abort(handle->inst->driver_submodule->data, rlm_sql_mysql_t);
-       rlm_sql_mysql_conn_t    *conn = talloc_get_type_abort(handle->conn, rlm_sql_mysql_conn_t);
+       rlm_sql_mysql_t const   *inst = talloc_get_type_abort_const(query_ctx->inst->driver_submodule->data, rlm_sql_mysql_t);
+       rlm_sql_mysql_conn_t    *conn = talloc_get_type_abort(query_ctx->handle->conn, rlm_sql_mysql_conn_t);
        char const              *error;
        size_t                  i = 0;
 
@@ -685,7 +683,7 @@ static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
 
                FALL_THROUGH;
                case SERVER_WARNINGS_YES:
-                       ret = sql_warnings(ctx, out, outlen - 1, handle, config);
+                       ret = sql_warnings(ctx, out, outlen - 1, conn, config);
                        if (ret > 0) i += ret;
                        break;
 
index 7d47bd9303273ae57aaa4cb0cf07f96648715f43..64737f020b6d09e7e35a415d7bb245638cfeb2ac 100644 (file)
@@ -70,7 +70,7 @@ static sql_rcode_t sql_free_result(UNUSED fr_sql_query_t *query_ctx, UNUSED rlm_
  *
  */
 static size_t sql_error(UNUSED TALLOC_CTX *ctx, UNUSED sql_log_entry_t out[], UNUSED size_t outlen,
-                       UNUSED rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       UNUSED fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
        return 0;
 }
index 5c0a2e08abf478b82caa82f122714cc628c7b080..d42718aee10b6498e5ef40a986264a698307db4b 100644 (file)
@@ -127,26 +127,26 @@ static int sql_snprint_error(char *out, size_t outlen, rlm_sql_handle_t *handle,
        return 0;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller will free any memory allocated in ctx.
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], NDEBUG_UNUSED size_t outlen,
-                       rlm_sql_handle_t *handle, rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, rlm_sql_config_t const *config)
 {
        char errbuff[512];
        int ret;
 
        fr_assert(outlen > 0);
 
-       ret = sql_snprint_error(errbuff, sizeof(errbuff), handle, config);
+       ret = sql_snprint_error(errbuff, sizeof(errbuff), query_ctx->handle, config);
        if (ret < 0) return 0;
 
        out[0].type = L_ERR;
index 25f38817181a541493a8d3e92073f9659fdd9c25..849a7ee89ab638c65f19baada8fb9be1c6c3c3bc 100644 (file)
@@ -479,21 +479,21 @@ static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_con
        return 0;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller will free any memory allocated in ctx.
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen,
-                       rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
-       rlm_sql_postgres_conn_t *conn = handle->conn;
+       rlm_sql_postgres_conn_t *conn = query_ctx->handle->conn;
        char const              *p, *q;
        size_t                  i = 0;
 
index 66d5d7d1e50917e58c655df984f4376f7c52bd5f..ee9aa588e46569892a37b397d7a8609e3568d2a6 100644 (file)
@@ -632,21 +632,21 @@ static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_con
        return RLM_SQL_OK;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller will free any memory allocated in ctx.
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of sql_log_entrys to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(UNUSED TALLOC_CTX *ctx, sql_log_entry_t out[], NDEBUG_UNUSED size_t outlen,
-                       rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
-       rlm_sql_sqlite_conn_t *conn = handle->conn;
+       rlm_sql_sqlite_conn_t *conn = query_ctx->handle->conn;
        char const *error;
 
        fr_assert(outlen > 0);
index 79acd9c93bc32a0a9dcdfc333f02c3cdb1528667..32427e892d8bc3247272d1adf17d3af452abe670 100644 (file)
@@ -279,21 +279,21 @@ static sql_rcode_t sql_free_result(fr_sql_query_t *query_ctx, UNUSED rlm_sql_con
        return 0;
 }
 
-/** Retrieves any errors associated with the connection handle
+/** Retrieves any errors associated with the query context
  *
  * @note Caller will free any memory allocated in ctx.
  *
  * @param ctx to allocate temporary error buffers in.
  * @param out Array of #sql_log_entry_t to fill.
  * @param outlen Length of out array.
- * @param handle rlm_sql connection handle.
+ * @param query_ctx Query context to retrieve error for.
  * @param config rlm_sql config.
  * @return number of errors written to the #sql_log_entry_t array.
  */
 static size_t sql_error(TALLOC_CTX *ctx, sql_log_entry_t out[], NDEBUG_UNUSED size_t outlen,
-                       rlm_sql_handle_t *handle, UNUSED rlm_sql_config_t const *config)
+                       fr_sql_query_t *query_ctx, UNUSED rlm_sql_config_t const *config)
 {
-       rlm_sql_unixodbc_conn_t         *conn = handle->conn;
+       rlm_sql_unixodbc_conn_t         *conn = query_ctx->handle->conn;
        SQLCHAR                         state[256];
        SQLCHAR                         errbuff[256];
        SQLINTEGER                      errnum = 0;
index 7e095b2de0db19387706eb26744364af386cc4f3..28d9fed1fdde6c68f4b5a90a060c22b6cdfbe9bc 100644 (file)
@@ -176,7 +176,7 @@ extern size_t sql_rcode_table_len;
  *     0  - If no error messages are available.
  *     >0 - Number of log entries
  */
-typedef size_t (*sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, rlm_sql_handle_t *handle,
+typedef size_t (*sql_error_t)(TALLOC_CTX *ctx, sql_log_entry_t out[], size_t outlen, fr_sql_query_t *handle,
                              rlm_sql_config_t const *config);
 
 typedef struct {
@@ -246,7 +246,7 @@ void                rlm_sql_query_log(rlm_sql_t const *inst, char const *filename, char const
 unlang_action_t rlm_sql_select_query(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx);
 unlang_action_t        rlm_sql_query(rlm_rcode_t *p_result, int *priority, request_t *request, void *uctx);
 unlang_action_t rlm_sql_fetch_row(rlm_rcode_t *p_result, UNUSED int *priority, request_t *request, void *uctx);
-void           rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t *handle, bool force_debug);
+void           rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, fr_sql_query_t *query_ctx, bool force_debug);
 fr_sql_query_t *fr_sql_query_alloc(TALLOC_CTX *ctx, rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t *handle, fr_trunk_t *trunk, char const *query_str, fr_sql_query_type_t type);
 
 /*
index abbda18965108f3422232de787aaf005e1f83859..e56eb7478cf3612ed3cf4e5921855fe9de7a0d5a 100644 (file)
@@ -332,7 +332,7 @@ unlang_action_t rlm_sql_fetch_row(rlm_rcode_t *p_result, UNUSED int *priority, r
 
        default:
                ROPTIONAL(RERROR, ERROR, "Error fetching row");
-               rlm_sql_print_error(inst, request, query_ctx->handle, false);
+               rlm_sql_print_error(inst, request, query_ctx, false);
                RETURN_MODULE_FAIL;
        }
 }
@@ -344,18 +344,20 @@ unlang_action_t rlm_sql_fetch_row(rlm_rcode_t *p_result, UNUSED int *priority, r
  *
  * @param inst Instance of rlm_sql.
  * @param request Current request, may be NULL.
- * @param handle Handle to retrieve errors for.
+ * @param query_ctx Query context to retrieve errors for.
  * @param force_debug Force all errors to be logged as debug messages.
  */
-void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, rlm_sql_handle_t *handle, bool force_debug)
+void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, fr_sql_query_t *query_ctx, bool force_debug)
 {
        char const      *driver = inst->driver_submodule->name;
        sql_log_entry_t log[20];
        size_t          num, i;
+       TALLOC_CTX      *log_ctx = talloc_new(NULL);
 
-       num = (inst->driver->sql_error)(handle->log_ctx, log, (NUM_ELEMENTS(log)), handle, &inst->config);
+       num = (inst->driver->sql_error)(log_ctx, log, (NUM_ELEMENTS(log)), query_ctx, &inst->config);
        if (num == 0) {
                ROPTIONAL(RERROR, ERROR, "Unknown error");
+               talloc_free(log_ctx);
                return;
        }
 
@@ -383,7 +385,7 @@ void rlm_sql_print_error(rlm_sql_t const *inst, request_t *request, rlm_sql_hand
                }
        }
 
-       talloc_free_children(handle->log_ctx);
+       talloc_free(log_ctx);
 }
 
 /** Automatically run the correct `finish` function when freeing an SQL query
@@ -485,7 +487,7 @@ unlang_action_t rlm_sql_query(rlm_rcode_t *p_result, UNUSED int *priority, reque
                 *      These are bad and should make rlm_sql return invalid
                 */
                case RLM_SQL_QUERY_INVALID:
-                       rlm_sql_print_error(inst, request, query_ctx->handle, false);
+                       rlm_sql_print_error(inst, request, query_ctx, false);
                        (inst->driver->sql_finish_query)(query_ctx, &inst->config);
                        RETURN_MODULE_INVALID;
 
@@ -500,7 +502,7 @@ unlang_action_t rlm_sql_query(rlm_rcode_t *p_result, UNUSED int *priority, reque
                 */
                case RLM_SQL_ERROR:
                        if (inst->driver->flags & RLM_SQL_RCODE_FLAGS_ALT_QUERY) {
-                               rlm_sql_print_error(inst, request, query_ctx->handle, false);
+                               rlm_sql_print_error(inst, request, query_ctx, false);
                                (inst->driver->sql_finish_query)(query_ctx, &inst->config);
                                RETURN_MODULE_FAIL;
                        }
@@ -510,7 +512,7 @@ unlang_action_t rlm_sql_query(rlm_rcode_t *p_result, UNUSED int *priority, reque
                 *      Driver suggested using an alternative query
                 */
                case RLM_SQL_ALT_QUERY:
-                       rlm_sql_print_error(inst, request, query_ctx->handle, true);
+                       rlm_sql_print_error(inst, request, query_ctx, true);
                        (inst->driver->sql_finish_query)(query_ctx, &inst->config);
                        break;
 
@@ -591,7 +593,7 @@ unlang_action_t rlm_sql_select_query(rlm_rcode_t *p_result, UNUSED int *priority
                case RLM_SQL_QUERY_INVALID:
                case RLM_SQL_ERROR:
                default:
-                       rlm_sql_print_error(inst, request, query_ctx->handle, false);
+                       rlm_sql_print_error(inst, request, query_ctx, false);
                        (inst->driver->sql_finish_select_query)(query_ctx, &inst->config);
                        if (query_ctx->rcode == RLM_SQL_QUERY_INVALID) RETURN_MODULE_INVALID;
                        RETURN_MODULE_FAIL;