-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.cpp
More file actions
225 lines (187 loc) · 5.29 KB
/
Copy pathmisc.cpp
File metadata and controls
225 lines (187 loc) · 5.29 KB
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
/*
*
* (c) 2012 Markus Dittrich
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License Version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License Version 3 for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include <iostream>
#include <fstream>
#include <cstdio>
#include <sys/stat.h>
#include <vector>
#include <openssl/sha.h>
#include "misc.hpp"
#include "mescalero.hpp"
using std::cerr;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::ifstream;
using std::shared_ptr;
//
// error reporting function
//
void
errMsg(std::string message)
{
cerr << "Error: " << message << "\n" << endl;
}
//
// helper function to print out a file's hash
//
void
printHash(const string& fileName, const Sha256Hash& sha256Hash)
{
cout << fileName << ": ";
for (const unsigned char& item : *sha256Hash) {
printf("%02x", item);
}
cout << endl;
}
//
// convert a Sha256Hash type into a string represenation
//
void
hashToString(const Sha256Hash& sha256Hash, string& hashString)
{
hashString.clear();
char c[3];
for (const unsigned char& item : *sha256Hash) {
sprintf(c, "%02x", item);
hashString += c;
}
}
//
// check current file status against what we have stored
// in the database and complain if there's mismatch
//
void
checkAndPrintResult(const string& hashString,
const vector<std::string>& result,
const string& fileName, const struct stat *sb)
{
bool status{false};
string m;
if (result[1] != hashString) {
status = true;
m += "hash mismatch :: \n";
m = m + "(F) " + hashString + "\n";
m = m + "(E) " + result[1] + "\n";
}
if (result[2] != to_string(sb->st_uid) ) {
status = true;
resultFormatter("uid", to_string(sb->st_uid),
result[2], m);
}
if (result[3] != to_string(sb->st_gid) ) {
status = true;
resultFormatter("gid", to_string(sb->st_gid),
result[3], m);
}
if (result[4] != to_string(sb->st_mode) ) {
status = true;
resultFormatter("mode", to_string(sb->st_mode),
result[4], m);
}
if (result[5] != to_string(sb->st_size) ) {
status = true;
resultFormatter("size", to_string(sb->st_size),
result[5], m);
}
if (result[6] != to_string(sb->st_mtime) ) {
status = true;
resultFormatter("mtime", to_string(sb->st_mtime),
result[6], m);
}
if (result[7] != to_string(sb->st_ctime) ) {
status = true;
resultFormatter("ctime", to_string(sb->st_ctime),
result[7], m);
}
if (status) {
cout << "+++++++++ Mismatch ++++++++++++++++++++\n";
cout << "filename : " << fileName << "\n";
cout << m;
cout << "++++++++++++++++++++++++++++++++++++++\n" << endl;
}
}
//
// simple pretty printer for outputting expected versus
// found results
//
void
resultFormatter(const string &name, const string &found,
const string &expected, string &result)
{
result = result + name + " mismatch :: ";
result = result + "(F) " + found + " | ";
result = result + "(C) " + expected + "\n";
}
//
// function computing the sha256 hash of the file
// reference by ifstream file
//
Sha256Hash
hashAsSha256(ifstream &file)
{
// initialize OpenSSL
SHA256_CTX context;
unsigned char md[SHA256_DIGEST_LENGTH];
SHA256_Init(&context);
// process file
char buf[BUFSIZ];
while (file) {
file.read(buf, BUFSIZ);
SHA256_Update(&context, buf, file.gcount());
}
SHA256_Final(md, &context);
Sha256Hash hash(new vector<unsigned char>);
std::copy(md, md+SHA256_DIGEST_LENGTH, std::back_inserter(*hash));
return hash;
}
//
// sconcho usage information
//
void
usage()
{
cout << "mescalero v " << VERSION << " (C) 2012 Markus Dittrich\n\n"
<< "usage: mescalero [options]\n\n"
<< "Available options (at least one is required):\n\n"
<< "\t-p\n"
<< "\t password (*)\n\n"
<< "\t-d\n"
<< "\t path to database (*)\n\n"
<< "\t-c\n"
<< "\t check file properties\n\n"
<< "\t-u\n"
<< "\t update file properties\n\n"
<< "\t-l\n"
<< "\t list the current set of file paths\n\n"
<< "\t-f <list of file paths>\n"
<< "\t set new file paths for file checking.\n"
<< "\t NOTE: This will erase all previously added paths and\n"
<< "\t replace them with the new ones.\n\n"
<< "\t-a\n"
<< "\t Similar to -f but will append/remove file paths from the\n"
<< "\t current list. Filepaths can be prefixed with '+' or '-' to\n"
<< "\t append or remove them from the current list, respectively.\n"
<< "\t Without any '+' or '-' prefix, '+' is assumed implicitly.\n\n"
<< "Options with (*) are required.\n"
<< "\n"
<< endl;
}