From c720f21e1dd7892b36f964de570c1b40b121b3d0 Mon Sep 17 00:00:00 2001
From: downhill <downhill@geekhabitat.de>
Date: Tue, 23 Oct 2018 16:33:54 +0200
Subject: [PATCH] some bugfixes, config to example config, wrote function to
 get new uidNumber

---
 nameko-func.py                     | 27 ++++++++++++++++++++-------
 nameko.conf => nameko.conf.example |  1 +
 2 files changed, 21 insertions(+), 7 deletions(-)
 rename nameko.conf => nameko.conf.example (99%)

diff --git a/nameko-func.py b/nameko-func.py
index 3e3b8a5..1305c70 100644
--- a/nameko-func.py
+++ b/nameko-func.py
@@ -43,16 +43,16 @@ def ldapservers():
 # returns the full dn
 def user_or_customer(uid):
     server = ldapservers()
-    conn = Connection(server)
+    conn = Connection(server, config['LDAP']['LDAPMANAGER'], config['LDAP']['LDAPMANAGERPASSWORD'])
     conn.bind()
     search_customers = conn.search('ou=customers,dc=ungleich,dc=ch', '(%s)' % uid)
     if search_customers:
         conn.unbind()
         return '%s,ou=customers,dc=ungleich,dc=ch' % uid
-    search_users = conn.search('ou=customers,dc=ungleich,dc=ch', '(%s)' % uid)
+    search_users = conn.search('ou=users,dc=ungleich,dc=ch', '(%s)' % uid)
     if search_users:
         conn.unbind()
-        return '%s,ou=customers,dc=ungleich,dc=ch' % uid
+        return '%s,ou=users,dc=ungleich,dc=ch' % uid
     conn.unbind()
     return False
 
@@ -67,7 +67,7 @@ class UserLookUp(object):
         # Setup the search parameter and connect to LDAP
         LDAP_UID = 'uid=%s' % user
         server = ldapservers()
-        conn = Connection(server)
+        conn = Connection(server, config['LDAP']['LDAPMANAGER'], config['LDAP']['LDAPMANAGERPASSWORD'])
         conn.bind()
         # Strange result. It keeps complaining LDAP_UID not set if I try to directly
         # substitute x and y to the if
@@ -114,8 +114,8 @@ class CreateUser(object):
         w[0].mail = email
         w[0].userPassword = password
         w[0].homeDirectory = '/home/%s' % user
-        # TODO: Learn how to get the last uidNumber and what gidNumber to use
-        w[0].uidNumber = randint(1200,50000)
+        # Set uidNumber as last used uidNumber+1
+        w[0].uidNumber = self.get_new_uid_number(conn)
         w[0].gidNumber = randint(1200,50000)
         if not w.commit():
             conn.unbind()
@@ -125,6 +125,19 @@ class CreateUser(object):
         self.dispatch('ldap', '%s [Info CreateUser] %s created.\n' % (datetime.now(), dn) )
         return True
 
+    # Function to get the next uid number. Not elegant, but LAM does it too and didn't really find anything
+    # nicer. The sorted() seems to be quite efficient, so it shouldn't take too long even on larger arrays
+    def get_new_uid_number(self, conn):
+        conn.search('dc=ungleich,dc=ch', '(&(objectClass=posixAccount)(uidNumber=*))', attributes = [ 'uidNumber' ])
+        newuid = 0
+        uidlist = []
+        for c in conn.response:
+            uidlist.append(c['attribute']['uidNumber'])
+        newuid = sorted(uidlist)[len(uidlist)-1]
+        return newuid
+
+
+
 # Returns some basic data from an user
 class GetUserData(object):
     name = "getuserdata"
@@ -135,7 +148,7 @@ class GetUserData(object):
         # Setup the search parameter and connect to LDAP
         LDAP_UID = 'uid=%s' % user
         server = ldapservers()
-        conn = Connection(server)
+        conn = Connection(server, config['LDAP']['LDAPMANAGER'], config['LDAP']['LDAPMANAGERPASSWORD'])
         conn.bind()
         if not conn.bound:
             self.dispatch('ldap', '%s [Error GetUserData] Could not connect to LDAP server.\n' % datetime.now() )
diff --git a/nameko.conf b/nameko.conf.example
similarity index 99%
rename from nameko.conf
rename to nameko.conf.example
index 7510441..53c0b66 100644
--- a/nameko.conf
+++ b/nameko.conf.example
@@ -17,3 +17,4 @@ LDAPSERVER1 = localhost
 # Change to something which has enough access to create users, change things around, etc
 LDAPMANAGER = cn=manager,dc=ungleich,dc=ch
 LDAPMANAGERPASSWORD = foobar
+