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