1 /***************************************************************************
4 * Fri Jul 16 12:08:34 2004
5 * Copyright 2004-2009 Simon MORLAT
6 * simon.morlat@linphone.org
7 ****************************************************************************/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "linphonecore.h"
27 #include <eXosip2/eXosip.h>
28 #include <osipparser2/osip_message.h>
31 extern LinphoneProxyConfig *linphone_core_get_proxy_config_from_rid(LinphoneCore *lc, int rid);
34 * @addtogroup authentication
39 * Create a LinphoneAuthInfo object with supplied information.
41 * The object can be created empty, that is with all arguments set to NULL.
42 * Username, userid, password and realm can be set later using specific methods.
44 LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid,
45 const char *passwd, const char *ha1,const char *realm)
47 LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
48 if (username!=NULL && (strlen(username)>0) ) obj->username=ms_strdup(username);
49 if (userid!=NULL && (strlen(userid)>0)) obj->userid=ms_strdup(userid);
50 if (passwd!=NULL && (strlen(passwd)>0)) obj->passwd=ms_strdup(passwd);
51 if (ha1!=NULL && (strlen(ha1)>0)) obj->ha1=ms_strdup(ha1);
52 if (realm!=NULL && (strlen(realm)>0)) obj->realm=ms_strdup(realm);
61 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
62 if (info->passwd!=NULL) {
63 ms_free(info->passwd);
66 if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
72 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
74 ms_free(info->username);
77 if (username && strlen(username)>0) info->username=ms_strdup(username);
83 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
85 ms_free(info->userid);
88 if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
92 * Destroys a LinphoneAuthInfo object.
94 void linphone_auth_info_destroy(LinphoneAuthInfo *obj){
95 if (obj->username!=NULL) ms_free(obj->username);
96 if (obj->userid!=NULL) ms_free(obj->userid);
97 if (obj->passwd!=NULL) ms_free(obj->passwd);
98 if (obj->ha1!=NULL) ms_free(obj->ha1);
99 if (obj->realm!=NULL) ms_free(obj->realm);
103 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
106 sprintf(key,"auth_info_%i",pos);
107 lp_config_clean_section(config,key);
112 if (obj->username!=NULL){
113 lp_config_set_string(config,key,"username",obj->username);
115 if (obj->userid!=NULL){
116 lp_config_set_string(config,key,"userid",obj->userid);
118 if (obj->passwd!=NULL){
119 lp_config_set_string(config,key,"passwd",obj->passwd);
122 lp_config_set_string(config,key,"ha1",obj->ha1);
124 if (obj->realm!=NULL){
125 lp_config_set_string(config,key,"realm",obj->realm);
129 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
132 const char *username,*userid,*passwd,*ha1,*realm;
134 sprintf(key,"auth_info_%i",pos);
135 if (!lp_config_has_section(config,key)){
139 username=lp_config_get_string(config,key,"username",NULL);
140 userid=lp_config_get_string(config,key,"userid",NULL);
141 passwd=lp_config_get_string(config,key,"passwd",NULL);
142 ha1=lp_config_get_string(config,key,"ha1",NULL);
143 realm=lp_config_get_string(config,key,"realm",NULL);
144 return linphone_auth_info_new(username,userid,passwd,ha1,realm);
147 static bool_t key_match(const char *tmp1, const char *tmp2){
148 if (tmp1==NULL && tmp2==NULL) return TRUE;
149 if (tmp1!=NULL && tmp2!=NULL && strcmp(tmp1,tmp2)==0) return TRUE;
154 static char * remove_quotes(char * input){
156 if (*input=='"') input++;
157 tmp=strchr(input,'"');
162 static int realm_match(const char *realm1, const char *realm2){
163 if (realm1==NULL && realm2==NULL) return TRUE;
164 if (realm1!=NULL && realm2!=NULL){
165 if (strcmp(realm1,realm2)==0) return TRUE;
170 strncpy(tmp1,realm1,sizeof(tmp1)-1);
171 strncpy(tmp2,realm2,sizeof(tmp2)-1);
172 p1=remove_quotes(tmp1);
173 p2=remove_quotes(tmp2);
174 return strcmp(p1,p2)==0;
181 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
183 LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
186 LinphoneAuthInfo *ret=NULL,*candidate=NULL;
187 for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
188 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
190 /*return the authinfo for any realm provided that there is only one for that username*/
191 if (key_match(pinfo->username,username)){
193 ms_warning("There are several auth info for username '%s'",username);
199 /*return the exact authinfo, or an authinfo for which realm was not supplied yet*/
200 if (pinfo->realm!=NULL){
201 if (realm_match(pinfo->realm,realm)
202 && key_match(pinfo->username,username))
205 if (key_match(pinfo->username,username))
210 if (ret==NULL && candidate!=NULL)
215 static void refresh_exosip_auth_info(LinphoneCore *lc){
218 eXosip_clear_authentication_info();
219 for (elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem)){
220 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
222 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
223 else userid=info->userid;
224 eXosip_add_authentication_info(info->username,userid,
225 info->passwd,info->ha1,info->realm);
231 * Adds authentication information to the LinphoneCore.
233 * This information will be used during all SIP transacations that require authentication.
235 void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
238 LinphoneAuthInfo *ai;
240 /* find if we are attempting to modify an existing auth info */
241 ai=linphone_core_find_auth_info(lc,info->realm,info->username);
243 elem=ms_list_find(lc->auth_info,ai);
245 ms_error("AuthInfo list corruption ?");
248 linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
249 elem->data=(void *)info;
251 lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
253 refresh_exosip_auth_info(lc);
254 /* if the user was prompted, re-allow automatic_action */
255 if (lc->automatic_action>0) lc->automatic_action--;
260 * This method is used to abort a user authentication request initiated by LinphoneCore
261 * from the auth_info_requested callback of LinphoneCoreVTable.
263 void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info){
264 if (lc->automatic_action>0) lc->automatic_action--;
268 * Removes an authentication information object.
270 void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
271 int len=ms_list_size(lc->auth_info);
275 lc->auth_info=ms_list_remove(lc->auth_info,info);
276 newlen=ms_list_size(lc->auth_info);
277 /*printf("len=%i newlen=%i\n",len,newlen);*/
278 linphone_auth_info_destroy(info);
280 linphone_auth_info_write_config(lc->config,NULL,i);
282 for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
283 linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i);
285 refresh_exosip_auth_info(lc);
290 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
292 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
293 return lc->auth_info;
297 * Clear all authentication information.
299 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
303 eXosip_clear_authentication_info();
305 for(i=0,elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem),i++){
306 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
307 linphone_auth_info_destroy(info);
308 linphone_auth_info_write_config(lc->config,NULL,i);
310 ms_list_free(lc->auth_info);
314 void linphone_authentication_ok(LinphoneCore *lc, eXosip_event_t *ev){
315 char *prx_realm=NULL,*www_realm=NULL;
316 osip_proxy_authorization_t *prx_auth;
317 osip_authorization_t *www_auth;
318 osip_message_t *msg=ev->request;
320 LinphoneAuthInfo *as=NULL;
322 username=osip_uri_get_username(msg->from->url);
323 osip_message_get_proxy_authorization(msg,0,&prx_auth);
324 osip_message_get_authorization(msg,0,&www_auth);
326 prx_realm=osip_proxy_authorization_get_realm(prx_auth);
328 www_realm=osip_authorization_get_realm(www_auth);
330 if (prx_realm==NULL && www_realm==NULL){
331 ms_message("No authentication info in the request, ignoring");
334 /* see if we already have this auth information , not to ask it everytime to the user */
336 as=linphone_core_find_auth_info(lc,prx_realm,username);
338 as=linphone_core_find_auth_info(lc,www_realm,username);
340 ms_message("Authentication for user=%s realm=%s is working.",username,prx_realm ? prx_realm : www_realm);
346 void linphone_core_find_or_ask_for_auth_info(LinphoneCore *lc,const char *username,const char* realm, int tid)
348 LinphoneAuthInfo *as=linphone_core_find_auth_info(lc,realm,username);
349 if ( as==NULL || (as!=NULL && as->works==FALSE && as->first_time==FALSE)){
350 if (lc->vtable.auth_info_requested!=NULL){
351 lc->vtable.auth_info_requested(lc,realm,username);
352 lc->automatic_action++;/*suspends eXosip_automatic_action until the user supplies a password */
355 if (as) as->first_time=FALSE;
358 void linphone_process_authentication(LinphoneCore *lc, eXosip_event_t *ev)
360 char *prx_realm=NULL,*www_realm=NULL;
361 osip_proxy_authenticate_t *prx_auth;
362 osip_www_authenticate_t *www_auth;
363 osip_message_t *resp=ev->response;
367 if (strcmp(ev->request->sip_method,"REGISTER")==0) {
368 gstate_new_state(lc, GSTATE_REG_FAILED, "Authentication required");
372 username=osip_uri_get_username(resp->from->url);
373 prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
374 www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
376 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
378 www_realm=osip_www_authenticate_get_realm(www_auth);
380 if (prx_realm==NULL && www_realm==NULL){
381 ms_warning("No realm in the server response.");
384 /* see if we already have this auth information , not to ask it everytime to the user */
386 linphone_core_find_or_ask_for_auth_info(lc,username,prx_realm,ev->tid);
388 linphone_core_find_or_ask_for_auth_info(lc,username,www_realm,ev->tid);