71 lines
2.0 KiB
Python
Executable File
71 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
""" Script to administrate mandaye server
|
|
"""
|
|
|
|
import base64
|
|
|
|
from optparse import OptionParser
|
|
|
|
from mandaye import config
|
|
from mandaye.log import logger
|
|
from mandaye.models import ExtUser
|
|
from mandaye.db import sql_session
|
|
|
|
|
|
def get_cmd_options():
|
|
usage = "usage: %prog --createdb|--cryptpwd"
|
|
parser = OptionParser(usage=usage)
|
|
parser.add_option("--createdb",
|
|
dest="createdb",
|
|
default=False,
|
|
action="store_true",
|
|
help="Create the 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()
|
|
if not options.createdb:
|
|
parser.error("You must use option --createdb")
|
|
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:
|
|
logger.info("Creating database...")
|
|
if config.db_url:
|
|
from mandaye.models import Base
|
|
from sqlalchemy import create_engine
|
|
engine = create_engine(config.db_url)
|
|
Base.metadata.create_all(engine)
|
|
logger.info("Database created")
|
|
if options.cryptpwd:
|
|
for user in sql_session().query(ExtUser).all():
|
|
user.password = encrypt_pwd(user.password)
|
|
sql_session().commit()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|