1 /**************************************************
2 OpengateM - a MAC address authentication system
3 module to control cache for watchlist
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
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.
13 Copyright (C) 2014 Opengate Project Team
14 Written by Yoshiaki Watanabe
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.
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.
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.
30 Email: watanaby@is.saga-u.ac.jp
31 **************************************************/
32 #include "opengatemd.h"
34 /* HashTable to store MacAddress->none */
35 static DB* watchlistHash;
37 /* specific record is included in the watchlist table */
38 static int foundAll=FALSE;
39 static int cacheItemCount=0;
41 /****************************************
42 initialize watchlist Cache and load from MySQL
43 ****************************************/
44 void initWatchlistCache(void) {
45 char macAddress[ADDRMAXLN];
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__);
53 /* if watchlist table is not found in management db, set no cache */
54 if(!IsTableFoundInMngDb("watchlist")){
60 /* if "ALL" record is included in the table, skip address loading */
61 foundAll=IsAllFoundInWatchlistTable();
64 /* read MySQL and insert to hash */
65 while(GetNextRecordFromWatchlistTableInMngDb(macAddress)){
66 AddWatchlistCacheItem(macAddress);
71 /****************************************
72 add an item to watchlist cache
73 ****************************************/
74 int addWatchlistCacheItem(char* macAddress) {
78 char hashValueStr[]="";
80 /* check address format */
81 if(isNull(macAddress)) return FALSE;
82 if(!ReFormatMacAddr(macAddress)) return FALSE;
84 /** setup hash key **/
85 /* hash key : string of mac address */
86 hashKey.data = macAddress;
87 hashKey.size = strlen(macAddress) + 1;
89 /** setup hash value **/
91 hashVal.data = hashValueStr;
93 if(watchlistHash->put(watchlistHash, &hashKey, &hashVal, 0) == -1) {
94 err_msg("ERR at %s#%d: fail to put into hash table",__FILE__,__LINE__);
101 /****************************************
102 if address is found in cache return true,
104 ****************************************/
105 int isAddrFoundInWatchlistCache(char* macAddress){
111 /* if cache is empty, return false */
112 if(cacheItemCount==0) return FALSE;
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;
119 /* if null or illegal form, return */
120 if(isNull(macAddress)) return ERROR;
121 if(!ReFormatMacAddr(macAddress)) return ERROR;
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);
129 /* get is failed, return false */
130 if(ret!=0) return FALSE;
132 /* get is successed */
136 /****************************************
137 Memory free for watchlist Cache
138 ****************************************/
139 void freeWatchlistCache(void) {
141 watchlistHash->close(watchlistHash);
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( )");
154 int AddWatchlistCacheItem(char* macAddress) {
156 if(debug>1) err_msg("DEBUG:=>addWatchlistCacheItem(%s)",
158 ret = addWatchlistCacheItem(macAddress);
159 if(debug>1) err_msg("DEBUG:(%d)<=addWatchlistCacheItem( )",ret);
163 int IsAddrFoundInWatchlistCache(char* macAddress){
165 if(debug>1) err_msg("DEBUG:=>isAddrFoundInWatchlistCache(%s)", macAddress);
166 ret = isAddrFoundInWatchlistCache(macAddress);
167 if(debug>1) err_msg("DEBUG:(%d)<=isAddrFoundInWatchlistCache()",ret);
171 void FreeWatchlistCache(void) {
172 if(debug>1) err_msg("DEBUG:=>freeWatchlistCache()");
173 freeWatchlistCache();
174 if(debug>1) err_msg("DEBUG:<=freeWatchlistCache()");