// Louis 2004-7-24
#include
#include
#include
#include
#include
#define file_name "/ var / named / master / ca" #define database "rbl" #define table_conf "configure" #define Table_Data "RBL_EFF"
// shell command # define stop_named "killall -9 name" #define start_named "/ usr / sbin / named -u named -c /etc/named.conf"
// to Trim The Blank in The Left of The Stringint Ltrim (Char * String);
INT main () {? file * fp = null ;? char data [4096] ;? char * server_ip = null ;? char * server_domain = null ;? int result ;? char * ip [4 ], * TMP = null ;? INT i ;?? // for time? time_t now ;? struct tm * now_parsed ;? char curtime [24] ;?? // for database ;? // username, password used to connection connect To Database ;? char * username ;? char * password ;?? mysql ?? * mydata ;? mysql_res? * my_res ;? mysql_row? row ;?? // itialize the buffer? MEMSET (Data, 0, Sizeof (data) ) ;? MEMSET (SQL, 0, SIZEOF (SQL)) ;? MEMSET (CURTIME, 0, SIZEOF (CURTIME));? for (i = 0; i <4; i )?? ip [i] = null ;? // open file? fp = fopen (file_name, "w") ;? IF (fp == null)? {?? syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] Open file% s error", ? File_name);? Return -1 ;?}? // t GET conf Data from Database? // 0, ready for connection ;? usrname = "root" ;? Password = "ksg123"; mydata = mysql_init ((Mysql *) 0) ;? IF (myData == null)? {? Syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] mysql_init failed");? Return -1 ;? ??}? If (my sql_real_connect (MyData, NULL, username, password, NULL, MYSQL_PORT, NULL, 0) == NULL)? {syslog (LOG_LOCAL4 |? LOG_INFO, "[RBL_REFLUSH DEBUG] Connect database Failed") ;? return -1 ;?}? ? // debug syslog???? (LOG_LOCAL4 | LOG_INFO, "[RBL_REFLUSH DEBUG] mysql_real_connect finished") ;? // 1, choose the database ;? if (! mysql_select_db (MyData, DATABASE) = 0) {syslog (LOG_LOCAL4 | LOG_INFO, "[RBL_REFLUSH DEBUG] mysql_select_db database Failed") ;? return -1 ;?} // debug syslog (LOG_LOCAL4 |?? LOG_INFO, "[RBL_REFLUSH DEBUG] mysql_select_db finished") ;? // get ip address?? Sprintf (SQL, "
Select Value from% s where name = 'ip' ", table_conf);? // get the mydata for this session? {mysql_query (mydata, sql))? {? syslog (log_local4 | log_info," [RBL_REFLUSH Debug] MySQL_QUERY, SELECT DATA from Table Faled ") ;?? Return -1 ;?} else {?? my_res = mysql_store_result (mydata) ;?? //?? // There maybe Wrong.? Row = mysql_fetch_row (my_res); ?????? ration (server_ip == null)? {?? syslog (log_local4 | log_info, "[rbl_reflush debug] mysql_fetch_row, get ip failed"); ?? return -1; ?} else {? // NEED TO TRIM THE blank in the left? ???????? IF (LTRIM (Server_IP)! = 0)? ?????? syslog (log_local4 | log_info, " RBL_REFLUSH DEBUG] Trim server_ip Failed ") ;?}} // debug syslog (LOG_LOCAL4 |????? LOG_INFO," [RBL_REFLUSH DEBUG] mysql_fetch_row get IP finished ") ;? // get domain name sprintf (sql,?? "SELECT VALUE FROM% s where name = 'dnsname'", table_conf) ;? // get the mydata for this session? f (mysql_query (mydata, sql))? {? syslog (log_local4 | log_info, "[RBL_REFLUSH Debug ] mysql_qu ERY, SELECT DATA from Table Faled ") ;?? Return -1 ;?} else {?? my_res = mysql_store_Result (mydata); row = mysql_fetch_row (my_res); server_domain = row [0] ;?? IF (Server_domain == NULL)? {?? syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] mysql_fetch_row, get domain failed"); ?? Return -1;?} else {? // need to Trim To Blank in The Left ?? IF (Ltrim (Server_Domain)! = 0) ??????? ?? syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] Trim Server_Domain failed");???}? ?? // debug? syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] mysql_fetch_row get domain finished");
syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] Server IP IS% S, LENGTH IS% D", Server_IP, Strlen (Server_IP)) ;? Syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] Server Domain IS% s, Length is% d ", server_domain, strlen (server_domain));? // Write the Changeless Content (Data) to file ;? Sprintf (data," $ TTL 2W1D / N @ in SOA% s. root .% s. (/ N1000; serial / n2h; refresh / N30m; /??? Retry / N2W1D; EXPIRY / N1H); minimum / n ", server_domain, server_domain); rsult = fputs (data, fp);? IF (result == EOF)? {?? syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] Write File% S Error" ,? file_name);? Return -1 ;? ??}? sprintf (data, "@ IN NS M5 / N? IN A% S / N% s IN A% S /N2.0.0.127 in A 127.0.0.2 / n ", /??? Server_ip, server_domain, server_ip) ;? Result = fputs (data , fp) ;? if (result == EOF)? {?? syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] Write File% S Error",? file_name);? Return -1;? ??}? // Write the change (from Database) to file? // 2, get data From specific table ;? // 2.1, get the local time? Time (& now) ;? NOW_PARSED = localtime (now); sprintf (curtime, "% .4d -%. 2D -%. 2D% .2D:%. 2D:%. 2D ", ??????????? NOW_PARSED-> TM_YEAR 1900, now_PARSED-> TM_MON 1, ???????????? NOW_PARSED-> TM_MDAY, NOW_PARSED- > TM_HOUR, ???????????? NOW_PARSED-> TM_MIN, NOW_PARSED-> TM_SEC); ???????????????????????????????????????????????????????????????. f RBL_REFLUSH Debug] Current Time IPUG] CURRENT TIME IS% S ", CURTIME); ??? // 2.2, create the sql? Sprintf (SQL," SELECT DIM ROM RBL_EFF WHERE Stoptime> '% s' ", CURTIME);
? // 2.3? EXECUTE THE SQL? IF (MySQL_QUERY (MYDATA, SQL))? {?? Syslog (log_local4 | log_info, "[RBL_REFLUSH Debug] mysql_query, select data from table failed");? Return -1;? } else {? // get the result?? my_res = mysql_store_result (mydata); //??? (r_ = mysql_fetch_row (my_res)) {? ?? // for debug ?? syslog (log_local4 | Log_info, "[RBL_REFLUSH Debug] Fetch The Row:% S", Row [0]); ?? // Divide The Row, Fill Into the Array ?? IP [0] = STRTOK (Row [0], "."); ??? = 1;?? While (tmp = strtok (null, "))) {? ??? ip [i] = TMP;? ??? i = i ;?? ?} ?? // 2.4 Write the result to file? ?? Sprintf (data, "% s.% S.% S.% S in a 127.0.0.2/N", IP [3], IP [2] , IP [1], IP [0]);?? result = fputs (data, fp); ?? if (result == EOF) ?? {? ??? syslog (log_local4 | log_info, "[ RBL_REFLUSH Debug] Write File% S Error ",? File_name);? ??? Return -1;?? ??} ?? // for debug? ?? syslog (log_local4 | log_info," [RBL_REFLUSH Debug] AFTER STRTOK, IP Address IS:% S ", DATA);?}?}? // close the file? fclose (fp);? // restart the na Med Service, Do Not Handle The Return Value of System ()? system (stop_named) ;? system (start_named) ;? Return 0;}
INT LTRIM (Char * String) {? char t [128]; ??????? f (! Strlen (String)) Return -1;
• STRCPY (T, String) ;? buf = T;
While (* buf == '' && strlen (BUF) && (* buf <49) && (* buf> 57)) ??? * buf ;? strcpy (string, buf) ;? Return 0;}