From 52de6ef7ea4e57e94f60cb2a91c366dc8a1315b8 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 25 May 2018 14:20:54 -0400 Subject: [PATCH 1/3] delete authenticator when destructing. reduce memleak --- src/zm_remote_camera.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/zm_remote_camera.cpp b/src/zm_remote_camera.cpp index 0acd4e709..25a963a17 100644 --- a/src/zm_remote_camera.cpp +++ b/src/zm_remote_camera.cpp @@ -55,6 +55,10 @@ RemoteCamera::~RemoteCamera() { freeaddrinfo(hp); hp = NULL; } + if ( mAuthenticator ) { + delete mAuthenticator; + mAuthenticator = NULL; + } } void RemoteCamera::Initialise() { From dd8e4da9ba14d1ccd12c7dbd6d00d3565187f2bb Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 25 May 2018 14:21:06 -0400 Subject: [PATCH 2/3] google code style --- src/zm_rtsp_auth.cpp | 59 +++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/src/zm_rtsp_auth.cpp b/src/zm_rtsp_auth.cpp index 9b91ff65c..81aa13c27 100644 --- a/src/zm_rtsp_auth.cpp +++ b/src/zm_rtsp_auth.cpp @@ -27,15 +27,14 @@ namespace zm { Authenticator::Authenticator( const std::string &username, const std::string &password) : - fCnonce( "0a4f113b" ), + fCnonce("0a4f113b"), fUsername(username), fPassword(password) { #ifdef HAVE_GCRYPT_H // Special initialisation for libgcrypt - if ( !gcry_check_version( GCRYPT_VERSION ) ) - { - Fatal( "Unable to initialise libgcrypt" ); + if ( !gcry_check_version(GCRYPT_VERSION) ) { + Fatal("Unable to initialise libgcrypt"); } gcry_control( GCRYCTL_DISABLE_SECMEM, 0 ); gcry_control( GCRYCTL_INITIALIZATION_FINISHED, 0 ); @@ -64,36 +63,34 @@ void Authenticator::authHandleHeader(std::string headerData) size_t digest_match_len = strlen(digest_match); // Check if basic auth - if (strncasecmp(headerData.c_str(),basic_match,strlen(basic_match)) == 0) - { + if ( strncasecmp(headerData.c_str(),basic_match,strlen(basic_match)) == 0 ) { fAuthMethod = AUTH_BASIC; - Debug( 2, "Set authMethod to Basic"); + Debug(2, "Set authMethod to Basic"); } // Check if digest auth - else if (strncasecmp( headerData.c_str(),digest_match,digest_match_len ) == 0) - { + else if (strncasecmp( headerData.c_str(),digest_match,digest_match_len ) == 0) { fAuthMethod = AUTH_DIGEST; Debug( 2, "Set authMethod to Digest"); StringVector subparts = split(headerData.substr(digest_match_len, headerData.length() - digest_match_len), ","); // subparts are key="value" - for ( size_t i = 0; i < subparts.size(); i++ ) - { - StringVector kvPair = split( trimSpaces( subparts[i] ), "=" ); - std::string key = trimSpaces( kvPair[0] ); - if (key == "realm") { - fRealm = trimSet( kvPair[1], "\""); + for ( size_t i = 0; i < subparts.size(); i++ ) { + StringVector kvPair = split(trimSpaces(subparts[i]), "="); + std::string key = trimSpaces(kvPair[0]); + if ( key == "realm" ) { + fRealm = trimSet(kvPair[1], "\""); continue; } - if (key == "nonce") { - fNonce = trimSet( kvPair[1], "\""); + if ( key == "nonce" ) { + fNonce = trimSet(kvPair[1], "\""); continue; } - if (key == "qop") { - fQop = trimSet( kvPair[1], "\""); + if ( key == "qop" ) { + fQop = trimSet(kvPair[1], "\""); continue; } } - Debug( 2, "Auth data completed. User: %s, realm: %s, nonce: %s, qop: %s", username().c_str(), fRealm.c_str(), fNonce.c_str(), fQop.c_str() ); + Debug(2, "Auth data completed. User: %s, realm: %s, nonce: %s, qop: %s", + username().c_str(), fRealm.c_str(), fNonce.c_str(), fQop.c_str()); } } @@ -103,12 +100,9 @@ std::string Authenticator::quote( const std::string &src ) { std::string Authenticator::getAuthHeader(std::string method, std::string uri) { std::string result = "Authorization: "; - if (fAuthMethod == AUTH_BASIC) - { + if (fAuthMethod == AUTH_BASIC) { result += "Basic " + base64Encode( username() + ":" + password() ); - } - else if (fAuthMethod == AUTH_DIGEST) - { + } else if (fAuthMethod == AUTH_DIGEST) { result += std::string("Digest ") + "username=\"" + quote(username()) + "\", realm=\"" + quote(realm()) + "\", " + "nonce=\"" + quote(nonce()) + "\", uri=\"" + quote(uri) + "\""; @@ -153,8 +147,7 @@ std::string Authenticator::computeDigestResponse(std::string &method, std::strin gnutls_datum_t md5dataha1 = { (unsigned char*)ha1Data.c_str(), ha1Data.length() }; gnutls_fingerprint( GNUTLS_DIG_MD5, &md5dataha1, md5buf, &md5len ); #endif - for ( unsigned int j = 0; j < md5len; j++ ) - { + for ( unsigned int j = 0; j < md5len; j++ ) { sprintf(&md5HexBuf[2*j], "%02x", md5buf[j] ); } md5HexBuf[md5len*2]='\0'; @@ -169,8 +162,7 @@ std::string Authenticator::computeDigestResponse(std::string &method, std::strin gnutls_datum_t md5dataha2 = { (unsigned char*)ha2Data.c_str(), ha2Data.length() }; gnutls_fingerprint( GNUTLS_DIG_MD5, &md5dataha2, md5buf, &md5len ); #endif - for ( unsigned int j = 0; j < md5len; j++ ) - { + for ( unsigned int j = 0; j < md5len; j++ ) { sprintf( &md5HexBuf[2*j], "%02x", md5buf[j] ); } md5HexBuf[md5len*2]='\0'; @@ -191,22 +183,21 @@ std::string Authenticator::computeDigestResponse(std::string &method, std::strin gnutls_datum_t md5datadigest = { (unsigned char*)digestData.c_str(), digestData.length() }; gnutls_fingerprint( GNUTLS_DIG_MD5, &md5datadigest, md5buf, &md5len ); #endif - for ( unsigned int j = 0; j < md5len; j++ ) - { + for ( unsigned int j = 0; j < md5len; j++ ) { sprintf( &md5HexBuf[2*j], "%02x", md5buf[j] ); } md5HexBuf[md5len*2]='\0'; return md5HexBuf; #else // HAVE_DECL_MD5 - Error( "You need to build with gnutls or openssl installed to use digest authentication" ); - return( 0 ); + Error("You need to build with gnutls or openssl installed to use digest authentication"); + return 0; #endif // HAVE_DECL_MD5 } void Authenticator::checkAuthResponse(std::string &response) { std::string authLine; - StringVector lines = split( response, "\r\n" ); + StringVector lines = split(response, "\r\n"); const char* authenticate_match = "WWW-Authenticate:"; size_t authenticate_match_len = strlen(authenticate_match); From 8792190f75f32643f2c49c5389257de86efebf01 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 25 May 2018 14:21:41 -0400 Subject: [PATCH 3/3] use 0 for return code when exiting cleanly --- src/zmc.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/zmc.cpp b/src/zmc.cpp index df116eaea..77b88ddc6 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -366,5 +366,5 @@ int main(int argc, char *argv[]) { logTerm(); zmDbClose(); - return result; + return zm_terminate ? 0 : result; }