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