~ubuntu-branches/ubuntu/vivid/libapache2-mod-auth-openidc/vivid-proposed

« back to all changes in this revision

Viewing changes to src/cache/memcache.c

  • Committer: Package Import Robot
  • Author(s): Hans Zandbelt
  • Date: 2014-10-13 12:23:35 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20141013122335-31wgnq50ascmubib
Tags: 1.6.0-1
new upstream release; add libssl-dev dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
 
73
73
/* create the cache context */
74
74
static void *oidc_cache_memcache_cfg_create(apr_pool_t *pool) {
75
 
        oidc_cache_cfg_memcache_t *context = apr_pcalloc(pool, sizeof(oidc_cache_cfg_memcache_t));
 
75
        oidc_cache_cfg_memcache_t *context = apr_pcalloc(pool,
 
76
                        sizeof(oidc_cache_cfg_memcache_t));
76
77
        context->cache_memcache = NULL;
77
78
        return context;
78
79
}
81
82
 * initialize the memcache struct to a number of memcache servers
82
83
 */
83
84
static int oidc_cache_memcache_post_config(server_rec *s) {
84
 
        oidc_cfg *cfg = (oidc_cfg *) ap_get_module_config(
85
 
                        s->module_config, &auth_openidc_module);
 
85
        oidc_cfg *cfg = (oidc_cfg *) ap_get_module_config(s->module_config,
 
86
                        &auth_openidc_module);
86
87
 
87
 
        if (cfg->cache_cfg != NULL) return APR_SUCCESS;
88
 
        oidc_cache_cfg_memcache_t *context = oidc_cache_memcache_cfg_create(s->process->pool);
 
88
        if (cfg->cache_cfg != NULL)
 
89
                return APR_SUCCESS;
 
90
        oidc_cache_cfg_memcache_t *context = oidc_cache_memcache_cfg_create(
 
91
                        s->process->pool);
89
92
        cfg->cache_cfg = context;
90
93
 
91
94
        apr_status_t rv = APR_SUCCESS;
95
98
        apr_pool_t *p = s->process->pool;
96
99
 
97
100
        if (cfg->cache_memcache_servers == NULL) {
98
 
                ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
99
 
                                "oidc_cache_memcache_post_config: cache type is set to \"memcache\", but no valid OIDCMemCacheServers setting was found");
 
101
                oidc_serror(s,
 
102
                                "cache type is set to \"memcache\", but no valid OIDCMemCacheServers setting was found");
100
103
                return HTTP_INTERNAL_SERVER_ERROR;
101
104
        }
102
105
 
111
114
        /* allocated space for the number of servers */
112
115
        rv = apr_memcache_create(p, nservers, 0, &context->cache_memcache);
113
116
        if (rv != APR_SUCCESS) {
114
 
                ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
115
 
                                "oidc_cache_memcache_init: failed to create memcache object of '%d' size",
 
117
                oidc_serror(s, "failed to create memcache object of '%d' size",
116
118
                                nservers);
117
119
                return HTTP_INTERNAL_SERVER_ERROR;
118
120
        }
129
131
                /* parse out host and port */
130
132
                rv = apr_parse_addr_port(&host_str, &scope_id, &port, split, p);
131
133
                if (rv != APR_SUCCESS) {
132
 
                        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
133
 
                                        "oidc_cache_memcache_init: failed to parse cache server: '%s'",
 
134
                        oidc_serror(s, "failed to parse cache server: '%s'", split);
 
135
                        return HTTP_INTERNAL_SERVER_ERROR;
 
136
                }
 
137
 
 
138
                if (host_str == NULL) {
 
139
                        oidc_serror(s,
 
140
                                        "failed to parse cache server, no hostname specified: '%s'",
134
141
                                        split);
135
142
                        return HTTP_INTERNAL_SERVER_ERROR;
136
143
                }
137
144
 
138
 
                if (host_str == NULL) {
139
 
                        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
140
 
                                        "oidc_cache_memcache_init: failed to parse cache server, "
141
 
                                                        "no hostname specified: '%s'", split);
142
 
                        return HTTP_INTERNAL_SERVER_ERROR;
143
 
                }
144
 
 
145
145
                if (port == 0)
146
146
                        port = 11211;
147
147
 
149
149
                // TODO: tune this
150
150
                rv = apr_memcache_server_create(p, host_str, port, 0, 1, 1, 60, &st);
151
151
                if (rv != APR_SUCCESS) {
152
 
                        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
153
 
                                        "oidc_cache_memcache_init: failed to create cache server: %s:%d",
154
 
                                        host_str, port);
 
152
                        oidc_serror(s, "failed to create cache server: %s:%d", host_str,
 
153
                                        port);
155
154
                        return HTTP_INTERNAL_SERVER_ERROR;
156
155
                }
157
156
 
158
157
                /* add the memcache server struct to the list */
159
158
                rv = apr_memcache_add_server(context->cache_memcache, st);
160
159
                if (rv != APR_SUCCESS) {
161
 
                        ap_log_error(APLOG_MARK, APLOG_ERR, rv, s,
162
 
                                        "oidc_cache_memcache_init: failed to add cache server: %s:%d",
163
 
                                        host_str, port);
 
160
                        oidc_serror(s, "failed to add cache server: %s:%d", host_str, port);
164
161
                        return HTTP_INTERNAL_SERVER_ERROR;
165
162
                }
166
163
 
172
169
}
173
170
 
174
171
/*
 
172
 * assemble single key name based on section/key input
 
173
 */
 
174
static char *oidc_cache_memcache_get_key(apr_pool_t *pool, const char *section,
 
175
                const char *key) {
 
176
        return apr_psprintf(pool, "%s:%s", section, key);
 
177
}
 
178
 
 
179
/*
175
180
 * get a name/value pair from memcache
176
181
 */
177
 
static apr_byte_t oidc_cache_memcache_get(request_rec *r, const char *key,
178
 
                const char **value) {
 
182
static apr_byte_t oidc_cache_memcache_get(request_rec *r, const char *section,
 
183
                const char *key, const char **value) {
179
184
 
180
 
        ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r,
181
 
                        "oidc_cache_memcache_get: entering \"%s\"", key);
 
185
        oidc_debug(r, "enter, section=\"%s\", key=\"%s\"", section, key);
182
186
 
183
187
        oidc_cfg *cfg = ap_get_module_config(r->server->module_config,
184
188
                        &auth_openidc_module);
185
 
        oidc_cache_cfg_memcache_t *context = (oidc_cache_cfg_memcache_t *)cfg->cache_cfg;
 
189
        oidc_cache_cfg_memcache_t *context =
 
190
                        (oidc_cache_cfg_memcache_t *) cfg->cache_cfg;
186
191
 
187
192
        apr_size_t len = 0;
188
193
 
189
194
        /* get it */
190
 
        apr_status_t rv = apr_memcache_getp(context->cache_memcache, r->pool, key,
191
 
                        (char **)value, &len, NULL);
 
195
        apr_status_t rv = apr_memcache_getp(context->cache_memcache, r->pool,
 
196
                        oidc_cache_memcache_get_key(r->pool, section, key), (char **) value,
 
197
                        &len, NULL);
192
198
 
193
199
        // TODO: error strings ?
194
200
        if (rv != APR_SUCCESS) {
195
 
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
196
 
                                "oidc_cache_memcache_get: apr_memcache_getp returned an error");
 
201
                oidc_error(r, "apr_memcache_getp returned an error");
197
202
                return FALSE;
198
203
        }
199
204
 
200
205
        /* do sanity checking on the string value */
201
 
        if ( (*value) && (strlen(*value) != len) ) {
202
 
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
203
 
                                "oidc_cache_memcache_get: apr_memcache_getp returned less bytes than expected: strlen(value) [%zu] != len [%" APR_SIZE_T_FMT "]", strlen(*value), len);
 
206
        if ((*value) && (strlen(*value) != len)) {
 
207
                oidc_error(r,
 
208
                                "apr_memcache_getp returned less bytes than expected: strlen(value) [%zu] != len [%" APR_SIZE_T_FMT "]",
 
209
                                strlen(*value), len);
204
210
                return FALSE;
205
211
        }
206
212
 
210
216
/*
211
217
 * store a name/value pair in memcache
212
218
 */
213
 
static apr_byte_t oidc_cache_memcache_set(request_rec *r, const char *key,
214
 
                const char *value, apr_time_t expiry) {
 
219
static apr_byte_t oidc_cache_memcache_set(request_rec *r, const char *section,
 
220
                const char *key, const char *value, apr_time_t expiry) {
215
221
 
216
 
        ap_log_rerror(APLOG_MARK, OIDC_DEBUG, 0, r,
217
 
                        "oidc_cache_memcache_set: entering \"%s\"", key);
 
222
        oidc_debug(r, "enter, section=\"%s\", key=\"%s\"", section, key);
218
223
 
219
224
        oidc_cfg *cfg = ap_get_module_config(r->server->module_config,
220
225
                        &auth_openidc_module);
221
 
        oidc_cache_cfg_memcache_t *context = (oidc_cache_cfg_memcache_t *)cfg->cache_cfg;
 
226
        oidc_cache_cfg_memcache_t *context =
 
227
                        (oidc_cache_cfg_memcache_t *) cfg->cache_cfg;
222
228
 
223
229
        apr_status_t rv = APR_SUCCESS;
224
230
 
225
231
        /* see if we should be clearing this entry */
226
232
        if (value == NULL) {
227
233
 
228
 
                rv = apr_memcache_delete(context->cache_memcache, key, 0);
 
234
                rv = apr_memcache_delete(context->cache_memcache,
 
235
                                oidc_cache_memcache_get_key(r->pool, section, key), 0);
229
236
 
230
237
                // TODO: error strings ?
231
238
                if (rv != APR_SUCCESS) {
232
 
                        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
233
 
                                        "oidc_cache_memcache_set: apr_memcache_delete returned an error");
 
239
                        oidc_error(r, "apr_memcache_delete returned an error");
234
240
                }
235
241
 
236
242
        } else {
239
245
                apr_uint32_t timeout = apr_time_sec(expiry - apr_time_now());
240
246
 
241
247
                /* store it */
242
 
                rv = apr_memcache_set(context->cache_memcache, key, (char *)value,
243
 
                                strlen(value), timeout, 0);
 
248
                rv = apr_memcache_set(context->cache_memcache,
 
249
                                oidc_cache_memcache_get_key(r->pool, section, key),
 
250
                                (char *) value, strlen(value), timeout, 0);
244
251
 
245
252
                // TODO: error strings ?
246
253
                if (rv != APR_SUCCESS) {
247
 
                        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
248
 
                                        "oidc_cache_memcache_set: apr_memcache_set returned an error");
 
254
                        oidc_error(r, "apr_memcache_set returned an error");
249
255
                }
250
256
        }
251
257