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 void linphone_auth_info_set_passwd(LinphoneAuthInfo *info, const char *passwd){
57 if (info->passwd!=NULL) {
58 ms_free(info->passwd);
61 if (passwd!=NULL && (strlen(passwd)>0)) info->passwd=ms_strdup(passwd);
67 void linphone_auth_info_set_username(LinphoneAuthInfo *info, const char *username){
69 ms_free(info->username);
72 if (username && strlen(username)>0) info->username=ms_strdup(username);
78 void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
80 ms_free(info->userid);
83 if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
87 * Destroys a LinphoneAuthInfo object.
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);
98 void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, int pos)
101 sprintf(key,"auth_info_%i",pos);
102 lp_config_clean_section(config,key);
107 if (obj->username!=NULL){
108 lp_config_set_string(config,key,"username",obj->username);
110 if (obj->userid!=NULL){
111 lp_config_set_string(config,key,"userid",obj->userid);
113 if (obj->passwd!=NULL){
114 lp_config_set_string(config,key,"passwd",obj->passwd);
117 lp_config_set_string(config,key,"ha1",obj->ha1);
119 if (obj->realm!=NULL){
120 lp_config_set_string(config,key,"realm",obj->realm);
124 LinphoneAuthInfo *linphone_auth_info_new_from_config_file(LpConfig * config, int pos)
127 const char *username,*userid,*passwd,*ha1,*realm;
129 sprintf(key,"auth_info_%i",pos);
130 if (!lp_config_has_section(config,key)){
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);
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;
149 static char * remove_quotes(char * input){
151 if (*input=='"') input++;
152 tmp=strchr(input,'"');
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;
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;
176 * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
178 LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
181 LinphoneAuthInfo *ret=NULL,*candidate=NULL;
182 for (elem=lc->auth_info;elem!=NULL;elem=elem->next){
183 LinphoneAuthInfo *pinfo=(LinphoneAuthInfo*)elem->data;
185 /*return the authinfo for any realm provided that there is only one for that username*/
186 if (key_match(pinfo->username,username)){
188 ms_warning("There are several auth info for username '%s'",username);
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))
200 if (key_match(pinfo->username,username))
205 if (ret==NULL && candidate!=NULL)
211 * Adds authentication information to the LinphoneCore.
213 * This information will be used during all SIP transacations that require authentication.
215 void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
218 LinphoneAuthInfo *ai;
220 /* find if we are attempting to modify an existing auth info */
221 ai=linphone_core_find_auth_info(lc,info->realm,info->username);
223 elem=ms_list_find(lc->auth_info,ai);
225 ms_error("AuthInfo list corruption ?");
228 linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
229 elem->data=(void *)info;
231 lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
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);
242 sai.username=ai->username;
243 sai.userid=ai->userid;
245 sai.password=ai->passwd;
246 sal_op_authenticate(op,&sai);
254 * This method is used to abort a user authentication request initiated by LinphoneCore
255 * from the auth_info_requested callback of LinphoneCoreVTable.
257 void linphone_core_abort_authentication(LinphoneCore *lc, LinphoneAuthInfo *info){
261 * Removes an authentication information object.
263 void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
264 int len=ms_list_size(lc->auth_info);
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);
273 linphone_auth_info_write_config(lc->config,NULL,i);
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);
281 * Returns an unmodifiable list of currently entered LinphoneAuthInfo.
283 const MSList *linphone_core_get_auth_info_list(const LinphoneCore *lc){
284 return lc->auth_info;
288 * Clear all authentication information.
290 void linphone_core_clear_all_auth_info(LinphoneCore *lc){
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);
298 ms_list_free(lc->auth_info);