int main(int argc, char **argv) { int nodeCount; int cleaning=0; if (argc < 4) { usage(argv); } if(strcmp("clean",argv[3])==0){ cleaning=1; } zoo_set_debug_level(ZOO_LOG_LEVEL_INFO); zoo_deterministic_conn_order(1); // enable deterministic order zh = zookeeper_init(argv[1], listener, 10000, 0, 0, 0); if (!zh) return errno; LOG_INFO(LOGSTREAM, "Checking server connection..."); ensureConnected(); if(cleaning==1){ int rc = 0; deletedCounter=0; rc=recursiveDelete(argv[2]); if(rc==ZOK){ LOG_INFO(LOGSTREAM, "Successfully deleted a subtree starting at %s (%d nodes)", argv[2],deletedCounter); exit(0); } exit(1); } nodeCount=atoi(argv[3]); createRoot(argv[2]); while(1) { ensureConnected(); LOG_INFO(LOGSTREAM, "Creating children for path %s",argv[2]); doCreateNodes(argv[2],nodeCount); waitCounter(); LOG_INFO(LOGSTREAM, "Starting the write cycle for path %s",argv[2]); doWrites(argv[2],nodeCount); waitCounter(); LOG_INFO(LOGSTREAM, "Starting the read cycle for path %s",argv[2]); doReads(argv[2],nodeCount); waitCounter(); LOG_INFO(LOGSTREAM, "Starting the delete cycle for path %s",argv[2]); doDeletes(argv[2],nodeCount); waitCounter(); } zookeeper_close(zh); return 0; }
QString OscapScannerRemoteSsh::createRemoteTemporaryDirectory(bool cancelOnFailure) { ensureConnected(); SshSyncProcess proc(mSshConnection, this); proc.setCommand("mktemp"); proc.setArguments(QStringList("-d")); proc.setCancelRequestSource(&mCancelRequested); proc.run(); if (proc.getExitCode() != 0) { emit errorMessage( QObject::tr("Failed to create a valid temporary dir. " "Diagnostic info: %1").arg(proc.getDiagnosticInfo()) ); if (cancelOnFailure) { mCancelRequested = true; } signalCompletion(mCancelRequested); return ""; } return proc.getStdOutContents().trimmed(); }
QString OscapScannerRemoteSsh::copyFileOver(const QString& localPath) { ensureConnected(); QString ret = createRemoteTemporaryFile(); { SshSyncProcess proc(mSshConnection, this); proc.setStdInFile(localPath); proc.setCommand("tee"); proc.setArguments(QStringList(ret)); proc.setCancelRequestSource(&mCancelRequested); proc.run(); if (proc.getExitCode() != 0) { emit errorMessage( QObject::tr("Failed to copy '%1' over to the remote machine! " "Diagnostic info:\n%2").arg(localPath).arg(proc.getDiagnosticInfo()) ); mCancelRequested = true; } } return ret; }
//----------------------------------------------------------------------------- // 描述: 转换字符串使之在SQL中合法 //----------------------------------------------------------------------------- string MySqlQuery::escapeString(const string& str) { if (str.empty()) return ""; int srcLen = (int)str.size(); Buffer buffer(srcLen * 2 + 1); char *end; ensureConnected(); end = (char*)buffer.data(); end += mysql_real_escape_string(&getConnObject(), end, str.c_str(), srcLen); *end = '\0'; return (char*)buffer.data(); }
//----------------------------------------------------------------------------- // 描述: 执行SQL (返回数据集) //----------------------------------------------------------------------------- DbDataSet* DbQuery::query() { ensureConnected(); DbDataSet *dataSet = database_->createDbDataSet(this); try { // 执行查询 doExecute(dataSet); // 初始化数据集 dataSet->initDataSet(); // 初始化数据集各字段的定义 dataSet->initFieldDefs(); } catch (Exception&) { delete dataSet; dataSet = NULL; throw; } return dataSet; }
QString OscapScannerRemoteSsh::copyInputFileOver() { ensureConnected(); QString localPath = ""; QTemporaryFile inputARFFile; inputARFFile.setAutoRemove(true); if (mScannerMode == SM_OFFLINE_REMEDIATION) { inputARFFile.open(); inputARFFile.write(getARFForRemediation()); inputARFFile.close(); localPath = inputARFFile.fileName(); } else { localPath = mSession->getOpenedFilePath(); } return copyFileOver(localPath); }
//----------------------------------------------------------------------------- // 描述: 取得查询器所用的数据库连接 //----------------------------------------------------------------------------- DbConnection* DbQuery::getDbConnection() { ensureConnected(); return dbConnection_; }
//----------------------------------------------------------------------------- // 描述: 执行SQL (无返回结果) //----------------------------------------------------------------------------- void DbQuery::execute() { ensureConnected(); doExecute(NULL); }
void OscapScannerRemoteSsh::evaluate() { if (mDryRun) { signalCompletion(mCancelRequested); return; } ensureConnected(); if (mCancelRequested) { signalCompletion(true); return; } { SshSyncProcess proc(mSshConnection, this); emit infoMessage(QObject::tr("Checking if oscap is available on remote machine...")); proc.setCommand(QString("command")); proc.setArguments(QStringList() << "-v" << SCAP_WORKBENCH_REMOTE_OSCAP_PATH); proc.setCancelRequestSource(&mCancelRequested); proc.run(); if (proc.getExitCode() != 0) { emit errorMessage( QObject::tr("Failed to locate oscap on remote machine. " "Please, check that openscap-scanner is installed on the remote machine.") ); mCancelRequested = true; signalCompletion(mCancelRequested); return; } emit infoMessage(QObject::tr("Querying capabilities on remote machine...")); proc.setCommand(SCAP_WORKBENCH_REMOTE_OSCAP_PATH); proc.setArguments(QStringList("-V")); proc.setCancelRequestSource(&mCancelRequested); proc.run(); if (proc.getExitCode() != 0) { emit errorMessage( QObject::tr("Failed to query capabilities of oscap on remote machine.\n" "Diagnostic info:\n%1").arg(proc.getDiagnosticInfo()) ); mCancelRequested = true; signalCompletion(mCancelRequested); return; } mCapabilities.parse(proc.getStdOutContents()); } if (!checkPrerequisites()) { mCancelRequested = true; signalCompletion(mCancelRequested); return; } QStringList baseArgs; baseArgs.append("-o"); baseArgs.append(QString("ControlPath=%1").arg(mSshConnection._getMasterSocket())); baseArgs.append(mTarget); QString diagnosticInfo; emit infoMessage(QObject::tr("Copying input data to remote target...")); const QString inputFile = copyInputFileOver(); const QString tailoringFile = mSession->hasTailoring() ? copyFileOver(mSession->getTailoringFilePath()) : QString(); if (mCancelRequested) { signalCompletion(true); return; } const QString reportFile = createRemoteTemporaryFile(); const QString resultFile = createRemoteTemporaryFile(); const QString arfFile = createRemoteTemporaryFile(); const QString workingDir = createRemoteTemporaryDirectory(); // TODO: We could be leaking any of the temporary files at this point! if (mCancelRequested) { signalCompletion(true); return; } QStringList args; if (mScannerMode == SM_OFFLINE_REMEDIATION) { args = buildOfflineRemediationArgs(inputFile, resultFile, reportFile, arfFile); } else { args = buildEvaluationArgs(inputFile, tailoringFile, resultFile, reportFile, arfFile, mScannerMode == SM_SCAN_ONLINE_REMEDIATION); } const QString sshCmd = args.join(" "); emit infoMessage(QObject::tr("Starting the remote process...")); QProcess process(this); process.start(SCAP_WORKBENCH_LOCAL_SSH_PATH, baseArgs + QStringList(QString("cd '%1'; " SCAP_WORKBENCH_REMOTE_OSCAP_PATH " %2").arg(workingDir).arg(sshCmd))); process.waitForStarted(); if (process.state() != QProcess::Running) { emit errorMessage(QObject::tr("Failed to start ssh. Perhaps the executable was not found?")); mCancelRequested = true; } const unsigned int pollInterval = 100; emit infoMessage(QObject::tr("Processing on the remote machine...")); while (!process.waitForFinished(pollInterval)) { // read everything new readStdOut(process); watchStdErr(process); // pump the event queue, mainly because the user might want to cancel QAbstractEventDispatcher::instance(mScanThread)->processEvents(QEventLoop::AllEvents); if (mCancelRequested) { emit infoMessage(QObject::tr("Cancellation was requested! Terminating...")); // TODO: On Windows we have to kill immediately, terminate() posts WM_CLOSE // but oscap doesn't have any event loop running. process.terminate(); break; } } if (mCancelRequested) { unsigned int waited = 0; while (!process.waitForFinished(pollInterval)) { waited += pollInterval; if (waited > 10000) // 10 seconds should be enough for the process to terminate { emit warningMessage(QObject::tr("The oscap process didn't terminate in time, it will be killed instead.")); // if it didn't terminate, we have to kill it at this point process.kill(); break; } } } else { // read everything left over readStdOut(process); watchStdErr(process); mResults = readRemoteFile(resultFile, QObject::tr("XCCDF results")).toUtf8(); mReport = readRemoteFile(reportFile, QObject::tr("XCCDF report (HTML)")).toUtf8(); mARF = readRemoteFile(arfFile, QObject::tr("Result DataStream (ARF)")).toUtf8(); } emit infoMessage(QObject::tr("Cleaning up...")); // Remove all the temporary remote files removeRemoteFile(inputFile, QObject::tr("input file")); if (!tailoringFile.isEmpty()) removeRemoteFile(tailoringFile, QObject::tr("tailoring file")); removeRemoteFile(resultFile, QObject::tr("XCCDF result file")); removeRemoteFile(reportFile, QObject::tr("XCCDF report file")); removeRemoteFile(arfFile, QObject::tr("Result DataStream file")); removeRemoteDirectory(workingDir, QObject::tr("Temporary Working Directory")); emit infoMessage(QObject::tr("Processing has been finished!")); signalCompletion(mCancelRequested); }