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