def delete_static(request, domain, entry): if domain.endswith('.'): domain = domain[:-1] try: domain = Domain.user_objects(request.user).get(name=domain) except Domain.DoesNotExist: raise Http404 try: instance = DNSEntryCache.user_objects(request.user).get(pk=int(entry)) except DNSEntryCache.DoesNotExist: raise Http404 form = None if request.method == 'POST': form = ConfirmDeleteForm(request.POST) print(form['confirmed']) if form.is_valid(): dnsutils.doUpdate(domain.master, domain.tsig_key, domain.tsig_type, domain.name, False, 'delete', str(instance.ttl), instance.type, instance.fqdn, instance.data) instance.delete() messages.success( request, "Successfully deleted entry %s %s %s %s" % (instance.fqdn, instance.ttl, instance.type, instance.data)) return redirect('show_domain', domain.name) return render(request, 'manager/delete_static.html', { 'domain': domain, 'entry': instance, 'form': form })
def put(self, request, domain_id, pk, format=None): record = self.get_object(domain_id, pk) data = JSONParser().parse(request) serializer = DNSEntryCacheSerializer(record, data=data) if serializer.is_valid(): new_instance = serializer.save() try: dnsutils.doUpdate(record.domain.master, record.domain.tsig_key, record.domain.tsig_type, record.domain.name, False, 'delete', str(record.ttl), record.type, record.fqdn, record.data) dnsutils.doUpdate(new_instance.domain.master, new_instance.domain.tsig_key, new_instance.domain.tsig_type, new_instance.domain.name, False, 'add', str(new_instance.ttl), new_instance.type, new_instance.fqdn, new_instance.data) except (DNSException, dnsutils.DynDNSException): logger.exception("Failed to update value to dns, domain %s" % record.domain.name) transaction.rollback() return JSONResponse( {"detail": 'Cannot update values to DNS-server'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def add_static(request, domain): try: domain = Domain.user_objects(request.user).get(name=domain) except Domain.DoesNotExist: raise Http404 try: synchronize(domain) except: messages.error(request, "Cannot refresh dns-entries from server") form = None response_data = { 'dns_record_types': dnsutils.DNS_RECORD_TYPES, 'domain': domain, 'form': form } if request.method == 'POST': instance = DNSEntryCache() instance.domain = domain form = StaticEntryForm(request.POST, initial={'name': ''}, instance=instance) if form.is_valid(): response_data['form'] = form try: dnsutils.validate_data(form.cleaned_data['type'].strip(), form.cleaned_data['data'].strip(), form.cleaned_data['name'].strip()) except dnsutils.DNSRecordException as e: messages.error(request, str(e)) return render(request, 'manager/add_static.html', response_data) # Save data entry = form.save() # Add record also to dns-server dnsutils.doUpdate(domain.master, domain.tsig_key, domain.tsig_type, domain.name, False, 'add', str(form.cleaned_data['ttl']), form.cleaned_data['type'], entry.fqdn, form.cleaned_data['data']) messages.success( request, "Successfully added entry %s %s %s %s" % (entry.fqdn, entry.ttl, entry.type, entry.data)) return redirect('show_domain', domain.name) return render(request, 'manager/add_static.html', response_data)
def doUpdate(environ, start_response): status = '403 Forbidden' GET = urlparse.parse_qs(environ['QUERY_STRING']) addr = environ['REMOTE_ADDR'] errors = '' if 'secret' in GET: if len(GET['secret']) == 1: if GET['secret'][0] in settings.clients: client = settings.clients[GET['secret'][0]] if 'ip' in GET and len(GET['ip']) == 1: addr = GET['ip'][0].strip() if not utils.check_ipv4(addr): errors = "Only IPv4 currently supported!" status = "503 Service Unavailable" if not errors: try: old = socket.gethostbyname(client) dnsutils.doUpdate(settings.server, settings.keyfile, settings.origin, False, 'delete', '360', 'A', client, old) logger.info("Deleted old entry %s IN A 360 %s" % (client, old)) except socket.gaierror: # address not set pass except dnsutils.DynDNSException as e: logger.exception(e) errors = "%s: %s" % (str(e), traceback.format_exc()) status = '503 Service Unavailable' except Exception as e: logger.exception(e) errors = "%s: %s" % (str(e) or type(e), traceback.format_exc()) status = '503 Service Unavailable' if not errors: try: dnsutils.doUpdate(settings.server, settings.keyfile, settings.origin, False, 'update', '360', 'A', client, addr) status = '200 OK' logger.info("Added new entry %s IN A 360 %s" % (client, addr)) except dnsutils.DynDNSException as e: logger.exception(e) errors = "%s: %s" % (str(e) or type(e), traceback.format_exc()) status = '503 Service Unavailable' except Exception as e: logger.exception(e) errors = "%s: %s" % (str(e) or type(e), traceback.format_exc()) status = '503 Service Unavailable' start_response(status, [('Content-Type', 'text/plain')]) yield(status) yield("\n") if debugging: yield("%s" % errors) yield("\n")
def delete_client(client): """ Delete dyndns client :param request: :param client: :return: """ synchronize(client.domain) for entry in DNSEntryCache.objects.filter(domain=client.domain, name=client.name).all(): dnsutils.doUpdate(entry.domain.master, entry.domain.tsig_key, client.domain.tsig_type, entry.domain.fqdn, False, 'delete', str(entry.ttl), entry.type, entry.fqdn, entry.data) entry.delete()
def delete(self, request, domain_id, pk, format=None): record = self.get_object(domain_id, pk) try: dnsutils.doUpdate(record.domain.master, record.domain.tsig_key, record.domain.tsig_type, record.domain.name, False, 'delete', str(record.ttl), record.type, record.fqdn, record.data) except (DNSException, dnsutils.DynDNSException): logger.exception("Failed to update value to dns, domain %s" % record.domain.name) transaction.rollback() return JSONResponse( {"detail": 'Cannot delete record from DNS-server'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) record.delete() return HttpResponse(status=status.HTTP_204_NO_CONTENT)
def update(request, secret): """ Update dns record. TODO: - query ttl - query records from master instead of nearest resolver """ pw_hash = hash_password(secret) client_ip = request.META['REMOTE_ADDR'] client_type = 'A' if ':' in client_ip: client_type = 'AAAA' try: client = Client.objects.get(secret=pw_hash) except Client.DoesNotExist: return JsonResponse({'status': 'ERROR', 'msg': 'Invalid secret'}) logging.info("Updating %s to %s" % (client.fqdn, client_ip)) try: for address in dnsutils.do_resolve(client.fqdn, client_type, client.domain.master): dnsutils.doUpdate(client.domain.master, client.domain.tsig_key, client.domain.tsig_type, client.domain.fqdn, False, 'delete', '60', client_type, client.fqdn, address) except Exception as e: logger.exception(e) return JsonResponse({'status': 'ERROR', 'msg': 'Internal error'}) try: dnsutils.doUpdate(client.domain.master, client.domain.tsig_key, client.domain.tsig_type, client.domain.fqdn, False, 'update', '60', client_type, client.fqdn, client_ip) except Exception as e: logger.exception(e) return JsonResponse({'status': 'ERROR', 'msg': 'Internal error'}) return JsonResponse({ 'status': 'OK', 'msg': 'Successfully updated %s address to %s' % (client.fqdn, client_ip) })
def post(self, request, domain_id, format=None): domain = self.get_domain(domain_id) data = JSONParser().parse(request) serializer = DNSEntryCacheSerializer(data=data) if serializer.is_valid(): record = serializer.save(domain=domain) try: dnsutils.doUpdate(domain.master, domain.tsig_key, domain.tsig_type, domain.name, False, 'add', str(record.ttl), record.type, record.fqdn, record.data) except (DNSException, dnsutils.DynDNSException): logger.exception("Failed to update value to dns, domain %s" % record.domain.name) transaction.rollback() return JSONResponse( {"detail": 'Cannot update values to DNS-server'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return JSONResponse(serializer.data, status=status.HTTP_201_CREATED) return JSONResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def edit_static(request, domain, entry): if domain.endswith('.'): domain = domain[:-1] try: domain = Domain.user_objects(request.user).get(name=domain) except Domain.DoesNotExist: raise Http404 #synchronize(domain) try: instance = DNSEntryCache.user_objects(request.user).get(pk=int(entry)) old_instance = DNSEntryCache.user_objects( request.user).get(pk=int(entry)) except DNSEntryCache.DoesNotExist: raise Http404 name = str(instance.name) response_data = { 'dns_record_types': dnsutils.DNS_RECORD_TYPES, 'instance': instance, 'domain': domain, 'form': None } if request.method == 'POST': form = StaticEntryEditForm(request.POST, initial={'name': ''}, instance=instance) if form.is_valid(): response_data['form'] = form if form.cleaned_data['name'] != name: messages.error( request, "To change entry name, delete old record and add a new one." ) return render(request, 'manager/add_static.html', response_data) try: dnsutils.validate_data(form.cleaned_data['type'], form.cleaned_data['data'], form.cleaned_data['name']) except dnsutils.DNSRecordException as e: messages.error(request, str(e)) return render(request, 'manager/add_static.html', response_data) entry = form.save() # Add to real dns try: dnsutils.doUpdate(domain.master, domain.tsig_key, domain.tsig_type, domain.name, False, 'delete', str(old_instance.ttl), old_instance.type, old_instance.fqdn, old_instance.data) dnsutils.doUpdate(domain.master, domain.tsig_key, domain.tsig_type, domain.name, False, 'add', str(form.cleaned_data['ttl']), form.cleaned_data['type'], entry.fqdn, form.cleaned_data['data']) messages.success( request, "Successfully updated entry %s %s %s %s" % (entry.fqdn, entry.ttl, entry.type, entry.data)) except Exception: messages.error(request, "Cannot update values to DNS-server.") transaction.rollback() return redirect('show_domain', domain.name) else: response_data['form'] = StaticEntryForm(instance=instance) return render(request, 'manager/add_static.html', response_data)
def doUpdate(Client, TTL, Type, Target): dnsutils.doUpdate(settings.server, settings.keyfile, settings.origin, False, 'update', TTL, Type, Client ,Target)