]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/zone2json.cc
Merge remote-tracking branch 'origin/master' into rec-edsn-unaligned-test
[thirdparty/pdns.git] / pdns / zone2json.cc
CommitLineData
ee681377 1/*
6edbf68a
PL
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
ee681377
PD
23/* accepts a named.conf or a zone as parameter and outputs heaps of sql */
24
870a0fe4
AT
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
ee681377
PD
28#include <unistd.h>
29#include <string>
30#include <map>
31
32#include <iostream>
33#include <stdio.h>
34#include "namespaces.hh"
35
36#include "dns.hh"
37#include "arguments.hh"
7c0cb150 38#include "bindparserclasses.hh"
ee681377
PD
39#include "statbag.hh"
40#include "misc.hh"
41#include "dnspacket.hh"
42#include "zoneparser-tng.hh"
43#include "dnsrecords.hh"
44#include <boost/algorithm/string.hpp>
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <unistd.h>
fa8fd4d2 48
bf6dc747 49#include "json11.hpp"
ee681377 50
bf6dc747 51using namespace json11;
ee681377
PD
52
53StatBag S;
ee681377
PD
54static int g_numRecords;
55
bf6dc747 56static Json::object emitRecord(const string& zoneName, const DNSName &DNSqname, const string &qtype, const string &ocontent, int ttl)
ee681377 57{
b9bafae0 58 int prio=0;
ee681377
PD
59 string retval;
60 g_numRecords++;
61 string content(ocontent);
62 if(qtype == "MX" || qtype == "SRV") {
335da0ba 63 prio=pdns_stou(content);
ee681377
PD
64
65 string::size_type pos = content.find_first_not_of("0123456789");
66 if(pos != string::npos)
67 boost::erase_head(content, pos);
68 trim_left(content);
69 }
70
bf6dc747 71 Json::object dict;
ee681377 72
39313d8e 73 dict["name"] = DNSqname.toString();
bf6dc747
AT
74 dict["type"] = qtype;
75 dict["ttl"] = ttl;
76 dict["prio"] = prio;
77 dict["content"] = content;
ee681377 78
bf6dc747 79 return dict;
ee681377
PD
80}
81
82/* 2 modes of operation, either --named or --zone (the latter needs $ORIGIN)
83 2 further modes: --mysql or --oracle
84*/
85
86ArgvMap &arg()
87{
88 static ArgvMap theArg;
89 return theArg;
90}
91
92
93int main(int argc, char **argv)
5761d098 94try
ee681377
PD
95{
96 vector<string> lines;
97
ee681377 98 reportAllTypes();
ee681377 99 std::ios_base::sync_with_stdio(false);
ee681377
PD
100
101 ::arg().setSwitch("verbose","Verbose comments on operation")="no";
102 ::arg().setSwitch("on-error-resume-next","Continue after errors")="no";
103 ::arg().set("zone","Zonefile to parse")="";
104 ::arg().set("zone-name","Specify an $ORIGIN in case it is not present")="";
105 ::arg().set("named-conf","Bind 8/9 named.conf to parse")="";
106
107 ::arg().set("soa-minimum-ttl","Do not change")="0";
108 ::arg().set("soa-refresh-default","Do not change")="0";
109 ::arg().set("soa-retry-default","Do not change")="0";
110 ::arg().set("soa-expire-default","Do not change")="0";
111
112 ::arg().setCmd("help","Provide a helpful message");
a03a8f0a 113 ::arg().setCmd("version","Print the version");
ee681377
PD
114
115 S.declare("logmessages");
116
117 string namedfile="";
118 string zonefile="";
119
120 ::arg().parse(argc, argv);
a03a8f0a
PL
121
122 if(::arg().mustDo("version")){
123 cerr<<"zone2json "<<VERSION<<endl;
124 exit(0);
125 }
126
ff5ba4f9
WA
127 if(::arg().mustDo("help")) {
128 cout<<"syntax:"<<endl<<endl;
129 cout<<::arg().helpstring()<<endl;
130 exit(0);
131 }
132
133 if(argc<2) {
ee681377
PD
134 cerr<<"syntax:"<<endl<<endl;
135 cerr<<::arg().helpstring()<<endl;
136 exit(1);
137 }
138
139 namedfile=::arg()["named-conf"];
140 zonefile=::arg()["zone"];
141
142 int count=0, num_domainsdone=0;
143
144 if(zonefile.empty()) {
145 BindParser BP;
146 BP.setVerbose(::arg().mustDo("verbose"));
147 BP.parse(namedfile.empty() ? "./named.conf" : namedfile);
148
149 vector<BindDomainInfo> domains=BP.getDomains();
150 struct stat st;
151 for(vector<BindDomainInfo>::iterator i=domains.begin(); i!=domains.end(); ++i) {
152 if(stat(i->filename.c_str(), &st) == 0) {
153 i->d_dev = st.st_dev;
154 i->d_ino = st.st_ino;
155 }
156 }
157
158 sort(domains.begin(), domains.end()); // put stuff in inode order
159
160 int numdomains=domains.size();
161 int tick=numdomains/100;
bf6dc747
AT
162 cout << "[";
163
ee681377
PD
164 for(vector<BindDomainInfo>::const_iterator i=domains.begin();
165 i!=domains.end();
166 ++i)
167 {
168 if(i->type!="master" && i->type!="slave") {
39313d8e 169 cerr<<" Warning! Skipping '"<<i->type<<"' zone '"<<i->name<<"'"<<endl;
ee681377
PD
170 continue;
171 }
172 lines.clear();
173 try {
bf6dc747
AT
174 Json::object obj;
175 Json::array recs;
ee681377
PD
176 ZoneParserTNG zpt(i->filename, i->name, BP.getDirectory());
177 DNSResourceRecord rr;
39313d8e 178 obj["name"] = i->name.toString();
bf6dc747 179
ee681377 180 while(zpt.get(rr))
39313d8e 181 recs.push_back(emitRecord(i->name.toString(), rr.qname, rr.qtype.getName(), rr.content, rr.ttl));
bf6dc747
AT
182 obj["records"] = recs;
183 Json tmp = obj;
184 cout<<tmp.dump();
185 if(i+1 < domains.end()) cout<<",";
ee681377 186 num_domainsdone++;
bf6dc747 187 }
ee681377
PD
188 catch(std::exception &ae) {
189 if(!::arg().mustDo("on-error-resume-next"))
190 throw;
191 else
192 cerr<<endl<<ae.what()<<endl;
193 }
3f81d239 194 catch(PDNSException &ae) {
ee681377
PD
195 if(!::arg().mustDo("on-error-resume-next"))
196 throw;
197 else
198 cerr<<ae.reason<<endl;
199 }
200 if(!tick || !((count++)%tick))
201 cerr<<"\r"<<count*100/numdomains<<"% done ("<<i->filename<<")\033\133\113";
202 }
bf6dc747 203 cout << "]" << endl;
ee681377
PD
204 cerr<<"\r100% done\033\133\113"<<endl;
205 }
206 else {
b873e23a 207 ZoneParserTNG zpt(zonefile, DNSName(::arg()["zone-name"]));
ee681377 208 DNSResourceRecord rr;
bf6dc747
AT
209 string zname;
210 Json::object obj;
211 Json::array records;
212
213 obj["name"] = ::arg()["zone-name"];
214
ee681377 215 while(zpt.get(rr))
b873e23a 216 records.push_back(emitRecord(::arg()["zone-name"], rr.qname, rr.qtype.getName(), rr.content, rr.ttl));
bf6dc747
AT
217 obj["records"] = records;
218
219 Json tmp = obj;
220
221 cout<<tmp.dump()<<endl;
222
ee681377
PD
223 num_domainsdone=1;
224 }
225 cerr<<num_domainsdone<<" domains were fully parsed, containing "<<g_numRecords<<" records\n";
5761d098
CH
226
227 return 0;
ee681377 228
5761d098
CH
229}
230catch(PDNSException &ae) {
231 cerr<<"\nFatal error: "<<ae.reason<<endl;
232 return 1;
233}
234catch(std::exception &e) {
235 cerr<<"\ndied because of STL error: "<<e.what()<<endl;
236 return 1;
237}
238catch(...) {
239 cerr<<"\ndied because of unknown exception"<<endl;
ee681377 240 return 1;
ee681377 241}