3 # update our DNS names using TSIG-GSS
5 # Copyright (C) Andrew Tridgell 2010
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 # Find right directory when running from source tree
28 sys.path.insert(0, "bin/python")
32 from samba import getopt as options, Ldb
33 from ldb import SCOPE_SUBTREE, SCOPE_BASE, LdbError
35 from samba import glue
36 from samba.auth import system_session
37 from samba.samdb import SamDB
41 parser = optparse.OptionParser("samba_dnsupdate")
42 sambaopts = options.SambaOptions(parser)
43 parser.add_option_group(sambaopts)
44 parser.add_option_group(options.VersionOptions(parser))
45 parser.add_option("--verbose", action="store_true")
50 opts, args = parser.parse_args()
56 lp = sambaopts.get_loadparm()
58 domain = lp.get("realm")
59 host = lp.get("netbios name")
60 IPs = glue.interface_ips(lp)
61 nsupdate_cmd = lp.get('nsupdate command')
64 print "No IP interfaces - skipping DNS updates"
69 ########################################################
70 # get credentials if we haven't got them already
71 def get_credentials(lp):
72 from samba.credentials import Credentials
73 global ccachename, creds
79 creds.set_machine_account(lp)
81 print "Failed to set machine account"
84 (tmp_fd, ccachename) = tempfile.mkstemp()
85 creds.get_named_ccache(lp, ccachename)
88 #############################################
89 # an object to hold a parsed DNS line
98 if d.type == "A": return "%s:%s:%s" % (self.type, self.name, self.ip)
99 if d.type == "SRV": return "%s:%s:%s:%s" % (self.type, self.name, self.dest, self.port)
100 if d.type == "CNAME": return "%s:%s:%s" % (self.type, self.name, self.dest)
103 ################################################
104 # parse a DNS line from
105 def parse_dns_line(line, sub_vars):
107 subline = samba.substitute_var(line, sub_vars)
108 list = subline.split()
115 d.ip = list[2] # usually $IP, which gets replaced
116 elif d.type == 'CNAME':
119 print "Received unexpected DNS reply of type %s" % d.type
123 ############################################
124 # see if two hostnames match
125 def hostname_match(h1, h2):
128 return h1.lower().rstrip('.') == h2.lower().rstrip('.')
131 ############################################
132 # check that a DNS entry exists
133 def check_dns_name(d):
135 print "Looking for DNS entry %s" % d
137 ans = dns.resolver.query(d.name, d.type)
138 except dns.resolver.NXDOMAIN:
141 # we need to be sure that our IP is there
143 if str(rdata) == str(d.ip):
145 if d.type == 'CNAME':
146 for i in range(len(ans)):
147 if hostname_match(ans[i].target, d.dest):
151 print "Got %u replies in SRV lookup for %s" % (len(ans), d.name)
152 for i in range(len(ans)):
155 print "Checking %s against %s" % (rdata, d)
156 if str(rdata.port) == str(d.port) and hostname_match(rdata.target, d.dest):
159 print "Failed to find DNS entry %s" % d
163 ###########################################
164 # get the list of substitution vars
165 def get_subst_vars():
169 samdb = SamDB(url=lp.get("sam database"), session_info=system_session(), lp=lp)
171 vars['DNSDOMAIN'] = lp.get('realm').lower()
172 vars['HOSTNAME'] = lp.get('netbios name').lower() + "." + vars['DNSDOMAIN']
173 vars['NTDSGUID'] = samdb.get_ntds_GUID()
174 vars['SITE'] = samdb.server_site_name()
175 res = samdb.search(base=None, scope=SCOPE_BASE, attrs=["objectGUID"])
176 guid = samdb.schema_format_value("objectGUID", res[0]['objectGUID'][0])
177 vars['DOMAINGUID'] = guid
181 ############################################
182 # call nsupdate for an entry
183 def call_nsupdate(d):
184 global ccachename, nsupdate_cmd
187 print "Calling nsupdate for %s" % d
188 (tmp_fd, tmpfile) = tempfile.mkstemp()
189 f = os.fdopen(tmp_fd, 'w')
191 f.write("update add %s %u A %s\n" % (d.name, default_ttl, d.ip))
193 f.write("update add %s %u SRV 0 100 %s %s\n" % (d.name, default_ttl, d.port, d.dest))
194 if d.type == "CNAME":
195 f.write("update add %s %u SRV %s\n" % (d.name, default_ttl, d.dest))
201 os.putenv("KRB5CCNAME", ccachename)
202 os.system("%s %s" % (nsupdate_cmd, tmpfile))
206 # get the list of DNS entries we should have
207 dns_update_list = lp.private_path('dns_update_list')
209 file = open(dns_update_list, "r")
211 # get the substitution dictionary
212 sub_vars = get_subst_vars()
214 # build up a list of update commands to pass to nsupdate
218 # read each line, and check that the DNS name exists
219 line = file.readline()
221 line = line.rstrip().lstrip()
223 line = file.readline()
225 d = parse_dns_line(line, sub_vars)
227 line = file.readline()
229 # now expand the entries, if any are A record with ip set to $IP
230 # then replace with multiple entries, one for each interface IP
232 if d.type == 'A' and d.ip == "$IP":
234 for i in range(len(IPs)-1):
239 # now check if the entries already exist on the DNS server
241 if not check_dns_name(d):
242 update_list.append(d)
244 if len(update_list) == 0:
246 print "No DNS updates needed"
252 # ask nsupdate to add entries as needed
253 for d in update_list:
256 # delete the ccache if we created it
257 if ccachename is not None:
258 os.unlink(ccachename)