Eliminate '#ifdef HAVE_FOO' for Posix-mandated FOOs.
[mmh] / 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 = lkopen (file, O_RDWR | O_CREAT | O_NONBLOCK, mode)) == NOTOK) {
52             switch (errno) {
53 #if defined(FCNTL_LOCKING) || defined(LOCKF_LOCKING)
54                 case EACCES:
55                 case EAGAIN:
56 #endif
57
58 #ifdef FLOCK_LOCKING
59                 case EWOULDBLOCK:
60 #endif
61                 case ETXTBSY: 
62                     j = errno;
63                     sleep (5);
64                     break;
65
66                 default: 
67                     /* just return error */
68                     return NOTOK;
69             }
70         }
71
72         /* good file descriptor */
73         break;
74     }
75
76     errno = j;
77
78     /*
79      * Return if we still failed after 4 attempts,
80      * or we just want to skip the sanity checks.
81      */
82     if (fd == NOTOK || mbx_style == OTHER_FORMAT)
83         return fd;
84
85     /*
86      * Do sanity checks on maildrop.
87      */
88     if (fstat (fd, &st) == NOTOK) {
89         /*
90          * The stat failed.  So we make sure file
91          * has right ownership/modes
92          */
93         chown (file, uid, gid);
94         chmod (file, mode);
95     } else if (st.st_size > (off_t) 0) {
96         int status;
97
98         /* check the maildrop */
99         switch (mbx_style) {
100             case MMDF_FORMAT: 
101             default: 
102                 status = mbx_chk_mmdf (fd);
103                 break;
104
105             case MBOX_FORMAT: 
106                 status = mbx_chk_mbox (fd);
107                 break;
108         }
109
110         /* if error, attempt to close it */
111         if (status == NOTOK) {
112             close (fd);
113             return NOTOK;
114         }
115     }
116
117     return fd;
118 }
119
120
121 /*
122  * Check/prepare MBOX style maildrop for appending.
123  */
124
125 static int
126 mbx_chk_mbox (int fd)
127 {
128     /* just seek to the end */
129     if (lseek (fd, (off_t) 0, SEEK_END) == (off_t) NOTOK)
130         return NOTOK;
131
132     return OK;
133 }
134
135
136 /*
137  * Check/prepare MMDF style maildrop for appending.
138  */
139
140 static int
141 mbx_chk_mmdf (int fd)
142 {
143     size_t count;
144     char ldelim[BUFSIZ];
145
146     count = strlen (mmdlm2);
147
148     /* casting -count to off_t, seem to break FreeBSD 2.2.6 */
149     if (lseek (fd, (long) (-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 (lkclose (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))) < 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         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 ? (mode_t) (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     long 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 = (long) 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 }