37 va_start (arg, format);
38 done = vprintf(format, arg);
54 PGresult *
PQexecCheck(
const char *desc,
char *
SQL,
char *file,
const int line)
131 if (NULL == user || NULL == password)
135 char SQL[MAXSQL] = {0};
137 char user_seed[myBUFSIZ] = {0};
138 char pass_hash_valid[myBUFSIZ] = {0};
139 unsigned char pass_hash_actual_raw[21] = {0};
140 char pass_hash_actual[41] = {0};
143 snprintf(
SQL,MAXSQL,
"SELECT user_seed, user_pass, user_perm, user_pk from users where user_name=$1;");
144 const char *values[1] = {user};
145 int lengths[1] = {strlen(user)};
147 result = PQexecParams(
pgConn,
SQL, 1, NULL, values, lengths, binary, 0);
152 if (!PQntuples(result))
156 strcpy(user_seed, PQgetvalue(result, 0, 0));
157 strcpy(pass_hash_valid, PQgetvalue(result, 0, 1));
158 *userPerm = atoi(PQgetvalue(result, 0, 2));
159 *userId = atoi(PQgetvalue(result, 0, 3));
161 if (pass_hash_valid[0] &&
162 strcmp(crypt(password, pass_hash_valid), pass_hash_valid) == 0)
166 if (user_seed[0] && pass_hash_valid[0])
168 strcat(user_seed, password);
169 gcry_md_hash_buffer(GCRY_MD_SHA1, pass_hash_actual_raw, user_seed,
177 char temp[256] = {0};
178 for (i = 0; i < 20; i++)
180 snprintf(temp, 256,
"%02x", pass_hash_actual_raw[i]);
181 strcat(pass_hash_actual, temp);
183 return (strcmp(pass_hash_valid, pass_hash_actual) == 0) ? 0 : 1;
203 if (perms < wanted_permissions)
272 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);
278 count = atol(PQgetvalue(result,0,0));
323 PGresult *result, *pfileResult;
324 char SQL[MAXSQL], desc[myBUFSIZ];
327 if(0 != permission_upload) {
328 return permission_upload;
331 snprintf(tempTable,
sizeof(tempTable),
"delup_%ld_pfile",uploadId);
332 snprintf(
SQL,MAXSQL,
"DROP TABLE IF EXISTS %s;",tempTable);
335 snprintf(desc, myBUFSIZ,
"Deleting upload %ld",uploadId);
349 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);
353 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);
357 snprintf(
SQL,MAXSQL,
"SELECT COUNT(*) FROM %s;",tempTable);
362 printf(
"# Created pfile table %s with %ld entries\n", tempTable, atol(PQgetvalue(result,0,0)));
368 snprintf(
SQL,MAXSQL,
"SELECT pfile FROM %s ORDER BY pfile_pk;",tempTable);
377 maxRow = PQntuples(pfileResult);
378 for(Row=0; Row<maxRow; Row++) {
379 S = PQgetvalue(pfileResult,Row,0);
382 printf(
"TEST: Delete %s %s\n",
"files",S);
389 printf(
"TEST: Delete %s %s\n",
"gold",S);
400 PQclear(pfileResult);
411 snprintf(
SQL,MAXSQL,
"DELETE FROM foldercontents WHERE (foldercontents_mode & 2) != 0 AND child_id = %ld;",uploadId);
416 snprintf(
SQL,MAXSQL,
"DELETE FROM bucket_container USING uploadtree WHERE uploadtree_fk = uploadtree_pk AND upload_fk = %ld;",uploadId);
420 snprintf(
SQL,MAXSQL,
"DELETE FROM tag_uploadtree USING uploadtree WHERE uploadtree_fk = uploadtree_pk AND upload_fk = %ld;",uploadId);
424 snprintf(
SQL,MAXSQL,
"SELECT uploadtree_tablename FROM upload WHERE upload_pk = %ld;",uploadId);
430 if (PQntuples(result)) {
443 snprintf(
SQL, MAXSQL,
"WITH alld AS ("
444 "SELECT *, ROW_NUMBER() OVER "
445 "(PARTITION BY clearing_event_pk ORDER BY scope DESC) rnum "
446 "FROM clearing_event ce "
447 "INNER JOIN clearing_decision_event cde "
448 "ON cde.clearing_event_fk = ce.clearing_event_pk "
449 "INNER JOIN clearing_decision cd "
450 "ON cd.clearing_decision_pk = cde.clearing_decision_fk "
451 "AND cd.uploadtree_fk IN "
452 "(SELECT uploadtree_pk FROM %s WHERE upload_fk = %ld)) "
453 "DELETE FROM clearing_event ce USING alld AS ad "
454 "WHERE ad.rnum = 1 AND ad.scope = 0 "
455 "AND ce.clearing_event_pk = ad.clearing_event_pk;",
460 snprintf(
SQL, MAXSQL,
"DELETE FROM clearing_decision_event AS cde "
461 "USING clearing_decision AS cd "
462 "WHERE cd.scope = 0 "
463 "AND cd.uploadtree_fk IN "
464 "(SELECT uploadtree_pk FROM %s WHERE upload_fk = %ld) "
465 "AND cd.clearing_decision_pk = cde.clearing_decision_fk;",
470 snprintf(
SQL, MAXSQL,
"DELETE FROM clearing_decision "
471 "WHERE scope = 0 AND uploadtree_fk IN "
472 "(SELECT uploadtree_pk FROM %s WHERE upload_fk = %ld);",
477 snprintf(
SQL, MAXSQL,
"DELETE FROM license_ref_bulk "
478 "WHERE uploadtree_fk IN "
479 "(SELECT uploadtree_pk FROM %s WHERE upload_fk = %ld);",
484 snprintf(
SQL, MAXSQL,
"DELETE FROM %s WHERE upload_fk = %ld;",
497 snprintf(
SQL,MAXSQL,
"DELETE FROM pfile USING %s WHERE pfile.pfile_pk = %s.pfile_pk;",tempTable,tempTable);
500 snprintf(
SQL,MAXSQL,
"DROP TABLE %s;",tempTable);
504 snprintf(
SQL,MAXSQL,
"UPDATE upload SET expire_action = 'd', "
505 "expire_date = now(), pfile_fk = NULL WHERE upload_pk = %ld;", uploadId);
508 PQexecCheckClear(NULL,
"SET statement_timeout = 120000;", __FILE__, __LINE__);
536 int unlinkContent (
long child,
long parent,
int mode,
int userId,
int userPerm)
543 snprintf(
SQL,MAXSQL,
"SELECT COUNT(DISTINCT parent_fk) FROM foldercontents WHERE foldercontents_mode=%d AND child_id=%ld",mode,child);
546 snprintf(
SQL,MAXSQL,
"SELECT COUNT(parent_fk) FROM foldercontents WHERE foldercontents_mode=%d AND"
547 " child_id in (SELECT upload_pk FROM folderlist WHERE pfile_fk="
548 "(SELECT pfile_fk FROM folderlist WHERE upload_pk=%ld limit 1))",
556 cnt = atoi(PQgetvalue(result,0,0));
561 snprintf(
SQL,MAXSQL,
"SELECT COUNT(DISTINCT parent_fk) FROM foldercontents WHERE foldercontents_mode=1 AND child_id=%ld",parent);
567 cntUpload = atoi(PQgetvalue(result,0,0));
573 snprintf(
SQL,MAXSQL,
"DELETE FROM foldercontents WHERE foldercontents_mode=%d AND child_id =%ld AND parent_fk=%ld",mode,child,parent);
600 int r, i, rc, maxRow;
601 int count, resultUploadCount;
604 char SQL[MAXSQL], SQLUpload[MAXSQL];
605 char SQLFolder[MAXSQLFolder];
606 PGresult *result, *resultUpload, *resultFolder;
613 if(DelFlag && rc > 0){
617 snprintf(SQLFolder, MAXSQLFolder,
"SELECT COUNT(*) FROM folderlist WHERE folder_pk=%ld",Parent);
618 resultFolder = PQexec(
pgConn, SQLFolder);
619 count= atoi(PQgetvalue(resultFolder,0,0));
620 PQclear(resultFolder);
623 snprintf(
SQL,MAXSQL,
"SELECT folder_pk,foldercontents_mode,name,description,upload_pk,pfile_fk FROM folderlist WHERE parent=%ld"
624 " ORDER BY name,parent,folder_pk ", Parent);
630 maxRow = PQntuples(result);
631 for(r=0; r < maxRow; r++)
633 if (atol(PQgetvalue(result,r,0)) == Parent)
638 Fid = atol(PQgetvalue(result,r,0));
643 for(i=0; i<Depth; i++)
647 printf(
"%4ld :: %s",Fid,PQgetvalue(result,r,2));
648 Desc = PQgetvalue(result,r,3);
651 printf(
" (%s)",Desc);
660 printf(
"Deleting the folder failed.");
667 if (DelFlag==1 &&
unlinkContent(Parent,Row,1,userId,userPerm)==0)
677 snprintf(SQLUpload, MAXSQL,
"SELECT COUNT(*) FROM folderlist WHERE pfile_fk=%ld", atol(PQgetvalue(result,r,5)));
678 resultUpload = PQexec(
pgConn, SQLUpload);
679 resultUploadCount = atoi(PQgetvalue(resultUpload,0,0));
680 if(count < 2 && resultUploadCount < 2)
682 rc =
deleteUpload(atol(PQgetvalue(result,r,4)),userId, userPerm);
689 printf(
"Deleting the folder failed since it contains uploads you can't delete.");
694 rc =
unlinkContent(atol(PQgetvalue(result,r,4)),Parent,2,userId,userPerm);
709 for(i=0; i<Depth; i++)
713 printf(
"%4s :: Contains: %s\n",
"--",PQgetvalue(result,r,2));
725 printf(
"INFO: Default folder not deleted.\n");
735 printf(
"INFO: folder id=%ld will be deleted with flag %d\n",Parent,DelFlag);
749 snprintf(
SQL,MAXSQL,
"DELETE FROM foldercontents WHERE foldercontents_mode=1 AND parent_fk=%ld AND child_id=%ld",Row,Parent);
751 snprintf(
SQL,MAXSQL,
"DELETE FROM foldercontents WHERE foldercontents_mode=1 AND child_id=%ld",Parent);
754 printf(
"TEST: %s\n",
SQL);
761 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);
763 snprintf(
SQL,MAXSQL,
"DELETE FROM folder WHERE folder_pk = '%ld';",Parent);
766 printf(
"TEST: %s\n",
SQL);
790 int maxRow = PQntuples(result);
797 for(i=0; i < maxRow; i++)
799 Fid = atol(PQgetvalue(result,i,1));
805 for(j=0; (j<maxRow) && !
Match; j++)
807 if ((i!=j) && (atol(PQgetvalue(result,j,0)) == Fid))
Match=1;
809 if (!
Match && !atol(PQgetvalue(result,i,4)))
813 printf(
"# Unlinked folders\n");
816 printf(
"%4ld :: %s",Fid,PQgetvalue(result,i,2));
817 Desc = PQgetvalue(result,i,3);
820 printf(
" (%s)",Desc);
844 int maxRow = PQntuples(result);
849 for(i=0; i < maxRow; i++)
851 Fid = atol(PQgetvalue(result,i,1));
857 for(j=0; (j<maxRow) && !
Match; j++)
859 if ((i!=j) && (atol(PQgetvalue(result,j,0)) == Fid))
Match=1;
861 if (!
Match && atol(PQgetvalue(result,i,4)))
865 printf(
"# Unlinked uploads (uploads without folders)\n");
868 printf(
"%4s",PQgetvalue(result,i,4));
869 printf(
" :: %s",PQgetvalue(result,i,2));
870 Desc = PQgetvalue(result,i,3);
873 printf(
" (%s)",Desc);
895 snprintf(
SQL,MAXSQL,
"SELECT folder_pk,parent,name,description,upload_pk FROM folderlist ORDER BY name,parent,folder_pk;");
928 printf(
"you do not have the permsssion to view the folder list.\n");
932 printf(
"# Folders\n");
933 snprintf(
SQL,MAXSQL,
"SELECT folder_name from folder where folder_pk =1;");
940 printf(
"%4d :: %s\n", 1, PQgetvalue(result,0,0));
970 char *
SQL =
"SELECT upload_pk,upload_desc,upload_filename FROM upload ORDER BY upload_pk;";
971 printf(
"# Uploads\n");
979 maxRow = PQntuples(result);
980 for(Row=0; Row < maxRow; Row++)
982 NewPid = atol(PQgetvalue(result,Row,0));
989 if (NewPid >= 0 && (userPerm ==
PERM_ADMIN || rc == 0))
992 printf(
"%ld :: %s",NewPid,PQgetvalue(result,Row,2));
993 S = PQgetvalue(result,Row,1);
994 if (S && S[0]) printf(
" (%s)",S);
1020 if(pFolder == 0) pFolder= -1 ;
1048 const char s[2] =
" ";
1058 if (
Verbose > 1) fprintf(stderr,
"DEBUG: Line='%s'\n",Parm);
1062 while(isspace(L[0])) L++;
1065 if (!strncasecmp(L,
"DELETE",6) && isspace(L[6]))
1070 else if (!strncasecmp(L,
"LIST",4) && isspace(L[4]))
1075 while(isspace(L[0])) L++;
1077 if (!strncasecmp(L,
"UPLOAD",6) && (isspace(L[6]) || !L[6]))
1082 else if (!strncasecmp(L,
"LICENSE",7) && (isspace(L[7]) || !L[7]))
1087 else if (!strncasecmp(L,
"FOLDER",6) && (isspace(L[6]) || !L[6]))
1095 token = strtok(a,
s);
1097 while( token != NULL )
1099 fd[i] = atol(token);
1100 token = strtok(NULL,
s);
1105 if ((Type==1) && (Target==1))
1109 else if ((Type==1) && (Target==3))
1113 else if (((Type==2) && (Target==1)) || ((Type==2) && (Target==2)))
1117 else if ((Type==2) && (Target==3))
1123 LOG_ERROR(
"Unknown command: '%s'\n",Parm);
1152 snprintf(
SQL,MAXSQL,
"SELECT user_perm FROM users WHERE user_pk='%d';", userId);
1158 userPerm = atoi(PQgetvalue(result, 0, 0));
1162 if (returnCode != 0)
1165 LOG_FATAL(
"Due to permission problems, the delagent was not able to list or delete the requested objects or they did not exist.");
1181 if (exitVal) LOG_ERROR(
"Exiting with status %d", exitVal);
char SQL[256]
SQL query to execute.
char * uploadtree_tablename
upload.uploadtree_tablename
int listFoldersFindDetatchedUploads(PGresult *result, int userId, int userPerm)
Given a PGresult, find detached uploads.
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
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 readAndProcessParameter(char *Parm, int userId, int userPerm)
Parse parameters.
void doSchedulerTasks()
process the jobs from 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
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 listFoldersRecurse(long Parent, int Depth, long Row, int DelFlag, int userId, int userPerm)
Draw folder tree.
int authentication(char *user, char *password, int *userId, int *userPerm)
if this account is valid
PGconn * pgConn
Database connection.
int printfInCaseOfVerbosity(const char *format,...)
If verbose is on, print to stdout.
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 Verbose
Verbose level.
PGresult * PQexecCheck(const char *desc, char *SQL, char *file, const int line)
simple wrapper which includes PQexec and fo_checkPQcommand for queries without result
int listUploads(int userId, int userPerm)
List every upload ID.
int listFoldersFindDetatched(int userId, int userPerm)
Given a user id, find detached folders and uploads.
int listFolders(int userId, int userPerm)
List every folder.
int deleteUpload(long uploadId, int userId, int userPerm)
Given an upload ID, delete it.
int check_write_permission_upload(long uploadId, int userId, int userPerm)
check if the user has read permission on the given upload
PGresult * PQexecCheckResult(const char *desc, char *SQL, char *file, const int line)
simple wrapper which includes PQexec and fo_checkPQresult for queries with result
int deleteFolder(long cFolder, long pFolder, int userId, int userPerm)
recursively delete a folder
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 s
The socket that the CLI will use to communicate.
FUNCTION int getEffectivePermissionOnUpload(PGconn *pgConn, long UploadPk, int user_pk, int user_perm)
Get users permission to this upload.
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...
#define PERM_WRITE
Read-Write permission.
#define PERM_READ
Read-only permission.
#define PERM_ADMIN
Administrator.
int fo_RepExist(char *Type, char *Filename)
Determine if a file exists.
int fo_RepRemove(char *Type, char *Filename)
Delete a repository file.
void fo_scheduler_disconnect(int retcode)
Disconnect the scheduler connection.
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 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.