與named相關

// 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 named"
#define START_NAMED "/usr/sbin/named -u named -c /etc/named.conf"

// To trim the blank in the left of the string
int LTrim(char *string);

int main()
{
? FILE *fp = NULL;
? char data[4096];
? char sql[512];
? 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 connect to database;
? char *username;
? char *password;
?
? MYSQL??*MyData;
? MYSQL_RES?*My_Res;
? MYSQL_ROW?row;
?
? // initialize 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;
? }
? // to get conf data from database
? // 0, ready for connect database;
? username = "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( mysql_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
? if(mysql_query(MyData, sql))
? {
? ?syslog (LOG_LOCAL4 | LOG_INFO, "[RBL_REFLUSH DEBUG] mysql_query, select data from table Failed");
? ?return -1;
? }else{
? ?My_Res = mysql_store_result(MyData);
? ?//
? ?// there maybe wrong.
? ?row = mysql_fetch_row(My_Res);
? ?server_ip = row[0];
? ?if(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
? if(mysql_query(MyData, sql))
? {
? ?syslog (LOG_LOCAL4 | LOG_INFO, "[RBL_REFLUSH DEBUG] mysql_query, select data from table Failed");
? ?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 the 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(depend on the conf 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);
? result = 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 content(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);
???????????
? // for debug
? syslog (LOG_LOCAL4 | LOG_INFO, "[RBL_REFLUSH DEBUG] Current time is %s",? curtime);?
? // 2.2, create the sql
? sprintf(sql, "select distinct ip from 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);
? ?// read row
? ?while(row = 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], ".");
? ??i = 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 named service, do not handle the return value of system()
? system(STOP_NAMED);
? system(START_NAMED);
?
? return 0;
}

int LTrim(char *string)
{
?char t[128];???
?char *buf;????
?
?if (!strlen(string)) return -1;

?strcpy(t,string);
?buf=t;

?while (*buf == ' ' && strlen(buf) && (*buf < 49) && (*buf >57))?
??*buf++;
?strcpy(string,buf);
?return 0;
}

發佈了23 篇原創文章 · 獲贊 2 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章