#!/usr/libexec/flua1--[[2/*-3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2020, Ryan Moeller <[email protected]>6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/28]]--2930jail = require("jail")31ucl = require("ucl")3233name = "demo"3435local has_demo = false3637-- Make sure we don't have a demo jail to start with; "jid" and "name" are38-- always present.39for jparams in jail.list() do40if jparams["name"] == name then41has_demo = true42break43end44end4546if not has_demo then47-- Create a persistent jail named "demo" with all other parameters default.48jid, err = jail.setparams(name, {persist = "true"}, jail.CREATE)49if not jid then50error(err)51end52end5354-- Get a list of all known jail parameter names.55allparams = jail.allparams()5657-- Get all the parameters of the jail we created.58jid, res = jail.getparams(name, allparams)59if not jid then60error(res)61end6263-- Display the jail's parameters as a pretty-printed JSON object.64print(ucl.to_json(res))6566-- Confirm that we still have it for now.67has_demo = false68for jparams in jail.list() do69if jparams["name"] == name then70has_demo = true71break72end73end7475if not has_demo then76print("demo does not exist")77end7879-- Update the "persist" parameter to "false" to remove the jail.80jid, err = jail.setparams(name, {persist = "false"}, jail.UPDATE)81if not jid then82error(err)83end8485-- Verify that the jail is no longer on the system.86local is_persistent = false87has_demo = false88for jparams in jail.list({"persist"}) do89if jparams["name"] == name then90has_demo = true91jid = jparams["jid"]92is_persistent = jparams["persist"] ~= "false"93end94end9596-- In fact, it does remain until this process ends -- c'est la vie.97if has_demo then98io.write("demo still exists, jid " .. jid .. ", ")99if is_persistent then100io.write("persistent\n")101else102io.write("not persistent\n")103end104end105106107