OSDN Git Service

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