aboutsummaryrefslogtreecommitdiff
path: root/src/output.c
blob: 72a09c187771a0aa7887a1311f7b7aa8f887b705 (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
/* This file is part of NSsync 
   Copyright (C) 2011 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"

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

enum {
	f_ns_ttl,
	f_ns_type,
	f_ns_data,
	_ns_nfields
};
	
int
format_ns_record(MYSQL_ROW row, unsigned nf, void *data)
{
	FILE *fp = data;
	if (nf != _ns_nfields) {
		error("NS query returned wrong number of fields");
		return 1;
	}
	/* FIXME: TTL */
	fprintf(fp, "\t%s\t%s\n", 
		row[f_ns_type], row[f_ns_data]);
	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)
{
	FILE *fp = 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(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_data]);
	else
		fprintf(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;
}

int
format_soa_record(MYSQL_ROW row, unsigned nf, void *data)
{
	FILE *fp = data;
	struct wordsplit ws;
	const char *env[3];
	
	if (nf != _soa_nfields) {
		error("SOA query returned wrong number of fields");
		return 1;
	}
	if (!row[f_soa_type])
		return 0;
	fprintf(fp, "$ORIGIN .\n");
	if (row[f_soa_ttl])
		fprintf(fp, "$TTL %s\n", row[f_soa_ttl]);
	fprintf(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]);
	
	env[0] = "zone";
	env[1] = row[f_soa_zone];
	env[2] = 0;
	
	ws.ws_env = env;
	if (wordsplit(ns_query, &ws,
		      WRDSF_NOCMD | WRDSF_ENV | WRDSF_ENV_KV | WRDSF_NOSPLIT |
		      WRDSF_KEEPUNDEF)) {
		error("cannot split ns_query: %s", wordsplit_strerror(&ws));
		exit(EX_SOFTWARE);
	}

	if (sql_do_query(ws.ws_wordv[0], format_ns_record, fp))
		exit(EX_UNAVAILABLE);

	if (wordsplit((rev_rr_query && is_reverse_zone(row[f_soa_zone])) ?
		      rev_rr_query : rr_query, &ws,
		      WRDSF_NOCMD | WRDSF_ENV | WRDSF_ENV_KV | WRDSF_NOSPLIT |
		      WRDSF_KEEPUNDEF | WRDSF_REUSE)) {
		error("cannot split rr_query: %s", wordsplit_strerror(&ws));
		exit(EX_SOFTWARE);
	}
	
	if (sql_do_query(ws.ws_wordv[0], format_rr_record, fp))
		exit(EX_UNAVAILABLE);
	
	wordsplit_free(&ws);
	
	return 0;
}

void
format_soa(FILE *fp)
{
	if (sql_do_query(soa_query, format_soa_record, fp))
		exit(EX_UNAVAILABLE);
}

Return to:

Send suggestions and report system problems to the System administrator.