Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys
- import getpass
- import MySQLdb
- NOTE = "\033[34;1m[*]\033[0m"
- SUCC = "\033[32;1m[!]\033[0m"
- INPUT = "\033[36;1m[>]\033[0m"
- ERR = "\033[31;1m[!]\033[0m"
- # colorid: black 0, red 1, green 2, yellow 3, cyan 6
- def fmt_color(msg, colorid):
- if sys.stdout.isatty():
- # running in a real terminal
- return '\033[1;' + str(30 + colorid) + 'm' + msg + '\033[0m'
- else:
- # no color support, e.g. redirection
- return msg
- def fmt_bold(msg):
- if sys.stdout.isatty():
- # running in a real terminal
- return '\033[1m' + msg + '\033[0m'
- else:
- # no color support, e.g. redirection
- return msg
- def fmt_red(msg):
- return fmt_color(msg, 1)
- def fmt_green(msg):
- return fmt_color(msg, 2)
- def fmt_yellow(msg):
- return fmt_color(msg, 3)
- def fmt_cyan(msg):
- return fmt_color(msg, 6)
- class DB_vdomain:
- def __init__(self, identifier, name):
- self.identifier = identifier
- self.name = name
- class DB_vuser:
- def __init__(self, identifier, vdomain_id, name):
- self.identifier = identifier
- self.vdomain_id = vdomain_id
- self.name = name
- class DB_valias:
- def __init__(self, identifier, vdomain_id, source, destination):
- self.identifier = identifier
- self.vdomain_id = vdomain_id
- self.source = source
- self.destination = destination
- def db_get_vdomains():
- vdomains = []
- dbcursor.execute('SELECT id, name FROM virtual_domains;')
- for row in dbcursor.fetchall():
- vdomains.append(DB_vdomain(row[0], row[1]))
- return vdomains
- def db_create_vdomain(name):
- dbcursor.execute('INSERT INTO virtual_domains (name) VALUES (%s);', (name,))
- def db_delete_vdomain(identifier):
- dbcursor.execute('DELETE FROM virtual_domains where id=%s;', (identifier,))
- def db_get_vusers(vdomain_id=None):
- vusers = []
- if vdomain_id:
- dbcursor.execute('SELECT id, domain_id, email FROM virtual_users WHERE domain_id = %s;', (vdomain_id,))
- else:
- dbcursor.execute('SELECT id, domain_id, email FROM virtual_users;')
- for row in dbcursor.fetchall():
- vusers.append(DB_vuser(row[0], row[1], row[2]))
- return vusers
- def db_create_vuser(vdomain_id, email, password):
- dbcursor.execute('INSERT INTO virtual_users (domain_id, email, password) VALUES ( %s, %s,CONCAT("{SHA256-CRYPT}", ENCRYPT (%s, CONCAT("$5$", SUBSTRING(SHA(RAND()), -16)))));', (vdomain_id, email, password,))
- def db_update_vuser(email, password):
- dbcursor.execute('UPDATE virtual_users SET password=CONCAT("{SHA256-CRYPT}", ENCRYPT (%s, CONCAT("$5$", SUBSTRING(SHA(RAND()), -16)))) WHERE email=%s;', (password, email,))
- def db_delete_vuser(identifier):
- dbcursor.execute('DELETE FROM virtual_users WHERE id=%s;', (identifier,))
- def db_get_valiases(vdomain_id=None):
- valiases = []
- if vdomain_id:
- dbcursor.execute("SELECT id, domain_id, source, destination FROM virtual_aliases WHERE domain_id = %s;", (vdomain_id,))
- else:
- dbcursor.execute("SELECT id, domain_id, source, destination FROM virtual_aliases;")
- for row in dbcursor.fetchall():
- valiases.append(DB_valias(row[0], row[1], row[2], row[3]))
- return valiases
- def db_create_valias(vdomain_id, srcemail, dstemail):
- dbcursor.execute('INSERT INTO virtual_aliases (domain_id, source, destination) VALUES (%s, %s, %s);', (vdomain_id, srcemail, dstemail,))
- def db_delete_valias(srcemail):
- dbcursor.execute('DELETE FROM virtual_aliases WHERE source=%s;', (srcemail,))
- def menu_main():
- while True:
- print()
- print(' Options:')
- print()
- print(' * ' + fmt_bold('q') + 'uit program')
- print(' * ' + fmt_bold('a') + 'dd domain')
- print(' * ' + fmt_bold('m') + 'anage domain')
- print(' * ' + fmt_bold('d') + 'elete domain')
- print(' * ' + fmt_bold('l') + 'ist domains')
- print()
- input_str = input(fmt_cyan('Action [q/a/m/d/l]: '))
- if input_str in ('quit', 'exit', 'q', 'Q'):
- return
- elif input_str == 'a':
- menu_adddomain()
- elif input_str == 'd':
- menu_deletedomain()
- elif input_str == 'l':
- menu_listdomain()
- elif input_str == 'm':
- menu_managedomain()
- else:
- print()
- print(ERR + ' unknown option \'' + input_str + '\' given.')
- def menu_adddomain():
- print()
- name = input(fmt_cyan('Name for new domain: '))
- print()
- print('Create new domain \'' + fmt_bold(name) + '\'?')
- print()
- ack = input(fmt_yellow('y/N: '))
- print()
- if ack not in ('y', 'Y', 'yes'):
- print(NOTE + ' no changes made')
- return
- db_create_vdomain(name)
- print(SUCC + ' domain \'' + fmt_bold(name) + '\' successfully created')
- def menu_deletedomain():
- domains = db_get_vdomains()
- print()
- if not len(domains):
- print(NOTE + ' no domain found to delete!')
- return
- print(' Available domains:')
- print()
- counter = 1
- for domain in domains:
- print(' ' + fmt_bold(str(counter)) + ' ' + domain.name)
- counter += 1
- print()
- identifier = int(input(fmt_cyan('Domain to delete: ')))
- print()
- if identifier <= 0 or identifier > len(domains):
- print(ERR + ' invalid domain identifier \'' + str(identifier) + '\' given')
- return
- domain = domains[identifier - 1]
- print('Delete domain \'' + fmt_bold(domain.name) + '\'?')
- print()
- ack = input(fmt_yellow('y/N: '))
- print()
- if ack not in ('y', 'Y', 'yes'):
- print(NOTE + ' no changes made')
- return
- db_delete_vdomain(domain.identifier)
- print(SUCC + ' domain \'' + fmt_bold(domain.name) + '\' successfully deleted')
- return
- def menu_listdomain():
- domains = db_get_vdomains()
- print()
- print(SUCC + ' Found ' + fmt_bold(str(len(domains))) + ' domains')
- for domain in domains:
- print(' ' + fmt_bold('-') + ' ' + domain.name)
- def menu_adduseralias(domain):
- print()
- input_str = input(fmt_cyan('Create [u]ser or [a]lias: '))
- print()
- if input_str == 'u':
- return menu_adduser(domain)
- elif input_str == 'a':
- return menu_addalias(domain)
- else:
- print(ERR + ' unknown option \'' + input_str + '\' given.')
- return
- def menu_adduser(domain):
- print()
- name = input(fmt_cyan('Name for new user (@' + domain.name + ' will be appended): '))
- name += '@' + domain.name
- print()
- print('Create new email \'' + fmt_bold(name) + '\'?')
- print()
- ack = input(fmt_yellow('y/N: '))
- if ack not in ('y', 'Y', 'yes'):
- print(NOTE + ' no changes made')
- return
- password = getpass.getpass(fmt_cyan('Password: '))
- password2 = getpass.getpass(fmt_cyan('Password (repeat): '))
- print()
- if password != password2:
- print(ERR + ' passwords do not match - no changes made')
- return
- db_create_vuser(domain.identifier, name, password)
- print(SUCC + ' email \'' + fmt_bold(name) + '\' successfully created')
- def menu_addalias(domain):
- print()
- source = input(fmt_cyan('Source for new alias (@' + domain.name + ' will be appended): '))
- source += '@' + domain.name
- print()
- destination = input(fmt_cyan('Destination for new alias (enter full email address): '))
- print()
- print('Create new alias from \'' + fmt_bold(source) + '\' to \'' + fmt_bold(destination) + '\'?')
- print()
- ack = input(fmt_yellow('y/N: '))
- if ack not in ('y', 'Y', 'yes'):
- print(NOTE + ' no changes made')
- return
- db_create_valias(domain.identifier, source, destination)
- print(SUCC + ' alias \'' + fmt_bold(source) + '\' to \'' + fmt_bold(destination) + '\' successfully created')
- def menu_pwchangeuser(domain):
- users = db_get_vusers(domain.identifier)
- print()
- if not len(users):
- print(NOTE + ' no user found!')
- return
- print(' Available users:')
- print()
- counter = 1
- for user in users:
- print(' ' + fmt_bold(str(counter)) + ' ' + user.name)
- counter += 1
- print()
- identifier = int(input(fmt_cyan('Change password for user: ')))
- print()
- if identifier <= 0 or identifier > len(users):
- print(ERR + ' invalid user identifier \'' + str(identifier) + '\' given')
- return
- user = users[identifier - 1]
- print('Changing password for user \'' + fmt_bold(user.name) + '\'..')
- print()
- password = getpass.getpass(fmt_cyan('Password: '))
- password2 = getpass.getpass(fmt_cyan('Password (repeat): '))
- print()
- if password != password2:
- print(ERR + ' passwords do not match - no changes made')
- return
- db_update_vuser(user.name, password)
- print(SUCC + ' password for user \'' + fmt_bold(user.name) + '\' successfully changed')
- def menu_deleteuser(domain):
- users = db_get_vusers(domain.identifier)
- aliases = db_get_valiases(domain.identifier)
- print()
- if not len(users) and not len(aliases):
- print(NOTE + ' no user or aliases found to delete!')
- return
- print(' Available users:')
- print()
- counter = 1
- for user in users:
- print(' ' + fmt_bold(str(counter)) + ' ' + user.name)
- counter += 1
- print()
- print(' Available aliases:')
- print()
- for alias in aliases:
- print(' ' + fmt_bold(str(counter)) + ' ' + alias.source.ljust(35) + ' ' + fmt_bold('->') + ' ' + alias.destination)
- counter += 1
- print()
- identifier = int(input(fmt_cyan('User/Alias to delete: ')))
- print()
- if identifier <= 0 or identifier >= counter:
- print(ERR + ' invalid user/alias identifier \'' + str(identifier) + '\' given')
- return
- if identifier < len(users):
- user = users[identifier - 1]
- print('Delete user \'' + fmt_bold(user.name) + '\'?')
- print()
- ack = input(fmt_yellow('y/N: '))
- print()
- if ack not in ('y', 'Y', 'yes'):
- print(NOTE + ' no changes made')
- return
- db_delete_vuser(user.identifier)
- print(SUCC + ' user \'' + fmt_bold(user.name) + '\' successfully deleted')
- return
- else:
- alias = aliases[identifier - len(users) - 1]
- print('Delete alias \'' + fmt_bold(alias.source) + '\'?')
- print()
- ack = input(fmt_yellow('y/N: '))
- print()
- if ack not in ('y', 'Y', 'yes'):
- print(NOTE + ' no changes made')
- return
- db_delete_valias(alias.source)
- print(SUCC + ' alias \'' + fmt_bold(alias.source) + '\' successfully deleted')
- return
- def menu_listusers(domain):
- users = db_get_vusers(domain.identifier)
- aliases = db_get_valiases(domain.identifier)
- print()
- print(SUCC + ' Found ' + fmt_bold(str(len(users))) + ' users')
- for user in users:
- print(' ' + fmt_bold('-') + ' ' + user.name)
- print()
- print(SUCC + ' Found ' + fmt_bold(str(len(aliases))) + ' aliases')
- for alias in aliases:
- print(' ' + fmt_bold('-') + ' ' + alias.source.ljust(35) + ' ' + fmt_bold('->') + ' ' + alias.destination)
- def menu_managedomain():
- domains = db_get_vdomains()
- print()
- if not len(domains):
- print(NOTE + ' no domain found to manage!')
- return
- print(' Available domains:')
- print()
- counter = 1
- for domain in domains:
- print(' ' + fmt_bold(str(counter)) + ' ' + domain.name)
- counter += 1
- print()
- identifier = int(input(fmt_cyan('Domain to manage: ')))
- if identifier <= 0 or identifier > len(domains):
- print()
- print(ERR + ' invalid domain identifier \'' + str(identifier) + '\' given')
- return
- domain = domains[identifier - 1]
- while True:
- print()
- print('Managing domain \'' + fmt_bold(domain.name) + '\'')
- print()
- print(' Options:')
- print()
- print(' * ' + fmt_bold('q') + 'uit managing')
- print(' * ' + fmt_bold('l') + 'ist users and aliases')
- print(' * ' + fmt_bold('a') + 'dd user/alias')
- print(' * ' + fmt_bold('d') + 'elete user/alias')
- print(' * ' + fmt_bold('c') + 'hange user password')
- print()
- input_str = input(fmt_cyan('Action [q/l/a/d/c]: '))
- if input_str in ('quit', 'exit', 'q', 'Q'):
- return
- elif input_str == 'l':
- menu_listusers(domain)
- elif input_str == 'a':
- menu_adduseralias(domain)
- elif input_str == 'd':
- menu_deleteuser(domain)
- elif input_str == 'c':
- menu_pwchangeuser(domain)
- else:
- print()
- print(ERR + ' unknown option \'' + input_str + '\' given.')
- if __name__ == '__main__':
- global dbcursor #pylint: disable=global-at-module-level
- print()
- print(fmt_yellow('#'))
- print(fmt_yellow('# ISPMail userctl'))
- print(fmt_yellow('#'))
- db = MySQLdb.connect(
- host='localhost',
- user='root',
- #password='password',
- db='mailserver')
- dbcursor = db.cursor()
- try:
- menu_main()
- except KeyboardInterrupt:
- db.rollback()
- dbcursor.close()
- dbcursor = None
- db.close()
- print()
- print()
- print(NOTE + ' Bye! ' + fmt_yellow('No changes saved'))
- print()
- exit(1)
- db.commit()
- dbcursor.close()
- dbcursor = None
- db.close()
- print()
- print(SUCC + ' Bye..')
- print()
- exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement