]>
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;
49 if ((fd
= lkopenspool (file
, O_RDWR
| O_CREAT
|
50 O_NONBLOCK
, mode
, &failed_to_lock
)) == NOTOK
) {
60 /* good file descriptor */
67 * Return if we still failed after 4 attempts,
68 * or we just want to skip the sanity checks.
70 if (fd
== NOTOK
|| mbx_style
== OTHER_FORMAT
)
74 * Do sanity checks on maildrop.
76 if (fstat (fd
, &st
) == NOTOK
) {
78 * The stat failed. So we make sure file
79 * has right ownership/modes
81 if (chown (file
, uid
, gid
) < 0) {
82 advise (file
, "chown");
84 if (chmod (file
, mode
) < 0) {
85 advise (file
, "chmod");
87 } else if (st
.st_size
> (off_t
) 0) {
90 /* check the maildrop */
94 status
= mbx_chk_mmdf (fd
);
98 status
= mbx_chk_mbox (fd
);
102 /* if error, attempt to close it */
103 if (status
== NOTOK
) {
114 * Check/prepare MBOX style maildrop for appending.
118 mbx_chk_mbox (int fd
)
120 /* just seek to the end */
121 if (lseek (fd
, (off_t
) 0, SEEK_END
) == (off_t
) NOTOK
)
129 * Check/prepare MMDF style maildrop for appending.
133 mbx_chk_mmdf (int fd
)
138 count
= strlen (mmdlm2
);
140 if (lseek (fd
, -count
, SEEK_END
) == (off_t
) NOTOK
)
142 if (read (fd
, ldelim
, count
) != count
)
147 if (strcmp (ldelim
, mmdlm2
)
148 && write (fd
, "\n", 1) != 1
149 && write (fd
, mmdlm2
, count
) != count
)
157 mbx_read (FILE *fp
, long pos
, struct drop
**drops
)
163 struct drop
*cp
, *dp
, *ep
, *pp
;
166 pp
= mh_xcalloc(len
, sizeof *pp
);
168 ld1
= (long) strlen (mmdlm1
);
169 ld2
= (long) strlen (mmdlm2
);
171 fseek (fp
, pos
, SEEK_SET
);
172 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
174 if (strcmp (buffer
, mmdlm1
) == 0)
175 pos
+= ld1
, dp
->d_start
= (long) pos
;
177 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
178 for (bp
= buffer
; *bp
; bp
++, size
++)
183 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
184 if (strcmp (buffer
, mmdlm2
) == 0)
187 pos
+= (long) strlen (buffer
);
188 for (bp
= buffer
; *bp
; bp
++, size
++)
193 if (dp
->d_start
!= (long) pos
) {
195 dp
->d_size
= (long) size
;
202 int curlen
= dp
- pp
;
204 cp
= (struct drop
*) mh_xrealloc ((char *) pp
,
205 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
206 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
219 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
220 long pos
, off_t stop
, int mapping
, int noisy
)
228 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
230 if (write (md
, mmdlm1
, j
) != j
)
232 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
235 fseek (fp
, pos
, SEEK_SET
);
236 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
238 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
240 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
242 if (write (md
, buffer
, i
) != i
)
246 for (cp
= buffer
; i
-- > 0; size
++)
251 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
253 if (write (md
, mmdlm2
, j
) != j
)
256 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
263 * Append message to end of file or maildrop.
267 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
268 int mapping
, char *text
, int noisy
)
273 char *cp
, buffer
[BUFSIZ
+ 1]; /* Space for NUL. */
276 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
283 if (write (md
, mmdlm1
, j
) != j
)
285 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
289 if (write (md
, text
, i
) != i
)
291 for (cp
= text
; *cp
++; size
++)
296 while ((i
= read (fd
, buffer
, sizeof buffer
- 1)) > 0) {
297 buffer
[i
] = '\0'; /* Terminate for stringdex(). */
299 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
301 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
303 if (write (md
, buffer
, i
) != i
)
306 for (cp
= buffer
; i
-- > 0; size
++)
311 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
313 if (write (md
, mmdlm2
, j
) != j
)
316 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
318 return (i
!= NOTOK
? OK
: NOTOK
);
321 if ((j
= dup (fd
)) == NOTOK
)
323 if ((fp
= fdopen (j
, "r")) == NULL
) {
327 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
329 /* If text is given, we add it to top of message */
332 if (write (md
, text
, i
) != i
)
334 for (cp
= text
; *cp
++; size
++)
339 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
342 * Check the first line, and make some changes.
344 if (j
== 0 && !text
) {
346 * Change the "Return-Path:" field (if in first line)
349 if (has_prefix(buffer
, "Return-Path:")) {
350 char tmpbuffer
[sizeof buffer
];
353 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
355 if (!(fp
= strchr(ep
+ 1, ' ')))
356 fp
= strchr(ep
+ 1, '\n');
357 tp
= dctime(dlocaltimenow());
358 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
359 (int)(fp
- ep
), ep
, tp
);
360 } else if (has_prefix(buffer
, "X-Envelope-From:")) {
362 * Change the "X-Envelope-From:" field
363 * (if first line) back to "From ".
365 char tmpbuffer
[sizeof buffer
];
368 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
370 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
371 } else if (!has_prefix(buffer
, "From ")) {
373 * If there is already a "From " line,
374 * then leave it alone. Else we add one.
376 char tmpbuffer
[sizeof buffer
];
379 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
380 ep
= "nobody@nowhere";
381 tp
= dctime(dlocaltimenow());
382 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
387 * If this is not first line, and begins with
388 * "From ", then prepend line with ">".
390 if (j
!= 0 && has_prefix(buffer
, "From ")) {
391 if (write (md
, ">", 1) < 0) {
392 advise (mailbox
, "write");
397 if (write (md
, buffer
, i
) != i
) {
402 for (cp
= buffer
; i
-- > 0; size
++)
406 if (write (md
, "\n", 1) != 1) {
414 lseek (fd
, (off_t
) 0, SEEK_END
);
415 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
417 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
425 mbx_size (int md
, off_t start
, off_t stop
)
431 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
437 fseek (fp
, start
, SEEK_SET
);
438 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
439 if (fgetc (fp
) == '\n')
448 * Close and unlock file/maildrop.
452 mbx_close (char *mailbox
, int md
)
454 if (lkclosespool (md
, mailbox
) == 0)
461 * This function is performed implicitly by getbbent.c:
462 * bb->bb_map = map_name (bb->bb_file);
466 map_name (char *file
)
469 static char buffer
[BUFSIZ
];
471 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
472 dp
= cp
+ strlen (cp
);
474 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
476 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
477 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
484 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
489 struct drop
*mp
, *dp
;
491 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
492 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
499 dp
= mh_xcalloc(msgp
+ 1, sizeof *dp
);
500 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
502 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
503 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
512 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
513 tdp
->d_id
= ntohl(tdp
->d_id
);
514 tdp
->d_size
= ntohl(tdp
->d_size
);
515 tdp
->d_start
= ntohl(tdp
->d_start
);
516 tdp
->d_stop
= ntohl(tdp
->d_stop
);
524 return (i
/ sizeof(*dp
));
529 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
530 off_t stop
, long pos
, int size
, int noisy
)
536 struct drop d1
, d2
, *rp
;
540 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
543 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
548 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
549 (void) m_unlink (file
);
550 mbx_close (file
, fd
);
551 if ((fd
= map_open (file
, md
)) == NOTOK
)
557 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
559 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
562 mbx_close (file
, fd
);
566 switch (i
= mbx_read (fp
, 0, &rp
)) {
569 mbx_close (file
, fd
);
578 for (dp
= rp
; i
-- >0; dp
++) {
579 if (dp
->d_start
== start
)
581 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
582 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
584 admonish (file
, "write error");
585 mbx_close (file
, fd
);
600 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
603 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
604 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
606 admonish (file
, "write error");
607 mbx_close (file
, fd
);
614 dp
->d_start
= (long) last
;
615 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
617 lseek (fd
, (off_t
) 0, SEEK_SET
);
618 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
620 admonish (file
, "write error");
621 mbx_close (file
, fd
);
625 mbx_close (file
, fd
);
632 map_open (char *file
, int md
)
637 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
638 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
643 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
649 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
651 inform("%s: missing or partial index, continuing...", file
);
656 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
658 dp
->d_id
= ntohl(tmpd
.d_id
);
659 dp
->d_size
= ntohl(tmpd
.d_size
);
660 dp
->d_start
= ntohl(tmpd
.d_start
);
661 dp
->d_stop
= ntohl(tmpd
.d_stop
);
664 if (dp
->d_size
!= DRVRSN
) {
666 inform("%s: version mismatch (%d != %d), continuing...", file
,
671 if (dp
->d_stop
!= pos
) {
672 if (noisy
&& pos
!= (long) 0)
673 inform("%s: pointer mismatch or incomplete index (%ld!=%ld), "
674 "continuing...", file
, dp
->d_stop
, (long) pos
);
678 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
680 inform("%s: corrupt index(1), continuing...", file
);
685 count
= strlen (mmdlm2
);
686 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
687 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
688 || (ntohl(dl
->d_stop
) != dp
->d_stop
689 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
691 inform("%s: corrupt index(2), continuing...", file
);