File: redis.c

package info (click to toggle)
proftpd-mod-proxy 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,476 kB
  • sloc: ansic: 43,512; perl: 43,487; sh: 3,479; makefile: 248
file content (374 lines) | stat: -rw-r--r-- 9,684 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * ProFTPD - mod_proxy TLS Redis implementation
 * Copyright (c) 2017-2020 TJ Saunders
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 *
 * As a special exemption, TJ Saunders and other respective copyright holders
 * give permission to link this program with OpenSSL, and distribute the
 * resulting executable, without including the source code for OpenSSL in the
 * source distribution.
 */

#include "mod_proxy.h"

#include "redis.h"
#include "proxy/tls.h"
#include "proxy/tls/redis.h"

#ifdef PR_USE_OPENSSL

extern xaset_t *server_list;

static const char *trace_channel = "proxy.tls.redis";

static void *redis_prefix = NULL;
static size_t redis_prefixsz = 0;
static unsigned long redis_opts = 0UL;

static char *make_key(pool *p, unsigned int vhost_id) {
  char *key;
  size_t keysz;

  keysz = 64;
  key = pcalloc(p, keysz + 1);
  snprintf(key, keysz, "proxy_tls_sessions:vhost#%u", vhost_id);

  return key;
}

static int tls_redis_add_sess(pool *p, void *redis, const char *sess_key,
    SSL_SESSION *sess) {
  int res, xerrno = 0;
  pool *tmp_pool;
  char *key;
  BIO *bio;
  char *data = NULL;
  long datalen = 0;

  bio = BIO_new(BIO_s_mem());
  BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  res = PEM_write_bio_SSL_SESSION(bio, sess);
  if (res != 1) {
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error writing PEM-encoded SSL session data: %s", proxy_tls_get_errors());
  }
  (void) BIO_flush(bio);

  datalen = BIO_get_mem_data(bio, &data);
  if (data == NULL) {
    pr_trace_msg(trace_channel, 9,
      "no PEM data found for SSL session, not caching");
    BIO_free(bio);
    return 0;
  }

  data[datalen] = '\0';

  if (redis_opts & PROXY_TLS_OPT_ENABLE_DIAGS) {
    BIO *diags_bio;

    diags_bio = BIO_new(BIO_s_mem());
    if (diags_bio != NULL) {
      if (SSL_SESSION_print(diags_bio, sess) == 1) {
        char *diags_data = NULL;
        long diags_datalen = 0;

        diags_datalen = BIO_get_mem_data(diags_bio, &diags_data);
        if (diags_data != NULL) {
          diags_data[diags_datalen] = '\0';
          (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
            "[tls.redis] caching SSL session (%lu bytes):\n%s",
            (unsigned long) datalen, diags_data);
        }
      }
    }
  }

  tmp_pool = make_sub_pool(p);

  key = make_key(tmp_pool, main_server->sid);
  res = pr_redis_hash_set(redis, &proxy_module, key, sess_key, data, datalen);
  xerrno = errno;

  if (res < 0) {
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error setting value for field '%s' in Redis hash '%s': %s", sess_key,
      key, strerror(xerrno));

    destroy_pool(tmp_pool);
    BIO_free(bio);
    errno = xerrno;
    return -1;
  }

  pr_trace_msg(trace_channel, 17, "cached SSL session (%lu bytes) for key '%s'",
    (unsigned long) datalen, sess_key);

  destroy_pool(tmp_pool);
  BIO_free(bio);
  return 0;
}

static int tls_redis_remove_sess(pool *p, void *redis, const char *sess_key) {
  int res, xerrno;
  pool *tmp_pool;
  char *key;

  tmp_pool = make_sub_pool(p);

  key = make_key(tmp_pool, main_server->sid);
  res = pr_redis_hash_delete(redis, &proxy_module, key, sess_key);
  xerrno = errno;

  if (res < 0) {
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error deleting field '%s' from Redis hash '%s': %s", sess_key, key,
      strerror(xerrno));

    destroy_pool(tmp_pool);
    errno = xerrno;
    return -1;
  }

  pr_trace_msg(trace_channel, 17, "removed cached SSL session for key '%s'",
    sess_key);
  destroy_pool(tmp_pool);
  return 0;
}

static SSL_SESSION *tls_redis_get_sess(pool *p, void *redis,
    const char *sess_key) {
  int res, xerrno;
  pool *tmp_pool;
  BIO *bio;
  char *key;
  char *data = NULL;
  size_t datalen = 0;
  SSL_SESSION *sess = NULL;

  tmp_pool = make_sub_pool(p);

  key = make_key(tmp_pool, main_server->sid);
  res = pr_redis_hash_get(tmp_pool, redis, &proxy_module, key, sess_key,
    (void **) &data, &datalen);
  xerrno = errno;

  if (res < 0) {
    if (xerrno != ENOENT) {
      (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
        "error getting value for field '%s' from Redis hash '%s': %s", sess_key,
        key, strerror(xerrno));
    }

    destroy_pool(tmp_pool);
    errno = xerrno;
    return NULL;
  }

  pr_trace_msg(trace_channel, 19,
    "retrieved cached session (%lu bytes) for key '%s'",
    (unsigned long) datalen, sess_key);

  bio = BIO_new_mem_buf((char *) data, datalen);
  sess = PEM_read_bio_SSL_SESSION(bio, NULL, 0, NULL);
  destroy_pool(tmp_pool);

  if (sess == NULL) {
    pr_trace_msg(trace_channel, 3,
      "error converting database entry to SSL session: %s",
      proxy_tls_get_errors());
  }

  BIO_free(bio);

  if (sess == NULL) {
    errno = ENOENT;
    return NULL;
  }

  pr_trace_msg(trace_channel, 17, "retrieved cached SSL session for key '%s'",
    sess_key);
  return sess;
}

static int tls_redis_count_sess(pool *p, void *redis) {
  int res, xerrno;
  uint64_t count = 0;
  pool *tmp_pool;
  char *key;

  tmp_pool = make_sub_pool(p);

  key = make_key(tmp_pool, main_server->sid);
  res = pr_redis_hash_count(redis, &proxy_module, key, &count);
  xerrno = errno;

  if (res < 0) {
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error getting size of Redis hash '%s': %s", key, strerror(xerrno));

    destroy_pool(tmp_pool);
    errno = xerrno;
    return -1;
  }

  destroy_pool(tmp_pool);
  return (int) count;
}

/* Initialization routines */

static int tls_redis_truncate_tables(pool *p, pr_redis_t *redis,
    unsigned int vhost_id) {
  register unsigned int i;
  int res, xerrno;
  pool *tmp_pool;
  const char *key;
  array_header *fields = NULL;

  tmp_pool = make_sub_pool(p);

  key = make_key(tmp_pool, vhost_id);
  res = pr_redis_hash_keys(tmp_pool, redis, &proxy_module, key, &fields);
  xerrno = errno;

  if (res < 0) {
    if (xerrno == ENOENT) {
      /* Ignore. */
      res = 0;

    } else {
      (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
        "error obtaining fields from Redis hash '%s': %s", key,
        strerror(errno));
    }

    destroy_pool(tmp_pool);
    errno = xerrno;
    return res;
  }

  pr_trace_msg(trace_channel, 17, "deleting %u %s for hash '%s'",
    fields->nelts, fields->nelts != 1 ? "fields" : "field", key);

  for (i = 0; i < fields->nelts; i++) {
    char *field;

    field = ((char **) fields->elts)[i];
    pr_trace_msg(trace_channel, 17, "deleting field '%s' from Redis hash '%s'",
      field, key);
    res = pr_redis_hash_delete(redis, &proxy_module, key, field);
    if (res < 0) {
      pr_trace_msg(trace_channel, 4,
        "error deleting field '%s' from Redis hash '%s': %s", field, key,
        strerror(errno));
    }
  }

  destroy_pool(tmp_pool);
  return 0;
}

static int tls_redis_init(pool *p, const char *tables_path, int flags) {
  int res, xerrno = 0;
  server_rec *s;
  pr_redis_t *redis = NULL;

  (void) tables_path;
  (void) flags;

  redis = pr_redis_conn_new(p, &proxy_module, 0);
  xerrno = errno;

  if (redis == NULL) {
    (void) pr_log_pri(PR_LOG_NOTICE, MOD_PROXY_VERSION
      ": error opening Redis connection: %s", strerror(xerrno));
    errno = xerrno;
    return -1;
  }

  (void) pr_redis_conn_set_namespace(redis, &proxy_module, redis_prefix,
    redis_prefixsz);

  for (s = (server_rec *) server_list->xas_list; s; s = s->next) {
    res = tls_redis_truncate_tables(p, redis, s->sid);
    if (res < 0) {
      pr_trace_msg(trace_channel, 3,
        "error truncating Redis keys for server '%s': %s", s->ServerName,
        strerror(errno));
    }
  }

  (void) pr_redis_conn_close(redis);
  return 0;
}

static int tls_redis_close(pool *p, void *redis) {
  if (redis != NULL) {
    if (pr_redis_conn_close(redis) < 0) {
      (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
        "error closing Redis connection: %s", strerror(errno));
    }
  }

  return 0;
}

static void *tls_redis_open(pool *p, const char *tables_dir,
    unsigned long opts) {
  int xerrno = 0;
  pr_redis_t *redis;

  redis = pr_redis_conn_new(p, &proxy_module, 0);
  xerrno = errno;

  if (redis == NULL) {
    (void) pr_log_writefile(proxy_logfd, MOD_PROXY_VERSION,
      "error opening Redis connection: %s", strerror(xerrno));
    errno = xerrno;
    return NULL;
  }

  (void) pr_redis_conn_set_namespace(redis, &proxy_module, redis_prefix,
    redis_prefixsz);
  redis_opts = opts;
  return redis;
}
#endif /* PR_USE_OPENSSL */

int proxy_tls_redis_as_datastore(struct proxy_tls_datastore *ds, void *ds_data,
    size_t ds_datasz) {
  if (ds == NULL) {
    errno = EINVAL;
    return -1;
  }

#ifdef PR_USE_OPENSSL
  redis_prefix = ds_data;
  redis_prefixsz = ds_datasz;

  ds->add_sess = tls_redis_add_sess;
  ds->remove_sess = tls_redis_remove_sess;
  ds->get_sess = tls_redis_get_sess;
  ds->count_sess = tls_redis_count_sess;

  ds->init = tls_redis_init;
  ds->open = tls_redis_open;
  ds->close = tls_redis_close;
#endif /* PR_USE_OPENSSL */

  return 0;
}