Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / modules / rlm_sql / sql.c
index b05d86a..72c164b 100644 (file)
  *
  *   You should have received a copy of the GNU General Public License
  *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
- * Copyright 2001  The FreeRADIUS server project
+ * Copyright 2001,2006  The FreeRADIUS server project
  * Copyright 2000  Mike Machado <mike@innercite.com>
  * Copyright 2000  Alan DeKok <aland@ox.org>
  * Copyright 2001  Chad Miller <cmiller@surfsouth.com>
  */
 
-#include       "autoconf.h"
+#include <freeradius-devel/ident.h>
+RCSID("$Id$")
+
+#include       <freeradius-devel/radiusd.h>
 
-#include       <sys/types.h>
-#include       <sys/socket.h>
-#include       <sys/time.h>
 #include       <sys/file.h>
-#include       <string.h>
 #include       <sys/stat.h>
-#include       <netinet/in.h>
 
-#include       <stdio.h>
-#include       <stdlib.h>
-#include       <netdb.h>
 #include       <pwd.h>
-#include       <time.h>
 #include       <ctype.h>
-#include       <unistd.h>
 #include       <signal.h>
-#include       <errno.h>
 #include       <sys/wait.h>
 
-#include       "radiusd.h"
-#include       "conffile.h"
 #include       "rlm_sql.h"
 
 #ifdef HAVE_PTHREAD_H
-#include       <pthread.h>
 #endif
 
 
@@ -106,10 +95,11 @@ int sql_init_socketpool(SQL_INST * inst)
                radlog(L_DBG, "rlm_sql (%s): starting %d",
                       inst->config->xlat_name, i);
 
-               sqlsocket = rad_malloc(sizeof(SQLSOCK));
+               sqlsocket = rad_malloc(sizeof(*sqlsocket));
                if (sqlsocket == NULL) {
                        return -1;
                }
+               memset(sqlsocket, 0, sizeof(*sqlsocket));
                sqlsocket->conn = NULL;
                sqlsocket->id = i;
                sqlsocket->state = sockunconnected;
@@ -342,6 +332,22 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
        }
 
        /*
+        *      Verify the 'op' field
+        */
+       if (row[4] != NULL && row[4][0] != '\0') {
+               ptr = row[4];
+               operator = gettoken(&ptr, buf, sizeof(buf));
+       }
+       if (operator <= T_EOL) {
+               /*
+                *  Complain about empty or invalid 'op' field
+                */
+               operator = T_OP_CMP_EQ;
+               radlog(L_ERR, "rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
+               radlog(L_ERR, "rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
+       }
+
+       /*
         *      The 'Value' field may be empty or NULL
         */
        value = row[3];
@@ -353,7 +359,6 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
           ((row[3][0] == '\'') || (row[3][0] == '`') || (row[3][0] == '"')) &&
           (row[3][0] == row[3][strlen(row[3])-1])) {
 
-               value = row[3];
                token = gettoken(&value, buf, sizeof(buf));
                switch (token) {
                        /*
@@ -382,22 +387,6 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
        }
 
        /*
-        *      Verify the 'op' field
-        */
-       if (row[4] != NULL && row[4][0] != '\0') {
-               ptr = row[4];
-               operator = gettoken(&ptr, buf, sizeof(buf));
-       }
-       if (operator <= T_EOL) {
-               /*
-                *  Complain about empty or invalid 'op' field
-                */
-               operator = T_OP_CMP_EQ;
-               radlog(L_ERR, "rlm_sql: The 'op' field for attribute '%s = %s' is NULL, or non-existent.", row[2], row[3]);
-               radlog(L_ERR, "rlm_sql: You MUST FIX THIS if you want the configuration to behave as you expect.");
-       }
-
-       /*
         *      Create the pair
         */
        pair = pairmake(row[2], value, operator);
@@ -407,7 +396,7 @@ int sql_userparse(VALUE_PAIR ** first_pair, SQL_ROW row)
        }
        if (do_xlat) {
                pair->flags.do_xlat = 1;
-               strNcpy(pair->strvalue, buf, sizeof(pair->strvalue));
+               strlcpy(pair->vp_strvalue, buf, sizeof(pair->vp_strvalue));
                pair->length = 0;
        }
 
@@ -483,7 +472,9 @@ int rlm_sql_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
 
        if (ret == SQL_DOWN) {
                /* close the socket that failed */
-               (inst->module->sql_close)(sqlsocket, inst->config);
+               if (sqlsocket->state == sockconnected) {
+                       (inst->module->sql_close)(sqlsocket, inst->config);
+               }
 
                /* reconnect the socket */
                if (connect_single_socket(sqlsocket, inst) < 0) {
@@ -526,7 +517,9 @@ int rlm_sql_select_query(SQLSOCK *sqlsocket, SQL_INST *inst, char *query)
 
        if (ret == SQL_DOWN) {
                /* close the socket that failed */
-               (inst->module->sql_close)(sqlsocket, inst->config);
+               if (sqlsocket->state == sockconnected) {
+                       (inst->module->sql_close)(sqlsocket, inst->config);
+               }
 
                /* reconnect the socket */
                if (connect_single_socket(sqlsocket, inst) < 0) {