-
Notifications
You must be signed in to change notification settings - Fork 17
/
encfs_wrapper.cpp
252 lines (223 loc) · 6.07 KB
/
encfs_wrapper.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
/*
* This file is part of Cryptkeeper.
* Copyright (C) 2007 Tom Morton
*
* Cryptkeeper 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.
*
* Cryptkeeper 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 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <mntent.h>
#include <poll.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <assert.h>
// only for the gettext _
#include "cryptkeeper.h"
bool is_mounted(const char *mount_dir)
{
FILE *f = setmntent("/etc/mtab", "r");
char *mount_dir_expanded = realpath(mount_dir, NULL);
if (mount_dir_expanded == NULL) {
// no such file or dir, ...
// so: not mounted
// perror("cryptkeeper, is_mounted");
return false;
}
for (;;) {
char *mnt_dir_expanded;
struct mntent *m = getmntent(f);
if (!m) break;
mnt_dir_expanded = realpath(m->mnt_dir, NULL);
if (strcmp(mount_dir_expanded, mnt_dir_expanded)==0) {
free(mnt_dir_expanded);
return true;
}
}
return false;
}
int encfs_stash_get_info (const char *crypt_dir, char **output)
{
int status_fd[2];
*output = NULL;
assert (pipe (status_fd) == 0);
int pid = fork ();
if (pid == 0) {
dup2 (status_fd[1], 1);
close (status_fd[0]);
execlp ("encfsctl", "encfsctl", "info", crypt_dir, NULL);
exit (0);
}
close (status_fd[1]);
char buf[1024];
memset (buf, 0, sizeof (buf));
read (status_fd[0], buf, sizeof (buf));
int status;
waitpid (pid, &status, 0);
*output = strdup (buf);
return status;
}
int encfs_stash_change_password (const char *crypt_dir, const char *old_password, const char *new_password, char **output)
{
int fd[2], status_fd[2];
assert (pipe (fd) == 0);
assert (pipe (status_fd) == 0);
int pid = fork ();
if (pid == 0) {
dup2 (fd[0], 0);
dup2 (status_fd[1], 1);
close (fd[1]);
close (status_fd[0]);
execlp ("encfsctl", "encfsctl", "autopasswd", crypt_dir, NULL);
exit (0);
}
close (fd[0]);
close (status_fd[1]);
write (fd[1], old_password, strlen (old_password));
write (fd[1], "\n", 1);
write (fd[1], new_password, strlen (new_password));
write (fd[1], "\n", 1);
close (fd[1]);
char buf[1024];
memset (buf, 0, sizeof (buf));
read (status_fd[0], buf, sizeof (buf));
//printf ("YES [%s]\n", buf);
char *wank;
wank = strchr (buf, '\n');
//if (wank) wank = strchr (wank+1, '\n');
//printf ("YEES [%s]\n", wank);
close (status_fd[0]);
int status;
waitpid (pid, &status, 0);
*output = strdup (wank);
return status;
}
int encfs_stash_new (const char *crypt_dir, const char *mount_dir, const char *password)
{
int fd[2];
assert (pipe (fd) == 0);
mkdir (crypt_dir, 0700);
mkdir (mount_dir, 0700);
int pid = fork ();
if (pid == 0) {
dup2 (fd[0], 0);
// don't want to see encfses stdout bullshit
int devnull = open("/dev/null", O_WRONLY);
dup2(devnull, 1);
close (fd[1]);
execlp ("encfs", "encfs", "-S", crypt_dir, mount_dir, NULL);
exit (0);
}
close (fd[0]);
// paranoid default setup mode
//write (fd[1], "y\n", 2);
//write (fd[1], "y\n", 2);
write (fd[1], "p\n", 2);
write (fd[1], password, strlen (password));
write (fd[1], "\n", 1);
close (fd[1]);
int status;
waitpid (pid, &status, 0);
return !is_mounted(mount_dir);
// return status;
}
int encfs_stash_mount(const char *crypt_dir, const char *mount_dir, const char *password, int idle_timeout,
bool allow_other, char **output)
{
mkdir (mount_dir, 0700);
if (rmdir (mount_dir) == -1) {
char buf[256];
snprintf(buf, sizeof(buf), "%s\n\n%s",
_("The encrypted folder could not be mounted because the mount point is not empty:"),
mount_dir);
*output = strdup(buf);
return 255;
}
mkdir (mount_dir, 0700);
int fd[2], status_fd[2];
assert(pipe(fd) == 0);
assert(pipe(status_fd) == 0);
int pid = fork ();
if (pid == 0) {
dup2(fd[0], 0);
dup2(status_fd[1], 1);
dup2(status_fd[1], 2);
close(fd[1]);
close(status_fd[0]);
if (idle_timeout == 0) {
if (allow_other) {
execlp ("encfs", "encfs", "-S", crypt_dir, mount_dir, "--", "-o", "allow_other", NULL);
} else {
execlp ("encfs", "encfs", "-S", crypt_dir, mount_dir, NULL);
}
} else {
char buf[256];
snprintf (buf, sizeof (buf), "--idle=%d", idle_timeout);
if (allow_other) {
execlp ("encfs", "encfs", buf, "-S", crypt_dir, mount_dir, "--", "-o", "allow_other", NULL);
}
else {
execlp ("encfs", "encfs", buf, "-S", crypt_dir, mount_dir, NULL);
}
}
exit (0);
}
close(status_fd[1]);
close(fd[0]);
write(fd[1], password, strlen(password));
write(fd[1], "\n", 1);
close(fd[1]);
// unfortunately encfs does not report errors in a consistent way. eg
// the 'mountpoint is not empty' error is not written to fd=1 but the
// password incorrect error msg is -- hence the stupid crap at the
// start of this function
struct pollfd pfd;
pfd.fd = status_fd[0];
pfd.events = POLLIN;
poll(&pfd, 1, 1000);
if (pfd.revents & POLLIN) {
// encfs has a message for us
char buf[256];
memset(buf,0,sizeof(buf));
read(status_fd[0], buf, sizeof(buf));
close(status_fd[0]);
*output = strdup(buf);
} else {
*output = NULL;
}
int status;
waitpid (pid, &status, 0);
//printf("status %d\n", status);
//return status;
//return output != NULL
// encfs returned status is useless, so we just have to test like this...
return !is_mounted(mount_dir);
}
int encfs_stash_unmount (const char *mount_dir)
{
// unmount
int pid = fork ();
if (pid == 0) {
execlp ("fusermount", "fusermount", "-u", mount_dir, NULL);
exit (0);
}
int status;
waitpid (pid, &status, 0);
//return status;
return is_mounted(mount_dir);
}