/** * \brief Test pkgagent.c ProcessUpload function * give the upload_pk of debian binary package, * get the package information about this upload id */ void test_ProcessUpload() { struct debpkginfo *pi; long upload_pk; char *ErrorBuf; int predictValue = 0; pi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo)); /* perpare testing data in database */ db_conn = fo_dbconnect(DBConfFile, &ErrorBuf); upload_pk = prepare_Database(db_conn, pi); if (upload_pk == -1) CU_FAIL_FATAL("Prepare database data ERROR!"); if (prepare_Repository() == -1) { remove_Database(db_conn, pi, upload_pk); CU_FAIL_FATAL("Prepare repository data ERROR!"); } /* Test ProcessUpload function */ int Result = ProcessUpload(upload_pk); printf("ProcessUpload Result is:%d\n", Result); CU_ASSERT_EQUAL(Result, predictValue); /* Clear testing data in database */ if (remove_Database(db_conn, pi, upload_pk) == -1) CU_FAIL_FATAL("Remove database data ERROR!"); if (remove_Repository() == -1) CU_FAIL_FATAL("Remove repository data ERROR!"); PQfinish(db_conn); memset(pi, 0, sizeof(struct debpkginfo)); free(pi); }
/** * \brief main function for the pkgagent * * There are 2 ways to use the pkgagent agent: * 1. Command Line Analysis :: test a rpm file from the command line * 2. Agent Based Analysis :: run from the scheduler * * +-----------------------+ * | Command Line Analysis | * +-----------------------+ * * To analyze a rpm file from the command line: * file :: if files are rpm package listed, display their meta data * -v :: verbose (-vv = more verbose) * * example: * $ ./pkgagent rpmfile * * +----------------------+ * | Agent Based Analysis | * +----------------------+ * * To run the pkgagent as an agent simply run with no command line args * no file :: process data from the scheduler * -i :: initialize the database, then exit * * example: * $ upload_pk | ./pkgagent * * \param argc the number of command line arguments * \param argv the command line arguments * \return 0 on a successful program execution */ int main (int argc, char *argv[]) { int c; char *agent_desc = "Pulls metadata out of RPM or DEBIAN packages"; //struct rpmpkginfo *glb_rpmpi; //struct debpkginfo *glb_debpi; int Agent_pk; int ars_pk = 0; int upload_pk = 0; // the upload primary key int user_pk = 0; // the upload primary key char *AgentARSName = "pkgagent_ars"; int rv; PGresult *ars_result; char sqlbuf[1024]; char *COMMIT_HASH; char *VERSION; char agent_rev[MAXCMD]; int CmdlineFlag = 0; /* run from command line flag, 1 yes, 0 not */ fo_scheduler_connect(&argc, argv, &db_conn); //glb_rpmpi = (struct rpmpkginfo *)malloc(sizeof(struct rpmpkginfo)); //glb_debpi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo)); COMMIT_HASH = fo_sysconfig("pkgagent", "COMMIT_HASH"); VERSION = fo_sysconfig("pkgagent", "VERSION"); sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH); Agent_pk = fo_GetAgentKey(db_conn, basename(argv[0]), 0, agent_rev, agent_desc); /* Process command-line */ while((c = getopt(argc,argv,"ic:CvVh")) != -1) { switch(c) { case 'i': PQfinish(db_conn); /* DB was opened above, now close it and exit */ exit(0); case 'v': Verbose++; break; case 'c': break; /* handled by fo_scheduler_connect() */ case 'C': CmdlineFlag = 1; break; case 'V': printf("%s", BuildVersion); PQfinish(db_conn); return(0); default: Usage(argv[0]); PQfinish(db_conn); exit(-1); } } /* If no args, run from scheduler! */ if (CmdlineFlag == 0) { user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */ while(fo_scheduler_next()) { upload_pk = atoi(fo_scheduler_current()); /* Check Permissions */ if (GetUploadPerm(db_conn, upload_pk, user_pk) < PERM_WRITE) { LOG_ERROR("You have no update permissions on upload %d", upload_pk); continue; } if (Verbose) { printf("PKG: pkgagent read %d\n", upload_pk); } if (upload_pk ==0) continue; /* check if pkgagent ars table exist? * if exist, check duplicate request * if not exist, don't check duplicate request */ rv = fo_tableExists(db_conn, AgentARSName); if (rv) { /* check ars table to see if this is duplicate request*/ snprintf(sqlbuf, sizeof(sqlbuf), "select ars_pk from pkgagent_ars,agent \ where agent_pk=agent_fk and ars_success=true \ and upload_fk='%d' and agent_fk='%d'", upload_pk, Agent_pk); ars_result = PQexec(db_conn, sqlbuf); if (fo_checkPQresult(db_conn, ars_result, sqlbuf, __FILE__, __LINE__)) exit(-1); if (PQntuples(ars_result) > 0) { PQclear(ars_result); LOG_WARNING("Ignoring requested pkgagent analysis of upload %d - Results are already in database.\n",upload_pk); continue; } PQclear(ars_result); } /* Record analysis start in pkgagent_ars, the pkgagent audit trail. */ ars_pk = fo_WriteARS(db_conn, ars_pk, upload_pk, Agent_pk, AgentARSName, 0, 0); /* process the upload_pk pkgagent */ if(ProcessUpload(upload_pk) != 0) return -1; /* Record analysis success in pkgagent_ars. */ if (ars_pk) fo_WriteARS(db_conn, ars_pk, upload_pk, Agent_pk, AgentARSName, 0, 1); } } else { if (Verbose) { printf("DEBUG: running in cli mode, processing file(s)\n"); } for (; optind < argc; optind++) { struct rpmpkginfo *rpmpi; rpmpi = (struct rpmpkginfo *)malloc(sizeof(struct rpmpkginfo)); rpmReadConfigFiles(NULL, NULL); //if(ProcessUpload(atoi(argv[optind])) == 0) if(GetMetadata(argv[optind],rpmpi) != -1) printf("OK\n"); else printf("Fail\n"); #ifdef _RPM_4_4_COMPAT rpmFreeCrypto(); int i; for(i=0; i< rpmpi->req_size; i++) free(rpmpi->requires[i]); #endif /* After RPM4.4 version*/ free(rpmpi->requires); free(rpmpi); rpmFreeMacros(NULL); } } PQfinish(db_conn); fo_scheduler_disconnect(0); return(0); } /* main() */
int main (int argc, char *argv[]) { char Parm[MAXCMD]; int c; char *agent_desc = "Pulls metadata out of RPM or DEBIAN packages"; //struct rpmpkginfo *glb_rpmpi; //struct debpkginfo *glb_debpi; int Agent_pk; long upload_pk = 0; // the upload primary key extern int AlarmSecs; //glb_rpmpi = (struct rpmpkginfo *)malloc(sizeof(struct rpmpkginfo)); //glb_debpi = (struct debpkginfo *)malloc(sizeof(struct debpkginfo)); DB = DBopen(); if (!DB) { printf("FATAL: Unable to connect to database\n"); fflush(stdout); exit(-1); } Agent_pk = GetAgentKey(DB, basename(argv[0]), 0, SVN_REV, agent_desc); /* Process command-line */ while((c = getopt(argc,argv,"iv")) != -1) { switch(c) { case 'i': DBclose(DB); /* DB was opened above, now close it and exit */ exit(0); case 'v': Verbose++; break; default: Usage(argv[0]); DBclose(DB); exit(-1); } } /* If no args, run from scheduler! */ if (argc == 1) { signal(SIGALRM,ShowHeartbeat); alarm(AlarmSecs); printf("OK\n"); /* inform scheduler that we are ready */ fflush(stdout); while(ReadLine(stdin,Parm,MAXCMD) >= 0) { if (Verbose) { printf("PKG: pkgagent read %s\n", Parm);} fflush(stdout); upload_pk = atoi(Parm); if (upload_pk ==0) continue; if(!ProcessUpload(upload_pk)) return -1; sleep(15); printf("OK\n"); fflush(stdout); } } else { /* printf("DEBUG: running in cli mode, processing file(s)\n"); */ for (; optind < argc; optind++) { //struct rpmpkginfo *rpmpi; //rpmpi = (struct rpmpkginfo *)malloc(sizeof(struct rpmpkginfo)); //GetMetadata(argv[optind],rpmpi); ProcessUpload(atoi(argv[optind])); } } DBclose(DB); return(0); } /* main() */
bool Process(SSL_CTX* ctx, const char* id, const char* host, unsigned short port) { SocketSSLEx sock; if ( ! sock.Create(false)) { std::cerr << "Create socket failed!" << std::endl; return false; } if ( ! sock.Connect(ctx, host, port)) { std::cerr << "Can not connect to " << host << ":" << port << "!" << std::endl; sock.Close(); return false; } std::cout << "IDS Connected to CMS!" << std::endl; if (sizeof(SYMBOL) != sock.SendEx(SYMBOL, sizeof(SYMBOL))) { std::cerr << "Send symbol to CMS failed!" << std::endl; sock.Close(); return false; } if ( ! sock.SendPackage(id, strlen(id))) { std::cerr << "Send ID failed!" << std::endl; sock.Close(); return false; } std::cout << "Send ID '" << id << "' OK!" << std::endl; for (;;) { int n = sock.WaitData(100, true); if (n > 0) { char cmdData[4]; if (sizeof(cmdData) != sock.RecvEx(cmdData, sizeof(cmdData))) { std::cerr << "Read cmd failed!" << std::endl; break; } std::string cmd(cmdData, cmdData + 4); std::vector<char> result; if (cmd == "Send") { sending_data_tag = 1; if ( ! ProcessSend(sock, result)) { break; } } else if (cmd == "File") { sending_data_tag = 1; if ( ! ProcessUpload(sock, result)) { break; } } else if (cmd == "Down") { sending_data_tag = 1; if ( ! ProcessDownload(sock, result)) { break; } sending_data_tag = 0; check_time = time(NULL); continue; } else if (cmd == "Noop") { check_time = time(NULL); continue; } else { sending_data_tag = 1; std::string text = CreateJSON(1, "未知命令"); result.insert(result.begin(), text.begin(), text.end()); } if ( ! sock.SendPackage(&result[0], result.size())) { std::cerr << "Send result failed!" << std::endl; break; } sending_data_tag = 0; check_time = time(NULL); std::cout << "Send back data (" << result.size() << " bytes)." << std::endl; std::cout << std::string(result.begin(), result.end()) << std::endl; } } sock.Close(); return true; }