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