Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 157 additions & 69 deletions src/delagent/agent/delagent.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
delagent: Remove an upload from the DB and repository

Copyright (C) 2007-2013 Hewlett-Packard Development Company, L.P.
Copyright (C) 2015-2016 Siemens AG

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -31,30 +32,74 @@ char BuildVersion[]="delagent build version: NULL.\n";
#endif


/***********************************************
usage():
Command line options allow you to write the agent so it works
stand alone, in addition to working with the scheduler.
This simplifies code development and testing.
So if you have options, have a usage().
Here are some suggested options (in addition to the program
specific options you may already have).
***********************************************/
void usage (char *Name)
{
fprintf(stderr,"Usage: %s [options]\n",Name);
fprintf(stderr," List or delete uploads.\n");
fprintf(stderr," Options\n");
fprintf(stderr," -i :: Initialize the DB, then exit.\n");
fprintf(stderr," -u :: List uploads IDs.\n");
fprintf(stderr," -U # :: Delete upload ID.\n");
//fprintf(stderr," -L # :: Delete ALL licenses associated with upload ID.\n");
fprintf(stderr," -f :: List folder IDs.\n");
fprintf(stderr," -F # :: Delete folder ID and all uploads under this folder.\n");
fprintf(stderr," Folder '1' is the default folder. '-F 1' will delete\n");
fprintf(stderr," every upload and folder in the navigation tree.\n");
fprintf(stderr," -s :: Run from the scheduler.\n");
fprintf(stderr," -T :: TEST -- do not update the DB or delete any files (just pretend)\n");
fprintf(stderr," -v :: Verbose (-vv for more verbose)\n");
fprintf(stderr," -c # :: Specify the directory for the system configuration\n");
fprintf(stderr," -V :: print the version info, then exit.\n");
fprintf(stderr," --user|-n # :: user name\n");
fprintf(stderr," --password|-p # :: password\n");
} /* usage() */

void writeMessageAfterDelete(char *kind, long id, char *user_name, int returnedCode)
{
if (0 == returnedCode)
{
fprintf(stdout, "The %s '%ld' is deleted by the user '%s'.\n", kind, id, user_name);
}
else
{
fprintf(stdout, "Deletion failed: user '%s' does not have the permsssion to delete the %s '%ld', or the %s '%ld' does not exist.\n", user_name, kind, id, kind, id);
exit(returnedCode);
}
}

/**
* \brief main function for the delagent
*
* There are 2 ways to use the delagent agent:
* 1. Command Line :: delete/list upload from the command line
* 1. Command Line :: delete/list upload/folder/license from the command line
* 2. Agent Based :: run from the scheduler
*
* +-----------------------+
* | Command Line Analysis |
* +-----------------------+
*
* List or delete uploads.
* -h :: help (print this message), then exit.
* -i :: Initialize the DB
* -u :: List uploads IDs.
* -U # :: Delete upload ID.
* -L # :: Delete ALL licenses associated with upload ID.
* -f :: List folder IDs.
* -F # :: Delete folder ID and all uploads under this folder.
* -T :: TEST -- do not update the DB or delete any files (just pretend).
* -v :: Verbose (-vv for more verbose).
* -V :: print the version info, then exit.
* -h :: help (print this message), then exit.
* -i :: Initialize the DB
* -u :: List uploads IDs.
* -U # :: Delete upload ID.
* -L # :: Delete ALL licenses associated with upload ID.
* -f :: List folder IDs.
* -F # :: Delete folder ID and all uploads under this folder.
* -T :: TEST -- do not update the DB or delete any files (just pretend).
* -v :: Verbose (-vv for more verbose).
* -V :: print the version info, then exit.
* -c SYSCONFDIR :: Specify the directory for the system configuration.
* --user # :: user name
* --user # :: user name
* --password # :: password
*
* +----------------------+
Expand All @@ -72,13 +117,11 @@ char BuildVersion[]="delagent build version: NULL.\n";
int main (int argc, char *argv[])
{
int c;
int ListProj=0, ListFolder=0;
long DelUpload=0, DelFolder=0, DelLicense=0;
int Scheduler=0; /* should it run from the scheduler? */
int GotArg=0;
int listProj=0, listFolder=0;
long delUpload=0, delFolder=0, delLicense=0;
int scheduler=0; /* should it run from the scheduler? */
int gotArg=0;
char *agent_desc = "Deletes upload. Other list/delete options available from the command line.";
char *Parm = NULL;
//int Agent_pk = 0;
char *COMMIT_HASH;
char *VERSION;
char agent_rev[myBUFSIZ];
Expand All @@ -87,6 +130,7 @@ int main (int argc, char *argv[])
char *password = NULL;
int user_id = -1;
int user_perm = -1;
int returnedCode = 0;

fo_scheduler_connect(&argc, argv, &db_conn);

Expand All @@ -96,84 +140,128 @@ int main (int argc, char *argv[])
{"password", required_argument, 0, 'p'},
{0, 0, 0, 0}
};

while ((c = getopt_long (argc, argv, "n:p:ifF:lL:sTuU:vVc:h",
long_options, &option_index)) != -1)
{
switch (c)
{
case 'n':
case 'n':
user_name = optarg;
break;
break;
case 'p':
password = optarg;
break;
case 'i':
PQfinish(db_conn);
return(0);
case 'f': ListFolder=1; GotArg=1; break;
case 'F': DelFolder=atol(optarg); GotArg=1; break;
case 'L': DelLicense=atol(optarg); GotArg=1; break;
case 's': Scheduler=1; GotArg=1; break;
case 'T': Test++; break;
case 'u': ListProj=1; GotArg=1; break;
case 'U': DelUpload=atol(optarg); GotArg=1; break;
case 'v': Verbose++; break;
case 'c': GotArg=1; break; /* handled by fo_scheduler_connect() */
case 'V': printf("%s", BuildVersion); PQfinish(db_conn); return(0);
default: Usage(argv[0]); exit(-1);
case 'f':
listFolder=1;
gotArg=1;
break;
case 'F':
delFolder=atol(optarg);
gotArg=1;
break;
case 'L':
delLicense=atol(optarg);
gotArg=1;
break;
case 's':
scheduler=1;
gotArg=1;
break;
case 'T':
Test++;
break;
case 'u':
listProj=1;
gotArg=1;
break;
case 'U':
delUpload=atol(optarg);
gotArg=1;
break;
case 'v':
Verbose++;
break;
case 'c':
gotArg=1;
break; /* handled by fo_scheduler_connect() */
case 'V':
printf("%s", BuildVersion);
PQfinish(db_conn);
return(0);
default:
usage(argv[0]);
exit(-1);
}
}

if (!GotArg)
{
Usage(argv[0]);
exit(-1);
}

if (Scheduler != 1 && 1 != authentication(user_name, password, &user_id, &user_perm))
if (!gotArg)
{
LOG_FATAL("User name or password is invalid.\n");
usage(argv[0]);
exit(-1);
}

COMMIT_HASH = fo_sysconfig("delagent", "COMMIT_HASH");
VERSION = fo_sysconfig("delagent", "VERSION");
sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
/* Get the Agent Key from the DB */
fo_GetAgentKey(db_conn, basename(argv[0]), 0, agent_rev, agent_desc);

if (ListProj) ListUploads(user_id, user_perm);
if (ListFolder) ListFolders(user_id);

alarm(60); /* from this point on, handle the alarm */
if (DelUpload)
if (scheduler != 1)
{
if (1 != check_permission_del(DelUpload, user_id, user_perm))
if (0 != authentication(user_name, password, &user_id, &user_perm))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remind me why little Bobby tables cannot cause a problem here

LOG_FATAL("You '%s' does not have the permsssion to delete the upload '%ld', or the upload '%ld' does not exist.\n", user_name, DelUpload, DelUpload);
LOG_FATAL("User name or password is invalid.\n");
exit(-1);
}
DeleteUpload(DelUpload);
fprintf(stdout, "The upload '%ld' is deleted by the user '%s'.\n", DelUpload, user_name);
}
if (DelFolder) { DeleteFolder(DelFolder); }
if (DelLicense) { DeleteLicense(DelLicense); }

/* process from the scheduler */
if (Scheduler)
{
while(fo_scheduler_next())
COMMIT_HASH = fo_sysconfig("delagent", "COMMIT_HASH");
VERSION = fo_sysconfig("delagent", "VERSION");
sprintf(agent_rev, "%s.%s", VERSION, COMMIT_HASH);
/* Get the Agent Key from the DB */
fo_GetAgentKey(db_conn, basename(argv[0]), 0, agent_rev, agent_desc);

if (listProj)
{
Parm = fo_scheduler_current();

if (ReadParameter(Parm) < 0)
exit(-1);
returnedCode = listUploads(user_id, user_perm);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if returnedCode != 0 you might want to leave the program here before you set it again if listFolder is true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Its fixed now

if (returnedCode < 0)
{
return returnedCode;
}
if (listFolder)
{
returnedCode = listFolders(user_id, user_perm);
}
if (returnedCode < 0)
{
return returnedCode;
}

alarm(60); /* from this point on, handle the alarm */
if (delUpload)
{
returnedCode = deleteUpload(delUpload, user_id, user_perm);

writeMessageAfterDelete("upload", delUpload, user_name, returnedCode);
}
if (delFolder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, or are delUpload and delFolder mutually exclusive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function writeMessageAfterDelete keeps track of that and exits if the returned code is not 0

{
returnedCode = deleteFolder(delFolder, user_id, user_perm);

writeMessageAfterDelete("folder", delFolder, user_name, returnedCode);
}
if (delLicense)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^

{
returnedCode = deleteLicense(delLicense, user_perm);

writeMessageAfterDelete("license", delLicense, user_name, returnedCode);
}
}
else
{
/* process from the scheduler */
doSchedulerTasks();
}
fo_scheduler_disconnect(0);

PQfinish(db_conn);
fo_scheduler_disconnect(0);
return(0);
return(returnedCode);
} /* main() */

36 changes: 24 additions & 12 deletions src/delagent/agent/delagent.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/********************************************************
Copyright (C) 2007-2012 Hewlett-Packard Development Company, L.P.
Copyright (C) 2015-2016 Siemens AG

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -42,18 +43,29 @@ extern PGconn* db_conn;
#define MAXSQL 1024
#define MAXLINE 1024
#define myBUFSIZ 2048
#define ADMIN_PERM 10

void DeleteLicense(long UploadId);
void DeleteUpload(long UploadId);
void ListFoldersRecurse(long Parent, int Depth, long Row, int DelFlag);
int UnlinkContent (long child, long parent, int mode);
void ListFolders(int user_id);
void ListUploads (int user_id, int user_perm);
void DeleteFolder(long FolderId);
int ReadParameter(char *Parm);
void Usage(char *Name);

/* authentication and permission checking */
int authentication(char *user, char * password, int *user_id, int *user_perm);
int check_permission_del(long upload_id, int user_id, int user_perm);

int check_permission_upload(int wantedPermissions, long upload_id, int user_id, int user_perm);
int check_read_permission_upload(long upload_id, int user_id, int user_perm);
int check_write_permission_upload(long upload_id, int user_id, int user_perm);
int check_permission_folder(long folder_id, int user_id, int user_perm);
int check_permission_license(long license_id, int user_perm);

/* functions that list things */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the captital letter at the start signify anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that is just the style the code was written...

int listFolders(int user_id, int user_perm);
int listUploads(int user_id, int user_perm);

/* function that delete actual things */
int deleteLicense(long UploadId, int user_perm);
int deleteUpload(long UploadId, int user_id, int user_perm);
int deleteFolder(long FolderId, int user_id, int user_perm);

/* for usage from scheduler */
void doSchedulerTasks();

/* misc */
void usage(char *Name);

#endif /* _DELAGENT_H */
Loading