Rename SQL data types so they don't conflict with drivers
[freeradius.git] / src / modules / rlm_sql / sql.c
index f4893a3..ba35d14 100644 (file)
@@ -40,143 +40,74 @@ RCSID("$Id$")
 #endif
 
 
-/*
- * Connect to a server.  If error, set this socket's state to be
- * "sockunconnected" and set a grace period, during which we won't try
- * connecting again (to prevent unduly lagging the server and being
- * impolite to a DB server that may be having other issues).  If
- * successful in connecting, set state to sockconnected.
- * - chad
- */
-static int connect_single_socket(SQLSOCK *sqlsocket, SQL_INST *inst)
+static void *sql_conn_create(void *ctx)
 {
        int rcode;
-       radlog(L_DBG, "rlm_sql (%s): Attempting to connect %s #%d",
-              inst->config->xlat_name, inst->module->name, sqlsocket->id);
+       rlm_sql_t *inst = ctx;
+       rlm_sql_handle_t *handle;
+
+       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) {
-               radlog(L_DBG, "rlm_sql (%s): Connected new DB handle, #%d",
-                      inst->config->xlat_name, sqlsocket->id);
-               sqlsocket->state = sockconnected;
-               return(0);
+         exec_trigger(NULL, inst->cs, "modules.sql.open", FALSE);
+               return handle;
        }
 
-       /*
-        *  Error, or SQL_DOWN.
-        */
-       radlog(L_CONS | L_ERR, "rlm_sql (%s): Failed to connect DB handle #%d", inst->config->xlat_name, sqlsocket->id);
-       inst->connect_after = time(NULL) + inst->config->connect_failure_retry_delay;
-       sqlsocket->state = sockunconnected;
-       return(-1);
+       exec_trigger(NULL, inst->cs, "modules.sql.fail", TRUE);
+
+       free(handle);
+       return NULL;
 }
 
 
-/*************************************************************************
- *
- *     Function: sql_init_socketpool
- *
- *     Purpose: Connect to the sql server, if possible
- *
- *************************************************************************/
-int sql_init_socketpool(SQL_INST * inst)
+static int sql_conn_delete(void *ctx, void *connection)
 {
-       int i, rcode;
-       int success = 0;
-       SQLSOCK *sqlsocket;
-
-       inst->connect_after = 0;
-       inst->sqlpool = NULL;
+       rlm_sql_t *inst = ctx;
+       rlm_sql_handle_t *handle = connection;
 
-       for (i = 0; i < inst->config->num_sql_socks; i++) {
-               radlog(L_DBG, "rlm_sql (%s): starting %d",
-                      inst->config->xlat_name, i);
+       exec_trigger(NULL, inst->cs, "modules.sql.close", FALSE);
 
-               sqlsocket = rad_malloc(sizeof(*sqlsocket));
-               if (sqlsocket == NULL) {
-                       return -1;
-               }
-               memset(sqlsocket, 0, sizeof(*sqlsocket));
-               sqlsocket->conn = NULL;
-               sqlsocket->id = i;
-               sqlsocket->state = sockunconnected;
-
-#ifdef HAVE_PTHREAD_H
-               rcode = pthread_mutex_init(&sqlsocket->mutex,NULL);
-               if (rcode != 0) {
-                       radlog(L_ERR, "rlm_sql: Failed to init lock: %s",
-                              strerror(errno));
-                       return 0;
-               }
-#endif
-
-               if (time(NULL) > inst->connect_after) {
-                       /*
-                        *      This sets the sqlsocket->state, and
-                        *      possibly also inst->connect_after
-                        */
-                       if (connect_single_socket(sqlsocket, inst) == 0) {
-                               success = 1;
-                       }
-               }
-
-               /* Add this socket to the list of sockets */
-               sqlsocket->next = inst->sqlpool;
-               inst->sqlpool = sqlsocket;
+       if (handle->conn) {
+               (inst->module->sql_close)(handle, inst->config);
        }
-       inst->last_used = NULL;
-
-       if (!success) {
-               radlog(L_DBG, "rlm_sql (%s): Failed to connect to any SQL server.",
-                      inst->config->xlat_name);
+       if (inst->module->sql_destroy_socket) {
+               (inst->module->sql_destroy_socket)(handle, inst->config);
        }
+       free(handle);
 
-       return 1;
+       return 0;
 }
 
+
 /*************************************************************************
  *
- *     Function: sql_poolfree
+ *     Function: sql_init_socketpool
  *
- *     Purpose: Clean up and free sql pool
+ *     Purpose: Connect to the sql server, if possible
  *
  *************************************************************************/
-void sql_poolfree(SQL_INST * inst)
+int sql_init_socketpool(rlm_sql_t * inst)
 {
-       SQLSOCK *cur;
-       SQLSOCK *next;
+       inst->pool = fr_connection_pool_init(inst->cs, inst,
+                                            sql_conn_create,
+                                            NULL,
+                                            sql_conn_delete);
+       if (!inst->pool) return -1;
 
-       for (cur = inst->sqlpool; cur; cur = next) {
-               next = cur->next;
-               sql_close_socket(inst, cur);
-       }
-
-       inst->sqlpool = NULL;
+       return 1;
 }
 
-
 /*************************************************************************
  *
- *     Function: sql_close_socket
+ *     Function: sql_poolfree
  *
- *     Purpose: Close and free a sql sqlsocket
+ *     Purpose: Clean up and free sql pool
  *
  *************************************************************************/
-int sql_close_socket(SQL_INST *inst, SQLSOCK * sqlsocket)
+void sql_poolfree(rlm_sql_t * inst)
 {
-       radlog(L_DBG, "rlm_sql (%s): Closing sqlsocket %d",
-              inst->config->xlat_name, sqlsocket->id);
-       if (sqlsocket->state == sockconnected) {
-               (inst->module->sql_close)(sqlsocket, inst->config);
-       }
-       if (inst->module->sql_destroy_socket) {
-               (inst->module->sql_destroy_socket)(sqlsocket, inst->config);
-       }
-#ifdef HAVE_PTHREAD_H
-       pthread_mutex_destroy(&sqlsocket->mutex);
-#endif
-       free(sqlsocket);
-       return 1;
+       fr_connection_pool_delete(inst->pool);
 }
 
 
@@ -184,123 +115,24 @@ int sql_close_socket(SQL_INST *inst, SQLSOCK * sqlsocket)
  *
  *     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)
 {
-       SQLSOCK *cur, *start;
-       int tried_to_connect = 0;
-       int unconnected = 0;
-
-       /*
-        *      Start at the last place we left off.
-        */
-       start = inst->last_used;
-       if (!start) start = inst->sqlpool;
-
-       cur = start;
-
-       while (cur) {
-#ifdef HAVE_PTHREAD_H
-               /*
-                *      If this socket is in use by another thread,
-                *      skip it, and try another socket.
-                *
-                *      If it isn't used, then grab it ourselves.
-                */
-               if (pthread_mutex_trylock(&cur->mutex) != 0) {
-                       goto next;
-               } /* else we now have the lock */
-#endif
-
-               /*
-                *      If we happen upon an unconnected socket, and
-                *      this instance's grace period on
-                *      (re)connecting has expired, then try to
-                *      connect it.  This should be really rare.
-                */
-               if ((cur->state == sockunconnected) && (time(NULL) > inst->connect_after)) {
-                       radlog(L_INFO, "rlm_sql (%s): Trying to (re)connect unconnected handle %d..", inst->config->xlat_name, cur->id);
-                       tried_to_connect++;
-                       connect_single_socket(cur, inst);
-               }
-
-               /* if we still aren't connected, ignore this handle */
-               if (cur->state == sockunconnected) {
-                       radlog(L_DBG, "rlm_sql (%s): Ignoring unconnected handle %d..", inst->config->xlat_name, cur->id);
-                       unconnected++;
-#ifdef HAVE_PTHREAD_H
-                       pthread_mutex_unlock(&cur->mutex);
-#endif
-                       goto next;
-               }
-
-               /* should be connected, grab it */
-               radlog(L_DBG, "rlm_sql (%s): Reserving sql socket id: %d", inst->config->xlat_name, cur->id);
-
-               if (unconnected != 0 || tried_to_connect != 0) {
-                       radlog(L_INFO, "rlm_sql (%s): got socket %d after skipping %d unconnected handles, tried to reconnect %d though", inst->config->xlat_name, cur->id, unconnected, tried_to_connect);
-               }
-
-               /*
-                *      The socket is returned in the locked
-                *      state.
-                *
-                *      We also remember where we left off,
-                *      so that the next search can start from
-                *      here.
-                *
-                *      Note that multiple threads MAY over-write
-                *      the 'inst->last_used' variable.  This is OK,
-                *      as it's a pointer only used for reading.
-                */
-               inst->last_used = cur->next;
-               return cur;
-
-               /* move along the list */
-       next:
-               cur = cur->next;
-
-               /*
-                *      Because we didnt start at the start, once we
-                *      hit the end of the linklist, we should go
-                *      back to the beginning and work toward the
-                *      middle!
-                */
-               if (!cur) {
-                       cur = inst->sqlpool;
-               }
-
-               /*
-                *      If we're at the socket we started
-                */
-               if (cur == start) {
-                       break;
-               }
-       }
-
-       /* We get here if every DB handle is unconnected and unconnectABLE */
-       radlog(L_INFO, "rlm_sql (%s): There are no DB handles to use! skipped %d, tried to connect %d", inst->config->xlat_name, unconnected, tried_to_connect);
-       return NULL;
+       return fr_connection_get(inst->pool);
 }
 
 /*************************************************************************
  *
  *     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)
 {
-#ifdef HAVE_PTHREAD_H
-       pthread_mutex_unlock(&sqlsocket->mutex);
-#endif
-
-       radlog(L_DBG, "rlm_sql (%s): Released sql socket id: %d",
-              inst->config->xlat_name, sqlsocket->id);
-
+       fr_connection_release(inst->pool, handle);
        return 0;
 }
 
@@ -312,10 +144,10 @@ 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;
-       char *ptr, *value;
+       VALUE_PAIR *vp;
+       const char *ptr, *value;
        char buf[MAX_STRING_LEN];
        char do_xlat = 0;
        FR_TOKEN token, operator = T_EOL;
@@ -334,8 +166,13 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
        if (row[4] != NULL && row[4][0] != '\0') {
                ptr = row[4];
                operator = gettoken(&ptr, buf, sizeof(buf));
-       }
-       if (operator <= T_EOL) {
+               if ((operator < T_OP_ADD) ||
+                   (operator > T_OP_CMP_EQ)) {
+                       radlog(L_ERR, "rlm_sql: Invalid operator \"%s\" for attribute %s", row[4], row[2]);
+                       return -1;
+               }
+
+       } else {
                /*
                 *  Complain about empty or invalid 'op' field
                 */
@@ -386,21 +223,26 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
        /*
         *      Create the pair
         */
-       pair = pairmake(row[2], value, operator);
-       if (pair == NULL) {
-               radlog(L_ERR, "rlm_sql: Failed to create the pair: %s", librad_errstr);
+       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) {
-               pair->flags.do_xlat = 1;
-               strlcpy(pair->vp_strvalue, buf, sizeof(pair->vp_strvalue));
-               pair->length = 0;
+               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;
 }
 
@@ -412,36 +254,24 @@ 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->conn) {
-               ret = (inst->module->sql_fetch_row)(sqlsocket, inst->config);
-       } else {
-               ret = SQL_DOWN;
+       if (!*handle || !(*handle)->conn) {
+               return -1;
        }
-
-       if (ret == SQL_DOWN) {
-               /* close the socket that failed, but only if it was open */
-               if (sqlsocket->conn) {
-                       (inst->module->sql_close)(sqlsocket, inst->config);
-               }
-
-               /* reconnect the socket */
-               if (connect_single_socket(sqlsocket, inst) < 0) {
-                       radlog(L_ERR, "rlm_sql (%s): reconnect failed, database down?", inst->config->xlat_name);
-                       return -1;
-               }
-
-               /* retry the query on the newly connected socket */
-               ret = (inst->module->sql_fetch_row)(sqlsocket, inst->config);
-
-               if (ret) {
-                       radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
-                              inst->config->xlat_name);
-                       return -1;
-               }
+       
+       /* 
+        * We can't implement reconnect logic here, because the caller may require
+        * the original connection to free up queries or result sets associated with
+        * that connection.
+        */
+       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)(*handle, inst->config));
        }
 
        return ret;
@@ -454,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;
 
@@ -465,31 +295,37 @@ int rlm_sql_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
                return -1;
        }
 
-       ret = (inst->module->sql_query)(sqlsocket, inst->config, query);
-
-       if (ret == SQL_DOWN) {
-               /* close the socket that failed */
-               if (sqlsocket->state == sockconnected) {
-                       (inst->module->sql_close)(sqlsocket, inst->config);
-               }
+       if (!*handle || !(*handle)->conn) {
+               ret = -1;
+               goto sql_down;
+       }
+       
+       while (1) {
+               DEBUG("rlm_sql (%s): Executing query: '%s'",
+                     inst->config->xlat_name, query);
 
-               /* reconnect the socket */
-               if (connect_single_socket(sqlsocket, inst) < 0) {
-                       radlog(L_ERR, "rlm_sql (%s): reconnect failed, database down?", inst->config->xlat_name);
-                       return -1;
+               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.
+                */
+               if (ret == SQL_DOWN) {
+                       sql_down:
+                       *handle = fr_connection_reconnect(inst->pool, *handle);
+                       if (!*handle) return SQL_DOWN;
+                       
+                       continue;
                }
-
-               /* retry the query on the newly connected socket */
-               ret = (inst->module->sql_query)(sqlsocket, inst->config, query);
-
-               if (ret) {
-                       radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
-                              inst->config->xlat_name);
-                       return -1;
+               
+               if (ret < 0) {
+                       radlog(L_ERR,
+                                  "rlm_sql (%s): Database query error: '%s'",
+                                  inst->config->xlat_name,
+                                  (inst->module->sql_error)(*handle, inst->config));
                }
+               
+               return ret;
        }
-
-       return ret;
 }
 
 /*************************************************************************
@@ -499,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;
 
@@ -510,31 +346,37 @@ int rlm_sql_select_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
                return -1;
        }
 
-       ret = (inst->module->sql_select_query)(sqlsocket, inst->config, query);
-
-       if (ret == SQL_DOWN) {
-               /* close the socket that failed */
-               if (sqlsocket->state == sockconnected) {
-                       (inst->module->sql_close)(sqlsocket, inst->config);
-               }
+       if (!*handle || !(*handle)->conn) {
+               ret = -1;
+               goto sql_down;
+       }
+       
+       while (1) {
+               DEBUG("rlm_sql (%s): Executing query: '%s'",
+                     inst->config->xlat_name, query);
 
-               /* reconnect the socket */
-               if (connect_single_socket(sqlsocket, inst) < 0) {
-                       radlog(L_ERR, "rlm_sql (%s): reconnect failed, database down?", inst->config->xlat_name);
-                       return -1;
+               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.
+                */
+               if (ret == SQL_DOWN) {
+                       sql_down:
+                       *handle = fr_connection_reconnect(inst->pool, *handle);
+                       if (!*handle) return SQL_DOWN;
+                       
+                       continue;
                }
-
-               /* retry the query on the newly connected socket */
-               ret = (inst->module->sql_select_query)(sqlsocket, inst->config, query);
-
-               if (ret) {
-                       radlog(L_ERR, "rlm_sql (%s): failed after re-connect",
-                              inst->config->xlat_name);
-                       return -1;
+               
+               if (ret < 0) {
+                       radlog(L_ERR,
+                                  "rlm_sql (%s): Database query error '%s'",
+                                  inst->config->xlat_name,
+                                  (inst->module->sql_error)(*handle, inst->config));
                }
+               
+               return ret;
        }
-
-       return ret;
 }
 
 
@@ -545,63 +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 there's no query, return an error.
-        */
-       if (!query || !*query) {
+       if (rlm_sql_select_query(handle, inst, query))
                return -1;
-       }
 
-       if (rlm_sql_select_query(sqlsocket, inst, query)) {
-               radlog(L_ERR, "rlm_sql_getvpdata: database query error");
-               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 */
 }