]> sjero.net Git - linphone/blob - coreapi/authentication.c
4b5d10fa8560d9f1e146c714310638b2ab4254bd
[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 /**
84  * Sets the password.
85 **/
86 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
87         if (info->passwd!=NULL) {
88                 ms_free(info->passwd);
89                 info->passwd=NULL;
90         }
91         if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
92 }
93
94 /**
95  * Sets the username.
96 **/
97 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
98         if (info->username){
99                 ms_free(info->username);
100                 info->username=NULL;
101         }
102         if (username && strlen(username)>0) info->username=ms_strdup(username);
103 }
104
105 /**
106  * Sets userid.
107 **/
108 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
109         if (info->userid){
110                 ms_free(info->userid);
111                 info->userid=NULL;
112         }
113         if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
114 }
115
116 /**
117  * Destroys a LinphoneAuthInfo object.
118 **/
119 void linphone_auth_info_destroy(LinphoneAuthInfo *obj){
120         if (obj->username!=NULL) ms_free(obj->username);
121         if (obj->userid!=NULL) ms_free(obj->userid);
122         if (obj->passwd!=NULL) ms_free(obj->passwd);
123         if (obj->ha1!=NULL) ms_free(obj->ha1);
124         if (obj->realm!=NULL) ms_free(obj->realm);
125         ms_free(obj);
126 }
127
128 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
129 {
130         char key[50];
131         sprintf(key,"auth_info_%i",pos);
132         lp_config_clean_section(config,key);
133         
134         if (obj==NULL || lp_config_get_int(config, "sip", "store_auth_info", 1) == 0){
135                 return;
136         }               
137         if (obj->username!=NULL){
138                 lp_config_set_string(config,key,"username",obj->username);
139         }
140         if (obj->userid!=NULL){
141                 lp_config_set_string(config,key,"userid",obj->userid);
142         }
143         if (obj->passwd!=NULL){
144                 lp_config_set_string(config,key,"passwd",obj->passwd);
145         }
146         if (obj->ha1!=NULL){
147                 lp_config_set_string(config,key,"ha1",obj->ha1);
148         }
149         if (obj->realm!=NULL){
150                 lp_config_set_string(config,key,"realm",obj->realm);
151         }
152 }
153
154 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
155 {
156         char key[50];
157         const char *username,*userid,*passwd,*ha1,*realm;
158         
159         sprintf(key,"auth_info_%i",pos);
160         if (!lp_config_has_section(config,key)){
161                 return NULL;
162         }
163         
164         username=lp_config_get_string(config,key,"username",NULL);
165         userid=lp_config_get_string(config,key,"userid",NULL);
166         passwd=lp_config_get_string(config,key,"passwd",NULL);
167         ha1=lp_config_get_string(config,key,"ha1",NULL);
168         realm=lp_config_get_string(config,key,"realm",NULL);
169         return linphone_auth_info_new(username,userid,passwd,ha1,realm);
170 }
171
172 static bool_t key_match(const char *tmp1, const char *tmp2){
173         if (tmp1==NULL && tmp2==NULL) return TRUE;
174         if (tmp1!=NULL && tmp2!=NULL && strcmp(tmp1,tmp2)==0) return TRUE;
175         return FALSE;
176         
177 }
178
179 static char * remove_quotes(char * input){
180         char *tmp;
181         if (*input=='"') input++;
182         tmp=strchr(input,'"');
183         if (tmp) *tmp='\0';
184         return input;
185 }
186
187 static int realm_match(const char *realm1, const char *realm2){
188         if (realm1==NULL && realm2==NULL) return TRUE;
189         if (realm1!=NULL && realm2!=NULL){
190                 if (strcmp(realm1,realm2)==0) return TRUE;
191                 else{
192                         char tmp1[128];
193                         char tmp2[128];
194                         char *p1,*p2;
195                         strncpy(tmp1,realm1,sizeof(tmp1)-1);
196                         strncpy(tmp2,realm2,sizeof(tmp2)-1);
197                         p1=remove_quotes(tmp1);
198                         p2=remove_quotes(tmp2);
199                         return strcmp(p1,p2)==0;
200                 }
201         }
202         return FALSE;
203 }
204
205 /**
206  * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
207 **/
208 const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
209 {
210         MSList *elem;
211         LinphoneAuthInfo *ret=NULL,*candidate=NULL;
212         for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
213                 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
214                 if (realm==NULL){
215                         /*return the authinfo for any realm provided that there is only one for that username*/
216                         if (key_match(pinfo->username,username)){
217                                 if (ret!=NULL){
218                                         ms_warning("There are several auth info for username '%s'",username);
219                                         return NULL;
220                                 }
221                                 ret=pinfo;
222                         }
223                 }else{
224                         /*return the exact authinfo, or an authinfo for which realm was not supplied yet*/
225                         if (pinfo->realm!=NULL){
226                                 if (realm_match(pinfo->realm,realm) 
227                                         && key_match(pinfo->username,username))
228                                         ret=pinfo;
229                         }else{
230                                 if (key_match(pinfo->username,username))
231                                         candidate=pinfo;
232                         }
233                 }
234         }
235         if (ret==NULL && candidate!=NULL)
236                 ret=candidate;
237         return ret;
238 }
239
240 static void write_auth_infos(LinphoneCore *lc){
241         MSList *elem;
242         int i;
243
244         if (!linphone_core_ready(lc)) return;
245         for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
246                 LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
247                 linphone_auth_info_write_config(lc->config,ai,i);
248         }
249         linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
250 }
251
252 /**
253  * Adds authentication information to the LinphoneCore.
254  * 
255  * This information will be used during all SIP transacations that require authentication.
256 **/
257 void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info)
258 {
259         LinphoneAuthInfo *ai;
260         MSList *elem;
261         MSList *l;
262         
263         /* find if we are attempting to modify an existing auth info */
264         ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
265         if (ai!=NULL){
266                 lc->auth_info=ms_list_remove(lc->auth_info,ai);
267                 linphone_auth_info_destroy(ai);
268         }
269         lc->auth_info=ms_list_append(lc->auth_info,linphone_auth_info_clone(info));
270         /* retry pending authentication operations */
271         for(l=elem=sal_get_pending_auths(lc->sal);elem!=NULL;elem=elem->next){
272                 const char *username,*realm;
273                 SalOp *op=(SalOp*)elem->data;
274                 LinphoneAuthInfo *ai;
275                 sal_op_get_auth_requested(op,&realm,&username);
276                 ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
277                 if (ai){
278                         SalAuthInfo sai;
279                         sai.username=ai->username;
280                         sai.userid=ai->userid;
281                         sai.realm=ai->realm;
282                         sai.password=ai->passwd;
283                         sal_op_authenticate(op,&sai);
284                         ai->usecount++;
285                 }
286         }
287         ms_list_free(l);
288         write_auth_infos(lc);
289 }
290
291
292 /**
293  * This method is used to abort a user authentication request initiated by LinphoneCore
294  * from the auth_info_requested callback of LinphoneCoreVTable.
295 **/
296 void linphone_core_abort_authentication(LinphoneCore *lc,  LinphoneAuthInfo *info){
297 }
298
299 /**
300  * Removes an authentication information object.
301 **/
302 void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
303         LinphoneAuthInfo *r;
304         r=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
305         if (r){
306                 lc->auth_info=ms_list_remove(lc->auth_info,r);
307                 /*printf("len=%i newlen=%i\n",len,newlen);*/
308                 linphone_auth_info_destroy(r);
309                 write_auth_infos(lc);
310         }
311 }
312
313 /**
314  * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
315 **/
316 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
317         return lc->auth_info;
318 }
319
320 /**
321  * Clear all authentication information.
322 **/
323 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
324         MSList *elem;
325         int i;
326         for(i=0,elem=lc->auth_info;elem!=NULL;elem=ms_list_next(elem),i++){
327                 LinphoneAuthInfo *info=(LinphoneAuthInfo*)elem->data;
328                 linphone_auth_info_destroy(info);
329                 linphone_auth_info_write_config(lc->config,NULL,i);
330         }
331         ms_list_free(lc->auth_info);
332         lc->auth_info=NULL;
333 }
334
335 /**
336  * @}
337 **/