]> sjero.net Git - linphone/blob - coreapi/authentication.c
sal in progress, near to code complete.
[linphone] / coreapi / authentication.c
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
31 extern 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 **/
44 LinphoneAuthInfo *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         return obj;
55 }
56
57 /**
58  * Sets the password.
59 **/
60 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
61         if (info->passwd!=NULL) {
62                 ms_free(info->passwd);
63                 info->passwd=NULL;
64         }
65         if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
66 }
67
68 /**
69  * Sets the username.
70 **/
71 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
72         if (info->username){
73                 ms_free(info->username);
74                 info->username=NULL;
75         }
76         if (username && strlen(username)>0) info->username=ms_strdup(username);
77 }
78
79 /**
80  * Sets userid.
81 **/
82 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
83         if (info->userid){
84                 ms_free(info->userid);
85                 info->userid=NULL;
86         }
87         if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
88 }
89
90 /**
91  * Destroys a LinphoneAuthInfo object.
92 **/
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);
99         ms_free(obj);
100 }
101
102 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
103 {
104         char key[50];
105         sprintf(key,"auth_info_%i",pos);
106         lp_config_clean_section(config,key);
107         
108         if (obj==NULL){
109                 return;
110         }               
111         if (obj->username!=NULL){
112                 lp_config_set_string(config,key,"username",obj->username);
113         }
114         if (obj->userid!=NULL){
115                 lp_config_set_string(config,key,"userid",obj->userid);
116         }
117         if (obj->passwd!=NULL){
118                 lp_config_set_string(config,key,"passwd",obj->passwd);
119         }
120         if (obj->ha1!=NULL){
121                 lp_config_set_string(config,key,"ha1",obj->ha1);
122         }
123         if (obj->realm!=NULL){
124                 lp_config_set_string(config,key,"realm",obj->realm);
125         }
126 }
127
128 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
129 {
130         char key[50];
131         const char *username,*userid,*passwd,*ha1,*realm;
132         
133         sprintf(key,"auth_info_%i",pos);
134         if (!lp_config_has_section(config,key)){
135                 return NULL;
136         }
137         
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);
144 }
145
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;
149         return FALSE;
150         
151 }
152
153 static char * remove_quotes(char * input){
154         char *tmp;
155         if (*input=='"') input++;
156         tmp=strchr(input,'"');
157         if (tmp) *tmp='\0';
158         return input;
159 }
160
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;
165                 else{
166                         char tmp1[128];
167                         char tmp2[128];
168                         char *p1,*p2;
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;
174                 }
175         }
176         return FALSE;
177 }
178
179 /**
180  * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
181 **/
182 LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
183 {
184         MSList *elem;
185         LinphoneAuthInfo *ret=NULL,*candidate=NULL;
186         for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
187                 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
188                 if (realm==NULL){
189                         /*return the authinfo for any realm provided that there is only one for that username*/
190                         if (key_match(pinfo->username,username)){
191                                 if (ret!=NULL){
192                                         ms_warning("There are several auth info for username '%s'",username);
193                                         return NULL;
194                                 }
195                                 ret=pinfo;
196                         }
197                 }else{
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))
202                                         ret=pinfo;
203                         }else{
204                                 if (key_match(pinfo->username,username))
205                                         candidate=pinfo;
206                         }
207                 }
208         }
209         if (ret==NULL && candidate!=NULL)
210                 ret=candidate;
211         return ret;
212 }
213
214 static void refresh_exosip_auth_info(LinphoneCore *lc){
215         MSList *elem;
216         eXosip_lock();
217         eXosip_clear_authentication_info();
218         for (elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem)){
219                 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
220                 char *userid;
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);
225         }
226         eXosip_unlock();
227 }
228
229 /**
230  * Adds authentication information to the LinphoneCore.
231  * 
232  * This information will be used during all SIP transacations that require authentication.
233 **/
234 void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
235 {
236         MSList *elem;
237         LinphoneAuthInfo *ai;
238         
239         /* find if we are attempting to modify an existing auth info */
240         ai=linphone_core_find_auth_info(lc,info->realm,info->username);
241         if (ai!=NULL){
242                 elem=ms_list_find(lc->auth_info,ai);
243                 if (elem==NULL){
244                         ms_error("AuthInfo list corruption ?");
245                         return;
246                 }
247                 linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
248                 elem->data=(void *)info;
249         }else {
250                 lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
251         }
252         refresh_exosip_auth_info(lc);
253         /* if the user was prompted, re-allow automatic_action */
254 }
255
256
257 /**
258  * This method is used to abort a user authentication request initiated by LinphoneCore
259  * from the auth_info_requested callback of LinphoneCoreVTable.
260 **/
261 void linphone_core_abort_authentication(LinphoneCore *lc,  LinphoneAuthInfo *info){
262         if (lc->automatic_action>0) lc->automatic_action--;
263 }
264
265 /**
266  * Removes an authentication information object.
267 **/
268 void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
269         int len=ms_list_size(lc->auth_info);
270         int newlen;
271         int i;
272         MSList *elem;
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);
277         for (i=0;i<len;i++){
278                 linphone_auth_info_write_config(lc->config,NULL,i);
279         }
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);
282         }
283         refresh_exosip_auth_info(lc);
284         
285 }
286
287 /**
288  * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
289 **/
290 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
291         return lc->auth_info;
292 }
293
294 /**
295  * Clear all authentication information.
296 **/
297 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
298         MSList *elem;
299         int i;
300         eXosip_lock();
301         eXosip_clear_authentication_info();
302         eXosip_unlock();
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);
307         }
308         ms_list_free(lc->auth_info);
309         lc->auth_info=NULL;
310 }
311
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;
317         char *username;
318         LinphoneAuthInfo *as=NULL;
319
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);
323         if (prx_auth!=NULL)
324                 prx_realm=osip_proxy_authorization_get_realm(prx_auth);
325         if (www_auth!=NULL)
326                 www_realm=osip_authorization_get_realm(www_auth);
327         
328         if (prx_realm==NULL && www_realm==NULL){
329                 ms_message("No authentication info in the request, ignoring");
330                 return;
331         }
332         /* see if we already have this auth information , not to ask it everytime to the user */
333         if (prx_realm!=NULL)
334                 as=linphone_core_find_auth_info(lc,prx_realm,username);
335         if (www_realm!=NULL) 
336                 as=linphone_core_find_auth_info(lc,www_realm,username);
337         if (as){
338                 ms_message("Authentication for user=%s realm=%s is working.",username,prx_realm ? prx_realm : www_realm);
339                 as->works=TRUE;
340         }
341 }
342
343
344 void linphone_core_find_or_ask_for_auth_info(LinphoneCore *lc,const char *username,const char* realm, int tid)
345 {
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 */
351                 }
352         }
353         if (as) as->first_time=FALSE;
354 }
355
356 void linphone_process_authentication(LinphoneCore *lc, eXosip_event_t *ev)
357 {
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;
362         char *username;
363
364         /*
365         if (strcmp(ev->request->sip_method,"REGISTER")==0) {
366                 gstate_new_state(lc, GSTATE_REG_FAILED, "Authentication required");
367         }
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 **/