summaryrefslogtreecommitdiffstats
path: root/skripti/zone/update.py
blob: ead2c393f8e9238c830c288843e74056166e06cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python3
import dns.zone
import dns.resolver
import dns.update
import dns.tsigkeyring
import json
import sys
import math
with open(sys.argv[1], "r") as db:
	lines = db.readlines()
newconfig = json.loads(lines.pop(0)[:-1])
domena = newconfig["d"]
strežniki = [dns.resolver.resolve(domena, "SOA")[0].mname]
naslovi = []
for strežnik in strežniki:
	for i in dns.resolver.resolve(strežnik, "AAAA"):
		naslovi.append(i.address)
	for i in dns.resolver.resolve(strežnik, "A"):
		naslovi.append(i.address)
for naslov in naslovi: # opcijsko dodaj tule kakšen try catch
	zone = None
	zone = dns.zone.from_xfr(dns.query.xfr(naslov, domena))
	if zone != None:
		break
config = None
try:
	config = json.loads(b''.join(zone["_urejevalnik"].get_rdataset(dns.rdataclass.IN, dns.rdatatype.TXT)[0].strings).decode())
except KeyError:
	pass
except json.decoder.JSONDecodeError:
	pass
if config == None:
	config = {"v": 0, "d": domena, "c": {}, "t": 1, "+": 100, "i": {}}
rrs = []
for r in zone.iterate_rdatas():
	if r[0].to_unicode() == "_urejevalnik" or r[2].rdtype in [dns.rdatatype.RRSIG, dns.rdatatype.NSEC, dns.rdatatype.NSEC3, dns.rdatatype.DNSKEY]:
		continue
	commentkey = r[0].to_unicode() + " " + r[2].rdtype.name
	komentar = ""
	if commentkey in config["c"].keys():
		komentar = config["c"][commentkey]
		del config["c"][commentkey]
	konec = "\t"
	if r[0].to_unicode() in config["i"].keys():
		konec = config["i"][r[0].to_unicode()]
	vrednost = ""
	if r[2].rdtype == dns.rdatatype.TXT:
		for string in r[2].strings:
			bajti = b''
			for char in string:
				if char < ord(b' '):
					bajti += b'\\' + ("%03d" % ord(char)).encode()
				else:
					bajti += bytes([char])
			niz = ""
			for znak in bajti.replace(b'\\', b'\\\\').replace(b'"', b'\\"').decode('utf-8', errors='surrogateescape'):
				if '\udc80' <= znak <= '\udcff':
					niz += '\\'+("%03d" % (ord(znak)-0xdc00))
				else:
					niz += znak
			vrednost = '"' + niz + '"'
	else:
		vrednost = r[2].to_text()
	rrs.append((r[1], r[0].to_unicode(), komentar, None, r[2].rdclass, r[2].rdtype, vrednost))
komentar = ""
lineno = 1
novikomentarji = {}
novikonci = {}
keyring = None
plus = 0
minus = 0
if len(sys.argv) == 3:
	with open(sys.argv[2]) as file:
		ključ = file.read()
	keyring = dns.tsigkeyring.from_text({ključ.split()[ključ.split().index("key")+1].replace('"', "").replace(";", ""): ključ.split()[ključ.split().index("secret")+1].replace('"', "").replace(";", "")})
update = dns.update.Update(domena, keyring=keyring)
while True:
	try:
		lineno += 1
		line = lines.pop(0)[:-1] # odstranimo zadnji \n, ki ga zraven da .readlines
		if len(line.split()) == 0 or line[0] == ';' or line[0] == '#' or line[0] == '/':
			komentar += line + "\n"
			continue
		ime = line.split()[0]
		konec = ""
		index = len(ime)
		while line[index] in ["\t", " "]:
			konec += line[index]
			index += 1
		nizi = line.split()[1:]
		tip = None
		razred = None
		ttl = None
		while tip == None:
			try:
				ttl = int(nizi[0])
				nizi.pop(0)
			except ValueError:
				pass
			try:
				razred = dns.rdataclass.from_text(nizi[0])
				nizi.pop(0)
			except dns.rdataclass.UnknownRdataclass:
				pass
			try:
				tip = dns.rdatatype.from_text(nizi[0])
				for i in [" ", "\t"]:
					for j in [" ", "\t"]:
						try:
							datastart = line.index(i+nizi[0]+j)+len(i+nizi[0]+i)
						except ValueError:
							continue
						break
					else:
						continue
					break
				nizi.pop(0)
			except dns.rdatatype.UnknownRdatatype:
				pass
		if tip == None:
			print(f"NAPAKA: na vrstici {lineno} ne najdem tipa zapisa. Vrstica je lahko bodisi komentar, ki se začne z ';', bodisi je v obliki IME [TTL={newconfig['t']}] [CLASS=IN] TIP PODATKI.")
			print(f"Vsebina neveljavne vrstice: " + line)
			sys.exit(1)
		while line[datastart] in [" ", "\t"]:
			datastart += 1
		data = line[datastart:]
		if razred == None:
			razred = dns.rdataclass.IN
		if ttl == None:
			ttl = newconfig["t"]
		ime = dns.name.from_unicode(ime, dns.name.from_unicode(domena)).choose_relativity(dns.name.from_unicode(domena), True).to_unicode()
		if tip == dns.rdatatype.SOA:
			data = data.split()
			data[2] = str(int(data[2])+newconfig["+"])
			data = " ".join(data)
		tapl = (ttl, ime, komentar, None, razred, tip, data)
		if komentar != "":
			novikomentarji[ime + " " + tip.to_text(tip)] = komentar
		if konec != "\t":
			novikonci[ime] = konec
		if not tapl in rrs:
			print("+ " + komentar.replace("\n", "\n+ ") + ime + konec + str(ttl) + "\t" + razred.to_text(razred) + "\t" + tip.to_text(tip) + "\t" + data)
			plus += 1
			update.add(ime, ttl, tip, data)
		else:
			rrs.remove(tapl)
		komentar = ""
	except IndexError:
		break
obstoječ = "" # zadnji komentar
for komentar in config["c"].values():
	obstoječ += komentar
for rr in rrs:
	print("- " + komentar.replace("\n", "\n- ") + rr[1] + konec + str(rr[0]) + "\t" + rr[4].to_text(rr[4]) + "\t" + rr[5].to_text(rr[5]) + "\t" + rr[6])
	minus += 1
	update.delete(rr[1], rr[5].to_text(rr[5]), rr[6])
if obstoječ != komentar:
	print("- " + "\n+ ".join(obstoječ.split("\n")))
	print("+ " + "\n+ ".join(komentar.split("\n")))
	plus += 1
	minus += 1
novikomentarji["z"] = komentar
newconfig["c"] = novikomentarji
newconfig["i"] = novikonci
odziv = input(f"(-{minus}/+{plus}) Ali želite te spremembe poslati na strežnik? [Y/D/J/n] ")
if len(odziv) != 0 and odziv[0] in ["n", "N", "0", "f", "F"]:
	print("Prekinjam. Nasvidenje!")
	sys.exit(0)
jason = json.dumps(newconfig)
jasonsplit = " ".join(['"' + jason[i*255:i*255+255].replace("\\", "\\\\").replace('"', '\\"') + '"' for i in range(math.ceil(len(jason)/255))])
update.replace("_urejevalnik", 1, dns.rdatatype.TXT, jasonsplit)
response = dns.query.tcp(update, naslov)
print("Poslal zahtevo. Odziv strežnika:")
print(response)