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 */
48 #include "sql_postgresql.h"
50 /* Internal function. Return true if the postgresql status value
51 * indicates successful completion of the query. Return false otherwise
54 status_is_ok(ExecStatusType status)
56 return status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK;
60 /* Internal function. Return the number of affected rows of the result
61 * as an int instead of the string that postgresql provides */
63 affected_rows(PGresult * result)
65 return atoi(PQcmdTuples(result));
68 /* Internal function. Free the row of the current result that's stored
69 * in the pg_sock struct. */
71 free_result_row(rlm_sql_postgres_sock * pg_sock)
74 if (pg_sock->row != NULL) {
75 for (i = pg_sock->num_fields-1; i >= 0; i--) {
76 if (pg_sock->row[i] != NULL) {
77 xfree(pg_sock->row[i]);
80 xfree((char*)pg_sock->row);
82 pg_sock->num_fields = 0;
86 /*************************************************************************
88 * Function: sql_create_socket
90 * Purpose: Establish connection to the db
92 *************************************************************************/
93 int sql_init_socket(SQLSOCK *sqlsocket, SQL_CONFIG *config) {
94 char connstring[2048];
96 rlm_sql_postgres_sock *pg_sock;
98 if (config->sql_server[0] != '\0') {
104 if (config->sql_port[0] != '\0') {
110 sqlsocket->conn = (rlm_sql_postgres_sock *)rad_malloc(sizeof(rlm_sql_postgres_sock));
112 pg_sock = sqlsocket->conn;
114 snprintf(connstring, sizeof(connstring),
115 "dbname=%s%s%s%s%s user=%s password=%s",
116 config->sql_db, host, config->sql_server,
117 port, config->sql_port,
118 config->sql_login, config->sql_password);
120 pg_sock->result=NULL;
121 pg_sock->conn=PQconnectdb(connstring);
123 if (PQstatus(sqlsocket->conn) == CONNECTION_BAD) {
124 radlog(L_ERR, "sql_postgresql: Couldn't connect socket to PostgreSQL server %s@%s:%s", config->sql_login, config->sql_server, config->sql_db);
125 radlog(L_ERR, "sql_postgresql: Postgresql error '%s'", PQerrorMessage(pg_sock->conn));
126 PQfinish(pg_sock->conn);
133 /*************************************************************************
135 * Function: sql_query
137 * Purpose: Issue a query to the database
139 *************************************************************************/
140 int sql_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
142 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
144 if (config->sqltrace)
145 radlog(L_DBG,"query:\n%s", querystr);
147 if (pg_sock->conn == NULL) {
148 radlog(L_ERR, "Socket not connected");
152 pg_sock->result = PQexec(pg_sock->conn, querystr);
153 if (!pg_sock->result)
155 radlog(L_ERR, "PostgreSQL Query failed Error: %s",
156 PQerrorMessage(pg_sock->conn));
159 ExecStatusType status = PQresultStatus(pg_sock->result);
161 radlog(L_DBG, "rlm_postgresql Status: %s", PQresStatus(status));
163 radlog(L_DBG, "sql_postgresql: affected rows = %s",
164 PQcmdTuples(pg_sock->result));
166 if (!status_is_ok(status))
169 if (strncasecmp("select", querystr, 6) != 0) {
170 /* store the number of affected rows because the sql module
171 * calls finish_query before it retrieves the number of affected
172 * rows from the driver */
173 pg_sock->affected_rows = affected_rows(pg_sock->result);
176 if ((sql_store_result(sqlsocket, config) == 0)
177 && (sql_num_fields(sqlsocket, config) >= 0))
186 /*************************************************************************
188 * Function: sql_select_query
190 * Purpose: Issue a select query to the database
192 *************************************************************************/
193 int sql_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config, char *querystr) {
194 sql_query(sqlsocket, config, querystr);
198 /*************************************************************************
200 * Function: sql_store_result
202 * Purpose: database specific store_result function. Returns a result
205 *************************************************************************/
206 int sql_store_result(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
207 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
209 pg_sock->cur_row = 0;
210 pg_sock->affected_rows = affected_rows(pg_sock->result);
215 /*************************************************************************
217 * Function: sql_num_fields
219 * Purpose: database specific num_fields function. Returns number
220 * of columns from query
222 *************************************************************************/
223 int sql_num_fields(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
226 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
228 if (!(num = PQnfields(pg_sock->result))) {
229 radlog(L_ERR, "PostgreSQL Error: Cannot get result");
230 radlog(L_ERR, "PostgreSQL error: %s", PQerrorMessage(pg_sock->conn));
236 /*************************************************************************
238 * Function: sql_fetch_row
240 * Purpose: database specific fetch_row. Returns a SQL_ROW struct
241 * with all the data for the query
243 *************************************************************************/
244 SQL_ROW sql_fetch_row(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
247 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
249 if (pg_sock->cur_row >= PQntuples(pg_sock->result))
252 free_result_row(pg_sock);
254 records = PQnfields(pg_sock->result);
255 pg_sock->num_fields = records;
257 if ((PQntuples(pg_sock->result) > 0) && (records > 0)) {
258 pg_sock->row = (char **)rad_malloc(records*sizeof(char *)+1);
259 memset(pg_sock->row, '\0', records*sizeof(char *)+1);
261 for (i = 0; i < records; i++) {
262 len = PQgetlength(pg_sock->result, pg_sock->cur_row, i);
263 pg_sock->row[i] = (char *)rad_malloc(len+1);
264 memset(pg_sock->row[i], '\0', len+1);
265 strncpy(pg_sock->row[i], PQgetvalue(pg_sock->result, pg_sock->cur_row,i),len);
276 /*************************************************************************
278 * Function: sql_free_result
280 * Purpose: database specific free_result. Frees memory allocated
283 *************************************************************************/
284 int sql_free_result(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
286 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
288 if (pg_sock->result) {
289 PQclear(pg_sock->result);
290 pg_sock->result = NULL;
292 free_result_row(pg_sock);
299 /*************************************************************************
301 * Function: sql_error
303 * Purpose: database specific error. Returns error associated with
306 *************************************************************************/
307 char *sql_error(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
309 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
311 return PQerrorMessage(pg_sock->conn);
315 /*************************************************************************
317 * Function: sql_close
319 * Purpose: database specific close. Closes an open database
322 *************************************************************************/
323 int sql_close(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
325 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
327 PQfinish(pg_sock->conn);
328 pg_sock->conn = NULL;
334 /*************************************************************************
336 * Function: sql_finish_query
338 * Purpose: End the query, such as freeing memory
340 *************************************************************************/
341 int sql_finish_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
343 return sql_free_result(sqlsocket, config);
348 /*************************************************************************
350 * Function: sql_finish_select_query
352 * Purpose: End the select query, such as freeing memory or result
354 *************************************************************************/
355 int sql_finish_select_query(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
357 return sql_free_result(sqlsocket, config);
361 /*************************************************************************
363 * Function: sql_affected_rows
365 * Purpose: Return the number of rows affected by the last query.
367 *************************************************************************/
368 int sql_affected_rows(SQLSOCK * sqlsocket, SQL_CONFIG *config) {
369 rlm_sql_postgres_sock *pg_sock = sqlsocket->conn;
371 return pg_sock->affected_rows;
376 not_implemented(SQLSOCK * sqlsocket, SQL_CONFIG *config)
378 radlog(L_ERR, "sql_postgresql: calling unimplemented function");
383 /* Exported to rlm_sql */
384 rlm_sql_module_t rlm_sql_postgresql = {
385 "rlm_sql_postgresql",
387 not_implemented, /* sql_destroy_socket*/
390 not_implemented, /* sql_store_result */
391 not_implemented, /* sql_num_fields */
392 not_implemented, /* sql_num_rows */
394 not_implemented, /* sql_free_result */
398 sql_finish_select_query,