Skip to content

Commit

Permalink
Set password from commandline
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Mottram committed May 25, 2021
1 parent 3807eff commit d9d465b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions miniircd
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,11 @@ def main() -> None:
metavar="X",
help="Deletes an account with the given nickname",
)
ap.add_argument(
"--set-password",
metavar="X",
help="Set password with nickname:newpassword",
)
ap.add_argument(
"--verbose",
action="store_true",
Expand Down Expand Up @@ -2131,6 +2136,38 @@ def main() -> None:
fp.write(new_passwords_file)
sys.exit()

if args.set_password:
if ':' not in args.set_password:
args.error("Specify nickname:password")
fields = args.set_password.split(':')
nickname = fields[0]
password = fields[1].replace('\n', '')
filename = os.path.join(args.state_dir, 'passwords')
if os.path.isfile(filename):
if nickname + ' ' in open(filename).read():
new_passwords_file = ''
with open(filename, "r") as fp:
lines = fp.readlines()
new_passwords_file = ''
for line in lines:
if not line.endswith('\n'):
line += '\n'
if not line.startswith(nickname + ' '):
new_passwords_file += line
else:
salt = line.split(' ')[1]
pwdhash = hashlib.pbkdf2_hmac('sha512',
password.encode('utf-8'),
salt.encode('utf-8'), 100000)
pwdhash = binascii.hexlify(pwdhash)
new_line = nickname + ' ' + salt + ' ' + pwdhash
if line.endswith(' oper\n'):
new_line += ' oper'
new_passwords_file += new_line + '\n'
with open(filename, "w+") as fp:
fp.write(new_passwords_file)
sys.exit()

if bool(args.ssl_cert_file) != bool(args.ssl_key_file):
args.error("Must specify both --ssl-cert-file and --ssl-key-file")
if args.ssl_pem_file:
Expand Down

0 comments on commit d9d465b

Please sign in to comment.