Esempio n. 1
0
File: dbus.c Progetto: chaind/chaind
static DBusHandlerResult handle_chaind_get_block(DBusConnection* conn, DBusMessage* msg, void* userdata)
{
    char const* hash_string = NULL;
    if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &hash_string, DBUS_TYPE_INVALID)) {
        return DBUS_HANDLER_RESULT_HANDLED;
    }

    if(strlen(hash_string) != 64 || !is_hex_string(hash_string, 64)) {
        return DBUS_HANDLER_RESULT_HANDLED;
    }

    size_t path_size = strlen(DBUS_BLOCK_PATH) + 1 + 64 + 1;
    char* path = (char *)alloca(sizeof(char) * path_size);
    sprintf(path, "%s/%s", DBUS_BLOCK_PATH, hash_string);
    path[path_size - 1] = 0;

    DBusMessage* response = dbus_message_new_method_return(msg);
    if(response == NULL) return DBUS_HANDLER_RESULT_NEED_MEMORY;

    if(!dbus_message_append_args(response, DBUS_TYPE_OBJECT_PATH, (void*)&path, DBUS_TYPE_INVALID)) {
        return DBUS_HANDLER_RESULT_NEED_MEMORY;
    }

    if(!dbus_connection_send(conn, response, NULL)) return DBUS_HANDLER_RESULT_NEED_MEMORY;
    dbus_connection_flush(conn);
    dbus_message_unref(response);

    return DBUS_HANDLER_RESULT_HANDLED;
}
Esempio n. 2
0
/* Converts \uxxxx or \Uxxxxyyyy escapes into their UTF-8 equivalents.
 * Returns a newly allocated string. The input string must be valid utf-8.
 */
char *str_unescape_unicode(const char *utf8_string)
{
    if(utf8_string == NULL)
        return NULL;

    dstring_t *unescaped = dstring_new(NULL);
    const char *p = utf8_string;
    while(p && *p)
    {
        gunichar uc = g_utf8_get_char(p);
        p = g_utf8_next_char(p);
        if(uc == '\\')
        {
            if(*p == 'u' && is_hex_string(p+1, 4))
            {
                p++;
                uc = htons(get_ucs4_16(p));
                p += 4;
            }
            else if(*p == 'U' && is_hex_string(p+1, 8))
            {
                p++;
                uc = htonl(get_ucs4_32(p));
                p += 8;
            }

            if(!g_unichar_validate(uc))
            {
                continue;
            }
        }

        char utf8_buf[6];
        int utf8_len = g_unichar_to_utf8(uc, utf8_buf);
        dstring_append_len(unescaped, utf8_buf, utf8_len);
    }
    char *result = unescaped->string;
    dstring_free(unescaped, 0);

    return result;
}
int is_string_of_radix(char *string, int radix)
{
	if (radix == 16)
		return is_hex_string(string);
	else if (radix == 10)
		return is_decimal_string(string);
	else if (radix == 8)
		return is_octal_string(string);
	else if (radix == 2)
		return is_binary_string(string);
	else
		return FALSE;
}
/* Saves the union ipt_matchinfo in parseable form to stdout. */
static void
save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
{
	const struct ipt_string_info *info =
	    (const struct ipt_string_info*) match->data;

	if (is_hex_string(info->string, info->len)) {
		printf("--hex-string %s", (info->invert) ? "! ": "");
		print_hex_string(info->string, info->len);
	} else {
		printf("--string %s", (info->invert) ? "! ": "");
		print_string(info->string, info->len);
	}
}
Esempio n. 5
0
/*
 * Parses the given little endian hex string into a little endian bit string padded or truncated to
 * binary_size bits. Throws an error if there are non-hex characters in the input string.
 */
char *convert_hex_string_of_size_to_bit_string(char *orig_string, int binary_size)
{
	if (!is_hex_string(orig_string))
		error_message(PARSE_ERROR, -1, -1, "Invalid hex number: %s.\n", orig_string);

	char *bit_string = calloc(1,sizeof(char));
	char *string     = strdup(orig_string);
	int   size       = strlen(string);

	// Change to big endian. (We want to add higher order bits at the end.)
	reverse_string(string, size);

	int count = 0;
	int i;
	for (i = 0; i < size; i++)
	{
		char temp[] = {string[i],'\0'};

		unsigned long value = strtoul(temp, NULL, 16);
		int k;
		for (k = 0; k < 4; k++)
		{
			char bit = value % 2;
			value /= 2;
			bit_string = realloc(bit_string, sizeof(char) * (count + 2));
			bit_string[count++] = '0' + bit;
			bit_string[count]   = '\0';
		}
	}
	free(string);

	// Pad with zeros to binary_size.
	while (count < binary_size)
	{
		bit_string = realloc(bit_string, sizeof(char) * (count + 2));
		bit_string[count++] = '0';
		bit_string[count]   = '\0';
	}

	// Truncate to binary_size
	bit_string[binary_size] = '\0';
	// Change to little endian
	reverse_string(bit_string, binary_size);
	// Copy out only the bits before the truncation.
	char *return_string = strdup(bit_string);
	free(bit_string);
	return return_string;
}
/* Prints out the matchinfo. */
static void
print(const struct ipt_ip *ip,
      const struct ipt_entry_match *match,
      int numeric)
{
	const struct ipt_string_info *info =
	    (const struct ipt_string_info*) match->data;

	if (is_hex_string(info->string, info->len)) {
		printf("STRING match %s", (info->invert) ? "!" : "");
		print_hex_string(info->string, info->len);
	} else {
		printf("STRING match %s", (info->invert) ? "!" : "");
		print_string(info->string, info->len);
	}
}
Esempio n. 7
0
uint32_t string_to_int(char *num_str)
{
    /* PRE:  The given pointer is valid and legal. */
    /* POST: Returns the uint32_t representation of the given string. */

    assert (num_str != NULL);

    while (*num_str == '=' || *num_str == '#' || isspace(*num_str)) num_str++;

    if (is_hex_string(num_str)) {
        /* Expression is hexadecimal */
        return (uint32_t)strtol(num_str + 2, NULL, 16); /* + 2 removes "0x" */

    } else {
        /* Expression is decimal */
        return (uint32_t)strtol(num_str, NULL, 10);
    }
}
Esempio n. 8
0
/* Saves the union ipt_matchinfo in parseable form to stdout. */
static void string_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_string_info *info =
	    (const struct xt_string_info*) match->data;

	if (is_hex_string(info->pattern, info->patlen)) {
		printf("--hex-string %s", (info->invert) ? "! ": "");
		print_hex_string(info->pattern, info->patlen);
	} else {
		printf("--string %s", (info->invert) ? "! ": "");
		print_string(info->pattern, info->patlen);
	}
	printf("--algo %s ", info->algo);
	if (info->from_offset != 0)
		printf("--from %u ", info->from_offset);
	if (info->to_offset != 0)
		printf("--to %u ", info->to_offset);
}
Esempio n. 9
0
/* Prints out the matchinfo. */
static void
string_print(const void *ip, const struct xt_entry_match *match, int numeric)
{
	const struct xt_string_info *info =
	    (const struct xt_string_info*) match->data;

	if (is_hex_string(info->pattern, info->patlen)) {
		printf("STRING match %s", (info->invert) ? "!" : "");
		print_hex_string(info->pattern, info->patlen);
	} else {
		printf("STRING match %s", (info->invert) ? "!" : "");
		print_string(info->pattern, info->patlen);
	}
	printf("ALGO name %s ", info->algo);
	if (info->from_offset != 0)
		printf("FROM %u ", info->from_offset);
	if (info->to_offset != 0)
		printf("TO %u ", info->to_offset);
}
Esempio n. 10
0
File: dbus.c Progetto: chaind/chaind
static DBusHandlerResult filter_message(DBusConnection* conn, DBusMessage* msg, void* userdata)
{
    //const char *interface = dbus_message_get_interface(msg);
    //const char *method    = dbus_message_get_member(msg);
    const char *object    = dbus_message_get_path(msg);

    //if(interface != NULL) printf("dbus interface: %s\n", interface);
    //if(method != NULL) printf("dbus method: %s\n", method);
    //if(object != NULL) printf("dbus object: %s\n", object);

    if(object != NULL 
     && strlen(object) == (strlen(DBUS_BLOCK_PATH) + 1 + 64) 
     && strncmp(DBUS_BLOCK_PATH, object, strlen(DBUS_BLOCK_PATH)) == 0 
     && object[strlen(DBUS_BLOCK_PATH)] == '/'
     && is_hex_string(&object[strlen(DBUS_BLOCK_PATH)+1], 64)) {
        return dbus_block_filter_message(conn, msg, userdata, &object[strlen(DBUS_BLOCK_PATH)+1]);
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
Esempio n. 11
0
static void string_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_string_info *info =
	    (const struct xt_string_info*) match->data;
	const int revision = match->u.user.revision;
	int invert = (revision == 0 ? info->u.v0.invert :
				    info->u.v1.flags & XT_STRING_FLAG_INVERT);

	if (is_hex_string(info->pattern, info->patlen)) {
		printf("%s --hex-string", (invert) ? " !" : "");
		print_hex_string(info->pattern, info->patlen);
	} else {
		printf("%s --string", (invert) ? " !": "");
		print_string(info->pattern, info->patlen);
	}
	printf(" --algo %s", info->algo);
	if (info->from_offset != 0)
		printf(" --from %u", info->from_offset);
	if (info->to_offset != 0)
		printf(" --to %u", info->to_offset);
	if (revision > 0 && info->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
		printf(" --icase");
}
Esempio n. 12
0
static void
string_print(const void *ip, const struct xt_entry_match *match, int numeric)
{
	const struct xt_string_info *info =
	    (const struct xt_string_info*) match->data;
	const int revision = match->u.user.revision;
	int invert = (revision == 0 ? info->u.v0.invert :
				    info->u.v1.flags & XT_STRING_FLAG_INVERT);

	if (is_hex_string(info->pattern, info->patlen)) {
		printf(" STRING match %s", invert ? "!" : "");
		print_hex_string(info->pattern, info->patlen);
	} else {
		printf(" STRING match %s", invert ? "!" : "");
		print_string(info->pattern, info->patlen);
	}
	printf(" ALGO name %s", info->algo);
	if (info->from_offset != 0)
		printf(" FROM %u", info->from_offset);
	if (info->to_offset != 0)
		printf(" TO %u", info->to_offset);
	if (revision > 0 && info->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
		printf(" ICASE");
}
Esempio n. 13
0
static void interpret(char *name, char *val, int comma, int rtype)
{
	int type;

	while (*name == ' '||*name == '(')
		name++;


	/* Do some fixups */
	if (rtype == AUDIT_EXECVE && name[0] == 'a')
		type = T_ESCAPED;
	else if (rtype == AUDIT_AVC && strcmp(name, "saddr") == 0)
		type = -1;
	else if (strcmp(name, "acct") == 0) {
		// Remove trailing punctuation
		int len = strlen(val);
		if (val[len-1] == ':')
			val[len-1] = 0;

		if (val[0] == '"')
			type = T_ESCAPED;
		else if (is_hex_string(val))
			type = T_ESCAPED;
		else
			type = -1;
	} else
		type = audit_lookup_type(name);

	switch(type) {
		case T_UID:
			print_uid(val);
			break;
		case T_GID:
			print_gid(val);
			break;
		case T_SYSCALL:
			print_syscall(val);
			break;
		case T_ARCH:
			print_arch(val);
			break;
		case T_EXIT:
			print_exit(val);
			break;
		case T_ESCAPED:
			print_escaped(val);
			break;
		case T_PERM:
			print_perm(val);
			break;
		case T_MODE:
			print_mode(val);
			break;
		case T_SOCKADDR:
			print_sockaddr(val);
			break;
		case T_FLAGS:
			print_flags(val);
			break;
		case T_PROMISC:
			print_promiscuous(val);
			break;
		case T_CAPABILITY:
			print_capabilities(val);
			break;
		case T_SIGNAL:
			print_signals(val);
			break;
		case T_KEY:
			print_key(val);
			break;
		case T_LIST:
			print_list(val);
			break;
		case T_TTY_DATA:
			print_tty_data(val);
			break;
		default:
			printf("%s%c", val, comma ? ',' : ' ');
	}
}
Esempio n. 14
0
int	set_result_type(AGENT_RESULT *result, int value_type, int data_type, char *c)
{
	int		ret = FAIL;
	zbx_uint64_t	value_uint64;
	double		value_double;

	assert(result);

	switch (value_type)
	{
		case ITEM_VALUE_TYPE_UINT64:
			zbx_rtrim(c, " \"");
			zbx_ltrim(c, " \"+");
			del_zeroes(c);

			switch (data_type)
			{
				case ITEM_DATA_TYPE_BOOLEAN:
					if (SUCCEED == is_boolean(c, &value_uint64))
					{
						SET_UI64_RESULT(result, value_uint64);
						ret = SUCCEED;
					}
					break;
				case ITEM_DATA_TYPE_OCTAL:
					if (SUCCEED == is_uoct(c))
					{
						ZBX_OCT2UINT64(value_uint64, c);
						SET_UI64_RESULT(result, value_uint64);
						ret = SUCCEED;
					}
					break;
				case ITEM_DATA_TYPE_DECIMAL:
					if (SUCCEED == is_uint64(c, &value_uint64))
					{
						SET_UI64_RESULT(result, value_uint64);
						ret = SUCCEED;
					}
					break;
				case ITEM_DATA_TYPE_HEXADECIMAL:
					if (SUCCEED == is_uhex(c))
					{
						ZBX_HEX2UINT64(value_uint64, c);
						SET_UI64_RESULT(result, value_uint64);
						ret = SUCCEED;
					}
					else if (SUCCEED == is_hex_string(c))
					{
						zbx_remove_whitespace(c);
						ZBX_HEX2UINT64(value_uint64, c);
						SET_UI64_RESULT(result, value_uint64);
						ret = SUCCEED;
					}
					break;
				default:
					THIS_SHOULD_NEVER_HAPPEN;
					break;
			}
			break;
		case ITEM_VALUE_TYPE_FLOAT:
			zbx_rtrim(c, " \"");
			zbx_ltrim(c, " \"+");

			if (SUCCEED != is_double(c))
				break;
			value_double = atof(c);

			SET_DBL_RESULT(result, value_double);
			ret = SUCCEED;
			break;
		case ITEM_VALUE_TYPE_STR:
			zbx_replace_invalid_utf8(c);
			SET_STR_RESULT(result, zbx_strdup(NULL, c));
			ret = SUCCEED;
			break;
		case ITEM_VALUE_TYPE_TEXT:
			zbx_replace_invalid_utf8(c);
			SET_TEXT_RESULT(result, zbx_strdup(NULL, c));
			ret = SUCCEED;
			break;
		case ITEM_VALUE_TYPE_LOG:
			zbx_replace_invalid_utf8(c);
			add_log_result(result, c);
			ret = SUCCEED;
			break;
	}

	if (SUCCEED != ret)
	{
		char	*error = NULL;

		zbx_remove_chars(c, "\r\n");
		zbx_replace_invalid_utf8(c);

		if (ITEM_VALUE_TYPE_UINT64 == value_type)
			error = zbx_dsprintf(error,
					"Received value [%s] is not suitable for value type [%s] and data type [%s]",
					c, zbx_item_value_type_string(value_type),
					zbx_item_data_type_string(data_type));
		else
			error = zbx_dsprintf(error,
					"Received value [%s] is not suitable for value type [%s]",
					c, zbx_item_value_type_string(value_type));

		SET_MSG_RESULT(result, error);
	}

	return ret;
}