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