proxmaster/clientsdb.py

160 lines
5.5 KiB
Python
Raw Normal View History

2016-02-15 05:30:43 -05:00
# -*- coding: utf-8
#
# manage clientsdb.json
#import site packages
import json
2016-03-03 20:51:54 -05:00
import hmac
import bcrypt
2016-02-15 05:30:43 -05:00
#import local packages
import ioconfig
import utils
def addclient(vmid, vmname, clientid, clientname, clientemail, vmpass):
""" add new client with the requested vm to the clientsdb.json """
2016-02-15 05:30:43 -05:00
clientsdb = readclientsdb()
2016-03-01 22:01:33 -05:00
2016-02-15 05:30:43 -05:00
if str(clientid) in clientsdb:
2016-03-31 10:40:40 -04:00
ioconfig.logger.info('client[{}]> already exist. merging.'.format(clientid))
2016-02-15 05:30:43 -05:00
else:
2016-03-31 10:40:40 -04:00
ioconfig.logger.info('client[{}]> does not exist. creating...'.format(clientid))
#generate password and send it to the client
newpass = utils.genpassword(30)
2016-03-31 19:37:21 -04:00
ioconfig.logger.info('client[{}]> initial password is: {}'.format(clientid, newpass))
2016-03-31 10:40:40 -04:00
salt = bcrypt.gensalt()
2016-04-01 19:53:16 -04:00
b_newpass = newpass.encode('ascii')
encpasswd = bcrypt.hashpw(b_newpass, salt).decode('ascii')
vcard = { 'name':str(clientname), 'email':str(clientemail), 'encpasswd':str(encpasswd), 'id':str(clientid) }
2016-02-15 05:30:43 -05:00
newclient = { str(clientid):vcard }
clientsdb.update(newclient)
2016-03-31 19:37:21 -04:00
#Send initial email to the user as we will use the internal auth from now on.
###utils.sendmail(clientemail, '{} logged in.'.format)
2016-03-31 19:37:21 -04:00
#TODO: Sync with proxmaster-admin database (shell command could be used for this one)
ioconfig.logger.info('client[{}]> vmid {} is now owned by {} ({})'.format(clientid, vmid, clientemail, clientname))
2016-03-30 18:12:38 -04:00
#create initial vm template
vmdata = { 'hostname':str(vmname), 'vmid':str(vmid), 'ownerid':str(clientid) }
2016-02-15 05:30:43 -05:00
clientsdb[str(clientid)][str(vmid)] = vmdata
writeclientsdb(clientsdb)
2016-03-03 20:51:54 -05:00
2016-03-31 10:40:40 -04:00
def setencpasswd(clientemail, newpass):
2016-03-03 20:51:54 -05:00
""" setup a new management password """
salt = bcrypt.gensalt()
2016-04-01 19:53:16 -04:00
b_newpass = newpass.encode('ascii')
encpasswd = bcrypt.hashpw(b_newpass, salt).decode('ascii')
2016-03-03 20:51:54 -05:00
try:
clientsdb = readclientsdb()
2016-03-31 10:40:40 -04:00
path = utils.get_path(clientsdb, clientemail)
2016-03-03 20:51:54 -05:00
c_id = str(path[0])
#check the returned path with forward query
2016-03-31 10:40:40 -04:00
query = clientsdb[c_id]['email']
2016-03-31 19:37:21 -04:00
#ioconfig.logger.info('client[{}]> path={}'.format(c_id, str(path)))
2016-03-03 20:51:54 -05:00
except:
2016-04-08 10:48:18 -04:00
ioconfig.logger.critical('clients> client {} not found'.format(clientemail))
2016-03-03 20:51:54 -05:00
raise
2016-03-31 10:40:40 -04:00
if query != clientemail:
2016-03-31 19:37:21 -04:00
ioconfig.logger.critical('clients> test query returns different vmname! check clients db for consistency!')
2016-03-03 20:51:54 -05:00
raise
else:
2016-03-31 10:40:40 -04:00
clientsdb[c_id]['encpasswd'] = encpasswd
2016-03-31 19:37:21 -04:00
ioconfig.logger.info('client[{}]> {} password changed!'.format(c_id, clientemail))
2016-03-03 20:51:54 -05:00
writeclientsdb(clientsdb)
2016-03-31 10:40:40 -04:00
#TODO: Send new email to the client to notify the password change. This time sending the password in plain text is not needed.
2016-03-03 20:51:54 -05:00
def checkin(clientid):
2016-06-26 11:09:22 -04:00
""" returns a list of owned vmids if client id matches the client database. (logged-in users)"""
#1. search for the client
try:
clientsdb = readclientsdb()
c_id = clientsdb[str(clientid)]
2016-05-25 17:56:14 -04:00
#c_id.pop('encpasswd')
email = c_id['email']
ioconfig.logger.info('client[{}]> {} active'.format(clientid, email))
return c_id
except:
ioconfig.logger.error('clients> user id: {} could not be checked.'.format(clientid))
return None
2016-03-31 10:40:40 -04:00
def validate(clientemail, password):
2016-06-26 11:09:22 -04:00
""" returns a list of owned vmids if credentials match an user from the database. (fresh logins)"""
2016-03-31 10:40:40 -04:00
#1. search for the client
try:
clientsdb = readclientsdb()
path = utils.get_path(clientsdb, clientemail)
c_id = str(path[0])
except:
ioconfig.logger.error('clients> {} was not found in the database!'.format(clientemail))
#log bad ips here...
return None
2016-03-31 10:40:40 -04:00
#2. check the password
encpass = clientsdb[c_id]['encpasswd']
b_srvpass = password.encode('ascii', 'ignore')
b_encpass = encpass.encode('ascii', 'ignore')
2016-03-31 10:40:40 -04:00
if (hmac.compare_digest(bcrypt.hashpw(b_srvpass, b_encpass), b_encpass)):
#login successful
2016-03-31 19:37:21 -04:00
ioconfig.logger.info('client[{}]> {} logged in successfully'.format(c_id, clientemail))
2016-03-31 10:40:40 -04:00
#TODO: Notify admin
#3. generate vmlist to return the owned ids to the client.
return clientvms(clientsdb[c_id])
else:
2016-05-23 16:52:27 -04:00
ioconfig.logger.warning('client[{}]> {} access denied!'.format(c_id, clientemail))
2016-03-31 10:40:40 -04:00
#cant compare password
#TODO: Log attempts and block.
return None
2016-03-31 10:40:40 -04:00
def clientvms(vmlist):
""" generate vmlist """
2016-03-31 19:37:21 -04:00
response = {}
for vmid,data in vmlist.items():
2016-05-16 08:56:40 -04:00
response[vmid] = data
return response
2016-02-15 05:30:43 -05:00
def vmowner(vmid, vmname, verbose):
""" find the owner of the vm """
clientsdb = readclientsdb()
try:
clientid = utils.find_rec(clientsdb, str(vmid))[0]['ownerid']
2016-02-15 05:30:43 -05:00
clientname = clientsdb[str(clientid)]['name']
except:
raise
clientid = '0' #unknown owner
clientname = 'unknown'
if verbose:
2016-04-08 10:48:18 -04:00
ioconfig.logger.info('client[{}]> {} is the owner of {} ({})'.format(str(clientid), clientname, str(vmid), vmname))
2016-02-15 05:30:43 -05:00
return clientid
def readclientsdb():
""" read client db """
try:
with open('clients.json') as dbr:
clientsdb = json.load(dbr)
dbr.close()
except:
clientsdb = {}
ioconfig.logger.warning('clients> initializing...')
#writeclientsdb(clientsdb)
return clientsdb
def writeclientsdb(clientsdb):
""" write db """
with open('clients.json', 'w') as dbw:
json.dump(clientsdb, dbw)
dbw.close()
2016-03-03 20:51:54 -05:00
if __name__ == '__main__':
2016-06-25 11:27:45 -04:00
setencpasswd('fqdn', '123456')
2016-03-31 19:37:21 -04:00