gocage/cmd/console.go

40 lines
803 B
Go
Raw Normal View History

2022-04-05 20:58:33 +02:00
package cmd
import (
2022-06-18 11:08:03 +02:00
"os"
2022-04-05 20:58:33 +02:00
"fmt"
2022-06-18 11:08:03 +02:00
"log"
"strconv"
2022-04-05 20:58:33 +02:00
"strings"
2022-06-18 11:08:03 +02:00
"syscall"
2022-04-05 20:58:33 +02:00
)
func ShellJail(args []string) error {
2022-06-18 11:08:03 +02:00
// We cant shell more than one jail bc we replace gocage execution with jexec, so there wont be no return to gocage
2022-04-05 20:58:33 +02:00
if len(args) > 0 {
2022-06-18 11:08:03 +02:00
for _, cj := range gJails {
if strings.EqualFold(cj.Name, args[0]) {
shellJail(cj)
2022-04-05 20:58:33 +02:00
}
}
}
2022-04-24 16:49:54 +02:00
2022-06-18 11:08:03 +02:00
fmt.Printf("Jail not found: %s\n", args[0])
2022-04-05 20:58:33 +02:00
return nil
}
2022-06-18 11:08:03 +02:00
func shellJail(jail Jail) {
jid := strconv.Itoa(jail.JID)
err := syscall.Exec("/usr/sbin/jexec", []string{"jexec", jid, "/bin/csh"}, os.Environ())
// We should never get here, as syscall.Exec replace the gocage binary execution with jexec
// This means the moment syscall.Exec fires, gocage execution halt.
2022-04-05 20:58:33 +02:00
if err != nil {
2022-06-18 11:08:03 +02:00
log.Printf("Exec returned %v\n", err)
2022-04-05 20:58:33 +02:00
}
2022-04-24 16:49:54 +02:00
2022-06-18 11:08:03 +02:00
return
2022-04-05 20:58:33 +02:00
}