gocage/cmd/struct.go
2024-09-05 09:32:13 +02:00

654 lines
23 KiB
Go

package cmd
import (
"fmt"
"sort"
"time"
"strings"
)
const (
IPv4 = 0
IPv6 = 1
)
type NatDesc struct {
Proto string
JailPort string
HostPort string
}
// To allow sorting, just duplicate fields in JailSort below
type Jail struct {
Name string
InternalName string
JID int
Config JailConfig
RootPath string
ConfigPath string
ConfigUpdated bool
Running bool
// No need, Config.Release always represent what is running (plus it know release for non-running jails)
//Release string
Devfs_ruleset int // The effective devfs ruleset generated at runtime
Zpool string
Datastore string
}
// iocage struct as stored in config.json
// CONFIG_VERSION = 27
//
// Fields in this struct are acquired by their name using reflection
// So these char are forbidden for field name: -+.
// Array should be forbidden, or else you'll need to rewrite setJailProperty()
//
// To allow sorting, just duplicate fields in JailConfigSort below
type JailConfig struct {
Config_version string `json:"CONFIG_VERSION"`
Allow_chflags int `json:"allow_chflags"`
Allow_mlock int `json:"allow_mlock"`
Allow_mount int `json:"allow_mount"`
Allow_mount_devfs int `json:"allow_mount_devfs"`
Allow_mount_fusefs int `json:"allow_mount_fusefs"`
Allow_mount_nullfs int `json:"allow_mount_nullfs"`
Allow_mount_procfs int `json:"allow_mount_procfs"`
Allow_mount_tmpfs int `json:"allow_mount_tmpfs"`
Allow_mount_zfs int `json:"allow_mount_zfs"`
Allow_quotas int `json:"allow_quotas"`
Allow_raw_sockets int `json:"allow_raw_sockets"`
Allow_set_hostname int `json:"allow_set_hostname"`
Allow_socket_af int `json:"allow_socket_af"`
Allow_sysvipc int `json:"allow_sysvipc"`
Allow_tun int `json:"allow_tun"`
Allow_vmm int `json:"allow_vmm"`
Assign_localhost int `json:"assign_localhost"`
Available string `json:"available"`
Basejail int `json:"basejail"`
Boot int `json:"boot"`
Bpf int `json:"bpf"`
Children_max string `json:"children_max"`
Cloned_release string `json:"cloned_release"`
Comment string `json:"comment"`
Compression string `json:"compression"`
Compressratio string `json:"compressratio"`
Coredumpsize string `json:"coredumpsize"`
Count string `json:"count"`
Cpuset string `json:"cpuset"`
Cputime string `json:"cputime"`
Datasize string `json:"datasize"`
Dedup string `json:"dedup"`
Defaultrouter string `json:"defaultrouter"`
Defaultrouter6 string `json:"defaultrouter6"`
Depends string `json:"depends"`
Devfs_ruleset string `json:"devfs_ruleset"`
Dhcp int `json:"dhcp"`
Enforce_statfs string `json:"enforce_statfs"`
Exec_clean int `json:"exec_clean"`
Exec_created string `json:"exec_created"`
Exec_fib string `json:"exec_fib"`
Exec_jail_user string `json:"exec_jail_user"`
Exec_poststart string `json:"exec_poststart"`
Exec_poststop string `json:"exec_poststop"`
Exec_prestart string `json:"exec_prestart"`
Exec_prestop string `json:"exec_prestop"`
Exec_start string `json:"exec_start"`
Exec_stop string `json:"exec_stop"`
Exec_system_jail_user string `json:"exec_system_jail_user"`
Exec_system_user string `json:"exec_system_user"`
Exec_timeout string `json:"exec_timeout"`
Host_domainname string `json:"host_domainname"`
Host_hostname string `json:"host_hostname"`
Host_hostuuid string `json:"host_hostuuid"`
Host_time int `json:"host_time"`
Hostid string `json:"hostid"`
Hostid_strict_check int `json:"hostid_strict_check"`
// Specify multiple net cards with "vnet0:bridge0,vnet1:bridge1"
Interfaces string `json:"interfaces"`
Ip4 string `json:"ip4"`
// Specify multiples IP with "vnet0|192.168.1.42,vnet1|10.0.0.3"
Ip4_addr string `json:"ip4_addr"`
Ip4_saddrsel string `json:"ip4_saddrsel"`
Ip6 string `json:"ip6"`
Ip6_addr string `json:"ip6_addr"`
Ip6_saddrsel string `json:"ip4_saddrsel"`
Ip_hostname int `json:"ip_hostname"`
Jail_zfs int `json:"jail_zfs"`
Jail_zfs_dataset string `json:"jail_zfs_dataset"`
Jail_zfs_mountpoint string `json:"jail_zfs_mountpoint"`
Last_started string `json:"last_started"`
Localhost_ip string `json:"localhost_ip"`
Login_flags string `json:"login_flags"`
Mac_prefix string `json:"mac_prefix"`
Maxproc string `json:"maxproc"`
Memorylocked string `json:"memorylocked"`
Memoryuse string `json:"memoryuse"`
Min_dyn_devfs_ruleset string `json:"min_dyn_devfs_ruleset"`
Mount_devfs int `json:"mount_devfs"`
Mount_fdescfs int `json:"mount_fdescfs"`
Mount_linprocfs int `json:"mount_linprocfs"`
Mount_procfs int `json:"mount_procfs"`
Mountpoint string `json:"mountpoint"`
Msgqqueued string `json:"msgqqueued"`
Msgqsize string `json:"msgqsize"`
Nat int `json:"nat"`
Nat_backend string `json:"nat_backend"`
Nat_forwards string `json:"nat_forwards"`
Nat_interface string `json:"nat_interface"`
Nat_prefix string `json:"nat_prefix"`
Nmsgq string `json:"nmsgq"`
Notes string `json:"notes"`
Nsem string `json:"nsem"`
Nsemop string `json:"nsemop"`
Nshm string `json:"nshm"`
Nthr string `json:"nthr"`
Openfiles string `json:"openfiles"`
Origin string `json:"origin"`
Owner string `json:"owner"`
Pcpu string `json:"pcpu"`
Plugin_name string `json:"plugin_name"`
Plugin_repository string `json:"plugin_repository"`
Priority string `json:"priority"`
Pseudoterminals string `json:"pseudoterminals"`
Quota string `json:"quota"`
Readbps string `json:"readbps"`
Readiops string `json:"readiops"`
Release string `json:"release"`
Reservation string `json:"reservation"`
Resolver string `json:"resolver"`
Rlimits string `json:"rlimits"`
Rtsold int `json:"rtsold"`
Securelevel string `json:"securelevel"`
Shmsize string `json:"shmsize"`
Stacksize string `json:"stacksize"`
Stop_timeout string `json:"stop_timeout"`
Swapuse string `json:"swapuse"`
Sync_state string `json:"sync_state"`
Sync_target string `json:"sync_target"`
Sync_tgt_zpool string `json:"sync_tgt_zpool"`
Sysvmsg string `json:"sysvmsg"`
Sysvsem string `json:"sysvsem"`
Sysvshm string `json:"sysvshm"`
Template int `json:"template"`
// Go don't like a variable named "type" (And i dont care about finding a cleaner way)
Jailtype string `json:"type"`
Used string `json:"used"`
Vmemoryuse string `json:"vmemoryuse"`
Vnet int `json:"vnet"`
Vnet0_mac string `json:"vnet0_mac"`
Vnet1_mac string `json:"vnet1_mac"`
Vnet2_mac string `json:"vnet2_mac"`
Vnet3_mac string `json:"vnet3_mac"`
Vnet_default_interface string `json:"vnet_default_interface"`
Vnet_interfaces string `json:"vnet_interfaces"`
Wallclock string `json:"wallclock"`
Writebps string `json:"writebps"`
Writeiops string `json:"writeiops"`
}
// Represent an fstab line
type Mount struct {
Device string
Mountpoint string
Type string
Options []string
Fs_Freq int
Fs_Passno int
}
type Snapshot struct {
// snapshot name is stored after '@' in dataset name
Name string
Datastore string
Jailname string
Mountpoint string
Used uint64
Referenced uint64
Creation time.Time
}
type FreeBSDVersion struct {
major int
minor int
flavor string
patchLevel int
}
type JailHost struct {
hostname string
hostid string
arch string
default_gateway4 string
default_gateway6 string
default_interface string
version FreeBSDVersion
}
type Datastore struct {
Name string
Mountpoint string
ZFSDataset string
DefaultJailConfig JailConfig
Used uint64
Referenced uint64
Available uint64
}
// Fields in this struct are acquired by their name using reflection
// So these char are forbidden for field name: -+.
type JailSort struct {
NameInc jailLessFunc
NameDec jailLessFunc
InternalNameInc jailLessFunc
InternalNameDec jailLessFunc
JIDInc jailLessFunc
JIDDec jailLessFunc
RootPathInc jailLessFunc
RootPathDec jailLessFunc
ConfigPathInc jailLessFunc
ConfigPathDec jailLessFunc
RunningInc jailLessFunc
RunningDec jailLessFunc
DatastoreInc jailLessFunc
DatastoreDec jailLessFunc
ZpoolInc jailLessFunc
ZpoolDec jailLessFunc
Devfs_rulesetInc jailLessFunc
Devfs_rulesetDec jailLessFunc
Config JailConfigSort
}
type JailConfigSort struct {
Config_versionInc jailLessFunc
Config_versionDec jailLessFunc
Allow_chflagsInc jailLessFunc
Allow_chflagsDec jailLessFunc
Allow_mlockInc jailLessFunc
Allow_mlockDec jailLessFunc
Allow_mountInc jailLessFunc
Allow_mountDec jailLessFunc
Allow_mount_devfsInc jailLessFunc
Allow_mount_devfsDec jailLessFunc
Allow_mount_fusefsInc jailLessFunc
Allow_mount_fusefsDec jailLessFunc
Allow_mount_nullfsInc jailLessFunc
Allow_mount_nullfsDec jailLessFunc
Allow_mount_procfsInc jailLessFunc
Allow_mount_procfsDec jailLessFunc
Allow_mount_tmpfsInc jailLessFunc
Allow_mount_tmpfsDec jailLessFunc
Allow_mount_zfsInc jailLessFunc
Allow_mount_zfsDec jailLessFunc
Allow_quotasInc jailLessFunc
Allow_quotasDec jailLessFunc
Allow_raw_socketsInc jailLessFunc
Allow_raw_socketsDec jailLessFunc
Allow_set_hostnameInc jailLessFunc
Allow_set_hostnameDec jailLessFunc
Allow_socket_afInc jailLessFunc
Allow_socket_afDec jailLessFunc
Allow_sysvipcInc jailLessFunc
Allow_sysvipcDec jailLessFunc
Allow_tunInc jailLessFunc
Allow_tunDec jailLessFunc
Allow_vmmInc jailLessFunc
Allow_vmmDec jailLessFunc
Assign_localhostInc jailLessFunc
Assign_localhostDec jailLessFunc
AvailableInc jailLessFunc
AvailableDec jailLessFunc
BasejailInc jailLessFunc
BasejailDec jailLessFunc
BootInc jailLessFunc
BootDec jailLessFunc
BpfInc jailLessFunc
BpfDec jailLessFunc
Children_maxInc jailLessFunc
Children_maxDec jailLessFunc
Cloned_releaseInc jailLessFunc
Cloned_releaseDec jailLessFunc
CommentInc jailLessFunc
CommentDec jailLessFunc
CompressionInc jailLessFunc
CompressionDec jailLessFunc
CompressratioInc jailLessFunc
CompressratioDec jailLessFunc
CoredumpsizeInc jailLessFunc
CoredumpsizeDec jailLessFunc
CountInc jailLessFunc
CountDec jailLessFunc
CpusetInc jailLessFunc
CpusetDec jailLessFunc
CputimeInc jailLessFunc
CputimeDec jailLessFunc
DatasizeInc jailLessFunc
DatasizeDec jailLessFunc
DedupInc jailLessFunc
DedupDec jailLessFunc
DefaultrouterInc jailLessFunc
DefaultrouterDec jailLessFunc
Defaultrouter6Inc jailLessFunc
Defaultrouter6Dec jailLessFunc
DependsInc jailLessFunc
DependsDec jailLessFunc
Devfs_rulesetInc jailLessFunc
Devfs_rulesetDec jailLessFunc
DhcpInc jailLessFunc
DhcpDec jailLessFunc
Enforce_statfsInc jailLessFunc
Enforce_statfsDec jailLessFunc
Exec_cleanInc jailLessFunc
Exec_cleanDec jailLessFunc
Exec_createdInc jailLessFunc
Exec_createdDec jailLessFunc
Exec_fibInc jailLessFunc
Exec_fibDec jailLessFunc
Exec_jail_userInc jailLessFunc
Exec_jail_userDec jailLessFunc
Exec_poststartInc jailLessFunc
Exec_poststartDec jailLessFunc
Exec_poststopInc jailLessFunc
Exec_poststopDec jailLessFunc
Exec_prestartInc jailLessFunc
Exec_prestartDec jailLessFunc
Exec_prestopInc jailLessFunc
Exec_prestopDec jailLessFunc
Exec_startInc jailLessFunc
Exec_startDec jailLessFunc
Exec_stopInc jailLessFunc
Exec_stopDec jailLessFunc
Exec_system_jail_userInc jailLessFunc
Exec_system_jail_userDec jailLessFunc
Exec_system_userInc jailLessFunc
Exec_system_userDec jailLessFunc
Exec_timeoutInc jailLessFunc
Exec_timeoutDec jailLessFunc
Host_domainnameInc jailLessFunc
Host_domainnameDec jailLessFunc
Host_hostnameInc jailLessFunc
Host_hostnameDec jailLessFunc
Host_hostuuidInc jailLessFunc
Host_hostuuidDec jailLessFunc
Host_timeInc jailLessFunc
Host_timeDec jailLessFunc
HostidInc jailLessFunc
HostidDec jailLessFunc
Hostid_strict_checkInc jailLessFunc
Hostid_strict_checkDec jailLessFunc
InterfacesInc jailLessFunc
InterfacesDec jailLessFunc
Ip4Inc jailLessFunc
Ip4Dec jailLessFunc
Ip4_addrInc jailLessFunc
Ip4_addrDec jailLessFunc
Ip4_saddrselInc jailLessFunc
Ip4_saddrselDec jailLessFunc
Ip6Inc jailLessFunc
Ip6Dec jailLessFunc
Ip6_addrInc jailLessFunc
Ip6_addrDec jailLessFunc
Ip6_saddrselInc jailLessFunc
Ip6_saddrselDec jailLessFunc
Ip_hostnameInc jailLessFunc
Ip_hostnameDec jailLessFunc
Jail_zfsInc jailLessFunc
Jail_zfsDec jailLessFunc
Jail_zfs_datasetInc jailLessFunc
Jail_zfs_datasetDec jailLessFunc
Jail_zfs_mountpointInc jailLessFunc
Jail_zfs_mountpointDec jailLessFunc
Last_startedInc jailLessFunc
Last_startedDec jailLessFunc
Localhost_ipInc jailLessFunc
Localhost_ipDec jailLessFunc
Login_flagsInc jailLessFunc
Login_flagsDec jailLessFunc
Mac_prefixInc jailLessFunc
Mac_prefixDec jailLessFunc
MaxprocInc jailLessFunc
MaxprocDec jailLessFunc
MemorylockedInc jailLessFunc
MemorylockedDec jailLessFunc
MemoryuseInc jailLessFunc
MemoryuseDec jailLessFunc
Min_dyn_devfs_rulesetInc jailLessFunc
Min_dyn_devfs_rulesetDec jailLessFunc
Mount_devfsInc jailLessFunc
Mount_devfsDec jailLessFunc
Mount_fdescfsInc jailLessFunc
Mount_fdescfsDec jailLessFunc
Mount_linprocfsInc jailLessFunc
Mount_linprocfsDec jailLessFunc
Mount_procfsInc jailLessFunc
Mount_procfsDec jailLessFunc
MountpointInc jailLessFunc
MountpointDec jailLessFunc
MsgqqueuedInc jailLessFunc
MsgqqueuedDec jailLessFunc
MsgqsizeInc jailLessFunc
MsgqsizeDec jailLessFunc
NatInc jailLessFunc
NatDec jailLessFunc
Nat_backendInc jailLessFunc
Nat_backendDec jailLessFunc
Nat_forwardsInc jailLessFunc
Nat_forwardsDec jailLessFunc
Nat_interfaceInc jailLessFunc
Nat_interfaceDec jailLessFunc
Nat_prefixInc jailLessFunc
Nat_prefixDec jailLessFunc
NmsgqInc jailLessFunc
NmsgqDec jailLessFunc
NotesInc jailLessFunc
NotesDec jailLessFunc
NsemInc jailLessFunc
NsemDec jailLessFunc
NsemopInc jailLessFunc
NsemopDec jailLessFunc
NshmInc jailLessFunc
NshmDec jailLessFunc
NthrInc jailLessFunc
NthrDec jailLessFunc
OpenfilesInc jailLessFunc
OpenfilesDec jailLessFunc
OriginInc jailLessFunc
OriginDec jailLessFunc
OwnerInc jailLessFunc
OwnerDec jailLessFunc
PcpuInc jailLessFunc
PcpuDec jailLessFunc
Plugin_nameInc jailLessFunc
Plugin_nameDec jailLessFunc
Plugin_repositoryInc jailLessFunc
Plugin_repositoryDec jailLessFunc
PriorityInc jailLessFunc
PriorityDec jailLessFunc
PseudoterminalsInc jailLessFunc
PseudoterminalsDec jailLessFunc
QuotaInc jailLessFunc
QuotaDec jailLessFunc
ReadbpsInc jailLessFunc
ReadbpsDec jailLessFunc
ReadiopsInc jailLessFunc
ReadiopsDec jailLessFunc
ReleaseInc jailLessFunc
ReleaseDec jailLessFunc
ReservationInc jailLessFunc
ReservationDec jailLessFunc
ResolverInc jailLessFunc
ResolverDec jailLessFunc
RlimitsInc jailLessFunc
RlimitsDec jailLessFunc
RtsoldInc jailLessFunc
RtsoldDec jailLessFunc
SecurelevelInc jailLessFunc
SecurelevelDec jailLessFunc
ShmsizeInc jailLessFunc
ShmsizeDec jailLessFunc
StacksizeInc jailLessFunc
StacksizeDec jailLessFunc
Stop_timeoutInc jailLessFunc
Stop_timeoutDec jailLessFunc
SwapuseInc jailLessFunc
SwapuseDec jailLessFunc
Sync_stateInc jailLessFunc
Sync_stateDec jailLessFunc
Sync_targetInc jailLessFunc
Sync_targetDec jailLessFunc
Sync_tgt_zpoolInc jailLessFunc
Sync_tgt_zpoolDec jailLessFunc
SysvmsgInc jailLessFunc
SysvmsgDec jailLessFunc
SysvsemInc jailLessFunc
SysvsemDec jailLessFunc
SysvshmInc jailLessFunc
SysvshmDec jailLessFunc
TemplateInc jailLessFunc
TemplateDec jailLessFunc
JailtypeInc jailLessFunc
JailtypeDec jailLessFunc
UsedInc jailLessFunc
UsedDec jailLessFunc
VmemoryuseInc jailLessFunc
VmemoryuseDec jailLessFunc
VnetInc jailLessFunc
VnetDec jailLessFunc
Vnet0_macInc jailLessFunc
Vnet0_macDec jailLessFunc
Vnet1_macInc jailLessFunc
Vnet1_macDec jailLessFunc
Vnet2_macInc jailLessFunc
Vnet2_macDec jailLessFunc
Vnet3_macInc jailLessFunc
Vnet3_macDec jailLessFunc
Vnet_default_interfaceInc jailLessFunc
Vnet_default_interfaceDec jailLessFunc
Vnet_interfacesInc jailLessFunc
Vnet_interfacesDec jailLessFunc
WallclockInc jailLessFunc
WallclockDec jailLessFunc
WritebpsInc jailLessFunc
WritebpsDec jailLessFunc
WriteiopsInc jailLessFunc
WriteiopsDec jailLessFunc
}
type SnapshotSort struct {
NameInc snapshotLessFunc
NameDec snapshotLessFunc
DatastoreInc snapshotLessFunc
DatastoreDec snapshotLessFunc
JailnameInc snapshotLessFunc
JailnameDec snapshotLessFunc
MountpointInc snapshotLessFunc
MountpointDec snapshotLessFunc
UsedInc snapshotLessFunc
UsedDec snapshotLessFunc
ReferencedInc snapshotLessFunc
ReferencedDec snapshotLessFunc
CreationInc snapshotLessFunc
CreationDec snapshotLessFunc
}
type DatastoreSort struct {
NameInc datastoreLessFunc
NameDec datastoreLessFunc
MountpointInc datastoreLessFunc
MountpointDec datastoreLessFunc
ZFSDatasetInc datastoreLessFunc
ZFSDatasetDec datastoreLessFunc
UsedInc datastoreLessFunc
UsedDec datastoreLessFunc
ReferencedInc datastoreLessFunc
ReferencedDec datastoreLessFunc
AvailableInc datastoreLessFunc
AvailableDec datastoreLessFunc
}
type Field struct {
Name string
MaxLen int
Value string
}
func (j *Jail) PrintJSON(fields []string) ([]byte, error) {
return j.PrintJSONWithUpper("", fields)
}
func (j *Jail) PrintJSONWithUpper(upper string, fields []string) ([]byte, error) {
var res []byte
if len(fields) < 1 {
return res, nil
}
if len(upper) > 0 {
res = append(res, []byte(fmt.Sprintf("\"%s\": {", upper))...)
} else {
res = append(res, []byte("{")...)
}
// Reorder fields so we got fields of a sub-structure adjacents
sort.Strings(fields)
var ffname string
offset := 0
for i, _ := range fields {
if i + offset >= len(fields) {
break
}
// Is this struct in struct?
if strings.Contains(fields[i+offset], ".") {
// Count items in this struct
var subfields []string
var newUpper string
for _, sf := range fields[i+offset:] {
if strings.Split(sf, ".")[0] == strings.Split(fields[i+offset], ".")[0] {
newUpper = strings.Split(sf, ".")[0]
sub := strings.Join(strings.Split(string(sf), ".")[1:], ".")
subfields = append(subfields, sub)
}
}
out, err := j.PrintJSONWithUpper(newUpper, subfields)
if err != nil {
return []byte{}, err
}
res = append(res, out...)
offset += len(subfields)-1
if i + offset < len(fields) - 1 {
res = append(res, []byte(",")...)
}
continue
}
if len(upper) > 0 {
ffname = fmt.Sprintf("%s.%s", upper, fields[i+offset])
} else {
ffname = fields[i+offset]
}
val, _, err := getStructFieldValue(j, ffname)
if err != nil {
return []byte{}, fmt.Errorf("Error accessing field \"%s\" : %v\n", fields[i+offset], err)
}
res = append(res, []byte(fmt.Sprintf("\"%s\": ", fields[i+offset]))...)
switch val.Interface().(type) {
case string:
res = append(res, []byte(fmt.Sprintf("\"%s\"", val.Interface().(string)))...)
case int:
res = append(res, []byte(fmt.Sprintf("%d", val.Interface().(int)))...)
case bool:
res = append(res, []byte(fmt.Sprintf("%t", val.Interface().(bool)))...)
default:
fmt.Printf("ERREUR dans Jail.PrintJSONWithUpper() : Type inconnu : %T\n", val.Interface())
}
if i + offset < len(fields) - 1 {
res = append(res, []byte(",")...)
}
}
res = append(res, []byte("}")...)
return res, nil
}