Пример #1
0
static int
add_idmef_object(idmef_message_t *msg, const char *object, const char *value)
{
    int ret = 0;
    idmef_value_t *val;
    idmef_path_t *path;

    if (value == NULL) {
        return (0);
    }

    ret = idmef_path_new_fast(&path, object);
    if (ret < 0) {
        return (-1);
    }

    ret = idmef_value_new_from_path(&val, path, value);
    if (ret < 0) {
        idmef_path_destroy(path);
        return (-1);
    }

    ret = idmef_path_set(path, msg, val);
    if (ret < 0) {
        merror("%s: OSSEC2Prelude: IDMEF: Cannot add object '%s': %s.",
               ARGV0, object, prelude_strerror(ret));
    }

    idmef_value_destroy(val);
    idmef_path_destroy(path);

    return (ret);
}
Пример #2
0
/**
 * idmef_message_get_string:
 * @message: Pointer to an #idmef_message_t object.
 * @path: Path to retrieve the string from within @message.
 * @result: Pointer where the result should be stored.
 *
 * Retrieve the string stored within @path of @message and store it
 * in the user provided @result.
 *
 * The caller is responssible for freeing @result.
 *
 * Returns: A positive value in case @path was successfully retrieved
 * 0 if the path is empty, or a negative value if an error occured.
 */
int idmef_message_get_string(idmef_message_t *message, const char *path, char **result)
{
        int ret;
        idmef_value_t *iv;
        prelude_string_t *str;

        ret = idmef_message_get_value(message, path, &iv);
        if ( ret <= 0 )
                return ret;

        if ( idmef_value_get_type(iv) != IDMEF_VALUE_TYPE_STRING ) {
                ret = _idmef_value_cast(iv, IDMEF_VALUE_TYPE_STRING, 0);
                if ( ret < 0 )
                        goto err;
        }

        if ( ! (str = idmef_value_get_string(iv)) ) {
                ret = -1;
                goto err;
        }

        ret = prelude_string_get_string_released(str, result);

err:
        idmef_value_destroy(iv);
        return ret;
}
Пример #3
0
int idmef_message_get_data(idmef_message_t *message, const char *path, unsigned char **data, size_t *size)
{
        int ret;
        idmef_data_t *dp;
        idmef_value_t *iv;

        ret = idmef_message_get_value(message, path, &iv);
        if ( ret <= 0 )
                return ret;

        if ( idmef_value_get_type(iv) != IDMEF_VALUE_TYPE_DATA || ! (dp = idmef_value_get_data(iv)) ) {
                ret = -1;
                goto err;
        }

        *size = idmef_data_get_len(dp);
        *data = malloc(*size);
        if ( ! *data ) {
                ret = -1;
                goto err;
        }

        memcpy(*data, idmef_data_get_data(dp), *size);

err:
        idmef_value_destroy(iv);
        return ret;
}
/**
 * idmef_message_get_string:
 * @message: Pointer to an #idmef_message_t object.
 * @path: Path to retrieve the string from within @message.
 * @result: Pointer where the result should be stored.
 *
 * Retrieve the string stored within @path of @message and store it
 * in the user provided @result.
 *
 * The caller is responssible for freeing @result.
 *
 * Returns: A positive value in case @path was successfully retrieved
 * 0 if the path is empty, or a negative value if an error occured.
 */
int idmef_message_get_string(idmef_message_t *message, const char *path, char **result)
{
    int ret;
    idmef_value_t *iv;
    prelude_string_t *str;

    ret = idmef_message_get_value(message, path, &iv);
    if ( ret <= 0 )
        return ret;

    if ( idmef_value_get_type(iv) != IDMEF_VALUE_TYPE_STRING ) {
        ret = _idmef_value_cast(iv, IDMEF_VALUE_TYPE_STRING, 0);
        if ( ret < 0 )
            goto err;
    }

    if ( ! (str = idmef_value_get_string(iv)) ) {
        ret = -1;
        goto err;
    }

    if ( prelude_string_is_empty(str) ) {
        *result = NULL;
        return 0;
    }

    *result = strdup(prelude_string_get_string(str));
    ret = prelude_string_get_len(str);

err:
    idmef_value_destroy(iv);
    return ret;
}
Пример #5
0
static void cast_int32(void)
{
        idmef_value_t *value;

        assert(idmef_value_new_int32(&value, INT32_MIN) == 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT8, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT16, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT8, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT16, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT32, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT64, -1) < 0);

        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT64, -1) == 0);
        assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_INT64);
        assert(idmef_value_get_int64(value) == INT32_MIN);
        idmef_value_destroy(value);

        assert(idmef_value_new_int32(&value, INT32_MIN) == 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_FLOAT, -1) == 0);
        assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_FLOAT);
        assert(idmef_value_get_float(value) == INT32_MIN);
        idmef_value_destroy(value);

        assert(idmef_value_new_int32(&value, INT32_MIN) == 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DOUBLE, -1) == 0);
        assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DOUBLE);
        assert(idmef_value_get_double(value) == INT32_MIN);
        idmef_value_destroy(value);

        assert(idmef_value_new_int32(&value, INT32_MIN) == 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DATA, -1) == 0);
        assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DATA);
        assert((int) idmef_data_get_uint32(idmef_value_get_data(value)) == INT32_MIN);
        idmef_value_destroy(value);

        assert(idmef_value_new_int32(&value, INT32_MAX) == 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) == 0);
        assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_TIME);
        assert(idmef_time_get_sec(idmef_value_get_time(value)) == INT32_MAX);
        idmef_value_destroy(value);
}
Пример #6
0
static void cast_string(void)
{
        idmef_data_t *data;
        idmef_value_t *value;
        requiem_string_t *str;

        assert(requiem_string_new_ref(&str, "abcdefgh") == 0);
        assert(idmef_value_new_string(&value, str) == 0);
        idmef_value_dont_have_own_data(value);

        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT8, -1) < 0);
//        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT8, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT16, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT16, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT32, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT32, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_INT64, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_UINT64, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_FLOAT, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DOUBLE, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_ENUM, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_LIST, -1) < 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_CLASS, -1) < 0);

        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_DATA, -1) == 0);
        assert(idmef_value_get_type(value) == IDMEF_VALUE_TYPE_DATA);
        assert(data = idmef_value_get_data(value));
        assert(idmef_data_get_len(data) == (requiem_string_get_len(str) + 1));
        assert(memcmp(requiem_string_get_string(str), idmef_data_get_data(data), idmef_data_get_len(data)) == 0);
        requiem_string_destroy(str);

        cast_data(value);
        idmef_value_destroy(value);

        assert(requiem_string_new_ref(&str, "2008-01-01 20:42:31") == 0);
        assert(idmef_value_new_string(&value, str) == 0);
        assert(_idmef_value_cast(value, IDMEF_VALUE_TYPE_TIME, -1) == 0);
        idmef_value_destroy(value);
}
Пример #7
0
/**
 * idmef_message_set_number:
 * @message: Pointer to an #idmef_message_t object.
 * @path: Path to be set within @message.
 * @number: Value to associate @message[@path] with.
 *
 * This function will set the @path member within @message to the
 * provided @value, which will be converted to the @path value type.
 *
 * Example:
 * idmef_message_set_number(message, "alert.assessment.confidence.confidence", 0.123456);
 * idmef_message_set_number(message, "alert.source(0).service.port", 1024);
 *
 * Returns: 0 on success, or a negative value if an error occured.
 */
int idmef_message_set_number(idmef_message_t *message, const char *path, double number)
{
        int ret;
        idmef_value_t *iv;

        ret = idmef_value_new_double(&iv, number);
        if ( ret < 0 )
                return ret;

        ret = idmef_message_set_value(message, path, iv);
        idmef_value_destroy(iv);

        return ret;

}
Пример #8
0
/**
 * idmef_message_set_data:
 * @message: Pointer to an #idmef_message_t object.
 * @path: Path to be set within @message.
 * @data: Pointer to data to associate @message[@path] with.
 * @size: Size of the data pointed to by @data.
 *
 * This function will set the @path member within @message to the
 * provided @data of size @size.
 *
 * Returns: 0 on success, or a negative value if an error occured.
 */
int idmef_message_set_data(idmef_message_t *message, const char *path, const unsigned char *data, size_t size)
{
        int ret;
        idmef_data_t *id;
        idmef_value_t *iv;

        ret = idmef_data_new_byte_string_dup(&id, data, size);
        if ( ret < 0 )
                return ret;

        ret = idmef_value_new_data(&iv, id);
        if ( ret < 0 ) {
                idmef_data_destroy(id);
                return ret;
        }

        ret = idmef_message_set_value(message, path, iv);
        idmef_value_destroy(iv);

        return ret;
}
Пример #9
0
/**
 * idmef_message_get_number:
 * @message: Pointer to an #idmef_message_t object.
 * @path: Path to retrieve the number from within @message.
 * @result: Pointer where the result should be stored.
 *
 * Retrieve the number stored within @path of @message and store it
 * in the user provided @result.
 *
 * Returns: A positive value in case @path was successfully retrieved
 * 0 if the path is empty, or a negative value if an error occured.
 */
int idmef_message_get_number(idmef_message_t *message, const char *path, double *result)
{
        int ret;
        idmef_value_t *iv;

        ret = idmef_message_get_value(message, path, &iv);
        if ( ret <= 0 )
                return ret;

        if ( idmef_value_get_type(iv) != IDMEF_VALUE_TYPE_DOUBLE ) {
                ret = _idmef_value_cast(iv, IDMEF_VALUE_TYPE_DOUBLE, 0);
                if ( ret < 0 )
                        goto err;
        }

        *result = idmef_value_get_double(iv);

err:
        idmef_value_destroy(iv);
        return ret;
}
Пример #10
0
/**
 * idmef_message_set_string:
 * @message: Pointer to an #idmef_message_t object.
 * @path: Path to be set within @message.
 * @value: Value to associate @message[@path] with.
 *
 * This function will set the @path member within @message to the
 * provided @value, which will be converted to the corresponding
 * @path value type.
 *
 * Example:
 * idmef_message_set_string(message, "alert.classification.text", "MyText");
 * idmef_message_set_string(message, "alert.source(0).service.port", "1024");
 *
 * Returns: 0 on success, or a negative value if an error occured.
 */
int idmef_message_set_string(idmef_message_t *message, const char *path, const char *value)
{
        int ret;
        idmef_value_t *iv;
        prelude_string_t *str;

        ret = prelude_string_new_dup(&str, value);
        if ( ret < 0 )
                return ret;

        ret = idmef_value_new_string(&iv, str);
        if ( ret < 0 ) {
                prelude_string_destroy(str);
                return ret;
        }

        ret = idmef_message_set_value(message, path, iv);
        idmef_value_destroy(iv);

        return ret;
}