account_creator.c 52.2 KB
Newer Older
1 2
/*
linphone
3
Copyright (C) 2010-2017 Belledonne Communications SARL
4 5 6 7 8 9 10 11 12 13 14 15 16

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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
*/

20
#include "linphone/account_creator.h"
21
#include "linphone/core.h"
22
#include "linphone/lpconfig.h"
23 24

#include "c-wrapper/c-wrapper.h"
25
#include "linphone/api/c-dial-plan.h"
26

27
#if !_WIN32
28
	#include "regex.h"
29
#endif
30

31 32
#include <bctoolbox/crypto.h>

33 34 35
// TODO: From coreapi. Remove me later.
#include "private.h"

36
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneAccountCreatorCbs);
37

38
BELLE_SIP_INSTANCIATE_VPTR(LinphoneAccountCreatorCbs, belle_sip_object_t,
39 40 41 42 43 44 45
	NULL, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE
);

/************************** Start Misc **************************/
46 47 48 49 50 51
static const char* ha1_for_passwd(const char* username, const char* realm, const char* passwd) {
	static char ha1[33];
	sal_auth_compute_ha1(username, realm, passwd, ha1);
	return ha1;
}

Erwan Croze's avatar
Erwan Croze committed
52
static unsigned int validate_uri(const char* username, const char* domain, const char* display_name) {
53
	LinphoneAddress* addr;
Benjamin REIS's avatar
Benjamin REIS committed
54
	unsigned int status = 0;
Erwan Croze's avatar
Erwan Croze committed
55
	LinphoneProxyConfig* proxy = linphone_proxy_config_new();
Erwan Croze's avatar
Erwan Croze committed
56
	linphone_proxy_config_set_identity(proxy, "sip:?@domain.com");
57 58 59 60 61 62 63

	if (username) {
		addr = linphone_proxy_config_normalize_sip_uri(proxy, username);
	} else {
		addr = linphone_address_clone(linphone_proxy_config_get_identity_address(proxy));
	}

Erwan Croze's avatar
Erwan Croze committed
64 65 66 67
	if (addr == NULL) {
		status = 1;
		goto end;
	}
68

Erwan Croze's avatar
Erwan Croze committed
69 70
	if (domain && linphone_address_set_domain(addr, domain) != 0) {
		status = 1;
71 72
	}

Erwan Croze's avatar
Erwan Croze committed
73 74 75
	if (display_name && (!strlen(display_name) || linphone_address_set_display_name(addr, display_name) != 0)) {
		status = 1;
	}
76
	linphone_address_unref(addr);
Erwan Croze's avatar
Erwan Croze committed
77 78 79
end:
	linphone_proxy_config_destroy(proxy);
	return status;
80 81 82 83 84 85
}

static char* _get_identity(const LinphoneAccountCreator *creator) {
	char *identity = NULL;
	if ((creator->username || creator->phone_number)) {
		//we must escape username
86
		LinphoneProxyConfig* proxy = linphone_core_create_proxy_config(creator->core);
87 88 89
		LinphoneAddress* addr;

		addr = linphone_proxy_config_normalize_sip_uri(proxy, creator->username ? creator->username : creator->phone_number);
90
		if (addr == NULL) goto end;
91 92 93

		identity = linphone_address_as_string(addr);
		linphone_address_unref(addr);
94 95
		end:
		linphone_proxy_config_destroy(proxy);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	}
	return identity;
}

static bool_t is_matching_regex(const char *entry, const char* regex) {
#if _WIN32
	return TRUE;
#else
	regex_t regex_pattern;
	char err_msg[256];
	int res;
	res = regcomp(&regex_pattern, regex, REG_EXTENDED | REG_NOSUB);
	if(res != 0) {
		regerror(res, &regex_pattern, err_msg, sizeof(err_msg));
		ms_error("Could not compile regex '%s: %s", regex, err_msg);
		return FALSE;
	}
	res = regexec(&regex_pattern, entry, 0, NULL, 0);
	regfree(&regex_pattern);
	return (res != REG_NOMATCH);
#endif
}

119
LinphoneProxyConfig * linphone_account_creator_create_proxy_config(const LinphoneAccountCreator *creator) {
120 121 122 123
	LinphoneAuthInfo *info;
	LinphoneProxyConfig *cfg = linphone_core_create_proxy_config(creator->core);
	char *identity_str = _get_identity(creator);
	LinphoneAddress *identity = linphone_address_new(identity_str);
Erwan Croze's avatar
Erwan Croze committed
124

125 126 127 128 129 130 131 132 133 134 135 136 137
	ms_free(identity_str);
	if (creator->display_name) {
		linphone_address_set_display_name(identity, creator->display_name);
	}
	linphone_proxy_config_set_identity_address(cfg, identity);
	if (creator->phone_country_code) {
		linphone_proxy_config_set_dial_prefix(cfg, creator->phone_country_code);
	} else if (creator->phone_number) {
		int dial_prefix_number = linphone_dial_plan_lookup_ccc_from_e164(creator->phone_number);
		char buff[4];
		snprintf(buff, sizeof(buff), "%d", dial_prefix_number);
		linphone_proxy_config_set_dial_prefix(cfg, buff);
	}
Erwan Croze's avatar
Erwan Croze committed
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
	linphone_proxy_config_enable_register(cfg, TRUE);

	info = linphone_auth_info_new(linphone_address_get_username(identity), // username
								NULL, //user id
								creator->password, // passwd
								creator->password ? NULL : creator->ha1,  // ha1
								!creator->password && creator->ha1 ? linphone_address_get_domain(identity) : NULL,  // realm - assumed to be domain
								linphone_address_get_domain(identity) // domain
	);
	linphone_core_add_auth_info(creator->core, info);
	linphone_address_unref(identity);

	if (linphone_core_add_proxy_config(creator->core, cfg) != -1) {
		linphone_core_set_default_proxy(creator->core, cfg);
		return cfg;
	}

	linphone_core_remove_auth_info(creator->core, info);
	return NULL;
}
Erwan Croze's avatar
Erwan Croze committed
159 160 161 162

LinphoneProxyConfig * linphone_account_creator_configure(const LinphoneAccountCreator *creator) {
	return linphone_account_creator_create_proxy_config(creator);
}
163 164 165 166
/************************** End Misc **************************/

/************************** Start Account Creator Cbs **************************/

167 168
static LinphoneAccountCreatorCbs * linphone_account_creator_cbs_new(void) {
	return belle_sip_object_new(LinphoneAccountCreatorCbs);
169 170
}

171 172 173
LinphoneAccountCreatorCbs * linphone_account_creator_cbs_ref(LinphoneAccountCreatorCbs *cbs) {
	belle_sip_object_ref(cbs);
	return cbs;
174 175
}

176 177
void linphone_account_creator_cbs_unref(LinphoneAccountCreatorCbs *cbs) {
	belle_sip_object_unref(cbs);
178 179
}

180 181
void *linphone_account_creator_cbs_get_user_data(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->user_data;
182 183
}

184 185
void linphone_account_creator_cbs_set_user_data(LinphoneAccountCreatorCbs *cbs, void *ud) {
	cbs->user_data = ud;
186 187
}

188 189
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_create_account(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->create_account_response_cb;
190 191
}

192 193
void linphone_account_creator_cbs_set_create_account(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->create_account_response_cb = cb;
194 195
}

196 197
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_is_account_exist(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->is_account_exist_response_cb;
198 199
}

200 201
void linphone_account_creator_cbs_set_is_account_exist(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->is_account_exist_response_cb = cb;
202 203
}

204 205
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_activate_account(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->activate_account_response_cb;
206 207
}

208 209
void linphone_account_creator_cbs_set_activate_account(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->activate_account_response_cb = cb;
210 211
}

212 213
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_is_account_activated(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->is_account_activated_response_cb;
214 215
}

216 217
void linphone_account_creator_cbs_set_is_account_activated(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->is_account_activated_response_cb = cb;
218 219
}

220 221
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_link_account(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->link_account_response_cb;
222 223
}

224 225
void linphone_account_creator_cbs_set_link_account(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->link_account_response_cb = cb;
226 227
}

228 229
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_activate_alias(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->activate_alias_response_cb;
230
}
231

232 233
void linphone_account_creator_cbs_set_activate_alias(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->activate_alias_response_cb = cb;
234 235
}

236 237
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_is_alias_used(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->is_alias_used_response_cb;
238 239
}

240 241
void linphone_account_creator_cbs_set_is_alias_used(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->is_alias_used_response_cb = cb;
242 243
}

244 245
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_is_account_linked(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->is_account_linked_response_cb;
246
}
247

248 249
void linphone_account_creator_cbs_set_is_account_linked(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->is_account_linked_response_cb = cb;
250 251
}

252 253
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_recover_account(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->recover_account_response_cb;
254 255
}

256 257
void linphone_account_creator_cbs_set_recover_account(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->recover_account_response_cb = cb;
258 259
}

260 261
LinphoneAccountCreatorCbsStatusCb linphone_account_creator_cbs_get_update_account(const LinphoneAccountCreatorCbs *cbs) {
	return cbs->update_account_response_cb;
262 263
}

264 265
void linphone_account_creator_cbs_set_update_account(LinphoneAccountCreatorCbs *cbs, LinphoneAccountCreatorCbsStatusCb cb) {
	cbs->update_account_response_cb = cb;
266 267 268 269
}
/************************** End Account Creator Cbs **************************/

/************************** Start Account Creator data **************************/
270
static void _linphone_account_creator_destroy(LinphoneAccountCreator *creator) {
271 272
	/*this will drop all pending requests if any*/
	if (creator->xmlrpc_session) linphone_xml_rpc_session_release(creator->xmlrpc_session);
273 274 275
	if (creator->service != NULL && linphone_account_creator_service_get_destructor_cb(creator->service) != NULL)
		linphone_account_creator_service_get_destructor_cb(creator->service)(creator);
	linphone_account_creator_cbs_unref(creator->cbs);
276
	linphone_proxy_config_unref(creator->proxy_cfg);
277
	linphone_account_creator_reset(creator);
278 279 280 281 282 283 284 285 286 287 288
}

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphoneAccountCreator);

BELLE_SIP_INSTANCIATE_VPTR(LinphoneAccountCreator, belle_sip_object_t,
	(belle_sip_object_destroy_t)_linphone_account_creator_destroy,
	NULL, // clone
	NULL, // marshal
	FALSE
);

289
LinphoneAccountCreator * _linphone_account_creator_new(LinphoneCore *core, const char *xmlrpc_url) {
290
	LinphoneAccountCreator *creator;
Erwan Croze's avatar
Erwan Croze committed
291
	const char* domain = lp_config_get_string(core->config, "assistant", "domain", NULL);
292
	creator = belle_sip_object_new(LinphoneAccountCreator);
293 294
	creator->service = linphone_core_get_account_creator_service(core);
	creator->cbs = linphone_account_creator_cbs_new();
295
	creator->core = core;
Erwan Croze's avatar
Erwan Croze committed
296
	creator->transport = LinphoneTransportTcp;
297
	creator->xmlrpc_session = (xmlrpc_url) ? linphone_xml_rpc_session_new(core, xmlrpc_url) : NULL;
Erwan Croze's avatar
Erwan Croze committed
298 299 300
	if (domain) {
		linphone_account_creator_set_domain(creator, domain);
	}
301
	creator->proxy_cfg = linphone_core_create_proxy_config(core);
302 303
	if (creator->service != NULL && linphone_account_creator_service_get_constructor_cb(creator->service) != NULL)
		linphone_account_creator_service_get_constructor_cb(creator->service)(creator);
304 305 306
	return creator;
}

307 308 309 310
LinphoneAccountCreator * linphone_account_creator_new(LinphoneCore *core, const char *xmlrpc_url) {
	return _linphone_account_creator_new(core, xmlrpc_url);
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
#define _reset_field(field) \
	if (field) { \
		ms_free(field); \
		field = NULL; \
	}

void linphone_account_creator_reset(LinphoneAccountCreator *creator) {
	_reset_field(creator->username);
	_reset_field(creator->display_name);
	_reset_field(creator->password);
	_reset_field(creator->ha1);
	_reset_field(creator->phone_number);
	_reset_field(creator->phone_country_code);
	_reset_field(creator->email);
	_reset_field(creator->language);
	_reset_field(creator->activation_code);
	_reset_field(creator->domain);
	_reset_field(creator->route);
}

#undef _reset_field

333 334 335 336
LinphoneAccountCreator * linphone_core_create_account_creator(LinphoneCore *core, const char *xmlrpc_url) {
	return _linphone_account_creator_new(core, xmlrpc_url);
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
LinphoneAccountCreator * linphone_account_creator_ref(LinphoneAccountCreator *creator) {
	belle_sip_object_ref(creator);
	return creator;
}

void linphone_account_creator_unref(LinphoneAccountCreator *creator) {
	belle_sip_object_unref(creator);
}

void *linphone_account_creator_get_user_data(const LinphoneAccountCreator *creator) {
	return creator->user_data;
}

void linphone_account_creator_set_user_data(LinphoneAccountCreator *creator, void *ud) {
	creator->user_data = ud;
}

354
LinphoneAccountCreatorUsernameStatus linphone_account_creator_set_username(LinphoneAccountCreator *creator, const char *username) {
355 356
	int min_length = lp_config_get_int(creator->core->config, "assistant", "username_min_length", -1);
	int max_length = lp_config_get_int(creator->core->config, "assistant", "username_max_length", -1);
Ronan's avatar
Ronan committed
357
	bool_t use_phone_number = !!lp_config_get_int(creator->core->config, "assistant", "use_phone_number", 0);
358
	const char* regex = lp_config_get_string(creator->core->config, "assistant", "username_regex", 0);
359 360
	if (!username) {
		creator->username = NULL;
361
		return LinphoneAccountCreatorUsernameStatusOk;
362
	} else if (min_length > 0 && strlen(username) < (size_t)min_length) {
363
		return LinphoneAccountCreatorUsernameStatusTooShort;
364
	} else if (max_length > 0 && strlen(username) > (size_t)max_length) {
365
		return LinphoneAccountCreatorUsernameStatusTooLong;
366
	} else if (use_phone_number && !linphone_proxy_config_is_phone_number(NULL, username)) {
367
		return LinphoneAccountCreatorUsernameStatusInvalid;
368
	} else if (regex && !is_matching_regex(username, regex)) {
369
		return LinphoneAccountCreatorUsernameStatusInvalidCharacters;
Erwan Croze's avatar
Erwan Croze committed
370
	} else if (validate_uri(username, NULL, NULL) != 0) {
371
		return LinphoneAccountCreatorUsernameStatusInvalid;
372
	}
373

374
	set_string(&creator->username, username, TRUE);
375
	return LinphoneAccountCreatorUsernameStatusOk;
376 377 378 379 380 381
}

const char * linphone_account_creator_get_username(const LinphoneAccountCreator *creator) {
	return creator->username;
}

382
LinphoneAccountCreatorPhoneNumberStatusMask linphone_account_creator_set_phone_number(LinphoneAccountCreator *creator, const char *phone_number, const char *country_code) {
383
	char *normalized_phone_number;
384
	LinphoneAccountCreatorPhoneNumberStatusMask return_status = 0;
385
	if (!phone_number || !country_code) {
386 387
		if (!phone_number && !country_code) {
			creator->phone_number = NULL;
388
			creator->phone_country_code = NULL;
389
			return (LinphoneAccountCreatorPhoneNumberStatusMask)LinphoneAccountCreatorPhoneNumberStatusOk;
390
		} else {
391
			return (LinphoneAccountCreatorPhoneNumberStatusMask)LinphoneAccountCreatorPhoneNumberStatusInvalid;
392
		}
393
	} else {
394
		LinphoneProxyConfig *numCfg = creator->proxy_cfg;
395 396
		creator->phone_country_code = ms_strdup(country_code[0] == '+' ? &country_code[1] : country_code);
		linphone_proxy_config_set_dial_prefix(numCfg, creator->phone_country_code);
397 398
		normalized_phone_number = linphone_proxy_config_normalize_phone_number(numCfg, phone_number);
		if (!normalized_phone_number) {
399
			return LinphoneAccountCreatorPhoneNumberStatusInvalid;
400 401 402 403
		}

		// if phone is valid, we lastly want to check that length is OK
		{
Erwan Croze's avatar
Erwan Croze committed
404
			const LinphoneDialPlan* plan = linphone_dial_plan_by_ccc(creator->phone_country_code);
Ghislain MARY's avatar
Ghislain MARY committed
405
			int size = (int)strlen(phone_number);
406
			if (linphone_dial_plan_is_generic(plan)) {
407
				return_status = LinphoneAccountCreatorPhoneNumberStatusInvalidCountryCode;
408
			}
409
			if (size < linphone_dial_plan_get_national_number_length(plan) - 1) {
410
				return_status += LinphoneAccountCreatorPhoneNumberStatusTooShort;
Erwan Croze's avatar
Erwan Croze committed
411
				goto end;
412
			} else if (size > linphone_dial_plan_get_national_number_length(plan) + 1) {
413
				return_status += LinphoneAccountCreatorPhoneNumberStatusTooLong;
414
				goto end;
415
			} else if (return_status & LinphoneAccountCreatorPhoneNumberStatusInvalidCountryCode) {
Erwan Croze's avatar
Erwan Croze committed
416
				goto end;
417 418 419
			}
		}
	}
420
	set_string(&creator->phone_number, normalized_phone_number, TRUE);
421
	return_status = LinphoneAccountCreatorPhoneNumberStatusOk;
Erwan Croze's avatar
Erwan Croze committed
422 423 424
end:
	ms_free(normalized_phone_number);
	return return_status;
425 426 427 428 429 430
}

const char * linphone_account_creator_get_phone_number(const LinphoneAccountCreator *creator) {
	return creator->phone_number;
}

431
LinphoneAccountCreatorPasswordStatus linphone_account_creator_set_password(LinphoneAccountCreator *creator, const char *password) {
432 433
	int min_length = lp_config_get_int(creator->core->config, "assistant", "password_min_length", -1);
	int max_length = lp_config_get_int(creator->core->config, "assistant", "password_max_length", -1);
434 435
	if (!password) {
		creator->password = NULL;
436
		return LinphoneAccountCreatorPasswordStatusTooShort;
437
	}
438
	if (min_length > 0 && strlen(password) < (size_t)min_length) {
439
		return LinphoneAccountCreatorPasswordStatusTooShort;
440
	} else if (max_length > 0 && strlen(password) > (size_t)max_length) {
441
		return LinphoneAccountCreatorPasswordStatusTooLong;
442
	}
443
	set_string(&creator->password, password, FALSE);
444
	return LinphoneAccountCreatorPasswordStatusOk;
445 446 447 448 449 450
}

const char * linphone_account_creator_get_password(const LinphoneAccountCreator *creator) {
	return creator->password;
}

451
LinphoneAccountCreatorPasswordStatus linphone_account_creator_set_ha1(LinphoneAccountCreator *creator, const char *ha1){
Margaux Clerc's avatar
Margaux Clerc committed
452
	set_string(&creator->ha1, ha1, FALSE);
453
	return LinphoneAccountCreatorPasswordStatusOk;
Margaux Clerc's avatar
Margaux Clerc committed
454 455 456 457 458 459
}

const char * linphone_account_creator_get_ha1(const LinphoneAccountCreator *creator) {
	return creator->ha1;
}

460
LinphoneAccountCreatorActivationCodeStatus linphone_account_creator_set_activation_code(LinphoneAccountCreator *creator, const char *activation_code){
461
	set_string(&creator->activation_code, activation_code, FALSE);
462
	return LinphoneAccountCreatorActivationCodeStatusOk;
463 464
}

465 466 467 468
const char * linphone_account_creator_get_activation_code(const LinphoneAccountCreator *creator) {
	return creator->activation_code;
}

469
LinphoneAccountCreatorLanguageStatus linphone_account_creator_set_language(LinphoneAccountCreator *creator, const char *lang) {
470
	set_string(&creator->language, lang, FALSE);
471
	return LinphoneAccountCreatorLanguageStatusOk;
472 473 474 475
}

const char * linphone_account_creator_get_language(const LinphoneAccountCreator *creator) {
	return creator->language;
476 477
}

478
LinphoneAccountCreatorUsernameStatus linphone_account_creator_set_display_name(LinphoneAccountCreator *creator, const char *display_name) {
Erwan Croze's avatar
Erwan Croze committed
479
	if (validate_uri(NULL, display_name, NULL) != 0) {
480
		return LinphoneAccountCreatorUsernameStatusInvalid;
481
	}
482
	set_string(&creator->display_name, display_name, FALSE);
483
	return LinphoneAccountCreatorUsernameStatusOk;
484 485
}

486 487
const char * linphone_account_creator_get_display_name(const LinphoneAccountCreator *creator) {
	return creator->display_name;
488 489
}

490
LinphoneAccountCreatorEmailStatus linphone_account_creator_set_email(LinphoneAccountCreator *creator, const char *email) {
491
	if (!email || !is_matching_regex(email, "^.+@.+\\..*$")) {
492
		return LinphoneAccountCreatorEmailStatusMalformed;
493 494
	}
	if (!is_matching_regex(email, "^.+@.+\\.[A-Za-z]{2}[A-Za-z]*$")) {
495
		return LinphoneAccountCreatorEmailStatusInvalidCharacters;
496
	}
497
	set_string(&creator->email, email, TRUE);
498
	return LinphoneAccountCreatorEmailStatusOk;
499 500
}

501 502
const char * linphone_account_creator_get_email(const LinphoneAccountCreator *creator) {
	return creator->email;
503 504
}

Erwan Croze's avatar
Erwan Croze committed
505
LinphoneAccountCreatorDomainStatus linphone_account_creator_set_domain(LinphoneAccountCreator *creator, const char *domain) {
Erwan Croze's avatar
Erwan Croze committed
506
	if (domain && validate_uri(NULL, domain, NULL) != 0) {
Erwan Croze's avatar
Erwan Croze committed
507
		return LinphoneAccountCreatorDomainInvalid;
Erwan Croze's avatar
Erwan Croze committed
508 509 510
	}

	set_string(&creator->domain, domain, TRUE);
Erwan Croze's avatar
Erwan Croze committed
511
	return LinphoneAccountCreatorDomainOk;
Erwan Croze's avatar
Erwan Croze committed
512 513 514 515 516 517
 }

const char * linphone_account_creator_get_domain(const LinphoneAccountCreator *creator) {
	return creator->domain;
}

Erwan Croze's avatar
Erwan Croze committed
518
LinphoneAccountCreatorTransportStatus linphone_account_creator_set_transport(LinphoneAccountCreator *creator, LinphoneTransportType transport) {
Erwan Croze's avatar
Erwan Croze committed
519
	if (!linphone_core_sip_transport_supported(creator->core, transport)) {
Erwan Croze's avatar
Erwan Croze committed
520
		return LinphoneAccountCreatorTransportUnsupported;
Erwan Croze's avatar
Erwan Croze committed
521 522
	}
	creator->transport = transport;
Erwan Croze's avatar
Erwan Croze committed
523
	return LinphoneAccountCreatorTransportOk;
Erwan Croze's avatar
Erwan Croze committed
524 525 526 527 528 529
}

LinphoneTransportType linphone_account_creator_get_transport(const LinphoneAccountCreator *creator) {
	return creator->transport;
}

530
LinphoneAccountCreatorStatus linphone_account_creator_set_route(LinphoneAccountCreator *creator, const char *route) {
Erwan Croze's avatar
Erwan Croze committed
531
	if (!route || linphone_proxy_config_set_route(creator->proxy_cfg, route) != 0)
532
		return LinphoneAccountCreatorStatusRequestFailed;
Erwan Croze's avatar
Erwan Croze committed
533 534

	set_string(&creator->route, route, TRUE);
535
	return LinphoneAccountCreatorStatusRequestOk;
Erwan Croze's avatar
Erwan Croze committed
536 537 538 539 540 541
}

const char * linphone_account_creator_get_route(const LinphoneAccountCreator *creator) {
	return creator->route;
}

542 543
LinphoneAccountCreatorCbs * linphone_account_creator_get_callbacks(const LinphoneAccountCreator *creator) {
	return creator->cbs;
544 545
}

546 547
LinphoneAccountCreatorService * linphone_account_creator_get_service(const LinphoneAccountCreator *creator) {
	return creator->service;
548 549
}

550 551 552 553
LinphoneAccountCreatorStatus linphone_account_creator_is_account_exist(LinphoneAccountCreator *creator) {
	if (creator->service->is_account_exist_request_cb == NULL
		|| creator->cbs->is_account_exist_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
554
	}
555

556
	return creator->service->is_account_exist_request_cb(creator);
557 558
}

559 560 561 562
LinphoneAccountCreatorStatus linphone_account_creator_create_account(LinphoneAccountCreator *creator) {
	if (creator->service->create_account_request_cb == NULL
		|| creator->cbs->create_account_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
563
	}
564

565
	return creator->service->create_account_request_cb(creator);
566 567
}

568 569 570 571
LinphoneAccountCreatorStatus linphone_account_creator_is_account_activated(LinphoneAccountCreator *creator) {
	if (creator->service->is_account_activated_request_cb == NULL
		|| creator->cbs->is_account_activated_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
572 573
	}

574
	return creator->service->is_account_activated_request_cb(creator);
575 576
}

577 578 579 580
LinphoneAccountCreatorStatus linphone_account_creator_activate_account(LinphoneAccountCreator *creator) {
	if (creator->service->activate_account_request_cb == NULL
		|| creator->cbs->activate_account_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
581 582
	}

583
	return creator->service->activate_account_request_cb(creator);
584 585
}

586 587 588 589
LinphoneAccountCreatorStatus linphone_account_creator_link_account(LinphoneAccountCreator *creator) {
	if (creator->service->link_account_request_cb == NULL
		|| creator->cbs->link_account_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
590
	}
591

592
	return creator->service->link_account_request_cb(creator);
593 594
}

595 596 597 598
LinphoneAccountCreatorStatus linphone_account_creator_activate_alias(LinphoneAccountCreator *creator) {
	if (creator->service->activate_alias_request_cb == NULL
		|| creator->cbs->activate_alias_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
599
	}
600

601
	return creator->service->activate_alias_request_cb(creator);
602 603
}

604 605 606 607
LinphoneAccountCreatorStatus linphone_account_creator_is_alias_used(LinphoneAccountCreator *creator) {
	if (creator->service->is_alias_used_request_cb == NULL
		|| creator->cbs->is_alias_used_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
608
	}
609

610
	return creator->service->is_alias_used_request_cb(creator);
611 612
}

613 614 615 616
LinphoneAccountCreatorStatus linphone_account_creator_is_account_linked(LinphoneAccountCreator *creator) {
	if (creator->service->is_account_linked_request_cb == NULL
		|| creator->cbs->is_account_linked_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
617
	}
618

619
	return creator->service->is_account_linked_request_cb(creator);
620 621
}

622 623 624 625
LinphoneAccountCreatorStatus linphone_account_creator_recover_account(LinphoneAccountCreator *creator) {
	if (creator->service->recover_account_request_cb == NULL
		|| creator->cbs->recover_account_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
626 627
	}

628
	return creator->service->recover_account_request_cb(creator);
629
}
630

631 632 633 634
LinphoneAccountCreatorStatus linphone_account_creator_update_account(LinphoneAccountCreator *creator) {
	if (creator->service->update_account_request_cb == NULL
		|| creator->cbs->update_account_response_cb == NULL) {
		return LinphoneAccountCreatorStatusMissingCallbacks;
635 636
	}

637
	return creator->service->update_account_request_cb(creator);
638
}
639 640 641
/************************** End Account Creator data **************************/

/************************** Start Account Creator Linphone **************************/
642

643
LinphoneAccountCreatorStatus linphone_account_creator_constructor_linphone(LinphoneAccountCreator *creator) {
644
	LinphoneAddress *addr;
645 646 647 648 649 650 651 652
	const char *identity = lp_config_get_default_string(creator->core->config, "proxy", "reg_identity", NULL);
	const char *proxy = lp_config_get_default_string(creator->core->config, "proxy", "reg_proxy", NULL);
	const char *route = lp_config_get_default_string(creator->core->config, "proxy", "reg_route", NULL);
	const char *realm = lp_config_get_default_string(creator->core->config, "proxy", "realm", NULL);
	linphone_proxy_config_set_realm(creator->proxy_cfg, realm ? realm : "sip.linphone.org");
	linphone_proxy_config_set_route(creator->proxy_cfg, route ? route : "sip.linphone.org");
	linphone_proxy_config_set_server_addr(creator->proxy_cfg, proxy ? proxy : "sip.linphone.org");
	addr = linphone_address_new(identity ? identity : "sip:username@sip.linphone.org");
653 654
	linphone_proxy_config_set_identity_address(creator->proxy_cfg, addr);
	linphone_address_unref(addr);
655
	return LinphoneAccountCreatorStatusRequestOk;
Erwan Croze's avatar
Erwan Croze committed
656 657
}

658
/****************** START OF ACCOUNT USED SECTION *****************************/
659
static void _is_account_exist_response_cb(LinphoneXmlRpcRequest *request) {
660
	LinphoneAccountCreator *creator = (LinphoneAccountCreator *)linphone_xml_rpc_request_get_user_data(request);
661 662
	if (creator->cbs->is_account_exist_response_cb != NULL) {
		LinphoneAccountCreatorStatus status = LinphoneAccountCreatorStatusRequestFailed;
663
		const char* resp = linphone_xml_rpc_request_get_string_response(request);
664
		if (linphone_xml_rpc_request_get_status(request) == LinphoneXmlRpcStatusOk) {
665 666 667 668
			status = (strcmp(resp, "ERROR_ACCOUNT_DOESNT_EXIST") == 0) ? LinphoneAccountCreatorStatusAccountNotExist : (
							(strcmp(resp, "ERROR_ALIAS_DOESNT_EXIST") == 0) ? LinphoneAccountCreatorStatusAccountExist :
														LinphoneAccountCreatorStatusAccountExistWithAlias);
			if (status == LinphoneAccountCreatorStatusAccountExistWithAlias) {
669 670
				set_string(&creator->phone_number, resp, FALSE);
			}
671
		}
672
		creator->cbs->is_account_exist_response_cb(creator, status, resp);
673 674 675
	}
}

676
LinphoneAccountCreatorStatus linphone_account_creator_is_account_exist_linphone(LinphoneAccountCreator *creator) {
677
	LinphoneXmlRpcRequest *request;
678
	if (!creator->username && !creator->phone_number) {
679 680
		if (creator->cbs->is_account_exist_response_cb != NULL) {
			creator->cbs->is_account_exist_response_cb(creator, LinphoneAccountCreatorStatusMissingArguments, "Missing required parameters");
681
		}
682
		return LinphoneAccountCreatorStatusMissingArguments;
683
	}
684

685 686 687 688
	ms_debug("Account creator: is_account_exist (%s=%s, domain=%s)",
		(creator->username) ? "username" : "phone number",
		(creator->username) ? creator->username : creator->phone_number,
		linphone_proxy_config_get_domain(creator->proxy_cfg));
Ghislain MARY's avatar
Ghislain MARY committed
689 690 691
	request = linphone_xml_rpc_request_new(LinphoneXmlRpcArgString, "get_phone_number_for_account");
	linphone_xml_rpc_request_add_string_arg(request, creator->username ? creator->username : creator->phone_number);
	linphone_xml_rpc_request_add_string_arg(request, linphone_proxy_config_get_domain(creator->proxy_cfg));
692
	linphone_xml_rpc_request_set_user_data(request, creator);
693
	linphone_xml_rpc_request_cbs_set_response(linphone_xml_rpc_request_get_callbacks(request), _is_account_exist_response_cb);
694 695
	linphone_xml_rpc_session_send_request(creator->xmlrpc_session, request);
	linphone_xml_rpc_request_unref(request);
696

697
	return LinphoneAccountCreatorStatusRequestOk;
698
}
699
/****************** END OF CREATE ACCOUNT USED SECTION ************************/
700

701
/****************** START OF CREATE ACCOUNT SECTION ***************************/
702
static void _create_account_cb_custom(LinphoneXmlRpcRequest *request) {
703
	LinphoneAccountCreator *creator = (LinphoneAccountCreator *)linphone_xml_rpc_request_get_user_data(request);
704 705
	if (creator->cbs->create_account_response_cb != NULL) {
		LinphoneAccountCreatorStatus status = LinphoneAccountCreatorStatusRequestFailed;
706
		const char* resp = linphone_xml_rpc_request_get_string_response(request);
707
		if (linphone_xml_rpc_request_get_status(request) == LinphoneXmlRpcStatusOk) {
708 709 710 711 712
			status = (strcmp(resp, "OK") == 0) ? LinphoneAccountCreatorStatusAccountCreated
			: (strcmp(resp, "ERROR_CANNOT_SEND_SMS") == 0) ? LinphoneAccountCreatorStatusServerError
			: (strcmp(resp, "ERROR_ACCOUNT_ALREADY_IN_USE") == 0) ? LinphoneAccountCreatorStatusAccountExist
			: (strcmp(resp, "ERROR_ALIAS_ALREADY_IN_USE") == 0) ? LinphoneAccountCreatorStatusAccountExistWithAlias
			:LinphoneAccountCreatorStatusAccountNotCreated;
713
		}
714
		creator->cbs->create_account_response_cb(creator, status, resp);
715 716 717
	}
}

718
static LinphoneXmlRpcRequest * _create_account_with_phone_custom(LinphoneAccountCreator *creator) {
719 720 721
	LinphoneXmlRpcRequest *request;
	if (!creator->phone_number) {
		return NULL;
722
	}
723 724 725 726 727 728
	ms_debug("Account creator: create_account_with_phone (phone number=%s, username=%s, domain=%s, language=%s)",
		creator->phone_number,
		(creator->username) ? creator->username : creator->phone_number,
		linphone_proxy_config_get_domain(creator->proxy_cfg),
		creator->language);

Ghislain MARY's avatar
Ghislain MARY committed
729 730 731 732 733 734 735 736
	request = linphone_xml_rpc_request_new(LinphoneXmlRpcArgString, "create_phone_account");
	linphone_xml_rpc_request_add_string_arg(request, creator->phone_number);
	linphone_xml_rpc_request_add_string_arg(request, creator->username ? creator->username : creator->phone_number);
	linphone_xml_rpc_request_add_string_arg(request, creator->password ?
		ha1_for_passwd(creator->username ? creator->username : creator->phone_number, linphone_proxy_config_get_domain(creator->proxy_cfg), creator->password) : "");
	linphone_xml_rpc_request_add_string_arg(request, linphone_core_get_user_agent(creator->core));
	linphone_xml_rpc_request_add_string_arg(request, linphone_proxy_config_get_domain(creator->proxy_cfg));
	linphone_xml_rpc_request_add_string_arg(request, creator->language);
737
	return request;
738 739
}

740
static LinphoneXmlRpcRequest * _create_account_with_email_custom(LinphoneAccountCreator *creator) {
741
	LinphoneXmlRpcRequest *request;
742
	if (!creator->username || !creator->email || !creator->password) {
743 744
		return NULL;
	}
745 746 747 748 749
	ms_debug("Account creator: create_account_with_email (username=%s, email=%s, domain=%s)",
		creator->username,
		creator->email,
		linphone_proxy_config_get_domain(creator->proxy_cfg));

Ghislain MARY's avatar
Ghislain MARY committed
750 751 752 753 754 755 756
	request = linphone_xml_rpc_request_new(LinphoneXmlRpcArgString, "create_email_account");
	linphone_xml_rpc_request_add_string_arg(request, creator->username);
	linphone_xml_rpc_request_add_string_arg(request, creator->email);
	linphone_xml_rpc_request_add_string_arg(request,
		ha1_for_passwd(creator->username ? creator->username : creator->phone_number, linphone_proxy_config_get_domain(creator->proxy_cfg), creator->password));
	linphone_xml_rpc_request_add_string_arg(request, linphone_core_get_user_agent(creator->core));
	linphone_xml_rpc_request_add_string_arg(request, linphone_proxy_config_get_domain(creator->proxy_cfg));
757
	return request;
758 759
}

760
LinphoneAccountCreatorStatus linphone_account_creator_create_account_linphone(LinphoneAccountCreator *creator) {
761
	LinphoneXmlRpcRequest *request;
762
	char *identity = _get_identity(creator);
763 764
	if (!identity || (!(request = _create_account_with_phone_custom(creator))
		&& !(request = _create_account_with_email_custom(creator)))) {
765 766
		if (creator->cbs->create_account_response_cb != NULL) {
			creator->cbs->create_account_response_cb(creator, LinphoneAccountCreatorStatusMissingArguments, "Missing required parameters");
767
		}
768
		if (identity) ms_free(identity);
769
		return LinphoneAccountCreatorStatusMissingArguments;
770
	}
771

772
	linphone_xml_rpc_request_set_user_data(request, creator);
773
	linphone_xml_rpc_request_cbs_set_response(linphone_xml_rpc_request_get_callbacks(request), _create_account_cb_custom);
774 775 776
	linphone_xml_rpc_session_send_request(creator->xmlrpc_session, request);
	linphone_xml_rpc_request_unref(request);
	ms_free(identity);
777
	return LinphoneAccountCreatorStatusRequestOk;
778
}
779
/****************** END OF CREATE ACCOUNT SECTION *****************************/
780

781
/****************** START OF VALIDATE ACCOUNT SECTION *************************/
782
static void _activate_account_cb_custom(LinphoneXmlRpcRequest *request) {
783
	LinphoneAccountCreator *creator = (LinphoneAccountCreator *)linphone_xml_rpc_request_get_user_data(request);
784 785
	if (creator->cbs->activate_account_response_cb != NULL) {
		LinphoneAccountCreatorStatus status = LinphoneAccountCreatorStatusRequestFailed;
786
		const char* resp = linphone_xml_rpc_request_get_string_response(request);
787
		if (linphone_xml_rpc_request_get_status(request) == LinphoneXmlRpcStatusOk) {
788
			if (strcmp(resp, "ERROR_ACCOUNT_ALREADY_ACTIVATED") == 0) {
789
				status = LinphoneAccountCreatorStatusAccountAlreadyActivated;
790
			} else if (strstr(resp, "ERROR_") == resp) {
791
				status = LinphoneAccountCreatorStatusAccountNotActivated;
792
			} else {
793
				status = LinphoneAccountCreatorStatusAccountActivated;
794 795
				set_string(&creator->ha1, resp, FALSE);
			}
796
		}
797
		creator->cbs->activate_account_response_cb(creator, status, resp);
798 799 800
	}
}

801
LinphoneAccountCreatorStatus linphone_account_creator_activate_account_linphone(LinphoneAccountCreator *creator) {
802
	LinphoneXmlRpcRequest *request;
803
	if (!creator->phone_number || !creator->activation_code) {
804 805
		if (creator->cbs->is_account_activated_response_cb != NULL) {
			creator->cbs->is_account_activated_response_cb(creator, LinphoneAccountCreatorStatusMissingArguments, "Missing required parameters");
806
		}
807
		return LinphoneAccountCreatorStatusMissingArguments;
808
	}
809 810 811 812 813
	ms_debug("Account creator: activate_account_phone (phone number=%s, username=%s, activation code=%s, domain=%s)",
		creator->phone_number,
		creator->username ? creator->username : creator->phone_number,
		creator->activation_code,
		linphone_proxy_config_get_domain(creator->proxy_cfg));
Ghislain MARY's avatar