45 va_start (arg, format);
46 done = vprintf(format, arg);
61 PGresult *
PQexecCheck(
const char *desc,
char *
SQL,
char *file,
const int line)
74 result = PQexec(
pgConn, SQL);
107 if (NULL == user || NULL == password)
111 char SQL[MAXSQL] = {0};
113 char user_seed[myBUFSIZ] = {0};
114 char pass_hash_valid[41] = {0};
115 unsigned char pass_hash_actual_raw[21] = {0};
116 char pass_hash_actual[41] = {0};
119 snprintf(SQL,MAXSQL,
"SELECT user_seed, user_pass, user_perm, user_pk from users where user_name=$1;");
120 const char *values[1] = {user};
121 int lengths[1] = {strlen(user)};
123 result = PQexecParams(
pgConn, SQL, 1, NULL, values, lengths, binary, 0);
128 if (!PQntuples(result))
132 strcpy(user_seed, PQgetvalue(result, 0, 0));
133 strcpy(pass_hash_valid, PQgetvalue(result, 0, 1));
134 *userPerm = atoi(PQgetvalue(result, 0, 2));
135 *userId = atoi(PQgetvalue(result, 0, 3));
137 if (user_seed[0] && pass_hash_valid[0])
139 strcat(user_seed, password);
140 gcry_md_hash_buffer(GCRY_MD_SHA1, pass_hash_actual_raw, user_seed,
148 char temp[256] = {0};
149 for (i = 0; i < 20; i++)
151 snprintf(temp, 256,
"%02x", pass_hash_actual_raw[i]);
152 strcat(pass_hash_actual, temp);
154 return (strcmp(pass_hash_valid, pass_hash_actual) == 0) ? 0 : 1;
174 if (perms < wanted_permissions)
243 snprintf(SQL,MAXSQL,
"SELECT count(*) FROM folder JOIN users ON (users.user_pk = folder.user_fk OR users.user_perm = 10) WHERE folder_pk = %ld AND users.user_pk = %d;",folder_id,userId);
244 result = PQexec(
pgConn, SQL);
249 count = atol(PQgetvalue(result,0,0));
294 PGresult *result, *pfileResult;
295 char SQL[MAXSQL], desc[myBUFSIZ];
298 if(0 != permission_upload) {
299 return permission_upload;
302 snprintf(tempTable,
sizeof(tempTable),
"DelUp_%ld_pfile",uploadId);
303 snprintf(SQL,MAXSQL,
"DROP TABLE IF EXISTS %s;",tempTable);
306 snprintf(desc, myBUFSIZ,
"Deleting upload %ld",uploadId);
320 snprintf(SQL,MAXSQL,
"SELECT DISTINCT pfile_pk,pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfile INTO %s FROM uploadtree INNER JOIN pfile ON upload_fk = %ld AND pfile_fk = pfile_pk;",tempTable,uploadId);
321 PQexecCheckClear(
"Getting list of pfiles to delete", SQL, __FILE__, __LINE__);
324 snprintf(SQL, MAXSQL,
"DELETE FROM %s WHERE pfile_pk IN (SELECT pfile_pk FROM %s INNER JOIN uploadtree ON pfile_pk = pfile_fk WHERE upload_fk != %ld)", tempTable, tempTable, uploadId);
328 snprintf(SQL,MAXSQL,
"SELECT COUNT(*) FROM %s;",tempTable);
329 result = PQexec(
pgConn, SQL);
333 printf(
"# Created pfile table %s with %ld entries\n", tempTable, atol(PQgetvalue(result,0,0)));
339 snprintf(SQL,MAXSQL,
"SELECT * FROM %s ORDER BY pfile_pk;",tempTable);
340 pfileResult = PQexec(
pgConn, SQL);
346 maxRow = PQntuples(pfileResult);
347 for(Row=0; Row<maxRow; Row++) {
348 S = PQgetvalue(pfileResult,Row,1);
351 printf(
"TEST: Delete %s %s\n",
"files",S);
358 printf(
"TEST: Delete %s %s\n",
"gold",S);
366 PQclear(pfileResult);
377 snprintf(SQL,MAXSQL,
"DELETE FROM foldercontents WHERE (foldercontents_mode & 2) != 0 AND child_id = %ld;",uploadId);
382 snprintf(SQL,MAXSQL,
"DELETE FROM bucket_container USING uploadtree WHERE uploadtree_fk = uploadtree_pk AND upload_fk = %ld;",uploadId);
386 snprintf(SQL,MAXSQL,
"DELETE FROM tag_uploadtree USING uploadtree WHERE uploadtree_fk = uploadtree_pk AND upload_fk = %ld;",uploadId);
391 snprintf(SQL,MAXSQL,
"SELECT uploadtree_tablename FROM upload WHERE upload_pk = %ld;",uploadId);
392 result = PQexec(
pgConn, SQL);
396 if (PQntuples(result)) {
397 strcpy(uploadtree_tablename, PQgetvalue(result, 0, 0));
399 if (strcasecmp(uploadtree_tablename,
"uploadtree_a")) {
400 snprintf(SQL,MAXSQL,
"DROP TABLE %s;", uploadtree_tablename);
407 snprintf(SQL, MAXSQL,
"DELETE FROM clearing_decision_event USING clearing_event WHERE clearing_decision_event.clearing_event_fk = clearing_event.clearing_event_pk AND clearing_event.uploadtree_fk IN (SELECT uploadtree_pk FROM uploadtree INNER JOIN %s ON uploadtree.pfile_fk = %s.pfile_pk WHERE upload_fk = %ld);", tempTable, tempTable, uploadId);
408 PQexecCheckClear(
"Deleting from clearing_decision_event", SQL, __FILE__, __LINE__);
411 snprintf(SQL, MAXSQL,
"DELETE FROM clearing_event WHERE uploadtree_fk IN (SELECT uploadtree_pk FROM uploadtree INNER JOIN %s ON uploadtree.pfile_fk = %s.pfile_pk WHERE upload_fk = %ld);", tempTable, tempTable, uploadId);
415 snprintf(SQL, MAXSQL,
"DELETE FROM uploadtree WHERE upload_fk = %ld;", uploadId);
419 snprintf(SQL,MAXSQL,
"DELETE FROM pfile USING %s WHERE pfile.pfile_pk = %s.pfile_pk;",tempTable,tempTable);
422 snprintf(SQL,MAXSQL,
"DROP TABLE %s;",tempTable);
426 snprintf(SQL,MAXSQL,
"UPDATE upload SET expire_action = 'd', " 427 "expire_date = now(), pfile_fk = NULL WHERE upload_pk = %ld;", uploadId);
430 PQexecCheckClear(NULL,
"SET statement_timeout = 120000;", __FILE__, __LINE__);
458 int unlinkContent (
long child,
long parent,
int mode,
int userId,
int userPerm)
465 snprintf(SQL,MAXSQL,
"SELECT COUNT(DISTINCT parent_fk) FROM foldercontents WHERE foldercontents_mode=%d AND child_id=%ld",mode,child);
468 snprintf(SQL,MAXSQL,
"SELECT COUNT(parent_fk) FROM foldercontents WHERE foldercontents_mode=%d AND" 469 " child_id in (SELECT upload_pk FROM folderlist WHERE pfile_fk=" 470 "(SELECT pfile_fk FROM folderlist WHERE upload_pk=%ld limit 1))",
473 result = PQexec(
pgConn, SQL);
478 cnt = atoi(PQgetvalue(result,0,0));
483 snprintf(SQL,MAXSQL,
"SELECT COUNT(DISTINCT parent_fk) FROM foldercontents WHERE foldercontents_mode=1 AND child_id=%ld",parent);
484 result = PQexec(
pgConn, SQL);
489 cntUpload = atoi(PQgetvalue(result,0,0));
495 snprintf(SQL,MAXSQL,
"DELETE FROM foldercontents WHERE foldercontents_mode=%d AND child_id =%ld AND parent_fk=%ld",mode,child,parent);
522 int r, i, rc, maxRow;
523 int count, resultUploadCount;
526 char SQL[MAXSQL], SQLUpload[MAXSQL];
527 char SQLFolder[MAXSQLFolder];
528 PGresult *result, *resultUpload, *resultFolder;
535 if(DelFlag && rc > 0){
539 snprintf(SQLFolder, MAXSQLFolder,
"SELECT COUNT(*) FROM folderlist WHERE folder_pk=%ld",Parent);
540 resultFolder = PQexec(
pgConn, SQLFolder);
541 count= atoi(PQgetvalue(resultFolder,0,0));
542 PQclear(resultFolder);
545 snprintf(SQL,MAXSQL,
"SELECT folder_pk,foldercontents_mode,name,description,upload_pk,pfile_fk FROM folderlist WHERE parent=%ld" 546 " ORDER BY name,parent,folder_pk ", Parent);
547 result = PQexec(
pgConn, SQL);
552 maxRow = PQntuples(result);
553 for(r=0; r < maxRow; r++)
555 if (atol(PQgetvalue(result,r,0)) == Parent)
560 Fid = atol(PQgetvalue(result,r,0));
565 for(i=0; i<Depth; i++)
569 printf(
"%4ld :: %s",Fid,PQgetvalue(result,r,2));
570 Desc = PQgetvalue(result,r,3);
573 printf(
" (%s)",Desc);
582 printf(
"Deleting the folder failed.");
589 if (DelFlag==1 &&
unlinkContent(Parent,Row,1,userId,userPerm)==0)
599 snprintf(SQLUpload, MAXSQL,
"SELECT COUNT(*) FROM folderlist WHERE pfile_fk=%ld", atol(PQgetvalue(result,r,5)));
600 resultUpload = PQexec(
pgConn, SQLUpload);
601 resultUploadCount = atoi(PQgetvalue(resultUpload,0,0));
602 if(count < 2 && resultUploadCount < 2)
604 rc =
deleteUpload(atol(PQgetvalue(result,r,4)),userId, userPerm);
611 printf(
"Deleting the folder failed since it contains uploads you can't delete.");
616 rc =
unlinkContent(atol(PQgetvalue(result,r,4)),Parent,2,userId,userPerm);
631 for(i=0; i<Depth; i++)
635 printf(
"%4s :: Contains: %s\n",
"--",PQgetvalue(result,r,2));
647 printf(
"INFO: Default folder not deleted.\n");
657 printf(
"INFO: folder id=%ld will be deleted with flag %d\n",Parent,DelFlag);
671 snprintf(SQL,MAXSQL,
"DELETE FROM foldercontents WHERE foldercontents_mode=1 AND parent_fk=%ld AND child_id=%ld",Row,Parent);
673 snprintf(SQL,MAXSQL,
"DELETE FROM foldercontents WHERE foldercontents_mode=1 AND child_id=%ld",Parent);
676 printf(
"TEST: %s\n",SQL);
683 snprintf(SQL,MAXSQL,
"DELETE FROM folder f USING foldercontents fc WHERE f.folder_pk = fc.child_id AND fc.parent_fk='%ld' AND f.folder_pk = '%ld';",Row,Parent);
685 snprintf(SQL,MAXSQL,
"DELETE FROM folder WHERE folder_pk = '%ld';",Parent);
688 printf(
"TEST: %s\n",SQL);
712 int maxRow = PQntuples(result);
719 for(i=0; i < maxRow; i++)
721 Fid = atol(PQgetvalue(result,i,1));
727 for(j=0; (j<maxRow) && !Match; j++)
729 if ((i!=j) && (atol(PQgetvalue(result,j,0)) == Fid)) Match=1;
731 if (!Match && !atol(PQgetvalue(result,i,4)))
735 printf(
"# Unlinked folders\n");
738 printf(
"%4ld :: %s",Fid,PQgetvalue(result,i,2));
739 Desc = PQgetvalue(result,i,3);
742 printf(
" (%s)",Desc);
766 int maxRow = PQntuples(result);
771 for(i=0; i < maxRow; i++)
773 Fid = atol(PQgetvalue(result,i,1));
779 for(j=0; (j<maxRow) && !Match; j++)
781 if ((i!=j) && (atol(PQgetvalue(result,j,0)) == Fid)) Match=1;
783 if (!Match && atol(PQgetvalue(result,i,4)))
787 printf(
"# Unlinked uploads (uploads without folders)\n");
790 printf(
"%4s",PQgetvalue(result,i,4));
791 printf(
" :: %s",PQgetvalue(result,i,2));
792 Desc = PQgetvalue(result,i,3);
795 printf(
" (%s)",Desc);
817 snprintf(SQL,MAXSQL,
"SELECT folder_pk,parent,name,description,upload_pk FROM folderlist ORDER BY name,parent,folder_pk;");
818 result = PQexec(
pgConn, SQL);
850 printf(
"you do not have the permsssion to view the folder list.\n");
854 printf(
"# Folders\n");
855 snprintf(SQL,MAXSQL,
"SELECT folder_name from folder where folder_pk =1;");
856 result = PQexec(
pgConn, SQL);
862 printf(
"%4d :: %s\n", 1, PQgetvalue(result,0,0));
892 char *
SQL =
"SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk;";
893 printf(
"# Uploads\n");
894 result = PQexec(
pgConn, SQL);
901 maxRow = PQntuples(result);
902 for(Row=0; Row < maxRow; Row++)
904 NewPid = atol(PQgetvalue(result,Row,0));
911 if (NewPid >= 0 && (userPerm ==
PERM_ADMIN || rc == 0))
914 printf(
"%ld :: %s",NewPid,PQgetvalue(result,Row,2));
915 S = PQgetvalue(result,Row,1);
916 if (S && S[0]) printf(
" (%s)",S);
942 if(pFolder == 0) pFolder= -1 ;
970 const char s[2] =
" ";
980 if (
Verbose > 1) fprintf(stderr,
"DEBUG: Line='%s'\n",Parm);
984 while(isspace(L[0])) L++;
987 if (!strncasecmp(L,
"DELETE",6) && isspace(L[6]))
992 else if (!strncasecmp(L,
"LIST",4) && isspace(L[4]))
997 while(isspace(L[0])) L++;
999 if (!strncasecmp(L,
"UPLOAD",6) && (isspace(L[6]) || !L[6]))
1004 else if (!strncasecmp(L,
"LICENSE",7) && (isspace(L[7]) || !L[7]))
1009 else if (!strncasecmp(L,
"FOLDER",6) && (isspace(L[6]) || !L[6]))
1017 token = strtok(a, s);
1019 while( token != NULL )
1021 fd[i] = atol(token);
1022 token = strtok(NULL, s);
1027 if ((Type==1) && (Target==1))
1031 else if ((Type==1) && (Target==3))
1035 else if (((Type==2) && (Target==1)) || ((Type==2) && (Target==2)))
1039 else if ((Type==2) && (Target==3))
1045 LOG_ERROR(
"Unknown command: '%s'\n",Parm);
1074 snprintf(SQL,MAXSQL,
"SELECT user_perm FROM users WHERE user_pk='%d';", userId);
1075 result = PQexec(
pgConn, SQL);
1080 userPerm = atoi(PQgetvalue(result, 0, 0));
1084 if (returnCode != 0)
1087 LOG_FATAL(
"Due to permission problems, the delagent was not able to list or delete the requested objects or they did not exist.");
1103 if (exitVal) LOG_ERROR(
"Exiting with status %d", exitVal);
int fo_checkPQresult(PGconn *pgConn, PGresult *result, char *sql, char *FileID, int LineNumb)
Check the result status of a postgres SELECT.
int listFoldersFindDetatched(int userId, int userPerm)
Given a user id, find detached folders and uploads.
int deleteFolder(long cFolder, long pFolder, int userId, int userPerm)
recursively delete a folder
int check_permission_upload(int wanted_permissions, long uploadId, int userId, int userPerm)
check if the upload can be deleted, that is the user have the permission to delete this upload ...
void doSchedulerTasks()
process the jobs from scheduler
int Verbose
Verbose level.
int s
The socket that the CLI will use to communicate.
char * fo_scheduler_current()
Get the last read string from the scheduler.
int check_write_permission_folder(long folder_id, int userId, int userPerm)
check if the upload can be deleted, that is the user have the permission to delete this upload ...
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
int readAndProcessParameter(char *Parm, int userId, int userPerm)
Parse parameters.
int check_read_permission_upload(long uploadId, int userId, int userPerm)
check if the user has read permission on the given upload
int listFoldersFindDetatchedFolders(PGresult *result, int userId, int userPerm)
Given a PGresult, find detached folders.
int check_write_permission_license(long license_id, int userPerm)
check if the license can be deleted, that is the user have the permission to delete this license ...
int printfInCaseOfVerbosity(const char *format,...)
If verbose is on, print to stdout.
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...
int listFoldersFindDetatchedUploads(PGresult *result, int userId, int userPerm)
Given a PGresult, find detached uploads.
int check_write_permission_upload(long uploadId, int userId, int userPerm)
check if the user has read permission on the given upload
char SQL[256]
SQL query to execute.
PGresult * PQexecCheck(const char *desc, char *SQL, char *file, const int line)
simple wrapper which includes PQexec and fo_checkPQcommand
int deleteUpload(long uploadId, int userId, int userPerm)
Given an upload ID, delete it.
int fo_scheduler_userID()
Gets the id of the user that created the job that the agent is running.
PGconn * pgConn
Database connection.
FUNCTION int getEffectivePermissionOnUpload(PGconn *pgConn, long UploadPk, int user_pk, int user_perm)
Get users permission to this upload.
char * uploadtree_tablename
upload.uploadtree_tablename
#define PERM_READ
Read-only permission.
char * fo_scheduler_next()
Get the next data to process from the scheduler.
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...
int unlinkContent(long child, long parent, int mode, int userId, int userPerm)
remove link between parent and (child,mode) if there are other parents
int listFoldersRecurse(long Parent, int Depth, long Row, int DelFlag, int userId, int userPerm)
Draw folder tree.
int listFolders(int userId, int userPerm)
List every folder.
void exitNow(int exitVal)
Exit function. This does all cleanup and should be used instead of calling exit() or main() return...
void PQexecCheckClear(const char *desc, char *SQL, char *file, const int line)
Execute SQL query and create the result.
int authentication(char *user, char *password, int *userId, int *userPerm)
if this account is valid
int listUploads(int userId, int userPerm)
List every upload ID.
#define PERM_ADMIN
Administrator.
#define PERM_WRITE
Read-Write permission.
int fo_RepRemove(char *Type, char *Filename)
Delete a repository file.
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.