1 /***************************************************************************
4 * Fri Jul 16 12:08:34 2004
5 * Copyright 2004-2009 Simon MORLAT
6 * simon.morlat@linphone.org
7 ****************************************************************************/
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.
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.
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.
25 #include "linphonecore.h"
30 * @addtogroup authentication
35 * Create a LinphoneAuthInfo object with supplied information.
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.
40 LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *userid,
41 const char *passwd, const char *ha1,const char *realm)
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);
56 const char *linphone_auth_info_get_username(const LinphoneAuthInfo *i){
63 const char *linphone_auth_info_get_passwd(const LinphoneAuthInfo *i){
67 const char *linphone_auth_info_get_userid(const LinphoneAuthInfo *i){
74 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
75 if (info->passwd!=NULL) {
76 ms_free(info->passwd);
79 if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
85 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
87 ms_free(info->username);
90 if (username && strlen(username)>0) info->username=ms_strdup(username);
96 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
98 ms_free(info->userid);
101 if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
105 * Destroys a LinphoneAuthInfo object.
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);
116 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
119 sprintf(key,"auth_info_%i",pos);
120 lp_config_clean_section(config,key);
125 if (obj->username!=NULL){
126 lp_config_set_string(config,key,"username",obj->username);
128 if (obj->userid!=NULL){
129 lp_config_set_string(config,key,"userid",obj->userid);
131 if (obj->passwd!=NULL){
132 lp_config_set_string(config,key,"passwd",obj->passwd);
135 lp_config_set_string(config,key,"ha1",obj->ha1);
137 if (obj->realm!=NULL){
138 lp_config_set_string(config,key,"realm",obj->realm);
142 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
145 const char *username,*userid,*passwd,*ha1,*realm;
147 sprintf(key,"auth_info_%i",pos);
148 if (!lp_config_has_section(config,key)){
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);
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;
167 static char * remove_quotes(char * input){
169 if (*input=='"') input++;
170 tmp=strchr(input,'"');
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;
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;
194 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
196 LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
199 LinphoneAuthInfo *ret=NULL,*candidate=NULL;
200 for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
201 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
203 /*return the authinfo for any realm provided that there is only one for that username*/
204 if (key_match(pinfo->username,username)){
206 ms_warning("There are several auth info for username '%s'",username);
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))
218 if (key_match(pinfo->username,username))
223 if (ret==NULL && candidate!=NULL)
229 * Adds authentication information to the LinphoneCore.
231 * This information will be used during all SIP transacations that require authentication.
233 void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
236 LinphoneAuthInfo *ai;
238 /* find if we are attempting to modify an existing auth info */
239 ai=linphone_core_find_auth_info(lc,info->realm,info->username);
241 elem=ms_list_find(lc->auth_info,ai);
243 ms_error("AuthInfo list corruption ?");
246 linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
247 elem->data=(void *)info;
249 lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
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);
260 sai.username=ai->username;
261 sai.userid=ai->userid;
263 sai.password=ai->passwd;
264 sal_op_authenticate(op,&sai);
272 * This method is used to abort a user authentication request initiated by LinphoneCore
273 * from the auth_info_requested callback of LinphoneCoreVTable.
275 void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info){
279 * Removes an authentication information object.
281 void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
282 int len=ms_list_size(lc->auth_info);
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);
291 linphone_auth_info_write_config(lc->config,NULL,i);
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);
299 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
301 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
302 return lc->auth_info;
306 * Clear all authentication information.
308 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
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);
316 ms_list_free(lc->auth_info);