class WifiNetwork(models.Model): ssid = models.CharField(verbose_name='SSID', db_index=True, max_length=255) hidden_network = models.BooleanField(verbose_name='hidden?', default=False) encryption_type = models.CharField(verbose_name='encryption type', max_length=30, choices=(('WEP', 'WEP'), ('WPA', 'WPA'), ('Any', _('Any'))), default=None, blank=True, null=True) is_hotspot = models.BooleanField(verbose_name='hidden?', default=False) password = models.CharField(verbose_name='Password', db_index=True, max_length=255, blank=True, default=None, null=True)
class User(AbstractUser): first_name = models.CharField(max_length=200) last_name = models.CharField(max_length=200) lotado = models.CharField(max_length=200, blank=True) matricula = models.CharField(max_length=200, blank=True) chefia = models.NullBooleanField() def __str__(self): return self.first_name + ' ' + self.last_name def __unicode__(self): return self.first_name + ' ' + self.last_name
class DjangoUser(AbstractBaseUser, PermissionsMixin): username = models.CharField(_('username'), max_length=250, unique=True, help_text=_('Required. Letters, digits and "/"/@/./+/-/_ only.'), validators=[validators.RegexValidator(r'^[/\w.@+_\-]+$', _('Enter a valid username. '), 'invalid'), ]) first_name = models.CharField(_('first name'), max_length=30, blank=True) last_name = models.CharField(_('last name'), max_length=30, blank=True) email = models.EmailField(_('email address'), blank=True) is_staff = models.BooleanField(_('staff status'), default=False, help_text=_('Designates whether the user can log into this admin ' 'site.')) is_active = models.BooleanField(_('active'), default=True, help_text=_('Designates whether this user should be treated as ' 'active. Unselect this instead of deleting accounts.')) date_joined = models.DateTimeField(_('date joined'), default=timezone.now) objects = UserManager() USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['email'] class Meta(object): verbose_name = _('user') verbose_name_plural = _('users') def get_full_name(self): """ Returns the first_name plus the last_name, with a space in between. """ full_name = '%s %s' % (self.first_name, self.last_name) return full_name.strip() def get_short_name(self): """Returns the short name for the user.""" return self.first_name def email_user(self, subject, message, from_email=None, **kwargs): """ Sends an email to this User. """ send_mail(subject, message, from_email, [self.email], **kwargs)
class Service(models.Model): fqdn = models.CharField(_('Host fqdn'), db_index=True, blank=True, default=None, null=True, max_length=255) scheme = models.CharField(_('Scheme'), db_index=True, blank=False, default='https', max_length=40) hostname = models.CharField(_('Service hostname'), db_index=True, blank=False, default='localhost', max_length=255) port = models.IntegerField(_('Port'), db_index=True, blank=False, default=443) protocol = models.CharField(_('tcp, udp or socket'), db_index=True, choices=(('tcp', 'tcp'), ('udp', 'udp'), ('socket', 'socket'),), default='tcp', max_length=10) encryption = models.CharField(_('encryption'), db_index=True, choices=(('none', _('No encryption')), ('tls', _('SSL/TLS')), ('starttls', _('START TLS')),), max_length=10, default='none') kerberos_service = models.CharField(_('Kerberos service'), blank=True, null=True, default=None, max_length=40) description = models.TextField(_('description'), blank=True, default=_('Service')) dns_srv = models.CharField(_('DNS SRV field'), blank=True, null=True, default=None, max_length=90) status = models.IntegerField(_('Status'), default=None, null=True, blank=True, db_index=True) status_last_update = models.DateTimeField(_('Status last update'), default=None, null=True, blank=True, db_index=True) default_tls_ports = {'http': 443, 'smtp': 465, 'ldap': 636, 'imap': 993, 'pop3': 995, 'xmpp': 5223, 'irc': 6697} default_ports = {'ssh': 22, 'smtp': 25, 'tftp': 69, 'http': 80, 'krb': 88, 'pop3': 110, 'nntp': 119, 'ntp': 123, 'imap': 143, 'snmp': 161, 'irc': 6667, 'ldap': 389, 'syslog': 514, 'mysql': 3306, 'rdp': 3389, 'xmpp': 5222, 'pgsql': 5432, 'vnc': 5900, 'nfs': 2049, } @property def smart_scheme(self): return '%ss' % self.scheme if self.encryption == 'tls' else self.scheme @property def smart_port(self): # noinspection PyTypeChecker if self.encryption == 'tls' and self.port == self.default_tls_ports.get(self.scheme): return '' elif self.encryption != 'tls' and self.port == self.default_ports.get(self.scheme): return '' return ':%d' % self.port @property def principal_name(self): fqdn = self.fqdn if self.kerberos_service == 'cifs': fqdn = self.hostname return '%s/%s@%s' % (self.kerberos_service, fqdn, settings.PENATES_REALM) def __str__(self): return '%s://%s%s/' % (self.smart_scheme, self.hostname, self.smart_port) def __unicode__(self): return '%s://%s%s/' % (self.smart_scheme, self.hostname, self.smart_port) def __repr__(self): return '%s://%s%s/' % (self.smart_scheme, self.hostname, self.smart_port)
class AdminUser(AbstractBaseUser): """Functionnal admin users, for example to retrieve configuration""" USERNAME_FIELD = 'username' username = models.CharField(_('username'), max_length=250, unique=True, validators=[validators.RegexValidator(r'^[/\w.@+_\-]+$', _('Enter a valid username. '), 'invalid'), ]) user_permissions = models.ManyToManyField(Permission, related_name="admin_user_set", related_query_name="admin_user") class Meta(object): permissions = ( ('administration', 'can administrate services'), ('supervision', 'can get supervision configuration'), ('dhcp', 'can get DHCP configuration'), ) def get_full_name(self): return self.username def get_short_name(self): return self.username
class TapirUser(LdapUser): username_validator = validators.UsernameValidator phone_number = models.CharField(_("Phone number"), blank=True, max_length=20) birthdate = models.DateField(_("Birthdate"), blank=True, null=True) street = models.CharField(_("Street and house number"), max_length=150, blank=True) street_2 = models.CharField(_("Extra address line"), max_length=150, blank=True) postcode = models.CharField(_("Postcode"), max_length=32, blank=True) city = models.CharField(_("City"), max_length=50, blank=True) country = CountryField(_("Country"), blank=True, default="DE") preferred_language = models.CharField( _("Preferred Language"), choices=utils.models.PREFERRED_LANGUAGES, default="de", max_length=16, ) def get_display_name(self): return UserUtils.build_display_name(self.first_name, self.last_name) def get_display_address(self): return UserUtils.build_display_address( self.street, self.street_2, self.postcode, self.city ) def get_absolute_url(self): return reverse("accounts:user_detail", args=[self.pk]) def send_welcome_email(self): return self.send_password_reset_email( subject_template_name="accounts/welcome_email_subject.txt", email_template_name="accounts/welcome_email.txt", ) def send_password_reset_email(self, subject_template_name, email_template_name): context = { "site_url": settings.SITE_URL, "uid": urlsafe_base64_encode(force_bytes(self.pk)), "user": self, "token": default_token_generator.make_token(self), } subject = loader.render_to_string(subject_template_name, context) # Email subject *must not* contain newlines subject = "".join(subject.splitlines()) body = loader.render_to_string(email_template_name, context) email_message = EmailMultiAlternatives(subject, body, to=[self.email]) email_message.send()
class MountPoint(models.Model): host = models.ForeignKey(Host, db_index=True) mount_point = models.CharField(_('mount point'), max_length=255, default='/') device = models.CharField(_('device'), max_length=255, default='/dev') fs_type = models.CharField(_('fs type'), max_length=100, default='ext2') options = models.CharField(_('options'), max_length=100, blank=True, default='')
class RecoveryKey(models.Model): kind = models.CharField(verbose_name=_('kind'), max_length=255, db_index=True, default='filevault2', choices=(('filevault2', _('Filevault 2')), )) serial_number = models.CharField(verbose_name=_('serial number'), db_index=True, max_length=255, default=None) recovery_key = models.TextField(verbose_name=_('recovery key'), default='', blank=True)
class Host(models.Model): """ host.fqdn = "machineX.infra.test.example.org" host.hostname = host.sqdn = "machineX" host.admin_fqdn = "machineX.admin.test.example.org" host.principal = "computer/[email protected]" """ fqdn = models.CharField(_('Host fqdn'), db_index=True, blank=True, default=None, null=True, max_length=255, help_text='hostname.infra.domain') owner = models.CharField(_('Owner username'), db_index=True, blank=True, default=None, null=True, max_length=255) main_ip_address = models.GenericIPAddressField(_('Main IP address'), db_index=True, blank=True, default=None, null=True) main_mac_address = models.CharField(_('Main MAC address'), db_index=True, blank=True, default=None, null=True, max_length=255) admin_ip_address = models.GenericIPAddressField(_('Admin IP address'), db_index=True, blank=True, default=None, null=True) admin_mac_address = models.CharField(_('Admin MAC address'), db_index=True, blank=True, default=None, null=True, max_length=255) serial = models.CharField(_('Serial number'), db_index=True, blank=True, default=None, null=True, max_length=255) model_name = models.CharField(_('Model name'), db_index=True, blank=True, default=None, null=True, max_length=255) location = models.CharField(_('Location'), db_index=True, blank=True, default=None, null=True, max_length=255) os_name = models.CharField(_('OS Name'), db_index=True, blank=True, default=None, null=True, max_length=255) bootp_filename = models.CharField(_('BootP filename'), blank=True, default=None, null=True, max_length=255) proc_model = models.CharField(_('Proc model'), db_index=True, blank=True, default=None, null=True, max_length=255) proc_count = models.IntegerField(_('Proc count'), db_index=True, blank=True, default=None, null=True) core_count = models.IntegerField(_('Core count'), db_index=True, blank=True, default=None, null=True) memory_size = models.IntegerField(_('Memory size'), db_index=True, blank=True, default=None, null=True) disk_size = models.IntegerField(_('Disk size'), db_index=True, blank=True, default=None, null=True) def __str__(self): return self.fqdn def __unicode__(self): return self.fqdn @property def admin_fqdn(self): return '%s.%s%s' % (self.hostname(), settings.PDNS_ADMIN_PREFIX, settings.PENATES_DOMAIN) def hostname(self): # noinspection PyTypeChecker return self.fqdn.partition('.')[0] @property def sqdn(self): # noinspection PyTypeChecker return self.hostname() @property def principal(self): return principal_from_hostname(self.fqdn, settings.PENATES_REALM) @staticmethod def register_host(short_hostname, main_ip_address=None, admin_ip_address=None): fqdn = '%s.%s%s' % (short_hostname, settings.PDNS_INFRA_PREFIX, settings.PENATES_DOMAIN) principal = principal_from_hostname(fqdn, settings.PENATES_REALM) add_principal(principal) Host.objects.get_or_create(fqdn=fqdn) # create private key, public key, public certificate, public SSH key entry = entry_from_hostname(fqdn) pki = PKI() pki.ensure_certificate(entry) # create DNS records if main_ip_address: Domain.ensure_auto_record(main_ip_address, fqdn, unique=True, override_reverse=True) Host.objects.filter(fqdn=fqdn).update(main_ip_address=main_ip_address) if admin_ip_address: admin_fqdn = '%s.%s%s' % (short_hostname, settings.PDNS_ADMIN_PREFIX, settings.PENATES_DOMAIN) Domain.ensure_auto_record(admin_ip_address, admin_fqdn, unique=True, override_reverse=False) Host.objects.filter(fqdn=fqdn).update(admin_ip_address=admin_ip_address) return principal @staticmethod def register_mac_address(fqdn, main_ip_address, main_mac_address, admin_ip_address, admin_mac_address): main_mac_address = main_mac_address.replace('-', ':').upper() admin_mac_address = admin_mac_address.replace('-', ':').upper() if main_ip_address: Host.objects.filter(fqdn=fqdn).update(main_ip_address=main_ip_address, main_mac_address=main_mac_address) Record.objects.filter(name=fqdn).update(content=main_ip_address) if admin_ip_address and admin_mac_address: domain_name = '%s%s' % (settings.PDNS_ADMIN_PREFIX, settings.PENATES_DOMAIN) long_admin_hostname = '%s.%s' % (fqdn.partition('.')[0], domain_name) Host.objects.filter(fqdn=fqdn) \ .update(admin_ip_address=admin_ip_address, admin_mac_address=admin_mac_address) Domain.ensure_auto_record(admin_ip_address, long_admin_hostname, unique=True, override_reverse=False)
class PrincipalTest(models.Model): """Only used for testing """ name = models.CharField(max_length=255, primary_key=True, db_index=True) flags = models.IntegerField(db_index=True, default=None, blank=True, null=True)