Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 201 lines (176 sloc) 7.618 kb
1034616 initial commit on new repo. The old repo was contaminated with sensitive...
uberj authored
1 import database
f2632d1 various changes. One notable change was the reversal of all ip's in the ...
uberj authored
2 from utilities import ip2long, long2ip
1034616 initial commit on new repo. The old repo was contaminated with sensitive...
uberj authored
3 import pdb
4 import pprint
5 import printer
6 import re
7 import copy
8
9 class Reverse_Zone(object):
10 BUILD_DIR="./build"
11 SERIAL = 1
12
13
14 def __init__( self, cur, zone_fd, domain, dname ):
15 self.cur = cur
16 self.domain = domain
17 self.dname = dname
18 self.printer = printer.Printer( fd=zone_fd )
19
20 """
21 Strategy:
22 1) Build a tree of master and parent domains (masters are at the root).
23 2) Get all ip's needed into a list.
24 3) Do a right ordered traversal of the tree.
25 * For all the ip's, if an ip belongs in that domain add it and remove it from the list.
26
27
28 Walk through (right order traversal) the tree. SOA's should be generated and records should be removed from the record list.
29 1) Call walk_tree on all children
30 2) Determine if there is an SOA
31 * print SOA if you have one
32 3) Print NS asscoiated with domain
33 4) Print all ip's in domain. Remove those ip's
34 @param node: dictionary representing a tree
35 @param records: global record list (global to recursive stack)
36 """
37 def walk_tree( self, cur_domain, cur_dname, records ):
38 self.cur.execute("SELECT id, name FROM domain WHERE name LIKE '%.in-addr.arpa' AND master_domain="+str(cur_domain)+" ORDER BY name")
39 domains = self.cur.fetchall()
40 for domain in domains:
41 child_domain = domain[0]
42 child_dname = domain[1]
43 if self.check_for_SOA( child_domain, child_dname ):
44 rzone_fd = open( "%s/%s" % (Reverse_Zone.BUILD_DIR, child_dname), "w+")
45 new_rzone = Reverse_Zone( self.cur, rzone_fd, child_domain, child_dname )
46 new_rzone.gen_SOA( child_domain, child_dname ) # SOA ALWAYS has to be first thing.
47 new_rzone.walk_tree( child_domain, child_dname, records )
48 else:
49 self.walk_tree( child_domain, child_dname, records )
50 self.gen_domain( cur_domain, cur_dname, records )
51
52 """
53 SQL Wrapper
54 """
55 def get_dname( self, domain ):
56 cur.execute("SELECT name FROM domain WHERE id=%s" % (domain) )
57 return cur.fetchone()[0]
58
59 """
60 Go go through all the records and add them to the correct zone file.
61 1) If there is an SOA create a new Zone and call walk_tree.
62 """
63 def gen_domain( self, domain, dname, records ):
64 if domain == 0 or dname == "root":
65 print "Root domain, skipping"
66 return
67 else:
68 print "Generating %s" % (dname)
69 #if not re.search( "^10" ,self.ip_from_domainname(dname) ):
70 # return
71
72 self.gen_ORIGIN( domain, dname, 999 )
73 self.gen_NS( domain, dname )
f2632d1 various changes. One notable change was the reversal of all ip's in the ...
uberj authored
74 self.gen_ORIGIN( domain, 'in-addr.arpa', 999 )
1034616 initial commit on new repo. The old repo was contaminated with sensitive...
uberj authored
75 records_to_remove = []
7ecb5d3 @uberj fixed bug that was causing reverse ips to be dropped into the wrong reve...
authored
76 search_string = "^"+self.ip_from_domainname(dname).replace('.','\.')+"\."
1034616 initial commit on new repo. The old repo was contaminated with sensitive...
uberj authored
77 for record in records:
78 longip, name = record
79 ip = long2ip(longip)
80 # TODO compile this
81 if re.search( search_string, ip ):
82 self.printer.print_PTR( ip, name )
83 records_to_remove.append( record )
84
85 for record in records_to_remove:
86 records.remove(record)
87
88 self.gen_ORIGIN( domain, dname, 999 )
89
90 def gen_ORIGIN( self, domain, dname, ttl ):
91 origin = "$ORIGIN %s.\n" % (dname)
92 origin += "$TTL %s\n" % (ttl)
93 self.printer.print_raw( origin )
94
95 def gen_NS( self, domain, dname ):
96 self.cur.execute("SELECT * FROM `nameserver` WHERE `domain`='%s';" % (domain))
97 records = self.cur.fetchall()
98 self.printer.print_NS( '', [ record[1] for record in records ] )
99
100 """
101 This function may be redundant.
102 """
103 def check_for_SOA( self, domain, dname ):
104 self.cur.execute("SELECT * FROM `soa` WHERE `domain`='%s' ;" % (domain))
105 records = self.cur.fetchall() # Could use fetch one. Want to do check though.
106 if len(records) > 1:
107 self.printer.print_raw( ";Sanity Check failed\n" )
108 if not records:
109 # We don't have an SOA for this domain.
110 return False
111 else:
112 return True
113
114 def gen_SOA( self, domain, dname ):
115 self.cur.execute("SELECT * FROM `soa` WHERE `domain`='%s' ;" % (domain))
116 records = self.cur.fetchall() # Could use fetch one. Want to do check though.
117 if len(records) > 1:
118 self.printer.print_raw( ";Sanity Check failed" )
119 if not records:
120 # We don't have an SOA for this domain.
121 self.printer.print_raw( ";No soa for "+dname+" "+str(domain) )
122 return
123 record = records[0]
124 primary_master = record[2]
125 contact = record[3]
126 REFRESH = record[4]
127 RETRY = record[5]
128 EXPIRE = record[6]
129 MINIMUM = record[7] #TODO What is minimum, using TTL
eadea12 @uberj cleaning up the TTL update thing.
authored
130 self.printer.print_SOA( record[7], dname, primary_master, contact, Reverse_Zone.SERIAL, REFRESH, RETRY, EXPIRE, MINIMUM )
1034616 initial commit on new repo. The old repo was contaminated with sensitive...
uberj authored
131
132 """
133 We need ip's from: host, ranges, and pointer.
134 """
135 def gen_all_records( self ):
136 # SQL is not like magic.
137 PTR_records = []
138 PTR_records += self.gen_host_records()
139 PTR_records += self.gen_dyn_records()
140 PTR_records += self.gen_pointer_records()
141 return PTR_records
142
143 def gen_pointer_records( self ):
144 self.cur.execute("SELECT ip, hostname FROM pointer WHERE type = 'reverse';")
145 return list(self.cur.fetchall())
146
147
148 def gen_host_records( self ):
149 self.cur.execute("SELECT host.ip, CONCAT(host.name, '.', domain.name) FROM host, domain WHERE host.ip != 0 AND host.domain = domain.id;")
150 return list(self.cur.fetchall())
151
152 def gen_dyn_records( self ):
153 self.cur.execute("SELECT start, end FROM ranges WHERE type='dynamic' ORDER by start")
154 ip_ranges = self.cur.fetchall()
155 PTR_records = []
156 for ip_range in ip_ranges:
157 for ip in range(ip_range[0],ip_range[1]+1):
158 PTR_records.append( (ip,long2ip(ip).replace('.','-')) )
159 return PTR_records
160
161
162 def build_tree( self, cur_domain, tree ):
163 self.cur.execute("SELECT id FROM domain WHERE name LIKE '%.in-addr.arpa' AND master_domain="+str(cur_domain))
164 domains = self.cur.fetchall()
165 for domain in domains:
166 parent_domain = domain[0]
167 parent_tree = {}
168 tree[parent_domain] = self.build_tree( parent_domain, parent_tree )
169 return tree
170
171 def ip_from_domainname( self, dname ):
172 ip_data = re.search("(\d+).(\d*).?(\d*).?(\d*).*",dname)
173 try:
174 octets = list(reversed(list(ip_data.groups(0)))) # reverse the list, remove all duplicates (set), make it a list again.
175 except NoneType:
176 pdb.set_trace()
177 while '' in octets:
178 octets.remove('')
179 #ip_mask = ( octets + (["0"] * 4) )[:4]
180 return '.'.join(octets)
181
182 """
183 print rz.ip_from_domainname( '139.201.199.in-addr.arpa' )
184 print rz.ip_from_domainname( '10.in-addr.arpa' )
185 print rz.ip_from_domainname( '193.128.in-addr.arpa' )
186 print rz.ip_from_domainname( '16.211.140.in-addr.arpa' )
187 pp.pprint( gen_all_records() )
188 #pp.pprint( rz.build_tree(0, {}) )
189 #rz.walk_tree( rz.build_tree(0, {}), rz.gen_all_records() )
190 records = rz.gen_all_records()
191 before = len(records)
192 rz.walk_tree( 0, 'root', records )
193 print "id="+str(id(records))
194 after = len(records)
195 for record in records:
196 print "%s %s" % (long2ip(record[0]), record[1])
197 #walk_tree( build_tree(0, '', {}), [])
198 print before
199 print after
200 """
Something went wrong with that request. Please try again.