various fixes plus use global db functions in logger
parent
7533d09631
commit
d3ec50aa69
|
@ -27,10 +27,11 @@ MYSQL dbconn;
|
|||
|
||||
int zmDbConnected = false;
|
||||
|
||||
void zmDbConnect()
|
||||
{
|
||||
if ( !mysql_init( &dbconn ) )
|
||||
{
|
||||
void zmDbConnect() {
|
||||
if ( zmDbConnected )
|
||||
return;
|
||||
|
||||
if ( !mysql_init( &dbconn ) ) {
|
||||
Error( "Can't initialise database connection: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
|
@ -38,48 +39,35 @@ void zmDbConnect()
|
|||
if ( mysql_options( &dbconn, MYSQL_OPT_RECONNECT, &reconnect ) )
|
||||
Fatal( "Can't set database auto reconnect option: %s", mysql_error( &dbconn ) );
|
||||
std::string::size_type colonIndex = staticConfig.DB_HOST.find( ":" );
|
||||
if ( colonIndex == std::string::npos )
|
||||
{
|
||||
if ( !mysql_real_connect( &dbconn, staticConfig.DB_HOST.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, NULL, 0 ) )
|
||||
{
|
||||
if ( colonIndex == std::string::npos ) {
|
||||
if ( !mysql_real_connect( &dbconn, staticConfig.DB_HOST.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, NULL, 0 ) ) {
|
||||
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::string dbHost = staticConfig.DB_HOST.substr( 0, colonIndex );
|
||||
std::string dbPortOrSocket = staticConfig.DB_HOST.substr( colonIndex+1 );
|
||||
if ( dbPortOrSocket[0] == '/' )
|
||||
{
|
||||
if ( !mysql_real_connect( &dbconn, NULL, staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, dbPortOrSocket.c_str(), 0 ) )
|
||||
{
|
||||
if ( dbPortOrSocket[0] == '/' ) {
|
||||
if ( !mysql_real_connect( &dbconn, NULL, staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, dbPortOrSocket.c_str(), 0 ) ) {
|
||||
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( !mysql_real_connect( &dbconn, dbHost.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, atoi(dbPortOrSocket.c_str()), NULL, 0 ) )
|
||||
{
|
||||
} else {
|
||||
if ( !mysql_real_connect( &dbconn, dbHost.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, atoi(dbPortOrSocket.c_str()), NULL, 0 ) ) {
|
||||
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( mysql_select_db( &dbconn, staticConfig.DB_NAME.c_str() ) )
|
||||
{
|
||||
if ( mysql_select_db( &dbconn, staticConfig.DB_NAME.c_str() ) ) {
|
||||
Error( "Can't select database: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
zmDbConnected = true;
|
||||
}
|
||||
|
||||
void zmDbClose()
|
||||
{
|
||||
if ( zmDbConnected )
|
||||
{
|
||||
void zmDbClose() {
|
||||
if ( zmDbConnected ) {
|
||||
mysql_close( &dbconn );
|
||||
// mysql_init() call implicitly mysql_library_init() but
|
||||
// mysql_close() does not call mysql_library_end()
|
||||
|
|
|
@ -156,6 +156,7 @@ bool EventStream::loadEventData( int event_id ) {
|
|||
else
|
||||
snprintf( event_data->path, sizeof(event_data->path), "%s/%s/%ld/%ld", staticConfig.PATH_WEB.c_str(), storage_path, event_data->monitor_id, event_data->event_id );
|
||||
}
|
||||
delete storage; storage = NULL;
|
||||
|
||||
updateFrameRate( (double)event_data->frame_count/event_data->duration );
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include "zm_config.h"
|
||||
#include "zm_utils.h"
|
||||
#include "zm_db.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
@ -333,45 +334,7 @@ Logger::Level Logger::databaseLevel( Logger::Level databaseLevel ) {
|
|||
databaseLevel = limit(databaseLevel);
|
||||
if ( mDatabaseLevel != databaseLevel ) {
|
||||
if ( databaseLevel > NOLOG && mDatabaseLevel <= NOLOG ) {
|
||||
if ( !mDbConnected ) {
|
||||
if ( !mysql_init( &mDbConnection ) ) {
|
||||
Fatal( "Can't initialise database connection: %s", mysql_error( &mDbConnection ) );
|
||||
exit( mysql_errno( &mDbConnection ) );
|
||||
}
|
||||
my_bool reconnect = 1;
|
||||
if ( mysql_options( &mDbConnection, MYSQL_OPT_RECONNECT, &reconnect ) )
|
||||
Fatal( "Can't set database auto reconnect option: %s", mysql_error( &mDbConnection ) );
|
||||
std::string::size_type colonIndex = staticConfig.DB_HOST.find( ":" );
|
||||
if ( colonIndex == std::string::npos ) {
|
||||
if ( !mysql_real_connect( &mDbConnection, staticConfig.DB_HOST.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, NULL, 0 ) ) {
|
||||
Fatal( "Can't connect to database: %s", mysql_error( &mDbConnection ) );
|
||||
exit( mysql_errno( &mDbConnection ) );
|
||||
}
|
||||
} else {
|
||||
std::string dbHost = staticConfig.DB_HOST.substr( 0, colonIndex );
|
||||
std::string dbPortOrSocket = staticConfig.DB_HOST.substr( colonIndex+1 );
|
||||
if ( dbPortOrSocket[0] == '/' ) {
|
||||
if ( !mysql_real_connect( &mDbConnection, NULL, staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, dbPortOrSocket.c_str(), 0 ) ) {
|
||||
Fatal( "Can't connect to database: %s", mysql_error( &mDbConnection ) );
|
||||
exit( mysql_errno( &mDbConnection ) );
|
||||
}
|
||||
} else {
|
||||
if ( !mysql_real_connect( &mDbConnection, dbHost.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, atoi(dbPortOrSocket.c_str()), NULL, 0 ) ) {
|
||||
Fatal( "Can't connect to database: %s", mysql_error( &mDbConnection ) );
|
||||
exit( mysql_errno( &mDbConnection ) );
|
||||
}
|
||||
}
|
||||
} // end if has colon
|
||||
unsigned long mysqlVersion = mysql_get_server_version( &mDbConnection );
|
||||
if ( mysqlVersion < 50019 )
|
||||
if ( mysql_options( &mDbConnection, MYSQL_OPT_RECONNECT, &reconnect ) )
|
||||
Fatal( "Can't set database auto reconnect option: %s", mysql_error( &mDbConnection ) );
|
||||
if ( mysql_select_db( &mDbConnection, staticConfig.DB_NAME.c_str() ) ) {
|
||||
Fatal( "Can't select database: %s", mysql_error( &mDbConnection ) );
|
||||
exit( mysql_errno( &mDbConnection ) );
|
||||
}
|
||||
mDbConnected = true;
|
||||
} // end if ! mDbConnected
|
||||
zmDbConnect();
|
||||
} // end if ( databaseLevel > NOLOG && mDatabaseLevel <= NOLOG )
|
||||
mDatabaseLevel = databaseLevel;
|
||||
} // end if ( mDatabaseLevel != databaseLevel )
|
||||
|
@ -439,10 +402,7 @@ void Logger::closeFile() {
|
|||
}
|
||||
|
||||
void Logger::closeDatabase() {
|
||||
if ( mDbConnected ) {
|
||||
mysql_close( &mDbConnection );
|
||||
mDbConnected = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Logger::openSyslog() {
|
||||
|
@ -548,13 +508,13 @@ void Logger::logPrint( bool hex, const char * const filepath, const int line, co
|
|||
char sql[ZM_SQL_MED_BUFSIZ];
|
||||
char escapedString[(strlen(syslogStart)*2)+1];
|
||||
|
||||
mysql_real_escape_string( &mDbConnection, escapedString, syslogStart, strlen(syslogStart) );
|
||||
mysql_real_escape_string( &dbconn, escapedString, syslogStart, strlen(syslogStart) );
|
||||
|
||||
snprintf( sql, sizeof(sql), "insert into Logs ( TimeKey, Component, ServerId, Pid, Level, Code, Message, File, Line ) values ( %ld.%06ld, '%s', %d, %d, %d, '%s', '%s', '%s', %d )", timeVal.tv_sec, timeVal.tv_usec, mId.c_str(), staticConfig.SERVER_ID, tid, level, classString, escapedString, file, line );
|
||||
if ( mysql_query( &mDbConnection, sql ) ) {
|
||||
if ( mysql_query( &dbconn, sql ) ) {
|
||||
Level tempDatabaseLevel = mDatabaseLevel;
|
||||
databaseLevel( NOLOG );
|
||||
Error( "Can't insert log entry: sql(%s) error(%s)", sql, mysql_error( &mDbConnection ) );
|
||||
Error( "Can't insert log entry: sql(%s) error(%s)", sql, mysql_error( &dbconn ) );
|
||||
databaseLevel(tempDatabaseLevel);
|
||||
}
|
||||
}
|
||||
|
@ -566,6 +526,8 @@ void Logger::logPrint( bool hex, const char * const filepath, const int line, co
|
|||
|
||||
free(filecopy);
|
||||
if ( level <= FATAL ) {
|
||||
logTerm();
|
||||
zmDbClose();
|
||||
if ( level <= PANIC )
|
||||
abort();
|
||||
exit( -1 );
|
||||
|
|
|
@ -27,16 +27,14 @@
|
|||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
User::User()
|
||||
{
|
||||
User::User() {
|
||||
username[0] = password[0] = 0;
|
||||
enabled = false;
|
||||
stream = events = control = monitors = system = PERM_NONE;
|
||||
monitor_ids = 0;
|
||||
}
|
||||
|
||||
User::User( MYSQL_ROW &dbrow )
|
||||
{
|
||||
User::User( MYSQL_ROW &dbrow ) {
|
||||
int index = 0;
|
||||
strncpy( username, dbrow[index++], sizeof(username) );
|
||||
strncpy( password, dbrow[index++], sizeof(password) );
|
||||
|
@ -48,22 +46,18 @@ User::User( MYSQL_ROW &dbrow )
|
|||
system = (Permission)atoi( dbrow[index++] );
|
||||
monitor_ids = 0;
|
||||
char *monitor_ids_str = dbrow[index++];
|
||||
if ( monitor_ids_str && *monitor_ids_str )
|
||||
{
|
||||
if ( monitor_ids_str && *monitor_ids_str ) {
|
||||
monitor_ids = new int[strlen(monitor_ids_str)];
|
||||
int n_monitor_ids = 0;
|
||||
const char *ptr = monitor_ids_str;
|
||||
do
|
||||
{
|
||||
do {
|
||||
int id = 0;
|
||||
while( isdigit( *ptr ) )
|
||||
{
|
||||
while( isdigit( *ptr ) ) {
|
||||
id *= 10;
|
||||
id += *ptr-'0';
|
||||
ptr++;
|
||||
}
|
||||
if ( id )
|
||||
{
|
||||
if ( id ) {
|
||||
monitor_ids[n_monitor_ids++] = id;
|
||||
if ( !*ptr )
|
||||
break;
|
||||
|
@ -75,21 +69,16 @@ User::User( MYSQL_ROW &dbrow )
|
|||
}
|
||||
}
|
||||
|
||||
User::~User()
|
||||
{
|
||||
User::~User() {
|
||||
delete monitor_ids;
|
||||
}
|
||||
|
||||
bool User::canAccess( int monitor_id )
|
||||
{
|
||||
if ( !monitor_ids )
|
||||
{
|
||||
bool User::canAccess( int monitor_id ) {
|
||||
if ( !monitor_ids ) {
|
||||
return( true );
|
||||
}
|
||||
for ( int i = 0; monitor_ids[i]; i++ )
|
||||
{
|
||||
if ( monitor_ids[i] == monitor_id )
|
||||
{
|
||||
for ( int i = 0; monitor_ids[i]; i++ ) {
|
||||
if ( monitor_ids[i] == monitor_id ) {
|
||||
return( true );
|
||||
}
|
||||
}
|
||||
|
@ -98,8 +87,7 @@ bool User::canAccess( int monitor_id )
|
|||
|
||||
// Function to load a user from username and password
|
||||
// Please note that in auth relay mode = none, password is NULL
|
||||
User *zmLoadUser( const char *username, const char *password )
|
||||
{
|
||||
User *zmLoadUser( const char *username, const char *password ) {
|
||||
char sql[ZM_SQL_SML_BUFSIZ] = "";
|
||||
char safer_username[65]; // current db username size is 32
|
||||
char safer_password[129]; // current db password size is 64
|
||||
|
@ -114,22 +102,20 @@ User *zmLoadUser( const char *username, const char *password )
|
|||
snprintf( sql, sizeof(sql), "select Username, Password, Enabled, Stream+0, Events+0, Control+0, Monitors+0, System+0, MonitorIds from Users where Username = '%s' and Enabled = 1", safer_username );
|
||||
}
|
||||
|
||||
if ( mysql_query( &dbconn, sql ) )
|
||||
{
|
||||
if ( mysql_query( &dbconn, sql ) ) {
|
||||
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
|
||||
MYSQL_RES *result = mysql_store_result( &dbconn );
|
||||
if ( !result )
|
||||
{
|
||||
if ( !result ) {
|
||||
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
int n_users = mysql_num_rows( result );
|
||||
|
||||
if ( n_users != 1 )
|
||||
{
|
||||
if ( n_users != 1 ) {
|
||||
mysql_free_result( result );
|
||||
Warning( "Unable to authenticate user %s", username );
|
||||
return( 0 );
|
||||
}
|
||||
|
@ -145,13 +131,11 @@ User *zmLoadUser( const char *username, const char *password )
|
|||
}
|
||||
|
||||
// Function to validate an authentication string
|
||||
User *zmLoadAuthUser( const char *auth, bool use_remote_addr )
|
||||
{
|
||||
User *zmLoadAuthUser( const char *auth, bool use_remote_addr ) {
|
||||
#if HAVE_DECL_MD5 || HAVE_DECL_GNUTLS_FINGERPRINT
|
||||
#ifdef HAVE_GCRYPT_H
|
||||
// Special initialisation for libgcrypt
|
||||
if ( !gcry_check_version( GCRYPT_VERSION ) )
|
||||
{
|
||||
if ( !gcry_check_version( GCRYPT_VERSION ) ) {
|
||||
Fatal( "Unable to initialise libgcrypt" );
|
||||
}
|
||||
gcry_control( GCRYCTL_DISABLE_SECMEM, 0 );
|
||||
|
@ -159,11 +143,9 @@ User *zmLoadAuthUser( const char *auth, bool use_remote_addr )
|
|||
#endif // HAVE_GCRYPT_H
|
||||
|
||||
const char *remote_addr = "";
|
||||
if ( use_remote_addr )
|
||||
{
|
||||
if ( use_remote_addr ) {
|
||||
remote_addr = getenv( "REMOTE_ADDR" );
|
||||
if ( !remote_addr )
|
||||
{
|
||||
if ( !remote_addr ) {
|
||||
Warning( "Can't determine remote address, using null" );
|
||||
remote_addr = "";
|
||||
}
|
||||
|
@ -173,28 +155,25 @@ User *zmLoadAuthUser( const char *auth, bool use_remote_addr )
|
|||
char sql[ZM_SQL_SML_BUFSIZ] = "";
|
||||
snprintf( sql, sizeof(sql), "SELECT Username, Password, Enabled, Stream+0, Events+0, Control+0, Monitors+0, System+0, MonitorIds FROM Users WHERE Enabled = 1" );
|
||||
|
||||
if ( mysql_query( &dbconn, sql ) )
|
||||
{
|
||||
if ( mysql_query( &dbconn, sql ) ) {
|
||||
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
|
||||
MYSQL_RES *result = mysql_store_result( &dbconn );
|
||||
if ( !result )
|
||||
{
|
||||
if ( !result ) {
|
||||
Error( "Can't use query result: %s", mysql_error( &dbconn ) );
|
||||
exit( mysql_errno( &dbconn ) );
|
||||
}
|
||||
int n_users = mysql_num_rows( result );
|
||||
|
||||
if ( n_users < 1 )
|
||||
{
|
||||
if ( n_users < 1 ) {
|
||||
mysql_free_result( result );
|
||||
Warning( "Unable to authenticate user" );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
while( MYSQL_ROW dbrow = mysql_fetch_row( result ) )
|
||||
{
|
||||
while( MYSQL_ROW dbrow = mysql_fetch_row( result ) ) {
|
||||
const char *user = dbrow[0];
|
||||
const char *pass = dbrow[1];
|
||||
|
||||
|
@ -204,7 +183,7 @@ User *zmLoadAuthUser( const char *auth, bool use_remote_addr )
|
|||
unsigned char md5sum[md5len];
|
||||
|
||||
time_t now = time( 0 );
|
||||
unsigned int hours =config.auth_hash_ttl;
|
||||
unsigned int hours = config.auth_hash_ttl;
|
||||
|
||||
if ( ! hours ) {
|
||||
Warning("No value set for ZM_AUTH_HASH_TTL. Defaulting to 2.");
|
||||
|
@ -234,23 +213,23 @@ User *zmLoadAuthUser( const char *auth, bool use_remote_addr )
|
|||
gnutls_fingerprint( GNUTLS_DIG_MD5, &md5data, md5sum, &md5len );
|
||||
#endif
|
||||
auth_md5[0] = '\0';
|
||||
for ( unsigned int j = 0; j < md5len; j++ )
|
||||
{
|
||||
for ( unsigned int j = 0; j < md5len; j++ ) {
|
||||
sprintf( &auth_md5[2*j], "%02x", md5sum[j] );
|
||||
}
|
||||
Debug( 1, "Checking auth_key '%s' -> auth_md5 '%s' == '%s'", auth_key, auth_md5, auth );
|
||||
|
||||
if ( !strcmp( auth, auth_md5 ) )
|
||||
{
|
||||
if ( !strcmp( auth, auth_md5 ) ) {
|
||||
// We have a match
|
||||
User *user = new User( dbrow );
|
||||
Debug(1, "Authenticated user '%s'", user->getUsername() );
|
||||
mysql_free_result( result );
|
||||
return( user );
|
||||
} else {
|
||||
Debug(1, "No match for %s", auth );
|
||||
}
|
||||
}
|
||||
}
|
||||
mysql_free_result( result );
|
||||
#else // HAVE_DECL_MD5
|
||||
Error( "You need to build with gnutls or openssl installed to use hash based authentication" );
|
||||
#endif // HAVE_DECL_MD5
|
||||
|
|
|
@ -61,6 +61,7 @@ if ( isset( $_REQUEST['streamMode'] ) )
|
|||
else
|
||||
$streamMode = 'video';
|
||||
|
||||
$replayMode = '';
|
||||
if ( isset( $_REQUEST['replayMode'] ) )
|
||||
$replayMode = validHtmlStr($_REQUEST['replayMode']);
|
||||
if ( isset( $_COOKIE['replayMode']) && preg_match('#^[a-z]+$#', $_COOKIE['replayMode']) )
|
||||
|
|
Loading…
Reference in New Issue