fail2ban_init.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ============================================================================
  3. Name : generate_paging_conf.sh
  4. Author : ssc
  5. Version : v1.0
  6. Copyright : ZYCOO copyright
  7. Description : Generate paging info from mysql to paging conf file
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <assert.h>
  15. #include <time.h>
  16. #include <ctype.h>
  17. #include <mysql/mysql.h>
  18. MYSQL *g_conn; // mysql 连接
  19. MYSQL_RES *g_res; // mysql group记录集
  20. MYSQL_ROW g_row; // 字符串数组,mysql 记录行
  21. MYSQL_RES *d_res; // mysql device记录集
  22. MYSQL_ROW d_row; // 字符串数组,mysql 记录行
  23. #define NORMAL_SIZE 256
  24. #define MAX_SIZE 2048
  25. #define MIDLE_SIZE 512
  26. #define MINI_SIZE 64
  27. #define CONFIG_FILE "/etc/fail2ban/jail.conf"
  28. #define KEYVALLEN 100
  29. #define VERSION "V1.0.1"
  30. #define FAIL2BAN_BASIC_SQL "select name,enable,max_retry,find_time,ban_time from t_pbx_fail2ban_basic"
  31. #define FAIL2BAN_SIP_IGNORED_SQL "select ip,netmask_length from t_pbx_fail2ban_ignored where protocol_sip='1' and enable='1'"
  32. #define FAIL2BAN_SSH_IGNORED_SQL "select ip,netmask_length from t_pbx_fail2ban_ignored where protocol_ssh='1' and enable='1'"
  33. char g_host_name[MINI_SIZE];
  34. char g_user_name[MINI_SIZE];
  35. char g_password[MINI_SIZE];
  36. char g_db_name[MINI_SIZE];
  37. const unsigned int g_db_port = 3306;
  38. char * mytime(){
  39. time_t my_time;
  40. time(&my_time);
  41. char *time_string = ctime(&my_time);
  42. if (time_string[strlen(time_string) - 1] == '\n')
  43. {
  44. time_string[strlen(time_string) - 1] = '\0';
  45. }
  46. return time_string;
  47. }
  48. void print_mysql_error(const char *msg) { // 打印最后一次错误
  49. if (msg)
  50. printf("%s: %s\n", msg, mysql_error(g_conn));
  51. else
  52. puts(mysql_error(g_conn));
  53. }
  54. int executesql(const char * sql) {
  55. /*query the database according the sql*/
  56. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  57. return -1; // 表示失败
  58. return 0; // 成功执行
  59. }
  60. int init_mysql() { // 初始化连接
  61. // init the database connection
  62. g_conn = mysql_init(NULL);
  63. /* connect the database */
  64. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  65. return -1;
  66. // 是否连接已经可用
  67. if (executesql("set names utf8")) // 如果失败
  68. return -1;
  69. return 0; // 返回成功
  70. }
  71. int main(int argc, char **argv) {
  72. char in[8] = {0};
  73. char tmp[MIDLE_SIZE] = {0};
  74. char ignored[MIDLE_SIZE] = {0};
  75. char cmd[MIDLE_SIZE] = {0};
  76. strcpy(g_host_name,getenv("MYSQL"));
  77. strcpy(g_user_name,getenv("MYSQL_USER"));
  78. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  79. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  80. if (init_mysql()){
  81. print_mysql_error(NULL);
  82. exit(1);
  83. }
  84. if (executesql(FAIL2BAN_BASIC_SQL)){
  85. print_mysql_error(NULL);
  86. exit(1);
  87. }
  88. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  89. FILE *conf_fail2ban_fp = fopen(CONFIG_FILE, "w+");
  90. if (conf_fail2ban_fp == NULL){
  91. perror("Open paging conf file Error: ");
  92. exit(1);
  93. }
  94. fprintf(conf_fail2ban_fp, "[DEFAULT]\n\
  95. ignoreip = 127.0.0.1/32\n\
  96. bantime = 3600\n\
  97. maxretry = 3\n\
  98. backend = auto\n\
  99. banaction = iptables-multiport\n\
  100. mta = mail\n\
  101. protocol = tcp\n\
  102. chain = INPUT\n\
  103. action_ = %%(banaction)s[name=%%(__name__)s, port=\"%%(port)s\", protocol=\"%%(protocol)s\", chain=\"%%(chain)s\"]\n\
  104. action_mw = %%(banaction)s[name=%%(__name__)s, port=\"%%(port)s\", protocol=\"%%(protocol)s\", chain=\"%%(chain)s\"]\n\
  105. action_mwl = %%(banaction)s[name=%%(__name__)s, port=\"%%(port)s\", protocol=\"%%(protocol)s\", chain=\"%%(chain)s\"]\n\
  106. action = %%(action_)s\n\n\
  107. "\
  108. );
  109. while ((g_row=mysql_fetch_row(g_res)))
  110. { // 打印结果集
  111. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL || g_row[3] == NULL || g_row[4] == NULL)
  112. {
  113. printf("some feild is empty!\n");
  114. continue;
  115. }
  116. if(strcmp((const char *)g_row[1], "1") == 0)
  117. strcpy(in, "true");
  118. else
  119. strcpy(in, "false");
  120. if(strcmp((const char*)g_row[0], "sip") == 0){
  121. if (executesql(FAIL2BAN_SIP_IGNORED_SQL)){
  122. print_mysql_error(NULL);
  123. exit(1);
  124. }
  125. d_res = mysql_store_result(g_conn);
  126. memset(ignored,0,sizeof(ignored));
  127. while(d_row = mysql_fetch_row(d_res))
  128. {
  129. strcat(ignored,(char *)d_row[0]);
  130. strcat(ignored,"/");
  131. strcat(ignored,(char *)d_row[1]);
  132. strcat(ignored," ");
  133. }
  134. fprintf(conf_fail2ban_fp, "[sip-iptables]\n\
  135. enabled = %s\n\
  136. ignoreip = 127.0.0.1/32 %s \n\
  137. filter = sip\n\
  138. action = iptables-allports[name=VOIP, protocol=all]\n\
  139. logpath = /var/log/asterisk/messages\n\
  140. maxretry = %s\n\
  141. findtime = %s\n\
  142. bantime = %s\n\n\
  143. ",\
  144. in, ignored, g_row[2], g_row[3], g_row[4]
  145. );
  146. mysql_free_result(d_res);
  147. }
  148. else if(strcmp((const char*)g_row[0], "ssh") == 0)
  149. {
  150. if (executesql(FAIL2BAN_SSH_IGNORED_SQL)){
  151. print_mysql_error(NULL);
  152. exit(1);
  153. }
  154. d_res = mysql_store_result(g_conn);
  155. memset(ignored,0,sizeof(ignored));
  156. while(d_row = mysql_fetch_row(d_res))
  157. {
  158. strcat(ignored,(char *)d_row[0]);
  159. strcat(ignored,"/");
  160. strcat(ignored,(char *)d_row[1]);
  161. strcat(ignored," ");
  162. }
  163. fprintf(conf_fail2ban_fp, "[SSH]\n\
  164. enabled = %s\n\
  165. ignoreip = 127.0.0.1/32 %s \n\
  166. port = 22\n\
  167. filter = sshd\n\
  168. logpath = /init/logs/auth.log\n\
  169. maxretry = %s\n\
  170. findtime = %s\n\
  171. bantime = %s\n\n\
  172. ",\
  173. in, ignored, g_row[2], g_row[3], g_row[4]
  174. );
  175. mysql_free_result(d_res);
  176. }
  177. }
  178. fclose(conf_fail2ban_fp);
  179. mysql_free_result(g_res); // 释放结果集
  180. mysql_close(g_conn); // 关闭链接
  181. }