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
40 ** a file or maildrop.
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
364 ** (if in first line) back to "From ".
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:"
380 ** field (if first line) back
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 "
392 ** line, then leave it alone.
395 char tmpbuffer[BUFSIZ];
398 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
399 ep = "nobody@nowhere";
400 tp = dctime(dlocaltimenow());
401 snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
406 ** If this is not first line, and begins with
407 ** "From ", then prepend line with ">".
409 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
414 if (write (md, buffer, i) != i) {
419 for (cp = buffer; i-- > 0; size++)
423 if (write (md, "\n", 1) != 1) {
431 lseek (fd, (off_t) 0, SEEK_END);
432 stop = lseek (md, (off_t) 0, SEEK_CUR);
434 map_write (mailbox, md, 0, (long) 0, start,
435 stop, pos, size, noisy);
443 mbx_size (int md, off_t start, off_t stop)
449 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
455 fseek (fp, start, SEEK_SET);
456 for (i = 0, pos = stop - start; pos-- > 0; i++)
457 if (fgetc (fp) == '\n')
466 ** Close and unlock file/maildrop.
470 mbx_close (char *mailbox, int md)
472 if (lkclose (md, mailbox) == 0)
479 ** This function is performed implicitly by getbbent.c:
480 ** bb->bb_map = map_name (bb->bb_file);
484 map_name (char *file)
486 register char *cp, *dp;
487 static char buffer[BUFSIZ];
489 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
490 dp = cp + strlen (cp);
492 snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
494 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
495 (int)(cp - file), file, (int)(dp - cp), cp, ".map");
502 map_read (char *file, long pos, struct drop **drops, int noisy)
504 register int i, md, msgp;
507 register struct drop *mp, *dp;
509 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
510 || map_chk (cp, md, mp = &d, pos, noisy)) {
517 dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
523 memcpy((char *) dp, (char *) mp, sizeof(*dp));
525 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
526 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
531 register struct drop *tdp;
534 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
535 tdp->d_id = ntohl(tdp->d_id);
536 tdp->d_size = ntohl(tdp->d_size);
537 tdp->d_start = ntohl(tdp->d_start);
538 tdp->d_stop = ntohl(tdp->d_stop);
546 return (i / sizeof(*dp));
551 map_write (char *mailbox, int md, int id, long last, off_t start,
552 off_t stop, long pos, int size, int noisy)
557 register struct drop *dp;
558 struct drop d1, d2, *rp;
562 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
565 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
570 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
572 mbx_close (file, fd);
573 if ((fd = map_open (file, md)) == NOTOK)
579 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
581 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
584 mbx_close (file, fd);
588 switch (i = mbx_read (fp, 0, &rp, noisy)) {
591 mbx_close (file, fd);
600 for (dp = rp; i-- >0; dp++) {
601 if (dp->d_start == start)
603 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
604 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
606 admonish (file, "write error");
607 mbx_close (file, fd);
622 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
625 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
626 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
628 admonish (file, "write error");
629 mbx_close (file, fd);
636 dp->d_start = (long) last;
637 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
639 lseek (fd, (off_t) 0, SEEK_SET);
640 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
642 admonish (file, "write error");
643 mbx_close (file, fd);
647 mbx_close (file, fd);
654 map_open (char *file, int md)
659 mode = fstat (md, &st) != NOTOK ? (mode_t) (st.st_mode & 0777) : m_gmprot ();
660 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
665 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
669 register struct drop *dl;
671 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
673 admonish (NULL, "%s: missing or partial index", file);
678 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
680 dp->d_id = ntohl(tmpd.d_id);
681 dp->d_size = ntohl(tmpd.d_size);
682 dp->d_start = ntohl(tmpd.d_start);
683 dp->d_stop = ntohl(tmpd.d_stop);
686 if (dp->d_size != DRVRSN) {
688 admonish (NULL, "%s: version mismatch (%d != %d)",
689 file, dp->d_size, DRVRSN);
693 if (dp->d_stop != pos) {
694 if (noisy && pos != (long) 0)
696 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
697 file, dp->d_stop, (long) pos);
701 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
703 admonish (NULL, "%s: corrupt index(1)", file);
708 count = (long) strlen (mmdlm2);
709 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
710 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
711 || (ntohl(dl->d_stop) != dp->d_stop
712 && ntohl(dl->d_stop) + count != dp->d_stop)) {
714 admonish (NULL, "%s: corrupt index(2)", file);