]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add support for "open_query" option to rlm_sql_postgresql
authorNick Porter <nick@portercomputing.co.uk>
Tue, 19 Aug 2025 11:49:39 +0000 (12:49 +0100)
committerNick Porter <nick@portercomputing.co.uk>
Tue, 19 Aug 2025 11:49:39 +0000 (12:49 +0100)
The option is listed (commented out) in the stock queries.conf

src/modules/rlm_sql/drivers/rlm_sql_postgresql/rlm_sql_postgresql.c

index 95464aefcfa48545fb1d455e1c1d7dc5bb18cf26..c6610a44a96c44142ba97195a349bbcc1c9896d8 100644 (file)
@@ -271,6 +271,43 @@ static void _sql_connect_io_notify(fr_event_list_t *el, int fd, UNUSED int flags
        }
 }
 
+static void _sql_connect_query_run(connection_t *conn, UNUSED connection_state_t prev,
+                                  UNUSED connection_state_t state, void *uctx)
+{
+       rlm_sql_t const                 *sql = talloc_get_type_abort_const(uctx, rlm_sql_t);
+       rlm_sql_postgresql_t            *inst = talloc_get_type_abort(sql->driver_submodule->data, rlm_sql_postgresql_t);
+       rlm_sql_postgres_conn_t         *sql_conn = talloc_get_type_abort(conn->h, rlm_sql_postgres_conn_t);
+       int                             err;
+       PGresult                        *result, *tmp_result;
+       ExecStatusType                  status;
+       sql_rcode_t                     rcode;
+
+       DEBUG2("Executing \"%s\"", sql->config.connect_query);
+
+       err = PQsendQuery(sql_conn->db, sql->config.connect_query);
+       if (!err) {
+       fail:
+               ERROR("Failed running \"open_query\": %s", PQerrorMessage(sql_conn->db));
+               connection_signal_reconnect(conn, CONNECTION_FAILED);
+               return;
+       }
+
+       result = PQgetResult(sql_conn->db);
+       if (!result) goto fail;
+
+       /*
+        *  Clear up any additional results returned
+        */
+       while ((tmp_result = PQgetResult(sql_conn->db))) PQclear(tmp_result);
+
+       status = PQresultStatus(result);
+
+       rcode = sql_classify_error(inst, status, result);
+       PQclear(result);
+
+       if (rcode != RLM_SQL_OK) goto fail;
+}
+
 CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
 static connection_state_t _sql_connection_init(void **h, connection_t *conn, void *uctx)
 {
@@ -324,6 +361,9 @@ static connection_state_t _sql_connection_init(void **h, connection_t *conn, voi
 
        *h = c;
 
+       if (sql->config.connect_query) connection_add_watch_post(conn, CONNECTION_STATE_CONNECTED,
+                                                                _sql_connect_query_run, true, sql);
+
        return CONNECTION_STATE_CONNECTING;
 }