1
/***************************************************************************
4
* Fri Jul 16 12:08:34 2004
5
* Copyright 2004 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"
28
#include <osipparser2/osip_message.h>
32
extern LinphoneProxyConfig *linphone_core_get_proxy_config_from_rid(LinphoneCore *lc, int rid);
33
extern void linphone_proxy_config_set_realm(LinphoneProxyConfig *cfg, const char *realm);
34
extern void linphone_core_retry_proxy_register(LinphoneCore *lc, const char *realm);
36
LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid,
37
const char *passwd, const char *ha1,const char *realm)
39
LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
40
if (username!=NULL && (strlen(username)>0) ) obj->username=ms_strdup(username);
41
if (userid!=NULL && (strlen(userid)>0)) obj->userid=ms_strdup(userid);
42
if (passwd!=NULL && (strlen(passwd)>0)) obj->passwd=ms_strdup(passwd);
43
if (ha1!=NULL && (strlen(ha1)>0)) obj->ha1=ms_strdup(ha1);
44
if (realm!=NULL && (strlen(realm)>0)) obj->realm=ms_strdup(realm);
48
void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
49
if (info->passwd!=NULL) {
50
ms_free(info->passwd);
53
if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
56
void linphone_auth_info_destroy(LinphoneAuthInfo *obj){
57
if (obj->username!=NULL) ms_free(obj->username);
58
if (obj->userid!=NULL) ms_free(obj->userid);
59
if (obj->passwd!=NULL) ms_free(obj->passwd);
60
if (obj->ha1!=NULL) ms_free(obj->ha1);
61
if (obj->realm!=NULL) ms_free(obj->realm);
65
void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
68
sprintf(key,"auth_info_%i",pos);
69
lp_config_clean_section(config,key);
74
if (obj->username!=NULL){
75
lp_config_set_string(config,key,"username",obj->username);
77
if (obj->userid!=NULL){
78
lp_config_set_string(config,key,"userid",obj->userid);
80
if (obj->passwd!=NULL){
81
lp_config_set_string(config,key,"passwd",obj->passwd);
84
lp_config_set_string(config,key,"ha1",obj->ha1);
86
if (obj->realm!=NULL){
87
lp_config_set_string(config,key,"realm",obj->realm);
89
lp_config_sync(config);
92
LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
95
const char *username,*userid,*passwd,*ha1,*realm;
97
sprintf(key,"auth_info_%i",pos);
98
if (!lp_config_has_section(config,key)){
102
username=lp_config_get_string(config,key,"username",NULL);
103
userid=lp_config_get_string(config,key,"userid",NULL);
104
passwd=lp_config_get_string(config,key,"passwd",NULL);
105
ha1=lp_config_get_string(config,key,"ha1",NULL);
106
realm=lp_config_get_string(config,key,"realm",NULL);
107
return linphone_auth_info_new(username,userid,passwd,ha1,realm);
110
static bool_t key_match(const char *tmp1, const char *tmp2){
111
if (tmp1==NULL && tmp2==NULL) return TRUE;
112
if (tmp1!=NULL && tmp2!=NULL && strcmp(tmp1,tmp2)==0) return TRUE;
117
static int auth_info_compare(const void *pinfo,const void *pref){
118
LinphoneAuthInfo *info=(LinphoneAuthInfo*)pinfo;
119
LinphoneAuthInfo *ref=(LinphoneAuthInfo*)pref;
120
if (key_match(info->realm,ref->realm) && key_match(info->username,ref->username)) return 0;
124
LinphoneAuthInfo *linphone_core_auth_info_find(LinphoneCore *lc, const char *realm, const char *username)
126
LinphoneAuthInfo ref;
128
ref.realm=(char*)realm;
129
ref.username=(char*)username;
130
elem=ms_list_find_custom(lc->auth_info,auth_info_compare,(void*)&ref);
131
if (elem==NULL) return NULL;
132
return (LinphoneAuthInfo*)elem->data;
135
void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
140
if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
141
else userid=info->userid;
143
eXosip_add_authentication_info(info->username,userid,
144
info->passwd,info->ha1,info->realm);
146
/* find if we are attempting to modify an existing auth info */
147
elem=ms_list_find_custom(lc->auth_info,auth_info_compare,info);
149
linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
150
elem->data=(void *)info;
151
n=ms_list_position(lc->auth_info,elem);
153
lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
154
n=ms_list_size(lc->auth_info)-1;
156
/* find if we need to restart a register request */
157
linphone_core_retry_proxy_register(lc,info->realm);
160
void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
161
int len=ms_list_size(lc->auth_info);
165
lc->auth_info=ms_list_remove(lc->auth_info,info);
166
newlen=ms_list_size(lc->auth_info);
167
/*printf("len=%i newlen=%i\n",len,newlen);*/
168
linphone_auth_info_destroy(info);
170
linphone_auth_info_write_config(lc->config,NULL,i);
172
for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
173
linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i);
178
void linphone_core_clear_all_auth_info(LinphoneCore *lc){
182
eXosip_clear_authentication_info();
184
for(i=0,elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem),i++){
185
LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
186
linphone_auth_info_destroy(info);
187
linphone_auth_info_write_config(lc->config,NULL,i);
189
ms_list_free(lc->auth_info);
193
bool_t linphone_core_find_or_ask_for_auth_info(LinphoneCore *lc,const char *username,const char* realm, bool_t force_ask)
195
if (force_ask || linphone_core_auth_info_find(lc,realm,username)==NULL){
196
if (lc->vtable.auth_info_requested!=NULL) {
197
lc->vtable.auth_info_requested(lc,realm,username);
205
bool_t linphone_process_authentication(LinphoneCore *lc, osip_message_t *resp, LinphoneProxyConfig *cfg, bool_t force_ask)
207
char *prx_realm=NULL,*www_realm=NULL;
208
osip_proxy_authenticate_t *prx_auth;
209
osip_www_authenticate_t *www_auth;
213
username=osip_uri_get_username(resp->from->url);
214
prx_auth=(osip_proxy_authenticate_t*)osip_list_get(resp->proxy_authenticates,0);
215
www_auth=(osip_proxy_authenticate_t*)osip_list_get(resp->www_authenticates,0);
217
prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
219
www_realm=osip_www_authenticate_get_realm(www_auth);
221
if (prx_realm==NULL && www_realm==NULL){
222
ms_warning("No realm in the server response.");
227
linphone_proxy_config_set_realm(cfg,prx_realm);
228
else if (www_realm!=NULL)
229
linphone_proxy_config_set_realm(cfg,www_realm);
231
/* see if we already have this auth information , not to ask it everytime to the user */
233
have_it=linphone_core_find_or_ask_for_auth_info(lc,username,prx_realm,force_ask);
235
have_it=linphone_core_find_or_ask_for_auth_info(lc,username,www_realm, force_ask) && have_it;
239
void linphone_register_authentication_required(LinphoneCore *lc,eXosip_event_t *ev){
240
osip_message_t *resp;
241
LinphoneProxyConfig *cfg;
242
eXosip_reg_t *reg=eXosip_event_get_reginfo(ev);
243
ms_return_if_fail(reg!=NULL);
245
resp=reg->r_last_tr->last_response;
246
ms_return_if_fail(resp!=NULL);
247
cfg=linphone_core_get_proxy_config_from_rid(lc,ev->rid);
248
ms_return_if_fail(cfg!=NULL);
249
if (linphone_process_authentication(lc,resp,cfg,cfg->auth_pending)){
250
/* we have the information, so retry the register */
252
eXosip_register(ev->rid,-1);
255
cfg->auth_pending=TRUE;
258
void linphone_invite_authentication_required(LinphoneCore* lc,LinphoneCall *lcall)
260
osip_message_t *resp;
261
eXosip_call_t *call=NULL;
262
eXosip_call_find(lcall->cid,&call);
263
ms_return_if_fail(call!=NULL);
264
resp=call->c_out_tr->last_response;
265
ms_return_if_fail(resp!=NULL);
266
if (linphone_process_authentication(lc,resp,NULL,lcall->auth_pending)){
268
eXosip_retry_call(lcall->cid);
271
linphone_call_destroy(lcall);