make signup script a template

This commit is contained in:
2023-01-21 22:19:17 +05:30
parent 79d0cc3771
commit a85dba1c41
4 changed files with 30 additions and 27 deletions

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)