~ubuntu-branches/ubuntu/quantal/linphone/quantal

« back to all changes in this revision

Viewing changes to coreapi/authentication.c

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-11-15 10:34:50 UTC
  • mfrom: (1.2.1 upstream) (2.1.8 feisty)
  • Revision ID: james.westby@ubuntu.com-20061115103450-qgafwcks2lkhctlj
* New upstream release.
* Enable video support.
* Fix mismatched #endif in mscommon.h, closes: #398307.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *            authentication.c
 
3
 *
 
4
 *  Fri Jul 16 12:08:34 2004
 
5
 *  Copyright  2004  Simon MORLAT
 
6
 *  simon.morlat@linphone.org
 
7
 ****************************************************************************/
 
8
 
 
9
/*
 
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.
 
14
 *
 
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.
 
19
 *
 
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.
 
23
 */
 
24
 
 
25
#include "linphonecore.h"
 
26
#include "private.h"
 
27
#include <eXosip.h>
 
28
#include <osipparser2/osip_message.h>
 
29
#include "eXosip2.h"
 
30
#include "lpconfig.h"
 
31
 
 
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);
 
35
 
 
36
LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid,
 
37
                                                                                                                const char *passwd, const char *ha1,const char *realm)
 
38
{
 
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);
 
45
        return obj;
 
46
}
 
47
 
 
48
void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
 
49
        if (info->passwd!=NULL) {
 
50
                ms_free(info->passwd);
 
51
                info->passwd=NULL;
 
52
        }
 
53
        if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
 
54
}
 
55
 
 
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);
 
62
        ms_free(obj);
 
63
}
 
64
 
 
65
void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
 
66
{
 
67
        char key[50];
 
68
        sprintf(key,"auth_info_%i",pos);
 
69
        lp_config_clean_section(config,key);
 
70
        
 
71
        if (obj==NULL){
 
72
                return;
 
73
        }               
 
74
        if (obj->username!=NULL){
 
75
                lp_config_set_string(config,key,"username",obj->username);
 
76
        }
 
77
        if (obj->userid!=NULL){
 
78
                lp_config_set_string(config,key,"userid",obj->userid);
 
79
        }
 
80
        if (obj->passwd!=NULL){
 
81
                lp_config_set_string(config,key,"passwd",obj->passwd);
 
82
        }
 
83
        if (obj->ha1!=NULL){
 
84
                lp_config_set_string(config,key,"ha1",obj->ha1);
 
85
        }
 
86
        if (obj->realm!=NULL){
 
87
                lp_config_set_string(config,key,"realm",obj->realm);
 
88
        }
 
89
        lp_config_sync(config);
 
90
}
 
91
 
 
92
LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
 
93
{
 
94
        char key[50];
 
95
        const char *username,*userid,*passwd,*ha1,*realm;
 
96
        
 
97
        sprintf(key,"auth_info_%i",pos);
 
98
        if (!lp_config_has_section(config,key)){
 
99
                return NULL;
 
100
        }
 
101
        
 
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);
 
108
}
 
109
 
 
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;
 
113
        return FALSE;
 
114
        
 
115
}
 
116
 
 
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;
 
121
        return -1;
 
122
}
 
123
 
 
124
LinphoneAuthInfo *linphone_core_auth_info_find(LinphoneCore *lc, const char *realm, const char *username)
 
125
{
 
126
        LinphoneAuthInfo ref;
 
127
        MSList *elem;
 
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;
 
133
}
 
134
 
 
135
void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
 
136
{
 
137
        int n;
 
138
        MSList *elem;
 
139
        char *userid;
 
140
        if (info->userid==NULL || info->userid[0]=='\0') userid=info->username;
 
141
        else userid=info->userid;
 
142
        eXosip_lock();
 
143
        eXosip_add_authentication_info(info->username,userid,
 
144
                                info->passwd,info->ha1,info->realm);
 
145
        eXosip_unlock();
 
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);
 
148
        if (elem!=NULL){
 
149
                linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
 
150
                elem->data=(void *)info;
 
151
                n=ms_list_position(lc->auth_info,elem);
 
152
        }else {
 
153
                lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
 
154
                n=ms_list_size(lc->auth_info)-1;
 
155
        }
 
156
        /* find if we need to restart a register request */
 
157
        linphone_core_retry_proxy_register(lc,info->realm);
 
158
}
 
159
 
 
160
void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
 
161
        int len=ms_list_size(lc->auth_info);
 
162
        int newlen;
 
163
        int i;
 
164
        MSList *elem;
 
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);
 
169
        for (i=0;i<len;i++){
 
170
                linphone_auth_info_write_config(lc->config,NULL,i);
 
171
        }
 
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);
 
174
        }
 
175
        
 
176
}
 
177
 
 
178
void linphone_core_clear_all_auth_info(LinphoneCore *lc){
 
179
        MSList *elem;
 
180
        int i;
 
181
        eXosip_lock();
 
182
        eXosip_clear_authentication_info();
 
183
        eXosip_unlock();
 
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);
 
188
        }
 
189
        ms_list_free(lc->auth_info);
 
190
        lc->auth_info=NULL;
 
191
}
 
192
 
 
193
bool_t linphone_core_find_or_ask_for_auth_info(LinphoneCore *lc,const char *username,const char* realm, bool_t force_ask)
 
194
{
 
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);
 
198
                }
 
199
                return FALSE;
 
200
        }else{
 
201
                return TRUE;
 
202
        }
 
203
}
 
204
 
 
205
bool_t linphone_process_authentication(LinphoneCore *lc, osip_message_t *resp, LinphoneProxyConfig *cfg, bool_t force_ask)
 
206
{
 
207
        char *prx_realm=NULL,*www_realm=NULL;
 
208
        osip_proxy_authenticate_t *prx_auth;
 
209
        osip_www_authenticate_t *www_auth;
 
210
        char *username;
 
211
        bool_t have_it=TRUE;
 
212
        
 
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);
 
216
        if (prx_auth!=NULL)
 
217
                prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
 
218
        if (www_auth!=NULL)
 
219
                www_realm=osip_www_authenticate_get_realm(www_auth);
 
220
        
 
221
        if (prx_realm==NULL && www_realm==NULL){
 
222
                ms_warning("No realm in the server response.");
 
223
                return FALSE;
 
224
        }
 
225
        if (cfg!=NULL) {
 
226
                if (prx_realm!=NULL)
 
227
                        linphone_proxy_config_set_realm(cfg,prx_realm);
 
228
                else if (www_realm!=NULL)
 
229
                        linphone_proxy_config_set_realm(cfg,www_realm);
 
230
        }
 
231
        /* see if we already have this auth information , not to ask it everytime to the user */
 
232
        if (prx_realm!=NULL) 
 
233
                have_it=linphone_core_find_or_ask_for_auth_info(lc,username,prx_realm,force_ask);
 
234
        if (www_realm!=NULL) 
 
235
                have_it=linphone_core_find_or_ask_for_auth_info(lc,username,www_realm, force_ask) && have_it;
 
236
        return have_it;
 
237
}
 
238
 
 
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);
 
244
        
 
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 */
 
251
                eXosip_lock();
 
252
                eXosip_register(ev->rid,-1);
 
253
                eXosip_unlock();
 
254
        }
 
255
        cfg->auth_pending=TRUE;
 
256
}
 
257
 
 
258
void linphone_invite_authentication_required(LinphoneCore* lc,LinphoneCall *lcall)
 
259
{
 
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)){
 
267
                eXosip_lock();
 
268
                eXosip_retry_call(lcall->cid);
 
269
                eXosip_unlock();
 
270
        }else{
 
271
                linphone_call_destroy(lcall);
 
272
                lc->call=NULL;
 
273
        }
 
274
        
 
275
}