# -*-eselect-*- vim: ft=eselect # Copyright 2005-2011 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 or later # $ DESCRIPTION="Manage the /usr/lib/metasploit" MAINTAINER="zerochaos@pentoo.ch" SVN_DATE='$Date: 2011-09-25 15:27:36 +0200 (Sun, 25 Sep 2011) $' VERSION=$(svn_date_to_version "${SVN_DATE}") # sort function for metasploit versions, to be used in a pipe sort_metasploit_versions() { local vsort="sort --version-sort" # Test if our sort supports the --version-sort option # (should be GNU sort, since the metasploit module is GNU/Linux specific) ${vsort} /dev/null || vsort=sort # We sort metasploit versions as follows: # 1. Run sed to prepend the version string by the numeric version # and an additional rank indicator that is 0 for release candidates # or 1 otherwise. After this step we have, for example: # 2.6.29 1 linux-2.6.29 # 2.6.29 0 linux-2.6.29-rc8 # 2. sort --version-sort # 3. Run sed again to remove the prepended keys from step 1. sed -e 's/^\(metasploit-\)\?\([[:digit:].]\+\)[-_]rc/\2 0 &/' \ -e 't;s/^\(linux-\)\?\([[:digit:].]\+\)/\2 1 &/' \ | LC_ALL=C ${vsort} | sed 's/.* //' } # find a list of metasploit symlink targets find_targets() { local f for f in "${EROOT}"/usr/lib/metasploit-[[:digit:]]*; do [[ -d ${f} ]] && basename "${f}" done | sort_metasploit_versions } # remove the metasploit symlink remove_symlink() { rm "${EROOT}/usr/lib/metasploit" } # set the metasploit symlink set_symlink() { local target=$1 if is_number "${target}"; then local targets=( $(find_targets) ) target=${targets[target-1]} fi if [[ -z ${target} ]]; then die -q "Target \"$1\" doesn't appear to be valid!" elif [[ -d ${EROOT}/usr/lib/${target} ]]; then ln -s "${target}" "${EROOT}/usr/lib/metasploit" elif [[ -d ${EROOT}/usr/lib/metasploit-${target} ]]; then ln -s "linux-${target}" "${EROOT}/usr/lib/metasploit" else die -q "Target \"$1\" doesn't appear to be valid!" fi } ### show action ### describe_show() { echo "Show the current metasploit symlink" } do_show() { write_list_start "Current metasploit symlink:" if [[ -L ${EROOT}/usr/lib/metasploit ]]; then local metasploit=$(canonicalise "${EROOT}/usr/lib/metasploit") write_kv_list_entry "${metasploit%/}" "" else write_kv_list_entry "(unset)" "" fi } ### list action ### describe_list() { echo "List available metasploit symlink targets" } do_list() { local i targets=( $(find_targets) ) write_list_start "Available metasploit symlink targets:" for (( i = 0; i < ${#targets[@]}; i++ )); do [[ ${targets[i]} = \ $(basename "$(canonicalise "${EROOT}/usr/lib/metasploit")") ]] \ && targets[i]=$(highlight_marker "${targets[i]}") done write_numbered_list -m "(none found)" "${targets[@]}" } ### set action ### describe_set() { echo "Set a new metasploit symlink target" } describe_set_parameters() { echo "" } describe_set_options() { echo "target : Target name or number (from 'list' action)" } do_set() { [[ -z $1 ]] && die -q "You didn't tell me what to set the symlink to" [[ $# -gt 1 ]] && die -q "Too many parameters" if [[ -L ${EROOT}/usr/lib/metasploit ]]; then # existing symlink remove_symlink || die -q "Couldn't remove existing symlink" set_symlink "$1" || die -q "Couldn't set a new symlink" elif [[ -e ${EROOT}/usr/lib/metasploit ]]; then # we have something strange die -q "${EROOT}/usr/lib/metasploit exists but is not a symlink" else set_symlink "$1" || die -q "Couldn't set a new symlink" fi }