Loading README.md +32 −13 Original line number Diff line number Diff line Utility for creating a new user account in postfixadmin, and setting account values via Nextcloud. ## Running ``` create-account: image: docker-create-account:0.1.0 container_name: create-account environment: - NEXTCLOUD_ADMIN_USER=${NEXTCLOUD_ADMIN_USER} - NEXTCLOUD_ADMIN_PASSWORD=${NEXTCLOUD_ADMIN_PASSWORD} - POSTFIXADMIN_SSH_PASSWORD=${POSTFIXADMIN_SSH_PASSWORD} - DOMAIN=${DOMAIN} - CREATE_ACCOUNT_PASSWORD=${CREATE_ACCOUNT_PASSWORD} networks: - serverbase depends_on: - nextcloud - postfixadmin ``` ## Usage Send a `PUT` request to `example.com:9000/create-account`. Required parameters: ``` target_email password password_confirm displayname email_quota fallback_email nextcloud_quota auth # must be the same as the CREATE_ACCOUNT_PASSWORD env var (for client authorization) target_email # email to create (including domain) password # user password password_confirm # user password confirmation displayname # user displayname email_quota # email storage quota in postfix fallback_email # email for password reset in Nextcloud nextcloud_quota # storage quota in Nextcloud ``` You can use the following command to make a request: ``` curl -X PUT https://example.com:9000/create-account \ -d target_email=user@example.com -d password=pw -d password_confirm=pw \ -d displayname=name -d email_quota=quota -d fallback_email=user@otherdomain.com \ -d nextcloud_quota=-1 -d auth=$CREATE_ACCOUNT_PASSWORD -d target_email=user@example.com -d \ password=pw -d password_confirm=pw -d displayname=name -d email_quota=quota \ -d fallback_email=user@otherdomain.com -d nextcloud_quota=-1 ``` ## Response values: Loading @@ -31,7 +49,8 @@ On success, status code will be 200 and response body: On failure, status code will be 4xx or 5xx and response body one of the following: ``` '{"success": false, "message": "username_forbidden"}' '{"success": false, "message": "username_taken"}' '{"success": false, "message": "internal_error"}' {"success": false, "message": "username_forbidden"} # this username is not allowed {"success": false, "message": "username_taken"} # this username is already taken {"success": false, "message": "internal_error"} # failed to connect to postfixadmin or Nextcloud {"success": false, "message": "no_auth"} # auth missing or wrong ``` app/main.py +23 −15 Original line number Diff line number Diff line Loading @@ -24,11 +24,13 @@ PORT_NUMBER = 9000 class MyHandler(BaseHTTPRequestHandler): def do_PUT(self): if self.path == '/create-account' or self.path == '/create-account/': # TODO: require a password 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()} print(f'Received request PUT /create-account, parameters {postvars}') if postvars['auth'] != os.environ['CREATE_ACCOUNT_PASSWORD']: self.respond(401, "no_auth") return self.create_account(postvars['target_email'], postvars['password'], postvars['password_confirm'], postvars['displayname'], postvars['email_quota'], postvars['fallback_email'], Loading @@ -44,6 +46,7 @@ class MyHandler(BaseHTTPRequestHandler): return # create account via postfixadmin ssh with paramiko.SSHClient() as ssh: try: ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy) ssh.connect(hostname='postfixadmin', username='pfexec', password=os.environ['POSTFIXADMIN_SSH_PASSWORD']) Loading @@ -60,6 +63,11 @@ class MyHandler(BaseHTTPRequestHandler): f'--name {shlex.quote(displayname)} --quota {shlex.quote(email_quota)} --active 1 --welcome-mail 0') print(stdout.read()) print(stderr.read()) except (ssh.SSHException, ssh.AuthenticationException, ssh.socket.error, ssh.BadHostKeyException) as e: print(e) data = json.dumps({'success': False, 'message': 'internal_error'}) self.respond(500, data) return # Edit nextcloud account, set quota and email auth = HTTPBasicAuth(os.environ['NEXTCLOUD_ADMIN_USER'], os.environ['NEXTCLOUD_ADMIN_PASSWORD']) Loading Loading
README.md +32 −13 Original line number Diff line number Diff line Utility for creating a new user account in postfixadmin, and setting account values via Nextcloud. ## Running ``` create-account: image: docker-create-account:0.1.0 container_name: create-account environment: - NEXTCLOUD_ADMIN_USER=${NEXTCLOUD_ADMIN_USER} - NEXTCLOUD_ADMIN_PASSWORD=${NEXTCLOUD_ADMIN_PASSWORD} - POSTFIXADMIN_SSH_PASSWORD=${POSTFIXADMIN_SSH_PASSWORD} - DOMAIN=${DOMAIN} - CREATE_ACCOUNT_PASSWORD=${CREATE_ACCOUNT_PASSWORD} networks: - serverbase depends_on: - nextcloud - postfixadmin ``` ## Usage Send a `PUT` request to `example.com:9000/create-account`. Required parameters: ``` target_email password password_confirm displayname email_quota fallback_email nextcloud_quota auth # must be the same as the CREATE_ACCOUNT_PASSWORD env var (for client authorization) target_email # email to create (including domain) password # user password password_confirm # user password confirmation displayname # user displayname email_quota # email storage quota in postfix fallback_email # email for password reset in Nextcloud nextcloud_quota # storage quota in Nextcloud ``` You can use the following command to make a request: ``` curl -X PUT https://example.com:9000/create-account \ -d target_email=user@example.com -d password=pw -d password_confirm=pw \ -d displayname=name -d email_quota=quota -d fallback_email=user@otherdomain.com \ -d nextcloud_quota=-1 -d auth=$CREATE_ACCOUNT_PASSWORD -d target_email=user@example.com -d \ password=pw -d password_confirm=pw -d displayname=name -d email_quota=quota \ -d fallback_email=user@otherdomain.com -d nextcloud_quota=-1 ``` ## Response values: Loading @@ -31,7 +49,8 @@ On success, status code will be 200 and response body: On failure, status code will be 4xx or 5xx and response body one of the following: ``` '{"success": false, "message": "username_forbidden"}' '{"success": false, "message": "username_taken"}' '{"success": false, "message": "internal_error"}' {"success": false, "message": "username_forbidden"} # this username is not allowed {"success": false, "message": "username_taken"} # this username is already taken {"success": false, "message": "internal_error"} # failed to connect to postfixadmin or Nextcloud {"success": false, "message": "no_auth"} # auth missing or wrong ```
app/main.py +23 −15 Original line number Diff line number Diff line Loading @@ -24,11 +24,13 @@ PORT_NUMBER = 9000 class MyHandler(BaseHTTPRequestHandler): def do_PUT(self): if self.path == '/create-account' or self.path == '/create-account/': # TODO: require a password 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()} print(f'Received request PUT /create-account, parameters {postvars}') if postvars['auth'] != os.environ['CREATE_ACCOUNT_PASSWORD']: self.respond(401, "no_auth") return self.create_account(postvars['target_email'], postvars['password'], postvars['password_confirm'], postvars['displayname'], postvars['email_quota'], postvars['fallback_email'], Loading @@ -44,6 +46,7 @@ class MyHandler(BaseHTTPRequestHandler): return # create account via postfixadmin ssh with paramiko.SSHClient() as ssh: try: ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy) ssh.connect(hostname='postfixadmin', username='pfexec', password=os.environ['POSTFIXADMIN_SSH_PASSWORD']) Loading @@ -60,6 +63,11 @@ class MyHandler(BaseHTTPRequestHandler): f'--name {shlex.quote(displayname)} --quota {shlex.quote(email_quota)} --active 1 --welcome-mail 0') print(stdout.read()) print(stderr.read()) except (ssh.SSHException, ssh.AuthenticationException, ssh.socket.error, ssh.BadHostKeyException) as e: print(e) data = json.dumps({'success': False, 'message': 'internal_error'}) self.respond(500, data) return # Edit nextcloud account, set quota and email auth = HTTPBasicAuth(os.environ['NEXTCLOUD_ADMIN_USER'], os.environ['NEXTCLOUD_ADMIN_PASSWORD']) Loading