3 * dropsbr.c -- create/read/manipulate mail drops
12 # include <h/dropsbr.h>
18 # include "mmdfonly.h"
26 # include <netinet/in.h>
39 static int mbx_chk_mbox (int);
40 static int mbx_chk_mmdf (int);
41 static int map_open (char *, int);
45 * Main entry point to open/create and lock
50 mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
57 /* attempt to open and lock file */
58 for (count = 4; count > 0; count--) {
59 if ((fd = lkopen (file, O_RDWR | O_CREAT | O_NONBLOCK, mode)) == NOTOK) {
61 #if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
75 /* just return error */
80 /* good file descriptor */
87 * Return if we still failed after 4 attempts,
88 * or we just want to skip the sanity checks.
90 if (fd == NOTOK || mbx_style == OTHER_FORMAT)
94 * Do sanity checks on maildrop.
96 if (fstat (fd, &st) == NOTOK) {
98 * The stat failed. So we make sure file
99 * has right ownership/modes
101 chown (file, uid, gid);
103 } else if (st.st_size > (off_t) 0) {
106 /* check the maildrop */
110 status = mbx_chk_mmdf (fd);
114 status = mbx_chk_mbox (fd);
118 /* if error, attempt to close it */
119 if (status == NOTOK) {
130 * Check/prepare MBOX style maildrop for appending.
134 mbx_chk_mbox (int fd)
136 /* just seek to the end */
137 if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
145 * Check/prepare MMDF style maildrop for appending.
149 mbx_chk_mmdf (int fd)
154 count = strlen (mmdlm2);
156 /* casting -count to off_t, seem to break FreeBSD 2.2.6 */
157 if (lseek (fd, (long) (-count), SEEK_END) == (off_t) NOTOK)
159 if (read (fd, ldelim, count) != count)
164 if (strcmp (ldelim, mmdlm2)
165 && write (fd, "\n", 1) != 1
166 && write (fd, mmdlm2, count) != count)
174 mbx_read (FILE *fp, long pos, struct drop **drops, int noisy)
176 register int len, size;
177 register long ld1, ld2;
180 register struct drop *cp, *dp, *ep, *pp;
182 pp = (struct drop *) calloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
185 admonish (NULL, "unable to allocate drop storage");
189 ld1 = (long) strlen (mmdlm1);
190 ld2 = (long) strlen (mmdlm2);
192 fseek (fp, pos, SEEK_SET);
193 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
195 if (strcmp (buffer, mmdlm1) == 0)
196 pos += ld1, dp->d_start = (long) pos;
198 dp->d_start = (long)pos , pos += (long) strlen (buffer);
199 for (bp = buffer; *bp; bp++, size++)
204 while (fgets (buffer, sizeof(buffer), fp) != NULL)
205 if (strcmp (buffer, mmdlm2) == 0)
208 pos += (long) strlen (buffer);
209 for (bp = buffer; *bp; bp++, size++)
214 if (dp->d_start != (long) pos) {
216 dp->d_size = (long) size;
223 register int curlen = dp - pp;
225 cp = (struct drop *) realloc ((char *) pp,
226 (size_t) (len += MAXFOLDER) * sizeof(*pp));
229 admonish (NULL, "unable to allocate drop storage");
233 dp = cp + curlen, ep = (pp = cp) + len - 1;
246 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
247 long pos, off_t stop, int mapping, int noisy)
249 register int i, j, size;
255 off = (long) lseek (md, (off_t) 0, SEEK_CUR);
257 if (write (md, mmdlm1, j) != j)
259 start = lseek (md, (off_t) 0, SEEK_CUR);
262 fseek (fp, pos, SEEK_SET);
263 while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
265 for (j = 0; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
267 for (j = 0; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
269 if (write (md, buffer, i) != i)
273 for (cp = buffer; i-- > 0; size++)
278 stop = lseek (md, (off_t) 0, SEEK_CUR);
280 if (write (md, mmdlm2, j) != j)
283 map_write (mailbox, md, id, last, start, stop, off, size, noisy);
290 * Append message to end of file or maildrop.
294 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
295 int mapping, char *text, int noisy)
300 char *cp, buffer[BUFSIZ];
303 pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
310 if (write (md, mmdlm1, j) != j)
312 start = lseek (md, (off_t) 0, SEEK_CUR);
316 if (write (md, text, i) != i)
318 for (cp = text; *cp++; size++)
323 while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
325 (j = stringdex (mmdlm1, buffer)) >= 0;
329 (j = stringdex (mmdlm2, buffer)) >= 0;
332 if (write (md, buffer, i) != i)
335 for (cp = buffer; i-- > 0; size++)
340 stop = lseek (md, (off_t) 0, SEEK_CUR);
342 if (write (md, mmdlm2, j) != j)
345 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
347 return (i != NOTOK ? OK : NOTOK);
350 if ((j = dup (fd)) == NOTOK)
352 if ((fp = fdopen (j, "r")) == NULL) {
356 start = lseek (md, (off_t) 0, SEEK_CUR);
358 /* If text is given, we add it to top of message */
361 if (write (md, text, i) != i)
363 for (cp = text; *cp++; size++)
368 for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
371 * Check the first line, and make some changes.
373 if (j == 0 && !text) {
375 * Change the "Return-Path:" field (if in first line)
378 if (!strncmp (buffer, "Return-Path:", 12)) {
379 char tmpbuffer[BUFSIZ];
382 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
384 if (!(fp = strchr(ep + 1, ' ')))
385 fp = strchr(ep + 1, '\n');
386 tp = dctime(dlocaltimenow());
387 snprintf (buffer, sizeof(buffer), "From %.*s %s",
389 } else if (!strncmp (buffer, "X-Envelope-From:", 16)) {
391 * Change the "X-Envelope-From:" field
392 * (if first line) back to "From ".
394 char tmpbuffer[BUFSIZ];
397 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
399 snprintf (buffer, sizeof(buffer), "From %s", ep);
400 } else if (strncmp (buffer, "From ", 5)) {
402 * If there is already a "From " line,
403 * then leave it alone. Else we add one.
405 char tmpbuffer[BUFSIZ];
408 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
409 ep = "nobody@nowhere";
410 tp = dctime(dlocaltimenow());
411 snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
416 * If this is not first line, and begins with
417 * "From ", then prepend line with ">".
419 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
424 if (write (md, buffer, i) != i) {
429 for (cp = buffer; i-- > 0; size++)
433 if (write (md, "\n", 1) != 1) {
441 lseek (fd, (off_t) 0, SEEK_END);
442 stop = lseek (md, (off_t) 0, SEEK_CUR);
444 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
452 mbx_size (int md, off_t start, off_t stop)
458 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
464 fseek (fp, start, SEEK_SET);
465 for (i = 0, pos = stop - start; pos-- > 0; i++)
466 if (fgetc (fp) == '\n')
475 * Close and unlock file/maildrop.
479 mbx_close (char *mailbox, int md)
481 lkclose (md, mailbox);
487 * This function is performed implicitly by getbbent.c:
488 * bb->bb_map = map_name (bb->bb_file);
492 map_name (char *file)
494 register char *cp, *dp;
495 static char buffer[BUFSIZ];
497 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
498 dp = cp + strlen (cp);
500 snprintf (buffer, sizeof(buffer), ".%.*s%s", dp - cp, cp, ".map");
502 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
503 cp - file, file, dp - cp, cp, ".map");
510 map_read (char *file, long pos, struct drop **drops, int noisy)
512 register int i, md, msgp;
515 register struct drop *mp, *dp;
517 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
518 || map_chk (cp, md, mp = &d, pos, noisy)) {
525 dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
531 memcpy((char *) dp, (char *) mp, sizeof(*dp));
533 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
534 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
539 register struct drop *tdp;
542 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
543 tdp->d_id = ntohl(tdp->d_id);
544 tdp->d_size = ntohl(tdp->d_size);
545 tdp->d_start = ntohl(tdp->d_start);
546 tdp->d_stop = ntohl(tdp->d_stop);
554 return (i / sizeof(*dp));
559 map_write (char *mailbox, int md, int id, long last, off_t start,
560 off_t stop, long pos, int size, int noisy)
565 register struct drop *dp;
566 struct drop d1, d2, *rp;
570 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
573 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
578 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
580 mbx_close (file, fd);
581 if ((fd = map_open (file, md)) == NOTOK)
587 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
589 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
592 mbx_close (file, fd);
596 switch (i = mbx_read (fp, 0, &rp, noisy)) {
599 mbx_close (file, fd);
608 for (dp = rp; i-- >0; dp++) {
609 if (dp->d_start == start)
611 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
612 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
614 admonish (file, "write error");
615 mbx_close (file, fd);
630 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
633 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
634 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
636 admonish (file, "write error");
637 mbx_close (file, fd);
644 dp->d_start = (long) last;
645 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
647 lseek (fd, (off_t) 0, SEEK_SET);
648 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
650 admonish (file, "write error");
651 mbx_close (file, fd);
655 mbx_close (file, fd);
662 map_open (char *file, int md)
667 mode = fstat (md, &st) != NOTOK ? (mode_t) (st.st_mode & 0777) : m_gmprot ();
668 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
673 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
677 register struct drop *dl;
679 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
681 admonish (NULL, "%s: missing or partial index", file);
686 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
688 dp->d_id = ntohl(tmpd.d_id);
689 dp->d_size = ntohl(tmpd.d_size);
690 dp->d_start = ntohl(tmpd.d_start);
691 dp->d_stop = ntohl(tmpd.d_stop);
694 if (dp->d_size != DRVRSN) {
696 admonish (NULL, "%s: version mismatch (%d != %d)", file,
701 if (dp->d_stop != pos) {
702 if (noisy && pos != (long) 0)
704 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
705 file, dp->d_stop, (long) pos);
709 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
711 admonish (NULL, "%s: corrupt index(1)", file);
716 count = (long) strlen (mmdlm2);
717 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
718 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
719 || (ntohl(dl->d_stop) != dp->d_stop
720 && ntohl(dl->d_stop) + count != dp->d_stop)) {
722 admonish (NULL, "%s: corrupt index(2)", file);