libmoost
/home/mhx/git/github/libmoost/include/moost/nagios/detail/nsca_enctype.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 #ifndef MOOST_NAGIOS_NSCA_CLIENT_NSCA_ENCTYPE_HPP__
00029 #define MOOST_NAGIOS_NSCA_CLIENT_NSCA_ENCTYPE_HPP__
00030 
00031 #include <string>
00032 #include <stdexcept>
00033 #include <iostream>
00034 
00035 #include <boost/lexical_cast.hpp>
00036 #include <boost/shared_ptr.hpp>
00037 #include <boost/function.hpp>
00038 #include <boost/regex.hpp>
00039 
00040 #include "nsca_common.hpp"
00041 
00042 #define MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, enc) \
00043    if(boost::regex_match(str, boost::regex(#enc, boost::regex::perl|boost::regex::icase))) \
00044    return nsca_encryption_method::ENCRYPT_ ## enc
00045 
00046 namespace moost { namespace nagios {
00047 
00048 
00049    struct nsca_enctype
00050    {
00051       nsca_enctype(std::string const & enc)
00052          : enc(validate_encryption_method(str2enc(enc)))
00053       {
00054       }
00055 
00056       nsca_enctype(int const enc)
00057          : enc(validate_encryption_method(enc))
00058       {
00059       }
00060 
00061       nsca_enctype(nsca_encryption_method::type const enc =
00062             nsca_encryption_method::ENCRYPT_NONE)
00063          : enc(enc)
00064       {
00065       }
00066 
00067       nsca_encryption_method::type enc;
00068 
00069       operator nsca_encryption_method::type() const
00070       {
00071          return enc;
00072       }
00073 
00074       operator int() const
00075       {
00076          return enc;
00077       }
00078 
00079       static nsca_encryption_method::type validate_encryption_method(int encryption_method)
00080       {
00081          switch(encryption_method)
00082          {
00083          case nsca_encryption_method::ENCRYPT_NONE:
00084          case nsca_encryption_method::ENCRYPT_XOR:
00085             break;
00086 
00087 #ifdef HAVE_LIBMCRYPT
00088          // these methods require mcrypt
00089          case nsca_encryption_method::ENCRYPT_DES:
00090          case nsca_encryption_method::ENCRYPT_3DES:
00091          case nsca_encryption_method::ENCRYPT_CAST128:
00092          case nsca_encryption_method::ENCRYPT_CAST256:
00093          case nsca_encryption_method::ENCRYPT_XTEA:
00094          case nsca_encryption_method::ENCRYPT_3WAY:
00095          case nsca_encryption_method::ENCRYPT_BLOWFISH:
00096          case nsca_encryption_method::ENCRYPT_TWOFISH:
00097          case nsca_encryption_method::ENCRYPT_LOKI97:
00098          case nsca_encryption_method::ENCRYPT_RC2:
00099          case nsca_encryption_method::ENCRYPT_ARCFOUR:
00100          case nsca_encryption_method::ENCRYPT_RIJNDAEL128:
00101          case nsca_encryption_method::ENCRYPT_RIJNDAEL192:
00102          case nsca_encryption_method::ENCRYPT_RIJNDAEL256:
00103          case nsca_encryption_method::ENCRYPT_WAKE:
00104          case nsca_encryption_method::ENCRYPT_SERPENT:
00105          case nsca_encryption_method::ENCRYPT_ENIGMA:
00106          case nsca_encryption_method::ENCRYPT_GOST:
00107          case nsca_encryption_method::ENCRYPT_SAFER64:
00108          case nsca_encryption_method::ENCRYPT_SAFER128:
00109          case nsca_encryption_method::ENCRYPT_SAFERPLUS:
00110             break;
00111 #endif
00112          default:
00113             throw std::invalid_argument("unknown encryption method");
00114          }
00115 
00116          return nsca_encryption_method::type(encryption_method);
00117       }
00118 
00119       // just returns a string of all the possible encryption options for use in "help"
00120       static std::string get_enc_helpstr()
00121       {
00122          char const delim = '|';
00123          std::stringstream ss;
00124 
00125          ss << "none" << delim;
00126          ss << "xor";
00127 
00128 #ifdef HAVE_LIBMCRYPT
00129          ss << delim;
00130          ss << "xor" << delim;
00131          ss << "des" << delim;
00132          ss << "3des" << delim;
00133          ss << "cast128" << delim;
00134          ss << "cast256" << delim;
00135          ss << "xtea" << delim;
00136          ss << "3way" << delim;
00137          ss << "blowfish" << delim;
00138          ss << "twofish" << delim;
00139          ss << "loki97" << delim;
00140          ss << "rc2" << delim;
00141          ss << "arcfour" << delim;
00142          ss << "rijndael128" << delim;
00143          ss << "rijndael192" << delim;
00144          ss << "rijndael256" << delim;
00145          ss << "wake" << delim;
00146          ss << "serpent" << delim;
00147          ss << "enigma" << delim;
00148          ss << "gost" << delim;
00149          ss << "safer64" << delim;
00150          ss << "safer128" << delim;
00151          ss << "saferplus";
00152 #endif
00153 
00154          return ss.str();
00155       }
00156 
00157       // convert str into the relevant enum value
00158       static int str2enc(std::string const & str)
00159       {
00160          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, NONE);
00161          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, XOR);
00162 
00163 #ifdef HAVE_LIBMCRYPT
00164          // these are only avaliable if mcrypt is installed
00165          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, DES);
00166          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, 3DES);
00167          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, CAST128);
00168          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, CAST256);
00169          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, XTEA);
00170          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, 3WAY);
00171          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, BLOWFISH);
00172          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, TWOFISH);
00173          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, LOKI97);
00174          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, RC2);
00175          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, ARCFOUR);
00176          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, RIJNDAEL128);
00177          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, RIJNDAEL192);
00178          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, RIJNDAEL256);
00179          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, WAKE);
00180          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, SERPENT);
00181          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, ENIGMA);
00182          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, GOST);
00183          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, SAFER64);
00184          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, SAFER128);
00185          MOOST_NAGIOS_NSCA_CONFIG_STR2ENC__(str, SAFERPLUS);
00186 #endif
00187          // sorry, who are you?
00188          throw std::invalid_argument("unknown encryption method");
00189       }
00190    };
00191 
00192 }}
00193 
00194 #endif