diff --git a/pandora_agents/win32/ChangeLog b/pandora_agents/win32/ChangeLog index 3893865358..bbd7c2e0d8 100644 --- a/pandora_agents/win32/ChangeLog +++ b/pandora_agents/win32/ChangeLog @@ -1,3 +1,14 @@ +2007-08-29 Esteban Sanchez + + * pandora_windows_service.cc: Fixed a typo. Disconnect ftp client when + an error happened. + + * ftp/pandora_ftp_client.cc: Set network timeout and ftp timeout. + Ignore error when executing moving operations, this is compatible + with FTP jails + + * windows/pandora_wmi.cc: Improved WQL queries and fixed some leaks. + 2007-08-28 Esteban Sanchez * main.cc: Fixed an error with previous commit. diff --git a/pandora_agents/win32/bin/PandoraAgent.exe b/pandora_agents/win32/bin/PandoraAgent.exe index 759b65bc00..319e0724a6 100755 Binary files a/pandora_agents/win32/bin/PandoraAgent.exe and b/pandora_agents/win32/bin/PandoraAgent.exe differ diff --git a/pandora_agents/win32/ftp/pandora_ftp_client.cc b/pandora_agents/win32/ftp/pandora_ftp_client.cc index 0e85be3251..e163a04057 100644 --- a/pandora_agents/win32/ftp/pandora_ftp_client.cc +++ b/pandora_agents/win32/ftp/pandora_ftp_client.cc @@ -155,6 +155,8 @@ Pandora_Ftp_Client::ftpFileFilename (const string remote_filename, curl_easy_setopt (this->curl, CURLOPT_UPLOAD, 1) ; curl_easy_setopt (this->curl, CURLOPT_URL, url.c_str ()); curl_easy_setopt (this->curl, CURLOPT_POSTQUOTE, headerlist); + curl_easy_setopt (this->curl, CURLOPT_TIMEOUT, 240); + curl_easy_setopt (this->curl, CURLOPT_FTP_RESPONSE_TIMEOUT, 60); curl_easy_setopt (this->curl, CURLOPT_READFUNCTION, read_func); curl_easy_setopt (this->curl, CURLOPT_READDATA, fd); curl_easy_setopt (curl, CURLOPT_INFILESIZE_LARGE, @@ -173,6 +175,8 @@ Pandora_Ftp_Client::ftpFileFilename (const string remote_filename, switch (this->result) { case CURLE_OK: + case CURLE_FTP_QUOTE_ERROR: /* These error happens when FTP is in a jail. + Transfer was OK, moving wasn't. */ break; case CURLE_COULDNT_CONNECT: throw Unknown_Host (); diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index 2cf3584e8a..156b61023e 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -238,18 +238,20 @@ Pandora_Windows_Service::copyFtpDataFile (string host, ftp_client.ftpFileFilename (remote_path + filename, filepath); } catch (FTP::Unknown_Host e) { - pandoraLog ("Failed when copying to %s (%s)", host.c_str (), - ftp_client.getError ().c_str ()); + pandoraLog ("Pandora Agent: Failed when copying to %s (%s)", + host.c_str (), ftp_client.getError ().c_str ()); + ftp_client.disconnect (); throw e; } catch (FTP::Authentication_Failed e) { pandoraLog ("Pandora Agent: Authentication Failed " "when connecting to %s (%s)", host.c_str (), ftp_client.getError ().c_str ()); + ftp_client.disconnect (); throw e; } catch (FTP::FTP_Exception e) { pandoraLog ("Pandora Agent: Failed when copying to %s (%s)", host.c_str (), ftp_client.getError ().c_str ()); - + ftp_client.disconnect (); throw e; } @@ -275,7 +277,7 @@ Pandora_Windows_Service::copyDataFile (string filename) copyScpDataFile (host, remote_path, filename); } else { pandoraLog ("Invalid transfer mode: %s." - "Please rechak transfer_mode option " + "Please recheck transfer_mode option " "in configuration file."); } diff --git a/pandora_agents/win32/windows/pandora_wmi.cc b/pandora_agents/win32/windows/pandora_wmi.cc index 9dfe023896..78982774a1 100644 --- a/pandora_agents/win32/windows/pandora_wmi.cc +++ b/pandora_agents/win32/windows/pandora_wmi.cc @@ -24,6 +24,7 @@ #include #include #include +#include using namespace std; using namespace Pandora_Wmi; @@ -35,10 +36,10 @@ getWmiStr (LPCWSTR computer) { wcscpy (wmi_str, L"winmgmts:{impersonationLevel=impersonate}!\\\\"); if (computer) { - wcsncat (wmi_str, computer, 128); + wcsncat (wmi_str, computer, 128); } else { - wcscat (wmi_str, L"."); - } + wcscat (wmi_str, L"."); + } wcscat (wmi_str, L"\\root\\cimv2"); @@ -54,39 +55,27 @@ getWmiStr (LPCWSTR computer) { */ int Pandora_Wmi::isProcessRunning (string process_name) { - CDhInitialize init; + CDhInitialize init; CDispPtr wmi_svc, quickfixes; string name; int result = 0; - - struct QFix { - CDhStringA name, description, state; - }; - + string query; + + query = "SELECT * FROM Win32_Process WHERE Name=\"" + process_name + "\""; + cout << "Query: " << query << endl; try { dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_Process")); - - FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%s", &fix.name, quickfix, - L".Name"); - - name = fix.name; - transform (name.begin (), name.end (), name.begin (), - (int (*) (int)) tolower); - - if (process_name == name) { - result++; - } + L".ExecQuery(%T)", + query.c_str ())); + + FOR_EACH (quickfix, quickfixes, NULL) { + result++; } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("isProcessRunning error. %s", errstr.c_str ()); } - + return result; } @@ -100,53 +89,40 @@ Pandora_Wmi::isProcessRunning (string process_name) { */ int Pandora_Wmi::isServiceRunning (string service_name) { - CDhInitialize init; + CDhInitialize init; CDispPtr wmi_svc, quickfixes; - string name, state; - - struct QFix { - CDhStringA name, state; - }; - + string query; + char *state; + int retval; + + query = "SELECT * FROM Win32_Service WHERE Name = \"" + service_name + "\""; + try { dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_Service")); - - FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%s", &fix.name, quickfix, - L".Name"); - - name = fix.name; - transform (name.begin (), name.end (), name.begin (), - (int (*) (int)) tolower); - - if (service_name == name) { - dhGetValue (L"%s", &fix.state, quickfix, - L".State"); - state = fix.state; - - if (state == "Running") { - return 1; - } else { - return 0; - } - } + L".ExecQuery(%T)", + query.c_str ())); + + FOR_EACH (quickfix, quickfixes, NULL) { + dhGetValue (L"%s", &state, quickfix, + L".State"); + + retval = (state == "Running") ? 1 : 0; + dhFreeString (state); + + return retval; } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("isServiceRunning error. %s", errstr.c_str ()); } - + return 0; } /** * Get the free space in a logical disk drive. * - * @param disk_id Disk drive letter (C. + * @param disk_id Disk drive letter (C: for example). * * @return Free space amount in MB. * @@ -160,26 +136,25 @@ Pandora_Wmi::getDiskFreeSpace (string disk_id) { string id, space_str; unsigned long long space = 0; string query; - + query = "SELECT DeviceID, FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = \"" + disk_id + "\""; try { - dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); + dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%T)", - query.c_str ())); - + L".ExecQuery(%T)", + query.c_str ())); + FOR_EACH (quickfix, quickfixes, NULL) { dhGetValue (L"%d", &space, quickfix, L".FreeSpace"); - cout << space << endl; - + return space / 1024 / 1024; } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("getDiskFreeSpace error. %s", errstr.c_str ()); } - + throw Pandora_Wmi_Exception (); } @@ -197,42 +172,31 @@ int Pandora_Wmi::getCpuUsagePercentage (int cpu_id) { CDhInitialize init; CDispPtr wmi_svc, quickfixes; - string id, cpu_id_str; - - cpu_id_str = "CPU"; - cpu_id_str += Pandora_Strutils::inttostr (cpu_id); - - struct QFix { - CDhStringA id; - long load_percentage; - }; - + string query; + long load_percentage; + std::ostringstream stm; + + stm << cpu_id; + query = "SELECT * FROM Win32_Processor WHERE DeviceID = \"CPU" + stm.str () + "\""; + try { - dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); + dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_Processor ")); - + L".ExecQuery(%T)", + query.c_str ())); + FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%s", &fix.id, quickfix, - L".DeviceID"); - - id = fix.id; - - if (cpu_id_str == id) { - dhGetValue (L"%d", &fix.load_percentage, quickfix, - L".LoadPercentage"); - - return fix.load_percentage; - } - + dhGetValue (L"%d", &load_percentage, quickfix, + L".LoadPercentage"); + + return load_percentage; } NEXT_THROW (quickfix); } catch (string errstr) { + cout << query << endl; + cout << errstr << endl; pandoraLog ("getCpuUsagePercentage error. %s", errstr.c_str ()); } - + throw Pandora_Wmi_Exception (); } @@ -247,29 +211,24 @@ long Pandora_Wmi::getFreememory () { CDhInitialize init; CDispPtr wmi_svc, quickfixes; + long free_memory; - struct QFix { - long free_memory; - }; - try { - dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); + dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_PerfRawData_PerfOS_Memory ")); - + L".ExecQuery(%S)", + L"SELECT * FROM Win32_PerfRawData_PerfOS_Memory ")); + FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%d", &fix.free_memory, quickfix, - L".AvailableMBytes"); - - return fix.free_memory; + dhGetValue (L"%d", &free_memory, quickfix, + L".AvailableMBytes"); + + return free_memory; } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("getFreememory error. %s", errstr.c_str ()); } - + throw Pandora_Wmi_Exception (); } @@ -280,33 +239,29 @@ Pandora_Wmi::getFreememory () { */ string Pandora_Wmi::getOSName () { - CDhInitialize init; + CDhInitialize init; CDispPtr wmi_svc, quickfixes; + char *name = NULL; string ret; - - struct QFix { - CDhStringA name, state, description; - }; - + try { - dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); + dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_OperatingSystem ")); - + L".ExecQuery(%S)", + L"SELECT * FROM Win32_OperatingSystem ")); + FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%s", &fix.name, quickfix, - L".Caption"); - - ret = fix.name; - + dhGetValue (L"%s", &name, quickfix, + L".Caption"); + + ret = name; + dhFreeString (name); + } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("getOSName error. %s", errstr.c_str ()); } - + return ret; } @@ -317,33 +272,29 @@ Pandora_Wmi::getOSName () { */ string Pandora_Wmi::getOSVersion () { - CDhInitialize init; + CDhInitialize init; CDispPtr wmi_svc, quickfixes; + char *version = NULL; string ret; - - struct QFix { - CDhStringA name, state, description; - }; - + try { - dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); + dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_OperatingSystem ")); - + L".ExecQuery(%S)", + L"SELECT * FROM Win32_OperatingSystem ")); + FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%s", &fix.name, quickfix, - L".CSDVersion"); - - ret = fix.name; - + dhGetValue (L"%s", &version, quickfix, + L".CSDVersion"); + + ret = version; + dhFreeString (version); + } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("getOSVersion error. %s", errstr.c_str ()); } - + return ret; } @@ -354,28 +305,24 @@ Pandora_Wmi::getOSVersion () { */ string Pandora_Wmi::getOSBuild () { - CDhInitialize init; + CDhInitialize init; CDispPtr wmi_svc, quickfixes; + char *build = NULL; string ret; - - struct QFix { - CDhStringA name, state, description; - }; - + try { - dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); + dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_OperatingSystem ")); + L".ExecQuery(%S)", + L"SELECT * FROM Win32_OperatingSystem ")); FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%s", &fix.name, quickfix, - L".Version"); + dhGetValue (L"%s", &build, quickfix, + L".Version"); - ret = fix.name; - + ret = build; + dhFreeString (build); + } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("getOSBuild error. %s", errstr.c_str ()); @@ -391,28 +338,24 @@ Pandora_Wmi::getOSBuild () { */ string Pandora_Wmi::getSystemName () { - CDhInitialize init; + CDhInitialize init; CDispPtr wmi_svc, quickfixes; + char *name = NULL; string ret; - struct QFix { - CDhStringA name, state, description; - }; - try { - dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); + dhCheck (dhGetObject (getWmiStr (L"."), NULL, &wmi_svc)); dhCheck (dhGetValue (L"%o", &quickfixes, wmi_svc, - L".ExecQuery(%S)", - L"SELECT * FROM Win32_OperatingSystem ")); + L".ExecQuery(%S)", + L"SELECT * FROM Win32_OperatingSystem ")); FOR_EACH (quickfix, quickfixes, NULL) { - QFix fix = { 0 }; - - dhGetValue (L"%s", &fix.name, quickfix, - L".CSName"); + dhGetValue (L"%s", &name, quickfix, + L".CSName"); - ret = fix.name; - + ret = name; + dhFreeString (name); + } NEXT_THROW (quickfix); } catch (string errstr) { pandoraLog ("getSystemName error. %s", errstr.c_str ());