/*
 *  Copyright (c) 1996-2007 Digi International Inc., All Rights Reserved
 *
 *  This software contains proprietary and confidential information of Digi
 *  International Inc.  By accepting transfer of this copy, Recipient agrees
 *  to retain this software in confidence, to prevent disclosure to others,
 *  and to make no use of this software other than that for which it was
 *  delivered.  This is an unpublished copyrighted work of Digi International
 *  Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 *  prohibited.
 *
 *  Restricted Rights Legend
 *
 *  Use, duplication, or disclosure by the Government is subject to
 *  restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
 *  Technical Data and Computer Software clause at DFARS 252.227-7031 or
 *  subparagraphs (c)(1) and (2) of the Commercial Computer Software -
 *  Restricted Rights at 48 CFR 52.227-19, as applicable.
 *
 *  Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 *
 * =======================================================================
 * This file contains the security functions.  
 *
 *
 * Edit Date/Ver   Edit Description
 * ==============  ===================================================
 *	DXL	08/21/02	Primary and secondary server will use default if 
 *						no IP address stored in memory.
 *  JZW 05/20/03    Include narmapi.h to get NAInet_toa
 *  JZW 05/23/05    remove kerberos references
 */

#include <stdio.h>
#include <string.h>
#include <appconf_api.h>
#include <netoserr.h>
#include <boardParams.h>
#include <sysAccess.h>
#include <NASecurity.h>
#include "narmapi.h"

/*
 *
 *  Function: char *NASecGetSystemFixedKey (void)
 *
 *  Description:
 *
 *      This routine return the key for the gateway.
 *
 *  Parameters:
 *
 *      None
 *
 *  Return Values:
 *
 *	    key to gateway
 *		NULL
 *
 */

char *NASecGetSystemFixedKey (void)
{
    static char Key[NASYSACC_STRLEN_PASSWORD];

    /*  Security Gateway no longer supported  */
    return Key;
}


/*
 *
 *  Function: char *NAGetSntpPrimaryServer (void)
 *
 *  Description:
 *
 *      This routine return the IP Address of SNTP Primary Server
 *
 *  Parameters:
 *
 *      None
 *
 *  Return Values:
 *
 *	     An IP Address
 *
 */
char *NAGetSntpPrimaryServer (void)
{
    static char ipAddr1[16];
    char *valueStr;
    devBoardParamsType *params;
    int result;

    if (NAGetAppUseNetworkTimeProtocol ())
    {
        params = (devBoardParamsType *)malloc(sizeof(devBoardParamsType));
        if (params != NULL)
        {
            result = customizeReadDevBoardParams (params);             /* read params in NVRAM*/
            if (result == 0)
            {
                if (params->ipAddrSntp1 == 0)
                {
                    if ((valueStr = NAGetAppSntpPrimary ()))
                    {
                        free (params);
                        return (valueStr);
                    }
                }
                else
                {
                    NAInet_toa (params->ipAddrSntp1, ipAddr1);
                    free (params);
                    return (ipAddr1);
                }
            }

            free (params);
        }
    }

    return ("0.0.0.0");
}

/*
 *
 *  Function: char *NAGetSntpSecondaryServer (void)
 *
 *  Description:
 *
 *      This routine return the IP Address of SNTP Secondary Server
 *
 *  Parameters:
 *
 *      None
 *
 *  Return Values:
 *
 *	     An IP Address
 *
 */

char *NAGetSntpSecondaryServer (void)
{
    static char ipAddr2[16];
    char *valueStr;
    devBoardParamsType *params;
    int result;

    if (NAGetAppUseNetworkTimeProtocol ())
    {
        params = (devBoardParamsType *)malloc(sizeof(devBoardParamsType));
        if (params != NULL)
        {
            result = customizeReadDevBoardParams (params);             /* read params in NVRAM*/
            if (result == 0)
            {
                if (params->ipAddrSntp2 == 0)
                {
                    if ((valueStr = NAGetAppSntpSecondary ()))
                    {
                        free (params);
                        return (valueStr);
                    }
                }
                else
                {
                    NAInet_toa (params->ipAddrSntp2, ipAddr2);
                    free (params);
                    return (ipAddr2);
                }
            }

            free (params);
        }
    }

    return ("0.0.0.0");
}

/*
 *
 *  Function:	int NAKrb5GetConfigParameters (struct NAFUTUSEParams *pAppStruct)
 *
 *  Description:
 *
 *      This routine return the KRB5 configuration setting from memory
 *
 *  Parameters:
 *
 *      pAppStruct
 *
 *  Return Values:
 *
 *		0
 *	    NASEC_INVALID_ARGUMENT
 *		NASEC_SYSTEM_FAILURE
 *
 */

int NAKrb5GetConfigParameters (struct NAFUTUSEParams *pAppStruct)
{
    devBoardParamsType *params;
    int result;

    if (pAppStruct == NULL)
    {
        return (NASEC_INVALID_ARGUMENT);
    }

    params = (devBoardParamsType *)malloc(sizeof(devBoardParamsType));
    if (params == NULL)
    {
        return -1;
    }

    result = customizeReadDevBoardParams (params);             /* read params in NVRAM*/
    if (result != 0)
    {
        free (params);
        return (NASEC_SYSTEM_FAILURE);
    }

    memset (pAppStruct, 0, sizeof(struct NAFUTUSEParams));
    if ((strlen(params->resvfieldone) < NASEC_MAX_FUTUSE_USERNAME_LENGTH) &&
        (strlen(params->resvfieldtwo) < NASEC_MAX_FUTUSE_PASSWORD_LENGTH) &&
        (strlen(params->resvfieldfour) < NASEC_MAX_FUTUSE_REALM_NAME_LENGTH) &&
        (params->resvfieldthree != 0))
    {
        strcpy (pAppStruct->username, params->resvfieldone);
        strcpy (pAppStruct->password, params->resvfieldtwo);
        strcpy (pAppStruct->realm, params->resvfieldfour);
        NAInet_toa (params->resvfieldthree, &pAppStruct->KDCIpAddr[0]);
    }

    free (params);
    return 0;
}
