2021-12-18 21:34:43 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-24 16:49:54 +02:00
|
|
|
"os"
|
|
|
|
// "log"
|
2021-12-19 12:42:29 +01:00
|
|
|
"errors"
|
2021-12-18 21:34:43 +01:00
|
|
|
"os/exec"
|
2022-04-24 16:49:54 +02:00
|
|
|
"regexp"
|
|
|
|
// "reflect"
|
2021-12-19 12:42:29 +01:00
|
|
|
"strings"
|
2021-12-18 21:34:43 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO : Use SYS_RCTL_GET_RACCT syscall
|
|
|
|
func removeRctlRules(jail string, rules []string) error {
|
|
|
|
var cmd []string
|
|
|
|
|
|
|
|
if len(rules) == 0 {
|
|
|
|
rules[0] = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range rules {
|
|
|
|
if gUseSudo {
|
|
|
|
cmd = append(cmd, "sudo")
|
|
|
|
}
|
|
|
|
cmd = append(cmd, "/usr/bin/rctl")
|
|
|
|
cmd = append(cmd, "-r")
|
|
|
|
cmd = append(cmd, fmt.Sprintf("jail:%s:%s", jail, r))
|
|
|
|
|
2021-12-19 12:42:29 +01:00
|
|
|
// TODO : Log in another channel than stdout (will scramble display)
|
2021-12-18 21:34:43 +01:00
|
|
|
//log.Println(fmt.Sprintf("Removing all rules for jail %s: %s", jail, cmd))
|
|
|
|
|
|
|
|
//out, err := exec.Command(cmd[0], cmd[1:]...).Output()
|
|
|
|
_, err := exec.Command(cmd[0], cmd[1:]...).Output()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-19 12:42:29 +01:00
|
|
|
// TODO: Validate with >1 dataset
|
|
|
|
func umountAndUnjailZFS(jail *Jail) error {
|
|
|
|
var ds []string
|
|
|
|
|
|
|
|
// Make sure we have a string array
|
|
|
|
ds = append(ds, jail.Config.Jail_zfs_dataset)
|
|
|
|
|
|
|
|
for _, zd := range ds {
|
|
|
|
// 1. Get dataset and childs
|
|
|
|
cmd := fmt.Sprintf("zfs list -H -r -o name -S name %s/%s", jail.Zpool, zd)
|
|
|
|
out, err := executeCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(fmt.Sprintf("ERROR listing dataset %s/%s\n", jail.Zpool, zd))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
for _, c := range strings.Split(out, "\n") {
|
|
|
|
if len(c) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Printf("Unmounting dataset %s: ", c)
|
|
|
|
cmd := fmt.Sprintf("zfs umount %s", c)
|
|
|
|
_, err := executeCommandInJail(jail, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("OK\n")
|
|
|
|
}
|
2021-12-18 21:34:43 +01:00
|
|
|
}
|
2021-12-19 12:42:29 +01:00
|
|
|
|
|
|
|
// 2. Unjail dataset from the host
|
|
|
|
cmd := fmt.Sprintf("zfs unjail %s %s/%s", jail.InternalName, jail.Zpool, ds[len(ds)-1])
|
|
|
|
_, err := executeCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR unjailing %s/%s: %s\n", jail.Zpool, ds[len(ds)-1], err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2022-04-24 16:49:54 +02:00
|
|
|
|
2021-12-19 12:42:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func destroyVNetInterfaces(jail *Jail) error {
|
|
|
|
for _, i := range strings.Split(jail.Config.Ip4_addr, ",") {
|
|
|
|
iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID)
|
|
|
|
fmt.Printf("%s: ", iname)
|
|
|
|
_, err := executeCommand(fmt.Sprintf("ifconfig %s destroy", iname))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
fmt.Printf("OK\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 21:34:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-02 14:17:10 +02:00
|
|
|
// Jails copy the ruleset referenced as "devfs_ruleset" when starting, getting a new devsf_ruleset ID.
|
|
|
|
// This new ID can be obtained with 'jls -j $JID devfs_ruleset'
|
|
|
|
// This is the ID which needs to be removed. Original ID referenced is json should not be deleted
|
|
|
|
// or else it will require a restart of "devfs" service.
|
|
|
|
// But, stoppign the jail already removes this >1000 ID.
|
|
|
|
// So no need to call this function.
|
2021-12-19 12:42:29 +01:00
|
|
|
func deleteDevfsRuleset(jail *Jail) error {
|
|
|
|
cmd := "devfs rule showsets"
|
|
|
|
out, err := executeCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("ERROR listing rulesets: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range strings.Split(out, "\n") {
|
|
|
|
if r == jail.Config.Devfs_ruleset {
|
|
|
|
cmd := fmt.Sprintf("devfs rule -s %s delset", jail.Config.Devfs_ruleset)
|
|
|
|
_, err := executeCommand(cmd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-19 19:06:41 +01:00
|
|
|
func umountJailFsFromHost(jail *Jail, mountpoint string) error {
|
|
|
|
cmd := "mount -p"
|
|
|
|
out, err := executeCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Error executing mount: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
remSpPtrn := regexp.MustCompile(`\s+`)
|
|
|
|
for _, l := range strings.Split(out, "\n") {
|
|
|
|
f := strings.Split(remSpPtrn.ReplaceAllString(l, " "), " ")
|
|
|
|
if len(f) > 2 {
|
|
|
|
if strings.EqualFold(f[1], fmt.Sprintf("%s%s", jail.RootPath, mountpoint)) {
|
|
|
|
cmd = fmt.Sprintf("umount %s%s", jail.RootPath, mountpoint)
|
|
|
|
_, err := executeCommand(cmd)
|
|
|
|
if err != nil {
|
2022-06-19 19:14:11 +02:00
|
|
|
return errors.New(fmt.Sprintf("Error umounting %s%s: %s", jail.RootPath, mountpoint, err.Error()))
|
2021-12-19 19:06:41 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-19 12:42:29 +01:00
|
|
|
// Internal usage only
|
|
|
|
func stopJail(jail *Jail) error {
|
|
|
|
cmd := "jail -q"
|
|
|
|
|
|
|
|
// Test if conf file exist (iocage created)
|
|
|
|
cf := fmt.Sprintf("/var/run/jail.%s.conf", jail.InternalName)
|
|
|
|
file, err := os.Open(cf)
|
|
|
|
if err != nil {
|
|
|
|
file.Close()
|
|
|
|
cmd = fmt.Sprintf("%s -f %s", cmd, cf)
|
|
|
|
}
|
|
|
|
cmd = fmt.Sprintf("%s -r %s", cmd, jail.InternalName)
|
|
|
|
|
|
|
|
_, err = executeCommand(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-18 21:34:43 +01:00
|
|
|
/*
|
|
|
|
Stop jail:
|
|
|
|
Remove rctl rules
|
|
|
|
Execute prestop if set (jailhost perimeter)
|
|
|
|
Execute stop if set (inside jail)
|
2021-12-19 12:42:29 +01:00
|
|
|
Umount ZFS dataset from inside jail
|
|
|
|
Unjail ZFS dataset from jailhost
|
|
|
|
If VNet
|
|
|
|
Delete VNet interface on host
|
2021-12-18 21:34:43 +01:00
|
|
|
Delete devfs ruleset
|
2021-12-19 12:42:29 +01:00
|
|
|
Effectively stop jail process
|
|
|
|
Umount all mountpoints from $jail/fstab
|
2021-12-19 19:06:41 +01:00
|
|
|
Umount proc if set
|
|
|
|
Umount linprocfs if set
|
|
|
|
Umount fdescfs if set
|
|
|
|
Umount devfs if set
|
2021-12-18 21:34:43 +01:00
|
|
|
|
|
|
|
Use setfib for each command
|
|
|
|
|
|
|
|
Shouldnt rctl rules be removed last, when jail is stopped?
|
|
|
|
*/
|
2021-12-19 12:42:29 +01:00
|
|
|
func StopJail(args []string) {
|
2021-12-18 21:34:43 +01:00
|
|
|
// Current jail were stopping
|
|
|
|
var cj *Jail
|
|
|
|
|
|
|
|
for _, j := range args {
|
|
|
|
fmt.Printf("> Stopping jail %s\n", j)
|
|
|
|
|
|
|
|
for _, rj := range gJails {
|
|
|
|
if rj.Name == j {
|
|
|
|
cj = &rj
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cj == nil {
|
|
|
|
fmt.Printf("Jail not found: %s\n", j)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cj.Running == false {
|
|
|
|
fmt.Printf("Jail %s is not running!\n", cj.Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-18 22:23:55 +01:00
|
|
|
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
|
|
|
|
if err == nil && len(out) > 0 {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Remove RCTL rules:\n")
|
2021-12-18 22:23:55 +01:00
|
|
|
err := removeRctlRules(cj.InternalName, []string{""})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Remove RCTL rules: OK\n")
|
2021-12-18 22:23:55 +01:00
|
|
|
}
|
2021-12-18 21:34:43 +01:00
|
|
|
}
|
|
|
|
|
2022-04-24 16:49:54 +02:00
|
|
|
if len(cj.Config.Exec_prestop) > 0 {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Execute prestop:\n")
|
|
|
|
_, err := executeCommand(cj.Config.Exec_prestop)
|
2021-12-18 21:34:43 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Execute prestop: OK\n")
|
2021-12-18 21:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-24 16:49:54 +02:00
|
|
|
|
|
|
|
if len(cj.Config.Exec_stop) > 0 {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Execute stop:\n")
|
|
|
|
_, err := executeCommandInJail(cj, cj.Config.Exec_stop)
|
2021-12-18 21:34:43 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Execute stop: OK\n")
|
2021-12-18 21:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-19 12:42:29 +01:00
|
|
|
|
2021-12-18 21:34:43 +01:00
|
|
|
if cj.Config.Jail_zfs > 0 {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Umount jailed ZFS:\n")
|
|
|
|
err := umountAndUnjailZFS(cj)
|
2021-12-18 21:34:43 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Umount jailed ZFS: OK\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cj.Config.Vnet > 0 && len(cj.Config.Ip4_addr) > 0 {
|
|
|
|
fmt.Printf(" > Destroy VNet interfaces:\n")
|
|
|
|
err := destroyVNetInterfaces(cj)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" > Destroy VNet interfaces: OK\n")
|
2021-12-18 21:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
2022-04-24 16:49:54 +02:00
|
|
|
|
2022-04-02 14:17:10 +02:00
|
|
|
/*fmt.Printf(" > Remove devfsruleset %s:\n", cj.Config.Devfs_ruleset)
|
2021-12-19 12:42:29 +01:00
|
|
|
err = deleteDevfsRuleset(cj)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" > Remove devfsruleset %s: OK\n", cj.Config.Devfs_ruleset)
|
2022-04-02 14:17:10 +02:00
|
|
|
}*/
|
2022-04-24 16:49:54 +02:00
|
|
|
|
2021-12-19 12:42:29 +01:00
|
|
|
fmt.Printf(" > Stop jail %s:\n", cj.Name)
|
|
|
|
err = stopJail(cj)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" > Stop jail %s: OK\n", cj.Name)
|
|
|
|
}
|
2021-12-19 19:06:41 +01:00
|
|
|
|
|
|
|
if cj.Config.Mount_procfs > 0 {
|
|
|
|
fmt.Printf(" > Umount procfs:\n")
|
|
|
|
err := umountJailFsFromHost(cj, "/proc")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" > Umount procfs: OK\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cj.Config.Mount_linprocfs > 0 {
|
|
|
|
fmt.Printf(" > Umount linprocfs:\n")
|
|
|
|
err := umountJailFsFromHost(cj, "/compat/linux/proc")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" > Umount linprocfs: OK\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-19 19:14:11 +02:00
|
|
|
// FIXME: /dev/fd is mounted even with Mount_fdescfs = 0 ?!
|
|
|
|
//if cj.Config.Mount_fdescfs > 0 {
|
2021-12-19 19:06:41 +01:00
|
|
|
fmt.Printf(" > Umount fdescfs:\n")
|
2022-06-19 19:14:11 +02:00
|
|
|
err = umountJailFsFromHost(cj, "/dev/fd")
|
2021-12-19 19:06:41 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" > Umount fdescfs: OK\n")
|
|
|
|
}
|
2022-06-19 19:14:11 +02:00
|
|
|
//}
|
2021-12-19 19:06:41 +01:00
|
|
|
|
|
|
|
if cj.Config.Mount_devfs > 0 {
|
|
|
|
fmt.Printf(" > Umount devfs:\n")
|
|
|
|
err := umountJailFsFromHost(cj, "/dev")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" > Umount devfs: OK\n")
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 14:17:10 +02:00
|
|
|
|
|
|
|
// Remove local mounts from $JAIL/fstab
|
|
|
|
fstab := strings.Replace(cj.ConfigPath, "config.json", "fstab", 1)
|
|
|
|
mounts, err := getFstab(fstab)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
if len(mounts) > 0 {
|
|
|
|
fmt.Printf(" > Umount mountpoints from %s\n", fstab)
|
|
|
|
errs := 0
|
|
|
|
for _, m := range mounts {
|
|
|
|
err = umountJailFsFromHost(cj, m.Mountpoint)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR: %s\n", err.Error())
|
|
|
|
errs += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if errs == 0 {
|
|
|
|
fmt.Printf(" > Umount mountpoints from %s: OK\n", fstab)
|
|
|
|
}
|
|
|
|
}
|
2022-06-19 19:14:11 +02:00
|
|
|
|
|
|
|
// Remove parameter file
|
|
|
|
pfile := fmt.Sprintf("/var/run/jail.%s.conf", cj.InternalName)
|
|
|
|
if err = os.Remove(pfile); err != nil {
|
|
|
|
fmt.Printf("Error deleting parameter file %s\n", pfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
cj.InternalName = ""
|
2021-12-18 21:34:43 +01:00
|
|
|
}
|
|
|
|
}
|