PJSIP XML, XPIDF: Fix buffer size overwrite memory corruption error.
[asterisk/asterisk.git] / res / res_pjsip_dialog_info_body_generator.c
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@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 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <depend>res_pjsip_pubsub</depend>
23         <depend>res_pjsip_exten_state</depend>
24         <support_level>core</support_level>
25  ***/
26
27 #include "asterisk.h"
28
29 #include <pjsip.h>
30 #include <pjsip_simple.h>
31 #include <pjlib.h>
32
33 #include "asterisk/module.h"
34 #include "asterisk/res_pjsip.h"
35 #include "asterisk/res_pjsip_pubsub.h"
36 #include "asterisk/res_pjsip_presence_xml.h"
37 #include "asterisk/res_pjsip_body_generator_types.h"
38
39 /*! \brief Structure which contains dialog-info+xml state information */
40 struct dialog_info_xml_state {
41         /*! \brief Version to place into the next NOTIFY */
42         unsigned int version;
43 };
44
45 /*! \brief Destructor for dialog-info+xml information */
46 static void dialog_info_xml_state_destroy(void *obj)
47 {
48         ast_free(obj);
49 }
50
51 /*! \brief Datastore for attaching dialog-info+xml state information */
52 static const struct ast_datastore_info dialog_info_xml_datastore = {
53         .type = "dialog-info+xml",
54         .destroy = dialog_info_xml_state_destroy,
55 };
56
57 static void *dialog_info_allocate_body(void *data)
58 {
59         struct ast_sip_exten_state_data *state_data = data;
60
61         return ast_sip_presence_xml_create_node(state_data->pool, NULL, "dialog-info");
62 }
63
64 static struct ast_datastore *dialog_info_xml_state_find_or_create(struct ast_sip_subscription *sub)
65 {
66         struct ast_datastore *datastore = ast_sip_subscription_get_datastore(sub, "dialog-info+xml");
67
68         if (datastore) {
69                 return datastore;
70         }
71
72         datastore = ast_sip_subscription_alloc_datastore(&dialog_info_xml_datastore, "dialog-info+xml");
73         if (!datastore) {
74                 return NULL;
75         }
76         datastore->data = ast_calloc(1, sizeof(struct dialog_info_xml_state));
77         if (!datastore->data || ast_sip_subscription_add_datastore(sub, datastore)) {
78                 ao2_ref(datastore, -1);
79                 return NULL;
80         }
81
82         return datastore;
83 }
84
85 static unsigned int dialog_info_xml_get_version(struct ast_sip_subscription *sub, unsigned int *version)
86 {
87         struct ast_datastore *datastore = dialog_info_xml_state_find_or_create(sub);
88         struct dialog_info_xml_state *state;
89
90         if (!datastore) {
91                 return -1;
92         }
93
94         state = datastore->data;
95         *version = state->version++;
96         ao2_ref(datastore, -1);
97
98         return 0;
99 }
100
101 static int dialog_info_generate_body_content(void *body, void *data)
102 {
103         pj_xml_node *dialog_info = body, *dialog, *state;
104         struct ast_sip_exten_state_data *state_data = data;
105         char *local = ast_strdupa(state_data->local), *stripped, *statestring = NULL;
106         char *pidfstate = NULL, *pidfnote = NULL;
107         enum ast_sip_pidf_state local_state;
108         unsigned int version;
109         char version_str[32], sanitized[PJSIP_MAX_URL_SIZE];
110
111         if (!local || !state_data->sub) {
112                 return -1;
113         }
114
115         if (dialog_info_xml_get_version(state_data->sub, &version)) {
116                 ast_log(LOG_WARNING, "dialog-info+xml version could not be retrieved from datastore\n");
117                 return -1;
118         }
119
120         stripped = ast_strip_quoted(local, "<", ">");
121         ast_sip_sanitize_xml(stripped, sanitized, sizeof(sanitized));
122
123         ast_sip_presence_exten_state_to_str(state_data->exten_state, &statestring,
124                         &pidfstate, &pidfnote, &local_state);
125
126         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "xmlns", "urn:ietf:params:xml:ns:dialog-info");
127
128         snprintf(version_str, sizeof(version_str), "%u", version);
129         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "version", version_str);
130
131         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "state", "full");
132         ast_sip_presence_xml_create_attr(state_data->pool, dialog_info, "entity", sanitized);
133
134         dialog = ast_sip_presence_xml_create_node(state_data->pool, dialog_info, "dialog");
135         ast_sip_presence_xml_create_attr(state_data->pool, dialog, "id", state_data->exten);
136
137         state = ast_sip_presence_xml_create_node(state_data->pool, dialog, "state");
138         pj_strdup2(state_data->pool, &state->content, statestring);
139
140         if (state_data->exten_state == AST_EXTENSION_ONHOLD) {
141                 pj_xml_node *local_node, *target, *param;
142
143                 local_node = ast_sip_presence_xml_create_node(state_data->pool, dialog, "local");
144                 target = ast_sip_presence_xml_create_node(state_data->pool, local_node, "target");
145                 ast_sip_presence_xml_create_attr(state_data->pool, target, "uri", sanitized);
146                 param = ast_sip_presence_xml_create_node(state_data->pool, target, "param");
147                 ast_sip_presence_xml_create_attr(state_data->pool, param, "pname", "+sip.rendering");
148                 ast_sip_presence_xml_create_attr(state_data->pool, param, "pvalue", "no");
149         }
150
151         return 0;
152 }
153
154 /* The maximum number of times the ast_str() for the body text can grow before we declare an XML body
155  * too large to send.
156  */
157 #define MAX_STRING_GROWTHS 3
158
159 static void dialog_info_to_string(void *body, struct ast_str **str)
160 {
161         pj_xml_node *dialog_info = body;
162         int growths = 0;
163         int size;
164
165         do {
166                 size = pj_xml_print(dialog_info, ast_str_buffer(*str), ast_str_size(*str) - 1, PJ_TRUE);
167                 if (size <= AST_PJSIP_XML_PROLOG_LEN) {
168                         ast_str_make_space(str, ast_str_size(*str) * 2);
169                         ++growths;
170                 }
171         } while (size <= AST_PJSIP_XML_PROLOG_LEN && growths < MAX_STRING_GROWTHS);
172         if (size <= AST_PJSIP_XML_PROLOG_LEN) {
173                 ast_log(LOG_WARNING, "dialog-info+xml body text too large\n");
174                 return;
175         }
176
177         *(ast_str_buffer(*str) + size) = '\0';
178         ast_str_update(*str);
179 }
180
181 static struct ast_sip_pubsub_body_generator dialog_info_body_generator = {
182         .type = "application",
183         .subtype = "dialog-info+xml",
184         .body_type = AST_SIP_EXTEN_STATE_DATA,
185         .allocate_body = dialog_info_allocate_body,
186         .generate_body_content = dialog_info_generate_body_content,
187         .to_string = dialog_info_to_string,
188         /* No need for a destroy_body callback since we use a pool */
189 };
190
191 static int load_module(void)
192 {
193         CHECK_PJSIP_PUBSUB_MODULE_LOADED();
194
195         if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
196                 return AST_MODULE_LOAD_DECLINE;
197         }
198
199         return AST_MODULE_LOAD_SUCCESS;
200 }
201
202 static int unload_module(void)
203 {
204         ast_sip_pubsub_unregister_body_generator(&dialog_info_body_generator);
205         return 0;
206 }
207
208 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Dialog Info+XML Provider",
209         .support_level = AST_MODULE_SUPPORT_CORE,
210         .load = load_module,
211         .unload = unload_module,
212         .load_pri = AST_MODPRI_CHANNEL_DEPEND,
213 );