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