]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/backends/bind/zone2ldap.cc
and the final bit of whitespace/tab cleanup
[thirdparty/pdns.git] / pdns / backends / bind / zone2ldap.cc
CommitLineData
ad26538e 1/*
eefd15f9
BH
2 * PowerDNS BIND Zone to LDAP converter
3 * Copyright (C) 2003 Norbert Sendetzky
874300a8 4 * Copyright (C) 2007 bert hubert
eefd15f9
BH
5 *
6 * This program is free software; you can redistribute it and/or modify
22dc646a
BH
7 * it under the terms of the GNU General Public License version 2
8 * the Free Software Foundation
9 *
eefd15f9
BH
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
06bd9ccf 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
ad26538e
BH
19*/
20
21
ad26538e 22#include <map>
eefd15f9 23#include <string>
ad26538e
BH
24#include <iostream>
25#include <stdio.h>
ad26538e 26#include "arguments.hh"
ad26538e
BH
27#include "bindparser.hh"
28#include "statbag.hh"
874300a8 29#include <boost/function.hpp>
ee0f881e 30#include "dnsrecords.hh"
ad26538e 31#include "misc.hh"
eefd15f9 32#include "dns.hh"
874300a8 33#include "zoneparser-tng.hh"
eefd15f9
BH
34
35using std::map;
36using std::string;
37using std::vector;
ad26538e 38
ad26538e
BH
39StatBag S;
40ArgvMap args;
eefd15f9 41bool g_dnsttl;
ad26538e
BH
42string g_basedn;
43string g_zonename;
44map<string,bool> g_objects;
45
eefd15f9 46static void callback_simple( unsigned int domain_id, const string &domain, const string &qtype, const string &content, int ttl, int prio )
ad26538e 47{
4957a608
BH
48 string host;
49 string::size_type pos;
50 vector<string> parts;
51 string domain2 = stripDot( domain );
52
53
54 if( ( pos = domain2.rfind( g_zonename ) ) == string::npos )
55 {
56 cerr << "Domain " << domain2 << " not part of " << g_zonename << endl;
57 return;
58 }
59
60 host = stripDot( domain2.substr( 0, pos ) );
61
62 cout << "dn: dc=";
63 if( !host.empty() ) { cout << host << ",dc="; }
64 cout << g_zonename << "," << g_basedn << endl;
65
66 if( host.empty() ) { host = g_zonename; }
67
68 if( !g_objects[domain2] )
69 {
70 g_objects[domain2] = true;
71
72 cout << "changetype: add" << endl;
73 cout << "objectclass: dnsdomain2" << endl;
74 cout << "objectclass: domainrelatedobject" << endl;
75 cout << "dc: " << host << endl;
76 if( g_dnsttl ) { cout << "dnsttl: " << ttl << endl; }
77 cout << "associateddomain: " << domain2 << endl;
78 }
79 else
80 {
81 cout << "changetype: modify" << endl;
82 cout << "add: " << qtype << "Record" << endl;
83 }
84
85 cout << qtype << "Record: ";
86 if( prio != 0 ) { cout << prio << " "; }
87 cout << stripDot( content ) << endl << endl;
e1d03bb0
BH
88}
89
90
eefd15f9 91
e1d03bb0
BH
92static void callback_tree( unsigned int domain_id, const string &domain, const string &qtype, const string &content, int ttl, int prio )
93{
4957a608
BH
94 unsigned int i;
95 string dn, net;
96 vector<string> parts;
97 string domain2 = stripDot( domain );
98
99 stringtok( parts, domain2, "." );
100 if( parts.empty() ) { return; }
101
102 for( i = parts.size() - 1; i > 0; i-- )
103 {
104 net = parts[i] + net;
105 dn = "dc=" + parts[i] + "," + dn;
106
107 if( !g_objects[net] )
108 {
109 g_objects[net] = true;
110
111 cout << "dn: " << dn << g_basedn << endl;
112 cout << "changetype: add" << endl;
113 cout << "objectclass: dnsdomain2" << endl;
114 cout << "objectclass: domainrelatedobject" << endl;
115 cout << "dc: " << parts[i] << endl;
116 cout << "associateddomain: " << net << endl << endl;
117 }
118
119 net = "." + net;
120 }
121
122 cout << "dn: " << "dc=" << parts[0] << "," << dn << g_basedn << endl;
123
124 if( !g_objects[domain2] )
125 {
126 g_objects[domain2] = true;
127
128 cout << "changetype: add" << endl;
129 cout << "objectclass: dnsdomain2" << endl;
130 cout << "objectclass: domainrelatedobject" << endl;
131 cout << "dc: " << parts[0] << endl;
132 if( g_dnsttl ) { cout << "dnsttl: " << ttl << endl; }
133 cout << "associateddomain: " << domain2 << endl;
134 }
135 else
136 {
137 cout << "changetype: modify" << endl;
138 cout << "add: " << qtype << "Record" << endl;
139 }
140
141 cout << qtype << "Record: ";
142 if( prio != 0 ) { cout << prio << " "; }
143 cout << stripDot( content ) << endl << endl;
ad26538e
BH
144}
145
146
eefd15f9 147
ad26538e
BH
148int main( int argc, char* argv[] )
149{
4957a608
BH
150 BindParser BP;
151 vector<string> parts;
ad26538e
BH
152
153
4957a608
BH
154 try
155 {
ad26538e 156#if __GNUC__ >= 3
4957a608 157 ios_base::sync_with_stdio( false );
ad26538e 158#endif
4957a608
BH
159 reportAllTypes();
160 args.setCmd( "help", "Provide a helpful message" );
161 args.setSwitch( "verbose", "Verbose comments on operation" ) = "no";
162 args.setSwitch( "resume", "Continue after errors" ) = "no";
163 args.setSwitch( "dnsttl", "Add dnsttl attribute to every entry" ) = "no";
164 args.set( "named-conf", "Bind 8 named.conf to parse" ) = "";
165 args.set( "zone-file", "Zone file to parse" ) = "";
166 args.set( "zone-name", "Specify a zone name if zone is set" ) = "";
167 args.set( "basedn", "Base DN to store objects below" ) = "ou=hosts,o=mycompany,c=de";
168 args.set( "layout", "How to arrange entries in the directory (simple or as tree)" ) = "simple";
169
170 args.parse( argc, argv );
171
172 if( argc < 2 || args.mustDo( "help" ) )
173 {
174 cerr << "Syntax:" << endl << endl;
175 cerr << args.helpstring() << endl;
176 exit( 1 );
177 }
178
179 g_basedn = args["basedn"];
180 g_dnsttl = args.mustDo( "dnsttl" );
181 typedef boost::function<void(unsigned int, const string &, const string &, const string &, int, int)> callback_t;
182 callback_t callback = callback_simple;
183 if( args["layout"] == "tree" )
184 {
185 callback=callback_tree;
186 }
187
188 if( !args["named-conf"].empty() )
189 {
190 BP.setVerbose( args.mustDo( "verbose" ) );
191 BP.parse( args["named-conf"] );
192// ZP.setDirectory( BP.getDirectory() );
193 const vector<BindDomainInfo> &domains = BP.getDomains();
194
195 for( vector<BindDomainInfo>::const_iterator i = domains.begin(); i != domains.end(); i++ )
196 {
197 try
198 {
199 if( i->name != "." && i->name != "localhost" && i->name != "0.0.127.in-addr.arpa" )
200 {
201 cerr << "Parsing file: " << i->filename << ", domain: " << i->name << endl;
202 g_zonename = i->name;
203 ZoneParserTNG zpt(i->filename, i->name, BP.getDirectory());
204 DNSResourceRecord rr;
205 while(zpt.get(rr))
206 callback(0, rr.qname, rr.qtype.getName(), rr.content, rr.ttl, rr.priority);
207 }
208 }
209 catch( AhuException &ae )
210 {
211 cerr << "Fatal error: " << ae.reason << endl;
212 if( !args.mustDo( "resume" ) )
213 {
214 return 1;
215 }
216 }
217 }
218 }
219 else
220 {
221 if( args["zone-file"].empty() || args["zone-name"].empty() )
222 {
223 cerr << "Error: At least zone-file and zone-name are required" << endl;
224 return 1;
225 }
226
227 g_zonename = args["zone-name"];
228 ZoneParserTNG zpt(args["zone-file"], args["zone-name"]);
229 DNSResourceRecord rr;
230 while(zpt.get(rr))
231 callback(0, rr.qname, rr.qtype.getName(), rr.content, rr.ttl, rr.priority);
232 }
233 }
234 catch( AhuException &ae )
235 {
236 cerr << "Fatal error: " << ae.reason << endl;
237 return 1;
238 }
239 catch( std::exception &e )
240 {
241 cerr << "Died because of STL error: " << e.what() << endl;
242 return 1;
243 }
244 catch( ... )
245 {
246 cerr << "Died because of unknown exception" << endl;
247 return 1;
248 }
249
250 return 0;
ad26538e 251}