aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-05-06 11:39:34 +0200
committerpukkamustard <pukkamustard@posteo.net>2021-05-06 11:39:34 +0200
commitee11464b9991fd330f2d814ec11498e31dfa5e77 (patch)
tree2de7904eb4ac70edec6382cb756e679668d93ce0
parentbbf123fd06b73d849fc9568c7b11209826c051d0 (diff)
utils: add jblis-directory.py
-rw-r--r--README.org16
-rw-r--r--utils/README.org13
-rw-r--r--utils/guix.scm21
-rwxr-xr-xutils/jblis-directory.py124
4 files changed, 174 insertions, 0 deletions
diff --git a/README.org b/README.org
index 66aa1a9..6d98970 100644
--- a/README.org
+++ b/README.org
@@ -29,7 +29,23 @@ Debian Linux VM running the Ejabberd XMPP server.
Being prepared to replace qfwfq.
+** [[./utils][Utils]]
+
+Tools and utilities for managing jblis.xyz (e.g. tool for adding new user to LDAP).
+
* Ideas
+** Biboumi IRC bridge
+
+Use [[https://biboumi.louiz.org/][biboumi]] as an XMPP-IRC bridge.
+
+** Mumble
+
+For voice conferences.
+
+** Funkwhale
+
+Revive the last.fm days...
+
** Mokum
A web UI for users to change password, create invites, etc..
diff --git a/utils/README.org b/utils/README.org
new file mode 100644
index 0000000..243c3fa
--- /dev/null
+++ b/utils/README.org
@@ -0,0 +1,13 @@
+#+TITLE: jblis-utils
+
+To get an environment where the utilities can be used run:
+
+#+BEGIN_SRC sh
+guix environment -l guix.scm
+#+END_SRC
+
+* jblis-directory.py
+
+A Python script to list, add and delete users from jblis.
+
+For help and usage see ~./jblis-directory.py --help~
diff --git a/utils/guix.scm b/utils/guix.scm
new file mode 100644
index 0000000..32bad58
--- /dev/null
+++ b/utils/guix.scm
@@ -0,0 +1,21 @@
+(use-modules
+ (guix packages)
+ (guix download)
+ (guix build-system python)
+ ((guix licenses) #:prefix license:)
+ (gnu packages python-xyz))
+
+(define-public jblis-utils
+ (package
+ (name "jblis-utils")
+ (version "0.0.0")
+ (home-page "https://jblis.xyz")
+ (source #f)
+ (build-system python-build-system)
+ (propagated-inputs
+ `(("python-ldap3" ,python-ldap3)))
+ (synopsis #f)
+ (description #f)
+ (license license:agpl3+)))
+
+jblis-utils
diff --git a/utils/jblis-directory.py b/utils/jblis-directory.py
new file mode 100755
index 0000000..d465615
--- /dev/null
+++ b/utils/jblis-directory.py
@@ -0,0 +1,124 @@
+#!/usr/bin/env python
+
+import argparse
+import getpass
+import ldap3
+import string
+import secrets
+
+
+from ldap3.utils.hashed import hashed
+
+
+def list_users(args):
+ server = ldap3.Server(args.server)
+ conn = ldap3.Connection(server, auto_bind=True)
+ conn.search("dc=jblis,dc=xyz", "(objectclass=person)")
+
+ for entry in conn.entries:
+ print(entry.entry_dn)
+
+
+def generate_random_password():
+ alphabet = string.ascii_letters + string.digits
+ password = "".join(secrets.choice(alphabet) for i in range(8))
+ return password
+
+
+def add_user(args):
+ # get admin password
+ admin_dn = "cn=admin,dc=jblis,dc=xyz"
+ password = getpass.getpass(prompt=f"Admin password ({admin_dn}): ")
+
+ # connect with LDAP server
+ server = ldap3.Server(args.server)
+ conn = ldap3.Connection(server, admin_dn, password, auto_bind=True)
+
+ username = args.username
+
+ # generate or prompt for password
+ password = generate_random_password()
+ if args.prompt_password:
+ password = getpass.getpass(prompt=f"Password for {username}: ")
+
+ hashed_password = hashed(ldap3.HASHED_SALTED_SHA, password)
+
+ # add the user
+ dn = f"cn={username},ou=users,dc=jblis,dc=xyz"
+
+ added = conn.add(
+ dn,
+ ["person", "shadowAccount"],
+ {"sn": username, "uid": username, "userPassword": hashed_password},
+ )
+
+ if added:
+ print(f"User {username} added!\n\nUser: {username}\nPassword: {password}")
+ else:
+ print("Could not add user (maybe already exists?).")
+
+
+def delete_user(args):
+ # get admin password
+ admin_dn = "cn=admin,dc=jblis,dc=xyz"
+ password = getpass.getpass(prompt=f"Admin password ({admin_dn}): ")
+
+ # connect with LDAP server
+ server = ldap3.Server(args.server)
+ conn = ldap3.Connection(server, admin_dn, password, auto_bind=True)
+
+ username = args.username
+ dn = f"cn={username},ou=users,dc=jblis,dc=xyz"
+ if conn.delete(dn):
+ print("User deleted.")
+ else:
+ print("User not deleted (maybe there is no such user?).")
+
+
+def main():
+ parser = argparse.ArgumentParser(description="Manage the jblis.xyz directory.")
+
+ # common options
+ parser.add_argument(
+ "--server",
+ help="LDAP server to connect with (default: ldap://localhost:389)",
+ default="ldap://localhost:389",
+ )
+
+ parser.set_defaults(func=lambda args: parser.print_usage())
+
+ subparsers = parser.add_subparsers(help="commands")
+
+ # create parser for the "list" command
+ parser_list_users = subparsers.add_parser("list", help="print list of users")
+ parser_list_users.set_defaults(func=list_users)
+
+ # create parser for the "add" command
+ parser_add_user = subparsers.add_parser("add", help="add a new user")
+ parser_add_user.add_argument(
+ "username", metavar="USERNAME", help="username of user to add"
+ )
+ parser_add_user.add_argument(
+ "--prompt-password",
+ action="store_true",
+ dest="prompt_password",
+ help="prompt for user password instead of generating a new random password",
+ )
+ parser_add_user.set_defaults(func=add_user)
+
+ # create parser for the "delete" command
+ parser_delete_user = subparsers.add_parser("delete", help="delete user")
+ parser_delete_user.add_argument(
+ "username", metavar="USERNAME", help="username of user to delete"
+ )
+ parser_delete_user.set_defaults(func=delete_user)
+
+ # parse the arguments
+ args = parser.parse_args()
+
+ # call the command function
+ args.func(args)
+
+
+if __name__ == "__main__":
+ main()