summaryrefslogtreecommitdiffstats
path: root/usr.sbin/ctm/ctm_rmail/ctm_rmail.c
blob: a46a58aa87348ef9d178fb82627d9481dd4dfc86 (plain)
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
 * Accept one (or more) ASCII encoded chunks that together make a compressed
 * CTM delta.  Decode them and reconstruct the deltas.  Any completed
 * deltas may be passed to ctm for unpacking.
 *
 * Author: Stephen McKay
 *
 * NOTICE: This is free software.  I hope you get some use from this program.
 * In return you should think about all the nice people who give away software.
 * Maybe you should write some free software too.
 *
 * $FreeBSD$
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include "error.h"
#include "options.h"

#define CTM_STATUS	".ctm_status"

char *piece_dir = NULL;		/* Where to store pieces of deltas. */
char *delta_dir = NULL;		/* Where to store completed deltas. */
char *base_dir = NULL;		/* The tree to apply deltas to. */
int delete_after = 0;		/* Delete deltas after ctm applies them. */
int apply_verbose = 0;		/* Run with '-v' */
int set_time = 0;		/* Set the time of the files that is changed. */
int mask = 0;			/* The current umask */

void apply_complete(void);
int read_piece(char *input_file);
int combine_if_complete(char *delta, int pce, int npieces);
int combine(char *delta, int npieces, char *dname, char *pname, char *tname);
int decode_line(char *line, char *out_buf);
int lock_file(char *name);

/*
 * If given a '-p' flag, read encoded delta pieces from stdin or file
 * arguments, decode them and assemble any completed deltas.  If given
 * a '-b' flag, pass any completed deltas to 'ctm' for application to
 * the source tree.  The '-d' flag is mandatory, but either of '-p' or
 * '-b' can be omitted.  If given the '-l' flag, notes and errors will
 * be timestamped and written to the given file.
 *
 * Exit status is 0 for success or 1 for indigestible input.  That is,
 * 0 means the encode input pieces were decoded and stored, and 1 means
 * some input was discarded.  If a delta fails to apply, this won't be
 * reflected in the exit status.  In this case, the delta is left in
 * 'deltadir'.
 */
int
main(int argc, char **argv)
    {
    char *log_file = NULL;
    int status = 0;
    int fork_ctm = 0;

    mask = umask(0);
    umask(mask);

    err_prog_name(argv[0]);

    OPTIONS("[-Dfuv] [-p piecedir] [-d deltadir] [-b basedir] [-l log] [file ...]")
	FLAG('D', delete_after)
	FLAG('f', fork_ctm)
	FLAG('u', set_time)
	FLAG('v', apply_verbose)
	STRING('p', piece_dir)
	STRING('d', delta_dir)
	STRING('b', base_dir)
	STRING('l', log_file)
    ENDOPTS

    if (delta_dir == NULL)
	usage();

    if (piece_dir == NULL && (base_dir == NULL || argc > 1))
	usage();

    if (log_file != NULL)
	err_set_log(log_file);

    /*
     * Digest each file in turn, or just stdin if no files were given.
     */
    if (argc <= 1)
	{
	if (piece_dir != NULL)
	    status = read_piece(NULL);
	}
    else
	{
	while (*++argv != NULL)
	    status |= read_piece(*argv);
	}

    /*
     * Maybe it's time to look for and apply completed deltas with ctm.
     *
     * Shall we report back to sendmail immediately, and let a child do
     * the work?  Sendmail will be waiting for us to complete, delaying
     * other mail, and possibly some intermediate process (like MH slocal)
     * will terminate us if we take too long!
     *
     * If fork() fails, it's unlikely we'll be able to run ctm, so give up.
     * Also, the child exit status is unimportant.
     */
    if (base_dir != NULL)
	if (!fork_ctm || fork() == 0)
	    apply_complete();

    return status;
    }


/*
 * Construct the file name of a piece of a delta.
 */
#define mk_piece_name(fn,d,p,n)	\
    sprintf((fn), "%s/%s+%03d-%03d", piece_dir, (d), (p), (n))

/*
 * Construct the file name of an assembled delta.
 */
#define mk_delta_name(fn,d)	\
    sprintf((fn), "%s/%s", delta_dir, (d))

/*
 * If the next required delta is now present, let ctm lunch on it and any
 * contiguous deltas.
 */
void
apply_complete()
    {
    int i, dn;
    int lfd;
    FILE *fp, *ctm;
    struct stat sb;
    char class[20];
    char delta[30];
    char junk[2];
    char fname[PATH_MAX];
    char here[PATH_MAX];
    char buf[PATH_MAX*2];

    /*
     * Grab a lock on the ctm mutex file so that we can be sure we are
     * working alone, not fighting another ctm_rmail!
     */
    strcpy(fname, delta_dir);
    strcat(fname, "/.mutex_apply");
    if ((lfd = lock_file(fname)) < 0)
	return;

    /*
     * Find out which delta ctm needs next.
     */
    sprintf(fname, "%s/%s", base_dir, CTM_STATUS);
    if ((fp = fopen(fname, "r")) == NULL)
	{
	close(lfd);
	return;
	}

    i = fscanf(fp, "%19s %d %c", class, &dn, junk);
    fclose(fp);
    if (i != 2)
	{
	close(lfd);
	return;
	}

    /*
     * We might need to convert the delta filename to an absolute pathname.
     */
    here[0] = '\0';
    if (delta_dir[0] != '/')
	{
	getcwd(here, sizeof(here)-1);
	i = strlen(here) - 1;
	if (i >= 0 && here[i] != '/')
	    {
	    here[++i] = '/';
	    here[++i] = '\0';
	    }
	}

    /*
     * Keep applying deltas until we run out or something bad happens.
     */
    for (;;)
	{
	sprintf(delta, "%s.%04d.gz", class, ++dn);
	mk_delta_name(fname, delta);

	if (stat(fname, &sb) < 0)
	    break;

	sprintf(buf, "(cd %s && ctm %s%s%s%s) 2>&1", base_dir,
				set_time ? "-u " : "",
				apply_verbose ? "-v " : "", here, fname);
	if ((ctm = popen(buf, "r")) == NULL)
	    {
	    err("ctm failed to apply %s", delta);
	    break;
	    }

	while (fgets(buf, sizeof(buf), ctm) != NULL)
	    {
	    i = strlen(buf) - 1;
	    if (i >= 0 && buf[i] == '\n')
		buf[i] = '\0';
	    err("ctm: %s", buf);
	    }

	if (pclose(ctm) != 0)
	    {
	    err("ctm failed to apply %s", delta);
	    break;
	    }

	if (delete_after)
	    unlink(fname);

	err("%s applied%s", delta, delete_after ? " and deleted" : "");
	}

    /*
     * Closing the lock file clears the lock.
     */
    close(lfd);
    }


/*
 * This cheap plastic checksum effectively rotates our checksum-so-far
 * left one, then adds the character.  We only want 16 bits of it, and
 * don't care what happens to the rest.  It ain't much, but it's small.
 */
#define add_ck(sum,x)	\
    ((sum) += ((x)&0xff) + (sum) + (((sum)&0x8000) ? 1 : 0))


/*
 * Decode the data between BEGIN and END, and stash it in the staging area.
 * Multiple pieces can be present in a single file, bracketed by BEGIN/END.
 * If we have all pieces of a delta, combine them.  Returns 0 on success,
 * and 1 for any sort of failure.
 */
int
read_piece(char *input_file)
    {
    int status = 0;
    FILE *ifp, *ofp = 0;
    int decoding = 0;
    int got_one = 0;
    int line_no = 0;
    int i, n;
    int pce, npieces;
    unsigned claimed_cksum;
    unsigned short cksum = 0;
    char out_buf[200];
    char line[200];
    char delta[30];
    char pname[PATH_MAX];
    char tname[PATH_MAX];
    char junk[2];

    ifp = stdin;
    if (input_file != NULL && (ifp = fopen(input_file, "r")) == NULL)
	{
	err("cannot open '%s' for reading", input_file);
	return 1;
	}

    while (fgets(line, sizeof(line), ifp) != NULL)
	{
	line_no++;

	/*
	 * Remove all trailing white space.
	 */
	i = strlen(line) - 1;
	while (i > 0 && isspace(line[i]))
		line[i--] = '\0';

	/*
	 * Look for the beginning of an encoded piece.
	 */
	if (!decoding)
	    {
	    char *s;
	    int fd = -1;

	    if (sscanf(line, "CTM_MAIL BEGIN %29s %d %d %c",
		    delta, &pce, &npieces, junk) != 3)
		continue;

	    while ((s = strchr(delta, '/')) != NULL)
		*s = '_';

	    got_one++;
	    strcpy(tname, piece_dir);
	    strcat(tname, "/p.XXXXXXXXXX");
	    if ((fd = mkstemp(tname)) == -1 ||
		(ofp = fdopen(fd, "w")) == NULL)
		{
		if (fd != -1) {
		    err("cannot open '%s' for writing", tname);
		    close(fd);
		    }
		else
		    err("*mkstemp: '%s'", tname);
		status++;
		continue;
		}

	    cksum = 0xffff;
	    decoding++;
	    continue;
	    }

	/*
	 * We are decoding.  Stop if we see the end flag.
	 */
	if (sscanf(line, "CTM_MAIL END %d %c", &claimed_cksum, junk) == 1)
	    {
	    int e;

	    decoding = 0;

	    fflush(ofp);
	    e = ferror(ofp);
	    fclose(ofp);

	    if (e)
		err("error writing %s", tname);

	    if (cksum != claimed_cksum)
		err("checksum: read %d, calculated %d", claimed_cksum, cksum);

	    if (e || cksum != claimed_cksum)
		{
		err("%s %d/%d discarded", delta, pce, npieces);
		unlink(tname);
		status++;
		continue;
		}

	    mk_piece_name(pname, delta, pce, npieces);
	    if (rename(tname, pname) < 0)
		{
		err("*rename: '%s' to '%s'", tname, pname);
		err("%s %d/%d lost!", delta, pce, npieces);
		unlink(tname);
		status++;
		continue;
		}

	    err("%s %d/%d stored", delta, pce, npieces);

	    if (!combine_if_complete(delta, pce, npieces))
		status++;
	    continue;
	    }

	/*
	 * Must be a line of encoded data.  Decode it, sum it, and save it.
	 */
	n = decode_line(line, out_buf);
	if (n <= 0)
	    {
	    err("line %d: illegal character: '%c'", line_no, line[-n]);
	    err("%s %d/%d discarded", delta, pce, npieces);

	    fclose(ofp);
	    unlink(tname);

	    status++;
	    decoding = 0;
	    continue;
	    }

	for (i = 0; i < n; i++)
	    add_ck(cksum, out_buf[i]);

	fwrite(out_buf, sizeof(char), n, ofp);
	}

    if (decoding)
	{
	err("truncated file");
	err("%s %d/%d discarded", delta, pce, npieces);

	fclose(ofp);
	unlink(tname);

	status++;
	}

    if (ferror(ifp))
	{
	err("error reading %s", input_file == NULL ? "stdin" : input_file);
	status++;
	}

    if (input_file != NULL)
	fclose(ifp);

    if (!got_one)
	{
	err("message contains no delta");
	status++;
	}

    return (status != 0);
    }


/*
 * Put the pieces together to form a delta, if they are all present.
 * Returns 1 on success (even if we didn't do anything), and 0 on failure.
 */
int
combine_if_complete(char *delta, int pce, int npieces)
    {
    int i, e;
    int lfd;
    struct stat sb;
    char pname[PATH_MAX];
    char dname[PATH_MAX];
    char tname[PATH_MAX];

    /*
     * We can probably just rename() it into place if it is a small delta.
     */
    if (npieces == 1)
	{
	mk_delta_name(dname, delta);
	mk_piece_name(pname, delta, 1, 1);
	if (rename(pname, dname) == 0)
	    {
	    chmod(dname, 0666 & ~mask);
	    err("%s complete", delta);
	    return 1;
	    }
	}

    /*
     * Grab a lock on the reassembly mutex file so that we can be sure we are
     * working alone, not fighting another ctm_rmail!
     */
    strcpy(tname, delta_dir);
    strcat(tname, "/.mutex_build");
    if ((lfd = lock_file(tname)) < 0)
	return 0;

    /*
     * Are all of the pieces present?  Of course the current one is,
     * unless all pieces are missing because another ctm_rmail has
     * processed them already.
     */
    for (i = 1; i <= npieces; i++)
	{
	if (i == pce)
	    continue;
	mk_piece_name(pname, delta, i, npieces);
	if (stat(pname, &sb) < 0)
	    {
	    close(lfd);
	    return 1;
	    }
	}

    /*
     * Stick them together.  Let combine() use our file name buffers, since
     * we're such good buddies. :-)
     */
    e = combine(delta, npieces, dname, pname, tname);
    close(lfd);
    return e;
    }


/*
 * Put the pieces together to form a delta.
 * Returns 1 on success, and 0 on failure.
 * Note: dname, pname, and tname are room for some file names that just
 * happened to by lying around in the calling routine.  Waste not, want not!
 */
int
combine(char *delta, int npieces, char *dname, char *pname, char *tname)
    {
    FILE *dfp, *pfp;
    int i, n, e;
    char buf[BUFSIZ];
    int fd = -1;

    strcpy(tname, delta_dir);
    strcat(tname, "/d.XXXXXXXXXX");
    if ((fd = mkstemp(tname)) == -1 ||
	(dfp = fdopen(fd, "w")) == NULL)
	{
	if (fd != -1) {
	    close(fd);
	    err("cannot open '%s' for writing", tname);
	    }
	else
	    err("*mkstemp: '%s'", tname);
	return 0;
	}

    /*
     * Reconstruct the delta by reading each piece in order.
     */
    for (i = 1; i <= npieces; i++)
	{
	mk_piece_name(pname, delta, i, npieces);
	if ((pfp = fopen(pname, "r")) == NULL)
	    {
	    err("cannot open '%s' for reading", pname);
	    fclose(dfp);
	    unlink(tname);
	    return 0;
	    }
	while ((n = fread(buf, sizeof(char), sizeof(buf), pfp)) != 0)
	    fwrite(buf, sizeof(char), n, dfp);
	e = ferror(pfp);
	fclose(pfp);
	if (e)
	    {
	    err("error reading '%s'", pname);
	    fclose(dfp);
	    unlink(tname);
	    return 0;
	    }
	}
    fflush(dfp);
    e = ferror(dfp);
    fclose(dfp);
    if (e)
	{
	err("error writing '%s'", tname);
	unlink(tname);
	return 0;
	}

    mk_delta_name(dname, delta);
    if (rename(tname, dname) < 0)
	{
	err("*rename: '%s' to '%s'", tname, dname);
	unlink(tname);
	return 0;
	}
    chmod(dname, 0666 & ~mask);

    /*
     * Throw the pieces away.
     */
    for (i = 1; i <= npieces; i++)
	{
	mk_piece_name(pname, delta, i, npieces);
	if (unlink(pname) < 0)
	    err("*unlink: '%s'", pname);
	}

    err("%s complete", delta);
    return 1;
    }


/*
 * MIME BASE64 decode table.
 */
static unsigned char from_b64[0x80] =
    {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
    0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
    0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
    0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
    0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
    0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
    0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
    0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff
    };


/*
 * Decode a line of ASCII into binary.  Returns the number of bytes in
 * the output buffer, or < 0 on indigestable input.  Error output is
 * the negative of the index of the inedible character.
 */
int
decode_line(char *line, char *out_buf)
    {
    unsigned char *ip = (unsigned char *)line;
    unsigned char *op = (unsigned char *)out_buf;
    unsigned long bits;
    unsigned x;

    for (;;)
	{
	if (*ip >= 0x80 || (x = from_b64[*ip]) >= 0x40)
	    break;
	bits = x << 18;
	ip++;
	if (*ip < 0x80 && (x = from_b64[*ip]) < 0x40)
	    {
	    bits |= x << 12;
	    *op++ = bits >> 16;
	    ip++;
	    if (*ip < 0x80 && (x = from_b64[*ip]) < 0x40)
		{
		bits |= x << 6;
		*op++ = bits >> 8;
		ip++;
		if (*ip < 0x80 && (x = from_b64[*ip]) < 0x40)
		    {
		    bits |= x;
		    *op++ = bits;
		    ip++;
		    }
		}
	    }
	}

    if (*ip == '\0' || *ip == '\n')
	return op - (unsigned char *)out_buf;
    else
	return -(ip - (unsigned char *)line);
    }


/*
 * Create and lock the given file.
 *
 * Clearing the lock is as simple as closing the file descriptor we return.
 */
int
lock_file(char *name)
    {
    int lfd;

    if ((lfd = open(name, O_WRONLY|O_CREAT, 0600)) < 0)
	{
	err("*open: '%s'", name);
	return -1;
	}
    if (flock(lfd, LOCK_EX) < 0)
	{
	close(lfd);
	err("*flock: '%s'", name);
	return -1;
	}
    return lfd;
    }
OpenPOWER on IntegriCloud