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;
621 mode_t old_umask = umask(0007);
622 rc = system(command);
629 setenv(
"HOME", homeenv, 1);
636 ASPRINTF_MEM_ERROR_LOG;
637 free(tmp_file_directory);
638 return ASPRINTF_MEM_ERROR;
640 systemError(__LINE__, rc, command)
645 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);
647 rc = system(delete_tmpdir_cmd);
648 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
650 free(tmp_file_directory);
651 free(delete_tmpdir_cmd);
655 res = asprintf(&command,
"tar -cf '%s' -C '%s' ./ 1>/dev/null",
GlobalTempFile, tmp_file_directory);
658 ASPRINTF_MEM_ERROR_LOG;
659 free(tmp_file_directory);
660 free(delete_tmpdir_cmd);
661 return ASPRINTF_MEM_ERROR;
663 free(tmp_file_directory);
664 rc = system(command);
667 systemError(__LINE__, rc, command)
669 rc = system(delete_tmpdir_cmd);
670 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
671 LOG_FATAL(
"DeleteTempDirCmd is:%s\n", delete_tmpdir_cmd);
672 free(delete_tmpdir_cmd);
677 rc = system(delete_tmpdir_cmd);
678 if (!WIFEXITED(rc)) systemError(__LINE__, rc, delete_tmpdir_cmd)
679 free(delete_tmpdir_cmd);
702 while(S[0] && isdigit(S[0])) S++;
703 while(S[0] && isspace(S[0])) S++;
710 while((GLen < STRMAX-4) && S[SLen] && !isspace(S[SLen]))
712 if ((S[SLen] ==
'\'') || isspace(S[SLen]) || !isprint(S[SLen]))
721 while(S[0] && isspace(S[0])) S++;
726 snprintf(
GlobalTempFile,STRMAX-1,
"%s/wget.%d",TempFileDir,getpid());
732 while((GLen < STRMAX-4) && S[SLen])
734 if ((S[SLen] ==
'\\') && isprint(S[SLen+1]))
736 LOG_FATAL(
"S[SLen] is:%c\n", S[SLen]);
741 else if ((S[SLen] !=
'\\') && isspace(S[SLen]))
break;
742 else if ((S[SLen] ==
'\'') || isspace(S[SLen]) || !isprint(S[SLen]))
744 sprintf(
GlobalURL+GLen,
"%%%02x",(
unsigned char)(S[SLen]));
752 while(S[0] && isspace(S[0])) S++;
754 char Type[][4] = {
"SVN",
"Git",
"CVS"};
787 NewPath = strdup(DirPath);
789 if ((subs = strstr(NewPath,
"%H")) )
792 gethostname(HostName,
sizeof(HostName));
795 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath, HostName, subs+2);
797 NewPath = strdup(TmpPath);
800 if ((subs = strstr(NewPath,
"%R")) )
805 snprintf(TmpPath,
sizeof(TmpPath),
"%s%s%s", NewPath,
fo_config_get(
sysconfig,
"FOSSOLOGY",
"path", NULL), subs+2);
807 NewPath = strdup(TmpPath);
827 int Archivefs(
char *Path,
char *TempFile,
char *TempFileDir,
struct stat Status)
833 res = asprintf(&cmd ,
"mkdir -p '%s' >/dev/null 2>&1", TempFileDir);
836 ASPRINTF_MEM_ERROR_LOG;
840 rc_system = system(cmd);
841 if (!WIFEXITED(rc_system))
843 LOG_FATAL(
"[%s:%d] Could not create temporary directory", __FILE__, __LINE__);
844 systemError(__LINE__, rc_system, cmd)
850 if (S_ISDIR(Status.st_mode))
852 res = asprintf(&cmd,
"tar %s -cf '%s' -C '%s' ./ 1>/dev/null",
GlobalParam, TempFile, Path);
855 ASPRINTF_MEM_ERROR_LOG;
858 rc_system = system(cmd);
859 if (!WIFEXITED(rc_system))
861 systemError(__LINE__, rc_system, cmd)
866 }
else if (strstr(Path,
"*"))
870 res = asprintf(&cmd,
"mkdir -p '%s/temp' > /dev/null 2>&1 && cp -r %s '%s/temp' > /dev/null 2>&1", TempFileDir, Path, TempFileDir);
873 ASPRINTF_MEM_ERROR_LOG;
876 rc_system = system(cmd);
879 systemError(__LINE__, rc_system, cmd)
884 res = asprintf(&cmd,
"tar -cf '%s' -C %s/temp ./ 1> /dev/null && rm -rf %s/temp > /dev/null 2>&1", TempFile, TempFileDir, TempFileDir);
887 ASPRINTF_MEM_ERROR_LOG;
890 rc_system = system(cmd);
893 systemError(__LINE__, rc_system, cmd)
898 }
else if(S_ISREG(Status.st_mode))
900 res = asprintf(&cmd,
"cp '%s' '%s' >/dev/null 2>&1", Path, TempFile);
903 ASPRINTF_MEM_ERROR_LOG;
906 rc_system = system(cmd);
909 systemError(__LINE__, rc_system, cmd)
928 char *http_proxy_host = NULL;
929 char *http_proxy_port = NULL;
930 char *http_temp = NULL;
932 for (i = 0; i < 6; i++)
936 GError* error1 = NULL;
937 GError* error2 = NULL;
938 GError* error3 = NULL;
939 GError* error4 = NULL;
958 if (http_proxy_port && http_proxy_port[0])
961 http_temp = strchr(http_proxy_port,
'/');
962 if (http_temp && http_temp[0])
964 count_temp = http_temp - http_proxy_port;
965 http_proxy_port[count_temp] = 0;
971 if (http_proxy_host && http_proxy_host[0])
986 printf(
"Usage: %s [options] [OBJ]\n",Name);
987 printf(
" -h :: help (print this message), then exit.\n");
988 printf(
" -i :: Initialize the DB connection then exit (nothing downloaded)\n");
989 printf(
" -g group :: Set the group on processed files (e.g., -g fossy).\n");
990 printf(
" -G :: Do NOT copy the file to the gold repository.\n");
991 printf(
" -d dir :: directory for downloaded file storage\n");
992 printf(
" -k key :: upload key identifier (number)\n");
993 printf(
" -A acclist :: Specify comma-separated lists of file name suffixes or patterns to accept.\n");
994 printf(
" -R rejlist :: Specify comma-separated lists of file name suffixes or patterns to reject.\n");
995 printf(
" -l depth :: Specify recursion maximum depth level depth. The default maximum depth is 5.\n");
996 printf(
" -c configdir :: Specify the directory for the system configuration.\n");
997 printf(
" -C :: run from command line.\n");
998 printf(
" -v :: verbose (-vv = more verbose).\n");
999 printf(
" -V :: print the version info, then exit.\n");
1000 printf(
" OBJ :: if a URL is listed, then it is retrieved.\n");
1001 printf(
" if a file is listed, then it used.\n");
1002 printf(
" if OBJ and Key are provided, then it is inserted into\n");
1003 printf(
" the DB and repository.\n");
1004 printf(
" no file :: process data from the scheduler.\n");
1016 #define PREFIXMAX 10
1018 const char needle[] =
" ";
1019 const char needle2[] =
"//";
1021 char *username = NULL;
1022 char *password = NULL;
1023 char http[PREFIXMAX] =
"";
1024 char URI[FILEPATH] =
"";
1027 char *additionalParams = NULL;
1036 strcpy(URI, temp + 2);
1037 if (strlen(
GlobalURL) - strlen(URI) > PREFIXMAX - 1)
1046 while( token != NULL )
1048 if (1 == index) username = token;
1051 additionalParams = token + strlen(token) + 1;
1054 token = strtok(NULL, needle);
1057 snprintf(
GlobalURL, URLMAX-1,
"%s%s:%s@%s", http, username, password, URI);
1059 if (strlen(additionalParams) > 0) {
1060 memmove(
GlobalParam, additionalParams, strlen(additionalParams) +1);
1073 const char needle[] =
" ";
1075 int secondIndex = 0;
1076 char *username = NULL;
1078 char newParam[STRMAX];
1082 memset(newParam,
'\0', STRMAX);
1088 while( token != NULL )
1094 token = strtok(NULL, needle);
1098 sprintf(newParam,
" --username %s --password ****", username);
1107 secondIndex = strcspn(
GlobalURL + index + 1,
":");
1108 index = index + secondIndex + 1;
1110 beg = (
char *)malloc(index + 2);
1111 memset(beg,
'\0', index + 2);
1115 sprintf(newParam,
"%s****%s", beg, end);
1128 char Type[][4] = {
"SVN",
"Git",
"CVS"};
1145 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",
GlobalProxy[4],
GlobalProxy[5],
GlobalURL,
GlobalParam, tmpfile_dir);
1149 res = asprintf(&command,
"svn export %s %s %s --no-auth-cache",
GlobalURL,
GlobalParam, tmpfile_dir);
1157 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);
1161 res = asprintf(&command,
"git clone %s %s %s && 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.