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