size_t PolyRegions::read(const std::string& location) { fs::path directory; try { directory = SC_FS_PATH(location); } catch ( ... ) { SEISCOMP_ERROR("Invalid path '%s'", location.c_str()); return 0; } if ( !fs::exists(directory) ) return regionCount(); fs::directory_iterator end_itr; std::vector<std::string> files; try { for ( fs::directory_iterator itr(directory); itr != end_itr; ++itr ) { if ( fs::is_directory(*itr) ) continue; if ( boost::regex_match(SC_FS_IT_LEAF(itr), boost::regex(".*\\.(?:fep)")) ) files.push_back(SC_FS_IT_STR(itr)); } } catch ( const std::exception &ex ) { SEISCOMP_ERROR("Reading regions: %s", ex.what()); return regionCount(); } std::sort(files.begin(), files.end()); for ( size_t i = 0; i < files.size(); ++i ) { if ( !readFepBoundaries(files[i]) ) SEISCOMP_ERROR("Error reading file: %s", files[i].c_str()); } info(); // Sort the features according to their rank std::sort(_regions.begin(), _regions.end(), compareByRank); // store directory path the data was read from _dataDir = directory.string(); return regionCount(); }
bool isValid() { static bool hasValidated = false; static bool validates = false; if ( hasValidated ) return validates; Environment *env = Environment::Instance(); if ( env == NULL ) { cerr << "FATAL ERROR: No environment available" << endl; return false; } hasValidated = true; string licenseDir = env->configDir() + "/key"; string licenseFile = licenseDir + "/License"; string licenseKeyfile = licenseDir + "/License.key"; string licenseSignature = licenseDir + "/License.signed"; boost::filesystem::path path = SC_FS_PATH(env->shareDir()) / SC_FS_PATH("licenses") / SC_FS_PATH("seiscomp3.crt"); if ( !Seiscomp::Util::fileExists(path.string().c_str()) ) { path = SC_FS_PATH(env->configDir()) / SC_FS_PATH("licenses") / SC_FS_PATH("seiscomp3.crt"); if ( !Seiscomp::Util::fileExists(path.string()) ) { path = SC_FS_PATH(env->configDir()) / SC_FS_PATH("key") / SC_FS_PATH("License.crt"); } } X509 *x509 = readCertificate(path.string()); if ( x509 ) { ASN1_TIME* notAfter = X509_get_notAfter(x509), * notBefore = X509_get_notBefore(x509); time_t ptime = time(NULL); int res = X509_cmp_time(notBefore, &ptime); if ( res == 0 || res > 0 ) { X509_free(x509); cerr << "FATAL ERROR: License has expired: " << path.string() << endl; return false; } res = X509_cmp_time(notAfter, &ptime); if ( res == 0 || res < 0 ) { X509_free(x509); cerr << "FATAL ERROR: License has expired: " << path.string() << endl; return false; } OpenSSL_add_all_algorithms(); OpenSSL_add_all_ciphers(); OpenSSL_add_all_digests(); EVP_PKEY* pkey=X509_get_pubkey(x509); if ( !pkey ) { X509_free(x509); EVP_cleanup(); cerr << "FATAL ERROR: License verification has failed: " << path.string() << endl; return false; } res = X509_verify(x509, pkey); if ( res != 1 ) { X509_free(x509); EVP_PKEY_free(pkey); EVP_cleanup(); cerr << "FATAL ERROR: License verification has failed: " << path.string() << endl; return false; } char *buf; if ( readNID(&buf, x509, NID_netscape_comment) ) { licenseText = buf; delete buf; } EVP_PKEY_free(pkey); X509_free(x509); EVP_cleanup(); return true; } // Read license file MD5_CTX ctx; MD5_Init(&ctx); unsigned char digest[MD5_DIGEST_LENGTH]; char data[64]; size_t len; ifstream f; try { f.open(licenseFile.c_str(), ios_base::in); } catch ( std::exception &e ) { cerr << "FATAL ERROR: Failed to open license file: " << licenseFile << endl; validates = false; return false; } if ( !f.good() ) { cerr << "FATAL ERROR: Failed to open license file: " << licenseFile << endl; validates = false; return false; } licenseText.clear(); try { while ( (len = f.rdbuf()->sgetn(data, sizeof(data))) > 0 ) { licenseText.append(data, len); MD5_Update(&ctx, data, len); } } catch ( ... ) { cerr << "FATAL ERROR: Invalid license file: " << licenseFile << endl; f.close(); validates = false; return false; } f.close(); MD5_Final(digest, &ctx); int strength = 0; RSA *publicKey = readKey(licenseKeyfile.c_str(), PUBLIC, 1024, 8192, strength); if ( publicKey == NULL ) { cerr << "FATAL ERROR: Invalid key file: " << licenseKeyfile << endl; validates = false; return false; } BIO *bio_file = NULL, *b64_file; b64_file = BIO_new(BIO_f_base64()); bio_file = BIO_new_file(licenseSignature.c_str(), "r"); bio_file = BIO_push(b64_file, bio_file); int sigLength = strength / 8; unsigned char *signature = new unsigned char[sigLength]; sigLength = BIO_read(bio_file, signature, sigLength); BIO_free_all(bio_file); if ( sigLength <= 0 ) { delete [] signature; cerr << "FATAL ERROR: Empty signature" << endl; validates = false; return false; } validates = RSA_verify(NID_md5, digest, MD5_DIGEST_LENGTH, signature, sigLength, publicKey); delete [] signature; /* if ( validates ) { cerr << "-----BEGIN LICENSE-----" << endl; cerr << licenseText << endl; cerr << "-----END LICENSE-----" << endl << endl; } */ return validates; }
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> bool SC3GF1DArchive::setSource(std::string source) { fs::path directory; try { directory = SC_FS_PATH(source); } catch ( ... ) { SEISCOMP_ERROR("Unable to open directory: %s", source.c_str()); return false; } _baseDirectory = source; fs::directory_iterator end_itr; try { for ( fs::directory_iterator itr(directory); itr != end_itr; ++itr ) { if ( !fs::is_directory(*itr) ) continue; std::string name = SC_FS_IT_LEAF(itr); /* size_t pos = name.rfind("_efl"); if ( pos == std::string::npos ) continue; */ std::string model = name/*.substr(0, pos)*/; std::ifstream ifDesc; int depthFrom = -1, depthTo = -1, depthSpacing = -1; int distanceFrom = -1, distanceTo = -1, distanceSpacing = -1; std::string line; ifDesc.open((_baseDirectory + "/" + name + ".desc").c_str()); if ( !ifDesc.is_open() ) { SEISCOMP_WARNING("Unable to find model description, skipping directory: %s", name.c_str()); continue; } bool validModel = true; DoubleList &depths = _models[model].depths; DoubleList &dists = _models[model].distances; while ( getline(ifDesc, line) ) { Core::trim(line); if ( line.empty() ) continue; if ( line[0] == '#' ) continue; std::stringstream ss(line); ss >> line; if ( line == "depth" ) { ss >> depthFrom >> depthTo >> depthSpacing; if ( (depthSpacing < 0) || (depthFrom > depthTo) ) { SEISCOMP_WARNING("Invalid description format, skipping directory: %s", name.c_str()); validModel = false; break; } if ( depthSpacing == 0 ) depths.insert(depthFrom); else { for ( int i = depthFrom; i <= depthTo; i += depthSpacing ) depths.insert(i); } } else if ( line == "distance" ) { ss >> distanceFrom >> distanceTo >> distanceSpacing; if ( (distanceSpacing < 0) || (distanceFrom > distanceTo) ) { SEISCOMP_WARNING("Invalid description format, skipping directory: %s", name.c_str()); validModel = false; break; } if ( distanceSpacing == 0 ) dists.insert(distanceFrom); else { for ( int i = distanceFrom; i <= distanceTo; i += distanceSpacing ) dists.insert(i); } } }