2 * dropsbr.c -- create/read/manipulate mail drops
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
13 #include <h/dropsbr.h>
22 # include <netinet/in.h>
33 static int mbx_chk_mbox (int);
34 static int mbx_chk_mmdf (int);
35 static int map_open (char *, int);
39 * Main entry point to open/create and lock
44 mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
51 /* attempt to open and lock file */
52 for (count = 4; count > 0; count--) {
53 if ((fd = lkopen (file, O_RDWR | O_CREAT | O_NONBLOCK, mode)) == NOTOK) {
55 #if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
69 /* just return error */
74 /* good file descriptor */
81 * Return if we still failed after 4 attempts,
82 * or we just want to skip the sanity checks.
84 if (fd == NOTOK || mbx_style == OTHER_FORMAT)
88 * Do sanity checks on maildrop.
90 if (fstat (fd, &st) == NOTOK) {
92 * The stat failed. So we make sure file
93 * has right ownership/modes
95 chown (file, uid, gid);
97 } else if (st.st_size > (off_t) 0) {
100 /* check the maildrop */
104 status = mbx_chk_mmdf (fd);
108 status = mbx_chk_mbox (fd);
112 /* if error, attempt to close it */
113 if (status == NOTOK) {
124 * Check/prepare MBOX style maildrop for appending.
128 mbx_chk_mbox (int fd)
130 /* just seek to the end */
131 if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
139 * Check/prepare MMDF style maildrop for appending.
143 mbx_chk_mmdf (int fd)
148 count = strlen (mmdlm2);
150 /* casting -count to off_t, seem to break FreeBSD 2.2.6 */
151 if (lseek (fd, (long) (-count), SEEK_END) == (off_t) NOTOK)
153 if (read (fd, ldelim, count) != count)
158 if (strcmp (ldelim, mmdlm2)
159 && write (fd, "\n", 1) != 1
160 && write (fd, mmdlm2, count) != count)
168 mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
170 register int len, size;
171 register long ld1, ld2;
174 register struct drop *cp, *dp, *ep, *pp;
176 pp = (struct drop *) calloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
179 admonish (NULL, "unable to allocate drop storage");
183 ld1 = (long) strlen (mmdlm1);
184 ld2 = (long) strlen (mmdlm2);
186 fseek (fp, pos, SEEK_SET);
187 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
189 if (strcmp (buffer, mmdlm1) == 0)
190 pos += ld1, dp->d_start = (long) pos;
192 dp->d_start = (long)pos , pos += (long) strlen (buffer);
193 for (bp = buffer; *bp; bp++, size++)
198 while (fgets (buffer, sizeof(buffer), fp) != NULL)
199 if (strcmp (buffer, mmdlm2) == 0)
202 pos += (long) strlen (buffer);
203 for (bp = buffer; *bp; bp++, size++)
208 if (dp->d_start != (long) pos) {
210 dp->d_size = (long) size;
217 register int curlen = dp - pp;
219 cp = (struct drop *) mh_xrealloc ((char *) pp,
220 (size_t) (len += MAXFOLDER) * sizeof(*pp));
221 dp = cp + curlen, ep = (pp = cp) + len - 1;
234 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
235 long pos, off_t stop, int mapping, int noisy)
237 register int i, j, size;
243 off = (long) lseek (md, (off_t) 0, SEEK_CUR);
245 if (write (md, mmdlm1, j) != j)
247 start = lseek (md, (off_t) 0, SEEK_CUR);
250 fseek (fp, pos, SEEK_SET);
251 while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
253 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
255 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
257 if (write (md, buffer, i) != i)
261 for (cp = buffer; i-- > 0; size++)
266 stop = lseek (md, (off_t) 0, SEEK_CUR);
268 if (write (md, mmdlm2, j) != j)
271 map_write (mailbox, md, id, last, start, stop, off, size, noisy);
278 * Append message to end of file or maildrop.
282 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
283 int mapping, char *text, int noisy)
288 char *cp, buffer[BUFSIZ];
291 pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
298 if (write (md, mmdlm1, j) != j)
300 start = lseek (md, (off_t) 0, SEEK_CUR);
304 if (write (md, text, i) != i)
306 for (cp = text; *cp++; size++)
311 while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
313 (j = stringdex (mmdlm1, buffer)) >= 0;
317 (j = stringdex (mmdlm2, buffer)) >= 0;
320 if (write (md, buffer, i) != i)
323 for (cp = buffer; i-- > 0; size++)
328 stop = lseek (md, (off_t) 0, SEEK_CUR);
330 if (write (md, mmdlm2, j) != j)
333 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
335 return (i != NOTOK ? OK : NOTOK);
338 if ((j = dup (fd)) == NOTOK)
340 if ((fp = fdopen (j, "r")) == NULL) {
344 start = lseek (md, (off_t) 0, SEEK_CUR);
346 /* If text is given, we add it to top of message */
349 if (write (md, text, i) != i)
351 for (cp = text; *cp++; size++)
356 for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
359 * Check the first line, and make some changes.
361 if (j == 0 && !text) {
363 * Change the "Return-Path:" field (if in first line)
366 if (!strncmp (buffer, "Return-Path:", 12)) {
367 char tmpbuffer[BUFSIZ];
370 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
372 if (!(fp = strchr(ep + 1, ' ')))
373 fp = strchr(ep + 1, '\n');
374 tp = dctime(dlocaltimenow());
375 snprintf (buffer, sizeof(buffer), "From %.*s %s",
376 (int)(fp - ep), ep, tp);
377 } else if (!strncmp (buffer, "X-Envelope-From:", 16)) {
379 * Change the "X-Envelope-From:" field
380 * (if first line) back to "From ".
382 char tmpbuffer[BUFSIZ];
385 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
387 snprintf (buffer, sizeof(buffer), "From %s", ep);
388 } else if (strncmp (buffer, "From ", 5)) {
390 * If there is already a "From " line,
391 * then leave it alone. Else we add one.
393 char tmpbuffer[BUFSIZ];
396 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
397 ep = "nobody@nowhere";
398 tp = dctime(dlocaltimenow());
399 snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
404 * If this is not first line, and begins with
405 * "From ", then prepend line with ">".
407 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
412 if (write (md, buffer, i) != i) {
417 for (cp = buffer; i-- > 0; size++)
421 if (write (md, "\n", 1) != 1) {
429 lseek (fd, (off_t) 0, SEEK_END);
430 stop = lseek (md, (off_t) 0, SEEK_CUR);
432 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
440 mbx_size (int md, off_t start, off_t stop)
446 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
452 fseek (fp, start, SEEK_SET);
453 for (i = 0, pos = stop - start; pos-- > 0; i++)
454 if (fgetc (fp) == '\n')
463 * Close and unlock file/maildrop.
467 mbx_close (char *mailbox, int md)
469 if (lkclose (md, mailbox) == 0)
476 * This function is performed implicitly by getbbent.c:
477 * bb->bb_map = map_name (bb->bb_file);
481 map_name (char *file)
483 register char *cp, *dp;
484 static char buffer[BUFSIZ];
486 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
487 dp = cp + strlen (cp);
489 snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
491 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
492 (int)(cp - file), file, (int)(dp - cp), cp, ".map");
499 map_read (char *file, long pos, struct drop **drops, int noisy)
501 register int i, md, msgp;
504 register struct drop *mp, *dp;
506 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
507 || map_chk (cp, md, mp = &d, pos, noisy)) {
514 dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
520 memcpy((char *) dp, (char *) mp, sizeof(*dp));
522 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
523 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
528 register struct drop *tdp;
531 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
532 tdp->d_id = ntohl(tdp->d_id);
533 tdp->d_size = ntohl(tdp->d_size);
534 tdp->d_start = ntohl(tdp->d_start);
535 tdp->d_stop = ntohl(tdp->d_stop);
543 return (i / sizeof(*dp));
548 map_write (char *mailbox, int md, int id, long last, off_t start,
549 off_t stop, long pos, int size, int noisy)
554 register struct drop *dp;
555 struct drop d1, d2, *rp;
559 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
562 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
567 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
569 mbx_close (file, fd);
570 if ((fd = map_open (file, md)) == NOTOK)
576 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
578 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
581 mbx_close (file, fd);
585 switch (i = mbx_read (fp, 0, &rp, noisy)) {
588 mbx_close (file, fd);
597 for (dp = rp; i-- >0; dp++) {
598 if (dp->d_start == start)
600 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
601 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
603 admonish (file, "write error");
604 mbx_close (file, fd);
619 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
622 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
623 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
625 admonish (file, "write error");
626 mbx_close (file, fd);
633 dp->d_start = (long) last;
634 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
636 lseek (fd, (off_t) 0, SEEK_SET);
637 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
639 admonish (file, "write error");
640 mbx_close (file, fd);
644 mbx_close (file, fd);
651 map_open (char *file, int md)
656 mode = fstat (md, &st) != NOTOK ? (mode_t) (st.st_mode & 0777) : m_gmprot ();
657 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
662 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
666 register struct drop *dl;
668 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
670 admonish (NULL, "%s: missing or partial index", file);
675 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
677 dp->d_id = ntohl(tmpd.d_id);
678 dp->d_size = ntohl(tmpd.d_size);
679 dp->d_start = ntohl(tmpd.d_start);
680 dp->d_stop = ntohl(tmpd.d_stop);
683 if (dp->d_size != DRVRSN) {
685 admonish (NULL, "%s: version mismatch (%d != %d)",
686 file, dp->d_size, DRVRSN);
690 if (dp->d_stop != pos) {
691 if (noisy && pos != (long) 0)
693 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
694 file, dp->d_stop, (long) pos);
698 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
700 admonish (NULL, "%s: corrupt index(1)", file);
705 count = (long) strlen (mmdlm2);
706 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
707 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
708 || (ntohl(dl->d_stop) != dp->d_stop
709 && ntohl(dl->d_stop) + count != dp->d_stop)) {
711 admonish (NULL, "%s: corrupt index(2)", file);