-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·190 lines (173 loc) · 4.96 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdbool.h>
#include <sys/wait.h>
#include "argparser.h"
#include "dynstr.h"
#include "futils.h"
#include "defs.h"
#include "defs.h"
#include "timeutils.h"
#include "osutils.h"
bool syntacticalAnalysis(const char path[]) {
const char *const pathCopy = strdup(path);
if (!pathCopy) {
perror("strdup");
exit(1);
}
int pipefd[2];
getPipe(pipefd);
const char *argv[] = { SCRIPT_NAME, pathCopy, NULL };
const char *envp[] = { NULL };
pid_t pid = getForkPID();
if (pid == 0) {
closeFD(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
closeFD(pipefd[1]);
execve(SCRIPT_NAME, (char *const *)argv, (char *const *)envp);
perror("execve");
exit(1);
}
closeFD(pipefd[1]);
char buffer[MAX_FILE_NAME];
ssize_t bytesRead = readHelper(pipefd[0], buffer, MAX_FILE_NAME);
buffer[bytesRead - 1] = 0;
closeFD(pipefd[0]);
int status;
waitpid(pid, &status, 0);
printf("Script (pid: %d) finished with status %d.\n", pid, WEXITSTATUS(status));
if (status) {
exit(WEXITSTATUS(status));
}
return !strcmp(buffer, SAFE_STR);
}
void iterDirRec(const char dirname[], String *json, const char safeSpace[], int *potDanger) {
DIR *dir = openDir(dirname);
for (struct dirent *deBuff; (deBuff = readdir(dir)) != NULL;) {
const char *d_name = deBuff->d_name;
if (!strncmp(d_name, DIR_PREF, strlen(DIR_PREF)) || !strcmp(d_name, ".") || !strcmp(d_name, "..")) {
continue;
}
struct stat statBuff;
char pathTo[strlen(dirname) + strlen(d_name) + 3];
*pathTo = 0;
strcat(pathTo, dirname);
strcat(pathTo, d_name);
if (stat(pathTo, &statBuff) == -1) {
perror("stat");
exit(1);
}
if (S_ISDIR(statBuff.st_mode)) {
append(json, d_name);
append(json, ":[");
strcat(pathTo, "/");
iterDirRec(pathTo, json, safeSpace, potDanger);
append(json, "],");
} else {
if (!hasRights(statBuff.st_mode)) {
if (!syntacticalAnalysis(pathTo)) {
(*potDanger)++;
//moveFile(pathTo, tmp, statBuff.st_size);
char command[strlen(pathTo) + strlen(safeSpace) + 10];
sprintf(command, "mv %s %s", pathTo, safeSpace);
system(command);
continue;
}
}
char tmp[30];
append(json, d_name);
append(json, ":{size:");
sprintf(tmp, "%ld", statBuff.st_size);
append(json, tmp);
append(json, ",last_modif:\"");
append(json, ctime(&statBuff.st_mtime));
pop(json);
append(json, "\", inode_number:");
sprintf(tmp, "%ld", statBuff.st_ino);
append(json, tmp);
append(json, "},");//*/
}
}
pop(json);
closeDir(dir);
}
int snapshot(const char targetDir[], const char pathToPut[], const char safeSpace[], int i) {
char snapTarget[TARGET_MAX_LEN] = "";
if (pathToPut == NULL) {
strcat(snapTarget, targetDir);
strcat(snapTarget, DIR_PREF);
strcat(snapTarget, getCurrDateTime());
} else {
strcat(snapTarget, pathToPut);
strcat(snapTarget, DIR_PREF);
strcat(snapTarget, getCurrDateTime());
}
{
char tmp[10] = {0};
sprintf(tmp, "_(%d)", i);
strcat(snapTarget, tmp);
}//*/
puts(snapTarget);
String json;
initStr(&json);
append(&json, "[");
int potDanger = 0;
iterDirRec(targetDir, &json, safeSpace, &potDanger);
append(&json, "]\n");
int fd = getFD(strcat(snapTarget, ".json"), O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
writeHelper(fd, json.buffer, json.len);
closeFD(fd);
freeStr(&json);//*/
return potDanger;
}
void wrongUsage() {
fprintf(stderr, "Wrong usage!\n");
exit(1);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
wrongUsage();
}
Args args;
initArgs(argc, argv, &args);
ArgPair *targets = getVal(&args, "-t");
ArgPair *safeSpaceArg = getVal(&args, "-s");
if (targets == NULL || safeSpaceArg == NULL) {
wrongUsage();
}
const int numProcs = targets->cnt;
int pipefd[numProcs][2];
for (int i = 0; i < numProcs; i++) {
getPipe(pipefd[i]);
}
ArgPair *out = getVal(&args, "-o");
const char *where = ((out == NULL) ? NULL : out->values[0]);
for (int i = 0; i < numProcs; i++) {
pid_t pid = getForkPID();
if (pid == 0) {
int potDanger = snapshot(targets->values[i], where, safeSpaceArg->values[0], i);
closeFD(pipefd[i][0]);
writeHelper(pipefd[i][1], &potDanger, sizeof(potDanger));
closeFD(pipefd[i][1]);
return 0;
}
}
for (int i = 0, cnt = 1; i < numProcs; i++, cnt++) {
pid_t pid;
int status;
closeFD(pipefd[i][1]);
int potDanger = -1;
readHelper(pipefd[i][0], &potDanger, sizeof(potDanger));
closeFD(pipefd[i][0]);
if ((pid = wait(&status)) > 0) {
const char format[] = "Proccess %d with pid %d exited with code %d and %d potentially dangerous files.\n";
printf(format, cnt, pid, WEXITSTATUS(status), potDanger);
}
}
return 0;
}