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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 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 #include <freeradius-devel/ident.h>
43 #include <freeradius-devel/autoconf.h>
50 #include <freeradius-devel/radiusd.h>
54 #include "sql_postgresql.h"
56 typedef struct rlm_sql_postgres_sock {
63 } rlm_sql_postgres_sock;
68 static int sql_num_fields(SQLSOCK * sqlsocket, SQL_CONFIG *config);
71 /* Internal function. Return true if the postgresql status value
72 * indicates successful completion of the query. Return false otherwise
74 status_is_ok(ExecStatusType status)
76 return status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK;
81 /* Internal function. Return the number of affected rows of the result
82 * as an int instead of the string that postgresql provides */
84 affected_rows(PGresult * result)
86 return atoi(PQcmdTuples(result));
89 /* Internal function. Free the row of the current result that's stored
90 * in the pg_sock struct. */
92 free_result_row(rlm_sql_postgres_sock * pg_sock)
95 if (pg_sock->row != NULL) {
96 for (i = pg_sock->num_fields-1; i >= 0; i--) {
97 if (pg_sock->row[i] != NULL) {
98 free(pg_sock->row[i]);
101 free((char*)pg_sock->row);
103 pg_sock->num_fields = 0;
108 /*************************************************************************
109 * Function: check_fatal_error
111 * Purpose: Check error type and behave accordingly
113 *************************************************************************/
115 static int check_fatal_error (char *errorcode)
120 Check the error code to see if we should reconnect or not
121 Error Code table taken from
122 http://www.postgresql.org/docs/8.1/interactive/errcodes-appendix.html
125 while(errorcodes[x].errorcode != NULL){
126 if (strcmp(errorcodes[x].errorcode, errorcode) == 0){
127 radlog(L_DBG, "rlm_sql_postgresql: Postgresql Fatal Error: [%s: %s] Occurred!!", errorcode, errorcodes[x].meaning);
128 if (errorcodes[x].shouldreconnect == 1)
136 radlog(L_DBG, "rlm_sql_postgresql: Postgresql Fatal Error: [%s] Occurred!!", errorcode);
137 /* We don't seem to have a matching error class/code */
143 /*************************************************************************
145 * Function: sql_create_socket
147 * Purpose: Establish connection to the db
149 *************************************************************************/
150 static int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
151 char connstring[2048];
153 rlm_sql_postgres_sock *pg_sock;
155 if (config->sql_server[0] != '\0') {
161 if (config->sql_port[0] != '\0') {
167 if (!sqlsocket->conn) {
168 sqlsocket->conn = (rlm_sql_postgres_sock *)rad_malloc(sizeof(rlm_sql_postgres_sock));
169 if (!sqlsocket->conn) {
174 pg_sock = sqlsocket->conn;
175 memset(pg_sock, 0, sizeof(*pg_sock));
177 snprintf(connstring, sizeof(connstring),
178 "dbname=%s%s%s%s%s user=%s password=%s",
179 config->sql_db, host, config->sql_server,
180 port, config->sql_port,
181 config->sql_login, config->sql_password);
183 pg_sock->result=NULL;
184 pg_sock->conn=PQconnectdb(connstring);
186 if (PQstatus(pg_sock->conn) != CONNECTION_OK) {
187 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);
188 /*radlog(L_ERR, "rlm_sql_postgresql: Postgresql error '%s'", PQerrorMessage(pg_sock->conn));*/
189 PQfinish(pg_sock->conn);
196 /*************************************************************************
198 * Function: sql_query
200 * Purpose: Issue a query to the database
202 *************************************************************************/
203 static int sql_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
205 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
210 if (config->sqltrace)
211 radlog(L_DBG,"rlm_sql_postgresql: query:\n%s", querystr);
213 if (pg_sock->conn == NULL) {
214 radlog(L_ERR, "rlm_sql_postgresql: Socket not connected");
218 pg_sock->result = PQexec(pg_sock->conn, querystr);
220 * Returns a PGresult pointer or possibly a null pointer.
221 * A non-null pointer will generally be returned except in
222 * out-of-memory conditions or serious errors such as inability
223 * to send the command to the server. If a null pointer is
224 * returned, it should be treated like a PGRES_FATAL_ERROR
227 if (!pg_sock->result)
229 radlog(L_ERR, "rlm_sql_postgresql: PostgreSQL Query failed Error: %s",
230 PQerrorMessage(pg_sock->conn));
231 /* As this error COULD be a connection error OR an out-of-memory
232 * condition return value WILL be wrong SOME of the time regardless!
233 * Pick your poison....
237 ExecStatusType status = PQresultStatus(pg_sock->result);
238 radlog(L_DBG, "rlm_sql_postgresql: Status: %s", PQresStatus(status));
242 case PGRES_COMMAND_OK:
243 /*Successful completion of a command returning no data.*/
245 /*affected_rows function only returns
246 the number of affected rows of a command
249 pg_sock->affected_rows = affected_rows(pg_sock->result);
250 radlog(L_DBG, "rlm_sql_postgresql: query affected rows = %i", pg_sock->affected_rows);
255 case PGRES_TUPLES_OK:
256 /*Successful completion of a command returning data (such as a SELECT or SHOW).*/
258 pg_sock->cur_row = 0;
259 pg_sock->affected_rows = PQntuples(pg_sock->result);
260 numfields = PQnfields(pg_sock->result); /*Check row storing functions..*/
261 radlog(L_DBG, "rlm_sql_postgresql: query affected rows = %i , fields = %i", pg_sock->affected_rows, numfields);
266 case PGRES_BAD_RESPONSE:
267 /*The server's response was not understood.*/
268 radlog(L_DBG, "rlm_sql_postgresql: Bad Response From Server!!");
273 case PGRES_NONFATAL_ERROR:
274 /*A nonfatal error (a notice or warning) occurred. Possibly never returns*/
280 case PGRES_FATAL_ERROR:
281 /*A fatal error occurred.*/
283 errorcode = PQresultErrorField(pg_sock->result, PG_DIAG_SQLSTATE);
284 errormsg = PQresultErrorField(pg_sock->result, PG_DIAG_MESSAGE_PRIMARY);
285 radlog(L_DBG, "rlm_sql_postgresql: Error %s", errormsg);
286 return check_fatal_error(errorcode);
291 /* FIXME: An unhandled error occurred.*/
293 /* PGRES_EMPTY_QUERY PGRES_COPY_OUT PGRES_COPY_IN */
303 Note to self ... sql_store_result returns 0 anyway
304 after setting the sqlsocket->affected_rows..
305 sql_num_fields returns 0 at worst case which means the check below
306 has a really small chance to return false..
307 lets remove it then .. yuck!!
311 if ((sql_store_result(sqlsocket, config) == 0)
312 && (sql_num_fields(sqlsocket, config) >= 0))
322 /*************************************************************************
324 * Function: sql_select_query
326 * Purpose: Issue a select query to the database
328 *************************************************************************/
329 static int sql_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
330 return sql_query(sqlsocket, config, querystr);
334 /*************************************************************************
336 * Function: sql_destroy_socket
338 * Purpose: Free socket and private connection data
340 *************************************************************************/
341 static int sql_destroy_socket(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config)
343 free(sqlsocket->conn);
344 sqlsocket->conn = NULL;
348 /*************************************************************************
350 * Function: sql_fetch_row
352 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
353 * with all the data for the query in 'sqlsocket->row'. Returns
354 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'.
356 *************************************************************************/
357 static int sql_fetch_row(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
360 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
362 sqlsocket->row = NULL;
364 if (pg_sock->cur_row >= PQntuples(pg_sock->result))
367 free_result_row(pg_sock);
369 records = PQnfields(pg_sock->result);
370 pg_sock->num_fields = records;
372 if ((PQntuples(pg_sock->result) > 0) && (records > 0)) {
373 pg_sock->row = (char **)rad_malloc((records+1)*sizeof(char *));
374 memset(pg_sock->row, '\0', (records+1)*sizeof(char *));
376 for (i = 0; i < records; i++) {
377 len = PQgetlength(pg_sock->result, pg_sock->cur_row, i);
378 pg_sock->row[i] = (char *)rad_malloc(len+1);
379 memset(pg_sock->row[i], '\0', len+1);
380 strncpy(pg_sock->row[i], PQgetvalue(pg_sock->result, pg_sock->cur_row,i),len);
383 sqlsocket->row = pg_sock->row;
391 /*************************************************************************
393 * Function: sql_free_result
395 * Purpose: database specific free_result. Frees memory allocated
398 *************************************************************************/
399 static int sql_free_result(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
401 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
403 if (pg_sock->result) {
404 PQclear(pg_sock->result);
405 pg_sock->result = NULL;
408 free_result_row(pg_sock);
415 /*************************************************************************
417 * Function: sql_error
419 * Purpose: database specific error. Returns error associated with
422 *************************************************************************/
423 static char *sql_error(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
425 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
427 return PQerrorMessage(pg_sock->conn);
431 /*************************************************************************
433 * Function: sql_close
435 * Purpose: database specific close. Closes an open database
438 *************************************************************************/
439 static int sql_close(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
441 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
443 if (!pg_sock->conn) return 0;
445 PQfinish(pg_sock->conn);
446 pg_sock->conn = NULL;
452 /*************************************************************************
454 * Function: sql_finish_query
456 * Purpose: End the query, such as freeing memory
458 *************************************************************************/
459 static int sql_finish_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
461 return sql_free_result(sqlsocket, config);
466 /*************************************************************************
468 * Function: sql_finish_select_query
470 * Purpose: End the select query, such as freeing memory or result
472 *************************************************************************/
473 static int sql_finish_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
475 return sql_free_result(sqlsocket, config);
479 /*************************************************************************
481 * Function: sql_affected_rows
483 * Purpose: Return the number of rows affected by the last query.
485 *************************************************************************/
486 static int sql_affected_rows(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
487 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
489 return pg_sock->affected_rows;
493 static int NEVER_RETURNS
494 not_implemented(UNUSED SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config)
496 radlog(L_ERR, "sql_postgresql: calling unimplemented function");
501 /* Exported to rlm_sql */
502 rlm_sql_module_t rlm_sql_postgresql = {
503 "rlm_sql_postgresql",
508 not_implemented, /* sql_store_result */
509 not_implemented, /* sql_num_fields */
510 not_implemented, /* sql_num_rows */
512 not_implemented, /* sql_free_result */
516 sql_finish_select_query,