]>
code.delx.au - monosys/blob - hacks/dnsctl
11 def increment_serial(line
):
12 current_serial
= re
.search(R
"\b\d\d*\b", line
).group(0)
14 current
= int(current_serial
)
15 current_num
= current
% 100
16 current_date
= (current
- current_num
) / 100
17 new_date
= int(datetime
.datetime
.now().strftime("%Y%m%d"))
18 if current_date
== new_date
:
19 next_num
= current_num
+ 1
24 raise ValueError("Too many serial changes today!")
25 new_serial
= str(new_date
) + str(next_num
).zfill(2)
26 line
= line
.replace(current_serial
, new_serial
)
31 source_ip
, source_port
, dest_ip
, dest_port
= os
.environ
["SSH_CONNECTION"].split()
32 line
= re
.sub(R
"\b\d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?\b", source_ip
, line
)
35 def update_dyndns(zonefile
, dnslabel
):
37 with
open(zonefile
, encoding
="utf-8") as f
:
39 if line
.find("Serial") >= 0:
40 line
= increment_serial(line
)
41 elif line
.find("DYNDNS") >= 0 and line
.find(dnslabel
) >= 0:
42 line
= replace_ip(line
)
45 with
open(zonefile
, "w", encoding
="utf-8") as f
:
51 def read_zonefile(zonefile
):
52 with
open(zonefile
, encoding
="utf-8") as f
:
53 sys
.stdout
.write(f
.read())
56 def write_zonefile(zonefile
):
57 data
= sys
.stdin
.read()
58 if not data
.strip().endswith("; END"):
59 print("Missing end of file marker -- ; END")
62 with
open(zonefile
, "w", encoding
="utf-8") as f
:
68 subprocess
.check_call(["sudo", "journalctl", "-u", "nsd", "-f"])
71 subprocess
.check_call(["sudo", "systemctl", "reload", "nsd"])
76 parser
= argparse
.ArgumentParser(description
="Edit zone files")
78 parser
.add_argument("--zonefile",
79 help="the zone file to operate on")
82 action_group
= parser
.add_mutually_exclusive_group(required
=True)
84 action_group
.add_argument("--logs", action
="store_true",
85 help="show bind logs")
87 action_group
.add_argument("--dyndns",
88 help="update the specified dnslabel with the SSH origin IP")
90 action_group
.add_argument("--read", action
="store_true",
91 help="print the zone file to stdout")
93 action_group
.add_argument("--write", action
="store_true",
94 help="save the contents of stdin to the zone file")
96 ssh_args
= os
.environ
.get("SSH_ORIGINAL_COMMAND", "--help").split()[1:]
97 args
= parser
.parse_args(ssh_args
)
99 if not args
.logs
and not args
.zonefile
:
100 print("Required parameter: --zonefile\n")
106 def parse_cmdline_args():
107 parser
= argparse
.ArgumentParser(description
="Edit zone files")
109 parser
.add_argument("--allow-zonefile", required
=True, action
="append",
110 help="specify allowed zone files")
112 parser
.add_argument("--allow-write", action
="store_true",
113 help="allow --write option")
115 parser
.add_argument("--allow-dyndns",
116 help="allow --dyndns option")
118 return parser
.parse_args()
121 cmdline_args
= parse_cmdline_args()
122 ssh_args
= parse_ssh_args()
124 if ssh_args
.zonefile
and ssh_args
.zonefile
not in cmdline_args
.allow_zonefile
:
125 print("The specified zonefile is not on the allowed list:", cmdline_args
.allow_zonefile
)
128 if ssh_args
.dyndns
and ssh_args
.dyndns
!= cmdline_args
.allow_dyndns
:
129 print("Dynamic DNS is only allowed for:", cmdline_args
.allow_dyndns
)
132 if ssh_args
.write
and not cmdline_args
.allow_write
:
133 print("Write to zonefile is not allowed")
145 update_dyndns(args
.zonefile
, args
.dyndns
)
147 read_zonefile(args
.zonefile
)
149 write_zonefile(args
.zonefile
)
151 assert False, "Bad action"
153 if __name__
== "__main__":