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