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, stop, pos,
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; j++) {
357 ** Check the first line, and make some changes.
359 if (j == 0 && !text) {
361 ** Change the "Return-Path:" field
362 ** (if in first line) back to "From ".
364 if (!strncmp(buffer, "Return-Path:", 12)) {
365 char tmpbuffer[BUFSIZ];
368 strncpy(tmpbuffer, buffer,
371 if (!(fp = strchr(ep + 1, ' ')))
372 fp = strchr(ep + 1, '\n');
373 tp = dctime(dlocaltimenow());
374 snprintf(buffer, sizeof(buffer), "From %.*s %s", (int)(fp - ep), ep, tp);
375 } else if (!strncmp(buffer, "X-Envelope-From:",
378 ** Change the "X-Envelope-From:"
379 ** field (if first line) back
382 char tmpbuffer[BUFSIZ];
385 strncpy(tmpbuffer, buffer,
388 snprintf(buffer, sizeof(buffer),
390 } else if (strncmp(buffer, "From ", 5)) {
392 ** If there is already a "From "
393 ** line, then leave it alone.
396 char tmpbuffer[BUFSIZ];
399 strncpy(tmpbuffer, buffer,
401 ep = "nobody@nowhere";
402 tp = dctime(dlocaltimenow());
403 snprintf(buffer, sizeof(buffer),
410 ** If this is not first line, and begins with
411 ** "From ", then prepend line with ">".
413 if (j != 0 && strncmp(buffer, "From ", 5) == 0) {
418 if (write(md, buffer, i) != i) {
423 for (cp = buffer; i-- > 0; size++)
427 if (write(md, "\n", 1) != 1) {
435 lseek(fd, (off_t) 0, SEEK_END);
436 stop = lseek(md, (off_t) 0, SEEK_CUR);
438 map_write(mailbox, md, 0, (long) 0, start, stop, pos,
447 mbx_size(int md, off_t start, off_t stop)
453 if ((fd = dup(md)) == NOTOK || (fp = fdopen(fd, "r")) == NULL) {
459 fseek(fp, start, SEEK_SET);
460 for (i = 0, pos = stop - start; pos-- > 0; i++)
461 if (fgetc(fp) == '\n')
470 ** Close and unlock file/maildrop.
474 mbx_close(char *mailbox, int md)
476 if (lkclose(md, mailbox) == 0)
483 ** This function is performed implicitly by getbbent.c:
484 ** bb->bb_map = map_name(bb->bb_file);
490 register char *cp, *dp;
491 static char buffer[BUFSIZ];
493 if ((dp = strchr(cp = mhbasename(file), '.')) == NULL)
494 dp = cp + strlen(cp);
496 snprintf(buffer, sizeof(buffer), ".%.*s%s",
497 (int)(dp - cp), cp, ".map");
499 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
500 (int)(cp - file), file, (int)(dp - cp),
508 map_read(char *file, long pos, struct drop **drops, int noisy)
510 register int i, md, msgp;
513 register struct drop *mp, *dp;
515 if ((md = open(cp = map_name(file), O_RDONLY)) == NOTOK
516 || map_chk(cp, md, mp = &d, pos, noisy)) {
523 dp = (struct drop *) calloc((size_t) (msgp + 1), sizeof(*dp));
529 memcpy((char *) dp, (char *) mp, sizeof(*dp));
531 lseek(md, (off_t) sizeof(*mp), SEEK_SET);
532 if ((i = read(md, (char *) (dp + 1), msgp * sizeof(*dp))) <
538 register struct drop *tdp;
541 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
542 tdp->d_id = ntohl(tdp->d_id);
543 tdp->d_size = ntohl(tdp->d_size);
544 tdp->d_start = ntohl(tdp->d_start);
545 tdp->d_stop = ntohl(tdp->d_stop);
553 return (i / sizeof(*dp));
558 map_write(char *mailbox, int md, int id, long last, off_t start,
559 off_t stop, long pos, int size, int noisy)
564 register struct drop *dp;
565 struct drop d1, d2, *rp;
569 if ((fd = map_open(file = map_name(mailbox), md)) == NOTOK)
572 if ((fstat(fd, &st) == OK) && (st.st_size > 0))
577 if (!clear && map_chk(file, fd, &d1, pos, noisy)) {
580 if ((fd = map_open(file, md)) == NOTOK)
586 if ((td = dup(md)) == NOTOK ||
587 (fp = fdopen(td, "r")) == NULL) {
589 admonish(file, "unable to %s", td != NOTOK ?
597 switch (i = mbx_read(fp, 0, &rp, noisy)) {
609 for (dp = rp; i-- >0; dp++) {
610 if (dp->d_start == start)
612 lseek(fd, (off_t) (++d1.d_id * sizeof(*dp)),
614 if (write(fd, (char *)dp, sizeof(*dp)) != sizeof(*dp)) {
616 admonish(file, "write error");
631 dp->d_size = (long) (size ? size : mbx_size(fd, start, stop));
634 lseek(fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
635 if (write(fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
637 admonish(file, "write error");
645 dp->d_start = (long) last;
646 dp->d_stop = lseek(md, (off_t) 0, SEEK_CUR);
648 lseek(fd, (off_t) 0, SEEK_SET);
649 if (write(fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
651 admonish(file, "write error");
663 map_open(char *file, int md)
668 mode = fstat(md, &st) != NOTOK ?
669 (mode_t) (st.st_mode & 0777) : m_gmprot();
670 return mbx_open(file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
675 map_chk(char *file, int fd, struct drop *dp, long pos, int noisy)
679 register struct drop *dl;
681 if (read(fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
683 admonish(NULL, "%s: missing or partial index", file);
688 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
690 dp->d_id = ntohl(tmpd.d_id);
691 dp->d_size = ntohl(tmpd.d_size);
692 dp->d_start = ntohl(tmpd.d_start);
693 dp->d_stop = ntohl(tmpd.d_stop);
696 if (dp->d_size != DRVRSN) {
698 admonish(NULL, "%s: version mismatch (%d != %d)",
699 file, dp->d_size, DRVRSN);
703 if (dp->d_stop != pos) {
704 if (noisy && pos != (long) 0)
705 admonish(NULL, "%s: pointer mismatch or incomplete index (%ld!=%ld)", 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);