Skip to content

Commit 84f6b8a

Browse files
committed
T7557: Update locked user check without spwd module
1 parent 2455914 commit 84f6b8a

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/op_mode/show_users.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ def is_locked(user_name: str) -> bool:
4747
"""Check if a given user has password in shadow db"""
4848

4949
try:
50-
import warnings
51-
with warnings.catch_warnings():
52-
warnings.filterwarnings("ignore",category=DeprecationWarning)
53-
import spwd
54-
encrypted_password = spwd.getspnam(user_name)[1]
55-
return encrypted_password == '*' or encrypted_password.startswith('!')
56-
except (KeyError, PermissionError):
50+
with open('/etc/shadow', 'r') as f:
51+
for line in f.readlines():
52+
args = line.strip().split(':')
53+
if len(args) < 2:
54+
continue
55+
if args[0] == user_name:
56+
return len(args[1]) > 0 and args[1].startswith('!')
57+
except PermissionError:
5758
print('Cannot access shadow database, ensure this script is run with sufficient permissions')
5859
sys.exit(1)
5960

0 commit comments

Comments
 (0)