本文整理汇总了Python中spwd.getspall方法的典型用法代码示例。如果您正苦于以下问题:Python spwd.getspall方法的具体用法?Python spwd.getspall怎么用?Python spwd.getspall使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类spwd
的用法示例。
在下文中一共展示了spwd.getspall方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import spwd [as 别名]
# 或者: from spwd import getspall [as 别名]
def __init__(self, anonymous_user=None):
if os.geteuid() != 0 or not spwd.getspall():
raise AuthorizerError("super user privileges are required")
self.anonymous_user = anonymous_user
if self.anonymous_user is not None:
try:
pwd.getpwnam(self.anonymous_user).pw_dir
except KeyError:
raise AuthorizerError('no such user %s' % anonymous_user)
# --- overridden / private API
示例2: check_for_default_passwords
# 需要导入模块: import spwd [as 别名]
# 或者: from spwd import getspall [as 别名]
def check_for_default_passwords(config_path):
"""
Check if the 'pi' user current password hash is in our list of default password hashes.
"""
base_dir = Path(config_path)
pass_hashes_file_path = base_dir.joinpath('pass_hashes.txt') # For deb installation.
if not pass_hashes_file_path.is_file():
base_dir = Path(__file__).resolve().parent.parent
pass_hashes_file_path = base_dir.joinpath('misc/pass_hashes.txt')
with pass_hashes_file_path.open() as f:
read_data = f.read()
known_passwords = {}
for username_password in read_data.splitlines():
username, password = username_password.split(':', maxsplit=1)
pw = known_passwords.get(username, [])
pw.append(password)
known_passwords[username] = pw
def hash_matches(pwdp, plaintext_password):
i = pwdp.rfind('$')
salt = pwdp[:i]
crypted = crypt.crypt(plaintext_password, salt)
return crypted == pwdp
usernames = set()
for shadow in spwd.getspall():
encrypted_password = shadow.sp_pwdp
for password in known_passwords.get(shadow.sp_namp, []):
if hash_matches(encrypted_password, password):
usernames.add(shadow.sp_namp)
return list(usernames)
示例3: test_accounts_nopassword
# 需要导入模块: import spwd [as 别名]
# 或者: from spwd import getspall [as 别名]
def test_accounts_nopassword():
try:
import spwd
except ImportError:
logger.info("Import of spwd failed ")
return TestResult(Result.SKIP, "Unable to import 'spwd' module")
disabled = []
locked = []
passworded = []
no_password = []
shadow_entries = spwd.getspall()
for entry in shadow_entries:
# passwords which start with ! have been locked
if entry.sp_pwd.startswith('!'):
locked.append(entry.sp_nam)
# passwords which start with * have been disabled
elif entry.sp_pwd.startswith('*'):
disabled.append(entry.sp_nam)
# blank passwords are bad!
elif entry.sp_pwd == "":
no_password.append(entry.sp_nam)
# otherwise the account has a password
else:
passworded.append(entry.sp_nam)
if len(no_password) > 0:
notes = "Account(s) { " + str(no_password) + " } have no password!"
test_result = Result.FAIL
else:
notes = ("Disabled: " + str(len(disabled)) + ", Locked: " +
str(len(locked)) + ", Password: " + str(len(passworded)) +
", No Password: " + str(len(no_password)))
test_result = Result.PASS
return TestResult(test_result, notes)