/* * Create and send a challenge */ static inline int challenge(struct sip_msg* _msg, str* _realm, int _qop, int _code, char* _message, char* _challenge_msg) { int auth_hf_len; struct hdr_field* h; auth_body_t* cred = 0; char *auth_hf; int ret, hftype = 0; /* Makes gcc happy */ struct sip_uri uri; switch(_code) { case 401: get_authorized_cred(_msg->authorization, &h); hftype = HDR_AUTHORIZATION; break; case 407: get_authorized_cred(_msg->proxy_auth, &h); hftype = HDR_PROXYAUTH; break; } if (h) cred = (auth_body_t*)(h->parsed); if (_realm->len == 0) { if (get_realm(_msg, hftype, &uri) < 0) { LOG(L_ERR, "challenge(): Error while extracting URI\n"); if (send_resp(_msg, 400, MESSAGE_400, 0, 0) == -1) { LOG(L_ERR, "challenge(): Error while sending response\n"); return -1; } return 0; } _realm = &uri.host; strip_realm(_realm); } auth_hf = build_auth_hf(0, (cred ? cred->stale : 0), _realm, &auth_hf_len, _qop, _challenge_msg); if (!auth_hf) { LOG(L_ERR, "ERROR: challenge: no mem w/cred\n"); return -1; } ret = send_resp(_msg, _code, _message, auth_hf, auth_hf_len); if (auth_hf) pkg_free(auth_hf); if (ret == -1) { LOG(L_ERR, "challenge(): Error while sending response\n"); return -1; } return 0; }
/* * Create and send a challenge */ static inline int challenge(struct sip_msg* _msg, gparam_p _realm, int _qop, int _code, char* _message, char* _challenge_msg) { int auth_hf_len; struct hdr_field* h; auth_body_t* cred = 0; char *auth_hf; int ret; hdr_types_t hftype = 0; /* Makes gcc happy */ struct sip_uri *uri; str realm; str reason; switch(_code) { case 401: get_authorized_cred(_msg->authorization, &h); hftype = HDR_AUTHORIZATION_T; break; case 407: get_authorized_cred(_msg->proxy_auth, &h); hftype = HDR_PROXYAUTH_T; break; } if (h) cred = (auth_body_t*)(h->parsed); if(fixup_get_svalue(_msg, _realm, &realm)!=0) { LM_ERR("invalid realm parameter"); if (send_resp(_msg, 500, &auth_500_err, 0, 0)==-1) return -1; else return 0; } if (realm.len == 0) { if (get_realm(_msg, hftype, &uri) < 0) { LM_ERR("failed to extract URI\n"); if (send_resp(_msg, 400, &auth_400_err, 0, 0) == -1) { LM_ERR("failed to send the response\n"); return -1; } return 0; } realm = uri->host; strip_realm(&realm); } auth_hf = build_auth_hf(0, (cred ? cred->stale : 0), &realm, &auth_hf_len, _qop, _challenge_msg); if (!auth_hf) { LM_ERR("failed to generate nonce\n"); return -1; } reason.s = _message; reason.len = strlen(_message); ret = send_resp(_msg, _code, &reason, auth_hf, auth_hf_len); if (auth_hf) pkg_free(auth_hf); if (ret == -1) { LM_ERR("failed to send the response\n"); return -1; } return 0; }