]>
diplodocus.org Git - nmh/blob - uip/dropsbr.c
2274137fb42da9cdc863dade32059defb467e044
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>
18 # include <netinet/in.h>
29 static int mbx_chk_mbox (int);
30 static int mbx_chk_mmdf (int);
31 static int map_open (char *, int);
35 * Main entry point to open/create and lock
40 mbx_open (char *file
, int mbx_style
, uid_t uid
, gid_t gid
, mode_t mode
)
42 int j
, count
, fd
= NOTOK
;
47 /* attempt to open and lock file */
48 for (count
= 4; count
> 0; count
--) {
49 int failed_to_lock
= 0;
50 if ((fd
= lkopenspool (file
, O_RDWR
| O_CREAT
|
51 O_NONBLOCK
, mode
, &failed_to_lock
)) == NOTOK
) {
61 /* good file descriptor */
68 * Return if we still failed after 4 attempts,
69 * or we just want to skip the sanity checks.
71 if (fd
== NOTOK
|| mbx_style
== OTHER_FORMAT
)
75 * Do sanity checks on maildrop.
77 if (fstat (fd
, &st
) == NOTOK
) {
79 * The stat failed. So we make sure file
80 * has right ownership/modes
82 if (chown (file
, uid
, gid
) < 0) {
83 advise (file
, "chown");
85 if (chmod (file
, mode
) < 0) {
86 advise (file
, "chmod");
88 } else if (st
.st_size
> (off_t
) 0) {
91 /* check the maildrop */
95 status
= mbx_chk_mmdf (fd
);
99 status
= mbx_chk_mbox (fd
);
103 /* if error, attempt to close it */
104 if (status
== NOTOK
) {
115 * Check/prepare MBOX style maildrop for appending.
119 mbx_chk_mbox (int fd
)
121 /* just seek to the end */
122 if (lseek (fd
, (off_t
) 0, SEEK_END
) == (off_t
) NOTOK
)
130 * Check/prepare MMDF style maildrop for appending.
134 mbx_chk_mmdf (int fd
)
139 count
= strlen (mmdlm2
);
141 if (lseek (fd
, -count
, SEEK_END
) == (off_t
) NOTOK
)
143 if (read (fd
, ldelim
, count
) != count
)
148 if (strcmp (ldelim
, mmdlm2
)
149 && write (fd
, "\n", 1) != 1
150 && write (fd
, mmdlm2
, count
) != count
)
158 mbx_read (FILE *fp
, long pos
, struct drop
**drops
)
164 struct drop
*cp
, *dp
, *ep
, *pp
;
167 pp
= mh_xcalloc(len
, sizeof *pp
);
169 ld1
= (long) strlen (mmdlm1
);
170 ld2
= (long) strlen (mmdlm2
);
172 fseek (fp
, pos
, SEEK_SET
);
173 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
175 if (strcmp (buffer
, mmdlm1
) == 0)
176 pos
+= ld1
, dp
->d_start
= (long) pos
;
178 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
179 for (bp
= buffer
; *bp
; bp
++, size
++)
184 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
185 if (strcmp (buffer
, mmdlm2
) == 0)
188 pos
+= (long) strlen (buffer
);
189 for (bp
= buffer
; *bp
; bp
++, size
++)
194 if (dp
->d_start
!= (long) pos
) {
196 dp
->d_size
= (long) size
;
203 int curlen
= dp
- pp
;
205 cp
= (struct drop
*) mh_xrealloc ((char *) pp
,
206 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
207 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
220 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
221 long pos
, off_t stop
, int mapping
, int noisy
)
229 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
231 if (write (md
, mmdlm1
, j
) != j
)
233 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
236 fseek (fp
, pos
, SEEK_SET
);
237 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
239 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
241 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
243 if (write (md
, buffer
, i
) != i
)
247 for (cp
= buffer
; i
-- > 0; size
++)
252 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
254 if (write (md
, mmdlm2
, j
) != j
)
257 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
264 * Append message to end of file or maildrop.
268 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
269 int mapping
, char *text
, int noisy
)
274 char *cp
, buffer
[BUFSIZ
+ 1]; /* Space for NUL. */
277 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
284 if (write (md
, mmdlm1
, j
) != j
)
286 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
290 if (write (md
, text
, i
) != i
)
292 for (cp
= text
; *cp
++; size
++)
297 while ((i
= read (fd
, buffer
, sizeof buffer
- 1)) > 0) {
298 buffer
[i
] = '\0'; /* Terminate for stringdex(). */
300 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
302 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
304 if (write (md
, buffer
, i
) != i
)
307 for (cp
= buffer
; i
-- > 0; size
++)
312 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
314 if (write (md
, mmdlm2
, j
) != j
)
317 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
319 return (i
!= NOTOK
? OK
: NOTOK
);
322 if ((j
= dup (fd
)) == NOTOK
)
324 if ((fp
= fdopen (j
, "r")) == NULL
) {
328 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
330 /* If text is given, we add it to top of message */
333 if (write (md
, text
, i
) != i
)
335 for (cp
= text
; *cp
++; size
++)
340 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
343 * Check the first line, and make some changes.
345 if (j
== 0 && !text
) {
347 * Change the "Return-Path:" field (if in first line)
350 if (has_prefix(buffer
, "Return-Path:")) {
351 char tmpbuffer
[sizeof buffer
];
354 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
356 if (!(fp
= strchr(ep
+ 1, ' ')))
357 fp
= strchr(ep
+ 1, '\n');
358 tp
= dctime(dlocaltimenow());
359 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
360 (int)(fp
- ep
), ep
, tp
);
361 } else if (has_prefix(buffer
, "X-Envelope-From:")) {
363 * Change the "X-Envelope-From:" field
364 * (if first line) back to "From ".
366 char tmpbuffer
[sizeof buffer
];
369 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
371 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
372 } else if (!has_prefix(buffer
, "From ")) {
374 * If there is already a "From " line,
375 * then leave it alone. Else we add one.
377 char tmpbuffer
[sizeof buffer
];
380 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
381 ep
= "nobody@nowhere";
382 tp
= dctime(dlocaltimenow());
383 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
388 * If this is not first line, and begins with
389 * "From ", then prepend line with ">".
391 if (j
!= 0 && has_prefix(buffer
, "From ")) {
392 if (write (md
, ">", 1) < 0) {
393 advise (mailbox
, "write");
398 if (write (md
, buffer
, i
) != i
) {
403 for (cp
= buffer
; i
-- > 0; size
++)
407 if (write (md
, "\n", 1) != 1) {
415 lseek (fd
, (off_t
) 0, SEEK_END
);
416 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
418 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
426 mbx_size (int md
, off_t start
, off_t stop
)
432 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
438 fseek (fp
, start
, SEEK_SET
);
439 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
440 if (fgetc (fp
) == '\n')
449 * Close and unlock file/maildrop.
453 mbx_close (char *mailbox
, int md
)
455 if (lkclosespool (md
, mailbox
) == 0)
462 * This function is performed implicitly by getbbent.c:
463 * bb->bb_map = map_name (bb->bb_file);
467 map_name (char *file
)
470 static char buffer
[BUFSIZ
];
472 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
473 dp
= cp
+ strlen (cp
);
475 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
477 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
478 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
485 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
490 struct drop
*mp
, *dp
;
492 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
493 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
500 dp
= mh_xcalloc(msgp
+ 1, sizeof *dp
);
501 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
503 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
504 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
513 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
514 tdp
->d_id
= ntohl(tdp
->d_id
);
515 tdp
->d_size
= ntohl(tdp
->d_size
);
516 tdp
->d_start
= ntohl(tdp
->d_start
);
517 tdp
->d_stop
= ntohl(tdp
->d_stop
);
525 return (i
/ sizeof(*dp
));
530 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
531 off_t stop
, long pos
, int size
, int noisy
)
537 struct drop d1
, d2
, *rp
;
541 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
544 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
549 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
550 (void) m_unlink (file
);
551 mbx_close (file
, fd
);
552 if ((fd
= map_open (file
, md
)) == NOTOK
)
558 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
560 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
563 mbx_close (file
, fd
);
567 switch (i
= mbx_read (fp
, 0, &rp
)) {
570 mbx_close (file
, fd
);
579 for (dp
= rp
; i
-- >0; dp
++) {
580 if (dp
->d_start
== start
)
582 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
583 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
585 admonish (file
, "write error");
586 mbx_close (file
, fd
);
601 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
604 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
605 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
607 admonish (file
, "write error");
608 mbx_close (file
, fd
);
615 dp
->d_start
= (long) last
;
616 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
618 lseek (fd
, (off_t
) 0, SEEK_SET
);
619 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
621 admonish (file
, "write error");
622 mbx_close (file
, fd
);
626 mbx_close (file
, fd
);
633 map_open (char *file
, int md
)
638 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
639 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
644 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
650 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
652 inform("%s: missing or partial index, continuing...", file
);
657 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
659 dp
->d_id
= ntohl(tmpd
.d_id
);
660 dp
->d_size
= ntohl(tmpd
.d_size
);
661 dp
->d_start
= ntohl(tmpd
.d_start
);
662 dp
->d_stop
= ntohl(tmpd
.d_stop
);
665 if (dp
->d_size
!= DRVRSN
) {
667 inform("%s: version mismatch (%d != %d), continuing...", file
,
672 if (dp
->d_stop
!= pos
) {
673 if (noisy
&& pos
!= (long) 0)
674 inform("%s: pointer mismatch or incomplete index (%ld!=%ld), "
675 "continuing...", file
, dp
->d_stop
, (long) pos
);
679 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
681 inform("%s: corrupt index(1), continuing...", file
);
686 count
= strlen (mmdlm2
);
687 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
688 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
689 || (ntohl(dl
->d_stop
) != dp
->d_stop
690 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
692 inform("%s: corrupt index(2), continuing...", file
);