]> diplodocus.org Git - nmh/blob - uip/dropsbr.c
h/nmh.h: Remove unused NLENGTH(dirent) macro.
[nmh] / uip / dropsbr.c
1
2 /*
3 * dropsbr.c -- create/read/manipulate mail drops
4 *
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.
8 */
9
10 #include <h/nmh.h>
11 #include <h/utils.h>
12
13 #include <h/mh.h>
14 #include <h/dropsbr.h>
15 #include <h/mts.h>
16 #include <h/tws.h>
17
18 #ifdef NTOHLSWAP
19 # include <netinet/in.h>
20 #else
21 # undef ntohl
22 # define ntohl(n) (n)
23 #endif
24
25 #include <fcntl.h>
26
27 /*
28 * static prototypes
29 */
30 static int mbx_chk_mbox (int);
31 static int mbx_chk_mmdf (int);
32 static int map_open (char *, int);
33
34
35 /*
36 * Main entry point to open/create and lock
37 * a file or maildrop.
38 */
39
40 int
41 mbx_open (char *file, int mbx_style, uid_t uid, gid_t gid, mode_t mode)
42 {
43 int j, count, fd = NOTOK;
44 struct stat st;
45
46 j = 0;
47
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) {
53 if (failed_to_lock) {
54 j = errno;
55 sleep (5);
56 continue;
57 } else {
58 return NOTOK;
59 }
60 }
61
62 /* good file descriptor */
63 break;
64 }
65
66 errno = j;
67
68 /*
69 * Return if we still failed after 4 attempts,
70 * or we just want to skip the sanity checks.
71 */
72 if (fd == NOTOK || mbx_style == OTHER_FORMAT)
73 return fd;
74
75 /*
76 * Do sanity checks on maildrop.
77 */
78 if (fstat (fd, &st) == NOTOK) {
79 /*
80 * The stat failed. So we make sure file
81 * has right ownership/modes
82 */
83 if (chown (file, uid, gid) < 0) {
84 advise (file, "chown");
85 }
86 if (chmod (file, mode) < 0) {
87 advise (file, "chmod");
88 }
89 } else if (st.st_size > (off_t) 0) {
90 int status;
91
92 /* check the maildrop */
93 switch (mbx_style) {
94 case MMDF_FORMAT:
95 default:
96 status = mbx_chk_mmdf (fd);
97 break;
98
99 case MBOX_FORMAT:
100 status = mbx_chk_mbox (fd);
101 break;
102 }
103
104 /* if error, attempt to close it */
105 if (status == NOTOK) {
106 close (fd);
107 return NOTOK;
108 }
109 }
110
111 return fd;
112 }
113
114
115 /*
116 * Check/prepare MBOX style maildrop for appending.
117 */
118
119 static int
120 mbx_chk_mbox (int fd)
121 {
122 /* just seek to the end */
123 if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
124 return NOTOK;
125
126 return OK;
127 }
128
129
130 /*
131 * Check/prepare MMDF style maildrop for appending.
132 */
133
134 static int
135 mbx_chk_mmdf (int fd)
136 {
137 ssize_t count;
138 char ldelim[BUFSIZ];
139
140 count = strlen (mmdlm2);
141
142 if (lseek (fd, -count, SEEK_END) == (off_t) NOTOK)
143 return NOTOK;
144 if (read (fd, ldelim, count) != count)
145 return NOTOK;
146
147 ldelim[count] = 0;
148
149 if (strcmp (ldelim, mmdlm2)
150 && write (fd, "\n", 1) != 1
151 && write (fd, mmdlm2, count) != count)
152 return NOTOK;
153
154 return OK;
155 }
156
157
158 int
159 mbx_read (FILE *fp, long pos, struct drop **drops)
160 {
161 int len, size;
162 long ld1, ld2;
163 char *bp;
164 char buffer[BUFSIZ];
165 struct drop *cp, *dp, *ep, *pp;
166
167 len = MAXFOLDER;
168 pp = mh_xcalloc(len, sizeof *pp);
169
170 ld1 = (long) strlen (mmdlm1);
171 ld2 = (long) strlen (mmdlm2);
172
173 fseek (fp, pos, SEEK_SET);
174 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
175 size = 0;
176 if (strcmp (buffer, mmdlm1) == 0)
177 pos += ld1, dp->d_start = (long) pos;
178 else {
179 dp->d_start = (long)pos , pos += (long) strlen (buffer);
180 for (bp = buffer; *bp; bp++, size++)
181 if (*bp == '\n')
182 size++;
183 }
184
185 while (fgets (buffer, sizeof(buffer), fp) != NULL)
186 if (strcmp (buffer, mmdlm2) == 0)
187 break;
188 else {
189 pos += (long) strlen (buffer);
190 for (bp = buffer; *bp; bp++, size++)
191 if (*bp == '\n')
192 size++;
193 }
194
195 if (dp->d_start != (long) pos) {
196 dp->d_id = 0;
197 dp->d_size = (long) size;
198 dp->d_stop = pos;
199 dp++;
200 }
201 pos += ld2;
202
203 if (dp >= ep) {
204 int curlen = dp - pp;
205
206 cp = (struct drop *) mh_xrealloc ((char *) pp,
207 (size_t) (len += MAXFOLDER) * sizeof(*pp));
208 dp = cp + curlen, ep = (pp = cp) + len - 1;
209 }
210 }
211
212 if (dp == pp)
213 free(pp);
214 else
215 *drops = pp;
216 return (dp - pp);
217 }
218
219
220 int
221 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
222 long pos, off_t stop, int mapping, int noisy)
223 {
224 int i, j, size;
225 off_t start;
226 long off;
227 char *cp;
228 char buffer[BUFSIZ];
229
230 off = (long) lseek (md, (off_t) 0, SEEK_CUR);
231 j = strlen (mmdlm1);
232 if (write (md, mmdlm1, j) != j)
233 return NOTOK;
234 start = lseek (md, (off_t) 0, SEEK_CUR);
235 size = 0;
236
237 fseek (fp, pos, SEEK_SET);
238 while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
239 i = strlen (buffer);
240 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
241 continue;
242 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
243 continue;
244 if (write (md, buffer, i) != i)
245 return NOTOK;
246 pos += (long) i;
247 if (mapping)
248 for (cp = buffer; i-- > 0; size++)
249 if (*cp++ == '\n')
250 size++;
251 }
252
253 stop = lseek (md, (off_t) 0, SEEK_CUR);
254 j = strlen (mmdlm2);
255 if (write (md, mmdlm2, j) != j)
256 return NOTOK;
257 if (mapping)
258 map_write (mailbox, md, id, last, start, stop, off, size, noisy);
259
260 return OK;
261 }
262
263
264 /*
265 * Append message to end of file or maildrop.
266 */
267
268 int
269 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
270 int mapping, char *text, int noisy)
271 {
272 int i, j, size;
273 off_t start, stop;
274 long pos;
275 char *cp, buffer[BUFSIZ + 1]; /* Space for NUL. */
276 FILE *fp;
277
278 pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
279 size = 0;
280
281 switch (mbx_style) {
282 case MMDF_FORMAT:
283 default:
284 j = strlen (mmdlm1);
285 if (write (md, mmdlm1, j) != j)
286 return NOTOK;
287 start = lseek (md, (off_t) 0, SEEK_CUR);
288
289 if (text) {
290 i = strlen (text);
291 if (write (md, text, i) != i)
292 return NOTOK;
293 for (cp = text; *cp++; size++)
294 if (*cp == '\n')
295 size++;
296 }
297
298 while ((i = read (fd, buffer, sizeof buffer - 1)) > 0) {
299 buffer[i] = '\0'; /* Terminate for stringdex(). */
300
301 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
302 continue;
303 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
304 continue;
305 if (write (md, buffer, i) != i)
306 return NOTOK;
307 if (mapping)
308 for (cp = buffer; i-- > 0; size++)
309 if (*cp++ == '\n')
310 size++;
311 }
312
313 stop = lseek (md, (off_t) 0, SEEK_CUR);
314 j = strlen (mmdlm2);
315 if (write (md, mmdlm2, j) != j)
316 return NOTOK;
317 if (mapping)
318 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
319
320 return (i != NOTOK ? OK : NOTOK);
321
322 case MBOX_FORMAT:
323 if ((j = dup (fd)) == NOTOK)
324 return NOTOK;
325 if ((fp = fdopen (j, "r")) == NULL) {
326 close (j);
327 return NOTOK;
328 }
329 start = lseek (md, (off_t) 0, SEEK_CUR);
330
331 /* If text is given, we add it to top of message */
332 if (text) {
333 i = strlen (text);
334 if (write (md, text, i) != i)
335 return NOTOK;
336 for (cp = text; *cp++; size++)
337 if (*cp == '\n')
338 size++;
339 }
340
341 for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
342
343 /*
344 * Check the first line, and make some changes.
345 */
346 if (j == 0 && !text) {
347 /*
348 * Change the "Return-Path:" field (if in first line)
349 * back to "From ".
350 */
351 if (has_prefix(buffer, "Return-Path:")) {
352 char tmpbuffer[sizeof buffer];
353 char *tp, *ep, *fp;
354
355 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
356 ep = tmpbuffer + 13;
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:")) {
363 /*
364 * Change the "X-Envelope-From:" field
365 * (if first line) back to "From ".
366 */
367 char tmpbuffer[sizeof buffer];
368 char *ep;
369
370 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
371 ep = tmpbuffer + 17;
372 snprintf (buffer, sizeof(buffer), "From %s", ep);
373 } else if (!has_prefix(buffer, "From ")) {
374 /*
375 * If there is already a "From " line,
376 * then leave it alone. Else we add one.
377 */
378 char tmpbuffer[sizeof buffer];
379 char *tp, *ep;
380
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);
385 }
386 }
387
388 /*
389 * If this is not first line, and begins with
390 * "From ", then prepend line with ">".
391 */
392 if (j != 0 && has_prefix(buffer, "From ")) {
393 if (write (md, ">", 1) < 0) {
394 advise (mailbox, "write");
395 }
396 size++;
397 }
398 i = strlen (buffer);
399 if (write (md, buffer, i) != i) {
400 fclose (fp);
401 return NOTOK;
402 }
403 if (mapping)
404 for (cp = buffer; i-- > 0; size++)
405 if (*cp++ == '\n')
406 size++;
407 }
408 if (write (md, "\n", 1) != 1) {
409 fclose (fp);
410 return NOTOK;
411 }
412 if (mapping)
413 size += 2;
414
415 fclose (fp);
416 lseek (fd, (off_t) 0, SEEK_END);
417 stop = lseek (md, (off_t) 0, SEEK_CUR);
418 if (mapping)
419 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
420
421 return OK;
422 }
423 }
424
425
426 int
427 mbx_size (int md, off_t start, off_t stop)
428 {
429 int i, fd;
430 long pos;
431 FILE *fp;
432
433 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
434 if (fd != NOTOK)
435 close (fd);
436 return NOTOK;
437 }
438
439 fseek (fp, start, SEEK_SET);
440 for (i = 0, pos = stop - start; pos-- > 0; i++)
441 if (fgetc (fp) == '\n')
442 i++;
443
444 fclose (fp);
445 return i;
446 }
447
448
449 /*
450 * Close and unlock file/maildrop.
451 */
452
453 int
454 mbx_close (char *mailbox, int md)
455 {
456 if (lkclosespool (md, mailbox) == 0)
457 return OK;
458 return NOTOK;
459 }
460
461
462 /*
463 * This function is performed implicitly by getbbent.c:
464 * bb->bb_map = map_name (bb->bb_file);
465 */
466
467 char *
468 map_name (char *file)
469 {
470 char *cp, *dp;
471 static char buffer[BUFSIZ];
472
473 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
474 dp = cp + strlen (cp);
475 if (cp == file)
476 snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
477 else
478 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
479 (int)(cp - file), file, (int)(dp - cp), cp, ".map");
480
481 return buffer;
482 }
483
484
485 int
486 map_read (char *file, long pos, struct drop **drops, int noisy)
487 {
488 int i, md, msgp;
489 char *cp;
490 struct drop d;
491 struct drop *mp, *dp;
492
493 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
494 || map_chk (cp, md, mp = &d, pos, noisy)) {
495 if (md != NOTOK)
496 close (md);
497 return 0;
498 }
499
500 msgp = mp->d_id;
501 dp = mh_xcalloc(msgp + 1, sizeof *dp);
502 memcpy((char *) dp, (char *) mp, sizeof(*dp));
503
504 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
505 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) <
506 (int) sizeof(*dp)) {
507 i = 0;
508 free(dp);
509 } else {
510 #ifdef NTOHLSWAP
511 struct drop *tdp;
512 int j;
513
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);
519 }
520 #endif
521 *drops = dp;
522 }
523
524 close (md);
525
526 return (i / sizeof(*dp));
527 }
528
529
530 int
531 map_write (char *mailbox, int md, int id, long last, off_t start,
532 off_t stop, long pos, int size, int noisy)
533 {
534 int i;
535 int clear, fd, td;
536 char *file;
537 struct drop *dp;
538 struct drop d1, d2, *rp;
539 FILE *fp;
540 struct stat st;
541
542 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
543 return NOTOK;
544
545 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
546 clear = 0;
547 else
548 clear = 1;
549
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)
554 return NOTOK;
555 clear++;
556 }
557
558 if (clear) {
559 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
560 if (noisy)
561 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
562 if (td != NOTOK)
563 close (td);
564 mbx_close (file, fd);
565 return NOTOK;
566 }
567
568 switch (i = mbx_read (fp, 0, &rp)) {
569 case NOTOK:
570 fclose (fp);
571 mbx_close (file, fd);
572 return NOTOK;
573
574 case OK:
575 fclose (fp);
576 break;
577
578 default:
579 d1.d_id = 0;
580 for (dp = rp; i-- >0; dp++) {
581 if (dp->d_start == start)
582 dp->d_id = id;
583 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
584 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
585 if (noisy)
586 admonish (file, "write error");
587 mbx_close (file, fd);
588 fclose (fp);
589 return NOTOK;
590 }
591 }
592 free(rp);
593 fclose (fp);
594 break;
595 }
596 }
597 else {
598 if (last == 0)
599 last = d1.d_start;
600 dp = &d2;
601 dp->d_id = id;
602 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
603 dp->d_start = start;
604 dp->d_stop = stop;
605 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
606 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
607 if (noisy)
608 admonish (file, "write error");
609 mbx_close (file, fd);
610 return NOTOK;
611 }
612 }
613
614 dp = &d1;
615 dp->d_size = DRVRSN;
616 dp->d_start = (long) last;
617 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
618
619 lseek (fd, (off_t) 0, SEEK_SET);
620 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
621 if (noisy)
622 admonish (file, "write error");
623 mbx_close (file, fd);
624 return NOTOK;
625 }
626
627 mbx_close (file, fd);
628
629 return OK;
630 }
631
632
633 static int
634 map_open (char *file, int md)
635 {
636 mode_t mode;
637 struct stat st;
638
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);
641 }
642
643
644 int
645 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
646 {
647 ssize_t count;
648 struct drop d, tmpd;
649 struct drop *dl;
650
651 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
652 #ifdef notdef
653 inform("%s: missing or partial index, continuing...", file);
654 #endif /* notdef */
655 return NOTOK;
656 }
657 #ifndef NTOHLSWAP
658 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
659 #else
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);
664 #endif
665
666 if (dp->d_size != DRVRSN) {
667 if (noisy)
668 inform("%s: version mismatch (%d != %d), continuing...", file,
669 dp->d_size, DRVRSN);
670 return NOTOK;
671 }
672
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);
677 return NOTOK;
678 }
679
680 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
681 if (noisy)
682 inform("%s: corrupt index(1), continuing...", file);
683 return NOTOK;
684 }
685
686 dl = &d;
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)) {
692 if (noisy)
693 inform("%s: corrupt index(2), continuing...", file);
694 return NOTOK;
695 }
696
697 return OK;
698 }