Commit c96bf194 by Őry Máté

one: fix ssh key validation

parent 92bda738
...@@ -12,7 +12,7 @@ from firewall.tasks import reload_firewall_lock ...@@ -12,7 +12,7 @@ from firewall.tasks import reload_firewall_lock
from one.util import keygen from one.util import keygen
from school.models import Person from school.models import Person
import subprocess, tempfile, os, stat, re import subprocess, tempfile, os, stat, re, base64, struct
pwgen = User.objects.make_random_password pwgen = User.objects.make_random_password
...@@ -26,6 +26,7 @@ def create_user_profile(sender, instance, created, **kwargs): ...@@ -26,6 +26,7 @@ def create_user_profile(sender, instance, created, **kwargs):
d.save() d.save()
post_save.connect(create_user_profile, sender=User) post_save.connect(create_user_profile, sender=User)
""" """
Cloud related details of a user Cloud related details of a user
""" """
...@@ -36,7 +37,7 @@ class UserCloudDetails(models.Model): ...@@ -36,7 +37,7 @@ class UserCloudDetails(models.Model):
help_text=_('Generated password for accessing store from Windows.')) help_text=_('Generated password for accessing store from Windows.'))
ssh_key = models.ForeignKey('SshKey', null=True, verbose_name=_('SSH key (public)'), ssh_key = models.ForeignKey('SshKey', null=True, verbose_name=_('SSH key (public)'),
help_text=_('Generated SSH public key for accessing store from Linux.')) help_text=_('Generated SSH public key for accessing store from Linux.'))
ssh_private_key = models.TextField(verbose_name=_('SSH key (private)'), ssh_private_key = models.TextField(verbose_name=_('SSH key (private)'), null=True,
help_text=_('Generated SSH private key for accessing store from Linux.')) help_text=_('Generated SSH private key for accessing store from Linux.'))
""" """
...@@ -51,6 +52,8 @@ class UserCloudDetails(models.Model): ...@@ -51,6 +52,8 @@ class UserCloudDetails(models.Model):
except: except:
self.ssh_key = SshKey(user=self.user, key=pub) self.ssh_key = SshKey(user=self.user, key=pub)
self.ssh_key.save() self.ssh_key.save()
self.ssh_key_id = self.ssh_key.id
self.save()
""" """
Generate new Samba password. Generate new Samba password.
...@@ -58,15 +61,12 @@ class UserCloudDetails(models.Model): ...@@ -58,15 +61,12 @@ class UserCloudDetails(models.Model):
def reset_smb(self): def reset_smb(self):
self.smb_password = pwgen() self.smb_password = pwgen()
""" def reset_keys(sender, instance, created, **kwargs):
Generate key pair and Samba password if needed. if created:
""" instance.reset_smb()
def clean(self): instance.reset_keys()
super(UserCloudDetails, self).clean()
if not self.ssh_key: post_save.connect(reset_keys, sender=UserCloudDetails)
self.reset_keys()
if not self.smb_password or len(self.smb_password) == 0:
self.reset_smb()
""" """
Validate OpenSSH keys (length and type). Validate OpenSSH keys (length and type).
...@@ -100,7 +100,7 @@ SSH public key (in OpenSSH format). ...@@ -100,7 +100,7 @@ SSH public key (in OpenSSH format).
class SshKey(models.Model): class SshKey(models.Model):
user = models.ForeignKey(User, null=False, blank=False) user = models.ForeignKey(User, null=False, blank=False)
key = models.CharField(max_length=2000, verbose_name=_('SSH key'), key = models.CharField(max_length=2000, verbose_name=_('SSH key'),
help_text=_('<a href="/info/ssh/">SSH public key in OpenSSH format</a> used for shell login ' help_text=_('<a href="/info/ssh/">SSH public key in OpenSSH format</a> used for shell and store login '
'(2048+ bit RSA preferred). Example: <code>ssh-rsa AAAAB...QtQ== ' '(2048+ bit RSA preferred). Example: <code>ssh-rsa AAAAB...QtQ== '
'john</code>.'), validators=[OpenSshKeyValidator()]) 'john</code>.'), validators=[OpenSshKeyValidator()])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment