]>
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>
17 # include <netinet/in.h>
28 static int mbx_chk_mbox (int);
29 static int mbx_chk_mmdf (int);
30 static int map_open (char *, int);
34 * Main entry point to open/create and lock
39 mbx_open (char *file
, int mbx_style
, uid_t uid
, gid_t gid
, mode_t mode
)
41 int j
, count
, fd
= NOTOK
;
46 /* attempt to open and lock file */
47 for (count
= 4; count
> 0; count
--) {
48 int failed_to_lock
= 0;
50 if ((fd
= lkopenspool (file
, O_RDWR
| O_CREAT
| O_NONBLOCK
,
51 mode
, &failed_to_lock
)) != NOTOK
)
64 * Return if we still failed after 4 attempts,
65 * or we just want to skip the sanity checks.
67 if (fd
== NOTOK
|| mbx_style
== OTHER_FORMAT
)
71 * Do sanity checks on maildrop.
73 if (fstat (fd
, &st
) == NOTOK
) {
75 * The stat failed. So we make sure file
76 * has right ownership/modes
78 if (chown (file
, uid
, gid
) < 0) {
79 advise (file
, "chown");
81 if (chmod (file
, mode
) < 0) {
82 advise (file
, "chmod");
84 } else if (st
.st_size
> (off_t
) 0) {
87 /* check the maildrop */
91 status
= mbx_chk_mmdf (fd
);
95 status
= mbx_chk_mbox (fd
);
99 /* if error, attempt to close it */
100 if (status
== NOTOK
) {
111 * Check/prepare MBOX style maildrop for appending.
115 mbx_chk_mbox (int fd
)
117 /* just seek to the end */
118 if (lseek (fd
, (off_t
) 0, SEEK_END
) == (off_t
) NOTOK
)
126 * Check/prepare MMDF style maildrop for appending.
130 mbx_chk_mmdf (int fd
)
135 count
= strlen (mmdlm2
);
137 if (lseek (fd
, -count
, SEEK_END
) == (off_t
) NOTOK
)
139 if (read (fd
, ldelim
, count
) != count
)
144 if (strcmp (ldelim
, mmdlm2
)
145 && write (fd
, "\n", 1) != 1
146 && write (fd
, mmdlm2
, count
) != count
)
154 mbx_read (FILE *fp
, long pos
, struct drop
**drops
)
160 struct drop
*cp
, *dp
, *ep
, *pp
;
163 pp
= mh_xcalloc(len
, sizeof *pp
);
165 ld1
= (long) strlen (mmdlm1
);
166 ld2
= (long) strlen (mmdlm2
);
168 fseek (fp
, pos
, SEEK_SET
);
169 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
171 if (strcmp (buffer
, mmdlm1
) == 0)
172 pos
+= ld1
, dp
->d_start
= (long) pos
;
174 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
175 for (bp
= buffer
; *bp
; bp
++, size
++)
180 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
181 if (strcmp (buffer
, mmdlm2
) == 0)
184 pos
+= (long) strlen (buffer
);
185 for (bp
= buffer
; *bp
; bp
++, size
++)
190 if (dp
->d_start
!= (long) pos
) {
192 dp
->d_size
= (long) size
;
199 int curlen
= dp
- pp
;
201 cp
= (struct drop
*) mh_xrealloc ((char *) pp
,
202 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
203 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
216 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
217 long pos
, off_t stop
, int mapping
, int noisy
)
225 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
227 if (write (md
, mmdlm1
, j
) != j
)
229 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
232 fseek (fp
, pos
, SEEK_SET
);
233 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
235 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
237 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
239 if (write (md
, buffer
, i
) != i
)
243 for (cp
= buffer
; i
-- > 0; size
++)
248 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
250 if (write (md
, mmdlm2
, j
) != j
)
253 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
260 * Append message to end of file or maildrop.
264 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
265 int mapping
, char *text
, int noisy
)
270 char *cp
, buffer
[BUFSIZ
+ 1]; /* Space for NUL. */
273 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
280 if (write (md
, mmdlm1
, j
) != j
)
282 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
286 if (write (md
, text
, i
) != i
)
288 for (cp
= text
; *cp
++; size
++)
293 while ((i
= read (fd
, buffer
, sizeof buffer
- 1)) > 0) {
294 buffer
[i
] = '\0'; /* Terminate for stringdex(). */
296 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
298 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
300 if (write (md
, buffer
, i
) != i
)
303 for (cp
= buffer
; i
-- > 0; size
++)
308 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
310 if (write (md
, mmdlm2
, j
) != j
)
313 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
315 return (i
!= NOTOK
? OK
: NOTOK
);
318 if ((j
= dup (fd
)) == NOTOK
)
320 if ((fp
= fdopen (j
, "r")) == NULL
) {
324 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
326 /* If text is given, we add it to top of message */
329 if (write (md
, text
, i
) != i
)
331 for (cp
= text
; *cp
++; size
++)
336 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
339 * Check the first line, and make some changes.
341 if (j
== 0 && !text
) {
343 * Change the "Return-Path:" field (if in first line)
346 if (has_prefix(buffer
, "Return-Path:")) {
347 char tmpbuffer
[sizeof buffer
];
350 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
352 if (!(fp
= strchr(ep
+ 1, ' ')))
353 fp
= strchr(ep
+ 1, '\n');
354 tp
= dctime(dlocaltimenow());
355 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
356 (int)(fp
- ep
), ep
, tp
);
357 } else if (has_prefix(buffer
, "X-Envelope-From:")) {
359 * Change the "X-Envelope-From:" field
360 * (if first line) back to "From ".
362 char tmpbuffer
[sizeof buffer
];
365 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
367 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
368 } else if (!has_prefix(buffer
, "From ")) {
370 * If there is already a "From " line,
371 * then leave it alone. Else we add one.
373 char tmpbuffer
[sizeof buffer
];
376 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
377 ep
= "nobody@nowhere";
378 tp
= dctime(dlocaltimenow());
379 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
384 * If this is not first line, and begins with
385 * "From ", then prepend line with ">".
387 if (j
!= 0 && has_prefix(buffer
, "From ")) {
388 if (write (md
, ">", 1) < 0) {
389 advise (mailbox
, "write");
394 if (write (md
, buffer
, i
) != i
) {
399 for (cp
= buffer
; i
-- > 0; size
++)
403 if (write (md
, "\n", 1) != 1) {
411 lseek (fd
, (off_t
) 0, SEEK_END
);
412 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
414 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
422 mbx_size (int md
, off_t start
, off_t stop
)
428 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
434 fseek (fp
, start
, SEEK_SET
);
435 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
436 if (fgetc (fp
) == '\n')
445 * Close and unlock file/maildrop.
449 mbx_close (char *mailbox
, int md
)
451 if (lkclosespool (md
, mailbox
) == 0)
458 * This function is performed implicitly by getbbent.c:
459 * bb->bb_map = map_name (bb->bb_file);
463 map_name (char *file
)
466 static char buffer
[BUFSIZ
];
468 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
469 dp
= cp
+ strlen (cp
);
471 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
473 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
474 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
481 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
486 struct drop
*mp
, *dp
;
488 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
489 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
496 dp
= mh_xcalloc(msgp
+ 1, sizeof *dp
);
497 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
499 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
500 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
509 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
510 tdp
->d_id
= ntohl(tdp
->d_id
);
511 tdp
->d_size
= ntohl(tdp
->d_size
);
512 tdp
->d_start
= ntohl(tdp
->d_start
);
513 tdp
->d_stop
= ntohl(tdp
->d_stop
);
521 return (i
/ sizeof(*dp
));
526 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
527 off_t stop
, long pos
, int size
, int noisy
)
533 struct drop d1
, d2
, *rp
;
537 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
540 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
545 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
546 (void) m_unlink (file
);
547 mbx_close (file
, fd
);
548 if ((fd
= map_open (file
, md
)) == NOTOK
)
554 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
556 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
559 mbx_close (file
, fd
);
563 switch (i
= mbx_read (fp
, 0, &rp
)) {
566 mbx_close (file
, fd
);
575 for (dp
= rp
; i
-- >0; dp
++) {
576 if (dp
->d_start
== start
)
578 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
579 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
581 admonish (file
, "write error");
582 mbx_close (file
, fd
);
597 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
600 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
601 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
603 admonish (file
, "write error");
604 mbx_close (file
, fd
);
611 dp
->d_start
= (long) last
;
612 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
614 lseek (fd
, (off_t
) 0, SEEK_SET
);
615 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
617 admonish (file
, "write error");
618 mbx_close (file
, fd
);
622 mbx_close (file
, fd
);
629 map_open (char *file
, int md
)
634 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
635 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
640 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
646 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
648 inform("%s: missing or partial index, continuing...", file
);
653 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
655 dp
->d_id
= ntohl(tmpd
.d_id
);
656 dp
->d_size
= ntohl(tmpd
.d_size
);
657 dp
->d_start
= ntohl(tmpd
.d_start
);
658 dp
->d_stop
= ntohl(tmpd
.d_stop
);
661 if (dp
->d_size
!= DRVRSN
) {
663 inform("%s: version mismatch (%d != %d), continuing...", file
,
668 if (dp
->d_stop
!= pos
) {
669 if (noisy
&& pos
!= (long) 0)
670 inform("%s: pointer mismatch or incomplete index (%ld!=%ld), "
671 "continuing...", file
, dp
->d_stop
, (long) pos
);
675 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
677 inform("%s: corrupt index(1), continuing...", file
);
682 count
= strlen (mmdlm2
);
683 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
684 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
685 || (ntohl(dl
->d_stop
) != dp
->d_stop
686 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
688 inform("%s: corrupt index(2), continuing...", file
);