Rename SQL data types so they don't conflict with drivers
[freeradius.git] / src / modules / rlm_sql / sql.c
index 63282fd..ba35d14 100644 (file)
@@ -43,39 +43,38 @@ RCSID("$Id$")
 static void *sql_conn_create(void *ctx)
 {
        int rcode;
-       SQL_INST *inst = ctx;
-       SQLSOCK *sqlsocket;
+       rlm_sql_t *inst = ctx;
+       rlm_sql_handle_t *handle;
 
-       sqlsocket = rad_malloc(sizeof(*sqlsocket));
-       memset(sqlsocket, 0, sizeof(*sqlsocket));
+       handle = rad_calloc(sizeof(*handle));
 
-       rcode = (inst->module->sql_init_socket)(sqlsocket, inst->config);
+       rcode = (inst->module->sql_init_socket)(handle, inst->config);
        if (rcode == 0) {
          exec_trigger(NULL, inst->cs, "modules.sql.open", FALSE);
-               return sqlsocket;
+               return handle;
        }
 
        exec_trigger(NULL, inst->cs, "modules.sql.fail", TRUE);
 
-       free(sqlsocket);
+       free(handle);
        return NULL;
 }
 
 
 static int sql_conn_delete(void *ctx, void *connection)
 {
-       SQL_INST *inst = ctx;
-       SQLSOCK *sqlsocket = connection;
+       rlm_sql_t *inst = ctx;
+       rlm_sql_handle_t *handle = connection;
 
        exec_trigger(NULL, inst->cs, "modules.sql.close", FALSE);
 
-       if (sqlsocket->conn) {
-               (inst->module->sql_close)(sqlsocket, inst->config);
+       if (handle->conn) {
+               (inst->module->sql_close)(handle, inst->config);
        }
        if (inst->module->sql_destroy_socket) {
-               (inst->module->sql_destroy_socket)(sqlsocket, inst->config);
+               (inst->module->sql_destroy_socket)(handle, inst->config);
        }
-       free(sqlsocket);
+       free(handle);
 
        return 0;
 }
@@ -88,7 +87,7 @@ static int sql_conn_delete(void *ctx, void *connection)
  *     Purpose: Connect to the sql server, if possible
  *
  *************************************************************************/
-int sql_init_socketpool(SQL_INST * inst)
+int sql_init_socketpool(rlm_sql_t * inst)
 {
        inst->pool = fr_connection_pool_init(inst->cs, inst,
                                             sql_conn_create,
@@ -106,7 +105,7 @@ int sql_init_socketpool(SQL_INST * inst)
  *     Purpose: Clean up and free sql pool
  *
  *************************************************************************/
-void sql_poolfree(SQL_INST * inst)
+void sql_poolfree(rlm_sql_t * inst)
 {
        fr_connection_pool_delete(inst->pool);
 }
@@ -116,10 +115,10 @@ void sql_poolfree(SQL_INST * inst)
  *
  *     Function: sql_get_socket
  *
- *     Purpose: Return a SQL sqlsocket from the connection pool
+ *     Purpose: Return a SQL handle from the connection pool
  *
  *************************************************************************/
-SQLSOCK * sql_get_socket(SQL_INST * inst)
+rlm_sql_handle_t * sql_get_socket(rlm_sql_t * inst)
 {
        return fr_connection_get(inst->pool);
 }
@@ -128,12 +127,12 @@ SQLSOCK * sql_get_socket(SQL_INST * inst)
  *
  *     Function: sql_release_socket
  *
- *     Purpose: Frees a SQL sqlsocket back to the connection pool
+ *     Purpose: Frees a SQL handle back to the connection pool
  *
  *************************************************************************/
-int sql_release_socket(SQL_INST * inst, SQLSOCK * sqlsocket)
+int sql_release_socket(rlm_sql_t * inst, rlm_sql_handle_t * handle)
 {
-       fr_connection_release(inst->pool, sqlsocket);
+       fr_connection_release(inst->pool, handle);
        return 0;
 }
 
@@ -145,9 +144,9 @@ int sql_release_socket(SQL_INST * inst, SQLSOCK * sqlsocket)
  *     Purpose: Read entries from the database and fill VALUE_PAIR structures
  *
  *************************************************************************/
-int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
+int sql_userparse(VALUE_PAIR **head, rlm_sql_row_t row)
 {
-       VALUE_PAIR *pair;
+       VALUE_PAIR *vp;
        const char *ptr, *value;
        char buf[MAX_STRING_LEN];
        char do_xlat = 0;
@@ -224,20 +223,26 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
        /*
         *      Create the pair
         */
-       if (do_xlat) {
-               pair = pairmake_xlat(row[2], value, operator);
-       } else {
-               pair = pairmake(row[2], value, operator);
-       }
-       if (pair == NULL) {
-               radlog(L_ERR, "rlm_sql: Failed to create the pair: %s", fr_strerror());
+       vp = pairmake(row[2], NULL, operator);
+       if (!vp) {
+               radlog(L_ERR, "rlm_sql: Failed to create the pair: %s",
+                      fr_strerror());
                return -1;
        }
+       
+       if (do_xlat) {
+               if (pairmark_xlat(vp, value) < 0) {
+                       radlog(L_ERR, "rlm_sql: Error marking pair for xlat");
+                       
+                       pairbasicfree(vp);
+                       return -1;
+               }
+       }
 
        /*
         *      Add the pair into the packet
         */
-       pairadd(first_pair, pair);
+       pairadd(head, vp);
        return 0;
 }
 
@@ -249,11 +254,11 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
  *     Purpose: call the module's sql_fetch_row and implement re-connect
  *
  *************************************************************************/
-int rlm_sql_fetch_row(SQLSOCK **sqlsocket, SQL_INST *inst)
+int rlm_sql_fetch_row(rlm_sql_handle_t **handle, rlm_sql_t *inst)
 {
        int ret;
 
-       if (!*sqlsocket || !(*sqlsocket)->conn) {
+       if (!*handle || !(*handle)->conn) {
                return -1;
        }
        
@@ -262,11 +267,11 @@ int rlm_sql_fetch_row(SQLSOCK **sqlsocket, SQL_INST *inst)
         * the original connection to free up queries or result sets associated with
         * that connection.
         */
-       ret = (inst->module->sql_fetch_row)(*sqlsocket, inst->config);
+       ret = (inst->module->sql_fetch_row)(*handle, inst->config);
        
        if (ret < 0) {
-               radlog(L_ERR, "rlm_sql (%s): error fetching row: %s", inst->config->xlat_name,
-                          (inst->module->sql_error)(*sqlsocket, inst->config));
+               radlog(L_ERR, "rlm_sql (%s): Error fetching row: %s", inst->config->xlat_name,
+                          (inst->module->sql_error)(*handle, inst->config));
        }
 
        return ret;
@@ -279,7 +284,7 @@ int rlm_sql_fetch_row(SQLSOCK **sqlsocket, SQL_INST *inst)
  *     Purpose: call the module's sql_query and implement re-connect
  *
  *************************************************************************/
-int rlm_sql_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
+int rlm_sql_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char *query)
 {
        int ret;
 
@@ -290,30 +295,33 @@ int rlm_sql_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
                return -1;
        }
 
-       if (!*sqlsocket || !(*sqlsocket)->conn) {
+       if (!*handle || !(*handle)->conn) {
                ret = -1;
                goto sql_down;
        }
        
        while (1) {
-               ret = (inst->module->sql_query)(*sqlsocket, inst->config, query);
+               DEBUG("rlm_sql (%s): Executing query: '%s'",
+                     inst->config->xlat_name, query);
+
+               ret = (inst->module->sql_query)(*handle, inst->config, query);
                /*
-                * Run through all available sockets until we exhaust all existing sockets
-                * in the pool and fail to establish a *new* connection.
+                * Run through all available sockets until we exhaust all existing
+                * sockets in the pool and fail to establish a *new* connection.
                 */
                if (ret == SQL_DOWN) {
                        sql_down:
-                       *sqlsocket = fr_connection_reconnect(inst->pool, *sqlsocket);
-                       if (!*sqlsocket) return -1;
+                       *handle = fr_connection_reconnect(inst->pool, *handle);
+                       if (!*handle) return SQL_DOWN;
                        
                        continue;
                }
                
                if (ret < 0) {
-                       radlog(L_ERR, "rlm_sql (%s): database query error, %s: %s",
+                       radlog(L_ERR,
+                                  "rlm_sql (%s): Database query error: '%s'",
                                   inst->config->xlat_name,
-                                  query,
-                                  (inst->module->sql_error)(*sqlsocket, inst->config));
+                                  (inst->module->sql_error)(*handle, inst->config));
                }
                
                return ret;
@@ -327,7 +335,7 @@ int rlm_sql_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
  *     Purpose: call the module's sql_select_query and implement re-connect
  *
  *************************************************************************/
-int rlm_sql_select_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
+int rlm_sql_select_query(rlm_sql_handle_t **handle, rlm_sql_t *inst, char *query)
 {
        int ret;
 
@@ -338,30 +346,33 @@ int rlm_sql_select_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
                return -1;
        }
 
-       if (!*sqlsocket || !(*sqlsocket)->conn) {
+       if (!*handle || !(*handle)->conn) {
                ret = -1;
                goto sql_down;
        }
        
        while (1) {
-               ret = (inst->module->sql_select_query)(*sqlsocket, inst->config, query);
+               DEBUG("rlm_sql (%s): Executing query: '%s'",
+                     inst->config->xlat_name, query);
+
+               ret = (inst->module->sql_select_query)(*handle, inst->config, query);
                /*
-                * Run through all available sockets until we exhaust all existing sockets
-                * in the pool and fail to establish a *new* connection.
+                * Run through all available sockets until we exhaust all existing
+                * sockets in the pool and fail to establish a *new* connection.
                 */
                if (ret == SQL_DOWN) {
                        sql_down:
-                       *sqlsocket = fr_connection_reconnect(inst->pool, *sqlsocket);
-                       if (!*sqlsocket) return -1;
+                       *handle = fr_connection_reconnect(inst->pool, *handle);
+                       if (!*handle) return SQL_DOWN;
                        
                        continue;
                }
                
                if (ret < 0) {
-                       radlog(L_ERR, "rlm_sql (%s): database query error, %s: %s",
+                       radlog(L_ERR,
+                                  "rlm_sql (%s): Database query error '%s'",
                                   inst->config->xlat_name,
-                                  query,
-                                  (inst->module->sql_error)(*sqlsocket, inst->config));
+                                  (inst->module->sql_error)(*handle, inst->config));
                }
                
                return ret;
@@ -376,56 +387,66 @@ int rlm_sql_select_query(SQLSOCK **sqlsocket, SQL_INST *inst, char *query)
  *     Purpose: Get any group check or reply pairs
  *
  *************************************************************************/
-int sql_getvpdata(SQL_INST * inst, SQLSOCK **sqlsocket, VALUE_PAIR **pair, char *query)
+int sql_getvpdata(rlm_sql_t * inst, rlm_sql_handle_t **handle, VALUE_PAIR **pair, char *query)
 {
-       SQL_ROW row;
+       rlm_sql_row_t row;
        int     rows = 0;
 
-       if (rlm_sql_select_query(sqlsocket, inst, query)) {
-               radlog(L_ERR, "rlm_sql_getvpdata: database query error");
+       if (rlm_sql_select_query(handle, inst, query))
                return -1;
-       }
-       while (rlm_sql_fetch_row(sqlsocket, inst) == 0) {
-               row = (*sqlsocket)->row;
+
+       while (rlm_sql_fetch_row(handle, inst) == 0) {
+               row = (*handle)->row;
                if (!row)
                        break;
                if (sql_userparse(pair, row) != 0) {
-                       radlog(L_ERR | L_CONS, "rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
-                       (inst->module->sql_finish_select_query)(*sqlsocket, inst->config);
+                       radlog(L_ERR, "rlm_sql (%s): Error getting data from database", inst->config->xlat_name);
+                       
+                       (inst->module->sql_finish_select_query)(*handle, inst->config);
+                       
                        return -1;
                }
                rows++;
        }
-       (inst->module->sql_finish_select_query)(*sqlsocket, inst->config);
+       (inst->module->sql_finish_select_query)(*handle, inst->config);
 
        return rows;
 }
 
-void query_log(REQUEST *request, SQL_INST *inst, char *querystr)
+/*
+ *     Log the query to a file.
+ */
+void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
+                      sql_acct_section_t *section, char *query)
 {
-       FILE   *sqlfile = NULL;
+       int fd;
+       const char *filename = NULL;
+       char buffer[8192];
 
-       if (inst->config->sqltrace) {
-               char buffer[8192];
+       if (section) filename = section->logfile;
 
-               if (!radius_xlat(buffer, sizeof(buffer),
-                                inst->config->tracefile, request, NULL)) {
-                 radlog(L_ERR, "rlm_sql (%s): xlat failed.",
-                        inst->config->xlat_name);
-                 return;
-               }
+       if (!filename) filename = inst->config->logfile;
 
-               if ((sqlfile = fopen(buffer, "a")) == (FILE *) NULL) {
-                       radlog(L_ERR, "rlm_sql (%s): Couldn't open file %s",
-                              inst->config->xlat_name,
-                              buffer);
-               } else {
-                       int fd = fileno(sqlfile);
-
-                       rad_lockfd(fd, MAX_QUERY_LEN);
-                       fputs(querystr, sqlfile);
-                       fputs(";\n", sqlfile);
-                       fclose(sqlfile); /* and release the lock */
-               }
+       if (!filename) return;
+
+       if (!radius_xlat(buffer, sizeof(buffer), filename, request, NULL, NULL)) {
+               radlog(L_ERR, "rlm_sql (%s): xlat failed.",
+                      inst->config->xlat_name);
+               return;
+       }
+
+       fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0666);
+       if (fd < 0) {
+               radlog(L_ERR, "rlm_sql (%s): Couldn't open logfile '%s': %s",
+                      inst->config->xlat_name, buffer, strerror(errno));
+               return;
+       }
+
+       if ((rad_lockfd(fd, MAX_QUERY_LEN) < 0) ||
+           (write(fd, query, strlen(query)) < 0) ||
+           (write(fd, ";\n", 2) < 0)) {
+               radlog(L_ERR, "rlm_sql (%s): Failed writing to logfile '%s': %s",
+                      inst->config->xlat_name, buffer, strerror(errno));
        }
+       close(fd);              /* and release the lock */
 }