2 * sql_postgresql.c Postgresql rlm_sql driver
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
21 * Copyright 2000 Mike Machado <mike@innercite.com>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
28 * Use blocking queries and delete unused functions. In
29 * rlm_sql_postgresql replace all functions that are not really used
30 * with the not_implemented function.
32 * Add a new field to the rlm_sql_postgres_sock struct to store the
33 * number of rows affected by a query because the sql module calls
34 * finish_query before it retrieves the number of affected rows from the
37 * Bernhard Herzog <bh@intevation.de>
40 /* Modification of rlm_sql_mysql to handle postgres */
52 typedef struct rlm_sql_postgres_sock {
59 } rlm_sql_postgres_sock;
62 static int sql_store_result(SQLSOCK * sqlsocket, SQL_CONFIG *config);
63 static int sql_num_fields(SQLSOCK * sqlsocket, SQL_CONFIG *config);
65 /* Internal function. Return true if the postgresql status value
66 * indicates successful completion of the query. Return false otherwise
69 status_is_ok(ExecStatusType status)
71 return status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK;
75 /* Internal function. Return the number of affected rows of the result
76 * as an int instead of the string that postgresql provides */
78 affected_rows(PGresult * result)
80 return atoi(PQcmdTuples(result));
83 /* Internal function. Free the row of the current result that's stored
84 * in the pg_sock struct. */
86 free_result_row(rlm_sql_postgres_sock * pg_sock)
89 if (pg_sock->row != NULL) {
90 for (i = pg_sock->num_fields-1; i >= 0; i--) {
91 if (pg_sock->row[i] != NULL) {
92 xfree(pg_sock->row[i]);
95 xfree((char*)pg_sock->row);
97 pg_sock->num_fields = 0;
101 /*************************************************************************
103 * Function: sql_check_error
105 * Purpose: check the error to see if the server is down
107 * Note: It is possible that something other than a connection error
108 * could cause PGRES_FATAL_ERROR. If that happens a reconnect will
109 * occur anyway. Not optimal, but I couldn't find a way to check it.
110 * Peter Nixon <codemonkey@peternixon.net>
113 ************************************************************************/
114 static int sql_check_error(int error) {
116 case PGRES_FATAL_ERROR:
118 radlog(L_DBG, "rlm_sql_postgresql: Postgresql check_error: %s, returning SQL_DOWN", PQresStatus(error));
122 case PGRES_COMMAND_OK:
123 case PGRES_TUPLES_OK:
128 case PGRES_NONFATAL_ERROR:
129 case PGRES_BAD_RESPONSE:
131 radlog(L_DBG, "rlm_sql_postgresql: Postgresql check_error: %s received", PQresStatus(error));
138 /*************************************************************************
140 * Function: sql_create_socket
142 * Purpose: Establish connection to the db
144 *************************************************************************/
145 static int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
146 char connstring[2048];
148 rlm_sql_postgres_sock *pg_sock;
150 if (config->sql_server[0] != '\0') {
156 if (config->sql_port[0] != '\0') {
162 if (!sqlsocket->conn) {
163 sqlsocket->conn = (rlm_sql_postgres_sock *)rad_malloc(sizeof(rlm_sql_postgres_sock));
164 if (!sqlsocket->conn) {
169 pg_sock = sqlsocket->conn;
170 memset(pg_sock, 0, sizeof(*pg_sock));
172 snprintf(connstring, sizeof(connstring),
173 "dbname=%s%s%s%s%s user=%s password=%s",
174 config->sql_db, host, config->sql_server,
175 port, config->sql_port,
176 config->sql_login, config->sql_password);
178 pg_sock->result=NULL;
179 pg_sock->conn=PQconnectdb(connstring);
181 if (PQstatus(pg_sock->conn) == CONNECTION_BAD) {
182 radlog(L_ERR, "rlm_sql_postgresql: Couldn't connect socket to PostgreSQL server %s@%s:%s", config->sql_login, config->sql_server, config->sql_db);
183 radlog(L_ERR, "rlm_sql_postgresql: Postgresql error '%s'", PQerrorMessage(pg_sock->conn));
184 PQfinish(pg_sock->conn);
191 /*************************************************************************
193 * Function: sql_query
195 * Purpose: Issue a query to the database
197 *************************************************************************/
198 static int sql_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
200 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
202 if (config->sqltrace)
203 radlog(L_DBG,"rlm_sql_postgresql: query:\n%s", querystr);
205 if (pg_sock->conn == NULL) {
206 radlog(L_ERR, "rlm_sql_postgresql: Socket not connected");
210 pg_sock->result = PQexec(pg_sock->conn, querystr);
211 /* Returns a result pointer or possibly a NULL pointer.
212 * A non-NULL pointer will generally be returned except in
213 * out-of-memory conditions or serious errors such as inability
214 * to send the command to the backend. If a NULL is returned,
215 * it should be treated like a PGRES_FATAL_ERROR result.
216 * Use PQerrorMessage to get more information about the error.
218 if (!pg_sock->result)
220 radlog(L_ERR, "rlm_sql_postgresql: PostgreSQL Query failed Error: %s",
221 PQerrorMessage(pg_sock->conn));
224 ExecStatusType status = PQresultStatus(pg_sock->result);
226 radlog(L_DBG, "rlm_sql_postgresql: Status: %s", PQresStatus(status));
228 radlog(L_DBG, "rlm_sql_postgresql: affected rows = %s",
229 PQcmdTuples(pg_sock->result));
231 if (!status_is_ok(status))
232 return sql_check_error(status);
234 if (strncasecmp("select", querystr, 6) != 0) {
235 /* store the number of affected rows because the sql module
236 * calls finish_query before it retrieves the number of affected
237 * rows from the driver */
238 pg_sock->affected_rows = affected_rows(pg_sock->result);
241 if ((sql_store_result(sqlsocket, config) == 0)
242 && (sql_num_fields(sqlsocket, config) >= 0))
251 /*************************************************************************
253 * Function: sql_select_query
255 * Purpose: Issue a select query to the database
257 *************************************************************************/
258 static int sql_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
259 return sql_query(sqlsocket, config, querystr);
263 /*************************************************************************
265 * Function: sql_store_result
267 * Purpose: database specific store_result function. Returns a result
270 *************************************************************************/
271 static int sql_store_result(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
272 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
274 pg_sock->cur_row = 0;
275 pg_sock->affected_rows = PQntuples(pg_sock->result);
280 /*************************************************************************
282 * Function: sql_destroy_socket
284 * Purpose: Free socket and private connection data
286 *************************************************************************/
287 static int sql_destroy_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config)
289 free(sqlsocket->conn);
290 sqlsocket->conn = NULL;
294 /*************************************************************************
296 * Function: sql_num_fields
298 * Purpose: database specific num_fields function. Returns number
299 * of columns from query
301 *************************************************************************/
302 static int sql_num_fields(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
305 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
307 if (!(num = PQnfields(pg_sock->result))) {
308 radlog(L_ERR, "rlm_sql_postgresql: PostgreSQL Error: Cannot get result");
309 radlog(L_ERR, "rlm_sql_postgresql: PostgreSQL error: %s", PQerrorMessage(pg_sock->conn));
315 /*************************************************************************
317 * Function: sql_fetch_row
319 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
320 * with all the data for the query in 'sqlsocket->row'. Returns
321 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'.
323 *************************************************************************/
324 static int sql_fetch_row(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
327 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
329 sqlsocket->row = NULL;
331 if (pg_sock->cur_row >= PQntuples(pg_sock->result))
334 free_result_row(pg_sock);
336 records = PQnfields(pg_sock->result);
337 pg_sock->num_fields = records;
339 if ((PQntuples(pg_sock->result) > 0) && (records > 0)) {
340 pg_sock->row = (char **)rad_malloc((records+1)*sizeof(char *));
341 memset(pg_sock->row, '\0', (records+1)*sizeof(char *));
343 for (i = 0; i < records; i++) {
344 len = PQgetlength(pg_sock->result, pg_sock->cur_row, i);
345 pg_sock->row[i] = (char *)rad_malloc(len+1);
346 memset(pg_sock->row[i], '\0', len+1);
347 strncpy(pg_sock->row[i], PQgetvalue(pg_sock->result, pg_sock->cur_row,i),len);
350 sqlsocket->row = pg_sock->row;
359 /*************************************************************************
361 * Function: sql_free_result
363 * Purpose: database specific free_result. Frees memory allocated
366 *************************************************************************/
367 static int sql_free_result(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
369 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
371 if (pg_sock->result) {
372 PQclear(pg_sock->result);
373 pg_sock->result = NULL;
377 * Commented out because it appears to free memory too early.
379 free_result_row(pg_sock);
387 /*************************************************************************
389 * Function: sql_error
391 * Purpose: database specific error. Returns error associated with
394 *************************************************************************/
395 static char *sql_error(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
397 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
399 return PQerrorMessage(pg_sock->conn);
403 /*************************************************************************
405 * Function: sql_close
407 * Purpose: database specific close. Closes an open database
410 *************************************************************************/
411 static int sql_close(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
413 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
415 if (!pg_sock->conn) return 0;
417 PQfinish(pg_sock->conn);
418 pg_sock->conn = NULL;
424 /*************************************************************************
426 * Function: sql_finish_query
428 * Purpose: End the query, such as freeing memory
430 *************************************************************************/
431 static int sql_finish_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
433 return sql_free_result(sqlsocket, config);
438 /*************************************************************************
440 * Function: sql_finish_select_query
442 * Purpose: End the select query, such as freeing memory or result
444 *************************************************************************/
445 static int sql_finish_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
447 return sql_free_result(sqlsocket, config);
451 /*************************************************************************
453 * Function: sql_affected_rows
455 * Purpose: Return the number of rows affected by the last query.
457 *************************************************************************/
458 static int sql_affected_rows(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
459 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
461 return pg_sock->affected_rows;
465 static int NEVER_RETURNS
466 not_implemented(SQLSOCK * sqlsocket, SQL_CONFIG *config)
468 radlog(L_ERR, "sql_postgresql: calling unimplemented function");
473 /* Exported to rlm_sql */
474 rlm_sql_module_t rlm_sql_postgresql = {
475 "rlm_sql_postgresql",
480 not_implemented, /* sql_store_result */
481 not_implemented, /* sql_num_fields */
482 not_implemented, /* sql_num_rows */
484 not_implemented, /* sql_free_result */
488 sql_finish_select_query,