aboutsummaryrefslogtreecommitdiff
path: root/src/output.c
blob: fa2a62151c334d19f084fa5fe138b02ddc376817 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* This file is part of NSsync 
   Copyright (C) 2011-2017 Sergey Poznyakoff
  
   NSsync is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
  
   NSsync is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with NSsync.  If not, see <http://www.gnu.org/licenses/>. */

#include "nssync.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#define S(s) ((s) ? (s) : NULL)

enum {
	f_ns_ttl,
	f_ns_type,
	f_ns_data,
	_ns_nfields
};
	
int
save_ns_record(MYSQL_ROW row, unsigned nf, void *data)
{
	struct nssync *sp = data;
	struct nsdef *ns;
	if (nf != _ns_nfields) {
		error("NS query returned wrong number of fields");
		return 1;
	}

	ns = grecs_malloc(sizeof(*ns));
	ns->type = grecs_strdup(row[f_ns_type]);
	ns->data = grecs_strdup(row[f_ns_data]);
	/* FIXME: TTL */
	ns->next = NULL;

	if (sp->nsdef_tail)
		sp->nsdef_tail->next = ns;
	else
		sp->nsdef_head = ns;
	sp->nsdef_tail = ns;

	if (check_ns && !sp->myzone && strcasecmp(ns->type, "NS") == 0) {
		struct addrinfo *res, *ap, hints;
		int rc;

		memset(&hints, 0, sizeof(hints));
		hints.ai_family = AF_INET;
		rc = getaddrinfo(ns->data, NULL, &hints, &res);
		if (rc) {
			error("can't resolve %s: %s", ns->data,
			      gai_strerror(rc));
			return 0;
		}

		for (ap = res; ap; ap = ap->ai_next) {
			if (is_my_sockaddr((struct sockaddr_in*)res->ai_addr)) {
				sp->myzone = 1;
				break;
			}
		}
		freeaddrinfo(res);
	}
	
	return 0;
}


enum {
	f_rr_host,
	f_rr_ttl,
	f_rr_type,
	f_rr_priority,
	f_rr_data,
	_rr_nfields
};

int
format_rr_record(MYSQL_ROW row, unsigned nf, void *data)
{
	struct nssync *sp = data;
	if (nf != _rr_nfields) {
		error("RR query returned wrong number of fields");
		return 1;
	}
	if (!row[f_rr_type])
		return 0;
	if (strcasecmp(row[f_rr_type], "MX") == 0)
		fprintf(sp->fp, "%s\t%s\tMX\t%s\t%s\n",
			row[f_rr_host], S(row[f_rr_ttl]),
			row[f_rr_priority] ? row[f_rr_priority] : "1", 
                        row[f_rr_data]);
	else
		fprintf(sp->fp, "%s\t%s\t%s\t%s\n",
			row[f_rr_host], S(row[f_rr_ttl]),
			row[f_rr_type], row[f_rr_data]);
	return 0;
}


enum {
	f_soa_zone,
	f_soa_ttl,
	f_soa_type,
	f_soa_data,
	f_soa_person,
	f_soa_serial,
	f_soa_refresh,
	f_soa_retry,
	f_soa_expire,
	f_soa_minimum,
	_soa_nfields
};

int
is_reverse_zone(const char *str)
{
	static char suffix[] = ".in-addr.arpa";
	size_t len = strlen(str);
	
	return len > sizeof(suffix) &&
		strcasecmp(str + len - sizeof(suffix) + 1, suffix) == 0;
}

void
free_nsdef(struct nssync *sp)
{
	struct nsdef *ns;

	for (ns = sp->nsdef_head; ns; ) {
		struct nsdef *next = ns->next;
		free(ns->type);
		free(ns->data);
		free(ns);
		ns = next;
	}
	sp->nsdef_head = sp->nsdef_tail = NULL;
}

int
format_soa_record(MYSQL_ROW row, unsigned nf, void *data)
{
	struct nssync *sp = data;
	struct wordsplit ws;
	const char *env[3];
	int wsflags = WRDSF_NOCMD | WRDSF_ENV | WRDSF_ENV_KV |
		      WRDSF_NOSPLIT | WRDSF_KEEPUNDEF;
	struct nsdef *ns;
	
	if (nf != _soa_nfields) {
		error("SOA query returned wrong number of fields");
		return 1;
	}
	if (!row[f_soa_type])
		return 0;

	free_nsdef(sp);
	sp->myzone = !check_ns;

	/* Fill the list of NS servers */
	env[0] = "zone";
	env[1] = row[f_soa_zone];
	env[2] = 0;

	if (sp->ns_query) {
		ws.ws_env = env;
		if (wordsplit(sp->ns_query, &ws, wsflags)) {
			error("cannot split ns_query: %s",
			      wordsplit_strerror(&ws));
			exit(EX_SOFTWARE);
		}
		wsflags |= WRDSF_REUSE;
		if (sql_do_query(ws.ws_wordv[0], save_ns_record, sp))
			exit(EX_UNAVAILABLE);
	}

	if (!sp->myzone) {
		debug(1,("%s: not served by me", row[f_soa_zone]));
		wordsplit_free(&ws);
		return 0;
	}
	
	grecs_free(sp->file_name);
	sp->file_name = bindcf_lookup(sp, row[f_soa_zone]);
	if (!sp->file_name)
		return 0;
	debug(2, ("%s: zone %s, file %s", sp->tag, row[f_soa_zone],
		  sp->file_name));

	fflush(sp->fp);
	fseek(sp->fp, 0, SEEK_SET);
	ftruncate(fileno(sp->fp), 0);

	fprintf(sp->fp, "; Zone file generated by %s\n", PACKAGE_STRING);
	fprintf(sp->fp, "; Do not edit! See %s for details\n", config_file);
	if (row[f_soa_ttl])
		fprintf(sp->fp, "$TTL %s\n", row[f_soa_ttl]);
	fprintf(sp->fp,
		"%s. %s IN SOA  %s %s (\n"
		"\t%s ; Serial\n"
		"\t%s ; Refresh\n"
		"\t%s ; Retry\n"
		"\t%s ; Expire\n"
		"\t%s) ; Minimum\n",
		row[f_soa_zone],
		S(row[f_soa_ttl]),
		row[f_soa_data],
		row[f_soa_person],
		row[f_soa_serial],
		row[f_soa_refresh],
		row[f_soa_retry],
		row[f_soa_expire],
		row[f_soa_minimum]);

	for (ns = sp->nsdef_head; ns; ns = ns->next)
		/* FIXME: TTL */
		fprintf(sp->fp, "\t%s\t%s\n", ns->type, ns->data);
	
	if (wordsplit((sp->rev_rr_query && is_reverse_zone(row[f_soa_zone])) ?
		      sp->rev_rr_query : sp->rr_query, &ws, wsflags)) {
		error("cannot split rr_query: %s", wordsplit_strerror(&ws));
		exit(EX_SOFTWARE);
	}
	
	if (sql_do_query(ws.ws_wordv[0], format_rr_record, sp))
		exit(EX_UNAVAILABLE);
	
	wordsplit_free(&ws);

	fflush(sp->fp);

	if (compare(sp->temp_file_name, sp->file_name)) {
		debug(1,("copying %s to %s", sp->temp_file_name,
			 sp->file_name));
		if (copy_file(sp->temp_file_name, sp->fp, sp->file_name) == 0)
			dlz_success(row[f_soa_zone]);
		else
			dlz_error(row[f_soa_zone], "can't copy file");
	}

	return 0;
}

void
format_zones(struct nssync *sp)
{
	debug(1, ("%s: formatting zones into %s",
		  sp->tag, sp->temp_file_name));
	if (sql_do_query(sp->soa_query, format_soa_record, sp))
		exit(EX_UNAVAILABLE);
}

Return to:

Send suggestions and report system problems to the System administrator.