/
usr
/
libexec
/
oracle-cloud-agent
/
ocatools
/
New Directory
Upload File
HOME
#!/bin/bash set -euo pipefail die() { echo "oca-privileged-helper: $*" >&2 exit 1 } find_cmd() { local candidate for candidate in "$@"; do if [[ -x "${candidate}" ]]; then printf '%s\n' "${candidate}" return fi done die "required command not found: $*" } no_space() { [[ -n "${1:-}" && "${1}" != *[[:space:]]* ]] } is_abs_no_space() { [[ "${1:-}" == /* ]] && no_space "$1" } is_uint() { [[ "${1:-}" =~ ^[0-9]+$ ]] } is_positive_pid() { [[ "${1:-}" =~ ^[1-9][0-9]*$ ]] } is_kill_signal() { [[ "${1:-}" == "-9" || "${1:-}" == "-15" ]] } is_safe_name() { [[ "${1:-}" =~ ^[A-Za-z0-9._:+,@%=-]+$ ]] } is_package_name() { [[ "${1:-}" =~ ^[A-Za-z0-9][A-Za-z0-9._:+-]*$ ]] } is_sha256() { [[ "${1:-}" =~ ^[[:xdigit:]]{64}$ ]] } is_wlp_package_name() { case "${1:-}" in wlp-agent|wlp-agent-stable|wlp-agent-unstable|kernel-uek-devel|kernel-uek-devel-*) return 0 ;; esac return 1 } is_iqn() { [[ "${1:-}" == iqn.* ]] && no_space "$1" } is_portal() { [[ "${1:-}" == *:* ]] && no_space "$1" } is_iscsi_node_path() { local path="${1:-}" local parent local resolved_parent [[ "$path" == /var/lib/iscsi/nodes/* ]] || return 1 no_space "$path" || return 1 has_path_traversal "$path" && return 1 [[ ! -L "$path" && -f "$path" ]] || return 1 is_root_owned_directory /var/lib/iscsi/nodes || return 1 parent="${path%/*}" resolved_parent="$(cd -P -- "$parent" 2>/dev/null && pwd -P)" || return 1 [[ "$resolved_parent" == /var/lib/iscsi/nodes || "$resolved_parent" == /var/lib/iscsi/nodes/* ]] } is_iscsi_config() { case "${1:-}" in node.startup|\ node.session.initial_login_retry_max|\ node.conn\[0\].timeo.login_timeout|\ node.conn\[0\].timeo.logout_timeout|\ node.session.auth.authmethod|\ node.session.auth.username|\ node.session.auth.password|\ node.session.timeo.replacement_timeout|\ node.session.queue_depth) return 0 ;; esac return 1 } is_cpu_range() { [[ "${1:-}" =~ ^[0-9]+(-[0-9]+)?(,[0-9]+(-[0-9]+)?)*$ ]] } is_cpu_mask() { [[ "${1:-}" =~ ^[0-9A-Fa-f]+(,[0-9A-Fa-f]+)*$ ]] } is_sysfs_write_path() { case "${1:-}" in /sys/module/dm_mod/parameters/dm_numa_node|\ /sys/block/*/device/scsi_disk/*/max_retries|\ /sys/block/*/queue/scheduler|\ /sys/bus/workqueue/devices/iscsi_q_*/cpumask|\ /sys/class/net/*/queues/rx-*/rps_cpus) no_space "$1" return ;; esac return 1 } is_safe_mountpoint() { is_abs_no_space "$1" || return 1 case "$1" in /|/bin|/boot|/dev|/etc|/home|/lib|/lib64|/opt|/proc|/root|/run|/sbin|/sys|/tmp|/usr|/var) return 1 ;; esac } is_agent_package_file() { is_abs_no_space "$1" || return 1 case "$1" in /var/lib/oracle-cloud-agent/*.rpm|\ /var/lib/oracle-cloud-agent/*.deb|\ /var/lib/oracle-cloud-agent/pool*/*.rpm|\ /var/lib/oracle-cloud-agent/pool*/*.deb|\ /var/lib/oracle-cloud-agent/pool*/*/*.rpm|\ /var/lib/oracle-cloud-agent/pool*/*/*.deb) return 0 ;; esac return 1 } is_agent_snap_package_file() { is_abs_no_space "$1" || return 1 case "$1" in /var/lib/oracle-cloud-agent/*.snap|\ /var/lib/oracle-cloud-agent/pool*/*.snap|\ /var/lib/oracle-cloud-agent/pool*/*/*.snap) return 0 ;; esac return 1 } is_um_package_file() { case "${1:-}" in /var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/packages/temp-unified-monitoring.rpm|\ /var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/packages/temp-unified-monitoring.deb) return 0 ;; esac return 1 } has_path_traversal() { case "${1:-}" in */../*|*/..) return 0 ;; esac return 1 } resolve_regular_file() { local path="${1:-}" local dir local base local resolved_dir [[ ! -L "$path" ]] || return 1 [[ -f "$path" ]] || return 1 dir="${path%/*}" base="${path##*/}" [[ -n "$dir" && -n "$base" && "$dir" != "$path" ]] || return 1 resolved_dir="$(cd -P -- "$dir" 2>/dev/null && pwd -P)" || return 1 printf "%s/%s\n" "$resolved_dir" "$base" } is_root_owned_directory() { local path="${1:-}" local stat_cmd local uid local mode [[ -d "$path" && ! -L "$path" ]] || return 1 stat_cmd="$(find_cmd /usr/bin/stat /bin/stat)" read -r uid mode < <("$stat_cmd" -Lc '%u %a' -- "$path" 2>/dev/null) || return 1 [[ "$uid" == "0" ]] || return 1 (( (8#$mode & 0022) == 0 )) } is_wlp_package_file() { local path="${1:-}" local resolved is_abs_no_space "$path" || return 1 has_path_traversal "$path" && return 1 [[ "$path" =~ ^/var/lib/oracle-cloud-agent/plugins/oci-wlp/downloads/wlp/[^/]+/[^/]+/[^/]+[.](rpm|deb)$ ]] || return 1 resolved="$(resolve_regular_file "$path")" || return 1 [[ "$resolved" == "$path" ]] || return 1 case "$resolved" in /var/lib/oracle-cloud-agent/plugins/oci-wlp/*) return 0 ;; esac return 1 } is_wlp_service() { [[ "${1:-}" =~ ^wlp-agent(-osqueryd)?(-stable|-unstable)?[.]service$ ]] } is_wlp_key_file() { is_abs_no_space "$1" || return 1 case "$1" in /etc/oracle-cloud-agent/plugins/oci-wlp/rpm-gpg-pub-key|\ /etc/oracle-cloud-agent/plugins/oci-wlp/deb-gpg-pub-key|\ /snap/oracle-cloud-agent/current/plugins/oci-wlp/deb-gpg-pub-key) return 0 ;; esac return 1 } is_vss_appscan_output_file() { local path="${1:-}" local resolved case "$path" in /var/log/oracle-cloud-agent/plugins/oci-vulnerabilityscan/appscan/last-scan-pkginfo-file.json|\ /var/log/oracle-cloud-agent/plugins/oci-vulnerabilityscan/appscan/last-scan-stat.json) ;; *) return 1 ;; esac resolved="$(resolve_regular_file "$path")" || return 1 [[ "$resolved" == "$path" ]] } is_vss_appscan_output_dir_blocking_file() { local path="${1:-}" [[ "$path" == "/var/log/oracle-cloud-agent/plugins/oci-vulnerabilityscan/appscan" ]] || return 1 [[ -f "$path" && ! -L "$path" ]] } is_vss_qualys_wrapper_source() { local path="/var/lib/oracle-cloud-agent/plugins/oci-vulnerabilityscan/qualys/agent_image/QualysAgentWrapper.sh" local resolved resolved="$(resolve_regular_file "$path")" || return 1 [[ "$resolved" == "$path" ]] } is_approved_vss_qualys_wrapper_hash() { case "${1,,}" in 0512f3ce2437828efc10e16d18fe6b5d7a9a4f05cc958e5c60f29c22f001a3ba) return 0 ;; esac return 1 } vss_qualys_wrapper_destination() { case "$0" in /var/usrlocal/libexec/oracle-cloud-agent/ocatools/*) printf '%s\n' /var/usrlocal/libexec/oracle-cloud-agent/plugins/oci-vulnerabilityscan/qualys/QualysAgentWrapper.sh ;; /usr/libexec/oracle-cloud-agent/ocatools/*|\ /snap/oracle-cloud-agent/*/usr/libexec/oracle-cloud-agent/ocatools/*) printf '%s\n' /usr/libexec/oracle-cloud-agent/plugins/oci-vulnerabilityscan/qualys/QualysAgentWrapper.sh ;; *) die "VSS Qualys wrapper helper path is not allowed" ;; esac } run_iscsiadm() { local cmd cmd="$(find_cmd /usr/sbin/iscsiadm /sbin/iscsiadm)" if [[ $# -eq 6 && "$1" == "--mode" && "$2" == "discovery" && "$3" == "--type" && "$4" == "sendtargets" && "$5" == "--portal" ]] && is_portal "$6"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "-m" && "$2" == "session" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 8 && "$1" == "-m" && "$2" == "node" && "$3" == "-o" && "$4" == "new" && "$5" == "-T" && "$7" == "-p" ]] && is_iqn "$6" && is_portal "$8"; then exec "$cmd" "$@" fi if [[ $# -eq 8 && "$1" == "-m" && "$2" == "node" && "$3" == "-o" && "$4" == "delete" && "$5" == "-T" && "$7" == "-p" ]] && is_iqn "$6" && is_portal "$8"; then exec "$cmd" "$@" fi if [[ $# -eq 6 && "$1" == "--mode" && "$2" == "node" && "$3" == "--op" && "$4" == "update" && "$5" == "--name" && "$6" == "node.startup" ]]; then die "missing --value for node.startup update" fi if [[ $# -eq 8 && "$1" == "--mode" && "$2" == "node" && "$3" == "--op" && "$4" == "update" && "$5" == "--name" && "$6" == "node.startup" && "$7" == "--value" && "$8" == "automatic" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 12 && "$1" == "-m" && "$2" == "node" && "$3" == "-T" && "$5" == "-p" && "$7" == "-o" && "$8" == "update" && "$9" == "-n" && "${11}" == "-v" ]] && is_iqn "$4" && is_portal "$6" && is_iscsi_config "${10}" && no_space "${12}"; then exec "$cmd" "$@" fi if [[ $# -eq 7 && "$1" == "-m" && "$2" == "node" && "$3" == "-T" && "$5" == "-p" ]] && is_iqn "$4" && is_portal "$6" && [[ "$7" == "-l" || "$7" == "-u" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 4 && "$1" == "--mode" && ( "$2" == "node" || "$2" == "session" ) && "$3" == "--op" && "$4" == "show" ]]; then exec "$cmd" "$@" fi die "iscsiadm arguments are not allowed" } run_dmsetup() { local cmd cmd="$(find_cmd /usr/sbin/dmsetup /sbin/dmsetup)" if [[ $# -eq 3 && "$1" == "remove" && "$2" == "-f" ]] && is_safe_name "$3"; then exec "$cmd" "$@" fi if [[ $# -eq 4 && "$1" == "message" && "$2" == /dev/mapper/* && "$3" == "0" && "$4" == "fail_if_no_path" ]] && no_space "$2"; then exec "$cmd" "$@" fi die "dmsetup arguments are not allowed" } run_multipath() { local cmd cmd="$(find_cmd /usr/sbin/multipath /sbin/multipath)" if [[ $# -eq 1 && ( "$1" == "-t" || "$1" == "-ll" ) ]]; then exec "$cmd" "$@" fi if [[ $# -eq 2 && ( "$1" == "-f" || "$1" == "-w" ) ]] && is_safe_name "$2"; then exec "$cmd" "$@" fi die "multipath arguments are not allowed" } run_taskset() { local cmd cmd="$(find_cmd /usr/bin/taskset /bin/taskset)" if [[ $# -eq 3 && "$1" == "-cp" ]] && is_cpu_range "$2" && is_uint "$3"; then exec "$cmd" "$@" fi die "taskset arguments are not allowed" } run_tee() { local cmd cmd="$(find_cmd /usr/bin/tee /bin/tee)" [[ $# -eq 1 ]] || die "tee requires exactly one path" is_sysfs_write_path "$1" || die "tee path is not allowed" exec "$cmd" "$1" } run_snap() { local cmd cmd="$(find_cmd /usr/bin/snap)" if [[ $# -eq 3 && "$1" == "refresh" && "$2" == "--hold" && "$3" == "oracle-cloud-agent" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 5 && "$1" == "refresh" && "$2" == "oracle-cloud-agent" && "$3" =~ ^--channel=[0-9]+[.][0-9]+[.]x/stable$ && "$4" == "--classic" && "$5" == "--amend" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 4 && "$1" == "install" && "$2" == "--dangerous" && "$3" == "--classic" ]] && is_agent_snap_package_file "$4"; then exec "$cmd" "$@" fi if [[ $# -eq 3 && "$1" == "remove" && "$2" == "--purge" && "$3" == "oca-plugin-falcon" ]]; then exec "$cmd" "$@" fi die "snap arguments are not allowed" } process_in_cgroup() { local pid="$1" local owner="$2" local line [[ -r "/proc/${pid}/cgroup" ]] || return 1 while IFS= read -r line; do case "$owner" in oca) case "$line" in */oracle-cloud-agent.service|\ */snap.oracle-cloud-agent.oracle-cloud-agent.service|\ */oca.slice/*) return 0 ;; esac ;; um) case "$line" in */unified-monitoring-agent.service) return 0 ;; esac ;; esac done < "/proc/${pid}/cgroup" return 1 } has_trusted_oca_executable() { local pid="$1" local readlink_cmd local stat_cmd local executable local uid local mode readlink_cmd="$(find_cmd /usr/bin/readlink /bin/readlink)" stat_cmd="$(find_cmd /usr/bin/stat /bin/stat)" executable="$("$readlink_cmd" -f -- "/proc/${pid}/exe" 2>/dev/null)" || return 1 case "$executable" in /usr/libexec/oracle-cloud-agent/plugins/*|\ /var/usrlocal/libexec/oracle-cloud-agent/plugins/*|\ /snap/oracle-cloud-agent/*/plugins/*) ;; *) return 1 ;; esac read -r uid mode < <("$stat_cmd" -Lc '%u %a' -- "$executable" 2>/dev/null) || return 1 [[ "$uid" == "0" ]] || return 1 (( (8#$mode & 0022) == 0 )) } is_oca_process() { local pid="$1" process_in_cgroup "$pid" oca || has_trusted_oca_executable "$pid" } is_um_process() { process_in_cgroup "$1" um } run_scoped_kill() { local owner="$1" shift local cmd local signal local scope local pid local target cmd="$(find_cmd /usr/bin/kill /bin/kill)" [[ $# -eq 3 ]] || die "${owner} kill requires signal, scope, and PID" signal="$1" scope="$2" pid="$3" is_kill_signal "$signal" || die "${owner} kill signal is not allowed" is_positive_pid "$pid" || die "${owner} kill PID is not allowed" [[ "$scope" == "process" || "$scope" == "group" ]] || die "${owner} kill scope is not allowed" case "$owner:$scope" in agent:process|agent:group) is_oca_process "$pid" || die "PID does not belong to Oracle Cloud Agent" ;; um:process) is_um_process "$pid" || die "PID does not belong to Unified Monitoring" ;; um:group) # UM terminates both UMA service groups and its own timed systemctl group. is_um_process "$pid" || is_oca_process "$pid" || die "PID does not belong to Unified Monitoring" ;; *) die "${owner} kill scope is not allowed" ;; esac target="$pid" if [[ "$scope" == "group" ]]; then target="-$pid" fi if "$cmd" "$signal" -- "$target"; then return 0 fi # The plugin may exit after its ownership is validated but before the # signal is delivered. A missing process group is already stopped. if [[ "$scope" == "group" ]] && ! "$cmd" -0 -- "$target"; then return 0 fi return 1 } run_um_package_manager() { local tool="$1" shift local cmd case "$tool" in yum) cmd="$(find_cmd /usr/bin/yum /bin/yum)" ;; dnf) cmd="$(find_cmd /usr/bin/dnf /bin/dnf)" ;; *) die "unsupported package manager: ${tool}" ;; esac if [[ $# -eq 4 && "$1" == "--disablerepo=*" && "$3" == "-y" && ( "$2" == "install" || "$2" == "downgrade" ) ]] && is_um_package_file "$4"; then exec "$cmd" "$@" fi if [[ $# -eq 4 && "$1" == "--disablerepo=*" && "$2" == "remove" && "$3" == "-y" ]] && is_package_name "$4"; then exec "$cmd" "$@" fi die "${tool} arguments are not allowed" } run_um_stage_package() { local source="${1:-}" local destination="${2:-}" local destination_dir local cmd local stat_cmd local owner local mode [[ $# -eq 2 ]] || die "UM package staging requires source and destination" case "$source:$destination" in /var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/temp-unified-monitoring.rpm:/var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/packages/temp-unified-monitoring.rpm|\ /var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/temp-unified-monitoring.deb:/var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/packages/temp-unified-monitoring.deb) ;; *) die "UM package staging path is not allowed" ;; esac resolve_regular_file "$source" >/dev/null || die "UM package source is not allowed" stat_cmd="$(find_cmd /usr/bin/stat /bin/stat)" read -r owner mode < <("$stat_cmd" -Lc '%U %a' -- "$source" 2>/dev/null) || die "UM package source metadata is unavailable" [[ "$owner" == "oracle-cloud-agent" || "$owner" == "snap_daemon" ]] || die "UM package source owner is not allowed" (( (8#$mode & 0022) == 0 )) || die "UM package source is writable by group or other" destination_dir="${destination%/*}" is_root_owned_directory "$destination_dir" || die "UM package destination directory is not allowed" [[ ! -L "$destination" ]] || die "UM package destination is not allowed" cmd="$(find_cmd /usr/bin/install /bin/install)" exec "$cmd" -o root -g root -m 0644 -- "$source" "$destination" } run_um_install_evergreen() { local cmd [[ $# -eq 0 ]] || die "Evergreen UMA installation does not accept arguments" cmd="$(find_cmd /usr/bin/cp /bin/cp)" exec "$cmd" -r /var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/opt/. /opt/ } run_updater_package_manager() { local tool="$1" shift local cmd case "$tool" in yum) cmd="$(find_cmd /usr/bin/yum /bin/yum)" ;; dnf) cmd="$(find_cmd /usr/bin/dnf /bin/dnf)" ;; dpkg) cmd="$(find_cmd /usr/bin/dpkg /bin/dpkg)" ;; *) die "unsupported updater package manager: ${tool}" ;; esac if [[ "$tool" == "dpkg" ]]; then if [[ $# -eq 3 && "$1" == "--force-confold" && "$2" == "-i" ]] && is_agent_package_file "$3"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "--purge" ]] && is_package_name "$2"; then exec "$cmd" "$@" fi else if [[ $# -eq 4 && "$1" == "--disablerepo=*" && "$2" == "install" && "$3" == "-y" ]] && is_agent_package_file "$4"; then exec "$cmd" "$@" fi if [[ $# -eq 4 && "$1" == "--disablerepo=*" && "$2" == "remove" && "$3" == "-y" ]] && is_package_name "$4"; then exec "$cmd" "$@" fi fi die "updater ${tool} arguments are not allowed" } run_updater_dpkg_sig() { local cmd cmd="$(find_cmd /usr/bin/dpkg-sig)" if [[ $# -eq 2 && "$1" == "--verify" ]] && is_agent_package_file "$2"; then exec "$cmd" "$@" fi die "updater dpkg-sig arguments are not allowed" } run_multipath_config() { local source="/tmp/multipath.conf" local destination="/etc/multipath.conf" local cmd local stat_cmd local owner local mode [[ $# -eq 0 ]] || die "multipath config does not accept arguments" resolve_regular_file "$source" >/dev/null || die "multipath config source is not allowed" stat_cmd="$(find_cmd /usr/bin/stat /bin/stat)" read -r owner mode < <("$stat_cmd" -Lc '%U %a' -- "$source" 2>/dev/null) || die "multipath config source metadata is unavailable" [[ "$owner" == "oracle-cloud-agent" || "$owner" == "snap_daemon" ]] || die "multipath config source owner is not allowed" (( (8#$mode & 0022) == 0 )) || die "multipath config source is writable by group or other" is_root_owned_directory /etc || die "multipath config destination directory is not allowed" [[ ! -L "$destination" ]] || die "multipath config destination is not allowed" cmd="$(find_cmd /usr/bin/install /bin/install)" exec "$cmd" -o root -g root -m 0644 -- "$source" "$destination" } run_um_apt() { local cmd cmd="$(find_cmd /usr/bin/apt /bin/apt)" if [[ $# -eq 1 && "$1" == "update" ]]; then export DEBIAN_FRONTEND=noninteractive exec "$cmd" "$@" fi if [[ $# -eq 3 && "$1" == "update" && "$2" == "-o" && "$3" == "Dpkg::Lock::Timeout=60" ]]; then export DEBIAN_FRONTEND=noninteractive exec "$cmd" "$@" fi if [[ $# -eq 6 && "$1" == "--allow-downgrades" && "$2" == "install" && "$3" == "-o" && "$4" == "Dpkg::Options::=--force-confold" && "$5" == "-y" ]] && is_um_package_file "$6"; then export DEBIAN_FRONTEND=noninteractive exec "$cmd" "$@" fi die "UM apt arguments are not allowed" } run_um_rpm() { local cmd cmd="$(find_cmd /usr/bin/rpm /bin/rpm)" if [[ $# -eq 2 && "$1" == "--import" ]] && is_abs_no_space "$2"; then case "$2" in /etc/oracle-cloud-agent/plugins/unifiedmonitoring/rpm-gpg-pub-key-centos7|\ /etc/oracle-cloud-agent/plugins/unifiedmonitoring/rpm-gpg-pub-key-centos8|\ /etc/oracle-cloud-agent/plugins/unifiedmonitoring/rpm-gpg-pub-key-rhel9|\ /etc/oracle-cloud-agent/plugins/unifiedmonitoring/rpm-gpg-pub-key-rhel10) exec "$cmd" "$@" ;; esac fi if [[ $# -eq 2 && "$1" == "-e" ]] && [[ "$2" == unified-monitoring-agent* ]] && is_package_name "$2"; then exec "$cmd" "$@" fi die "UM rpm arguments are not allowed" } run_um_gpg() { local cmd cmd="$(find_cmd /usr/bin/gpg /bin/gpg)" if [[ $# -eq 2 && "$1" == "--import" && "$2" == "/etc/oracle-cloud-agent/plugins/unifiedmonitoring/rpm-gpg-pub-key-centos8" ]]; then export DEBIAN_FRONTEND=noninteractive exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "--verify" && "$2" == "/var/lib/oracle-cloud-agent/plugins/unifiedmonitoring/packages/temp-unified-monitoring.deb" ]]; then exec "$cmd" "$@" fi die "UM gpg arguments are not allowed" } run_wlp_systemctl() { local cmd cmd="$(find_cmd /usr/bin/systemctl /bin/systemctl /usr/sbin/systemctl)" if [[ $# -eq 1 && "$1" == "daemon-reload" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 2 ]]; then case "$1" in start|stop|restart|enable|disable|status|is-active|is-enabled) is_wlp_service "$2" || die "WLP service is not allowed" exec "$cmd" "$@" ;; esac fi die "WLP systemctl arguments are not allowed" } run_wlp_rpm() { local cmd cmd="$(find_cmd /usr/bin/rpm /bin/rpm /usr/sbin/rpm)" if [[ $# -eq 2 && "$1" == "--import" ]] && is_wlp_key_file "$2"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "-Kv" ]] && is_wlp_package_file "$2"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "-e" ]] && [[ "$2" == wlp-agent* ]] && is_package_name "$2"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "-q" ]] && is_package_name "$2"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "-Uvh" ]] && is_wlp_package_file "$2"; then exec "$cmd" "$@" fi if [[ $# -eq 3 && "$1" == "-Uvh" && "$2" == "--oldpackage" ]] && is_wlp_package_file "$3"; then exec "$cmd" "$@" fi die "WLP rpm arguments are not allowed" } run_wlp_gpg() { local cmd cmd="$(find_cmd /usr/bin/gpg /bin/gpg /usr/sbin/gpg)" if [[ $# -eq 2 && "$1" == "--import" ]] && is_wlp_key_file "$2"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "--verify" ]] && is_wlp_package_file "$2"; then exec "$cmd" "$@" fi if [[ $# -eq 7 && "$1" == "--batch" && "$2" == "--no-tty" && "$3" == "--no-options" && "$4" == "--status-fd" && "$5" == "1" && "$6" == "--with-colons" && "$7" == "--verify" ]]; then die "missing WLP gpg verify package path" fi if [[ $# -eq 8 && "$1" == "--batch" && "$2" == "--no-tty" && "$3" == "--no-options" && "$4" == "--status-fd" && "$5" == "1" && "$6" == "--with-colons" && "$7" == "--verify" ]] && is_wlp_package_file "$8"; then exec "$cmd" "$@" fi die "WLP gpg arguments are not allowed" } run_wlp_apt_like() { local tool="$1" shift local cmd case "$tool" in apt-get) cmd="$(find_cmd /usr/bin/apt-get /bin/apt-get /usr/sbin/apt-get)" ;; apt) cmd="$(find_cmd /usr/bin/apt /bin/apt /usr/sbin/apt)" ;; *) die "unsupported WLP apt command: ${tool}" ;; esac export DEBIAN_FRONTEND=noninteractive export NEEDRESTART_SUSPEND=1 if [[ $# -eq 1 && "$1" == "update" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 3 && "$1" == "-y" && "$2" == "install" ]] && { is_wlp_package_name "$3" || is_wlp_package_file "$3"; }; then exec "$cmd" "$@" fi if [[ $# -eq 4 && "$1" == "-y" && "$2" == "install" && "$3" == "--allow-downgrades" ]] && is_wlp_package_file "$4"; then exec "$cmd" "$@" fi if [[ $# -eq 3 && "$1" == "-y" && ( "$2" == "remove" || "$2" == "purge" ) ]] && [[ "$3" == wlp-agent* ]] && is_package_name "$3"; then exec "$cmd" "$@" fi if [[ $# -eq 2 && "$1" == "autoremove" && "$2" == "-y" ]]; then exec "$cmd" "$@" fi if [[ $# -eq 3 && "$1" == "-f" && "$2" == "install" && "$3" == "-y" ]]; then exec "$cmd" "$@" fi die "WLP ${tool} arguments are not allowed" } run_wlp_yum_like() { local tool="$1" shift local cmd case "$tool" in yum) cmd="$(find_cmd /usr/bin/yum /bin/yum)" ;; dnf) cmd="$(find_cmd /usr/bin/dnf /bin/dnf)" ;; *) die "unsupported WLP package manager: ${tool}" ;; esac if [[ $# -eq 3 && "$1" == "-y" && "$2" == "install" ]] && { is_wlp_package_name "$3" || is_wlp_package_file "$3"; }; then exec "$cmd" "$@" fi die "WLP ${tool} arguments are not allowed" } run_cpa_yum() { local cmd cmd="$(find_cmd /usr/bin/yum /bin/yum)" if [[ $# -ge 3 && "$1" == "-y" && ( "$2" == "install" || "$2" == "remove" || "$2" == "update" || "$2" == "downgrade" ) ]]; then local verb="$2" shift 2 for package in "$@"; do is_package_name "$package" || die "CPA yum package is not allowed: ${package}" done exec "$cmd" -y "$verb" "$@" fi if [[ $# -ge 3 && ( "$1" == "install" || "$1" == "remove" || "$1" == "update" || "$1" == "downgrade" ) && "$2" == "-y" ]]; then local verb="$1" shift 2 for package in "$@"; do is_package_name "$package" || die "CPA yum package is not allowed: ${package}" done exec "$cmd" "$verb" -y "$@" fi die "CPA yum arguments are not allowed" } run_uc_bump() { local cmd cmd="$(find_cmd /usr/bin/uc)" if [[ $# -eq 2 && ( "$1" == "activate" || "$1" == "deactivate" ) ]] && is_safe_name "$2"; then exec "$cmd" "$@" fi die "uc arguments are not allowed" } run_ln_force() { local cmd cmd="$(find_cmd /usr/bin/ln /bin/ln)" if [[ $# -eq 2 && "$1" == /dev/mapper/* && "$2" == /dev/oracleoci/oraclevd* ]] && no_space "$1" && no_space "$2"; then exec "$cmd" -fs "$1" "$2" fi die "ln-force arguments are not allowed" } # No caller-supplied path or options: the agent only consumes its merged RPM config. run_agent_cleanup_upgrade() { [[ $# -eq 0 ]] || die "agent-cleanup-upgrade does not accept arguments" local path=/etc/oracle-cloud-agent/agent.yml.rpmnew local parent for parent in / /etc /etc/oracle-cloud-agent; do is_root_owned_directory "$parent" || die "agent upgrade parent is not root controlled" done [[ ! -L "$path" ]] || die "agent upgrade config is a symlink" [[ ! -e "$path" || -f "$path" ]] || die "agent upgrade config is not a regular file" exec "$(find_cmd /usr/bin/rm /bin/rm)" -f -- "$path" } run_rm_consistent_device() { local cmd cmd="$(find_cmd /usr/bin/rm /bin/rm)" if [[ $# -eq 1 && "$1" == /dev/oracleoci/oraclevd* ]] && no_space "$1"; then exec "$cmd" -f "$1" fi die "rm-consistent-device arguments are not allowed" } run_vss_appscan_cat_output() { local cmd cmd="$(find_cmd /usr/bin/cat /bin/cat)" [[ $# -eq 1 ]] || die "vss-appscan-cat-output requires one path" is_vss_appscan_output_file "$1" || die "VSS app scan output path is not allowed" exec "$cmd" -- "$1" } run_vss_appscan_rm_output_file() { local cmd cmd="$(find_cmd /usr/bin/rm /bin/rm)" [[ $# -eq 1 ]] || die "vss-appscan-rm-output-file requires one path" is_vss_appscan_output_dir_blocking_file "$1" || die "VSS app scan output directory file is not allowed" exec "$cmd" -f -- "$1" } run_vss_appscan_clean_syft_tmp() { local cmd local -a paths cmd="$(find_cmd /usr/bin/rm /bin/rm)" [[ $# -eq 0 ]] || die "vss-appscan-clean-syft-tmp does not accept arguments" shopt -s nullglob paths=(/tmp/syft-archive-contents*) [[ ${#paths[@]} -gt 0 ]] || return 0 exec "$cmd" -rf -- "${paths[@]}" } run_vss_install_qualys_wrapper() { local cmd local mktemp_cmd local sha_cmd local source="/var/lib/oracle-cloud-agent/plugins/oci-vulnerabilityscan/qualys/agent_image/QualysAgentWrapper.sh" local destination local expected_hash="${1:-}" local staged_source local actual_hash cmd="$(find_cmd /usr/bin/install /bin/install)" mktemp_cmd="$(find_cmd /usr/bin/mktemp /bin/mktemp)" sha_cmd="$(find_cmd /usr/bin/sha256sum /bin/sha256sum)" [[ $# -eq 1 ]] || die "vss-install-qualys-wrapper requires a SHA-256 checksum" is_sha256 "$expected_hash" || die "VSS Qualys wrapper checksum is not allowed" is_vss_qualys_wrapper_source || die "VSS Qualys wrapper source is not allowed" destination="$(vss_qualys_wrapper_destination)" [[ ! -L "$destination" ]] || die "VSS Qualys wrapper destination is not allowed" staged_source="$($mktemp_cmd /tmp/oca-qualys-wrapper.XXXXXX)" || die "failed to create VSS Qualys wrapper staging file" "$cmd" -o root -g root -m 0600 -- "$source" "$staged_source" || { rm -f -- "$staged_source" die "failed to stage VSS Qualys wrapper" } read -r actual_hash _ < <("$sha_cmd" -- "$staged_source" 2>/dev/null) || { rm -f -- "$staged_source" die "failed to hash VSS Qualys wrapper" } [[ "${actual_hash,,}" == "${expected_hash,,}" ]] || { rm -f -- "$staged_source" die "VSS Qualys wrapper checksum does not match" } is_approved_vss_qualys_wrapper_hash "$actual_hash" || { rm -f -- "$staged_source" die "VSS Qualys wrapper checksum is not approved" } "$cmd" -D -o root -g root -m 0755 -- "$staged_source" "$destination" || { rm -f -- "$staged_source" die "failed to install VSS Qualys wrapper" } rm -f -- "$staged_source" } run_vss_chown_qualys_download() { local cmd local path=/var/lib/oracle-cloud-agent/plugins/oci-vulnerabilityscan/qualys/download cmd="$(find_cmd /usr/bin/chown /bin/chown)" [[ $# -eq 0 ]] || die "vss-chown-qualys-download does not accept arguments" [[ -d "$path" && ! -L "$path" ]] || die "VSS Qualys download directory is not allowed" exec "$cmd" -R snap_daemon:snap_daemon -- "$path" } run_vss_chown_qualys_agent_image() { local cmd local path=/var/lib/oracle-cloud-agent/plugins/oci-vulnerabilityscan/qualys/agent_image cmd="$(find_cmd /usr/bin/chown /bin/chown)" [[ $# -eq 0 ]] || die "vss-chown-qualys-agent-image does not accept arguments" [[ -d "$path" && ! -L "$path" ]] || die "VSS Qualys agent image directory is not allowed" exec "$cmd" -R snap_daemon:snap_daemon -- "$path" } authorize_operation() { local operation="$1" local caller="${SUDO_USER:-root}" [[ "$EUID" -eq 0 ]] || die "helper must run as root" if [[ "$caller" == "root" ]]; then return fi case "$caller" in oracle-cloud-agent) case "$operation" in agent-kill|agent-cleanup-upgrade|um-kill|\ iscsiadm|iscsi-node-size|rm-iscsi-node|dmsetup|multipath|taskset|tee|umount|\ multipath-config|\ um-yum|um-dnf|um-apt|um-rpm|um-gpg|\ um-stage-package|um-install-evergreen|\ wlp-systemctl|wlp-rpm|wlp-gpg|wlp-apt-get|wlp-apt|wlp-yum|wlp-dnf|\ ln-force|rm-consistent-device|\ vss-appscan-cat-output|vss-appscan-rm-output-file|vss-appscan-clean-syft-tmp|\ vss-install-qualys-wrapper|vss-chown-qualys-download|vss-chown-qualys-agent-image) return ;; esac ;; snap_daemon) case "$operation" in agent-kill|snap|um-kill|\ iscsiadm|iscsi-node-size|rm-iscsi-node|dmsetup|multipath|taskset|tee|umount|\ multipath-config|\ um-yum|um-dnf|um-apt|um-rpm|um-gpg|\ um-stage-package|\ wlp-systemctl|wlp-rpm|wlp-gpg|wlp-apt-get|wlp-apt|wlp-yum|wlp-dnf|\ ln-force|rm-consistent-device|\ vss-appscan-cat-output|vss-appscan-rm-output-file|vss-appscan-clean-syft-tmp|\ vss-install-qualys-wrapper|vss-chown-qualys-download|vss-chown-qualys-agent-image) return ;; esac ;; oracle-cloud-agent-updater) case "$operation" in updater-yum|updater-dnf|updater-dpkg|updater-dpkg-sig) return ;; esac ;; ocarun) case "$operation" in cpa-yum|uc-bump) return ;; esac ;; esac die "operation ${operation} is not allowed for ${caller}" } helper_name="${0##*/}" case "$helper_name" in oca-privileged-helper) [[ $# -gt 0 ]] || die "operation is required" op="$1" shift ;; oca-privileged-helper-*) op="${helper_name#oca-privileged-helper-}" ;; *) die "helper name is not allowed" ;; esac authorize_operation "$op" case "$op" in iscsiadm) run_iscsiadm "$@" ;; iscsi-node-size) [[ $# -eq 1 ]] || die "iscsi-node-size requires one path" is_iscsi_node_path "$1" || die "iSCSI node path is not allowed" exec "$(find_cmd /usr/bin/stat /bin/stat)" -c%s "$1" ;; rm-iscsi-node) [[ $# -eq 1 ]] || die "rm-iscsi-node requires one path" is_iscsi_node_path "$1" || die "iSCSI node path is not allowed" exec "$(find_cmd /usr/bin/rm /bin/rm)" -f "$1" ;; dmsetup) run_dmsetup "$@" ;; multipath) run_multipath "$@" ;; taskset) run_taskset "$@" ;; tee) run_tee "$@" ;; umount) [[ $# -eq 1 ]] || die "umount requires one mountpoint" is_safe_mountpoint "$1" || die "mountpoint is not allowed" exec "$(find_cmd /usr/bin/umount /bin/umount)" "$1" ;; snap) run_snap "$@" ;; agent-kill) run_scoped_kill agent "$@" ;; agent-cleanup-upgrade) run_agent_cleanup_upgrade "$@" ;; um-kill) run_scoped_kill um "$@" ;; multipath-config) run_multipath_config "$@" ;; updater-yum) run_updater_package_manager yum "$@" ;; updater-dnf) run_updater_package_manager dnf "$@" ;; updater-dpkg) run_updater_package_manager dpkg "$@" ;; updater-dpkg-sig) run_updater_dpkg_sig "$@" ;; um-yum) run_um_package_manager yum "$@" ;; um-dnf) run_um_package_manager dnf "$@" ;; um-apt) run_um_apt "$@" ;; um-rpm) run_um_rpm "$@" ;; um-gpg) run_um_gpg "$@" ;; um-stage-package) run_um_stage_package "$@" ;; um-install-evergreen) run_um_install_evergreen "$@" ;; wlp-systemctl) run_wlp_systemctl "$@" ;; wlp-rpm) run_wlp_rpm "$@" ;; wlp-gpg) run_wlp_gpg "$@" ;; wlp-apt-get) run_wlp_apt_like apt-get "$@" ;; wlp-apt) run_wlp_apt_like apt "$@" ;; wlp-yum) run_wlp_yum_like yum "$@" ;; wlp-dnf) run_wlp_yum_like dnf "$@" ;; cpa-yum) run_cpa_yum "$@" ;; uc-bump) run_uc_bump "$@" ;; ln-force) run_ln_force "$@" ;; rm-consistent-device) run_rm_consistent_device "$@" ;; vss-appscan-cat-output) run_vss_appscan_cat_output "$@" ;; vss-appscan-rm-output-file) run_vss_appscan_rm_output_file "$@" ;; vss-appscan-clean-syft-tmp) run_vss_appscan_clean_syft_tmp "$@" ;; vss-install-qualys-wrapper) run_vss_install_qualys_wrapper "$@" ;; vss-chown-qualys-download) run_vss_chown_qualys_download "$@" ;; vss-chown-qualys-agent-image) run_vss_chown_qualys_agent_image "$@" ;; *) die "operation is not allowed: ${op}" ;; esac