|
32 | 32 | . /usr/local/etc/bastille/bastille.conf
|
33 | 33 |
|
34 | 34 | usage() {
|
35 |
| - error_exit "Usage: bastille mount TARGET host_path container_path [filesystem_type options dump pass_number]" |
| 35 | + error_exit "Usage: bastille mount TARGET HOST_PATH JAIL_PATH [filesystem_type options dump pass_number]" |
36 | 36 | }
|
37 | 37 |
|
38 | 38 | # Handle special-case commands first.
|
39 |
| -case "$1" in |
40 |
| -help|-h|--help) |
41 |
| - usage |
42 |
| - ;; |
| 39 | +case "${1}" in |
| 40 | + help|-h|--help) |
| 41 | + usage |
| 42 | + ;; |
43 | 43 | esac
|
44 | 44 |
|
45 |
| -if [ $# -lt 2 ]; then |
| 45 | +if [ "$#" -lt 3 ] || [ "$#" -gt 6 ]; then |
46 | 46 | usage
|
47 |
| -elif [ $# -eq 2 ]; then |
48 |
| - _fstab="$@ nullfs ro 0 0" |
| 47 | +fi |
| 48 | + |
| 49 | +TARGET="${1}" |
| 50 | +shift |
| 51 | + |
| 52 | +if [ "$#" -eq 2 ]; then |
| 53 | + _fstab="$(echo "$* nullfs ro 0 0" | sed 's#\\ #\\040#g')" |
49 | 54 | else
|
50 |
| - _fstab="$@" |
| 55 | + _fstab="$(echo "$*" | sed 's#\\ #\\040#g')" |
51 | 56 | fi
|
52 | 57 |
|
53 | 58 | bastille_root_check
|
| 59 | +set_target "${TARGET}" |
54 | 60 |
|
55 |
| -## assign needed variables |
56 |
| -_hostpath=$(echo "${_fstab}" | awk '{print $1}') |
57 |
| -_jailpath=$(echo "${_fstab}" | awk '{print $2}') |
| 61 | +# Assign variables |
| 62 | +_hostpath_fstab=$(echo "${_fstab}" | awk '{print $1}') |
| 63 | +_hostpath="$(echo "${_hostpath_fstab}" 2>/dev/null | sed 's#\\040# #g')" |
| 64 | +_jailpath_fstab=$(echo "${_fstab}" | awk '{print $2}') |
| 65 | +_jailpath="$(echo "${_jailpath_fstab}" 2>/dev/null | sed 's#\\040# #g')" |
58 | 66 | _type=$(echo "${_fstab}" | awk '{print $3}')
|
59 | 67 | _perms=$(echo "${_fstab}" | awk '{print $4}')
|
60 | 68 | _checks=$(echo "${_fstab}" | awk '{print $5" "$6}')
|
61 | 69 |
|
62 |
| -## if any variables are empty, bail out |
| 70 | +# Exit if any variables are empty |
63 | 71 | if [ -z "${_hostpath}" ] || [ -z "${_jailpath}" ] || [ -z "${_type}" ] || [ -z "${_perms}" ] || [ -z "${_checks}" ]; then
|
64 | 72 | error_notify "FSTAB format not recognized."
|
65 |
| - warn "Format: /host/path jail/path nullfs ro 0 0" |
| 73 | + warn "Format: /host/path /jail/path nullfs ro 0 0" |
66 | 74 | warn "Read: ${_fstab}"
|
67 |
| - exit 1 |
| 75 | + usage |
68 | 76 | fi
|
69 | 77 |
|
70 |
| -# if host path doesn't exist, type is not "nullfs" or are using advanced mount type "tmpfs,linprocfs,linsysfs, fdescfs, |
71 |
| -# procfs" |
| 78 | +# Exit if host path doesn't exist, type is not "nullfs", or mount is an advanced mount type "tmpfs,linprocfs,linsysfs,fdescfs,procfs" |
72 | 79 | if { [ "${_hostpath}" = "tmpfs" ] && [ "$_type" = "tmpfs" ]; } || \
|
73 | 80 | { [ "${_hostpath}" = "linprocfs" ] && [ "${_type}" = "linprocfs" ]; } || \
|
74 | 81 | { [ "${_hostpath}" = "linsysfs" ] && [ "${_type}" = "linsysfs" ]; } || \
|
75 | 82 | { [ "${_hostpath}" = "proc" ] && [ "${_type}" = "procfs" ]; } || \
|
76 | 83 | { [ "${_hostpath}" = "fdesc" ] && [ "${_type}" = "fdescfs" ]; } then
|
77 | 84 | warn "Detected advanced mount type ${_hostpath}"
|
78 |
| -elif [ ! -d "${_hostpath}" ] || [ "${_type}" != "nullfs" ]; then |
79 |
| - error_notify "Detected invalid host path or incorrect mount type in FSTAB." |
80 |
| - warn "Format: /host/path jail/path nullfs ro 0 0" |
| 85 | +elif [ ! -e "${_hostpath}" ] || [ "${_type}" != "nullfs" ]; then |
| 86 | + error_notify "Invalid host path or incorrect mount type in FSTAB." |
| 87 | + warn "Format: /host/path /jail/path nullfs ro 0 0" |
81 | 88 | warn "Read: ${_fstab}"
|
82 |
| - exit 1 |
| 89 | + usage |
83 | 90 | fi
|
84 | 91 |
|
85 |
| -## if mount permissions are not "ro" or "rw" |
| 92 | +# Mount permissions need to be "ro" or "rw" |
86 | 93 | if [ "${_perms}" != "ro" ] && [ "${_perms}" != "rw" ]; then
|
87 | 94 | error_notify "Detected invalid mount permissions in FSTAB."
|
88 |
| - warn "Format: /host/path jail/path nullfs ro 0 0" |
| 95 | + warn "Format: /host/path /jail/path nullfs ro 0 0" |
89 | 96 | warn "Read: ${_fstab}"
|
90 |
| - exit 1 |
| 97 | + usage |
91 | 98 | fi
|
92 | 99 |
|
93 |
| -## if check & pass are not "0 0 - 1 1"; bail out |
| 100 | +# Dump and pass need to be "0 0 - 1 1" |
94 | 101 | if [ "${_checks}" != "0 0" ] && [ "${_checks}" != "1 0" ] && [ "${_checks}" != "0 1" ] && [ "${_checks}" != "1 1" ]; then
|
95 | 102 | error_notify "Detected invalid fstab options in FSTAB."
|
96 |
| - warn "Format: /host/path jail/path nullfs ro 0 0" |
| 103 | + warn "Format: /host/path /jail/path nullfs ro 0 0" |
97 | 104 | warn "Read: ${_fstab}"
|
98 |
| - exit 1 |
| 105 | + usage |
99 | 106 | fi
|
100 | 107 |
|
101 | 108 | for _jail in ${JAILS}; do
|
| 109 | + |
102 | 110 | info "[${_jail}]:"
|
103 | 111 |
|
104 |
| - ## aggregate variables into FSTAB entry |
105 |
| - _fullpath="${bastille_jailsdir}/${_jail}/root/${_jailpath}" |
106 |
| - _fstab_entry="${_hostpath} ${_fullpath} ${_type} ${_perms} ${_checks}" |
| 112 | + _fullpath_fstab="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath_fstab}" 2>/dev/null | sed 's#//#/#' )" |
| 113 | + _fullpath="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath}" 2>/dev/null | sed 's#//#/#' )" |
| 114 | + _fstab_entry="${_hostpath_fstab} ${_fullpath_fstab} ${_type} ${_perms} ${_checks}" |
107 | 115 |
|
108 |
| - ## Create mount point if it does not exist. -- cwells |
109 |
| - if [ ! -d "${_fullpath}" ]; then |
110 |
| - if ! mkdir -p "${_fullpath}"; then |
111 |
| - error_exit "Failed to create mount point inside jail." |
112 |
| - fi |
| 116 | + # Check if mount point has already been added |
| 117 | + _existing_mount="$(echo ${_fullpath_fstab} 2>/dev/null | sed 's#\\#\\\\#g')" |
| 118 | + if grep -Eq "[[:blank:]]${_existing_mount}.*[[:blank:]]" "${bastille_jailsdir}/${_jail}/fstab"; then |
| 119 | + warn "Mountpoint already present in ${bastille_jailsdir}/${_jail}/fstab" |
| 120 | + grep -E "[[:blank:]]${_existing_mount}" "${bastille_jailsdir}/${_jail}/fstab" |
| 121 | + continue |
113 | 122 | fi
|
114 | 123 |
|
115 |
| - ## if entry doesn't exist, add; else show existing entry |
116 |
| - if ! egrep -q "[[:blank:]]${_fullpath}[[:blank:]]" "${bastille_jailsdir}/${_jail}/fstab" 2> /dev/null; then |
117 |
| - if ! echo "${_fstab_entry}" >> "${bastille_jailsdir}/${_jail}/fstab"; then |
118 |
| - error_exit "Failed to create fstab entry: ${_fstab_entry}" |
| 124 | + |
| 125 | + # Create mount point if it does not exist |
| 126 | + if [ -d "${_hostpath}" ] && [ ! -d "${_fullpath}" ]; then |
| 127 | + mkdir -p "${_fullpath}" || error_continue "Failed to create mount point." |
| 128 | + elif [ -f "${_hostpath}" ] ; then |
| 129 | + _filename="$( basename ${_hostpath} )" |
| 130 | + if echo "${_fullpath}" 2>/dev/null | grep -qow "${_filename}"; then |
| 131 | + mkdir -p "$( dirname "${_fullpath}" )" || error_continue "Failed to create mount point." |
| 132 | + if [ ! -f "${_fullpath}" ]; then |
| 133 | + touch "${_fullpath}" || error_continue "Failed to create mount point." |
| 134 | + else |
| 135 | + error_notify "Failed. File exists at mount point." |
| 136 | + warn "${_fullpath}" |
| 137 | + continue |
| 138 | + fi |
| 139 | + else |
| 140 | + _fullpath_fstab="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath_fstab}/${_filename}" 2>/dev/null | sed 's#//#/#' )" |
| 141 | + _fullpath="$( echo "${bastille_jailsdir}/${_jail}/root/${_jailpath}/${_filename}" 2>/dev/null | sed 's#//#/#' )" |
| 142 | + _fstab_entry="${_hostpath_fstab} ${_fullpath_fstab} ${_type} ${_perms} ${_checks}" |
| 143 | + mkdir -p "$( dirname "${_fullpath}" )" || error_continue "Failed to create mount point." |
| 144 | + if [ ! -f "${_fullpath}" ]; then |
| 145 | + touch "${_fullpath}" || error_continue "Failed to create mount point." |
| 146 | + else |
| 147 | + error_notify "Failed. File exists at mount point." |
| 148 | + warn "${_fullpath}" |
| 149 | + continue |
| 150 | + fi |
119 | 151 | fi
|
120 |
| - echo "Added: ${_fstab_entry}" |
121 |
| - else |
122 |
| - warn "Mountpoint already present in ${bastille_jailsdir}/${_jail}/fstab" |
123 |
| - egrep "[[:blank:]]${_fullpath}[[:blank:]]" "${bastille_jailsdir}/${_jail}/fstab" |
124 |
| - fi |
125 |
| - mount -F "${bastille_jailsdir}/${_jail}/fstab" -a |
126 |
| - echo |
| 152 | + fi |
| 153 | + |
| 154 | + # Add entry to fstab and mount |
| 155 | + echo "${_fstab_entry}" >> "${bastille_jailsdir}/${_jail}/fstab" || error_continue "Failed to create fstab entry: ${_fstab_entry}" |
| 156 | + mount -F "${bastille_jailsdir}/${_jail}/fstab" -a || error_continue "Failed to mount volume: ${_fullpath}" |
| 157 | + echo "Added: ${_fstab_entry}" |
127 | 158 | done
|
0 commit comments