From a51a5af5096f15ad73475bc997a1bac7db2d4663 Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Fri, 6 Jan 2017 00:14:38 +0100 Subject: [PATCH 1/8] fixed wrong usage of mongoc_cursor_more causing infinite loops --- be-mongo.c | 160 +++++++++++++++++++++++++++-------------------------- 1 file changed, 83 insertions(+), 77 deletions(-) diff --git a/be-mongo.c b/be-mongo.c index bba3c851..a3c99c96 100644 --- a/be-mongo.c +++ b/be-mongo.c @@ -148,19 +148,18 @@ char *be_mongo_getuser(void *handle, const char *username, const char *password, NULL); /* Read Prefs, NULL for default */ - while (!mongoc_cursor_error (cursor, &error) && - mongoc_cursor_more (cursor)) { - if (mongoc_cursor_next (cursor, &doc)) { - - bson_iter_init(&iter, doc); - bson_iter_find(&iter, conf->password_loc); - - char *src = (char *)bson_iter_utf8(&iter, NULL); - size_t tmp = strlen(src) + 1; - result = (char *) malloc(tmp); - memset(result, 0, tmp); - memcpy(result, src, tmp); - } + if (!mongoc_cursor_error (cursor, &error) && + mongoc_cursor_next (cursor, &doc)) { + + bson_iter_init(&iter, doc); + bson_iter_find(&iter, conf->password_loc); + + char *src = (char *)bson_iter_utf8(&iter, NULL); + size_t tmp = strlen(src) + 1; + result = (char *) malloc(tmp); + memset(result, 0, tmp); + memcpy(result, src, tmp); + } if (mongoc_cursor_error (cursor, &error)) { @@ -218,16 +217,14 @@ int be_mongo_superuser(void *conf, const char *username) NULL, NULL); - while (!mongoc_cursor_error (cursor, &error) && - mongoc_cursor_more (cursor)) { - if (mongoc_cursor_next (cursor, &doc)) { - bson_iter_init(&iter, doc); - bson_iter_find(&iter, handle->superuser_loc); + if (!mongoc_cursor_error (cursor, &error) && + mongoc_cursor_next (cursor, &doc)) { + bson_iter_init(&iter, doc); + bson_iter_find(&iter, handle->superuser_loc); - result = (int64_t) bson_iter_as_int64(&iter); + result = (int64_t) bson_iter_as_int64(&iter); - } - } + } if (mongoc_cursor_error (cursor, &error)) { fprintf (stderr, "Cursor Failure: %s\n", error.message); @@ -249,8 +246,8 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co const bson_t *doc; bson_iter_t iter; - bool check = false; - int match = 0, foundFlag = 0; + bool check = false, foundFlag = false; + int match = 0; bson_t query; @@ -268,61 +265,15 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co NULL, NULL); - while (!mongoc_cursor_error (cursor, &error) && - mongoc_cursor_more (cursor)) { - if (foundFlag == 0 && mongoc_cursor_next (cursor, &doc)) { - bson_iter_init(&iter, doc); - bson_iter_find(&iter, handle->topic_loc); - - int64_t topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); - - bson_destroy(&query); - mongoc_cursor_destroy(cursor); - mongoc_collection_destroy(collection); - - bson_init(&query); - bson_append_int64(&query, handle->topicId_loc, -1, topId); - collection = mongoc_client_get_collection(handle->client, handle->database, handle->topics_coll); - cursor = mongoc_collection_find(collection, - MONGOC_QUERY_NONE, - 0, - 0, - 0, - &query, - NULL, - NULL); - foundFlag = 1; - } - if (foundFlag == 1 && mongoc_cursor_next(cursor, &doc)) { - - bson_iter_init(&iter, doc); - bson_iter_find(&iter, handle->topic_loc); - uint32_t len; - const uint8_t *arr; - bson_iter_array(&iter, &len, &arr); - bson_t b; - - - - if (bson_init_static(&b, arr, len)) { - bson_iter_init(&iter, &b); - while (bson_iter_next(&iter)) { - - char *str = bson_iter_dup_utf8(&iter, &len); - - mosquitto_topic_matches_sub(str, topic, &check); - if (check) { - match = 1; - bson_free(str); - break; - } - bson_free(str); - } - } - - } + if (!mongoc_cursor_error (cursor, &error) && + mongoc_cursor_next (cursor, &doc)) { - } + bson_iter_init(&iter, doc); + bson_iter_find(&iter, handle->topic_loc); + + topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); + foundFlag = true; + } if ( (mongoc_cursor_error (cursor, &error)) && (match != 1) ) { fprintf (stderr, "Cursor Failure: %s\n", error.message); @@ -331,6 +282,61 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co bson_destroy(&query); mongoc_cursor_destroy (cursor); mongoc_collection_destroy(collection); + + + + if (foundFlag) { + bson_init(&query); + bson_append_int64(&query, handle->topicId_loc, -1, topId); + collection = mongoc_client_get_collection(handle->client, handle->database, handle->topics_coll); + cursor = mongoc_collection_find(collection, + MONGOC_QUERY_NONE, + 0, + 0, + 0, + &query, + NULL, + NULL); + + + if (!mongoc_cursor_error (cursor, &error) && + mongoc_cursor_next(cursor, &doc)) { + + bson_iter_init(&iter, doc); + bson_iter_find(&iter, handle->topic_loc); + uint32_t len; + const uint8_t *arr; + bson_iter_array(&iter, &len, &arr); + bson_t b; + + + if (bson_init_static(&b, arr, len)) { + bson_iter_init(&iter, &b); + while (bson_iter_next(&iter)) { + + char *str = bson_iter_dup_utf8(&iter, &len); + mosquitto_topic_matches_sub(str, topic, &check); + if (check) { + match = 1; + bson_free(str); + break; + } + bson_free(str); + } + } + + } + + if ( (mongoc_cursor_error (cursor, &error)) && (match != 1) ) { + fprintf (stderr, "Cursor Failure: %s\n", error.message); + } + + bson_destroy(&query); + mongoc_cursor_destroy(cursor); + mongoc_collection_destroy(collection); + + } + return match; } From b57a368773f4a84a4a4d3a87c3ed6330c3c8e0e0 Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Fri, 6 Jan 2017 00:44:08 +0100 Subject: [PATCH 2/8] fixed wrong usage of mongoc_cursor_more causing infinite loops --- be-mongo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/be-mongo.c b/be-mongo.c index a3c99c96..0a308123 100644 --- a/be-mongo.c +++ b/be-mongo.c @@ -247,7 +247,7 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co bson_iter_t iter; bool check = false, foundFlag = false; - int match = 0; + int match = 0, topId = 0; bson_t query; From 085162bb8f7acfb1c54d673db2f86d867f6a9efb Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Fri, 6 Jan 2017 01:32:41 +0100 Subject: [PATCH 3/8] compatibility with libmongoc > 1.3.1 --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 6adfbe87..3bf4f04b 100644 --- a/Makefile +++ b/Makefile @@ -93,6 +93,7 @@ ifneq ($(BACKEND_MONGO), no) BE_CFLAGS += -I/usr/local/include/ BE_CFLAGS += -I/usr/local/include/libmongoc-1.0/ BE_CFLAGS += -I/usr/local/include/libbson-1.0/ + BE_CFLAGS += -Wno-deprecated-declarations BE_LDFLAGS += -L/usr/local/lib BE_LDADD += -lmongoc-1.0 -lbson-1.0 OBJS += be-mongo.o From fff09583f0215c442baad2ea54f5e8d13c5be8a8 Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Mon, 27 Feb 2017 22:56:50 +0100 Subject: [PATCH 4/8] changed topId from int64 to oid --- be-mongo.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/be-mongo.c b/be-mongo.c index 0a308123..426e0437 100644 --- a/be-mongo.c +++ b/be-mongo.c @@ -270,8 +270,9 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co bson_iter_init(&iter, doc); bson_iter_find(&iter, handle->topic_loc); - - topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); + //http://mongoc.org/libbson/1.0.2/bson_oid_t.html + //topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); + topId = (bson_oid_t) bson_iter_oid(&iter);//, NULL); foundFlag = true; } @@ -287,7 +288,7 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co if (foundFlag) { bson_init(&query); - bson_append_int64(&query, handle->topicId_loc, -1, topId); + bson_append_oid(&query, handle->topicId_loc, -1, topId); collection = mongoc_client_get_collection(handle->client, handle->database, handle->topics_coll); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, From 5697146e572f2edf5952dabbb9c187e06025c8e7 Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Mon, 27 Feb 2017 23:15:14 +0100 Subject: [PATCH 5/8] changed topId from int64 to oid --- be-mongo.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/be-mongo.c b/be-mongo.c index 426e0437..30ead9c2 100644 --- a/be-mongo.c +++ b/be-mongo.c @@ -247,7 +247,8 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co bson_iter_t iter; bool check = false, foundFlag = false; - int match = 0, topId = 0; + int match = 0; + bson_oid_t *topId; bson_t query; @@ -288,7 +289,7 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co if (foundFlag) { bson_init(&query); - bson_append_oid(&query, handle->topicId_loc, -1, topId); + bson_append_oid(&query, handle->topicId_loc, -1, &topId); collection = mongoc_client_get_collection(handle->client, handle->database, handle->topics_coll); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, From 3ff8532bca98765e6d21121ccde89dddaff7dad8 Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Mon, 27 Feb 2017 23:16:44 +0100 Subject: [PATCH 6/8] changed topId from int64 to oid --- be-mongo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/be-mongo.c b/be-mongo.c index 30ead9c2..284366a3 100644 --- a/be-mongo.c +++ b/be-mongo.c @@ -273,7 +273,7 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co bson_iter_find(&iter, handle->topic_loc); //http://mongoc.org/libbson/1.0.2/bson_oid_t.html //topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); - topId = (bson_oid_t) bson_iter_oid(&iter);//, NULL); + topId = bson_iter_oid(&iter);//, NULL); foundFlag = true; } From 19a4278efb74338da98951f25c9737fc840c3972 Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Mon, 27 Feb 2017 23:21:03 +0100 Subject: [PATCH 7/8] changed topId from int64 to oid --- be-mongo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/be-mongo.c b/be-mongo.c index 284366a3..50bb5614 100644 --- a/be-mongo.c +++ b/be-mongo.c @@ -248,7 +248,7 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co bool check = false, foundFlag = false; int match = 0; - bson_oid_t *topId; + const bson_oid_t *topId; bson_t query; From 0cb1816a79887929a1733a2bf816733d38ffc293 Mon Sep 17 00:00:00 2001 From: Stefano Terna Date: Mon, 27 Feb 2017 23:26:48 +0100 Subject: [PATCH 8/8] changed topId from int64 to oid --- be-mongo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/be-mongo.c b/be-mongo.c index 50bb5614..cac85582 100644 --- a/be-mongo.c +++ b/be-mongo.c @@ -289,7 +289,7 @@ int be_mongo_aclcheck(void *conf, const char *clientid, const char *username, co if (foundFlag) { bson_init(&query); - bson_append_oid(&query, handle->topicId_loc, -1, &topId); + bson_append_oid(&query, handle->topicId_loc, -1, topId); collection = mongoc_client_get_collection(handle->client, handle->database, handle->topics_coll); cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE,