| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "asterisk.h" |
| 27 |
|
| 28 |
#include <pjsip.h> |
| 29 |
#include <pjsip_ua.h> |
| 30 |
|
| 31 |
#include "asterisk/res_pjsip.h" |
| 32 |
#include "asterisk/res_pjsip_session.h" |
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/causes.h" |
| 35 |
|
| 36 |
|
| 37 |
struct rfc3329_store_data { |
| 38 |
int last_rx_status_code; |
| 39 |
}; |
| 40 |
|
| 41 |
static void datastore_destroy_cb(void *data) |
| 42 |
{ |
| 43 |
struct rfc3329_store_data *d = data; |
| 44 |
if (d) { |
| 45 |
ast_free(d); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
static const struct ast_datastore_info rfc3329_store_datastore = { |
| 51 |
.type = "rfc3329_store", |
| 52 |
.destroy = datastore_destroy_cb |
| 53 |
}; |
| 54 |
|
| 55 |
static void rfc3329_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata) |
| 56 |
{ |
| 57 |
RAII_VAR(struct ast_datastore *, datastore, ast_sip_session_get_datastore(session, "rfc3329_store"), ao2_cleanup); |
| 58 |
static const pj_str_t str_security_server = { "Security-Server", 15 }; |
| 59 |
struct ast_sip_contact_status *contact_status = NULL; |
| 60 |
struct ast_sip_security_mechanism *mech; |
| 61 |
struct rfc3329_store_data *store_data; |
| 62 |
pjsip_generic_string_hdr *header; |
| 63 |
char buf[128]; |
| 64 |
char *hdr_val; |
| 65 |
char *mechanism; |
| 66 |
|
| 67 |
if (!session || !session->endpoint || !session->endpoint->security_negotiation |
| 68 |
|| !session->contact || !(contact_status = ast_sip_get_contact_status(session->contact)) |
| 69 |
|| !session->inv_session->dlg) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
ao2_lock(contact_status); |
| 74 |
if (AST_VECTOR_SIZE(&contact_status->security_mechanisms)) { |
| 75 |
goto out; |
| 76 |
} |
| 77 |
|
| 78 |
if (!datastore |
| 79 |
&& (datastore = ast_sip_session_alloc_datastore(&rfc3329_store_datastore, "rfc3329_store")) |
| 80 |
&& (store_data = ast_calloc(1, sizeof(struct rfc3329_store_data)))) { |
| 81 |
|
| 82 |
store_data->last_rx_status_code = rdata->msg_info.msg->line.status.code; |
| 83 |
datastore->data = store_data; |
| 84 |
ast_sip_session_add_datastore(session, datastore); |
| 85 |
} else { |
| 86 |
ast_log(AST_LOG_WARNING, "Could not store session data. Still attempting requests, but they might be missing necessary headers.\n"); |
| 87 |
} |
| 88 |
|
| 89 |
header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_security_server, NULL); |
| 90 |
for (; header; |
| 91 |
header = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_security_server, header->next)) { |
| 92 |
|
| 93 |
ast_copy_pj_str(buf, &header->hvalue, sizeof(buf)); |
| 94 |
hdr_val = ast_skip_blanks(buf); |
| 95 |
|
| 96 |
while ((mechanism = ast_strsep(&hdr_val, ',', AST_STRSEP_ALL))) { |
| 97 |
if (!ast_sip_str_to_security_mechanism(&mech, mechanism)) { |
| 98 |
AST_VECTOR_APPEND(&contact_status->security_mechanisms, mech); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
out: |
| 104 |
ao2_unlock(contact_status); |
| 105 |
ao2_cleanup(contact_status); |
| 106 |
} |
| 107 |
|
| 108 |
static void add_outgoing_request_headers(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, struct pjsip_tx_data *tdata, |
| 109 |
struct ast_datastore *datastore) |
| 110 |
{ |
| 111 |
static const pj_str_t security_verify = { "Security-Verify", 15 }; |
| 112 |
struct pjsip_generic_string_hdr *hdr = NULL; |
| 113 |
struct ast_sip_contact_status *contact_status = NULL; |
| 114 |
struct rfc3329_store_data *store_data; |
| 115 |
|
| 116 |
if (endpoint->security_negotiation != AST_SIP_SECURITY_NEG_MEDIASEC) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
contact_status = ast_sip_get_contact_status(contact); |
| 121 |
hdr = pjsip_msg_find_hdr_by_name(tdata->msg, &security_verify, NULL); |
| 122 |
|
| 123 |
if (contact_status == NULL) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
ao2_lock(contact_status); |
| 128 |
if (AST_VECTOR_SIZE(&contact_status->security_mechanisms) && hdr == NULL) { |
| 129 |
|
| 130 |
ast_sip_add_security_headers(&contact_status->security_mechanisms, "Security-Verify", 0, tdata); |
| 131 |
} |
| 132 |
if (datastore) { |
| 133 |
store_data = datastore->data; |
| 134 |
if (store_data->last_rx_status_code == 401) { |
| 135 |
|
| 136 |
ast_sip_add_security_headers(&endpoint->security_mechanisms, "Security-Client", 0, tdata); |
| 137 |
} |
| 138 |
} |
| 139 |
ao2_unlock(contact_status); |
| 140 |
|
| 141 |
ao2_cleanup(contact_status); |
| 142 |
} |
| 143 |
|
| 144 |
static void rfc3329_outgoing_request(struct ast_sip_session *session, struct pjsip_tx_data *tdata) |
| 145 |
{ |
| 146 |
RAII_VAR(struct ast_datastore *, datastore, ast_sip_session_get_datastore(session, "rfc3329_store"), ao2_cleanup); |
| 147 |
if (session->contact == NULL) { |
| 148 |
return; |
| 149 |
} |
| 150 |
add_outgoing_request_headers(session->endpoint, session->contact, tdata, datastore); |
| 151 |
} |
| 152 |
|
| 153 |
static struct ast_sip_session_supplement rfc3329_supplement = { |
| 154 |
.incoming_response = rfc3329_incoming_response, |
| 155 |
.outgoing_request = rfc3329_outgoing_request, |
| 156 |
}; |
| 157 |
|
| 158 |
static void rfc3329_options_request(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, struct pjsip_tx_data *tdata) |
| 159 |
{ |
| 160 |
add_outgoing_request_headers(endpoint, contact, tdata, NULL); |
| 161 |
} |
| 162 |
|
| 163 |
static struct ast_sip_supplement rfc3329_options_supplement = { |
| 164 |
.method = "OPTIONS", |
| 165 |
.outgoing_request = rfc3329_options_request, |
| 166 |
}; |
| 167 |
|
| 168 |
static int load_module(void) |
| 169 |
{ |
| 170 |
ast_sip_session_register_supplement(&rfc3329_supplement); |
| 171 |
ast_sip_register_supplement(&rfc3329_options_supplement); |
| 172 |
return AST_MODULE_LOAD_SUCCESS; |
| 173 |
} |
| 174 |
|
| 175 |
static int unload_module(void) |
| 176 |
{ |
| 177 |
ast_sip_session_unregister_supplement(&rfc3329_supplement); |
| 178 |
ast_sip_unregister_supplement(&rfc3329_options_supplement); |
| 179 |
return 0; |
| 180 |
} |
| 181 |
|
| 182 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP RFC3329 Support (partial)", |
| 183 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 184 |
.load = load_module, |
| 185 |
.unload = unload_module, |
| 186 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 187 |
.requires = "res_pjsip,res_pjsip_session", |
| 188 |
); |