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))
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.
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.
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) && 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) *
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,
280 ** Append message to end of file or maildrop.
284 mbx_copy(char *mailbox, int mbx_style, int md, int fd, int mapping,
285 char *text, int noisy)
290 char *cp, buffer[BUFSIZ];
293 pos = (long) lseek(md, (off_t) 0, SEEK_CUR);
300 if (write(md, mmdlm1, j) != j)
302 start = lseek(md, (off_t) 0, SEEK_CUR);
306 if (write(md, text, i) != i)
308 for (cp = text; *cp++; size++)
313 while ((i = read(fd, buffer, sizeof(buffer))) > 0) {
314 for (j = 0; (j = stringdex(mmdlm1, buffer)) >= 0; buffer[j]++)
316 for (j = 0; (j = stringdex(mmdlm2, buffer)) >= 0; buffer[j]++)
318 if (write(md, buffer, i) != i)
321 for (cp = buffer; i-- > 0; size++)
326 stop = lseek(md, (off_t) 0, SEEK_CUR);
328 if (write(md, mmdlm2, j) != j)
331 map_write(mailbox, md, 0, (long) 0, start,
332 stop, pos, size, noisy);
334 return (i != NOTOK ? OK : NOTOK);
337 if ((j = dup(fd)) == NOTOK)
339 if ((fp = fdopen(j, "r")) == NULL) {
343 start = lseek(md, (off_t) 0, SEEK_CUR);
345 /* If text is given, we add it to top of message */
348 if (write(md, text, i) != i)
350 for (cp = text; *cp++; size++)
355 for (j = 0; fgets(buffer, sizeof(buffer), fp) != NULL;
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:",
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", (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);
486 register char *cp, *dp;
487 static char buffer[BUFSIZ];
489 if ((dp = strchr(cp = mhbasename(file), '.')) == NULL)
490 dp = cp + strlen(cp);
492 snprintf(buffer, sizeof(buffer), ".%.*s%s",
493 (int)(dp - cp), cp, ".map");
495 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
496 (int)(cp - file), file, (int)(dp - cp),
504 map_read(char *file, long pos, struct drop **drops, int noisy)
506 register int i, md, msgp;
509 register struct drop *mp, *dp;
511 if ((md = open(cp = map_name(file), O_RDONLY)) == NOTOK
512 || map_chk(cp, md, mp = &d, pos, noisy)) {
519 dp = (struct drop *) calloc((size_t) (msgp + 1), sizeof(*dp));
525 memcpy((char *) dp, (char *) mp, sizeof(*dp));
527 lseek(md, (off_t) sizeof(*mp), SEEK_SET);
528 if ((i = read(md, (char *) (dp + 1), msgp * sizeof(*dp))) <
534 register struct drop *tdp;
537 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
538 tdp->d_id = ntohl(tdp->d_id);
539 tdp->d_size = ntohl(tdp->d_size);
540 tdp->d_start = ntohl(tdp->d_start);
541 tdp->d_stop = ntohl(tdp->d_stop);
549 return (i / sizeof(*dp));
554 map_write(char *mailbox, int md, int id, long last, off_t start,
555 off_t stop, long pos, int size, int noisy)
560 register struct drop *dp;
561 struct drop d1, d2, *rp;
565 if ((fd = map_open(file = map_name(mailbox), md)) == NOTOK)
568 if ((fstat(fd, &st) == OK) && (st.st_size > 0))
573 if (!clear && map_chk(file, fd, &d1, pos, noisy)) {
576 if ((fd = map_open(file, md)) == NOTOK)
582 if ((td = dup(md)) == NOTOK ||
583 (fp = fdopen(td, "r")) == NULL) {
585 admonish(file, "unable to %s", td != NOTOK ?
593 switch (i = mbx_read(fp, 0, &rp, noisy)) {
605 for (dp = rp; i-- >0; dp++) {
606 if (dp->d_start == start)
608 lseek(fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
609 if (write(fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
611 admonish(file, "write error");
627 dp->d_size = (long) (size ? size : mbx_size(fd, start, stop));
630 lseek(fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
631 if (write(fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
633 admonish(file, "write error");
641 dp->d_start = (long) last;
642 dp->d_stop = lseek(md, (off_t) 0, SEEK_CUR);
644 lseek(fd, (off_t) 0, SEEK_SET);
645 if (write(fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
647 admonish(file, "write error");
659 map_open(char *file, int md)
664 mode = fstat(md, &st) != NOTOK ?
665 (mode_t) (st.st_mode & 0777) : m_gmprot();
666 return mbx_open(file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
671 map_chk(char *file, int fd, struct drop *dp, long pos, int noisy)
675 register struct drop *dl;
677 if (read(fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
679 admonish(NULL, "%s: missing or partial index", file);
684 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
686 dp->d_id = ntohl(tmpd.d_id);
687 dp->d_size = ntohl(tmpd.d_size);
688 dp->d_start = ntohl(tmpd.d_start);
689 dp->d_stop = ntohl(tmpd.d_stop);
692 if (dp->d_size != DRVRSN) {
694 admonish(NULL, "%s: version mismatch (%d != %d)",
695 file, dp->d_size, DRVRSN);
699 if (dp->d_stop != pos) {
700 if (noisy && pos != (long) 0)
701 admonish(NULL, "%s: pointer mismatch or incomplete index (%ld!=%ld)", file, dp->d_stop, (long) pos);
705 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek(fd, (off_t) 0, SEEK_END)) {
707 admonish(NULL, "%s: corrupt index(1)", file);
712 count = (long) strlen(mmdlm2);
713 lseek(fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
714 if (read(fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
715 || (ntohl(dl->d_stop) != dp->d_stop
716 && ntohl(dl->d_stop) + count != dp->d_stop)) {
718 admonish(NULL, "%s: corrupt index(2)", file);