-
Notifications
You must be signed in to change notification settings - Fork 1
/
pa5-encfs.c
570 lines (441 loc) · 10.7 KB
/
pa5-encfs.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
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
#define FUSE_USE_VERSION 28
#define HAVE_SETXATTR
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef linux
/* For pread()/pwrite() */
#define _XOPEN_SOURCE 500
#endif
#define XATTR_ENCRYPTED "user.encrypted"
#include <fuse.h>
#include <linux/limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/time.h>
#include "aes-crypt.h"
#ifdef HAVE_SETXATTR
#include <sys/xattr.h>
#endif
const int ENCRYPT = 1;
const int DECRYPT = 0;
const int COPY = -1;
char * mirrorDir;
char * keyPhrase;
/* function for appending our mirror path to the path name */
static void appendPath(char newPath[PATH_MAX], const char* path){
strcpy(newPath, mirrorDir);
strncat(newPath, path, PATH_MAX);
}
static int xmp_getattr(const char *path, struct stat *stbuf)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = lstat(newPath, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int xmp_access(const char *path, int mask)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = access(newPath, mask);
if (res == -1)
return -errno;
return 0;
}
static int xmp_readlink(const char *path, char *buf, size_t size)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = readlink(newPath, buf, size - 1);
if (res == -1)
return -errno;
buf[res] = '\0';
return 0;
}
static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
DIR *dp;
struct dirent *de;
(void) offset;
(void) fi;
char newPath[PATH_MAX];
appendPath(newPath, path);
dp = opendir(newPath);
if (dp == NULL)
return -errno;
while ((de = readdir(dp)) != NULL) {
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, 0))
break;
}
closedir(dp);
return 0;
}
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
/* On Linux this could just be 'mknod(path, mode, rdev)' but this
is more portable */
if (S_ISREG(mode)) {
res = open(newPath, O_CREAT | O_EXCL | O_WRONLY, mode);
if (res >= 0)
res = close(res);
} else if (S_ISFIFO(mode))
res = mkfifo(path, mode);
else
res = mknod(path, mode, rdev);
if (res == -1)
return -errno;
return 0;
}
static int xmp_mkdir(const char *path, mode_t mode)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = mkdir(newPath, mode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_unlink(const char *path)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = unlink(newPath);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rmdir(const char *path)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = rmdir(newPath);
if (res == -1)
return -errno;
return 0;
}
static int xmp_symlink(const char *from, const char *to)
{
int res;
res = symlink(from, to);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rename(const char *from, const char *to)
{
int res;
res = rename(from, to);
if (res == -1)
return -errno;
return 0;
}
static int xmp_link(const char *from, const char *to)
{
int res;
res = link(from, to);
if (res == -1)
return -errno;
return 0;
}
static int xmp_chmod(const char *path, mode_t mode)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = chmod(newPath, mode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_chown(const char *path, uid_t uid, gid_t gid)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = lchown(newPath, uid, gid);
if (res == -1)
return -errno;
return 0;
}
static int xmp_truncate(const char *path, off_t size)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = truncate(newPath, size);
if (res == -1)
return -errno;
return 0;
}
static int xmp_utimens(const char *path, const struct timespec ts[2])
{
int res;
struct timeval tv[2];
char newPath[PATH_MAX];
appendPath(newPath, path);
tv[0].tv_sec = ts[0].tv_sec;
tv[0].tv_usec = ts[0].tv_nsec / 1000;
tv[1].tv_sec = ts[1].tv_sec;
tv[1].tv_usec = ts[1].tv_nsec / 1000;
res = utimes(newPath, tv);
if (res == -1)
return -errno;
return 0;
}
static int xmp_open(const char *path, struct fuse_file_info *fi)
{
int res;
fprintf(stdout, "**************************************\n");
fprintf(stdout, "opening File\n");
fprintf(stdout, "**************************************\n");
char newPath[PATH_MAX];
appendPath(newPath, path);
res = open(newPath, fi->flags);
if (res == -1)
return -errno;
close(res);
return 0;
}
static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
fprintf(stdout, "**************************************\n");
fprintf(stdout, "Reading File\n");
fprintf(stdout, "**************************************\n");
int res;
(void) fi;
(void) offset;
char newPath[PATH_MAX];
appendPath(newPath, path);
// Open file and Create Tempfile
FILE * pFile = fopen (newPath,"r");
FILE *tempfile = tmpfile();
// Get attributes for
int value = 0;
getxattr(newPath, XATTR_ENCRYPTED, &value, sizeof(value));
fprintf(stdout, "XATTR VALUE IS: %d\n", value);
// Depending on the attribute value, decrypt or copy
int action = value ? DECRYPT : COPY;
do_crypt(pFile, tempfile, action, keyPhrase);
// Move pointer back to head of files.
rewind(tempfile);
rewind(pFile);
// size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
// ptr = buffer, size = block size, nmemb = number of blocks
res = fread(buf, 1, size, tempfile);
if (res == -1)
res = -errno;
// Cleanup
fclose(tempfile);
fclose(pFile);
return res;
}
static int xmp_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
fprintf(stdout, "**************************************\n");
fprintf(stdout, "Writing File\n");
fprintf(stdout, "**************************************\n");
int res;
(void) offset;
(void) fi;
char newPath[PATH_MAX];
appendPath(newPath, path);
// Open the file for reading AND writing
FILE * pFile = fopen (newPath,"r+");
FILE *tempfile = tmpfile();
// Get Xattr to determin if we need to decrpyt
int value = 0;
getxattr(newPath, XATTR_ENCRYPTED, &value, sizeof(value));
fprintf(stdout, "XATTR VALUE IS: %d\n", value);
// Depending on the attribute value, decrypt or copy
int action = value? DECRYPT: COPY;
do_crypt(pFile, tempfile, action, keyPhrase);
rewind(pFile);
// Get file descriptor of tempfile to use with pwrite
int fd = fileno(tempfile);
res = pwrite(fd, buf, size, offset);
if (res == -1)
res = -errno;
//Rewind file pointer to start of file for tempfile
rewind(tempfile);
// Only encrypt if it was a previously encrypted file
action = (action == DECRYPT) ? ENCRYPT : COPY;
do_crypt(tempfile, pFile, action, keyPhrase);
// Cleanup
fclose(pFile);
fclose(tempfile);
return res;
}
static int xmp_statfs(const char *path, struct statvfs *stbuf)
{
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = statvfs(newPath, stbuf);
if (res == -1)
return -errno;
return 0;
}
static int xmp_create(const char* path, mode_t mode, struct fuse_file_info* fi) {
(void) fi;
int res;
char newPath[PATH_MAX];
appendPath(newPath, path);
res = creat(newPath, mode);
if(res == -1)
return -errno;
// Encrypt files created in fuse filesystem
int encryptValue = 1;
int ret = setxattr(newPath, XATTR_ENCRYPTED, &encryptValue, sizeof(encryptValue), 0);
if (ret == -1){
fprintf(stdout, "THE VALUE ENCODING FAILED\n");
return -errno;
}
close(res);
return 0;
}
static int xmp_release(const char *path, struct fuse_file_info *fi)
{
fprintf(stdout, "**************************************\n");
fprintf(stdout, "Release File\n");
fprintf(stdout, "**************************************\n");
/* Just a stub. This method is optional and can safely be left
unimplemented */
(void) fi;
(void) path;
return 0;
}
static int xmp_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
/* Just a stub. This method is optional and can safely be left
unimplemented */
(void) path;
(void) isdatasync;
(void) fi;
return 0;
}
#ifdef HAVE_SETXATTR
static int xmp_setxattr(const char *path, const char *name, const char *value,
size_t size, int flags)
{
char newPath[PATH_MAX];
appendPath(newPath, path);
int res = lsetxattr(newPath, name, value, size, flags);
if (res == -1)
return -errno;
return 0;
}
static int xmp_getxattr(const char *path, const char *name, char *value,
size_t size)
{
char newPath[PATH_MAX];
appendPath(newPath, path);
int res = lgetxattr(newPath, name, value, size);
if (res == -1)
return -errno;
return res;
}
static int xmp_listxattr(const char *path, char *list, size_t size)
{
char newPath[PATH_MAX];
appendPath(newPath, path);
int res = llistxattr(newPath, list, size);
if (res == -1)
return -errno;
return res;
}
static int xmp_removexattr(const char *path, const char *name)
{
char newPath[PATH_MAX];
appendPath(newPath, path);
int res = lremovexattr(newPath, name);
if (res == -1)
return -errno;
return 0;
}
#endif /* HAVE_SETXATTR */
static struct fuse_operations xmp_oper = {
.getattr = xmp_getattr,
.access = xmp_access,
.readlink = xmp_readlink,
.readdir = xmp_readdir,
.mknod = xmp_mknod,
.mkdir = xmp_mkdir,
.symlink = xmp_symlink,
.unlink = xmp_unlink,
.rmdir = xmp_rmdir,
.rename = xmp_rename,
.link = xmp_link,
.chmod = xmp_chmod,
.chown = xmp_chown,
.truncate = xmp_truncate,
.utimens = xmp_utimens,
.open = xmp_open,
.read = xmp_read,
.write = xmp_write,
.statfs = xmp_statfs,
.create = xmp_create,
.release = xmp_release,
.fsync = xmp_fsync,
#ifdef HAVE_SETXATTR
.setxattr = xmp_setxattr,
.getxattr = xmp_getxattr,
.listxattr = xmp_listxattr,
.removexattr = xmp_removexattr,
#endif
};
void usage(){
printf("Usage: ./pa5-encfs <key-phrase> <mirror-dir> <mount-point> [options]");
}
int main(int argc, char *argv[])
{
if(argc <= 3){
usage();
exit(EXIT_FAILURE);
}
keyPhrase = argv[1];
mirrorDir = realpath(argv[2], NULL);
if (mirrorDir == NULL){
printf("Failed loading mirror directory\n");
exit(EXIT_FAILURE);
}
if (realpath(argv[3], NULL) == NULL){
printf("Failed loading mount point\n");
exit(EXIT_FAILURE);
}
printf("Key Phrase: %s \n", keyPhrase);
printf("Mirror Directory: %s \n", mirrorDir);
int i;
for ( i = 3; i < argc; i++){
argv[i-2] = argv[i];
}
argv[argc-1] = NULL;
argv[argc-2] = NULL;
argc -= 2;
umask(0);
return fuse_main(argc, argv, &xmp_oper, NULL);
}