]>
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
+ 1]; /* Space for NUL. */
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
- 1)) > 0) {
299 buffer
[i
] = '\0'; /* Terminate for stringdex(). */
301 for ( ; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
303 for ( ; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
305 if (write (md
, buffer
, i
) != i
)
308 for (cp
= buffer
; i
-- > 0; size
++)
313 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
315 if (write (md
, mmdlm2
, j
) != j
)
318 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
320 return (i
!= NOTOK
? OK
: NOTOK
);
323 if ((j
= dup (fd
)) == NOTOK
)
325 if ((fp
= fdopen (j
, "r")) == NULL
) {
329 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
331 /* If text is given, we add it to top of message */
334 if (write (md
, text
, i
) != i
)
336 for (cp
= text
; *cp
++; size
++)
341 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
344 * Check the first line, and make some changes.
346 if (j
== 0 && !text
) {
348 * Change the "Return-Path:" field (if in first line)
351 if (has_prefix(buffer
, "Return-Path:")) {
352 char tmpbuffer
[sizeof buffer
];
355 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
357 if (!(fp
= strchr(ep
+ 1, ' ')))
358 fp
= strchr(ep
+ 1, '\n');
359 tp
= dctime(dlocaltimenow());
360 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
361 (int)(fp
- ep
), ep
, tp
);
362 } else if (has_prefix(buffer
, "X-Envelope-From:")) {
364 * Change the "X-Envelope-From:" field
365 * (if first line) back to "From ".
367 char tmpbuffer
[sizeof buffer
];
370 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
372 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
373 } else if (!has_prefix(buffer
, "From ")) {
375 * If there is already a "From " line,
376 * then leave it alone. Else we add one.
378 char tmpbuffer
[sizeof buffer
];
381 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
382 ep
= "nobody@nowhere";
383 tp
= dctime(dlocaltimenow());
384 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
389 * If this is not first line, and begins with
390 * "From ", then prepend line with ">".
392 if (j
!= 0 && has_prefix(buffer
, "From ")) {
393 if (write (md
, ">", 1) < 0) {
394 advise (mailbox
, "write");
399 if (write (md
, buffer
, i
) != i
) {
404 for (cp
= buffer
; i
-- > 0; size
++)
408 if (write (md
, "\n", 1) != 1) {
416 lseek (fd
, (off_t
) 0, SEEK_END
);
417 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
419 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
427 mbx_size (int md
, off_t start
, off_t stop
)
433 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
439 fseek (fp
, start
, SEEK_SET
);
440 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
441 if (fgetc (fp
) == '\n')
450 * Close and unlock file/maildrop.
454 mbx_close (char *mailbox
, int md
)
456 if (lkclosespool (md
, mailbox
) == 0)
463 * This function is performed implicitly by getbbent.c:
464 * bb->bb_map = map_name (bb->bb_file);
468 map_name (char *file
)
471 static char buffer
[BUFSIZ
];
473 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
474 dp
= cp
+ strlen (cp
);
476 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
478 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
479 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
486 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
491 struct drop
*mp
, *dp
;
493 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
494 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
501 dp
= mh_xcalloc(msgp
+ 1, sizeof *dp
);
502 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
504 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
505 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
514 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
515 tdp
->d_id
= ntohl(tdp
->d_id
);
516 tdp
->d_size
= ntohl(tdp
->d_size
);
517 tdp
->d_start
= ntohl(tdp
->d_start
);
518 tdp
->d_stop
= ntohl(tdp
->d_stop
);
526 return (i
/ sizeof(*dp
));
531 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
532 off_t stop
, long pos
, int size
, int noisy
)
538 struct drop d1
, d2
, *rp
;
542 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
545 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
550 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
551 (void) m_unlink (file
);
552 mbx_close (file
, fd
);
553 if ((fd
= map_open (file
, md
)) == NOTOK
)
559 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
561 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
564 mbx_close (file
, fd
);
568 switch (i
= mbx_read (fp
, 0, &rp
)) {
571 mbx_close (file
, fd
);
580 for (dp
= rp
; i
-- >0; dp
++) {
581 if (dp
->d_start
== start
)
583 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
584 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
586 admonish (file
, "write error");
587 mbx_close (file
, fd
);
602 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
605 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
606 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
608 admonish (file
, "write error");
609 mbx_close (file
, fd
);
616 dp
->d_start
= (long) last
;
617 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
619 lseek (fd
, (off_t
) 0, SEEK_SET
);
620 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
622 admonish (file
, "write error");
623 mbx_close (file
, fd
);
627 mbx_close (file
, fd
);
634 map_open (char *file
, int md
)
639 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
640 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
645 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
651 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
653 inform("%s: missing or partial index, continuing...", file
);
658 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
660 dp
->d_id
= ntohl(tmpd
.d_id
);
661 dp
->d_size
= ntohl(tmpd
.d_size
);
662 dp
->d_start
= ntohl(tmpd
.d_start
);
663 dp
->d_stop
= ntohl(tmpd
.d_stop
);
666 if (dp
->d_size
!= DRVRSN
) {
668 inform("%s: version mismatch (%d != %d), continuing...", file
,
673 if (dp
->d_stop
!= pos
) {
674 if (noisy
&& pos
!= (long) 0)
675 inform("%s: pointer mismatch or incomplete index (%ld!=%ld), "
676 "continuing...", file
, dp
->d_stop
, (long) pos
);
680 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
682 inform("%s: corrupt index(1), continuing...", file
);
687 count
= strlen (mmdlm2
);
688 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
689 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
690 || (ntohl(dl
->d_stop
) != dp
->d_stop
691 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
693 inform("%s: corrupt index(2), continuing...", file
);