]>
diplodocus.org Git - nmh/blob - uip/dropsbr.c
1 /* dropsbr.c -- create/read/manipulate mail drops
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
12 #include <h/dropsbr.h>
15 #include "../sbr/lock_file.h"
16 #include "../sbr/m_mktemp.h"
19 # include <netinet/in.h>
30 static int mbx_chk_mbox (int);
31 static int mbx_chk_mmdf (int);
32 static int map_open (char *, int);
36 * Main entry point to open/create and lock
41 mbx_open (char *file
, int mbx_style
, uid_t uid
, gid_t gid
, mode_t mode
)
43 int j
, count
, fd
= NOTOK
;
48 /* attempt to open and lock file */
49 for (count
= 4; count
> 0; count
--) {
50 int failed_to_lock
= 0;
52 if ((fd
= lkopenspool (file
, O_RDWR
| O_CREAT
| O_NONBLOCK
,
53 mode
, &failed_to_lock
)) != NOTOK
)
66 * Return if we still failed after 4 attempts,
67 * or we just want to skip the sanity checks.
69 if (fd
== NOTOK
|| mbx_style
== OTHER_FORMAT
)
73 * Do sanity checks on maildrop.
75 if (fstat (fd
, &st
) == NOTOK
) {
77 * The stat failed. So we make sure file
78 * has right ownership/modes
80 if (chown (file
, uid
, gid
) < 0) {
81 advise (file
, "chown");
83 if (chmod (file
, mode
) < 0) {
84 advise (file
, "chmod");
86 } else if (st
.st_size
> (off_t
) 0) {
89 /* check the maildrop */
93 status
= mbx_chk_mmdf (fd
);
97 status
= mbx_chk_mbox (fd
);
101 /* if error, attempt to close it */
102 if (status
== NOTOK
) {
113 * Check/prepare MBOX style maildrop for appending.
117 mbx_chk_mbox (int fd
)
119 /* just seek to the end */
120 if (lseek (fd
, (off_t
) 0, SEEK_END
) == (off_t
) NOTOK
)
128 * Check/prepare MMDF style maildrop for appending.
132 mbx_chk_mmdf (int fd
)
137 count
= strlen (mmdlm2
);
139 if (lseek (fd
, -count
, SEEK_END
) == (off_t
) NOTOK
)
141 if (read (fd
, ldelim
, count
) != count
)
146 if (strcmp (ldelim
, mmdlm2
)
147 && write (fd
, "\n", 1) != 1
148 && write (fd
, mmdlm2
, count
) != count
)
156 mbx_read (FILE *fp
, long pos
, struct drop
**drops
)
162 struct drop
*cp
, *dp
, *ep
, *pp
;
165 pp
= mh_xcalloc(len
, sizeof *pp
);
167 ld1
= (long) strlen (mmdlm1
);
168 ld2
= (long) strlen (mmdlm2
);
170 fseek (fp
, pos
, SEEK_SET
);
171 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
173 if (strcmp (buffer
, mmdlm1
) == 0)
174 pos
+= ld1
, dp
->d_start
= (long) pos
;
176 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
177 for (bp
= buffer
; *bp
; bp
++, size
++)
182 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
183 if (strcmp (buffer
, mmdlm2
) == 0)
186 pos
+= (long) strlen (buffer
);
187 for (bp
= buffer
; *bp
; bp
++, size
++)
192 if (dp
->d_start
!= (long) pos
) {
194 dp
->d_size
= (long) size
;
201 int curlen
= dp
- pp
;
203 cp
= (struct drop
*) mh_xrealloc ((char *) pp
,
204 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
205 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
218 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
219 long pos
, off_t stop
, int mapping
, int noisy
)
227 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
229 if (write (md
, mmdlm1
, j
) != j
)
231 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
234 fseek (fp
, pos
, SEEK_SET
);
235 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
237 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
239 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
241 if (write (md
, buffer
, i
) != i
)
245 for (cp
= buffer
; i
-- > 0; size
++)
250 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
252 if (write (md
, mmdlm2
, j
) != j
)
255 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
262 * Append message to end of file or maildrop.
266 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
267 int mapping
, char *text
, int noisy
)
272 char *cp
, buffer
[BUFSIZ
+ 1]; /* Space for NUL. */
275 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
282 if (write (md
, mmdlm1
, j
) != j
)
284 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
288 if (write (md
, text
, i
) != i
)
290 for (cp
= text
; *cp
++; size
++)
295 while ((i
= read (fd
, buffer
, sizeof buffer
- 1)) > 0) {
296 buffer
[i
] = '\0'; /* Terminate for stringdex(). */
298 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
300 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
302 if (write (md
, buffer
, i
) != i
)
305 for (cp
= buffer
; i
-- > 0; size
++)
310 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
312 if (write (md
, mmdlm2
, j
) != j
)
315 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
317 return (i
!= NOTOK
? OK
: NOTOK
);
320 if ((j
= dup (fd
)) == NOTOK
)
322 if ((fp
= fdopen (j
, "r")) == NULL
) {
326 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
328 /* If text is given, we add it to top of message */
331 if (write (md
, text
, i
) != i
)
333 for (cp
= text
; *cp
++; size
++)
338 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
341 * Check the first line, and make some changes.
343 if (j
== 0 && !text
) {
345 * Change the "Return-Path:" field (if in first line)
348 if (has_prefix(buffer
, "Return-Path:")) {
349 char tmpbuffer
[sizeof buffer
];
352 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
354 if (!(fp
= strchr(ep
+ 1, ' ')))
355 fp
= strchr(ep
+ 1, '\n');
356 tp
= dctime(dlocaltimenow());
357 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
358 (int)(fp
- ep
), ep
, tp
);
359 } else if (has_prefix(buffer
, "X-Envelope-From:")) {
361 * Change the "X-Envelope-From:" field
362 * (if first line) back to "From ".
364 char tmpbuffer
[sizeof buffer
];
367 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
369 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
370 } else if (!has_prefix(buffer
, "From ")) {
372 * If there is already a "From " line,
373 * then leave it alone. Else we add one.
375 char tmpbuffer
[sizeof buffer
];
378 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
379 ep
= "nobody@nowhere";
380 tp
= dctime(dlocaltimenow());
381 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
386 * If this is not first line, and begins with
387 * "From ", then prepend line with ">".
389 if (j
!= 0 && has_prefix(buffer
, "From ")) {
390 if (write (md
, ">", 1) < 0) {
391 advise (mailbox
, "write");
396 if (write (md
, buffer
, i
) != i
) {
401 for (cp
= buffer
; i
-- > 0; size
++)
405 if (write (md
, "\n", 1) != 1) {
413 lseek (fd
, (off_t
) 0, SEEK_END
);
414 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
416 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
424 mbx_size (int md
, off_t start
, off_t stop
)
430 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
436 fseek (fp
, start
, SEEK_SET
);
437 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
438 if (fgetc (fp
) == '\n')
447 * Close and unlock file/maildrop.
451 mbx_close (char *mailbox
, int md
)
453 if (lkclosespool (md
, mailbox
) == 0)
460 * This function is performed implicitly by getbbent.c:
461 * bb->bb_map = map_name (bb->bb_file);
465 map_name (char *file
)
468 static char buffer
[BUFSIZ
];
470 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
471 dp
= cp
+ strlen (cp
);
473 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
475 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
476 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
483 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
488 struct drop
*mp
, *dp
;
490 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
491 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
498 dp
= mh_xcalloc(msgp
+ 1, sizeof *dp
);
499 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
501 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
502 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
511 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
512 tdp
->d_id
= ntohl(tdp
->d_id
);
513 tdp
->d_size
= ntohl(tdp
->d_size
);
514 tdp
->d_start
= ntohl(tdp
->d_start
);
515 tdp
->d_stop
= ntohl(tdp
->d_stop
);
523 return (i
/ sizeof(*dp
));
528 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
529 off_t stop
, long pos
, int size
, int noisy
)
535 struct drop d1
, d2
, *rp
;
539 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
542 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
547 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
548 (void) m_unlink (file
);
549 mbx_close (file
, fd
);
550 if ((fd
= map_open (file
, md
)) == NOTOK
)
556 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
558 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
561 mbx_close (file
, fd
);
565 switch (i
= mbx_read (fp
, 0, &rp
)) {
568 mbx_close (file
, fd
);
577 for (dp
= rp
; i
-- >0; dp
++) {
578 if (dp
->d_start
== start
)
580 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
581 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
583 admonish (file
, "write error");
584 mbx_close (file
, fd
);
599 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
602 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
603 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
605 admonish (file
, "write error");
606 mbx_close (file
, fd
);
613 dp
->d_start
= (long) last
;
614 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
616 lseek (fd
, (off_t
) 0, SEEK_SET
);
617 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
619 admonish (file
, "write error");
620 mbx_close (file
, fd
);
624 mbx_close (file
, fd
);
631 map_open (char *file
, int md
)
636 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
637 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
642 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
648 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
650 inform("%s: missing or partial index, continuing...", file
);
655 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
657 dp
->d_id
= ntohl(tmpd
.d_id
);
658 dp
->d_size
= ntohl(tmpd
.d_size
);
659 dp
->d_start
= ntohl(tmpd
.d_start
);
660 dp
->d_stop
= ntohl(tmpd
.d_stop
);
663 if (dp
->d_size
!= DRVRSN
) {
665 inform("%s: version mismatch (%d != %d), continuing...", file
,
670 if (dp
->d_stop
!= pos
) {
671 if (noisy
&& pos
!= (long) 0)
672 inform("%s: pointer mismatch or incomplete index (%ld!=%ld), "
673 "continuing...", file
, dp
->d_stop
, (long) pos
);
677 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
679 inform("%s: corrupt index(1), continuing...", file
);
684 count
= strlen (mmdlm2
);
685 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
686 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
687 || (ntohl(dl
->d_stop
) != dp
->d_stop
688 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
690 inform("%s: corrupt index(2), continuing...", file
);