]>
diplodocus.org Git - nmh/blob - uip/dropsbr.c
3 * dropsbr.c -- create/read/manipulate mail drops
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
16 # include <h/dropsbr.h>
22 # include "mmdfonly.h"
30 # include <netinet/in.h>
43 static int mbx_chk_mbox (int);
44 static int mbx_chk_mmdf (int);
45 static int map_open (char *, int);
49 * Main entry point to open/create and lock
54 mbx_open (char *file
, int mbx_style
, uid_t uid
, gid_t gid
, mode_t mode
)
61 /* attempt to open and lock file */
62 for (count
= 4; count
> 0; count
--) {
63 if ((fd
= lkopen (file
, O_RDWR
| O_CREAT
| O_NONBLOCK
, mode
)) == NOTOK
) {
65 #if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
79 /* just return error */
84 /* good file descriptor */
91 * Return if we still failed after 4 attempts,
92 * or we just want to skip the sanity checks.
94 if (fd
== NOTOK
|| mbx_style
== OTHER_FORMAT
)
98 * Do sanity checks on maildrop.
100 if (fstat (fd
, &st
) == NOTOK
) {
102 * The stat failed. So we make sure file
103 * has right ownership/modes
105 chown (file
, uid
, gid
);
107 } else if (st
.st_size
> (off_t
) 0) {
110 /* check the maildrop */
114 status
= mbx_chk_mmdf (fd
);
118 status
= mbx_chk_mbox (fd
);
122 /* if error, attempt to close it */
123 if (status
== NOTOK
) {
134 * Check/prepare MBOX style maildrop for appending.
138 mbx_chk_mbox (int fd
)
140 /* just seek to the end */
141 if (lseek (fd
, (off_t
) 0, SEEK_END
) == (off_t
) NOTOK
)
149 * Check/prepare MMDF style maildrop for appending.
153 mbx_chk_mmdf (int fd
)
158 count
= strlen (mmdlm2
);
160 /* casting -count to off_t, seem to break FreeBSD 2.2.6 */
161 if (lseek (fd
, (long) (-count
), SEEK_END
) == (off_t
) NOTOK
)
163 if (read (fd
, ldelim
, count
) != count
)
168 if (strcmp (ldelim
, mmdlm2
)
169 && write (fd
, "\n", 1) != 1
170 && write (fd
, mmdlm2
, count
) != count
)
178 mbx_read (FILE *fp
, long pos
, struct drop
**drops
, int noisy
)
180 register int len
, size
;
181 register long ld1
, ld2
;
184 register struct drop
*cp
, *dp
, *ep
, *pp
;
186 pp
= (struct drop
*) calloc ((size_t) (len
= MAXFOLDER
), sizeof(*dp
));
189 admonish (NULL
, "unable to allocate drop storage");
193 ld1
= (long) strlen (mmdlm1
);
194 ld2
= (long) strlen (mmdlm2
);
196 fseek (fp
, pos
, SEEK_SET
);
197 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
199 if (strcmp (buffer
, mmdlm1
) == 0)
200 pos
+= ld1
, dp
->d_start
= (long) pos
;
202 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
203 for (bp
= buffer
; *bp
; bp
++, size
++)
208 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
209 if (strcmp (buffer
, mmdlm2
) == 0)
212 pos
+= (long) strlen (buffer
);
213 for (bp
= buffer
; *bp
; bp
++, size
++)
218 if (dp
->d_start
!= (long) pos
) {
220 dp
->d_size
= (long) size
;
227 register int curlen
= dp
- pp
;
229 cp
= (struct drop
*) realloc ((char *) pp
,
230 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
233 admonish (NULL
, "unable to allocate drop storage");
237 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
250 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
251 long pos
, off_t stop
, int mapping
, int noisy
)
253 register int i
, j
, size
;
259 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
261 if (write (md
, mmdlm1
, j
) != j
)
263 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
266 fseek (fp
, pos
, SEEK_SET
);
267 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
269 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
271 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
273 if (write (md
, buffer
, i
) != i
)
277 for (cp
= buffer
; i
-- > 0; size
++)
282 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
284 if (write (md
, mmdlm2
, j
) != j
)
287 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
294 * Append message to end of file or maildrop.
298 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
299 int mapping
, char *text
, int noisy
)
304 char *cp
, buffer
[BUFSIZ
];
307 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
314 if (write (md
, mmdlm1
, j
) != j
)
316 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
320 if (write (md
, text
, i
) != i
)
322 for (cp
= text
; *cp
++; size
++)
327 while ((i
= read (fd
, buffer
, sizeof(buffer
))) > 0) {
329 (j
= stringdex (mmdlm1
, buffer
)) >= 0;
333 (j
= stringdex (mmdlm2
, buffer
)) >= 0;
336 if (write (md
, buffer
, i
) != i
)
339 for (cp
= buffer
; i
-- > 0; size
++)
344 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
346 if (write (md
, mmdlm2
, j
) != j
)
349 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
351 return (i
!= NOTOK
? OK
: NOTOK
);
354 if ((j
= dup (fd
)) == NOTOK
)
356 if ((fp
= fdopen (j
, "r")) == NULL
) {
360 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
362 /* If text is given, we add it to top of message */
365 if (write (md
, text
, i
) != i
)
367 for (cp
= text
; *cp
++; size
++)
372 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
375 * Check the first line, and make some changes.
377 if (j
== 0 && !text
) {
379 * Change the "Return-Path:" field (if in first line)
382 if (!strncmp (buffer
, "Return-Path:", 12)) {
383 char tmpbuffer
[BUFSIZ
];
386 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
388 if (!(fp
= strchr(ep
+ 1, ' ')))
389 fp
= strchr(ep
+ 1, '\n');
390 tp
= dctime(dlocaltimenow());
391 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
393 } else if (!strncmp (buffer
, "X-Envelope-From:", 16)) {
395 * Change the "X-Envelope-From:" field
396 * (if first line) back to "From ".
398 char tmpbuffer
[BUFSIZ
];
401 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
403 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
404 } else if (strncmp (buffer
, "From ", 5)) {
406 * If there is already a "From " line,
407 * then leave it alone. Else we add one.
409 char tmpbuffer
[BUFSIZ
];
412 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
413 ep
= "nobody@nowhere";
414 tp
= dctime(dlocaltimenow());
415 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
420 * If this is not first line, and begins with
421 * "From ", then prepend line with ">".
423 if (j
!= 0 && strncmp (buffer
, "From ", 5) == 0) {
428 if (write (md
, buffer
, i
) != i
) {
433 for (cp
= buffer
; i
-- > 0; size
++)
437 if (write (md
, "\n", 1) != 1) {
445 lseek (fd
, (off_t
) 0, SEEK_END
);
446 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
448 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
456 mbx_size (int md
, off_t start
, off_t stop
)
462 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
468 fseek (fp
, start
, SEEK_SET
);
469 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
470 if (fgetc (fp
) == '\n')
479 * Close and unlock file/maildrop.
483 mbx_close (char *mailbox
, int md
)
485 lkclose (md
, mailbox
);
491 * This function is performed implicitly by getbbent.c:
492 * bb->bb_map = map_name (bb->bb_file);
496 map_name (char *file
)
498 register char *cp
, *dp
;
499 static char buffer
[BUFSIZ
];
501 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
502 dp
= cp
+ strlen (cp
);
504 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", dp
- cp
, cp
, ".map");
506 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
507 cp
- file
, file
, dp
- cp
, cp
, ".map");
514 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
516 register int i
, md
, msgp
;
519 register struct drop
*mp
, *dp
;
521 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
522 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
529 dp
= (struct drop
*) calloc ((size_t) (msgp
+ 1), sizeof(*dp
));
535 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
537 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
538 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) < sizeof(*dp
)) {
543 register struct drop
*tdp
;
546 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
547 tdp
->d_id
= ntohl(tdp
->d_id
);
548 tdp
->d_size
= ntohl(tdp
->d_size
);
549 tdp
->d_start
= ntohl(tdp
->d_start
);
550 tdp
->d_stop
= ntohl(tdp
->d_stop
);
558 return (i
/ sizeof(*dp
));
563 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
564 off_t stop
, long pos
, int size
, int noisy
)
569 register struct drop
*dp
;
570 struct drop d1
, d2
, *rp
;
574 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
577 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
582 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
584 mbx_close (file
, fd
);
585 if ((fd
= map_open (file
, md
)) == NOTOK
)
591 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
593 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
596 mbx_close (file
, fd
);
600 switch (i
= mbx_read (fp
, 0, &rp
, noisy
)) {
603 mbx_close (file
, fd
);
612 for (dp
= rp
; i
-- >0; dp
++) {
613 if (dp
->d_start
== start
)
615 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
616 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
618 admonish (file
, "write error");
619 mbx_close (file
, fd
);
634 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
637 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
638 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
640 admonish (file
, "write error");
641 mbx_close (file
, fd
);
648 dp
->d_start
= (long) last
;
649 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
651 lseek (fd
, (off_t
) 0, SEEK_SET
);
652 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
654 admonish (file
, "write error");
655 mbx_close (file
, fd
);
659 mbx_close (file
, fd
);
666 map_open (char *file
, int md
)
671 mode
= fstat (md
, &st
) != NOTOK
? (mode_t
) (st
.st_mode
& 0777) : m_gmprot ();
672 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
677 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
681 register struct drop
*dl
;
683 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
685 admonish (NULL
, "%s: missing or partial index", file
);
690 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
692 dp
->d_id
= ntohl(tmpd
.d_id
);
693 dp
->d_size
= ntohl(tmpd
.d_size
);
694 dp
->d_start
= ntohl(tmpd
.d_start
);
695 dp
->d_stop
= ntohl(tmpd
.d_stop
);
698 if (dp
->d_size
!= DRVRSN
) {
700 admonish (NULL
, "%s: version mismatch (%d != %d)", file
,
705 if (dp
->d_stop
!= pos
) {
706 if (noisy
&& pos
!= (long) 0)
708 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
709 file
, dp
->d_stop
, (long) pos
);
713 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
715 admonish (NULL
, "%s: corrupt index(1)", file
);
720 count
= (long) strlen (mmdlm2
);
721 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
722 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
723 || (ntohl(dl
->d_stop
) != dp
->d_stop
724 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
726 admonish (NULL
, "%s: corrupt index(2)", file
);