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){
86 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
87 if (info->passwd!=NULL) {
88 ms_free(info->passwd);
91 if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
97 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
99 ms_free(info->username);
102 if (username && strlen(username)>0) info->username=ms_strdup(username);
108 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
110 ms_free(info->userid);
113 if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
117 * Destroys a LinphoneAuthInfo object.
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);
128 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
131 sprintf(key,"auth_info_%i",pos);
132 lp_config_clean_section(config,key);
134 if (obj==NULL || lp_config_get_int(config, "sip", "store_auth_info", 1) == 0){
137 if (obj->username!=NULL){
138 lp_config_set_string(config,key,"username",obj->username);
140 if (obj->userid!=NULL){
141 lp_config_set_string(config,key,"userid",obj->userid);
143 if (obj->passwd!=NULL){
144 lp_config_set_string(config,key,"passwd",obj->passwd);
147 lp_config_set_string(config,key,"ha1",obj->ha1);
149 if (obj->realm!=NULL){
150 lp_config_set_string(config,key,"realm",obj->realm);
154 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
157 const char *username,*userid,*passwd,*ha1,*realm;
159 sprintf(key,"auth_info_%i",pos);
160 if (!lp_config_has_section(config,key)){
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);
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;
179 static char * remove_quotes(char * input){
181 if (*input=='"') input++;
182 tmp=strchr(input,'"');
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;
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;
206 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
208 const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
211 LinphoneAuthInfo *ret=NULL,*candidate=NULL;
212 for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
213 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
215 /*return the authinfo for any realm provided that there is only one for that username*/
216 if (key_match(pinfo->username,username)){
218 ms_warning("There are several auth info for username '%s'",username);
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))
230 if (key_match(pinfo->username,username))
235 if (ret==NULL && candidate!=NULL)
240 static void write_auth_infos(LinphoneCore *lc){
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);
249 linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
253 * Adds authentication information to the LinphoneCore.
255 * This information will be used during all SIP transacations that require authentication.
257 void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info)
259 LinphoneAuthInfo *ai;
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);
266 lc->auth_info=ms_list_remove(lc->auth_info,ai);
267 linphone_auth_info_destroy(ai);
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);
279 sai.username=ai->username;
280 sai.userid=ai->userid;
282 sai.password=ai->passwd;
283 sal_op_authenticate(op,&sai);
288 write_auth_infos(lc);
293 * This method is used to abort a user authentication request initiated by LinphoneCore
294 * from the auth_info_requested callback of LinphoneCoreVTable.
296 void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info){
300 * Removes an authentication information object.
302 void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
304 r=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
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);
314 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
316 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
317 return lc->auth_info;
321 * Clear all authentication information.
323 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
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);
331 ms_list_free(lc->auth_info);