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;
66 static int sql_close(SQLSOCK *sqlsocket, SQL_CONFIG *config);
68 /* Internal function. Return true if the postgresql status value
69 * indicates successful completion of the query. Return false otherwise
71 status_is_ok(ExecStatusType status)
73 return status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK;
78 /* Internal function. Return the number of affected rows of the result
79 * as an int instead of the string that postgresql provides */
81 affected_rows(PGresult * result)
83 return atoi(PQcmdTuples(result));
86 /* Internal function. Free the row of the current result that's stored
87 * in the pg_sock struct. */
89 free_result_row(rlm_sql_postgres_sock * pg_sock)
92 if (pg_sock->row != NULL) {
93 for (i = pg_sock->num_fields-1; i >= 0; i--) {
94 if (pg_sock->row[i] != NULL) {
95 free(pg_sock->row[i]);
98 free((char*)pg_sock->row);
100 pg_sock->num_fields = 0;
105 /*************************************************************************
106 * Function: check_fatal_error
108 * Purpose: Check error type and behave accordingly
110 *************************************************************************/
112 static int check_fatal_error (char *errorcode)
117 Check the error code to see if we should reconnect or not
118 Error Code table taken from
119 http://www.postgresql.org/docs/8.1/interactive/errcodes-appendix.html
122 while(errorcodes[x].errorcode != NULL){
123 if (strcmp(errorcodes[x].errorcode, errorcode) == 0){
124 radlog(L_DBG, "rlm_sql_postgresql: Postgresql Fatal Error: [%s: %s] Occurred!!", errorcode, errorcodes[x].meaning);
125 if (errorcodes[x].shouldreconnect == 1)
133 radlog(L_DBG, "rlm_sql_postgresql: Postgresql Fatal Error: [%s] Occurred!!", errorcode);
134 /* We don't seem to have a matching error class/code */
140 /*************************************************************************
142 * Function: sql_create_socket
144 * Purpose: Establish connection to the db
146 *************************************************************************/
147 static int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
148 char connstring[2048];
150 rlm_sql_postgres_sock *pg_sock;
152 if (config->sql_server[0] != '\0') {
158 if (config->sql_port[0] != '\0') {
164 if (!sqlsocket->conn) {
165 sqlsocket->conn = (rlm_sql_postgres_sock *)rad_malloc(sizeof(rlm_sql_postgres_sock));
166 if (!sqlsocket->conn) {
171 pg_sock = sqlsocket->conn;
172 memset(pg_sock, 0, sizeof(*pg_sock));
174 snprintf(connstring, sizeof(connstring),
175 "dbname=%s%s%s%s%s user=%s password=%s",
176 config->sql_db, host, config->sql_server,
177 port, config->sql_port,
178 config->sql_login, config->sql_password);
180 pg_sock->result=NULL;
181 pg_sock->conn=PQconnectdb(connstring);
183 if (PQstatus(pg_sock->conn) != CONNECTION_OK) {
184 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);
185 /*radlog(L_ERR, "rlm_sql_postgresql: Postgresql error '%s'", PQerrorMessage(pg_sock->conn));*/
186 sql_close(sqlsocket, config);
193 /*************************************************************************
195 * Function: sql_query
197 * Purpose: Issue a query to the database
199 *************************************************************************/
200 static int sql_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
202 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
207 if (config->sqltrace)
208 radlog(L_DBG,"rlm_sql_postgresql: query:\n%s", querystr);
210 if (pg_sock->conn == NULL) {
211 radlog(L_ERR, "rlm_sql_postgresql: Socket not connected");
215 pg_sock->result = PQexec(pg_sock->conn, querystr);
217 * Returns a PGresult pointer or possibly a null pointer.
218 * A non-null pointer will generally be returned except in
219 * out-of-memory conditions or serious errors such as inability
220 * to send the command to the server. If a null pointer is
221 * returned, it should be treated like a PGRES_FATAL_ERROR
224 if (!pg_sock->result)
226 radlog(L_ERR, "rlm_sql_postgresql: PostgreSQL Query failed Error: %s",
227 PQerrorMessage(pg_sock->conn));
228 /* As this error COULD be a connection error OR an out-of-memory
229 * condition return value WILL be wrong SOME of the time regardless!
230 * Pick your poison....
234 ExecStatusType status = PQresultStatus(pg_sock->result);
235 radlog(L_DBG, "rlm_sql_postgresql: Status: %s", PQresStatus(status));
239 case PGRES_COMMAND_OK:
240 /*Successful completion of a command returning no data.*/
242 /*affected_rows function only returns
243 the number of affected rows of a command
246 pg_sock->affected_rows = affected_rows(pg_sock->result);
247 radlog(L_DBG, "rlm_sql_postgresql: query affected rows = %i", pg_sock->affected_rows);
252 case PGRES_TUPLES_OK:
253 /*Successful completion of a command returning data (such as a SELECT or SHOW).*/
255 pg_sock->cur_row = 0;
256 pg_sock->affected_rows = PQntuples(pg_sock->result);
257 numfields = PQnfields(pg_sock->result); /*Check row storing functions..*/
258 radlog(L_DBG, "rlm_sql_postgresql: query affected rows = %i , fields = %i", pg_sock->affected_rows, numfields);
263 case PGRES_BAD_RESPONSE:
264 /*The server's response was not understood.*/
265 radlog(L_DBG, "rlm_sql_postgresql: Bad Response From Server!!");
270 case PGRES_NONFATAL_ERROR:
271 /*A nonfatal error (a notice or warning) occurred. Possibly never returns*/
277 case PGRES_FATAL_ERROR:
278 /*A fatal error occurred.*/
280 errorcode = PQresultErrorField(pg_sock->result, PG_DIAG_SQLSTATE);
281 errormsg = PQresultErrorField(pg_sock->result, PG_DIAG_MESSAGE_PRIMARY);
282 radlog(L_DBG, "rlm_sql_postgresql: Error %s", errormsg);
283 return check_fatal_error(errorcode);
288 /* FIXME: An unhandled error occurred.*/
290 /* PGRES_EMPTY_QUERY PGRES_COPY_OUT PGRES_COPY_IN */
300 Note to self ... sql_store_result returns 0 anyway
301 after setting the sqlsocket->affected_rows..
302 sql_num_fields returns 0 at worst case which means the check below
303 has a really small chance to return false..
304 lets remove it then .. yuck!!
308 if ((sql_store_result(sqlsocket, config) == 0)
309 && (sql_num_fields(sqlsocket, config) >= 0))
319 /*************************************************************************
321 * Function: sql_select_query
323 * Purpose: Issue a select query to the database
325 *************************************************************************/
326 static int sql_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
327 return sql_query(sqlsocket, config, querystr);
331 /*************************************************************************
333 * Function: sql_destroy_socket
335 * Purpose: Free socket and private connection data
337 *************************************************************************/
338 static int sql_destroy_socket(SQLSOCK *sqlsocket, UNUSED SQL_CONFIG *config)
340 free(sqlsocket->conn);
341 sqlsocket->conn = NULL;
345 /*************************************************************************
347 * Function: sql_fetch_row
349 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
350 * with all the data for the query in 'sqlsocket->row'. Returns
351 * 0 on success, -1 on failure, SQL_DOWN if 'database is down'.
353 *************************************************************************/
354 static int sql_fetch_row(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
357 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
359 sqlsocket->row = NULL;
361 if (pg_sock->cur_row >= PQntuples(pg_sock->result))
364 free_result_row(pg_sock);
366 records = PQnfields(pg_sock->result);
367 pg_sock->num_fields = records;
369 if ((PQntuples(pg_sock->result) > 0) && (records > 0)) {
370 pg_sock->row = (char **)rad_malloc((records+1)*sizeof(char *));
371 memset(pg_sock->row, '\0', (records+1)*sizeof(char *));
373 for (i = 0; i < records; i++) {
374 len = PQgetlength(pg_sock->result, pg_sock->cur_row, i);
375 pg_sock->row[i] = (char *)rad_malloc(len+1);
376 memset(pg_sock->row[i], '\0', len+1);
377 strlcpy(pg_sock->row[i], PQgetvalue(pg_sock->result, pg_sock->cur_row,i),len + 1);
380 sqlsocket->row = pg_sock->row;
388 /*************************************************************************
390 * Function: sql_free_result
392 * Purpose: database specific free_result. Frees memory allocated
395 *************************************************************************/
396 static int sql_free_result(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
398 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
400 if (pg_sock->result) {
401 PQclear(pg_sock->result);
402 pg_sock->result = NULL;
405 free_result_row(pg_sock);
412 /*************************************************************************
414 * Function: sql_error
416 * Purpose: database specific error. Returns error associated with
419 *************************************************************************/
420 static char *sql_error(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
422 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
424 return PQerrorMessage(pg_sock->conn);
428 /*************************************************************************
430 * Function: sql_close
432 * Purpose: database specific close. Closes an open database
435 *************************************************************************/
436 static int sql_close(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
438 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
440 if (!pg_sock->conn) return 0;
442 /* PQfinish also frees the memory used by the PGconn structure */
443 PQfinish(pg_sock->conn);
444 pg_sock->conn = NULL;
450 /*************************************************************************
452 * Function: sql_finish_query
454 * Purpose: End the query, such as freeing memory
456 *************************************************************************/
457 static int sql_finish_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
459 return sql_free_result(sqlsocket, config);
464 /*************************************************************************
466 * Function: sql_finish_select_query
468 * Purpose: End the select query, such as freeing memory or result
470 *************************************************************************/
471 static int sql_finish_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
473 return sql_free_result(sqlsocket, config);
477 /*************************************************************************
479 * Function: sql_affected_rows
481 * Purpose: Return the number of rows affected by the last query.
483 *************************************************************************/
484 static int sql_affected_rows(SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config) {
485 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
487 return pg_sock->affected_rows;
491 static int NEVER_RETURNS
492 not_implemented(UNUSED SQLSOCK * sqlsocket, UNUSED SQL_CONFIG *config)
494 radlog(L_ERR, "sql_postgresql: calling unimplemented function");
499 /* Exported to rlm_sql */
500 rlm_sql_module_t rlm_sql_postgresql = {
501 "rlm_sql_postgresql",
506 not_implemented, /* sql_store_result */
507 not_implemented, /* sql_num_fields */
508 not_implemented, /* sql_num_rows */
510 not_implemented, /* sql_free_result */
514 sql_finish_select_query,