Esempio n. 1
0
switch_status_t mod_xml_radius_check_conditions(switch_channel_t *channel, switch_xml_t conditions) {
	switch_xml_t condition, param;
	char *channel_var = NULL;
	const char *channel_val = NULL;
	char *regex = NULL;
	char *anti = NULL;
	int all_matched = 1;
	int result = 0;
	
	if ( (condition = switch_xml_child(conditions, "condition")) == NULL) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to locate a condition under the conditions section\n");
		return SWITCH_STATUS_FALSE;
	}
	
	for (; condition; condition = condition->next) {
		
		if ( (param = switch_xml_child(condition, "param")) == NULL) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to locate a param under this condition\n");
			return SWITCH_STATUS_FALSE;
		}
		
		all_matched = 1;
		for (; param && all_matched; param = param->next) {
			channel_var = (char *) switch_xml_attr(param, "var");
			regex = (char *) switch_xml_attr(param, "regex");
			anti = (char *) switch_xml_attr(param, "anti");
			
			if ( channel_var == NULL || regex == NULL ) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Improperly constructed mod_radius condition: %s %s\n", channel_var, regex);
				continue;
			}
			
			if ( ( channel_val = switch_channel_get_variable(channel, channel_var) ) == NULL ) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, 
								  "Improperly constructed mod_radius condition, no such channel variable: %s %s\n", channel_var, regex);
				continue;
			}

			result = ( switch_regex_match( channel_val, regex) != SWITCH_STATUS_SUCCESS);
			if (( anti == NULL && result ) || ( anti != NULL && !result ) ){
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Didn't match: %s == %s \n", switch_channel_get_variable(channel, channel_var), regex);
				all_matched = 0;
			} else {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Result of %s match: %s == %s \n", 
								  anti, switch_channel_get_variable(channel, channel_var), regex);
			}
		}

		if ( all_matched ) {
			return SWITCH_STATUS_SUCCESS;
		}
	}
	
	return SWITCH_STATUS_FALSE;
}
Esempio n. 2
0
switch_status_t mod_xml_radius_check_conditions(switch_channel_t *channel, switch_xml_t conditions) {
	switch_xml_t condition, param;
	char *channel_var = NULL;
	char *regex = NULL;
	int all_matched = 1;
	
	if ( (condition = switch_xml_child(conditions, "condition")) == NULL) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to locate a condition under the conditions section\n");
		return SWITCH_STATUS_FALSE;
	}
	
	for (; condition; condition = condition->next) {
		
		if ( (param = switch_xml_child(condition, "param")) == NULL) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to locate a param under this condition\n");
			return SWITCH_STATUS_FALSE;
		}
		
		all_matched = 1;
		for (; param && all_matched; param = param->next) {
			channel_var = (char *) switch_xml_attr(param, "var");
			regex = (char *) switch_xml_attr(param, "regex");
			
			if ( channel_var == NULL || regex == NULL ) {
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Improperly constructed mod_radius condition: %s %s\n", channel_var, regex);
			}
			
			if ( switch_regex_match( switch_channel_get_variable(channel, channel_var), regex) != SWITCH_STATUS_SUCCESS) {
				all_matched = 0;
			}
		}

		if ( all_matched ) {
			return SWITCH_STATUS_SUCCESS;
		}
	}
	
	return SWITCH_STATUS_FALSE;
}
Esempio n. 3
0
/**
 * Start execution of call sendfax component
 * @param call the call to send fax to
 * @param msg the original request
 * @param session_data the call's session
 */
static iks *start_sendfax_component(struct rayo_actor *call, struct rayo_message *msg, void *session_data)
{
	iks *iq = msg->payload;
	switch_core_session_t *session = (switch_core_session_t *)session_data;
	struct fax_component *sendfax_component = NULL;
	iks *sendfax = iks_find(iq, "sendfax");
	iks *response = NULL;
	switch_event_t *execute_event = NULL;
	switch_channel_t *channel = switch_core_session_get_channel(session);
	switch_memory_pool_t *pool;
	iks *document;
	const char *fax_document;
	const char *fax_header;
	const char *fax_identity;
	const char *pages;

	/* validate attributes */
	if (!VALIDATE_RAYO_SENDFAX(sendfax)) {
		return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
	}

	/* fax is only allowed if the call is not currently joined */
	if (rayo_call_is_joined(RAYO_CALL(call))) {
		return iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "can't send fax on a joined call");
	}

	if (rayo_call_is_faxing(RAYO_CALL(call))) {
		return iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "fax already in progress");
	}

	/* get fax document */
	document = iks_find(sendfax, "document");
	if (!document) {
		return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "missing document");
	}
	fax_document = iks_find_attrib_soft(document, "url");
	if (zstr(fax_document)) {
		return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "missing document url");
	}

	/* is valid URL type? */
	if (!strncasecmp(fax_document, "http://", 7) || !strncasecmp(fax_document, "https://", 8)) {
		switch_stream_handle_t stream = { 0 };
		SWITCH_STANDARD_STREAM(stream);
		/* need to fetch document from server... */
		switch_api_execute("http_get", fax_document, session, &stream);
		if (!zstr(stream.data) && !strncmp(fax_document, SWITCH_PATH_SEPARATOR, strlen(SWITCH_PATH_SEPARATOR))) {
			fax_document = switch_core_session_strdup(session, stream.data);
		} else {
			switch_safe_free(stream.data);
			return iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "failed to fetch document");
		}
		switch_safe_free(stream.data);
	} else if (!strncasecmp(fax_document, "file://", 7)) {
		fax_document = fax_document + 7;
		if (zstr(fax_document)) {
			return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "invalid file:// url");
		}
	} else if (strncasecmp(fax_document, SWITCH_PATH_SEPARATOR, strlen(SWITCH_PATH_SEPARATOR))) {
		return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "unsupported url type");
	}

	/* does document exist? */
	if (switch_file_exists(fax_document, pool) != SWITCH_STATUS_SUCCESS) {
		return iks_new_error_detailed_printf(iq, STANZA_ERROR_BAD_REQUEST, "file not found: %s", fax_document);
	}

	/* get fax identity and header */
	fax_identity = iks_find_attrib_soft(document, "identity");
	if (!zstr(fax_identity)) {
		switch_channel_set_variable(channel, "fax_ident", fax_identity);
	} else {
		switch_channel_set_variable(channel, "fax_ident", NULL);
	}
	fax_header = iks_find_attrib_soft(document, "header");
	if (!zstr(fax_header)) {
		switch_channel_set_variable(channel, "fax_header", fax_header);
	} else {
		switch_channel_set_variable(channel, "fax_header", NULL);
	}

	/* get pages to send */
	pages = iks_find_attrib_soft(document, "pages");
	if (!zstr(pages)) {
		if (switch_regex_match(pages, "[1-9][0-9]*(-[1-9][0-9]*)?") == SWITCH_STATUS_FALSE) {
			return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "invalid pages value");
		} else {
			int start = 0;
			int end = 0;
			char *pages_dup = switch_core_session_strdup(session, pages);
			char *sep = strchr(pages_dup, '-');
			if (sep) {
				*sep = '\0';
				sep++;
				end = atoi(sep);
			}
			start = atoi(pages_dup);
			if (end && end < start) {
				return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "invalid pages value");
			}
			switch_channel_set_variable(channel, "fax_start_page", pages_dup);
			switch_channel_set_variable(channel, "fax_end_page", sep);
		}
	} else {
		switch_channel_set_variable(channel, "fax_start_page", NULL);
		switch_channel_set_variable(channel, "fax_end_page", NULL);
	}

	/* create sendfax component */
	switch_core_new_memory_pool(&pool);
	sendfax_component = switch_core_alloc(pool, sizeof(*sendfax_component));
	rayo_component_init((struct rayo_component *)sendfax_component, pool, RAT_CALL_COMPONENT, "sendfax", NULL, call, iks_find_attrib(iq, "from"));

	/* add channel variable so that fax component can be located from fax events */
	switch_channel_set_variable(channel, "rayo_fax_jid", RAYO_JID(sendfax_component));

	/* clear fax result variables */
	switch_channel_set_variable(channel, "fax_success", NULL);
	switch_channel_set_variable(channel, "fax_result_code", NULL);
	switch_channel_set_variable(channel, "fax_result_text", NULL);
	switch_channel_set_variable(channel, "fax_document_transferred_pages", NULL);
	switch_channel_set_variable(channel, "fax_document_total_pages", NULL);
	switch_channel_set_variable(channel, "fax_image_resolution", NULL);
	switch_channel_set_variable(channel, "fax_image_size", NULL);
	switch_channel_set_variable(channel, "fax_bad_rows", NULL);
	switch_channel_set_variable(channel, "fax_transfer_rate", NULL);
	switch_channel_set_variable(channel, "fax_ecm_used", NULL);
	switch_channel_set_variable(channel, "fax_local_station_id", NULL);
	switch_channel_set_variable(channel, "fax_remote_station_id", NULL);

	/* clear fax interrupt variable */
	switch_channel_set_variable(switch_core_session_get_channel(session), "rayo_read_frame_interrupt", NULL);

	rayo_call_set_faxing(RAYO_CALL(call), 1);

	/* execute txfax APP */
	if (switch_event_create(&execute_event, SWITCH_EVENT_COMMAND) == SWITCH_STATUS_SUCCESS) {
		switch_event_add_header_string(execute_event, SWITCH_STACK_BOTTOM, "call-command", "execute");
		switch_event_add_header_string(execute_event, SWITCH_STACK_BOTTOM, "execute-app-name", "txfax");
		switch_event_add_header_string(execute_event, SWITCH_STACK_BOTTOM, "execute-app-arg", fax_document);
		if (!switch_channel_test_flag(channel, CF_PROXY_MODE)) {
			switch_channel_set_flag(channel, CF_BLOCK_BROADCAST_UNTIL_MEDIA);
		}

		if (switch_core_session_queue_private_event(session, &execute_event, SWITCH_FALSE) != SWITCH_STATUS_SUCCESS) {
			response = iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "failed to txfax (queue event failed)");
			if (execute_event) {
				switch_event_destroy(&execute_event);
			}
			rayo_call_set_faxing(RAYO_CALL(call), 0);
			RAYO_UNLOCK(sendfax_component);
		} else {
			/* component starting... */
			rayo_component_send_start(RAYO_COMPONENT(sendfax_component), iq);
		}
	} else {
		response = iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "failed to create txfax event");
		rayo_call_set_faxing(RAYO_CALL(call), 0);
		RAYO_UNLOCK(sendfax_component);
	}

	return response;
}
SWITCH_DECLARE(switch_status_t) switch_xml_config_parse_event(switch_event_t *event, int count, switch_bool_t reload,
															  switch_xml_config_item_t *instructions)
{
	switch_xml_config_item_t *item;
	int matched_count = 0;

	for (item = instructions; item->key; item++) {
		const char *value = switch_event_get_header(event, item->key);
		switch_bool_t changed = SWITCH_FALSE;
		switch_xml_config_callback_t callback = (switch_xml_config_callback_t) item->function;
		void *ptr = item->ptr;

		//switch_assert(ptr);

		if (value) {
			matched_count++;
		}

		if (reload && !switch_test_flag(item, CONFIG_RELOADABLE)) {
			continue;
		}

		if (!value && switch_test_flag(item, CONFIG_REQUIRED)) {
			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Required parameter [%s] is missing\n", item->key);
			return SWITCH_STATUS_FALSE;
		}

		switch (item->type) {
		case SWITCH_CONFIG_INT:
			{
				switch_xml_config_int_options_t *int_options = (switch_xml_config_int_options_t *) item->data;
				int *dest = (int *) ptr;
				int intval;
				if (value) {
					if (switch_is_number(value)) {
						intval = atoi(value);
					} else {
						intval = (int) (intptr_t) item->defaultvalue;
						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s], setting default [%d]\n",
										  value, item->key, intval);
					}

					if (int_options) {
						/* Enforce validation options */
						if ((int_options->enforce_min && !(intval >= int_options->min)) || (int_options->enforce_max && !(intval <= int_options->max))) {
							/* Validation failed, set default */
							intval = (int) (intptr_t) item->defaultvalue;
							/* Then complain */
							if (int_options->enforce_min && int_options->enforce_max) {
								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
												  "Invalid value [%s] for parameter [%s], should be between [%d] and [%d], setting default [%d]\n", value,
												  item->key, int_options->min, int_options->max, intval);
							} else {
								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
												  "Invalid value [%s] for parameter [%s], should be %s [%d], setting default [%d]\n", value, item->key,
												  int_options->enforce_min ? "at least" : "at max",
												  int_options->enforce_min ? int_options->min : int_options->max, intval);
							}
						}
					}
				} else {
					intval = (int) (intptr_t) item->defaultvalue;
				}

				if (*dest != intval) {
					*dest = intval;
					changed = SWITCH_TRUE;
				}
			}
			break;
		case SWITCH_CONFIG_ATOMIC:
			{
				switch_xml_config_atomic_options_t *atomic_options = (switch_xml_config_atomic_options_t *) item->data;
				switch_atomic_t *dest = (switch_atomic_t *) ptr;
				uint32_t uintval;
				if (value) {
					if (switch_is_number(value)) {
						uintval = (uint32_t) strtol(value, NULL, 10);
					} else {
						uintval = (uint32_t) (uintptr_t) item->defaultvalue;
						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s], setting default [%u]\n",
										  value, item->key, uintval);
					}

					if (atomic_options) {
						/* Enforce validation options */
						if ((atomic_options->enforce_min && !(uintval >= atomic_options->min)) || (atomic_options->enforce_max && !(uintval <= atomic_options->max))) {
							/* Validation failed, set default */
							uintval = (uint32_t) (uintptr_t) item->defaultvalue;
							/* Then complain */
							if (atomic_options->enforce_min && atomic_options->enforce_max) {
								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
												  "Invalid value [%s] for parameter [%s], should be between [%u] and [%u], setting default [%u]\n", value,
												  item->key, atomic_options->min, atomic_options->max, uintval);
							} else {
								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
												  "Invalid value [%s] for parameter [%s], should be %s [%u], setting default [%u]\n", value, item->key,
												  atomic_options->enforce_min ? "at least" : "at max",
												  atomic_options->enforce_min ? atomic_options->min : atomic_options->max, uintval);
							}
						}
					}
				} else {
					uintval = (uint32_t) (uintptr_t) item->defaultvalue;
				}

				if (switch_atomic_read(dest) != uintval) {
					switch_atomic_set(dest, uintval);
					changed = SWITCH_TRUE;
				}
			}
			break;
		case SWITCH_CONFIG_STRING:
			{
				switch_xml_config_string_options_t string_options_default = { 0 };
				switch_xml_config_string_options_t *string_options =
					item->data ? (switch_xml_config_string_options_t *) item->data : &string_options_default;
				const char *newstring = NULL;

				/* Perform validation */
				if (value) {
					if (!zstr(string_options->validation_regex)) {
						if (switch_regex_match(value, string_options->validation_regex) == SWITCH_STATUS_SUCCESS) {
							newstring = value;	/* Regex match, accept value */
						} else {
							newstring = (char *) item->defaultvalue;	/* Regex failed */
							if (newstring) {
								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s], setting default [%s]\n",
												  value, item->key, newstring);
							} else {
								switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s]\n", value, item->key);
							}
							switch_xml_config_item_print_doc(SWITCH_LOG_ERROR, item);
						}
					} else {
						newstring = value;	/* No validation */
					}
				} else {
					newstring = (char *) item->defaultvalue;
				}

				if (string_options->length > 0) {
					/* We have a preallocated buffer */
					char *dest = (char *) ptr;

					if (newstring) {
						if (strncasecmp(dest, newstring, string_options->length)) {
							switch_copy_string(dest, newstring, string_options->length);
							changed = SWITCH_TRUE;
						}
					} else {
						if (*dest != '\0') {
							*dest = '\0';
							changed = SWITCH_TRUE;
						}
					}
				} else if (string_options->pool) {
					/* Pool-allocated buffer */
					char **dest = (char **) ptr;

					if (newstring) {
						if (!*dest || strcmp(*dest, newstring)) {
							*dest = switch_core_strdup(string_options->pool, newstring);
						}
					} else {
						if (*dest) {
							changed = SWITCH_TRUE;
							*dest = NULL;
						}
					}
				} else {
					/* Dynamically allocated buffer */
					char **dest = (char **) ptr;

					if (newstring) {
						if (!*dest || strcmp(*dest, newstring)) {
							switch_safe_free(*dest);
							*dest = strdup(newstring);
							changed = SWITCH_TRUE;
						}
					} else {
						if (*dest) {
							switch_safe_free(*dest);
							changed = SWITCH_TRUE;
						}
					}
				}
			}
			break;
		case SWITCH_CONFIG_BOOL:
			{
				switch_bool_t *dest = (switch_bool_t *) ptr;
				switch_bool_t newval = SWITCH_FALSE;

				if (value && switch_true(value)) {
					newval = SWITCH_TRUE;
				} else if (value && switch_false(value)) {
					newval = SWITCH_FALSE;
				} else if (value) {
					/* Value isnt true or false */
					newval = (switch_bool_t) (intptr_t) item->defaultvalue;
					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s], setting default [%s]\n",
									  value, item->key, newval ? "true" : "false");
					switch_xml_config_item_print_doc(SWITCH_LOG_ERROR, item);
				} else {
					newval = (switch_bool_t) (intptr_t) item->defaultvalue;
				}

				if (*dest != newval) {
					*dest = newval;
					changed = SWITCH_TRUE;
				}
			}
			break;
		case SWITCH_CONFIG_CUSTOM:
			break;
		case SWITCH_CONFIG_ENUM:
			{
				switch_xml_config_enum_item_t *enum_options = (switch_xml_config_enum_item_t *) item->data;
				int *dest = (int *) ptr;
				int newval = 0;
				switch_status_t lookup_result = SWITCH_STATUS_SUCCESS;

				if (value) {
					lookup_result = switch_xml_config_enum_str2int(enum_options, value, &newval);
				} else {
					newval = (int) (intptr_t) item->defaultvalue;
				}

				if (lookup_result != SWITCH_STATUS_SUCCESS) {
					newval = (int) (intptr_t) item->defaultvalue;
					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid value [%s] for parameter [%s]\n", value, item->key);
					switch_xml_config_item_print_doc(SWITCH_LOG_ERROR, item);
				}

				if (*dest != newval) {
					changed = SWITCH_TRUE;
					*dest = newval;
				}
			}
			break;
		case SWITCH_CONFIG_FLAG:
			{
				int32_t *dest = (int32_t *) ptr;
				int index = (int) (intptr_t) item->data;
				int8_t currentval = (int8_t) ! !(*dest & index);
				int newval = 0;

				if (value) {
					newval = switch_true(value);
				} else {
					newval = (switch_bool_t) (intptr_t) item->defaultvalue;
				}

				if (newval != currentval) {
					changed = SWITCH_TRUE;
					if (newval) {
						*dest |= (1 << index);
					} else {
						*dest &= ~(1 << index);
					}
				}
			}
			break;
		case SWITCH_CONFIG_FLAGARRAY:
			{
				int8_t *dest = (int8_t *) ptr;
				unsigned int index = (unsigned int) (intptr_t) item->data;
				int8_t newval = value ? !!switch_true(value) : (int8_t) ((intptr_t) item->defaultvalue);
				if (dest[index] != newval) {
					changed = SWITCH_TRUE;
					dest[index] = newval;
				}
			}
			break;
		case SWITCH_CONFIG_LAST:
			break;
		default:
			break;
		}

		if (callback) {
			callback(item, value, (reload ? CONFIG_RELOAD : CONFIG_LOAD), changed);
		}
	}

	if (count != matched_count) {
		/* User made a mistake, find it */
		switch_event_header_t *header;
		for (header = event->headers; header; header = header->next) {
			switch_bool_t found = SWITCH_FALSE;
			for (item = instructions; item->key; item++) {
				if (!strcasecmp(header->name, item->key)) {
					found = SWITCH_TRUE;
					break;
				}
			}

			if (!found) {
				/* Tell the user */
				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING,
								  "Configuration parameter [%s] is unfortunately not valid, you might want to double-check that.\n", header->name);
			}
		}
	}

	return SWITCH_STATUS_SUCCESS;
}
Esempio n. 5
0
/**
 * @param url to check
 * @return true if this is an S3 url
 */
int aws_s3_is_s3_url(const char *url)
{
	/* AWS bucket naming rules are complex... this match only supports virtual hosting of buckets */
	return !zstr(url) && switch_regex_match(url, "^https?://\\w[-\\w.]{1,61}\\w\\.s3([-\\w]+)?\\.amazonaws\\.com/.*$") == SWITCH_STATUS_SUCCESS;
}