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