remote_provisioning.c 5.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
remote_provisioning.c
Copyright (C) 2010  Belledonne Communications SARL

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

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.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
19
#include "private.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "xml2lpc.h"

#define XML2LPC_CALLBACK_BUFFER_SIZE  1024

static void xml2lpc_callback(void *ctx, xml2lpc_log_level level, const char *fmt, va_list list) {
	char buffer[XML2LPC_CALLBACK_BUFFER_SIZE];
	vsnprintf(buffer, XML2LPC_CALLBACK_BUFFER_SIZE, fmt, list);

	if (level == XML2LPC_ERROR)
		ms_error("%s", buffer);
	else if (level == XML2LPC_WARNING)
		ms_warning("%s", buffer);
	/*else
		ms_message("%s", buffer); // Don't log debug messages */
}

static void linphone_remote_provisioning_apply(LinphoneCore *lc, const char *xml) {
	xml2lpc_context *context = xml2lpc_context_new(xml2lpc_callback, lc);
	int result = xml2lpc_set_xml_string(context, xml);
39
	char * error_msg = NULL;
40
	if (result == 0) {
41 42
		LpConfig * lpc = linphone_core_get_config(lc);
		result = xml2lpc_convert(context, lpc);
43
		if (result == 0) {
44 45 46 47 48 49
			// if the remote provisioning added a proxy config and none was set before, set it
			if (lp_config_has_section(lpc, "proxy_0") && lp_config_get_int(lpc, "sip", "default_proxy", -1) == -1){
				lp_config_set_int(lpc, "sip", "default_proxy", 0);
			}
			lp_config_sync(lpc);

50
		} else {
51
			error_msg = "xml to lpc failed";
52 53
		}
	} else {
54
		error_msg = "invalid xml";
55
	}
56 57 58 59 60

	xml2lpc_context_destroy(context);
	linphone_configuring_terminated(lc
									,error_msg ? LinphoneConfiguringFailed : LinphoneConfiguringSuccessful
									, error_msg);
61 62
}

63
int linphone_remote_provisioning_load_file( LinphoneCore* lc, const char* file_path){
64
	int status = -1;
65
	char* provisioning=ms_load_path_content(file_path, NULL);
66 67 68 69

	if (provisioning){
		linphone_remote_provisioning_apply(lc, provisioning);
		status = 0;
70
		ms_free(provisioning);
71 72 73 74
	}
	return status;
}

75 76
static void belle_request_process_response_event(void *ctx, const belle_http_response_event_t *event) {
	LinphoneCore *lc = (LinphoneCore *)ctx;
77 78
	belle_sip_message_t *message = BELLE_SIP_MESSAGE(event->response);
	const char *body = belle_sip_message_get_body(message);
79

80
	if (belle_http_response_get_status_code(event->response) == 200) {
81
		linphone_remote_provisioning_apply(lc, body);
82
	} else {
83
		linphone_configuring_terminated(lc, LinphoneConfiguringFailed, "http error");
84 85 86 87 88
	}
}

static void belle_request_process_io_error(void *ctx, const belle_sip_io_error_event_t *event) {
	LinphoneCore *lc = (LinphoneCore *)ctx;
89
	linphone_configuring_terminated(lc, LinphoneConfiguringFailed, "http io error");
90 91 92 93
}

static void belle_request_process_timeout(void *ctx, const belle_sip_timeout_event_t *event) {
	LinphoneCore *lc = (LinphoneCore *)ctx;
94
	linphone_configuring_terminated(lc, LinphoneConfiguringFailed, "http timeout");
95 96 97 98
}

static void belle_request_process_auth_requested(void *ctx, belle_sip_auth_event_t *event) {
	LinphoneCore *lc = (LinphoneCore *)ctx;
99
	linphone_configuring_terminated(lc, LinphoneConfiguringFailed, "http auth requested");
100 101
}

102
int linphone_remote_provisioning_download_and_apply(LinphoneCore *lc, const char *remote_provisioning_uri) {
103

104 105
	belle_generic_uri_t *uri=belle_generic_uri_parse(remote_provisioning_uri);
	const char* scheme = uri ? belle_generic_uri_get_scheme(uri) : NULL;
106
	const char *host = uri ? belle_generic_uri_get_host(uri) : NULL;
107 108 109 110

	if( scheme && (strcmp(scheme,"file") == 0) ){
		// We allow for 'local remote-provisioning' in case the file is to be opened from the hard drive.
		const char* file_path = remote_provisioning_uri + strlen("file://"); // skip scheme
111 112 113
		if (uri) {
			belle_sip_object_unref(uri);
		}
114
		return linphone_remote_provisioning_load_file(lc, file_path);
115
	} else if( scheme && strncmp(scheme, "http", 4) == 0 && host && strlen(host) > 0) {
116
		belle_http_request_listener_callbacks_t belle_request_listener={0};
117
		belle_http_request_t *request;
118

119 120 121 122
		belle_request_listener.process_response=belle_request_process_response_event;
		belle_request_listener.process_auth_requested=belle_request_process_auth_requested;
		belle_request_listener.process_io_error=belle_request_process_io_error;
		belle_request_listener.process_timeout=belle_request_process_timeout;
123

Simon Morlat's avatar
Simon Morlat committed
124
		lc->provisioning_http_listener = belle_http_request_listener_create_from_callbacks(&belle_request_listener, lc);
125 126

		request=belle_http_request_create("GET",uri, NULL);
127

Simon Morlat's avatar
Simon Morlat committed
128
		return belle_http_provider_send_request(lc->http_provider, request, lc->provisioning_http_listener);
129
	} else {
130
		ms_error("Invalid provisioning URI [%s] (missing scheme or host ?)",remote_provisioning_uri);
131 132 133
		if (uri) {
			belle_sip_object_unref(uri);
		}
134
		return -1;
135
	}
136 137
}

138 139 140 141
int linphone_core_set_provisioning_uri(LinphoneCore *lc, const char *remote_provisioning_uri) {
	belle_generic_uri_t *uri=remote_provisioning_uri?belle_generic_uri_parse(remote_provisioning_uri):NULL;
	if (!remote_provisioning_uri||uri) {
		lp_config_set_string(lc->config,"misc","config-uri",remote_provisioning_uri);
142 143 144
		if (uri) {
			belle_sip_object_unref(uri);
		}
145 146 147 148 149
		return 0;
	}
	ms_error("Invalid provisioning URI [%s] (could not be parsed)",remote_provisioning_uri);
	return -1;

150 151 152 153 154
}

const char*linphone_core_get_provisioning_uri(const LinphoneCore *lc){
	return lp_config_get_string(lc->config,"misc","config-uri",NULL);
}
155 156 157 158

bool_t linphone_core_is_provisioning_transient(LinphoneCore *lc) {
	return lp_config_get_int(lc->config, "misc", "transient_provisioning", 0) == 1;
}