make signup script a template

This commit is contained in:
Arya 2023-01-21 22:19:17 +05:30
parent 79d0cc3771
commit a85dba1c41
Signed by: arya
GPG Key ID: 842D12BDA50DF120
4 changed files with 30 additions and 27 deletions

View File

@ -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).

View File

@ -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 </dev/urandom | head -c 64)\"\n" +
"useradd -Um -s /bin/bash " + username + "\n" +
"chmod 711 /home/" + username + "\n" +
"printf \"%s\\n%s\" \"${pass}\" \"${pass}\" | passwd " + username + "\n" +
"mkdir /home/" + username + "/.ssh\n" +
"echo '" + ssh + "' > /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)

View File

@ -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, " ")

View File

@ -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 /