-
Notifications
You must be signed in to change notification settings - Fork 2
/
rlm_memcached.c
246 lines (215 loc) · 6.94 KB
/
rlm_memcached.c
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
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/modules.h>
#include <libmemcached/memcached.h>
enum {
RLM_MEMCACHED_OPS_GET,
RLM_MEMCACHED_OPS_SET
};
typedef struct rlm_memcached {
struct {
const char *action;
const char *config;
vp_tmpl_t *key;
vp_tmpl_t *value;
vp_tmpl_t *output_attr;
int ttl;
} cfg;
int action;
const char *name;
fr_connection_pool_t *pool;
} rlm_memcached_t;
typedef struct rlm_memcached_conn {
struct memcached_st *mc;
} rlm_memcached_conn_t;
static const CONF_PARSER module_config[] = {
{"action", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_REQUIRED, rlm_memcached_t, cfg.action), NULL},
{"config", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_NOT_EMPTY, rlm_memcached_t, cfg.config), NULL},
{"key", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_TMPL | PW_TYPE_REQUIRED, rlm_memcached_t, cfg.key), NULL},
{"value", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_TMPL, rlm_memcached_t, cfg.value), NULL},
{"output_attr", FR_CONF_OFFSET(PW_TYPE_STRING | PW_TYPE_TMPL, rlm_memcached_t, cfg.output_attr), NULL},
{"ttl", FR_CONF_OFFSET(PW_TYPE_SIGNED, rlm_memcached_t, cfg.ttl), "0"},
CONF_PARSER_TERMINATOR
};
/**
* Detach module.
* @param[in] instance Module instance.
* @return Zero on success.
*/
static int mod_detach(void *instance) {
rlm_memcached_t *inst = instance;
fr_connection_pool_free(inst->pool);
return 0;
}
/**
* Module connection destructor.
* @param[in] conn Connection handle.
* @return Zero on success.
*/
static int mod_conn_free(rlm_memcached_conn_t *conn) {
memcached_free(conn->mc);
DEBUG("rlm_memcached: closed connection");
return 0;
}
/**
* Module connection constructor.
* @param[in] ctx Talloc context.
* @param[in] instance Module instance.
* @return NULL on error, else a connection handle.
*/
static void *mod_conn_create(TALLOC_CTX *ctx, void *instance) {
rlm_memcached_t *inst = instance;
struct memcached_st *mc = memcached(inst->cfg.config, strlen(inst->cfg.config));
if (!mc) {
ERROR("rlm_memcached (%s): Failed to create memcached instance", inst->name);
return NULL;
}
rlm_memcached_conn_t *conn = talloc_zero(ctx, rlm_memcached_conn_t);
conn->mc = mc;
talloc_set_destructor(conn, mod_conn_free);
return conn;
}
/**
* Instantiate module.
* @param[in] conf Module config.
* @param[in] instance Module instance.
* @return Zero on success.
*/
static int mod_instantiate(CONF_SECTION *conf, void *instance) {
rlm_memcached_t *inst = instance;
inst->name = cf_section_name2(conf);
if (!inst->name) {
inst->name = cf_section_name1(conf);
}
if (!strcasecmp(inst->cfg.action, "get")) {
inst->action = RLM_MEMCACHED_OPS_GET;
if (!inst->cfg.output_attr || (inst->cfg.output_attr->type != TMPL_TYPE_ATTR)) {
cf_log_err_cs(conf, "Invalid option 'output_attr'");
goto err;
}
} else if (!strcasecmp(inst->cfg.action, "set")) {
inst->action = RLM_MEMCACHED_OPS_SET;
if (!inst->cfg.value) {
cf_log_err_cs(conf, "Invalid option 'value'");
goto err;
}
} else {
cf_log_err_cs(conf, "Invalid option 'action', use 'get' or 'set'");
goto err;
}
if (!cf_pair_find(conf, "pool")) {
if (!inst->cfg.config) {
cf_log_err_cs(conf, "Invalid or missing 'config' option");
goto err;
} else {
char err_buf[256] = {0};
memcached_return_t mret = libmemcached_check_configuration(inst->cfg.config, strlen(inst->cfg.config),
err_buf, sizeof(err_buf));
if (mret != MEMCACHED_SUCCESS) {
cf_log_err_cs(conf, "Invalid option 'config': %s", err_buf);
goto err;
}
}
} else {
if (inst->cfg.config) {
cf_log_err_cs(conf, "Can't use 'config' option when foreign connection pool specified");
goto err;
}
}
inst->pool = fr_connection_pool_module_init(conf, inst, mod_conn_create, NULL, inst->name);
if (!inst->pool) {
goto err;
}
return 0;
err:
return -1;
}
/**
* Main module procedure.
* @param[in] instance Module instance.
* @param[in] request Radius request.
* @return One of #rlm_rcode_t.
*/
static rlm_rcode_t mod_proc(void *instance, REQUEST *request) {
rlm_memcached_t *inst = instance;
rlm_memcached_conn_t *conn = NULL;
rlm_rcode_t code = RLM_MODULE_FAIL;
memcached_return_t mret;
conn = fr_connection_get(inst->pool);
if (!conn) {
goto end;
}
char *key = NULL;
ssize_t key_len = tmpl_aexpand(request, &key, request, inst->cfg.key, NULL, NULL);
if (key_len < 0) {
goto end;
} else if (key_len == 0) {
RDEBUG2("empty key, will not make request");
goto end;
}
if (inst->action == RLM_MEMCACHED_OPS_GET) {
uint32_t flags = 0;
size_t value_len = 0;
char *value = memcached_get(conn->mc, key, (size_t) key_len, &value_len, &flags, &mret);
if (mret == MEMCACHED_NOTFOUND) {
code = RLM_MODULE_NOTFOUND;
goto end;
} else if (mret != MEMCACHED_SUCCESS) {
RWDEBUG2("failed to get %s: %s", key, memcached_strerror(conn->mc, mret));
goto end;
}
VALUE_PAIR *vp = NULL;
if (tmpl_find_vp(&vp, request, inst->cfg.output_attr) != 0) {
RADIUS_PACKET *packet = radius_packet(request, inst->cfg.output_attr->tmpl_list);
VALUE_PAIR **vps = radius_list(request, inst->cfg.output_attr->tmpl_list);
vp = fr_pair_afrom_da(packet, inst->cfg.output_attr->tmpl_da);
fr_pair_add(vps, vp);
}
fr_pair_value_from_str(vp, value, (size_t) value_len);
RDEBUG2("set %s = %s", inst->cfg.output_attr->name, value);
code = RLM_MODULE_UPDATED;
free(value);
} else {
char *value = NULL;
ssize_t value_len = tmpl_aexpand(request, &value, request, inst->cfg.value, NULL, NULL);
if (value_len < 0) {
RERROR("failed to substitute attributes for value '%s'", inst->cfg.value->name);
goto end;
}
mret = memcached_set(conn->mc, key, (size_t) key_len, value, (size_t) value_len, inst->cfg.ttl, 0);
if (mret != MEMCACHED_SUCCESS) {
RERROR("write '%s' = '%s' failed: %s", key, value, memcached_strerror(conn->mc, mret));
goto end;
}
RDEBUG2("wrote '%s' = '%s'", key, value);
code = RLM_MODULE_OK;
}
end:
if (conn) fr_connection_release(inst->pool, conn);
return code;
}
/* globally exported name */
extern module_t rlm_memcached;
module_t rlm_memcached = {
.magic = RLM_MODULE_INIT,
.name = "memcached",
.type = RLM_TYPE_THREAD_SAFE | RLM_TYPE_HUP_SAFE,
.inst_size = sizeof(rlm_memcached_t),
.config = module_config,
.bootstrap = NULL,
.instantiate = mod_instantiate,
.detach = mod_detach,
.methods = {
[MOD_AUTHENTICATE] = mod_proc,
[MOD_AUTHORIZE] = mod_proc,
[MOD_PREACCT] = mod_proc,
[MOD_ACCOUNTING] = mod_proc,
[MOD_SESSION] = NULL,
[MOD_PRE_PROXY] = mod_proc,
[MOD_POST_PROXY] = mod_proc,
[MOD_POST_AUTH] = mod_proc,
#ifdef WITH_COA
[MOD_RECV_COA] = mod_proc,
[MOD_SEND_COA] = mod_proc,
#endif
},
};