Ignore:
Timestamp:
Nov 8, 2007 9:24:30 AM (17 years ago)
Author:
bennylp
Message:
  • Added option to send empty Authorization header in outgoing requests
  • When UAS has sent answer in reliable 1xx, do not put SDP in 2xx
  • Handle the case when UPDATE is challenged with 401/407
  • Obsolete --service-route option in pjsua
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/src/pjsip/sip_auth_client.c

    r1539 r1561  
    533533 
    534534 
     535/* 
     536 * Set the preference for the client authentication session. 
     537 */ 
     538PJ_DEF(pj_status_t) pjsip_auth_clt_set_prefs(pjsip_auth_clt_sess *sess, 
     539                                             const pjsip_auth_clt_pref *p) 
     540{ 
     541    PJ_ASSERT_RETURN(sess && p, PJ_EINVAL); 
     542 
     543    pj_memcpy(&sess->pref, p, sizeof(*p)); 
     544    pj_strdup(sess->pool, &sess->pref.algorithm, &p->algorithm); 
     545    //if (sess->pref.algorithm.slen == 0) 
     546    //  sess->pref.algorithm = pj_str("md5"); 
     547 
     548    return PJ_SUCCESS; 
     549} 
     550 
     551 
     552/* 
     553 * Get the preference for the client authentication session. 
     554 */ 
     555PJ_DEF(pj_status_t) pjsip_auth_clt_get_prefs(pjsip_auth_clt_sess *sess, 
     556                                             pjsip_auth_clt_pref *p) 
     557{ 
     558    PJ_ASSERT_RETURN(sess && p, PJ_EINVAL); 
     559 
     560    pj_memcpy(p, &sess->pref, sizeof(pjsip_auth_clt_pref)); 
     561    return PJ_SUCCESS; 
     562} 
     563 
     564 
    535565/*  
    536566 * Create Authorization/Proxy-Authorization response header based on the challege 
     
    699729 
    700730 
     731/* Find credential in list of (Proxy-)Authorization headers */ 
     732static pjsip_authorization_hdr* get_header_for_realm(const pjsip_hdr *hdr_list, 
     733                                                     const pj_str_t *realm) 
     734{ 
     735    pjsip_authorization_hdr *h; 
     736 
     737    h = (pjsip_authorization_hdr*)hdr_list->next; 
     738    while (h != (pjsip_authorization_hdr*)hdr_list) { 
     739        if (pj_stricmp(&h->credential.digest.realm, realm)==0) 
     740            return h; 
     741        h = h->next; 
     742    } 
     743 
     744    return NULL; 
     745} 
     746 
    701747 
    702748/* Initialize outgoing request. */ 
     
    706752    const pjsip_method *method; 
    707753    pjsip_cached_auth *auth; 
     754    pjsip_hdr added; 
    708755 
    709756    PJ_ASSERT_RETURN(sess && tdata, PJ_EINVAL); 
     
    711758    PJ_ASSERT_RETURN(tdata->msg->type==PJSIP_REQUEST_MSG, 
    712759                     PJSIP_ENOTREQUESTMSG); 
     760 
     761    /* Init list */ 
     762    pj_list_init(&added); 
    713763 
    714764    /* Get the method. */ 
     
    729779                        pjsip_authorization_hdr *hauth; 
    730780                        hauth = pjsip_hdr_shallow_clone(tdata->pool, entry->hdr); 
    731                         pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hauth); 
     781                        //pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hauth); 
     782                        pj_list_push_back(&added, hauth); 
    732783                        break; 
    733784                    } 
     
    777828                return status; 
    778829             
    779             pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hauth); 
     830            //pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hauth); 
     831            pj_list_push_back(&added, hauth); 
    780832        } 
    781833#       endif   /* PJSIP_AUTH_QOP_SUPPORT && PJSIP_AUTH_AUTO_SEND_NEXT */ 
    782834 
    783835        auth = auth->next; 
     836    } 
     837 
     838    if (sess->pref.initial_auth == PJ_FALSE) { 
     839        pjsip_hdr *h; 
     840 
     841        /* Don't want to send initial empty Authorization header, so 
     842         * just send whatever available in the list (maybe empty). 
     843         */ 
     844 
     845        h = added.next; 
     846        while (h != &added) { 
     847            pjsip_hdr *next = h->next; 
     848            pjsip_msg_add_hdr(tdata->msg, h); 
     849            h = next; 
     850        } 
     851    } else { 
     852        /* For each realm, add either the cached authorization header 
     853         * or add an empty authorization header. 
     854         */ 
     855        unsigned i; 
     856        char *uri_str; 
     857        int len; 
     858 
     859        uri_str = pj_pool_alloc(tdata->pool, PJSIP_MAX_URL_SIZE); 
     860        len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, tdata->msg->line.req.uri, 
     861                              uri_str, PJSIP_MAX_URL_SIZE); 
     862        if (len < 1 || len >= PJSIP_MAX_URL_SIZE) 
     863            return PJSIP_EURITOOLONG; 
     864 
     865        for (i=0; i<sess->cred_cnt; ++i) { 
     866            pjsip_cred_info *c = &sess->cred_info[i]; 
     867            pjsip_authorization_hdr *h; 
     868 
     869            h = get_header_for_realm(&added, &c->realm); 
     870            if (h) { 
     871                pj_list_erase(h); 
     872                pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)h); 
     873            } else { 
     874                enum { HDRLEN = 256 }; 
     875                const pj_str_t hname = pj_str("Authorization"); 
     876                pj_str_t hval; 
     877                pjsip_generic_string_hdr *hs; 
     878                char *hdr; 
     879 
     880                hdr = pj_pool_alloc(tdata->pool, HDRLEN); 
     881                len = pj_ansi_snprintf( 
     882                    hdr, HDRLEN, 
     883                    "%.*s username=\"%.*s\", realm=\"%.*s\"," 
     884                    " nonce=\"\", uri=\"%s\",%s%.*s%s response=\"\"", 
     885                    (int)c->scheme.slen, c->scheme.ptr, 
     886                    (int)c->username.slen, c->username.ptr, 
     887                    (int)c->realm.slen, c->realm.ptr, 
     888                    uri_str, 
     889                    (sess->pref.algorithm.slen ? " algorithm=" : ""), 
     890                    (int)sess->pref.algorithm.slen, sess->pref.algorithm.ptr, 
     891                    (sess->pref.algorithm.slen ? "," : "")); 
     892 
     893                PJ_ASSERT_RETURN(len>0 && len<HDRLEN, PJ_ETOOBIG); 
     894 
     895                hval.ptr = hdr; 
     896                hval.slen = len; 
     897                hs = pjsip_generic_string_hdr_create(tdata->pool, &hname,  
     898                                                     &hval); 
     899                pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)hs); 
     900            } 
     901        } 
    784902    } 
    785903 
Note: See TracChangeset for help on using the changeset viewer.