Fix correct authentication behavior for artificial endpoint.
[asterisk/asterisk.git] / res / res_pjsip_authenticator_digest.c
index cc312b1..e0f633f 100644 (file)
@@ -41,7 +41,9 @@ AO2_GLOBAL_OBJ_STATIC(entity_id);
  */
 static int digest_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
 {
-       return endpoint->inbound_auths.num > 0;
+       RAII_VAR(struct ast_sip_endpoint *, artificial, ast_sip_get_artificial_endpoint(), ao2_cleanup);
+
+       return endpoint == artificial || AST_VECTOR_SIZE(&endpoint->inbound_auths) > 0;
 }
 
 static void auth_store_cleanup(void *data)
@@ -290,6 +292,8 @@ enum digest_verify_result {
        AUTH_SUCCESS,
        /*! Authentication credentials correct but nonce mismatch */
        AUTH_STALE,
+       /*! Authentication credentials were not provided */
+       AUTH_NOAUTH,
 };
 
 /*!
@@ -330,6 +334,11 @@ static int verify(struct ast_sip_auth *auth, pjsip_rx_data *rdata, pj_pool_t *po
                        return AUTH_SUCCESS;
                }
        }
+
+       if (authed == PJSIP_EAUTHNOAUTH) {
+               return AUTH_NOAUTH;
+       }
+
        return AUTH_FAIL;
 }
 
@@ -376,12 +385,16 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint
        enum digest_verify_result *verify_res;
        enum ast_sip_check_auth_result res;
        int i;
+       int failures = 0;
+       size_t auth_size;
 
        RAII_VAR(struct ast_sip_endpoint *, artificial_endpoint,
                 ast_sip_get_artificial_endpoint(), ao2_cleanup);
 
-       auths = ast_alloca(endpoint->inbound_auths.num * sizeof(*auths));
-       verify_res = ast_alloca(endpoint->inbound_auths.num * sizeof(*verify_res));
+       auth_size = AST_VECTOR_SIZE(&endpoint->inbound_auths);
+
+       auths = ast_alloca(auth_size * sizeof(*auths));
+       verify_res = ast_alloca(auth_size * sizeof(*verify_res));
 
        if (!auths) {
                return AST_SIP_AUTHENTICATION_ERROR;
@@ -394,7 +407,7 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint
                goto cleanup;
        }
 
-       for (i = 0; i < endpoint->inbound_auths.num; ++i) {
+       for (i = 0; i < auth_size; ++i) {
                if (ast_strlen_zero(auths[i]->realm)) {
                        ast_string_field_set(auths[i], realm, "asterisk");
                }
@@ -403,16 +416,23 @@ static enum ast_sip_check_auth_result digest_check_auth(struct ast_sip_endpoint
                        res = AST_SIP_AUTHENTICATION_SUCCESS;
                        goto cleanup;
                }
+               if (verify_res[i] == AUTH_FAIL) {
+                       failures++;
+               }
        }
 
-       for (i = 0; i < endpoint->inbound_auths.num; ++i) {
+       for (i = 0; i < auth_size; ++i) {
                challenge(auths[i]->realm, tdata, rdata, verify_res[i] == AUTH_STALE);
        }
 
-       res = AST_SIP_AUTHENTICATION_CHALLENGE;
+       if (failures == auth_size) {
+               res = AST_SIP_AUTHENTICATION_FAILED;
+       } else {
+               res = AST_SIP_AUTHENTICATION_CHALLENGE;
+       }
 
 cleanup:
-       ast_sip_cleanup_auths(auths, endpoint->inbound_auths.num);
+       ast_sip_cleanup_auths(auths, auth_size);
        return res;
 }