OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / managementdb.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3
4  module to control management database that includes following tables
5   list of mac addresses and their owners
6   list of usage log
7  this database is implemented with MySql 
8
9 Copyright (C) 2011 Opengate Project Team
10 Written by Yoshiaki Watanabe
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 as published by the Free Software Foundation; either version 2
15 of the License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
26 Email: watanaby@is.saga-u.ac.jp
27 **************************************************/
28 #include "opengatemmng.h"
29 #include <mysql.h>
30
31 MYSQL mysql;
32
33 /*****************************/
34 /* initialize management db  */
35 /*****************************/
36 int initMngDb(void){
37
38   /* set parameters read from conf file */
39   char *server = GetConfValue("MySqlDb/Server");
40   char *user =  GetConfValue("MySqlDb/User");
41   char *password = GetConfValue("MySqlDb/Password");
42   char *database = GetConfValue("MySqlDb/Database");
43   my_bool reconnect;
44
45 /* initialize mysql */
46   mysql_library_init(-1,NULL,NULL);
47   if(mysql_init(&mysql)==NULL){
48      err_msg("ERR at %s#%d: mysql init: %s",__FILE__,__LINE__,
49              mysql_error(&mysql));
50      terminateProg(0);
51   }
52
53   /* Connect to database */
54   if (!mysql_real_connect(&mysql, server,
55                            user, password, database, 0, NULL, 0)) {
56     err_msg("ERR at %s#%d: mysql connect: %s",__FILE__,__LINE__,
57              mysql_error(&mysql));
58     terminateProg(0);
59   }
60
61   /* set auto-reconnect true */
62   reconnect = TRUE;
63   mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect);  
64
65   return TRUE;
66 }
67
68 /**********************************/
69 /*close management db             */
70 /**********************************/
71 void closeMngDb(void){
72   mysql_close(&mysql);
73   mysql_library_end();
74 }
75
76
77 /*********************************************************/
78 /* get nic vendor from table nicvendors in management db */
79 /* macAddress:(in),vendor:(out),bufferLength:(in)        */ 
80 /*********************************************************/
81 int getNicVendorFromMngDb(char* macAddress, char* vendor, int bufferLength){
82
83   MYSQL_RES *res;
84   MYSQL_ROW row;
85   int found=FALSE;
86   char queryStr[BUFFMAXLN];
87   char macHeader[ADDRMAXLN];
88
89   /* set default values (="?") */
90   vendor[0]='?';
91   vendor[1]='\0';
92  
93   /* prepare query string */
94   /* macHeader is first 3 bytes of macAddess that represents vender code */
95   /* macAddress="11:22:33:44:55:66" -> macHeader="11:22:33" */
96   strlcpy(macHeader, macAddress, ADDRMAXLN);
97   macHeader[8]='\0';
98   snprintf(queryStr, BUFFMAXLN, 
99            "select org from nicvendors where oui='%s'",
100            macHeader);
101
102   /* send SQL query */
103   if (mysql_query(&mysql, queryStr)){
104      err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
105              mysql_error(&mysql));
106      return FALSE;
107   }
108   res = mysql_use_result(&mysql);
109   
110   /* get a row from query result */
111   row = mysql_fetch_row(res);
112
113   /* if not found, return false */
114   if(row==NULL)  found=FALSE;
115
116   /* if found, get the vendor name */
117   else {
118     strlcpy(vendor, row[0], bufferLength);
119     found=TRUE;
120   }
121
122   /* free memory area */
123   mysql_free_result(res);
124   return found;
125 }
126
127 /*************************************************/
128 /* The macAddr is already registered or not      */
129 /* the row having status="D"(deleted) is ignored */
130 /* macAddr:(input)                               */
131 /*************************************************/
132 int isMacAddrFoundInMngDb(char* macAddr){
133
134   MYSQL_RES *res=NULL;
135   MYSQL_ROW row;
136   char queryStr[BUFFMAXLN];
137   int ret;
138
139   /* prepare query string */
140   snprintf(queryStr, BUFFMAXLN, 
141            "select * from macaddrs "
142            " where macAddress='%s' and status!='D'",
143            macAddr);
144   
145   /* send SQL query */
146   if (mysql_query(&mysql, queryStr)){
147     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
148             mysql_error(&mysql));
149     return FALSE;
150   }
151   
152   /* store query result */
153   res = mysql_store_result(&mysql);
154
155   /* get a row from query result */
156   row = mysql_fetch_row(res);
157   
158   /* if found, return true */
159   if(row!=NULL) ret=TRUE;
160
161   /* if not found, return false */
162   else ret=FALSE;
163
164   /* free memory area */
165   mysql_free_result(res);
166   
167   return ret;
168 }
169
170 /******************************************************/
171 /* The count of registered mac addresses for the user */
172 /* the row having status="D"(deleted) is ignored      */
173 /* all-args:(input)                                   */
174 /******************************************************/
175 int countMacAddrsForUserInMngDb(char* userId, char* extraId){
176
177   MYSQL_RES *res=NULL;
178   MYSQL_ROW row;
179   char queryStr[BUFFMAXLN];
180   char countStr[WORDMAXLN];
181   int count=10000;
182
183   /* prepare query string */
184   snprintf(queryStr, BUFFMAXLN, 
185            "select count(*) from macaddrs "
186            " where userId='%s' and extraId='%s' and status!='D'",
187            userId, extraId);
188
189   /* send SQL query */
190   if (mysql_query(&mysql, queryStr)){
191     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
192             mysql_error(&mysql));
193     return count;
194   }
195
196   /* store query result */
197   res = mysql_store_result(&mysql);
198
199   /* get a row from query result */
200   row = mysql_fetch_row(res);
201
202   /* if found, return the count */
203   if(row!=NULL){
204     strlcpy(countStr, row[0], WORDMAXLN);
205     count=atoi(countStr);
206   }
207
208   /* free memory area */
209   mysql_free_result(res);
210
211   return count;
212 }
213
214 /***********************************************************/
215 /* register mac address and the owner to the management db */
216 /* all-args:(input)                                        */
217 /***********************************************************/
218 int registMacAddrToMngDb(char* macAddr, char* deviceName, char* userId, char* extraId, char* mailAddr){
219
220   char queryStr[BUFFMAXLN];
221   int count;
222
223    /* if alreay registered, return false */
224   if(IsMacAddrFoundInMngDb(macAddr)){
225     SetMessage(ExistentMacAddr);
226     return FALSE;
227   }
228  
229   /* if the count of devices for a user exceeds the limit(in conf), return false */
230   count=CountMacAddrsForUserInMngDb(userId,extraId);
231   if(count >= atoi(GetConfValue("MaxDevices"))){
232     SetMessage(DeviceCountOver);
233     return FALSE;
234   }
235  
236   /* prepare query string */
237   snprintf(queryStr, BUFFMAXLN, 
238            "insert into macaddrs "
239            "(macAddress, status, device, userId, extraId, "
240            " entryDate, renewDate, limitDate, mailAddress) "
241            "values ('%s','A','%s', '%s', '%s', "
242            " now(), now(), %s, '%s')",
243            macAddr, deviceName, userId, extraId, 
244            GetConfValue("LimitDate"), mailAddr);
245
246   /* send SQL query */
247   if (mysql_query(&mysql, queryStr)){
248      err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
249              mysql_error(&mysql));
250      return FALSE;
251   }
252
253   return TRUE;
254 }
255
256 /********************************************************/
257 /* get next mac address for the user from management db */
258 /* at first call, execute query and return first row    */
259 /* from second call, return next row of previous query  */
260 /* repeat until return=false (end of list, clear work)  */
261 /* userId,extraId:(input), others:(output)              */
262 /********************************************************/
263 int getNextMacAddrFromMngDb(char* userId, char* extraId, char* macAddress, char* deviceName, char* entryDate, char* limitDate, char* status, char* mailAddress){
264
265   static MYSQL_RES *res=NULL;
266   MYSQL_ROW row;
267   char queryStr[BUFFMAXLN];
268
269   /* set default values */
270   macAddress[0]=deviceName[0]=entryDate[0]=limitDate[0]='\0';
271
272   /* if do not get result yet */
273   if(res==NULL){
274
275     /* prepare query string */
276     snprintf(queryStr, BUFFMAXLN, 
277       "select macAddress, device, entryDate, limitDate, status, mailAddress "
278       "from macaddrs where userId='%s' and extraId='%s' and status!='D'",
279       userId, extraId);
280
281     /* send SQL query */
282     if (mysql_query(&mysql, queryStr)){
283       err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
284               mysql_error(&mysql));
285       return FALSE;
286     }
287     
288     /* store result */
289     res = mysql_store_result(&mysql);
290   }
291
292   /* output table name */
293   row = mysql_fetch_row(res);
294
295   /* if found, return values */
296   if(row!=NULL){
297     strlcpy(macAddress, row[0],ADDRMAXLN);
298     strlcpy(deviceName,row[1],WORDMAXLN);
299     strlcpy(entryDate,row[2],WORDMAXLN);
300     strlcpy(limitDate,row[3],WORDMAXLN);
301     strlcpy(status,row[4],WORDMAXLN);
302     strlcpy(mailAddress,row[5],BUFFMAXLN);
303     return TRUE;
304   }
305   /* if not found, free memory area */
306   else{
307     mysql_free_result(res);
308     return FALSE;
309   }
310 }
311
312
313 /***************************************************/
314 /* write mac modification log to the management db */
315 /* all-args:(input)                                */
316 /***************************************************/
317 int putMacModifyLogToMngDb(char* userId, char* extraId, char* macAddr, char modifyType){
318
319   char queryStr[BUFFMAXLN];
320  
321   /* prepare query string */
322   snprintf(queryStr, BUFFMAXLN, 
323            "insert into macmodify "
324            "(userId, extraId, macAddress, modifyType, modifyDate) "
325            "values ('%s', '%s', '%s', '%c', now())",
326            userId, extraId, macAddr, modifyType);
327
328   /* send SQL query */
329   if (mysql_query(&mysql, queryStr)){
330      err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
331              mysql_error(&mysql));
332      return FALSE;
333   }
334
335   return TRUE;
336 }
337
338 /************************************************************/
339 /* The count of mac address modification in last 24 hours   */
340 /* (the modification count is checked to cope with abuse)   */
341 /* all-args:(input)                                         */
342 /* indicate either of userID&extraId or macAddress to count */ 
343 /************************************************************/
344 int countMacModifyPerDayInMngDb(char* userId, char* extraId, char* macAddress){
345
346   MYSQL_RES *res=NULL;
347   MYSQL_ROW row;
348   char queryStr[BUFFMAXLN];
349   char countStr[WORDMAXLN];
350   int count=10000;
351
352   /* prepare query string */
353   /* count for one macAddress if not null, elsecount for one userID)*/
354   if(!isNull(macAddress)){
355     snprintf(queryStr, BUFFMAXLN, 
356            "select count(*) from macmodify "
357            " where macAddress='%s' and "
358            " modifyDate > adddate(now(), interval -1 day) ",
359            macAddress);
360   }else{
361     snprintf(queryStr, BUFFMAXLN, 
362            "select count(*) from macmodify "
363            " where userId='%s' and extraId='%s' and "
364            " modifyDate > adddate(now(), interval -1 day) ",
365            userId, extraId);
366   }
367   
368   /* send SQL query */
369   if (mysql_query(&mysql, queryStr)){
370     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
371             mysql_error(&mysql));
372     return count;
373   }
374
375   /* store query result */
376   res = mysql_store_result(&mysql);
377
378   /* get a row from query result */
379   row = mysql_fetch_row(res);
380
381   /* if found, return count (if not, return large value for safe) */
382   if(row!=NULL){
383     strlcpy(countStr, row[0], WORDMAXLN);
384     count=atoi(countStr);
385   }
386
387   /* free memory area */
388   mysql_free_result(res);
389
390   return count;
391 }
392
393 /*********************************************************/
394 /* delete a mac address registered in the management db  */
395 /* in actuality, not delete row, but set the status="D"  */
396 /*  when usage log for the address is found in log table */
397 /* macAddr:(input)                                       */
398 /*********************************************************/
399 int delMacAddrFromMngDb(char* macAddr){
400
401   char queryStr[BUFFMAXLN];
402
403   /* if the mac is found in the past session log,        */
404   /* the owner info is needed for the log inspection     */
405   /* thus the info is not deleted, but set as status="D" */
406   if(IsSessionFoundInMngDb(macAddr)){
407
408     /* prepare query string */
409     /* don't touch device of status="I"(set Inactive by admin) */
410     snprintf(queryStr, BUFFMAXLN, 
411              "update macaddrs set status='D',limitDate=now() "
412              " where macAddress='%s' and (status='A' or status='P')", macAddr);
413   }
414
415   /* if mac is not found in past session log, the row is removed */
416   else{
417
418     /* prepare query string */
419     /* don't touch device of status="I"(set Inactive by admin) */
420     snprintf(queryStr, BUFFMAXLN, 
421              "delete from macaddrs "
422              " where macAddress='%s' and (status='A' or status='P')", macAddr);
423   }
424
425   /* send SQL query */
426   if (mysql_query(&mysql, queryStr)){
427     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
428             mysql_error(&mysql));
429     return FALSE;
430   }
431   return TRUE;
432 }
433
434 /******************************************/
435 /* renew the limit date for a mac address */
436 /*  in the management db                  */
437 /* macAddr:(input)                        */
438 /* LimitDate in conf file is a SQL string */
439 /* to make a future date                  */
440 /* eg, "adddate(now(),interval 1 month)"  */
441 /******************************************/
442 int renewMacAddrInMngDb(char* macAddr){
443
444   char queryStr[BUFFMAXLN];
445
446   /* prepare query string */
447   /* don't touch device set as inactive by admin */
448   snprintf(queryStr, BUFFMAXLN, 
449            "update macaddrs set status='A', renewDate=now(), limitDate=%s "
450            " where (status='A' or status='P') and macAddress='%s'", 
451            GetConfValue("LimitDate"),macAddr);
452
453   /* send SQL query */
454   if (mysql_query(&mysql, queryStr)){
455     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
456             mysql_error(&mysql));
457     return FALSE;
458   }
459   return TRUE;
460 }
461
462 /******************************************
463 pause the usage of the device for a mac address
464  registered in the management db
465 ******************************************/
466 int pauseMacAddrInMngDb(char* macAddr){
467
468   char queryStr[BUFFMAXLN];
469
470   /* prepare query string */
471   /* don't touch device set as inactive by admin */
472   snprintf(queryStr, BUFFMAXLN, 
473            "update macaddrs set status='P' "
474            " where status='A' and macAddress='%s' and limitDate>now()", 
475            macAddr);
476
477   /* send SQL query */
478   if (mysql_query(&mysql, queryStr)){
479     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
480             mysql_error(&mysql));
481     return FALSE;
482   }
483   return TRUE;
484 }
485
486 /******************************************
487  is the mac address used in session log
488 ******************************************/
489 int isSessionFoundInMngDb(char* macAddr){
490
491   MYSQL_RES *res=NULL;
492   MYSQL_ROW row;
493   char queryStr[BUFFMAXLN];
494   int ret;
495
496   /* prepare query string */
497   /* get row for addr.entry<session.open<addr.limit */
498   snprintf(queryStr, BUFFMAXLN, 
499            "select * from macaddrs, sessionmd "
500            " where macaddrs.macAddress=sessionmd.macAddress and "
501            " macaddrs.macAddress='%s' and "
502            " entryDate<openTime and openTime<limitDate",
503            macAddr);
504   
505   /* send SQL query */
506   if (mysql_query(&mysql, queryStr)){
507     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
508             mysql_error(&mysql));
509     return FALSE;
510   }
511   
512   /* store result */
513   res = mysql_store_result(&mysql);
514
515   /* output table name */
516   row = mysql_fetch_row(res);
517   
518   /* if found, return true */
519   if(row!=NULL) ret=TRUE;
520
521   /* if not found, return false */
522   else ret=FALSE;
523
524   mysql_free_result(res);
525   return ret;
526 }
527
528 /*******************************************
529  get next next usage log from management db
530  if end of list, return false
531 *******************************************/
532 int getNextUsageLogFromMngDb(char* userId, char* extraId, char* macAddr, char* deviceName, char* openTime, char* closeTime, char* gatewayName){
533   static MYSQL_RES *res=NULL;
534   MYSQL_ROW row;
535   char queryStr[BUFFMAXLN];
536
537   /* set default values */
538   macAddr[0]=deviceName[0]=openTime[0]=gatewayName[0]='\0';
539
540   /* if do not get result yet */
541   if(res==NULL){
542
543     /* prepare query string */
544     /* get log where addr.entry < session.open < addr.limit */
545     /*  (the same device may be registered by other users in old days) */
546     snprintf(queryStr, BUFFMAXLN, 
547              "select macaddrs.macAddress, device, openTime, closeTime, "
548              " gatewayName "
549              " from macaddrs, sessionmd "
550              " where macaddrs.macAddress=sessionmd.macAddress "
551              " and entryDate < openTime and openTime < limitDate "
552              " and (%s<closeTime or closeTime=0) "
553              " and userId='%s' and extraId='%s'",
554              GetConfValue("ShowLogAfter"), userId, extraId);
555
556     /* send SQL query */
557     if (mysql_query(&mysql, queryStr)){
558       err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
559               mysql_error(&mysql));
560       return FALSE;
561     }
562     
563     /* store result */
564     res = mysql_store_result(&mysql);
565   }
566
567   /* output table name */
568   row = mysql_fetch_row(res);
569
570   /* if found, return values */
571   if(row!=NULL){
572     strlcpy(macAddr, row[0],ADDRMAXLN);
573     strlcpy(deviceName,row[1],WORDMAXLN);
574     strlcpy(openTime,row[2],WORDMAXLN);
575     strlcpy(closeTime,row[3],WORDMAXLN);
576     strlcpy(gatewayName,row[4],WORDMAXLN);
577     return TRUE;
578   }
579   /* if not found, free memory area */
580   else{
581     mysql_free_result(res);
582     return FALSE;
583   }
584 }
585
586
587 /*******************************************
588  get next mail address near limit date
589 from management db 
590 *******************************************/
591 int getNextMailAddressFromMngDb(char* mailAddress, char* limitDate, char* device){
592
593   static MYSQL_RES *res=NULL;
594   MYSQL_ROW row;
595   char queryStr[BUFFMAXLN];
596   char* mailTiming=NULL;
597
598   /* set default values */
599   mailAddress[0]=limitDate[0]='\0';
600
601   /* if do not get result yet */
602   if(res==NULL){
603
604     /* get mail send timing from conf file */
605     mailTiming = GetConfValue("Mail/Timing");
606     if(isNull(mailTiming)) return FALSE;
607
608     /* prepare query string */
609     /* [select mailAddress,limitDate,device from macaddrs 
610         where (  date(now())=date(adddate(limitDate, interval -1 day)) 
611               OR date(now())=date(adddate(limitDate, interval -7 day))  ) 
612              AND (status='A' OR status='P') order by mailAddress,limitDate] */
613     snprintf(queryStr, BUFFMAXLN, 
614              "select mailAddress,limitDate,device from macaddrs "
615              " where (%s) and (status='A' or status='P') "
616              " order by mailAddress,limitDate", mailTiming);
617     /*       mailTiming is a condition string defined in conf file */ 
618
619     /* send SQL query */
620     if (mysql_query(&mysql, queryStr)){
621       err_msg("ERR at %s#%d: mysql query[%s]: %s",__FILE__,__LINE__,queryStr,
622               mysql_error(&mysql));
623       return FALSE;
624     }
625     
626     /* store result */
627     res = mysql_store_result(&mysql);
628   }
629
630   /* output table name */
631   row = mysql_fetch_row(res);
632
633   /* if found, return values */
634   if(row!=NULL){
635     strlcpy(mailAddress, row[0], BUFFMAXLN);
636     strlcpy(limitDate, row[1], WORDMAXLN);
637     strlcpy(device, row[2], WORDMAXLN);
638     return TRUE;
639   }
640   /* if not found, free memory area */
641   else{
642     mysql_free_result(res);
643     return FALSE;
644   }
645 }
646
647 /*******************************************
648  get the date corresponding to ShowLogAfter in conf file
649 *******************************************/
650 int getTimeRangeToShowLog(char* beginTime, char* endTime, int* dateCount){
651
652   static MYSQL_RES *res=NULL;
653   MYSQL_ROW row;
654   char queryStr[BUFFMAXLN];
655   char* showLogAfter;
656   char countStr[WORDMAXLN];
657
658   /* get conf value for beginning day for listing */
659   showLogAfter=GetConfValue("ShowLogAfter");
660   if(isNull(showLogAfter)) return FALSE;
661
662   /* set default values */
663   beginTime[0]='\0';
664   endTime[0]='\0';
665   *dateCount=0;
666
667   /* if do not get result yet */
668   if(res==NULL){
669
670     /* prepare query string */
671     /* select (adddate(now(), interval -1 month)), 
672        now(), 
673        datediff(now(),(adddate(now(), interval -1 month))) + 1; */
674     snprintf(queryStr, BUFFMAXLN, 
675              "select (%s), now(), datediff(now(),(%s)) + 1", 
676              showLogAfter, showLogAfter);
677
678     /* send SQL query */
679     if (mysql_query(&mysql, queryStr)){
680       err_msg("ERR at %s#%d: mysql query[%s]: %s",__FILE__,__LINE__,queryStr,
681               mysql_error(&mysql));
682       return FALSE;
683     }
684     
685     /* store result */
686     res = mysql_store_result(&mysql);
687   }
688
689   /* output table name */
690   row = mysql_fetch_row(res);
691
692   /* if found, return values */
693   if(row!=NULL){
694     strlcpy(beginTime, row[0], WORDMAXLN);
695     strlcpy(endTime, row[1], WORDMAXLN);
696     strlcpy(countStr, row[2], WORDMAXLN);
697     *dateCount=atoi(countStr);
698     return TRUE;
699   }
700   /* if not found, free memory area */
701   else{
702     mysql_free_result(res);
703     return FALSE;
704   }
705 }
706
707 /********************************************
708 rename the device name for indicated mac address
709 ********************************************/
710 int renameDeviceNameInMngDb(char* macAddr, char* nameStr){
711
712   char queryStr[BUFFMAXLN];
713
714   /* prepare query string */
715   snprintf(queryStr, BUFFMAXLN, 
716            "update macaddrs set device='%s' "
717            " where (status='A' or status='P') and macAddress='%s'", 
718            nameStr, macAddr);
719
720   /* send SQL query */
721   if (mysql_query(&mysql, queryStr)){
722     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
723             mysql_error(&mysql));
724     return FALSE;
725   }
726   return TRUE;
727 }
728
729 /********************************************
730 rename the mail address for indicated mac address
731 ********************************************/
732 int renameMailAddressInMngDb(char* macAddr, char* mailStr){
733
734   char queryStr[BUFFMAXLN];
735
736   /* prepare query string */
737   snprintf(queryStr, BUFFMAXLN, 
738            "update macaddrs set mailAddress='%s' "
739            " where (status='A' or status='P') and macAddress='%s'", 
740            mailStr, macAddr);
741
742   /* send SQL query */
743   if (mysql_query(&mysql, queryStr)){
744     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
745             mysql_error(&mysql));
746     return FALSE;
747   }
748   return TRUE;
749 }
750
751 /********************************************
752 Register or Update MacAddress which is unlinked to user
753 ********************************************/
754 int regOrUpNobodyMacAddr(char* macAddress){
755
756   int modified=FALSE;
757
758   /* macAddress in inactive status cannot be modified */
759   if(IsMacAddrStatusInactiveInMngDb(macAddress)) return FALSE;
760   
761   /* if mac is already registered, update it */
762   if(IsMacAddrFoundInMngDb(macAddress)){
763     if(RenewMacAddrInMngDb(macAddress)){
764       PutMacModifyLogToMngDb("?", "", macAddress, 'E'); /*(userId,extraId,.)*/
765       modified=TRUE;
766     }
767   }
768
769   /* if mac is not yet registered, register it */ 
770   else{
771     if(RegistMacAddrToMngDb(macAddress,"?","?","","")){ /*(dev,user,ext,mail) */
772         PutMacModifyLogToMngDb("?","", macAddress, 'R'); /* (userId,extraId,.) */
773       modified=TRUE;
774     }
775   }
776   return modified;
777 }
778
779 /******************************************
780  Is the macAddr  in INACTIVE('I') status
781 ******************************************/
782 int isMacAddrStatusInactiveInMngDb(char* macAddr){
783
784   MYSQL_RES *res=NULL;
785   MYSQL_ROW row;
786   char queryStr[BUFFMAXLN];
787   int ret;
788
789   /* prepare query string */
790   snprintf(queryStr, BUFFMAXLN, 
791            "select * from macaddrs "
792            " where macAddress='%s' and status='I'",
793            macAddr);
794   
795   /* send SQL query */
796   if (mysql_query(&mysql, queryStr)){
797     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
798             mysql_error(&mysql));
799     return FALSE;
800   }
801   
802   /* store result */
803   res = mysql_store_result(&mysql);
804
805   /* output table name */
806   row = mysql_fetch_row(res);
807   
808   /* if found, return true */
809   if(row!=NULL) ret=TRUE;
810
811   /* if not found, return false */
812   else ret=FALSE;
813
814   mysql_free_result(res);
815   return ret;
816 }
817
818 /********************************************
819  routines for debugging output
820 ********************************************/
821
822 int InitMngDb(void){
823   int ret;
824   if(debug>1) err_msg("DEBUG:=>initMngDb()");
825   ret=initMngDb();
826   if(debug>1) err_msg("DEBUG:(%d)<=initMngDb()",ret);
827   return ret;
828 }
829 void CloseMngDb(void){
830   if(debug>1) err_msg("DEBUG:=>closeMngDb()");
831   closeMngDb();
832   if(debug>1) err_msg("DEBUG:<=closeMngDb()");
833 }
834
835 int GetNextMacAddrFromMngDb(char* userId, char* extraId, char* macAddress, char* deviceName, char* entryDate, char* limitDate, char* status, char* mailAddress){
836   int ret;
837   if(debug>1) err_msg("DEBUG:=>getNextMacAddrFromMngDb(%s,%s)",userId,extraId);
838   ret=getNextMacAddrFromMngDb(userId,extraId,macAddress,deviceName,
839                               entryDate,limitDate, status,mailAddress);
840   if(debug>1) err_msg("DEBUG:(%d)<=getNextMacAddrFromMngDb(,,%s,%s,%s,%s,%s,%s)",
841                       ret,macAddress,deviceName,entryDate,limitDate,status,mailAddress);
842   return ret;
843 }
844
845 int RegistMacAddrToMngDb(char* macAddr, char* deviceName, char* userId, char* extraId, char* mailAddress){
846   int ret;
847   if(debug>1) err_msg("DEBUG:=>registMacAddrToMngDb(%s,%s,%s,%s,%s)",
848                       macAddr,deviceName,userId,extraId,mailAddress);
849   ret=registMacAddrToMngDb(macAddr,deviceName,userId,extraId, mailAddress);
850   if(debug>1) err_msg("DEBUG:(%d)<=registMacAddrToMngDb( )",ret);
851   return ret;
852 }
853
854 int IsMacAddrFoundInMngDb(char* macAddr){
855   int ret;
856   if(debug>1) err_msg("DEBUG:=>isMacAddrFoundInMngDb(%s)", macAddr);
857   ret=isMacAddrFoundInMngDb(macAddr);
858   if(debug>1) err_msg("DEBUG:(%d)<=isMacAddrFoundInMngDb( )",ret);
859   return ret;
860 }
861
862 int CountMacAddrsForUserInMngDb(char* userId, char* extraId){
863   int ret;
864   if(debug>1) err_msg("DEBUG:=>countMacAddrsForUserInMngDb(%s,%s)",
865                       userId,extraId);
866   ret=countMacAddrsForUserInMngDb(userId,extraId);
867   if(debug>1) err_msg("DEBUG:(%d)<=countMacAddrsForUserInMngDb( )",ret);
868   return ret;
869 }
870
871 int PutMacModifyLogToMngDb(char* userId, char* extraId, char* macAddr, char modifyType){
872   int ret;
873   if(debug>1) err_msg("DEBUG:=>putMacModifyLogToMngDb(%s,%s,%s,%c)",
874                       userId,extraId,macAddr,modifyType);
875   ret=putMacModifyLogToMngDb(userId,extraId,macAddr,modifyType);
876   if(debug>1) err_msg("DEBUG:(%d)<=putMacModifyLogToMngDb( )",ret);
877   return ret;
878 }
879
880 int GetNicVendorFromMngDb(char* macAddress, char* vendor, int bufferLength){
881   int ret;
882   if(debug>1) err_msg("DEBUG:=>getNicVendorFromMngDb(%s,,%d)",
883                       macAddress, bufferLength);
884   ret=getNicVendorFromMngDb(macAddress, vendor, bufferLength);
885   if(debug>1) err_msg("DEBUG:(%d)<=getNicVendorFromMngDb(%s)",ret, vendor);
886   return ret;
887 }
888
889
890 int CountMacModifyPerDayInMngDb(char* userId, char* extraId, char* macAddress){
891   int ret;
892   if(debug>1) err_msg("DEBUG:=>countMacModifyPerDayInMngDb(%s,%s,%s)",
893                       userId,extraId,macAddress);
894   ret=countMacModifyPerDayInMngDb(userId,extraId,macAddress);
895   if(debug>1) err_msg("DEBUG:(%d)<=countMacModifyPerDayInMngDb( )",ret);
896   return ret;
897 }
898
899 int DelMacAddrFromMngDb(char* macAddr){
900   int ret;
901   if(debug>1) err_msg("DEBUG:=>delMacAddrFromMngDb(%s)",macAddr);
902   ret=delMacAddrFromMngDb(macAddr);
903   if(debug>1) err_msg("DEBUG:(%d)<=delMacAddrFromMngDb( )",ret);
904   return ret;
905 }
906
907 int RenewMacAddrInMngDb(char* macAddr){
908   int ret;
909   if(debug>1) err_msg("DEBUG:=>renewMacAddrInMngDb(%s)",macAddr);
910   ret=renewMacAddrInMngDb(macAddr);
911   if(debug>1) err_msg("DEBUG:(%d)<=renewMacAddrinMngDb( )",ret);
912   return ret;
913 }
914
915 int PauseMacAddrInMngDb(char* macAddr){
916   int ret;
917   if(debug>1) err_msg("DEBUG:=>pauseMacAddrInMngDb(%s)",macAddr);
918   ret=pauseMacAddrInMngDb(macAddr);
919   if(debug>1) err_msg("DEBUG:(%d)<=pauseMacAddrinMngDb( )",ret);
920   return ret;
921 }
922
923 int IsSessionFoundInMngDb(char* macAddr){
924   int ret;
925   if(debug>1) err_msg("DEBUG:=>isSessionFoundInMngDb(%s)",macAddr);
926   ret=isSessionFoundInMngDb(macAddr);
927   if(debug>1) err_msg("DEBUG:(%d)<=isSessionFoundInMngDb( )",ret);
928   return ret;
929 }
930
931 int GetNextUsageLogFromMngDb(char* userId, char* extraId, char* macAddr, 
932  char* deviceName, char* openTime, char* closeTime, char* gatewayName){
933   int ret;
934   if(debug>1) err_msg("DEBUG:=>getNextUsageLogFromMngDb(%s,%s)",userId,extraId);
935   ret=getNextUsageLogFromMngDb(userId,extraId,macAddr,deviceName,
936                               openTime,closeTime,gatewayName);
937   if(debug>1) err_msg("DEBUG:(%d)<=getNextUsageLogFromMngDb(,,%s,%s,%s,%s,%s)",
938                  ret,macAddr,deviceName,openTime,closeTime,gatewayName);
939   return ret;
940 }
941
942 int GetNextMailAddressFromMngDb(char* mailAddress, char* limitDate, char*device){
943   int ret;
944   if(debug>1) err_msg("DEBUG:=>getnextMailAddressFromMngDb( )");
945   ret=getNextMailAddressFromMngDb(mailAddress, limitDate, device);
946   if(debug>1) err_msg("DEBUG:(%d)<=getNextMailAddressFromMngDb(%s,%s,%s)",
947                       ret,mailAddress,limitDate,device);
948   return ret;
949 }
950
951 int GetTimeRangeToShowLog(char* beginTime, char* endTime, int* dateCount){
952   int ret;
953   if(debug>1) err_msg("DEBUG:=>getTimeRangeToShowLog( )");
954   ret=getTimeRangeToShowLog(beginTime, endTime, dateCount);
955   if(debug>1) err_msg("DEBUG:(%d)<=getTimeRangeToShowLog(%s,%s,%d)",
956                       ret,beginTime,endTime,*dateCount);
957   return ret;
958 }
959
960 int RenameDeviceNameInMngDb(char* macAddr, char* nameStr){
961   int ret;
962   if(debug>1) err_msg("DEBUG:=>renameDevideNameInMngDb(%s,%s)",macAddr,nameStr);
963   ret=renameDeviceNameInMngDb(macAddr, nameStr);
964   if(debug>1) err_msg("DEBUG:(%d)<=renameDeviceNameInMngDb()",ret);
965   return ret;
966 }
967
968 int RenameMailAddressInMngDb(char* macAddr, char* mailStr){
969   int ret;
970   if(debug>1) err_msg("DEBUG:=>renameMailAddressInMngDb(%s,%s)",macAddr,mailStr);
971   ret=renameMailAddressInMngDb(macAddr, mailStr);
972   if(debug>1) err_msg("DEBUG:(%d)<=renameMailAddressInMngDb()",ret);
973   return ret;
974 }
975
976 int RegOrUpNobodyMacAddr(char* macAddress){
977   int ret;
978   if(debug>1) err_msg("DEBUG:=>regOrUpNobodyMacAddr(%s)",macAddress);
979   ret=regOrUpNobodyMacAddr(macAddress);
980   if(debug>1) err_msg("DEBUG:(%d)<=regOrUpNobodyMacAddr()",ret);
981   return ret;
982 }
983
984
985 int IsMacAddrStatusInactiveInMngDb(char* macAddress){
986     int ret;
987   if(debug>1) err_msg("DEBUG:=>isMacAddrStatusInactiveInMngDb(%s)",macAddress);
988   ret=isMacAddrStatusInactiveInMngDb(macAddress);
989   if(debug>1) err_msg("DEBUG:(%d)<=isMacAddrStatusInactiveInMngDb()",ret);
990   return ret;
991 }