diff options
author | Matthew Jordan <mjordan@digium.com> | 2013-01-02 18:11:59 +0000 |
---|---|---|
committer | Matthew Jordan <mjordan@digium.com> | 2013-01-02 18:11:59 +0000 |
commit | 8fb5bdce9ab9f7f3758545753cbc787653920753 (patch) | |
tree | 65809194af8a4fa757fef26a30a2cc9c3ed02182 /res | |
parent | 1fb06fde957fb466388c764384c2e14698e2dc15 (diff) |
Prevent exhaustion of system resources through exploitation of event cache
Asterisk maintains an internal cache for devices in the event subsystem. The
device state cache holds the state of each device known to Asterisk, such that
consumers of device state information can query for the last known state for
a particular device, even if it is not part of an active call. The concept of
a device in Asterisk can include entities that do not have a physical
representation. One way that this occurred was when anonymous calls are allowed
in Asterisk. A device was automatically created and stored in the cache for
each anonymous call that occurred; this was possible in the SIP and IAX2
channel drivers and through channel drivers that utilized the
res_jabber/res_xmpp resource modules (Gtalk, Jingle, and Motif). These devices
are never removed from the system, allowing anonymous calls to potentially
exhaust a system's resources.
This patch changes the event cache subsystem and device state management to
no longer cache devices that are not associated with a physical entity.
(issue ASTERISK-20175)
Reported by: Russell Bryant, Leif Madsen, Joshua Colp
Tested by: kmoore
patches:
event-cachability-3.diff uploaded by jcolp (license 5000)
........
Merged revisions 378303 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........
Merged revisions 378320 from http://svn.asterisk.org/svn/asterisk/branches/10
........
Merged revisions 378321 from http://svn.asterisk.org/svn/asterisk/branches/11
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@378322 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'res')
-rw-r--r-- | res/res_calendar.c | 8 | ||||
-rw-r--r-- | res/res_jabber.c | 61 |
2 files changed, 50 insertions, 19 deletions
diff --git a/res/res_calendar.c b/res/res_calendar.c index ed441d10a..fd4237513 100644 --- a/res/res_calendar.c +++ b/res/res_calendar.c @@ -620,9 +620,9 @@ static struct ast_calendar_event *destroy_event(struct ast_calendar_event *event * but haven't hit the end event yet, go ahead and set the devicestate to the current busy status */ if (event->bs_start_sched < 0 && event->bs_end_sched >= 0) { if (!calendar_is_busy(event->owner)) { - ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Calendar:%s", event->owner->name); + ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name); } else { - ast_devstate_changed(AST_DEVICE_BUSY, "Calendar:%s", event->owner->name); + ast_devstate_changed(AST_DEVICE_BUSY, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name); } } @@ -880,9 +880,9 @@ static int calendar_devstate_change(const void *data) /* We can have overlapping events, so ignore the event->busy_state and check busy state * based on all events in the calendar */ if (!calendar_is_busy(event->owner)) { - ast_devstate_changed(AST_DEVICE_NOT_INUSE, "Calendar:%s", event->owner->name); + ast_devstate_changed(AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name); } else { - ast_devstate_changed(AST_DEVICE_BUSY, "Calendar:%s", event->owner->name); + ast_devstate_changed(AST_DEVICE_BUSY, AST_DEVSTATE_CACHABLE, "Calendar:%s", event->owner->name); } event = ast_calendar_unref_event(event); diff --git a/res/res_jabber.c b/res/res_jabber.c index fa45fdf6d..f74d6acf6 100644 --- a/res/res_jabber.c +++ b/res/res_jabber.c @@ -361,7 +361,7 @@ static char *aji_cli_create_leafnode(struct ast_cli_entry *e, int cmd, static void aji_create_affiliations(struct aji_client *client, const char *node); static iks* aji_pubsub_iq_create(struct aji_client *client, const char *type); static void aji_publish_device_state(struct aji_client *client, const char * device, - const char *device_state); + const char *device_state, unsigned int cachable); static int aji_handle_pubsub_error(void *data, ikspak *pak); static int aji_handle_pubsub_event(void *data, ikspak *pak); static void aji_pubsub_subscribe(struct aji_client *client, const char *node); @@ -375,7 +375,7 @@ static void aji_publish_mwi(struct aji_client *client, const char *mailbox, static void aji_devstate_cb(const struct ast_event *ast_event, void *data); static void aji_mwi_cb(const struct ast_event *ast_event, void *data); static iks* aji_build_publish_skeleton(struct aji_client *client, const char *node, - const char *event_type); + const char *event_type, unsigned int cachable); /* No transports in this version */ /* static int aji_create_transport(char *label, struct aji_client *client); @@ -3275,6 +3275,7 @@ static void aji_devstate_cb(const struct ast_event *ast_event, void *data) { const char *device; const char *device_state; + unsigned int cachable; struct aji_client *client; if (ast_eid_cmp(&ast_eid_default, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID))) { @@ -3286,7 +3287,8 @@ static void aji_devstate_cb(const struct ast_event *ast_event, void *data) client = ASTOBJ_REF((struct aji_client *) data); device = ast_event_get_ie_str(ast_event, AST_EVENT_IE_DEVICE); device_state = ast_devstate_str(ast_event_get_ie_uint(ast_event, AST_EVENT_IE_STATE)); - aji_publish_device_state(client, device, device_state); + cachable = ast_event_get_ie_uint(ast_event, AST_EVENT_IE_CACHABLE); + aji_publish_device_state(client, device, device_state, cachable); ASTOBJ_UNREF(client, ast_aji_client_destroy); } @@ -3327,11 +3329,13 @@ static void aji_init_event_distribution(struct aji_client *client) */ static int aji_handle_pubsub_event(void *data, ikspak *pak) { - char *item_id, *device_state, *context; + char *item_id, *device_state, *context, *cachable_str; int oldmsgs, newmsgs; iks *item, *item_content; struct ast_eid pubsub_eid; struct ast_event *event; + unsigned int cachable = AST_DEVSTATE_CACHABLE; + item = iks_find(iks_find(iks_find(pak->x, "event"), "items"), "item"); if (!item) { ast_log(LOG_ERROR, "Could not parse incoming PubSub event\n"); @@ -3346,11 +3350,14 @@ static int aji_handle_pubsub_event(void *data, ikspak *pak) } if (!strcasecmp(iks_name(item_content), "state")) { device_state = iks_find_cdata(item, "state"); + if ((cachable_str = iks_find_cdata(item, "cachable"))) { + sscanf(cachable_str, "%30d", &cachable); + } if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE, - AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, item_id, AST_EVENT_IE_STATE, - AST_EVENT_IE_PLTYPE_UINT, ast_devstate_val(device_state), AST_EVENT_IE_EID, - AST_EVENT_IE_PLTYPE_RAW, &pubsub_eid, sizeof(pubsub_eid), - AST_EVENT_IE_END))) { + AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, item_id, AST_EVENT_IE_STATE, + AST_EVENT_IE_PLTYPE_UINT, ast_devstate_val(device_state), AST_EVENT_IE_EID, + AST_EVENT_IE_PLTYPE_RAW, &pubsub_eid, sizeof(pubsub_eid), + AST_EVENT_IE_END))) { return IKS_FILTER_EAT; } } else if (!strcasecmp(iks_name(item_content), "mailbox")) { @@ -3370,7 +3377,13 @@ static int aji_handle_pubsub_event(void *data, ikspak *pak) iks_name(item_content)); return IKS_FILTER_EAT; } - ast_event_queue_and_cache(event); + + if (cachable == AST_DEVSTATE_CACHABLE) { + ast_event_queue_and_cache(event); + } else { + ast_event_queue(event); + } + return IKS_FILTER_EAT; } @@ -3445,7 +3458,7 @@ static void aji_pubsub_subscribe(struct aji_client *client, const char *node) * \return iks * */ static iks* aji_build_publish_skeleton(struct aji_client *client, const char *node, - const char *event_type) + const char *event_type, unsigned int cachable) { iks *request = aji_pubsub_iq_create(client, "set"); iks *pubsub, *publish, *item; @@ -3459,8 +3472,24 @@ static iks* aji_build_publish_skeleton(struct aji_client *client, const char *no } item = iks_insert(publish, "item"); iks_insert_attrib(item, "id", node); - return item; + if (cachable == AST_DEVSTATE_NOT_CACHABLE) { + iks *options, *x, *field_form_type, *field_persist; + + options = iks_insert(pubsub, "publish-options"); + x = iks_insert(options, "x"); + iks_insert_attrib(x, "xmlns", "jabber:x:data"); + iks_insert_attrib(x, "type", "submit"); + field_form_type = iks_insert(x, "field"); + iks_insert_attrib(field_form_type, "var", "FORM_TYPE"); + iks_insert_attrib(field_form_type, "type", "hidden"); + iks_insert_cdata(iks_insert(field_form_type, "value"), "http://jabber.org/protocol/pubsub#publish-options", 0); + field_persist = iks_insert(x, "field"); + iks_insert_attrib(field_persist, "var", "pubsub#persist_items"); + iks_insert_cdata(iks_insert(field_persist, "value"), "0", 1); + } + + return item; } /*! @@ -3471,11 +3500,11 @@ static iks* aji_build_publish_skeleton(struct aji_client *client, const char *no * \return void */ static void aji_publish_device_state(struct aji_client *client, const char *device, - const char *device_state) + const char *device_state, unsigned int cachable) { - iks *request = aji_build_publish_skeleton(client, device, "device_state"); + iks *request = aji_build_publish_skeleton(client, device, "device_state", cachable); iks *state; - char eid_str[20]; + char eid_str[20], cachable_str[2]; if (ast_test_flag(&pubsubflags, AJI_PUBSUB_AUTOCREATE)) { if (ast_test_flag(&pubsubflags, AJI_XEP0248)) { aji_create_pubsub_node(client, "leaf", device, "device_state"); @@ -3487,6 +3516,8 @@ static void aji_publish_device_state(struct aji_client *client, const char *devi state = iks_insert(request, "state"); iks_insert_attrib(state, "xmlns", "http://asterisk.org"); iks_insert_attrib(state, "eid", eid_str); + snprintf(cachable_str, sizeof(cachable_str), "%u", cachable); + iks_insert_attrib(state, "cachable", cachable_str); iks_insert_cdata(state, device_state, strlen(device_state)); ast_aji_send(client, iks_root(request)); iks_delete(request); @@ -3508,7 +3539,7 @@ static void aji_publish_mwi(struct aji_client *client, const char *mailbox, char eid_str[20]; iks *mailbox_node, *request; snprintf(full_mailbox, sizeof(full_mailbox), "%s@%s", mailbox, context); - request = aji_build_publish_skeleton(client, full_mailbox, "message_waiting"); + request = aji_build_publish_skeleton(client, full_mailbox, "message_waiting", 1); ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default); mailbox_node = iks_insert(request, "mailbox"); iks_insert_attrib(mailbox_node, "xmlns", "http://asterisk.org"); |