100f9729c152a0df7788acc7624aed8616dcdc0f
[mmh] / uip / mhcachesbr.c
1 /*
2 ** mhcachesbr.c -- routines to manipulate the MIME content cache
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/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <h/md5.h>
13 #include <errno.h>
14 #include <setjmp.h>
15 #include <signal.h>
16 #include <h/mts.h>
17 #include <h/tws.h>
18 #include <h/mime.h>
19 #include <h/mhparse.h>
20 #include <h/mhcachesbr.h>
21 #include <h/utils.h>
22
23 #ifdef TIME_WITH_SYS_TIME
24 # include <sys/time.h>
25 # include <time.h>
26 #else
27 # ifdef TM_IN_SYS_TIME
28 #  include <sys/time.h>
29 # else
30 #  include <time.h>
31 # endif
32 #endif
33
34 #ifdef HAVE_SYS_WAIT_H
35 # include <sys/wait.h>
36 #endif
37
38 extern int debugsw;
39
40 extern pid_t xpid;  /* mhshowsbr.c or mhbuildsbr.c */
41
42 /* cache policies */
43 int rcachesw = CACHE_ASK;
44 int wcachesw = CACHE_ASK;
45
46 /*
47 ** Location of public and private cache.  These must
48 ** be set before these routines are called.
49 */
50 char *cache_public;
51 char *cache_private;
52
53
54 /* mhparse.c (OR) mhbuildsbr.c */
55 int pidcheck (int);
56
57 /* mhmisc.c */
58 int part_ok (CT, int);
59 int type_ok (CT, int);
60 int make_intermediates (char *);
61 void content_error (char *, CT, char *, ...);
62 void flush_errors (void);
63
64 /*
65 ** prototypes
66 */
67 void cache_all_messages (CT *);
68 int find_cache (CT, int, int *, char *, char *, int);
69
70 /*
71 ** static prototypes
72 */
73 static void cache_content (CT);
74 static int find_cache_aux (int, char *, char *, char *, int);
75 static int find_cache_aux2 (char *, char *, char *, int);
76
77
78 /*
79 ** Top level entry point to cache content
80 ** from a group of messages
81 */
82
83 void
84 cache_all_messages (CT *cts)
85 {
86         CT ct, *ctp;
87
88         for (ctp = cts; *ctp; ctp++) {
89                 ct = *ctp;
90                 if (type_ok (ct, 1)) {
91                         cache_content (ct);
92                         if (ct->c_fp) {
93                                 fclose (ct->c_fp);
94                                 ct->c_fp = NULL;
95                         }
96                         if (ct->c_ceclosefnx)
97                                 (*ct->c_ceclosefnx) (ct);
98                 }
99         }
100         flush_errors ();
101 }
102
103
104 /*
105 ** Entry point to cache content from external sources.
106 */
107
108 static void
109 cache_content (CT ct)
110 {
111         int cachetype;
112         char *file, cachefile[BUFSIZ];
113         CE ce = ct->c_cefile;
114
115         if (!ct->c_id) {
116                 advise (NULL, "no %s: field in %s", ID_FIELD, ct->c_file);
117                 return;
118         }
119
120         if (!ce) {
121                 advise (NULL, "unable to decode %s", ct->c_file);
122                 return;
123         }
124
125 /* THIS NEEDS TO BE FIXED */
126 #if 0
127         if (ct->c_ceopenfnx == openMail) {
128                 advise (NULL, "a radish may no know Greek, but I do...");
129                 return;
130         }
131 #endif
132
133         if (find_cache (NULL, wcachesw != CACHE_NEVER ? wcachesw : CACHE_ASK,
134                         &cachetype, ct->c_id, cachefile, sizeof(cachefile))
135                         == NOTOK) {
136                 advise (NULL, "unable to cache %s's contents", ct->c_file);
137                 return;
138         }
139         if (wcachesw != CACHE_NEVER && wcachesw != CACHE_ASK) {
140                 fflush (stdout);
141                 fprintf (stderr, "caching message %s as file %s\n", ct->c_file,
142                         cachefile);
143         }
144
145         if (ce->ce_file) {
146                 int mask = umask (cachetype ? ~m_gmprot () : 0222);
147                 FILE *fp;
148
149                 if (debugsw)
150                         fprintf (stderr, "caching by copying %s...\n",
151                                         ce->ce_file);
152
153                 file = NULL;
154                 if ((*ct->c_ceopenfnx) (ct, &file) == NOTOK)
155                         goto reset_umask;
156
157                 if ((fp = fopen (cachefile, "w"))) {
158                         int cc;
159                         char buffer[BUFSIZ];
160                         FILE *gp = ce->ce_fp;
161
162                         fseek (gp, 0L, SEEK_SET);
163
164                         while ((cc = fread (buffer, sizeof(*buffer),
165                                 sizeof(buffer), gp)) > 0)
166                                 fwrite (buffer, sizeof(*buffer), cc, fp);
167                         fflush (fp);
168
169                         if (ferror (gp)) {
170                                 admonish (ce->ce_file, "error reading");
171                                 unlink (cachefile);
172                         } else {
173                                 if (ferror (fp)) {
174                                         admonish (cachefile, "error writing");
175                                         unlink (cachefile);
176                                 }
177                         }
178                         fclose (fp);
179                 } else
180                         content_error (cachefile, ct,
181                                         "unable to fopen for writing");
182 reset_umask:
183                 umask (mask);
184         } else {
185                 if (debugsw)
186                         fprintf (stderr, "in place caching...\n");
187
188                 file = cachefile;
189                 if ((*ct->c_ceopenfnx) (ct, &file) != NOTOK)
190                         chmod (cachefile, cachetype ? m_gmprot () : 0444);
191         }
192 }
193
194
195 int
196 find_cache (CT ct, int policy, int *writing, char *id,
197         char *buffer, int buflen)
198 {
199         int status = NOTOK;
200
201         if (id == NULL)
202                 return NOTOK;
203         id = trimcpy (id);
204
205         if (debugsw)
206                 fprintf (stderr, "find_cache %s(%d) %s %s\n",
207                         caches[policy].sw, policy,
208                         writing ? "writing" : "reading", id);
209
210         switch (policy) {
211                 case CACHE_NEVER:
212                 default:
213                         break;
214
215                 case CACHE_ASK:
216                 case CACHE_PUBLIC:
217                         if (cache_private && !writing
218                                 && find_cache_aux (writing ? 2 : 0,
219                                 cache_private, id, buffer, buflen) == OK) {
220                                 if (access (buffer, R_OK) != NOTOK) {
221 got_private:
222                                         if (writing)
223                                                 *writing = 1;
224 got_it:
225                                         status = OK;
226                                         break;
227                                 }
228                         }
229                         if (cache_public && find_cache_aux (writing ? 1 : 0,
230                                 cache_public, id, buffer, buflen) == OK) {
231                                 if (writing || access (buffer, R_OK) != NOTOK) {
232                                         if (writing)
233                                                 *writing = 0;
234                                         goto got_it;
235                                 }
236                         }
237                         break;
238
239                 case CACHE_PRIVATE:
240                         if (cache_private && find_cache_aux (writing ? 2 : 0,
241                                 cache_private, id, buffer, buflen) == OK) {
242                                 if (writing || access (buffer, R_OK) != NOTOK)
243                                         goto got_private;
244                         }
245                         break;
246
247         }
248
249         if (status == OK && policy == CACHE_ASK) {
250                 int len, buflen;
251                 char *bp, query[BUFSIZ];
252
253                 if (xpid) {
254                         if (xpid < 0)
255                                 xpid = -xpid;
256                         pidcheck (pidwait (xpid, NOTOK));
257                         xpid = 0;
258                 }
259
260                 /* Get buffer ready to go */
261                 bp = query;
262                 buflen = sizeof(query);
263
264                 /* Now, construct query */
265                 if (writing) {
266                         snprintf (bp, buflen, "Make cached, publically-accessible copy");
267                 } else {
268                         struct stat st;
269
270                         snprintf (bp, buflen, "Use cached copy");
271                         len = strlen (bp);
272                         bp += len;
273                         buflen -= len;
274
275                         if (ct->c_partno) {
276                                 snprintf (bp, buflen, " of content %s", ct->c_partno);
277                                 len = strlen (bp);
278                                 bp += len;
279                                 buflen -= len;
280                         }
281
282                         stat (buffer, &st);
283                         snprintf (bp, buflen, " (size %lu octets)",
284                                 (unsigned long) st.st_size);
285                 }
286                 len = strlen (bp);
287                 bp += len;
288                 buflen -= len;
289
290                 snprintf (bp, buflen, "\n    in file %s? ", buffer);
291
292                 /* Now, check answer */
293                 if (!getanswer (query))
294                         status = NOTOK;
295         }
296
297         if (status == OK && writing) {
298                 if (*writing && strchr(buffer, '/'))
299                         make_intermediates (buffer);
300                 unlink (buffer);
301         }
302
303         free (id);
304         return status;
305 }
306
307
308 static int
309 find_cache_aux (int writing, char *directory, char *id,
310         char *buffer, int buflen)
311 {
312         int mask, usemap;
313         char mapfile[BUFSIZ], mapname[BUFSIZ];
314         FILE *fp;
315         static int partno, pid;
316         static time_t clock = 0;
317
318 #ifdef BSD42
319         usemap = strchr (id, '/') ? 1 : 0;
320 #else
321         usemap = 1;
322 #endif
323
324         if (debugsw)
325                 fprintf (stderr, "find_cache_aux %s usemap=%d\n", directory, usemap);
326
327         snprintf (mapfile, sizeof(mapfile), "%s/cache.map", directory);
328         if (find_cache_aux2 (mapfile, id, mapname, sizeof(mapname)) == OK)
329                 goto done_map;
330
331         if (!writing) {
332                 if (usemap)
333                         return NOTOK;
334
335 use_raw:
336                 snprintf (buffer, buflen, "%s/%s", directory, id);
337                 return OK;
338         }
339
340         if (!usemap && access (mapfile, W_OK) == NOTOK)
341                 goto use_raw;
342
343         if (clock != 0) {
344                 time_t now;
345
346                 time (&now);
347                 if (now > clock)
348                         clock = 0;
349         } else {
350                 pid = getpid ();
351         }
352
353         if (clock == 0) {
354                 time (&clock);
355                 partno = 0;
356         } else {
357                 if (partno > 0xff) {
358                         clock++;
359                         partno = 0;
360                 }
361         }
362
363         snprintf (mapname, sizeof(mapname), "%08x%04x%02x",
364                 (unsigned int) (clock & 0xffffffff),
365                 (unsigned int) (pid & 0xffff),
366                 (unsigned int) (partno++ & 0xff));
367
368         if (debugsw)
369                 fprintf (stderr, "creating mapping %s->%s\n", mapname, id);
370
371         make_intermediates (mapfile);
372         mask = umask (writing == 2 ? 0077 : 0);
373         if (!(fp = lkfopen (mapfile, "a")) && errno == ENOENT) {
374                 int fd;
375
376                 if ((fd = creat (mapfile, 0666)) != NOTOK) {
377                         close (fd);
378                         fp = lkfopen (mapfile, "a");
379                 }
380         }
381         umask (mask);
382         if (!fp)
383                 return NOTOK;
384         fprintf (fp, "%s: %s\n", mapname, id);
385         lkfclose (fp, mapfile);
386
387 done_map:
388         if (*mapname == '/')
389                 strncpy (buffer, mapname, buflen);
390         else
391                 snprintf (buffer, buflen, "%s/%s", directory, mapname);
392         if (debugsw)
393                 fprintf (stderr, "use %s\n", buffer);
394
395         return OK;
396 }
397
398
399 static int
400 find_cache_aux2 (char *mapfile, char *id, char *mapname, int namelen)
401 {
402         int state;
403         char buf[BUFSIZ], name[NAMESZ];
404         FILE *fp;
405
406         if (!(fp = lkfopen (mapfile, "r")))
407                 return NOTOK;
408
409         for (state = FLD;;) {
410                 int result;
411                 char *cp, *dp;
412
413                 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
414                         case FLD:
415                         case FLDPLUS:
416                         case FLDEOF:
417                                 strncpy (mapname, name, namelen);
418                                 if (state != FLDPLUS)
419                                         cp = buf;
420                                 else {
421                                         cp = add (buf, NULL);
422                                         while (state == FLDPLUS) {
423                                                 state = m_getfld (state, name, buf, sizeof(buf), fp);
424                                                 cp = add (buf, cp);
425                                         }
426                                 }
427                                 dp = trimcpy (cp);
428                                 if (cp != buf)
429                                         free (cp);
430                                 if (debugsw)
431                                         fprintf (stderr, "compare %s to %s <- %s\n", id, dp,
432                                                          mapname);
433                                 result = strcmp (id, dp);
434                                 free (dp);
435                                 if (result == 0) {
436                                         lkfclose (fp, mapfile);
437                                         return OK;
438                                 }
439                                 if (state != FLDEOF)
440                                         continue;
441                                 /* else fall... */
442
443                         case BODY:
444                         case BODYEOF:
445                         case FILEEOF:
446                         default:
447                                 break;
448                 }
449                 break;
450         }
451
452         lkfclose (fp, mapfile);
453         return NOTOK;
454 }