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