]> sjero.net Git - linphone/blobdiff - coreapi/authentication.c
Aac-eld add missing header according to RFC3640 3.3.6
[linphone] / coreapi / authentication.c
index fc27142db18a06a799782e9c6fd17d1a3968a625..8ab1c21ffdb406ecba2ebc1f8e82f5a4677dc687 100644 (file)
@@ -50,6 +50,43 @@ LinphoneAuthInfo *linphone_auth_info_new(const char *username, const char *useri
        return obj;
 }
 
+static LinphoneAuthInfo *linphone_auth_info_clone(const LinphoneAuthInfo *ai){
+       LinphoneAuthInfo *obj=ms_new0(LinphoneAuthInfo,1);
+       if (ai->username) obj->username=ms_strdup(ai->username);
+       if (ai->userid) obj->userid=ms_strdup(ai->userid);
+       if (ai->passwd) obj->passwd=ms_strdup(ai->passwd);
+       if (ai->ha1)    obj->ha1=ms_strdup(ai->ha1);
+       if (ai->realm)  obj->realm=ms_strdup(ai->realm);
+       obj->works=FALSE;
+       obj->usecount=0;
+       return obj;
+}
+
+/**
+ * Returns username.
+**/
+const char *linphone_auth_info_get_username(const LinphoneAuthInfo *i){
+       return i->username;
+}
+
+/**
+ * Returns password.
+**/
+const char *linphone_auth_info_get_passwd(const LinphoneAuthInfo *i){
+       return i->passwd;
+}
+
+const char *linphone_auth_info_get_userid(const LinphoneAuthInfo *i){
+       return i->userid;
+}
+
+const char *linphone_auth_info_get_realm(const LinphoneAuthInfo *i){
+       return i->realm;
+}
+const char *linphone_auth_info_get_ha1(const LinphoneAuthInfo *i){
+       return i->ha1;
+}
+
 /**
  * Sets the password.
 **/
@@ -83,6 +120,27 @@ void linphone_auth_info_set_userid(LinphoneAuthInfo *info, const char *userid){
        if (userid && strlen(userid)>0) info->userid=ms_strdup(userid);
 }
 
+/**
+ * Sets realm.
+**/
+void linphone_auth_info_set_realm(LinphoneAuthInfo *info, const char *realm){
+       if (info->realm){
+               ms_free(info->realm);
+               info->realm=NULL;
+       }
+       if (realm && strlen(realm)>0) info->realm=ms_strdup(realm);
+}
+/**
+ * Sets ha1.
+**/
+void linphone_auth_info_set_ha1(LinphoneAuthInfo *info, const char *ha1){
+       if (info->ha1){
+               ms_free(info->ha1);
+               info->ha1=NULL;
+       }
+       if (ha1 && strlen(ha1)>0) info->ha1=ms_strdup(ha1);
+}
+
 /**
  * Destroys a LinphoneAuthInfo object.
 **/
@@ -101,7 +159,7 @@ void linphone_auth_info_write_config(LpConfig *config, LinphoneAuthInfo *obj, in
        sprintf(key,"auth_info_%i",pos);
        lp_config_clean_section(config,key);
        
-       if (obj==NULL){
+       if (obj==NULL || lp_config_get_int(config, "sip", "store_auth_info", 1) == 0){
                return;
        }               
        if (obj->username!=NULL){
@@ -175,7 +233,7 @@ static int realm_match(const char *realm1, const char *realm2){
 /**
  * Retrieves a LinphoneAuthInfo previously entered into the LinphoneCore.
 **/
-LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
+const LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *realm, const char *username)
 {
        MSList *elem;
        LinphoneAuthInfo *ret=NULL,*candidate=NULL;
@@ -207,36 +265,43 @@ LinphoneAuthInfo *linphone_core_find_auth_info(LinphoneCore *lc, const char *rea
        return ret;
 }
 
+static void write_auth_infos(LinphoneCore *lc){
+       MSList *elem;
+       int i;
+
+       if (!linphone_core_ready(lc)) return;
+       for(elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
+               LinphoneAuthInfo *ai=(LinphoneAuthInfo*)(elem->data);
+               linphone_auth_info_write_config(lc->config,ai,i);
+       }
+       linphone_auth_info_write_config(lc->config,NULL,i); /* mark the end */
+}
+
 /**
  * Adds authentication information to the LinphoneCore.
  * 
  * This information will be used during all SIP transacations that require authentication.
 **/
-void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
+void linphone_core_add_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info)
 {
-       MSList *elem;
        LinphoneAuthInfo *ai;
+       MSList *elem;
+       MSList *l;
        
        /* find if we are attempting to modify an existing auth info */
-       ai=linphone_core_find_auth_info(lc,info->realm,info->username);
+       ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
        if (ai!=NULL){
-               elem=ms_list_find(lc->auth_info,ai);
-               if (elem==NULL){
-                       ms_error("AuthInfo list corruption ?");
-                       return;
-               }
-               linphone_auth_info_destroy((LinphoneAuthInfo*)elem->data);
-               elem->data=(void *)info;
-       }else {
-               lc->auth_info=ms_list_append(lc->auth_info,(void *)info);
+               lc->auth_info=ms_list_remove(lc->auth_info,ai);
+               linphone_auth_info_destroy(ai);
        }
+       lc->auth_info=ms_list_append(lc->auth_info,linphone_auth_info_clone(info));
        /* retry pending authentication operations */
-       for(elem=sal_get_pending_auths(lc->sal);elem!=NULL;elem=elem->next){
+       for(l=elem=sal_get_pending_auths(lc->sal);elem!=NULL;elem=elem->next){
                const char *username,*realm;
                SalOp *op=(SalOp*)elem->data;
                LinphoneAuthInfo *ai;
                sal_op_get_auth_requested(op,&realm,&username);
-               ai=linphone_core_find_auth_info(lc,realm,username);
+               ai=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,realm,username);
                if (ai){
                        SalAuthInfo sai;
                        sai.username=ai->username;
@@ -247,6 +312,8 @@ void linphone_core_add_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info)
                        ai->usecount++;
                }
        }
+       ms_list_free(l);
+       write_auth_infos(lc);
 }
 
 
@@ -260,20 +327,14 @@ void linphone_core_abort_authentication(LinphoneCore *lc,  LinphoneAuthInfo *inf
 /**
  * Removes an authentication information object.
 **/
-void linphone_core_remove_auth_info(LinphoneCore *lc, LinphoneAuthInfo *info){
-       int len=ms_list_size(lc->auth_info);
-       int newlen;
-       int i;
-       MSList *elem;
-       lc->auth_info=ms_list_remove(lc->auth_info,info);
-       newlen=ms_list_size(lc->auth_info);
-       /*printf("len=%i newlen=%i\n",len,newlen);*/
-       linphone_auth_info_destroy(info);
-       for (i=0;i<len;i++){
-               linphone_auth_info_write_config(lc->config,NULL,i);
-       }
-       for (elem=lc->auth_info,i=0;elem!=NULL;elem=ms_list_next(elem),i++){
-               linphone_auth_info_write_config(lc->config,(LinphoneAuthInfo*)elem->data,i);
+void linphone_core_remove_auth_info(LinphoneCore *lc, const LinphoneAuthInfo *info){
+       LinphoneAuthInfo *r;
+       r=(LinphoneAuthInfo*)linphone_core_find_auth_info(lc,info->realm,info->username);
+       if (r){
+               lc->auth_info=ms_list_remove(lc->auth_info,r);
+               /*printf("len=%i newlen=%i\n",len,newlen);*/
+               linphone_auth_info_destroy(r);
+               write_auth_infos(lc);
        }
 }