Example #1
0
void ast_ari_bridges_create_with_id(struct ast_variable *headers,
	struct ast_ari_bridges_create_with_id_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_bridge *, bridge, find_bridge(response, args->bridge_id), ao2_cleanup);
	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);

	if (bridge) {
		/* update */
		if (!ast_strlen_zero(args->name)) {
			if (!strcmp(args->name, bridge->name)) {
				ast_ari_response_error(
					response, 500, "Internal Error",
					"Changing bridge name is not implemented");
				return;
			}
		}
		if (!ast_strlen_zero(args->type)) {
			ast_ari_response_error(
				response, 500, "Internal Error",
				"Supplying a bridge type when updating a bridge is not allowed.");
			return;
		}
		ast_ari_response_ok(response,
			ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
		return;
	}

	bridge = stasis_app_bridge_create(args->type, args->name, args->bridge_id);
	if (!bridge) {
		ast_ari_response_error(
			response, 500, "Internal Error",
			"Unable to create bridge");
		return;
	}

	ast_bridge_lock(bridge);
	snapshot = ast_bridge_snapshot_create(bridge);
	ast_bridge_unlock(bridge);

	if (!snapshot) {
		ast_ari_response_error(
			response, 500, "Internal Error",
			"Unable to create snapshot for new bridge");
		return;
	}

	ast_ari_response_ok(response,
		ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
}
Example #2
0
void ast_ari_bridges_create(struct ast_variable *headers,
	struct ast_ari_bridges_create_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_bridge *, bridge, stasis_app_bridge_create(args->type, args->name, args->bridge_id), ao2_cleanup);
	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);

	if (!bridge) {
		ast_ari_response_error(
			response, 500, "Internal Error",
			"Unable to create bridge");
		return;
	}

	ast_bridge_lock(bridge);
	snapshot = ast_bridge_snapshot_create(bridge);
	ast_bridge_unlock(bridge);

	if (!snapshot) {
		ast_ari_response_error(
			response, 500, "Internal Error",
			"Unable to create snapshot for new bridge");
		return;
	}

	ast_ari_response_ok(response,
		ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
}
Example #3
0
void ast_ari_recordings_get_stored(struct ast_variable *headers,
	struct ast_ari_recordings_get_stored_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
		ao2_cleanup);
	struct ast_json *json;

	recording = stasis_app_stored_recording_find_by_name(
		args->recording_name);
	if (recording == NULL) {
		ast_ari_response_error(response, 404, "Not Found",
			"Recording not found");
		return;
	}

	json = stasis_app_stored_recording_to_json(recording);
	if (json == NULL) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}
Example #4
0
void ast_ari_asterisk_get_module(struct ast_variable *headers,
	struct ast_ari_asterisk_get_module_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;
	int module_retrieved = 0;

	ast_assert(response != NULL);

	if (!ast_module_check(args->module_name)) {
		ast_ari_response_error(
			response, 404, "Not Found",
			"Module could not be found in running modules");
		return;
	}

	json = ast_json_object_create();
	if (!json) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	module_retrieved = ast_update_module_list_condition(&identify_module, NULL, json,
	                                                    args->module_name);
	if (!module_retrieved) {
		ast_ari_response_error(
			response, 409, "Conflict",
			"Module information could not be retrieved");
		return;
	}

	ast_ari_response_ok(response, json);
}
Example #5
0
void ast_ari_asterisk_get_global_var(struct ast_variable *headers,
	struct ast_ari_asterisk_get_global_var_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	RAII_VAR(struct ast_str *, tmp, NULL, ast_free);

	const char *value;

	ast_assert(response != NULL);

	if (ast_strlen_zero(args->variable)) {
		ast_ari_response_error(
			response, 400, "Bad Request",
			"Variable name is required");
		return;
	}

	tmp = ast_str_create(32);
	if (!tmp) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	value = ast_str_retrieve_variable(&tmp, 0, NULL, NULL, args->variable);

	if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	ast_ari_response_ok(response, ast_json_ref(json));
}
void ast_ari_channels_get(struct ast_variable *headers,
	struct ast_ari_channels_get_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
	struct stasis_cache *cache;
	struct ast_channel_snapshot *snapshot;

	cache = ast_channel_cache();
	if (!cache) {
		ast_ari_response_error(
			response, 500, "Internal Server Error",
			"Message bus not initialized");
		return;
	}

	msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
				   args->channel_id);
	if (!msg) {
		ast_ari_response_error(
			response, 404, "Not Found",
			"Channel not found");
		return;
	}

	snapshot = stasis_message_data(msg);
	ast_assert(snapshot != NULL);

	ast_ari_response_ok(response,
				ast_channel_snapshot_to_json(snapshot, NULL));
}
Example #7
0
void ast_ari_sounds_list(struct ast_variable *headers,
	struct ast_ari_sounds_list_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ao2_container *, sound_files, NULL, ao2_cleanup);
	struct ast_json *sounds_blob;
	RAII_VAR(struct ast_media_index *, sounds_index, ast_sounds_get_index(), ao2_cleanup);

	if (!sounds_index) {
		ast_ari_response_error(response, 500, "Internal Error", "Sounds index not available");
		return;
	}

	sound_files = ast_media_get_media(sounds_index);
	if (!sound_files) {
		ast_ari_response_error(response, 500, "Internal Error", "Allocation Error");
		return;
	}

	sounds_blob = ast_json_array_create();
	if (!sounds_blob) {
		ast_ari_response_error(response, 500, "Internal Error", "Allocation Error");
		return;
	}

	ao2_callback_data(sound_files, OBJ_NODATA, append_sound_cb, sounds_blob, args);

	if (!ast_json_array_size(sounds_blob)) {
		ast_ari_response_error(response, 404, "Not Found", "No sounds found that matched the query");
		return;
	}

	ast_ari_response_ok(response, sounds_blob);
}
Example #8
0
void ast_ari_channels_list(struct ast_variable *headers,
                           struct ast_ari_channels_list_args *args,
                           struct ast_ari_response *response)
{
    RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
    RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
    RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
    struct ao2_iterator i;
    void *obj;
    struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();

    cache = ast_channel_cache();
    if (!cache) {
        ast_ari_response_error(
            response, 500, "Internal Server Error",
            "Message bus not initialized");
        return;
    }
    ao2_ref(cache, +1);

    snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
    if (!snapshots) {
        ast_ari_response_alloc_failed(response);
        return;
    }

    json = ast_json_array_create();
    if (!json) {
        ast_ari_response_alloc_failed(response);
        return;
    }

    for (i = ao2_iterator_init(snapshots, 0);
            (obj = ao2_iterator_next(&i)); ao2_cleanup(obj)) {
        RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
        struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
        int r;

        if (sanitize && sanitize->channel_snapshot
                && sanitize->channel_snapshot(snapshot)) {
            continue;
        }

        r = ast_json_array_append(
                json, ast_channel_snapshot_to_json(snapshot, NULL));
        if (r != 0) {
            ast_ari_response_alloc_failed(response);
            ao2_cleanup(obj);
            ao2_iterator_destroy(&i);
            return;
        }
    }
    ao2_iterator_destroy(&i);

    ast_ari_response_ok(response, ast_json_ref(json));
}
Example #9
0
void ast_ari_asterisk_list_modules(struct ast_variable *headers,
	struct ast_ari_asterisk_list_modules_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	json = ast_json_array_create();
	ast_update_module_list_data(&process_module_list, NULL, json);

	ast_ari_response_ok(response, json);
}
Example #10
0
void ast_ari_get_stored_sound(struct ast_variable *headers, struct ast_get_stored_sound_args *args, struct ast_ari_response *response)
{
	struct ast_json *sound_blob;

	sound_blob = create_sound_blob(args->sound_id, NULL);
	if (!sound_blob) {
		ast_ari_response_error(response, 404, "Not Found", "Sound not found");
		return;
	}

	ast_ari_response_ok(response, sound_blob);
}
void ast_ari_mailboxes_list(struct ast_variable *headers,
	struct ast_ari_mailboxes_list_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	if (!(json = stasis_app_mailboxes_to_json())) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}
Example #12
0
void ast_ari_bridges_get(struct ast_variable *headers,
	struct ast_ari_bridges_get_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_bridge_snapshot *, snapshot, ast_bridge_snapshot_get_latest(args->bridge_id), ao2_cleanup);
	if (!snapshot) {
		ast_ari_response_error(
			response, 404, "Not Found",
			"Bridge not found");
		return;
	}

	ast_ari_response_ok(response,
		ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer()));
}
void ast_ari_device_states_get(struct ast_variable *headers,
	struct ast_ari_device_states_get_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	if (!(json = stasis_app_device_state_to_json(
		      args->device_name, ast_device_state(args->device_name)))) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}
Example #14
0
static void return_sorcery_object(struct ast_sorcery *sorcery, void *sorcery_obj,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, return_set, NULL, ast_json_unref);
	struct ast_variable *change_set;
	struct ast_variable *it_change_set;

	return_set = ast_json_array_create();
	if (!return_set) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	/* Note that we can't use the sorcery JSON change set directly,
	 * as it will hand us back an Object (with fields), and we need
	 * a more generic representation of whatever the API call asked
	 * for, i.e., a list of tuples.
	 */
	change_set = ast_sorcery_objectset_create(sorcery, sorcery_obj);
	if (!change_set) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	for (it_change_set = change_set; it_change_set; it_change_set = it_change_set->next) {
		struct ast_json *tuple;

		tuple = ast_json_pack("{s: s, s: s}",
			"attribute", it_change_set->name,
			"value", it_change_set->value);
		if (!tuple) {
			ast_variables_destroy(change_set);
			ast_ari_response_alloc_failed(response);
			return;
		}

		if (ast_json_array_append(return_set, tuple)) {
			ast_json_unref(tuple);
			ast_variables_destroy(change_set);
			ast_ari_response_alloc_failed(response);
			return;
		}
	}
	ast_variables_destroy(change_set);

	ast_ari_response_ok(response, ast_json_ref(return_set));
}
Example #15
0
void ast_ari_bridges_list(struct ast_variable *headers,
	struct ast_ari_bridges_list_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
	RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	struct ao2_iterator i;
	void *obj;

	cache = ast_bridge_cache();
	if (!cache) {
		ast_ari_response_error(
			response, 500, "Internal Server Error",
			"Message bus not initialized");
		return;
	}
	ao2_ref(cache, +1);

	snapshots = stasis_cache_dump(cache, ast_bridge_snapshot_type());
	if (!snapshots) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	json = ast_json_array_create();
	if (!json) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	i = ao2_iterator_init(snapshots, 0);
	while ((obj = ao2_iterator_next(&i))) {
		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
		struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
		struct ast_json *json_bridge = ast_bridge_snapshot_to_json(snapshot, stasis_app_get_sanitizer());

		if (!json_bridge || ast_json_array_append(json, json_bridge)) {
			ao2_iterator_destroy(&i);
			ast_ari_response_alloc_failed(response);
			return;
		}
	}
	ao2_iterator_destroy(&i);

	ast_ari_response_ok(response, ast_json_ref(json));
}
void ast_ari_mailboxes_get(struct ast_variable *headers,
	struct ast_ari_mailboxes_get_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	switch (stasis_app_mailbox_to_json(args->mailbox_name, &json)) {
	case STASIS_MAILBOX_MISSING:
		ast_ari_response_error(response, 404,
			"Not Found", "Mailbox is does not exist");
		return;
	case STASIS_MAILBOX_ERROR:
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	case STASIS_MAILBOX_OK:
		ast_ari_response_ok(response, json);
	}
}
Example #17
0
void ast_ari_recordings_get_stored_file(struct ast_tcptls_session_instance *ser,
	struct ast_variable *headers, struct ast_ari_recordings_get_stored_file_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_app_stored_recording *, recording,
		stasis_app_stored_recording_find_by_name(args->recording_name),
		ao2_cleanup);
	static const char *format_type_names[AST_MEDIA_TYPE_TEXT + 1] = {
		[AST_MEDIA_TYPE_UNKNOWN] = "binary",
		[AST_MEDIA_TYPE_AUDIO] = "audio",
		[AST_MEDIA_TYPE_VIDEO] = "video",
		[AST_MEDIA_TYPE_IMAGE] = "image",
		[AST_MEDIA_TYPE_TEXT] = "text",
	};
	struct ast_format *format;

	response->message = ast_json_null();

	if (!recording) {
		ast_ari_response_error(response, 404, "Not Found",
			"Recording not found");
		return;
	}

	format = ast_get_format_for_file_ext(stasis_app_stored_recording_get_extension(recording));
	if (!format) {
		ast_ari_response_error(response, 500, "Internal Server Error",
			"Format specified by recording not available or loaded");
		return;
	}

	response->fd = open(stasis_app_stored_recording_get_filename(recording), O_RDONLY);
	if (response->fd < 0) {
		ast_ari_response_error(response, 403, "Forbidden",
			"Recording could not be opened");
		return;
	}

	ast_str_append(&response->headers, 0, "Content-Type: %s/%s\r\n",
		format_type_names[ast_format_get_type(format)],
		stasis_app_stored_recording_get_extension(recording));
	ast_ari_response_ok(response, ast_json_null());
}
Example #18
0
void ast_ari_asterisk_list_log_channels(struct ast_variable *headers,
	struct ast_ari_asterisk_list_log_channels_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;
	int res;

	json = ast_json_array_create();
	res = ast_logger_get_channels(&process_log_list, json);

	if (res == AST_LOGGER_FAILURE) {
		ast_ari_response_error(response, 500, "Internal Server Error",
			"Response body is not valid");
		return;
	} else if (res == AST_LOGGER_ALLOC_ERROR) {
		ast_ari_response_error(response, 500, "Internal Server Error",
			"Allocation Failed");
		return;
	}

	ast_ari_response_ok(response, json);
}
Example #19
0
void ast_ari_recordings_list_stored(struct ast_variable *headers,
	struct ast_ari_recordings_list_stored_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ao2_container *, recordings, NULL, ao2_cleanup);
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	struct ao2_iterator i;
	void *obj;

	recordings = stasis_app_stored_recording_find_all();

	if (!recordings) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	json = ast_json_array_create();
	if (!json) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	i = ao2_iterator_init(recordings, 0);
	while ((obj = ao2_iterator_next(&i))) {
		RAII_VAR(struct stasis_app_stored_recording *, recording, obj,
			ao2_cleanup);

		int r = ast_json_array_append(
			json, stasis_app_stored_recording_to_json(recording));
		if (r != 0) {
			ast_ari_response_alloc_failed(response);
			ao2_iterator_destroy(&i);
			return;
		}
	}
	ao2_iterator_destroy(&i);

	ast_ari_response_ok(response, ast_json_ref(json));
}
Example #20
0
void ast_ari_endpoints_get(struct ast_variable *headers,
	struct ast_ari_endpoints_get_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);

	snapshot = ast_endpoint_latest_snapshot(args->tech, args->resource);
	if (!snapshot) {
		ast_ari_response_error(response, 404, "Not Found",
			"Endpoint not found");
		return;
	}

	json = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
	if (!json) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	ast_ari_response_ok(response, ast_json_ref(json));
}
Example #21
0
void ast_ari_playbacks_get(struct ast_variable *headers,
	struct ast_ari_playbacks_get_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
	struct ast_json *json;

	playback = stasis_app_playback_find_by_id(args->playback_id);
	if (playback == NULL) {
		ast_ari_response_error(response, 404, "Not Found",
			"Playback not found");
		return;
	}

	json = stasis_app_playback_to_json(playback);
	if (json == NULL) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}
Example #22
0
void ast_ari_asterisk_get_info(struct ast_variable *headers,
	struct ast_ari_asterisk_get_info_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	int show_all = args->only_count == 0;
	int show_build = show_all;
	int show_system = show_all;
	int show_config = show_all;
	int show_status = show_all;
	size_t i;
	int res = 0;

	for (i = 0; i < args->only_count; ++i) {
		if (strcasecmp("build", args->only[i]) == 0) {
			show_build = 1;
		} else if (strcasecmp("system", args->only[i]) == 0) {
			show_system = 1;
		} else if (strcasecmp("config", args->only[i]) == 0) {
			show_config = 1;
		} else if (strcasecmp("status", args->only[i]) == 0) {
			show_status = 1;
		} else {
			ast_log(LOG_WARNING, "Unrecognized info section '%s'\n",
				args->only[i]);
		}
	}

	json = ast_json_object_create();

	if (show_build) {
		res |= ast_json_object_set(json, "build",
			ast_json_pack(
				"{ s: s, s: s, s: s,"
				"  s: s, s: s, s: s }",

				"os", ast_build_os,
				"kernel", ast_build_kernel,
				"machine", ast_build_machine,

				"options", AST_BUILDOPTS,
				"date", ast_build_date,
				"user", ast_build_user));
	}

	if (show_system) {
		char eid_str[128];

		ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);

		res |= ast_json_object_set(json, "system",
			ast_json_pack("{ s: s, s: s }",
				"version", ast_get_version(),
				"entity_id", eid_str));
	}

	if (show_config) {
		struct ast_json *config = ast_json_pack(
			"{ s: s, s: s,"
			" s: { s: s, s: s } }",

			"name", ast_config_AST_SYSTEM_NAME,
			"default_language", ast_defaultlanguage,

			"setid",
			"user", ast_config_AST_RUN_USER,
			"group", ast_config_AST_RUN_GROUP);

		res |= ast_json_object_set(json, "config", config);

		if (ast_option_maxcalls) {
			res |= ast_json_object_set(config, "max_channels",
				ast_json_integer_create(ast_option_maxcalls));
		}

		if (ast_option_maxfiles) {
			res |= ast_json_object_set(config, "max_open_files",
				ast_json_integer_create(ast_option_maxfiles));
		}

		if (ast_option_maxload) {
			res |= ast_json_object_set(config, "max_load",
				ast_json_real_create(ast_option_maxload));
		}
	}

	if (show_status) {
		res |= ast_json_object_set(json, "status",
			ast_json_pack("{ s: o, s: o }",
				"startup_time",
				ast_json_timeval(ast_startuptime, NULL),
				"last_reload_time",
				ast_json_timeval(ast_lastreloadtime, NULL)));
	}

	if (res != 0) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	ast_ari_response_ok(response, ast_json_ref(json));
}
Example #23
0
void ast_ari_get_docs(const char *uri, struct ast_variable *headers,
                      struct ast_ari_response *response)
{
    RAII_VAR(struct ast_str *, absolute_path_builder, NULL, ast_free);
    RAII_VAR(char *, absolute_api_dirname, NULL, ast_std_free);
    RAII_VAR(char *, absolute_filename, NULL, ast_std_free);
    struct ast_json *obj = NULL;
    struct ast_variable *host = NULL;
    struct ast_json_error error = {};
    struct stat file_stat;

    ast_debug(3, "%s(%s)\n", __func__, uri);

    absolute_path_builder = ast_str_create(80);
    if (absolute_path_builder == NULL) {
        ast_ari_response_alloc_failed(response);
        return;
    }

    /* absolute path to the rest-api directory */
    ast_str_append(&absolute_path_builder, 0, "%s", ast_config_AST_DATA_DIR);
    ast_str_append(&absolute_path_builder, 0, "/rest-api/");
    absolute_api_dirname = realpath(ast_str_buffer(absolute_path_builder), NULL);
    if (absolute_api_dirname == NULL) {
        ast_log(LOG_ERROR, "Error determining real directory for rest-api\n");
        ast_ari_response_error(
            response, 500, "Internal Server Error",
            "Cannot find rest-api directory");
        return;
    }

    /* absolute path to the requested file */
    ast_str_append(&absolute_path_builder, 0, "%s", uri);
    absolute_filename = realpath(ast_str_buffer(absolute_path_builder), NULL);
    if (absolute_filename == NULL) {
        switch (errno) {
        case ENAMETOOLONG:
        case ENOENT:
        case ENOTDIR:
            ast_ari_response_error(
                response, 404, "Not Found",
                "Resource not found");
            break;
        case EACCES:
            ast_ari_response_error(
                response, 403, "Forbidden",
                "Permission denied");
            break;
        default:
            ast_log(LOG_ERROR,
                    "Error determining real path for uri '%s': %s\n",
                    uri, strerror(errno));
            ast_ari_response_error(
                response, 500, "Internal Server Error",
                "Cannot find file");
            break;
        }
        return;
    }

    if (!ast_begins_with(absolute_filename, absolute_api_dirname)) {
        /* HACKERZ! */
        ast_log(LOG_ERROR,
                "Invalid attempt to access '%s' (not in %s)\n",
                absolute_filename, absolute_api_dirname);
        ast_ari_response_error(
            response, 404, "Not Found",
            "Resource not found");
        return;
    }

    if (stat(absolute_filename, &file_stat) == 0) {
        if (!(file_stat.st_mode & S_IFREG)) {
            /* Not a file */
            ast_ari_response_error(
                response, 403, "Forbidden",
                "Invalid access");
            return;
        }
    } else {
        /* Does not exist */
        ast_ari_response_error(
            response, 404, "Not Found",
            "Resource not found");
        return;
    }

    /* Load resource object from file */
    obj = ast_json_load_new_file(absolute_filename, &error);
    if (obj == NULL) {
        ast_log(LOG_ERROR, "Error parsing resource file: %s:%d(%d) %s\n",
                error.source, error.line, error.column, error.text);
        ast_ari_response_error(
            response, 500, "Internal Server Error",
            "Yikes! Cannot parse resource");
        return;
    }

    /* Update the basePath properly */
    if (ast_json_object_get(obj, "basePath") != NULL) {
        for (host = headers; host; host = host->next) {
            if (strcasecmp(host->name, "Host") == 0) {
                break;
            }
        }
        if (host != NULL) {
            ast_json_object_set(
                obj, "basePath",
                ast_json_stringf("http://%s/ari", host->value));
        } else {
            /* Without the host, we don't have the basePath */
            ast_json_object_del(obj, "basePath");
        }
    }

    ast_ari_response_ok(response, obj);
}
Example #24
0
void ast_ari_recordings_copy_stored(struct ast_variable *headers,
	struct ast_ari_recordings_copy_stored_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_app_stored_recording *, src_recording, NULL,
		ao2_cleanup);
	RAII_VAR(struct stasis_app_stored_recording *, dst_recording, NULL,
		ao2_cleanup);
	struct ast_json *json;
	int res;

	src_recording = stasis_app_stored_recording_find_by_name(
		args->recording_name);
	if (src_recording == NULL) {
		ast_ari_response_error(response, 404, "Not Found",
			"Recording not found");
		return;
	}

	dst_recording = stasis_app_stored_recording_find_by_name(
		args->destination_recording_name);
	if (dst_recording) {
		ast_ari_response_error(response, 409, "Conflict",
			"A recording with the same name already exists on the system");
		return;
	}

	/* See if we got our name rejected */
	switch (errno) {
	case EINVAL:
		ast_ari_response_error(response, 400, "Bad request",
			"Invalid destination recording name");
		return;
	case EACCES:
		ast_ari_response_error(response, 403, "Forbidden",
			"Destination file path is forbidden");
		return;
	default:
		break;
	}

	res = stasis_app_stored_recording_copy(src_recording,
		args->destination_recording_name, &dst_recording);
	if (res) {
		switch (errno) {
		case EACCES:
		case EPERM:
			ast_ari_response_error(response, 500,
				"Internal Server Error",
				"Copy failed");
			break;
		default:
			ast_log(LOG_WARNING,
				"Unexpected error copying recording %s to %s: %s\n",
				args->recording_name, args->destination_recording_name, strerror(errno));
			ast_ari_response_error(response, 500,
				"Internal Server Error",
				"Copy failed");
			break;
		}
		return;
	}

	json = stasis_app_stored_recording_to_json(dst_recording);
	if (json == NULL) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}
Example #25
0
void ast_ari_endpoints_list_by_tech(struct ast_variable *headers,
	struct ast_ari_endpoints_list_by_tech_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
	RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	struct ao2_iterator i;
	void *obj;

	if (!ast_get_channel_tech(args->tech)) {
		ast_ari_response_error(response, 404, "Not Found",
				       "No Endpoints found - invalid tech %s", args->tech);
		return;
	}

	cache = ast_endpoint_cache();
	if (!cache) {
		ast_ari_response_error(
			response, 500, "Internal Server Error",
			"Message bus not initialized");
		return;
	}
	ao2_ref(cache, +1);

	snapshots = stasis_cache_dump(cache, ast_endpoint_snapshot_type());
	if (!snapshots) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	json = ast_json_array_create();
	if (!json) {
		ast_ari_response_alloc_failed(response);
		return;
	}

	i = ao2_iterator_init(snapshots, 0);
	while ((obj = ao2_iterator_next(&i))) {
		RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
		struct ast_endpoint_snapshot *snapshot = stasis_message_data(msg);
		struct ast_json *json_endpoint;
		int r;

		if (strcasecmp(args->tech, snapshot->tech) != 0) {
			continue;
		}

		json_endpoint = ast_endpoint_snapshot_to_json(snapshot, stasis_app_get_sanitizer());
		if (!json_endpoint) {
			continue;
		}

		r = ast_json_array_append(
			json, json_endpoint);
		if (r != 0) {
			ast_ari_response_alloc_failed(response);
			return;
		}
	}
	ao2_iterator_destroy(&i);
	ast_ari_response_ok(response, ast_json_ref(json));
}