authentication.c 8.92 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 24 25 26 27 28
 *  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.
 */
 
#include "linphonecore.h"
#include "private.h"
#include "lpconfig.h"

smorlat's avatar
smorlat committed
29 30 31 32 33 34 35 36 37 38 39
/**
 * @addtogroup authentication
 * @{
**/

/**
 * Create a LinphoneAuthInfo object with supplied information.
 *
 * The object can be created empty, that is with all arguments set to NULL.
 * Username, userid, password and realm can be set later using specific methods.
**/
aymeric's avatar
aymeric committed
40 41 42 43 44 45 46 47 48 49 50 51 52
LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid,
				   										const char *passwd, const char *ha1,const char *realm)
{
	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);
	obj->works=FALSE;
	return obj;
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/**
 * Returns username.
**/
const char *linphone_auth_info_get_username(const LinphoneAuthInfo *i){
	return i->username;
}

/**
 * Returns password.
**/
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;
}

smorlat's avatar
smorlat committed
71 72 73
/**
 * Sets the password.
**/
aymeric's avatar
aymeric committed
74 75 76 77 78 79 80 81
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
82 83 84
/**
 * Sets the username.
**/
smorlat's avatar
smorlat committed
85 86 87 88 89 90 91 92
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);
}

smorlat's avatar
smorlat committed
93 94 95
/**
 * Sets userid.
**/
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);
}

smorlat's avatar
smorlat committed
104 105 106
/**
 * Destroys a LinphoneAuthInfo object.
**/
aymeric's avatar
aymeric committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
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);
	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);
	
	if (obj==NULL){
		return;
	}		
	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->passwd!=NULL){
		lp_config_set_string(config,key,"passwd",obj->passwd);
	}
	if (obj->ha1!=NULL){
		lp_config_set_string(config,key,"ha1",obj->ha1);
	}
	if (obj->realm!=NULL){
		lp_config_set_string(config,key,"realm",obj->realm);
	}
}

LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
{
	char key[50];
	const char *username,*userid,*passwd,*ha1,*realm;
	
	sprintf(key,"auth_info_%i",pos);
	if (!lp_config_has_section(config,key)){
		return NULL;
	}
	
	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);
	return linphone_auth_info_new(username,userid,passwd,ha1,realm);
}

static bool_t key_match(const char *tmp1, const char *tmp2){
	if (tmp1==NULL && tmp2==NULL) return TRUE;
	if (tmp1!=NULL && tmp2!=NULL && strcmp(tmp1,tmp2)==0) return TRUE;
	return FALSE;
	
}

smorlat's avatar
smorlat committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
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);
184
			strncpy(tmp2,realm2,sizeof(tmp2)-1);
smorlat's avatar
smorlat committed
185 186 187 188 189 190 191 192
			p1=remove_quotes(tmp1);
			p2=remove_quotes(tmp2);
			return strcmp(p1,p2)==0;
		}
	}
	return FALSE;
}

smorlat's avatar
smorlat committed
193 194 195
/**
 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
**/
smorlat's avatar
smorlat committed
196
LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
aymeric's avatar
aymeric committed
197 198
{
	MSList *elem;
smorlat's avatar
smorlat committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	LinphoneAuthInfo *ret=NULL,*candidate=NULL;
	for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
		LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
		if (realm==NULL){
			/*return the authinfo for any realm provided that there is only one for that username*/
			if (key_match(pinfo->username,username)){
				if (ret!=NULL){
					ms_warning("There are several auth info for username '%s'",username);
					return NULL;
				}
				ret=pinfo;
			}
		}else{
			/*return the exact authinfo, or an authinfo for which realm was not supplied yet*/
			if (pinfo->realm!=NULL){
				if (realm_match(pinfo->realm,realm) 
					&& key_match(pinfo->username,username))
					ret=pinfo;
			}else{
				if (key_match(pinfo->username,username))
					candidate=pinfo;
			}
		}
smorlat's avatar
smorlat committed
222
	}
smorlat's avatar
smorlat committed
223 224 225
	if (ret==NULL && candidate!=NULL)
		ret=candidate;
	return ret;
aymeric's avatar
aymeric committed
226 227
}

smorlat's avatar
smorlat committed
228 229 230 231 232
/**
 * Adds authentication information to the LinphoneCore.
 * 
 * This information will be used during all SIP transacations that require authentication.
**/
233 234 235 236 237
void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
{
	MSList *elem;
	LinphoneAuthInfo *ai;
	
aymeric's avatar
aymeric committed
238
	/* find if we are attempting to modify an existing auth info */
smorlat's avatar
smorlat committed
239 240 241 242 243 244 245
	ai=linphone_core_find_auth_info(lc,info->realm,info->username);
	if (ai!=NULL){
		elem=ms_list_find(lc->auth_info,ai);
		if (elem==NULL){
			ms_error("AuthInfo list corruption ?");
			return;
		}
aymeric's avatar
aymeric committed
246 247 248 249 250
		linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
		elem->data=(void *)info;
	}else {
		lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
	}
Simon Morlat's avatar
Simon Morlat committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	/* retry pending authentication operations */
	for(elem=sal_get_pending_auths(lc->sal);elem!=NULL;elem=elem->next){
		const char *username,*realm;
		SalOp *op=(SalOp*)elem->data;
		LinphoneAuthInfo *ai;
		sal_op_get_auth_requested(op,&realm,&username);
		ai=linphone_core_find_auth_info(lc,realm,username);
		if (ai){
			SalAuthInfo sai;
			sai.username=ai->username;
			sai.userid=ai->userid;
			sai.realm=ai->realm;
			sai.password=ai->passwd;
			sal_op_authenticate(op,&sai);
			ai->usecount++;
		}
	}
aymeric's avatar
aymeric committed
268 269
}

smorlat's avatar
smorlat committed
270 271 272 273 274

/**
 * 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
275 276 277
void linphone_core_abort_authentication(LinphoneCore *lc,  LinphoneAuthInfo *info){
}

smorlat's avatar
smorlat committed
278 279 280
/**
 * Removes an authentication information object.
**/
aymeric's avatar
aymeric committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
	int len=ms_list_size(lc->auth_info);
	int newlen;
	int i;
	MSList *elem;
	lc->auth_info=ms_list_remove(lc->auth_info,info);
	newlen=ms_list_size(lc->auth_info);
	/*printf("len=%i newlen=%i\n",len,newlen);*/
	linphone_auth_info_destroy(info);
	for (i=0;i<len;i++){
		linphone_auth_info_write_config(lc->config,NULL,i);
	}
	for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
		linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i);
	}
}

smorlat's avatar
smorlat committed
298 299 300
/**
 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
**/
smorlat's avatar
smorlat committed
301 302 303 304
const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
	return lc->auth_info;
}

smorlat's avatar
smorlat committed
305 306 307
/**
 * Clear all authentication information.
**/
aymeric's avatar
aymeric committed
308 309 310 311 312 313 314 315 316 317 318 319
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
320 321 322
/**
 * @}
**/