forked from tbvdm/sigbak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd-export-attachments.c
445 lines (381 loc) · 9.06 KB
/
cmd-export-attachments.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
/*
* Copyright (c) 2018 Tim van der Molen <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "sigbak.h"
static enum cmd_status cmd_export_attachments(int, char **);
const struct cmd_entry cmd_export_attachments_entry = {
.name = "export-attachments",
.alias = "att",
.usage = "[-aIMm] [-p passfile] backup [directory]",
.exec = cmd_export_attachments
};
/*
* Create a file with a unique name. If a file with the specified name already
* exists, a new, unique name is used. Given a name of the form "base[.ext]",
* the new name is of the form "base-n[.ext]" where 1 < n < 1000.
*/
static FILE *
create_unique_file(int dfd, const char *name)
{
FILE *fp;
char *buf;
const char *base, *ext, *oldname;
size_t baselen, bufsize, namelen;
int fd, i;
buf = NULL;
oldname = name;
for (i = 2; i <= 1000; i++) {
fd = openat(dfd, name, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd != -1)
goto out;
if (errno != EEXIST) {
warn("%s", name);
goto out;
}
if (buf == NULL) {
namelen = strlen(name);
ext = strrchr(name, '.');
if (ext != NULL && ext != name && ext[1] != '\0')
baselen = ext - name;
else {
baselen = namelen;
ext = "";
}
if (namelen > SIZE_MAX - 5 || baselen > INT_MAX) {
warnx("Attachment filename too long");
goto out;
}
/* 4 for the "-n" affix and 1 for the NUL */
bufsize = namelen + 5;
if ((buf = malloc(bufsize)) == NULL) {
warn(NULL);
goto out;
}
base = name;
name = buf;
}
snprintf(buf, bufsize, "%.*s-%d%s", (int)baselen, base, i,
ext);
}
warnx("%s: Cannot generate unique name", oldname);
out:
if (fd == -1)
fp = NULL;
else if ((fp = fdopen(fd, "w")) == NULL) {
warn("%s", name);
close(fd);
}
free(buf);
return fp;
}
// get the name of an attachment file
// attention:
// this does not obey possible renames by create_unique_file()
// so it is better to use it with FLAG_FILENAME_ID which ensures a unique filename
char *
get_file_name(struct sbk_attachment *att, int flags)
{
struct tm *tm;
char *name, *tmp;
const char *ext;
time_t tt;
char base[100];
if (att->filename != NULL && *att->filename != '\0') {
if ((name = strdup(att->filename)) == NULL) {
warn(NULL);
return NULL;
}
sanitise_filename(name);
} else {
tt = att->time_sent / 1000;
if ((tm = localtime(&tt)) == NULL) {
warnx("localtime() failed");
return NULL;
}
snprintf(base, sizeof base,
"attachment-%d-%02d-%02d-%02d-%02d-%02d",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
if (att->content_type == NULL)
ext = NULL;
else
ext = mime_get_extension(att->content_type);
if (ext == NULL) {
if ((name = strdup(base)) == NULL) {
warn(NULL);
return NULL;
}
} else {
if (asprintf(&name, "%s.%s", base, ext) == -1) {
warnx("asprintf() failed");
return NULL;
}
}
}
if (flags & FLAG_FILENAME_ID) {
if (asprintf(&tmp, "%s %s",
sbk_attachment_id_to_string(att), name) == -1) {
warnx("asprintf() failed");
free(name);
return NULL;
}
free(name);
name = tmp;
}
return name;
}// get_file_name()
static FILE *
get_file(int dfd, struct sbk_attachment *att, int flags)
{
FILE *fp= 0;
char *name= get_file_name(att, flags);
if (name)
{ fp = create_unique_file(dfd, name);
free(name);
}
return fp;
}
static int
get_thread_directory(int dfd, struct sbk_thread *thd)
{
char *name;
int thd_dfd;
if ((name = get_recipient_filename(thd->recipient, NULL)) == NULL)
return -1;
if (mkdirat(dfd, name, 0777) == -1 && errno != EEXIST) {
warn("%s", name);
free(name);
return -1;
}
if ((thd_dfd = openat(dfd, name, O_RDONLY | O_DIRECTORY)) == -1) {
warn("%s", name);
free(name);
return -1;
}
free(name);
return thd_dfd;
}
static int
set_mtime(FILE *fp, uint64_t msec)
{
struct timespec ts[2];
if (fflush(fp) == EOF) {
warn("fflush");
return -1;
}
ts[0].tv_nsec = UTIME_OMIT;
ts[1].tv_sec = msec / 1000;
ts[1].tv_nsec = msec % 1000 * 1000000;
if (futimens(fileno(fp), ts) == -1) {
warn("futimens");
return -1;
}
return 0;
}
static int
export_thread_attachments(struct sbk_ctx *ctx, struct sbk_thread *thd, int dfd,
int flags)
{
struct sbk_attachment_list *lst;
struct sbk_attachment *att, *tmp;
FILE *fp;
int ret, thd_dfd;
if ((lst = sbk_get_attachments_for_thread(ctx, thd)) == NULL)
return -1;
if (~flags & FLAG_EXPORT_ALL) {
/* Skip long-message attachments */
TAILQ_FOREACH_SAFE(att, lst, entries, tmp)
if (att->content_type != NULL &&
strcmp(att->content_type, SBK_LONG_TEXT_TYPE) == 0)
TAILQ_REMOVE(lst, att, entries);
}
if (TAILQ_EMPTY(lst)) {
sbk_free_attachment_list(lst);
return 0;
}
if ((thd_dfd = get_thread_directory(dfd, thd)) == -1) {
sbk_free_attachment_list(lst);
return -1;
}
ret = 0;
TAILQ_FOREACH(att, lst, entries) {
if (att->file == NULL)
continue;
if ((fp = get_file(thd_dfd, att, flags)) == NULL) {
ret = -1;
continue;
}
if (sbk_write_file(ctx, att->file, fp) == -1) {
fclose(fp);
ret = -1;
continue;
}
switch (flags & FLAG_MTIME_MASK) {
case FLAG_MTIME_SENT:
if (set_mtime(fp, att->time_sent) == -1) {
fclose(fp);
ret = -1;
continue;
}
break;
case FLAG_MTIME_RECV:
if (set_mtime(fp, att->time_recv) == -1) {
fclose(fp);
ret = -1;
continue;
}
break;
}
if (fclose(fp) == EOF) {
warn("fclose");
ret = -1;
}
}
close(thd_dfd);
sbk_free_attachment_list(lst);
return ret;
}
static int
export_attachments(struct sbk_ctx *ctx, const char *outdir, int flags)
{
struct sbk_thread_list *lst;
struct sbk_thread *thd;
int dfd, ret;
if ((dfd = open(outdir, O_RDONLY | O_DIRECTORY)) == -1) {
warn("%s", outdir);
return -1;
}
if ((lst = sbk_get_threads(ctx)) == NULL) {
close(dfd);
return -1;
}
ret = 0;
SIMPLEQ_FOREACH(thd, lst, entries) {
if (export_thread_attachments(ctx, thd, dfd, flags) == -1)
ret = -1;
}
sbk_free_thread_list(lst);
close(dfd);
return ret;
}
static enum cmd_status
cmd_export_attachments(int argc, char **argv)
{
struct sbk_ctx *ctx;
char *backup, *passfile, passphr[128];
const char *outdir;
int c, flags, ret;
flags = 0;
passfile = NULL;
while ((c = getopt(argc, argv, "aIMmp:")) != -1)
switch (c) {
case 'a':
flags |= FLAG_EXPORT_ALL;
break;
case 'I':
flags |= FLAG_FILENAME_ID;
break;
case 'M':
flags &= ~FLAG_MTIME_RECV;
flags |= FLAG_MTIME_SENT;
break;
case 'm':
flags &= ~FLAG_MTIME_SENT;
flags |= FLAG_MTIME_RECV;
break;
case 'p':
passfile = optarg;
break;
default:
return CMD_USAGE;
}
argc -= optind;
argv += optind;
switch (argc) {
case 1:
backup = argv[0];
outdir = ".";
break;
case 2:
backup = argv[0];
outdir = argv[1];
if (mkdir(outdir, 0777) == -1 && errno != EEXIST) {
warn("mkdir: %s", outdir);
return CMD_ERROR;
}
break;
default:
return CMD_USAGE;
}
if (unveil(backup, "r") == -1)
err(1, "unveil: %s", backup);
if (unveil(outdir, "rwc") == -1)
err(1, "unveil: %s", outdir);
/* For SQLite */
if (unveil("/dev/urandom", "r") == -1)
err(1, "unveil: /dev/urandom");
/* For SQLite */
if (unveil("/tmp", "rwc") == -1)
err(1, "unveil: /tmp");
if (passfile == NULL) {
if (pledge("stdio rpath wpath cpath fattr tty", NULL) == -1)
err(1, "pledge");
} else {
if (strcmp(passfile, "-") != 0 && unveil(passfile, "r") == -1)
err(1, "unveil: %s", passfile);
if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
err(1, "pledge");
}
if ((ctx = sbk_ctx_new()) == NULL)
return CMD_ERROR;
if (get_passphrase(passfile, passphr, sizeof passphr) == -1) {
sbk_ctx_free(ctx);
return CMD_ERROR;
}
if (sbk_open(ctx, backup, passphr) == -1) {
explicit_bzero(passphr, sizeof passphr);
sbk_ctx_free(ctx);
return CMD_ERROR;
}
explicit_bzero(passphr, sizeof passphr);
if (flags & FLAG_MTIME_MASK) {
if (pledge("stdio rpath wpath cpath fattr", NULL) == -1)
err(1, "pledge");
} else {
if (pledge("stdio rpath wpath cpath", NULL) == -1)
err(1, "pledge");
}
ret = export_attachments(ctx, outdir, flags);
sbk_close(ctx);
sbk_ctx_free(ctx);
return (ret == -1) ? CMD_ERROR : CMD_OK;
}