diff --git a/redes/ldap/pyrado.py b/redes/ldap/pyrado.py new file mode 100644 index 0000000..99b0a41 --- /dev/null +++ b/redes/ldap/pyrado.py @@ -0,0 +1,60 @@ +from getpass import getpass +import subprocess +import ldap3 +from ldap3 import Server, Connection, ALL +import os + +# Get the userDN using WHOAMI /FQDN +def get_user_dn(): + try: + result = subprocess.run(['whoami', '/FQDN'], capture_output=True, text=True, check=True) + return result.stdout.strip() + except subprocess.CalledProcessError as e: + print(f"Error getting user DN: {e}") + return None + +# Get the LDAP server (LOGONSERVER environment variable) +def get_logon_server(): + return os.getenv('LOGONSERVER').replace('\\', '') # Clean up the format, e.g., from '\\SERVER' to 'SERVER' + +# Get the userDN and server +user_dn = get_user_dn() +server_name = get_logon_server() + +if user_dn and server_name: + # Set up the LDAP server and connection + server = Server(f'ldap://{server_name}') + conn = Connection(server, user=user_dn, password='your_password', auto_bind=True) # Provide the password if needed + + # Search for the user (adjust the search to fit your needs) + conn.search(user_dn, '(objectClass=person)', attributes=[ + 'distinguishedName', 'givenName', 'sn', 'displayName', 'mail', + 'userPrincipalName', 'sAMAccountName', 'userAccountControl', + 'profilePath', 'scriptPath', 'homeDirectory', 'homeDrive', 'mobile', 'info' + ]) + + # Check if the user was found + if conn.entries: + user = conn.entries[0] + print(f"DN: {user.distinguishedName}") + print(f"First name: {user.givenName}") + print(f"Last name: {user.sn}") + print(f"Display name: {user.displayName}") + print(f"Email: {user.mail}") + print(f"User logon name: {user.userPrincipalName}") + print(f"pre-Windows 2000 logon name: {user.sAMAccountName}") + print(f"Account Disabled: {user.userAccountControl}") + print(f"Profile path: {user.profilePath}") + print(f"Logon script: {user.scriptPath}") + print(f"Home folder, local path: {user.homeDirectory}") + print(f"Home folder, Connect, Drive: {user.homeDrive}") + print(f"Mobile: {user.mobile}") + print(f"Notes: {user.info}") + else: + print("User not found.") + + # Close the connection + conn.unbind() + +else: + print("Failed to retrieve user DN or server information.")