Changes for page LDAP-Benutzer unter Linux

Last modified by Jonas Jelten on 2025/02/25 12:14

From version 11.2
edited by Jonas Jelten
on 2024/12/12 16:08
Change comment: There is no comment for this version
To version 13.1
edited by Jonas Jelten
on 2024/12/12 16:12
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -14,7 +14,6 @@
14 14  
15 15  Ihr könnt gerne mit uns Rücksprache halten, wenn Ihr unseren LDAP für Loginzwecke benutzen möchtet, denn wir bleiben auch gerne darüber informiert, wo eine solche Konfiguration eingesetzt wird.
16 16  
17 -
18 18  # Anleitung
19 19  
20 20  Wir wählen den Weg mit `nslcd` und `pam-ldapd` und `nss-ldapd`, weil leichter zu debuggen ist, leichtgewichtiger (die pam/nss-module reden mit nslcd und bauen nicht jeder einzeln eine ldap-verbindung auf).
... ... @@ -174,4 +174,4 @@
174 174  AuthorizedKeysCommandUser nobody
175 175  ```
176 176  
177 -
176 +[[attach:ssh-ldap-key]]-programm zum download
ssh-ldap-key
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.jelten
Size
... ... @@ -1,0 +1,1 @@
1 +1.9 KB
Content
... ... @@ -1,0 +1,70 @@
1 +#!/usr/bin/env python3
2 +
3 +"""
4 +fetch ssh keys from CIT ITO LDAP
5 +
6 +license: GPLv3
7 +(c) 2024 Jonas Jelten <jj@sft.lol>
8 +"""
9 +
10 +import argparse
11 +import logging
12 +
13 +import ldap3
14 +
15 +
16 +def main():
17 + cli = argparse.ArgumentParser(description='ssh key fetcher from ldap')
18 + cli.add_argument("username")
19 + cli.add_argument("--ldap-server", default="ldaps://ldap.cit.tum.de")
20 + cli.add_argument("--base-dn", default="ou=dir,dc=cit,dc=tum,dc=de")
21 +
22 + cli.add_argument("--verbose", "-v", action="count", default=0)
23 +
24 + args = cli.parse_args()
25 +
26 + log_level = (logging.WARNING, logging.INFO, logging.DEBUG)[args.verbose]
27 + logging.basicConfig(
28 + level=log_level,
29 + format="[%(asctime)s] %(message)s"
30 + )
31 + detail_level = (ldap3.utils.log.ERROR, ldap3.utils.log.BASIC, ldap3.utils.log.NETWORK)[args.verbose]
32 + ldap3.utils.log.set_library_log_detail_level(detail_level)
33 + ldap3.utils.log.set_library_log_activation_level(log_level)
34 +
35 + keys = fetch_keys(user=args.username, server=args.ldap_server, basedn=args.base_dn)
36 + for key in keys:
37 + print(key)
38 +
39 +
40 +def fetch_keys(user, server, basedn):
41 + logging.debug("connecting to ldap server %r", server)
42 + conn = ldap3.Connection(server)
43 + if not conn.bind():
44 + raise Exception("failed to anonymous bind")
45 +
46 + uid = ldap3.utils.conv.escape_filter_chars(user)
47 +
48 + logging.debug("searching for user %r at base %r", uid, basedn)
49 + ok = conn.search(
50 + basedn,
51 + f"(& (objectClass=inetOrgPerson) (uid={uid}))",
52 + attributes=['uid', 'objectclass', 'sshPublicKey']
53 + )
54 + if not ok:
55 + raise ValueError(f"failed to perform ldap search: {conn.result}")
56 +
57 + if len(conn.entries) > 1:
58 + raise ValueError(f"more than one user found: {len(conn.entries)} entries returned")
59 +
60 + user = conn.entries[0]
61 +
62 + keys = list()
63 + for key in user.sshPublicKey:
64 + keys.append(ldap3.utils.conv.to_unicode(key))
65 + return keys
66 +
67 +
68 +if __name__ == "__main__":
69 + main()
70 +