]> diplodocus.org Git - nmh/blob - uip/dropsbr.c
Reverted commit 9a4b4a3d3b27fe4a7ff6d0b8724ce1c06b5917eb.
[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];
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))) > 0) {
299 /* valgrind noticed that stringdex depends on null
300 termination. */
301 buffer[i] = '\0';
302
303 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
304 continue;
305 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
306 continue;
307 if (write (md, buffer, i) != i)
308 return NOTOK;
309 if (mapping)
310 for (cp = buffer; i-- > 0; size++)
311 if (*cp++ == '\n')
312 size++;
313 }
314
315 stop = lseek (md, (off_t) 0, SEEK_CUR);
316 j = strlen (mmdlm2);
317 if (write (md, mmdlm2, j) != j)
318 return NOTOK;
319 if (mapping)
320 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
321
322 return (i != NOTOK ? OK : NOTOK);
323
324 case MBOX_FORMAT:
325 if ((j = dup (fd)) == NOTOK)
326 return NOTOK;
327 if ((fp = fdopen (j, "r")) == NULL) {
328 close (j);
329 return NOTOK;
330 }
331 start = lseek (md, (off_t) 0, SEEK_CUR);
332
333 /* If text is given, we add it to top of message */
334 if (text) {
335 i = strlen (text);
336 if (write (md, text, i) != i)
337 return NOTOK;
338 for (cp = text; *cp++; size++)
339 if (*cp == '\n')
340 size++;
341 }
342
343 for (j = 0; fgets (buffer, sizeof(buffer), fp) != NULL; j++) {
344
345 /*
346 * Check the first line, and make some changes.
347 */
348 if (j == 0 && !text) {
349 /*
350 * Change the "Return-Path:" field (if in first line)
351 * back to "From ".
352 */
353 if (HasPrefix(buffer, "Return-Path:")) {
354 char tmpbuffer[BUFSIZ];
355 char *tp, *ep, *fp;
356
357 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
358 ep = tmpbuffer + 13;
359 if (!(fp = strchr(ep + 1, ' ')))
360 fp = strchr(ep + 1, '\n');
361 tp = dctime(dlocaltimenow());
362 snprintf (buffer, sizeof(buffer), "From %.*s %s",
363 (int)(fp - ep), ep, tp);
364 } else if (HasPrefix(buffer, "X-Envelope-From:")) {
365 /*
366 * Change the "X-Envelope-From:" field
367 * (if first line) back to "From ".
368 */
369 char tmpbuffer[BUFSIZ];
370 char *ep;
371
372 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
373 ep = tmpbuffer + 17;
374 snprintf (buffer, sizeof(buffer), "From %s", ep);
375 } else if (!HasPrefix(buffer, "From ")) {
376 /*
377 * If there is already a "From " line,
378 * then leave it alone. Else we add one.
379 */
380 char tmpbuffer[BUFSIZ];
381 char *tp, *ep;
382
383 strncpy(tmpbuffer, buffer, sizeof(tmpbuffer));
384 ep = "nobody@nowhere";
385 tp = dctime(dlocaltimenow());
386 snprintf (buffer, sizeof(buffer), "From %s %s%s", ep, tp, tmpbuffer);
387 }
388 }
389
390 /*
391 * If this is not first line, and begins with
392 * "From ", then prepend line with ">".
393 */
394 if (j != 0 && HasPrefix(buffer, "From ")) {
395 if (write (md, ">", 1) < 0) {
396 advise (mailbox, "write");
397 }
398 size++;
399 }
400 i = strlen (buffer);
401 if (write (md, buffer, i) != i) {
402 fclose (fp);
403 return NOTOK;
404 }
405 if (mapping)
406 for (cp = buffer; i-- > 0; size++)
407 if (*cp++ == '\n')
408 size++;
409 }
410 if (write (md, "\n", 1) != 1) {
411 fclose (fp);
412 return NOTOK;
413 }
414 if (mapping)
415 size += 2;
416
417 fclose (fp);
418 lseek (fd, (off_t) 0, SEEK_END);
419 stop = lseek (md, (off_t) 0, SEEK_CUR);
420 if (mapping)
421 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
422
423 return OK;
424 }
425 }
426
427
428 int
429 mbx_size (int md, off_t start, off_t stop)
430 {
431 int i, fd;
432 long pos;
433 FILE *fp;
434
435 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
436 if (fd != NOTOK)
437 close (fd);
438 return NOTOK;
439 }
440
441 fseek (fp, start, SEEK_SET);
442 for (i = 0, pos = stop - start; pos-- > 0; i++)
443 if (fgetc (fp) == '\n')
444 i++;
445
446 fclose (fp);
447 return i;
448 }
449
450
451 /*
452 * Close and unlock file/maildrop.
453 */
454
455 int
456 mbx_close (char *mailbox, int md)
457 {
458 if (lkclosespool (md, mailbox) == 0)
459 return OK;
460 return NOTOK;
461 }
462
463
464 /*
465 * This function is performed implicitly by getbbent.c:
466 * bb->bb_map = map_name (bb->bb_file);
467 */
468
469 char *
470 map_name (char *file)
471 {
472 char *cp, *dp;
473 static char buffer[BUFSIZ];
474
475 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
476 dp = cp + strlen (cp);
477 if (cp == file)
478 snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
479 else
480 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
481 (int)(cp - file), file, (int)(dp - cp), cp, ".map");
482
483 return buffer;
484 }
485
486
487 int
488 map_read (char *file, long pos, struct drop **drops, int noisy)
489 {
490 int i, md, msgp;
491 char *cp;
492 struct drop d;
493 struct drop *mp, *dp;
494
495 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
496 || map_chk (cp, md, mp = &d, pos, noisy)) {
497 if (md != NOTOK)
498 close (md);
499 return 0;
500 }
501
502 msgp = mp->d_id;
503 dp = mh_xcalloc(msgp + 1, sizeof *dp);
504 memcpy((char *) dp, (char *) mp, sizeof(*dp));
505
506 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
507 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) <
508 (int) sizeof(*dp)) {
509 i = 0;
510 free(dp);
511 } else {
512 #ifdef NTOHLSWAP
513 struct drop *tdp;
514 int j;
515
516 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
517 tdp->d_id = ntohl(tdp->d_id);
518 tdp->d_size = ntohl(tdp->d_size);
519 tdp->d_start = ntohl(tdp->d_start);
520 tdp->d_stop = ntohl(tdp->d_stop);
521 }
522 #endif
523 *drops = dp;
524 }
525
526 close (md);
527
528 return (i / sizeof(*dp));
529 }
530
531
532 int
533 map_write (char *mailbox, int md, int id, long last, off_t start,
534 off_t stop, long pos, int size, int noisy)
535 {
536 int i;
537 int clear, fd, td;
538 char *file;
539 struct drop *dp;
540 struct drop d1, d2, *rp;
541 FILE *fp;
542 struct stat st;
543
544 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
545 return NOTOK;
546
547 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
548 clear = 0;
549 else
550 clear = 1;
551
552 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
553 (void) m_unlink (file);
554 mbx_close (file, fd);
555 if ((fd = map_open (file, md)) == NOTOK)
556 return NOTOK;
557 clear++;
558 }
559
560 if (clear) {
561 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
562 if (noisy)
563 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
564 if (td != NOTOK)
565 close (td);
566 mbx_close (file, fd);
567 return NOTOK;
568 }
569
570 switch (i = mbx_read (fp, 0, &rp)) {
571 case NOTOK:
572 fclose (fp);
573 mbx_close (file, fd);
574 return NOTOK;
575
576 case OK:
577 fclose (fp);
578 break;
579
580 default:
581 d1.d_id = 0;
582 for (dp = rp; i-- >0; dp++) {
583 if (dp->d_start == start)
584 dp->d_id = id;
585 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
586 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
587 if (noisy)
588 admonish (file, "write error");
589 mbx_close (file, fd);
590 fclose (fp);
591 return NOTOK;
592 }
593 }
594 free(rp);
595 fclose (fp);
596 break;
597 }
598 }
599 else {
600 if (last == 0)
601 last = d1.d_start;
602 dp = &d2;
603 dp->d_id = id;
604 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
605 dp->d_start = start;
606 dp->d_stop = stop;
607 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
608 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
609 if (noisy)
610 admonish (file, "write error");
611 mbx_close (file, fd);
612 return NOTOK;
613 }
614 }
615
616 dp = &d1;
617 dp->d_size = DRVRSN;
618 dp->d_start = (long) last;
619 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
620
621 lseek (fd, (off_t) 0, SEEK_SET);
622 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
623 if (noisy)
624 admonish (file, "write error");
625 mbx_close (file, fd);
626 return NOTOK;
627 }
628
629 mbx_close (file, fd);
630
631 return OK;
632 }
633
634
635 static int
636 map_open (char *file, int md)
637 {
638 mode_t mode;
639 struct stat st;
640
641 mode = fstat (md, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
642 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
643 }
644
645
646 int
647 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
648 {
649 ssize_t count;
650 struct drop d, tmpd;
651 struct drop *dl;
652
653 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
654 #ifdef notdef
655 admonish (NULL, "%s: missing or partial index", file);
656 #endif /* notdef */
657 return NOTOK;
658 }
659 #ifndef NTOHLSWAP
660 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
661 #else
662 dp->d_id = ntohl(tmpd.d_id);
663 dp->d_size = ntohl(tmpd.d_size);
664 dp->d_start = ntohl(tmpd.d_start);
665 dp->d_stop = ntohl(tmpd.d_stop);
666 #endif
667
668 if (dp->d_size != DRVRSN) {
669 if (noisy)
670 admonish (NULL, "%s: version mismatch (%d != %d)", file,
671 dp->d_size, DRVRSN);
672 return NOTOK;
673 }
674
675 if (dp->d_stop != pos) {
676 if (noisy && pos != (long) 0)
677 admonish (NULL,
678 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
679 file, dp->d_stop, (long) pos);
680 return NOTOK;
681 }
682
683 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
684 if (noisy)
685 admonish (NULL, "%s: corrupt index(1)", file);
686 return NOTOK;
687 }
688
689 dl = &d;
690 count = strlen (mmdlm2);
691 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
692 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
693 || (ntohl(dl->d_stop) != dp->d_stop
694 && ntohl(dl->d_stop) + count != dp->d_stop)) {
695 if (noisy)
696 admonish (NULL, "%s: corrupt index(2)", file);
697 return NOTOK;
698 }
699
700 return OK;
701 }