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 diff --git a/be-mongo.c b/be-mongo.c index bba3c851..cac85582 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,9 @@ 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; + const bson_oid_t *topId; bson_t query; @@ -268,61 +266,16 @@ 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); + //http://mongoc.org/libbson/1.0.2/bson_oid_t.html + //topId = (int64_t) bson_iter_as_int64(&iter);//, NULL); + topId = bson_iter_oid(&iter);//, NULL); + foundFlag = true; + } if ( (mongoc_cursor_error (cursor, &error)) && (match != 1) ) { fprintf (stderr, "Cursor Failure: %s\n", error.message); @@ -331,6 +284,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_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, + 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; }