78 lines
2.3 KiB
Python
Executable File
78 lines
2.3 KiB
Python
Executable File
#! /usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
""" Script to administrate mandaye server
|
|
"""
|
|
|
|
import os
|
|
os.environ['MANDAYE_CONFIG_MODULE'] = 'mandaye_cud.config'
|
|
|
|
import base64
|
|
|
|
from optparse import OptionParser
|
|
|
|
from mandaye import config
|
|
from mandaye.log import logger
|
|
|
|
def get_cmd_options():
|
|
usage = "usage: %prog --createdb|--upgradedb|--cryptpwd"
|
|
parser = OptionParser(usage=usage)
|
|
parser.add_option("--createdb",
|
|
dest="createdb",
|
|
default=False,
|
|
action="store_true",
|
|
help="Create Mandaye database"
|
|
)
|
|
parser.add_option("--upgradedb",
|
|
dest="upgradedb",
|
|
default=False,
|
|
action="store_true",
|
|
help="Upgrade Mandaye database"
|
|
)
|
|
parser.add_option("--cryptpwd",
|
|
dest="cryptpwd",
|
|
default=False,
|
|
action="store_true",
|
|
help="Crypt external password in Mandaye's database"
|
|
)
|
|
(options, args) = parser.parse_args()
|
|
return options
|
|
|
|
def encrypt_pwd(pwd):
|
|
from Crypto.Cipher import AES
|
|
logger.debug("Encrypt password")
|
|
enc_pwd = pwd
|
|
if config.encrypt_secret:
|
|
try:
|
|
cipher = AES.new(config.encrypt_secret, AES.MODE_CFB)
|
|
enc_pwd = cipher.encrypt(pwd)
|
|
enc_pwd = base64.b64encode(enc_pwd)
|
|
except Exception, e:
|
|
if config.debug:
|
|
traceback.print_exc()
|
|
logger.warning('Password encrypting failed %s' % e)
|
|
else:
|
|
logger.warning("You must set a secret to use pwd encryption")
|
|
return enc_pwd
|
|
|
|
def main():
|
|
options = get_cmd_options()
|
|
if options.createdb or options.upgradedb:
|
|
logger.info("Creating or upgrading database...")
|
|
from alembic.config import Config
|
|
from alembic import command
|
|
from mandaye import global_config
|
|
alembic_cfg = Config(global_config.alembic_cfg)
|
|
alembic_cfg.set_main_option("script_location", global_config.alembic_script_path)
|
|
command.upgrade(alembic_cfg, "head")
|
|
logger.info("Database upgraded")
|
|
if options.cryptpwd:
|
|
from mandaye.backends.default import ManagerSPUser
|
|
for user in ManagerSPUser.all():
|
|
user.password = encrypt_pwd(user.password)
|
|
ManagerSPUser.save()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|