xref: /freebsd/share/examples/flua/libjail.lua (revision b3e76948)
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
366a7647ecSKyle Evanslocal has_demo = false
376a7647ecSKyle Evans
386a7647ecSKyle Evans-- Make sure we don't have a demo jail to start with; "jid" and "name" are
396a7647ecSKyle Evans-- always present.
406a7647ecSKyle Evansfor jparams in jail.list() do
416a7647ecSKyle Evans    if jparams["name"] == name then
426a7647ecSKyle Evans        has_demo = true
436a7647ecSKyle Evans        break
446a7647ecSKyle Evans    end
456a7647ecSKyle Evansend
466a7647ecSKyle Evans
476a7647ecSKyle 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
536a7647ecSKyle 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
676a7647ecSKyle Evans-- Confirm that we still have it for now.
686a7647ecSKyle Evanshas_demo = false
696a7647ecSKyle Evansfor jparams in jail.list() do
706a7647ecSKyle Evans    if jparams["name"] == name then
716a7647ecSKyle Evans        has_demo = true
726a7647ecSKyle Evans        break
736a7647ecSKyle Evans    end
746a7647ecSKyle Evansend
756a7647ecSKyle Evans
766a7647ecSKyle Evansif not has_demo then
776a7647ecSKyle Evans    print("demo does not exist")
786a7647ecSKyle Evansend
796a7647ecSKyle 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
856a7647ecSKyle Evans
866a7647ecSKyle Evans-- Verify that the jail is no longer on the system.
876a7647ecSKyle Evanslocal is_persistent = false
886a7647ecSKyle Evanshas_demo = false
896a7647ecSKyle Evansfor jparams in jail.list({"persist"}) do
906a7647ecSKyle Evans    if jparams["name"] == name then
916a7647ecSKyle Evans        has_demo = true
926a7647ecSKyle Evans        jid = jparams["jid"]
936a7647ecSKyle Evans        is_persistent = jparams["persist"] ~= "false"
946a7647ecSKyle Evans    end
956a7647ecSKyle Evansend
966a7647ecSKyle Evans
976a7647ecSKyle Evans-- In fact, it does remain until this process ends -- c'est la vie.
986a7647ecSKyle Evansif has_demo then
996a7647ecSKyle Evans    io.write("demo still exists, jid " .. jid .. ", ")
1006a7647ecSKyle Evans    if is_persistent then
1016a7647ecSKyle Evans        io.write("persistent\n")
1026a7647ecSKyle Evans    else
1036a7647ecSKyle Evans        io.write("not persistent\n")
1046a7647ecSKyle Evans    end
1056a7647ecSKyle Evansend
106