OSDN Git Service

adjust logmode and logtable
[opengatem/opengatem.git] / mdsrc / watchlistcache.c
1 /**************************************************
2 OpengateM - a MAC address authentication system
3  module to control cache for watchlist
4
5   the cache holds the temporary copy of watchlist table existing in Management DB.
6   the cache is prepared at reloading the daemon.
7   Implemented with HashTable.
8   HashTable: Key= MAC Address, Val= none
9
10  The watchlist table has the data for specific syslog reporting
11  When a address in the list is used, syslog message with 'WARNING' property is write out.
12
13 Copyright (C) 2014 Opengate Project Team
14 Written by Yoshiaki Watanabe
15
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29
30 Email: watanaby@is.saga-u.ac.jp
31 **************************************************/
32 #include "opengatemd.h"
33
34 /* HashTable to store MacAddress->none */
35 static DB* watchlistHash;
36
37 /* specific record is included in the watchlist table */
38 static int foundAll=FALSE;
39 static int cacheItemCount=0;
40
41 /****************************************
42 initialize watchlist Cache and load from MySQL
43 ****************************************/
44 void initWatchlistCache(void) {
45   char macAddress[ADDRMAXLN];
46
47   /* prepare hash table */
48   if((watchlistHash = dbopen(NULL, O_CREAT | O_RDWR, 0644, DB_HASH, NULL)) == NULL) {
49     err_msg("ERR at %s#%d: fail to open watchlist hash table",__FILE__,__LINE__);
50     terminateProg(0);
51   }
52
53   /* if watchlist table is not found in management db, set no cache */
54   if(!IsTableFoundInMngDb("watchlist")){
55     foundAll=FALSE;
56     cacheItemCount=0;
57     return;
58   }
59   
60   /* if "ALL" record is included in the table, skip address loading */
61   foundAll=IsAllFoundInWatchlistTable();    
62   if(foundAll) return;
63
64   /* read MySQL and insert to hash */
65   while(GetNextRecordFromWatchlistTableInMngDb(macAddress)){
66     AddWatchlistCacheItem(macAddress);
67     cacheItemCount++;
68   }
69 }
70
71 /****************************************
72 add an item to watchlist cache 
73 ****************************************/
74 int addWatchlistCacheItem(char* macAddress) {
75
76   DBT hashKey;
77   DBT hashVal;
78   char hashValueStr[]="";
79
80   /* check address format */
81   if(isNull(macAddress)) return FALSE;
82   if(!ReFormatMacAddr(macAddress)) return FALSE;
83
84   /** setup hash key **/
85   /* hash key : string of mac address  */  
86   hashKey.data = macAddress;
87   hashKey.size = strlen(macAddress) + 1;
88
89   /** setup hash value **/
90   /* hash value : "" */
91   hashVal.data = hashValueStr;
92   hashVal.size = 1;    
93   if(watchlistHash->put(watchlistHash, &hashKey, &hashVal, 0) == -1) {
94     err_msg("ERR at %s#%d: fail to put into hash table",__FILE__,__LINE__);
95     return FALSE;
96   }
97
98   return TRUE;
99 }
100
101 /****************************************
102 if address is found in cache return true,
103 else return error.
104 ****************************************/
105 int isAddrFoundInWatchlistCache(char* macAddress){
106
107   DBT hashKey;
108   DBT hashVal;
109   int ret;
110
111   /* if cache is empty, return false */
112   if(cacheItemCount==0) return FALSE;
113
114   /* if watchlist table includes 'ALL' record, return true */
115   /* if searching the 'ALL' record is failed, return error */
116   if(foundAll==TRUE) return TRUE;
117   if(foundAll==ERROR) return ERROR;
118
119   /* if null or illegal form, return */
120   if(isNull(macAddress)) return ERROR;
121   if(!ReFormatMacAddr(macAddress)) return ERROR;
122
123   /***** get hashed item matched to the indicated mac */
124   hashKey.data = macAddress;
125   hashKey.size = strlen(macAddress) + 1;
126   memset(&hashVal, 0, sizeof(DBT));
127   ret=watchlistHash->get(watchlistHash, &hashKey, &hashVal, 0);
128
129   /* get is failed, return false */
130   if(ret!=0) return FALSE;
131
132   /* get is successed */
133   return TRUE;
134 }
135
136 /****************************************
137 Memory free for watchlist Cache
138 ****************************************/
139 void freeWatchlistCache(void) {
140
141   watchlistHash->close(watchlistHash);
142 }
143
144
145 /****************************************************
146  routines for debugging putput
147  ***************************************************/
148 void InitWatchlistCache(void) {
149   if(debug>1) err_msg("DEBUG:=>initWatchlistCache( )");
150   initWatchlistCache();
151   if(debug>1) err_msg("DEBUG:<=initWatchlistCache( )");
152 }
153
154 int AddWatchlistCacheItem(char* macAddress) {
155   int ret;
156   if(debug>1) err_msg("DEBUG:=>addWatchlistCacheItem(%s)",
157                       macAddress);
158   ret = addWatchlistCacheItem(macAddress);
159   if(debug>1) err_msg("DEBUG:(%d)<=addWatchlistCacheItem( )",ret);
160   return ret;
161 }
162
163 int IsAddrFoundInWatchlistCache(char* macAddress){
164   int ret;
165   if(debug>1) err_msg("DEBUG:=>isAddrFoundInWatchlistCache(%s)", macAddress);
166   ret = isAddrFoundInWatchlistCache(macAddress);
167   if(debug>1) err_msg("DEBUG:(%d)<=isAddrFoundInWatchlistCache()",ret);
168   return ret;
169 }
170
171 void FreeWatchlistCache(void) {
172   if(debug>1) err_msg("DEBUG:=>freeWatchlistCache()");
173   freeWatchlistCache();
174   if(debug>1) err_msg("DEBUG:<=freeWatchlistCache()");
175 }