]> diplodocus.org Git - nmh/blob - uip/dropsbr.c
Another pass at cleaning up (some of) the manpages.
[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, int noisy)
160 {
161 register int len, size;
162 register long ld1, ld2;
163 register char *bp;
164 char buffer[BUFSIZ];
165 register struct drop *cp, *dp, *ep, *pp;
166
167 pp = (struct drop *) mh_xcalloc ((size_t) (len = MAXFOLDER), sizeof(*dp));
168 if (pp == NULL) {
169 if (noisy)
170 admonish (NULL, "unable to allocate drop storage");
171 return NOTOK;
172 }
173
174 ld1 = (long) strlen (mmdlm1);
175 ld2 = (long) strlen (mmdlm2);
176
177 fseek (fp, pos, SEEK_SET);
178 for (ep = (dp = pp) + len - 1; fgets (buffer, sizeof(buffer), fp);) {
179 size = 0;
180 if (strcmp (buffer, mmdlm1) == 0)
181 pos += ld1, dp->d_start = (long) pos;
182 else {
183 dp->d_start = (long)pos , pos += (long) strlen (buffer);
184 for (bp = buffer; *bp; bp++, size++)
185 if (*bp == '\n')
186 size++;
187 }
188
189 while (fgets (buffer, sizeof(buffer), fp) != NULL)
190 if (strcmp (buffer, mmdlm2) == 0)
191 break;
192 else {
193 pos += (long) strlen (buffer);
194 for (bp = buffer; *bp; bp++, size++)
195 if (*bp == '\n')
196 size++;
197 }
198
199 if (dp->d_start != (long) pos) {
200 dp->d_id = 0;
201 dp->d_size = (long) size;
202 dp->d_stop = pos;
203 dp++;
204 }
205 pos += ld2;
206
207 if (dp >= ep) {
208 register int curlen = dp - pp;
209
210 cp = (struct drop *) mh_xrealloc ((char *) pp,
211 (size_t) (len += MAXFOLDER) * sizeof(*pp));
212 dp = cp + curlen, ep = (pp = cp) + len - 1;
213 }
214 }
215
216 if (dp == pp)
217 free ((char *) pp);
218 else
219 *drops = pp;
220 return (dp - pp);
221 }
222
223
224 int
225 mbx_write(char *mailbox, int md, FILE *fp, int id, long last,
226 long pos, off_t stop, int mapping, int noisy)
227 {
228 register int i, j, size;
229 off_t start;
230 long off;
231 register char *cp;
232 char buffer[BUFSIZ];
233
234 off = (long) lseek (md, (off_t) 0, SEEK_CUR);
235 j = strlen (mmdlm1);
236 if (write (md, mmdlm1, j) != j)
237 return NOTOK;
238 start = lseek (md, (off_t) 0, SEEK_CUR);
239 size = 0;
240
241 fseek (fp, pos, SEEK_SET);
242 while (fgets (buffer, sizeof(buffer), fp) && (pos < stop)) {
243 i = strlen (buffer);
244 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
245 continue;
246 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; buffer[j]++)
247 continue;
248 if (write (md, buffer, i) != i)
249 return NOTOK;
250 pos += (long) i;
251 if (mapping)
252 for (cp = buffer; i-- > 0; size++)
253 if (*cp++ == '\n')
254 size++;
255 }
256
257 stop = lseek (md, (off_t) 0, SEEK_CUR);
258 j = strlen (mmdlm2);
259 if (write (md, mmdlm2, j) != j)
260 return NOTOK;
261 if (mapping)
262 map_write (mailbox, md, id, last, start, stop, off, size, noisy);
263
264 return OK;
265 }
266
267
268 /*
269 * Append message to end of file or maildrop.
270 */
271
272 int
273 mbx_copy (char *mailbox, int mbx_style, int md, int fd,
274 int mapping, char *text, int noisy)
275 {
276 int i, j, size;
277 off_t start, stop;
278 long pos;
279 char *cp, buffer[BUFSIZ];
280 FILE *fp;
281
282 pos = (long) lseek (md, (off_t) 0, SEEK_CUR);
283 size = 0;
284
285 switch (mbx_style) {
286 case MMDF_FORMAT:
287 default:
288 j = strlen (mmdlm1);
289 if (write (md, mmdlm1, j) != j)
290 return NOTOK;
291 start = lseek (md, (off_t) 0, SEEK_CUR);
292
293 if (text) {
294 i = strlen (text);
295 if (write (md, text, i) != i)
296 return NOTOK;
297 for (cp = text; *cp++; size++)
298 if (*cp == '\n')
299 size++;
300 }
301
302 while ((i = read (fd, buffer, sizeof(buffer))) > 0) {
303 /* valgrind noticed that stringdex depends on null
304 termination. */
305 buffer[i] = '\0';
306
307 for ( ; (j = stringdex (mmdlm1, buffer)) >= 0; buffer[j]++)
308 continue;
309 for ( ; (j = stringdex (mmdlm2, buffer)) >= 0; 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 if (write (md, ">", 1) < 0) {
400 advise (mailbox, "write");
401 }
402 size++;
403 }
404 i = strlen (buffer);
405 if (write (md, buffer, i) != i) {
406 fclose (fp);
407 return NOTOK;
408 }
409 if (mapping)
410 for (cp = buffer; i-- > 0; size++)
411 if (*cp++ == '\n')
412 size++;
413 }
414 if (write (md, "\n", 1) != 1) {
415 fclose (fp);
416 return NOTOK;
417 }
418 if (mapping)
419 size += 2;
420
421 fclose (fp);
422 lseek (fd, (off_t) 0, SEEK_END);
423 stop = lseek (md, (off_t) 0, SEEK_CUR);
424 if (mapping)
425 map_write (mailbox, md, 0, (long) 0, start, stop, pos, size, noisy);
426
427 return OK;
428 }
429 }
430
431
432 int
433 mbx_size (int md, off_t start, off_t stop)
434 {
435 register int i, fd;
436 register long pos;
437 register FILE *fp;
438
439 if ((fd = dup (md)) == NOTOK || (fp = fdopen (fd, "r")) == NULL) {
440 if (fd != NOTOK)
441 close (fd);
442 return NOTOK;
443 }
444
445 fseek (fp, start, SEEK_SET);
446 for (i = 0, pos = stop - start; pos-- > 0; i++)
447 if (fgetc (fp) == '\n')
448 i++;
449
450 fclose (fp);
451 return i;
452 }
453
454
455 /*
456 * Close and unlock file/maildrop.
457 */
458
459 int
460 mbx_close (char *mailbox, int md)
461 {
462 if (lkclosespool (md, mailbox) == 0)
463 return OK;
464 return NOTOK;
465 }
466
467
468 /*
469 * This function is performed implicitly by getbbent.c:
470 * bb->bb_map = map_name (bb->bb_file);
471 */
472
473 char *
474 map_name (char *file)
475 {
476 register char *cp, *dp;
477 static char buffer[BUFSIZ];
478
479 if ((dp = strchr(cp = r1bindex (file, '/'), '.')) == NULL)
480 dp = cp + strlen (cp);
481 if (cp == file)
482 snprintf (buffer, sizeof(buffer), ".%.*s%s", (int)(dp - cp), cp, ".map");
483 else
484 snprintf (buffer, sizeof(buffer), "%.*s.%.*s%s",
485 (int)(cp - file), file, (int)(dp - cp), cp, ".map");
486
487 return buffer;
488 }
489
490
491 int
492 map_read (char *file, long pos, struct drop **drops, int noisy)
493 {
494 register int i, md, msgp;
495 register char *cp;
496 struct drop d;
497 register struct drop *mp, *dp;
498
499 if ((md = open (cp = map_name (file), O_RDONLY)) == NOTOK
500 || map_chk (cp, md, mp = &d, pos, noisy)) {
501 if (md != NOTOK)
502 close (md);
503 return 0;
504 }
505
506 msgp = mp->d_id;
507 dp = (struct drop *) mh_xcalloc ((size_t) (msgp + 1), sizeof(*dp));
508 if (dp == NULL) {
509 close (md);
510 return 0;
511 }
512
513 memcpy((char *) dp, (char *) mp, sizeof(*dp));
514
515 lseek (md, (off_t) sizeof(*mp), SEEK_SET);
516 if ((i = read (md, (char *) (dp + 1), msgp * sizeof(*dp))) <
517 (int) sizeof(*dp)) {
518 i = 0;
519 free ((char *) dp);
520 } else {
521 #ifdef NTOHLSWAP
522 register struct drop *tdp;
523 int j;
524
525 for (j = 0, tdp = dp; j < i / sizeof(*dp); j++, tdp++) {
526 tdp->d_id = ntohl(tdp->d_id);
527 tdp->d_size = ntohl(tdp->d_size);
528 tdp->d_start = ntohl(tdp->d_start);
529 tdp->d_stop = ntohl(tdp->d_stop);
530 }
531 #endif
532 *drops = dp;
533 }
534
535 close (md);
536
537 return (i / sizeof(*dp));
538 }
539
540
541 int
542 map_write (char *mailbox, int md, int id, long last, off_t start,
543 off_t stop, long pos, int size, int noisy)
544 {
545 register int i;
546 int clear, fd, td;
547 char *file;
548 register struct drop *dp;
549 struct drop d1, d2, *rp;
550 register FILE *fp;
551 struct stat st;
552
553 if ((fd = map_open (file = map_name (mailbox), md)) == NOTOK)
554 return NOTOK;
555
556 if ((fstat (fd, &st) == OK) && (st.st_size > 0))
557 clear = 0;
558 else
559 clear = 1;
560
561 if (!clear && map_chk (file, fd, &d1, pos, noisy)) {
562 (void) m_unlink (file);
563 mbx_close (file, fd);
564 if ((fd = map_open (file, md)) == NOTOK)
565 return NOTOK;
566 clear++;
567 }
568
569 if (clear) {
570 if ((td = dup (md)) == NOTOK || (fp = fdopen (td, "r")) == NULL) {
571 if (noisy)
572 admonish (file, "unable to %s", td != NOTOK ? "fdopen" : "dup");
573 if (td != NOTOK)
574 close (td);
575 mbx_close (file, fd);
576 return NOTOK;
577 }
578
579 switch (i = mbx_read (fp, 0, &rp, noisy)) {
580 case NOTOK:
581 fclose (fp);
582 mbx_close (file, fd);
583 return NOTOK;
584
585 case OK:
586 fclose (fp);
587 break;
588
589 default:
590 d1.d_id = 0;
591 for (dp = rp; i-- >0; dp++) {
592 if (dp->d_start == start)
593 dp->d_id = id;
594 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), SEEK_SET);
595 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
596 if (noisy)
597 admonish (file, "write error");
598 mbx_close (file, fd);
599 fclose (fp);
600 return NOTOK;
601 }
602 }
603 free ((char *) rp);
604 fclose (fp);
605 break;
606 }
607 }
608 else {
609 if (last == 0)
610 last = d1.d_start;
611 dp = &d2;
612 dp->d_id = id;
613 dp->d_size = (long) (size ? size : mbx_size (fd, start, stop));
614 dp->d_start = start;
615 dp->d_stop = stop;
616 lseek (fd, (off_t) (++d1.d_id * sizeof(*dp)), 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
625 dp = &d1;
626 dp->d_size = DRVRSN;
627 dp->d_start = (long) last;
628 dp->d_stop = lseek (md, (off_t) 0, SEEK_CUR);
629
630 lseek (fd, (off_t) 0, SEEK_SET);
631 if (write (fd, (char *) dp, sizeof(*dp)) != sizeof(*dp)) {
632 if (noisy)
633 admonish (file, "write error");
634 mbx_close (file, fd);
635 return NOTOK;
636 }
637
638 mbx_close (file, fd);
639
640 return OK;
641 }
642
643
644 static int
645 map_open (char *file, int md)
646 {
647 mode_t mode;
648 struct stat st;
649
650 mode = fstat (md, &st) != NOTOK ? (int) (st.st_mode & 0777) : m_gmprot ();
651 return mbx_open (file, OTHER_FORMAT, st.st_uid, st.st_gid, mode);
652 }
653
654
655 int
656 map_chk (char *file, int fd, struct drop *dp, long pos, int noisy)
657 {
658 ssize_t count;
659 struct drop d, tmpd;
660 register struct drop *dl;
661
662 if (read (fd, (char *) &tmpd, sizeof(*dp)) != sizeof(*dp)) {
663 #ifdef notdef
664 admonish (NULL, "%s: missing or partial index", file);
665 #endif /* notdef */
666 return NOTOK;
667 }
668 #ifndef NTOHLSWAP
669 *dp = tmpd; /* if ntohl(n)=(n), can use struct assign */
670 #else
671 dp->d_id = ntohl(tmpd.d_id);
672 dp->d_size = ntohl(tmpd.d_size);
673 dp->d_start = ntohl(tmpd.d_start);
674 dp->d_stop = ntohl(tmpd.d_stop);
675 #endif
676
677 if (dp->d_size != DRVRSN) {
678 if (noisy)
679 admonish (NULL, "%s: version mismatch (%d != %d)", file,
680 dp->d_size, DRVRSN);
681 return NOTOK;
682 }
683
684 if (dp->d_stop != pos) {
685 if (noisy && pos != (long) 0)
686 admonish (NULL,
687 "%s: pointer mismatch or incomplete index (%ld!=%ld)",
688 file, dp->d_stop, (long) pos);
689 return NOTOK;
690 }
691
692 if ((long) ((dp->d_id + 1) * sizeof(*dp)) != (long) lseek (fd, (off_t) 0, SEEK_END)) {
693 if (noisy)
694 admonish (NULL, "%s: corrupt index(1)", file);
695 return NOTOK;
696 }
697
698 dl = &d;
699 count = strlen (mmdlm2);
700 lseek (fd, (off_t) (dp->d_id * sizeof(*dp)), SEEK_SET);
701 if (read (fd, (char *) dl, sizeof(*dl)) != sizeof(*dl)
702 || (ntohl(dl->d_stop) != dp->d_stop
703 && ntohl(dl->d_stop) + count != dp->d_stop)) {
704 if (noisy)
705 admonish (NULL, "%s: corrupt index(2)", file);
706 return NOTOK;
707 }
708
709 return OK;
710 }