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);
58 static LinphoneAuthInfo *linphone_auth_info_clone(const LinphoneAuthInfo *ai){
59 LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
60 if (ai->username) obj->username=ms_strdup(ai->username);
61 if (ai->userid) obj->userid=ms_strdup(ai->userid);
62 if (ai->passwd) obj->passwd=ms_strdup(ai->passwd);
63 if (ai->ha1) obj->ha1=ms_strdup(ai->ha1);
64 if (ai->realm) obj->realm=ms_strdup(ai->realm);
73 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
74 if (info->passwd!=NULL) {
75 ms_free(info->passwd);
78 if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
84 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
86 ms_free(info->username);
89 if (username && strlen(username)>0) info->username=ms_strdup(username);
95 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
97 ms_free(info->userid);
100 if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
104 * Destroys a LinphoneAuthInfo object.
106 void linphone_auth_info_destroy(LinphoneAuthInfo *obj){
107 if (obj->username!=NULL) ms_free(obj->username);
108 if (obj->userid!=NULL) ms_free(obj->userid);
109 if (obj->passwd!=NULL) ms_free(obj->passwd);
110 if (obj->ha1!=NULL) ms_free(obj->ha1);
111 if (obj->realm!=NULL) ms_free(obj->realm);
115 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
118 sprintf(key,"auth_info_%i",pos);
119 lp_config_clean_section(config,key);
124 if (obj->username!=NULL){
125 lp_config_set_string(config,key,"username",obj->username);
127 if (obj->userid!=NULL){
128 lp_config_set_string(config,key,"userid",obj->userid);
130 if (obj->passwd!=NULL){
131 lp_config_set_string(config,key,"passwd",obj->passwd);
134 lp_config_set_string(config,key,"ha1",obj->ha1);
136 if (obj->realm!=NULL){
137 lp_config_set_string(config,key,"realm",obj->realm);
141 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
144 const char *username,*userid,*passwd,*ha1,*realm;
146 sprintf(key,"auth_info_%i",pos);
147 if (!lp_config_has_section(config,key)){
151 username=lp_config_get_string(config,key,"username",NULL);
152 userid=lp_config_get_string(config,key,"userid",NULL);
153 passwd=lp_config_get_string(config,key,"passwd",NULL);
154 ha1=lp_config_get_string(config,key,"ha1",NULL);
155 realm=lp_config_get_string(config,key,"realm",NULL);
156 return linphone_auth_info_new(username,userid,passwd,ha1,realm);
159 static bool_t key_match(const char *tmp1, const char *tmp2){
160 if (tmp1==NULL && tmp2==NULL) return TRUE;
161 if (tmp1!=NULL && tmp2!=NULL && strcmp(tmp1,tmp2)==0) return TRUE;
166 static char * remove_quotes(char * input){
168 if (*input=='"') input++;
169 tmp=strchr(input,'"');
174 static int realm_match(const char *realm1, const char *realm2){
175 if (realm1==NULL && realm2==NULL) return TRUE;
176 if (realm1!=NULL && realm2!=NULL){
177 if (strcmp(realm1,realm2)==0) return TRUE;
182 strncpy(tmp1,realm1,sizeof(tmp1)-1);
183 strncpy(tmp2,realm2,sizeof(tmp2)-1);
184 p1=remove_quotes(tmp1);
185 p2=remove_quotes(tmp2);
186 return strcmp(p1,p2)==0;
193 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
195 const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
198 LinphoneAuthInfo *ret=NULL,*candidate=NULL;
199 for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
200 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
202 /*return the authinfo for any realm provided that there is only one for that username*/
203 if (key_match(pinfo->username,username)){
205 ms_warning("There are several auth info for username '%s'",username);
211 /*return the exact authinfo, or an authinfo for which realm was not supplied yet*/
212 if (pinfo->realm!=NULL){
213 if (realm_match(pinfo->realm,realm)
214 && key_match(pinfo->username,username))
217 if (key_match(pinfo->username,username))
222 if (ret==NULL && candidate!=NULL)
227 static void refresh_exosip_auth_info(LinphoneCore *lc){
230 eXosip_clear_authentication_info();
231 for (elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem)){
232 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
234 if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
235 else userid=info->userid;
236 eXosip_add_authentication_info(info->username,userid,
237 info->passwd,info->ha1,info->realm);
243 * Adds authentication information to the LinphoneCore.
245 * This information will be used during all SIP transacations that require authentication.
247 void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info)
249 LinphoneAuthInfo *ai;
251 /* find if we are attempting to modify an existing auth info */
252 ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
254 lc->auth_info=ms_list_remove(lc->auth_info,ai);
255 linphone_auth_info_destroy(ai);
257 lc->auth_info=ms_list_append(lc->auth_info,linphone_auth_info_clone(info));
259 refresh_exosip_auth_info(lc);
260 /* if the user was prompted, re-allow automatic_action */
261 if (lc->automatic_action>0) lc->automatic_action--;
266 * This method is used to abort a user authentication request initiated by LinphoneCore
267 * from the auth_info_requested callback of LinphoneCoreVTable.
269 void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info){
270 if (lc->automatic_action>0) lc->automatic_action--;
274 * Removes an authentication information object.
276 void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
280 r=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
282 lc->auth_info=ms_list_remove(lc->auth_info,r);
283 /*printf("len=%i newlen=%i\n",len,newlen);*/
284 linphone_auth_info_destroy(r);
285 for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
286 linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i);
288 linphone_auth_info_write_config(lc->config,NULL,i);
289 refresh_exosip_auth_info(lc);
294 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
296 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
297 return lc->auth_info;
301 * Clear all authentication information.
303 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
307 eXosip_clear_authentication_info();
309 for(i=0,elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem),i++){
310 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
311 linphone_auth_info_destroy(info);
312 linphone_auth_info_write_config(lc->config,NULL,i);
314 ms_list_free(lc->auth_info);
318 void linphone_authentication_ok(LinphoneCore *lc, eXosip_event_t *ev){
319 char *prx_realm=NULL,*www_realm=NULL;
320 osip_proxy_authorization_t *prx_auth;
321 osip_authorization_t *www_auth;
322 osip_message_t *msg=ev->request;
324 LinphoneAuthInfo *as=NULL;
326 username=osip_uri_get_username(msg->from->url);
327 osip_message_get_proxy_authorization(msg,0,&prx_auth);
328 osip_message_get_authorization(msg,0,&www_auth);
330 prx_realm=osip_proxy_authorization_get_realm(prx_auth);
332 www_realm=osip_authorization_get_realm(www_auth);
334 if (prx_realm==NULL && www_realm==NULL){
335 ms_message("No authentication info in the request, ignoring");
338 /* see if we already have this auth information , not to ask it everytime to the user */
340 as=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,prx_realm,username);
342 as=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,www_realm,username);
344 ms_message("Authentication for user=%s realm=%s is working.",username,prx_realm ? prx_realm : www_realm);
350 void linphone_core_find_or_ask_for_auth_info(LinphoneCore *lc,const char *username,const char* realm, int tid)
352 LinphoneAuthInfo *as=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
353 if ( as==NULL || (as!=NULL && as->works==FALSE && as->first_time==FALSE)){
354 if (lc->vtable.auth_info_requested!=NULL){
355 lc->vtable.auth_info_requested(lc,realm,username);
356 lc->automatic_action++;/*suspends eXosip_automatic_action until the user supplies a password */
359 if (as) as->first_time=FALSE;
362 void linphone_process_authentication(LinphoneCore *lc, eXosip_event_t *ev)
364 char *prx_realm=NULL,*www_realm=NULL;
365 osip_proxy_authenticate_t *prx_auth;
366 osip_www_authenticate_t *www_auth;
367 osip_message_t *resp=ev->response;
371 if (strcmp(ev->request->sip_method,"REGISTER")==0) {
372 gstate_new_state(lc, GSTATE_REG_FAILED, "Authentication required");
376 username=osip_uri_get_username(resp->from->url);
377 prx_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->proxy_authenticates,0);
378 www_auth=(osip_proxy_authenticate_t*)osip_list_get(&resp->www_authenticates,0);
380 prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
382 www_realm=osip_www_authenticate_get_realm(www_auth);
384 if (prx_realm==NULL && www_realm==NULL){
385 ms_warning("No realm in the server response.");
388 /* see if we already have this auth information , not to ask it everytime to the user */
390 linphone_core_find_or_ask_for_auth_info(lc,username,prx_realm,ev->tid);
392 linphone_core_find_or_ask_for_auth_info(lc,username,www_realm,ev->tid);