Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #188 from iottly/master
Browse files Browse the repository at this point in the history
mongo backend fixes
  • Loading branch information
jpmens authored Mar 6, 2017
2 parents 34886c2 + 0cb1816 commit 7a8caca
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 77 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
162 changes: 85 additions & 77 deletions be-mongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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;

}
Expand Down

0 comments on commit 7a8caca

Please sign in to comment.