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);
60 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
61 if (info->passwd!=NULL) {
62 ms_free(info->passwd);
65 if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
71 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
73 ms_free(info->username);
76 if (username && strlen(username)>0) info->username=ms_strdup(username);
82 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
84 ms_free(info->userid);
87 if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
91 * Destroys a LinphoneAuthInfo object.
93 void linphone_auth_info_destroy(LinphoneAuthInfo *obj){
94 if (obj->username!=NULL) ms_free(obj->username);
95 if (obj->userid!=NULL) ms_free(obj->userid);
96 if (obj->passwd!=NULL) ms_free(obj->passwd);
97 if (obj->ha1!=NULL) ms_free(obj->ha1);
98 if (obj->realm!=NULL) ms_free(obj->realm);
102 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
105 sprintf(key,"auth_info_%i",pos);
106 lp_config_clean_section(config,key);
111 if (obj->username!=NULL){
112 lp_config_set_string(config,key,"username",obj->username);
114 if (obj->userid!=NULL){
115 lp_config_set_string(config,key,"userid",obj->userid);
117 if (obj->passwd!=NULL){
118 lp_config_set_string(config,key,"passwd",obj->passwd);
121 lp_config_set_string(config,key,"ha1",obj->ha1);
123 if (obj->realm!=NULL){
124 lp_config_set_string(config,key,"realm",obj->realm);
128 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
131 const char *username,*userid,*passwd,*ha1,*realm;
133 sprintf(key,"auth_info_%i",pos);
134 if (!lp_config_has_section(config,key)){
138 username=lp_config_get_string(config,key,"username",NULL);
139 userid=lp_config_get_string(config,key,"userid",NULL);
140 passwd=lp_config_get_string(config,key,"passwd",NULL);
141 ha1=lp_config_get_string(config,key,"ha1",NULL);
142 realm=lp_config_get_string(config,key,"realm",NULL);
143 return linphone_auth_info_new(username,userid,passwd,ha1,realm);
146 static bool_t key_match(const char *tmp1, const char *tmp2){
147 if (tmp1==NULL && tmp2==NULL) return TRUE;
148 if (tmp1!=NULL && tmp2!=NULL && strcmp(tmp1,tmp2)==0) return TRUE;
153 static char * remove_quotes(char * input){
155 if (*input=='"') input++;
156 tmp=strchr(input,'"');
161 static int realm_match(const char *realm1, const char *realm2){
162 if (realm1==NULL && realm2==NULL) return TRUE;
163 if (realm1!=NULL && realm2!=NULL){
164 if (strcmp(realm1,realm2)==0) return TRUE;
169 strncpy(tmp1,realm1,sizeof(tmp1)-1);
170 strncpy(tmp2,realm2,sizeof(tmp2)-1);
171 p1=remove_quotes(tmp1);
172 p2=remove_quotes(tmp2);
173 return strcmp(p1,p2)==0;
180 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
182 LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
185 LinphoneAuthInfo *ret=NULL,*candidate=NULL;
186 for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
187 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
189 /*return the authinfo for any realm provided that there is only one for that username*/
190 if (key_match(pinfo->username,username)){
192 ms_warning("There are several auth info for username '%s'",username);
198 /*return the exact authinfo, or an authinfo for which realm was not supplied yet*/
199 if (pinfo->realm!=NULL){
200 if (realm_match(pinfo->realm,realm)
201 && key_match(pinfo->username,username))
204 if (key_match(pinfo->username,username))
209 if (ret==NULL && candidate!=NULL)
214 static void refresh_exosip_auth_info(LinphoneCore *lc){
217 eXosip_clear_authentication_info();
218 for (elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem)){
219 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
221 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
222 else userid=info->userid;
223 eXosip_add_authentication_info(info->username,userid,
224 info->passwd,info->ha1,info->realm);
230 * Adds authentication information to the LinphoneCore.
232 * This information will be used during all SIP transacations that require authentication.
234 void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
237 LinphoneAuthInfo *ai;
239 /* find if we are attempting to modify an existing auth info */
240 ai=linphone_core_find_auth_info(lc,info->realm,info->username);
242 elem=ms_list_find(lc->auth_info,ai);
244 ms_error("AuthInfo list corruption ?");
247 linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
248 elem->data=(void *)info;
250 lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
252 refresh_exosip_auth_info(lc);
253 /* if the user was prompted, re-allow automatic_action */
258 * This method is used to abort a user authentication request initiated by LinphoneCore
259 * from the auth_info_requested callback of LinphoneCoreVTable.
261 void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info){
262 if (lc->automatic_action>0) lc->automatic_action--;
266 * Removes an authentication information object.
268 void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
269 int len=ms_list_size(lc->auth_info);
273 lc->auth_info=ms_list_remove(lc->auth_info,info);
274 newlen=ms_list_size(lc->auth_info);
275 /*printf("len=%i newlen=%i\n",len,newlen);*/
276 linphone_auth_info_destroy(info);
278 linphone_auth_info_write_config(lc->config,NULL,i);
280 for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
281 linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i);
283 refresh_exosip_auth_info(lc);
288 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
290 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
291 return lc->auth_info;
295 * Clear all authentication information.
297 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
301 eXosip_clear_authentication_info();
303 for(i=0,elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem),i++){
304 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
305 linphone_auth_info_destroy(info);
306 linphone_auth_info_write_config(lc->config,NULL,i);
308 ms_list_free(lc->auth_info);
312 void linphone_authentication_ok(LinphoneCore *lc, eXosip_event_t *ev){
313 char *prx_realm=NULL,*www_realm=NULL;
314 osip_proxy_authorization_t *prx_auth;
315 osip_authorization_t *www_auth;
316 osip_message_t *msg=ev->request;
318 LinphoneAuthInfo *as=NULL;
320 username=osip_uri_get_username(msg->from->url);
321 osip_message_get_proxy_authorization(msg,0,&prx_auth);
322 osip_message_get_authorization(msg,0,&www_auth);
324 prx_realm=osip_proxy_authorization_get_realm(prx_auth);
326 www_realm=osip_authorization_get_realm(www_auth);
328 if (prx_realm==NULL && www_realm==NULL){
329 ms_message("No authentication info in the request, ignoring");
332 /* see if we already have this auth information , not to ask it everytime to the user */
334 as=linphone_core_find_auth_info(lc,prx_realm,username);
336 as=linphone_core_find_auth_info(lc,www_realm,username);
338 ms_message("Authentication for user=%s realm=%s is working.",username,prx_realm ? prx_realm : www_realm);
344 void linphone_core_find_or_ask_for_auth_info(LinphoneCore *lc,const char *username,const char* realm, int tid)
346 LinphoneAuthInfo *as=linphone_core_find_auth_info(lc,realm,username);
347 if ( as==NULL || (as!=NULL && as->works==FALSE && as->first_time==FALSE)){
348 if (lc->vtable.auth_info_requested!=NULL){
349 lc->vtable.auth_info_requested(lc,realm,username);
350 lc->automatic_action++;/*suspends eXosip_automatic_action until the user supplies a password */
353 if (as) as->first_time=FALSE;
356 void linphone_process_authentication(LinphoneCore *lc, eXosip_event_t *ev)
358 char *prx_realm=NULL,*www_realm=NULL;
359 osip_proxy_authenticate_t *prx_auth;
360 osip_www_authenticate_t *www_auth;
361 osip_message_t *resp=ev->response;
365 if (strcmp(ev->request->sip_method,"REGISTER")==0) {
366 gstate_new_state(lc, GSTATE_REG_FAILED, "Authentication required");
370 username=osip_uri_get_username(resp->from->url);
371 prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
372 www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
374 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
376 www_realm=osip_www_authenticate_get_realm(www_auth);
378 if (prx_realm==NULL && www_realm==NULL){
379 ms_warning("No realm in the server response.");
382 /* see if we already have this auth information , not to ask it everytime to the user */
384 linphone_core_find_or_ask_for_auth_info(lc,username,prx_realm,ev->tid);
386 linphone_core_find_or_ask_for_auth_info(lc,username,www_realm,ev->tid);