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