34 lines
1.3 KiB
Python
34 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Fill absent pre-login Unix identity entries; preserve explicit administrator choices."""
|
|
import pathlib, pwd, re, shutil, sys, tempfile, os, time
|
|
|
|
def repair(path):
|
|
path = pathlib.Path(path)
|
|
text = path.read_text()
|
|
present = set(re.findall(r'^\s*(10|11)\s+', text, re.M))
|
|
account = pwd.getpwnam('nobody')
|
|
values = {'10': account.pw_gid, '11': account.pw_uid}
|
|
if any(v <= 0 for v in values.values()):
|
|
raise ValueError('nobody must have non-root UID and GID')
|
|
addition = ''.join(f'{key} {value}\n' for key, value in values.items() if key not in present)
|
|
if not addition:
|
|
return False
|
|
backup = str(path) + '.pre-default-identity-' + str(time.time_ns())
|
|
shutil.copy2(path, backup)
|
|
os.chmod(backup, 0o600)
|
|
fd, temp = tempfile.mkstemp(dir=path.parent)
|
|
try:
|
|
with os.fdopen(fd, 'w') as stream:
|
|
stream.write(text + ('\n' if text and not text.endswith('\n') else '') + '# Unprivileged identity before NetWare login\n' + addition)
|
|
st = path.stat()
|
|
os.chown(temp, st.st_uid, st.st_gid)
|
|
os.chmod(temp, 0o600)
|
|
os.replace(temp, path)
|
|
finally:
|
|
if os.path.exists(temp): os.unlink(temp)
|
|
print('Added missing default identity entries; backup: ' + backup)
|
|
return True
|
|
|
|
if __name__ == '__main__':
|
|
repair(sys.argv[1])
|