Commit eaa794b0 authored by DanmeiChen's avatar DanmeiChen

indentation of code, using tab

parent b2b0db49
......@@ -27,31 +27,31 @@ using namespace std;
void FileAuthDb::parsePasswd(string* pass, string user, string domain, passwd_algo_t* password){
// parse password and calcul passmd5, passsha256 if there is clrtxt pass.
int i;
for(i=0;i<3;i++){
if(pass[i].substr(0,7)=="clrtxt:"){
password->pass = pass[i].substr(7);
}
}
if(password->pass !=""){
string input;
input = user+":"+domain+":"+password->pass;
password->passmd5=syncMd5(input.c_str(), 16);
password->passsha256=syncSha256(input.c_str(), 32);
return;
}
for(i=0;i<3;i++){
if(pass[i].substr(0,4)=="md5:"){
password->passmd5 = pass[i].substr(4);
}
if(pass[i].substr(0,7)=="sha256:"){
password->passsha256 = pass[i].substr(7);
}
}
void FileAuthDb::parsePasswd(string *pass, string user, string domain, passwd_algo_t *password) {
// parse password and calcul passmd5, passsha256 if there is clrtxt pass.
int i;
for (i = 0; i < 3; i++) {
if (pass[i].substr(0, 7) == "clrtxt:") {
password->pass = pass[i].substr(7);
}
}
if (password->pass != "") {
string input;
input = user + ":" + domain + ":" + password->pass;
password->passmd5 = syncMd5(input.c_str(), 16);
password->passsha256 = syncSha256(input.c_str(), 32);
return;
}
for (i = 0; i < 3; i++) {
if (pass[i].substr(0, 4) == "md5:") {
password->passmd5 = pass[i].substr(4);
}
if (pass[i].substr(0, 7) == "sha256:") {
password->passsha256 = pass[i].substr(7);
}
}
}
FileAuthDb::FileAuthDb() {
......@@ -60,7 +60,7 @@ FileAuthDb::FileAuthDb() {
mLastSync = 0;
mFileString = ma->get<ConfigString>("datasource")->read();
sync();
sync();
}
void FileAuthDb::getUserWithPhoneFromBackend(const std::string &phone, const std::string &domain, AuthDbListener *listener) {
......@@ -76,7 +76,7 @@ void FileAuthDb::getUserWithPhoneFromBackend(const std::string &phone, const std
}
void FileAuthDb::getPasswordFromBackend(const std::string &id, const std::string &domain,
const std::string &authid, AuthDbListener *listener) {
const std::string &authid, AuthDbListener *listener) {
AuthDbResult res = AuthDbResult::PASSWORD_NOT_FOUND;
time_t now = getCurrentTime();
......@@ -86,7 +86,7 @@ void FileAuthDb::getPasswordFromBackend(const std::string &id, const std::string
string key(createPasswordKey(id, authid));
passwd_algo_t passwd;
passwd_algo_t passwd;
if (getCachedPassword(key, domain, passwd) == VALID_PASS_FOUND) {
res = AuthDbResult::PASSWORD_FOUND;
}
......@@ -94,113 +94,113 @@ void FileAuthDb::getPasswordFromBackend(const std::string &id, const std::string
}
void FileAuthDb::sync() {
LOGD("Syncing password file");
GenericStruct *cr = GenericManager::get()->getRoot();
GenericStruct *ma = cr->get<GenericStruct>("module::Authentication");
list<string> domains = ma->get<ConfigStringList>("auth-domains")->read();
mLastSync = getCurrentTime();
ifstream file;
stringstream ss;
ss.exceptions(ifstream::failbit | ifstream::badbit);
string line;
string user;
string domain;
passwd_algo_t password;
string userid;
string phone;
string pass[3];
string version;
string passwd_tag;
int i;
LOGD("Opening file %s", mFileString.c_str());
file.open(mFileString);
if (file.is_open()) {
while (file.good() && getline(file, line)) {
if (line.empty()) continue;
ss.clear();
ss.str(line);
version.clear();
getline(ss, version, ' ');
if(version.substr(0,8)=="version:")
version = version.substr(8);
else
LOGA("userdb.conf must start by version:X to be used.");
break;
}
if(version=="1"){
while (file.good() && getline(file, line)) {
if (line.empty()) continue;
ss.clear();
ss.str(line);
user.clear();
domain.clear();
pass[0].clear();
pass[1].clear();
pass[2].clear();
password.pass.clear();
password.passmd5.clear();
password.passsha256.clear();
userid.clear();
phone.clear();
try {
getline(ss, user, '@');
getline(ss, domain, ' ');
for(i=0;i<3 && (!ss.eof());i++){
passwd_tag.clear();
getline(ss, passwd_tag, ' ');
if(passwd_tag!=";")
pass[i]=strdup(passwd_tag.c_str());
else break;
}
if(passwd_tag!=";"){
if(ss.eof())
LOGA("In userdb.conf, the section of password must end with ';'");
else {
passwd_tag.clear();
getline(ss, passwd_tag, ' ');
if((!ss.eof())&&(passwd_tag!=";"))
LOGA("In userdb.conf, the section of password must end with ';'");
}
}
if (!ss.eof()) {
getline(ss, userid, ' ');
if (!ss.eof()) {
getline(ss, phone);
} else {
phone = "";
}
} else {
userid = user;
phone = "";
}
cacheUserWithPhone(phone, domain, user);
parsePasswd(pass,user,domain,&password);
if (find(domains.begin(), domains.end(), domain) != domains.end()) {
string key(createPasswordKey(user, userid));
cachePassword(key, domain, password, mCacheExpire);
} else if (find(domains.begin(), domains.end(), "*") != domains.end()) {
string key(createPasswordKey(user, userid));
cachePassword(key, domain, password, mCacheExpire);
} else {
LOGW("Not handled domain: %s", domain.c_str());
}
} catch (const stringstream::failure &e) {
LOGW("Incorrect line format: %s (error: %s)", line.c_str(), e.what());
}
}
} else {
LOGE("Version %s is not supported",version.c_str());
}
} else {
LOGE("Can't open file %s", mFileString.c_str());
}
LOGD("Syncing done");
LOGD("Syncing password file");
GenericStruct *cr = GenericManager::get()->getRoot();
GenericStruct *ma = cr->get<GenericStruct>("module::Authentication");
list<string> domains = ma->get<ConfigStringList>("auth-domains")->read();
mLastSync = getCurrentTime();
ifstream file;
stringstream ss;
ss.exceptions(ifstream::failbit | ifstream::badbit);
string line;
string user;
string domain;
passwd_algo_t password;
string userid;
string phone;
string pass[3];
string version;
string passwd_tag;
int i;
LOGD("Opening file %s", mFileString.c_str());
file.open(mFileString);
if (file.is_open()) {
while (file.good() && getline(file, line)) {
if (line.empty()) continue;
ss.clear();
ss.str(line);
version.clear();
getline(ss, version, ' ');
if (version.substr(0, 8) == "version:")
version = version.substr(8);
else
LOGA("userdb.conf must start by version:X to be used.");
break;
}
if (version == "1") {
while (file.good() && getline(file, line)) {
if (line.empty()) continue;
ss.clear();
ss.str(line);
user.clear();
domain.clear();
pass[0].clear();
pass[1].clear();
pass[2].clear();
password.pass.clear();
password.passmd5.clear();
password.passsha256.clear();
userid.clear();
phone.clear();
try {
getline(ss, user, '@');
getline(ss, domain, ' ');
for (i = 0; i < 3 && (!ss.eof()); i++) {
passwd_tag.clear();
getline(ss, passwd_tag, ' ');
if (passwd_tag != ";")
pass[i] = strdup(passwd_tag.c_str());
else break;
}
if (passwd_tag != ";") {
if (ss.eof())
LOGA("In userdb.conf, the section of password must end with ';'");
else {
passwd_tag.clear();
getline(ss, passwd_tag, ' ');
if ((!ss.eof()) && (passwd_tag != ";"))
LOGA("In userdb.conf, the section of password must end with ';'");
}
}
if (!ss.eof()) {
getline(ss, userid, ' ');
if (!ss.eof()) {
getline(ss, phone);
} else {
phone = "";
}
} else {
userid = user;
phone = "";
}
cacheUserWithPhone(phone, domain, user);
parsePasswd(pass, user, domain, &password);
if (find(domains.begin(), domains.end(), domain) != domains.end()) {
string key(createPasswordKey(user, userid));
cachePassword(key, domain, password, mCacheExpire);
} else if (find(domains.begin(), domains.end(), "*") != domains.end()) {
string key(createPasswordKey(user, userid));
cachePassword(key, domain, password, mCacheExpire);
} else {
LOGW("Not handled domain: %s", domain.c_str());
}
} catch (const stringstream::failure &e) {
LOGW("Incorrect line format: %s (error: %s)", line.c_str(), e.what());
}
}
} else {
LOGE("Version %s is not supported", version.c_str());
}
} else {
LOGE("Can't open file %s", mFileString.c_str());
}
LOGD("Syncing done");
}
This diff is collapsed.
......@@ -43,89 +43,89 @@
enum AuthDbResult { PENDING, PASSWORD_FOUND, PASSWORD_NOT_FOUND, AUTH_ERROR };
struct passwd_algo_t {
std::string pass;
std::string passmd5;
std::string passsha256;
std::string pass;
std::string passmd5;
std::string passsha256;
};
// Fw declaration
struct AuthDbTimings;
class AuthDbListener : public StatFinishListener {
public:
public:
virtual void onResult(AuthDbResult result, const std::string &passwd) = 0;
virtual void onResult(AuthDbResult result, const passwd_algo_t &passwd)=0;
virtual void onResult(AuthDbResult result, const passwd_algo_t &passwd) = 0;
virtual void onResults(std::list<std::string> &phones, std::set<std::string> &users);
virtual ~AuthDbListener();
};
class AuthDbBackend {
static AuthDbBackend *sUnique;
struct CachedPassword {
passwd_algo_t pass;
time_t expire_date;
CachedPassword(const passwd_algo_t &ipass, time_t idate) : pass(ipass), expire_date(idate) {
}
};
private:
struct CachedPassword {
passwd_algo_t pass;
time_t expire_date;
CachedPassword(const passwd_algo_t &ipass, time_t idate) : pass(ipass), expire_date(idate) {
}
};
private:
std::map<std::string, std::map<std::string, CachedPassword>> mCachedPasswords;
std::mutex mCachedPasswordMutex;
std::mutex mCachedUserWithPhoneMutex;
std::map<std::string, std::string> mPhone2User;
protected:
protected:
AuthDbBackend();
enum CacheResult { VALID_PASS_FOUND, EXPIRED_PASS_FOUND, NO_PASS_FOUND };
std::string createPasswordKey(const std::string &user, const std::string &auth);
bool cachePassword(const std::string &key, const std::string &domain, const passwd_algo_t &pass, int expires);
bool cachePassword(const std::string &key, const std::string &domain, const passwd_algo_t &pass, int expires);
bool cacheUserWithPhone(const std::string &phone, const std::string &domain, const std::string &user);
CacheResult getCachedPassword(const std::string &key, const std::string &domain, passwd_algo_t &pass);
CacheResult getCachedPassword(const std::string &key, const std::string &domain, passwd_algo_t &pass);
CacheResult getCachedUserWithPhone(const std::string &phone, const std::string &domain, std::string &user);
void createCachedAccount(const std::string & user, const std::string & domain, const std::string &auth_username, const passwd_algo_t &password, int expires, const std::string & phone_alias = "");
void createCachedAccount(const std::string &user, const std::string &domain, const std::string &auth_username, const passwd_algo_t &password, int expires, const std::string &phone_alias = "");
void clearCache();
int mCacheExpire;
public:
public:
virtual ~AuthDbBackend();
// warning: listener may be invoked on authdb backend thread, so listener must be threadsafe somehow!
void getPassword(const std::string & user, const std::string & domain, const std::string &auth_username, AuthDbListener *listener);
void getPasswordForAlgo(const std::string &user, const std::string &host, const std::string &auth_username,
AuthDbListener *listener, std::list<std::string> &list_algorithm);
void getPassword(const std::string &user, const std::string &domain, const std::string &auth_username, AuthDbListener *listener);
void getPasswordForAlgo(const std::string &user, const std::string &host, const std::string &auth_username,
AuthDbListener *listener, std::list<std::string> &list_algorithm);
void getUserWithPhone(const std::string &phone, const std::string &domain, AuthDbListener *listener);
void getUsersWithPhone(std::list<std::tuple<std::string,std::string,AuthDbListener *>> & creds, AuthDbListener *listener);
void getUsersWithPhone(std::list<std::tuple<std::string, std::string, AuthDbListener *>> &creds, AuthDbListener *listener);
virtual void getUserWithPhoneFromBackend(const std::string &, const std::string &, AuthDbListener *listener) = 0;
virtual void getUsersWithPhonesFromBackend(std::list<std::tuple<std::string,std::string,AuthDbListener*>> &creds, AuthDbListener *listener);
virtual void getUsersWithPhonesFromBackend(std::list<std::tuple<std::string, std::string, AuthDbListener *>> &creds, AuthDbListener *listener);
virtual void createAccount(const std::string &user, const std::string & domain, const std::string &auth_username, const std::string &password, int expires, const std::string &phone_alias = "");
virtual void createAccount(const std::string &user, const std::string &domain, const std::string &auth_username, const std::string &password, int expires, const std::string &phone_alias = "");
virtual void getPasswordFromBackend(const std::string &id, const std::string &domain,
const std::string &authid, AuthDbListener *listener) = 0;
const std::string &authid, AuthDbListener *listener) = 0;
static AuthDbBackend *get();
/* called by module_auth so that backends can declare their configuration to the ConfigurationManager */
static void declareConfig(GenericStruct *mc);
static std::string syncSha256(const char* input,size_t size);
static std::string syncMd5(const char* input,size_t size);
static std::string syncSha256(const char *input, size_t size);
static std::string syncMd5(const char *input, size_t size);
};
class FileAuthDb : public AuthDbBackend {
private:
private:
std::string mFileString;
time_t mLastSync;
void parsePasswd(std::string* pass, std::string user, std::string domain, passwd_algo_t* password);
void parsePasswd(std::string *pass, std::string user, std::string domain, passwd_algo_t *password);
protected:
protected:
void sync();
public:
public:
FileAuthDb();
virtual void getUserWithPhoneFromBackend(const std::string &phone, const std::string & domain, AuthDbListener *listener);
virtual void getUserWithPhoneFromBackend(const std::string &phone, const std::string &domain, AuthDbListener *listener);
virtual void getPasswordFromBackend(const std::string &id, const std::string &domain,
const std::string &authid, AuthDbListener *listener);
const std::string &authid, AuthDbListener *listener);
static void declareConfig(GenericStruct *mc){};
static void declareConfig(GenericStruct *mc) {};
};
#if ENABLE_ODBC
......
......@@ -57,16 +57,15 @@ static char* cJSON_strdup(const char* str)
return copy;
}
void cJSON_InitHooks(cJSON_Hooks* hooks)
{
if (!hooks) { /* Reset hooks */
cJSON_malloc = malloc;
cJSON_free = free;
return;
}
cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
void cJSON_InitHooks(cJSON_Hooks *hooks) {
if (!hooks) { /* Reset hooks */
cJSON_malloc = malloc;
cJSON_free = free;
return;
}
cJSON_malloc = (hooks->malloc_fn) ? hooks->malloc_fn : malloc;
cJSON_free = (hooks->free_fn) ? hooks->free_fn : free;
}
/* Internal constructor. */
......
This diff is collapsed.
......@@ -29,18 +29,18 @@ public:
belle_sip_object_unref(timer);
}
virtual void onResult(AuthDbResult result, const passwd_algo_t &passwd) {
belle_sip_source_cpp_func_t *func = new belle_sip_source_cpp_func_t([this, result, passwd](unsigned int events) {
this->processResponse(result, passwd.pass);
return BELLE_SIP_STOP;
});
belle_sip_source_t *timer = belle_sip_main_loop_create_cpp_timeout( mMainLoop
, func
, 0
, "OnAuthListener to mainthread");
belle_sip_object_unref(timer);
}
virtual void onResult(AuthDbResult result, const passwd_algo_t &passwd) {
belle_sip_source_cpp_func_t *func = new belle_sip_source_cpp_func_t([this, result, passwd](unsigned int events) {
this->processResponse(result, passwd.pass);
return BELLE_SIP_STOP;
});
belle_sip_source_t *timer = belle_sip_main_loop_create_cpp_timeout(mMainLoop
, func
, 0
, "OnAuthListener to mainthread");
belle_sip_object_unref(timer);
}
void onResults(list<std::string> &phones, set<std::string> &users) {
for(std::string phone : phones) {
if(users.size() == 0) {
......
......@@ -94,32 +94,32 @@ static void redisSofiaCleanup(void *privdata) {
}
static int redisSofiaAttach(redisAsyncContext *ac, su_root_t *root) {
redisContext *c = &(ac->c);
redisSofiaEvents *e;
/* Nothing should be attached when something is already attached */
if (ac->ev.data != NULL)
return REDIS_ERR;
/* Create container for context and r/w events */
e = (redisSofiaEvents*)malloc(sizeof(*e));
e->context = ac;
e->root = root;
/* Register functions to start/stop listening for events */
ac->ev.addRead = redisSofiaAddRead;
ac->ev.delRead = redisSofiaDelRead;
ac->ev.addWrite = redisSofiaAddWrite;
ac->ev.delWrite = redisSofiaDelWrite;
ac->ev.cleanup = redisSofiaCleanup;
ac->ev.data = e;
/* Initialize and install read/write events */
if (0!=su_wait_create(&e->wait, c->fd,SU_WAIT_IN|SU_WAIT_OUT)){
return REDIS_ERR;
}
e->index=su_root_register(root, &e->wait, redisSofiaEvent, e, su_pri_normal);
return REDIS_OK;
redisContext *c = &(ac->c);
redisSofiaEvents *e;
/* Nothing should be attached when something is already attached */
if (ac->ev.data != NULL)
return REDIS_ERR;
/* Create container for context and r/w events */
e = (redisSofiaEvents *)malloc(sizeof(*e));
e->context = ac;
e->root = root;
/* Register functions to start/stop listening for events */
ac->ev.addRead = redisSofiaAddRead;
ac->ev.delRead = redisSofiaDelRead;
ac->ev.addWrite = redisSofiaAddWrite;
ac->ev.delWrite = redisSofiaDelWrite;
ac->ev.cleanup = redisSofiaCleanup;
ac->ev.data = e;
/* Initialize and install read/write events */
if (0 != su_wait_create(&e->wait, c->fd, SU_WAIT_IN | SU_WAIT_OUT)) {
return REDIS_ERR;
}
e->index = su_root_register(root, &e->wait, redisSofiaEvent, e, su_pri_normal);
return REDIS_OK;
}
#endif
......@@ -239,67 +239,60 @@ getMessage( Socket fd, char* buf, int* len,
return FALSE;
}
if (err==0)
{
ortp_error("stun_udp: Connection timeout with stun server!");
*len = 0;
return FALSE;
}
if (FD_ISSET (fd, &fdSet))
{
*len = recvfrom(fd,
buf,
originalSize,
0,
(struct sockaddr *)&from,
(socklen_t*)&fromLen);
if ( *len == SOCKET_ERROR )
{
int e = getErrno();
switch (e)
{
case ENOTSOCK:
ortp_error("stun_udp: Error fd not a socket");
break;
case ECONNRESET:
ortp_error("stun_udp: Error connection reset - host not reachable");
break;
default:
ortp_error("stun_udp: Socket Error=%i", e);
}
return FALSE;
}
if ( *len < 0 )
{
ortp_error("stun_udp: socket closed? negative len");
return FALSE;
}
if ( *len == 0 )
{
ortp_error("stun_udp: socket closed? zero len");
return FALSE;
}
*srcPort = ntohs(from.sin_port);
*srcIp = ntohl(from.sin_addr.s_addr);
if ( (*len)+1 >= originalSize )
{
ortp_error("stun_udp: Received a message that was too large");
return FALSE;
}
buf[*len]=0;
return TRUE;
}
return FALSE;
if (err == 0) {
ortp_error("stun_udp: Connection timeout with stun server!");
*len = 0;
return FALSE;
}
if (FD_ISSET(fd, &fdSet)) {
*len = recvfrom(fd,
buf,
originalSize,
0,
(struct sockaddr *)&from,
(socklen_t *)&fromLen);
if (*len == SOCKET_ERROR) {
int e = getErrno();
switch (e) {
case ENOTSOCK:
ortp_error("stun_udp: Error fd not a socket");
break;
case ECONNRESET:
ortp_error("stun_udp: Error connection reset - host not reachable");
break;
default:
ortp_error("stun_udp: Socket Error=%i", e);
}
return FALSE;
}
if (*len < 0) {
ortp_error("stun_udp: socket closed? negative len");
return FALSE;
}
if (*len == 0) {
ortp_error("stun_udp: socket closed? zero len");
return FALSE;
}
*srcPort = ntohs(from.sin_port);
*srcIp = ntohl(from.sin_addr.s_addr);
if ((*len) + 1 >= originalSize) {
ortp_error("stun_udp: Received a message that was too large");
return FALSE;
}
buf[*len] = 0;
return TRUE;
}
return FALSE;
}
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment