Skip to content

Commit 8ffea71

Browse files
committed
Feat: show an error if the hostname is longer than 15 characters (#34)
1 parent 1503fdc commit 8ffea71

5 files changed

Lines changed: 148 additions & 4 deletions

File tree

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/computer.py‎

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import socket
2-
from linuxmusterLinuxclient7 import logging, ldapHelper, realm, localUserHelper
1+
import socket, os, subprocess
2+
from linuxmusterLinuxclient7 import logging, constants, ldapHelper, realm, localUserHelper
33

44
def hostname():
55
"""
@@ -10,6 +10,31 @@ def hostname():
1010
"""
1111
return socket.gethostname().split('.', 1)[0]
1212

13+
def hostnameIsValid():
14+
"""
15+
Check if the hostname can be used as a computer account name in the AD (max. 15 characters)
16+
17+
:return: True or False
18+
:rtype: bool
19+
"""
20+
return len(hostname()) <= constants.maxHostnameLength
21+
22+
def checkHostname():
23+
"""
24+
Check if the hostname is valid. If not, an error is logged and shown on the GDM login screen.
25+
26+
:return: True if the hostname is valid, False otherwise
27+
:rtype: bool
28+
"""
29+
if hostnameIsValid():
30+
_setLoginScreenWarning(None)
31+
return True
32+
33+
message = f"The hostname '{hostname()}' is longer than {constants.maxHostnameLength} characters, domain logins will not work! Please use a shorter hostname in the devices.csv."
34+
logging.error(message)
35+
_setLoginScreenWarning(message)
36+
return False
37+
1338
def krbHostName():
1439
"""
1540
Get the krb hostname, eg. `COMPUTER01$`
@@ -54,4 +79,52 @@ def isInAD():
5479
if not rc:
5580
return False
5681

57-
return "domain computers" in groups
82+
return "domain computers" in groups
83+
84+
# --------------------
85+
# - Helper functions -
86+
# --------------------
87+
88+
def _setLoginScreenWarning(message):
89+
"""
90+
Show a warning on the GDM login screen or remove it
91+
92+
:param message: The message to show, None to remove the warning
93+
:type message: str
94+
:return: True if the warning was changed, False otherwise
95+
:rtype: bool
96+
"""
97+
if not os.path.isdir(constants.gdmDconfDir):
98+
return False
99+
100+
if message == None:
101+
if not os.path.exists(constants.loginScreenWarningFile):
102+
return False
103+
os.remove(constants.loginScreenWarningFile)
104+
else:
105+
content = f"[org/gnome/login-screen]\nbanner-message-enable=true\nbanner-message-text=\"{message}\"\n"
106+
if os.path.exists(constants.loginScreenWarningFile):
107+
with open(constants.loginScreenWarningFile, "r") as file:
108+
if file.read() == content:
109+
return False
110+
with open(constants.loginScreenWarningFile, "w") as file:
111+
file.write(content)
112+
113+
subprocess.call(["/usr/share/gdm/generate-config"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
114+
if not _userIsLoggedIn():
115+
subprocess.call(["systemctl", "try-restart", "gdm"])
116+
return True
117+
118+
def _userIsLoggedIn():
119+
"""
120+
Check if a user (other than the display manager) is logged in
121+
122+
:return: True or False
123+
:rtype: bool
124+
"""
125+
result = subprocess.run(["loginctl", "list-users", "--no-legend"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
126+
for line in result.stdout.split("\n"):
127+
columns = line.split()
128+
if len(columns) >= 2 and not columns[1].startswith("gdm"):
129+
return True
130+
return False

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/constants.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
shareMountBasepath = "/home/{}/media"
1111
hiddenShareMountBasepath = "/srv/samba/{}"
1212
machineAccountSysvolMountPath = "/var/lib/samba/sysvol"
13+
maxHostnameLength = 15
14+
gdmDconfDir = "/usr/share/gdm/dconf"
15+
loginScreenWarningFile = gdmDconfDir + "/95-linuxmuster-linuxclient7"
1316
defaultShareLetterTemplate = " ({letter}:)"
1417

1518
etcBaseDir = "/etc/linuxmuster-linuxclient7"

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/setup.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def setup(domain=None, user=None):
1515
"""
1616
logging.info('#### linuxmuster-linuxclient7 setup ####')
1717

18+
if not computer.checkHostname():
19+
return False
20+
1821
userCacheCleared = realm.clearUserCache()
1922
if not userCacheCleared and isSetup():
2023
return False
@@ -88,6 +91,10 @@ def status():
8891

8992
logging.info("Linuxmuster-linuxclient7 is setup!")
9093

94+
logging.info("Checking hostname...")
95+
if not computer.checkHostname():
96+
return False
97+
9198
logging.info("Checking sssd krb5_child capabilities...")
9299
if not _checkSssdKrb5ChildCaps():
93100
return False

‎usr/lib/python3/dist-packages/linuxmusterLinuxclient7/tests/test_computer.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,70 @@
1+
from subprocess import CompletedProcess
12
from unittest import mock
23
from .. import computer
4+
import os
35

46
@mock.patch("linuxmusterLinuxclient7.computer.socket.gethostname")
57
def test_hostname(mockSocketGetHostname):
68
mockSocketGetHostname.return_value = "computer1.linuxmuster.lan"
79
assert computer.hostname() == "computer1"
810

11+
@mock.patch("linuxmusterLinuxclient7.computer.hostname")
12+
def test_hostnameIsValid(mockHostname):
13+
mockHostname.return_value = "computer1"
14+
assert computer.hostnameIsValid()
15+
mockHostname.return_value = "aabk-r100-pc012"
16+
assert computer.hostnameIsValid()
17+
mockHostname.return_value = "aabk-r100-pc0123"
18+
assert not computer.hostnameIsValid()
19+
20+
@mock.patch("linuxmusterLinuxclient7.computer._setLoginScreenWarning")
21+
@mock.patch("linuxmusterLinuxclient7.computer.hostname")
22+
def test_checkHostname(mockHostname, mockSetLoginScreenWarning):
23+
mockHostname.return_value = "computer1"
24+
assert computer.checkHostname()
25+
assert mockSetLoginScreenWarning.call_args.args[0] is None
26+
27+
mockHostname.return_value = "aabk-r100-pc0123"
28+
assert not computer.checkHostname()
29+
assert "aabk-r100-pc0123" in mockSetLoginScreenWarning.call_args.args[0]
30+
31+
@mock.patch("linuxmusterLinuxclient7.computer._userIsLoggedIn")
32+
@mock.patch("linuxmusterLinuxclient7.computer.subprocess.call")
33+
def test_setLoginScreenWarning(mockSubprocessCall, mockUserIsLoggedIn, tmp_path):
34+
warningFile = str(tmp_path / "95-linuxmuster-linuxclient7")
35+
mockUserIsLoggedIn.return_value = False
36+
37+
with mock.patch("linuxmusterLinuxclient7.computer.constants.gdmDconfDir", str(tmp_path)), mock.patch("linuxmusterLinuxclient7.computer.constants.loginScreenWarningFile", warningFile):
38+
assert not computer._setLoginScreenWarning(None)
39+
assert mockSubprocessCall.call_count == 0
40+
41+
assert computer._setLoginScreenWarning("Hostname too long")
42+
with open(warningFile) as file:
43+
content = file.read()
44+
assert "banner-message-enable=true" in content
45+
assert 'banner-message-text="Hostname too long"' in content
46+
assert ["systemctl", "try-restart", "gdm"] in [c.args[0] for c in mockSubprocessCall.call_args_list]
47+
48+
mockSubprocessCall.reset_mock()
49+
assert not computer._setLoginScreenWarning("Hostname too long")
50+
assert mockSubprocessCall.call_count == 0
51+
52+
mockUserIsLoggedIn.return_value = True
53+
assert computer._setLoginScreenWarning(None)
54+
assert not os.path.exists(warningFile)
55+
assert ["systemctl", "try-restart", "gdm"] not in [c.args[0] for c in mockSubprocessCall.call_args_list]
56+
57+
with mock.patch("linuxmusterLinuxclient7.computer.constants.gdmDconfDir", str(tmp_path / "missing")):
58+
assert not computer._setLoginScreenWarning("Hostname too long")
59+
60+
@mock.patch("linuxmusterLinuxclient7.computer.subprocess.run")
61+
def test_userIsLoggedIn(mockSubprocessRun):
62+
mockSubprocessRun.return_value = CompletedProcess(args=[], returncode=0, stdout="60578 gdm-greeter no active\n")
63+
assert not computer._userIsLoggedIn()
64+
65+
mockSubprocessRun.return_value = CompletedProcess(args=[], returncode=0, stdout="60578 gdm-greeter no active\n1662401376 yukigrun no active\n")
66+
assert computer._userIsLoggedIn()
67+
968
@mock.patch("linuxmusterLinuxclient7.computer.hostname")
1069
def test_krbHostName(mockHostname):
1170
mockHostname.return_value = "computer1"

‎usr/share/linuxmuster-linuxclient7/scripts/onBoot‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
# This script is called in root context when the client is booted
77
try:
88
import os, sys
9-
from linuxmusterLinuxclient7 import logging, hooks, shares, constants, keytab, realm
9+
from linuxmusterLinuxclient7 import logging, hooks, shares, constants, keytab, realm, computer
1010

1111
logging.info("====== onBoot started ======")
1212

13+
computer.checkHostname()
14+
1315
keytab.patchKeytab()
1416
realm.pullKerberosTicketForComputerAccount(timeout=5)
1517

0 commit comments

Comments
 (0)