]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/zone2json.cc
Merge remote-tracking branch 'origin/master' into rec-edsn-unaligned-test
[thirdparty/pdns.git] / pdns / zone2json.cc
1 /*
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
23 /* accepts a named.conf or a zone as parameter and outputs heaps of sql */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
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"
38 #include "bindparserclasses.hh"
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>
48
49 #include "json11.hpp"
50
51 using namespace json11;
52
53 StatBag S;
54 static int g_numRecords;
55
56 static Json::object emitRecord(const string& zoneName, const DNSName &DNSqname, const string &qtype, const string &ocontent, int ttl)
57 {
58 int prio=0;
59 string retval;
60 g_numRecords++;
61 string content(ocontent);
62 if(qtype == "MX" || qtype == "SRV") {
63 prio=pdns_stou(content);
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
71 Json::object dict;
72
73 dict["name"] = DNSqname.toString();
74 dict["type"] = qtype;
75 dict["ttl"] = ttl;
76 dict["prio"] = prio;
77 dict["content"] = content;
78
79 return dict;
80 }
81
82 /* 2 modes of operation, either --named or --zone (the latter needs $ORIGIN)
83 2 further modes: --mysql or --oracle
84 */
85
86 ArgvMap &arg()
87 {
88 static ArgvMap theArg;
89 return theArg;
90 }
91
92
93 int main(int argc, char **argv)
94 try
95 {
96 vector<string> lines;
97
98 reportAllTypes();
99 std::ios_base::sync_with_stdio(false);
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");
113 ::arg().setCmd("version","Print the version");
114
115 S.declare("logmessages");
116
117 string namedfile="";
118 string zonefile="";
119
120 ::arg().parse(argc, argv);
121
122 if(::arg().mustDo("version")){
123 cerr<<"zone2json "<<VERSION<<endl;
124 exit(0);
125 }
126
127 if(::arg().mustDo("help")) {
128 cout<<"syntax:"<<endl<<endl;
129 cout<<::arg().helpstring()<<endl;
130 exit(0);
131 }
132
133 if(argc<2) {
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;
162 cout << "[";
163
164 for(vector<BindDomainInfo>::const_iterator i=domains.begin();
165 i!=domains.end();
166 ++i)
167 {
168 if(i->type!="master" && i->type!="slave") {
169 cerr<<" Warning! Skipping '"<<i->type<<"' zone '"<<i->name<<"'"<<endl;
170 continue;
171 }
172 lines.clear();
173 try {
174 Json::object obj;
175 Json::array recs;
176 ZoneParserTNG zpt(i->filename, i->name, BP.getDirectory());
177 DNSResourceRecord rr;
178 obj["name"] = i->name.toString();
179
180 while(zpt.get(rr))
181 recs.push_back(emitRecord(i->name.toString(), rr.qname, rr.qtype.getName(), rr.content, rr.ttl));
182 obj["records"] = recs;
183 Json tmp = obj;
184 cout<<tmp.dump();
185 if(i+1 < domains.end()) cout<<",";
186 num_domainsdone++;
187 }
188 catch(std::exception &ae) {
189 if(!::arg().mustDo("on-error-resume-next"))
190 throw;
191 else
192 cerr<<endl<<ae.what()<<endl;
193 }
194 catch(PDNSException &ae) {
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 }
203 cout << "]" << endl;
204 cerr<<"\r100% done\033\133\113"<<endl;
205 }
206 else {
207 ZoneParserTNG zpt(zonefile, DNSName(::arg()["zone-name"]));
208 DNSResourceRecord rr;
209 string zname;
210 Json::object obj;
211 Json::array records;
212
213 obj["name"] = ::arg()["zone-name"];
214
215 while(zpt.get(rr))
216 records.push_back(emitRecord(::arg()["zone-name"], rr.qname, rr.qtype.getName(), rr.content, rr.ttl));
217 obj["records"] = records;
218
219 Json tmp = obj;
220
221 cout<<tmp.dump()<<endl;
222
223 num_domainsdone=1;
224 }
225 cerr<<num_domainsdone<<" domains were fully parsed, containing "<<g_numRecords<<" records\n";
226
227 return 0;
228
229 }
230 catch(PDNSException &ae) {
231 cerr<<"\nFatal error: "<<ae.reason<<endl;
232 return 1;
233 }
234 catch(std::exception &e) {
235 cerr<<"\ndied because of STL error: "<<e.what()<<endl;
236 return 1;
237 }
238 catch(...) {
239 cerr<<"\ndied because of unknown exception"<<endl;
240 return 1;
241 }