Skip to content

Commit 0f4e9ab

Browse files
committed
Add cmd to check smbclient connection.
1 parent 8e2d1cc commit 0f4e9ab

3 files changed

Lines changed: 108 additions & 1 deletion

File tree

‎usr/lib/python3/dist-packages/linuxmusterCli/main.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
check_attic,
2020
userlog,
2121
check_parents,
22+
check_smbclient,
2223
quotas,
2324
student,
2425
schoolclass,
@@ -57,7 +58,7 @@ def handle(self, record):
5758
log = logging.getLogger()
5859
log.setLevel(logging.INFO)
5960
stdout = CLILogHandler(sys.stdout)
60-
stdout.setLevel(logging.INFO)
61+
stdout.setLevel(logging.WARNING)
6162
log.handlers = [stdout]
6263

6364
console = Console()
@@ -70,6 +71,7 @@ def handle(self, record):
7071
app.add_typer(up.app, name='up')
7172
app.add_typer(check_attic.app, name='check_attic')
7273
app.add_typer(check_parents.app, name='check_parents')
74+
app.add_typer(check_smbclient.app, name='check_smbclient')
7375
app.add_typer(userlog.app, name='userlog')
7476
app.add_typer(quotas.app, name='quotas')
7577
app.add_typer(student.app, name='student')

‎usr/lib/python3/dist-packages/linuxmusterCli/typers/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .up import *
77
from .check_attic import *
88
from .check_parents import *
9+
from .check_smbclient import *
910
from .userlog import *
1011
from .state import state
1112
from .quotas import *
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)