]> sjero.net Git - linphone/blob - coreapi/authentication.c
Aac-eld add missing header according to RFC3640 3.3.6
[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 "lpconfig.h"
28
29 /**
30  * @addtogroup authentication
31  * @{
32 **/
33
34 /**
35  * Create a LinphoneAuthInfo object with supplied information.
36  *
37  * The object can be created empty, that is with all arguments set to NULL.
38  * Username, userid, password and realm can be set later using specific methods.
39 **/
40 LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid,
41                                                                                                                 const char *passwd, const char *ha1,const char *realm)
42 {
43         LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
44         if (username!=NULL && (strlen(username)>0) ) obj->username=ms_strdup(username);
45         if (userid!=NULL && (strlen(userid)>0)) obj->userid=ms_strdup(userid);
46         if (passwd!=NULL && (strlen(passwd)>0)) obj->passwd=ms_strdup(passwd);
47         if (ha1!=NULL && (strlen(ha1)>0)) obj->ha1=ms_strdup(ha1);
48         if (realm!=NULL && (strlen(realm)>0)) obj->realm=ms_strdup(realm);
49         obj->works=FALSE;
50         return obj;
51 }
52
53 static LinphoneAuthInfo *linphone_auth_info_clone(const LinphoneAuthInfo *ai){
54         LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
55         if (ai->username) obj->username=ms_strdup(ai->username);
56         if (ai->userid) obj->userid=ms_strdup(ai->userid);
57         if (ai->passwd) obj->passwd=ms_strdup(ai->passwd);
58         if (ai->ha1)    obj->ha1=ms_strdup(ai->ha1);
59         if (ai->realm)  obj->realm=ms_strdup(ai->realm);
60         obj->works=FALSE;
61         obj->usecount=0;
62         return obj;
63 }
64
65 /**
66  * Returns username.
67 **/
68 const char *linphone_auth_info_get_username(const LinphoneAuthInfo *i){
69         return i->username;
70 }
71
72 /**
73  * Returns password.
74 **/
75 const char *linphone_auth_info_get_passwd(const LinphoneAuthInfo *i){
76         return i->passwd;
77 }
78
79 const char *linphone_auth_info_get_userid(const LinphoneAuthInfo *i){
80         return i->userid;
81 }
82
83 const char *linphone_auth_info_get_realm(const LinphoneAuthInfo *i){
84         return i->realm;
85 }
86 const char *linphone_auth_info_get_ha1(const LinphoneAuthInfo *i){
87         return i->ha1;
88 }
89
90 /**
91  * Sets the password.
92 **/
93 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
94         if (info->passwd!=NULL) {
95                 ms_free(info->passwd);
96                 info->passwd=NULL;
97         }
98         if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
99 }
100
101 /**
102  * Sets the username.
103 **/
104 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
105         if (info->username){
106                 ms_free(info->username);
107                 info->username=NULL;
108         }
109         if (username && strlen(username)>0) info->username=ms_strdup(username);
110 }
111
112 /**
113  * Sets userid.
114 **/
115 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
116         if (info->userid){
117                 ms_free(info->userid);
118                 info->userid=NULL;
119         }
120         if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
121 }
122
123 /**
124  * Sets realm.
125 **/
126 void linphone_auth_info_set_realm(LinphoneAuthInfo *info, const char *realm){
127         if (info->realm){
128                 ms_free(info->realm);
129                 info->realm=NULL;
130         }
131         if (realm && strlen(realm)>0) info->realm=ms_strdup(realm);
132 }
133 /**
134  * Sets ha1.
135 **/
136 void linphone_auth_info_set_ha1(LinphoneAuthInfo *info, const char *ha1){
137         if (info->ha1){
138                 ms_free(info->ha1);
139                 info->ha1=NULL;
140         }
141         if (ha1 && strlen(ha1)>0) info->ha1=ms_strdup(ha1);
142 }
143
144 /**
145  * Destroys a LinphoneAuthInfo object.
146 **/
147 void linphone_auth_info_destroy(LinphoneAuthInfo *obj){
148         if (obj->username!=NULL) ms_free(obj->username);
149         if (obj->userid!=NULL) ms_free(obj->userid);
150         if (obj->passwd!=NULL) ms_free(obj->passwd);
151         if (obj->ha1!=NULL) ms_free(obj->ha1);
152         if (obj->realm!=NULL) ms_free(obj->realm);
153         ms_free(obj);
154 }
155
156 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
157 {
158         char key[50];
159         sprintf(key,"auth_info_%i",pos);
160         lp_config_clean_section(config,key);
161         
162         if (obj==NULL || lp_config_get_int(config, "sip", "store_auth_info", 1) == 0){
163                 return;
164         }               
165         if (obj->username!=NULL){
166                 lp_config_set_string(config,key,"username",obj->username);
167         }
168         if (obj->userid!=NULL){
169                 lp_config_set_string(config,key,"userid",obj->userid);
170         }
171         if (obj->passwd!=NULL){
172                 lp_config_set_string(config,key,"passwd",obj->passwd);
173         }
174         if (obj->ha1!=NULL){
175                 lp_config_set_string(config,key,"ha1",obj->ha1);
176         }
177         if (obj->realm!=NULL){
178                 lp_config_set_string(config,key,"realm",obj->realm);
179         }
180 }
181
182 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
183 {
184         char key[50];
185         const char *username,*userid,*passwd,*ha1,*realm;
186         
187         sprintf(key,"auth_info_%i",pos);
188         if (!lp_config_has_section(config,key)){
189                 return NULL;
190         }
191         
192         username=lp_config_get_string(config,key,"username",NULL);
193         userid=lp_config_get_string(config,key,"userid",NULL);
194         passwd=lp_config_get_string(config,key,"passwd",NULL);
195         ha1=lp_config_get_string(config,key,"ha1",NULL);
196         realm=lp_config_get_string(config,key,"realm",NULL);
197         return linphone_auth_info_new(username,userid,passwd,ha1,realm);
198 }
199
200 static bool_t key_match(const char *tmp1, const char *tmp2){
201         if (tmp1==NULL && tmp2==NULL) return TRUE;
202         if (tmp1!=NULL && tmp2!=NULL && strcmp(tmp1,tmp2)==0) return TRUE;
203         return FALSE;
204         
205 }
206
207 static char * remove_quotes(char * input){
208         char *tmp;
209         if (*input=='"') input++;
210         tmp=strchr(input,'"');
211         if (tmp) *tmp='\0';
212         return input;
213 }
214
215 static int realm_match(const char *realm1, const char *realm2){
216         if (realm1==NULL && realm2==NULL) return TRUE;
217         if (realm1!=NULL && realm2!=NULL){
218                 if (strcmp(realm1,realm2)==0) return TRUE;
219                 else{
220                         char tmp1[128];
221                         char tmp2[128];
222                         char *p1,*p2;
223                         strncpy(tmp1,realm1,sizeof(tmp1)-1);
224                         strncpy(tmp2,realm2,sizeof(tmp2)-1);
225                         p1=remove_quotes(tmp1);
226                         p2=remove_quotes(tmp2);
227                         return strcmp(p1,p2)==0;
228                 }
229         }
230         return FALSE;
231 }
232
233 /**
234  * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
235 **/
236 const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
237 {
238         MSList *elem;
239         LinphoneAuthInfo *ret=NULL,*candidate=NULL;
240         for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
241                 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
242                 if (realm==NULL){
243                         /*return the authinfo for any realm provided that there is only one for that username*/
244                         if (key_match(pinfo->username,username)){
245                                 if (ret!=NULL){
246                                         ms_warning("There are several auth info for username '%s'",username);
247                                         return NULL;
248                                 }
249                                 ret=pinfo;
250                         }
251                 }else{
252                         /*return the exact authinfo, or an authinfo for which realm was not supplied yet*/
253                         if (pinfo->realm!=NULL){
254                                 if (realm_match(pinfo->realm,realm) 
255                                         && key_match(pinfo->username,username))
256                                         ret=pinfo;
257                         }else{
258                                 if (key_match(pinfo->username,username))
259                                         candidate=pinfo;
260                         }
261                 }
262         }
263         if (ret==NULL && candidate!=NULL)
264                 ret=candidate;
265         return ret;
266 }
267
268 static void write_auth_infos(LinphoneCore *lc){
269         MSList *elem;
270         int i;
271
272         if (!linphone_core_ready(lc)) return;
273         for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
274                 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
275                 linphone_auth_info_write_config(lc->config,ai,i);
276         }
277         linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
278 }
279
280 /**
281  * Adds authentication information to the LinphoneCore.
282  * 
283  * This information will be used during all SIP transacations that require authentication.
284 **/
285 void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info)
286 {
287         LinphoneAuthInfo *ai;
288         MSList *elem;
289         MSList *l;
290         
291         /* find if we are attempting to modify an existing auth info */
292         ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
293         if (ai!=NULL){
294                 lc->auth_info=ms_list_remove(lc->auth_info,ai);
295                 linphone_auth_info_destroy(ai);
296         }
297         lc->auth_info=ms_list_append(lc->auth_info,linphone_auth_info_clone(info));
298         /* retry pending authentication operations */
299         for(l=elem=sal_get_pending_auths(lc->sal);elem!=NULL;elem=elem->next){
300                 const char *username,*realm;
301                 SalOp *op=(SalOp*)elem->data;
302                 LinphoneAuthInfo *ai;
303                 sal_op_get_auth_requested(op,&realm,&username);
304                 ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
305                 if (ai){
306                         SalAuthInfo sai;
307                         sai.username=ai->username;
308                         sai.userid=ai->userid;
309                         sai.realm=ai->realm;
310                         sai.password=ai->passwd;
311                         sal_op_authenticate(op,&sai);
312                         ai->usecount++;
313                 }
314         }
315         ms_list_free(l);
316         write_auth_infos(lc);
317 }
318
319
320 /**
321  * This method is used to abort a user authentication request initiated by LinphoneCore
322  * from the auth_info_requested callback of LinphoneCoreVTable.
323 **/
324 void linphone_core_abort_authentication(LinphoneCore *lc,  LinphoneAuthInfo *info){
325 }
326
327 /**
328  * Removes an authentication information object.
329 **/
330 void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
331         LinphoneAuthInfo *r;
332         r=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
333         if (r){
334                 lc->auth_info=ms_list_remove(lc->auth_info,r);
335                 /*printf("len=%i newlen=%i\n",len,newlen);*/
336                 linphone_auth_info_destroy(r);
337                 write_auth_infos(lc);
338         }
339 }
340
341 /**
342  * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
343 **/
344 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
345         return lc->auth_info;
346 }
347
348 /**
349  * Clear all authentication information.
350 **/
351 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
352         MSList *elem;
353         int i;
354         for(i=0,elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem),i++){
355                 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
356                 linphone_auth_info_destroy(info);
357                 linphone_auth_info_write_config(lc->config,NULL,i);
358         }
359         ms_list_free(lc->auth_info);
360         lc->auth_info=NULL;
361 }
362
363 /**
364  * @}
365 **/