|
| 1 | +import typer |
| 2 | +import shutil |
| 3 | +from typing_extensions import Annotated |
| 4 | +from rich.console import Console |
| 5 | +from rich.table import Table |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import pwd |
| 9 | +import subprocess |
| 10 | +import pexpect |
| 11 | +import time |
| 12 | +import smbclient |
| 13 | + |
| 14 | +from smbprotocol.exceptions import SMBOSError |
| 15 | +from pwinput import pwinput |
| 16 | +from linuxmusterTools.lmnconfig import SAMBA_REALM, SAMBA_DOMAIN, SAMBA_NETBIOS |
| 17 | + |
| 18 | + |
| 19 | +app = typer.Typer() |
| 20 | + |
| 21 | +@app.callback( |
| 22 | + invoke_without_command=True, |
| 23 | + help="""Check as teacher connection with samba share and kerberos authentication.""" |
| 24 | +) |
| 25 | +def check(): |
| 26 | + |
| 27 | + def p(*args, end="\n"): |
| 28 | + print("\033[1m\033[38;5;214m", *args, "\033[39m\033[0m", end=end) |
| 29 | + |
| 30 | + p('This script helps to test some domains to access samba shares, and you can try your own path too.', end="\n\n") |
| 31 | + |
| 32 | + print("-"*80, "\n") |
| 33 | + p("## Checking Samba domains:") |
| 34 | + p(f"\tSamba realm used by the Webui --> {SAMBA_REALM}") |
| 35 | + p(f"\tSamba netbios used by the Webui --> {SAMBA_NETBIOS}") |
| 36 | + p(f"\tSamba domain used by the Webui --> {SAMBA_DOMAIN}\n") |
| 37 | + p("Samba domain to try (optional, something like server.linuxmuster.lan):", end="") |
| 38 | + domain_user = input() |
| 39 | + |
| 40 | + print("-"*80, "\n") |
| 41 | + p("## Authentication:") |
| 42 | + p("Teacher login:", end="") |
| 43 | + teacher = input() |
| 44 | + uid = pwd.getpwnam(teacher).pw_uid |
| 45 | + |
| 46 | + if not teacher: |
| 47 | + print("\033[1m\033[91m Please enter a valid user login \033[39m\033[0m") |
| 48 | + sys.exit() |
| 49 | + |
| 50 | + pw = pwinput(prompt="\033[1m\033[38;5;214m Password:\033[39m\033[0m") |
| 51 | + |
| 52 | + if not pw: |
| 53 | + print("\033[1m\033[91m No password given, trying to authenticate via an existing Kerberos ticket \033[39m\033[0m") |
| 54 | + |
| 55 | + print("-"*80, "\n") |
| 56 | + p("## Getting Kerberos ticket") |
| 57 | + child = pexpect.spawn('/usr/bin/kinit', ['-c', f'/tmp/krb5cc_{uid}', teacher]) |
| 58 | + if pw: |
| 59 | + child.expect('.*') |
| 60 | + child.sendline(pw) |
| 61 | + |
| 62 | + # Waiting until the ticket is written |
| 63 | + time.sleep(2) |
| 64 | + |
| 65 | + if not os.path.isfile(f'/tmp/krb5cc_{uid}'): |
| 66 | + print("\033[1m\033[91m No valid Kerberos ticket found ! \033[39m\033[0m") |
| 67 | + sys.exit() |
| 68 | + |
| 69 | + p(f"Setting process uid={uid} and gid=100 for Kerberos ticket /tmp/krb5cc_{uid}") |
| 70 | + os.chown(f'/tmp/krb5cc_{uid}', uid, 100) |
| 71 | + os.setgid(100) |
| 72 | + os.setuid(uid) |
| 73 | + |
| 74 | + report = "" |
| 75 | + hosts = [SAMBA_NETBIOS, SAMBA_REALM, SAMBA_DOMAIN] |
| 76 | + if domain_user: |
| 77 | + hosts.append(domain_user) |
| 78 | + |
| 79 | + for host in hosts: |
| 80 | + path = f'\\\\{host}\\default-school\\teachers\\{teacher}' |
| 81 | + print("\n", "#"*80, "\n") |
| 82 | + p(f'Trying with host {host} ...', end="") |
| 83 | + try: |
| 84 | + p(f"Files located at {path}:", end="\n\n") |
| 85 | + files = [""] |
| 86 | + for f in smbclient.scandir(path): |
| 87 | + if f.is_dir(): |
| 88 | + files.append(f"D --> {f.name}\n") |
| 89 | + else: |
| 90 | + files.append(f"F --> {f.name}\n") |
| 91 | + files.sort() |
| 92 | + print(*files) |
| 93 | + result = f"\033[1m\033[92m SUCCESS with host {host}! \033[39m\033[0m" |
| 94 | + print(result) |
| 95 | + except Exception as e: |
| 96 | + result = f"\033[1m\033[91m FAILED with host {host}! \033[39m\033[0m" |
| 97 | + print(result) |
| 98 | + print("\033[1m\033[91m", e, "\033[39m\033[0m") |
| 99 | + report += f"{result}\n" |
| 100 | + |
| 101 | + print("\n", "-"*80, "\n") |
| 102 | + p('REPORT:', end="\n\n") |
| 103 | + print(report) |
| 104 | + |
0 commit comments