2010-03-24 08:50:50 +03:00
#!/usr/bin/env python
2010-02-25 07:12:53 +03:00
#
# update our DNS names using TSIG-GSS
#
# Copyright (C) Andrew Tridgell 2010
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
2010-03-09 15:34:10 +03:00
import fcntl
2010-02-25 07:12:53 +03:00
import sys
import tempfile
2010-11-15 11:09:14 +03:00
import subprocess
2010-02-25 07:12:53 +03:00
2010-03-03 06:28:42 +03:00
# ensure we get messages out immediately, so they get in the samba logs,
# and don't get swallowed by a timeout
os.putenv('PYTHONUNBUFFERED', '1')
2010-09-29 07:43:58 +04:00
# forcing GMT avoids a problem in some timezones with kerberos. Both MIT
# heimdal can get mutual authentication errors due to the 24 second difference
# between UTC and GMT when using some zone files (eg. the PDT zone from
# the US)
os.putenv("TZ", "GMT")
2010-02-25 07:12:53 +03:00
# Find right directory when running from source tree
sys.path.insert(0, "bin/python")
import samba
import optparse
2010-03-29 18:08:11 +04:00
from samba import getopt as options
from ldb import SCOPE_BASE
2010-02-25 07:12:53 +03:00
from samba.auth import system_session
2010-02-26 05:30:44 +03:00
from samba.samdb import SamDB
2010-09-19 07:57:26 +04:00
from samba.dcerpc import netlogon, winbind
2010-03-05 03:45:10 +03:00
2010-04-01 17:20:25 +04:00
samba.ensure_external_module("dns", "dnspython")
2010-11-30 07:23:39 +03:00
import dns.resolver
import dns.exception
2010-02-26 05:30:44 +03:00
default_ttl = 900
2010-09-19 07:57:26 +04:00
am_rodc = False
2010-11-15 11:09:14 +03:00
error_count = 0
2010-02-26 05:30:44 +03:00
parser = optparse.OptionParser("samba_dnsupdate")
sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)
parser.add_option_group(options.VersionOptions(parser))
parser.add_option("--verbose", action="store_true")
2010-09-20 00:02:05 +04:00
parser.add_option("--all-names", action="store_true")
2010-03-09 15:34:10 +03:00
parser.add_option("--all-interfaces", action="store_true")
parser.add_option("--use-file", type="string", help="Use a file, rather than real DNS calls")
2010-09-28 08:07:17 +04:00
parser.add_option("--update-list", type="string", help="Add DNS names from the given file")
2010-11-15 11:09:14 +03:00
parser.add_option("--fail-immediately", action='store_true', help="Exit on first failure")
2010-02-26 05:30:44 +03:00
creds = None
ccachename = None
opts, args = parser.parse_args()
if len(args) != 0:
parser.print_usage()
sys.exit(1)
lp = sambaopts.get_loadparm()
domain = lp.get("realm")
host = lp.get("netbios name")
2010-03-09 15:34:10 +03:00
if opts.all_interfaces:
all_interfaces = True
else:
all_interfaces = False
2010-04-04 05:30:03 +04:00
IPs = samba.interface_ips(lp, all_interfaces)
2010-02-26 05:30:44 +03:00
nsupdate_cmd = lp.get('nsupdate command')
if len(IPs) == 0:
print "No IP interfaces - skipping DNS updates"
sys.exit(0)
2010-09-19 07:57:26 +04:00
if opts.verbose:
print "IPs: %s" % IPs
2010-02-26 05:30:44 +03:00
########################################################
# get credentials if we haven't got them already
def get_credentials(lp):
2010-09-16 08:13:48 +04:00
from samba import credentials
2010-02-26 05:30:44 +03:00
global ccachename, creds
if creds is not None:
return
2010-09-16 08:13:48 +04:00
creds = credentials.Credentials()
2010-02-26 05:30:44 +03:00
creds.guess(lp)
2010-04-08 23:01:17 +04:00
creds.set_machine_account(lp)
2010-09-16 08:13:48 +04:00
creds.set_krb_forwardable(credentials.NO_KRB_FORWARDABLE)
2010-02-26 05:30:44 +03:00
(tmp_fd, ccachename) = tempfile.mkstemp()
creds.get_named_ccache(lp, ccachename)
#############################################
# an object to hold a parsed DNS line
class dnsobj(object):
2010-03-09 15:34:10 +03:00
def __init__(self, string_form):
list = string_form.split()
2010-02-26 05:30:44 +03:00
self.dest = None
self.port = None
self.ip = None
2010-02-26 05:58:32 +03:00
self.existing_port = None
self.existing_weight = None
2010-03-09 15:34:10 +03:00
self.type = list[0]
2010-09-28 08:07:17 +04:00
self.name = list[1].lower()
2010-03-09 15:34:10 +03:00
if self.type == 'SRV':
2010-09-28 08:07:17 +04:00
self.dest = list[2].lower()
2010-03-09 15:34:10 +03:00
self.port = list[3]
elif self.type == 'A':
self.ip = list[2] # usually $IP, which gets replaced
elif self.type == 'CNAME':
2010-09-28 08:07:17 +04:00
self.dest = list[2].lower()
2010-03-09 15:34:10 +03:00
else:
print "Received unexpected DNS reply of type %s" % self.type
raise
2010-02-26 05:30:44 +03:00
def __str__(self):
2010-03-09 15:34:10 +03:00
if d.type == "A": return "%s %s %s" % (self.type, self.name, self.ip)
if d.type == "SRV": return "%s %s %s %s" % (self.type, self.name, self.dest, self.port)
if d.type == "CNAME": return "%s %s %s" % (self.type, self.name, self.dest)
2010-02-26 05:30:44 +03:00
################################################
# parse a DNS line from
def parse_dns_line(line, sub_vars):
subline = samba.substitute_var(line, sub_vars)
2010-03-09 15:34:10 +03:00
d = dnsobj(subline)
2010-02-26 05:30:44 +03:00
return d
############################################
# see if two hostnames match
def hostname_match(h1, h2):
h1 = str(h1)
h2 = str(h2)
return h1.lower().rstrip('.') == h2.lower().rstrip('.')
############################################
# check that a DNS entry exists
def check_dns_name(d):
2010-02-26 05:58:32 +03:00
normalised_name = d.name.rstrip('.') + '.'
2010-02-26 05:30:44 +03:00
if opts.verbose:
2010-02-26 05:58:32 +03:00
print "Looking for DNS entry %s as %s" % (d, normalised_name)
2010-03-09 15:34:10 +03:00
if opts.use_file is not None:
try:
dns_file = open(opts.use_file, "r")
except IOError:
return False
2010-04-19 11:18:20 +04:00
for line in dns_file:
line = line.strip()
2010-04-27 12:24:52 +04:00
if line == '' or line[0] == "#":
2010-03-09 15:34:10 +03:00
continue
if line.lower() == str(d).lower():
return True
return False
2010-02-26 05:30:44 +03:00
try:
2010-11-30 07:23:39 +03:00
ans = dns.resolver.query(normalised_name, d.type)
except dns.exception.DNSException:
if opts.verbose:
print "Failed to find DNS entry %s" % d
2010-02-26 05:30:44 +03:00
return False
if d.type == 'A':
# we need to be sure that our IP is there
for rdata in ans:
if str(rdata) == str(d.ip):
return True
if d.type == 'CNAME':
for i in range(len(ans)):
if hostname_match(ans[i].target, d.dest):
return True
if d.type == 'SRV':
2010-02-26 05:58:32 +03:00
for rdata in ans:
2010-02-26 05:30:44 +03:00
if opts.verbose:
print "Checking %s against %s" % (rdata, d)
2010-02-26 05:58:32 +03:00
if hostname_match(rdata.target, d.dest):
if str(rdata.port) == str(d.port):
return True
else:
d.existing_port = str(rdata.port)
d.existing_weight = str(rdata.weight)
2010-11-30 07:23:39 +03:00
2010-02-26 05:30:44 +03:00
if opts.verbose:
2010-11-30 07:23:39 +03:00
print "Failed to find matching DNS entry %s" % d
2010-03-09 15:34:10 +03:00
2010-02-26 05:30:44 +03:00
return False
###########################################
# get the list of substitution vars
def get_subst_vars():
2010-09-19 07:57:26 +04:00
global lp, am_rodc
2010-02-26 05:30:44 +03:00
vars = {}
2010-04-04 05:30:03 +04:00
samdb = SamDB(url=lp.get("sam database"), session_info=system_session(),
2010-04-08 23:01:17 +04:00
lp=lp)
2010-02-26 05:30:44 +03:00
vars['DNSDOMAIN'] = lp.get('realm').lower()
2010-09-19 07:57:26 +04:00
vars['DNSFOREST'] = lp.get('realm').lower()
2010-02-26 05:30:44 +03:00
vars['HOSTNAME'] = lp.get('netbios name').lower() + "." + vars['DNSDOMAIN']
vars['NTDSGUID'] = samdb.get_ntds_GUID()
vars['SITE'] = samdb.server_site_name()
res = samdb.search(base=None, scope=SCOPE_BASE, attrs=["objectGUID"])
guid = samdb.schema_format_value("objectGUID", res[0]['objectGUID'][0])
vars['DOMAINGUID'] = guid
2010-09-19 07:57:26 +04:00
am_rodc = samdb.am_rodc()
2010-02-26 05:30:44 +03:00
return vars
############################################
# call nsupdate for an entry
def call_nsupdate(d):
global ccachename, nsupdate_cmd
if opts.verbose:
print "Calling nsupdate for %s" % d
2010-03-09 15:34:10 +03:00
if opts.use_file is not None:
wfile = open(opts.use_file, 'a')
fcntl.lockf(wfile, fcntl.LOCK_EX)
wfile.write(str(d)+"\n")
fcntl.lockf(wfile, fcntl.LOCK_UN)
return
2010-09-28 08:07:17 +04:00
normalised_name = d.name.rstrip('.') + '.'
2010-02-26 05:30:44 +03:00
(tmp_fd, tmpfile) = tempfile.mkstemp()
f = os.fdopen(tmp_fd, 'w')
if d.type == "A":
2010-09-28 08:07:17 +04:00
f.write("update add %s %u A %s\n" % (normalised_name, default_ttl, d.ip))
2010-02-26 05:30:44 +03:00
if d.type == "SRV":
2010-02-26 05:58:32 +03:00
if d.existing_port is not None:
2010-09-28 08:07:17 +04:00
f.write("update delete %s SRV 0 %s %s %s\n" % (normalised_name, d.existing_weight,
2010-02-26 05:58:32 +03:00
d.existing_port, d.dest))
2010-09-28 08:07:17 +04:00
f.write("update add %s %u SRV 0 100 %s %s\n" % (normalised_name, default_ttl, d.port, d.dest))
2010-02-26 05:30:44 +03:00
if d.type == "CNAME":
2010-09-28 08:07:17 +04:00
f.write("update add %s %u CNAME %s\n" % (normalised_name, default_ttl, d.dest))
2010-02-26 05:30:44 +03:00
if opts.verbose:
f.write("show\n")
f.write("send\n")
f.close()
os.putenv("KRB5CCNAME", ccachename)
2010-11-15 11:09:14 +03:00
try:
cmd = "%s %s" % (nsupdate_cmd, tmpfile)
subprocess.check_call(cmd, shell=True)
2010-11-17 04:33:02 +03:00
except Exception, estr:
2010-11-15 11:09:14 +03:00
global error_count
if opts.fail_immediately:
sys.exit(1)
error_count = error_count + 1
2010-11-17 04:33:02 +03:00
if opts.verbose:
print("Failed nsupdate: %s : %s" % (str(d), estr))
2010-02-26 05:30:44 +03:00
os.unlink(tmpfile)
2010-09-19 07:57:26 +04:00
def rodc_dns_update(d, t):
'''a single DNS update via the RODC netlogon call'''
global sub_vars
if opts.verbose:
print "Calling netlogon RODC update for %s" % d
2010-09-20 00:02:40 +04:00
typemap = {
netlogon.NlDnsLdapAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsGcAtSite : netlogon.NlDnsDomainNameAlias,
netlogon.NlDnsDsaCname : netlogon.NlDnsDomainNameAlias,
netlogon.NlDnsKdcAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsDcAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsRfc1510KdcAtSite : netlogon.NlDnsInfoTypeNone,
netlogon.NlDnsGenericGcAtSite : netlogon.NlDnsDomainNameAlias
}
2010-09-19 07:57:26 +04:00
w = winbind.winbind("irpc:winbind_server", lp)
dns_names = netlogon.NL_DNS_NAME_INFO_ARRAY()
dns_names.count = 1
name = netlogon.NL_DNS_NAME_INFO()
name.type = t
2010-09-20 00:02:40 +04:00
name.dns_domain_info_type = typemap[t]
2010-09-19 07:57:26 +04:00
name.priority = 0
name.weight = 0
if d.port is not None:
name.port = int(d.port)
name.dns_register = True
dns_names.names = [ name ]
site_name = sub_vars['SITE'].decode('utf-8')
2010-11-18 06:53:20 +03:00
global error_count
2010-09-19 07:57:26 +04:00
try:
ret_names = w.DsrUpdateReadOnlyServerDnsRecords(site_name, default_ttl, dns_names)
if ret_names.names[0].status != 0:
print("Failed to set DNS entry: %s (status %u)" % (d, ret_names.names[0].status))
2010-11-18 06:53:20 +03:00
error_count = error_count + 1
2010-09-20 00:02:40 +04:00
except RuntimeError, reason:
print("Error setting DNS entry of type %u: %s: %s" % (t, d, reason))
2010-11-18 06:53:20 +03:00
error_count = error_count + 1
if error_count != 0 and opts.fail_immediately:
sys.exit(1)
2010-09-19 07:57:26 +04:00
def call_rodc_update(d):
'''RODCs need to use the netlogon API for nsupdate'''
global lp, sub_vars
# we expect failure for 3268 if we aren't a GC
if d.port is not None and int(d.port) == 3268:
return
# map the DNS request to a netlogon update type
map = {
2010-09-20 00:02:40 +04:00
netlogon.NlDnsLdapAtSite : '_ldap._tcp.${SITE}._sites.${DNSDOMAIN}',
2010-09-19 07:57:26 +04:00
netlogon.NlDnsGcAtSite : '_ldap._tcp.${SITE}._sites.gc._msdcs.${DNSDOMAIN}',
netlogon.NlDnsDsaCname : '${NTDSGUID}._msdcs.${DNSFOREST}',
netlogon.NlDnsKdcAtSite : '_kerberos._tcp.${SITE}._sites.dc._msdcs.${DNSDOMAIN}',
netlogon.NlDnsDcAtSite : '_ldap._tcp.${SITE}._sites.dc._msdcs.${DNSDOMAIN}',
netlogon.NlDnsRfc1510KdcAtSite : '_kerberos._tcp.${SITE}._sites.${DNSDOMAIN}',
netlogon.NlDnsGenericGcAtSite : '_gc._tcp.${SITE}._sites.${DNSFOREST}'
}
for t in map:
subname = samba.substitute_var(map[t], sub_vars)
if subname.lower() == d.name.lower():
# found a match - do the update
rodc_dns_update(d, t)
return
if opts.verbose:
print("Unable to map to netlogon DNS update: %s" % d)
2010-02-26 05:30:44 +03:00
# get the list of DNS entries we should have
2010-09-28 08:07:17 +04:00
if opts.update_list:
dns_update_list = opts.update_list
else:
dns_update_list = lp.private_path('dns_update_list')
2010-02-26 05:30:44 +03:00
2010-09-27 04:40:05 +04:00
# use our private krb5.conf to avoid problems with the wrong domain
# bind9 nsupdate wants the default domain set
krb5conf = lp.private_path('krb5.conf')
os.putenv('KRB5_CONFIG', krb5conf)
2010-02-26 05:30:44 +03:00
file = open(dns_update_list, "r")
# get the substitution dictionary
sub_vars = get_subst_vars()
# build up a list of update commands to pass to nsupdate
update_list = []
dns_list = []
# read each line, and check that the DNS name exists
2010-04-27 12:24:52 +04:00
for line in file:
line = line.strip()
if line == '' or line[0] == "#":
2010-02-26 05:30:44 +03:00
continue
d = parse_dns_line(line, sub_vars)
dns_list.append(d)
# now expand the entries, if any are A record with ip set to $IP
# then replace with multiple entries, one for each interface IP
for d in dns_list:
if d.type == 'A' and d.ip == "$IP":
d.ip = IPs[0]
for i in range(len(IPs)-1):
2010-11-15 02:54:50 +03:00
d2 = dnsobj(str(d))
2010-02-26 05:30:44 +03:00
d2.ip = IPs[i+1]
dns_list.append(d2)
# now check if the entries already exist on the DNS server
for d in dns_list:
2010-09-20 00:02:05 +04:00
if opts.all_names or not check_dns_name(d):
2010-02-26 05:30:44 +03:00
update_list.append(d)
if len(update_list) == 0:
if opts.verbose:
print "No DNS updates needed"
sys.exit(0)
# get our krb5 creds
get_credentials(lp)
# ask nsupdate to add entries as needed
for d in update_list:
2010-09-19 07:57:26 +04:00
if am_rodc:
2010-09-30 04:33:49 +04:00
if d.name.lower() == domain.lower():
continue
if d.type != 'A':
call_rodc_update(d)
else:
call_nsupdate(d)
2010-09-19 07:57:26 +04:00
else:
call_nsupdate(d)
2010-02-26 05:30:44 +03:00
# delete the ccache if we created it
if ccachename is not None:
os.unlink(ccachename)
2010-11-15 11:09:14 +03:00
2010-11-17 04:33:02 +03:00
if error_count != 0:
print("Failed update of %u entries" % error_count)
2010-11-15 11:09:14 +03:00
sys.exit(error_count)