Debug: Entry args dump for cfg_request_identity()
[tac_plus.git] / db.c
diff --git a/db.c b/db.c
index 9cbbbe1..b5c156c 100644 (file)
--- a/db.c
+++ b/db.c
@@ -1,7 +1,7 @@
 /*
      Verify that this user/password is valid per a database.
      Return 1 if verified, 0 otherwise.
-     
+
      Format of connection string (look like internet URL):
 
        db://user:password@hostname/table?name&passwd
@@ -22,7 +22,7 @@
                  'default authentication = db <string>'
      28-nov-1998 Added code for NULL database %)
      14-dec-1999 Add code for MySQL and also more check
-     
+
      FUTURE:
      Make *_db_verify() the functions is reenterable
      More security for connection to database
      Separate debug logging
      Perfomance testing on 10000 records in Oracle database
      (in guide sayd about 3 auth/sec on Ultra 2 - hmm)
-     
+
      -------------------------------------------------------
      fil@artelecom.ru                   http://twister.pp.ru
+
  ****************************************************************************
                                    PART II
 
    I am added some extra extension. Like MySQL and PostgreSQL database support
-   And change most of lines for use dynamic memory allocation. db_accounting 
+   And change most of lines for use dynamic memory allocation. db_accounting
    added by me.
-  
+
    devrim(devrim@gazi.edu.tr)
 */
 
-#if defined(DB)
-#include <stdio.h>
+
 #include "tac_plus.h"
+
+#ifdef DB
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "db.h"
+#include "report.h"
+#include "do_acct.h"
+#include "main.h"
+#include "do_author.h"                 /* for "struct identity" */
+#include "utils.h"
+
+
+#ifdef DB_MYSQL
+#include "db_mysql.h"
+#endif
+#ifdef DB_NULL
+#include "db_null.h"
+#endif
+#ifdef DB_PGSQL
+#include "db_pgsql.h"
+#endif
+
+
+static int check_db_type TAC_ARGS((char *db_type));
+
+
 /* The databases  recognized by this function */
 #define DEFINED_DB {"null","mysql","pgsql"}
 
-char *find_attr_value(); 
+int db_verify TAC_ARGS((const char *user, const char *users_passwd, const char *str_conn));
 
 int
 db_verify(user, users_passwd, str_conn)
-char *user, *users_passwd;      /* Username and gived password   */
-char *str_conn;                 /* String connection to database */
+const char *user;                      /* username ... */
+const char *users_passwd;              /* ... and given password */
+const char *str_conn;                  /* string connection to database */
 {
     char *buffer;
     char *db_pref, *db_user, *db_password;
@@ -65,11 +94,7 @@ char *str_conn;                 /* String connection to database */
     if (debug & DEBUG_PASSWD_FLAG)
        report(LOG_DEBUG, "verify %s by database at %s", user, str_conn);
 
-    buffer = db_pref = (char *)malloc( strlen(str_conn) + 1 );
-    if( buffer == NULL ){
-       report(LOG_DEBUG, "Error allocation memory");
-        return(0);
-    }
+    buffer = db_pref = (char *) tac_malloc( strlen(str_conn) + 1 );
 
     strcpy( buffer, str_conn );
 
@@ -80,10 +105,10 @@ char *str_conn;                 /* String connection to database */
            free(buffer);
            return(0);
     }
-    *db_user = '\0'; 
+    *db_user = '\0';
 
        /* For recognize db authentication database */
-    
+
     if (check_db_type(db_pref)) {
        report(LOG_DEBUG, "%s DB authentication scheme didn't recognize by tac_plus",db_pref);
        free(buffer);
@@ -101,7 +126,7 @@ char *str_conn;                 /* String connection to database */
     }
     *db_password = '\0';
     db_password++;
-    
+
     db_hostname = (char *)strstr( db_password, "@" );
     if( db_hostname == NULL ){
        if (debug & DEBUG_PASSWD_FLAG)
@@ -111,27 +136,27 @@ char *str_conn;                 /* String connection to database */
     }
     *db_hostname = '\0';
     db_hostname++;
-    
+
     db_name = (char *)strstr( db_hostname, "/" );
     if( db_name == NULL ){
         if (debug & DEBUG_PASSWD_FLAG)
            report(LOG_DEBUG, "Error parse db_name");
        free(buffer);
         return(0);
-    } 
+    }
     *db_name = '\0';
     db_name++;
-    
+
     db_table = (char *)strstr( db_name, "/" );
     if( db_table == NULL ){
         if (debug & DEBUG_PASSWD_FLAG)
            report(LOG_DEBUG, "Error parse db_table");
        free(buffer);
         return(0);
-    } 
+    }
     *db_table = '\0';
     db_table++;
-    
+
     dbfield_name = (char *)strstr( db_table, "?" );
     if( dbfield_name == NULL){
        if (debug & DEBUG_PASSWD_FLAG)
@@ -152,7 +177,7 @@ char *str_conn;                 /* String connection to database */
     }
     *dbfield_passwd = '\0';
     dbfield_passwd++;
-    
+
 
     /* Parse database connection string */
        if (debug & DEBUG_PASSWD_FLAG)
@@ -216,6 +241,8 @@ char *str_conn;                 /* String connection to database */
 }
 
 
+int db_acct TAC_ARGS((struct acct_rec *rec));
+
 /* Db accounting routine */
 int
 db_acct(rec)
@@ -227,12 +254,7 @@ struct acct_rec *rec;
     char *a_username,*s_name,*c_name,*elapsed_time,*bytes_in,*bytes_out;
     int ret;
 
-    buffer = db_pref = (char *)malloc( strlen(session.db_acct) + 1 );
-       
-    if( buffer == NULL ){
-       report(LOG_DEBUG, "Error allocation memory");
-        return(0);
-    }
+    buffer = db_pref = (char *) tac_malloc( strlen(session.db_acct) + 1 );
 
     strcpy( buffer, session.db_acct);
 
@@ -243,10 +265,10 @@ struct acct_rec *rec;
         free(buffer);
        return(0);
     }
-    *db_user = '\0'; 
+    *db_user = '\0';
 
        /* For recognize db accouting database */
-    
+
     if( check_db_type(db_pref) ) {
        report(LOG_DEBUG, "%s DB accounting scheme didn't recognize by tac_plus",db_pref);
         free(buffer);
@@ -264,7 +286,7 @@ struct acct_rec *rec;
     }
     *db_password = '\0';
     db_password++;
-    
+
     db_hostname = (char *)strstr( db_password, "@" );
     if( db_hostname == NULL ){
        if (debug & DEBUG_PASSWD_FLAG)
@@ -274,52 +296,52 @@ struct acct_rec *rec;
     }
     *db_hostname = '\0';
     db_hostname++;
-    
+
     db_name = (char *)strstr( db_hostname, "/" );
     if( db_name == NULL ){
         if (debug & DEBUG_PASSWD_FLAG)
            report(LOG_DEBUG, "Error parse db_name");
         free(buffer);
         return(0);
-    } 
+    }
     *db_name = '\0';
     db_name++;
-    
+
     db_table = (char *)strstr( db_name, "/" );
     if( db_table == NULL ){
         if (debug & DEBUG_PASSWD_FLAG)
            report(LOG_DEBUG, "Error parse db_table");
         free(buffer);
         return(0);
-    } 
+    }
     *db_table = '\0';
     db_table++;
 
 /* Find some attributes  for accounting */
     a_username=rec->identity->username;
        if (a_username==NULL ) {
-               if (debug & DEBUG_PASSWD_FLAG) 
+               if (debug & DEBUG_PASSWD_FLAG)
                        report(LOG_DEBUG,"db_acct: Can't find username!");
                        free(buffer);
                        return(0);
        }
     s_name=rec->identity->NAS_name;
        if (s_name==NULL) {
-               if (debug & DEBUG_PASSWD_FLAG) 
+               if (debug & DEBUG_PASSWD_FLAG)
                        report(LOG_DEBUG,"db_acct: Can't find NAS name!");
                        free(buffer);
                        return(0);
        }
     c_name=find_attr_value("addr", rec->args, rec->num_args);
        if (c_name==NULL) {
-               if (debug & DEBUG_PASSWD_FLAG) 
+               if (debug & DEBUG_PASSWD_FLAG)
                        report(LOG_DEBUG,"db_acct: Can't find client adress!");
-       /* Can't find client adress so give NAC_address attribute value */ 
+       /* Can't find client adress so give NAC_address attribute value */
                c_name=rec->identity->NAC_address;
        }
     elapsed_time=find_attr_value("elapsed_time", rec->args, rec->num_args);
        if (elapsed_time==NULL) {
-               if (debug & DEBUG_PASSWD_FLAG) 
+               if (debug & DEBUG_PASSWD_FLAG)
                        report(LOG_DEBUG,"db_acct: Can't get elapsed time!");
                        free(buffer);
                        return(0);
@@ -384,8 +406,10 @@ struct acct_rec *rec;
 
 }
 
+static int check_db_type TAC_ARGS((char *db_type));
+
 /* For checking DB type */
-int 
+static int
 check_db_type(db_type)
 char *db_type;
 {
@@ -400,4 +424,9 @@ for (i=0; dbp[i] ; i++ ) {
 }
 return ret;
 }
+
+#else /* DB */
+
+TAC_SOURCEFILE_EMPTY
+
 #endif /* DB */