2021-01-01 17:28:34 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-01-05 11:43:24 +00:00
|
|
|
from flask import Flask, jsonify, request, json
|
2020-12-28 11:55:33 +00:00
|
|
|
from flask_restful import Resource, Api, reqparse
|
2021-01-04 14:49:01 +00:00
|
|
|
import base64
|
2020-12-28 11:55:33 +00:00
|
|
|
import pandas as pd
|
|
|
|
import ast
|
|
|
|
import subprocess
|
|
|
|
import os
|
2021-05-29 19:35:40 +00:00
|
|
|
import fileinput
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
app = Flask(__name__)
|
|
|
|
api = Api(app)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/systemVersion", methods=["GET"])
|
|
|
|
def uname():
|
|
|
|
uname = subprocess.check_output(["uname", "-arm"])
|
|
|
|
return jsonify(uname)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/getDKIM", methods=["GET"])
|
|
|
|
def getDkimKey():
|
2021-01-04 14:00:44 +00:00
|
|
|
with open("/var/domain") as domainFile:
|
2021-01-01 18:38:10 +00:00
|
|
|
domain = domainFile.readline()
|
2021-01-01 18:42:03 +00:00
|
|
|
domain = domain.rstrip("\n")
|
2021-01-04 14:40:32 +00:00
|
|
|
catProcess = subprocess.Popen(["cat", "/var/dkim/" + domain + ".selector.txt"], stdout=subprocess.PIPE)
|
|
|
|
dkim = catProcess.communicate()[0]
|
2021-01-05 11:27:00 +00:00
|
|
|
dkim = base64.b64encode(dkim)
|
2021-01-05 11:35:33 +00:00
|
|
|
dkim = str(dkim, 'utf-8')
|
2021-01-05 11:31:22 +00:00
|
|
|
print(dkim)
|
2021-01-05 11:43:24 +00:00
|
|
|
response = app.response_class(
|
|
|
|
response=json.dumps(dkim),
|
|
|
|
status=200,
|
|
|
|
mimetype='application/json'
|
|
|
|
)
|
|
|
|
return response
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/pythonVersion", methods=["GET"])
|
|
|
|
def getPythonVersion():
|
|
|
|
pythonVersion = subprocess.check_output(["python","--version"])
|
|
|
|
return jsonify(pythonVersion)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/apply", methods=["GET"])
|
|
|
|
def rebuildSystem():
|
|
|
|
rebuildResult = subprocess.Popen(["nixos-rebuild","switch"])
|
|
|
|
rebuildResult.communicate()[0]
|
2021-07-07 16:01:32 +00:00
|
|
|
return jsonify(
|
|
|
|
status=rebuildResult.returncode
|
|
|
|
)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/rollback", methods=["GET"])
|
|
|
|
def rollbackSystem():
|
|
|
|
rollbackResult = subprocess.Popen(["nixos-rebuild","switch","--rollback"])
|
|
|
|
rollbackResult.communicate()[0]
|
|
|
|
return jsonify(rollbackResult.returncode)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/upgrade", methods=["GET"])
|
|
|
|
def upgradeSystem():
|
|
|
|
upgradeResult = subprocess.Popen(["nixos-rebuild","switch","--upgrade"])
|
|
|
|
upgradeResult.communicate()[0]
|
2021-07-07 15:57:48 +00:00
|
|
|
return jsonify(
|
|
|
|
status=upgradeResult.returncode
|
|
|
|
)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2021-05-29 19:35:40 +00:00
|
|
|
@app.route("/createUser", methods=["POST"])
|
2020-12-28 11:55:33 +00:00
|
|
|
def createUser():
|
2021-07-02 15:25:56 +00:00
|
|
|
print("[INFO] Opening /etc/nixos/users.nix...", sep="")
|
2021-06-23 17:45:25 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/users.nix", "r")
|
2021-07-02 15:25:56 +00:00
|
|
|
print("done")
|
2021-05-29 19:35:40 +00:00
|
|
|
fileContent = list()
|
|
|
|
index = int(0)
|
2021-07-02 15:25:56 +00:00
|
|
|
|
|
|
|
print("[INFO] Reading file content...", sep="")
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2021-05-29 19:35:40 +00:00
|
|
|
while True:
|
|
|
|
line = readOnlyFileDescriptor.readline()
|
|
|
|
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
fileContent.append(line)
|
2021-07-02 15:25:56 +00:00
|
|
|
print("[DEBUG] Read line!")
|
2021-05-29 19:35:40 +00:00
|
|
|
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2021-05-29 19:35:40 +00:00
|
|
|
userTemplate = """
|
2021-07-02 15:39:24 +00:00
|
|
|
|
2021-06-21 07:17:02 +00:00
|
|
|
#begin
|
2021-05-29 19:35:40 +00:00
|
|
|
\"{0}\" = {{
|
|
|
|
isNormalUser = true;
|
|
|
|
hashedPassword = \"{1}\";
|
2021-06-21 07:17:02 +00:00
|
|
|
}};
|
2021-07-02 15:39:24 +00:00
|
|
|
#end
|
|
|
|
""".format(request.headers.get("X-User"), request.headers.get("X-Password"))
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2021-05-29 19:35:40 +00:00
|
|
|
for line in fileContent:
|
2021-07-02 15:45:57 +00:00
|
|
|
index += 1
|
2021-06-21 07:17:02 +00:00
|
|
|
if line.startswith(" #begin"):
|
2021-07-02 15:25:56 +00:00
|
|
|
print("[DEBUG] Found user configuration snippet match!")
|
|
|
|
print("[INFO] Writing new user configuration snippet to memory...", sep="")
|
2021-07-02 15:45:57 +00:00
|
|
|
fileContent.insert(index-1, userTemplate)
|
2021-07-02 15:25:56 +00:00
|
|
|
print("done")
|
2021-07-02 15:55:22 +00:00
|
|
|
break
|
2021-05-29 19:35:40 +00:00
|
|
|
|
2021-07-02 15:25:56 +00:00
|
|
|
print("[INFO] Writing data from memory to file...", sep="")
|
2021-06-30 15:19:37 +00:00
|
|
|
readWriteFileDescriptor = open("/etc/nixos/users.nix", "w")
|
2021-07-02 15:25:56 +00:00
|
|
|
print("done")
|
2021-05-29 19:35:40 +00:00
|
|
|
operationResult = readWriteFileDescriptor.writelines(fileContent)
|
|
|
|
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2021-05-29 19:35:40 +00:00
|
|
|
return jsonify(
|
2021-06-21 07:17:02 +00:00
|
|
|
result=0,
|
|
|
|
descriptor = operationResult
|
2021-05-29 19:35:40 +00:00
|
|
|
)
|
2021-05-26 16:35:20 +00:00
|
|
|
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/deleteUser", methods=["DELETE"])
|
|
|
|
def deleteUser():
|
|
|
|
user = subprocess.Popen(["userdel",request.headers.get("X-User")])
|
|
|
|
user.communicate()[0]
|
|
|
|
return jsonify(user.returncode)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
@app.route("/serviceStatus", methods=["GET"])
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2020-12-28 11:55:33 +00:00
|
|
|
def getServiceStatus():
|
|
|
|
imapService = subprocess.Popen(["systemctl", "status", "dovecot2.service"])
|
|
|
|
imapService.communicate()[0]
|
|
|
|
smtpService = subprocess.Popen(["systemctl", "status", "postfix.service"])
|
|
|
|
smtpService.communicate()[0]
|
|
|
|
httpService = subprocess.Popen(["systemctl", "status", "nginx.service"])
|
|
|
|
httpService.communicate()[0]
|
|
|
|
return jsonify(
|
|
|
|
imap=imapService.returncode,
|
|
|
|
smtp=smtpService.returncode,
|
|
|
|
http=httpService.returncode
|
|
|
|
)
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
|
2021-04-13 13:54:30 +00:00
|
|
|
@app.route("/decryptDisk", methods=["POST"])
|
|
|
|
def requestDiskDecryption():
|
2021-06-21 07:17:02 +00:00
|
|
|
|
|
|
|
decryptionCommand = '''
|
|
|
|
echo -n {0} | cryptsetup luksOpen /dev/sdb decryptedVar'''.format(request.headers.get("X-Decryption-Key"))
|
|
|
|
|
|
|
|
decryptionService = subprocess.Popen(decryptionCommand, shell=True, stdout=subprocess.PIPE)
|
|
|
|
decryptionService.communicate()
|
2021-04-13 13:54:30 +00:00
|
|
|
return jsonify(
|
|
|
|
status=decryptionService.returncode
|
|
|
|
)
|
2021-05-26 16:35:20 +00:00
|
|
|
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
@app.route("/services/ssh/enable", methods=["POST"])
|
2021-06-21 07:17:02 +00:00
|
|
|
|
2021-05-26 16:35:20 +00:00
|
|
|
def enableSSH():
|
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/configuration.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/configuration.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("services.openssh.enable = false;", "services.openssh.enable = true;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/bitwarden/enable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
|
|
|
def enableBitwarden():
|
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/passmgr/bitwarden.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/passmgr/bitwarden.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = false;", "enable = true;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
|
|
|
# Bitwarden
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/bitwarden/disable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
|
|
|
def disableBitwarden():
|
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/passmgr/bitwarden.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/passmgr/bitwarden.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = true;", "enable = false;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
|
|
|
#Gitea
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/gitea/disable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def disableGitea():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/git/gitea.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/git/gitea.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = true;", "enable = false;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/gitea/enable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def enableGitea():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/git/gitea.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/git/gitea.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = false;", "enable = true;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
|
|
|
#Nextcloud
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/nextcloud/disable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def disableNextcloud():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/nextcloud/nextcloud.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/nextcloud/nextcloud.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = true;", "enable = false;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/nextcloud/enable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def enableNextcloud():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/nextcloud/nextcloud.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/nextcloud/nextcloud.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = false;", "enable = true;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
|
|
|
#Pleroma
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/pleroma/disable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def disablePleroma():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/social/pleroma.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/social/pleroma.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = true;", "enable = false;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/pleroma/enable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def enablePleroma():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/social/pleroma.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/social/pleroma.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = false;", "enable = true;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
|
|
|
#Ocserv
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/ocserv/disable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def disableOcserv():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/vpn/ocserv.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/vpn/ocserv.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = true;", "enable = false;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/ocserv/enable", methods=["POST"])
|
2021-08-20 14:59:12 +00:00
|
|
|
|
2021-08-23 13:34:29 +00:00
|
|
|
def enableOcserv():
|
2021-08-20 14:59:12 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/vpn/ocserv.nix", "rt")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/vpn/ocserv.nix", "wt")
|
|
|
|
|
|
|
|
for line in readOnlyFileDescriptor:
|
|
|
|
readWriteFileDescriptor.write(line.replace("enable = false;", "enable = true;"))
|
|
|
|
|
|
|
|
readWriteFileDescriptor.close()
|
|
|
|
readOnlyFileDescriptor.close()
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
status=0
|
|
|
|
)
|
2021-05-26 16:35:20 +00:00
|
|
|
|
2021-08-25 04:44:39 +00:00
|
|
|
@app.route("/services/ssh/key/send", methods=["PUT"])
|
2021-08-23 13:34:29 +00:00
|
|
|
|
|
|
|
def readKey():
|
|
|
|
|
|
|
|
requestBody = request.get_json()
|
|
|
|
|
|
|
|
publicKey = requestBody.data(["public_key"])
|
|
|
|
|
|
|
|
|
|
|
|
print("[INFO] Opening /etc/nixos/configuration.nix...", sep="")
|
2021-08-25 04:51:24 +00:00
|
|
|
readOnlyFileDescriptor = open("/etc/nixos/configuration.nix", "r")
|
2021-08-23 13:34:29 +00:00
|
|
|
print("done")
|
|
|
|
fileContent = list()
|
|
|
|
index = int(0)
|
|
|
|
|
|
|
|
print("[INFO] Reading file content...", sep="")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
line = readOnlyFileDescriptor.readline()
|
|
|
|
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
fileContent.append(line)
|
|
|
|
print("[DEBUG] Read line!")
|
|
|
|
|
|
|
|
for line in fileContent:
|
|
|
|
index += 1
|
|
|
|
if "openssh.authorizedKeys.keys = [" in line:
|
|
|
|
print("[DEBUG] Found SSH key configuration snippet match!")
|
|
|
|
print("[INFO] Writing new SSH key", sep="")
|
2021-08-25 05:07:02 +00:00
|
|
|
fileContent.append(index, "\n \"" + publicKey + "\"")
|
2021-08-23 13:34:29 +00:00
|
|
|
print("done")
|
|
|
|
break
|
|
|
|
|
|
|
|
print("[INFO] Writing data from memory to file...", sep="")
|
|
|
|
readWriteFileDescriptor = open("/etc/nixos/configuration.nix", "w")
|
|
|
|
print("done")
|
|
|
|
operationResult = readWriteFileDescriptor.writelines(fileContent)
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
result=0,
|
|
|
|
descriptor = operationResult
|
|
|
|
)
|
|
|
|
|
2021-06-21 07:17:02 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(port=5050, debug=False)
|