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