2 * Send a compressed CTM delta to a recipient mailing list by encoding it
3 * in safe ASCII characters, in mailer-friendly chunks, and passing them
4 * to sendmail. Optionally, the chunks can be queued to be sent later by
5 * ctm_dequeue in controlled bursts. The encoding is almost the same as
6 * MIME BASE64, and is protected by a simple checksum.
8 * Author: Stephen McKay
10 * NOTICE: This is free software. I hope you get some use from this program.
11 * In return you should think about all the nice people who give away software.
12 * Maybe you should write some free software too.
14 * $FreeBSD: src/usr.sbin/ctm/ctm_smail/ctm_smail.c,v 1.12 1999/08/28 01:16:02 peter Exp $
22 #include <sys/types.h>
30 #define DEF_MAX_MSG 64000 /* Default maximum mail msg minus headers. */
32 #define LINE_LENGTH 76 /* Chars per encoded line. Divisible by 4. */
34 int chop_and_send_or_queue(FILE *dfp, char *delta, off_t ctm_size,
35 long max_msg_size, char *mail_alias, char *queue_dir);
36 int chop_and_send(FILE *dfp, char *delta, long msg_size, int npieces,
38 int chop_and_queue(FILE *dfp, char *delta, long msg_size, int npieces,
39 char *mail_alias, char *queue_dir);
40 void clean_up_queue(char *queue_dir);
41 int encode_body(FILE *sm_fp, FILE *delta_fp, long msg_size, unsigned *sum);
42 void write_header(FILE *sfp, char *mail_alias, char *delta, int pce,
44 void write_trailer(FILE *sfp, unsigned sum);
45 int apologise(char *delta, off_t ctm_size, long max_ctm_size,
47 FILE *open_sendmail(void);
48 int close_sendmail(FILE *fp);
51 main(int argc, char **argv)
56 long max_msg_size = DEF_MAX_MSG;
57 long max_ctm_size = 0;
58 char *log_file = NULL;
59 char *queue_dir = NULL;
64 err_prog_name(argv[0]);
66 OPTIONS("[-l log] [-m maxmsgsize] [-c maxctmsize] [-q queuedir] ctm-delta mail-alias")
67 NUMBER('m', max_msg_size)
68 NUMBER('c', max_ctm_size)
70 STRING('q', queue_dir)
77 err_set_log(log_file);
82 if ((delta = strrchr(delta_file, '/')) == NULL)
87 if ((dfp = fopen(delta_file, "r")) == NULL || fstat(fileno(dfp), &sb) < 0)
89 err("*%s", delta_file);
93 if (max_ctm_size != 0 && sb.st_size > max_ctm_size)
94 status = apologise(delta, sb.st_size, max_ctm_size, mail_alias);
96 status = chop_and_send_or_queue(dfp, delta, sb.st_size, max_msg_size,
97 mail_alias, queue_dir);
106 * Carve our CTM delta into pieces, encode them, and send or queue them.
107 * Returns 0 on success, and 1 on failure.
110 chop_and_send_or_queue(FILE *dfp, char *delta, off_t ctm_size,
111 long max_msg_size, char *mail_alias, char *queue_dir)
119 #define howmany(x,y) (((x)+((y)-1)) / (y))
122 * Work out how many pieces we need, bearing in mind that each piece
123 * grows by 4/3 when encoded. We count the newlines too, but ignore
124 * all mail headers and piece headers. They are a "small" (almost
125 * constant) per message overhead that we make the user worry about. :-)
127 exp_size = ctm_size * 4 / 3;
128 exp_size += howmany(exp_size, LINE_LENGTH);
129 npieces = howmany(exp_size, max_msg_size);
130 msg_size = howmany(ctm_size, npieces);
134 if (queue_dir == NULL)
135 status = chop_and_send(dfp, delta, msg_size, npieces, mail_alias);
138 status = chop_and_queue(dfp, delta, msg_size, npieces, mail_alias,
141 clean_up_queue(queue_dir);
149 * Carve our CTM delta into pieces, encode them, and send them.
150 * Returns 0 on success, and 1 on failure.
153 chop_and_send(FILE *dfp, char *delta, long msg_size, int npieces,
161 * Send each chunk directly to sendmail as it is generated.
162 * No temporary files necessary. If things turn ugly, we just
163 * have to live with the fact the we have sent only part of
166 for (pce = 1; pce <= npieces; pce++)
170 if ((sfp = open_sendmail()) == NULL)
173 write_header(sfp, mail_alias, delta, pce, npieces);
174 read_error = encode_body(sfp, dfp, msg_size, &sum);
176 write_trailer(sfp, sum);
178 if (!close_sendmail(sfp) || read_error)
181 err("%s %d/%d sent to %s", delta, pce, npieces, mail_alias);
189 * Construct the tmp queue file name of a delta piece.
191 #define mk_tmp_name(fn,qd,p) \
192 sprintf((fn), "%s/.%08ld.%03d", (qd), (long)getpid(), (p))
195 * Construct the final queue file name of a delta piece.
197 #define mk_queue_name(fn,qd,d,p,n) \
198 sprintf((fn), "%s/%s+%03d-%03d", (qd), (d), (p), (n))
201 * Carve our CTM delta into pieces, encode them, and queue them.
202 * Returns 0 on success, and 1 on failure.
205 chop_and_queue(FILE *dfp, char *delta, long msg_size, int npieces,
206 char *mail_alias, char *queue_dir)
211 char tname[PATH_MAX];
212 char qname[PATH_MAX];
215 * Store each piece in the queue directory, but under temporary names,
216 * so that they can be deleted without unpleasant consequences if
217 * anything goes wrong. We could easily fill up a disk, for example.
219 for (pce = 1; pce <= npieces; pce++)
223 mk_tmp_name(tname, queue_dir, pce);
224 if ((qfp = fopen(tname, "w")) == NULL)
226 err("cannot open '%s' for writing", tname);
230 write_header(qfp, mail_alias, delta, pce, npieces);
231 if (encode_body(qfp, dfp, msg_size, &sum))
233 write_trailer(qfp, sum);
236 write_error = ferror(qfp);
240 err("error writing '%s'", tname);
245 * Give the warm success message now, instead of all in a rush
246 * during the rename phase.
248 err("%s %d/%d queued for %s", delta, pce, npieces, mail_alias);
252 * Rename the pieces into place. If an error occurs now, we are
253 * stuffed, but there is no neat way to back out. rename() should
254 * only fail now under extreme circumstances.
256 for (pce = 1; pce <= npieces; pce++)
258 mk_tmp_name(tname, queue_dir, pce);
259 mk_queue_name(qname, queue_dir, delta, pce, npieces);
260 if (rename(tname, qname) < 0)
262 err("*rename: '%s' to '%s'", tname, qname);
272 * There may be temporary files cluttering up the queue directory.
275 clean_up_queue(char *queue_dir)
278 char tname[PATH_MAX];
280 err("discarding queued delta pieces");
281 for (pce = 1; ; pce++)
283 mk_tmp_name(tname, queue_dir, pce);
284 if (unlink(tname) < 0)
291 * MIME BASE64 encode table.
293 static char to_b64[0x40] =
294 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
297 * This cheap plastic checksum effectively rotates our checksum-so-far
298 * left one, then adds the character. We only want 16 bits of it, and
299 * don't care what happens to the rest. It ain't much, but it's small.
301 #define add_ck(sum,x) \
302 ((sum) += ((x)&0xff) + (sum) + (((sum)&0x8000) ? 1 : 0))
305 * Encode the body. Use an encoding almost the same as MIME BASE64.
307 * Characters are read from delta_fp and encoded characters are written
308 * to sm_fp. At most 'msg_size' characters should be read from delta_fp.
310 * The body consists of lines of up to LINE_LENGTH characters. Each group
311 * of 4 characters encodes 3 input characters. Each output character encodes
312 * 6 bits. Thus 64 different characters are needed in this representation.
315 encode_body(FILE *sm_fp, FILE *delta_fp, long msg_size, unsigned *sum)
317 unsigned short cksum = 0xffff;
321 unsigned char inbuf[LINE_LENGTH*3/4];
322 char outbuf[LINE_LENGTH+1];
325 * Round up to the nearest line boundary, for the tiniest of gains,
326 * and lots of neatness. :-)
328 msg_size += (LINE_LENGTH*3/4) - 1;
329 msg_size -= msg_size % (LINE_LENGTH*3/4);
333 want = (msg_size < sizeof(inbuf)) ? msg_size : sizeof(inbuf);
334 if ((n = fread(inbuf, sizeof(char), want, delta_fp)) == 0)
338 for (i = 0; i < n; i++)
339 add_ck(cksum, inbuf[i]);
342 * Produce a line of encoded data. Every line length will be a
343 * multiple of 4, except for, perhaps, the last line.
349 *op++ = to_b64[ip[0] >> 2];
350 *op++ = to_b64[(ip[0] << 4 & 0x3f) | ip[1] >> 4];
351 *op++ = to_b64[(ip[1] << 2 & 0x3f) | ip[2] >> 6];
352 *op++ = to_b64[ip[2] & 0x3f];
358 *op++ = to_b64[ip[0] >> 2];
359 *op++ = to_b64[(ip[0] << 4 & 0x3f) | ip[1] >> 4];
361 *op++ = to_b64[ip[1] << 2 & 0x3f];
364 fwrite(outbuf, sizeof(char), op - outbuf, sm_fp);
367 if (ferror(delta_fp))
369 err("error reading input file.");
380 * Write the mail header and data header.
383 write_header(FILE *sfp, char *mail_alias, char *delta, int pce, int npieces)
385 fprintf(sfp, "From: owner-%s\n", mail_alias);
386 fprintf(sfp, "To: %s\n", mail_alias);
387 fprintf(sfp, "Subject: ctm-mail %s %d/%d\n\n", delta, pce, npieces);
389 fprintf(sfp, "CTM_MAIL BEGIN %s %d %d\n", delta, pce, npieces);
394 * Write the data trailer.
397 write_trailer(FILE *sfp, unsigned sum)
399 fprintf(sfp, "CTM_MAIL END %ld\n", (long)sum);
404 * We're terribly sorry, but the delta is too big to send.
405 * Returns 0 on success, 1 on failure.
408 apologise(char *delta, off_t ctm_size, long max_ctm_size, char *mail_alias)
412 sfp = open_sendmail();
416 fprintf(sfp, "From: owner-%s\n", mail_alias);
417 fprintf(sfp, "To: %s\n", mail_alias);
418 fprintf(sfp, "Subject: ctm-notice %s\n\n", delta);
420 fprintf(sfp, "%s is %ld bytes. The limit is %ld bytes.\n\n", delta,
421 (long)ctm_size, max_ctm_size);
422 fprintf(sfp, "You can retrieve this delta via ftpmail, "
423 "or your good mate at the university.\n");
425 if (!close_sendmail(sfp))
433 * Start a pipe to sendmail. Sendmail will decode the destination
434 * from the message contents.
442 sprintf(buf, "%s -odq -t", _PATH_SENDMAIL);
443 if ((fp = popen(buf, "w")) == NULL)
444 err("cannot start sendmail");
450 * Close a pipe to sendmail. Sendmail will then do its bit.
451 * Return 1 on success, 0 on failure.
454 close_sendmail(FILE *fp)
461 err("error writing to sendmail");
465 if ((status = pclose(fp)) != 0)
466 err("sendmail failed with status %d", status);
468 return (status == 0);