summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVladislav Grishenko <themiron@mail.ru>2021-04-07 05:50:07 +0500
committerVladislav Grishenko <themiron@mail.ru>2021-04-07 05:50:54 +0500
commit0ae932e8e39a4cfc88dc2102789791e376fe21cf (patch)
tree133b803abf4cd70a5733ef16c4ab3d5117d4c314
parent63ea2beb79e30417dcea4a0926a3a18303872658 (diff)
downloadaccel-ppp-0ae932e8e39a4cfc88dc2102789791e376fe21cf.tar.gz
accel-ppp-0ae932e8e39a4cfc88dc2102789791e376fe21cf.zip
ippool: fix next pool handling
* fix no next pool when it was defined after referencing: 192.168.1.2-255,name=pool1,next=pool2 192.168.2.2-255,name=pool2 * fix UB when next pool was set to same pool: 192.168.1.2-255,name=pool1,next=pool1 * add warning about empty/not defined next pools
-rw-r--r--accel-pppd/extra/ippool.c136
1 files changed, 65 insertions, 71 deletions
diff --git a/accel-pppd/extra/ippool.c b/accel-pppd/extra/ippool.c
index 00f22f1..2b3ef3f 100644
--- a/accel-pppd/extra/ippool.c
+++ b/accel-pppd/extra/ippool.c
@@ -539,63 +539,58 @@ static int parse_vendor_opt(const char *opt)
}
#endif
-static void parse_options(const char *opt, char **pool_name, generate_func *generate, struct ippool_t **next)
+static int parse_options(const char *opt, struct ippool_t **pool, generate_func *generate, struct ippool_t **next)
{
- char *ptr1, *ptr2;
- int len;
- char tmp[256];
-
- ptr1 = strstr(opt, ",name=");
- if (ptr1) {
- ptr1 += 6;
- for (ptr2 = ptr1; *ptr2 && *ptr2 != ','; ptr2++);
- len = ptr2 - ptr1;
- *pool_name = _malloc(len + 1);
- memcpy(*pool_name, ptr1, len);
- (*pool_name)[len] = 0;
- }
-
- ptr1 = strstr(opt, ",allocator=");
- if (ptr1) {
- ptr1 += 11;
- for (ptr2 = ptr1; *ptr2 && *ptr2 != ','; ptr2++);
- len = ptr2 - ptr1;
-
- if (len == 3 && memcmp(ptr1, "p2p", 3) == 0)
+ char *name, *ptr;
+
+ name = strstr(opt, ",name=");
+ if (name) {
+ name += sizeof(",name=") - 1;
+ ptr = strchrnul(name, ',');
+ name = _strndup(name, ptr - name);
+ if (!name)
+ return -1;
+ *pool = find_pool(name, 1);
+ } else if ((name = strchr(opt, ',')) && !strchr(name + 1, '=')) {
+ name = _strdup(name + 1);
+ if (!name)
+ return -1;
+ *pool = find_pool(name, 1);
+ } else
+ *pool = def_pool;
+
+ name = strstr(opt, ",allocator=");
+ if (name) {
+ name += sizeof(",allocator=") - 1;
+ ptr = strchrnul(name, ',');
+ name = strncpy(alloca(ptr - name + 1), name, ptr - name + 1);
+ if (strcmp(name, "p2p") == 0)
*generate = generate_pool_p2p;
- else if (len == 5 && memcmp(ptr1, "net30", 5) == 0)
+ else if (strcmp(name, "net30") == 0)
*generate = generate_pool_net30;
- else
+ else {
log_error("ipool: '%s': unknown allocator\n", opt);
- }
-
- ptr1 = strstr(opt, ",next=");
- if (ptr1) {
- ptr1 += 6;
- for (ptr2 = ptr1; *ptr2 && *ptr2 != ','; ptr2++);
- if (*ptr2 == ',') {
- len = ptr2 - ptr1;
- memcpy(tmp, ptr1, len);
- tmp[len] = 0;
- ptr1 = tmp;
+ return -1;
}
+ } else
+ *generate = generate_pool_p2p;
+
+ name = strstr(opt, ",next=");
+ if (name) {
+ name += sizeof(",next=") - 1;
+ ptr = strchrnul(name, ',');
+ name = strncpy(alloca(ptr - name + 1), name, ptr - name + 1);
+ *next = find_pool(name, 0);
+ if (!*next) {
+ name = _strdup(name);
+ if (!name)
+ return -1;
+ *next = find_pool(name, 1);
+ }
+ } else
+ *next = NULL;
- *next = find_pool(ptr1, 0);
- if (!(*next))
- log_error("ippool: %s: next pool not found\n", opt);
- }
-
- if (!*pool_name) {
- ptr1 = strchr(opt, ',');
- if (!ptr1)
- return;
-
- for (ptr2 = ptr1 + 1; *ptr2 && *ptr2 != '='; ptr2++);
- if (*ptr2 == '=')
- return;
-
- *pool_name = _strdup(ptr1 + 1);
- }
+ return 0;
}
static void ippool_init1(void)
@@ -607,10 +602,8 @@ static void ippool_init2(void)
{
struct conf_sect_t *s = conf_get_section("ip-pool");
struct conf_option_t *opt;
- struct ippool_t *p;
- char *pool_name = NULL;
+ struct ippool_t *pool, *next;
generate_func generate;
- struct ippool_t *next;
if (!s)
return;
@@ -623,9 +616,7 @@ static void ippool_init2(void)
if (!strcmp(opt->name, "vendor")) {
conf_vendor = parse_vendor_opt(opt->val);
continue;
- }
-
- if (!strcmp(opt->name, "attr")) {
+ } else if (!strcmp(opt->name, "attr")) {
conf_attr = parse_attr_opt(opt->val);
continue;
}
@@ -636,30 +627,34 @@ static void ippool_init2(void)
else if (!strcmp(opt->name, "shuffle"))
conf_shuffle = atoi(opt->val);
else {
- pool_name = NULL;
- generate = generate_pool_p2p;
- next = NULL;
-
- parse_options(opt->raw, &pool_name, &generate, &next);
-
- p = pool_name ? find_pool(pool_name, 1) : def_pool;
+ if (parse_options(opt->raw, &pool, &generate, &next)) {
+ log_error("ippool: failed to parse '%s'\n", opt->raw);
+ continue;
+ }
if (!strcmp(opt->name, "gw"))
- add_range(p, &p->gw_list, opt->val, generate);
+ add_range(pool, &pool->gw_list, opt->val, generate);
else if (!strcmp(opt->name, "tunnel"))
- add_range(p, &p->tunnel_list, opt->val, generate);
+ add_range(pool, &pool->tunnel_list, opt->val, generate);
else if (!opt->val || strchr(opt->name, ','))
- add_range(p, &p->tunnel_list, opt->name, generate);
+ add_range(pool, &pool->tunnel_list, opt->name, generate);
- p->next = next;
+ if (pool == next)
+ log_warn("ippool: %s: same next pool\n", opt->raw);
+ else if (next)
+ pool->next = next;
}
}
if (def_pool->generate)
def_pool->generate(def_pool);
- list_for_each_entry(p, &pool_list, entry)
- p->generate(p);
+ list_for_each_entry(pool, &pool_list, entry) {
+ if (pool->generate)
+ pool->generate(pool);
+ else
+ log_warn("ippool: pool '%s' is empty or not defined\n", pool->name);
+ }
#ifdef USE_BACKUP
backup_register_module(&backup_mod);
@@ -673,4 +668,3 @@ static void ippool_init2(void)
DEFINE_INIT(51, ippool_init1);
DEFINE_INIT2(52, ippool_init2);
-