8aef6d4ab7ec48323e5ac97a1eb69a703dd2f95e
[tac_plus.git] / db_mysql.c
1 #if defined(DB_MYSQL) && defined(DB)
2
3 /*
4 Writen by Devrim SERAL(devrim@tef.gazi.edu.tr)
5 */
6
7 #include "tac_plus.h"
8 #include <stdio.h>
9 #include "mysql.h"
10 #define SQLCMDL 1024
11 #define AUTHSQL "SELECT %s FROM %s WHERE %s=\"%s\""
12 #define ACCTSQL "INSERT INTO %s (usern,s_name,c_name,elapsed_time,bytes_in,bytes_out,fin_t) VALUES (\"%s\",\"%s\",\"%s\",%s,%s,%s,NOW())"  
13
14 MYSQL mysqldb;
15 MYSQL_RES *res;
16 MYSQL_ROW row;
17 MYSQL_FIELD *table_field;
18
19 int mysql_db_verify(user, users_passwd, db_user, db_password,
20         db_hostname,db_name, db_table, dbfield_name, dbfield_passwd)
21
22
23 char *user, *users_passwd;      /* Username and gived password   */
24 char *db_user;                  /* db's parameters               */
25 char *db_password;
26 char *db_hostname;
27 char *db_name;
28 char *db_table;
29 char *dbfield_name;
30 char *dbfield_passwd;
31
32 {
33
34 char *real_passwd;
35 char *mysqlcmd;
36 int sql_len;
37
38    if (debug & DEBUG_AUTHEN_FLAG)
39         report(LOG_DEBUG, "MySQL: verify %s", user);
40         
41 /* Connect database server */
42
43    if ( !( mysql_connect(&mysqldb,db_hostname,db_user,db_password) ) )
44         {
45                 if (debug & DEBUG_AUTHEN_FLAG)
46                     report(LOG_DEBUG, "MySQL: cannot connect as %s", db_user);
47                 return(0);
48         }
49
50 /*Select tacacs db */
51
52     if ( mysql_select_db(&mysqldb,db_name) )
53         {
54                 if (debug & DEBUG_AUTHEN_FLAG)
55                    report(LOG_DEBUG, "MySQL: cannot find database named %s",db_name);
56                 return(0);
57         }
58
59 /* Check select string length */
60
61 sql_len=strlen(dbfield_passwd)+strlen(dbfield_name)+strlen(db_table)+strlen(user)+strlen(AUTHSQL);
62
63   if ( sql_len> SQLCMDL )
64         {
65                 if (debug & DEBUG_AUTHEN_FLAG)
66                      report(LOG_DEBUG, "MySQL: Sql cmd exceed alowed limits");
67                 return(0);
68         }
69
70 /* Prepare select string */
71
72 mysqlcmd=(char *) malloc(sql_len);
73
74 if(mysqlcmd==NULL) { 
75         if (debug & DEBUG_AUTHEN_FLAG)
76                 report(LOG_ERR, "mysql_db_verify: mysqlcmd malloc error");
77         return(0);
78 }
79
80 sprintf(mysqlcmd,AUTHSQL,dbfield_passwd,db_table,dbfield_name,user);
81
82 /*  Query database */
83
84     if (mysql_query(&mysqldb,mysqlcmd))
85         {
86         if (debug & DEBUG_AUTHEN_FLAG)
87                 report(LOG_DEBUG, "MySQL: cannot query database ");
88         free(mysqlcmd);
89         return(0);
90         }
91
92     free(mysqlcmd);
93     
94     if (!(res = mysql_store_result(&mysqldb)))
95         {
96         if (debug & DEBUG_AUTHEN_FLAG)
97                 report(LOG_DEBUG, "MySQL: cannot store result");
98         return(0);
99         }  
100    
101    if(!(row = mysql_fetch_row(res)))
102         {
103         if (debug & DEBUG_AUTHEN_FLAG)
104                 report(LOG_DEBUG, "MySQL: cannot fetch row");
105         return(0);
106         }  
107   
108    if (strlen(row[0]) <=0 )
109         {
110         if (debug & DEBUG_AUTHEN_FLAG)
111                 report(LOG_DEBUG, "MySQL: DB passwd entry is NULL");
112         return(0);
113         }
114   /* Allocate memory for real_passwd */
115         real_passwd=(char *) malloc(strlen(row[0])+1);
116         strcpy(real_passwd,row[0]);
117  
118    if (!mysql_eof(res))
119         {
120         if (debug & DEBUG_AUTHEN_FLAG)
121                 report(LOG_DEBUG, "MySQL:  Result not end!!");
122         return(0);
123         }
124
125     mysql_free_result(res);
126     mysql_close(&mysqldb);
127   
128 if (debug & DEBUG_AUTHEN_FLAG)   
129      report(LOG_DEBUG, "MySQL: verify password '%s' to DES encrypted string '%s'", users_passwd, real_passwd);
130
131     /* Try to verify the password */
132     if (!des_verify(users_passwd, real_passwd)) {
133         free(real_passwd);
134         return (0);
135     }
136     free(real_passwd);
137     return (1); /* Return 1 if verified, 0 otherwise. */
138 }
139
140 int 
141 mysql_db_acct(db_user,db_password,db_hostname,db_name,db_table,s_name,c_name,a_username,elapsed_time,bytes_in,bytes_out)
142
143 char *db_user;                  /* db's parameters              */
144 char *db_password;
145 char *db_hostname;
146 char *db_name;
147 char *db_table;
148 char *s_name, *c_name,*a_username,*elapsed_time,*bytes_in,*bytes_out;
149
150 {
151
152 char *mysqlcmd;
153 int sql_len;
154         
155 /* Connect database server */
156
157    if (!(mysql_connect(&mysqldb,db_hostname,db_user,db_password)))
158         {
159         if (debug & DEBUG_ACCT_FLAG)
160                 report(LOG_DEBUG, "MySQL: cannot connect as %s", db_user);
161                 return(0);
162         }
163
164 /*Select tacacs db */
165
166     if (mysql_select_db(&mysqldb,db_name))
167         {
168         if (debug & DEBUG_ACCT_FLAG)
169                 report(LOG_DEBUG, "MySQL: cannot find database named %s",db_name);
170                 return(0);
171         }
172
173 /* Check buffer overflow for select string */
174 sql_len=strlen(db_table)+strlen(a_username)+strlen(s_name)+strlen(c_name)+strlen(elapsed_time)+strlen(bytes_in)+strlen(bytes_out)+strlen(ACCTSQL);  
175
176 if ( sql_len >SQLCMDL)
177         {
178         if (debug & DEBUG_ACCT_FLAG)
179                 report(LOG_DEBUG, "MySQL: Sql cmd exceed alowed limits");
180                 return(0);
181         }
182  
183
184 /* Prepare select string */
185 mysqlcmd=(char *) malloc(sql_len);
186
187 if(mysqlcmd==NULL) { 
188         if (debug & DEBUG_ACCT_FLAG)
189                 report(LOG_ERR, "mysql_db_acct: mysqlcmd malloc error");
190         return(0);
191 }
192
193 sprintf(mysqlcmd,ACCTSQL,db_table,a_username,s_name,c_name,elapsed_time,bytes_in,bytes_out);
194
195 /*  Query database */
196
197     if (mysql_query(&mysqldb,mysqlcmd))
198         {
199         if (debug & DEBUG_ACCT_FLAG)
200                 report(LOG_DEBUG, "MySQL: cannot query database");
201         free(mysqlcmd);
202         return(0);
203         }
204
205         free(mysqlcmd);
206
207 /* Check if accounting is sucess */
208     if ( mysql_affected_rows( &mysqldb ) < 0 )
209         {
210         if (debug & DEBUG_ACCT_FLAG)
211                 report(LOG_DEBUG, "MySQL: Insert isn't sucess");
212         return(0);
213         }
214         return (1); /* Return 1 if verified, 0 otherwise. */
215 }
216 #endif