FOSSology  4.4.0
Open Source License Compliance by Open Source Software
main.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
13 #define _GNU_SOURCE
14 #include "wget_agent.h"
15 #include <gcrypt.h>
16 
17 #ifdef COMMIT_HASH_S
18 char BuildVersion[]="wget_agent build version: " VERSION_S " r(" COMMIT_HASH_S ").\n";
19 #else
20 char BuildVersion[]="wget_agent build version: NULL.\n";
21 #endif
22 
96 int main (int argc, char *argv[])
97 {
98  int arg;
99  char *Parm = NULL;
100  char *TempFileDir=NULL;
101  int c;
102  int InitFlag=0;
103  int CmdlineFlag = 0; /* run from command line flag, 1 yes, 0 not */
104  int user_pk;
105  char *agent_desc = "Network downloader. Uses wget(1).";
106 
107  memset(GlobalTempFile,'\0',STRMAX);
108  memset(GlobalURL,'\0',URLMAX);
109  memset(GlobalParam,'\0',STRMAX);
110  memset(GlobalType,'\0',STRMAX);
111  GlobalUploadKey = -1;
112  int upload_pk = 0; // the upload primary key
113  //int Agent_pk;
114  char *COMMIT_HASH;
115  char *VERSION;
116  char agent_rev[STRMAX];
117 
118  /* open the connection to the scheduler and configuration */
119  fo_scheduler_connect(&argc, argv, &pgConn);
120 
121  /* Process command-line */
122  while((c = getopt(argc,argv,"d:Gg:ik:A:R:l:Cc:Vvh")) != -1)
123  {
124  switch(c)
125  {
126  case 'd':
127  TempFileDir = PathCheck(optarg);
128  break;
129  case 'g':
130  {
131  struct group *SG;
132  SG = getgrnam(optarg);
133  if (SG) ForceGroup = SG->gr_gid;
134  }
135  break;
136  case 'G':
138  break;
139  case 'i':
140  InitFlag=1;
141  break;
142  case 'k':
143  GlobalUploadKey = atol(optarg);
144  if (!GlobalTempFile[0])
145  strcpy(GlobalTempFile,"wget.default_download");
146  break;
147  case 'A':
148  strncat(GlobalParam, " -A ", STRMAX - strlen(GlobalParam) -1);
149  strncat(GlobalParam, optarg, STRMAX - strlen(GlobalParam) -1);
150  break;
151  case 'R':
152  strncat(GlobalParam, " -R ", STRMAX - strlen(GlobalParam) -1);
153  strncat(GlobalParam, optarg, STRMAX - strlen(GlobalParam) -1);
154  break;
155  case 'l':
156  strncat(GlobalParam, " -l ", STRMAX - strlen(GlobalParam) -1);
157  strncat(GlobalParam, optarg, STRMAX - strlen(GlobalParam) -1);
158  break;
159  case 'c': break; /* handled by fo_scheduler_connect() */
160  case 'C':
161  CmdlineFlag = 1;
162  break;
163  case 'v':
164  agent_verbose++; // global agent verbose flag.
165  break;
166  case 'V':
167  printf("%s", BuildVersion);
168  SafeExit(0);
169  default:
170  Usage(argv[0]);
171  SafeExit(-1);
172  }
173  }
174  if (argc - optind > 1)
175  {
176  Usage(argv[0]);
177  SafeExit(-1);
178  }
179 
180  /* When initializing the DB, don't do anything else */
181  if (InitFlag)
182  {
183  if (pgConn) PQfinish(pgConn);
184  SafeExit(0);
185  }
186 
187  /* Initialize gcrypt and disable security memory */
188  gcry_check_version(NULL);
189  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
190  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
191 
192  COMMIT_HASH = fo_sysconfig("wget_agent", "COMMIT_HASH");
193  VERSION = fo_sysconfig("wget_agent", "VERSION");
194  sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
195  /* Get the Agent Key from the DB */
196  fo_GetAgentKey(pgConn, basename(argv[0]), GlobalUploadKey, agent_rev, agent_desc);
197 
198  /* get proxy */
199  GetProxy();
200 
201  /* Run from the command-line (for testing) */
202  for(arg=optind; arg < argc; arg++)
203  {
204  memset(GlobalURL,'\0',sizeof(GlobalURL));
205  strncpy(GlobalURL,argv[arg],sizeof(GlobalURL)-1);
206  /* If the file contains "://" then assume it is a URL.
207  Else, assume it is a file. */
208  LOG_VERBOSE0("Command-line: %s",GlobalURL);
209  if (strstr(GlobalURL,"://"))
210  {
212  LOG_VERBOSE0("It's a URL");
213  if (GetURL(GlobalTempFile,GlobalURL,TempFileDir) != 0)
214  {
215  LOG_FATAL("Download of %s failed.",GlobalURL);
216  SafeExit(21);
217  }
218  if (GlobalUploadKey != -1) { DBLoadGold(); }
219  unlink(GlobalTempFile);
220  }
221  else /* must be a file */
222  {
223  LOG_VERBOSE0("It's a file -- GlobalUploadKey = %ld",GlobalUploadKey);
224  if (GlobalUploadKey != -1)
225  {
226  memcpy(GlobalTempFile,GlobalURL,STRMAX);
227  DBLoadGold();
228  }
229  }
230  }
231 
232  /* Run from scheduler! */
233  if (0 == CmdlineFlag)
234  {
235  user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */
236  while(fo_scheduler_next())
237  {
238  Parm = fo_scheduler_current(); /* get piece of information, including upload_pk, downloadfile url, and parameters */
239  if (Parm && Parm[0])
240  {
242  /* set globals: uploadpk, downloadfile url, parameters */
243  SetEnv(Parm,TempFileDir);
245 
246  /* Check Permissions */
247  if (GetUploadPerm(pgConn, upload_pk, user_pk) < PERM_WRITE)
248  {
249  LOG_ERROR("You have no update permissions on upload %d", upload_pk);
250  continue;
251  }
252 
253  char TempDir[STRMAX];
254  memset(TempDir,'\0',STRMAX);
255  snprintf(TempDir, STRMAX-1, "%s/wget", TempFileDir); // /var/local/lib/fossology/agents/wget
256  struct stat Status = {};
257 
258  if (GlobalType[0])
259  {
260  if (GetVersionControl() == 0)
261  {
262  DBLoadGold();
263  unlink(GlobalTempFile);
264  }
265  else
266  {
267  LOG_FATAL("upload %ld File retrieval failed: uploadpk=%ld tempfile=%s URL=%s Type=%s",
269  SafeExit(23);
270  }
271  }
272  else if (strstr(GlobalURL, "*") || stat(GlobalURL, &Status) == 0)
273  {
274  if (!Archivefs(GlobalURL, GlobalTempFile, TempFileDir, Status))
275  {
276  LOG_FATAL("Failed to archive. GlobalURL, GlobalTempFile, TempFileDir are: %s, %s, %s, "
277  "Mode is: %lo (octal)\n", GlobalURL, GlobalTempFile, TempFileDir, (unsigned long) Status.st_mode);
278  SafeExit(50);
279  }
280  DBLoadGold();
281  unlink(GlobalTempFile);
282  }
283  else
284  {
285  if (GetURL(GlobalTempFile,GlobalURL,TempDir) == 0)
286  {
287  DBLoadGold();
288  unlink(GlobalTempFile);
289  }
290  else
291  {
292  LOG_FATAL("upload %ld File retrieval failed: uploadpk=%ld tempfile=%s URL=%s",
294  SafeExit(22);
295  }
296  }
297  }
298  }
299  } /* if run from scheduler */
300 
301  SafeExit(0);
302  exit(0); /* to prevent compiler warning */
303 } /* main() */
304 
PGconn * pgConn
Database connection.
Definition: adj2nest.c:86
char BuildVersion[]
Definition: buckets.c:68
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:63
FUNCTION int GetUploadPerm(PGconn *pgConn, long UploadPk, int user_pk)
Get users permission to this upload.
Definition: libfossagent.c:378
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.
Definition: libfossagent.c:158
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33
void fo_scheduler_heart(int i)
This function must be called by agents to let the scheduler know they are alive and how many items th...
char * fo_sysconfig(const char *sectionname, const char *variablename)
gets a system configuration variable from the configuration data.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
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
Definition: main.c:76
const char * upload_pk
Definition: sqlstatements.h:82
void SafeExit(int rc)
Close scheduler and database connections, then exit.
Definition: utils.c:77
char * PathCheck(char *DirPath)
Check if path contains a "%U" or "%H". If so, substitute a unique ID for U.
Definition: utils.c:1662
char GlobalParam[STRMAX]
Additional parameters.
Definition: wget_agent.c:27
char GlobalType[STRMAX]
Type of download (FILE/version control)
Definition: wget_agent.c:26
int GetURL(char *TempFile, char *URL, char *TempFileDir)
Do the wget.
Definition: wget_agent.c:328
char GlobalURL[URLMAX]
URL to download.
Definition: wget_agent.c:25
int GetVersionControl()
Get source code from version control system.
Definition: wget_agent.c:566
void SetEnv(char *S, char *TempFileDir)
Convert input pairs into globals.
Definition: wget_agent.c:683
long GlobalUploadKey
Input for this system.
Definition: wget_agent.c:23
gid_t ForceGroup
Set to group id to be used for download files.
Definition: wget_agent.c:31
char GlobalTempFile[STRMAX]
Temp file to be used.
Definition: wget_agent.c:24
void DBLoadGold()
Insert a file into the database and repository.
Definition: wget_agent.c:81
void GetProxy()
Get proxy from fossology.conf.
Definition: wget_agent.c:916
int GlobalImportGold
Set to 0 to not store file in gold repository.
Definition: wget_agent.c:30
int Archivefs(char *Path, char *TempFile, char *TempFileDir, struct stat Status)
Copy downloaded files to temporary directory.
Definition: wget_agent.c:819