-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcryptsetup-gui.c
300 lines (232 loc) · 6.02 KB
/
cryptsetup-gui.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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define DEBUG true
bool decrypt(char* name, char* device, char* options, char* password);
bool mount(char* mountpoint);
char *strstrip(char *s);
void show_password_prompt(char* arg0);
void usage();
bool do_mount = false;
char* arg0 = NULL;
char *name = NULL, *device = NULL, *options = NULL;
char* mountpoint = "/dev/mapper/";
int main(int argc, char** argv) {
if (DEBUG)
printf("starting cryptsetup-gui\n");
arg0 = *argv;
argv++;
argc--;
if (argc == 0) {
usage();
exit(EXIT_SUCCESS);
}
if (strcmp(*argv, "-m") == 0) {
do_mount = true;
argv++;
argc--;
if (DEBUG)
printf("mount after unlocking\n");
}
if (argc != 1) {
usage();
exit(EXIT_SUCCESS);
}
char* cryptpoint = *argv;
char* ct = cryptpoint;
if (DEBUG)
printf("verifying cryptpoint\n");
while (*ct != 0) {
if (*ct < 'a' || *ct > 'z') {
fprintf(stderr, "Non a-z cryptpoint given\n");
exit(EXIT_FAILURE);
}
ct++;
}
if (DEBUG)
printf("cryptpoint verified\n");
size_t mps = strlen(mountpoint);
size_t cps = strlen(cryptpoint);
char* tmp = malloc(sizeof(char) * (mps + cps));
strncpy(tmp, mountpoint, mps);
strncpy(tmp + mps, cryptpoint, cps);
mountpoint = tmp;
if (DEBUG)
printf("mountpoint resolved to '%s'\n", mountpoint);
if (access(mountpoint, F_OK) == 0) {
if (DEBUG)
printf("/dev/mapper listing already exists\n");
if (DEBUG && !do_mount)
printf("not told to mount automatically (-m), exiting...\n");
// Mountpoint already exists...
if (do_mount && !mount(mountpoint)) {
fprintf(stderr, "Failed to mount device %s\n", cryptpoint);
exit(EXIT_FAILURE);
}
if (DEBUG && do_mount)
printf("mountpoint successfully mounted\n");
exit(EXIT_SUCCESS);
}
if (DEBUG)
printf("parsing crypttab\n");
FILE *f = fopen("/etc/crypttab", "re");
if (!f) {
perror("Failed to open crypttab for reading");
exit(EXIT_FAILURE);
}
if (DEBUG)
printf("crypttab opened\n");
char *l, *p = NULL;
char line[1024];
int n = 0;
for (;;) {
int k;
if (!fgets(line, sizeof(line), f))
break;
n++;
l = strstrip(line);
if (*l == '#' || *l == 0)
continue;
k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &p, &options);
free(p);
p = NULL;
if (k < 2 || k > 4) {
fprintf(stderr, "Failed to parse /etc/crypttab:%u, ignoring\n", n);
goto next;
}
if (strcmp(name, cryptpoint) == 0) {
// We found our cryptpoint
if (DEBUG)
printf("crypttab entry found for cryptpoint '%s'\n", device);
break;
}
next:
free(name);
free(device);
free(options);
name = NULL;
device = NULL;
options = NULL;
}
fclose(f);
if (DEBUG)
printf("crypttab parsing ended\n");
if (name == NULL || device == NULL) {
fprintf(stderr, "Entry for %s not found in crypttab\n", cryptpoint);
}
show_password_prompt(arg0);
return 0;
}
bool unlock(char* password) {
// Try decrypting (note that password is not needed)
if (!decrypt(name, device, options, password)) {
fprintf(stderr, "Failed to decrypt device %s\n", name);
return false;
}
if (do_mount) {
if (!mount(mountpoint)) {
fprintf(stderr, "Failed to mount device %s\n", name);
return false;
}
}
return true;
}
bool decrypt(char* name, char* device, char* options, char* password) {
// TODO: Respect options list
// We need to be weary of a bug in cryptsetup
// https://groups.google.com/forum/#!msg/linux.debian.bugs.dist/7yRXc5NGMJM/q80hakUzDVMJ
// cryptsetup drops privileges if EUID != UID
// so, we store the old UID so we can restore it later
uid_t ruid = getuid();
char* flags = "";
if (strstr(options, "allow-discards") != NULL) {
flags = "--allow-discards";
}
char* command = NULL;
asprintf(&command, "/usr/sbin/cryptsetup %s -q luksOpen %s %s", flags, device, name);
setreuid(0, 0);
fflush(stdout);
FILE *crypt = popen(command, "w");
free(command);
fprintf(crypt, "%s\n%d", password, EOF);
int ret = pclose(crypt);
// restore UID
setreuid(ruid, 0);
return WEXITSTATUS(ret) == 0;
}
bool mount(char* mountpoint) {
// Apparently, we need this EUID != UID fix for mount as well...
uid_t ruid = getuid();
char* command = NULL;
fflush(stdout);
asprintf(&command, "/bin/mount %s", mountpoint);
setreuid(0, 0);
FILE *mnt = popen(command, "r");
int ret = pclose(mnt);
setreuid(ruid, 0);
return WEXITSTATUS(ret) == 0;
}
// Thank you kernel
bool is_space(char s) {
return s == ' ' || s == '\t' || s == '\n';
}
char *strstrip(char *s) {
size_t size;
char *end;
size = strlen(s);
if (!size)
return s;
end = s + size - 1;
while (end >= s && is_space(*end))
end--;
*(end + 1) = '\0';
while (*s && is_space(*s))
s++;
return s;
}
void show_password_prompt(char* arg0) {
if (DEBUG)
printf("showing gui\n");
if (DEBUG)
printf("dropping permissions\n");
// We don't want to give the GTK any root access
uid_t ruid = getuid();
seteuid(ruid);
if (DEBUG)
printf("permissions dropped\n");
fflush(stdout);
char* command = NULL;
asprintf(&command, "%s-gtk", arg0);
FILE *pw = popen(command, "r");
free(command);
if (DEBUG)
printf("gui started\n");
// Now get the password
char password[1024];
fgets(password, 1024, pw);
strtok(password, "\n");
if (DEBUG)
printf("password received\n");
pclose(pw);
fflush(stdout);
if (DEBUG)
printf("gui closed, resuming root\n");
// Need root to do the unlocking
seteuid(0);
if (DEBUG)
printf("EUID now %d, unlocking...\n", geteuid());
if (unlock(password)) {
if (DEBUG)
printf("unlocked successfully\n");
} else {
fflush(stdout);
fprintf(stderr, "invalid password given, unlock not possible\n");
exit(EXIT_FAILURE);
}
}
void usage() {
printf("Usage: %s [-m] cryptpoint\n", arg0);
}