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