/
mysql2dns.c
204 lines (166 loc) · 5.12 KB
/
mysql2dns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <syslog.h>
#include <unistd.h>
#include <fcntl.h>
#include <paths.h>
int change_serials(MYSQL *dbh, char *serials, int len) {
MYSQL_RES *res;
MYSQL_ROW row;
if (mysql_query(dbh, "SELECT SUM(`serial`) FROM `domains`")) {
syslog(LOG_ERR, "%s", mysql_error(dbh));
return 0;
}
if (!(res = mysql_store_result(dbh))) {
syslog(LOG_ERR, "%s", mysql_error(dbh));
return 0;
}
if ((row = mysql_fetch_row(res)) && strcmp(row[0], serials)) {
snprintf(serials, len, "%s", row[0]);
mysql_free_result(res);
return 1;
}
return 0;
}
void update_zones(MYSQL *dbh, char *directory) {
MYSQL_RES *res1;
MYSQL_ROW row1;
FILE *zones;
char filename[256];
snprintf(filename, sizeof(filename), "%s/named.zones", directory);
if (!(zones = fopen(filename, "w"))) {
syslog(LOG_ERR, "Error opening file %s for writing", filename);
return;
}
fprintf(zones, "#\n# Automatically generated by mysql2dns - DO NOT EDIT!\n#\n\n");
if (mysql_query(dbh, "SELECT `id`, `domain`, `ttl`, `class`, `zonemaster`, `adminmailbox`, "
"`serial`, `refresh`, `retry`, `expire`, `negative` FROM `domains` ORDER BY `domain`")) {
syslog(LOG_ERR, "%s", mysql_error(dbh));
return;
}
if (!(res1 = mysql_store_result(dbh))) {
syslog(LOG_ERR, "%s", mysql_error(dbh));
return;
}
while ((row1 = mysql_fetch_row(res1))) {
MYSQL_RES *res2;
MYSQL_ROW row2;
FILE *db;
char query[256];
fprintf(zones, "zone \"%s\" %s {\n\ttype master;\n\tfile \"%s/%s.db\";\n};\n", row1[1], row1[3], directory, row1[1]);
snprintf(filename, sizeof(filename), "%s/%s.db", directory, row1[1]);
if (!(db = fopen(filename, "w"))) {
syslog(LOG_ERR, "Error opening file %s for writing", filename);
continue;
}
fprintf(db, ";\n; Automatically generated by mysql2dns - DO NOT EDIT!\n;\n\n");
fprintf(db, "%s.\t%s\t%s SOA\t%s %s (\n", row1[1], row1[2], row1[3], row1[4], row1[5]);
fprintf(db, "\t\t\t\t%s\t; Serial\n", row1[6]);
fprintf(db, "\t\t\t\t%s\t; Refresh\n", row1[7]);
fprintf(db, "\t\t\t\t%s\t; Retry\n", row1[8]);
fprintf(db, "\t\t\t\t%s\t; Expire\n", row1[9]);
fprintf(db, "\t\t\t\t%s )\t; Negative Cache TTL\n", row1[10]);
snprintf(query, sizeof(query), "SELECT `zone`, `ttl`, `class`, `type`, `address` "
"FROM `records` WHERE `domain` = %s ORDER BY `zone`", row1[0]);
if (mysql_query(dbh, query)) {
syslog(LOG_ERR, "%s", mysql_error(dbh));
continue;
}
if (!(res2 = mysql_store_result(dbh))) {
syslog(LOG_ERR, "%s", mysql_error(dbh));
continue;
}
while ((row2 = mysql_fetch_row(res2))) {
fprintf(db, "%s\t%s\t%s %s\t%s\n", row2[0], row2[1], row2[2], row2[3], row2[4]);
}
fclose(db);
mysql_free_result(res2);
}
fclose(zones);
mysql_free_result(res1);
}
int main(int argc, char **argv) {
FILE *fp;
int iostream;
char database[128];
char directory[128];
char execute[128];
char password[128];
char username[128];
char server[128];
char serials[32];
char buf[1024];
char val[128];
int update;
MYSQL dbh;
/* default values */
strcpy(database, "dns");
strcpy(directory, "/var/named/dynamic");
strcpy(execute, "");
strcpy(password, "");
strcpy(username, "root");
strcpy(server, "localhost");
strcpy(serials, "");
update = 60;
openlog(PROG_NAME, LOG_PID, LOG_DAEMON);
/* read options */
if ((fp = fopen("/etc/mysql2dns.conf", "r")) == NULL) {
syslog(LOG_ERR, "ERROR: reading /etc/mysql2dns.conf");
exit(EXIT_FAILURE);
}
while (fgets(buf, sizeof(buf), fp)) {
if ((buf[0] == '#') || (buf[0] == '\n')) {
continue;
} else if ((sscanf(buf, "database: %127[A-Za-z0-9 .:/_+-]", val)) == 1) {
snprintf(database, sizeof(database), "%s", val);
continue;
} else if ((sscanf(buf, "directory: %127[A-Za-z0-9 .:/_+-]", val)) == 1) {
snprintf(directory, sizeof(directory), "%s", val);
continue;
} else if ((sscanf(buf, "execute: %127[A-Za-z0-9 .:/_+-]", val)) == 1) {
snprintf(execute, sizeof(execute), "%s", val);
continue;
} else if ((sscanf(buf, "password: %127[A-Za-z0-9 .:/_+-]", val)) == 1) {
snprintf(password, sizeof(password), "%s", val);
continue;
} else if ((sscanf(buf, "username: %127[A-Za-z0-9 .:/_+-]", val)) == 1) {
snprintf(username, sizeof(username), "%s", val);
continue;
} else if ((sscanf(buf, "server: %127[A-Za-z0-9 .:/_+-]", val)) == 1) {
snprintf(server, sizeof(server), "%s", val);
continue;
} else if ((sscanf(buf, "update: %127[A-Za-z0-9 .:/_+-]", val)) == 1) {
update = atoi(val);
continue;
}
}
fclose(fp);
/* go to background */
if (fork())
exit(EXIT_SUCCESS);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
iostream = open(_PATH_DEVNULL, O_RDWR);
dup(iostream);
dup(iostream);
if (!mysql_init(&dbh)) {
syslog(LOG_ERR, "%s", mysql_error(&dbh));
exit(EXIT_FAILURE);
}
do {
if (!mysql_real_connect(&dbh, server, username, password, database, 0, NULL, 0)) {
syslog(LOG_ERR, "%s", mysql_error(&dbh));
} else if (change_serials(&dbh, serials, sizeof(serials))) {
update_zones(&dbh, directory);
if (strlen(execute)) {
system(execute);
}
mysql_close(&dbh);
}
} while (!sleep(update));
closelog();
return 0;
}