2 # ldd - DNS implementation in Python
3 # Copyright (C) 2006 Fredrik Tolf <fredrik@dolda2000.com>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 from getopt import getopt
24 from ldd import dbzone, rec, dn
27 print "usage: dnsdbtool [-d] DBDIR DBNAME [COMMAND [ARGS...]]"
28 print " dnsdbtool -c DBDIR DBNAME"
34 opts, args = getopt(sys.argv[1:], "cd")
37 dbzone.dnsdb.create(args[0], args[1])
42 db = dbzone.dnsdb(args[0], args[1])
46 class error(Exception):
47 def __init__(self, text):
53 def rrusage(name, syn):
56 u += " " + e[1].upper()
57 return "usage: " + name + u
60 rtype = rec.rtypebyname(line[0])
62 raise error("no such RR type " + line[0])
63 rtype = rec.rtypebyid(rtype)
65 if(len(line) != len(syn) + 1):
66 raise error(rrusage(rtype[1], syn))
67 return rec.rrdata(*line)
102 def assertargs(cmd, line, num, usage):
104 raise error("usage: " + cmd + " " + usage)
106 def runcommand(line):
107 global currrset, curname
111 assertargs(cmd, line, 3, "[-f FLAG] NAME TTL RR RRPARAM...")
112 opts, line = getopt(line, "f:")
114 head = rec.rrhead(dn.fromstring(line[0]), line[2])
116 data = mkrr(line[2:])
117 rr = rec.rr(head, ttl, data)
118 if "-f" in opts: rr.setflags(opts["-f"].split(","))
119 db.addrr(head.name, rr)
120 elif cmd == "rmname":
121 assertargs(cmd, line, 1, "NAME")
122 name = dn.fromstring(line[0])
125 assertargs(cmd, line, 2, "NAME RRTYPE")
126 name = dn.fromstring(line[0])
127 db.rmrtype(name, line[1])
128 elif cmd == "lsnames":
129 for name in db.listnames():
132 assertargs(cmd, line, 1, "NAME")
133 name = dn.fromstring(line[0])
134 rrset = db.lookup(name)
135 # dbzone.rootify(rrset, dn.fromstring("dolda2000.com."))
137 raise error("no such name in database")
141 assertargs(cmd, line, 1, "NAME")
142 name = dn.fromstring(line[0])
143 currrset = db.lookup(name)
148 assertargs(cmd, line, 2, "[-f FLAG] TTL RR RRPARAM...")
149 opts, line = getopt(line, "f:")
151 head = rec.rrhead(curname, line[1])
153 data = mkrr(line[1:])
154 rr = rec.rr(head, ttl, data)
155 if "-f" in opts: rr.setflags(opts["-f"].split(","))
159 raise error("no RRset loaded")
160 for i, rr in enumerate(currrset):
161 print str(i) + ": " + str(rr)
163 assertargs(cmd, line, 1, "ID")
165 currrset[rrid:rrid + 1] = []
167 assertargs(cmd, line, 2, "ID NEWTTL")
170 currrset[rrid].head.ttl = ttl
172 assertargs(cmd, line, 3, "ID NAME DATA")
174 currrset[rrid].data[line[1]] = line[2]
176 assertargs(cmd, line, 2, "ID FLAGS...")
178 currrset[rrid].setflags(line[1:])
180 assertargs(cmd, line, 2, "ID FLAGS...")
182 currrset[rrid].clrflags(line[1:])
188 db.set(name, currrset)
189 elif cmd == "?" or cmd == "help":
190 print "Available commands:"
191 print "addnm, rmname, rmrt, lsnames, lsrr, load, add, ls, rm,"
192 print "chttl, chdt, sf, cf, store"
194 print "no such command: " + cmd
198 if sys.stdin.isatty():
199 sys.stderr.write("> ")
200 line = sys.stdin.readline()
202 if sys.stdin.isatty(): print
205 tokens = tokenize(line)
206 if len(tokens) > 0: runcommand(tokens)
208 sys.stderr.write(str(sys.exc_info()[1]) + "\n")
218 sys.stderr.write(str(sys.exc_info()[1]) + "\n")