diff --git a/dyndomain b/dyndomain index 8dce999..a9dea32 100755 --- a/dyndomain +++ b/dyndomain @@ -75,7 +75,7 @@ def full_name(host, domain): def make_zone_list(wan_hostname, wan_addr, hosts, hosts_nat, domain): zone = [] - + wan_hostname = full_name(wan_hostname, domain) if wan_addr['ipv4'] != '': @@ -103,6 +103,10 @@ def read_zone_list_from_file(zone_filename): return zone_list +def make_delete_zone_list(zone_list, prev_zone_list): + return make_update_zone_list(prev_zone_list, zone_list) + + def make_update_zone_list(zone_list, prev_zone_list): update_zone_list = [] for entry in zone_list: @@ -128,18 +132,20 @@ def log(msg): logfile.write("%s - %s\n" % (stamp, msg)) -def log_update_zone(zone_list): +def log_update_zone(zone_list, delete_zone_list): for host,typea,addr in zone_list: log("%-20s %-6s %s" % (host, typea, addr)) + for host,typea,addr in delete_zone_list: + log("DELETE: %-20s %-6s %s" % (host, typea, addr)) -def ovh_update_zone(domain, zone_list): - if not len(zone_list): +def ovh_update_zone(domain, update_zone_list, delete_zone_list): + if not len(update_zone_list) and not len(delete_zone_list): return False client = ovh.Client() - for host, fieldtype, target in zone_list: + for host, fieldtype, target in update_zone_list: result = client.get('/domain/zone/%s/record' % domain, fieldType=fieldtype, subDomain=host) @@ -156,15 +162,24 @@ def ovh_update_zone(domain, zone_list): client.put('/domain/zone/%s/record/%ld' % (domain, id), target=target) + for host, fieldtype, target in delete_zone_list: + result = client.get('/domain/zone/%s/record' % domain, + fieldType=fieldtype, + subDomain=host) + if len(result) == 0: + continue + id = result[0] + #print("Delete entry for %s %s %s" % (host, fieldtype, target)) + client.delete('/domain/zone/%s/record/%d' % (domain, id)) #print("Refresh zone %s" % domain) client.post('/domain/zone/%s/refresh' % domain) return True -def send_update_mail(mail_to, mail_from, zone_domain, update_zone_list, wan): +def send_update_mail(mail_to, mail_from, zone_domain, update_zone_list, delete_zone_list, wan): #print('Send email to %s' % mail_to) msg = EmailMessage() - + msg['Subject'] = "Livebox update in %s" % zone_domain msg['From'] = mail_from msg['To' ] = mail_to @@ -174,9 +189,15 @@ def send_update_mail(mail_to, mail_from, zone_domain, update_zone_list, wan): txt = txt + "WAN IPv6 : %s\n" % wan['ipv6'] txt = txt + "\nZone %s has been updated:\n" % zone_domain - + for host,tp,addr in update_zone_list: txt = txt + " %-20s %-4s %s\n" % (host,tp,addr) + + txt = txt + "\nRemoved entries:\n" + + for host,tp,addr in delete_zone_list: + txt = txt + " %-20s %-4s %s\n" % (host,tp,addr) + txt = txt + '\n' msg.set_content(txt) @@ -184,7 +205,7 @@ def send_update_mail(mail_to, mail_from, zone_domain, update_zone_list, wan): s = smtplib.SMTP('localhost') s.send_message(msg) s.quit() - + load_conf() @@ -193,13 +214,17 @@ if not ping(wan_hostname): sys.exit(0) sysbus.load_conf() -sysbus.auth() +r = sysbus.auth(False) +if not r: + print('Error: cannot authenticate on livebox') + sys.exit(1) hosts = get_ipv6_hosts() wan = get_wan_addr() zone_list = make_zone_list(wan_hostname, wan, hosts, hosts_ipv4_nat_list, zone_subdomain) prev_zone_list = read_zone_list_from_file(zone_filename) +delete_zone_list = make_delete_zone_list(zone_list, prev_zone_list) update_zone_list = make_update_zone_list(zone_list, prev_zone_list) #print('zone_list:') @@ -208,13 +233,15 @@ update_zone_list = make_update_zone_list(zone_list, prev_zone_list) #pprint(prev_zone_list) #print('update_zone_list:') #pprint(update_zone_list) +#print('delete_zone_list:') +#pprint(delete_zone_list) -log_update_zone(update_zone_list) +log_update_zone(update_zone_list, delete_zone_list) -sucess = ovh_update_zone(zone_domain, update_zone_list) +sucess = ovh_update_zone(zone_domain, update_zone_list, delete_zone_list) if sucess: - new_zone_list = write_zone_list_to_file(zone_filename, prev_zone_list+update_zone_list) - send_update_mail(mail_to, mail_from, zone_domain, update_zone_list, wan) + new_zone_list = write_zone_list_to_file(zone_filename, zone_list) + send_update_mail(mail_to, mail_from, zone_domain, update_zone_list, delete_zone_list, wan)