2023-01-07 21:03:51 +05:30
package pages
import (
"github.com/gofiber/fiber/v2"
2023-01-17 20:36:47 +05:30
"os"
2023-01-07 21:03:51 +05:30
"github.com/containrrr/shoutrrr"
2023-01-07 22:14:25 +05:30
2023-01-07 22:24:04 +05:30
log "github.com/sirupsen/logrus"
2023-01-07 21:03:51 +05:30
)
// SignupPage is the signup page handler
func SignupPage ( c * fiber . Ctx ) error {
2023-01-07 22:14:25 +05:30
2023-01-07 21:03:51 +05:30
username := c . FormValue ( "username" )
email := c . FormValue ( "email" )
2023-01-17 20:36:47 +05:30
ssh := c . FormValue ( "ssh" )
2023-01-21 21:54:33 +05:30
ip := c . FormValue ( "ip" )
if username == "" || email == "" || ssh == "" || ip == "" {
log . Error ( "username, email, ssh and ip must be filled" , username , email , ssh , ip )
2023-01-17 20:36:47 +05:30
return c . SendStatus ( fiber . StatusBadRequest )
}
2023-01-07 21:03:51 +05:30
// create user file
f , err := os . Create ( "/var/publapi/users/" + username + ".sh" )
if err != nil {
2023-01-07 22:24:04 +05:30
log . Error ( "Error creating user file" , err )
2023-01-07 21:03:51 +05:30
return c . SendStatus ( fiber . StatusInternalServerError )
}
defer f . Close ( )
bashscript := "#!/bin/bash \n" +
2023-01-07 23:08:21 +05:30
"# Path: /var/publapi/users/" + username + ".sh\n" +
2023-01-07 21:03:51 +05:30
"# This file is generated by publapi. Do not edit this file.\n" +
2023-01-17 20:36:47 +05:30
"echo \"email of " + username + " is " + email + "\"\n" +
2023-01-08 15:55:26 +05:30
"pass=\"$(tr -dc A-Za-z0-9 </dev/urandom | head -c 64)\"\n" +
2023-01-07 21:12:28 +05:30
"useradd -Um -s /bin/bash " + username + "\n" +
2023-01-21 14:21:15 +05:30
"chmod 711 /home/" + username + "\n" +
2023-01-08 15:48:58 +05:30
"printf \"%s\\n%s\" \"${pass}\" \"${pass}\" | passwd " + username + "\n" +
2023-01-17 20:36:47 +05:30
"mkdir /home/" + username + "/.ssh\n" +
2023-01-17 20:39:46 +05:30
"echo '" + ssh + "' > /home/" + username + "/.ssh/authorized_keys\n" +
2023-01-17 20:36:47 +05:30
"chmod 700 /home/" + username + "/.ssh\n" +
"chmod 600 /home/" + username + "/.ssh/authorized_keys\n" +
"chown -R " + username + ":" + username + " /home/" + username + "/.ssh\n" +
2023-01-08 15:55:26 +05:30
"echo \"${pass}\" > /home/" + username + "/pass\n" +
"chmod 600 /home/" + username + "/pass\n" +
"chown " + username + ":" + username + " /home/" + username + "/pass\n" +
2023-01-21 18:59:56 +05:30
"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" +
2023-01-21 14:26:35 +05:30
"loginctl enable-linger " + username + "\n" +
2023-01-20 12:41:06 +05:30
"setquota -u " + username + " 20G 20G 0 0 /\n" +
2023-01-21 14:29:41 +05:30
"echo \"" + username + "'s account has been created!\"\n" +
2023-01-21 14:26:07 +05:30
"rm -rf $0"
2023-01-07 21:03:51 +05:30
2023-01-21 14:26:07 +05:30
chmoderr := os . Chmod ( "/var/publapi/users/" + username + ".sh" , 0700 )
if chmoderr != nil {
log . Error ( err )
}
2023-01-07 21:03:51 +05:30
// write to file
_ , err = f . WriteString ( bashscript )
if err != nil {
2023-01-07 22:24:04 +05:30
log . Error ( "Error writing to user file" , err )
2023-01-07 21:03:51 +05:30
return c . SendStatus ( fiber . StatusInternalServerError )
}
2023-01-17 20:36:47 +05:30
log . Info ( "Registration request for " + username + " has been submitted by the frontend and has been written to /var/publapi/users/" + username + ".sh" )
2023-01-07 21:32:06 +05:30
// send notification to admins
2023-01-21 21:54:33 +05:30
err = shoutrrr . Send ( os . Getenv ( "PUBLAPI_SHOUTRRRURL" ) , "New user signup! Please review /var/publapi/users/" + username + ".sh to approve or deny the user. IP: " + ip + " Email: " + email )
2023-01-07 21:32:06 +05:30
if err != nil {
2023-01-07 22:24:04 +05:30
log . Error ( "Error sending notification to admins" , err )
2023-01-07 21:32:06 +05:30
return c . SendStatus ( fiber . StatusInternalServerError )
}
2023-01-07 21:03:51 +05:30
return c . JSON ( fiber . Map {
"username" : username ,
"message" : "User created! Please allow us 24 hours or more to review your account." ,
"status" : c . Response ( ) . StatusCode ( ) ,
} )
}