1 /**************************************************
2 OpengateM - a MAC address authentication system
3 module to control cache for MAC address DB
5 the cache holds the temporary copy of DB to speedup checking.
6 Only the allowable MAC addresses are hold.
8 As ip address check by database is time consuming procedure,
9 the recently checked mac addresses are cached.
10 Implemented with HashTable.
13 Val= userId, extraId and cache time.
14 If MAC address is found in table and time is new, skip DB access.
15 If time is old, remove the cached item.
17 Copyright (C) 2012 Opengate Project Team
18 Written by Yoshiaki Watanabe
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License
22 as published by the Free Software Foundation; either version 2
23 of the License, or (at your option) any later version.
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with this program; if not, write to the Free Software
32 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 Email: watanaby@is.saga-u.ac.jp
35 **************************************************/
36 #include "opengatemd.h"
38 /* HashTable to store MacAddress->userId,extraId,Time */
41 /* Cache Timeout(seconds) (read from conf file) */
42 static int macCacheTimeout;
44 void dumpHashTable(DB* table);
46 /****************************************
47 add item to Mac cache(hash table:macHashDb)
48 key=macaddress, value="<found>SPACE<unixtime>SPACE<userId>SPACE<extraId>"
49 <found>: TRUE=found in DB, FALSE=not found in DB
50 ****************************************/
51 int addMacCacheItem(char* macAddress, char* userId, char* extraId, int found) {
55 char hashValueStr[BUFFMAXLN];
57 /* check address format */
58 if(isNull(macAddress)) return FALSE;
59 if(!ReFormatMacAddr(macAddress)) return FALSE;
61 /** setup hash key **/
62 /* hash key : string of mac address */
63 hashKey.data = macAddress;
64 hashKey.size = strlen(macAddress) + 1;
66 /** setup hash value **/
67 /* hash value : string "<found>SPACE<unixtime>SPACE<userId>SPACE<extraId>" */
68 snprintf(hashValueStr,BUFFMAXLN,"%d %ld %s %s",
69 found,time(NULL),userId,extraId);
70 hashVal.data = hashValueStr;
71 hashVal.size = strlen(hashValueStr) + 1;
72 if(macHashDb->put(macHashDb, &hashKey, &hashVal, 0) == -1) {
73 err_msg("ERR at %s#%d: fail to put into hash table",__FILE__,__LINE__);
80 /****************************************
81 query userid and extraid for mac address
82 if the address is in cache, return TRUE(found in DB) or FALSE(not found).
83 if not in cache, or too old, or error, return ERROR.
84 ****************************************/
85 int queryMacFromMacCache(char* macAddress, char* userId, char* extraId){
95 userId[0]=extraId[0]='\0';
97 /* if null or illegal form, return */
98 if(isNull(macAddress)) return ERROR;
99 if(!ReFormatMacAddr(macAddress)) return ERROR;
101 /***** get hashed item matched to the indicated mac */
102 hashKey.data = macAddress;
103 hashKey.size = strlen(macAddress) + 1;
104 memset(&hashVal, 0, sizeof(DBT));
105 ret=macHashDb->get(macHashDb, &hashKey, &hashVal, 0);
107 /* get is failed, return false */
108 if(ret!=0) return ERROR;
110 /* get is successed */
111 /* pick up the hash values */
112 ret=sscanf(hashVal.data,"%d %ld %s %s",&found,&entryTime,userId,extraId);
114 /* found, entryTime should be obtained */
115 if(ret<=1) return ERROR;
117 /* if entry time is older than timeout, return false */
118 if( entryTime + macCacheTimeout < time(NULL) ) return ERROR;
120 /* return the mac db access result (found/not found) */
124 /****************************************
126 ****************************************/
127 void initMacCache(void) {
129 /* prepare hash table */
130 if((macHashDb = dbopen(NULL, O_CREAT | O_RDWR, 0644, DB_HASH, NULL)) == NULL) {
131 err_msg("ERR at %s#%d: fail to open mac hash table",__FILE__,__LINE__);
135 /* set counter and timeout parameter */
136 if(isNull(GetConfValue("MacCacheTimeout"))){
137 err_msg("ERR at %s#%d: cannot get MacCacheTimeout from conf file",__FILE__,__LINE__);
140 macCacheTimeout=atoi(GetConfValue("MacCacheTimeout"));
144 /****************************************
145 Memory free for Mac Cache
146 ****************************************/
147 void freeMacCache(void) {
149 macHashDb->close(macHashDb);
152 /****************************************
153 delete item from Mac cache matched to the mac address
154 ****************************************/
155 int delMacCacheItem(char* macAddress) {
159 /* if null or illegal form, return */
160 if(isNull(macAddress)) return FALSE;
161 if(!ReFormatMacAddr(macAddress)) return FALSE;
163 /* delete the item from Hash Table */
164 hashKey.data = macAddress;
165 hashKey.size = strlen(macAddress) + 1;
166 macHashDb->del(macHashDb, &hashKey, 0);
171 /************************************
172 dump print of hash table to syslog
173 (debug routine for hash table)
174 ************************************/
175 void dumpHashTable(DB* table){
180 memset(&hashKey, 0, sizeof(DBT));
181 memset(&hashVal, 0, sizeof(DBT));
182 ret=table->seq(table, &hashKey, &hashVal, R_FIRST);
185 err_msg("%s:%s", (char*)hashKey.data, (char*)hashVal.data);
188 ret=table->seq(table, &hashKey, &hashVal, R_NEXT);
192 /****************************************************
193 routines for debugging putput
194 ***************************************************/
196 void InitMacCache(void) {
197 if(debug>1) err_msg("DEBUG:=>initmacCache( )");
199 if(debug>1) err_msg("DEBUG:<=initMacCache( )");
202 void FreeMacCache(void) {
203 if(debug>1) err_msg("DEBUG:=>freemacCache()");
205 if(debug>1) err_msg("DEBUG:<=freeMacCache()");
208 int AddMacCacheItem(char* macAddress, char* userId, char* extraId, int found) {
210 if(debug>1) err_msg("DEBUG:=>addMacCacheItem(%s,%s,%s,%d)", macAddress,userId,extraId,found);
211 ret = addMacCacheItem(macAddress,userId,extraId,found);
212 if(debug>1) err_msg("DEBUG:(%d)<=addMacCacheItem( )",ret);
216 int QueryMacFromMacCache(char* macAddress, char* userId, char* extraId){
218 if(debug>1) err_msg("DEBUG:=>queryMacFromMacCache(%s)", macAddress);
219 ret = queryMacFromMacCache(macAddress, userId, extraId);
220 if(debug>1) err_msg("DEBUG:(%d)<=queryMacFromMacCache(,%s,%s)",ret,userId,extraId);
224 int DelMacCacheItem(char* macAddress) {
226 if(debug>1) err_msg("DEBUG:=>delMacCacheItem(%s)", macAddress);
227 ret = delMacCacheItem(macAddress);
228 if(debug>1) err_msg("DEBUG:(%d)<=delMacCacheItem( )",ret);