-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxde_cryfs_exec.c
97 lines (92 loc) · 2.22 KB
/
lxde_cryfs_exec.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
#include "lxde_cryfs_exec.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int lxde_cryfs_exec_mount (const char* base, const char* mount, const char* cipher, const char* password, const char* blocksize, const char* unmount_idle) {
char prompt[1024] = {0};
FILE *shell = 0;
if (!blocksize || strlen (blocksize) < 1) {
blocksize = "32768";
}
if (!unmount_idle || strlen (unmount_idle) < 1) {
snprintf (prompt, sizeof(prompt), "CRYFS_FRONTEND=noninteractive cryfs --cipher %s --blocksize %s \"%s\" \"%s\"",
cipher, blocksize, base, mount);
} else {
snprintf (prompt, sizeof(prompt), "CRYFS_FRONTEND=noninteractive cryfs --cipher %s --blocksize %s --unmount-idle %s \"%s\" \"%s\"",
cipher, blocksize, unmount_idle, base, mount);
}
shell = popen (prompt, "w");
if (!shell) {
return 0;
}
fprintf(shell, "%s\n", password);
if (pclose (shell) != 0) {
return 0;
}
return 1;
}
int lxde_cryfs_exec_umount (const char* mount) {
char prompt[1024] = {0};
FILE *shell = 0;
if (!mount || strlen (mount) < 1) {
return 0;
}
snprintf (prompt, sizeof(prompt), "fusermount -u \"%s\"", mount);
shell = popen (prompt, "w");
if (!shell) {
return 0;
}
if (pclose (shell) != 0) {
return 0;
}
return 1;
}
int mounted_fs_size () {
char buffer[32] = {0};
int size = 0;
FILE* shell = popen("mount | grep cryfs | cut -d ' ' -f3 | wc -l", "r");
if (!shell) {
return size;
}
fread (buffer, 1, sizeof(buffer), shell);
if (pclose (shell) != 0) {
return size;
}
size = strtol (buffer, 0, 10);
return (size < 0 ? 0 : (size > 64 ? 64 : size));
}
int lxde_cryfs_mounted_fs (paths_t* path) {
char *line = NULL;
size_t len = 0;
ssize_t nread;
int size = mounted_fs_size ();
if (size < 1) {
return 0;
}
path->size = 0;
path->entry = calloc (size, sizeof(char*));
if (!path->entry) {
return 0;
}
FILE* shell = popen("mount | grep cryfs | cut -d ' ' -f3", "r");
if (!shell) {
free (path->entry);
path->entry = 0;
return 0;
}
for (int i = 0; i < size; ++i) {
if ((nread = getline (&line, &len, shell)) != -1) {
line[nread-1] = 0;
path->entry[i] = line;
path->size++;
line = NULL;
len = 0;
}
}
if (pclose (shell) != 0) {
free (path->entry);
path->entry = 0;
return 0;
}
return 1;
}