Wed Apr 6 11:29:47 2011

Asterisk developer's documentation


res_security_log.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2009, Digium, Inc.
00005  *
00006  * Russell Bryant <russell@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*!
00020  * \file
00021  *
00022  * \author Russell Bryant <russell@digium.com>
00023  *
00024  * \brief Security Event Logging
00025  *
00026  * \todo Make informational security events optional
00027  * \todo Escape quotes in string payload IE contents
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 206021 $");
00033 
00034 #include "asterisk/module.h"
00035 #include "asterisk/logger.h"
00036 #include "asterisk/event.h"
00037 #include "asterisk/threadstorage.h"
00038 #include "asterisk/strings.h"
00039 #include "asterisk/security_events.h"
00040 
00041 static const char LOG_SECURITY_NAME[] = "SECURITY";
00042 
00043 static int LOG_SECURITY;
00044 
00045 static struct ast_event_sub *security_event_sub;
00046 
00047 AST_THREADSTORAGE(security_event_buf);
00048 static const size_t SECURITY_EVENT_BUF_INIT_LEN = 256;
00049 
00050 enum ie_required {
00051    NOT_REQUIRED,
00052    REQUIRED
00053 };
00054 
00055 static int ie_is_present(const struct ast_event *event,
00056       const enum ast_event_ie_type ie_type)
00057 {
00058    return (ast_event_get_ie_raw(event, ie_type) != NULL);
00059 }
00060 
00061 static void append_ie(struct ast_str **str, const struct ast_event *event,
00062       const enum ast_event_ie_type ie_type, enum ie_required required)
00063 {
00064    if (!required && !ie_is_present(event, ie_type)) {
00065       /* Optional IE isn't present.  Ignore. */
00066       return;
00067    }
00068 
00069    /* At this point, it _better_ be there! */
00070    ast_assert(ie_is_present(event, ie_type));
00071 
00072    switch (ast_event_get_ie_pltype(ie_type)) {
00073    case AST_EVENT_IE_PLTYPE_UINT:
00074       ast_str_append(str, 0, ",%s=\"%u\"",
00075             ast_event_get_ie_type_name(ie_type),
00076             ast_event_get_ie_uint(event, ie_type));
00077       break;
00078    case AST_EVENT_IE_PLTYPE_STR:
00079       ast_str_append(str, 0, ",%s=\"%s\"",
00080             ast_event_get_ie_type_name(ie_type),
00081             ast_event_get_ie_str(event, ie_type));
00082       break;
00083    case AST_EVENT_IE_PLTYPE_BITFLAGS:
00084       ast_str_append(str, 0, ",%s=\"%u\"",
00085             ast_event_get_ie_type_name(ie_type),
00086             ast_event_get_ie_bitflags(event, ie_type));
00087       break;
00088    case AST_EVENT_IE_PLTYPE_UNKNOWN:
00089    case AST_EVENT_IE_PLTYPE_EXISTS:
00090    case AST_EVENT_IE_PLTYPE_RAW:
00091       ast_log(LOG_WARNING, "Unexpected payload type for IE '%s'\n",
00092             ast_event_get_ie_type_name(ie_type));
00093       break;
00094    }
00095 }
00096 
00097 static void append_ies(struct ast_str **str, const struct ast_event *event,
00098       const struct ast_security_event_ie_type *ies, enum ie_required required)
00099 {
00100    unsigned int i;
00101 
00102    for (i = 0; ies[i].ie_type != AST_EVENT_IE_END; i++) {
00103       append_ie(str, event, ies[i].ie_type, required);
00104    }
00105 }
00106 
00107 static void security_event_cb(const struct ast_event *event, void *data)
00108 {
00109    struct ast_str *str;
00110    enum ast_security_event_type event_type;
00111 
00112    if (!(str = ast_str_thread_get(&security_event_buf,
00113          SECURITY_EVENT_BUF_INIT_LEN))) {
00114       return;
00115    }
00116 
00117    /* Note that the event type is guaranteed to be valid here. */
00118    event_type = ast_event_get_ie_uint(event, AST_EVENT_IE_SECURITY_EVENT);
00119    ast_assert(event_type >= 0 && event_type < AST_SECURITY_EVENT_NUM_TYPES);
00120 
00121    ast_str_set(&str, 0, "%s=\"%s\"",
00122          ast_event_get_ie_type_name(AST_EVENT_IE_SECURITY_EVENT),
00123          ast_security_event_get_name(event_type));
00124 
00125    append_ies(&str, event,
00126          ast_security_event_get_required_ies(event_type), REQUIRED);
00127    append_ies(&str, event,
00128          ast_security_event_get_optional_ies(event_type), NOT_REQUIRED);
00129 
00130    ast_log_dynamic_level(LOG_SECURITY, "%s\n", ast_str_buffer(str));
00131 }
00132 
00133 static int load_module(void)
00134 {
00135    if ((LOG_SECURITY = ast_logger_register_level(LOG_SECURITY_NAME)) == -1) {
00136       return AST_MODULE_LOAD_DECLINE;
00137    }
00138 
00139    if (!(security_event_sub = ast_event_subscribe(AST_EVENT_SECURITY,
00140          security_event_cb, "Security Event Logger",
00141          NULL, AST_EVENT_IE_END))) {
00142       ast_logger_unregister_level(LOG_SECURITY_NAME);
00143       LOG_SECURITY = -1;
00144       return AST_MODULE_LOAD_DECLINE;
00145    }
00146 
00147    ast_verb(3, "Security Logging Enabled\n");
00148 
00149    return AST_MODULE_LOAD_SUCCESS;
00150 }
00151 
00152 static int unload_module(void)
00153 {
00154    if (security_event_sub) {
00155       security_event_sub = ast_event_unsubscribe(security_event_sub);
00156    }
00157 
00158    ast_verb(3, "Security Logging Disabled\n");
00159 
00160    return 0;
00161 }
00162 
00163 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Security Event Logging");

Generated on Wed Apr 6 11:29:47 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7