Skip to content

Commit 8c35727

Browse files
committed
phpmyadmin autologin: access keys generation
1 parent bcd0716 commit 8c35727

File tree

8 files changed

+114
-85
lines changed

8 files changed

+114
-85
lines changed

baseTemplate/templates/baseTemplate/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@
468468
<li class="listDatabases"><a href="{% url 'listDBs' %}"
469469
title="{% trans 'List Databases' %}"><span>{% trans "List Databases" %}</span></a>
470470
</li>
471-
<li><a href="/phpmyadmin/index.php" title="{% trans 'PHPMYAdmin' %}"
471+
<li><a href="{% url 'phpMyAdmin' %}" title="{% trans 'PHPMYAdmin' %}"
472472
target="_blank"><span>{% trans "PHPMYAdmin" %}</span></a></li>
473473
<!----<li><a href="{% url 'modifyPackage' %}" title="Change Password"><span>Change Password</span></a></li>--->
474474
</ul>

databases/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
43
from django.db import models
54
from websiteFunctions.models import Websites
65

@@ -15,3 +14,8 @@ class DBMeta(models.Model):
1514
database = models.ForeignKey(Databases, on_delete=models.CASCADE)
1615
key = models.CharField(max_length=200)
1716
value = models.TextField()
17+
18+
class GlobalUserDB(models.Model):
19+
username = models.CharField(max_length=200)
20+
password = models.CharField(max_length=500)
21+
token = models.CharField(max_length=20)

databases/static/databases/databases.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,9 @@ app.controller('listDBs', function ($scope, $http) {
569569

570570
app.controller('phpMyAdmin', function ($scope, $http, $window) {
571571

572-
function setupPHPMYAdminSession() {
572+
$scope.generateAccess = function() {
573573

574-
url = "/dataBases/setupPHPMYAdminSession";
574+
url = "/dataBases/generateAccess";
575575

576576
var data = {};
577577

@@ -587,7 +587,6 @@ app.controller('phpMyAdmin', function ($scope, $http, $window) {
587587

588588
function ListInitialDatas(response) {
589589

590-
591590
if (response.data.status === 1) {
592591
$window.location.href = '/phpmyadmin';
593592
}
@@ -598,6 +597,5 @@ app.controller('phpMyAdmin', function ($scope, $http, $window) {
598597
function cantLoadInitialDatas(response) {}
599598

600599
}
601-
setupPHPMYAdminSession();
602600

603601
});
Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,36 @@
11
{% extends "baseTemplate/index.html" %}
22
{% load i18n %}
3-
{% block title %}{% trans "phpMyAdmin - CyberPanel" %}{% endblock %}
3+
{% block title %}{% trans "PHPMYAdmin - CyberPanel" %}{% endblock %}
44
{% block content %}
55

6-
{% load static %}
7-
{% get_current_language as LANGUAGE_CODE %}
8-
<!-- Current language: {{ LANGUAGE_CODE }} -->
6+
{% load static %}
7+
{% get_current_language as LANGUAGE_CODE %}
8+
<!-- Current language: {{ LANGUAGE_CODE }} -->
99

10-
<div class="container">
11-
<div id="page-title">
12-
<h2>{% trans "Create Database" %}</h2>
13-
<p>{% trans "Create a new database on this page." %}</p>
14-
</div>
1510

16-
<div ng-controller="phpMyAdmin" class="panel">
17-
<div class="panel-body">
18-
<h3 class="content-box-header">
19-
{% trans "Create Database" %} <img ng-hide="createDatabaseLoading" src="{% static 'images/loading.gif' %}">
20-
</h3>
21-
<div class="example-box-wrapper">
22-
23-
<form class="form-horizontal bordered-row panel-body">
24-
25-
<div class="form-group">
26-
<label class="col-sm-3 control-label">{% trans "Select Website" %}</label>
27-
<div class="col-sm-6">
28-
<select ng-change="showDetailsBoxes()" ng-model="databaseWebsite" class="form-control">
29-
{% for items in websitesList %}
30-
<option>{{ items }}</option>
31-
{% endfor %}
32-
</select>
33-
</div>
34-
</div>
35-
36-
37-
<div class="form-group">
38-
<label class="col-sm-3 control-label"></label>
39-
<div class="col-sm-4">
40-
<div ng-hide="databaseCreationFailed" class="alert alert-danger">
41-
<p>{% trans "Cannot create database. Error message:" %} {$ errorMessage $}</p>
42-
</div>
43-
44-
<div ng-hide="databaseCreated" class="alert alert-success">
45-
<p>{% trans "Database created successfully." %}</p>
46-
</div>
47-
48-
49-
<div ng-hide="couldNotConnect" class="alert alert-danger">
50-
<p>{% trans "Could not connect to server. Please refresh this page." %}</p>
51-
</div>
52-
</div>
11+
<div ng-controller="phpMyAdmin" class="container">
12+
<div id="page-title">
13+
<h2>{% trans "PHPMYAdmin" %}</h2>
14+
<p>{% trans "Access your databases via PHPMYAdmin" %}</p>
15+
</div>
5316

17+
<div class="panel">
18+
<div class="panel-body">
19+
<h3 class="title-hero">
20+
{% trans "PHPMYAdmin" %}
21+
</h3>
22+
<div class="example-box-wrapper">
5423

24+
<p>{% trans "Auto-login for PHPMYAdmin is now supported. Click the button below to generate auto-access for PHPMYAdmin" %}</p>
25+
<br>
26+
<a ng-click="generateAccess()" href="#">
27+
<button class="btn btn-primary">Access Now
28+
</button>
29+
</a>
5530

5631
</div>
57-
58-
59-
</form>
60-
61-
62-
63-
32+
</div>
6433
</div>
65-
</div>
66-
</div>
6734

68-
69-
70-
</div>
71-
72-
73-
{% endblock %}
35+
</div>
36+
{% endblock %}

databases/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
url(r'^remoteAccess$', views.remoteAccess, name='remoteAccess'),
1818
url(r'^allowRemoteIP$', views.allowRemoteIP, name='allowRemoteIP'),
1919
url(r'^phpMyAdmin$', views.phpMyAdmin, name='phpMyAdmin'),
20-
url(r'^setupPHPMYAdminSession$', views.setupPHPMYAdminSession, name='setupPHPMYAdminSession'),
20+
url(r'^generateAccess$', views.generateAccess, name='generateAccess'),
2121
]

databases/views.py

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
import json
99
from plogical.processUtilities import ProcessUtilities
1010
from loginSystem.models import Administrator
11-
import CyberCP.settings as settings
11+
from plogical.acl import ACLManager
12+
from databases.models import GlobalUserDB
13+
from plogical import randomPassword
14+
from cryptography.fernet import Fernet
15+
from plogical.mysqlUtilities import mysqlUtilities
1216
# Create your views here.
1317

1418
def loadDatabaseHome(request):
@@ -150,27 +154,63 @@ def phpMyAdmin(request):
150154
except KeyError:
151155
return redirect(loadLoginPage)
152156

153-
def setupPHPMYAdminSession(request):
157+
def generateAccess(request):
154158
try:
155159

156160
userID = request.session['userID']
157161
admin = Administrator.objects.get(id = userID)
162+
currentACL = ACLManager.loadedACL(userID)
158163

159-
execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/databases/databaseManager.py"
160-
execPath = execPath + " generatePHPMYAdminData --userID " + str(userID)
161-
162-
output = ProcessUtilities.outputExecutioner(execPath)
163-
164-
if output.find("1,") > -1:
165-
request.session['PMA_single_signon_user'] = admin.userName
166-
request.session['PMA_single_signon_password'] = output.split(',')[1]
167-
data_ret = {'status': 1}
168-
json_data = json.dumps(data_ret)
169-
return HttpResponse(json_data)
170-
else:
171-
data_ret = {'status': 1}
172-
json_data = json.dumps(data_ret)
173-
return HttpResponse(json_data)
164+
try:
165+
GlobalUserDB.objects.get(username=admin.userName)
166+
except:
167+
168+
## Key generation
169+
170+
keySavePath = '/home/cyberpanel/phpmyadmin_%s' % (admin.userName)
171+
key = Fernet.generate_key()
172+
173+
writeToFile = open(keySavePath, 'w')
174+
writeToFile.write(key.decode())
175+
writeToFile.close()
176+
177+
command = 'chown root:root %s' % (keySavePath)
178+
ProcessUtilities.executioner(command)
179+
180+
command = 'chmod 600 %s' % (keySavePath)
181+
ProcessUtilities.executioner(command)
182+
183+
##
184+
185+
password = randomPassword.generate_pass()
186+
f = Fernet(key)
187+
GlobalUserDB(username=admin, password=f.encrypt(password.encode('utf-8'))).save()
188+
189+
sites = ACLManager.findWebsiteObjects(currentACL, userID)
190+
191+
createUser = 1
192+
193+
for site in sites:
194+
for db in site.databases_set.all():
195+
mysqlUtilities.addUserToDB(db.dbName, admin.userName, password, createUser)
196+
createUser = 0
197+
198+
# execPath = "/usr/local/CyberCP/bin/python /usr/local/CyberCP/databases/databaseManager.py"
199+
# execPath = execPath + " generatePHPMYAdminData --userID " + str(userID)
200+
#
201+
# output = ProcessUtilities.outputExecutioner(execPath)
202+
#
203+
# if output.find("1,") > -1:
204+
# request.session['PMA_single_signon_user'] = admin.userName
205+
# request.session['PMA_single_signon_password'] = output.split(',')[1]
206+
# data_ret = {'status': 1}
207+
# json_data = json.dumps(data_ret)
208+
# return HttpResponse(json_data)
209+
# else:
210+
211+
data_ret = {'status': 1}
212+
json_data = json.dumps(data_ret)
213+
return HttpResponse(json_data)
174214

175215

176216
except BaseException as msg:

plogical/acl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ def findAllSites(currentACL, userID, fetchChilds = 0):
445445

446446
@staticmethod
447447
def searchWebsiteObjects(currentACL, userID, searchTerm):
448+
448449
if currentACL['admin'] == 1:
449450
return Websites.objects.filter(domain__istartswith=searchTerm)
450451
else:

plogical/mysqlUtilities.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,29 @@ def enableRemoteMYSQL():
902902
print('0,%s "[mysqlUtilities.enableRemoteMYSQL]' % (str(msg)))
903903
return 0
904904

905+
@staticmethod
906+
def addUserToDB(database, user, password, createUser = 0):
907+
try:
908+
909+
connection, cursor = mysqlUtilities.setupConnection()
910+
911+
if connection == 0:
912+
return 0
913+
914+
if createUser:
915+
cursor.execute(
916+
"CREATE USER '" + user + "'@'%s' IDENTIFIED BY '" % (mysqlUtilities.LOCALHOST) + password + "'")
917+
918+
cursor.execute(
919+
"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'%s'" % (mysqlUtilities.LOCALHOST))
920+
connection.close()
921+
922+
return 1
923+
924+
except BaseException as msg:
925+
logging.CyberCPLogFileWriter.writeToFile(str(msg) + "[addUserToDB]")
926+
return 0
927+
905928
def main():
906929
parser = argparse.ArgumentParser(description='CyberPanel')
907930
parser.add_argument('function', help='Specific a function to call!')

0 commit comments

Comments
 (0)