#!/usr/bin/env bash
################################################################################
#                ____                     _ __                                 #
#     ___  __ __/ / /__ ___ ______ ______(_) /___ __                           #
#    / _ \/ // / / (_-</ -_) __/ // / __/ / __/ // /                           #
#   /_//_/\_,_/_/_/___/\__/\__/\_,_/_/ /_/\__/\_, /                            #
#                                            /___/ team                        #
#                                                                              #
# wnmap - modular nmap wrapper and automation script                           #
#                                                                              #
# FILE                                                                         #
# wnmap                                                                        #
#                                                                              #
# DATE                                                                         #
# 2013-06-19                                                                   #
#                                                                              #
# DESCRIPTION                                                                  #
# A shell script written with the purpose to automate and chain scans via nmap.#
# You can run nmap with a custom mode written by user and create directories   #
# for every mode with the xml/nmap files inside.                               #
#                                                                              #
# AUTHOR                                                                       #
# nrz@nullsecurity.net                                                         #
#                                                                              #
################################################################################

# !!! CHANGE THIS !!!
WNMAP_PATH="/usr/share/wnmap"

# source in files
source_files()
{
    . "${WNMAP_PATH}/src/core/globals.sh"
    . "${WNMAP_PATH}/src/core/wnmap.conf"
    . "${WNMAP_PATH}/src/core/checks.sh"
    . "${WNMAP_PATH}/src/core/getopt.sh"
    . "${WNMAP_PATH}/src/core/error.sh"
    . "${WNMAP_PATH}/src/core/help.sh"
    . "${WNMAP_PATH}/src/modules/scan.sh"
    . "${WNMAP_PATH}/src/modules/add_scan.sh"

    return "${SUCCESS}"
}


# controller and program flow
main()
{

    # check if WNMAP_PATH is set correctly
    if [ ! -d "${WNMAP_PATH}" ]
    then
        printf "%s\n" "[-] ERROR: adjust 'WNMAP_PATH' in src/core/wnmap first"
        exit 31337
    fi
    
    source_files
    check_soft
    check_argc ${*}
    get_opts ${*}
    check_args ${*}
   
    case "${opt_mode}" in
        "add_scan")
            add_scan ${*}
            ;;
        "user_scan")
            scan "${nmap_args}" "${d_scan}" "${target}"
            ;;
        "chain_scan")
            while read -r; do
                [[ "${REPLY:0:1}" == "#" ]] || [ -z "${REPLY}" ] && continue
                mode="${REPLY:0:2}"
                "${WNMAP_PATH}/${APP_NAME}" "${mode}" "${target}" "${REDO+-r}"
            done < "${MODE_FILE}"
            ;;
        "edit")
            "${EDITOR}" "${MODE_FILE}"
            ;;
        *)
            usage
    esac
    return "${SUCCESS}"
}


# program start
main ${*}

# EOF
