
/******************************************************************************
**
**  This program is free software; you can redistribute it and/or
**  modify it, however, you cannot sell it.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**
**  You should have received a copy of the license attached to the
**  use of this software.  If not, visit www.shmoo.com/osiris for
**  details.
**
******************************************************************************/

/*****************************************************************************
**
**  File:    error.h
**  Date:    June 2, 2002
**
**  Author:  Brian Wotring
**  Purpose: central error reference for all osiris applications.
**
******************************************************************************/

#ifndef ERROR_H_
#define ERROR_H_


#define MAX_ERROR_MESSAGE_LENGTH 	512


/* message error codes. */

#define OSI_ERROR_UNKNOWN		   -1L
#define OSI_OK						1L

#define MESSAGE_OK					OSI_OK
#define OSI_DB_OK					OSI_OK
#define OSI_FILE_OK					OSI_OK

#define OSI_ERROR_NULL_ARGUMENT				2L

#define OSI_ERROR_SOCKET_CLOSED		 		10L
#define OSI_ERROR_UNABLE_TO_CONNECT        	11L
#define OSI_ERROR_NO_RESPONSE	        	12L
#define OSI_ERROR_INVALID_RESPONSE			13L

#define OSI_ERROR_SCAN_START_FAILED			20L
#define OSI_ERROR_NO_CONFIG			    	21L
#define OSI_ERROR_DAEMON_NOT_IDLE	       	22L
#define OSI_ERROR_NO_SCANNING_PROCESS      	23L
#define OSI_ERROR_NO_PRIVLEDGES				24L

#define OSI_ERROR_READ_HOSTS_FAILED	       	30L
#define OSI_ERROR_NO_HOST_SPECIFIED	       	31L
#define OSI_ERROR_NO_CONFIG_SPECIFIED		32L
#define OSI_ERROR_NO_DB_SPECIFIED			33L
#define OSI_ERROR_NO_CONFIGS		       	34L
#define OSI_ERROR_NO_HOSTS                 	35L
#define OSI_ERROR_HOST_DOES_NOT_EXIST		36L
#define OSI_ERROR_CONFIG_DOES_NOT_EXIST		37L
#define OSI_ERROR_DATABASE_DOES_NOT_EXIST	38L
#define OSI_ERROR_CONFIG_IS_EMPTY			39L
#define OSI_ERROR_HOST_EXISTS				40L
#define OSI_ERROR_CONFIG_EXISTS				41L
#define OSI_ERROR_NO_FILTERS                42L
#define OSI_ERROR_READ_FILTERS_FAILED       43L


#define OSI_ERROR_MESSAGE_READ				50L
#define OSI_ERROR_MESSAGE_HEADER_TOO_SHORT	51L
#define OSI_ERROR_MESSAGE_NO_DATA			52L
#define OSI_ERROR_MESSAGE_PAYLOAD_TOO_LARGE	53L
#define OSI_ERROR_MESSAGE_PAYLOAD_TOO_SHORT	54L
#define OSI_ERROR_SETTING_MESSAGE_PAYLOAD	55L
#define OSI_ERROR_SENDING_MESSAGE			56L
#define OSI_ERROR_UNKNOWN_MESSAGE_TYPE		57L

#define OSI_ERROR_DB_INIT_FAILED			60L
#define OSI_ERROR_DB_CREATE_FAILED			61L
#define OSI_ERROR_DB_OPEN_FAILED			62L
#define OSI_ERROR_DB_NOT_OPENED				63L
#define OSI_ERROR_DB_STORING				64L
#define OSI_ERROR_DB_PATH				    65L
#define OSI_ERROR_DB_RETRIEVING				66L
#define OSI_ERROR_DB_DATA_TOO_LARGE			67L
#define OSI_ERROR_DB_INVALID_CURSOR			68L
#define OSI_ERROR_DB_LAST_ITEM				69L
#define OSI_ERROR_DB_ITEM_EXISTS			70L
#define OSI_ERROR_DB_NO_ERRORS				71L
#define OSI_ERROR_DB_ITEM_NOT_FOUND         72L
#define OSI_ERROR_DB_NO_FILTERS             73L
#define OSI_ERROR_BASE_DB_NOT_FOUND         74L

#define OSI_ERROR_FILE_NOT_FOUND			80L
#define OSI_ERROR_PATH_NOT_FOUND			81L
#define OSI_ERROR_ACCESS_DENIED           	82L
#define OSI_ERROR_WRITE_PROTECTED          	83L
#define OSI_ERROR_SHARING_VIOLATION        	84L
#define OSI_ERROR_DIRECTORY_NOT_EMPTY      	85L
#define OSI_ERROR_DISK_FULL                	86L
#define OSI_ERROR_INVALID_PATH             	87L
#define OSI_ERROR_IO_ERROR                	88L
#define OSI_ERROR_FILE_EXISTS             	89L
#define OSI_ERROR_UNABLE_TO_CREATE_FILE		90L

#define OSI_ERROR_UNABLE_TO_OPEN_DIRECTORY	100L
#define OSI_ERROR_UNABLE_TO_STAT_FILE		101L
#define OSI_ERROR_UNABLE_TO_READ_FILE		102L
#define OSI_ERROR_UNABLE_TO_WRITE_FILE  	103L

#define OSI_ERROR_UNABLE_TO_CREATE_PROCESS	110L

#define OSI_ERROR_SCANNER_WAS_KILLED		121L
#define OSI_ERROR_SCANNER_WAS_STOPPED		122L

#define OSI_ERROR_SESSION_KEY_NEGOTIATION_FAILED	130L

#define OSI_ERROR_NO_USER_LOGIN_SPECIFIED           140L
#define OSI_ERROR_SET_USER_LOGIN_FAILED             141L
#define OSI_ERROR_READ_USERS_FAILED                 142L
#define OSI_ERROR_NO_USERS                          143L
#define OSI_ERROR_USER_DOES_NOT_EXIST               144L
#define OSI_ERROR_DELETE_USER_FAILED                145L


typedef struct OSI_ERROR
{
    osi_uint64 type;
    osi_uint64 time;

    char message[MAX_ERROR_MESSAGE_LENGTH];

} OSI_ERROR;


/* used for quick return of string message based upon error code. */

struct e_keywords
{
    char *word;
    int  type;
};

static struct e_keywords error_keywords[] =
{
    { "unknown error occured.", OSI_ERROR_UNKNOWN },

    { "no error", OSI_OK },
    { "no error.", MESSAGE_OK },
    { "no error.", OSI_DB_OK },
    { "no error.", OSI_FILE_OK },

    { "null argument.", OSI_ERROR_NULL_ARGUMENT },

    { "error reading message.", OSI_ERROR_MESSAGE_READ },
    { "message header too short.", OSI_ERROR_MESSAGE_HEADER_TOO_SHORT },
    { "no data in message.", OSI_ERROR_MESSAGE_NO_DATA },
    { "message payload too large.", OSI_ERROR_MESSAGE_PAYLOAD_TOO_LARGE },
    { "message payload too short.", OSI_ERROR_MESSAGE_PAYLOAD_TOO_SHORT },
    { "error setting message payload.", OSI_ERROR_SETTING_MESSAGE_PAYLOAD },
    { "error sending message.", OSI_ERROR_SENDING_MESSAGE },

    { "socket was closed.", OSI_ERROR_SOCKET_CLOSED },
    { "unable to connect to host.", OSI_ERROR_UNABLE_TO_CONNECT },
    { "no response from host.", OSI_ERROR_NO_RESPONSE },
    { "invalid response.", OSI_ERROR_INVALID_RESPONSE },

    { "start scan failed.", OSI_ERROR_SCAN_START_FAILED },
    { "no config present.", OSI_ERROR_NO_CONFIG },
    { "daemon not idle.", OSI_ERROR_DAEMON_NOT_IDLE },
    { "no scanning process exists.", OSI_ERROR_NO_SCANNING_PROCESS },

    { "no privleges.", OSI_ERROR_NO_PRIVLEDGES },

    { "unable to read host listing.", OSI_ERROR_READ_HOSTS_FAILED },
    { "no host specified.", OSI_ERROR_NO_HOST_SPECIFIED },
    { "no config specified.", OSI_ERROR_NO_CONFIG_SPECIFIED },
    { "no database specified.", OSI_ERROR_NO_DB_SPECIFIED },
    { "no filters", OSI_ERROR_NO_FILTERS },
    { "unable to read notification filters.", OSI_ERROR_READ_FILTERS_FAILED },

    { "no hosts.", OSI_ERROR_NO_HOSTS },

    { "no configs exist for host.", OSI_ERROR_NO_CONFIGS },
    { "specified host doesn't exist.", OSI_ERROR_HOST_DOES_NOT_EXIST },
    { "specified config doesn't exist.", OSI_ERROR_CONFIG_DOES_NOT_EXIST },
    { "specified database doesn't exist.", OSI_ERROR_DATABASE_DOES_NOT_EXIST },

    { "config is empty.", OSI_ERROR_CONFIG_IS_EMPTY },
    { "host exists.", OSI_ERROR_HOST_EXISTS },
    { "config exists.", OSI_ERROR_CONFIG_EXISTS },

    { "unknown message type.", OSI_ERROR_UNKNOWN_MESSAGE_TYPE },
    { "unable to initialize database.", OSI_ERROR_DB_INIT_FAILED },
    { "unable to create database.", OSI_ERROR_DB_CREATE_FAILED },
    { "unable to open database.", OSI_ERROR_DB_OPEN_FAILED },
    { "database not opened.", OSI_ERROR_DB_NOT_OPENED },
    { "database write error.", OSI_ERROR_DB_STORING },
    { "invalid database path.", OSI_ERROR_DB_PATH },
    { "database read error.", OSI_ERROR_DB_RETRIEVING },
    { "data too large to store.", OSI_ERROR_DB_DATA_TOO_LARGE },
    { "database cursor error.", OSI_ERROR_DB_DATA_TOO_LARGE },
    { "last item.", OSI_ERROR_DB_LAST_ITEM },
    { "item exists.", OSI_ERROR_DB_ITEM_EXISTS },
    { "no error records exist.", OSI_ERROR_DB_NO_ERRORS },
    { "no filters exist.", OSI_ERROR_DB_NO_FILTERS },
    { "database item not found.", OSI_ERROR_DB_ITEM_NOT_FOUND },
    { "no base-db exists, host not initialized?", OSI_ERROR_BASE_DB_NOT_FOUND },
    { "file not found.", OSI_ERROR_FILE_NOT_FOUND },
    { "path not found.", OSI_ERROR_PATH_NOT_FOUND },
    { "access denied.", OSI_ERROR_ACCESS_DENIED },
    { "write protected.", OSI_ERROR_WRITE_PROTECTED },
    { "sharing violation.", OSI_ERROR_SHARING_VIOLATION },
    { "directory not empty.", OSI_ERROR_DIRECTORY_NOT_EMPTY },
    { "disk full!", OSI_ERROR_DISK_FULL },
    { "invalid path", OSI_ERROR_INVALID_PATH },
    { "i/o error.", OSI_ERROR_IO_ERROR },
    { "file exists.", OSI_ERROR_FILE_EXISTS },
    { "unable to create file.", OSI_ERROR_UNABLE_TO_CREATE_FILE },
    { "unable to open directory.", OSI_ERROR_UNABLE_TO_OPEN_DIRECTORY },
    { "unable to stat file.", OSI_ERROR_UNABLE_TO_STAT_FILE },
    { "unable to read file.", OSI_ERROR_UNABLE_TO_READ_FILE },
    { "unable to write file.", OSI_ERROR_UNABLE_TO_WRITE_FILE },

    { "unable to create process.", OSI_ERROR_UNABLE_TO_CREATE_PROCESS },
    { "scanner was killed.", OSI_ERROR_SCANNER_WAS_KILLED },
    { "scanner was stopped.", OSI_ERROR_SCANNER_WAS_STOPPED },
    { "session key negotiation failed.",
                        OSI_ERROR_SESSION_KEY_NEGOTIATION_FAILED },
    { "no user login specified.", OSI_ERROR_NO_USER_LOGIN_SPECIFIED },
    { "invalid user login.", OSI_ERROR_SET_USER_LOGIN_FAILED },
    { "error reading user database.", OSI_ERROR_READ_USERS_FAILED },
    { "no users.", OSI_ERROR_NO_USERS },
    { "user does not exist.", OSI_ERROR_USER_DOES_NOT_EXIST },
    { "unable to delete user.", OSI_ERROR_DELETE_USER_FAILED },

    { NULL, 0 }
};

char * osi_get_name_for_error( osi_uint64 error );

void wrap_error( OSI_ERROR *error );
void unwrap_error( OSI_ERROR *error );

void print_osi_error( OSI_ERROR *error );

void pack_error( OSI_ERROR *error, char *buffer, int buffer_size );
void unpack_error( OSI_ERROR *error, char *buffer, int buffer_size );

#endif

