This repository has been archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWWF Analyzer.cpp
executable file
·736 lines (568 loc) · 24 KB
/
WWF Analyzer.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
WWF Analyzer.cpp
By: Kevin McNee
This program analyzes world writable files reports and summarizes the results.
*/
#include <windows.h>
#include <cstdlib>
#include <cctype>
#include <limits>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <list>
#include <iterator>
#include "data structs.h"
#include "FileClassifier.h"
using namespace std;
void pause(void);
string trim(const string str);
unsigned long analyze(ifstream& file, string directory, string server_name, list<WWF_data>& lst_WWF);
void summarize(ofstream& report, list<WWF_data>& lst_WWF);
string get_server_name(string file_name);
bool set_prefs(void);
const int COL_WIDTH = 16; //the width of the columns in the reports
//user preferences
//summary report max items to show (default is unlimited)
static int SUMMARY_SERVERS = numeric_limits<int>::max(); //max number of servers to show
static int SUMMARY_OWNERS = numeric_limits<int>::max(); //max number of owners to show
static int HIGH_VOLUME = numeric_limits<int>::max(); //max number of the highest volume owners for each server (and vice versa) to show
static unsigned long MAX_CRITICAL = numeric_limits<unsigned long>::max(); //max number of critical files to show (default is unlimited)
static FileClassifier ignore_files_classifier;
static FileClassifier critical_files_classifier;
int main(void){
cout << "WWF Analyzer" << endl
<< "This program analyzes the world writable files reports" << endl
<< "and summarizes the results." << endl << endl;
//load the user's preferences, if the file does not exist, create it and have the user rerun the program
if(!set_prefs()) return EXIT_SUCCESS;
//these are for finding the files to be analyzed
WIN32_FIND_DATA file_data;
HANDLE hFind;
string directory; //the directory where the files are located
const string file_name_pattern = "*.*"; //the structure of the file name (we will allow any file name)
//loop until we get a valid directory
do{
cout << "Enter the directory containing the files:" << endl;
do{
getline(cin,directory);
} while(directory.empty());
//add trailing backslash if the user did not
if((directory.at(directory.length() - 1) != '\\') && (directory.at(directory.length() - 1) != '/')){
directory += '\\';
}
//get the first file in the given directory
hFind = FindFirstFile((directory + file_name_pattern).c_str(), &file_data);
//if no such file can be found
if(hFind == INVALID_HANDLE_VALUE){
cerr << "Error: The directory could not be found." << endl << endl;
}
} while(hFind == INVALID_HANDLE_VALUE);
cout << endl << endl << "Beginning analysis." << endl << endl;
list<WWF_data> lst_WWF; //list of WWF
//for each file in the directory
do{
//ignore directories and hidden files
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|(file_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) continue;
//determine the name of the server for the file that we're opening
string server_name = get_server_name(file_data.cFileName);
//if the file is a previously generated report, ignore it
string cmp1 = server_name;
cmp1 += " WWF Details Report.txt";
const string cmp2 = "WWF Summary Report.txt";
if((cmp1.compare(file_data.cFileName) == 0) || (cmp2.compare(file_data.cFileName) == 0)) continue;
//open the file
ifstream file ((directory + file_data.cFileName).c_str());
if(file.is_open()){
unsigned long WWF_found; //stores the number of WWFs found from the analysis
cout << "Analyzing WWFs on " << server_name << "..." << endl;
WWF_found = analyze(file, directory, server_name, lst_WWF);
cout << "Done analyzing " << server_name << ". " << WWF_found << " WWFs have been found." << endl << endl;
}
else{
cerr << "Error: " << file_data.cFileName << " could not be opened." << endl;
}
} while(FindNextFile(hFind, &file_data));
//if the reason that we stopped is something other than running out of files, something went wrong
if(GetLastError() != ERROR_NO_MORE_FILES){
cerr << "Error: The directory was not searched successfully." << endl;
pause();
return EXIT_FAILURE;
}
//make summary report
ofstream report;
report.open ((directory + "WWF Summary Report.txt").c_str());
if(report.is_open()){
summarize(report, lst_WWF);
report.close();
cout << endl << "Analysis has finished." << endl << endl
<< "The WWF Summary Report has been created in the" << endl
<< "same directory as the analyzed files." << endl << endl;
pause();
//open the Summary Report before the termination of this program
ShellExecute(NULL, "open", (directory + "WWF Summary Report.txt").c_str(), NULL, NULL, 1);
}
else{
cerr << "Error: Unable create report." << endl
<< "Make sure that you have write access for the directory" << endl
<< "and that any previously created reports are not in use," << endl
<< "so that they can be overwritten." << endl << endl;
pause();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
//prompts the user to press enter to continue
void pause(void){
cout << "Press enter to continue." << endl;
cin.ignore();
return;
}
//remove leading and trailing white space from str
//e.g. " Hello World! " -> "Hello World!"
string trim(const string str){
const string whitespaces = " \t\n\r"; //these characters are to be trimmed
//first non-whitespace
size_t first = str.find_first_not_of(whitespaces);
//the string is all whitespace just return an empty string
if(first == string::npos) return "";
//last non-whitespace
size_t last = str.find_last_not_of(whitespaces);
//return the substring from first to last
return str.substr(first, (last - first + 1));
}
//determine the server name from the given file name
//e.g. "MyServer-WWFiles-01012011.out" -> "MyServer"
string get_server_name(string file_name){
string server_name = "";
//we append the characters from the file name until we get to a non-alphanumeric character
string::iterator i;
i = file_name.begin();
while(i != file_name.end() && isalnum(*i)){
server_name += *i;
i++;
}
return server_name;
}
//get the user's preferences from the preferences file
//returns false if no preferences file exists, true otherwise
bool set_prefs(void){
const string pref_file = "WWF Analyzer.pref";
//open the preferences file and load the contents
ifstream file (pref_file.c_str());
if(file.is_open()){
//while we still have lines to read
while(file.good()){
//read the line
string line;
getline(file,line);
//get values
if((line.compare(0,16,"SUMMARY_SERVERS=") == 0) || (line.compare(0,17,"SUMMARY_SERVERS =") == 0)){
istringstream ss;
ss.str(line.substr(line.find_last_of('=') + 1));
int val;
ss >> val;
if(!ss.fail()){
SUMMARY_SERVERS = val;
}
}
else if((line.compare(0,15,"SUMMARY_OWNERS=") == 0) || (line.compare(0,16,"SUMMARY_OWNERS =") == 0)){
istringstream ss;
ss.str(line.substr(line.find_last_of('=') + 1));
int val;
ss >> val;
if(!ss.fail()){
SUMMARY_OWNERS = val;
}
}
else if((line.compare(0,12,"HIGH_VOLUME=") == 0) || (line.compare(0,13,"HIGH_VOLUME =") == 0)){
istringstream ss;
ss.str(line.substr(line.find_last_of('=') + 1));
int val;
ss >> val;
if(!ss.fail()){
HIGH_VOLUME = val;
}
}
else if((line.compare(0,13,"MAX_CRITICAL=") == 0) || (line.compare(0,14,"MAX_CRITICAL =") == 0)){
istringstream ss;
ss.str(line.substr(line.find_last_of('=') + 1));
unsigned long val;
ss >> val;
if(!ss.fail()){
MAX_CRITICAL = val;
}
}
else if(line.compare(0,2,"i.") == 0){
string val = trim(line.substr(2));
if(val != ""){
ignore_files_classifier.add_extension(val);
}
}
else if(line.compare(0,2,"i:") == 0){
string val = trim(line.substr(2));
if(val != ""){
ignore_files_classifier.add_substring(val);
}
}
else if(line.compare(0,2,"c.") == 0){
string val = trim(line.substr(2));
if(val != ""){
critical_files_classifier.add_extension(val);
}
}
else if(line.compare(0,2,"c:") == 0){
string val = trim(line.substr(2));
if(val != ""){
critical_files_classifier.add_substring(val);
}
}
}
}
//if the preferences file does not exist, we create a new one and ask the user to rerun the program
else{
cout << "The preferences file (" << pref_file << ") does not exist." << endl
<< "A default preferences file will be created in the directory" << endl
<< "where this program is located." << endl << endl
<< "Take a look at the file, make any changes you want, and then rerun this program.";
ofstream pref;
pref.open (pref_file.c_str());
if(pref.is_open()){
pref << "# WWF Analyzer Preferences File (" << pref_file << ")" << endl << endl
<< "# Comments are indicated by a hash character (#) in the first position of a line." << endl
<< "# Malformed lines are also ignored." << endl << endl
<< "# Customizing the Summary Report:" << endl
<< "# To limit the number of results shown, include the following lines:" << endl
<< "# For the max number of servers to show:" << endl
<< "#SUMMARY_SERVERS=X" << endl
<< "# For the max number of owners to show:" << endl
<< "#SUMMARY_OWNERS=X" << endl
<< "# For the max number of the highest volume owners for each server (and vice versa) to show:" << endl
<< "#HIGH_VOLUME=X" << endl
<< "# Where X is the desired value." << endl
<< "# If the corresponding value is not set here, the program will default to not limiting the number shown." << endl << endl
<< "# Ignoring files:" << endl
<< "# To ignore a certain file extension, type i.[extension] on a new line." << endl
<< "# e.x. to ignore log files:" << endl
<< "#i.log" << endl
<< "# To ignore any file that contains a certain substring, type i:[substring] on a new line." << endl
<< "# e.x. to ignore all files which contain the substring \"harmless\":" << endl
<< "#i:harmless" << endl
<< "# This can be used to ignore directories as well by including the slashes." << endl
<< "# e.x. to ignore all files in any directory called /tmp/:" << endl
<< "#i:/tmp/" << endl
<< "# Note that this will ignore */tmp/*, so you may want to specify" << endl
<< "# the entire path upto that directory, if there are multiple directories with the same name" << endl
<< "# but with different parent directories." << endl << endl
<< "# Critical files:" << endl
<< "# To have the program flag certain files as critical," << endl
<< "# use the same method for ignoring extensions and substrings, but with \"c\" instead of \"i\"" << endl
<< "# e.x. to flag sh files:" << endl
<< "#c.sh" << endl
<< "# e.x. to flag all files in the \"home\" directory:" << endl
<< "#c:/home/" << endl
<< "# The program will default to not limiting the number of critical files shown in the details reports." << endl
<< "# To limit the number shown, include:" << endl
<< "#MAX_CRITICAL=X" << endl
<< "# Where X is the desired value." << endl;
}
else{
cerr << "Error: Unable to create preferences file." << endl
<< "Make sure that you have write access for the directory." << endl << endl;
}
pause();
return false;
}
return true;
}
//analyze the file for the given server and store the data in the list of WWFs
//returns the number of WWFs found
unsigned long analyze(ifstream& file, string directory, string server_name, list<WWF_data>& lst_WWF){
unsigned long WWFs = 0;
unsigned long ignored_files = 0;
unsigned long critical_files = 0;
list<critical_file_owner> lst_critical_files;
//check if the first character in the file is non-ascii
//if so, then the file likely contains formatted text, which we can't read easily
if(file.good() && (file.peek() != EOF)){
if(!__isascii(file.peek())){
cerr << endl << "Error:" << endl
<< "The contents of this file are unintelligible." << endl
<< "This is probably due to formatting being applied to the text." << endl
<< "Try copying the text into a new text file and redo the analysis." << endl
<< "If you chose to continue, the analysis of the remaining files will be done," << endl
<< "but this file will not be analyzed and no WWFs will be recorded." << endl << endl;
pause();
return 0;
}
}
//while we still have lines to read
while(file.good()){
//read the line
string line;
getline(file,line);
//we extract the permissions, owner, and file name from each line and discard the rest
istringstream ss;
ss.str(line);
string permissions, owner, file_name, discard;
ss >> permissions >> discard >> owner >> discard >> discard >> discard >> discard >> discard;
//the file name is just the rest of the line
getline(ss,file_name);
file_name = trim(file_name);
//only continue to process the line if the line contains valid data
//we expect the permissions symbolic notation to be 10 characters long
//the first character in permissions must be one of the following:
//'-' for regular file, 'd' for directory, or 'l' for link, or b, c, p, s
//else, we have an invalid line
if(!ss.fail() && (file_name != "") && (permissions.size() == 10) && (permissions.at(0) == '-'
|| permissions.at(0) == 'd' || permissions.at(0) == 'l'
|| permissions.at(0) == 'b' || permissions.at(0) == 'c'
|| permissions.at(0) == 'p' || permissions.at(0) == 's')){
//if the file is world writable then the second last char will be "w", not "-"
//also we make sure that the file is not one of the ones to be ignored
if((permissions.at(8) == 'w') && (!ignore_files_classifier.satisfies(file_name))){
//stores whether or not we can add an occurrence to an existing owner/server pair
bool existing_element = false;
//check if there is an element for this owner on this server
for (list<WWF_data>::iterator i = lst_WWF.begin(); i != lst_WWF.end(); i++){
if((i->owner == owner) && (i->server == server_name)){
//increment the number of occurrences of this owner on this server
i->count += 1;
//if the file is critical
if(critical_files_classifier.satisfies(file_name)){
//count it
i->critical += 1;
critical_files++;
//include the file in the list of critical files
critical_file_owner new_critical_file;
new_critical_file.owner = owner;
new_critical_file.file = file_name;
lst_critical_files.push_back(new_critical_file);
}
existing_element = true; //we added to an existing pair
break; //we can stop searching
}
}
//if there were no existing pairs, we add a new one to the list
if(!existing_element){
WWF_data new_WWF;
new_WWF.server = server_name;
new_WWF.owner = owner;
new_WWF.count = 1;
if(critical_files_classifier.satisfies(file_name)){
new_WWF.critical = 1;
critical_files++;
critical_file_owner new_critical_file;
new_critical_file.owner = owner;
new_critical_file.file = file_name;
lst_critical_files.push_back(new_critical_file);
}
else{
new_WWF.critical = 0;
}
lst_WWF.push_front(new_WWF);
}
WWFs++;
}
else{
ignored_files++;
}
}
}
lst_WWF.sort(greater<WWF_data>());
//create server details report
ofstream report;
report.open ((directory + server_name + " WWF Details Report.txt").c_str());
if(report.is_open()){
report << server_name << " WWF Details Report" << endl << endl;
report << "Files Found:\t" << (WWFs + ignored_files) << endl
<< "Files Ignored:\t" << ignored_files << endl
<< "# of WWFs:\t" << WWFs << endl
<< "Critical Files:\t" << critical_files << endl << endl << endl;
if(WWFs > 0){
report << ' ' << setw(COL_WIDTH) << left << "Owner" << "# of Files (# Critical)" << endl
<< setfill('-') << setw(COL_WIDTH) << right << "+" << setw(COL_WIDTH + COL_WIDTH/2) << "" << setfill(' ') << endl;
//for each element for the current server, print the owner and the number of files (and any critical files)
for (list<WWF_data>::iterator i = lst_WWF.begin(); i != lst_WWF.end(); i++){
if(i->server == server_name){
report << ' ' << setw(COL_WIDTH - 2) << left << i->owner << "| " << i->count;
if(i->critical > 0){
report << " (" << i->critical << ")";
}
report << endl;
}
}
}
else{
report << "No WWFs to report.";
}
//display the critical files
if(critical_files > 0){
lst_critical_files.sort();
report << endl << endl << endl << "Critical files:";
//for each critical file, or until the max number to display is reached...
list<critical_file_owner>::iterator i = lst_critical_files.begin();
unsigned long num_file = 0;
for(; (num_file < MAX_CRITICAL) && (i != lst_critical_files.end()); num_file++, i++){
report << endl << ' ' << setw(COL_WIDTH - 2) << left << i->owner << i->file;
}
//if we've hit the max number to display but there are still more files, inform the user there are too many critical files
if((num_file == MAX_CRITICAL) && (i != lst_critical_files.end())){
report << endl << endl << MAX_CRITICAL
<< " critical files have been displayed. However, there are more critical files than this." << endl
<< "They have been omitted as per the value of the \"MAX_CRITICAL\" setting.";
}
}
}
else{
cerr << "Error: Unable create report." << endl
<< "Make sure that you have write access for the directory" << endl
<< "and that any previously created reports are not in use," << endl
<< "so that they can be overwritten." << endl << endl;
}
return WWFs;
}
void summarize(ofstream& report, list<WWF_data>& lst_WWF){
lst_WWF.sort(greater<WWF_data>());
report << "WWF Summary Report" << endl << endl;
//if there are no WWFs
if(lst_WWF.size() == 0){
report << "There are no WWFs to report.";
return;
}
report << " Most Files by Server" << endl
<< setfill('-') << setw(25) << "" << setfill(' ') << endl << endl
<< ' ' << setw(COL_WIDTH) << left << "Server"
<< ' ' << setw(COL_WIDTH) << left << "# of Files"
<< "Top Owners" << endl
<< setfill('-') << setw(COL_WIDTH) << right << "+"
<< setw(COL_WIDTH) << right << "+"
<< setw(COL_WIDTH + COL_WIDTH/2 + 1) << "" << setfill(' ') << endl;
list<occurrences> lst_servers; //list of servers by number of WWFs
//determine order of the servers based on number of files
for(list<WWF_data>::iterator i = lst_WWF.begin(); i != lst_WWF.end(); i++){
//for each server/owner pair, we look for the sum of the files for the server and update it with the new files
bool existing_element = false;
for (list<occurrences>::iterator j = lst_servers.begin(); j != lst_servers.end(); j++){
if(i->server == j->entity){
j->count += i->count;
existing_element = true;
break;
}
}
//if the server does not have a sum yet, we add it
if(!existing_element){
occurrences new_server;
new_server.entity = i->server;
new_server.count = i->count;
lst_servers.push_front(new_server);
}
}
lst_servers.sort(greater<occurrences>());
//for each server, or until the max number of servers to display is reached...
list<occurrences>::iterator i = lst_servers.begin();
int server = 0;
for(; (server < SUMMARY_SERVERS) && (i != lst_servers.end()); server++, i++){
//display the total number of WWFs for the server
report << ' ' << setw(COL_WIDTH - 2) << left << i->entity
<< "| " << setw(COL_WIDTH - 2) << left << i->count << "| " << endl;
//for each owner on the server, or until the max number of top owners to display is reached...
list<WWF_data>::iterator j = lst_WWF.begin();
int top_owner = 0;
for(; (top_owner < HIGH_VOLUME) && (j != lst_WWF.end()); j++){
if(j->server == i->entity){
//display the number of WWFs for the owner
report << setw(COL_WIDTH) << right << '|' << setw(COL_WIDTH + 1) << right << " | "
<< setw(COL_WIDTH) << left << j->owner
<< setw(COL_WIDTH/2) << right << j->count << endl;
top_owner++;
}
}
report << setw(COL_WIDTH) << right << '|' << setw(COL_WIDTH + 1) << right << " | " << endl;
}
report << endl << endl << endl;
report << " Most Files by Owner" << endl
<< setfill('-') << setw(25) << "" << setfill(' ') << endl << endl
<< ' ' << setw(COL_WIDTH) << left << "Owner"
<< ' ' << setw(COL_WIDTH) << left << "# of Files"
<< "Servers" << endl
<< setfill('-') << setw(COL_WIDTH) << right << "+"
<< setw(COL_WIDTH) << right << "+"
<< setw(COL_WIDTH + COL_WIDTH/2 + 1) << "" << setfill(' ') << endl;
list<occurrences> lst_owners; //list of owners by number of WWFs
//determine order of the owners based on number of files
for(list<WWF_data>::iterator k = lst_WWF.begin(); k != lst_WWF.end(); k++){
//for each server/owner pair, we look for the sum of the files for the owner and update it with the new files
bool existing_element = false;
for (list<occurrences>::iterator j = lst_owners.begin(); j != lst_owners.end(); j++){
if(k->owner == j->entity){
j->count += k->count;
existing_element = true;
break;
}
}
//if the owner does not have a sum yet, we add it
if(!existing_element){
occurrences new_owner;
new_owner.entity = k->owner;
new_owner.count = k->count;
lst_owners.push_front(new_owner);
}
}
lst_owners.sort(greater<occurrences>());
//for each server, or until the max number of owners to display is reached...
list<occurrences>::iterator k = lst_owners.begin();
int owner = 0;
for(; (owner < SUMMARY_OWNERS) && (k != lst_owners.end()); owner++, k++){
//display the total number of WWFs for the owner
report << ' ' << setw(COL_WIDTH - 2) << left << k->entity
<< "| " << setw(COL_WIDTH - 2) << left << k->count << "| " << endl;
//for each server which has the owner, or until the max number of top servers to display is reached...
list<WWF_data>::iterator j = lst_WWF.begin();
int top_server = 0;
for(; (top_server < HIGH_VOLUME) && (j != lst_WWF.end()); j++){
if(j->owner == k->entity){
//display the number of WWFs for the server
report << setw(COL_WIDTH) << right << '|' << setw(COL_WIDTH + 1) << right << " | "
<< setw(COL_WIDTH) << left << j->server
<< setw(COL_WIDTH/2) << right << j->count << endl;
top_server++;
}
}
report << setw(COL_WIDTH) << right << '|' << setw(COL_WIDTH + 1) << right << " | " << endl;
}
list<occurrences> lst_num_critical; //list of the number of critical files per server
//count the number of critical files an each server
for(list<WWF_data>::iterator i = lst_WWF.begin(); i != lst_WWF.end(); i++){
//if this entry has no critical files, move on
if(i->critical == 0) continue;
//for each server/owner pair, we look for the sum of the critical files for the server and update it with the new files
bool existing_element = false;
for (list<occurrences>::iterator j = lst_num_critical.begin(); j != lst_num_critical.end(); j++){
if(i->server == j->entity){
j->count += i->critical;
existing_element = true;
break;
}
}
//if the server does not have a sum yet, we add it
if(!existing_element){
occurrences new_server;
new_server.entity = i->server;
new_server.count = i->critical;
lst_num_critical.push_front(new_server);
}
}
lst_num_critical.sort(greater<occurrences>());
//display the servers with critical files
if(lst_num_critical.size() > 0){
report << endl << endl << endl << "Critical files have been found on the following servers:";
for(list<occurrences>::iterator i = lst_num_critical.begin(); i != lst_num_critical.end(); i++){
report << endl << i->entity << " (" << i->count << ")";
}
}
return;
}