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 (282 lines) | stat: -rw-r--r-- 7,707 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
/*
 * ProFTPD - mod_proxy SSH Redis implementation
 * Copyright (c) 2022 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/ssh.h"
#include "proxy/ssh/crypto.h"
#include "proxy/ssh/redis.h"

#if defined(PR_USE_OPENSSL)
#include <openssl/evp.h>

extern xaset_t *server_list;

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

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

static const char *redis_algo_field = "algo";
static const char *redis_blob_field = "blob";

static char *make_key(pool *p, const char *backend_uri) {
  char *key;
  size_t keysz;

  keysz = strlen(backend_uri) + 64;
  key = pcalloc(p, keysz + 1);
  snprintf(key, keysz, "proxy_ssh_hostkeys:%s", backend_uri);

  return key;
}

static int ssh_redis_update_hostkey(pool *p, void *dsh, unsigned int vhost_id,
    const char *backend_uri, const char *algo,
    const unsigned char *hostkey_data, uint32_t hostkey_datalen) {
  int res, xerrno = 0;
  pool *tmp_pool;
  pr_redis_t *redis;
  char *key, *data = NULL;
  long datalen = 0;
  size_t field_len;

  redis = dsh;

  tmp_pool = make_sub_pool(p);
  data = palloc(tmp_pool, (2 * hostkey_datalen) + 1);
  datalen = EVP_EncodeBlock((unsigned char *) data, hostkey_data,
    (int) hostkey_datalen);

  if (datalen == 0) {
    pr_trace_msg(trace_channel, 3,
      "error base640-encoding hostkey data: %s", proxy_ssh_crypto_get_errors());
    destroy_pool(tmp_pool);
    return 0;
  }

  key = make_key(tmp_pool, backend_uri);

  field_len = strlen(algo);
  res = pr_redis_hash_set(redis, &proxy_module, key, redis_algo_field,
    (void *) algo, field_len);
  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",
      redis_algo_field, key, strerror(xerrno));

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

  field_len = datalen;
  res = pr_redis_hash_set(redis, &proxy_module, key, redis_blob_field,
    (void *) data, field_len);
  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",
      redis_blob_field, key, strerror(xerrno));

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

  destroy_pool(tmp_pool);
  return 0;
}

static const unsigned char *ssh_redis_get_hostkey(pool *p, void *dsh,
    unsigned int vhost_id, const char *backend_uri, const char **algo,
    uint32_t *hostkey_datalen) {
  int have_padding = FALSE, res, xerrno;
  pool *tmp_pool;
  pr_redis_t *redis;
  pr_table_t *hostkey_tab;
  char *key;
  void *data = NULL;
  const unsigned char *hostkey_data = NULL;
  size_t blocklen = 0, datalen = 0, rem;

  redis = dsh;

  tmp_pool = make_sub_pool(p);
  key = make_key(tmp_pool, backend_uri);

  res = pr_redis_hash_getall(tmp_pool, redis, &proxy_module, key, &hostkey_tab);
  xerrno = errno;

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

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

  if (hostkey_tab == NULL) {
    destroy_pool(tmp_pool);
    errno = ENOENT;
    return NULL;
  }

  data = (void *) pr_table_kget(hostkey_tab, redis_algo_field,
    strlen(redis_algo_field), &datalen);
  if (data != NULL) {
    *algo = pstrndup(p, data, datalen);
  }

  data = (void *) pr_table_kget(hostkey_tab, redis_blob_field,
    strlen(redis_blob_field), &datalen);
  if (data == NULL) {
    pr_trace_msg(trace_channel, 3, "%s",
      "missing base64-decoding hostkey data from Redis, skipping");
    destroy_pool(tmp_pool);
    errno = ENOENT;
    return NULL;
  }

  /* Due to Base64's padding, we need to detect if the last block was padded
   * with zeros; we do this by looking for '=' characters at the end of the
   * text being decoded.  If we see these characters, then we will "trim" off
   * any trailing zero values in the decoded data, on the ASSUMPTION that they
   * are the auto-added padding bytes.
   */
  if (((char *) data)[datalen-1] == '=') {
    have_padding = TRUE;
  }

  blocklen = datalen;

  /* Ensure that the output buffer is divisible by 4, per OpenSSL
   * requirements.
   */
  rem = blocklen % 4;
  if (rem != 0) {
    blocklen += rem;
  }

  hostkey_data = pcalloc(p, blocklen);
  res = EVP_DecodeBlock((unsigned char *) hostkey_data, (unsigned char *) data,
    (int) datalen);
  if (res <= 0) {
    pr_trace_msg(trace_channel, 3,
      "error base64-decoding hostkey data: %s", proxy_ssh_crypto_get_errors());
    destroy_pool(tmp_pool);
    errno = EINVAL;
    return NULL;
  }

  if (have_padding == TRUE) {
    /* Assume that only one or two zero bytes of padding were added. */
    if (hostkey_data[res-1] == '\0') {
      res -= 1;

      if (hostkey_data[res-1] == '\0') {
        res -= 1;
      }
    }
  }

  *hostkey_datalen = res;

  pr_trace_msg(trace_channel, 19,
    "retrieved hostkey (algo '%s', %lu bytes) for vhost ID %u, URI '%s'",
    *algo, (unsigned long) *hostkey_datalen, vhost_id, backend_uri);
  return hostkey_data;
}

/* Initialization routines */

static int ssh_redis_init(pool *p, const char *tables_path, int flags) {
  /* We currently don't need to do anything, at init time, to any existing
   * SSH Redis keys.
   */
  return 0;
}

static int ssh_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 *ssh_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;
}

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

  redis_prefix = ds_data;
  redis_prefixsz = ds_datasz;

  ds->hostkey_add = ssh_redis_update_hostkey;
  ds->hostkey_get = ssh_redis_get_hostkey;
  ds->hostkey_update = ssh_redis_update_hostkey;

  ds->init = ssh_redis_init;
  ds->open = ssh_redis_open;
  ds->close = ssh_redis_close;

  return 0;
}
#endif /* PR_USE_OPENSSL */