source: mediastreamer2/linphone/coreapi/authentication.c @ 797:204f2e97bc62

Last change on this file since 797:204f2e97bc62 was 797:204f2e97bc62, checked in by smorlat <smorlat@…>, 3 years ago

Add API docs for liblinphone.

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@785 3f6dc0c8-ddfe-455d-9043-3cd528dc4637

File size: 11.7 KB
Line 
1/***************************************************************************
2 *            authentication.c
3 *
4 *  Fri Jul 16 12:08:34 2004
5 *  Copyright  2004-2009  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 <eXosip2/eXosip.h>
28#include <osipparser2/osip_message.h>
29#include "lpconfig.h"
30
31extern LinphoneProxyConfig *linphone_core_get_proxy_config_from_rid(LinphoneCore *lc, int rid);
32
33/**
34 * @addtogroup authentication
35 * @{
36**/
37
38/**
39 * Create a LinphoneAuthInfo object with supplied information.
40 *
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.
43**/
44LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid,
45                                                                                                                const char *passwd, const char *ha1,const char *realm)
46{
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);
53        obj->works=FALSE;
54        obj->first_time=TRUE;
55        return obj;
56}
57
58/**
59 * Sets the password.
60**/
61void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
62        if (info->passwd!=NULL) {
63                ms_free(info->passwd);
64                info->passwd=NULL;
65        }
66        if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
67}
68
69/**
70 * Sets the username.
71**/
72void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
73        if (info->username){
74                ms_free(info->username);
75                info->username=NULL;
76        }
77        if (username && strlen(username)>0) info->username=ms_strdup(username);
78}
79
80/**
81 * Sets userid.
82**/
83void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
84        if (info->userid){
85                ms_free(info->userid);
86                info->userid=NULL;
87        }
88        if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
89}
90
91/**
92 * Destroys a LinphoneAuthInfo object.
93**/
94void 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);
100        ms_free(obj);
101}
102
103void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
104{
105        char key[50];
106        sprintf(key,"auth_info_%i",pos);
107        lp_config_clean_section(config,key);
108       
109        if (obj==NULL){
110                return;
111        }               
112        if (obj->username!=NULL){
113                lp_config_set_string(config,key,"username",obj->username);
114        }
115        if (obj->userid!=NULL){
116                lp_config_set_string(config,key,"userid",obj->userid);
117        }
118        if (obj->passwd!=NULL){
119                lp_config_set_string(config,key,"passwd",obj->passwd);
120        }
121        if (obj->ha1!=NULL){
122                lp_config_set_string(config,key,"ha1",obj->ha1);
123        }
124        if (obj->realm!=NULL){
125                lp_config_set_string(config,key,"realm",obj->realm);
126        }
127}
128
129LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
130{
131        char key[50];
132        const char *username,*userid,*passwd,*ha1,*realm;
133       
134        sprintf(key,"auth_info_%i",pos);
135        if (!lp_config_has_section(config,key)){
136                return NULL;
137        }
138       
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);
145}
146
147static 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;
150        return FALSE;
151       
152}
153
154static char * remove_quotes(char * input){
155        char *tmp;
156        if (*input=='"') input++;
157        tmp=strchr(input,'"');
158        if (tmp) *tmp='\0';
159        return input;
160}
161
162static 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;
166                else{
167                        char tmp1[128];
168                        char tmp2[128];
169                        char *p1,*p2;
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;
175                }
176        }
177        return FALSE;
178}
179
180/**
181 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
182**/
183LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
184{
185        MSList *elem;
186        LinphoneAuthInfo *ret=NULL,*candidate=NULL;
187        for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
188                LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
189                if (realm==NULL){
190                        /*return the authinfo for any realm provided that there is only one for that username*/
191                        if (key_match(pinfo->username,username)){
192                                if (ret!=NULL){
193                                        ms_warning("There are several auth info for username '%s'",username);
194                                        return NULL;
195                                }
196                                ret=pinfo;
197                        }
198                }else{
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))
203                                        ret=pinfo;
204                        }else{
205                                if (key_match(pinfo->username,username))
206                                        candidate=pinfo;
207                        }
208                }
209        }
210        if (ret==NULL && candidate!=NULL)
211                ret=candidate;
212        return ret;
213}
214
215static void refresh_exosip_auth_info(LinphoneCore *lc){
216        MSList *elem;
217        eXosip_lock();
218        eXosip_clear_authentication_info();
219        for (elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem)){
220                LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
221                char *userid;
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);
226        }
227        eXosip_unlock();
228}
229
230/**
231 * Adds authentication information to the LinphoneCore.
232 *
233 * This information will be used during all SIP transacations that require authentication.
234**/
235void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
236{
237        MSList *elem;
238        LinphoneAuthInfo *ai;
239       
240        /* find if we are attempting to modify an existing auth info */
241        ai=linphone_core_find_auth_info(lc,info->realm,info->username);
242        if (ai!=NULL){
243                elem=ms_list_find(lc->auth_info,ai);
244                if (elem==NULL){
245                        ms_error("AuthInfo list corruption ?");
246                        return;
247                }
248                linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
249                elem->data=(void *)info;
250        }else {
251                lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
252        }
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--;
256}
257
258
259/**
260 * This method is used to abort a user authentication request initiated by LinphoneCore
261 * from the auth_info_requested callback of LinphoneCoreVTable.
262**/
263void linphone_core_abort_authentication(LinphoneCore *lc,  LinphoneAuthInfo *info){
264        if (lc->automatic_action>0) lc->automatic_action--;
265}
266
267/**
268 * Removes an authentication information object.
269**/
270void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
271        int len=ms_list_size(lc->auth_info);
272        int newlen;
273        int i;
274        MSList *elem;
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);
279        for (i=0;i<len;i++){
280                linphone_auth_info_write_config(lc->config,NULL,i);
281        }
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);
284        }
285        refresh_exosip_auth_info(lc);
286       
287}
288
289/**
290 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
291**/
292const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
293        return lc->auth_info;
294}
295
296/**
297 * Clear all authentication information.
298**/
299void linphone_core_clear_all_auth_info(LinphoneCore *lc){
300        MSList *elem;
301        int i;
302        eXosip_lock();
303        eXosip_clear_authentication_info();
304        eXosip_unlock();
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);
309        }
310        ms_list_free(lc->auth_info);
311        lc->auth_info=NULL;
312}
313
314void 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;
319        char *username;
320        LinphoneAuthInfo *as=NULL;
321
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);
325        if (prx_auth!=NULL)
326                prx_realm=osip_proxy_authorization_get_realm(prx_auth);
327        if (www_auth!=NULL)
328                www_realm=osip_authorization_get_realm(www_auth);
329       
330        if (prx_realm==NULL && www_realm==NULL){
331                ms_message("No authentication info in the request, ignoring");
332                return;
333        }
334        /* see if we already have this auth information , not to ask it everytime to the user */
335        if (prx_realm!=NULL)
336                as=linphone_core_find_auth_info(lc,prx_realm,username);
337        if (www_realm!=NULL) 
338                as=linphone_core_find_auth_info(lc,www_realm,username);
339        if (as){
340                ms_message("Authentication for user=%s realm=%s is working.",username,prx_realm ? prx_realm : www_realm);
341                as->works=TRUE;
342        }
343}
344
345
346void linphone_core_find_or_ask_for_auth_info(LinphoneCore *lc,const char *username,const char* realm, int tid)
347{
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 */
353                }
354        }
355        if (as) as->first_time=FALSE;
356}
357
358void linphone_process_authentication(LinphoneCore *lc, eXosip_event_t *ev)
359{
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;
364        char *username;
365       
366        if (strcmp(ev->request->sip_method,"REGISTER")==0) {
367                gstate_new_state(lc, GSTATE_REG_FAILED, "Authentication required");
368        }
369
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);
373        if (prx_auth!=NULL)
374                prx_realm=osip_proxy_authenticate_get_realm(prx_auth);
375        if (www_auth!=NULL)
376                www_realm=osip_www_authenticate_get_realm(www_auth);
377       
378        if (prx_realm==NULL && www_realm==NULL){
379                ms_warning("No realm in the server response.");
380                return;
381        }
382        /* see if we already have this auth information , not to ask it everytime to the user */
383        if (prx_realm!=NULL) 
384                linphone_core_find_or_ask_for_auth_info(lc,username,prx_realm,ev->tid);
385        if (www_realm!=NULL) 
386                linphone_core_find_or_ask_for_auth_info(lc,username,www_realm,ev->tid);
387}
388
389
390/**
391 * @}
392**/
Note: See TracBrowser for help on using the repository browser.