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