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);
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);
68 const char *linphone_auth_info_get_username(const LinphoneAuthInfo *i){
75 const char *linphone_auth_info_get_passwd(const LinphoneAuthInfo *i){
79 const char *linphone_auth_info_get_userid(const LinphoneAuthInfo *i){
83 const char *linphone_auth_info_get_realm(const LinphoneAuthInfo *i){
86 const char *linphone_auth_info_get_ha1(const LinphoneAuthInfo *i){
93 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
94 if (info->passwd!=NULL) {
95 ms_free(info->passwd);
98 if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
104 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
106 ms_free(info->username);
109 if (username && strlen(username)>0) info->username=ms_strdup(username);
115 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
117 ms_free(info->userid);
120 if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
126 void linphone_auth_info_set_realm(LinphoneAuthInfo *info, const char *realm){
128 ms_free(info->realm);
131 if (realm && strlen(realm)>0) info->realm=ms_strdup(realm);
136 void linphone_auth_info_set_ha1(LinphoneAuthInfo *info, const char *ha1){
141 if (ha1 && strlen(ha1)>0) info->ha1=ms_strdup(ha1);
145 * Destroys a LinphoneAuthInfo object.
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);
156 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
159 sprintf(key,"auth_info_%i",pos);
160 lp_config_clean_section(config,key);
162 if (obj==NULL || lp_config_get_int(config, "sip", "store_auth_info", 1) == 0){
165 if (obj->username!=NULL){
166 lp_config_set_string(config,key,"username",obj->username);
168 if (obj->userid!=NULL){
169 lp_config_set_string(config,key,"userid",obj->userid);
171 if (obj->passwd!=NULL){
172 lp_config_set_string(config,key,"passwd",obj->passwd);
175 lp_config_set_string(config,key,"ha1",obj->ha1);
177 if (obj->realm!=NULL){
178 lp_config_set_string(config,key,"realm",obj->realm);
182 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
185 const char *username,*userid,*passwd,*ha1,*realm;
187 sprintf(key,"auth_info_%i",pos);
188 if (!lp_config_has_section(config,key)){
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);
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;
207 static char * remove_quotes(char * input){
209 if (*input=='"') input++;
210 tmp=strchr(input,'"');
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;
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;
234 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
236 const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
239 LinphoneAuthInfo *ret=NULL,*candidate=NULL;
240 for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
241 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
243 /*return the authinfo for any realm provided that there is only one for that username*/
244 if (key_match(pinfo->username,username)){
246 ms_warning("There are several auth info for username '%s'",username);
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))
258 if (key_match(pinfo->username,username))
263 if (ret==NULL && candidate!=NULL)
268 static void write_auth_infos(LinphoneCore *lc){
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);
277 linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
281 * Adds authentication information to the LinphoneCore.
283 * This information will be used during all SIP transacations that require authentication.
285 void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info)
287 LinphoneAuthInfo *ai;
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);
294 lc->auth_info=ms_list_remove(lc->auth_info,ai);
295 linphone_auth_info_destroy(ai);
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);
307 sai.username=ai->username;
308 sai.userid=ai->userid;
310 sai.password=ai->passwd;
311 sal_op_authenticate(op,&sai);
316 write_auth_infos(lc);
321 * This method is used to abort a user authentication request initiated by LinphoneCore
322 * from the auth_info_requested callback of LinphoneCoreVTable.
324 void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info){
328 * Removes an authentication information object.
330 void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
332 r=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
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);
342 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
344 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
345 return lc->auth_info;
349 * Clear all authentication information.
351 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
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);
359 ms_list_free(lc->auth_info);