3 * dropsbr.c -- create/read/manipulate mail drops
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 #include <h/dropsbr.h>
23 # include <netinet/in.h>
34 static int mbx_chk_mbox (int);
35 static int mbx_chk_mmdf (int);
36 static int map_open (char *, int);
40 * Main entry point to open/create and lock
45 mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
52 /* attempt to open and lock file */
53 for (count = 4; count > 0; count--) {
54 if ((fd = lkopen (file, O_RDWR | O_CREAT | O_NONBLOCK, mode)) == NOTOK) {
56 #if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
70 /* just return error */
75 /* good file descriptor */
82 * Return if we still failed after 4 attempts,
83 * or we just want to skip the sanity checks.
85 if (fd == NOTOK || mbx_style == OTHER_FORMAT)
89 * Do sanity checks on maildrop.
91 if (fstat (fd, &st) == NOTOK) {
93 * The stat failed. So we make sure file
94 * has right ownership/modes
96 chown (file, uid, gid);
98 } else if (st.st_size > (off_t) 0) {
101 /* check the maildrop */
105 status = mbx_chk_mmdf (fd);
109 status = mbx_chk_mbox (fd);
113 /* if error, attempt to close it */
114 if (status == NOTOK) {
125 * Check/prepare MBOX style maildrop for appending.
129 mbx_chk_mbox (int fd)
131 /* just seek to the end */
132 if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
140 * Check/prepare MMDF style maildrop for appending.
144 mbx_chk_mmdf (int fd)
149 count = strlen (mmdlm2);
151 /* casting -count to off_t, seem to break FreeBSD 2.2.6 */
152 if (lseek (fd, (long) (-count), SEEK_END) == (off_t) NOTOK)
154 if (read (fd, ldelim, count) != count)
159 if (strcmp (ldelim, mmdlm2)
160 && write (fd, "\n", 1) != 1
161 && write (fd, mmdlm2, count) != count)
169 mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
171 register int len, size;
172 register long ld1, ld2;
175 register struct drop *cp, *dp, *ep, *pp;
177 pp = (struct drop *) calloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
180 admonish (NULL, "unable to allocate drop storage");
184 ld1 = (long) strlen (mmdlm1);
185 ld2 = (long) strlen (mmdlm2);
187 fseek (fp, pos, SEEK_SET);
188 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
190 if (strcmp (buffer, mmdlm1) == 0)
191 pos += ld1, dp->d_start = (long) pos;
193 dp->d_start = (long)pos , pos += (long) strlen (buffer);
194 for (bp = buffer; *bp; bp++, size++)
199 while (fgets (buffer, sizeof(buffer), fp) != NULL)
200 if (strcmp (buffer, mmdlm2) == 0)
203 pos += (long) strlen (buffer);
204 for (bp = buffer; *bp; bp++, size++)
209 if (dp->d_start != (long) pos) {
211 dp->d_size = (long) size;
218 register int curlen = dp - pp;
220 cp = (struct drop *) mh_xrealloc ((char *) pp,
221 (size_t) (len += MAXFOLDER) * sizeof(*pp));
222 dp = cp + curlen, ep = (pp = cp) + len - 1;
235 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
236 long pos, off_t stop, int mapping, int noisy)
238 register int i, j, size;
244 off = (long) lseek (md, (off_t) 0, SEEK_CUR);
246 if (write (md, mmdlm1, j) != j)
248 start = lseek (md, (off_t) 0, SEEK_CUR);
251 fseek (fp, pos, SEEK_SET);
252 while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
254 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
256 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
258 if (write (md, buffer, i) != i)
262 for (cp = buffer; i-- > 0; size++)
267 stop = lseek (md, (off_t) 0, SEEK_CUR);
269 if (write (md, mmdlm2, j) != j)
272 map_write (mailbox, md, id, last, start, stop, off, size, noisy);
279 * Append message to end of file or maildrop.
283 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
284 int mapping, char *text, int noisy)
289 char *cp, buffer[BUFSIZ];
292 pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
299 if (write (md, mmdlm1, j) != j)
301 start = lseek (md, (off_t) 0, SEEK_CUR);
305 if (write (md, text, i) != i)
307 for (cp = text; *cp++; size++)
312 while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
314 (j = stringdex (mmdlm1, buffer)) >= 0;
318 (j = stringdex (mmdlm2, buffer)) >= 0;
321 if (write (md, buffer, i) != i)
324 for (cp = buffer; i-- > 0; size++)
329 stop = lseek (md, (off_t) 0, SEEK_CUR);
331 if (write (md, mmdlm2, j) != j)
334 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
336 return (i != NOTOK ? OK : NOTOK);
339 if ((j = dup (fd)) == NOTOK)
341 if ((fp = fdopen (j, "r")) == NULL) {
345 start = lseek (md, (off_t) 0, SEEK_CUR);
347 /* If text is given, we add it to top of message */
350 if (write (md, text, i) != i)
352 for (cp = text; *cp++; size++)
357 for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
360 * Check the first line, and make some changes.
362 if (j == 0 && !text) {
364 * Change the "Return-Path:" field (if in first line)
367 if (!strncmp (buffer, "Return-Path:", 12)) {
368 char tmpbuffer[BUFSIZ];
371 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
373 if (!(fp = strchr(ep + 1, ' ')))
374 fp = strchr(ep + 1, '\n');
375 tp = dctime(dlocaltimenow());
376 snprintf (buffer, sizeof(buffer), "From %.*s %s",
377 (int)(fp - ep), ep, tp);
378 } else if (!strncmp (buffer, "X-Envelope-From:", 16)) {
380 * Change the "X-Envelope-From:" field
381 * (if first line) back to "From ".
383 char tmpbuffer[BUFSIZ];
386 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
388 snprintf (buffer, sizeof(buffer), "From %s", ep);
389 } else if (strncmp (buffer, "From ", 5)) {
391 * If there is already a "From " line,
392 * then leave it alone. Else we add one.
394 char tmpbuffer[BUFSIZ];
397 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
398 ep = "nobody@nowhere";
399 tp = dctime(dlocaltimenow());
400 snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
405 * If this is not first line, and begins with
406 * "From ", then prepend line with ">".
408 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
413 if (write (md, buffer, i) != i) {
418 for (cp = buffer; i-- > 0; size++)
422 if (write (md, "\n", 1) != 1) {
430 lseek (fd, (off_t) 0, SEEK_END);
431 stop = lseek (md, (off_t) 0, SEEK_CUR);
433 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
441 mbx_size (int md, off_t start, off_t stop)
447 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
453 fseek (fp, start, SEEK_SET);
454 for (i = 0, pos = stop - start; pos-- > 0; i++)
455 if (fgetc (fp) == '\n')
464 * Close and unlock file/maildrop.
468 mbx_close (char *mailbox, int md)
470 if (lkclose (md, mailbox) == 0)
477 * This function is performed implicitly by getbbent.c:
478 * bb->bb_map = map_name (bb->bb_file);
482 map_name (char *file)
484 register char *cp, *dp;
485 static char buffer[BUFSIZ];
487 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
488 dp = cp + strlen (cp);
490 snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
492 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
493 (int)(cp - file), file, (int)(dp - cp), cp, ".map");
500 map_read (char *file, long pos, struct drop **drops, int noisy)
502 register int i, md, msgp;
505 register struct drop *mp, *dp;
507 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
508 || map_chk (cp, md, mp = &d, pos, noisy)) {
515 dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
521 memcpy((char *) dp, (char *) mp, sizeof(*dp));
523 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
524 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
529 register struct drop *tdp;
532 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
533 tdp->d_id = ntohl(tdp->d_id);
534 tdp->d_size = ntohl(tdp->d_size);
535 tdp->d_start = ntohl(tdp->d_start);
536 tdp->d_stop = ntohl(tdp->d_stop);
544 return (i / sizeof(*dp));
549 map_write (char *mailbox, int md, int id, long last, off_t start,
550 off_t stop, long pos, int size, int noisy)
555 register struct drop *dp;
556 struct drop d1, d2, *rp;
560 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
563 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
568 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
570 mbx_close (file, fd);
571 if ((fd = map_open (file, md)) == NOTOK)
577 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
579 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
582 mbx_close (file, fd);
586 switch (i = mbx_read (fp, 0, &rp, noisy)) {
589 mbx_close (file, fd);
598 for (dp = rp; i-- >0; dp++) {
599 if (dp->d_start == start)
601 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
602 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
604 admonish (file, "write error");
605 mbx_close (file, fd);
620 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
623 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
624 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
626 admonish (file, "write error");
627 mbx_close (file, fd);
634 dp->d_start = (long) last;
635 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
637 lseek (fd, (off_t) 0, SEEK_SET);
638 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
640 admonish (file, "write error");
641 mbx_close (file, fd);
645 mbx_close (file, fd);
652 map_open (char *file, int md)
657 mode = fstat (md, &st) != NOTOK ? (mode_t) (st.st_mode & 0777) : m_gmprot ();
658 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
663 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
667 register struct drop *dl;
669 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
671 admonish (NULL, "%s: missing or partial index", file);
676 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
678 dp->d_id = ntohl(tmpd.d_id);
679 dp->d_size = ntohl(tmpd.d_size);
680 dp->d_start = ntohl(tmpd.d_start);
681 dp->d_stop = ntohl(tmpd.d_stop);
684 if (dp->d_size != DRVRSN) {
686 admonish (NULL, "%s: version mismatch (%d != %d)", file,
691 if (dp->d_stop != pos) {
692 if (noisy && pos != (long) 0)
694 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
695 file, dp->d_stop, (long) pos);
699 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
701 admonish (NULL, "%s: corrupt index(1)", file);
706 count = (long) strlen (mmdlm2);
707 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
708 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
709 || (ntohl(dl->d_stop) != dp->d_stop
710 && ntohl(dl->d_stop) + count != dp->d_stop)) {
712 admonish (NULL, "%s: corrupt index(2)", file);