OSDN Git Service

modified code to prevent DB inflation
[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   /*** insert modify log ***/
322   /* prepare query string */
323   snprintf(queryStr, BUFFMAXLN, 
324            "insert into macmodify "
325            "(userId, extraId, macAddress, modifyType, modifyDate) "
326            "values ('%s', '%s', '%s', '%c', now())",
327            userId, extraId, macAddr, modifyType);
328
329   /* send SQL query */
330   if (mysql_query(&mysql, queryStr)){
331      err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
332              mysql_error(&mysql));
333      return FALSE;
334   }
335   /*** remove old log ***/ 
336   /* prepare query string */
337   snprintf(queryStr, BUFFMAXLN, 
338            "delete from macmodify "
339            " where modifyDate < adddate(now(), interval -1 day) ");
340
341   /* send SQL query */
342   if (mysql_query(&mysql, queryStr)){
343      err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
344              mysql_error(&mysql));
345      return FALSE;
346   }
347
348   return TRUE;
349 }
350
351 /************************************************************/
352 /* The count of mac address modification in last 24 hours   */
353 /* (the modification count is checked to cope with abuse)   */
354 /* all-args:(input)                                         */
355 /* indicate either of userID&extraId or macAddress to count */ 
356 /************************************************************/
357 int countMacModifyPerDayInMngDb(char* userId, char* extraId, char* macAddress){
358
359   MYSQL_RES *res=NULL;
360   MYSQL_ROW row;
361   char queryStr[BUFFMAXLN];
362   char countStr[WORDMAXLN];
363   int count=10000;
364
365   /* prepare query string */
366   /* count for one macAddress if not null, elsecount for one userID)*/
367   if(!isNull(macAddress)){
368     snprintf(queryStr, BUFFMAXLN, 
369            "select count(*) from macmodify "
370            " where macAddress='%s' and "
371            " modifyDate > adddate(now(), interval -1 day) ",
372            macAddress);
373   }else{
374     snprintf(queryStr, BUFFMAXLN, 
375            "select count(*) from macmodify "
376            " where userId='%s' and extraId='%s' and "
377            " modifyDate > adddate(now(), interval -1 day) ",
378            userId, extraId);
379   }
380   
381   /* send SQL query */
382   if (mysql_query(&mysql, queryStr)){
383     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
384             mysql_error(&mysql));
385     return count;
386   }
387
388   /* store query result */
389   res = mysql_store_result(&mysql);
390
391   /* get a row from query result */
392   row = mysql_fetch_row(res);
393
394   /* if found, return count (if not, return large value for safe) */
395   if(row!=NULL){
396     strlcpy(countStr, row[0], WORDMAXLN);
397     count=atoi(countStr);
398   }
399
400   /* free memory area */
401   mysql_free_result(res);
402
403   return count;
404 }
405
406 /*********************************************************/
407 /* delete a mac address registered in the management db  */
408 /* actuality, not delete row, but set the status="D"     */
409 /* macAddr:(input)                                       */
410 /*********************************************************/
411 int delMacAddrFromMngDb(char* macAddr){
412
413   char queryStr[BUFFMAXLN];
414
415     /* prepare query string */
416     /* don't touch device of status="I"(set Inactive by admin) */
417     snprintf(queryStr, BUFFMAXLN, 
418              "update macaddrs set status='D',limitDate=now() "
419              " where macAddress='%s' and (status='A' or status='P')", macAddr);
420
421   /* send SQL query */
422   if (mysql_query(&mysql, queryStr)){
423     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
424             mysql_error(&mysql));
425     return FALSE;
426   }
427   return TRUE;
428 }
429
430 /******************************************/
431 /* renew the limit date for a mac address */
432 /*  in the management db                  */
433 /* macAddr:(input)                        */
434 /* LimitDate in conf file is a SQL string */
435 /* to make a future date                  */
436 /* eg, "adddate(now(),interval 1 month)"  */
437 /******************************************/
438 int renewMacAddrInMngDb(char* macAddr){
439
440   char queryStr[BUFFMAXLN];
441
442   /* prepare query string */
443   /* don't touch device set as inactive by admin */
444   snprintf(queryStr, BUFFMAXLN, 
445            "update macaddrs set status='A', renewDate=now(), limitDate=%s "
446            " where (status='A' or status='P') and macAddress='%s'", 
447            GetConfValue("LimitDate"),macAddr);
448
449   /* send SQL query */
450   if (mysql_query(&mysql, queryStr)){
451     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
452             mysql_error(&mysql));
453     return FALSE;
454   }
455   return TRUE;
456 }
457
458 /******************************************
459 pause the usage of the device for a mac address
460  registered in the management db
461 ******************************************/
462 int pauseMacAddrInMngDb(char* macAddr){
463
464   char queryStr[BUFFMAXLN];
465
466   /* prepare query string */
467   /* don't touch device set as inactive by admin */
468   snprintf(queryStr, BUFFMAXLN, 
469            "update macaddrs set status='P' "
470            " where status='A' and macAddress='%s' and limitDate>now()", 
471            macAddr);
472
473   /* send SQL query */
474   if (mysql_query(&mysql, queryStr)){
475     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
476             mysql_error(&mysql));
477     return FALSE;
478   }
479   return TRUE;
480 }
481
482 /*******************************************
483  get next mail address near limit date
484 from management db 
485 *******************************************/
486 int getNextMailAddressFromMngDb(char* mailAddress, char* limitDate, char* device){
487
488   static MYSQL_RES *res=NULL;
489   MYSQL_ROW row;
490   char queryStr[BUFFMAXLN];
491   char* mailTiming=NULL;
492
493   /* set default values */
494   mailAddress[0]=limitDate[0]='\0';
495
496   /* if do not get result yet */
497   if(res==NULL){
498
499     /* get mail send timing from conf file */
500     mailTiming = GetConfValue("Mail/Timing");
501     if(isNull(mailTiming)) return FALSE;
502
503     /* prepare query string */
504     /* [select mailAddress,limitDate,device from macaddrs 
505         where (  date(now())=date(adddate(limitDate, interval -1 day)) 
506               OR date(now())=date(adddate(limitDate, interval -7 day))  ) 
507              AND (status='A' OR status='P') order by mailAddress,limitDate] */
508     snprintf(queryStr, BUFFMAXLN, 
509              "select mailAddress,limitDate,device from macaddrs "
510              " where (%s) and (status='A' or status='P') "
511              " order by mailAddress,limitDate", mailTiming);
512     /*       mailTiming is a condition string defined in conf file */ 
513
514     /* send SQL query */
515     if (mysql_query(&mysql, queryStr)){
516       err_msg("ERR at %s#%d: mysql query[%s]: %s",__FILE__,__LINE__,queryStr,
517               mysql_error(&mysql));
518       return FALSE;
519     }
520     
521     /* store result */
522     res = mysql_store_result(&mysql);
523   }
524
525   /* output table name */
526   row = mysql_fetch_row(res);
527
528   /* if found, return values */
529   if(row!=NULL){
530     strlcpy(mailAddress, row[0], BUFFMAXLN);
531     strlcpy(limitDate, row[1], WORDMAXLN);
532     strlcpy(device, row[2], WORDMAXLN);
533     return TRUE;
534   }
535   /* if not found, free memory area */
536   else{
537     mysql_free_result(res);
538     return FALSE;
539   }
540 }
541
542 /*******************************************
543  get the date corresponding to ShowLogAfter in conf file
544 *******************************************/
545 int getTimeRangeToShowLog(char* beginTime, char* endTime, int* dateCount){
546
547   static MYSQL_RES *res=NULL;
548   MYSQL_ROW row;
549   char queryStr[BUFFMAXLN];
550   char* showLogAfter;
551   char countStr[WORDMAXLN];
552
553   /* get conf value for beginning day for listing */
554   showLogAfter=GetConfValue("ShowLogAfter");
555   if(isNull(showLogAfter)) return FALSE;
556
557   /* set default values */
558   beginTime[0]='\0';
559   endTime[0]='\0';
560   *dateCount=0;
561
562   /* if do not get result yet */
563   if(res==NULL){
564
565     /* prepare query string */
566     /* select (adddate(now(), interval -1 month)), 
567        now(), 
568        datediff(now(),(adddate(now(), interval -1 month))) + 1; */
569     snprintf(queryStr, BUFFMAXLN, 
570              "select (%s), now(), datediff(now(),(%s)) + 1", 
571              showLogAfter, showLogAfter);
572
573     /* send SQL query */
574     if (mysql_query(&mysql, queryStr)){
575       err_msg("ERR at %s#%d: mysql query[%s]: %s",__FILE__,__LINE__,queryStr,
576               mysql_error(&mysql));
577       return FALSE;
578     }
579     
580     /* store result */
581     res = mysql_store_result(&mysql);
582   }
583
584   /* output table name */
585   row = mysql_fetch_row(res);
586
587   /* if found, return values */
588   if(row!=NULL){
589     strlcpy(beginTime, row[0], WORDMAXLN);
590     strlcpy(endTime, row[1], WORDMAXLN);
591     strlcpy(countStr, row[2], WORDMAXLN);
592     *dateCount=atoi(countStr);
593     return TRUE;
594   }
595   /* if not found, free memory area */
596   else{
597     mysql_free_result(res);
598     return FALSE;
599   }
600 }
601
602 /********************************************
603 rename the device name for indicated mac address
604 ********************************************/
605 int renameDeviceNameInMngDb(char* macAddr, char* nameStr){
606
607   char queryStr[BUFFMAXLN];
608
609   /* prepare query string */
610   snprintf(queryStr, BUFFMAXLN, 
611            "update macaddrs set device='%s' "
612            " where (status='A' or status='P') and macAddress='%s'", 
613            nameStr, macAddr);
614
615   /* send SQL query */
616   if (mysql_query(&mysql, queryStr)){
617     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
618             mysql_error(&mysql));
619     return FALSE;
620   }
621   return TRUE;
622 }
623
624 /********************************************
625 rename the mail address for indicated mac address
626 ********************************************/
627 int renameMailAddressInMngDb(char* macAddr, char* mailStr){
628
629   char queryStr[BUFFMAXLN];
630
631   /* prepare query string */
632   snprintf(queryStr, BUFFMAXLN, 
633            "update macaddrs set mailAddress='%s' "
634            " where (status='A' or status='P') and macAddress='%s'", 
635            mailStr, macAddr);
636
637   /* send SQL query */
638   if (mysql_query(&mysql, queryStr)){
639     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
640             mysql_error(&mysql));
641     return FALSE;
642   }
643   return TRUE;
644 }
645
646 /********************************************
647 Register or Update MacAddress which is unlinked to user
648 ********************************************/
649 int regOrUpNobodyMacAddr(char* macAddress){
650
651   int modified=FALSE;
652
653   /* macAddress in inactive status cannot be modified */
654   if(IsMacAddrStatusInactiveInMngDb(macAddress)) return FALSE;
655   
656   /* if mac is already registered, update it */
657   if(IsMacAddrFoundInMngDb(macAddress)){
658     if(RenewMacAddrInMngDb(macAddress)){
659       PutMacModifyLogToMngDb("?", "", macAddress, 'E'); /*(userId,extraId,.)*/
660       modified=TRUE;
661     }
662   }
663
664   /* if mac is not yet registered, register it */ 
665   else{
666     if(RegistMacAddrToMngDb(macAddress,"?","?","","")){ /*(dev,user,ext,mail) */
667         PutMacModifyLogToMngDb("?","", macAddress, 'R'); /* (userId,extraId,.) */
668       modified=TRUE;
669     }
670   }
671   return modified;
672 }
673
674 /******************************************
675  Is the macAddr  in INACTIVE('I') status
676 ******************************************/
677 int isMacAddrStatusInactiveInMngDb(char* macAddr){
678
679   MYSQL_RES *res=NULL;
680   MYSQL_ROW row;
681   char queryStr[BUFFMAXLN];
682   int ret;
683
684   /* prepare query string */
685   snprintf(queryStr, BUFFMAXLN, 
686            "select * from macaddrs "
687            " where macAddress='%s' and status='I'",
688            macAddr);
689   
690   /* send SQL query */
691   if (mysql_query(&mysql, queryStr)){
692     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
693             mysql_error(&mysql));
694     return FALSE;
695   }
696   
697   /* store result */
698   res = mysql_store_result(&mysql);
699
700   /* output table name */
701   row = mysql_fetch_row(res);
702   
703   /* if found, return true */
704   if(row!=NULL) ret=TRUE;
705
706   /* if not found, return false */
707   else ret=FALSE;
708
709   mysql_free_result(res);
710   return ret;
711 }
712
713 /******************************************************/
714 /* Does the mac address belong to the userid@extraid  */
715 /* all-args:(input)                                   */
716 /* return value: true(1)=YES, false(0)=NO             */
717 /******************************************************/
718 int doesMacAddrBelongToUser(char* macAddr, char* userId, char* extraId){
719
720   MYSQL_RES *res=NULL;
721   MYSQL_ROW row;
722   char queryStr[BUFFMAXLN];
723   char existsStr[WORDMAXLN];
724   int exists=FALSE;
725
726   /* prepare query string */
727   snprintf(queryStr, BUFFMAXLN, 
728            "select exists(select * from macaddrs "
729            " where macAddress='%s' and userId='%s' "
730            " and extraId='%s' and status!='D') ",
731            macAddr, userId, extraId);
732
733   /* send SQL query */
734   if (mysql_query(&mysql, queryStr)){
735     err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
736             mysql_error(&mysql));
737     return FALSE;
738   }
739
740   /* store query result */
741   res = mysql_store_result(&mysql);
742
743   /* get a row from query result */
744   row = mysql_fetch_row(res);
745
746   /* if found, return the count */
747   if(row!=NULL){
748     strlcpy(existsStr, row[0], WORDMAXLN);
749     exists=atoi(existsStr);
750   }
751
752   /* free memory area */
753   mysql_free_result(res);
754
755   return exists;
756 }
757
758 /********************************************
759  routines for debugging output
760 ********************************************/
761
762 int InitMngDb(void){
763   int ret;
764   if(debug>1) err_msg("DEBUG:=>initMngDb()");
765   ret=initMngDb();
766   if(debug>1) err_msg("DEBUG:(%d)<=initMngDb()",ret);
767   return ret;
768 }
769 void CloseMngDb(void){
770   if(debug>1) err_msg("DEBUG:=>closeMngDb()");
771   closeMngDb();
772   if(debug>1) err_msg("DEBUG:<=closeMngDb()");
773 }
774
775 int GetNextMacAddrFromMngDb(char* userId, char* extraId, char* macAddress, char* deviceName, char* entryDate, char* limitDate, char* status, char* mailAddress){
776   int ret;
777   if(debug>1) err_msg("DEBUG:=>getNextMacAddrFromMngDb(%s,%s)",userId,extraId);
778   ret=getNextMacAddrFromMngDb(userId,extraId,macAddress,deviceName,
779                               entryDate,limitDate, status,mailAddress);
780   if(debug>1) err_msg("DEBUG:(%d)<=getNextMacAddrFromMngDb(,,%s,%s,%s,%s,%s,%s)",
781                       ret,macAddress,deviceName,entryDate,limitDate,status,mailAddress);
782   return ret;
783 }
784
785 int RegistMacAddrToMngDb(char* macAddr, char* deviceName, char* userId, char* extraId, char* mailAddress){
786   int ret;
787   if(debug>1) err_msg("DEBUG:=>registMacAddrToMngDb(%s,%s,%s,%s,%s)",
788                       macAddr,deviceName,userId,extraId,mailAddress);
789   ret=registMacAddrToMngDb(macAddr,deviceName,userId,extraId, mailAddress);
790   if(debug>1) err_msg("DEBUG:(%d)<=registMacAddrToMngDb( )",ret);
791   return ret;
792 }
793
794 int IsMacAddrFoundInMngDb(char* macAddr){
795   int ret;
796   if(debug>1) err_msg("DEBUG:=>isMacAddrFoundInMngDb(%s)", macAddr);
797   ret=isMacAddrFoundInMngDb(macAddr);
798   if(debug>1) err_msg("DEBUG:(%d)<=isMacAddrFoundInMngDb( )",ret);
799   return ret;
800 }
801
802 int CountMacAddrsForUserInMngDb(char* userId, char* extraId){
803   int ret;
804   if(debug>1) err_msg("DEBUG:=>countMacAddrsForUserInMngDb(%s,%s)",
805                       userId,extraId);
806   ret=countMacAddrsForUserInMngDb(userId,extraId);
807   if(debug>1) err_msg("DEBUG:(%d)<=countMacAddrsForUserInMngDb( )",ret);
808   return ret;
809 }
810
811 int PutMacModifyLogToMngDb(char* userId, char* extraId, char* macAddr, char modifyType){
812   int ret;
813   if(debug>1) err_msg("DEBUG:=>putMacModifyLogToMngDb(%s,%s,%s,%c)",
814                       userId,extraId,macAddr,modifyType);
815   ret=putMacModifyLogToMngDb(userId,extraId,macAddr,modifyType);
816   if(debug>1) err_msg("DEBUG:(%d)<=putMacModifyLogToMngDb( )",ret);
817   return ret;
818 }
819
820 int GetNicVendorFromMngDb(char* macAddress, char* vendor, int bufferLength){
821   int ret;
822   if(debug>1) err_msg("DEBUG:=>getNicVendorFromMngDb(%s,,%d)",
823                       macAddress, bufferLength);
824   ret=getNicVendorFromMngDb(macAddress, vendor, bufferLength);
825   if(debug>1) err_msg("DEBUG:(%d)<=getNicVendorFromMngDb(%s)",ret, vendor);
826   return ret;
827 }
828
829
830 int CountMacModifyPerDayInMngDb(char* userId, char* extraId, char* macAddress){
831   int ret;
832   if(debug>1) err_msg("DEBUG:=>countMacModifyPerDayInMngDb(%s,%s,%s)",
833                       userId,extraId,macAddress);
834   ret=countMacModifyPerDayInMngDb(userId,extraId,macAddress);
835   if(debug>1) err_msg("DEBUG:(%d)<=countMacModifyPerDayInMngDb( )",ret);
836   return ret;
837 }
838
839 int DelMacAddrFromMngDb(char* macAddr){
840   int ret;
841   if(debug>1) err_msg("DEBUG:=>delMacAddrFromMngDb(%s)",macAddr);
842   ret=delMacAddrFromMngDb(macAddr);
843   if(debug>1) err_msg("DEBUG:(%d)<=delMacAddrFromMngDb( )",ret);
844   return ret;
845 }
846
847 int RenewMacAddrInMngDb(char* macAddr){
848   int ret;
849   if(debug>1) err_msg("DEBUG:=>renewMacAddrInMngDb(%s)",macAddr);
850   ret=renewMacAddrInMngDb(macAddr);
851   if(debug>1) err_msg("DEBUG:(%d)<=renewMacAddrinMngDb( )",ret);
852   return ret;
853 }
854
855 int PauseMacAddrInMngDb(char* macAddr){
856   int ret;
857   if(debug>1) err_msg("DEBUG:=>pauseMacAddrInMngDb(%s)",macAddr);
858   ret=pauseMacAddrInMngDb(macAddr);
859   if(debug>1) err_msg("DEBUG:(%d)<=pauseMacAddrinMngDb( )",ret);
860   return ret;
861 }
862
863 int GetNextMailAddressFromMngDb(char* mailAddress, char* limitDate, char*device){
864   int ret;
865   if(debug>1) err_msg("DEBUG:=>getnextMailAddressFromMngDb( )");
866   ret=getNextMailAddressFromMngDb(mailAddress, limitDate, device);
867   if(debug>1) err_msg("DEBUG:(%d)<=getNextMailAddressFromMngDb(%s,%s,%s)",
868                       ret,mailAddress,limitDate,device);
869   return ret;
870 }
871
872 int GetTimeRangeToShowLog(char* beginTime, char* endTime, int* dateCount){
873   int ret;
874   if(debug>1) err_msg("DEBUG:=>getTimeRangeToShowLog( )");
875   ret=getTimeRangeToShowLog(beginTime, endTime, dateCount);
876   if(debug>1) err_msg("DEBUG:(%d)<=getTimeRangeToShowLog(%s,%s,%d)",
877                       ret,beginTime,endTime,*dateCount);
878   return ret;
879 }
880
881 int RenameDeviceNameInMngDb(char* macAddr, char* nameStr){
882   int ret;
883   if(debug>1) err_msg("DEBUG:=>renameDevideNameInMngDb(%s,%s)",macAddr,nameStr);
884   ret=renameDeviceNameInMngDb(macAddr, nameStr);
885   if(debug>1) err_msg("DEBUG:(%d)<=renameDeviceNameInMngDb()",ret);
886   return ret;
887 }
888
889 int RenameMailAddressInMngDb(char* macAddr, char* mailStr){
890   int ret;
891   if(debug>1) err_msg("DEBUG:=>renameMailAddressInMngDb(%s,%s)",macAddr,mailStr);
892   ret=renameMailAddressInMngDb(macAddr, mailStr);
893   if(debug>1) err_msg("DEBUG:(%d)<=renameMailAddressInMngDb()",ret);
894   return ret;
895 }
896
897 int RegOrUpNobodyMacAddr(char* macAddress){
898   int ret;
899   if(debug>1) err_msg("DEBUG:=>regOrUpNobodyMacAddr(%s)",macAddress);
900   ret=regOrUpNobodyMacAddr(macAddress);
901   if(debug>1) err_msg("DEBUG:(%d)<=regOrUpNobodyMacAddr()",ret);
902   return ret;
903 }
904
905
906 int IsMacAddrStatusInactiveInMngDb(char* macAddress){
907     int ret;
908   if(debug>1) err_msg("DEBUG:=>isMacAddrStatusInactiveInMngDb(%s)",macAddress);
909   ret=isMacAddrStatusInactiveInMngDb(macAddress);
910   if(debug>1) err_msg("DEBUG:(%d)<=isMacAddrStatusInactiveInMngDb()",ret);
911   return ret;
912 }
913
914 int DoesMacAddrBelongToUser(char* macAddr, char* userId, char* extraId){
915   int ret;
916   if(debug>1) err_msg("DEBUG:=>doesMacAddrBelongToUser(%s,%s,%s)",
917                       macAddr,userId,extraId);
918   ret=doesMacAddrBelongToUser(macAddr,userId,extraId);
919   if(debug>1) err_msg("DEBUG:(%d)<=doesMacAddrBelongToUser( )",ret);
920   return ret;
921 }