99 lines
2.6 KiB
Go
99 lines
2.6 KiB
Go
|
// Copyright 2021, johan@nosd.in
|
||
|
// +build freebsd
|
||
|
//
|
||
|
// Use libjail.so to get/set jail params
|
||
|
package jail
|
||
|
|
||
|
/*
|
||
|
#cgo CFLAGS: -I /usr/lib
|
||
|
#cgo LDFLAGS: -L. -ljail -lc
|
||
|
#include <stdlib.h>
|
||
|
#include <jail.h>
|
||
|
#include <utmpx.h>
|
||
|
#include <pwd.h>
|
||
|
*/
|
||
|
import "C"
|
||
|
import (
|
||
|
"strconv"
|
||
|
// "syscall"
|
||
|
"unsafe"
|
||
|
)
|
||
|
|
||
|
|
||
|
type Jail struct {
|
||
|
Name string
|
||
|
Jid int
|
||
|
Path string
|
||
|
}
|
||
|
|
||
|
|
||
|
// We can not use jail_getv ou jail_setv because they are variadic C functions (would need a C wrapper)
|
||
|
func GetJails() ([]Jail, error) {
|
||
|
var jls []Jail
|
||
|
var jl Jail
|
||
|
var err error
|
||
|
|
||
|
// Make "params" a list of 4 jails parameters
|
||
|
params := make([]C.struct_jailparam, 4)
|
||
|
|
||
|
// initialize parameter names
|
||
|
csname := C.CString("name")
|
||
|
defer C.free(unsafe.Pointer(csname))
|
||
|
csjid := C.CString("jid")
|
||
|
defer C.free(unsafe.Pointer(csjid))
|
||
|
cspath := C.CString("path")
|
||
|
defer C.free(unsafe.Pointer(cspath))
|
||
|
cslastjid := C.CString("lastjid")
|
||
|
defer C.free(unsafe.Pointer(cslastjid))
|
||
|
|
||
|
// initialize params struct with parameter names
|
||
|
C.jailparam_init(¶ms[0], csname)
|
||
|
C.jailparam_init(¶ms[1], csjid)
|
||
|
C.jailparam_init(¶ms[2], cspath)
|
||
|
|
||
|
// The key to retrieve jail. lastjid = 0 returns first jail and its jid as jailparam_get return value
|
||
|
C.jailparam_init(¶ms[3], cslastjid)
|
||
|
|
||
|
lastjailid := 0
|
||
|
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
||
|
defer C.free(unsafe.Pointer(cslastjidval))
|
||
|
|
||
|
C.jailparam_import(¶ms[3], cslastjidval)
|
||
|
|
||
|
// loop on existing jails
|
||
|
for lastjailid >= 0 {
|
||
|
// get parameter values
|
||
|
lastjailid = int(C.jailparam_get(¶ms[0], 4, 0))
|
||
|
if lastjailid > 0 {
|
||
|
nametmp := C.jailparam_export(¶ms[0])
|
||
|
jl.Name = C.GoString(nametmp)
|
||
|
// Memory mgmt : Non gere par Go
|
||
|
C.free(unsafe.Pointer(nametmp))
|
||
|
|
||
|
jidtmp := C.jailparam_export(¶ms[1])
|
||
|
jl.Jid, _ = strconv.Atoi(C.GoString(jidtmp))
|
||
|
// Memory mgmt : Non gere par Go
|
||
|
C.free(unsafe.Pointer(jidtmp))
|
||
|
|
||
|
pathtmp := C.jailparam_export(¶ms[2])
|
||
|
jl.Path = C.GoString(pathtmp)
|
||
|
// Memory mgmt : Non gere par Go
|
||
|
C.free(unsafe.Pointer(pathtmp))
|
||
|
|
||
|
jls = append(jls, jl)
|
||
|
//log.Debug("Got jid " + strconv.Itoa(jl.jid) + " with name " + jl.name)
|
||
|
|
||
|
// Prepare next loop iteration
|
||
|
cslastjidval := C.CString(strconv.Itoa(lastjailid))
|
||
|
defer C.free(unsafe.Pointer(cslastjidval))
|
||
|
C.jailparam_import(¶ms[3], cslastjidval)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Free 4 items of params list
|
||
|
C.jailparam_free(¶ms[0], 4)
|
||
|
|
||
|
return jls, err
|
||
|
}
|
||
|
|