Merge "vector: Traversal, retrieval, insert and locking enhancements"
authorMatt Jordan <mjordan@digium.com>
Tue, 5 May 2015 17:45:41 +0000 (12:45 -0500)
committerGerrit Code Review <gerrit2@gerrit.digium.api>
Tue, 5 May 2015 17:45:41 +0000 (12:45 -0500)
channels/chan_iax2.c
channels/chan_mgcp.c
channels/chan_sip.c
channels/chan_skinny.c
channels/chan_unistim.c
main/acl.c
main/manager.c
main/test.c
res/res_pjsip_acl.c

index 253160a..bb9c52b 100644 (file)
@@ -13746,6 +13746,7 @@ static int set_config(const char *config_file, int reload, int forced)
                } else if (!strcasecmp(v->name, "calltokenoptional")) {
                        if (add_calltoken_ignore(v->value)) {
                                ast_log(LOG_WARNING, "Invalid calltokenoptional address range - '%s' line %d\n", v->value, v->lineno);
+                               return -1;
                        }
                } else if (!strcasecmp(v->name, "calltokenexpiration")) {
                        int temp = -1;
index 602d38d..37935be 100644 (file)
@@ -4104,7 +4104,19 @@ static struct mgcp_gateway *build_gateway(char *cat, struct ast_variable *v)
                        ast_sockaddr_to_sin(&tmp, &gw->defaddr);
                } else if (!strcasecmp(v->name, "permit") ||
                        !strcasecmp(v->name, "deny")) {
-                       gw->ha = ast_append_ha(v->name, v->value, gw->ha, NULL);
+                       int acl_error = 0;
+                       gw->ha = ast_append_ha(v->name, v->value, gw->ha, &acl_error);
+                       if (acl_error) {
+                               ast_log(LOG_ERROR, "Invalid ACL '%s' specified for MGCP gateway '%s' on line %d. Not creating.\n",
+                                               v->value, cat, v->lineno);
+                               if (!gw_reload) {
+                                       ast_mutex_destroy(&gw->msgs_lock);
+                                       ast_free(gw);
+                               } else {
+                                       gw->delme = 1;
+                               }
+                               return NULL;
+                       }
                } else if (!strcasecmp(v->name, "port")) {
                        gw->addr.sin_port = htons(atoi(v->value));
                } else if (!strcasecmp(v->name, "context")) {
index 468bfbe..528e208 100644 (file)
@@ -30608,7 +30608,9 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
                                        ast_append_acl(v->name, v->value, &peer->acl, &ha_error, &acl_change_subscription_needed);
                                }
                                if (ha_error) {
-                                       ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
+                                       ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s. Deleting peer\n", v->lineno, v->value);
+                                       sip_unref_peer(peer, "Removing peer due to bad ACL configuration");
+                                       return NULL;
                                }
                        } else if (!strcasecmp(v->name, "contactpermit") || !strcasecmp(v->name, "contactdeny") || !strcasecmp(v->name, "contactacl")) {
                                int ha_error = 0;
@@ -30616,13 +30618,17 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, str
                                        ast_append_acl(v->name + 7, v->value, &peer->contactacl, &ha_error, &acl_change_subscription_needed);
                                }
                                if (ha_error) {
-                                       ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
+                                       ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s. Deleting peer\n", v->lineno, v->value);
+                                       sip_unref_peer(peer, "Removing peer due to bad contact ACL configuration");
+                                       return NULL;
                                }
                        } else if (!strcasecmp(v->name, "directmediapermit") || !strcasecmp(v->name, "directmediadeny") || !strcasecmp(v->name, "directmediaacl")) {
                                int ha_error = 0;
                                ast_append_acl(v->name + 11, v->value, &peer->directmediaacl, &ha_error, &acl_change_subscription_needed);
                                if (ha_error) {
-                                       ast_log(LOG_ERROR, "Bad directmedia ACL entry in configuration line %d : %s\n", v->lineno, v->value);
+                                       ast_log(LOG_ERROR, "Bad directmedia ACL entry in configuration line %d : %s. Deleting peer\n", v->lineno, v->value);
+                                       sip_unref_peer(peer, "Removing peer due to bad direct media ACL configuration");
+                                       return NULL;
                                }
                        } else if (!strcasecmp(v->name, "port")) {
                                peer->portinuri = 1;
@@ -31566,7 +31572,8 @@ static int reload_config(enum channelreloadreason reason)
                        int ha_error = 0;
                        ast_append_acl(v->name + 7, v->value, &sip_cfg.contact_acl, &ha_error, &acl_change_subscription_needed);
                        if (ha_error) {
-                               ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
+                               ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s. Failing to load chan_sip.so\n", v->lineno, v->value);
+                               return -1;
                        }
                } else if (!strcasecmp(v->name, "rtautoclear")) {
                        int i = atoi(v->value);
index 5f30a13..1d7d65a 100644 (file)
@@ -8044,7 +8044,14 @@ static void config_parse_variables(int type, void *item, struct ast_variable *vp
                        }
                } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
                        if (type & (TYPE_DEVICE)) {
-                               CDEV->ha = ast_append_ha(v->name, v->value, CDEV->ha, NULL);
+                               int acl_error = 0;
+
+                               CDEV->ha = ast_append_ha(v->name, v->value, CDEV->ha, &acl_error);
+                               if (acl_error) {
+                                       ast_log(LOG_ERROR, "Invalid ACL '%s' on line '%d'. Deleting device\n",
+                                                       v->value, v->lineno);
+                                       CDEV->prune = 1;
+                               }
                                continue;
                        }
                } else if (!strcasecmp(v->name, "allow")) {
index 4379aa5..51f811c 100644 (file)
@@ -6395,6 +6395,89 @@ static struct unistim_line *find_line_by_number(struct unistim_device *d, const
        return ret;
 }
 
+static void delete_device(struct unistim_device *d)
+{
+       struct unistim_line *l;
+       struct unistim_subchannel *sub;
+
+       if (unistimdebug) {
+               ast_verb(0, "Removing device '%s'\n", d->name);
+       }
+       AST_LIST_LOCK(&d->subs);
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&d->subs, sub, list){
+               if (sub->subtype == SUB_REAL) {
+                       if (!sub) {
+                               ast_log(LOG_ERROR, "Device '%s' without a subchannel !, aborting\n",
+                                               d->name);
+                               ast_config_destroy(cfg);
+                               return 0;
+                       }
+                       if (sub->owner) {
+                               ast_log(LOG_WARNING,
+                                               "Device '%s' was not deleted : a call is in progress. Try again later.\n",
+                                               d->name);
+                               d = d->next;
+                               continue;
+                       }
+               }
+               if (sub->subtype == SUB_THREEWAY) {
+                       ast_log(LOG_WARNING,
+                                       "Device '%s' with threeway call subchannels allocated, aborting.\n",
+                                       d->name);
+                       break;
+               }
+               AST_LIST_REMOVE_CURRENT(list);
+               ast_mutex_destroy(&sub->lock);
+               ast_free(sub);
+       }
+       AST_LIST_TRAVERSE_SAFE_END
+       AST_LIST_UNLOCK(&d->subs);
+
+
+       AST_LIST_LOCK(&d->lines);
+       AST_LIST_TRAVERSE_SAFE_BEGIN(&d->lines, l, list){
+               AST_LIST_REMOVE_CURRENT(list);
+               ast_mutex_destroy(&l->lock);
+               unistim_line_destroy(l);
+       }
+       AST_LIST_TRAVERSE_SAFE_END
+       AST_LIST_UNLOCK(&d->lines);
+
+       if (d->session) {
+               if (sessions == d->session) {
+                       sessions = d->session->next;
+               } else {
+                       s = sessions;
+                       while (s) {
+                               if (s->next == d->session) {
+                                       s->next = d->session->next;
+                                       break;
+                               }
+                               s = s->next;
+                       }
+               }
+               ast_mutex_destroy(&d->session->lock);
+               ast_free(d->session);
+       }
+       if (devices == d) {
+               devices = d->next;
+       } else {
+               struct unistim_device *d2 = devices;
+               while (d2) {
+                       if (d2->next == d) {
+                               d2->next = d->next;
+                               break;
+                       }
+                       d2 = d2->next;
+               }
+       }
+       if (d->tz) {
+               d->tz = ast_tone_zone_unref(d->tz);
+       }
+       ast_mutex_destroy(&d->lock);
+       ast_free(d);
+}
+
 static struct unistim_device *build_device(const char *cat, const struct ast_variable *v)
 {
        struct unistim_device *d;
@@ -6486,7 +6569,14 @@ static struct unistim_device *build_device(const char *cat, const struct ast_var
                } else if (!strcasecmp(v->name, "tn")) {
                        ast_copy_string(d->extension_number, v->value, sizeof(d->extension_number));
                } else if (!strcasecmp(v->name, "permit") || !strcasecmp(v->name, "deny")) {
-                       d->ha = ast_append_ha(v->name, v->value, d->ha, NULL);
+                       int acl_error = 0;
+                       d->ha = ast_append_ha(v->name, v->value, d->ha, &acl_error);
+                       if (acl_error) {
+                               ast_log(LOG_ERROR, "Invalid ACL '%s' specified for device '%s' on line %d. Deleting device\n",
+                                               v->value, cat, v->lineno);
+                               delete_device(d);
+                               return NULL;
+                       }
                } else if (!strcasecmp(v->name, "context")) {
                        ast_copy_string(d->context, v->value, sizeof(d->context));
                } else if (!strcasecmp(v->name, "maintext0")) {
@@ -6853,85 +6943,7 @@ static int reload_config(void)
        d = devices;
        while (d) {
                if (d->to_delete) {
-                       struct unistim_line *l;
-                       struct unistim_subchannel *sub;
-
-                       if (unistimdebug) {
-                               ast_verb(0, "Removing device '%s'\n", d->name);
-                       }
-                       AST_LIST_LOCK(&d->subs);
-                       AST_LIST_TRAVERSE_SAFE_BEGIN(&d->subs, sub, list){
-                               if (sub->subtype == SUB_REAL) {
-                                       if (!sub) {
-                                               ast_log(LOG_ERROR, "Device '%s' without a subchannel !, aborting\n",
-                                                               d->name);
-                                               ast_config_destroy(cfg);
-                                               return 0;
-                                       }
-                                       if (sub->owner) {
-                                               ast_log(LOG_WARNING,
-                                                               "Device '%s' was not deleted : a call is in progress. Try again later.\n",
-                                                               d->name);
-                                               d = d->next;
-                                               continue;
-                                       }
-                               }
-                               if (sub->subtype == SUB_THREEWAY) {
-                                       ast_log(LOG_WARNING,
-                                                       "Device '%s' with threeway call subchannels allocated, aborting.\n",
-                                                       d->name);
-                                       break;
-                               }
-                               AST_LIST_REMOVE_CURRENT(list);
-                               ast_mutex_destroy(&sub->lock);
-                               ast_free(sub);
-                       }
-                       AST_LIST_TRAVERSE_SAFE_END
-                       AST_LIST_UNLOCK(&d->subs);
-
-
-                       AST_LIST_LOCK(&d->lines);
-                       AST_LIST_TRAVERSE_SAFE_BEGIN(&d->lines, l, list){
-                               AST_LIST_REMOVE_CURRENT(list);
-                               ast_mutex_destroy(&l->lock);
-                               unistim_line_destroy(l);
-                       }
-                       AST_LIST_TRAVERSE_SAFE_END
-                       AST_LIST_UNLOCK(&d->lines);
-
-                       if (d->session) {
-                               if (sessions == d->session) {
-                                       sessions = d->session->next;
-                               } else {
-                                       s = sessions;
-                                       while (s) {
-                                               if (s->next == d->session) {
-                                                       s->next = d->session->next;
-                                                       break;
-                                               }
-                                               s = s->next;
-                                       }
-                               }
-                               ast_mutex_destroy(&d->session->lock);
-                               ast_free(d->session);
-                       }
-                       if (devices == d) {
-                               devices = d->next;
-                       } else {
-                               struct unistim_device *d2 = devices;
-                               while (d2) {
-                                       if (d2->next == d) {
-                                               d2->next = d->next;
-                                               break;
-                                       }
-                                       d2 = d2->next;
-                               }
-                       }
-                       if (d->tz) {
-                               d->tz = ast_tone_zone_unref(d->tz);
-                       }
-                       ast_mutex_destroy(&d->lock);
-                       ast_free(d);
+                       delete_device(d);
                        d = devices;
                        continue;
                }
index 92b6754..d133b2a 100644 (file)
@@ -479,7 +479,7 @@ void ast_append_acl(const char *sense, const char *stuff, struct ast_acl_list **
                AST_LIST_TRAVERSE(working_list, current, list) {
                        if (!strcasecmp(current->name, tmp)) { /* ACL= */
                                /* Inclusion of the same ACL multiple times isn't a catastrophic error, but it will raise the error flag and skip the entry. */
-                               ast_log(LOG_ERROR, "Named ACL '%s' is already included in the ast_acl container.", tmp);
+                               ast_log(LOG_ERROR, "Named ACL '%s' occurs multiple times in ACL definition. Please update your ACL configuration.", tmp);
                                if (error) {
                                        *error = 1;
                                }
index 2ff9df9..846f6e6 100644 (file)
@@ -8977,7 +8977,13 @@ static int __init_manager(int reload, int by_external_config)
                        } else if (!strcasecmp(var->name, "deny") ||
                                       !strcasecmp(var->name, "permit") ||
                                       !strcasecmp(var->name, "acl")) {
-                               ast_append_acl(var->name, var->value, &user->acl, NULL, &acl_subscription_flag);
+                               int acl_error = 0;
+
+                               ast_append_acl(var->name, var->value, &user->acl, &acl_error, &acl_subscription_flag);
+                               if (acl_error) {
+                                       ast_log(LOG_ERROR, "Invalid ACL '%s' for manager user '%s' on line %d. Deleting user\n");
+                                       user->keep = 0;
+                               }
                        }  else if (!strcasecmp(var->name, "read") ) {
                                user->readperm = get_perm(var->value);
                        }  else if (!strcasecmp(var->name, "write") ) {
index 47ae7f8..d1dce09 100644 (file)
@@ -110,6 +110,7 @@ static struct ast_test *test_free(struct ast_test *test);
 static int test_insert(struct ast_test *test);
 static struct ast_test *test_remove(ast_test_cb_t *cb);
 static int test_cat_cmp(const char *cat1, const char *cat2);
+static int registration_errors = 0;
 
 void ast_test_debug(struct ast_test *test, const char *fmt, ...)
 {
@@ -197,16 +198,19 @@ int ast_test_register(ast_test_cb_t *cb)
        struct ast_test *test;
 
        if (!cb) {
-               ast_log(LOG_WARNING, "Attempted to register test without all required information\n");
+               ast_log(LOG_ERROR, "Attempted to register test without all required information\n");
+               registration_errors++;
                return -1;
        }
 
        if (!(test = test_alloc(cb))) {
+               registration_errors++;
                return -1;
        }
 
        if (test_insert(test)) {
                test_free(test);
+               registration_errors++;
                return -1;
        }
 
@@ -619,7 +623,9 @@ static struct ast_test *test_alloc(ast_test_cb_t *cb)
 {
        struct ast_test *test;
 
-       if (!cb || !(test = ast_calloc(1, sizeof(*test)))) {
+       test = ast_calloc(1, sizeof(*test));
+       if (!test) {
+               ast_log(LOG_ERROR, "Failed to allocate test, registration failed.\n");
                return NULL;
        }
 
@@ -628,12 +634,12 @@ static struct ast_test *test_alloc(ast_test_cb_t *cb)
        test->cb(&test->info, TEST_INIT, test);
 
        if (ast_strlen_zero(test->info.name)) {
-               ast_log(LOG_WARNING, "Test has no name, test registration refused.\n");
+               ast_log(LOG_ERROR, "Test has no name, test registration refused.\n");
                return test_free(test);
        }
 
        if (ast_strlen_zero(test->info.category)) {
-               ast_log(LOG_WARNING, "Test %s has no category, test registration refused.\n",
+               ast_log(LOG_ERROR, "Test %s has no category, test registration refused.\n",
                                test->info.name);
                return test_free(test);
        }
@@ -641,21 +647,26 @@ static struct ast_test *test_alloc(ast_test_cb_t *cb)
        if (test->info.category[0] != '/' || test->info.category[strlen(test->info.category) - 1] != '/') {
                ast_log(LOG_WARNING, "Test category '%s' for test '%s' is missing a leading or trailing slash.\n",
                                test->info.category, test->info.name);
+               /* Flag an error anyways so test_registrations fails but allow the test to be
+                * registered. */
+               registration_errors++;
        }
 
        if (ast_strlen_zero(test->info.summary)) {
-               ast_log(LOG_WARNING, "Test %s%s has no summary, test registration refused.\n",
+               ast_log(LOG_ERROR, "Test %s%s has no summary, test registration refused.\n",
                                test->info.category, test->info.name);
                return test_free(test);
        }
 
        if (ast_strlen_zero(test->info.description)) {
-               ast_log(LOG_WARNING, "Test %s%s has no description, test registration refused.\n",
+               ast_log(LOG_ERROR, "Test %s%s has no description, test registration refused.\n",
                                test->info.category, test->info.name);
                return test_free(test);
        }
 
        if (!(test->status_str = ast_str_create(128))) {
+               ast_log(LOG_ERROR, "Failed to allocate status_str for %s%s, test registration failed.\n",
+                               test->info.category, test->info.name);
                return test_free(test);
        }
 
@@ -1095,19 +1106,38 @@ void __ast_test_suite_event_notify(const char *file, const char *func, int line,
        stasis_publish(ast_test_suite_topic(), msg);
 }
 
-#endif /* TEST_FRAMEWORK */
+AST_TEST_DEFINE(test_registrations)
+{
+       switch (cmd) {
+       case TEST_INIT:
+               info->name = "registrations";
+               info->category = "/main/test/";
+               info->summary = "Validate Test Registration Data.";
+               info->description = "Validate Test Registration Data.";
+               return AST_TEST_NOT_RUN;
+       case TEST_EXECUTE:
+               break;
+       }
 
-#ifdef TEST_FRAMEWORK
+       if (registration_errors) {
+               ast_test_status_update(test,
+                       "%d test registration error%s occurred.  See startup logs for details.\n",
+                       registration_errors, registration_errors > 1 ? "s" : "");
+               return AST_TEST_FAIL;
+       }
+
+       return AST_TEST_PASS;
+}
 
 static void test_cleanup(void)
 {
+       AST_TEST_UNREGISTER(test_registrations);
        ast_cli_unregister_multiple(test_cli, ARRAY_LEN(test_cli));
        ao2_cleanup(test_suite_topic);
        test_suite_topic = NULL;
        STASIS_MESSAGE_TYPE_CLEANUP(ast_test_suite_message_type);
 }
-
-#endif
+#endif /* TEST_FRAMEWORK */
 
 int ast_test_init(void)
 {
@@ -1124,6 +1154,8 @@ int ast_test_init(void)
                return -1;
        }
 
+       AST_TEST_REGISTER(test_registrations);
+
        /* Register cli commands */
        ast_cli_register_multiple(test_cli, ARRAY_LEN(test_cli));
 #endif
index a04f2b4..151ebed 100644 (file)
@@ -238,8 +238,21 @@ static int acl_handler(const struct aco_option *opt, struct ast_variable *var, v
 
        if (!strncmp(var->name, "contact_", 8)) {
                ast_append_acl(var->name + 8, var->value, &sip_acl->contact_acl, &error, &ignore);
+               if (error) {
+                       ast_log(LOG_ERROR, "Bad contact ACL '%s' at line '%d' of pjsip.conf\n",
+                                       var->value, var->lineno);
+               }
        } else {
                ast_append_acl(var->name, var->value, &sip_acl->acl, &error, &ignore);
+               if (error) {
+                       ast_log(LOG_ERROR, "Bad ACL '%s' at line '%d' of pjsip.conf\n",
+                                       var->value, var->lineno);
+               }
+       }
+
+       if (error) {
+               ast_log(LOG_ERROR, "There is an error in ACL configuration. Blocking ALL SIP traffic.\n");
+               ast_append_acl("deny", "0.0.0.0/0.0.0.0", &sip_acl->acl, NULL, &ignore);
        }
 
        return error;