5996d919bc54924fa394c84bea10004268959a52
[asterisk/asterisk.git] / res / res_pjsip / pjsip_outbound_auth.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 #include "asterisk.h"
20 #undef bzero
21 #define bzero bzero
22 #include "pjsip.h"
23
24 #include "asterisk/res_pjsip.h"
25 #include "asterisk/module.h"
26 #include "include/res_pjsip_private.h"
27
28 static pj_bool_t outbound_auth(pjsip_rx_data *rdata);
29
30 static pjsip_module outbound_auth_mod = {
31         .name = {"Outbound Authentication", 19},
32         .priority = PJSIP_MOD_PRIORITY_DIALOG_USAGE,
33         .on_rx_response = outbound_auth,
34 };
35
36 struct outbound_auth_cb_data {
37         ast_sip_dialog_outbound_auth_cb cb;
38         void *user_data;
39 };
40
41 static pj_bool_t outbound_auth(pjsip_rx_data *rdata)
42 {
43         RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
44         pjsip_transaction *tsx;
45         pjsip_dialog *dlg;
46         struct outbound_auth_cb_data *cb_data;
47         pjsip_tx_data *tdata;
48
49         if (rdata->msg_info.msg->line.status.code != 401 &&
50                         rdata->msg_info.msg->line.status.code != 407) {
51                 /* Doesn't pertain to us. Move on */
52                 return PJ_FALSE;
53         }
54
55         tsx = pjsip_rdata_get_tsx(rdata);
56         dlg = pjsip_rdata_get_dlg(rdata);
57         ast_assert(dlg != NULL && tsx != NULL);
58         endpoint = ast_sip_dialog_get_endpoint(dlg);
59
60         if (!endpoint) {
61                 return PJ_FALSE;
62         }
63
64         if (ast_sip_create_request_with_auth(&endpoint->outbound_auths, rdata, tsx, &tdata)) {
65                 return PJ_FALSE;
66         }
67
68         cb_data = dlg->mod_data[outbound_auth_mod.id];
69         if (cb_data) {
70                 cb_data->cb(dlg, tdata, cb_data->user_data);
71                 return PJ_TRUE;
72         }
73
74         pjsip_dlg_send_request(dlg, tdata, -1, NULL);
75         return PJ_TRUE;
76 }
77
78 int ast_sip_dialog_setup_outbound_authentication(pjsip_dialog *dlg, const struct ast_sip_endpoint *endpoint,
79                 ast_sip_dialog_outbound_auth_cb cb, void *user_data)
80 {
81         struct outbound_auth_cb_data *cb_data = PJ_POOL_ZALLOC_T(dlg->pool, struct outbound_auth_cb_data);
82         cb_data->cb = cb;
83         cb_data->user_data = user_data;
84
85         dlg->sess_count++;
86         pjsip_dlg_add_usage(dlg, &outbound_auth_mod, cb_data);
87         dlg->sess_count--;
88
89         return 0;
90 }
91
92 int ast_sip_initialize_outbound_authentication(void) {
93         return ast_sip_register_service(&outbound_auth_mod);
94 }