xref: /freebsd/share/examples/flua/libjail.lua (revision 6a7647eccd3ef35189c63a61b0ec8865fd559839)
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 * $FreeBSD$
3073577bf0SRyan Moeller */
3173577bf0SRyan Moeller]]--
3273577bf0SRyan Moeller
3373577bf0SRyan Moellerjail = require("jail")
3473577bf0SRyan Moellerucl = require("ucl")
3573577bf0SRyan Moeller
3673577bf0SRyan Moellername = "demo"
3773577bf0SRyan Moeller
38*6a7647ecSKyle Evanslocal has_demo = false
39*6a7647ecSKyle Evans
40*6a7647ecSKyle Evans-- Make sure we don't have a demo jail to start with; "jid" and "name" are
41*6a7647ecSKyle Evans-- always present.
42*6a7647ecSKyle Evansfor jparams in jail.list() do
43*6a7647ecSKyle Evans    if jparams["name"] == name then
44*6a7647ecSKyle Evans        has_demo = true
45*6a7647ecSKyle Evans        break
46*6a7647ecSKyle Evans    end
47*6a7647ecSKyle Evansend
48*6a7647ecSKyle Evans
49*6a7647ecSKyle Evansif not has_demo then
5073577bf0SRyan Moeller    -- Create a persistent jail named "demo" with all other parameters default.
5173577bf0SRyan Moeller    jid, err = jail.setparams(name, {persist = "true"}, jail.CREATE)
5273577bf0SRyan Moeller    if not jid then
5373577bf0SRyan Moeller        error(err)
5473577bf0SRyan Moeller    end
55*6a7647ecSKyle Evansend
5673577bf0SRyan Moeller
5773577bf0SRyan Moeller-- Get a list of all known jail parameter names.
5873577bf0SRyan Moellerallparams = jail.allparams()
5973577bf0SRyan Moeller
6073577bf0SRyan Moeller-- Get all the parameters of the jail we created.
6173577bf0SRyan Moellerjid, res = jail.getparams(name, allparams)
6273577bf0SRyan Moellerif not jid then
6373577bf0SRyan Moeller    error(res)
6473577bf0SRyan Moellerend
6573577bf0SRyan Moeller
6673577bf0SRyan Moeller-- Display the jail's parameters as a pretty-printed JSON object.
6773577bf0SRyan Moellerprint(ucl.to_json(res))
6873577bf0SRyan Moeller
69*6a7647ecSKyle Evans-- Confirm that we still have it for now.
70*6a7647ecSKyle Evanshas_demo = false
71*6a7647ecSKyle Evansfor jparams in jail.list() do
72*6a7647ecSKyle Evans    if jparams["name"] == name then
73*6a7647ecSKyle Evans        has_demo = true
74*6a7647ecSKyle Evans        break
75*6a7647ecSKyle Evans    end
76*6a7647ecSKyle Evansend
77*6a7647ecSKyle Evans
78*6a7647ecSKyle Evansif not has_demo then
79*6a7647ecSKyle Evans    print("demo does not exist")
80*6a7647ecSKyle Evansend
81*6a7647ecSKyle Evans
8273577bf0SRyan Moeller-- Update the "persist" parameter to "false" to remove the jail.
8373577bf0SRyan Moellerjid, err = jail.setparams(name, {persist = "false"}, jail.UPDATE)
8473577bf0SRyan Moellerif not jid then
8573577bf0SRyan Moeller    error(err)
8673577bf0SRyan Moellerend
87*6a7647ecSKyle Evans
88*6a7647ecSKyle Evans-- Verify that the jail is no longer on the system.
89*6a7647ecSKyle Evanslocal is_persistent = false
90*6a7647ecSKyle Evanshas_demo = false
91*6a7647ecSKyle Evansfor jparams in jail.list({"persist"}) do
92*6a7647ecSKyle Evans    if jparams["name"] == name then
93*6a7647ecSKyle Evans        has_demo = true
94*6a7647ecSKyle Evans        jid = jparams["jid"]
95*6a7647ecSKyle Evans        is_persistent = jparams["persist"] ~= "false"
96*6a7647ecSKyle Evans    end
97*6a7647ecSKyle Evansend
98*6a7647ecSKyle Evans
99*6a7647ecSKyle Evans-- In fact, it does remain until this process ends -- c'est la vie.
100*6a7647ecSKyle Evansif has_demo then
101*6a7647ecSKyle Evans    io.write("demo still exists, jid " .. jid .. ", ")
102*6a7647ecSKyle Evans    if is_persistent then
103*6a7647ecSKyle Evans        io.write("persistent\n")
104*6a7647ecSKyle Evans    else
105*6a7647ecSKyle Evans        io.write("not persistent\n")
106*6a7647ecSKyle Evans    end
107*6a7647ecSKyle Evansend
108