File: car.cpp

package info (click to toggle)
ucommon 7.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,068 kB
  • sloc: cpp: 45,917; ansic: 714; sh: 226; makefile: 173
file content (535 lines) | stat: -rw-r--r-- 14,088 bytes parent folder | download | duplicates (3)
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
// Copyright (C) 2010-2014 David Sugar, Tycho Softworks.
// Copyright (C) 2015-2020 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.

#include <ucommon/secure.h>
#include <sys/stat.h>

using namespace ucommon;

static shell::flagopt helpflag('h',"--help",    _TEXT("display this list"));
static shell::flagopt althelp('?', NULL, NULL);
static shell::stringopt tag('t', "--tag", _TEXT("tag annotation"), "text", "");
static shell::stringopt algo('c', "--cipher", _TEXT("cipher method (aes256)"), "method", "aes256");
static shell::flagopt decode('d', "--decode", _TEXT("decode archive"));
static shell::stringopt hash('h', "--digest", _TEXT("digest method (sha)"), "method", "sha");
static shell::flagopt noheader('n', "--no-header", _TEXT("without wrapper"));
static shell::stringopt out('o', "--output", _TEXT("output file"), "filename", "-");
static shell::flagopt quiet('q', "--quiet", _TEXT("quiet operation"));
static shell::flagopt recursive('R', "--recursive", _TEXT("recursive directory scan"));
static shell::flagopt altrecursive('r', NULL, NULL);
static shell::flagopt hidden('s', "--hidden", _TEXT("include hidden files"));
static shell::flagopt yes('y', "--overwrite", _TEXT("overwrite existing files"));

static bool binary = false;
static int exit_code = 0;
static const char *argv0 = "car";
static uint8_t frame[48], cbuf[48];
static cipher_t cipher;
static FILE *output = stdout;
static enum {d_text, d_file, d_scan, d_init} decoder = d_init;
static unsigned frames;

static void report(const char *path, int code)
{
    const char *err = _TEXT("i/o error");

    switch(code) {
    case EACCES:
    case EPERM:
        err = _TEXT("permission denied");
        break;
    case EROFS:
        err = _TEXT("read-only file system");
        break;
    case ENODEV:
    case ENOENT:
        err = _TEXT("no such file or directory");
        break;
    case ENOTDIR:
        err = _TEXT("not a directory");
        break;
    case ENOTEMPTY:
        err = _TEXT("directory not empty");
        break;
    case ENOSPC:
        err = _TEXT("no space left on device");
        break;
    case EBADF:
    case ENAMETOOLONG:
        err = _TEXT("bad file path");
        break;
    case EBUSY:
    case EINPROGRESS:
        err = _TEXT("file or directory busy");
        break;
    case EINTR:
        err = _TEXT("operation interrupted");
        break;
    case EISDIR:
        err = _TEXT("is a directory");
        break;
#ifdef  ELOOP
    case ELOOP:
        err = _TEXT("too many sym links");
        break;
#endif
    }

    if(path)
        shell::printf("%s: %s: %s\n", argv0, path, err);
    else
        shell::errexit(1, "*** %s: %s\n", argv0, err);

    exit_code = 1;
}

static bool encode(const char *path, FILE *fp, size_t offset = 0)
{
    memset(frame, 0, sizeof(frame));

    size_t count = fread(frame + offset, 1, sizeof(frame) - offset, fp);
    char buffer[128];

    if(ferror(fp)) {
        report(path, errno);
        return false;
    }

    if(count)
        count += offset;

    // add padd value for last frame...
    if(count < sizeof(frame))
        frame[sizeof(frame) - 1] = (char)(count - offset);

    // cipher it...
    size_t encoded = cipher.put(frame, sizeof(frame));
    if(encoded != sizeof(frame)) {
        report(path, EINTR);
        return false;
    }

    if(binary)
        fwrite(cbuf, sizeof(cbuf), 1, output);
    else {
        String::b64encode(buffer, cbuf, 48);
        fprintf(output, "%s\n", buffer);
    }

    // return status
    if(count == sizeof(frame))
        return true;

    return false;
}

static void encodestream(void)
{

    size_t offset = 6;

    if(fsys::is_tty(shell::input()))
        fputs("car: type your message\n", stderr);

    memset(frame, 0, sizeof(frame));

    for(;;) {
        if(!encode("-", stdin, offset))
            break;
        offset = 0;
    }

    if(!binary && !is(noheader))
        fprintf(output, "-----END CAR STREAM-----\n");
}

static void header(void)
{
    binary = true;

    memset(frame, 0, sizeof(frame));
    String::set((char *)frame, 6, ".car");
    frame[5] = 1;
    frame[4] = 0xff;
    String::set((char *)frame + 6, sizeof(frame) - 6, *tag);
    fwrite(frame, sizeof(frame), 1, output);
}

static void encodefile(const char *path, const char *name)
{
    char buffer[128];

    fsys::fileinfo_t ino;

    fsys::info(path, &ino);

    FILE *fp = fopen(path, "r");
    if(!fp) {
        report(name, errno);
        return;
    }

    lsb_setlong(frame, ino.st_size);
    frame[4] = 1;
    frame[5] = 0;
    String::set((char *)(frame + 6), sizeof(frame) - 6, name);

    cipher.put(frame, sizeof(frame));

    if(binary)
        fwrite(cbuf, sizeof(cbuf), 1, output);
    else {
        String::b64encode(buffer, cbuf, 48);
        fprintf(output, "%s\n", buffer);
    }

    for(;;) {
        if(!encode(name, fp))
            break;
    }
    fclose(fp);
}

static void final(void)
{
    size_t size;

    switch(decoder) {
    case d_init:
        return;
    case d_scan:
        cipher.put(frame, sizeof(frame));
        if(cbuf[4] > 0)
            return;
        size = cbuf[sizeof(frame) - 1];
        if(size)
            fwrite(cbuf + 6, size, 1, output);
        break;
    default:
        decoder = d_scan;
        cipher.put(frame, sizeof(frame));
        size = cbuf[sizeof(frame) - 1];
        if(size)
            fwrite(cbuf, size, 1, output);
    }
}

static void process(void)
{
    string_t path;
    int key;
    char *cp;

    switch(decoder) {
    case d_init:
        decoder = d_scan;
        return;
    case d_scan:
        cipher.put(frame, sizeof(frame));
        if(cbuf[4] == 0xff) // header...
            break;
        if(!cbuf[4]) {      // if message
            fwrite(cbuf + 6, sizeof(frame) - 6, 1, output);
            decoder = d_text;
            break;
        }
        decoder = d_file;
        frames = lsb_getlong(cbuf) / sizeof(frame);
        path = str((char *)(cbuf + 6));
        cp = strrchr((char *)(cbuf + 6), '/');
        if(cp) {
            *cp = 0;
            dir::create((char *)(cbuf + 6), 0640);
        }
        if(fsys::is_dir(*path))
            shell::errexit(8, "*** %s: %s: %s\n",
                argv0, *path, _TEXT("output is directory"));

        if(fsys::is_file(*path) && !is(yes)) {
            string_t prompt = str("overwrite ") + path + " <y/n>? ";
            if(is(quiet))
                key = 0;
            else
                key = shell::inkey(prompt);
            switch(key)
            {
            case 'y':
            case 'Y':
                printf("y\n");
                break;
            default:
                printf("n\n");
            case 0:
                shell::errexit(8, "*** %s: %s: %s\n",
                    argv0, *path, _TEXT("will not overwrite"));
            }
        }

        output = fopen(*path, "w");
        if(!output)
            shell::errexit(8, "*** %s: %s: %s\n",
                argv0, *path, _TEXT("cannot create"));
        if(!is(quiet))
            printf("decoding %s...\n", *path);
        break;
    case d_file:
        if(!frames) {
            final();
            break;
        }
        --frames;
    default:
        cipher.put(frame, sizeof(frame));
        fwrite(cbuf, sizeof(frame), 1, output);
    }
}

static void binarydecode(FILE *fp, const char *path)
{
    char buffer[48];

    memset(frame, 0, sizeof(frame));
    memset(buffer, 0, sizeof(buffer));
    if(fread(frame, sizeof(frame), 1, fp) < 1)
        shell::errexit(6, "*** %s: %s: %s\n",
            argv0, path, _TEXT("cannot read archive"));

    if(!eq((char *)frame, ".car", 4) || (frame[4] != 0xff))
        shell::errexit(6, "*** %s: %s: %s\n",
            argv0, path, _TEXT("not a cryptographic archive"));

    for(;;) {
        if(feof(fp)) {
            final();
            return;
        }

        if(ferror(fp)) {
            exit_code = errno;
            report(path, exit_code);
            return;
        }

        if(fread(buffer, sizeof(buffer), 1, fp) < 1)
            continue;
        process();
        memcpy(frame, buffer, sizeof(frame));
    }
}

static void streamdecode(FILE *fp, const char *path)
{
    char buffer[128];
    for(;;) {
        if(NULL == fgets(buffer, sizeof(buffer), fp) || feof(fp)) {
            shell::errexit(5, "*** %s: %s: %s\n",
                argv0, path, _TEXT("no archive found"));
        }
        if(ferror(fp)) {
            exit_code = errno;
            report(path, exit_code);
            return;
        }
        if(eq("-----BEGIN CAR STREAM-----\n", buffer))
            break;
    }
    for(;;) {
        if(feof(fp)) {
            final();
            return;
        }

        if(ferror(fp)) {
            exit_code = errno;
            report(path, exit_code);
            return;
        }

        if(fgets(buffer, sizeof(buffer), fp) == NULL)
            continue;

        if(eq("-----END CAR STREAM-----\n", buffer)) {
            final();
            return;
        }

        // ignore extra headers...
        if(strstr(buffer, ": "))
            continue;

        process();
        unsigned len = String::b64decode(frame, buffer, 48);
        if(len < 48) {
            report(path, EINTR);
            return;
        }
    }
}

static void scan(string_t path, string_t prefix)
{
    char filename[128];
    string_t filepath;
    string_t name;
    string_t subdir;
    dir_t dir(path);

    while(is(dir) && dir.read(filename, sizeof(filename))) {
        if(*filename == '.' && (filename[1] == '.' || !filename[1]))
            continue;

        if(*filename == '.' && !is(hidden))
            continue;

        filepath = str(path) + str("/") + str(filename);
        if(prefix[0])
            name ^= prefix + str("/") + str(filename);
        else
            name ^= str(filename);

        if(fsys::is_dir(filepath)) {
            if(is(recursive) || is(altrecursive))
                scan(filepath, name);
            else
                report(*filepath, EISDIR);
        }
        else
            encodefile(*filepath, *name);
    }
}

int main(int argc, char **argv)
{
    shell::bind("car");
    shell args(argc, argv);
    argv0 = args.argv0();
    unsigned count = 0;
    char passphrase[256];
    char confirm[256];
    const char *ext;

    argv0 = args.argv0();

    if(is(helpflag) || is(althelp)) {
        printf("%s\n", _TEXT("Usage: car [options] path..."));
        printf("%s\n\n", _TEXT("Crytographic archiver"));
        printf("%s\n", _TEXT("Options:"));
        shell::help();
        printf("\n%s\n", _TEXT("Report bugs to dyfet@gnu.org"));
        return 0;
    }

    if(!secure::init())
        shell::errexit(1, "*** %s: %s\n", argv0, _TEXT("not supported"));

    if(!Digest::has(*hash))
        shell::errexit(2, "*** %s: %s: %s\n",
            argv0, *hash, _TEXT("unknown or unsupported digest method"));

    if(!Cipher::has(*algo))
        shell::errexit(2, "*** %s: %s: %s\n",
            argv0, *algo, _TEXT("unknown or unsupported cipher method"));

    shell::getpass("passphrase: ", passphrase, sizeof(passphrase));
    shell::getpass("confirm: ", confirm, sizeof(confirm));

    if(!eq(passphrase, confirm))
        shell::errexit(3, "*** %s: %s\n",
            argv0, _TEXT("passphrase does not match confirmation"));

    // set key and cleanup buffers...
    skey_t key(*algo, *hash, passphrase);
    memset(passphrase, 0, sizeof(passphrase));
    memset(confirm, 0, sizeof(confirm));
    memset(cbuf, 0, sizeof(cbuf));

    if(is(decode))
        cipher.set(&key, Cipher::DECRYPT, cbuf, sizeof(cbuf));
    else
        cipher.set(&key, Cipher::ENCRYPT, cbuf, sizeof(cbuf));

    if(is(decode) && !args()) {
        streamdecode(stdin, "-");
        goto end;
    }

    if(is(decode) && args() > 1)
        shell::errexit(3, "*** %s: %s\n", argv0, _TEXT("specify only one file for decoder"));

    if(is(decode)) {
        FILE *fp = fopen(args[0], "r");
        if(!fp)
            shell::errexit(7, "*** %s: %s: %s\n",
                argv0, args[0], _TEXT("cannot open or access"));

        const char *ext = strrchr(args[0], '.');
        if(eq_case(ext, ".car"))
            binarydecode(fp, args[0]);
        else
            streamdecode(fp, args[0]);
        fclose(fp);
        goto end;
    }

    if(!eq(*out, "-")) {
        output = fopen(*out, "w");
        if(!output) {
            shell::errexit(4, "*** %s: %s: %s\n",
                argv0, *out, _TEXT("cannot create"));
        }
    }

    // if we are outputting to a car file, do it in binary
    ext = strrchr(*out, '.');
    if(eq_case(ext, ".car"))
        header();

    if(!binary && !is(noheader)) {
        fprintf(output, "-----BEGIN CAR STREAM-----\n");
        if(tag)
            fprintf(output, "Tag: %s\n", *tag);
    }

    if(!args()) {
        encodestream();
        goto end;
    }

    while(count < args()) {
        if(fsys::is_dir(args[count]))
            scan(str(args[count++]), "");
        else {
            const char *cp = args[count++];
            const char *ep = strrchr(cp, '/');
            if(!ep)
                ep = strrchr(cp, '\\');
            if(ep)
                ++ep;
            else
                ep = cp;
            encodefile(cp, ep);
        }
    }

    if(!binary && !is(noheader))
        fprintf(output, "-----END CAR STREAM-----\n");

end:
    return exit_code;
}