]>
diplodocus.org Git - nmh/blob - uip/dropsbr.c
3 * dropsbr.c -- create/read/manipulate mail drops
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
14 #include <h/dropsbr.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;
51 if ((fd
= lkopenspool (file
, O_RDWR
| O_CREAT
|
52 O_NONBLOCK
, mode
, &failed_to_lock
)) == NOTOK
) {
62 /* good file descriptor */
69 * Return if we still failed after 4 attempts,
70 * or we just want to skip the sanity checks.
72 if (fd
== NOTOK
|| mbx_style
== OTHER_FORMAT
)
76 * Do sanity checks on maildrop.
78 if (fstat (fd
, &st
) == NOTOK
) {
80 * The stat failed. So we make sure file
81 * has right ownership/modes
83 if (chown (file
, uid
, gid
) < 0) {
84 advise (file
, "chown");
86 if (chmod (file
, mode
) < 0) {
87 advise (file
, "chmod");
89 } else if (st
.st_size
> (off_t
) 0) {
92 /* check the maildrop */
96 status
= mbx_chk_mmdf (fd
);
100 status
= mbx_chk_mbox (fd
);
104 /* if error, attempt to close it */
105 if (status
== NOTOK
) {
116 * Check/prepare MBOX style maildrop for appending.
120 mbx_chk_mbox (int fd
)
122 /* just seek to the end */
123 if (lseek (fd
, (off_t
) 0, SEEK_END
) == (off_t
) NOTOK
)
131 * Check/prepare MMDF style maildrop for appending.
135 mbx_chk_mmdf (int fd
)
140 count
= strlen (mmdlm2
);
142 if (lseek (fd
, -count
, SEEK_END
) == (off_t
) NOTOK
)
144 if (read (fd
, ldelim
, count
) != count
)
149 if (strcmp (ldelim
, mmdlm2
)
150 && write (fd
, "\n", 1) != 1
151 && write (fd
, mmdlm2
, count
) != count
)
159 mbx_read (FILE *fp
, long pos
, struct drop
**drops
)
165 struct drop
*cp
, *dp
, *ep
, *pp
;
168 pp
= mh_xcalloc(len
, sizeof *pp
);
170 ld1
= (long) strlen (mmdlm1
);
171 ld2
= (long) strlen (mmdlm2
);
173 fseek (fp
, pos
, SEEK_SET
);
174 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
176 if (strcmp (buffer
, mmdlm1
) == 0)
177 pos
+= ld1
, dp
->d_start
= (long) pos
;
179 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
180 for (bp
= buffer
; *bp
; bp
++, size
++)
185 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
186 if (strcmp (buffer
, mmdlm2
) == 0)
189 pos
+= (long) strlen (buffer
);
190 for (bp
= buffer
; *bp
; bp
++, size
++)
195 if (dp
->d_start
!= (long) pos
) {
197 dp
->d_size
= (long) size
;
204 int curlen
= dp
- pp
;
206 cp
= (struct drop
*) mh_xrealloc ((char *) pp
,
207 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
208 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
221 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
222 long pos
, off_t stop
, int mapping
, int noisy
)
230 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
232 if (write (md
, mmdlm1
, j
) != j
)
234 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
237 fseek (fp
, pos
, SEEK_SET
);
238 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
240 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
242 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
244 if (write (md
, buffer
, i
) != i
)
248 for (cp
= buffer
; i
-- > 0; size
++)
253 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
255 if (write (md
, mmdlm2
, j
) != j
)
258 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
265 * Append message to end of file or maildrop.
269 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
270 int mapping
, char *text
, int noisy
)
275 char *cp
, buffer
[BUFSIZ
];
278 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
285 if (write (md
, mmdlm1
, j
) != j
)
287 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
291 if (write (md
, text
, i
) != i
)
293 for (cp
= text
; *cp
++; size
++)
298 while ((i
= read (fd
, buffer
, sizeof(buffer
))) > 0) {
299 /* valgrind noticed that stringdex depends on null
303 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
305 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
307 if (write (md
, buffer
, i
) != i
)
310 for (cp
= buffer
; i
-- > 0; size
++)
315 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
317 if (write (md
, mmdlm2
, j
) != j
)
320 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
322 return (i
!= NOTOK
? OK
: NOTOK
);
325 if ((j
= dup (fd
)) == NOTOK
)
327 if ((fp
= fdopen (j
, "r")) == NULL
) {
331 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
333 /* If text is given, we add it to top of message */
336 if (write (md
, text
, i
) != i
)
338 for (cp
= text
; *cp
++; size
++)
343 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
346 * Check the first line, and make some changes.
348 if (j
== 0 && !text
) {
350 * Change the "Return-Path:" field (if in first line)
353 if (has_prefix(buffer
, "Return-Path:")) {
354 char tmpbuffer
[BUFSIZ
];
357 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
359 if (!(fp
= strchr(ep
+ 1, ' ')))
360 fp
= strchr(ep
+ 1, '\n');
361 tp
= dctime(dlocaltimenow());
362 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
363 (int)(fp
- ep
), ep
, tp
);
364 } else if (has_prefix(buffer
, "X-Envelope-From:")) {
366 * Change the "X-Envelope-From:" field
367 * (if first line) back to "From ".
369 char tmpbuffer
[BUFSIZ
];
372 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
374 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
375 } else if (!has_prefix(buffer
, "From ")) {
377 * If there is already a "From " line,
378 * then leave it alone. Else we add one.
380 char tmpbuffer
[BUFSIZ
];
383 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
384 ep
= "nobody@nowhere";
385 tp
= dctime(dlocaltimenow());
386 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
391 * If this is not first line, and begins with
392 * "From ", then prepend line with ">".
394 if (j
!= 0 && has_prefix(buffer
, "From ")) {
395 if (write (md
, ">", 1) < 0) {
396 advise (mailbox
, "write");
401 if (write (md
, buffer
, i
) != i
) {
406 for (cp
= buffer
; i
-- > 0; size
++)
410 if (write (md
, "\n", 1) != 1) {
418 lseek (fd
, (off_t
) 0, SEEK_END
);
419 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
421 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
429 mbx_size (int md
, off_t start
, off_t stop
)
435 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
441 fseek (fp
, start
, SEEK_SET
);
442 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
443 if (fgetc (fp
) == '\n')
452 * Close and unlock file/maildrop.
456 mbx_close (char *mailbox
, int md
)
458 if (lkclosespool (md
, mailbox
) == 0)
465 * This function is performed implicitly by getbbent.c:
466 * bb->bb_map = map_name (bb->bb_file);
470 map_name (char *file
)
473 static char buffer
[BUFSIZ
];
475 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
476 dp
= cp
+ strlen (cp
);
478 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
480 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
481 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
488 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
493 struct drop
*mp
, *dp
;
495 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
496 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
503 dp
= mh_xcalloc(msgp
+ 1, sizeof *dp
);
504 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
506 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
507 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
516 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
517 tdp
->d_id
= ntohl(tdp
->d_id
);
518 tdp
->d_size
= ntohl(tdp
->d_size
);
519 tdp
->d_start
= ntohl(tdp
->d_start
);
520 tdp
->d_stop
= ntohl(tdp
->d_stop
);
528 return (i
/ sizeof(*dp
));
533 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
534 off_t stop
, long pos
, int size
, int noisy
)
540 struct drop d1
, d2
, *rp
;
544 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
547 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
552 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
553 (void) m_unlink (file
);
554 mbx_close (file
, fd
);
555 if ((fd
= map_open (file
, md
)) == NOTOK
)
561 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
563 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
566 mbx_close (file
, fd
);
570 switch (i
= mbx_read (fp
, 0, &rp
)) {
573 mbx_close (file
, fd
);
582 for (dp
= rp
; i
-- >0; dp
++) {
583 if (dp
->d_start
== start
)
585 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
586 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
588 admonish (file
, "write error");
589 mbx_close (file
, fd
);
604 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
607 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
608 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
610 admonish (file
, "write error");
611 mbx_close (file
, fd
);
618 dp
->d_start
= (long) last
;
619 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
621 lseek (fd
, (off_t
) 0, SEEK_SET
);
622 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
624 admonish (file
, "write error");
625 mbx_close (file
, fd
);
629 mbx_close (file
, fd
);
636 map_open (char *file
, int md
)
641 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
642 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
647 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
653 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
655 admonish (NULL
, "%s: missing or partial index", file
);
660 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
662 dp
->d_id
= ntohl(tmpd
.d_id
);
663 dp
->d_size
= ntohl(tmpd
.d_size
);
664 dp
->d_start
= ntohl(tmpd
.d_start
);
665 dp
->d_stop
= ntohl(tmpd
.d_stop
);
668 if (dp
->d_size
!= DRVRSN
) {
670 admonish (NULL
, "%s: version mismatch (%d != %d)", file
,
675 if (dp
->d_stop
!= pos
) {
676 if (noisy
&& pos
!= (long) 0)
678 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
679 file
, dp
->d_stop
, (long) pos
);
683 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
685 admonish (NULL
, "%s: corrupt index(1)", file
);
690 count
= strlen (mmdlm2
);
691 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
692 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
693 || (ntohl(dl
->d_stop
) != dp
->d_stop
694 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
696 admonish (NULL
, "%s: corrupt index(2)", file
);