3 * dropsbr.c -- create/read/manipulate mail drops
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
16 # include <h/dropsbr.h>
22 # include "mmdfonly.h"
30 # include <netinet/in.h>
41 static int mbx_chk_mbox (int);
42 static int mbx_chk_mmdf (int);
43 static int map_open (char *, int);
47 * Main entry point to open/create and lock
52 mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
59 /* attempt to open and lock file */
60 for (count = 4; count > 0; count--) {
61 if ((fd = lkopen (file, O_RDWR | O_CREAT | O_NONBLOCK, mode)) == NOTOK) {
63 #if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
77 /* just return error */
82 /* good file descriptor */
89 * Return if we still failed after 4 attempts,
90 * or we just want to skip the sanity checks.
92 if (fd == NOTOK || mbx_style == OTHER_FORMAT)
96 * Do sanity checks on maildrop.
98 if (fstat (fd, &st) == NOTOK) {
100 * The stat failed. So we make sure file
101 * has right ownership/modes
103 chown (file, uid, gid);
105 } else if (st.st_size > (off_t) 0) {
108 /* check the maildrop */
112 status = mbx_chk_mmdf (fd);
116 status = mbx_chk_mbox (fd);
120 /* if error, attempt to close it */
121 if (status == NOTOK) {
132 * Check/prepare MBOX style maildrop for appending.
136 mbx_chk_mbox (int fd)
138 /* just seek to the end */
139 if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
147 * Check/prepare MMDF style maildrop for appending.
151 mbx_chk_mmdf (int fd)
156 count = strlen (mmdlm2);
158 /* casting -count to off_t, seem to break FreeBSD 2.2.6 */
159 if (lseek (fd, (long) (-count), SEEK_END) == (off_t) NOTOK)
161 if (read (fd, ldelim, count) != count)
166 if (strcmp (ldelim, mmdlm2)
167 && write (fd, "\n", 1) != 1
168 && write (fd, mmdlm2, count) != count)
176 mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
178 register int len, size;
179 register long ld1, ld2;
182 register struct drop *cp, *dp, *ep, *pp;
184 pp = (struct drop *) calloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
187 admonish (NULL, "unable to allocate drop storage");
191 ld1 = (long) strlen (mmdlm1);
192 ld2 = (long) strlen (mmdlm2);
194 fseek (fp, pos, SEEK_SET);
195 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
197 if (strcmp (buffer, mmdlm1) == 0)
198 pos += ld1, dp->d_start = (long) pos;
200 dp->d_start = (long)pos , pos += (long) strlen (buffer);
201 for (bp = buffer; *bp; bp++, size++)
206 while (fgets (buffer, sizeof(buffer), fp) != NULL)
207 if (strcmp (buffer, mmdlm2) == 0)
210 pos += (long) strlen (buffer);
211 for (bp = buffer; *bp; bp++, size++)
216 if (dp->d_start != (long) pos) {
218 dp->d_size = (long) size;
225 register int curlen = dp - pp;
227 cp = (struct drop *) realloc ((char *) pp,
228 (size_t) (len += MAXFOLDER) * sizeof(*pp));
231 admonish (NULL, "unable to allocate drop storage");
235 dp = cp + curlen, ep = (pp = cp) + len - 1;
248 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
249 long pos, off_t stop, int mapping, int noisy)
251 register int i, j, size;
257 off = (long) lseek (md, (off_t) 0, SEEK_CUR);
259 if (write (md, mmdlm1, j) != j)
261 start = lseek (md, (off_t) 0, SEEK_CUR);
264 fseek (fp, pos, SEEK_SET);
265 while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
267 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
269 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
271 if (write (md, buffer, i) != i)
275 for (cp = buffer; i-- > 0; size++)
280 stop = lseek (md, (off_t) 0, SEEK_CUR);
282 if (write (md, mmdlm2, j) != j)
285 map_write (mailbox, md, id, last, start, stop, off, size, noisy);
292 * Append message to end of file or maildrop.
296 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
297 int mapping, char *text, int noisy)
302 char *cp, buffer[BUFSIZ];
305 pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
312 if (write (md, mmdlm1, j) != j)
314 start = lseek (md, (off_t) 0, SEEK_CUR);
318 if (write (md, text, i) != i)
320 for (cp = text; *cp++; size++)
325 while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
327 (j = stringdex (mmdlm1, buffer)) >= 0;
331 (j = stringdex (mmdlm2, buffer)) >= 0;
334 if (write (md, buffer, i) != i)
337 for (cp = buffer; i-- > 0; size++)
342 stop = lseek (md, (off_t) 0, SEEK_CUR);
344 if (write (md, mmdlm2, j) != j)
347 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
349 return (i != NOTOK ? OK : NOTOK);
352 if ((j = dup (fd)) == NOTOK)
354 if ((fp = fdopen (j, "r")) == NULL) {
358 start = lseek (md, (off_t) 0, SEEK_CUR);
360 /* If text is given, we add it to top of message */
363 if (write (md, text, i) != i)
365 for (cp = text; *cp++; size++)
370 for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
373 * Check the first line, and make some changes.
375 if (j == 0 && !text) {
377 * Change the "Return-Path:" field (if in first line)
380 if (!strncmp (buffer, "Return-Path:", 12)) {
381 char tmpbuffer[BUFSIZ];
384 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
386 if (!(fp = strchr(ep + 1, ' ')))
387 fp = strchr(ep + 1, '\n');
388 tp = dctime(dlocaltimenow());
389 snprintf (buffer, sizeof(buffer), "From %.*s %s",
391 } else if (!strncmp (buffer, "X-Envelope-From:", 16)) {
393 * Change the "X-Envelope-From:" field
394 * (if first line) back to "From ".
396 char tmpbuffer[BUFSIZ];
399 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
401 snprintf (buffer, sizeof(buffer), "From %s", ep);
402 } else if (strncmp (buffer, "From ", 5)) {
404 * If there is already a "From " line,
405 * then leave it alone. Else we add one.
407 char tmpbuffer[BUFSIZ];
410 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
411 ep = "nobody@nowhere";
412 tp = dctime(dlocaltimenow());
413 snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
418 * If this is not first line, and begins with
419 * "From ", then prepend line with ">".
421 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
426 if (write (md, buffer, i) != i) {
431 for (cp = buffer; i-- > 0; size++)
435 if (write (md, "\n", 1) != 1) {
443 lseek (fd, (off_t) 0, SEEK_END);
444 stop = lseek (md, (off_t) 0, SEEK_CUR);
446 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
454 mbx_size (int md, off_t start, off_t stop)
460 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
466 fseek (fp, start, SEEK_SET);
467 for (i = 0, pos = stop - start; pos-- > 0; i++)
468 if (fgetc (fp) == '\n')
477 * Close and unlock file/maildrop.
481 mbx_close (char *mailbox, int md)
483 lkclose (md, mailbox);
489 * This function is performed implicitly by getbbent.c:
490 * bb->bb_map = map_name (bb->bb_file);
494 map_name (char *file)
496 register char *cp, *dp;
497 static char buffer[BUFSIZ];
499 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
500 dp = cp + strlen (cp);
502 snprintf (buffer, sizeof(buffer), ".%.*s%s", dp - cp, cp, ".map");
504 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
505 cp - file, file, dp - cp, cp, ".map");
512 map_read (char *file, long pos, struct drop **drops, int noisy)
514 register int i, md, msgp;
517 register struct drop *mp, *dp;
519 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
520 || map_chk (cp, md, mp = &d, pos, noisy)) {
527 dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
533 memcpy((char *) dp, (char *) mp, sizeof(*dp));
535 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
536 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
541 register struct drop *tdp;
544 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
545 tdp->d_id = ntohl(tdp->d_id);
546 tdp->d_size = ntohl(tdp->d_size);
547 tdp->d_start = ntohl(tdp->d_start);
548 tdp->d_stop = ntohl(tdp->d_stop);
556 return (i / sizeof(*dp));
561 map_write (char *mailbox, int md, int id, long last, off_t start,
562 off_t stop, long pos, int size, int noisy)
567 register struct drop *dp;
568 struct drop d1, d2, *rp;
572 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
575 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
580 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
582 mbx_close (file, fd);
583 if ((fd = map_open (file, md)) == NOTOK)
589 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
591 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
594 mbx_close (file, fd);
598 switch (i = mbx_read (fp, 0, &rp, noisy)) {
601 mbx_close (file, fd);
610 for (dp = rp; i-- >0; dp++) {
611 if (dp->d_start == start)
613 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
614 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
616 admonish (file, "write error");
617 mbx_close (file, fd);
632 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
635 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
636 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
638 admonish (file, "write error");
639 mbx_close (file, fd);
646 dp->d_start = (long) last;
647 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
649 lseek (fd, (off_t) 0, SEEK_SET);
650 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
652 admonish (file, "write error");
653 mbx_close (file, fd);
657 mbx_close (file, fd);
664 map_open (char *file, int md)
669 mode = fstat (md, &st) != NOTOK ? (mode_t) (st.st_mode & 0777) : m_gmprot ();
670 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
675 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
679 register struct drop *dl;
681 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
683 admonish (NULL, "%s: missing or partial index", file);
688 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
690 dp->d_id = ntohl(tmpd.d_id);
691 dp->d_size = ntohl(tmpd.d_size);
692 dp->d_start = ntohl(tmpd.d_start);
693 dp->d_stop = ntohl(tmpd.d_stop);
696 if (dp->d_size != DRVRSN) {
698 admonish (NULL, "%s: version mismatch (%d != %d)", file,
703 if (dp->d_stop != pos) {
704 if (noisy && pos != (long) 0)
706 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
707 file, dp->d_stop, (long) pos);
711 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
713 admonish (NULL, "%s: corrupt index(1)", file);
718 count = (long) strlen (mmdlm2);
719 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
720 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
721 || (ntohl(dl->d_stop) != dp->d_stop
722 && ntohl(dl->d_stop) + count != dp->d_stop)) {
724 admonish (NULL, "%s: corrupt index(2)", file);