25 #define _GNU_SOURCE // for asprintf 27 #define ASPRINTF_MEM_ERROR 88 28 #define ASPRINTF_MEM_ERROR_LOG LOG_FATAL("Not enough memory for asprintf before line %d", __LINE__) 30 #include "wget_agent.h" 54 if (!Fname || (Fname[0]==
'\0'))
return(0);
55 if (Link) rc = stat64(Fname,&Stat);
56 else rc = lstat64(Fname,&Stat);
57 if (rc != 0)
return(0);
58 return(S_ISREG(Stat.st_mode));
80 if (NULL != strstr(URL,
"http://"))
return 7;
81 if (NULL != strstr(URL,
"https://"))
return 8;
82 if (NULL != strstr(URL,
"ftp://"))
return 6;
97 char *SHA1, *MD5, *Len;
105 memset(SHA256,
'\0',
sizeof(SHA256));
111 LOG_FATAL(
"upload %ld Unable to open temp file %s from %s",
130 if (rc) LOG_ERROR(
"chown failed on %s, error: %s",
GlobalTempFile, strerror(errno));
135 LOG_FATAL(
"upload %ld Unable to compute checksum for %s from %s",
142 LOG_FATAL(
"upload %ld No bytes downloaded from %s to %s.",
148 LOG_VERBOSE0(
"Unique %s",Unique);
152 LOG_VERBOSE0(
"Import Gold %s",Unique);
156 LOG_FATAL(
"upload %ld Failed to import %s from %s into repository gold %s",
165 if (rc) LOG_ERROR(
"chown failed on %s, error: %s", Path, strerror(errno));
173 LOG_VERBOSE0(
"Path is %s",Path);
177 LOG_FATAL(
"upload %ld Failed to determine repository location for %s in gold",
182 LOG_VERBOSE0(
"Import files %s",Path);
186 LOG_FATAL(
"upload %ld Failed to import %s from %s into files",
194 if (rc) LOG_ERROR(
"chown failed on %s, error: %s", Path, strerror(errno));
212 memset(SQL,
'\0',STRMAX);
213 snprintf(SQL,STRMAX-1,
"SELECT pfile_pk FROM pfile WHERE pfile_sha1 = '%.40s' AND pfile_md5 = '%.32s' AND pfile_size = %s;",
215 result = PQexec(
pgConn, SQL);
219 if (PQntuples(result) <=0)
222 memset(SQL,
'\0',STRMAX);
223 snprintf(SQL,STRMAX-1,
"INSERT INTO pfile (pfile_sha1, pfile_md5, pfile_sha256, pfile_size) VALUES ('%.40s','%.32s','%.64s',%s)",
224 SHA1,MD5,SHA256,Len);
226 result = PQexec(
pgConn, SQL);
229 result = PQexec(
pgConn,
"SELECT currval('pfile_pfile_pk_seq')");
233 PfileKey = atol(PQgetvalue(result,0,0));
234 LOG_VERBOSE0(
"pfile_pk = %ld",PfileKey);
238 result = PQexec(
pgConn,
"BEGIN");
241 memset(SQL,0,STRMAX);
242 snprintf(SQL,STRMAX-1,
"SELECT * FROM upload WHERE upload_pk=%ld FOR UPDATE;",
GlobalUploadKey);
244 result = PQexec(
pgConn, SQL);
247 memset(SQL,0,STRMAX);
248 snprintf(SQL,STRMAX-1,
"UPDATE upload SET pfile_fk=%ld WHERE upload_pk=%ld",
250 LOG_VERBOSE0(
"SQL=%s\n",SQL);
252 result = PQexec(
pgConn, SQL);
255 result = PQexec(
pgConn,
"COMMIT;");
285 memset(Sout,
'\0',SoutSize);
287 for(i=0,si=0; (si<SoutSize) && (Sin[i] !=
'\0'); i++)
289 if (Sin[i] ==
'#')
return(0);
290 if (!strchr(
"'`",Sin[i]) && !isspace(Sin[i])) Sout[si++] = Sin[i];
293 if (si+3 >= SoutSize)
return(0);
294 snprintf(Sout+si,4,
"%%%02X",Sin[i]);
298 return(Sin[i]==
'\0');
314 if (TempFile && TempFile[0])
318 return TempFileDirectory;
320 else if(TempFileDir && TempFileDir[0])
340 int GetURL(
char *TempFile,
char *URL,
char *TempFileDir)
343 char TaintedURL[STRMAX];
344 char TempFileDirectory[STRMAX+128];
345 char *delete_tmpdir_cmd;
349 memset(TempFileDirectory,
'\0',STRMAX+128);
352 sprintf(TempFileDirectory,
"%s.dir", TempFile);
353 res = asprintf(&delete_tmpdir_cmd,
"rm -rf %s", TempFileDirectory);
356 ASPRINTF_MEM_ERROR_LOG;
360 char WgetArgs[]=
"--no-check-certificate --progress=dot -rc -np -e robots=off";
363 char WgetArgs[]=
"--progress=dot -rc -np -e robots=off";
366 if (!
TaintURL(URL,TaintedURL,STRMAX))
368 LOG_FATAL(
"Failed to taint the URL '%s'",URL);
386 char no_proxy[STRMAX] = {0};
387 char proxy[STRMAX] = {0};
388 char proxy_temp[STRMAX] = {0};
394 snprintf(proxy_temp, STRMAX-1,
"export http_proxy='%s' ;",
GlobalProxy[0]);
395 strcat(proxy, proxy_temp);
399 snprintf(proxy_temp, STRMAX-1,
"export https_proxy='%s' ;",
GlobalProxy[1]);
400 strcat(proxy, proxy_temp);
404 snprintf(proxy_temp, STRMAX-1,
"export ftp_proxy='%s' ;",
GlobalProxy[2]);
405 strcat(proxy, proxy_temp);
409 snprintf(no_proxy, STRMAX-1,
"-e no_proxy='%s'",
GlobalProxy[3]);
417 res = asprintf(&cmd,
" %s /usr/bin/wget -q %s -P '%s' '%s' %s %s 2>&1",
418 proxy, WgetArgs, dest, TaintedURL,
GlobalParam, no_proxy);
422 res = asprintf(&cmd,
" %s /usr/bin/wget -q %s '%s' %s %s 2>&1",
423 proxy, WgetArgs, TaintedURL,
GlobalParam, no_proxy);
428 ASPRINTF_MEM_ERROR_LOG;
429 free(delete_tmpdir_cmd);
439 LOG_VERBOSE0(
"CMD: %s", cmd);
442 if (WIFEXITED(rc) && (WEXITSTATUS(rc) != 0))
444 LOG_FATAL(
"upload %ld Download failed; Return code %d from: %s",
GlobalUploadKey,WEXITSTATUS(rc),cmd);
446 rc_system = system(delete_tmpdir_cmd);
447 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
448 free(delete_tmpdir_cmd);
453 if (TempFile && TempFile[0])
460 LOG_FATAL(
"path %s is not http://, https://, or ftp://", TaintedURL);
462 rc_system = system(delete_tmpdir_cmd);
463 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
464 free(delete_tmpdir_cmd);
467 res = asprintf(&tmpfile_path,
"%s/%s", TempFileDirectory, TaintedURL + Position);
470 ASPRINTF_MEM_ERROR_LOG;
471 free(delete_tmpdir_cmd);
475 if (!stat(tmpfile_path, &sb))
477 if (S_ISDIR(sb.st_mode))
479 res = asprintf(&cmd,
"find '%s' -mindepth 1 -type d -empty -exec rmdir {} \\; > /dev/null 2>&1", tmpfile_path);
482 ASPRINTF_MEM_ERROR_LOG;
484 free(delete_tmpdir_cmd);
487 rc_system = system(cmd);
488 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, cmd)
491 res = asprintf(&cmd,
"tar -cf '%s' -C '%s' ./ 1>/dev/null", TempFile, tmpfile_path);
494 ASPRINTF_MEM_ERROR_LOG;
496 free(delete_tmpdir_cmd);
502 res = asprintf(&cmd,
"mv '%s' '%s' 2>&1", tmpfile_path, TempFile);
505 ASPRINTF_MEM_ERROR_LOG;
507 free(delete_tmpdir_cmd);
514 rc_system = system(cmd);
517 systemError(__LINE__, rc_system, cmd)
520 rc_system = system(delete_tmpdir_cmd);
521 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
522 free(delete_tmpdir_cmd);
529 res = asprintf(&cmd,
"find '%s' -type f -exec mv {} %s \\; > /dev/null 2>&1", TempFileDirectory, TempFile);
532 ASPRINTF_MEM_ERROR_LOG;
533 free(delete_tmpdir_cmd);
536 rc_system = system(cmd);
539 systemError(__LINE__, rc_system, cmd)
542 rc_system = system(delete_tmpdir_cmd);
543 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
544 free(delete_tmpdir_cmd);
551 if (TempFile && TempFile[0] && !
IsFile(TempFile,1))
553 LOG_FATAL(
"upload %ld File %s not created from URL: %s, CMD: %s",
GlobalUploadKey,TempFile,URL, cmd);
556 rc_system = system(delete_tmpdir_cmd);
557 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
558 free(delete_tmpdir_cmd);
565 rc_system = system(delete_tmpdir_cmd);
566 if (!WIFEXITED(rc_system)) systemError(__LINE__, rc_system, delete_tmpdir_cmd)
567 LOG_VERBOSE0(
"upload %ld Downloaded %s to %s",
GlobalUploadKey,URL,TempFile);
569 free(delete_tmpdir_cmd);
580 char *command = NULL;
581 char *tmp_file_directory;
582 char *delete_tmpdir_cmd;
587 char *homeenv = NULL;
590 homeenv = getenv(
"HOME");
591 if(NULL == homeenv) resethome = 1;
599 return ASPRINTF_MEM_ERROR;
602 setenv(
"HOME", tmp_home, 1);
609 ASPRINTF_MEM_ERROR_LOG;
610 return ASPRINTF_MEM_ERROR;
613 res = asprintf(&delete_tmpdir_cmd,
"rm -rf %s", tmp_file_directory);
616 ASPRINTF_MEM_ERROR_LOG;
617 free(tmp_file_directory);
618 return ASPRINTF_MEM_ERROR;
624 free(tmp_file_directory);
625 return ASPRINTF_MEM_ERROR;
627 rc = system(command);
633 setenv(
"HOME", homeenv, 1);
640 ASPRINTF_MEM_ERROR_LOG;
641 free(tmp_file_directory);
642 return ASPRINTF_MEM_ERROR;
644 systemError(__LINE__, rc, command)
649 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);
651 rc = system(delete_tmpdir_cmd);
652 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
654 free(tmp_file_directory);
655 free(delete_tmpdir_cmd);
659 res = asprintf(&command,
"tar -cf '%s' -C '%s' ./ 1>/dev/null",
GlobalTempFile, tmp_file_directory);
662 ASPRINTF_MEM_ERROR_LOG;
663 free(tmp_file_directory);
664 free(delete_tmpdir_cmd);
665 return ASPRINTF_MEM_ERROR;
667 free(tmp_file_directory);
668 rc = system(command);
671 systemError(__LINE__, rc, command)
673 rc = system(delete_tmpdir_cmd);
674 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
675 LOG_FATAL(
"DeleteTempDirCmd is:%s\n", delete_tmpdir_cmd);
676 free(delete_tmpdir_cmd);
681 rc = system(delete_tmpdir_cmd);
682 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
683 free(delete_tmpdir_cmd);
706 while(S[0] && isdigit(S[0])) S++;
707 while(S[0] && isspace(S[0])) S++;
714 while((GLen < STRMAX-4) && S[SLen] && !isspace(S[SLen]))
716 if ((S[SLen] ==
'\'') || isspace(S[SLen]) || !isprint(S[SLen]))
725 while(S[0] && isspace(S[0])) S++;
730 snprintf(
GlobalTempFile,STRMAX-1,
"%s/wget.%d",TempFileDir,getpid());
736 while((GLen < STRMAX-4) && S[SLen])
738 if ((S[SLen] ==
'\\') && isprint(S[SLen+1]))
740 LOG_FATAL(
"S[SLen] is:%c\n", S[SLen]);
745 else if ((S[SLen] !=
'\\') && isspace(S[SLen]))
break;
746 else if ((S[SLen] ==
'\'') || isspace(S[SLen]) || !isprint(S[SLen]))
748 sprintf(
GlobalURL+GLen,
"%%%02x",(
unsigned char)(S[SLen]));
756 while(S[0] && isspace(S[0])) S++;
758 char Type[][4] = {
"SVN",
"Git",
"CVS"};
791 NewPath = strdup(DirPath);
793 if ((subs = strstr(NewPath,
"%H")) )
796 gethostname(HostName,
sizeof(HostName));
799 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath, HostName, subs+2);
801 NewPath = strdup(TmpPath);
804 if ((subs = strstr(NewPath,
"%R")) )
809 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath,
fo_config_get(
sysconfig,
"FOSSOLOGY",
"path", NULL), subs+2);
811 NewPath = strdup(TmpPath);
831 int Archivefs(
char *Path,
char *TempFile,
char *TempFileDir,
struct stat Status)
837 res = asprintf(&cmd ,
"mkdir -p '%s' >/dev/null 2>&1", TempFileDir);
840 ASPRINTF_MEM_ERROR_LOG;
844 rc_system = system(cmd);
845 if (!WIFEXITED(rc_system))
847 LOG_FATAL(
"[%s:%d] Could not create temporary directory", __FILE__, __LINE__);
848 systemError(__LINE__, rc_system, cmd)
854 if (S_ISDIR(Status.st_mode))
856 res = asprintf(&cmd,
"tar %s -cf '%s' -C '%s' ./ 1>/dev/null",
GlobalParam, TempFile, Path);
859 ASPRINTF_MEM_ERROR_LOG;
862 rc_system = system(cmd);
863 if (!WIFEXITED(rc_system))
865 systemError(__LINE__, rc_system, cmd)
870 }
else if (strstr(Path,
"*"))
874 res = asprintf(&cmd,
"mkdir -p '%s/temp' > /dev/null 2>&1 && cp -r %s '%s/temp' > /dev/null 2>&1", TempFileDir, Path, TempFileDir);
877 ASPRINTF_MEM_ERROR_LOG;
880 rc_system = system(cmd);
883 systemError(__LINE__, rc_system, cmd)
888 res = asprintf(&cmd,
"tar -cf '%s' -C %s/temp ./ 1> /dev/null && rm -rf %s/temp > /dev/null 2>&1", TempFile, TempFileDir, TempFileDir);
891 ASPRINTF_MEM_ERROR_LOG;
894 rc_system = system(cmd);
897 systemError(__LINE__, rc_system, cmd)
902 }
else if(S_ISREG(Status.st_mode))
904 res = asprintf(&cmd,
"cp '%s' '%s' >/dev/null 2>&1", Path, TempFile);
907 ASPRINTF_MEM_ERROR_LOG;
910 rc_system = system(cmd);
913 systemError(__LINE__, rc_system, cmd)
932 char *http_proxy_host = NULL;
933 char *http_proxy_port = NULL;
934 char *http_temp = NULL;
936 for (i = 0; i < 6; i++)
940 GError* error1 = NULL;
941 GError* error2 = NULL;
942 GError* error3 = NULL;
943 GError* error4 = NULL;
962 if (http_proxy_port && http_proxy_port[0])
965 http_temp = strchr(http_proxy_port,
'/');
966 if (http_temp && http_temp[0])
968 count_temp = http_temp - http_proxy_port;
969 http_proxy_port[count_temp] = 0;
975 if (http_proxy_host && http_proxy_host[0])
990 printf(
"Usage: %s [options] [OBJ]\n",Name);
991 printf(
" -h :: help (print this message), then exit.\n");
992 printf(
" -i :: Initialize the DB connection then exit (nothing downloaded)\n");
993 printf(
" -g group :: Set the group on processed files (e.g., -g fossy).\n");
994 printf(
" -G :: Do NOT copy the file to the gold repository.\n");
995 printf(
" -d dir :: directory for downloaded file storage\n");
996 printf(
" -k key :: upload key identifier (number)\n");
997 printf(
" -A acclist :: Specify comma-separated lists of file name suffixes or patterns to accept.\n");
998 printf(
" -R rejlist :: Specify comma-separated lists of file name suffixes or patterns to reject.\n");
999 printf(
" -l depth :: Specify recursion maximum depth level depth. The default maximum depth is 5.\n");
1000 printf(
" -c configdir :: Specify the directory for the system configuration.\n");
1001 printf(
" -C :: run from command line.\n");
1002 printf(
" -v :: verbose (-vv = more verbose).\n");
1003 printf(
" -V :: print the version info, then exit.\n");
1004 printf(
" OBJ :: if a URL is listed, then it is retrieved.\n");
1005 printf(
" if a file is listed, then it used.\n");
1006 printf(
" if OBJ and Key are provided, then it is inserted into\n");
1007 printf(
" the DB and repository.\n");
1008 printf(
" no file :: process data from the scheduler.\n");
1020 #define PREFIXMAX 10 1022 const char needle[] =
" ";
1023 const char needle2[] =
"//";
1025 char *username = NULL;
1026 char *password = NULL;
1027 char http[PREFIXMAX] =
"";
1028 char URI[FILEPATH] =
"";
1031 char *additionalParams = NULL;
1040 strcpy(URI, temp + 2);
1041 if (strlen(
GlobalURL) - strlen(URI) > PREFIXMAX - 1)
1050 while( token != NULL )
1052 if (1 == index) username = token;
1055 additionalParams = token + strlen(token) + 1;
1058 token = strtok(NULL, needle);
1061 snprintf(
GlobalURL, URLMAX,
"%s%s:%s@%s", http, username, password, URI);
1063 if (strlen(additionalParams) > 0) {
1064 memmove(
GlobalParam, additionalParams, strlen(additionalParams) +1);
1077 const char needle[] =
" ";
1079 int secondIndex = 0;
1080 char *username = NULL;
1082 char newParam[STRMAX];
1086 memset(newParam,
'\0', STRMAX);
1092 while( token != NULL )
1098 token = strtok(NULL, needle);
1102 sprintf(newParam,
" --username %s --password ****", username);
1111 secondIndex = strcspn(
GlobalURL + index + 1,
":");
1112 index = index + secondIndex + 1;
1114 beg = (
char *)malloc(index + 2);
1115 memset(beg,
'\0', index + 2);
1119 sprintf(newParam,
"%s****%s", beg, end);
1132 char Type[][4] = {
"SVN",
"Git",
"CVS"};
1149 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);
1153 res = asprintf(&command,
"svn export %s %s %s --no-auth-cache >/dev/null 2>&1",
GlobalURL,
GlobalParam, tmpfile_dir);
1161 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);
1165 res = asprintf(&command,
"git clone %s %s %s >/dev/null 2>&1 && rm -rf %s/.git",
GlobalURL,
GlobalParam, tmpfile_dir, tmpfile_dir);
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
char * PathCheck(char *DirPath)
Check if path contains a "%H", "%R".
void MaskPassword()
Get the username from GlobalParam and create new parameters without password.
char * GlobalProxy[6]
Proxy from fossology.conf.
gid_t ForceGroup
Set to group id to be used for download files.
int Archivefs(char *Path, char *TempFile, char *TempFileDir, struct stat Status)
Copy downloaded files to temporary directory.
int GetPosition(char *URL)
Get the position (ending + 1) of http|https|ftp:// of one url.
long GlobalUploadKey
Input for this system.
char * GetVersionControlCommand(int withPassword)
get the command to run to get files from version control system
char GlobalParam[STRMAX]
Additional parameters.
int GetURL(char *TempFile, char *URL, char *TempFileDir)
Do the wget.
void Usage(char *Name)
Here are some suggested options.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
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, the error object is set and NULL is returned.
Cksum * SumComputeFile(FILE *Fin)
Compute the checksum, allocate and return a string containing the sum value.
char GlobalType[STRMAX]
Type of download (FILE/version control)
void replace_url_with_auth()
Translate authentication of git clone.
void GetProxy()
Get proxy from fossology.conf.
PGconn * pgConn
For the DB.
char GlobalURL[URLMAX]
URL to download.
void SafeExit(int rc)
Closes the connection to the server, free the database connection, and exit.
void SetEnv(char *S, char *TempFileDir)
Convert input pairs into globals.
char GlobalHttpProxy[STRMAX]
HTTP proxy command to use.
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...
void DBLoadGold()
Insert a file into the database and repository.
char GlobalTempFile[STRMAX]
Temp file to be used.
int GetVersionControl()
Get source code from version control system.
char * fo_RepMkPath(const char *Type, char *Filename)
Given a filename, construct the full path to the file.
Store check sum of a file.
uint64_t DataLen
Size of the file.
char * SumToString(Cksum *Sum)
Return string representing a Cksum. NOTE: The calling function must free() the string! ...
int GlobalImportGold
Set to 0 to not store file in gold repository.
int fo_RepImport(char *Source, char *Type, char *Filename, int Link)
Import a file into the repository.
int IsFile(char *Fname, int Link)
Given a filename, is it a file?
int TaintURL(char *Sin, char *Sout, int SoutSize)
Given a URL string, taint-protect it.
char * trim(char *ptext)
Trimming whitespace.
char * PrepareWgetDest(char *TempFile, char *TempFileDir, char *TempFileDirectory)
Prepare directory for wget.