]>
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 if ((fd
= lkopenspool (file
, O_RDWR
| O_CREAT
|
51 O_NONBLOCK
, mode
)) == NOTOK
) {
53 #if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
67 /* just return error */
72 /* good file descriptor */
79 * Return if we still failed after 4 attempts,
80 * or we just want to skip the sanity checks.
82 if (fd
== NOTOK
|| mbx_style
== OTHER_FORMAT
)
86 * Do sanity checks on maildrop.
88 if (fstat (fd
, &st
) == NOTOK
) {
90 * The stat failed. So we make sure file
91 * has right ownership/modes
93 chown (file
, uid
, gid
);
95 } else if (st
.st_size
> (off_t
) 0) {
98 /* check the maildrop */
102 status
= mbx_chk_mmdf (fd
);
106 status
= mbx_chk_mbox (fd
);
110 /* if error, attempt to close it */
111 if (status
== NOTOK
) {
122 * Check/prepare MBOX style maildrop for appending.
126 mbx_chk_mbox (int fd
)
128 /* just seek to the end */
129 if (lseek (fd
, (off_t
) 0, SEEK_END
) == (off_t
) NOTOK
)
137 * Check/prepare MMDF style maildrop for appending.
141 mbx_chk_mmdf (int fd
)
146 count
= strlen (mmdlm2
);
148 if (lseek (fd
, -count
, SEEK_END
) == (off_t
) NOTOK
)
150 if (read (fd
, ldelim
, count
) != count
)
155 if (strcmp (ldelim
, mmdlm2
)
156 && write (fd
, "\n", 1) != 1
157 && write (fd
, mmdlm2
, count
) != count
)
165 mbx_read (FILE *fp
, long pos
, struct drop
**drops
, int noisy
)
167 register int len
, size
;
168 register long ld1
, ld2
;
171 register struct drop
*cp
, *dp
, *ep
, *pp
;
173 pp
= (struct drop
*) calloc ((size_t) (len
= MAXFOLDER
), sizeof(*dp
));
176 admonish (NULL
, "unable to allocate drop storage");
180 ld1
= (long) strlen (mmdlm1
);
181 ld2
= (long) strlen (mmdlm2
);
183 fseek (fp
, pos
, SEEK_SET
);
184 for (ep
= (dp
= pp
) + len
- 1; fgets (buffer
, sizeof(buffer
), fp
);) {
186 if (strcmp (buffer
, mmdlm1
) == 0)
187 pos
+= ld1
, dp
->d_start
= (long) pos
;
189 dp
->d_start
= (long)pos
, pos
+= (long) strlen (buffer
);
190 for (bp
= buffer
; *bp
; bp
++, size
++)
195 while (fgets (buffer
, sizeof(buffer
), fp
) != NULL
)
196 if (strcmp (buffer
, mmdlm2
) == 0)
199 pos
+= (long) strlen (buffer
);
200 for (bp
= buffer
; *bp
; bp
++, size
++)
205 if (dp
->d_start
!= (long) pos
) {
207 dp
->d_size
= (long) size
;
214 register int curlen
= dp
- pp
;
216 cp
= (struct drop
*) mh_xrealloc ((char *) pp
,
217 (size_t) (len
+= MAXFOLDER
) * sizeof(*pp
));
218 dp
= cp
+ curlen
, ep
= (pp
= cp
) + len
- 1;
231 mbx_write(char *mailbox
, int md
, FILE *fp
, int id
, long last
,
232 long pos
, off_t stop
, int mapping
, int noisy
)
234 register int i
, j
, size
;
240 off
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
242 if (write (md
, mmdlm1
, j
) != j
)
244 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
247 fseek (fp
, pos
, SEEK_SET
);
248 while (fgets (buffer
, sizeof(buffer
), fp
) && (pos
< stop
)) {
250 for (j
= 0; (j
= stringdex (mmdlm1
, buffer
)) >= 0; buffer
[j
]++)
252 for (j
= 0; (j
= stringdex (mmdlm2
, buffer
)) >= 0; buffer
[j
]++)
254 if (write (md
, buffer
, i
) != i
)
258 for (cp
= buffer
; i
-- > 0; size
++)
263 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
265 if (write (md
, mmdlm2
, j
) != j
)
268 map_write (mailbox
, md
, id
, last
, start
, stop
, off
, size
, noisy
);
275 * Append message to end of file or maildrop.
279 mbx_copy (char *mailbox
, int mbx_style
, int md
, int fd
,
280 int mapping
, char *text
, int noisy
)
285 char *cp
, buffer
[BUFSIZ
];
288 pos
= (long) lseek (md
, (off_t
) 0, SEEK_CUR
);
295 if (write (md
, mmdlm1
, j
) != j
)
297 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
301 if (write (md
, text
, i
) != i
)
303 for (cp
= text
; *cp
++; size
++)
308 while ((i
= read (fd
, buffer
, sizeof(buffer
))) > 0) {
309 /* valgrind noticed that stringdex depends on null
314 (j
= stringdex (mmdlm1
, buffer
)) >= 0;
318 (j
= stringdex (mmdlm2
, buffer
)) >= 0;
321 if (write (md
, buffer
, i
) != i
)
324 for (cp
= buffer
; i
-- > 0; size
++)
329 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
331 if (write (md
, mmdlm2
, j
) != j
)
334 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
336 return (i
!= NOTOK
? OK
: NOTOK
);
339 if ((j
= dup (fd
)) == NOTOK
)
341 if ((fp
= fdopen (j
, "r")) == NULL
) {
345 start
= lseek (md
, (off_t
) 0, SEEK_CUR
);
347 /* If text is given, we add it to top of message */
350 if (write (md
, text
, i
) != i
)
352 for (cp
= text
; *cp
++; size
++)
357 for (j
= 0; fgets (buffer
, sizeof(buffer
), fp
) != NULL
; j
++) {
360 * Check the first line, and make some changes.
362 if (j
== 0 && !text
) {
364 * Change the "Return-Path:" field (if in first line)
367 if (!strncmp (buffer
, "Return-Path:", 12)) {
368 char tmpbuffer
[BUFSIZ
];
371 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
373 if (!(fp
= strchr(ep
+ 1, ' ')))
374 fp
= strchr(ep
+ 1, '\n');
375 tp
= dctime(dlocaltimenow());
376 snprintf (buffer
, sizeof(buffer
), "From %.*s %s",
377 (int)(fp
- ep
), ep
, tp
);
378 } else if (!strncmp (buffer
, "X-Envelope-From:", 16)) {
380 * Change the "X-Envelope-From:" field
381 * (if first line) back to "From ".
383 char tmpbuffer
[BUFSIZ
];
386 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
388 snprintf (buffer
, sizeof(buffer
), "From %s", ep
);
389 } else if (strncmp (buffer
, "From ", 5)) {
391 * If there is already a "From " line,
392 * then leave it alone. Else we add one.
394 char tmpbuffer
[BUFSIZ
];
397 strncpy(tmpbuffer
, buffer
, sizeof(tmpbuffer
));
398 ep
= "nobody@nowhere";
399 tp
= dctime(dlocaltimenow());
400 snprintf (buffer
, sizeof(buffer
), "From %s %s%s", ep
, tp
, tmpbuffer
);
405 * If this is not first line, and begins with
406 * "From ", then prepend line with ">".
408 if (j
!= 0 && strncmp (buffer
, "From ", 5) == 0) {
413 if (write (md
, buffer
, i
) != i
) {
418 for (cp
= buffer
; i
-- > 0; size
++)
422 if (write (md
, "\n", 1) != 1) {
430 lseek (fd
, (off_t
) 0, SEEK_END
);
431 stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
433 map_write (mailbox
, md
, 0, (long) 0, start
, stop
, pos
, size
, noisy
);
441 mbx_size (int md
, off_t start
, off_t stop
)
447 if ((fd
= dup (md
)) == NOTOK
|| (fp
= fdopen (fd
, "r")) == NULL
) {
453 fseek (fp
, start
, SEEK_SET
);
454 for (i
= 0, pos
= stop
- start
; pos
-- > 0; i
++)
455 if (fgetc (fp
) == '\n')
464 * Close and unlock file/maildrop.
468 mbx_close (char *mailbox
, int md
)
470 if (lkclosespool (md
, mailbox
) == 0)
477 * This function is performed implicitly by getbbent.c:
478 * bb->bb_map = map_name (bb->bb_file);
482 map_name (char *file
)
484 register char *cp
, *dp
;
485 static char buffer
[BUFSIZ
];
487 if ((dp
= strchr(cp
= r1bindex (file
, '/'), '.')) == NULL
)
488 dp
= cp
+ strlen (cp
);
490 snprintf (buffer
, sizeof(buffer
), ".%.*s%s", (int)(dp
- cp
), cp
, ".map");
492 snprintf (buffer
, sizeof(buffer
), "%.*s.%.*s%s",
493 (int)(cp
- file
), file
, (int)(dp
- cp
), cp
, ".map");
500 map_read (char *file
, long pos
, struct drop
**drops
, int noisy
)
502 register int i
, md
, msgp
;
505 register struct drop
*mp
, *dp
;
507 if ((md
= open (cp
= map_name (file
), O_RDONLY
)) == NOTOK
508 || map_chk (cp
, md
, mp
= &d
, pos
, noisy
)) {
515 dp
= (struct drop
*) calloc ((size_t) (msgp
+ 1), sizeof(*dp
));
521 memcpy((char *) dp
, (char *) mp
, sizeof(*dp
));
523 lseek (md
, (off_t
) sizeof(*mp
), SEEK_SET
);
524 if ((i
= read (md
, (char *) (dp
+ 1), msgp
* sizeof(*dp
))) <
530 register struct drop
*tdp
;
533 for (j
= 0, tdp
= dp
; j
< i
/ sizeof(*dp
); j
++, tdp
++) {
534 tdp
->d_id
= ntohl(tdp
->d_id
);
535 tdp
->d_size
= ntohl(tdp
->d_size
);
536 tdp
->d_start
= ntohl(tdp
->d_start
);
537 tdp
->d_stop
= ntohl(tdp
->d_stop
);
545 return (i
/ sizeof(*dp
));
550 map_write (char *mailbox
, int md
, int id
, long last
, off_t start
,
551 off_t stop
, long pos
, int size
, int noisy
)
556 register struct drop
*dp
;
557 struct drop d1
, d2
, *rp
;
561 if ((fd
= map_open (file
= map_name (mailbox
), md
)) == NOTOK
)
564 if ((fstat (fd
, &st
) == OK
) && (st
.st_size
> 0))
569 if (!clear
&& map_chk (file
, fd
, &d1
, pos
, noisy
)) {
571 mbx_close (file
, fd
);
572 if ((fd
= map_open (file
, md
)) == NOTOK
)
578 if ((td
= dup (md
)) == NOTOK
|| (fp
= fdopen (td
, "r")) == NULL
) {
580 admonish (file
, "unable to %s", td
!= NOTOK
? "fdopen" : "dup");
583 mbx_close (file
, fd
);
587 switch (i
= mbx_read (fp
, 0, &rp
, noisy
)) {
590 mbx_close (file
, fd
);
599 for (dp
= rp
; i
-- >0; dp
++) {
600 if (dp
->d_start
== start
)
602 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
603 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
605 admonish (file
, "write error");
606 mbx_close (file
, fd
);
621 dp
->d_size
= (long) (size
? size
: mbx_size (fd
, start
, stop
));
624 lseek (fd
, (off_t
) (++d1
.d_id
* sizeof(*dp
)), SEEK_SET
);
625 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
627 admonish (file
, "write error");
628 mbx_close (file
, fd
);
635 dp
->d_start
= (long) last
;
636 dp
->d_stop
= lseek (md
, (off_t
) 0, SEEK_CUR
);
638 lseek (fd
, (off_t
) 0, SEEK_SET
);
639 if (write (fd
, (char *) dp
, sizeof(*dp
)) != sizeof(*dp
)) {
641 admonish (file
, "write error");
642 mbx_close (file
, fd
);
646 mbx_close (file
, fd
);
653 map_open (char *file
, int md
)
658 mode
= fstat (md
, &st
) != NOTOK
? (int) (st
.st_mode
& 0777) : m_gmprot ();
659 return mbx_open (file
, OTHER_FORMAT
, st
.st_uid
, st
.st_gid
, mode
);
664 map_chk (char *file
, int fd
, struct drop
*dp
, long pos
, int noisy
)
668 register struct drop
*dl
;
670 if (read (fd
, (char *) &tmpd
, sizeof(*dp
)) != sizeof(*dp
)) {
672 admonish (NULL
, "%s: missing or partial index", file
);
677 *dp
= tmpd
; /* if ntohl(n)=(n), can use struct assign */
679 dp
->d_id
= ntohl(tmpd
.d_id
);
680 dp
->d_size
= ntohl(tmpd
.d_size
);
681 dp
->d_start
= ntohl(tmpd
.d_start
);
682 dp
->d_stop
= ntohl(tmpd
.d_stop
);
685 if (dp
->d_size
!= DRVRSN
) {
687 admonish (NULL
, "%s: version mismatch (%d != %d)", file
,
692 if (dp
->d_stop
!= pos
) {
693 if (noisy
&& pos
!= (long) 0)
695 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
696 file
, dp
->d_stop
, (long) pos
);
700 if ((long) ((dp
->d_id
+ 1) * sizeof(*dp
)) != (long) lseek (fd
, (off_t
) 0, SEEK_END
)) {
702 admonish (NULL
, "%s: corrupt index(1)", file
);
707 count
= strlen (mmdlm2
);
708 lseek (fd
, (off_t
) (dp
->d_id
* sizeof(*dp
)), SEEK_SET
);
709 if (read (fd
, (char *) dl
, sizeof(*dl
)) != sizeof(*dl
)
710 || (ntohl(dl
->d_stop
) != dp
->d_stop
711 && ntohl(dl
->d_stop
) + count
!= dp
->d_stop
)) {
713 admonish (NULL
, "%s: corrupt index(2)", file
);