#!/bin/sh

. ../common/ajax_common


get_dot1x_stat()
{
    ${FLOWEYE} dot1x stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


load_dot1x_config()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"radsvr\":`lib_radsvr_list`"
    printf ",\"config\":`get_dot1x_stat`"
    printf "}"
}


set_dot1x_config()
{
    cmdargs="ttl=${CGI_ttl} radsvrid=${CGI_dot1x_radsvrid} maxfail=${CGI_rejmac_maxfail} blocktime=${CGI_rejmac_blocktime}"
    errmsg=`${FLOWEYE} dot1x config ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "dot1x config ${cmdargs}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


list_dot1x_rule()
{
    ${FLOWEYE} dot1x list | grep -i "${CGI_keyword}" | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col=1;

        printf "%s{", dot;
        printf "\"id\":\"%s\"", $(col++);
        printf ",\"if\":\"%s\"", $(col++);
        printf ",\"disable\":\"%s\"", $(col++);
        printf ",\"vlan\":\"%s\"", $(col++);
        printf ",\"desc\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


get_dot1x_rule()
{
    ${FLOWEYE} dot1x get id=${CGI_id} | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


load_addrule_env()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"interface\":`lib_interface_list`"

    if [ "${CGI_id}" != "" ]; then
        printf ",\"rule\":`get_dot1x_rule`"
    fi

    printf "}"
}


add_dot1x_rule()
{
    cmdargs="id=${CGI_id} inif=${CGI_inif} vlan=${CGI_vlan} desc=${CGI_desc}"

    errmsg=`${FLOWEYE} dot1x add ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x add ${cmdargs}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


set_dot1x_rule()
{
    cmdargs="id=${CGI_id} inif=${CGI_inif} vlan=${CGI_vlan} desc=${CGI_desc}"

    errmsg=`${FLOWEYE} dot1x set ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x set ${cmdargs}"
        WEB_LOGGER "༭802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


enable_dot1x_rule()
{
    errmsg=`${FLOWEYE} dot1x enable id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x enable id=${CGI_id}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


disable_dot1x_rule()
{
    errmsg=`${FLOWEYE} dot1x disable id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x disable id=${CGI_id}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


rmv_dot1x_rule()
{
    errmsg=`${FLOWEYE} dot1x remove id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x remove id=${CGI_id}"
        WEB_LOGGER "ɾ802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


bat_enable_dot1x_rule()
{
	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} dot1x enable id=${id}`
		[ $? -ne 0 ] && retjson 1 "ʧ:${errmsg}"
		sync_floweye "route disable id=${id}"
	done

	WEB_LOGGER "802.1x֤" "ids=${CGI_items}"
	retjson 0 "ɹ"
}


bat_disable_dot1x_rule()
{
    for id in `echo ${CGI_items} | tr ";" " "`
    do
        errmsg=`${FLOWEYE} dot1x disable id=${id}`
        [ $? -ne 0 ] && retjson 1 "ʧ:${errmsg}"
        sync_floweye "dot1x disable id=${id}"
    done

    WEB_LOGGER "802.1x֤" "ids=${CGI_items}"
    retjson 0 "ɹ"
}


bat_rmv_dot1x_rule()
{
    for id in `echo ${CGI_items} | tr ";" " "`
    do
        errmsg=`${FLOWEYE} dot1x remove id=${id}`
        [ $? -ne 0 ] && retjson 1 "ʧ:${errmsg}"
        sync_floweye "dot1x remove id=${id}"
    done

    WEB_LOGGER "ɾ802.1x֤" "ids=${CGI_items}"
    retjson 0 "ɹ"
}

list_dot1x_client()
{
    [ "${CGI_limit}" = "" ] && CGI_limit=100
    [ "${CGI_page}"  = "" ] && CGI_page=1
    end=$(( ${CGI_page} * ${CGI_limit} ))
    start=$(( ${end} - ${CGI_limit} ))

    ${FLOWEYE} dot1x list client=1 | grep -i "${CGI_keyword}" | awk -F" " \
	-v start=${start} -v end=${end} \
    'BEGIN{
        row = 0;
        dot = "";
        printf "{\"list\":[";
    }{
        row++;
        if( row > end  || row <= start ) next;

        col=1;

        printf "%s{", dot;
        printf "\"id\":\"%s\"", $(col++);
        printf ",\"name\":\"%s\"", $(col++);
        printf ",\"mac\":\"%s\"", $(col++);
        printf ",\"stat\":\"%s\"", $(col++);
        printf ",\"ipv4\":\"%s\"", $(col++);
        printf ",\"ipv6\":\"%s\"", $(col++);
        printf ",\"if\":\"%s\"", $(col++);
        printf ",\"vlan\":\"%s\"", $(col++);
        printf ",\"active_time\":\"%s\"", $(col++);
        printf ",\"create_time\":\"%s\"", $(col++);
        printf ",\"count\":\"%s\"", $(col++);
        printf ",\"auth_fails\":\"%s\"", $(col++);
        printf ",\"auth_fail_time\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "],\"total\":\"%s\"}", row;
    }'
}


list_dot1x_mac()
{
    printf "{"

    ${FLOWEYE} macdesc list | awk \
    'BEGIN{
        dot = "";
        printf "\"desc\":{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $4;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'

    ${FLOWEYE} dot1x_stamac list | awk \
    'BEGIN{
        dot = "";
        printf ",\"dot1x\":[";
    }{
        printf "%s\"%s\"", dot, $2;
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'

    printf "}"
}


add_dot1x_mac()
{
    errmsg=`${FLOWEYE} dot1x_stamac add mac=${CGI_mac}`
    [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
    sync_floweye "dot1x_stamac add mac=${CGI_mac}"

    if [ "${CGI_desc}" != "" ]; then
        errmsg=`${FLOWEYE} macdesc set m=${CGI_mac} d=${CGI_desc}`
        [ $? -ne 0 ] && retjson 1 "עʧܣ${errmsg}"
        sync_floweye "macdesc set m=${CGI_mac} d=${CGI_desc}"
    fi
    
    WEB_LOGGER "802.1x׼MAC" "mac=${CGI_mac} note=${CGI_desc}"

    retjson 0 "ɹ"
}


edit_dot1x_mac()
{
    [ "${CGI_desc}" = "" ] && CGI_desc="NULL"

	errmsg=`${FLOWEYE} macdesc set m=${CGI_mac} d=${CGI_desc}`
    [ $? -ne 0 ] && retjson 1 "עʧܣ${errmsg}"
    sync_floweye "macdesc set m=${CGI_mac} d=${CGI_desc}"

    WEB_LOGGER "޸802.1x׼MAC" "mac=${CGI_mac} note=${CGI_desc}"

    retjson 0 "ɹ"
}


rmv_dot1x_mac()
{
    errmsg=`${FLOWEYE} dot1x_stamac remove mac=${CGI_mac}`
    [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
    sync_floweye "dot1x_stamac remove mac=${CGI_mac}"

	errmsg=`${FLOWEYE} macdesc set m=${CGI_mac} d=NULL`
    [ $? -ne 0 ] && retjson 1 "עʧܣ${errmsg}"
    sync_floweye "macdesc set m=${CGI_mac} d=NULL"

    WEB_LOGGER "ɾ802.1x׼MAC" "mac=${CGI_mac} note=${CGI_desc}"

    retjson 0 "ɹ"
}


bat_rmv_dot1x_mac()
{
    for mac in ${CGI_items}
    do
        errmsg=`${FLOWEYE} dot1x_stamac remove mac=${mac}`
        [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        sync_floweye "dot1x_stamac remove mac=${mac}"

        errmsg=`${FLOWEYE} macdesc set m=${mac} d=NULL`
        [ $? -ne 0 ] && retjson 1 "עʧܣ${errmsg}"
        sync_floweye "macdesc set m=${mac} d=NULL"
    done

    WEB_LOGGER "ɾ802.1x׼MAC"

    retjson 0 "ɹ"
}


import_dot1x_mac()
{
    [ ! -f ${CGI_file} ] && retjson 1 "NO_FILE"

    cat ${CGI_file} | tr -ds '\r' '' | while read mac desc
    do
        errmsg=`${FLOWEYE} dot1x_stamac add mac=${mac}`
        [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
        sync_floweye "dot1x_stamac add mac=${mac}"

        if [ "${desc}" != "" ]; then
            errmsg=`${FLOWEYE} macdesc set m=${mac} d=${desc}`
            [ $? -ne 0 ] && retjson 1 "עʧܣ${errmsg}"
            sync_floweye "macdesc set m=${mac} d=${desc}"
        fi
    done

    WEB_LOGGER "802.1x׼MAC"

    retjson 0 "ɹ"
}


export_dot1x_mac()
{
    file_name="802.1x_mac.conf"
    file_path="${WEB_DOWNLOAD}/${file_name}"

    mkdir -p ${WEB_DOWNLOAD}

    ${FLOWEYE} dot1x_stamac list | while read num mac
    do
        desc=`${FLOWEYE} macdesc get m=${mac}`

        if [ "${desc}" != "NEXIST" ]; then
            echo "${mac} ${desc}" >> ${file_path}
        else
            echo "${mac}" >> ${file_path}
        fi
    done

    retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
}


load_dot1x_mac_stat()
{
    ${FLOWEYE} dot1x_stamac stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


set_dot1x_mac_stat()
{
    errmsg=`${FLOWEYE} dot1x_stamac config mode=${CGI_mode}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "dot1x_stamac config mode=${CGI_mode}"
        WEB_LOGGER "޸dot1x׼MACģʽ" "mode=${CGI_mode}"
        retjson 0 "ɹ"
    fi
}


case "${CGI_action}" in

    "load_dot1x_config")
        retjson 0 "OK" "`load_dot1x_config`"
        ;;

    "set_dot1x_config")
        action_check
        set_dot1x_config
        ;;

    "list_dot1x_rule")
        retjson 0 "OK" "`list_dot1x_rule`"
        ;;

    "load_addrule_env")
        retjson 0 "OK" "`load_addrule_env`"
        ;;

    "add_dot1x_rule")
        action_check
        add_dot1x_rule
        ;;

    "set_dot1x_rule")
        action_check
        set_dot1x_rule
        ;;

    "enable_dot1x_rule")
        action_check
        enable_dot1x_rule
        ;;

    "disable_dot1x_rule")
        action_check
        disable_dot1x_rule
        ;;

    "rmv_dot1x_rule")
        action_check
        rmv_dot1x_rule
        ;;

    "bat_enable_dot1x_rule")
        action_check
        bat_enable_dot1x_rule
        ;;

    "bat_disable_dot1x_rule")
        action_check
        bat_disable_dot1x_rule
        ;;

    "bat_rmv_dot1x_rule")
        action_check
        bat_rmv_dot1x_rule
        ;;

    "list_dot1x_client")
        retjson 0 "OK" "`list_dot1x_client`"
        ;;
        
    "list_dot1x_mac")
        retjson 0 "OK" "`list_dot1x_mac`"
        ;;

    "add_dot1x_mac")
        action_check
        add_dot1x_mac
        ;;

    "edit_dot1x_mac")
        action_check
        edit_dot1x_mac
        ;;

    "rmv_dot1x_mac")
        action_check
        rmv_dot1x_mac
        ;;

    "bat_rmv_dot1x_mac")
        action_check
        bat_rmv_dot1x_mac
        ;;

    "import_dot1x_mac")
        action_check
        import_dot1x_mac
        ;;

    "export_dot1x_mac")
        action_check
        export_dot1x_mac
        ;;

    "load_dot1x_mac_stat")
        retjson 0 "OK" "`load_dot1x_mac_stat`"
        ;;

    "set_dot1x_mac_stat")
        action_check
        set_dot1x_mac_stat
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
        
esac
