Improve XML sanitization in NOTIFYs, especially for presence subtypes and messages.
[asterisk/asterisk.git] / res / res_pjsip_pidf_digium_body_supplement.c
1 /*
2  * asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2014, Digium, Inc.
5  *
6  * Kevin Harwell <kharwell@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         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_simple.h>
30 #include <pjlib.h>
31
32 #include "asterisk/module.h"
33 #include "asterisk/presencestate.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 static int pidf_supplement_body(void *body, void *data)
40 {
41         struct ast_sip_exten_state_data *state_data = data;
42         pj_xml_node *node;
43         char sanitized[256];
44
45         if (ast_strlen_zero(state_data->user_agent) ||
46             !strstr(state_data->user_agent, "digium")) {
47                 /* not a digium phone */
48                 return 0;
49         }
50
51         if (!(node = ast_sip_presence_xml_create_node(
52                       state_data->pool, body, "tuple"))) {
53                 ast_log(LOG_WARNING, "Unable to create PIDF tuple\n");
54                 return -1;
55         }
56
57         ast_sip_presence_xml_create_attr(
58                 state_data->pool, node, "id", "digium-presence");
59
60         if (!(node = ast_sip_presence_xml_create_node(
61                       state_data->pool, node, "status"))) {
62                 ast_log(LOG_WARNING, "Unable to create PIDF tuple status\n");
63                 return -1;
64         }
65
66         if (!(node = ast_sip_presence_xml_create_node(
67                       state_data->pool, node, "digium_presence"))) {
68                 ast_log(LOG_WARNING, "Unable to create digium presence\n");
69                 return -1;
70         }
71
72         if (!ast_strlen_zero(state_data->presence_message)) {
73                 ast_sip_sanitize_xml(state_data->presence_message, sanitized, sizeof(sanitized));
74                 pj_strdup2(state_data->pool, &node->content, sanitized);
75         }
76
77         ast_sip_presence_xml_create_attr(
78                 state_data->pool, node, "type", ast_presence_state2str(
79                         state_data->presence_state));
80
81         if (!ast_strlen_zero(state_data->presence_subtype)) {
82                 ast_sip_sanitize_xml(state_data->presence_subtype, sanitized, sizeof(sanitized));
83                 ast_sip_presence_xml_create_attr(
84                         state_data->pool, node, "subtype", sanitized);
85         }
86
87         return 0;
88 }
89
90 static struct ast_sip_pubsub_body_supplement pidf_supplement = {
91         .type = "application",
92         .subtype = "pidf+xml",
93         .supplement_body = pidf_supplement_body,
94 };
95
96 static int load_module(void)
97 {
98         if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
99                 return AST_MODULE_LOAD_DECLINE;
100         }
101         return AST_MODULE_LOAD_SUCCESS;
102 }
103
104 static int unload_module(void)
105 {
106         ast_sip_pubsub_unregister_body_supplement(&pidf_supplement);
107         return 0;
108 }
109
110 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP PIDF Digium presence supplement",
111                 .load = load_module,
112                 .unload = unload_module,
113                 .load_pri = AST_MODPRI_CHANNEL_DEPEND,
114 );