Debian Codegraph @

contrib/mod_tls_fscache.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_tls_fscache -- a module which provides a shared OCSP response
3 * cache using the filesystem
4 * Copyright (c) 2015-2016 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders and other respective copyright holders
21 * give permission to link this program with OpenSSL, and distribute the
22 * resulting executable, without including the source code for OpenSSL in the
23 * source distribution.
24 *
25 * This is mod_tls_fscache, contrib software for proftpd 1.3.x and above.
26 * For more information contact TJ Saunders <[email protected]>.
27 */
28
29 #include "conf.h"
30 #include "privs.h"
31 #include "mod_tls.h"
32
33 #define MOD_TLS_FSCACHE_VERSION "mod_tls_fscache/0.0"
34
35 /* Make sure the version of proftpd is as necessary. */
36 #if PROFTPD_VERSION_NUMBER < 0x0001030602
37 # error "ProFTPD 1.3.6rc2 or later required"
38 #endif
39
40 extern xaset_t *server_list;
41
42 module tls_fscache_module;
43
44 #if defined(PR_USE_OPENSSL_OCSP)
45 static tls_ocsp_cache_t ocsp_cache;
46 #endif /* PR_USE_OPENSSL_OCSP */
47
48 static const char *trace_channel = "tls.fscache";
49
50 #if defined(PR_USE_OPENSSL_OCSP)
51 static const char *fscache_get_errors(void) {
52 unsigned int count = 0;
53 unsigned long error_code;
54 BIO *bio = NULL;
55 char *data = NULL;
56 long datalen;
57 const char *error_data = NULL, *str = "(unknown)";
58 int error_flags = 0;
59
60 /* Use ERR_print_errors() and a memory BIO to build up a string with
61 * all of the error messages from the error queue.
62 */
63
64 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
65 if (error_code) {
66 bio = BIO_new(BIO_s_mem());
67 }
68
69 while (error_code) {
70 pr_signals_handle();
71
72 if (error_flags & ERR_TXT_STRING) {
73 BIO_printf(bio, "\n (%u) %s [%s]", ++count,
74 ERR_error_string(error_code, NULL), error_data);
75
76 } else {
77 BIO_printf(bio, "\n (%u) %s", ++count,
78 ERR_error_string(error_code, NULL));
79 }
80
81 error_data = NULL;
82 error_flags = 0;
83 error_code = ERR_get_error_line_data(NULL, NULL, &error_data, &error_flags);
84 }
85
86 datalen = BIO_get_mem_data(bio, &data);
87 if (data) {
88 data[datalen] = '\0';
89 str = pstrdup(permanent_pool, data);
90 }
91
92 if (bio != NULL) {
93 BIO_free(bio);
94 }
95
96 return str;
97 }
98
99 /* OCSP Cache implementation callbacks.
100 */
101
102 static int ocsp_cache_open(tls_ocsp_cache_t *cache, char *info) {
103 int res, xerrno = 0;
104 struct stat st;
105
106 pr_trace_msg(trace_channel, 9, "opening fscache cache %p", cache);
107
108 /* The info string must be formatted like:
109 *
110 * /path=%s
111 */
112
113 if (strncmp(info, "/path=", 6) != 0) {
114 pr_log_pri(PR_LOG_NOTICE, MOD_TLS_FSCACHE_VERSION
115 ": badly formatted info '%s', unable to open fscache", info);
116 errno = EINVAL;
117 return -1;
118 }
119
120 info += 6;
121
122 if (pr_fs_valid_path(info) < 0) {
123 pr_log_pri(PR_LOG_NOTICE, MOD_TLS_FSCACHE_VERSION
124 ": path '%s' not an absolute path", info);
125
126 errno = EINVAL;
127 return -1;
128 }
129
130 res = lstat(info, &st);
131 if (res < 0) {
132 xerrno = errno;
133
134 pr_log_pri(PR_LOG_NOTICE, MOD_TLS_FSCACHE_VERSION
135 ": unable to check '%s': %s", info, strerror(xerrno));
136
137 errno = xerrno;
138 return -1;
139 }
140
141 if (!S_ISDIR(st.st_mode)) {
142 xerrno = ENOTDIR;
143
144 pr_log_pri(PR_LOG_NOTICE, MOD_TLS_FSCACHE_VERSION
145 ": unable to use '%s': %s", info, strerror(xerrno));
146
147 errno = xerrno;
148 return -1;
149 }
150
151 /* Make sure that the directory is not world-writable; we don't want
152 * any/all users on the system to be able to futz with these.
153 */
154 if (st.st_mode & S_IWOTH) {
155 xerrno = EPERM;
156
157 pr_log_pri(PR_LOG_NOTICE, MOD_TLS_FSCACHE_VERSION
158 ": unable to use world-writable '%s' (perms %04o)", info,
159 st.st_mode & ~S_IFMT);
160
161 errno = xerrno;
162 return -1;
163 }
164
165 if (cache->cache_pool != NULL) {
166 char *prev_cache_dir;
167
168 /* XXX Are we a restarted server if this happens?
169 *
170 * If so, AND if cache->cache_data is NOT NULL, AND points to a
171 * directory which is NOT 'path', then should we clean up that
172 * other directory? (No; it could be used by another process on
173 * the machine, e.g. multiple different proftpd servers.)
174 *
175 * For now, we complain about this, and tell the admin to manually remove
176 * the old directory.
177 */
178
179 prev_cache_dir = cache->cache_data;
180 if (prev_cache_dir != NULL &&
181 strcmp(prev_cache_dir, info) != 0) {
182 pr_log_pri(PR_LOG_DEBUG, MOD_TLS_FSCACHE_VERSION
183 ": path '%s' does not match previously configured path '%s'",
184 info, prev_cache_dir);
185 }
186
187 destroy_pool(cache->cache_pool);
188 }
189
190 cache->cache_pool = make_sub_pool(session.pool);
191 pr_pool_tag(cache->cache_pool, MOD_TLS_FSCACHE_VERSION);
192
193 cache->cache_data = pstrdup(cache->cache_pool, info);
194 return 0;
195 }
196
197 static int ocsp_cache_close(tls_ocsp_cache_t *cache) {
198
199 if (cache != NULL) {
200 pr_trace_msg(trace_channel, 9, "closing fscache cache %p", cache);
201
202 if (cache->cache_pool != NULL) {
203 destroy_pool(cache->cache_pool);
204
205 /* XXX TODO */
206 }
207 }
208
209 return 0;
210 }
211
212 static int ocsp_cache_add(tls_ocsp_cache_t *cache, const char *fingerprint,
213 OCSP_RESPONSE *resp, time_t resp_age) {
214 int fd, res, resp_derlen = -1, xerrno = 0;
215 unsigned char *resp_der = NULL;
216 const char *cache_dir;
217 char *path, *tmpl;
218 pool *tmp_pool;
219 struct timeval tvs[2];
220
221 pr_trace_msg(trace_channel, 9, "adding OCSP response to fscache cache %p",
222 cache);
223
224 resp_derlen = i2d_OCSP_RESPONSE(resp, &resp_der);
225 if (resp_derlen <= 0) {
226 pr_trace_msg(trace_channel, 1,
227 "error DER-encoding OCSP response: %s", fscache_get_errors());
228 errno = EINVAL;
229 return -1;
230 }
231
232 cache_dir = cache->cache_data;
233 tmp_pool = make_sub_pool(cache->cache_pool);
234 pr_pool_tag(tmp_pool, "OCSP fscache add pool");
235
236 tmpl = pdircat(tmp_pool, cache_dir, "XXXXXX", NULL);
237 fd = mkstemp(tmpl);
238 if (fd < 0) {
239 xerrno = errno;
240
241 pr_trace_msg(trace_channel, 1,
242 "unable to obtain secure temporary file for OCSP response: %s",
243 strerror(xerrno));
244
245 destroy_pool(tmp_pool);
246 errno = xerrno;
247 return -1;
248 }
249
250 pr_trace_msg(trace_channel, 15,
251 "writing OCSP response to temporary file '%s'", tmpl);
252
253 res = write(fd, resp_der, resp_derlen);
254 if (res != resp_derlen) {
255 if (res < 0) {
256 xerrno = errno;
257
258 pr_trace_msg(trace_channel, 1,
259 "error writing OCSP response to '%s' (fd %d): %s", tmpl, fd,
260 strerror(xerrno));
261 errno = xerrno;
262
263 } else {
264 /* XXX Deal with short writes? */
265
266 pr_trace_msg(trace_channel, 1,
267 "only wrote %d of %d bytes of OCSP response to '%s' (fd %d)", res,
268 resp_derlen, tmpl, fd);
269 xerrno = EIO;
270 }
271
272 (void) unlink(tmpl);
273 (void) close(fd);
274 destroy_pool(tmp_pool);
275
276 errno = xerrno;
277 return -1;
278 }
279
280 res = close(fd);
281 if (res < 0) {
282 xerrno = errno;
283
284 pr_trace_msg(trace_channel, 1,
285 "error writing OCSP response to '%s': %s", tmpl, strerror(xerrno));
286
287 (void) unlink(tmpl);
288 destroy_pool(tmp_pool);
289
290 errno = xerrno;
291 return -1;
292 }
293
294 /* We ASSUME that rename(2) does not modify the mtime of the file. Ideally
295 * we would futimes(2) on the file descriptor, but calling close(2) on that
296 * fd might also change the mtime (due to close flushing out buffered data),
297 * thus we use the path.
298 */
299 tvs[0].tv_sec = tvs[1].tv_sec = resp_age;
300 tvs[0].tv_usec = tvs[1].tv_usec = 0;
301 res = utimes(tmpl, tvs);
302 if (res < 0) {
303 pr_trace_msg(trace_channel, 9,
304 "error setting atime/mtime on '%s' to %lu secs: %s", tmpl,
305 (unsigned long) resp_age, strerror(errno));
306 }
307
308 /* Atomically rename the temporary file into place. */
309 path = pstrcat(tmp_pool, cache_dir, "/", fingerprint, ".der", NULL);
310 res = rename(tmpl, path);
311 if (res < 0) {
312 xerrno = errno;
313
314 pr_trace_msg(trace_channel, 1,
315 "error renaming '%s' to '%s': %s", tmpl, path, strerror(xerrno));
316
317 (void) unlink(tmpl);
318
319 } else {
320 pr_trace_msg(trace_channel, 15, "renamed '%s' to '%s' (%d bytes)", tmpl,
321 path, resp_derlen);
322 }
323
324 destroy_pool(tmp_pool);
325 errno = xerrno;
326 return res;
327 }
328
329 static OCSP_RESPONSE *ocsp_cache_get(tls_ocsp_cache_t *cache,
330 const char *fingerprint, time_t *resp_age) {
331 int res, xerrno = 0;
332 const char *cache_dir, *path;
333 pool *tmp_pool;
334 BIO *bio = NULL;
335 OCSP_RESPONSE *resp = NULL;
336 struct stat st;
337 pr_fh_t *fh;
338
339 pr_trace_msg(trace_channel, 9, "getting OCSP response from fscache cache %p",
340 cache);
341
342 cache_dir = cache->cache_data;
343 tmp_pool = make_sub_pool(cache->cache_pool);
344 pr_pool_tag(tmp_pool, "OCSP fscache retrieval pool");
345
346 path = pstrcat(tmp_pool, cache_dir, "/", fingerprint, ".der", NULL);
347 pr_trace_msg(trace_channel, 15, "getting OCSP response at path '%s'", path);
348
349 fh = pr_fsio_open(path, O_RDONLY);
350 if (fh == NULL) {
351 xerrno = errno;
352
353 pr_trace_msg(trace_channel, 3, "error opening '%s': %s", path,
354 strerror(xerrno));
355
356 destroy_pool(tmp_pool);
357 errno = xerrno;
358 return NULL;
359 }
360
361 res = pr_fsio_fstat(fh, &st);
362 if (res < 0) {
363 xerrno = errno;
364
365 pr_trace_msg(trace_channel, 3, "error checking '%s': %s", path,
366 strerror(xerrno));
367
368 (void) pr_fsio_close(fh);
369 destroy_pool(tmp_pool);
370 errno = xerrno;
371 return NULL;
372 }
373
374 /* No symlinks or directories, only regular files. */
375 if (!S_ISREG(st.st_mode)) {
376 pr_trace_msg(trace_channel, 3, "path '%s' is NOT a regular file", path);
377
378 /* If it's a symlink, remove it. We cannot just do the same with
379 * a directory.
380 */
381 if (S_ISLNK(st.st_mode)) {
382 (void) unlink(path);
383 }
384
385 (void) pr_fsio_close(fh);
386 destroy_pool(tmp_pool);
387 errno = ENOENT;
388 return NULL;
389 }
390
391 bio = BIO_new_file(path, "r");
392 if (bio == NULL) {
393 xerrno = errno;
394
395 tls_log(MOD_TLS_FSCACHE_VERSION ": BIO_new_file('%s') failed: %s", path,
396 fscache_get_errors());
397 (void) pr_fsio_close(fh);
398 destroy_pool(tmp_pool);
399
400 errno = xerrno;
401 return NULL;
402 }
403
404 resp = d2i_OCSP_RESPONSE_bio(bio, NULL);
405 if (resp == NULL) {
406 pr_trace_msg(trace_channel, 3,
407 "error reading valid OCSP response from path '%s': %s", path,
408 fscache_get_errors());
409
410 /* If we can't read a valid OCSP response from this file, delete it. */
411 (void) unlink(path);
412
413 (void) pr_fsio_close(fh);
414 destroy_pool(tmp_pool);
415 errno = ENOENT;
416 return NULL;
417 }
418
419 BIO_free(bio);
420
421 /* Use the mtime of the file to determine how old it is. */
422 *resp_age = st.st_mtime;
423
424 (void) pr_fsio_close(fh);
425 destroy_pool(tmp_pool);
426 errno = xerrno;
427 return resp;
428 }
429
430 static int ocsp_cache_delete(tls_ocsp_cache_t *cache, const char *fingerprint) {
431 int res, xerrno = 0;
432 const char *cache_dir, *path;
433 pool *tmp_pool;
434
435 pr_trace_msg(trace_channel, 9,
436 "removing OCSP response from fscache cache %p", cache);
437
438 cache_dir = cache->cache_data;
439 tmp_pool = make_sub_pool(cache->cache_pool);
440 pr_pool_tag(tmp_pool, "OCSP fscache delete pool");
441
442 path = pstrcat(tmp_pool, cache_dir, "/", fingerprint, ".der", NULL);
443 pr_trace_msg(trace_channel, 15, "deleting OCSP response at path '%s'", path);
444
445 /* XXX Do we need root privs here? Should we use the FSIO API? */
446 res = unlink(path);
447 xerrno = errno;
448
449 destroy_pool(tmp_pool);
450 errno = xerrno;
451 return res;
452 }
453
454 static int ocsp_cache_clear(tls_ocsp_cache_t *cache) {
455 int res, xerrno = 0;
456 const char *cache_dir;
457 pool *tmp_pool;
458 DIR *dirh;
459 struct dirent *dent;
460
461 pr_trace_msg(trace_channel, 9, "clearing fscache cache %p", cache);
462
463 cache_dir = cache->cache_data;
464
465 dirh = opendir(cache_dir);
466 if (dirh == NULL) {
467 xerrno = errno;
468
469 pr_trace_msg(trace_channel, 3, "unable to open directory '%s': %s",
470 cache_dir, strerror(xerrno));
471
472 errno = xerrno;
473 return -1;
474 }
475
476 tmp_pool = make_sub_pool(cache->cache_pool);
477 pr_pool_tag(tmp_pool, "OCSP fscache clear pool");
478
479 dent = readdir(dirh);
480 while (dent != NULL) {
481 struct stat st;
482 size_t namelen;
483
484 pr_signals_handle();
485
486 namelen = strlen(dent->d_name);
487 /* Skip any path which does not end in ".der". */
488 if (pr_strnrstr(dent->d_name, namelen, ".der", 4, 0) == TRUE) {
489 pr_fh_t *fh;
490 char *path;
491
492 path = pstrcat(tmp_pool, cache_dir, "/", dent->d_name, ".der", NULL);
493
494 fh = pr_fsio_open(path, O_RDONLY);
495 if (fh != NULL) {
496 res = pr_fsio_fstat(fh, &st);
497 if (res < 0) {
498 pr_trace_msg(trace_channel, 3, "error checking path '%s': %s", path,
499 strerror(errno));
500
501 } else {
502 if (S_ISREG(st.st_mode) ||
503 S_ISLNK(st.st_mode)) {
504
505 pr_trace_msg(trace_channel, 15,
506 "deleting OCSP response at path '%s'", path);
507 res = unlink(path);
508 if (res < 0) {
509 pr_trace_msg(trace_channel, 3,
510 "error deleting path '%s': %s", path, strerror(errno));
511 }
512
513 } else {
514 pr_trace_msg(trace_channel, 3,
515 "ignoring non-file/symlink path '%s'", path);
516 }
517 }
518
519 (void) pr_fsio_close(fh);
520
521 } else {
522 pr_trace_msg(trace_channel, 3, "error opening path '%s': %s", path,
523 strerror(errno));
524 }
525 }
526
527 dent = readdir(dirh);
528 }
529
530 (void) closedir(dirh);
531 destroy_pool(tmp_pool);
532
533 return 0;
534 }
535
536 static int ocsp_cache_remove(tls_ocsp_cache_t *cache) {
537 int res;
538
539 if (cache == NULL) {
540 errno = EINVAL;
541 return -1;
542 }
543
544 pr_trace_msg(trace_channel, 9, "removing fscache cache %p", cache);
545 res = ocsp_cache_clear(cache);
546 return res;
547 }
548
549 static int ocsp_cache_status(tls_ocsp_cache_t *cache,
550 void (*statusf)(void *, const char *, ...), void *arg, int flags) {
551 int res, xerrno = 0;
552 unsigned int resp_count = 0;
553 const char *cache_dir;
554 pool *tmp_pool;
555 DIR *dirh;
556 struct dirent *dent;
557
558 pr_trace_msg(trace_channel, 9, "checking fscache cache %p", cache);
559
560 /* XXX TODO:
561 * If flags says "SHOW RESPONSES", print out the PEM versions of the
562 * responses?
563 */
564
565 cache_dir = cache->cache_data;
566
567 dirh = opendir(cache_dir);
568 if (dirh == NULL) {
569 xerrno = errno;
570
571 pr_trace_msg(trace_channel, 3, "unable to open directory '%s': %s",
572 cache_dir, strerror(xerrno));
573
574 errno = xerrno;
575 return -1;
576 }
577
578 tmp_pool = make_sub_pool(cache->cache_pool);
579 pr_pool_tag(tmp_pool, "OCSP fscache status pool");
580
581 dent = readdir(dirh);
582 while (dent != NULL) {
583 struct stat st;
584 size_t namelen;
585
586 pr_signals_handle();
587
588 /* Skip any path which does not end in ".der". */
589 namelen = strlen(dent->d_name);
590 if (pr_strnrstr(dent->d_name, namelen, ".der", 4, 0) == TRUE) {
591 pr_fh_t *fh;
592 char *path;
593
594 path = pstrcat(tmp_pool, cache_dir, "/", dent->d_name, ".der", NULL);
595
596 fh = pr_fsio_open(path, O_RDONLY);
597 if (fh != NULL) {
598 res = pr_fsio_fstat(fh, &st);
599 if (res < 0) {
600 pr_trace_msg(trace_channel, 3, "error checking path '%s': %s", path,
601 strerror(errno));
602
603 } else {
604 if (S_ISREG(st.st_mode) ||
605 S_ISLNK(st.st_mode)) {
606 resp_count++;
607
608 } else {
609 pr_trace_msg(trace_channel, 3,
610 "ignoring non-file/symlink path '%s'", path);
611 }
612 }
613
614 (void) pr_fsio_close(fh);
615
616 } else {
617 pr_trace_msg(trace_channel, 3, "error opening path '%s': %s", path,
618 strerror(errno));
619 }
620 }
621
622 dent = readdir(dirh);
623 }
624
625 (void) closedir(dirh);
626 destroy_pool(tmp_pool);
627
628 statusf(arg, "%s", "Filesystem (fs) OCSP response cache provided by "
629 MOD_TLS_FSCACHE_VERSION);
630 statusf(arg, "%s", "");
631 statusf(arg, "Current OCSP responses cached: %u", resp_count);
632
633 return 0;
634 }
635 #endif /* PR_USE_OPENSSL_OCSP */
636
637 /* Event Handlers
638 */
639
640 #if defined(PR_SHARED_MODULE)
641 static void fscache_mod_unload_ev(const void *event_data, void *user_data) {
642 if (strcmp("mod_tls_fscache.c", (const char *) event_data) == 0) {
643 pr_event_unregister(&tls_fscache_module, NULL, NULL);
644 (void) tls_ocsp_cache_unregister("fs");
645 }
646 }
647 #endif /* !PR_SHARED_MODULE */
648
649 /* Initialization functions
650 */
651
652 static int tls_fscache_init(void) {
653 #if defined(PR_USE_OPENSSL_OCSP)
654 # if defined(PR_SHARED_MODULE)
655 pr_event_register(&tls_fscache_module, "core.module-unload",
656 fscache_mod_unload_ev, NULL);
657 # endif /* !PR_SHARED_MODULE */
658
659 /* Prepare our cache handler. */
660 memset(&ocsp_cache, 0, sizeof(ocsp_cache));
661 ocsp_cache.open = ocsp_cache_open;
662 ocsp_cache.close = ocsp_cache_close;
663 ocsp_cache.add = ocsp_cache_add;
664 ocsp_cache.get = ocsp_cache_get;
665 ocsp_cache.delete = ocsp_cache_delete;
666 ocsp_cache.clear = ocsp_cache_clear;
667 ocsp_cache.remove = ocsp_cache_remove;
668 ocsp_cache.status = ocsp_cache_status;
669
670 /* Register ourselves with mod_tls. */
671 if (tls_ocsp_cache_register("fs", &ocsp_cache) < 0) {
672 int xerrno = errno;
673
674 pr_log_pri(PR_LOG_NOTICE, MOD_TLS_FSCACHE_VERSION
675 ": notice: error registering 'fs' OCSP cache: %s", strerror(xerrno));
676
677 errno = xerrno;
678 return -1;
679 }
680 #endif /* PR_USE_OPENSSL_OCSP */
681
682 return 0;
683 }
684
685 /* Module API tables
686 */
687
688 module tls_fscache_module = {
689 NULL, NULL,
690
691 /* Module API version 2.0 */
692 0x20,
693
694 /* Module name */
695 "tls_fscache",
696
697 /* Module configuration handler table */
698 NULL,
699
700 /* Module command handler table */
701 NULL,
702
703 /* Module authentication handler table */
704 NULL,
705
706 /* Module initialization function */
707 tls_fscache_init,
708
709 /* Session initialization function */
710 NULL,
711
712 /* Module version */
713 MOD_TLS_FSCACHE_VERSION
714 };