58 lines
1.5 KiB
Python
Executable file
58 lines
1.5 KiB
Python
Executable file
#! /usr/bin/env python3
|
|
|
|
import sys
|
|
import os
|
|
import argparse
|
|
import getpass
|
|
|
|
from xenu_nntp.config import Config
|
|
from xenu_nntp.db import Database
|
|
from xenu_nntp.user import User
|
|
from xenu_nntp.passwd import crypt
|
|
|
|
parser = argparse.ArgumentParser(description='Set password of account')
|
|
parser.add_argument('--config-file', '-f', type=str, help='Specify a configuration file location')
|
|
parser.add_argument('username', type=str, help='Username of account')
|
|
|
|
args = parser.parse_args()
|
|
|
|
config = Config.load(args.config_file)
|
|
db = Database.from_config(config)
|
|
|
|
user = db.get(User, {'username': args.username})
|
|
|
|
if user is None:
|
|
print(f"{sys.argv[0]}: unknown user '{args.username}'", file=sys.stderr)
|
|
exit(1)
|
|
|
|
attempts_total = 3
|
|
attempts_left = attempts_total
|
|
|
|
while attempts_left > 0:
|
|
attempts_left -= 1
|
|
|
|
password_1 = getpass.getpass()
|
|
|
|
if password_1 == '':
|
|
continue
|
|
|
|
password_2 = getpass.getpass('Enter password again: ')
|
|
|
|
if password_1 != password_2:
|
|
continue
|
|
|
|
user.password = crypt(password_2)
|
|
|
|
try:
|
|
db.update(user)
|
|
db.commit()
|
|
except Exception as e:
|
|
print(f"Unable to change password for user '{args.username}': {e}")
|
|
exit(1)
|
|
|
|
print(f"Password changed successfully for user '{args.username}'.")
|
|
exit(0)
|
|
|
|
if attempts_left == 0:
|
|
print(f"Failed to change password for user '{args.username}' after {attempts_total} attempts.", file=sys.stderr)
|
|
exit(1)
|