]>
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
, int noisy
)
161 register int len
, size
;
162 register long ld1
, ld2
;
165 register struct drop
*cp
, *dp
, *ep
, *pp
;
167 pp
= (struct drop
*) mh_xcalloc ((size_t) (len
= MAXFOLDER
), sizeof(*dp
));
170 admonish (NULL
, "unable to allocate drop storage");
174 ld1
= (long) strlen (mmdlm1
);
175 ld2
= (long) strlen (mmdlm2
);
177 fseek (fp
, pos
, SEEK_SET
);
178 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
180 if (strcmp (buffer
, mmdlm1
) == 0)
181 pos
+= ld1
, dp
->d_start
= (long) pos
;
183 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
184 for (bp
= buffer
; *bp
; bp
++, size
++)
189 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
190 if (strcmp (buffer
, mmdlm2
) == 0)
193 pos
+= (long) strlen (buffer
);
194 for (bp
= buffer
; *bp
; bp
++, size
++)
199 if (dp
->d_start
!= (long) pos
) {
201 dp
->d_size
= (long) size
;
208 register int curlen
= dp
- pp
;
210 cp
= (struct drop
*) mh_xrealloc ((char *) pp
,
211 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
212 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
225 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
226 long pos
, off_t stop
, int mapping
, int noisy
)
228 register int i
, j
, size
;
234 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
236 if (write (md
, mmdlm1
, j
) != j
)
238 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
241 fseek (fp
, pos
, SEEK_SET
);
242 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
244 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
246 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
248 if (write (md
, buffer
, i
) != i
)
252 for (cp
= buffer
; i
-- > 0; size
++)
257 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
259 if (write (md
, mmdlm2
, j
) != j
)
262 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
269 * Append message to end of file or maildrop.
273 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
274 int mapping
, char *text
, int noisy
)
279 char *cp
, buffer
[BUFSIZ
];
282 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
289 if (write (md
, mmdlm1
, j
) != j
)
291 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
295 if (write (md
, text
, i
) != i
)
297 for (cp
= text
; *cp
++; size
++)
302 while ((i
= read (fd
, buffer
, sizeof(buffer
))) > 0) {
303 /* valgrind noticed that stringdex depends on null
307 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
309 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
311 if (write (md
, buffer
, i
) != i
)
314 for (cp
= buffer
; i
-- > 0; size
++)
319 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
321 if (write (md
, mmdlm2
, j
) != j
)
324 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
326 return (i
!= NOTOK
? OK
: NOTOK
);
329 if ((j
= dup (fd
)) == NOTOK
)
331 if ((fp
= fdopen (j
, "r")) == NULL
) {
335 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
337 /* If text is given, we add it to top of message */
340 if (write (md
, text
, i
) != i
)
342 for (cp
= text
; *cp
++; size
++)
347 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
350 * Check the first line, and make some changes.
352 if (j
== 0 && !text
) {
354 * Change the "Return-Path:" field (if in first line)
357 if (!strncmp (buffer
, "Return-Path:", 12)) {
358 char tmpbuffer
[BUFSIZ
];
361 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
363 if (!(fp
= strchr(ep
+ 1, ' ')))
364 fp
= strchr(ep
+ 1, '\n');
365 tp
= dctime(dlocaltimenow());
366 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
367 (int)(fp
- ep
), ep
, tp
);
368 } else if (!strncmp (buffer
, "X-Envelope-From:", 16)) {
370 * Change the "X-Envelope-From:" field
371 * (if first line) back to "From ".
373 char tmpbuffer
[BUFSIZ
];
376 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
378 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
379 } else if (strncmp (buffer
, "From ", 5)) {
381 * If there is already a "From " line,
382 * then leave it alone. Else we add one.
384 char tmpbuffer
[BUFSIZ
];
387 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
388 ep
= "nobody@nowhere";
389 tp
= dctime(dlocaltimenow());
390 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
395 * If this is not first line, and begins with
396 * "From ", then prepend line with ">".
398 if (j
!= 0 && strncmp (buffer
, "From ", 5) == 0) {
399 if (write (md
, ">", 1) < 0) {
400 advise (mailbox
, "write");
405 if (write (md
, buffer
, i
) != i
) {
410 for (cp
= buffer
; i
-- > 0; size
++)
414 if (write (md
, "\n", 1) != 1) {
422 lseek (fd
, (off_t
) 0, SEEK_END
);
423 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
425 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
433 mbx_size (int md
, off_t start
, off_t stop
)
439 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
445 fseek (fp
, start
, SEEK_SET
);
446 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
447 if (fgetc (fp
) == '\n')
456 * Close and unlock file/maildrop.
460 mbx_close (char *mailbox
, int md
)
462 if (lkclosespool (md
, mailbox
) == 0)
469 * This function is performed implicitly by getbbent.c:
470 * bb->bb_map = map_name (bb->bb_file);
474 map_name (char *file
)
476 register char *cp
, *dp
;
477 static char buffer
[BUFSIZ
];
479 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
480 dp
= cp
+ strlen (cp
);
482 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
484 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
485 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
492 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
494 register int i
, md
, msgp
;
497 register struct drop
*mp
, *dp
;
499 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
500 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
507 dp
= (struct drop
*) mh_xcalloc ((size_t) (msgp
+ 1), sizeof(*dp
));
513 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
515 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
516 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
522 register struct drop
*tdp
;
525 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
526 tdp
->d_id
= ntohl(tdp
->d_id
);
527 tdp
->d_size
= ntohl(tdp
->d_size
);
528 tdp
->d_start
= ntohl(tdp
->d_start
);
529 tdp
->d_stop
= ntohl(tdp
->d_stop
);
537 return (i
/ sizeof(*dp
));
542 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
543 off_t stop
, long pos
, int size
, int noisy
)
548 register struct drop
*dp
;
549 struct drop d1
, d2
, *rp
;
553 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
556 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
561 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
562 (void) m_unlink (file
);
563 mbx_close (file
, fd
);
564 if ((fd
= map_open (file
, md
)) == NOTOK
)
570 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
572 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
575 mbx_close (file
, fd
);
579 switch (i
= mbx_read (fp
, 0, &rp
, noisy
)) {
582 mbx_close (file
, fd
);
591 for (dp
= rp
; i
-- >0; dp
++) {
592 if (dp
->d_start
== start
)
594 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
595 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
597 admonish (file
, "write error");
598 mbx_close (file
, fd
);
613 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
616 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
617 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
619 admonish (file
, "write error");
620 mbx_close (file
, fd
);
627 dp
->d_start
= (long) last
;
628 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
630 lseek (fd
, (off_t
) 0, SEEK_SET
);
631 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
633 admonish (file
, "write error");
634 mbx_close (file
, fd
);
638 mbx_close (file
, fd
);
645 map_open (char *file
, int md
)
650 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
651 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
656 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
660 register struct drop
*dl
;
662 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
664 admonish (NULL
, "%s: missing or partial index", file
);
669 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
671 dp
->d_id
= ntohl(tmpd
.d_id
);
672 dp
->d_size
= ntohl(tmpd
.d_size
);
673 dp
->d_start
= ntohl(tmpd
.d_start
);
674 dp
->d_stop
= ntohl(tmpd
.d_stop
);
677 if (dp
->d_size
!= DRVRSN
) {
679 admonish (NULL
, "%s: version mismatch (%d != %d)", file
,
684 if (dp
->d_stop
!= pos
) {
685 if (noisy
&& pos
!= (long) 0)
687 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
688 file
, dp
->d_stop
, (long) pos
);
692 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
694 admonish (NULL
, "%s: corrupt index(1)", file
);
699 count
= strlen (mmdlm2
);
700 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
701 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
702 || (ntohl(dl
->d_stop
) != dp
->d_stop
703 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
705 admonish (NULL
, "%s: corrupt index(2)", file
);