diff --git a/ComHTTP/HTTPConfig.cpp b/ComHTTP/HTTPConfig.cpp index c03fe276..32912e6b 100644 --- a/ComHTTP/HTTPConfig.cpp +++ b/ComHTTP/HTTPConfig.cpp @@ -1,412 +1,412 @@ -//==================================================================================== -// Open Computer and Software Inventory Next Generation -// Copyright (C) 2010 OCS Inventory NG Team. All rights reserved. -// Web: http://www.ocsinventory-ng.org - -// This code is open source and may be copied and modified as long as the source -// code is always made freely available. -// Please refer to the General Public Licence V2 http://www.gnu.org/ or Licence.txt -//==================================================================================== - -// HTTPConfig.cpp: implementation of the CHTTPConfig class. -// -////////////////////////////////////////////////////////////////////// - -#include "stdafx.h" -#include "HTTPConfig.h" - +//==================================================================================== +// Open Computer and Software Inventory Next Generation +// Copyright (C) 2010 OCS Inventory NG Team. All rights reserved. +// Web: http://www.ocsinventory-ng.org + +// This code is open source and may be copied and modified as long as the source +// code is always made freely available. +// Please refer to the General Public Licence V2 http://www.gnu.org/ or Licence.txt +//==================================================================================== + +// HTTPConfig.cpp: implementation of the CHTTPConfig class. +// +////////////////////////////////////////////////////////////////////// + +#include "stdafx.h" +#include "HTTPConfig.h" + #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif - -////////////////////////////////////////////////////////////////////// -// Construction/Destruction -////////////////////////////////////////////////////////////////////// - -CHTTPConfig::CHTTPConfig( LPCTSTR lpstrFile, LPCTSTR lpstrSection) -{ - Clear(); - // Load default configuration from default file - load( lpstrFile, lpstrSection); -} - -CHTTPConfig::~CHTTPConfig() -{ -} - -void CHTTPConfig::Clear() -{ - // Server address - m_csServerName = OCS_HTTP_DEFAULT_SERVER; - - // SSL connection required - m_uSSL = OCS_HTTP_SSL_VALIDATION_REQUIRED; - // Path to CA certificate chain file in PEM format - m_csCaBundle.Format( _T( "%s\\%s"), getDataFolder(), OCS_HTTP_CA_DEFAULT_BUNDLE); - - // HTTP credentials if needed - m_bAuthRequired = OCS_HTTP_AUTH_TYPE_NONE; - m_csHttpUser.Empty(); - m_csHttpPwd.Empty(); - - // Proxy type - m_uProxyType = OCS_HTTP_PROXY_TYPE_NONE; - m_csProxyName.Empty(); - // Proxy port - m_uProxyPort = OCS_HTTP_DEFAULT_PROXY_PORT; - // Proxy auth - m_bProxyAuthRequired = OCS_HTTP_AUTH_TYPE_NONE; - // Proxy credentials - m_csProxyUser.Empty(); - m_csProxyPwd.Empty(); -} - -LPCTSTR CHTTPConfig::getServer() -{ - return m_csServerName; -} - -BOOL CHTTPConfig::isSslServerValidationRequired() -{ - return (m_uSSL == OCS_HTTP_SSL_VALIDATION_REQUIRED); -} - -LPCTSTR CHTTPConfig::getCaBundle() -{ - return m_csCaBundle; -} - -BOOL CHTTPConfig::isHttpAuthRequired() -{ - return m_bAuthRequired; -} - -LPCTSTR CHTTPConfig::getHttpUser() -{ - return m_csHttpUser; -} - -LPCTSTR CHTTPConfig::getHttpPwd() -{ - return m_csHttpPwd; -} - -UINT CHTTPConfig::getProxyType() -{ - return m_uProxyType; -} - -LPCTSTR CHTTPConfig::getProxy() -{ - return m_csProxyName; -} - -UINT CHTTPConfig::getProxyPort() -{ - return m_uProxyPort; -} - -BOOL CHTTPConfig::isProxyAuthRequired() -{ - return m_bProxyAuthRequired; -} - -LPCTSTR CHTTPConfig::getProxyUser() -{ - return m_csProxyUser; -} - -LPCTSTR CHTTPConfig::getProxyPwd() -{ - return m_csProxyPwd; -} - -void CHTTPConfig::setServer( LPCTSTR lpstrServerName) -{ - m_csServerName = lpstrServerName; -} - -void CHTTPConfig::setServerSSL( UINT uRequireSSL) -{ - m_uSSL = uRequireSSL; -} - -void CHTTPConfig::setServerCA( LPCTSTR lpstrCaBundle) -{ - m_csCaBundle = lpstrCaBundle; -} - -void CHTTPConfig::setServerAuthUser( LPCTSTR lpstrHttpUser) -{ - m_csHttpUser = lpstrHttpUser; - if (!m_csHttpUser.IsEmpty()) - // Authentication is required if User ID not null - m_bAuthRequired = TRUE; -} - -void CHTTPConfig::setServerAuthPasswd( LPCTSTR lpstrHttpPwd) -{ - m_csHttpPwd = lpstrHttpPwd; -} - -void CHTTPConfig::setProxyType( UINT uProxyType) -{ - m_uProxyType = uProxyType; -} - -void CHTTPConfig::setProxy( LPCTSTR lpstrServerName) -{ - m_csProxyName = lpstrServerName; -} - -void CHTTPConfig::setProxyPort( UINT uPort) -{ - m_uProxyPort = uPort; -} - -void CHTTPConfig::setProxyAuthUser( LPCTSTR lpstrProxyUser) -{ - m_csProxyUser = lpstrProxyUser; - if (!m_csProxyUser.IsEmpty()) - // Authentication is required if User ID not null - m_bProxyAuthRequired = TRUE; -} - -void CHTTPConfig::setProxyAuthPasswd( LPCTSTR lpstrProxyPwd) -{ - m_csProxyPwd = lpstrProxyPwd; -} - -BOOL CHTTPConfig::load( LPCTSTR lpstrFile, LPCTSTR lpstrSection) -{ - try - { - // Crypted ocs db access - CHTTPCrypt myCrypt; - CString csConfigFile, - csBuffer; - - // Get path to config file - if (lpstrFile == NULL) - csConfigFile = getAgentConfig()->getConfigFile(); - else - csConfigFile = lpstrFile; - - // Server address - GetPrivateProfileString( lpstrSection, _T( "Server"), OCS_HTTP_DEFAULT_SERVER, m_csServerName.GetBuffer( 1024), 1024, csConfigFile); - m_csServerName.ReleaseBuffer(); - - // SSL connection required - m_uSSL = (UINT)GetPrivateProfileInt( lpstrSection, _T( "SSL"), OCS_HTTP_SSL_VALIDATION_REQUIRED, csConfigFile); - if (m_uSSL == OCS_HTTP_SSL_VALIDATION_REQUIRED) - { - // Path to CA certificate chain file in PEM format - csBuffer.Format( _T( "%s\\%s"), getDataFolder(), OCS_HTTP_CA_DEFAULT_BUNDLE); - GetPrivateProfileString( lpstrSection, _T( "CaBundle"), csBuffer, m_csCaBundle.GetBuffer( 1024), 1024, csConfigFile); - m_csCaBundle.ReleaseBuffer(); - } - else - m_csCaBundle.Empty(); - // HTTP credentials if needed - m_bAuthRequired = (BOOL)GetPrivateProfileInt( lpstrSection, _T( "AuthRequired"), OCS_HTTP_AUTH_TYPE_NONE, csConfigFile); - if (m_bAuthRequired) - { - // HTTP user - GetPrivateProfileString( lpstrSection, _T( "User"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); - csBuffer.ReleaseBuffer(); - myCrypt.getData( csBuffer, m_csHttpUser); - if (m_csHttpUser.IsEmpty()) - // No user provided - m_bAuthRequired = FALSE; - GetPrivateProfileString( lpstrSection, _T( "Pwd"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); - csBuffer.ReleaseBuffer(); - myCrypt.getData( csBuffer, m_csHttpPwd); - } - - // Proxy type - m_uProxyType = (UINT)GetPrivateProfileInt( lpstrSection, _T( "ProxyType"), OCS_HTTP_PROXY_TYPE_NONE, csConfigFile); - if (m_uProxyType) - { - // Retrieve an optionnal specified proxy - GetPrivateProfileString( lpstrSection, _T( "Proxy"), _T( ""), m_csProxyName.GetBuffer( 1024), 1024, csConfigFile); - m_csProxyName.ReleaseBuffer(); - // Proxy port - m_uProxyPort = (UINT)GetPrivateProfileInt( lpstrSection, _T( "ProxyPort"), OCS_HTTP_DEFAULT_PROXY_PORT, csConfigFile); - // Proxy auth - m_bProxyAuthRequired = (BOOL)GetPrivateProfileInt( lpstrSection, _T( "ProxyAuthRequired"), OCS_HTTP_AUTH_TYPE_NONE, csConfigFile); - // Proxy credentials - if(m_bProxyAuthRequired) - { - GetPrivateProfileString( lpstrSection, _T( "ProxyUser"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); - csBuffer.ReleaseBuffer(); - myCrypt.getData( csBuffer, m_csProxyUser); - if (m_csProxyUser.IsEmpty()) - // No user provided - m_bAuthRequired = FALSE; - GetPrivateProfileString( lpstrSection, _T( "ProxyPwd"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); - csBuffer.ReleaseBuffer(); - myCrypt.getData( csBuffer, m_csProxyPwd); - } - - } - else - { - m_bProxyAuthRequired = FALSE; - m_uProxyPort = 0; - } - return TRUE; - } - catch (CException *pEx) - { - pEx->Delete(); - return FALSE; - } -} - -BOOL CHTTPConfig::save( LPCTSTR lpstrFile, LPCTSTR lpstrSection) -{ - try - { - // Crypted ocs db access - CHTTPCrypt myCrypt; - CString csConfigFile, - csBuffer; - BOOL bResult; - - // Get path to config file - if (lpstrFile == NULL) - csConfigFile = getAgentConfig()->getConfigFile(); - else - csConfigFile = lpstrFile; - - // Server address - bResult = WritePrivateProfileString( lpstrSection, _T( "Server"), m_csServerName, csConfigFile); - // SSL connection - csBuffer.Format( _T( "%u"), m_uSSL); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "SSL"), csBuffer, csConfigFile); - // Path to CA certificate chain file in PEM format - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "CaBundle"), m_csCaBundle, csConfigFile); - // HTTP credentials - if (m_csHttpUser.IsEmpty()) - // Authentication is required if User ID not null - m_bAuthRequired = FALSE; - csBuffer.Format( _T( "%u"), m_bAuthRequired ? 1 : 0); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "AuthRequired"), csBuffer, csConfigFile); - // Clear user and password is not used - if (!m_bAuthRequired) - { - m_csHttpUser.Empty(); - m_csHttpPwd.Empty(); - } - bResult = bResult && myCrypt.setData( m_csHttpUser, csBuffer); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "User"), csBuffer, csConfigFile); - bResult = bResult && myCrypt.setData( m_csHttpPwd, csBuffer); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "Pwd"), csBuffer, csConfigFile); - // Proxy server - csBuffer.Format( _T( "%u"), m_uProxyType); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyType"), csBuffer, csConfigFile); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "Proxy"), m_csProxyName, csConfigFile); - csBuffer.Format( _T( "%u"), m_uProxyPort); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyPort"), csBuffer, csConfigFile); - if (m_csProxyUser.IsEmpty()) - // Authentication is required if User ID not null - m_bProxyAuthRequired = FALSE; - csBuffer.Format( _T( "%u"), m_bProxyAuthRequired ? 1 : 0); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyAuthRequired"), csBuffer, csConfigFile); - // Clear user and password is not used - if (!m_bAuthRequired) - { - m_csProxyUser.Empty(); - m_csProxyPwd.Empty(); - } - bResult = bResult && myCrypt.setData( m_csProxyUser, csBuffer); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyUser"), csBuffer, csConfigFile); - bResult = bResult && myCrypt.setData( m_csProxyPwd, csBuffer); - bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyPwd"), csBuffer, csConfigFile); - return bResult; - } - catch (CException *pEx) - { - pEx->Delete(); - return FALSE; - } -} - -BOOL CHTTPConfig::parseCommandLine(LPCTSTR lpstrCommand) -{ - try - { - // /SERVER=address - if (isRequired( lpstrCommand, _T( "server"))) - // get network address - setServer( getParamValue( lpstrCommand, _T( "server"))); - // /SSL=level - if (isRequired( lpstrCommand, _T( "ssl"))) - // get SSL level - setServerSSL( _ttol( getParamValue( lpstrCommand, _T( "ssl")))); - // /CA=path to cacert.pem - if (isRequired( lpstrCommand, _T( "ca"))) - // get path - setServerCA( getParamValue( lpstrCommand, _T( "ca"))); - // By default, server authentication is not required, unless user option specified and not null - // /USER=username - if (isRequired( lpstrCommand, _T( "user"))) - { - // get username - setServerAuthUser( getParamValue( lpstrCommand, _T( "user"))); - m_bAuthRequired = TRUE; - } - else - { - // This code is needed to set authRequired to FALSE, when saving new configuration - if (isRequired( lpstrCommand, _T( "save_conf"))) - m_bAuthRequired = FALSE; - } - // /PWD=userpwd - if (isRequired( lpstrCommand, _T( "pwd"))) - { - // get password - setServerAuthPasswd( getParamValue( lpstrCommand, _T( "pwd"))); - } - // /PROXY_TYPE=type - if (isRequired( lpstrCommand, _T( "proxy_type"))) - // get proxy type - setProxyType( _ttol( getParamValue( lpstrCommand, _T( "proxy_type")))); - // /PROXY=address - if (isRequired( lpstrCommand, _T( "proxy"))) - // get network address - setProxy( getParamValue( lpstrCommand, _T( "proxy"))); - // /PROXY_PORT=port - if (isRequired( lpstrCommand, _T( "proxy_port"))) - // get network port - setProxyPort( _ttol( getParamValue( lpstrCommand, _T( "proxy_port")))); - // By default, proxy authentication not required, unless proxy_user option specified and not null - // /PROXY_USER=username - if (isRequired( lpstrCommand, _T( "proxy_user"))) - { - // get username - setProxyAuthUser( getParamValue( lpstrCommand, _T( "proxy_user"))); - m_bProxyAuthRequired = TRUE; - } - else - { - // This code is needed to set authRequired to FALSE, when saving new configuration - if (isRequired( lpstrCommand, _T( "save_conf"))) - m_bProxyAuthRequired = FALSE; - } - // /PROXY_PWD:userpwd - if (isRequired( lpstrCommand, _T( "proxy_pwd"))) - { - // get password - setProxyAuthPasswd( getParamValue( lpstrCommand, _T( "proxy_pwd"))); - } - return TRUE; - } - catch (CException *pEx) - { - pEx->Delete(); - return FALSE; - } -} + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +CHTTPConfig::CHTTPConfig( LPCTSTR lpstrFile, LPCTSTR lpstrSection) +{ + Clear(); + // Load default configuration from default file + load( lpstrFile, lpstrSection); +} + +CHTTPConfig::~CHTTPConfig() +{ +} + +void CHTTPConfig::Clear() +{ + // Server address + m_csServerName = OCS_HTTP_DEFAULT_SERVER; + + // SSL connection required + m_uSSL = OCS_HTTP_SSL_VALIDATION_REQUIRED; + // Path to CA certificate chain file in PEM format + m_csCaBundle.Format( _T( "%s\\%s"), getDataFolder(), OCS_HTTP_CA_DEFAULT_BUNDLE); + + // HTTP credentials if needed + m_bAuthRequired = OCS_HTTP_AUTH_TYPE_NONE; + m_csHttpUser.Empty(); + m_csHttpPwd.Empty(); + + // Proxy type + m_uProxyType = OCS_HTTP_PROXY_TYPE_NONE; + m_csProxyName.Empty(); + // Proxy port + m_uProxyPort = OCS_HTTP_DEFAULT_PROXY_PORT; + // Proxy auth + m_bProxyAuthRequired = OCS_HTTP_AUTH_TYPE_NONE; + // Proxy credentials + m_csProxyUser.Empty(); + m_csProxyPwd.Empty(); +} + +LPCTSTR CHTTPConfig::getServer() +{ + return m_csServerName; +} + +BOOL CHTTPConfig::isSslServerValidationRequired() +{ + return (m_uSSL == OCS_HTTP_SSL_VALIDATION_REQUIRED); +} + +LPCTSTR CHTTPConfig::getCaBundle() +{ + return m_csCaBundle; +} + +BOOL CHTTPConfig::isHttpAuthRequired() +{ + return m_bAuthRequired; +} + +LPCTSTR CHTTPConfig::getHttpUser() +{ + return m_csHttpUser; +} + +LPCTSTR CHTTPConfig::getHttpPwd() +{ + return m_csHttpPwd; +} + +UINT CHTTPConfig::getProxyType() +{ + return m_uProxyType; +} + +LPCTSTR CHTTPConfig::getProxy() +{ + return m_csProxyName; +} + +UINT CHTTPConfig::getProxyPort() +{ + return m_uProxyPort; +} + +BOOL CHTTPConfig::isProxyAuthRequired() +{ + return m_bProxyAuthRequired; +} + +LPCTSTR CHTTPConfig::getProxyUser() +{ + return m_csProxyUser; +} + +LPCTSTR CHTTPConfig::getProxyPwd() +{ + return m_csProxyPwd; +} + +void CHTTPConfig::setServer( LPCTSTR lpstrServerName) +{ + m_csServerName = lpstrServerName; +} + +void CHTTPConfig::setServerSSL( UINT uRequireSSL) +{ + m_uSSL = uRequireSSL; +} + +void CHTTPConfig::setServerCA( LPCTSTR lpstrCaBundle) +{ + m_csCaBundle = lpstrCaBundle; +} + +void CHTTPConfig::setServerAuthUser( LPCTSTR lpstrHttpUser) +{ + m_csHttpUser = lpstrHttpUser; + if (!m_csHttpUser.IsEmpty()) + // Authentication is required if User ID not null + m_bAuthRequired = TRUE; +} + +void CHTTPConfig::setServerAuthPasswd( LPCTSTR lpstrHttpPwd) +{ + m_csHttpPwd = lpstrHttpPwd; +} + +void CHTTPConfig::setProxyType( UINT uProxyType) +{ + m_uProxyType = uProxyType; +} + +void CHTTPConfig::setProxy( LPCTSTR lpstrServerName) +{ + m_csProxyName = lpstrServerName; +} + +void CHTTPConfig::setProxyPort( UINT uPort) +{ + m_uProxyPort = uPort; +} + +void CHTTPConfig::setProxyAuthUser( LPCTSTR lpstrProxyUser) +{ + m_csProxyUser = lpstrProxyUser; + if (!m_csProxyUser.IsEmpty()) + // Authentication is required if User ID not null + m_bProxyAuthRequired = TRUE; +} + +void CHTTPConfig::setProxyAuthPasswd( LPCTSTR lpstrProxyPwd) +{ + m_csProxyPwd = lpstrProxyPwd; +} + +BOOL CHTTPConfig::load( LPCTSTR lpstrFile, LPCTSTR lpstrSection) +{ + try + { + // Crypted ocs db access + CHTTPCrypt myCrypt; + CString csConfigFile, + csBuffer; + + // Get path to config file + if (lpstrFile == NULL) + csConfigFile = getAgentConfig()->getConfigFile(); + else + csConfigFile = lpstrFile; + + // Server address + GetPrivateProfileString( lpstrSection, _T( "Server"), OCS_HTTP_DEFAULT_SERVER, m_csServerName.GetBuffer( 1024), 1024, csConfigFile); + m_csServerName.ReleaseBuffer(); + + // SSL connection required + m_uSSL = (UINT)GetPrivateProfileInt( lpstrSection, _T( "SSL"), OCS_HTTP_SSL_VALIDATION_REQUIRED, csConfigFile); + if (m_uSSL == OCS_HTTP_SSL_VALIDATION_REQUIRED) + { + // Path to CA certificate chain file in PEM format + csBuffer.Format( _T( "%s\\%s"), getDataFolder(), OCS_HTTP_CA_DEFAULT_BUNDLE); + GetPrivateProfileString( lpstrSection, _T( "CaBundle"), csBuffer, m_csCaBundle.GetBuffer( 1024), 1024, csConfigFile); + m_csCaBundle.ReleaseBuffer(); + } + else + m_csCaBundle.Empty(); + // HTTP credentials if needed + m_bAuthRequired = (BOOL)GetPrivateProfileInt( lpstrSection, _T( "AuthRequired"), OCS_HTTP_AUTH_TYPE_NONE, csConfigFile); + if (m_bAuthRequired) + { + // HTTP user + GetPrivateProfileString( lpstrSection, _T( "User"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); + csBuffer.ReleaseBuffer(); + myCrypt.getData( csBuffer, m_csHttpUser); + if (m_csHttpUser.IsEmpty()) + // No user provided + m_bAuthRequired = FALSE; + GetPrivateProfileString( lpstrSection, _T( "Pwd"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); + csBuffer.ReleaseBuffer(); + myCrypt.getData( csBuffer, m_csHttpPwd); + } + + // Proxy type + m_uProxyType = (UINT)GetPrivateProfileInt( lpstrSection, _T( "ProxyType"), OCS_HTTP_PROXY_TYPE_NONE, csConfigFile); + if (m_uProxyType) + { + // Retrieve an optionnal specified proxy + GetPrivateProfileString( lpstrSection, _T( "Proxy"), _T( ""), m_csProxyName.GetBuffer( 1024), 1024, csConfigFile); + m_csProxyName.ReleaseBuffer(); + // Proxy port + m_uProxyPort = (UINT)GetPrivateProfileInt( lpstrSection, _T( "ProxyPort"), OCS_HTTP_DEFAULT_PROXY_PORT, csConfigFile); + // Proxy auth + m_bProxyAuthRequired = (BOOL)GetPrivateProfileInt( lpstrSection, _T( "ProxyAuthRequired"), OCS_HTTP_AUTH_TYPE_NONE, csConfigFile); + // Proxy credentials + if(m_bProxyAuthRequired) + { + GetPrivateProfileString( lpstrSection, _T( "ProxyUser"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); + csBuffer.ReleaseBuffer(); + myCrypt.getData( csBuffer, m_csProxyUser); + if (m_csProxyUser.IsEmpty()) + // No user provided + m_bAuthRequired = FALSE; + GetPrivateProfileString( lpstrSection, _T( "ProxyPwd"), _T( ""), csBuffer.GetBuffer( 1024), 1024, csConfigFile); + csBuffer.ReleaseBuffer(); + myCrypt.getData( csBuffer, m_csProxyPwd); + } + + } + else + { + m_bProxyAuthRequired = FALSE; + m_uProxyPort = 0; + } + return TRUE; + } + catch (CException *pEx) + { + pEx->Delete(); + return FALSE; + } +} + +BOOL CHTTPConfig::save( LPCTSTR lpstrFile, LPCTSTR lpstrSection) +{ + try + { + // Crypted ocs db access + CHTTPCrypt myCrypt; + CString csConfigFile, + csBuffer; + BOOL bResult; + + // Get path to config file + if (lpstrFile == NULL) + csConfigFile = getAgentConfig()->getConfigFile(); + else + csConfigFile = lpstrFile; + + // Server address + bResult = WritePrivateProfileString( lpstrSection, _T( "Server"), m_csServerName, csConfigFile); + // SSL connection + csBuffer.Format( _T( "%u"), m_uSSL); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "SSL"), csBuffer, csConfigFile); + // Path to CA certificate chain file in PEM format + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "CaBundle"), m_csCaBundle, csConfigFile); + // HTTP credentials + if (m_csHttpUser.IsEmpty()) + // Authentication is required if User ID not null + m_bAuthRequired = FALSE; + csBuffer.Format( _T( "%u"), m_bAuthRequired ? 1 : 0); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "AuthRequired"), csBuffer, csConfigFile); + // Clear user and password is not used + if (!m_bAuthRequired) + { + m_csHttpUser.Empty(); + m_csHttpPwd.Empty(); + } + bResult = bResult && myCrypt.setData( m_csHttpUser, csBuffer); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "User"), csBuffer, csConfigFile); + bResult = bResult && myCrypt.setData( m_csHttpPwd, csBuffer); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "Pwd"), csBuffer, csConfigFile); + // Proxy server + csBuffer.Format( _T( "%u"), m_uProxyType); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyType"), csBuffer, csConfigFile); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "Proxy"), m_csProxyName, csConfigFile); + csBuffer.Format( _T( "%u"), m_uProxyPort); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyPort"), csBuffer, csConfigFile); + if (m_csProxyUser.IsEmpty()) + // Authentication is required if User ID not null + m_bProxyAuthRequired = FALSE; + csBuffer.Format( _T( "%u"), m_bProxyAuthRequired ? 1 : 0); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyAuthRequired"), csBuffer, csConfigFile); + // Clear user and password is not used + if (!m_bAuthRequired) + { + m_csProxyUser.Empty(); + m_csProxyPwd.Empty(); + } + bResult = bResult && myCrypt.setData( m_csProxyUser, csBuffer); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyUser"), csBuffer, csConfigFile); + bResult = bResult && myCrypt.setData( m_csProxyPwd, csBuffer); + bResult = bResult && WritePrivateProfileString( lpstrSection, _T( "ProxyPwd"), csBuffer, csConfigFile); + return bResult; + } + catch (CException *pEx) + { + pEx->Delete(); + return FALSE; + } +} + +BOOL CHTTPConfig::parseCommandLine(LPCTSTR lpstrCommand) +{ + try + { + // /SERVER=address + if (isRequired( lpstrCommand, _T( "server"))) + // get network address + setServer( getParamValue( lpstrCommand, _T( "server"))); + // /SSL=level + if (isRequired( lpstrCommand, _T( "ssl"))) + // get SSL level + setServerSSL( _ttol( getParamValue( lpstrCommand, _T( "ssl")))); + // /CA=path to cacert.pem + if (isRequired( lpstrCommand, _T( "ca"))) + // get path + setServerCA( getParamValue( lpstrCommand, _T( "ca"))); + // By default, server authentication is not required, unless user option specified and not null + // /USER=username + if (isRequired( lpstrCommand, _T( "user"))) + { + // get username + setServerAuthUser( getParamValue( lpstrCommand, _T( "user"))); + m_bAuthRequired = TRUE; + } + else + { + // This code is needed to set authRequired to FALSE, when saving new configuration + if (isRequired( lpstrCommand, _T( "save_conf"))) + m_bAuthRequired = FALSE; + } + // /PWD=userpwd + if (isRequired( lpstrCommand, _T( "pwd"))) + { + // get password + setServerAuthPasswd( getParamValue( lpstrCommand, _T( "pwd"))); + } + // /PROXY_TYPE=type + if (isRequired( lpstrCommand, _T( "proxy_type"))) + // get proxy type + setProxyType( _ttol( getParamValue( lpstrCommand, _T( "proxy_type")))); + // /PROXY=address + if (isRequired( lpstrCommand, _T( "proxy"))) + // get network address + setProxy( getParamValue( lpstrCommand, _T( "proxy"))); + // /PROXY_PORT=port + if (isRequired( lpstrCommand, _T( "proxy_port"))) + // get network port + setProxyPort( _ttol( getParamValue( lpstrCommand, _T( "proxy_port")))); + // By default, proxy authentication not required, unless proxy_user option specified and not null + // /PROXY_USER=username + if (isRequired( lpstrCommand, _T( "proxy_user"))) + { + // get username + setProxyAuthUser( getParamValue( lpstrCommand, _T( "proxy_user"))); + m_bProxyAuthRequired = TRUE; + } + else + { + // This code is needed to set authRequired to FALSE, when saving new configuration + if (isRequired( lpstrCommand, _T( "save_conf"))) + m_bProxyAuthRequired = FALSE; + } + // /PROXY_PWD:userpwd + if (isRequired( lpstrCommand, _T( "proxy_pwd"))) + { + // get password + setProxyAuthPasswd( getParamValue( lpstrCommand, _T( "proxy_pwd"))); + } + return TRUE; + } + catch (CException *pEx) + { + pEx->Delete(); + return FALSE; + } +} diff --git a/Service/OcsService.cpp b/Service/OcsService.cpp index a2053790..7192bb34 100644 --- a/Service/OcsService.cpp +++ b/Service/OcsService.cpp @@ -174,6 +174,7 @@ BOOL COcsService::loadConfig() m_iOldPrologFreq = GetPrivateProfileInt( OCS_SERVICE_SECTION, OCS_SERVICE_OLD_PROLOG_FREQ, DEFAULT_PROLOG_FREQ, csConfigFile); m_iTToWait = GetPrivateProfileInt( OCS_SERVICE_SECTION, OCS_SERVICE_TTO_WAIT, -1, csConfigFile); m_iWriteIniLatency = GetPrivateProfileInt( OCS_SERVICE_SECTION, _T( "WRITE_INI_LATENCY"), WRITE_TTOWAIT_EACH, csConfigFile); + m_iInventoryOnStatup = GetPrivateProfileInt(OCS_SERVICE_SECTION, OCS_SERVICE_INVENTORY_ON_STARTUP, INVENTORY_ON_STARTUP, csConfigFile); return TRUE; } @@ -204,6 +205,13 @@ BOOL COcsService::writeConfig( BOOL bFull) LogEvent( EVENTLOG_ERROR_TYPE, EVMSG_GENERIC_ERROR, csValue); bResult = FALSE; } + csValue.Format(_T("%d"), m_iInventoryOnStatup); + if (!WritePrivateProfileString(OCS_SERVICE_SECTION, OCS_SERVICE_INVENTORY_ON_STARTUP, csValue, csConfigFile)) + { + csValue.Format(_T("Failed to write new INVENTORY ON STARTUP Value : %s"), LookupError(GetLastError())); + LogEvent(EVENTLOG_ERROR_TYPE, EVMSG_GENERIC_ERROR, csValue); + bResult = FALSE; + } protectConfigFiles(); return bResult; } @@ -260,7 +268,7 @@ BOOL COcsService::OnInit() writeConfig(); } // Log service start - csMessage.Format( _T( "Service start parameters FREQ: %i, OLD_FREQ: %i, TTO_WAIT: %i"), m_iPrologFreq, m_iOldPrologFreq, m_iTToWait); + csMessage.Format(_T("Service start parameters FREQ: %i, OLD_FREQ: %i, TTO_WAIT: %i, INVENTORY_ON_STARTUP: %i"), m_iPrologFreq, m_iOldPrologFreq, m_iTToWait, m_iInventoryOnStatup); LogEvent( EVENTLOG_INFORMATION_TYPE, EVMSG_GENERIC_MESSAGE, csMessage); // Rotate log files rotateLogs(); @@ -274,6 +282,15 @@ BOOL COcsService::OnInit() // Portect OCS files to prevent delete protectCommonFiles(); protectConfigFiles(); + // Check if inventoryOnStartUp is enabled and launch an inventory if needed + if (m_iInventoryOnStatup) + { + csMessage.Format(_T("Inventory on startup is enabled, run agent.")); + LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_GENERIC_MESSAGE, csMessage); + runAgent(); + } + + return TRUE; } @@ -528,7 +545,7 @@ void COcsService::Run() bNotifyInventory = FALSE; m_iOldPrologFreq = m_iPrologFreq; writeConfig(); - csStatus.Format( _T( "OCS Inventory NG Agent executed successfully.\n\nNew service parameters: FREQ: %i, OLD_FREQ: %i, TTO_WAIT: %i"), m_iPrologFreq, vOld, m_iTToWait); + csStatus.Format( _T( "OCS Inventory NG Agent executed successfully.\n\nNew service parameters: FREQ: %i, OLD_FREQ: %i, TTO_WAIT: %i, INVENTORY_ON_STARTUP: %i"), m_iPrologFreq, vOld, m_iTToWait, m_iInventoryOnStatup); LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_GENERIC_MESSAGE, csStatus); } } diff --git a/Service/OcsService.h b/Service/OcsService.h index a968d594..a74b4678 100644 --- a/Service/OcsService.h +++ b/Service/OcsService.h @@ -25,7 +25,9 @@ #define OCS_SERVICE_RAND_FILE _T( "rand") #define OCS_SERVICE_TTO_WAIT _T( "TTO_WAIT") #define OCS_SERVICE_OLD_PROLOG_FREQ _T( "OLD_PROLOG_FREQ") +#define OCS_SERVICE_INVENTORY_ON_STARTUP _T( "INVENTORY_ON_STARTUP") #define RUN_OCS _T( "ocsinventory.exe") +#define INVENTORY_ON_STARTUP 0 // 0 (no) by default #define PROLOG_FREQ_UNIT 3600 // seconds #define DEFAULT_PROLOG_FREQ 10 // * PROLOG_FREQ_UNIT #define WRITE_TTOWAIT_EACH 60 // seconds @@ -84,6 +86,7 @@ class COcsService : int m_iPrologFreq; // Time between 2 agent run int m_iOldPrologFreq; // Last time between 2 agent run int m_iWriteIniLatency; // Time between TTO_WAIT write to file to save it + int m_iInventoryOnStatup; // Inventory on ocs agent start-up CObArray m_tCommonHandles; // Handle of OCS common files to protect them from being deleted CObArray m_tConfigHandles; // Handle of OCS config files to protect them from being deleted }; diff --git a/ocsinventory.ini.sample b/ocsinventory.ini.sample index d7c4d588..113e1d74 100644 --- a/ocsinventory.ini.sample +++ b/ocsinventory.ini.sample @@ -45,4 +45,5 @@ ProxyUser= ProxyPwd= [OCS Inventory Service] +INVENTORY_ON_STARTUP=1 PROLOG_FREQ=1