• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-c誰得cocoaqtpythonrubywindowsphpgameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwitterdombtronvb.nettestarduinodirectxpreviewerゲームエンジン

OpengateM Source Repository


Commit MetaInfo

Revisãob8c1333ccd5ba7308f082fe6b9315c9d464cc36e (tree)
Hora2016-06-24 17:45:47
Autorwatanaby <watanaby@user...>
Commiterwatanaby

Mensagem de Log

adjust logmode and logtable

Mudança Sumário

Diff

--- a/mdsrc/managementdb.c
+++ b/mdsrc/managementdb.c
@@ -41,8 +41,8 @@ int initMngDb(void){
4141 char *user = GetConfValue("MySqlDb/User");
4242 char *password = GetConfValue("MySqlDb/Password");
4343 char *database = GetConfValue("MySqlDb/Database");
44- char *saveLogStr = GetConfValue("SaveLogToMngDb");
4544 my_bool reconnect;
45+ char *confStr;
4646
4747 /* initialize mysql */
4848 mysql_library_init(-1,NULL,NULL);
@@ -64,10 +64,14 @@ int initMngDb(void){
6464 reconnect = TRUE;
6565 mysql_options(&mysql, MYSQL_OPT_RECONNECT, &reconnect);
6666
67- /* read a flag from conf file and hold in a variable */
68- if(isNull(saveLogStr)) saveLogToMngDb = 0;
69- else saveLogToMngDb = strtol(saveLogStr, NULL, 2);
67+ /* read logmode flag from conf file and hold in a variable */
68+ confStr = GetConfValue("SaveLogToMngDb");
69+ if(isNull(confStr)) saveLogToMngDb = 0; /* no conf means no-log */
70+ else saveLogToMngDb = strtol(confStr, NULL, 2);
7071
72+ /* adjust relation of logmode in conf and logtable in mngdb */
73+ AdjustLogModeAndLogTable();
74+
7175 return TRUE;
7276 }
7377
@@ -269,25 +273,19 @@ int isAllFoundInWatchlistTable(void){
269273
270274 /* send SQL query */
271275 if (mysql_query(&mysql, queryStr)){
272-
273- /* lost of 'watchlist table' is ignored */
274- if(strcmp(mysql_error(&mysql),
275- "Table 'opengatem.watchlist' doesn't exist")!=0){
276- err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
276+ err_msg("ERR at %s#%d: mysql query: %s",__FILE__,__LINE__,
277277 mysql_error(&mysql));
278- }
279278 return ERROR;
280279 }
281280
282-
283281 res = mysql_use_result(&mysql);
284282
285283 /* output table name */
286284 row = mysql_fetch_row(res);
287285
288286 /* if not found, row is NULL */
289- if(row==NULL) found=FALSE;
290- else found=TRUE;
287+ if(row==NULL) found=FALSE;
288+ else found=TRUE;
291289
292290 mysql_free_result(res);
293291
@@ -295,8 +293,85 @@ int isAllFoundInWatchlistTable(void){
295293 return found;
296294 }
297295
296+/******************************************
297+is found table in management db
298+input=table name
299+return value: 1=found,0=not found or error
300+******************************************/
301+int isTableFoundInMngDb(char* table){
302+ MYSQL_RES *res;
303+ MYSQL_ROW row;
304+ int found=FALSE;
305+ char queryStr[BUFFMAXLN];
306+
307+ /* if no table, return false */
308+ if(isNull(table)) return FALSE;
309+
310+ /* prepare query string */
311+ snprintf(queryStr, BUFFMAXLN,
312+ "show tables like '%s'", table);
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 ERROR;
319+ }
320+ res = mysql_use_result(&mysql);
321+
322+ /* output table row */
323+ row = mysql_fetch_row(res);
324+
325+ /* row==NULL means getting no record */
326+ if(row==NULL) found=FALSE;
327+ else found=TRUE;
328+
329+ mysql_free_result(res);
330+ return found;
331+}
332+
333+/******************************************
334+set closeTime to unclosed sessions in mngdb
335+return value: 1=success, 0=error
336+******************************************/
337+int updateCloseTimeToUnclosedSessionInMngDb(void){
338+
339+ char* queryStr="update sessionmd set closeTime=now() where closeTime=0";
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+adjust the relation of logmode(SaveLogToMngDb) in conf file
353+and logtable(sessionmd) in management db
354+***********************************************************/
355+void adjustLogModeAndLogTable(void){
356+
357+ /* check the existence of table sessionmd in management.db */
358+ int isSessionTableFoundInMngDb = IsTableFoundInMngDb("sessionmd");
359+
360+ /* if logmode=off and session table exists, cleanup all sessions */
361+ if(!saveLogToMngDb && isSessionTableFoundInMngDb){
362+ UpdateCloseTimeToUnclosedSessionInMngDb();
363+ }
364+
365+ /* if logmode=on and session table does not exist, write error */
366+ if(saveLogToMngDb && !isSessionTableFoundInMngDb){
367+ err_msg("ERR at %s#%d: please create mysql table. sample script is found in archive",
368+ __FILE__,__LINE__);
369+ }
370+}
371+
372+
298373 /********************************************
299- routines for debugging output
374+ routines for debugging
300375 ********************************************/
301376 int QueryMacFromMngDb(char* macAddress, char* userId, char* extraId){
302377 int ret;
@@ -354,9 +429,31 @@ int IsAllFoundInWatchlistTable(void){
354429 }
355430
356431 int DelOldSessionLogInMngDb(void){
357- int ret;
432+ int ret;
358433 if(debug>1) err_msg("DEBUG:=>delOldSessionLogInMngDb()");
359434 ret = delOldSessionLogInMngDb();
360435 if(debug>1) err_msg("DEBUG:(%d)<=delOldSessionLogInMngDb()", ret);
361436 return ret;
362437 }
438+
439+int IsTableFoundInMngDb(char* table){
440+ int ret;
441+ if(debug>1) err_msg("DEBUG:=>isTableFoundInMngDb(%s)", table);
442+ ret = isTableFoundInMngDb(table);
443+ if(debug>1) err_msg("DEBUG:(%d)<=isTableFoundInMngDb()", ret);
444+ return ret;
445+}
446+
447+int UpdateCloseTimeToUnclosedSessionInMngDb(void){
448+ int ret;
449+ if(debug>1) err_msg("DEBUG:=>updateCloseTimeToUnclosedSessionInMngDb()");
450+ ret = updateCloseTimeToUnclosedSessionInMngDb();
451+ if(debug>1) err_msg("DEBUG:(%d)<=updateCloseTimeToUnclosedSessionInMngDb()", ret);
452+ return ret;
453+}
454+
455+void AdjustLogModeAndLogTable(void){
456+ if(debug>1) err_msg("DEBUG:=>adjustLogModeAndLogTable()");
457+ adjustLogModeAndLogTable();
458+ if(debug>1) err_msg("DEBUG:<=adjustLogModeAndLogTable()");
459+}
--- a/mdsrc/opengatemd.c
+++ b/mdsrc/opengatemd.c
@@ -246,7 +246,7 @@ int main(int argc, char **argv)
246246 /* if the address is included in watchlist, report the detection */
247247 /* watchlist is the address list needing specific reporting */
248248 /* (e.g., suspicion of illegal access). no need for normal operation */
249- if(IsFoundInWatchlistCache(macAddress)==TRUE){
249+ if(IsAddrFoundInWatchlistCache(macAddress)==TRUE){
250250 err_msg_warn("WARN: find mac=%s ip=%s", macAddress, ipAddress);
251251 }
252252
--- a/mdsrc/opengatemd.h
+++ b/mdsrc/opengatemd.h
@@ -178,6 +178,9 @@ int PutOpenToMngDb(char* macAddress);
178178 int GetNextRecordFromWatchlistTableInMngDb(char* macAddress);
179179 int IsAllFoundInWatchlistTable(void);
180180 int DelOldSessionLogInMngDb(void);
181+int IsTableFoundInMngDb(char* table);
182+int UpdateCloseTimeToUnclosedSessionInMngDb(void);
183+void AdjustLogModeAndLogTable(void);
181184
182185 /* workdb.c */
183186 int SetupSqliteBusyTimeoutValue(void);
@@ -194,7 +197,7 @@ int GetSessionTableFromWorkDb(DB* sessionTable);
194197 int PutMacInfoToWorkDb(char* macAddress, int ttl, int isNat);
195198 int GetMacInfoFromWorkDb(char* macAddress, char* detectTimeStr, int* pTtl);
196199 int IsActiveRuleInWorkDb(int ruleNumber);
197-int IsFoundMacIpPairInWorkDb(char* macAddress, char* ipAddress);
200+int IsMacIpPairFoundInWorkDb(char* macAddress, char* ipAddress);
198201 int PutMacIpPairToWorkDb(char* macAddress, char* ipAddress);
199202 int DelMacIpPairsInWorkDb(char* macAddress);
200203 int DelOldMacInfoInWorkDb(void);
@@ -239,7 +242,7 @@ int AddMacCacheItem(char* macAddress, char* userId, char* extraId, int found);
239242 /* watchlistcache.c */
240243 void InitWatchlistCache(void);
241244 int AddWatchlistCacheItem(char* macAddress);
242-int IsFoundInWatchlistCache(char* macAddress);
245+int IsAddrFoundInWatchlistCache(char* macAddress);
243246 void FreeWatchlistCache(void);
244247
245248 /* signal.c */
--- a/mdsrc/session.c
+++ b/mdsrc/session.c
@@ -478,7 +478,7 @@ void setMacIpPair(char* macAddress, char* ipAddress, char* userId, char* extraId
478478 /* if the pair is not found in work db, record it */
479479 /* the pair in work db is used for saving session detail (remove at the end of session)*/
480480 /* one session includes plural pairs (one MAC corresponds to plural IPs) */
481- if(!IsFoundMacIpPairInWorkDb(macAddress, ipAddress)){
481+ if(!IsMacIpPairFoundInWorkDb(macAddress, ipAddress)){
482482 PutMacIpPairToWorkDb(macAddress, ipAddress);
483483
484484 /* write to syslog */
--- a/mdsrc/watchlistcache.c
+++ b/mdsrc/watchlistcache.c
@@ -50,6 +50,13 @@ void initWatchlistCache(void) {
5050 terminateProg(0);
5151 }
5252
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+
5360 /* if "ALL" record is included in the table, skip address loading */
5461 foundAll=IsAllFoundInWatchlistTable();
5562 if(foundAll) return;
@@ -95,7 +102,7 @@ int addWatchlistCacheItem(char* macAddress) {
95102 if address is found in cache return true,
96103 else return error.
97104 ****************************************/
98-int isFoundInWatchlistCache(char* macAddress){
105+int isAddrFoundInWatchlistCache(char* macAddress){
99106
100107 DBT hashKey;
101108 DBT hashVal;
@@ -153,11 +160,11 @@ int AddWatchlistCacheItem(char* macAddress) {
153160 return ret;
154161 }
155162
156-int IsFoundInWatchlistCache(char* macAddress){
163+int IsAddrFoundInWatchlistCache(char* macAddress){
157164 int ret;
158- if(debug>1) err_msg("DEBUG:=>isFoundInWatchlistCache(%s)", macAddress);
159- ret = isFoundInWatchlistCache(macAddress);
160- if(debug>1) err_msg("DEBUG:(%d)<=isFoundInWatchlistCache()",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);
161168 return ret;
162169 }
163170
--- a/mdsrc/workdb.c
+++ b/mdsrc/workdb.c
@@ -462,7 +462,7 @@ int isActiveRuleInWorkDb(int ruleNumber){
462462 /********************************************
463463 Is the MAC-IP pair found in work db
464464 ********************************************/
465-int isFoundMacIpPairInWorkDb(char* macAddress, char* ipAddress){
465+int isMacIpPairFoundInWorkDb(char* macAddress, char* ipAddress){
466466 sqlite3_stmt *stmt;
467467
468468 /* SQL SELECT COMMAND, where %x is replaced in snprintf */
@@ -678,12 +678,12 @@ int GetMacInfoFromWorkDb(char* macAddress, char* detectTimeStr, int* pTtl){
678678 return ret;
679679 }
680680
681-int IsFoundMacIpPairInWorkDb(char* macAddress, char* ipAddress){
681+int IsMacIpPairFoundInWorkDb(char* macAddress, char* ipAddress){
682682 int ret;
683- if(debug>1) err_msg("DEBUG:=>isFoundMacIpPairInWorkDb(%s,%s)",
683+ if(debug>1) err_msg("DEBUG:=>isMacIpPairFoundInWorkDb(%s,%s)",
684684 macAddress,ipAddress);
685- ret=isFoundMacIpPairInWorkDb(macAddress, ipAddress);
686- if(debug>1) err_msg("DEBUG:(%d)<=isfoundMacIpPairInWorkDb( )", ret);
685+ ret=isMacIpPairFoundInWorkDb(macAddress, ipAddress);
686+ if(debug>1) err_msg("DEBUG:(%d)<=isMacIpPairFoundInWorkDb( )", ret);
687687 return ret;
688688 }
689689