authentication.c 12.6 KB
Newer Older
aymeric's avatar
aymeric committed
1 2 3 4
/***************************************************************************
 *            authentication.c
 *
 *  Fri Jul 16 12:08:34 2004
5
 *  Copyright  2004-2009  Simon MORLAT
aymeric's avatar
aymeric committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *  simon.morlat@linphone.org
 ****************************************************************************/

/*
 *  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 Library 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.
 */
24

aymeric's avatar
aymeric committed
25 26 27 28
#include "linphonecore.h"
#include "private.h"
#include "lpconfig.h"

smorlat's avatar
smorlat committed
29 30 31 32 33
/**
 * @addtogroup authentication
 * @{
**/

34
LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid, const char *passwd, const char *ha1, const char *realm, const char *domain){
aymeric's avatar
aymeric committed
35 36 37 38 39 40
	LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
	if (username!=NULL && (strlen(username)>0) ) obj->username=ms_strdup(username);
	if (userid!=NULL && (strlen(userid)>0)) obj->userid=ms_strdup(userid);
	if (passwd!=NULL && (strlen(passwd)>0)) obj->passwd=ms_strdup(passwd);
	if (ha1!=NULL && (strlen(ha1)>0)) obj->ha1=ms_strdup(ha1);
	if (realm!=NULL && (strlen(realm)>0)) obj->realm=ms_strdup(realm);
41
	if (domain!=NULL && (strlen(domain)>0)) obj->domain=ms_strdup(domain);
aymeric's avatar
aymeric committed
42 43 44
	return obj;
}

45
LinphoneAuthInfo *linphone_auth_info_clone(const LinphoneAuthInfo *ai){
46 47 48 49 50 51
	LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
	if (ai->username) obj->username=ms_strdup(ai->username);
	if (ai->userid) obj->userid=ms_strdup(ai->userid);
	if (ai->passwd) obj->passwd=ms_strdup(ai->passwd);
	if (ai->ha1)	obj->ha1=ms_strdup(ai->ha1);
	if (ai->realm)	obj->realm=ms_strdup(ai->realm);
52
	if (ai->domain)	obj->domain=ms_strdup(ai->domain);
53 54 55
	return obj;
}

56 57 58 59 60 61 62 63 64 65 66 67
const char *linphone_auth_info_get_username(const LinphoneAuthInfo *i){
	return i->username;
}

const char *linphone_auth_info_get_passwd(const LinphoneAuthInfo *i){
	return i->passwd;
}

const char *linphone_auth_info_get_userid(const LinphoneAuthInfo *i){
	return i->userid;
}

jehan's avatar
jehan committed
68 69 70
const char *linphone_auth_info_get_realm(const LinphoneAuthInfo *i){
	return i->realm;
}
71 72 73 74 75

const char *linphone_auth_info_get_domain(const LinphoneAuthInfo *i){
	return i->domain;
}

jehan's avatar
jehan committed
76 77 78 79
const char *linphone_auth_info_get_ha1(const LinphoneAuthInfo *i){
	return i->ha1;
}

aymeric's avatar
aymeric committed
80 81 82 83 84 85 86 87
void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
	if (info->passwd!=NULL) {
		ms_free(info->passwd);
		info->passwd=NULL;
	}
	if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
}

smorlat's avatar
smorlat committed
88 89 90 91 92 93 94 95
void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
	if (info->username){
		ms_free(info->username);
		info->username=NULL;
	}
	if (username && strlen(username)>0) info->username=ms_strdup(username);
}

96 97 98 99 100 101 102 103
void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
	if (info->userid){
		ms_free(info->userid);
		info->userid=NULL;
	}
	if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
}

jehan's avatar
jehan committed
104 105 106 107 108 109 110
void linphone_auth_info_set_realm(LinphoneAuthInfo *info, const char *realm){
	if (info->realm){
		ms_free(info->realm);
		info->realm=NULL;
	}
	if (realm && strlen(realm)>0) info->realm=ms_strdup(realm);
}
111 112 113 114 115 116 117 118 119

void linphone_auth_info_set_domain(LinphoneAuthInfo *info, const char *domain){
	if (info->domain){
		ms_free(info->domain);
		info->domain=NULL;
	}
	if (domain && strlen(domain)>0) info->domain=ms_strdup(domain);
}

jehan's avatar
jehan committed
120 121 122 123 124 125 126 127
void linphone_auth_info_set_ha1(LinphoneAuthInfo *info, const char *ha1){
	if (info->ha1){
		ms_free(info->ha1);
		info->ha1=NULL;
	}
	if (ha1 && strlen(ha1)>0) info->ha1=ms_strdup(ha1);
}

smorlat's avatar
smorlat committed
128 129 130
/**
 * Destroys a LinphoneAuthInfo object.
**/
aymeric's avatar
aymeric committed
131 132 133 134 135 136
void linphone_auth_info_destroy(LinphoneAuthInfo *obj){
	if (obj->username!=NULL) ms_free(obj->username);
	if (obj->userid!=NULL) ms_free(obj->userid);
	if (obj->passwd!=NULL) ms_free(obj->passwd);
	if (obj->ha1!=NULL) ms_free(obj->ha1);
	if (obj->realm!=NULL) ms_free(obj->realm);
137
	if (obj->domain!=NULL) ms_free(obj->domain);
aymeric's avatar
aymeric committed
138 139 140 141 142 143 144 145
	ms_free(obj);
}

void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
{
	char key[50];
	sprintf(key,"auth_info_%i",pos);
	lp_config_clean_section(config,key);
146

147
	if (obj==NULL || lp_config_get_int(config, "sip", "store_auth_info", 1) == 0){
aymeric's avatar
aymeric committed
148
		return;
149
	}
150
	if (!obj->ha1 && obj->realm && obj->passwd && (obj->username||obj->userid) && lp_config_get_int(config, "sip", "store_ha1_passwd", 1) == 1) {
151 152 153 154
		/*compute ha1 to avoid storing clear text password*/
		obj->ha1=ms_malloc(33);
		sal_auth_compute_ha1(obj->userid?obj->userid:obj->username,obj->realm,obj->passwd,obj->ha1);
	}
aymeric's avatar
aymeric committed
155 156 157 158 159 160 161 162
	if (obj->username!=NULL){
		lp_config_set_string(config,key,"username",obj->username);
	}
	if (obj->userid!=NULL){
		lp_config_set_string(config,key,"userid",obj->userid);
	}
	if (obj->ha1!=NULL){
		lp_config_set_string(config,key,"ha1",obj->ha1);
163 164
	} else if (obj->passwd!=NULL){ /*only write passwd if no ha1*/
		lp_config_set_string(config,key,"passwd",obj->passwd);
aymeric's avatar
aymeric committed
165 166 167 168
	}
	if (obj->realm!=NULL){
		lp_config_set_string(config,key,"realm",obj->realm);
	}
169 170 171
	if (obj->domain!=NULL){
		lp_config_set_string(config,key,"domain",obj->domain);
	}
aymeric's avatar
aymeric committed
172 173 174 175 176
}

LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
{
	char key[50];
177 178
	const char *username,*userid,*passwd,*ha1,*realm,*domain;
	LinphoneAuthInfo *ret;
179

aymeric's avatar
aymeric committed
180 181 182 183
	sprintf(key,"auth_info_%i",pos);
	if (!lp_config_has_section(config,key)){
		return NULL;
	}
184

aymeric's avatar
aymeric committed
185 186 187 188 189
	username=lp_config_get_string(config,key,"username",NULL);
	userid=lp_config_get_string(config,key,"userid",NULL);
	passwd=lp_config_get_string(config,key,"passwd",NULL);
	ha1=lp_config_get_string(config,key,"ha1",NULL);
	realm=lp_config_get_string(config,key,"realm",NULL);
190
	domain=lp_config_get_string(config,key,"domain",NULL);
191
	ret=linphone_auth_info_new(username,userid,passwd,ha1,realm,domain);
192
	return ret;
aymeric's avatar
aymeric committed
193 194
}

smorlat's avatar
smorlat committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
static char * remove_quotes(char * input){
	char *tmp;
	if (*input=='"') input++;
	tmp=strchr(input,'"');
	if (tmp) *tmp='\0';
	return input;
}

static int realm_match(const char *realm1, const char *realm2){
	if (realm1==NULL && realm2==NULL) return TRUE;
	if (realm1!=NULL && realm2!=NULL){
		if (strcmp(realm1,realm2)==0) return TRUE;
		else{
			char tmp1[128];
			char tmp2[128];
			char *p1,*p2;
			strncpy(tmp1,realm1,sizeof(tmp1)-1);
212
			strncpy(tmp2,realm2,sizeof(tmp2)-1);
smorlat's avatar
smorlat committed
213 214 215 216 217 218 219 220
			p1=remove_quotes(tmp1);
			p2=remove_quotes(tmp2);
			return strcmp(p1,p2)==0;
		}
	}
	return FALSE;
}

221
static const LinphoneAuthInfo *find_auth_info(LinphoneCore *lc, const char *username, const char *realm, const char *domain){
aymeric's avatar
aymeric committed
222
	MSList *elem;
223
	const LinphoneAuthInfo *ret=NULL;
224

Sylvain Berfini's avatar
Sylvain Berfini committed
225 226 227
	for (elem=lc->auth_info;elem!=NULL;elem=elem->next) {
		LinphoneAuthInfo *pinfo = (LinphoneAuthInfo*)elem->data;
		if (username && pinfo->username && strcmp(username,pinfo->username)==0) {
228 229
			if (realm && domain){
				if (pinfo->realm && strcmp(realm,pinfo->realm)==0
Sylvain Berfini's avatar
Sylvain Berfini committed
230
					&& pinfo->domain && strcmp(domain,pinfo->domain)==0) {
231
					return pinfo;
smorlat's avatar
smorlat committed
232
				}
Sylvain Berfini's avatar
Sylvain Berfini committed
233 234 235
			} else if (realm) {
				if (pinfo->realm && realm_match(realm,pinfo->realm)) {
					if (ret!=NULL) {
236 237 238
						ms_warning("Non unique realm found for %s",username);
						return NULL;
					}
smorlat's avatar
smorlat committed
239
					ret=pinfo;
240
				}
Sylvain Berfini's avatar
Sylvain Berfini committed
241
			} else if (domain && pinfo->domain && strcmp(domain,pinfo->domain)==0) {
242
				return pinfo;
243
			} else if (!domain) {
Sylvain Berfini's avatar
Sylvain Berfini committed
244 245
				return pinfo;
			}
smorlat's avatar
smorlat committed
246
		}
smorlat's avatar
smorlat committed
247
	}
smorlat's avatar
smorlat committed
248
	return ret;
aymeric's avatar
aymeric committed
249 250
}

251
/**
252
 * Find authentication info matching realm, username, domain criteria.
253 254 255 256 257 258 259 260 261
 * First of all, (realm,username) pair are searched. If multiple results (which should not happen because realm are supposed to be unique), then domain is added to the search.
 * @param lc the LinphoneCore
 * @param realm the authentication 'realm' (optional)
 * @param username the SIP username to be authenticated (mandatory)
 * @param domain the SIP domain name (optional)
 * @return a #LinphoneAuthInfo
**/
const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username, const char *domain){
	const LinphoneAuthInfo *ai=NULL;
Sylvain Berfini's avatar
Sylvain Berfini committed
262
	if (realm){
263
		ai=find_auth_info(lc,username,realm,NULL);
Sylvain Berfini's avatar
Sylvain Berfini committed
264 265 266
		if (ai==NULL && domain){
			ai=find_auth_info(lc,username,realm,domain);
		}
267
	}
268
	if (ai == NULL && domain != NULL) {
Sylvain Berfini's avatar
Sylvain Berfini committed
269
		ai=find_auth_info(lc,username,NULL,domain);
270
	}
271 272 273 274 275 276
	if (ai==NULL){
		ai=find_auth_info(lc,username,NULL,NULL);
	}
	return ai;
}

277 278 279 280 281 282 283 284 285 286 287 288
static void write_auth_infos(LinphoneCore *lc){
	MSList *elem;
	int i;

	if (!linphone_core_ready(lc)) return;
	for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
		LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
		linphone_auth_info_write_config(lc->config,ai,i);
	}
	linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
}

289 290
LinphoneAuthInfo * linphone_core_create_auth_info(LinphoneCore *lc, const char *username, const char *userid, const char *passwd, const char *ha1, const char *realm, const char *domain) {
	return linphone_auth_info_new(username, userid, passwd, ha1, realm, domain);
291 292
}

smorlat's avatar
smorlat committed
293 294
/**
 * Adds authentication information to the LinphoneCore.
295 296
 *
 * This information will be used during all SIP transactions that require authentication.
smorlat's avatar
smorlat committed
297
**/
298
void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
299
	LinphoneAuthInfo *ai;
300
	MSList *elem;
301
	MSList *l;
302 303
	int restarted_op_count=0;
	bool_t updating=FALSE;
304

305 306 307 308
	if (info->ha1==NULL && info->passwd==NULL){
		ms_error("linphone_core_add_auth_info(): info supplied with empty password or ha1.");
		return;
	}
aymeric's avatar
aymeric committed
309
	/* find if we are attempting to modify an existing auth info */
310
	ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username,info->domain);
Sylvain Berfini's avatar
Sylvain Berfini committed
311
	if (ai!=NULL && ai->domain && info->domain && strcmp(ai->domain, info->domain)==0){
312 313
		lc->auth_info=ms_list_remove(lc->auth_info,ai);
		linphone_auth_info_destroy(ai);
314
		updating=TRUE;
aymeric's avatar
aymeric committed
315
	}
316
	lc->auth_info=ms_list_append(lc->auth_info,linphone_auth_info_clone(info));
317

Simon Morlat's avatar
Simon Morlat committed
318
	/* retry pending authentication operations */
319
	for(l=elem=sal_get_pending_auths(lc->sal);elem!=NULL;elem=elem->next){
Simon Morlat's avatar
Simon Morlat committed
320 321
		SalOp *op=(SalOp*)elem->data;
		LinphoneAuthInfo *ai;
322 323
		const SalAuthInfo *req_sai=sal_op_get_auth_requested(op);
		ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,req_sai->realm,req_sai->username,req_sai->domain);
Simon Morlat's avatar
Simon Morlat committed
324 325
		if (ai){
			SalAuthInfo sai;
326
			MSList* proxy;
Simon Morlat's avatar
Simon Morlat committed
327 328 329 330
			sai.username=ai->username;
			sai.userid=ai->userid;
			sai.realm=ai->realm;
			sai.password=ai->passwd;
331
			sai.ha1=ai->ha1;
332 333 334 335 336 337 338
			/*proxy case*/
			for (proxy=(MSList*)linphone_core_get_proxy_config_list(lc);proxy!=NULL;proxy=proxy->next) {
				if (proxy->data == sal_op_get_user_pointer(op)) {
					linphone_proxy_config_set_state((LinphoneProxyConfig*)(proxy->data),LinphoneRegistrationProgress,"Authentication...");
					break;
				}
			}
Simon Morlat's avatar
Simon Morlat committed
339
			sal_op_authenticate(op,&sai);
340
			restarted_op_count++;
Simon Morlat's avatar
Simon Morlat committed
341 342
		}
	}
343 344 345 346 347 348 349 350 351 352 353
	if (l){
		ms_message("linphone_core_add_auth_info(): restarted [%i] operation(s) after %s auth info for\n"
			"\tusername: [%s]\n"
			"\trealm [%s]\n"
			"\tdomain [%s]\n",
			restarted_op_count,
			updating ? "updating" : "adding",
			info->username ? info->username : "",
			info->realm ? info->realm : "",
			info->domain ? info->domain : "");
	}
354
	ms_list_free(l);
355
	write_auth_infos(lc);
aymeric's avatar
aymeric committed
356 357
}

smorlat's avatar
smorlat committed
358 359 360 361 362

/**
 * This method is used to abort a user authentication request initiated by LinphoneCore
 * from the auth_info_requested callback of LinphoneCoreVTable.
**/
aymeric's avatar
aymeric committed
363 364 365
void linphone_core_abort_authentication(LinphoneCore *lc,  LinphoneAuthInfo *info){
}

smorlat's avatar
smorlat committed
366 367 368
/**
 * Removes an authentication information object.
**/
369 370
void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
	LinphoneAuthInfo *r;
371
	r=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username,info->domain);
372 373 374
	if (r){
		lc->auth_info=ms_list_remove(lc->auth_info,r);
		linphone_auth_info_destroy(r);
375
		write_auth_infos(lc);
aymeric's avatar
aymeric committed
376 377 378
	}
}

smorlat's avatar
smorlat committed
379 380
/**
 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
381 382
 * @param[in] lc The LinphoneCore object
 * @return \mslist{LinphoneAuthInfo}
smorlat's avatar
smorlat committed
383
**/
smorlat's avatar
smorlat committed
384 385 386 387
const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
	return lc->auth_info;
}

smorlat's avatar
smorlat committed
388 389 390
/**
 * Clear all authentication information.
**/
aymeric's avatar
aymeric committed
391 392 393 394 395 396 397 398 399 400 401 402
void linphone_core_clear_all_auth_info(LinphoneCore *lc){
	MSList *elem;
	int i;
	for(i=0,elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem),i++){
		LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
		linphone_auth_info_destroy(info);
		linphone_auth_info_write_config(lc->config,NULL,i);
	}
	ms_list_free(lc->auth_info);
	lc->auth_info=NULL;
}

smorlat's avatar
smorlat committed
403 404 405
/**
 * @}
**/