Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: jcp for copying jail to jail #805

Merged
merged 23 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
228bb3b
cp: update to use new functions in common.sh
tschettervictor Jan 11, 2025
6040308
rcp: update to use new functions
tschettervictor Jan 11, 2025
14d7f4a
docs: cp documentation update for new functions
tschettervictor Jan 11, 2025
c67ab18
docs: add rcp documentation
tschettervictor Jan 11, 2025
816f243
cp: trim double // to /
tschettervictor Jan 12, 2025
f65b8c3
rcp: trim // to / and allow only single target
tschettervictor Jan 12, 2025
8e73e6f
cp: include rcp in cp and add jail mode
tschettervictor Jan 12, 2025
628f0fa
bastille: move cp to no action commands
tschettervictor Jan 14, 2025
1cfbe6b
bastille: remove rcp
tschettervictor Jan 14, 2025
6a1226b
Merge branch 'master' into cp-rcp-new-functions
tschettervictor Jan 18, 2025
22831e4
cp: Exit it -j and -r are both set
tschettervictor Jan 21, 2025
5746af3
common: Add debug mode
tschettervictor Jan 21, 2025
4c2539d
copy: Add jcp for jail to jail copy
tschettervictor Jan 21, 2025
086f4a6
jcp: Add missing fi
tschettervictor Jan 21, 2025
708c9c3
bastille: Add jcp/rcp to commands
tschettervictor Jan 21, 2025
42d1c13
cp: Allow only 3 arcs
tschettervictor Jan 21, 2025
25cc612
rcp: Allow only 3 args
tschettervictor Jan 21, 2025
9c0b600
docs: Document new jcp command
tschettervictor Jan 21, 2025
183a3c2
Merge branch 'master' into cp-rcp-new-functions
tschettervictor Jan 26, 2025
fe1eb18
docs: Typo in jcp
tschettervictor Jan 26, 2025
03649f6
Merge branch 'master' into cp-rcp-new-functions
yaazkal Jan 27, 2025
0ff98de
Merge branch 'BastilleBSD:master' into cp-rcp-new-functions
tschettervictor Jan 27, 2025
43e1868
README: Fix reference to rcp
tschettervictor Jan 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Available Commands:
config Get or set a config value for the targeted container(s).
console Console into a running container.
convert Convert a Thin container into a Thick container.
cp cp(1) files from host to targeted container(s).
cp cp(1) files from host or container to host or targeted container(s).
create Create a new thin container or a thick container if -T|--thick option specified.
destroy Destroy a stopped container or a FreeBSD release.
edit Edit container configuration files (advanced).
Expand All @@ -74,8 +74,8 @@ Available Commands:
list List containers (running).
mount Mount a volume inside the targeted container(s).
pkg Manipulate binary packages within targeted container(s). See pkg(8).
rcp cp(1) files from a jail to host.
yaazkal marked this conversation as resolved.
Show resolved Hide resolved
rdr Redirect host port to container port.
rcp reverse cp(1) files from a single container to the host.
tschettervictor marked this conversation as resolved.
Show resolved Hide resolved
rename Rename a container.
restart Restart a running container.
service Manage services within targeted container(s).
Expand Down
24 changes: 17 additions & 7 deletions docs/chapters/subcommands/cp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@
cp
==

This command allows efficiently copying files from host to container(s).
This command allows copying files from host to jail(s).

.. code-block:: shell

ishmael ~ # bastille cp ALL /tmp/resolv.conf-cf etc/resolv.conf
ishmael ~ # bastille cp ALL /tmp/resolv.conf-cf /etc/resolv.conf
[bastion]:

/tmp/resolv.conf-cf -> /usr/local/bastille/jails/bastion/root/etc/resolv.conf
[unbound0]:

/tmp/resolv.conf-cf -> /usr/local/bastille/jails/unbound0/root/etc/resolv.conf
[unbound1]:

/tmp/resolv.conf-cf -> /usr/local/bastille/jails/unbound1/root/etc/resolv.conf
[squid]:

/tmp/resolv.conf-cf -> /usr/local/bastille/jails/squid/root/etc/resolv.conf
[nginx]:

/tmp/resolv.conf-cf -> /usr/local/bastille/jails/nginx/root/etc/resolv.conf
[folsom]:
/tmp/resolv.conf-cf -> /usr/local/bastille/jails/folsom/root/etc/resolv.conf

Unless you see errors reported in the output the `cp` was successful.

.. code-block:: shell

ishmael ~ # bastille cp help
Usage: bastille cp [option(s)] TARGET HOST_PATH JAIL_PATH
Options:

-q | --quiet Suppress output.
-x | --debug Enable debug mode.
30 changes: 30 additions & 0 deletions docs/chapters/subcommands/jcp.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
===
jcp
===

This command allows copying files from jail to jail(s).

.. code-block:: shell

ishmael ~ # bastille jcp bastion /tmp/resolv.conf-cf ALL /etc/resolv.conf
[unbound0]:
/usr/local/bastille/jails/bastion/root/tmp/resolv.conf-cf -> /usr/local/bastille/jails/unbound0/root/etc/resolv.conf
[unbound1]:
/usr/local/bastille/jails/bastion/root/tmp/resolv.conf-cf -> /usr/local/bastille/jails/unbound1/root/etc/resolv.conf
[squid]:
/usr/local/bastille/jails/bastion/root/tmp/resolv.conf-cf -> /usr/local/bastille/jails/squid/root/etc/resolv.conf
[nginx]:
/usr/local/bastille/jails/bastion/root/tmp/resolv.conf-cf -> /usr/local/bastille/jails/nginx/root/etc/resolv.conf
[folsom]:
/usr/local/bastille/jails/bastion/root/tmp/resolv.conf-cf -> /usr/local/bastille/jails/folsom/root/etc/resolv.conf

Unless you see errors reported in the output the `jcp` was successful.

.. code-block:: shell

ishmael ~ # bastille jcp help
Usage: bastille jcp [option(s)] SOURCE_JAIL JAIL_PATH DEST_JAIL JAIL_PATH
Options:

-q | --quiet Suppress output.
-x | --debug Enable debug mode.
22 changes: 22 additions & 0 deletions docs/chapters/subcommands/rcp.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
===
rcp
===

This command allows copying files from jail to host.

.. code-block:: shell

ishmael ~ # bastille rcp bastion /test/testfile.txt /tmp/testfile.txt
[bastion]:
/usr/local/bastille/jails/bastion/root/test/testfile.txt -> /tmp/testfile.txt

Unless you see errors reported in the output the `rcp` was successful.

.. code-block:: shell

ishmael ~ # bastille rcp help
Usage: bastille rcp [option(s)] TARGET JAIL_PATH HOST_PATH
Options:

-q | --quiet Suppress output.
-x | --debug Enable debug mode.
6 changes: 4 additions & 2 deletions usr/local/bin/bastille
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,21 @@ Available Commands:
config Get or set a config value for the targeted container(s).
console Console into a running container.
convert Convert a Thin container into a Thick container.
cp cp(1) files from host to targeted container(s).
cp cp(1) files from host to jail(s).
create Create a new thin container or a thick container if -T|--thick option specified.
destroy Destroy a stopped container or a FreeBSD release.
edit Edit container configuration files (advanced).
etcupdate Update /etc directory to specified release.
export Exports a specified container.
help Help about any command.
htop Interactive process viewer (requires htop).
jcp cp(1) files from a jail to jail(s).
import Import a specified container.
limits Apply resources limits to targeted container(s). See rctl(8).
list List containers (running).
mount Mount a volume inside the targeted container(s).
pkg Manipulate binary packages within targeted container(s). See pkg(8).
rcp reverse cp(1) files from a single container to the host.
rcp cp(1) files from a jail to host.
rdr Redirect host port to container port.
rename Rename a container.
restart Restart a running container.
Expand Down Expand Up @@ -164,6 +165,7 @@ version|-v|--version)
help|-h|--help)
usage
;;

bootstrap|clone|console|create|cp|destroy|etcupdate|export|htop|import|jcp|list|mount|rcp|rdr|rename|restart|setup|start|top|umount|update|upgrade|verify)
# Nothing "extra" to do for these commands. -- cwells
;;
Expand Down
85 changes: 50 additions & 35 deletions usr/local/share/bastille/cp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,64 @@
. /usr/local/etc/bastille/bastille.conf

usage() {
error_exit "Usage: bastille cp [OPTION] TARGET HOST_PATH CONTAINER_PATH"
}
error_notify "Usage: bastille cp [option(s)] TARGET HOST_PATH JAIL_PATH"
cat << EOF
Options:

CPSOURCE="${1}"
CPDEST="${2}"
-q | --quiet Suppress output.
-x | --debug Enable debug mode.

# Handle special-case commands first.
case "$1" in
help|-h|--help)
usage
;;
-q|--quiet)
OPTION="${1}"
CPSOURCE="${2}"
CPDEST="${3}"
;;
esac
EOF
exit 1
}

# Handle options.
OPTION="-av"
while [ "$#" -gt 0 ]; do
case "${1}" in
-h|--help|help)
usage
;;
-q|--quiet)
OPTION="-a"
shift
;;
-x|--debug)
enable_debug
shift
;;
-*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
case ${_opt} in
q) OPTION="-a" ;;
x) enable_debug ;;
*) error_exit "Unknown Option: \"${1}\"" ;;
esac
done
shift
;;
*)
break
;;
esac
done

if [ $# -ne 2 ]; then
if [ "$#" -ne 3 ]; then
usage
fi

bastille_root_check
TARGET="${1}"
HOST_PATH="${2}"
JAIL_PATH="${3}"

case "${OPTION}" in
-q|--quiet)
OPTION="-a"
;;
*)
OPTION="-av"
;;
esac
bastille_root_check
set_target "${TARGET}"

for _jail in ${JAILS}; do
info "[${_jail}]:"
bastille_jail_path="${bastille_jailsdir}/${_jail}/root"
cp "${OPTION}" "${CPSOURCE}" "${bastille_jail_path}/${CPDEST}"
RETURN="$?"
if [ "${TARGET}" = "ALL" ]; then
# Display the return status for reference
echo -e "Returned: ${RETURN}\n"
else
echo
return "${RETURN}"
host_path="${HOST_PATH}"
jail_path="$(echo ${bastille_jailsdir}/${_jail}/root/${JAIL_PATH} | sed 's#//#/#g')"
if ! cp "${OPTION}" "${host_path}" "${jail_path}"; then
error_continue "CP failed: ${host_path} -> ${jail_path}"
fi
done
done
103 changes: 103 additions & 0 deletions usr/local/share/bastille/jcp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/sh
#
# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright (c) 2018-2025, Christer Edwards <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

. /usr/local/share/bastille/common.sh
. /usr/local/etc/bastille/bastille.conf

usage() {
error_notify "Usage: bastille jcp [option(s)] SOURCE_JAIL JAIL_PATH DEST_JAIL JAIL_PATH"
cat << EOF
Options:

-q | --quiet Suppress output.
-x | --debug Enable debug mode.

EOF
exit 1
}

# Handle options.
OPTION="-av"
while [ "$#" -gt 0 ]; do
case "${1}" in
-h|--help|help)
usage
;;
-q|--quiet)
OPTION="-a"
shift
;;
-x|--debug)
enable_debug
shift
;;
-*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
case ${_opt} in
q) OPTION="-a" ;;
x) enable_debug ;;
*) error_exit "Unknown Option: \"${1}\"" ;;
esac
done
shift
;;
*)
break
;;
esac
done

if [ "$#" -ne 4 ]; then
usage
fi

SOURCE_TARGET="${1}"
SOURCE_PATH="${2}"
DEST_TARGET="${3}"
DEST_PATH="${4}"

bastille_root_check
set_target_single "${SOURCE_TARGET}" && SOURCE_TARGET="${TARGET}"
set_target "${DEST_TARGET}" && DEST_TARGET="${JAILS}"

for _jail in ${DEST_TARGET}; do
if [ "${_jail}" = "${SOURCE_TARGET}" ]; then
continue
else
info "[${_jail}]:"
source_path="$(echo ${bastille_jailsdir}/${SOURCE_TARGET}/root/${SOURCE_PATH} | sed 's#//#/#g')"
dest_path="$(echo ${bastille_jailsdir}/${_jail}/root/${DEST_PATH} | sed 's#//#/#g')"
if ! cp "${OPTION}" "${source_path}" "${dest_path}"; then
error_continue "JCP failed: ${source_path} -> ${dest_path}"
fi
fi
done
Loading