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