xref: /freebsd/share/examples/flua/libjail.lua (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
173577bf0SRyan Moeller#!/usr/libexec/flua
273577bf0SRyan Moeller--[[
373577bf0SRyan Moeller/*-
473577bf0SRyan Moeller * SPDX-License-Identifier: BSD-2-Clause
573577bf0SRyan Moeller *
673577bf0SRyan Moeller * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org>
773577bf0SRyan Moeller *
873577bf0SRyan Moeller * Redistribution and use in source and binary forms, with or without
973577bf0SRyan Moeller * modification, are permitted provided that the following conditions
1073577bf0SRyan Moeller * are met:
1173577bf0SRyan Moeller * 1. Redistributions of source code must retain the above copyright
1273577bf0SRyan Moeller *    notice, this list of conditions and the following disclaimer.
1373577bf0SRyan Moeller * 2. Redistributions in binary form must reproduce the above copyright
1473577bf0SRyan Moeller *    notice, this list of conditions and the following disclaimer in the
1573577bf0SRyan Moeller *    documentation and/or other materials provided with the distribution.
1673577bf0SRyan Moeller *
1773577bf0SRyan Moeller * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1873577bf0SRyan Moeller * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1973577bf0SRyan Moeller * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2073577bf0SRyan Moeller * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2173577bf0SRyan Moeller * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2273577bf0SRyan Moeller * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2373577bf0SRyan Moeller * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2473577bf0SRyan Moeller * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2573577bf0SRyan Moeller * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2673577bf0SRyan Moeller * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2773577bf0SRyan Moeller * SUCH DAMAGE.
2873577bf0SRyan Moeller */
2973577bf0SRyan Moeller]]--
3073577bf0SRyan Moeller
3173577bf0SRyan Moellerjail = require("jail")
3273577bf0SRyan Moellerucl = require("ucl")
3373577bf0SRyan Moeller
3473577bf0SRyan Moellername = "demo"
3573577bf0SRyan Moeller
36*6a7647ecSKyle Evanslocal has_demo = false
37*6a7647ecSKyle Evans
38*6a7647ecSKyle Evans-- Make sure we don't have a demo jail to start with; "jid" and "name" are
39*6a7647ecSKyle Evans-- always present.
40*6a7647ecSKyle Evansfor jparams in jail.list() do
41*6a7647ecSKyle Evans    if jparams["name"] == name then
42*6a7647ecSKyle Evans        has_demo = true
43*6a7647ecSKyle Evans        break
44*6a7647ecSKyle Evans    end
45*6a7647ecSKyle Evansend
46*6a7647ecSKyle Evans
47*6a7647ecSKyle Evansif not has_demo then
4873577bf0SRyan Moeller    -- Create a persistent jail named "demo" with all other parameters default.
4973577bf0SRyan Moeller    jid, err = jail.setparams(name, {persist = "true"}, jail.CREATE)
5073577bf0SRyan Moeller    if not jid then
5173577bf0SRyan Moeller        error(err)
5273577bf0SRyan Moeller    end
53*6a7647ecSKyle Evansend
5473577bf0SRyan Moeller
5573577bf0SRyan Moeller-- Get a list of all known jail parameter names.
5673577bf0SRyan Moellerallparams = jail.allparams()
5773577bf0SRyan Moeller
5873577bf0SRyan Moeller-- Get all the parameters of the jail we created.
5973577bf0SRyan Moellerjid, res = jail.getparams(name, allparams)
6073577bf0SRyan Moellerif not jid then
6173577bf0SRyan Moeller    error(res)
6273577bf0SRyan Moellerend
6373577bf0SRyan Moeller
6473577bf0SRyan Moeller-- Display the jail's parameters as a pretty-printed JSON object.
6573577bf0SRyan Moellerprint(ucl.to_json(res))
6673577bf0SRyan Moeller
67*6a7647ecSKyle Evans-- Confirm that we still have it for now.
68*6a7647ecSKyle Evanshas_demo = false
69*6a7647ecSKyle Evansfor jparams in jail.list() do
70*6a7647ecSKyle Evans    if jparams["name"] == name then
71*6a7647ecSKyle Evans        has_demo = true
72*6a7647ecSKyle Evans        break
73*6a7647ecSKyle Evans    end
74*6a7647ecSKyle Evansend
75*6a7647ecSKyle Evans
76*6a7647ecSKyle Evansif not has_demo then
77*6a7647ecSKyle Evans    print("demo does not exist")
78*6a7647ecSKyle Evansend
79*6a7647ecSKyle Evans
8073577bf0SRyan Moeller-- Update the "persist" parameter to "false" to remove the jail.
8173577bf0SRyan Moellerjid, err = jail.setparams(name, {persist = "false"}, jail.UPDATE)
8273577bf0SRyan Moellerif not jid then
8373577bf0SRyan Moeller    error(err)
8473577bf0SRyan Moellerend
85*6a7647ecSKyle Evans
86*6a7647ecSKyle Evans-- Verify that the jail is no longer on the system.
87*6a7647ecSKyle Evanslocal is_persistent = false
88*6a7647ecSKyle Evanshas_demo = false
89*6a7647ecSKyle Evansfor jparams in jail.list({"persist"}) do
90*6a7647ecSKyle Evans    if jparams["name"] == name then
91*6a7647ecSKyle Evans        has_demo = true
92*6a7647ecSKyle Evans        jid = jparams["jid"]
93*6a7647ecSKyle Evans        is_persistent = jparams["persist"] ~= "false"
94*6a7647ecSKyle Evans    end
95*6a7647ecSKyle Evansend
96*6a7647ecSKyle Evans
97*6a7647ecSKyle Evans-- In fact, it does remain until this process ends -- c'est la vie.
98*6a7647ecSKyle Evansif has_demo then
99*6a7647ecSKyle Evans    io.write("demo still exists, jid " .. jid .. ", ")
100*6a7647ecSKyle Evans    if is_persistent then
101*6a7647ecSKyle Evans        io.write("persistent\n")
102*6a7647ecSKyle Evans    else
103*6a7647ecSKyle Evans        io.write("not persistent\n")
104*6a7647ecSKyle Evans    end
105*6a7647ecSKyle Evansend
106