dca_interface  6.3.4
malware_samples/malwareenumsample/main.cpp
1 /* IBM Source Code */
2 /* (C) Copyright IBM Corp. 2009, 2012 */
3 /* Licensed Materials - Property of IBM */
4 /* US Government Users Restricted Rights - Use duplication or disclosure restricted by GSA Schedule Contract with IBM Corp. */
5 
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 #include <fstream>
38 #include <ctime>
39 
40 #include "dca/dca_base.h"
42 #include "dca/dca_callbacks.h"
43 
44 using namespace dca;
45 using namespace dca_malware;
46 
47 const std::string S_ToolName = "malwareenumsample";
48 const std::string S_ToolVersion = "1.2";
49 
54 const std::string S_UsageString =
55  "<redist-folder> <ticket> <product> [<locale>]\n"
56  " redist-folder - the folder where the DCA is installed\n"
57  " ticket - a valid ticket\n"
58  " product - the product associated with your ticket\n"
59  " locale - optional locale for the Malware category info names and descriptions, default = en_US\n\n"
60  ;
61 
66 #ifdef WIN32
67 # define DCA_BINDIR "bin/Win32"
68 #else
69 # define DCA_BINDIR "bin/linux"
70 #endif
71 
76 #define DCA_INITDIR "init"
77 
82 #define DCA_LOGDIR "./logs"
83 
94 static void SetupInitData( const std::string& strRedistFolder,
95  InitData& aInitData )
96 {
97  aInitData.binDir = strRedistFolder + DCA_BINDIR;
98  aInitData.initDir = strRedistFolder + DCA_INITDIR;
99  aInitData.logDir = DCA_LOGDIR;
100 }
101 
110 static void SetupLicense( const std::string& strTicket,
111  const std::string& strProduct,
112  LicenseData& aLicenseData )
113 {
114  aLicenseData.ticket = strTicket;
115  aLicenseData.product = strProduct;
116 }
117 
124 static void PrintLicenseInfo( const License& aLicense )
125 {
126  const time_t expirationDate = aLicense.getExpirationDate();
127  struct tm *expirationTime = localtime( &expirationDate );
128 
129  std::cout << "License Info:" << std::endl;
130  std::cout << " DCA is " << ( aLicense.isLicensed() ? "licensed." :
131  "not licensed." ) << std::endl;
132  std::cout << " MaxUsers:" << aLicense.getMaxUsers() <<
133  std::endl;
134  std::cout << " MaxSessions:" << aLicense.getMaxSessions() <<
135  std::endl;
136  std::cout << " Ticket:" << aLicense.getTicket() <<
137  std::endl;
138  std::cout << " Session:" << aLicense.getSession() <<
139  std::endl;
140  std::cout << " Last Message:" << aLicense.getLastMessage() <<
141  std::endl;
142  std::cout << " Expiration Date:" << asctime( expirationTime ) <<
143  std::endl;
144 }
145 
150 static void PrintToolHeader()
151 {
152  std::cout << "IBM DCA Sample: " << S_ToolName << " (" << S_ToolVersion << ")" << std::endl;
153 }
154 
161 static void PrintUsage( const char *pSampleName )
162 {
163  std::cout << pSampleName << " usage:" << std::endl;
164  std::cout << S_UsageString << std::endl;
165 }
166 
174 void PrintLocale( const MalwareLocale& aMalwareLocale,
175  const std::string& strIndent = "" )
176 {
177  std::cout << strIndent << "\tMalwareLocale '" << aMalwareLocale.languageId() <<
178  "' (displayName=" << aMalwareLocale.displayName() << ")" << std::endl;
179 }
180 
187 void EnumLocales( const MalwareLocales& aMalwareLocales )
188 {
189  const DCA_SIZE_TYPE numOfLocales = aMalwareLocales.size();
190  if( numOfLocales > 0 ) {
191  std::cout << std::endl << "MalwareLocales (size=" << numOfLocales << ")" << std::endl;
192  for( DCA_INDEX_TYPE i = 0; i < numOfLocales; ++i ) {
193  const MalwareLocale myMalwareLocale = aMalwareLocales[ i ];
194  PrintLocale( myMalwareLocale, "\t" );
195  }
196  }
197  else std::cout << std::endl;
198 }
199 
208 void PrintEnum( const MalwareEnum& aMalwareEnum,
209  const std::string& strLocale,
210  const std::string& strIndent = "" )
211 {
212  std::cout << strIndent << "MalwareEnum: '" << aMalwareEnum.name( strLocale )
213  << "' (id=" << aMalwareEnum.id() << ")" << std::endl;
214 }
215 
224 void EnumCategoryEnums( const MalwareCategoriesInfo& aMalwareCategoriesInfo,
225  const MalwareCategory& aMalwareCategory,
226  const std::string& strLocale )
227 {
228  const DCA_INDEX_TYPE maxEnumsToDisplay = 5;
229 
230  DCA_SIZE_TYPE numOfEnums = aMalwareCategory.enumsSize();
231  if( numOfEnums > 0 ) {
232  const MalwareEnums myMalwareEnums = aMalwareCategoriesInfo.getEnums();
233  std::cout << " (enumsSize=" << numOfEnums << ")" << std::endl;
234 
235  for( DCA_INDEX_TYPE i = 0; i < numOfEnums; i++ ) {
236  // if too many items to display shorten output and display only the last one
237  if( i > maxEnumsToDisplay && numOfEnums > maxEnumsToDisplay ) {
238  // Shorten output... and stop enumeration
239  std::cout << "\t\t ...(" << (numOfEnums - maxEnumsToDisplay - 2) <<
240  " more)..." << std::endl;
241  // display the last enum item
242  const DCA_ENUM_ID_TYPE myLastEnumId = aMalwareCategory.enumId( numOfEnums - 1 );
243  const MalwareEnum myLastMalwareEnum = myMalwareEnums.byId( myLastEnumId );
244  PrintEnum( myLastMalwareEnum, strLocale, "\t\t" );
245  return;
246  }
247  const DCA_ENUM_ID_TYPE enumId = aMalwareCategory.enumId( i );
248  const MalwareEnum myMalwareEnum = myMalwareEnums.byId( enumId );
249  PrintEnum( myMalwareEnum, strLocale, "\t\t" );
250  }
251  }
252  else std::cout << std::endl;
253 }
254 
263 void PrintCategory( const MalwareCategory& aMalwareCategory,
264  const std::string& strLocale,
265  const std::string& strIndent = "" )
266 {
267  std::cout << strIndent << "MalwareCategory: '" << aMalwareCategory.name( strLocale ) <<
268  "' (id=" << aMalwareCategory.id() << ")";
269 }
270 
279 void EnumCategories( const MalwareCategoriesInfo& aMalwareCategoriesInfo,
280  const MalwareCategories& aMalwareCategories,
281  const std::string& strLocale )
282 {
283  const DCA_SIZE_TYPE numOfCategories = aMalwareCategories.size();
284 
285  if( numOfCategories > 0 ) {
286  std::cout << std::endl << "MalwareCategories (size=" << numOfCategories <<
287  ")" << std::endl;
288 
289  for( DCA_INDEX_TYPE i = 0; i < numOfCategories; ++i ) {
290  const MalwareCategory myMalwareCategory = aMalwareCategories[ i ];
291  PrintCategory( myMalwareCategory, strLocale, "\t" );
292  EnumCategoryEnums( aMalwareCategoriesInfo, myMalwareCategory, strLocale );
293  }
294  }
295  else std::cout << std::endl;
296 }
297 
305 void EnumEnums( const MalwareEnums& aMalwareEnums,
306  const std::string& strLocale )
307 {
308  const DCA_INDEX_TYPE maxEnumsToDisplay = 8;
309  const DCA_SIZE_TYPE numOfEnums = aMalwareEnums.size();
310 
311  std::cout << std::endl << "MalwareEnums (size=" << numOfEnums <<
312  ")" << std::endl;
313 
314  if( numOfEnums > 0 ) {
315 
316  for( DCA_INDEX_TYPE i = 0; i < numOfEnums; ++i ) {
317  // if too many items to display shorten output and display only the last one
318  if( i > maxEnumsToDisplay && numOfEnums > maxEnumsToDisplay ) {
319  // Shorten output... and stop enumeration
320  std::cout << "\t ...(" << (numOfEnums - maxEnumsToDisplay - 2) <<
321  " more)..." << std::endl;
322 
323  // display the last one
324  const MalwareEnum myLastMalwareEnum = aMalwareEnums[ numOfEnums - 1 ];
325  PrintEnum( myLastMalwareEnum, strLocale, "\t" );
326  return;
327  }
328  const MalwareEnum myMalwareEnum = aMalwareEnums[i];
329  PrintEnum( myMalwareEnum, strLocale, "\t" );
330  }
331  }
332  else std::cout << std::endl;
333 }
334 
342 void EnumMalwareCategoriesInfo( const MalwareCategoriesInfo& aMalwareCategoriesInfo,
343  const std::string& strLocale )
344 {
345  std::cout << "Enumeration of all available MalwareLocale, MalwareCategory and MalwareEnum objects:"
346  << std::endl;
347 
348  const MalwareLocales myMalwareLocales = aMalwareCategoriesInfo.getLocales();
349  EnumLocales( myMalwareLocales );
350 
351  const MalwareCategories myMalwareCategories = aMalwareCategoriesInfo.getCategories();
352  EnumCategories( aMalwareCategoriesInfo, myMalwareCategories, strLocale );
353 
354  const MalwareEnums myMalwareEnums = aMalwareCategoriesInfo.getEnums();
355  EnumEnums( myMalwareEnums, strLocale );
356 }
357 
365 int main( int argc, char *argv[] )
366 {
367  PrintToolHeader();
368 
369  int rc = 5;
370 
371  try {
372  if( argc < 4 ) {
373  PrintUsage( argv[0] );
374  return 5;
375  }
376 
377  std::string strRedistFolder = argv[ 1 ];
378  const std::string strTicket = argv[ 2 ];
379  const std::string strProduct = argv[ 3 ];
380  std::string strLocale = "en_US"; // Default locale
381 
382  if (argc > 4) {
383  strLocale = argv[4];
384  }
385 
386  if( strRedistFolder.empty() ||
387  strTicket.empty() ||
388  strProduct.empty() )
389  {
390  PrintUsage( argv[0] );
391  return 5;
392  }
393 
394  // check for trailing fileslash - and add if necessary
395  const char c = strRedistFolder[ strRedistFolder.length() - 1 ];
396  if( c != '/' && c != '\\' )
397  strRedistFolder += "/";
398 
399  std::cout << std::endl;
400 
401  // init the 3rd party libraries
402  InitCUrl();
404 
405  // setup DCA directories
406  InitData myInitData;
407  SetupInitData( strRedistFolder, myInitData );
408 
409  // instantiate DCA API
410  DcaInstance myDca;
411  myDca = DcaInstance::create( myInitData );
412 
413  // setup license data
414  LicenseData myLicenseData;
415  SetupLicense( strTicket, strProduct, myLicenseData );
416  const License myLicense = myDca.createLicense( myLicenseData );
417 
418  PrintLicenseInfo( myLicense );
419 
420  if( myLicense.isLicensed( MalwareClassification::ID ) ) {
421  // initialize the Malware Classification module and create a MalwareCategoriesInfo instance
422  const MalwareClassification myMalwareClassification = MalwareClassification::create( myDca, myLicense );
423 
424  // create a Malware categories info for printing out the available locales, categories and enums
425  const MalwareCategoriesInfo myCategoriesInfo = myMalwareClassification.getCategoriesInfo();
426 
427  // call enumeration routine
428  EnumMalwareCategoriesInfo( myCategoriesInfo, strLocale );
429 
430  rc = 0;
431  }
432  }
433  catch( const ExDca& ex ) {
434  std::cerr << "DCA Exception occured. Details: " << ex.getDescription() <<
435  " (" << ex.getReturnCode() << ")." << std::endl;
436  rc = 10;
437  }
438  catch( const std::exception& s ) {
439  std::cerr << "std::exception occured. Details: " << s.what() << "." << std::endl;
440  rc = 10;
441  }
442  catch(...) {
443  std::cerr << "Unknown exception caught." << std::endl;
444  rc = 10;
445  }
446 
447  // deinit the 3rd party libraries
449  DeinitCUrl();
450 
451  return rc;
452 }
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Definition: base_classes.h:547
Main class for the Malware classification.
Exception class used in the DCA.
Definition: base_classes.h:237
void InitCUrl()
Initializes libcurl. Do not use any DCA function before initializing libcurl.
std::string initDir
the directory in which the DCA init files are stored
Definition: base_classes.h:266
This header includes all header files of the Malware Classification Package.
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
The MalwareCategory class contains information for a single category.
DCA_ENUM_ID_TYPE id() const
Returns the internal id of the enumerable item.
DCA_CATEGORY_ID_TYPE id() const
Returns the internal id of a MalwareCategory class.
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
std::string languageId() const
Returns the language id of the locale.
Container class for all Malware enumeration objects.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
Definition: base_classes.h:265
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
DCA_ENUM_ID_TYPE enumId(DCA_INDEX_TYPE index) const
Returns the id of an enum item associated with the current MalwareCategory.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the category.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
MalwareLocales getLocales() const
Returns the contained MalwareLocales.
Encapsulation of a locale class, which allows access to the language id and the display name of local...
#define DCA_LOGDIR
Relative directory for logfile(s).
Definition of a container class for MalwareEnum objects.
Definition: malware_enums.h:30
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
std::string name(const std::string &localeString=std::string()) const
Returns the localized (display) name of the enumerable item.
static void PrintToolHeader()
Prints out the name and the version of this sample.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
MalwareCategories getCategories() const
Returns the contained MalwareCategories.
bool isLicensed(DCA_MODULE_ID_TYPE id=0, bool force=false) const
Checks whether the given License is valid for the given module id.
static void PrintLicenseInfo(const License &aLicense)
Prints out the information about the provided License.
std::string ticket
The ticket as provided in the license.
Definition: base_classes.h:548
This header includes all header files of the DCA Base Package.
DCA_SIZE_TYPE size() const
Returns the number of locales in the container.
Definition of a container class for MalwareLocale objects.
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
std::string product
The product code used with the license.
Definition: base_classes.h:549
std::string logDir
the directory in which the DCA log file should be created
Definition: base_classes.h:267
static void PrintUsage(const char *name)
Prints out the syntax of the sample.
std::string getLastMessage() const
Returns the last message received from our license server or if none available the last available mes...
size_t DCA_INDEX_TYPE
Type for index access (used for arrays and collections).
Definition: base_types.h:66
size_t DCA_SIZE_TYPE
Type for size (used for size of array and collections).
Definition: base_types.h:72
DCA_SIZE_TYPE size() const
Returns the number of enums in the container.
std::string getTicket() const
Returns the ticket of the license as string.
MalwareCategoriesInfo getCategoriesInfo() const
Retrieve the MalwareCategoriesInfo class.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
Definition of a container class for MalwareCategory objects.
int DCA_ENUM_ID_TYPE
Id type for MalwareEnum class.
Definition: malware_base.h:21
std::string displayName() const
Returns the display name of the locale.
std::string getSession() const
Returns the session of the license as string.
MalwareEnum byId(DCA_ENUM_ID_TYPE id) const
Returns the MalwareEnums with the given id.
DCA_SIZE_TYPE size() const
Returns the number of categories in the container.
DCA_SIZE_TYPE enumsSize() const
Returns the count of MalwareEnum items associated with current MalwareCategory.
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264
MalwareEnums getEnums() const
Returns the contained MalwareEnums.
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
static void SetupLicense(const std::string &ticket, const std::string &product, LicenseData &licenseData)
Sets up the given licenseData by copying the given ticket and product strings.
The MalwareEnum class describes a single enumerable item (currebtly not used for malware)
Definition: malware_enum.h:26
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
int main(int argc, char *argv[])
The main routine.