26 #include <json-c/json.h>
29 char BuildVersion[] =
"scanoss build version: " VERSION_S
" r(" COMMIT_HASH_S
").\n";
33 char *baseTMP =
"/tmp/scanoss";
42 extern int RebuildUpload(
long upload_pk,
char *tempFolder);
49 fptr = fopen(
"/home/fossy/snippet_scan.txt",
"a");
56 fprintf(fptr,
"->%s", msg);
60 void loadAgentConfiguration(PGconn *pg_conn)
63 char sqlA[] =
"select conf_value from sysconfig where variablename='ScAPIURL';";
65 result = PQexec(pg_conn, sqlA);
68 sprintf(ApiUrl,
"%s",
"");
70 if(PQgetisnull(result,0,0)){
71 char sqlHost[]=
"INSERT INTO sysconfig (variablename, conf_value, ui_label, vartype, group_name, group_order, description, validation_function, option_value) \
72 VALUES('ScAPIURL', '', 'SCANOSS API URL', 2, 'SCANOSS', 1, '(leave blank for default https://osskb.org/api/scan/direct))', NULL, NULL);";
73 result = PQexec(pg_conn, sqlHost);
75 LOG_ERROR(
"Can't default ScAPIURL") ;
77 sprintf(ApiUrl,
"%s",
"");
79 sprintf(ApiUrl,
"%s", PQgetvalue(result, 0, 0));
84 char sqlB[] =
"select conf_value from sysconfig where variablename='ScToken';";
86 result = PQexec(pg_conn, sqlB);
89 memset(accToken,
'\0',100);
92 if(PQgetisnull(result,0,0)){
93 char sqlToken[]=
"INSERT INTO sysconfig ( variablename, conf_value, ui_label, vartype, group_name, group_order, description, validation_function, option_value) \
94 VALUES( 'ScToken', '', 'SCANOSS access token', 2, 'SCANOSS', 2, 'Set token to access full scanning service.', NULL, NULL);";
95 result = PQexec(pg_conn, sqlToken);
97 LOG_ERROR(
"Can't store default ScToken") ;
99 printf(accToken,
"%s",
"");
101 sprintf(accToken,
"%s", PQgetvalue(result, 0, 0));
109 int createTables(PGconn*
pgConn)
116 snprintf(sql,
sizeof(sql),
"\
117 CREATE TABLE scanoss_fileinfo (\
118 pfile_fk int4 NOT NULL,\
119 matchtype text NULL,\
120 lineranges text NULL,\
123 filepath varchar NULL,\
124 fileinfo_pk serial4 NOT NULL\
127 result = PQexec(
pgConn, sql);
145 int main(
int argc,
char *argv[])
148 char *agent_desc =
"scanoss";
154 char *AgentARSName =
"scanoss_ars";
159 char agent_rev[MAXCMD];
166 sprintf(agent_rev,
"%s.%s", VERSION, COMMIT_HASH);
170 loadAgentConfiguration(
db_conn);
171 mkdir(baseTMP, 0700);
176 while ((c = getopt(argc, argv,
"ic:CvVh")) != -1)
189 strcpy(filename, argv[2]);
202 if (CmdlineFlag == 0)
210 LOG_ERROR(
"You have no update permissions on upload %d",
upload_pk);
220 memset(sqlbuf, 0,
sizeof(sqlbuf));
221 snprintf(sqlbuf,
sizeof(sqlbuf),
222 "select ars_pk from scanoss_ars,agent \
223 where agent_pk=agent_fk and ars_success=true \
224 and upload_fk='%d' and agent_fk='%d'",
230 char tempFolder[512];
231 sprintf(tempFolder,
"%s/%d",baseTMP,
upload_pk);
232 mkdir(tempFolder, 0700);
233 if (RebuildUpload(
upload_pk,tempFolder) != 0) {
234 LOG_ERROR(
"Error processing upload\n");
239 memset(cmdRemove,0,600);
240 sprintf(cmdRemove,
"rm -r %s",tempFolder);
241 FILE *removes = popen(cmdRemove,
"r");
251 char outputFile[MAXCMD];
252 unsigned char apiurl[410];
253 unsigned char key[110];
255 if(ApiUrl[0] !=
'\0') {
256 sprintf((
char *) apiurl,
"--apiurl %s", ApiUrl);
259 memset(apiurl, 0,
sizeof(apiurl));
262 if(accToken[0]!=
'\0' && accToken[0]!=
' ') {
263 sprintf((
char *)key,
"--key %s", accToken);
266 memset(key, 0,
sizeof(key));
269 char tempFolder[512];
270 sprintf(tempFolder,
"%s/%ld", baseTMP, time(NULL));
271 mkdir(tempFolder, 0700);
272 sprintf(outputFile,
"%s/result.json", tempFolder);
274 sprintf(Cmd,
"PYTHONPATH='/home/%s/pythondeps/' /home/%s/pythondeps/bin/scanoss-py "
275 "scan %s %s -o %s %s", FO_USER_S, FO_USER_S, apiurl, key,
276 outputFile, filename);
277 FILE *Fin = popen(Cmd,
"r");
279 LOG_ERROR(
"Snippet scan: failed to start scan %s", strerror(errno));
285 struct json_object *result_json = json_object_from_file(outputFile);
286 if (result_json == NULL) {
287 #if JSON_C_MINOR_VERSION > 12
288 LOG_ERROR(
"Unable to parse json output: %s", json_util_get_last_err());
290 LOG_ERROR(
"Unable to parse json output.");
294 sprintf(Cmd,
"rm -rf %s", tempFolder);
296 json_object_object_foreach(result_json, obj_filename, obj_val)
298 for (
int i = 0; i < json_object_array_length(obj_val); ++i) {
299 struct json_object *inner_obj = json_object_array_get_idx(obj_val, i);
300 struct json_object *licenses_array = json_object_object_get(inner_obj,
"licenses");
301 if (licenses_array == NULL) {
304 for (
int j = 0; j < json_object_array_length(licenses_array); ++j) {
305 struct json_object *license_obj = json_object_array_get_idx(licenses_array, j);
306 printf(
"%s -> %s\n", obj_filename,
307 json_object_get_string(json_object_object_get(license_obj,
"name")));
309 const char *matched = json_object_get_string(
310 json_object_object_get(inner_obj,
"matched"));
311 printf(
"%s matched with purls: ", matched);
312 struct json_object *purl_array = json_object_object_get(inner_obj,
"purl");
313 for (
int j = 0; j < json_object_array_length(purl_array); ++j) {
314 printf(
"%s,", json_object_get_string(
315 json_object_array_get_idx(purl_array, j)));
PGconn * pgConn
Database connection.
int Agent_pk
agent identifier
Usage()
Print Usage statement.
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
FUNCTION int fo_CreateARSTable(PGconn *pgConn, const char *tableName)
Create ars table if it doesn't already exist.
FUNCTION int fo_WriteARS(PGconn *pgConn, int ars_pk, int upload_pk, int agent_pk, const char *tableName, const char *ars_status, int ars_success)
Write ars record.
FUNCTION int fo_GetAgentKey(PGconn *pgConn, const char *agent_name, long Upload_pk, const char *rev, const char *agent_desc)
Get the latest enabled agent key (agent_pk) from the database.
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int fo_tableExists(PGconn *pgConn, const char *tableName)
Check if table exists. Note, this assumes the database name is 'fossology'.
int fo_checkPQcommand(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres commands (not select) If an error occured, write the error to s...
#define PERM_WRITE
Read-Write permission.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
char * fo_scheduler_current()
Get the last read string from the scheduler.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
void fo_scheduler_connect(int *argc, char **argv, PGconn **db_conn)
Establish a connection between an agent and the scheduler.
int main(int argc, char *argv[])
main function for the pkgagent
PGconn * db_conn
The connection to Database.
void ParseResults(char *folder)
Parse results from a temporary file and store results on database.
int ScanFolder(char *folder)
Scans a Temporary folder.