15 #define ASPRINTF_MEM_ERROR 88
16 #define ASPRINTF_MEM_ERROR_LOG LOG_FATAL("Not enough memory for asprintf before line %d", __LINE__)
18 #include "wget_agent.h"
42 if (!Fname || (Fname[0]==
'\0'))
return(0);
43 if (Link) rc = stat64(Fname,&Stat);
44 else rc = lstat64(Fname,&Stat);
45 if (rc != 0)
return(0);
46 return(S_ISREG(Stat.st_mode));
68 if (NULL != strstr(URL,
"http://"))
return 7;
69 if (NULL != strstr(URL,
"https://"))
return 8;
70 if (NULL != strstr(URL,
"ftp://"))
return 6;
85 char *SHA1, *MD5, *Len;
93 memset(SHA256,
'\0',
sizeof(SHA256));
99 LOG_FATAL(
"upload %ld Unable to open temp file %s from %s",
118 if (rc) LOG_ERROR(
"chown failed on %s, error: %s",
GlobalTempFile, strerror(errno));
123 LOG_FATAL(
"upload %ld Unable to compute checksum for %s from %s",
130 LOG_FATAL(
"upload %ld No bytes downloaded from %s to %s.",
136 LOG_VERBOSE0(
"Unique %s",Unique);
140 LOG_VERBOSE0(
"Import Gold %s",Unique);
144 LOG_FATAL(
"upload %ld Failed to import %s from %s into repository gold %s",
153 if (rc) LOG_ERROR(
"chown failed on %s, error: %s", Path, strerror(errno));
161 LOG_VERBOSE0(
"Path is %s",Path);
165 LOG_FATAL(
"upload %ld Failed to determine repository location for %s in gold",
170 LOG_VERBOSE0(
"Import files %s",Path);
174 LOG_FATAL(
"upload %ld Failed to import %s from %s into files",
182 if (rc) LOG_ERROR(
"chown failed on %s, error: %s", Path, strerror(errno));
200 memset(
SQL,
'\0',STRMAX);
201 snprintf(
SQL,STRMAX-1,
"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = %s;",
207 if (PQntuples(result) <=0)
210 memset(
SQL,
'\0',STRMAX);
211 snprintf(
SQL,STRMAX-1,
"INSERT INTO pfile (pfile_sha1, pfile_md5, pfile_sha256, pfile_size) VALUES ('%.40s','%.32s','%.64s',%s)",
212 SHA1,MD5,SHA256,Len);
217 result = PQexec(
pgConn,
"SELECT currval('pfile_pfile_pk_seq')");
221 PfileKey = atol(PQgetvalue(result,0,0));
222 LOG_VERBOSE0(
"pfile_pk = %ld",PfileKey);
226 result = PQexec(
pgConn,
"BEGIN");
229 memset(
SQL,0,STRMAX);
230 snprintf(
SQL,STRMAX-1,
"SELECT * FROM upload WHERE upload_pk=%ld FOR UPDATE;",
GlobalUploadKey);
235 memset(
SQL,0,STRMAX);
236 snprintf(
SQL,STRMAX-1,
"UPDATE upload SET pfile_fk=%ld WHERE upload_pk=%ld",
238 LOG_VERBOSE0(
"SQL=%s\n",
SQL);
243 result = PQexec(
pgConn,
"COMMIT;");
273 memset(Sout,
'\0',SoutSize);
275 for(i=0,si=0; (si<SoutSize) && (Sin[i] !=
'\0'); i++)
277 if (Sin[i] ==
'#')
return(0);
278 if (!strchr(
"'`",Sin[i]) && !isspace(Sin[i])) Sout[si++] = Sin[i];
281 if (si+3 >= SoutSize)
return(0);
282 snprintf(Sout+si,4,
"%%%02X",Sin[i]);
286 return(Sin[i]==
'\0');
302 if (TempFile && TempFile[0])
306 return TempFileDirectory;
308 else if(TempFileDir && TempFileDir[0])
328 int GetURL(
char *TempFile,
char *URL,
char *TempFileDir)
331 char TaintedURL[STRMAX];
332 char TempFileDirectory[STRMAX+128];
333 char *delete_tmpdir_cmd;
337 memset(TempFileDirectory,
'\0',STRMAX+128);
340 sprintf(TempFileDirectory,
"%s.dir", TempFile);
341 res = asprintf(&delete_tmpdir_cmd,
"rm -rf %s", TempFileDirectory);
344 ASPRINTF_MEM_ERROR_LOG;
348 char WgetArgs[]=
"--no-check-certificate --progress=dot -rc -np -e robots=off";
351 char WgetArgs[]=
"--progress=dot -rc -np -e robots=off";
354 if (!
TaintURL(URL,TaintedURL,STRMAX))
356 LOG_FATAL(
"Failed to taint the URL '%s'",URL);
374 char no_proxy[STRMAX] = {0};
375 char proxy[STRMAX] = {0};
376 char proxy_temp[STRMAX] = {0};
382 snprintf(proxy_temp, STRMAX-1,
"export http_proxy='%s' ;",
GlobalProxy[0]);
383 strcat(proxy, proxy_temp);
387 snprintf(proxy_temp, STRMAX-1,
"export https_proxy='%s' ;",
GlobalProxy[1]);
388 strcat(proxy, proxy_temp);
392 snprintf(proxy_temp, STRMAX-1,
"export ftp_proxy='%s' ;",
GlobalProxy[2]);
393 strcat(proxy, proxy_temp);
397 snprintf(no_proxy, STRMAX-1,
"-e no_proxy='%s'",
GlobalProxy[3]);
405 res = asprintf(&cmd,
" %s /usr/bin/wget -q %s -P '%s' '%s' %s %s 2>&1",
406 proxy, WgetArgs, dest, TaintedURL,
GlobalParam, no_proxy);
410 res = asprintf(&cmd,
" %s /usr/bin/wget -q %s '%s' %s %s 2>&1",
411 proxy, WgetArgs, TaintedURL,
GlobalParam, no_proxy);
416 ASPRINTF_MEM_ERROR_LOG;
417 free(delete_tmpdir_cmd);
427 LOG_VERBOSE0(
"CMD: %s", cmd);
430 if (WIFEXITED(rc) && (WEXITSTATUS(rc) != 0))
432 LOG_FATAL(
"upload %ld Download failed; Return code %d from: %s",
GlobalUploadKey,WEXITSTATUS(rc),cmd);
434 rc_system = system(delete_tmpdir_cmd);
435 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
436 free(delete_tmpdir_cmd);
441 if (TempFile && TempFile[0])
448 LOG_FATAL(
"path %s is not http://, https://, or ftp://", TaintedURL);
450 rc_system = system(delete_tmpdir_cmd);
451 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
452 free(delete_tmpdir_cmd);
455 res = asprintf(&tmpfile_path,
"%s/%s", TempFileDirectory, TaintedURL + Position);
458 ASPRINTF_MEM_ERROR_LOG;
459 free(delete_tmpdir_cmd);
463 if (!stat(tmpfile_path, &sb))
465 if (S_ISDIR(sb.st_mode))
467 res = asprintf(&cmd,
"find '%s' -mindepth 1 -type d -empty -exec rmdir {} \\; > /dev/null 2>&1", tmpfile_path);
470 ASPRINTF_MEM_ERROR_LOG;
472 free(delete_tmpdir_cmd);
475 rc_system = system(cmd);
476 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, cmd)
479 res = asprintf(&cmd,
"tar -cf '%s' -C '%s' ./ 1>/dev/null", TempFile, tmpfile_path);
482 ASPRINTF_MEM_ERROR_LOG;
484 free(delete_tmpdir_cmd);
490 res = asprintf(&cmd,
"mv '%s' '%s' 2>&1", tmpfile_path, TempFile);
493 ASPRINTF_MEM_ERROR_LOG;
495 free(delete_tmpdir_cmd);
502 rc_system = system(cmd);
505 systemError(__LINE__, rc_system, cmd)
508 rc_system = system(delete_tmpdir_cmd);
509 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
510 free(delete_tmpdir_cmd);
517 res = asprintf(&cmd,
"find '%s' -type f -exec mv {} %s \\; > /dev/null 2>&1", TempFileDirectory, TempFile);
520 ASPRINTF_MEM_ERROR_LOG;
521 free(delete_tmpdir_cmd);
524 rc_system = system(cmd);
527 systemError(__LINE__, rc_system, cmd)
530 rc_system = system(delete_tmpdir_cmd);
531 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
532 free(delete_tmpdir_cmd);
539 if (TempFile && TempFile[0] && !
IsFile(TempFile,1))
541 LOG_FATAL(
"upload %ld File %s not created from URL: %s, CMD: %s",
GlobalUploadKey,TempFile,URL, cmd);
544 rc_system = system(delete_tmpdir_cmd);
545 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
546 free(delete_tmpdir_cmd);
553 rc_system = system(delete_tmpdir_cmd);
554 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
555 LOG_VERBOSE0(
"upload %ld Downloaded %s to %s",
GlobalUploadKey,URL,TempFile);
557 free(delete_tmpdir_cmd);
568 char *command = NULL;
569 char *tmp_file_directory;
570 char *delete_tmpdir_cmd;
575 char *homeenv = NULL;
578 homeenv = getenv(
"HOME");
579 if(NULL == homeenv) resethome = 1;
587 return ASPRINTF_MEM_ERROR;
590 setenv(
"HOME", tmp_home, 1);
597 ASPRINTF_MEM_ERROR_LOG;
598 return ASPRINTF_MEM_ERROR;
601 res = asprintf(&delete_tmpdir_cmd,
"rm -rf %s", tmp_file_directory);
604 ASPRINTF_MEM_ERROR_LOG;
605 free(tmp_file_directory);
606 return ASPRINTF_MEM_ERROR;
612 free(tmp_file_directory);
613 return ASPRINTF_MEM_ERROR;
615 rc = system(command);
621 setenv(
"HOME", homeenv, 1);
628 ASPRINTF_MEM_ERROR_LOG;
629 free(tmp_file_directory);
630 return ASPRINTF_MEM_ERROR;
632 systemError(__LINE__, rc, command)
637 LOG_FATAL(
"please make sure the URL of repo is correct, also add correct proxy for your version control system, command is:%s, GlobalTempFile is:%s, rc is:%d. \n", command,
GlobalTempFile, rc);
639 rc = system(delete_tmpdir_cmd);
640 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
642 free(tmp_file_directory);
643 free(delete_tmpdir_cmd);
647 res = asprintf(&command,
"tar -cf '%s' -C '%s' ./ 1>/dev/null",
GlobalTempFile, tmp_file_directory);
650 ASPRINTF_MEM_ERROR_LOG;
651 free(tmp_file_directory);
652 free(delete_tmpdir_cmd);
653 return ASPRINTF_MEM_ERROR;
655 free(tmp_file_directory);
656 rc = system(command);
659 systemError(__LINE__, rc, command)
661 rc = system(delete_tmpdir_cmd);
662 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
663 LOG_FATAL(
"DeleteTempDirCmd is:%s\n", delete_tmpdir_cmd);
664 free(delete_tmpdir_cmd);
669 rc = system(delete_tmpdir_cmd);
670 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
671 free(delete_tmpdir_cmd);
694 while(S[0] && isdigit(S[0])) S++;
695 while(S[0] && isspace(S[0])) S++;
702 while((GLen < STRMAX-4) && S[SLen] && !isspace(S[SLen]))
704 if ((S[SLen] ==
'\'') || isspace(S[SLen]) || !isprint(S[SLen]))
713 while(S[0] && isspace(S[0])) S++;
718 snprintf(
GlobalTempFile,STRMAX-1,
"%s/wget.%d",TempFileDir,getpid());
724 while((GLen < STRMAX-4) && S[SLen])
726 if ((S[SLen] ==
'\\') && isprint(S[SLen+1]))
728 LOG_FATAL(
"S[SLen] is:%c\n", S[SLen]);
733 else if ((S[SLen] !=
'\\') && isspace(S[SLen]))
break;
734 else if ((S[SLen] ==
'\'') || isspace(S[SLen]) || !isprint(S[SLen]))
736 sprintf(
GlobalURL+GLen,
"%%%02x",(
unsigned char)(S[SLen]));
744 while(S[0] && isspace(S[0])) S++;
746 char Type[][4] = {
"SVN",
"Git",
"CVS"};
779 NewPath = strdup(DirPath);
781 if ((subs = strstr(NewPath,
"%H")) )
784 gethostname(HostName,
sizeof(HostName));
787 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath, HostName, subs+2);
789 NewPath = strdup(TmpPath);
792 if ((subs = strstr(NewPath,
"%R")) )
797 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath,
fo_config_get(
sysconfig,
"FOSSOLOGY",
"path", NULL), subs+2);
799 NewPath = strdup(TmpPath);
819 int Archivefs(
char *Path,
char *TempFile,
char *TempFileDir,
struct stat Status)
825 res = asprintf(&cmd ,
"mkdir -p '%s' >/dev/null 2>&1", TempFileDir);
828 ASPRINTF_MEM_ERROR_LOG;
832 rc_system = system(cmd);
833 if (!WIFEXITED(rc_system))
835 LOG_FATAL(
"[%s:%d] Could not create temporary directory", __FILE__, __LINE__);
836 systemError(__LINE__, rc_system, cmd)
842 if (S_ISDIR(Status.st_mode))
844 res = asprintf(&cmd,
"tar %s -cf '%s' -C '%s' ./ 1>/dev/null",
GlobalParam, TempFile, Path);
847 ASPRINTF_MEM_ERROR_LOG;
850 rc_system = system(cmd);
851 if (!WIFEXITED(rc_system))
853 systemError(__LINE__, rc_system, cmd)
858 }
else if (strstr(Path,
"*"))
862 res = asprintf(&cmd,
"mkdir -p '%s/temp' > /dev/null 2>&1 && cp -r %s '%s/temp' > /dev/null 2>&1", TempFileDir, Path, TempFileDir);
865 ASPRINTF_MEM_ERROR_LOG;
868 rc_system = system(cmd);
871 systemError(__LINE__, rc_system, cmd)
876 res = asprintf(&cmd,
"tar -cf '%s' -C %s/temp ./ 1> /dev/null && rm -rf %s/temp > /dev/null 2>&1", TempFile, TempFileDir, TempFileDir);
879 ASPRINTF_MEM_ERROR_LOG;
882 rc_system = system(cmd);
885 systemError(__LINE__, rc_system, cmd)
890 }
else if(S_ISREG(Status.st_mode))
892 res = asprintf(&cmd,
"cp '%s' '%s' >/dev/null 2>&1", Path, TempFile);
895 ASPRINTF_MEM_ERROR_LOG;
898 rc_system = system(cmd);
901 systemError(__LINE__, rc_system, cmd)
920 char *http_proxy_host = NULL;
921 char *http_proxy_port = NULL;
922 char *http_temp = NULL;
924 for (i = 0; i < 6; i++)
928 GError* error1 = NULL;
929 GError* error2 = NULL;
930 GError* error3 = NULL;
931 GError* error4 = NULL;
950 if (http_proxy_port && http_proxy_port[0])
953 http_temp = strchr(http_proxy_port,
'/');
954 if (http_temp && http_temp[0])
956 count_temp = http_temp - http_proxy_port;
957 http_proxy_port[count_temp] = 0;
963 if (http_proxy_host && http_proxy_host[0])
978 printf(
"Usage: %s [options] [OBJ]\n",Name);
979 printf(
" -h :: help (print this message), then exit.\n");
980 printf(
" -i :: Initialize the DB connection then exit (nothing downloaded)\n");
981 printf(
" -g group :: Set the group on processed files (e.g., -g fossy).\n");
982 printf(
" -G :: Do NOT copy the file to the gold repository.\n");
983 printf(
" -d dir :: directory for downloaded file storage\n");
984 printf(
" -k key :: upload key identifier (number)\n");
985 printf(
" -A acclist :: Specify comma-separated lists of file name suffixes or patterns to accept.\n");
986 printf(
" -R rejlist :: Specify comma-separated lists of file name suffixes or patterns to reject.\n");
987 printf(
" -l depth :: Specify recursion maximum depth level depth. The default maximum depth is 5.\n");
988 printf(
" -c configdir :: Specify the directory for the system configuration.\n");
989 printf(
" -C :: run from command line.\n");
990 printf(
" -v :: verbose (-vv = more verbose).\n");
991 printf(
" -V :: print the version info, then exit.\n");
992 printf(
" OBJ :: if a URL is listed, then it is retrieved.\n");
993 printf(
" if a file is listed, then it used.\n");
994 printf(
" if OBJ and Key are provided, then it is inserted into\n");
995 printf(
" the DB and repository.\n");
996 printf(
" no file :: process data from the scheduler.\n");
1008 #define PREFIXMAX 10
1010 const char needle[] =
" ";
1011 const char needle2[] =
"//";
1013 char *username = NULL;
1014 char *password = NULL;
1015 char http[PREFIXMAX] =
"";
1016 char URI[FILEPATH] =
"";
1019 char *additionalParams = NULL;
1028 strcpy(URI, temp + 2);
1029 if (strlen(
GlobalURL) - strlen(URI) > PREFIXMAX - 1)
1038 while( token != NULL )
1040 if (1 == index) username = token;
1043 additionalParams = token + strlen(token) + 1;
1046 token = strtok(NULL, needle);
1049 snprintf(
GlobalURL, URLMAX-1,
"%s%s:%s@%s", http, username, password, URI);
1051 if (strlen(additionalParams) > 0) {
1052 memmove(
GlobalParam, additionalParams, strlen(additionalParams) +1);
1065 const char needle[] =
" ";
1067 int secondIndex = 0;
1068 char *username = NULL;
1070 char newParam[STRMAX];
1074 memset(newParam,
'\0', STRMAX);
1080 while( token != NULL )
1086 token = strtok(NULL, needle);
1090 sprintf(newParam,
" --username %s --password ****", username);
1099 secondIndex = strcspn(
GlobalURL + index + 1,
":");
1100 index = index + secondIndex + 1;
1102 beg = (
char *)malloc(index + 2);
1103 memset(beg,
'\0', index + 2);
1107 sprintf(newParam,
"%s****%s", beg, end);
1120 char Type[][4] = {
"SVN",
"Git",
"CVS"};
1137 res = asprintf(&command,
"svn --config-option servers:global:http-proxy-host=%s --config-option servers:global:http-proxy-port=%s export %s %s %s --no-auth-cache >/dev/null 2>&1",
GlobalProxy[4],
GlobalProxy[5],
GlobalURL,
GlobalParam, tmpfile_dir);
1141 res = asprintf(&command,
"svn export %s %s %s --no-auth-cache >/dev/null 2>&1",
GlobalURL,
GlobalParam, tmpfile_dir);
1149 res = asprintf(&command,
"git config --global http.proxy %s && git clone %s %s %s && rm -rf %s/.git",
GlobalProxy[0],
GlobalURL,
GlobalParam, tmpfile_dir, tmpfile_dir);
1153 res = asprintf(&command,
"git clone %s %s %s >/dev/null 2>&1 && rm -rf %s/.git",
GlobalURL,
GlobalParam, tmpfile_dir, tmpfile_dir);
char * SumToString(Cksum *Sum)
Return string representing a Cksum. NOTE: The calling function must free() the string!
Cksum * SumComputeFile(FILE *Fin)
Compute the checksum, allocate and return a string containing the sum value.
char * trim(char *ptext)
Trimming whitespace.
char * fo_config_get(fo_conf *conf, const char *group, const char *key, GError **error)
Gets an element based on its group name and key name. If the group or key is not found,...
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
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...
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
Store check sum of a file.
uint64_t DataLen
Size of the file.
int IsFile(char *Fname, int Link)
Given a filename, is it a file?
char GlobalParam[STRMAX]
Additional parameters.
void Usage(char *Name)
Here are some suggested options.
char * GetVersionControlCommand(int withPassword)
get the command to run to get files from version control system
char GlobalType[STRMAX]
Type of download (FILE/version control)
int GetURL(char *TempFile, char *URL, char *TempFileDir)
Do the wget.
char * GlobalProxy[6]
Proxy from fossology.conf.
int GetPosition(char *URL)
Get the position (ending + 1) of http|https|ftp:// of one url.
char GlobalHttpProxy[STRMAX]
HTTP proxy command to use.
int TaintURL(char *Sin, char *Sout, int SoutSize)
Given a URL string, taint-protect it.
char * PrepareWgetDest(char *TempFile, char *TempFileDir, char *TempFileDirectory)
Prepare directory for wget.
PGconn * pgConn
For the DB.
char GlobalURL[URLMAX]
URL to download.
int GetVersionControl()
Get source code from version control system.
void SetEnv(char *S, char *TempFileDir)
Convert input pairs into globals.
long GlobalUploadKey
Input for this system.
gid_t ForceGroup
Set to group id to be used for download files.
void MaskPassword()
Get the username from GlobalParam and create new parameters without password.
void SafeExit(int rc)
Closes the connection to the server, free the database connection, and exit.
void replace_url_with_auth()
Translate authentication of git clone.
char GlobalTempFile[STRMAX]
Temp file to be used.
char * PathCheck(char *DirPath)
Check if path contains a "%H", "%R".
void DBLoadGold()
Insert a file into the database and repository.
void GetProxy()
Get proxy from fossology.conf.
int GlobalImportGold
Set to 0 to not store file in gold repository.
int Archivefs(char *Path, char *TempFile, char *TempFileDir, struct stat Status)
Copy downloaded files to temporary directory.