bh: utils/testing create_ldap_users.py,1.2,1.3

cvs at intevation.de cvs at intevation.de
Mon Oct 24 17:09:07 CEST 2005


Author: bh

Update of /kolabrepository/utils/testing
In directory doto:/tmp/cvs-serv18541/testing

Modified Files:
	create_ldap_users.py 
Log Message:
 - the script accepts many options on the command line now.  All
   important settings can be specified on the command line.

 - The script can also delete the automatically added users.

 - The scripts determines more information automatically from ldap (the
   k=kolab entry).


Index: create_ldap_users.py
===================================================================
RCS file: /kolabrepository/utils/testing/create_ldap_users.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- create_ldap_users.py	30 Jun 2005 09:34:45 -0000	1.2
+++ create_ldap_users.py	24 Oct 2005 15:09:04 -0000	1.3
@@ -1,7 +1,24 @@
 #!/bin/env python
 """Create some few kolab users in ldap.
 
-Security considerations: 
+Usage:
+
+create_ldap_users.py [-h hostname] [-p port] [-u admin_dn] [-n num] basedn cmd
+
+cmd may be one of
+
+  add      add num (default 10) users named autoNUM
+
+  delete   delete all users named auto*.  deletion is done by setting
+           kolabDeleteFlag so that the kolabd will later remove the users.
+
+Host and port default to localhost and the standard ldap port.
+
+The admin_dn should be the initial part of the dn of the admin account
+to use, that ist admin + ',cn=internal,' + base_dn will be used as the
+dn for the ldap bind.  The default is 'cn=manager'.
+
+Security considerations:
     The connection is not encrypted by default and thus the password
     can be sniffed.
 """
@@ -10,32 +27,34 @@
 # http://python-ldap.sourceforge.net/
 # on debian woody it's packaged as python-ldap
 
+import sys
+import getopt
 import ldap
 import ldap.modlist
 import getpass
+import time
 
-# adjust these values to match your setup
-# TODO: read some of these automatically from ldap
-mail_domain = "example.com"
-base_dn = "dc=example,dc=com"
-admin_dn_part = "cn=some admin,cn=internal"
-kolabserver = "kolabserver.example.com"
-ldap_uri = "ldap://"+ kolabserver + ":389/"
-home_server = kolabserver
+def open_ldap(ldapuri, admin_dn_part):
+    conn = ldap.initialize(ldapuri)
+    pwd = getpass.getpass("ldap bind password:")
+    conn.simple_bind_s(admin_dn_part + "," + "cn=internal" + "," + base_dn,
+                       pwd)
+    return conn
 
-# number of users to create.  All users have email addresses of the form
-# autotest%d at mail_domain where %d will be replaced by a number in
-# range(num_users)
-num_users = 10
+def fetch_kolab_info(conn):
+    return conn.search_s("k=kolab," + base_dn, ldap.SCOPE_BASE,
+                         filterstr='(objectClass=*)')[0][1]
 
-def add_user():
-    conn = ldap.initialize(ldap_uri)
-    pwd = getpass.getpass("ldap bind password:")
-    conn.simple_bind_s(admin_dn_part + "," + base_dn, pwd)
+def add_user(conn, num_users):
+    kolab_info = fetch_kolab_info(conn)
+
+    mail_domain = kolab_info["postfix-mydomain"][0]
+    # use the first of the kolab hosts as home server.
+    kolab_home_server = kolab_info["kolabHost"][0]
 
     common_attrs = {
         'objectClass': ['top', 'inetOrgPerson', 'kolabInetOrgPerson'],
-        'kolabHomeServer': [home_server],
+        'kolabHomeServer': [kolab_home_server],
         'kolabInvitationPolicy': ['ACT_MANUAL'],
         }
 
@@ -53,4 +72,66 @@
         print conn.add_s(dn, ldap.modlist.addModlist(descr))
 
 
-add_user()
+def delete_auto_users(conn):
+    hosts = fetch_kolab_info(conn)["kolabHost"]
+    result = conn.search_s(base_dn, ldap.SCOPE_ONELEVEL,
+                     filterstr="(&(objectClass=kolabInetOrgPerson)(cn=auto*))")
+    for dn, attrs in result:
+        print dn, hosts
+        conn.modify_s(dn, [(ldap.MOD_ADD, "kolabDeleteFlag", hosts)])
+
+
+def main():
+    global base_dn
+
+    hostname = "localhost"
+    port = None
+    admin_dn_part = "cn=manager"
+    base_dn = None
+    num_users = 10
+
+    opts, args = getopt.getopt(sys.argv[1:], 'h:p:u:n:',
+                               ["host=", "port=", "user=", "num="])
+    for optchar, value in opts:
+        if optchar in ("-h", "--host"):
+            hostname = value
+        elif optchar in ("-p", "--port"):
+            port = int(value)
+        elif optchar in ("-u", "--user"):
+            admin_dn_part = value
+        elif optchar in ("-n", "--num"):
+            num_users = int(value)
+        else:
+            print >>sys.stderr, "Unknown option", optchar
+            sys.exit(1)
+
+    if len(args) < 2:
+        print >>sys.stderr, "missing parameters"
+        sys.exit(1)
+    elif len(args) > 2:
+        print >>sys.stderr, "too many parameters"
+        sys.exit(1)
+
+    base_dn, cmd = args
+
+    if cmd not in ("add", "delete"):
+        print >>sys.stderr, "unknown command", repr(cmd)
+        sys.exit(1)
+
+
+    if port is not None:
+        hostname += ":%d" % port
+
+    uri = "ldap://" + hostname
+
+
+    conn = open_ldap(uri, admin_dn_part)
+    if cmd == "add":
+        add_user(conn, num_users = num_users)
+    elif cmd == "delete":
+        delete_auto_users(conn)
+
+
+
+if __name__ == "__main__":
+    main()





More information about the commits mailing list