Created
January 4, 2013 15:30
-
-
Save piger/4453478 to your computer and use it in GitHub Desktop.
A simple script that can be used to hard-reset all the users' credentials in a MoinMoin installation with a random password.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Hard-reset MoinMoin passwords | |
A simple script that can be used to hard-reset all the users' credentials in a | |
MoinMoin installation with a random password. | |
02/01/13 - Daniel Kertesz <[email protected]> | |
""" | |
import os | |
import re | |
import getopt | |
import base64 | |
from optparse import OptionParser | |
from MoinMoin.support.python_compatibility import hash_new | |
from MoinMoin.util import random_string | |
# Generated password length | |
PASSWORD_LENGTH = 15 | |
def create_moin_hash(password): | |
salt = random_string(20) | |
assert isinstance(salt, str) | |
h = hash_new("sha1", password) | |
h.update(salt) | |
return '{SSHA}' + base64.encodestring(h.digest() + salt).rstrip() | |
def parse_moin_userfile(filename): | |
fd = open(filename) | |
data = fd.read() | |
fd.close() | |
if not re.search(r'email=[^@]+@[^\n]+', data): | |
print "No mail found in %s, skipping." % filename | |
return False | |
m = re.search(r'enc_password=.*?[\n]', data) | |
if not m: | |
print "No enc_password found in %s, skipping" % filename | |
return False | |
random_pw = random_string(15) | |
new_pw = create_moin_hash(random_pw) | |
data = re.sub(r'enc_password=.*?[\n]', | |
'enc_password=%s\n' % new_pw, | |
data) | |
fd = open(filename, 'w') | |
fd.write(data) | |
fd.close() | |
return True | |
def main(): | |
parser = OptionParser() | |
parser.add_option('-d', '--user-dir', dest='user_dir', | |
metavar='DIRECTORY', | |
help="Specify a MoinMoin user directory") | |
(opts, args) = parser.parse_args() | |
if not opts.user_dir: | |
parser.error("You must specify a MoinMoin user directory with -d") | |
count = 0 | |
for root, dirs, files in os.walk(opts.user_dir): | |
for name in files: | |
if name.endswith('.trail'): continue | |
filename = os.path.join(root, name) | |
if parse_moin_userfile(filename): | |
count += 1 | |
print "Changed %d passwords" % count | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment