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