]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/zone2json.cc
Merge pull request #8440 from cmouse/shadow
[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 1 further mode: --mysql
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 ::arg().set("max-generate-steps", "Maximum number of $GENERATE steps when loading a zone from a file")="0";
112
113 ::arg().setCmd("help","Provide a helpful message");
114 ::arg().setCmd("version","Print the version");
115
116 S.declare("logmessages");
117
118 string namedfile="";
119 string zonefile="";
120
121 ::arg().parse(argc, argv);
122
123 if(::arg().mustDo("version")){
124 cerr<<"zone2json "<<VERSION<<endl;
125 exit(0);
126 }
127
128 if(::arg().mustDo("help")) {
129 cout<<"syntax:"<<endl<<endl;
130 cout<<::arg().helpstring()<<endl;
131 exit(0);
132 }
133
134 if(argc<2) {
135 cerr<<"syntax:"<<endl<<endl;
136 cerr<<::arg().helpstring()<<endl;
137 exit(1);
138 }
139
140 namedfile=::arg()["named-conf"];
141 zonefile=::arg()["zone"];
142
143 int count=0, num_domainsdone=0;
144
145 if(zonefile.empty()) {
146 BindParser BP;
147 BP.setVerbose(::arg().mustDo("verbose"));
148 BP.parse(namedfile.empty() ? "./named.conf" : namedfile);
149
150 vector<BindDomainInfo> domains=BP.getDomains();
151 struct stat st;
152 for(vector<BindDomainInfo>::iterator i=domains.begin(); i!=domains.end(); ++i) {
153 if(stat(i->filename.c_str(), &st) == 0) {
154 i->d_dev = st.st_dev;
155 i->d_ino = st.st_ino;
156 }
157 }
158
159 sort(domains.begin(), domains.end()); // put stuff in inode order
160
161 int numdomains=domains.size();
162 int tick=numdomains/100;
163 cout << "[";
164
165 for(vector<BindDomainInfo>::const_iterator i=domains.begin();
166 i!=domains.end();
167 ++i)
168 {
169 if(i->type!="master" && i->type!="slave") {
170 cerr<<" Warning! Skipping '"<<i->type<<"' zone '"<<i->name<<"'"<<endl;
171 continue;
172 }
173 lines.clear();
174 try {
175 Json::object obj;
176 Json::array recs;
177 ZoneParserTNG zpt(i->filename, i->name, BP.getDirectory());
178 zpt.setMaxGenerateSteps(::arg().asNum("max-generate-steps"));
179 DNSResourceRecord rr;
180 obj["name"] = i->name.toString();
181
182 while(zpt.get(rr))
183 recs.push_back(emitRecord(i->name.toString(), rr.qname, rr.qtype.getName(), rr.content, rr.ttl));
184 obj["records"] = recs;
185 Json tmp = obj;
186 cout<<tmp.dump();
187 if(i+1 < domains.end()) cout<<",";
188 num_domainsdone++;
189 }
190 catch(std::exception &ae) {
191 if(!::arg().mustDo("on-error-resume-next"))
192 throw;
193 else
194 cerr<<endl<<ae.what()<<endl;
195 }
196 catch(PDNSException &ae) {
197 if(!::arg().mustDo("on-error-resume-next"))
198 throw;
199 else
200 cerr<<ae.reason<<endl;
201 }
202 if(!tick || !((count++)%tick))
203 cerr<<"\r"<<count*100/numdomains<<"% done ("<<i->filename<<")\033\133\113";
204 }
205 cout << "]" << endl;
206 cerr<<"\r100% done\033\133\113"<<endl;
207 }
208 else {
209 ZoneParserTNG zpt(zonefile, DNSName(::arg()["zone-name"]));
210 zpt.setMaxGenerateSteps(::arg().asNum("max-generate-steps"));
211 DNSResourceRecord rr;
212 string zname;
213 Json::object obj;
214 Json::array records;
215
216 obj["name"] = ::arg()["zone-name"];
217
218 while(zpt.get(rr))
219 records.push_back(emitRecord(::arg()["zone-name"], rr.qname, rr.qtype.getName(), rr.content, rr.ttl));
220 obj["records"] = records;
221
222 Json tmp = obj;
223
224 cout<<tmp.dump()<<endl;
225
226 num_domainsdone=1;
227 }
228 cerr<<num_domainsdone<<" domains were fully parsed, containing "<<g_numRecords<<" records\n";
229
230 return 0;
231
232 }
233 catch(PDNSException &ae) {
234 cerr<<"\nFatal error: "<<ae.reason<<endl;
235 return 1;
236 }
237 catch(std::exception &e) {
238 cerr<<"\ndied because of STL error: "<<e.what()<<endl;
239 return 1;
240 }
241 catch(...) {
242 cerr<<"\ndied because of unknown exception"<<endl;
243 return 1;
244 }