diff --git a/nsupdate/api/views.py b/nsupdate/api/views.py index 535ecb1..97c8962 100644 --- a/nsupdate/api/views.py +++ b/nsupdate/api/views.py @@ -5,9 +5,12 @@ logger = logging.getLogger(__name__) from django.http import HttpResponse from django.conf import settings +from django.contrib.auth.hashers import check_password + from main.forms import * from main.models import Host import dns.inet +import os from main.dnstools import update, SameIpError @@ -21,7 +24,8 @@ def UpdateIpView(request): af = dns.inet.af_for_address(ipaddr) key = 'ipv4' if af == dns.inet.AF_INET else 'ipv6' request.session[key] = ipaddr - return HttpResponse('OK', content_type="text/plain") + image_data = open(settings.STATIC_ROOT+"/1px.gif", "rb").read() + return HttpResponse(image_data, mimetype="image/png") def basic_challenge(realm, content='Authorization Required'): @@ -61,10 +65,16 @@ def check_auth(username, password): :param password: update password :return: True if authenticated, False otherwise. """ - # in our case username == fqdn - hosts = Host.objects.filter(fqdn=username, update_secret=password) - assert len(hosts) < 2 - return bool(hosts) + fqdn = username + hosts = Host.objects.filter(fqdn=fqdn) + num_hosts = len(hosts) + if num_hosts == 0: + return False + if num_hosts > 1: + logging.error("fqdn %s has multiple entries" % fqdn) + return False + password_hash = hosts[0].update_secret + return check_password(password, password_hash) def Response(content): diff --git a/nsupdate/main/dnstools.py b/nsupdate/main/dnstools.py index 8c2f74d..3b9cbd8 100644 --- a/nsupdate/main/dnstools.py +++ b/nsupdate/main/dnstools.py @@ -27,7 +27,7 @@ def update(fqdn, ipaddr, ttl=60): current_ipaddr = query_ns(fqdn, rdtype) # check if ip really changed ok = ipaddr != current_ipaddr - except dns.resolver.NXDOMAIN: + except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): # no dns entry yet, ok ok = True if ok: diff --git a/nsupdate/main/migrations/0004_updated_host_fields.py b/nsupdate/main/migrations/0004_updated_host_fields.py new file mode 100644 index 0000000..fa74050 --- /dev/null +++ b/nsupdate/main/migrations/0004_updated_host_fields.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + + # Changing field 'Host.comment' + db.alter_column(u'main_host', 'comment', self.gf('django.db.models.fields.CharField')(max_length=256, null=True)) + # Adding unique constraint on 'Host', fields ['fqdn'] + db.create_unique(u'main_host', ['fqdn']) + + + def backwards(self, orm): + # Removing unique constraint on 'Host', fields ['fqdn'] + db.delete_unique(u'main_host', ['fqdn']) + + + # Changing field 'Host.comment' + db.alter_column(u'main_host', 'comment', self.gf('django.db.models.fields.CharField')(max_length=256)) + + models = { + u'auth.group': { + 'Meta': {'object_name': 'Group'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + u'auth.permission': { + 'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + u'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + u'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + u'main.host': { + 'Meta': {'object_name': 'Host'}, + 'comment': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '256', 'null': 'True', 'blank': 'True'}), + 'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}), + 'created_by': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"}), + 'fqdn': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '256'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'last_update': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}), + 'update_secret': ('django.db.models.fields.CharField', [], {'max_length': '256'}) + } + } + + complete_apps = ['main'] \ No newline at end of file diff --git a/nsupdate/main/models.py b/nsupdate/main/models.py index 889b8ab..bf07daa 100644 --- a/nsupdate/main/models.py +++ b/nsupdate/main/models.py @@ -5,9 +5,9 @@ from django.forms import ModelForm class Host(models.Model): """TODO: hash update_secret""" - fqdn = models.CharField(max_length=256) + fqdn = models.CharField(max_length=256,unique=True) update_secret = models.CharField(max_length=256) - comment = models.CharField(max_length=256,default='') + comment = models.CharField(max_length=256,default='',blank=True, null=True) last_update = models.DateTimeField(auto_now=True) created = models.DateTimeField(auto_now_add=True) diff --git a/nsupdate/main/templates/main/delete_host.html b/nsupdate/main/templates/main/delete_host.html index fb2169b..57d7b98 100644 --- a/nsupdate/main/templates/main/delete_host.html +++ b/nsupdate/main/templates/main/delete_host.html @@ -4,7 +4,6 @@ {% block content %}
- nsupdate.info is an open-source DynDNS service with IPv4 and IPv6 support. + nsupdate.info is a free and open-source dynamic DNS service with IPv4 and IPv6 support.
We think this are your IPs:
- {% elif session.ipv4 or session.ipv6 %} + {% elif request.session.ipv4 or request.session.ipv6 %}We think this is your IP:
{% endif %} - {% if session.ipv4 %} -Sourcecode available at GitHub - - Developed by Arne Schauf, + developed by Arne Schauf, Fabian Faessler, Thomas Waldmann - during the Django Dash 2013 + during the Django Dash 2013.