From a85dba1c41b08e2de9df1c8ee2398919168c358b Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Sat, 21 Jan 2023 22:19:17 +0530 Subject: [PATCH] make signup script a template --- main.go | 3 +-- pages/signup.go | 31 ++++++------------------------- utils/main.go | 4 ++++ utils/signup-script-template | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 utils/signup-script-template diff --git a/main.go b/main.go index 492d122..73fab6b 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,9 @@ package main import ( - "os" - "github.com/ProjectSegfault/publapi/pages" "github.com/gofiber/fiber/v2" + "os" ) // publapi is a simple API for Project Segfault's public shared server (pubnix). diff --git a/pages/signup.go b/pages/signup.go index c242317..705cd10 100644 --- a/pages/signup.go +++ b/pages/signup.go @@ -1,12 +1,13 @@ package pages import ( + "github.com/ProjectSegfault/publapi/utils" "github.com/gofiber/fiber/v2" "os" "github.com/containrrr/shoutrrr" - log "github.com/sirupsen/logrus" + "strings" ) // SignupPage is the signup page handler @@ -28,35 +29,15 @@ func SignupPage(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusInternalServerError) } defer f.Close() - bashscript := "#!/bin/bash \n" + - "# Path: /var/publapi/users/" + username + ".sh\n" + - "# This file is generated by publapi. Do not edit this file.\n" + - "echo \"email of " + username + " is " + email + "\"\n" + - "pass=\"$(tr -dc A-Za-z0-9 /home/" + username + "/.ssh/authorized_keys\n" + - "chmod 700 /home/" + username + "/.ssh\n" + - "chmod 600 /home/" + username + "/.ssh/authorized_keys\n" + - "chown -R " + username + ":" + username + " /home/" + username + "/.ssh\n" + - "echo \"${pass}\" > /home/" + username + "/pass\n" + - "chmod 600 /home/" + username + "/pass\n" + - "chown " + username + ":" + username + " /home/" + username + "/pass\n" + - "sed -i 's/REPLACEME/" + username + "/g' /home/" + username + "/{meta-info.env,Caddyfile}\n" + - "sed -i 's/EMAIL=/EMAIL=" + email + "/' /home/" + username + "/meta-info.env\n" + - "loginctl enable-linger " + username + "\n" + - "setquota -u " + username + " 20G 20G 0 0 /\n" + - "echo \"" + username + "'s account has been created!\"\n" + - "rm -rf $0" - chmoderr := os.Chmod("/var/publapi/users/"+username+".sh", 0700) if chmoderr != nil { log.Error(err) } + utils.Bashscript = strings.ReplaceAll(utils.Bashscript, "{{sshkey}}", ssh) + utils.Bashscript = strings.ReplaceAll(utils.Bashscript, "{{email}}", email) + utils.Bashscript = strings.ReplaceAll(utils.Bashscript, "{{username}}", username) // write to file - _, err = f.WriteString(bashscript) + _, err = f.WriteString(utils.Bashscript) if err != nil { log.Error("Error writing to user file", err) return c.SendStatus(fiber.StatusInternalServerError) diff --git a/utils/main.go b/utils/main.go index 3fc48d2..b6c9a55 100644 --- a/utils/main.go +++ b/utils/main.go @@ -1,9 +1,13 @@ package utils import ( + _ "embed" "strings" ) +//go:embed templates/signup-script-template +var Bashscript string + func Dedup(input string) string { unique := []string{} words := strings.Split(input, " ") diff --git a/utils/signup-script-template b/utils/signup-script-template new file mode 100644 index 0000000..2bd691c --- /dev/null +++ b/utils/signup-script-template @@ -0,0 +1,19 @@ +#!/bin/bash +# Path: /var/publapi/users/{{username}}.sh +# This file is generated by PublAPI. Do not edit this file. +echo "E-Mail of {{username}} is {{email}}." +useradd -Um -s /bin/bash {{username}} +chmod 711 /home/{{username}} +printf "%s\n%s" "${pass}" "${pass}" | passwd {{username}} +mkdir /home/{{username}}/.ssh +echo "{{sshkey}}" > /home/{{username}}/.ssh/authorized_keys +chmod 700 /home/{{username}}/.ssh +chmod 600 /home/{{username}}/.ssh/authorized_keys +chown -R {{username}}:username /home/username/.ssh +echo "${pass}" > /home/{{username}}/pass +chmod 600 /home/{{username}}/pass +chown {{username}}:username /home/username/pass +sed -i 's/REPLACEME/{{username}}/g' /home/username/{meta-info.env,Caddyfile} +sed -i 's/EMAIL=/EMAIL={{email}}/' /home/{{username}}/meta-info.env +loginctl enable-linger {{username}} +setquota -u {{username}} 20G 20G 0 0 /