OSDN Git Service

Fixed link error
[opengatem/opengatem.git] / mdsrc / workdb.c
1 /**************************************************
2 OpengateM - a MAC address authentication system
3    module for local work database
4
5 Copyright (C) 2011 Opengate Project Team
6 Written by Yoshiaki Watanabe
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22 Email: watanaby@is.saga-u.ac.jp
23 **************************************************/
24 #include "opengatemd.h"
25 #include <sqlite3.h>
26
27 static int sqliteBusyTimeout=100;  /* value used in sqite3_busy_timeout() */
28 static sqlite3 *dbMd; /* handle for opengatemd.db */
29
30 /*******************************************************************
31  read sqlite busy timeout value from conf and set it to static variable
32 *******************************************************************/
33 int setupSqliteBusyTimeoutValue(void){
34
35   char *str;
36   
37   /* if set in conf, use the value. if not, use the above default. */
38   str=GetConfValue("SqliteBusyTimeout");
39   if(str!=NULL) sqliteBusyTimeout=atoi(str);
40
41   return sqliteBusyTimeout;
42 }
43
44 /********************************************
45 initialize work db implemented with sqlite
46 ********************************************/
47 int initWorkDb(void){
48
49   char *pErrMsg=NULL;
50
51   /* SQL CREATE TABLE COMMANDs */
52   char *createCmd1="CREATE TABLE IF NOT EXISTS sessionmd "
53     "(macAddress TEXT PRIMARY KEY, "
54     "userId TEXT, extraId TEXT, openTime INTEGER, checkTime INTEGER, "
55     "ruleNumber INTEGER)";
56   char *createCmd2="CREATE TABLE IF NOT EXISTS macinfo "
57     "(macAddress TEXT PRIMARY KEY ON CONFLICT REPLACE, "
58     "detectTime INTEGER, ttl INTEGER, isNat INTEGER)";
59   char *createCmd3="CREATE TABLE IF NOT EXISTS macippair "
60     "(macAddress TEXT, "
61     "ipAddress TEXT, findTime INTEGER)";
62
63   /* setup SqLite3_busy_timeout read from conf */
64   SetupSqliteBusyTimeoutValue();
65
66   /* Open sqlite */
67   if(sqlite3_open(GetConfValue("SqliteDbMd"),&dbMd)!=SQLITE_OK){
68     err_msg("ERR at %s#%d: sqlite3_open",__FILE__,__LINE__);
69     terminateProg(0);
70   }
71   sqlite3_busy_timeout(dbMd, sqliteBusyTimeout);
72
73   /* create table1 */
74   if(sqlite3_exec(dbMd, createCmd1, NULL, NULL, &pErrMsg)!=SQLITE_OK){
75     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
76     terminateProg(0);
77   }
78
79   /* create table2 */
80   if(sqlite3_exec(dbMd, createCmd2, NULL, NULL, &pErrMsg)!=SQLITE_OK){
81     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
82     terminateProg(0);
83   }
84
85   /* create table3 */
86   if(sqlite3_exec(dbMd, createCmd3, NULL, NULL, &pErrMsg)!=SQLITE_OK){
87     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
88     terminateProg(0);
89   }
90
91   return TRUE;
92 }
93
94 /********************************************
95 finalize work db implemented with sqlite
96 ********************************************/
97 int finalizeWorkDb(void){
98
99   /* Close db for opengatemd */
100   sqlite3_close(dbMd);
101
102   return TRUE;
103 }
104
105 /************************************************************
106  insert session-info to work db at starting session
107 ************************************************************/
108 int insertSessionToWorkDb(char* macAddress, char* userId, char* extraId, 
109                         int ruleNumber){
110
111   int rc;
112   char *pErrMsg=NULL;
113
114   /* SQL INSERT COMMAND, where %x is replaced in snprintf */
115   char *insertFormat="INSERT INTO sessionmd "
116     "(macAddress, userId, extraId, openTime, checkTime, ruleNumber) "
117     "values ('%s','%s','%s', %d, %d, %d)";
118   char *insertCmd;
119   int resultFlag=TRUE;
120
121   /* Prepare insert command */
122   insertCmd=sqlite3_mprintf(insertFormat, macAddress, userId,extraId, 
123                             time(NULL), time(NULL), ruleNumber);
124
125   /* Execute insert to sqlite */
126   if((rc=sqlite3_exec(dbMd, insertCmd, NULL, NULL, &pErrMsg))!=SQLITE_OK){
127     resultFlag=FALSE;
128     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
129   }
130
131   /*Memory free for sqlite3 string */
132   sqlite3_free(insertCmd);
133
134   return resultFlag;
135 }
136
137 /*************************************************************
138  update checkTime to now
139 *************************************************************/
140 int updateCheckTimeInWorkDb(char* macAddress){
141
142   char *pErrMsg=NULL;
143
144   /* SQL UPDATE COMMAND, where %x is replaced in mprintf */
145   char *updateFormat="UPDATE sessionmd "
146     "SET checkTime=%d WHERE macAddress='%s'";
147   char *updateCmd;
148   int resultFlag=TRUE;
149
150   /* prepare command */
151   updateCmd=sqlite3_mprintf(updateFormat, time(NULL), macAddress);
152
153   /* execute update to sqlite */
154   if(sqlite3_exec(dbMd, updateCmd, NULL, NULL, &pErrMsg)!=SQLITE_OK){
155     resultFlag=FALSE;
156     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
157   }
158
159   /*memory free for sqlite3 string */
160   sqlite3_free(updateCmd);
161
162   return resultFlag;
163 }
164
165 /*************************************************************
166  delete session-info in work db at stop session
167 *************************************************************/
168 int delSessionFromWorkDb(char* macAddress){
169
170   char *pErrMsg=NULL;
171
172   /* SQL DELETE COMMAND, where %x is replaced in mprintf */
173   char *deleteFormat="DELETE FROM sessionmd WHERE macAddress='%s'";
174   char *deleteCmd;
175   int resultFlag=TRUE;
176
177   /* prepare command */
178   deleteCmd=sqlite3_mprintf(deleteFormat, macAddress);
179
180   /* execute delete */
181   if(sqlite3_exec(dbMd, deleteCmd, NULL, NULL, &pErrMsg)!=SQLITE_OK){
182     resultFlag=FALSE;
183     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
184   }
185
186   /*memory free for sqlite3 string */
187   sqlite3_free(deleteCmd);
188
189   return resultFlag;
190 }
191
192 /************************************************
193  get session-info from work db  
194 input = macAddress, output = others
195 *************************************************/
196 int getSessionFromWorkDb(char* macAddress, char* userId, char* extraId, 
197                          int* openTime, int* checkTime, 
198                          int* ruleNumber){
199
200   sqlite3_stmt *stmt;
201  
202   /* SQL SELECT COMMAND, where %x is replaced in snprintf */
203   char *selectFormat="SELECT userId, extraId, openTime, checkTime, "
204     "ruleNumber FROM sessionmd WHERE macAddress='%s'";
205   char *selectCmd;
206   int resultFlag=TRUE;
207
208   /* prepare command string */
209   selectCmd=sqlite3_mprintf(selectFormat, macAddress);
210   
211   /* compile to internal statement */
212   if(sqlite3_prepare(dbMd, selectCmd, BUFFMAXLN, &stmt, NULL)!=SQLITE_OK){
213     resultFlag=FALSE;
214     err_msg("ERR at %s#%d: sqlite3_prepare",__FILE__,__LINE__);
215
216     /* finalize */
217     sqlite3_free(selectCmd);
218     sqlite3_finalize(stmt);
219
220     return FALSE;
221   }
222
223   /* get first record */
224   if(sqlite3_step(stmt)==SQLITE_ROW){
225     strlcpy(userId, (char*)sqlite3_column_text(stmt, 0), USERMAXLN);
226     strlcpy(extraId, (char*)sqlite3_column_text(stmt, 1), USERMAXLN);
227     *openTime=(int)sqlite3_column_int(stmt, 2);
228     *checkTime=(int)sqlite3_column_int(stmt, 3);
229     *ruleNumber=(int)sqlite3_column_int(stmt, 4);
230     resultFlag=TRUE;
231   }else{
232     userId[0]='\0';
233     extraId[0]='\0';
234     *openTime=0;
235     *checkTime=0;
236     *ruleNumber=0;
237     resultFlag=FALSE;
238   }
239
240   /* finalize */
241   sqlite3_free(selectCmd);
242   sqlite3_finalize(stmt);
243   
244   return resultFlag;
245 }
246
247
248 /************************************************
249  close sessions that exceed time limit  
250  1. select timeover records and close the firewall 
251  2. delete the records
252 if delayed=FALSE, close all sessions without delay
253 if delayed=TRUE, close sessions that exceed time limit 
254 *************************************************/
255 int delUselessSessionsInWorkDb(int delayed){
256
257   char *pErrMsg=NULL;
258   int uselessLimitTime;
259
260   /* the session is useless, if it doesn't update after this time */
261   uselessLimitTime = time(NULL)-atoi(GetConfValue("UselessTimeout"));
262  
263   /* if delayed is false, all sessions(before now) are deleted */
264   if(!delayed) uselessLimitTime = time(NULL);
265
266   /* SQL SELECT COMMAND, where %x is replaced in snprintf */
267   char *selectFormat="SELECT ruleNumber, userId, extraId, "
268     "macAddress, openTime FROM sessionmd WHERE checkTime<%d";
269   char *deleteFormat="DELETE FROM sessionmd WHERE checkTime<%d";
270   char *selectCmd;
271   char *deleteCmd;
272   int resultFlag=TRUE;
273
274   /* prepare command string for select */
275   selectCmd=sqlite3_mprintf(selectFormat, uselessLimitTime);
276
277   /* exec command, callback function = CloseSession() */
278   if(sqlite3_exec(dbMd, selectCmd, CloseSession, NULL, &pErrMsg)!=SQLITE_OK){
279     resultFlag=FALSE;
280     err_msg("ERR at %s#%d: sqlite_exec:%s",__FILE__,__LINE__, pErrMsg);
281   }
282
283   /* prepare command string for update */
284   deleteCmd=sqlite3_mprintf(deleteFormat, uselessLimitTime);
285
286   /* exec command  */
287   if(sqlite3_exec(dbMd, deleteCmd, NULL, NULL, &pErrMsg)!=SQLITE_OK){
288     resultFlag=FALSE;
289     err_msg("ERR at %s#%d: sqlite_exec:%s",__FILE__,__LINE__, pErrMsg);
290   }
291     
292   /* finalize */
293   sqlite3_free(selectCmd);
294   sqlite3_free(deleteCmd);
295
296   return resultFlag;
297 }
298
299 /************************************************
300 get list of sessions from work DB and create hash table of sessions
301 for opengatemd 
302  key=macAddress, value=0 
303 *************************************************/
304 int getSessionTableFromWorkDb(DB* sessionTable){
305   DBT hashKey;
306   DBT hashVal;
307   sqlite3_stmt *stmt=NULL;
308   int resultFlag=FALSE;
309   char macAddress[ADDRMAXLN];
310   int zero=0;
311  
312   /* SQL SELECT COMMAND to get all mac address in session table */
313   char *selectCmd="SELECT macAddress FROM sessionmd";
314
315   /* compile to internal statement */
316   if(sqlite3_prepare(dbMd, selectCmd, BUFFMAXLN, &stmt, NULL)!=SQLITE_OK){
317     err_msg("ERR at %s#%d: sqlite3_prepare",__FILE__,__LINE__);
318
319     /* finalize */
320     sqlite3_finalize(stmt);
321     return FALSE;
322   }
323
324   /* get result rows */
325   while(sqlite3_step(stmt)==SQLITE_ROW){
326     resultFlag=TRUE;
327     strlcpy(macAddress,(char*)sqlite3_column_text(stmt, 0), ADDRMAXLN);
328
329     /* put to hash table */
330     hashVal.data = &zero;
331     hashVal.size = sizeof(int);    
332     hashKey.data = macAddress;
333     hashKey.size = strlen(macAddress)+1;
334     if(sessionTable->put(sessionTable, &hashKey, &hashVal, 0) == -1) {
335       err_msg("ERR at %s#%d: fail to put into hash table",__FILE__,__LINE__);
336     }
337   }
338
339   /* finalize */
340   sqlite3_finalize(stmt);
341
342   return resultFlag;
343 }
344
345
346 /**********************************
347  put out detected mac related info to macinfo table in work db
348  it is used at checking nat 
349 **********************************/
350 int putMacInfoToWorkDb(char* macAddress, int ttl, int isNat){
351
352   int rc;
353   char *pErrMsg=NULL;
354
355   /* SQL INSERT COMMAND, where %x is replaced in snprintf */
356   char *insertFormat="INSERT INTO macinfo "
357     "(macAddress, detectTime, ttl, isNat) "
358     "values ('%s', %d, %d, %d)";
359   char *insertCmd;
360   int resultFlag=TRUE;
361
362   /* Prepare insert command */
363   insertCmd=sqlite3_mprintf(insertFormat, macAddress, time(NULL), ttl, isNat);
364
365   /* Execute insert to sqlite */
366   if((rc=sqlite3_exec(dbMd, insertCmd, NULL, NULL, &pErrMsg))!=SQLITE_OK){
367     resultFlag=FALSE;
368     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
369   }
370
371   /*Memory free for sqlite3 string */
372   sqlite3_free(insertCmd);
373
374   return resultFlag;
375 }
376
377
378 /**********************************
379  get mac related info from macinfo table in work db 
380 **********************************/
381 int getMacInfoFromWorkDb(char* macAddress, char* detectTimeStr, int* pTtl){
382
383   sqlite3_stmt *stmt;
384
385   /* SQL SELECT COMMAND, where %x is replaced in snprintf */
386   char *selectFormat="SELECT datetime(detectTime,'unixepoch','localtime'), "
387     " ttl FROM macinfo WHERE macAddress='%s' ";
388   char *selectCmd;
389   int resultFlag=TRUE;
390
391   /* set default value */
392   *pTtl=0;
393   detectTimeStr[0]='?';
394   detectTimeStr[1]='\0';
395
396   /* Prepare select command */
397   selectCmd=sqlite3_mprintf(selectFormat, macAddress);
398
399   /* compile to internal statement */
400   if(sqlite3_prepare(dbMd, selectCmd, BUFFMAXLN, &stmt, NULL)!=SQLITE_OK){
401     resultFlag=FALSE;
402     err_msg("ERR at %s#%d: sqlite3_prepare",__FILE__,__LINE__);
403
404     /* finalize */
405     sqlite3_free(selectCmd);
406     sqlite3_finalize(stmt);
407     return FALSE;
408   }
409
410   /* get first record */
411   if(sqlite3_step(stmt)==SQLITE_ROW){
412     strlcpy(detectTimeStr, (char*)sqlite3_column_text(stmt, 0), WORDMAXLN);
413     *pTtl=(int)sqlite3_column_int(stmt, 1);
414     resultFlag=TRUE;
415   }else{
416     resultFlag=FALSE;
417   }
418
419   /* finalize */
420   sqlite3_free(selectCmd);
421   sqlite3_finalize(stmt);
422   return resultFlag;
423 }
424
425 /************************************
426 is the rule number active in opengatemd session table
427 ************************************/
428 int isActiveRuleInWorkDb(int ruleNumber){
429
430   sqlite3_stmt *stmt;
431  
432   /* SQL SELECT COMMAND, where %x is replaced in snprintf */
433   char *selectFormat="SELECT * FROM sessionmd "
434     " WHERE ruleNumber=%d";
435   char *selectCmd;
436   int resultFlag=TRUE;
437
438   /* prepare command string */
439   selectCmd=sqlite3_mprintf(selectFormat, ruleNumber);
440   
441   /* compile to internal statement */
442   if(sqlite3_prepare(dbMd, selectCmd, BUFFMAXLN, &stmt, NULL)!=SQLITE_OK){
443     resultFlag=FALSE;
444     err_msg("ERR at %s#%d: sqlite3_prepare",__FILE__,__LINE__);
445
446     /* finalize */
447     sqlite3_free(selectCmd);
448     sqlite3_finalize(stmt);
449     return FALSE;
450   }
451
452   /* get first record */
453   if(sqlite3_step(stmt)==SQLITE_ROW) resultFlag=TRUE;
454   else resultFlag=FALSE;
455
456   /* finalize */
457   sqlite3_free(selectCmd);
458   sqlite3_finalize(stmt);
459   return resultFlag;
460 }
461
462 /********************************************
463 Is the MAC-IP pair found in work db
464 ********************************************/
465 int isMacIpPairFoundInWorkDb(char* macAddress, char* ipAddress){
466   sqlite3_stmt *stmt;
467  
468   /* SQL SELECT COMMAND, where %x is replaced in snprintf */
469   char *selectFormat="SELECT * FROM macippair "
470     " WHERE macAddress='%s' AND ipAddress='%s'";
471   char *selectCmd;
472   int resultFlag=TRUE;
473
474   /* prepare command string */
475   selectCmd=sqlite3_mprintf(selectFormat, macAddress, ipAddress);
476   
477   /* compile to internal statement */
478   if(sqlite3_prepare(dbMd, selectCmd, BUFFMAXLN, &stmt, NULL)!=SQLITE_OK){
479     resultFlag=FALSE;
480     err_msg("ERR at %s#%d: sqlite3_prepare",__FILE__,__LINE__);
481
482     /* finalize */
483     sqlite3_free(selectCmd);
484     sqlite3_finalize(stmt);
485     return FALSE;
486   }
487
488   /* get first record */
489   if(sqlite3_step(stmt)==SQLITE_ROW) resultFlag=TRUE;
490   else resultFlag=FALSE;
491
492   /* finalize */
493   sqlite3_free(selectCmd);
494   sqlite3_finalize(stmt);
495   return resultFlag;
496 }
497
498
499 /********************************************
500 Insert MAC-IP pair to work db
501 ********************************************/
502 int putMacIpPairToWorkDb(char* macAddress, char* ipAddress){
503   int rc;
504   char *pErrMsg=NULL;
505
506   /* SQL INSERT COMMAND, where %x is replaced in snprintf */
507   char *insertFormat="INSERT INTO macippair "
508     "(macAddress, ipAddress, findTime) "
509     "values ('%s','%s', %d)";
510   char *insertCmd;
511   int resultFlag=TRUE;
512
513   /* Prepare insert command */
514   insertCmd=sqlite3_mprintf(insertFormat, macAddress, ipAddress, time(NULL));
515
516   /* Execute insert to sqlite */
517   if((rc=sqlite3_exec(dbMd, insertCmd, NULL, NULL, &pErrMsg))!=SQLITE_OK){
518     resultFlag=FALSE;
519     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
520   }
521
522   /*Memory free for sqlite3 string */
523   sqlite3_free(insertCmd);
524
525   return resultFlag;
526 }
527
528 /********************************************
529 Delete the mac-ip pairs in work db
530 input=macAddress only
531 ********************************************/
532 int delMacIpPairsInWorkDb(char* macAddress){
533   char *pErrMsg=NULL;
534
535   /* SQL DELETE COMMAND, where %x is replaced in mprintf */
536   char *deleteFormat="DELETE FROM macippair WHERE macAddress='%s'";
537   char *deleteCmd;
538   int resultFlag=TRUE;
539
540   /* prepare command */
541   deleteCmd=sqlite3_mprintf(deleteFormat, macAddress);
542
543   /* execute delete */
544   if(sqlite3_exec(dbMd, deleteCmd, NULL, NULL, &pErrMsg)!=SQLITE_OK){
545     resultFlag=FALSE;
546     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
547   }
548
549   /*memory free for sqlite3 string */
550   sqlite3_free(deleteCmd);
551
552   return resultFlag;
553
554 }
555
556 /********************************************
557 Delete old mac info in work db
558  (detected before 1 month)
559 ********************************************/
560 int delOldMacInfoInWorkDb(void){
561   char *pErrMsg=NULL;
562
563   /* SQL DELETE COMMAND */
564   char *deleteCmd="DELETE FROM macinfo WHERE detectTime<strftime('%s','now','-1 month')";
565   int resultFlag=TRUE;
566
567   /* execute delete */
568   if(sqlite3_exec(dbMd, deleteCmd, NULL, NULL, &pErrMsg)!=SQLITE_OK){
569     resultFlag=FALSE;
570     err_msg("ERR at %s#%d: sqlite3_exec: %s",__FILE__,__LINE__,pErrMsg);
571   }
572
573   return resultFlag;
574 }
575
576 /*********************************************************
577  routines for debugging output
578 *********************************************************/
579 int SetupSqliteBusyTimeoutValue(void){
580   int ret;
581   if(debug>1) err_msg("DEBUG:=>setupSqliteBusyTimeoutValue()");
582   ret=setupSqliteBusyTimeoutValue();
583   if(debug>1) err_msg("DEBUG:(%d)<=setupSqliteBusyTimeoutValue()",ret);
584   return ret;
585 }
586
587 int InitWorkDb(void){
588   int ret;
589   if(debug>1) err_msg("DEBUG:=>initWorkDb( )");
590   ret = initWorkDb();
591   if(debug>1) err_msg("DEBUG:(%d)<=initWorkDb( )",ret);
592   return ret;
593 }
594
595 int FinalizeWorkDb(void){
596   int ret;
597   if(debug>1) err_msg("DEBUG:=>finalizeWorkDb( )");
598   ret = finalizeWorkDb();
599   if(debug>1) err_msg("DEBUG:(%d)<=finalizeWorkDb( )",ret);
600   return ret;
601 }
602
603 int InsertSessionToWorkDb(char* macAddress, char* userId, char* extraId, 
604                          int ruleNumber){
605   int ret;
606   if(debug>1) err_msg("DEBUG:=>insertSessionToWorkDb(%s,%s,%s,%d)",
607                       macAddress, userId, extraId, ruleNumber);
608   ret = insertSessionToWorkDb(macAddress, userId, extraId, ruleNumber);
609   if(debug>1) err_msg("DEBUG:(%d)<=insertSessionToWorkDb( )",ret);
610   return ret;
611 }
612
613 int UpdateCheckTimeInWorkDb(char* macAddress){
614   int ret;
615   if(debug>1) err_msg("DEBUG:=>updateCheckTimeInWorkDb(%s)", macAddress);
616   ret = updateCheckTimeInWorkDb(macAddress);
617   if(debug>1) err_msg("DEBUG:(%d)<=updateCheckTimeInWorkDb( )",ret);
618   return ret;
619 }
620
621 int DelSessionFromWorkDb(char* macAddress){
622   int ret;
623   if(debug>1) err_msg("DEBUG:=>delSessionFromWorkDb(%s)", macAddress);
624   ret = delSessionFromWorkDb(macAddress);
625   if(debug>1) err_msg("DEBUG:(%d)<=delSessionFromWorkDb( )",ret);
626   return ret;
627 }
628
629 int GetSessionFromWorkDb(char* macAddress, char* userId, char* extraId, 
630                          int* openTime, int* checkTime, int* ruleNumber){
631   int ret;
632   if(debug>1) err_msg("DEBUG:=>getSessionFromWorkDb(%s)", macAddress);
633   ret = getSessionFromWorkDb(macAddress, userId, extraId, openTime, 
634                                checkTime, ruleNumber);
635   if(debug>1) err_msg("DEBUG:(%d)<=getSessionFromWorkDb(,%s,%s,%d,%d,%d)",
636                       ret,userId,extraId,*openTime,*checkTime, *ruleNumber);
637   return ret;
638 }
639
640 int DelUselessSessionsInWorkDb(int delayed){
641   int ret;
642   if(debug>1) err_msg("DEBUG:=>delUselessSessionsInWorkDb(%d)", delayed);
643   ret=delUselessSessionsInWorkDb(delayed);
644   if(debug>1) err_msg("DEBUG:(%d)<=delUselessSessionsInWorkDb( )",ret);
645   return ret;
646 }
647
648 int GetSessionTableFromWorkDb(DB* sessionTable){
649   int ret;
650   if(debug>1) err_msg("DEBUG:=>getSessionTableFromWorkDb( )");
651   ret=getSessionTableFromWorkDb(sessionTable);
652   if(debug>1) err_msg("DEBUG:(%d)<=getSessionTableFromWorkDb( )", ret);
653   return ret;
654 }
655
656 int PutMacInfoToWorkDb(char* macAddress, int ttl, int isNat){
657   int ret;
658   if(debug>1) err_msg("DEBUG:=>putMacInfoToWorkDb(%s,%d,%d)",macAddress,ttl,isNat);
659   ret=putMacInfoToWorkDb(macAddress,ttl,isNat);
660   if(debug>1) err_msg("DEBUG:(%d)<=putMacInfoToWorkDb( )", ret);
661   return ret;
662 }
663
664 int IsActiveRuleInWorkDb(int ruleNumber){
665   int ret;
666   if(debug>1) err_msg("DEBUG:=>isActiveRuleInWorkDb(%d)",ruleNumber);
667   ret=isActiveRuleInWorkDb(ruleNumber);
668   if(debug>1) err_msg("DEBUG:(%d)<=isActiveRuleInWorkDb( )", ret);
669   return ret;
670 }
671
672 int GetMacInfoFromWorkDb(char* macAddress, char* detectTimeStr, int* pTtl){
673   int ret;
674   if(debug>1) err_msg("DEBUG:=>getMacInfoFromWorkDb(%s)", macAddress);
675   ret = getMacInfoFromWorkDb(macAddress, detectTimeStr, pTtl);
676   if(debug>1) err_msg("DEBUG:(%d)<=getMacInfoFromWorkDb(,%s,%d)",
677                       ret, detectTimeStr, *pTtl);
678   return ret;
679 }
680
681 int IsMacIpPairFoundInWorkDb(char* macAddress, char* ipAddress){
682   int ret;
683   if(debug>1) err_msg("DEBUG:=>isMacIpPairFoundInWorkDb(%s,%s)",
684                       macAddress,ipAddress);
685   ret=isMacIpPairFoundInWorkDb(macAddress, ipAddress);
686   if(debug>1) err_msg("DEBUG:(%d)<=isMacIpPairFoundInWorkDb( )", ret);
687   return ret;
688 }
689
690 int PutMacIpPairToWorkDb(char* macAddress, char* ipAddress){
691   int ret;
692   if(debug>1) err_msg("DEBUG:=>putMacIpPairToWorkDb(%s,%s)",
693                       macAddress,ipAddress);
694   ret=putMacIpPairToWorkDb(macAddress, ipAddress);
695   if(debug>1) err_msg("DEBUG:(%d)<=putMacIpPairtoWorkDb( )", ret);
696   return ret;
697 }
698
699
700 int DelMacIpPairsInWorkDb(char* macAddress){
701   int ret;
702   if(debug>1) err_msg("DEBUG:=>delMacIpPairsInWorkDb(%s)",
703                       macAddress);
704   ret=delMacIpPairsInWorkDb(macAddress);
705   if(debug>1) err_msg("DEBUG:(%d)<=delMacIpPairsInWorkDb( )", ret);
706   return ret;
707 }
708
709 int DelOldMacInfoInWorkDb(void){
710   int ret;
711   if(debug>1) err_msg("DEBUG:=>delOldMacInfoInWorkDb()");
712   ret=delOldMacInfoInWorkDb();
713   if(debug>1) err_msg("DEBUG:(%d)<=delOldMacInfoInWorkDb()", ret);
714   return ret;
715 }