Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d8ae173b authored by Felix Ableitner's avatar Felix Ableitner
Browse files

Initial version

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Original line number Diff line number Diff line
.idea/

Dockerfile

0 → 100644
+9 −0
Original line number Diff line number Diff line
FROM alpine:3.9

COPY bin/main.py /usr/share/
RUN chmod 555 /usr/share/main.py
RUN apk add --no-cache python3
USER 9000
RUN ls -la /usr/share/main.py

ENTRYPOINT ["python3", "/usr/share/main.py"]

bin/main.py

0 → 100644
+81 −0
Original line number Diff line number Diff line
#!/usr/bin/python3

import time
from http.server import BaseHTTPRequestHandler, HTTPServer
import urllib.parse
import paramiko
import requests
from requests.auth import HTTPBasicAuth
import os

HOST_NAME = '0.0.0.0'
PORT_NUMBER = 9000


class MyHandler(BaseHTTPRequestHandler):
    def do_PUT(self):
        if self.path == '/create-account':
            length = int(self.headers['Content-Length'])
            postvars = urllib.parse.parse_qs(self.rfile.read(length))
            postvars = {k.decode(): v[0].decode() for k, v in postvars.items()}

            self.respond(100, 'test')
            self.create_account(postvars['target_email'], postvars['password'], postvars['password_confirm'],
                                postvars['displayname'], postvars['email_quota'], postvars['fallback_email'],
                                postvars['nextcloud_quota'])

    def create_account(self,
                       target_email: str, # was $resultmail
                       password: str, # was $pw
                       password_confirm: str, # was $pw2
                       displayname: str, # was $name
                       email_quota: str, # was $quota
                       fallback_email: str, # new parameter
                       nextcloud_quota: str # new parameter
                       ):
        # create account via postfixadmin ssh
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy)
        ssh.connect(hostname='postfixadmin', username='pfexec', password=os.environ['POSTFIXADMIN_SSH_KEY'])
        ftp = ssh.open_sftp()
        temp_file_name = '/tmp/' + target_email.split('@')[0] + '-details'
        temp_file = ftp.file(temp_file_name, 'w')
        temp_file.writeln(target_email)
        temp_file.writeln(password)
        temp_file.writeln(password_confirm)
        temp_file.writeln(displayname)
        temp_file.writeln(email_quota)
        temp_file.flush()
        ftp.close()
        stdin, stdout, stderr = ssh.exec_command('cat ' + temp_file_name + ' | /usr/local/bin/wrapper.sh')
        print(stdout.channel.recv_exit_status())
        # TODO: check exit code, handle errors
        ssh.exec_command('rm ' + temp_file_name)

        # Edit nextcloud account, set quota and email
        auth = HTTPBasicAuth(os.environ['NEXTCLOUD_ADMIN_USER'], os.environ['NEXTCLOUD_ADMIN_PASSWORD'])
        url = 'http://localhost/ocs/v1.php/cloud/users/' + target_email
        headers = {'OCS-APIRequest': 'true'}
        r = requests.put(url, data={'key': 'email', 'value': fallback_email}, headers=headers, auth=auth)
        # TODO: check status code
        r = requests.put(url, data={'key': 'quota', 'value': nextcloud_quota}, headers=headers, auth=auth)
        # TODO: check status code

        self.respond(200, "account created")

    def respond(self, status: int, message: str):
            self.send_response(status)
            self.end_headers()
            self.wfile.write(message.encode())


if __name__ == '__main__':
    server_class = HTTPServer
    httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
    print(time.asctime(), 'Server Starts - %s:%s' % (HOST_NAME, PORT_NUMBER))
    try:
        httpd.serve_forever()
    except (SystemExit, KeyboardInterrupt):
        pass
    httpd.server_close()
    print(time.asctime(), 'Server Stops - %s:%s' % (HOST_NAME, PORT_NUMBER))