Loading app/main.py +17 −18 Original line number Diff line number Diff line Loading @@ -45,27 +45,26 @@ class MyHandler(BaseHTTPRequestHandler): self.respond(403, 'this username is not allowed') return # create account via postfixadmin ssh ssh = paramiko.SSHClient() with paramiko.SSHClient() as ssh: ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy) ssh.connect(hostname='postfixadmin', username='pfexec', password=os.environ['POSTFIXADMIN_SSH_PASSWORD']) ftp = ssh.open_sftp() temp_file_name = '/tmp/' + target_username + target_email.split('@')[0] + '-details' temp_file = ftp.file(temp_file_name, 'w') temp_file.write(f'{target_email}\n{password}\n{password_confirm}\n{displayname}\n{email_quota}') temp_file.flush() temp_file.close() ftp.close() stdin, stdout, stderr = ssh.exec_command('cat ' + temp_file_name + ' | /usr/local/bin/wrapper.sh') stdin, stdout, stderr = ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox view "{target_email}"') if "error: the email is not valid!" in stderr.read().lower(): self.respond(403, 'this username is already used') return ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox add "{target_email}" ' + f'--password "{password}" --password2 {password_confirm} --name "{displayname}" ' + f'--quota {email_quota} --active 1 --welcome-mail 0') print(stdout.read()) print(stderr.read()) print(f'exit code: {stdout.channel.recv_exit_status()}') # TODO: check exit code, handle errors # TODO: commented for now to check the file #ssh.exec_command('rm ' + temp_file_name) # TODO: handle errors # Edit nextcloud account, set quota and email auth = HTTPBasicAuth(os.environ['NEXTCLOUD_ADMIN_USER'], os.environ['NEXTCLOUD_ADMIN_PASSWORD']) url = 'http://nextcloud/ocs/v1.php/cloud/users/' + target_email url = f'https://drive.{os.environ["DOMAIN"]}/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) print(r.text) Loading Loading
app/main.py +17 −18 Original line number Diff line number Diff line Loading @@ -45,27 +45,26 @@ class MyHandler(BaseHTTPRequestHandler): self.respond(403, 'this username is not allowed') return # create account via postfixadmin ssh ssh = paramiko.SSHClient() with paramiko.SSHClient() as ssh: ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy) ssh.connect(hostname='postfixadmin', username='pfexec', password=os.environ['POSTFIXADMIN_SSH_PASSWORD']) ftp = ssh.open_sftp() temp_file_name = '/tmp/' + target_username + target_email.split('@')[0] + '-details' temp_file = ftp.file(temp_file_name, 'w') temp_file.write(f'{target_email}\n{password}\n{password_confirm}\n{displayname}\n{email_quota}') temp_file.flush() temp_file.close() ftp.close() stdin, stdout, stderr = ssh.exec_command('cat ' + temp_file_name + ' | /usr/local/bin/wrapper.sh') stdin, stdout, stderr = ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox view "{target_email}"') if "error: the email is not valid!" in stderr.read().lower(): self.respond(403, 'this username is already used') return ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox add "{target_email}" ' + f'--password "{password}" --password2 {password_confirm} --name "{displayname}" ' + f'--quota {email_quota} --active 1 --welcome-mail 0') print(stdout.read()) print(stderr.read()) print(f'exit code: {stdout.channel.recv_exit_status()}') # TODO: check exit code, handle errors # TODO: commented for now to check the file #ssh.exec_command('rm ' + temp_file_name) # TODO: handle errors # Edit nextcloud account, set quota and email auth = HTTPBasicAuth(os.environ['NEXTCLOUD_ADMIN_USER'], os.environ['NEXTCLOUD_ADMIN_PASSWORD']) url = 'http://nextcloud/ocs/v1.php/cloud/users/' + target_email url = f'https://drive.{os.environ["DOMAIN"]}/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) print(r.text) Loading