3 * dropsbr.c -- create/read/manipulate mail drops
12 # include <h/dropsbr.h>
13 # include <zotnet/mts/mts.h>
14 # include <zotnet/tws/tws.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 *, 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", ep, tp);
412 strcat (buffer, tmpbuffer);
417 * If this is not first line, and begins with
418 * "From ", then prepend line with ">".
420 if (j != 0 && strncmp (buffer, "From ", 5) == 0) {
425 if (write (md, buffer, i) != i) {
430 for (cp = buffer; i-- > 0; size++)
434 if (write (md, "\n", 1) != 1) {
442 lseek (fd, (off_t) 0, SEEK_END);
443 stop = lseek (md, (off_t) 0, SEEK_CUR);
445 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
453 mbx_size (int md, off_t start, off_t stop)
459 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
465 fseek (fp, start, SEEK_SET);
466 for (i = 0, pos = stop - start; pos-- > 0; i++)
467 if (fgetc (fp) == '\n')
476 * Close and unlock file/maildrop.
480 mbx_close (char *mailbox, int md)
482 lkclose (md, mailbox);
488 * This function is performed implicitly by getbbent.c:
489 * bb->bb_map = map_name (bb->bb_file);
493 map_name (char *file)
495 register char *cp, *dp;
496 static char buffer[BUFSIZ];
498 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
499 dp = cp + strlen (cp);
501 snprintf (buffer, sizeof(buffer), ".%.*s%s", dp - cp, cp, ".map");
503 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
504 cp - file, file, dp - cp, cp, ".map");
511 map_read (char *file, long pos, struct drop **drops, int noisy)
513 register int i, md, msgp;
516 register struct drop *mp, *dp;
518 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
519 || map_chk (cp, md, mp = &d, pos, noisy)) {
526 dp = (struct drop *) calloc ((size_t) (msgp + 1), sizeof(*dp));
532 memcpy((char *) dp, (char *) mp, sizeof(*dp));
534 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
535 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) < sizeof(*dp)) {
540 register struct drop *tdp;
543 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
544 tdp->d_id = ntohl(tdp->d_id);
545 tdp->d_size = ntohl(tdp->d_size);
546 tdp->d_start = ntohl(tdp->d_start);
547 tdp->d_stop = ntohl(tdp->d_stop);
555 return (i / sizeof(*dp));
560 map_write (char *mailbox, int md, int id, long last, off_t start,
561 off_t stop, long pos, int size, int noisy)
566 register struct drop *dp;
567 struct drop d1, d2, *rp;
570 if ((fd = map_open (file = map_name (mailbox), &clear, md)) == NOTOK)
573 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
575 mbx_close (file, fd);
576 if ((fd = map_open (file, &clear, md)) == NOTOK)
582 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
584 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
587 mbx_close (file, fd);
591 switch (i = mbx_read (fp, 0, &rp, noisy)) {
594 mbx_close (file, fd);
602 for (dp = rp; i-- >0; dp++) {
603 if (dp->d_start == start)
605 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
606 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
608 admonish (file, "write error");
609 mbx_close (file, fd);
623 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
626 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
627 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
629 admonish (file, "write error");
630 mbx_close (file, fd);
637 dp->d_start = (long) last;
638 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
640 lseek (fd, (off_t) 0, SEEK_SET);
641 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
643 admonish (file, "write error");
644 mbx_close (file, fd);
648 mbx_close (file, fd);
655 map_open (char *file, int *clear, int md)
660 mode = fstat (md, &st) != NOTOK ? (mode_t) (st.st_mode & 0777) : m_gmprot ();
661 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
666 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
670 register struct drop *dl;
672 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
674 admonish (NULL, "%s: missing or partial index", file);
679 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
681 dp->d_id = ntohl(tmpd.d_id);
682 dp->d_size = ntohl(tmpd.d_size);
683 dp->d_start = ntohl(tmpd.d_start);
684 dp->d_stop = ntohl(tmpd.d_stop);
687 if (dp->d_size != DRVRSN) {
689 admonish (NULL, "%s: version mismatch (%d != %d)", file,
694 if (dp->d_stop != pos) {
695 if (noisy && pos != (long) 0)
697 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
698 file, dp->d_stop, (long) pos);
702 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
704 admonish (NULL, "%s: corrupt index(1)", file);
709 count = (long) strlen (mmdlm2);
710 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
711 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
712 || (ntohl(dl->d_stop) != dp->d_stop
713 && ntohl(dl->d_stop) + count != dp->d_stop)) {
715 admonish (NULL, "%s: corrupt index(2)", file);