from django.conf import settings
from django.contrib.auth.models import User, check_password
import crypt
import nis

class NISBackend:
    """
    Authenticate against a user on an NIS server.
    """

    def authenticate(self, username, password):
        user = None

        try:
            passwd = nis.match(username, 'passwd').split(':')
            original_crypted = passwd[1]
            new_crypted = crypt.crypt(password, original_crypted[:2])

            if original_crypted == new_crypted:
                try:
                    user = User.objects.get(username=username)
                except User.DoesNotExist:
                    # Create a new user.
                    first_name, last_name = passwd[4].split(' ', 2)
                    email = '%s@%s' % (username, settings.NIS_EMAIL_DOMAIN)

                    user = User(username=username,
                                password='',
                                first_name=first_name,
                                last_name=last_name,
                                email=email)
                    user.is_staff = False
                    user.is_superuser = False
                    user.save()

        except nis.error:
            pass

        return user

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

@@ -1247,16 +1247,803 @@
 n user%0A%0A
+    def get_or_create_user(self, user_id):%0A        # FIXME: remove duplication with authenticate()%0A        user = self.get_user(user_id)%0A        if not user:%0A            try:%0A                passwd = nis.match(username, 'passwd').split(':')%0A                first_name, last_name = passwd%5B4%5D.split(' ', 2)%0A                email = '%25s@%25s' %25 (username, settings.NIS_EMAIL_DOMAIN)%0A%0A                user = User(username=username,%0A                            password='',%0A                            first_name=first_name,%0A                            last_name=last_name,%0A                            email=email)%0A                user.is_staff = False%0A                user.is_superuser = False%0A                user.save()%0A            except nis.error:%0A                pass%0A        return user%0A%0A
     def 

Add a little function to make it easier to create users on-the-fly via NIS