Skip to content

Commit

Permalink
Added support for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
cnelson committed May 31, 2012
1 parent be89ba2 commit 2634555
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions samples/cdr_mongodb.conf
Expand Up @@ -3,3 +3,7 @@ hostname=127.0.0.1
port=27017
dbname=asterisk
collection=cdr
;you can optionally set a username and password
;if your mongodb requires authentication
;username=someguy
;password=omg916
25 changes: 22 additions & 3 deletions src/cdr_mongodb.c
Expand Up @@ -59,7 +59,7 @@ static char *desc = "MongoDB CDR Backend";
static char *name = "mongodb";
static char *config = "cdr_mongodb.conf";

static struct ast_str *hostname = NULL, *dbname = NULL, *dbcollection = NULL, *dbnamespace = NULL;
static struct ast_str *hostname = NULL, *dbname = NULL, *dbcollection = NULL, *dbnamespace = NULL, *dbuser = NULL, *password = NULL;

static int dbport = 0;
static int connected = 0;
Expand Down Expand Up @@ -100,6 +100,14 @@ static int mongodb_log(struct ast_cdr *cdr)
return -1;
}

if (ast_str_strlen(dbuser) != 0 && (mongo_cmd_authenticate(&conn, ast_str_buffer(dbname), ast_str_buffer(dbuser), ast_str_buffer(password)) != MONGO_OK)) {
mongo_destroy( &conn );
ast_log(LOG_ERROR, "Method: mongodb_log, MongoDB failed to authenticate to do %s with username %s!\n", ast_str_buffer(dbname), ast_str_buffer(dbuser));
connected = 0;
records = 0;
return -1;
}

ast_debug(1, "mongodb: Locking mongodb_lock.\n");
ast_mutex_lock(&mongodb_lock);

Expand Down Expand Up @@ -303,6 +311,8 @@ static int _load_module(int reload)
res |= load_config_string(cfg, "global", "dbname", &dbname, "astriskcdrdb");
res |= load_config_string(cfg, "global", "collection", &dbcollection, "cdr");
res |= load_config_number(cfg, "global", "port", &dbport, 27017);
res |= load_config_string(cfg, "global", "username", &dbuser, "");
res |= load_config_string(cfg, "global", "password", &password, "");

if (res < 0) {
return AST_MODULE_LOAD_FAILURE;
Expand All @@ -312,15 +322,24 @@ static int _load_module(int reload)
ast_debug(1, "Got port of %d\n", dbport);
ast_debug(1, "Got dbname of %s\n", ast_str_buffer(dbname));
ast_debug(1, "Got dbcollection of %s\n", ast_str_buffer(dbcollection));

ast_debug(1, "Got user of %s\n", ast_str_buffer(dbuser));
ast_debug(1, "Got password of %s\n", ast_str_buffer(password));


dbnamespace = ast_str_create(255);
ast_str_set(&dbnamespace, 0, "%s.%s", ast_str_buffer(dbname), ast_str_buffer(dbcollection));

if (mongo_connect(&conn , ast_str_buffer(hostname), dbport) != MONGO_OK) {
ast_log(LOG_ERROR, "Method: _load_module, MongoDB failed to connect to %s:%d!\n", ast_str_buffer(hostname), dbport);
res = -1;
} else {
connected = 1;
if (ast_str_strlen(dbuser) != 0 && (mongo_cmd_authenticate(&conn, ast_str_buffer(dbname), ast_str_buffer(dbuser), ast_str_buffer(password)) != MONGO_OK)) {
ast_log(LOG_ERROR, "Method: _load_module, MongoDB failed to authenticate to do %s with username %s!\n", ast_str_buffer(dbname), ast_str_buffer(dbuser));
res = -1;
} else {
connected = 1;
}

mongo_destroy(&conn);
}

Expand Down

0 comments on commit 2634555

Please sign in to comment.