|
| 1 | +#!/usr/bin/env python |
| 2 | +import hashlib, imp, os, sqlite3 |
| 3 | + |
| 4 | +# Load the openshift helper library |
| 5 | +lib_path = os.environ['OPENSHIFT_REPO_DIR'] + 'wsgi/openshift/' |
| 6 | +modinfo = imp.find_module('openshiftlibs', [lib_path]) |
| 7 | +openshiftlibs = imp.load_module('openshiftlibs', modinfo[0], modinfo[1], modinfo[2]) |
| 8 | + |
| 9 | +# Open the database |
| 10 | +conn = sqlite3.connect(os.environ['OPENSHIFT_DATA_DIR'] + '/sqlite3.db') |
| 11 | +c = conn.cursor() |
| 12 | + |
| 13 | +# Grab the default security info |
| 14 | +c.execute('SELECT password FROM AUTH_USER WHERE id = 1') |
| 15 | +pw_info = c.fetchone()[0] |
| 16 | + |
| 17 | +# The password is stored as [hashtype]$[salt]$[hashed] |
| 18 | +pw_fields = pw_info.split("$") |
| 19 | +hashtype = pw_fields[0] |
| 20 | +old_salt = pw_fields[1] |
| 21 | +old_pass = pw_fields[2] |
| 22 | + |
| 23 | +# Randomly generate a new password and a new salt |
| 24 | +# The PASSWORD value below just sets the length (12) |
| 25 | +# for the real new password. |
| 26 | +old_keys = { 'SALT': old_salt, 'PASS': '123456789ABC' } |
| 27 | +use_keys = openshiftlibs.openshift_secure(old_keys) |
| 28 | + |
| 29 | +# Encrypt the new password |
| 30 | +new_salt = use_keys['SALT'] |
| 31 | +new_pass = use_keys['PASS'] |
| 32 | +new_hashed = hashlib.sha1(new_salt + new_pass).hexdigest() |
| 33 | +new_pw_info = "$".join([hashtype,new_salt,new_hashed]) |
| 34 | + |
| 35 | +# Update the database |
| 36 | +c.execute('UPDATE AUTH_USER SET password = ? WHERE id = 1', [new_pw_info]) |
| 37 | +conn.commit() |
| 38 | +c.close() |
| 39 | +conn.close() |
| 40 | + |
| 41 | +# Print the new password info |
| 42 | +print "CLIENT_MESSAGE: The password for user 'admin' in your Django app is " + new_pass + " ...be sure to write this down as you will not see this message again.\n" |
0 commit comments