File: apache.h

package info (click to toggle)
liboauth2 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,300 kB
  • sloc: ansic: 20,329; makefile: 190; sh: 13
file content (356 lines) | stat: -rw-r--r-- 16,200 bytes parent folder | download | duplicates (2)
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
#ifndef _OAUTH2_APACHE_H_
#define _OAUTH2_APACHE_H_

/***************************************************************************
 *
 * Copyright (C) 2018-2025 - ZmartZone Holding BV
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @Author: Hans Zandbelt - hans.zandbelt@openidc.com
 *
 **************************************************************************/

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include <oauth2/http.h>
#include <oauth2/log.h>
#include <oauth2/util.h>
#include <oauth2/version.h>

// avoid errors about ap_auto_config overriding these, so undefine first
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION

#include <httpd.h>

#include <http_config.h>
#include <http_log.h>

#include <mod_auth.h>

extern oauth2_cfg_server_callback_funcs_t oauth2_apache_server_callback_funcs;

/*
 * logging
 */

extern oauth2_uint_t log_level_log2apache[];
extern oauth2_uint_t log_level_apache2oauth2[];

#ifndef APLOG_USE_MODULE
#define APLOG_USE_MODULE(foo)                                                  \
	extern module AP_MODULE_DECLARE_DATA foo##_module;                     \
	AP_MAYBE_UNUSED(static int *const aplog_module_index) =                \
	    &(foo##_module.module_index)
#endif

#define OAUTH2_APACHE_LOG(foo)                                                 \
                                                                               \
	APLOG_USE_MODULE(foo);                                                 \
                                                                               \
	static void foo##_log_server(                                          \
	    oauth2_log_sink_t *sink, const char *filename, unsigned long line, \
	    const char *function, oauth2_log_level_t level, const char *msg)   \
	{                                                                      \
		ap_log_error(                                                  \
		    filename, line,                                            \
		    aplog_module_index ? *aplog_module_index                   \
				       : APLOG_NO_MODULE,                      \
		    log_level_log2apache[level], 0,                            \
		    (const server_rec *)oauth2_log_sink_ctx_get(sink),         \
		    "%s: %s", function, msg);                                  \
	}                                                                      \
                                                                               \
	static void foo##_log_request(                                         \
	    oauth2_log_sink_t *sink, const char *filename, unsigned long line, \
	    const char *function, oauth2_log_level_t level, const char *msg)   \
	{                                                                      \
		ap_log_rerror(                                                 \
		    filename, line,                                            \
		    aplog_module_index ? *aplog_module_index                   \
				       : APLOG_NO_MODULE,                      \
		    log_level_log2apache[level], 0,                            \
		    (const request_rec *)oauth2_log_sink_ctx_get(sink),        \
		    "%s: %s", function, msg);                                  \
	}

/*
 * parent/child cleanup
 */

apr_status_t oauth2_apache_child_cleanup(void *data, module *m,
					 const char *package_name_version);

#define OAUTH2_APACHE_CHILD_CLEANUP(foo)                                       \
	static apr_status_t foo##_child_cleanup(void *data)                    \
	{                                                                      \
		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,                       \
			     (const server_rec *)data, "%s: %s", __FUNCTION__, \
			     "enter");                                         \
		return oauth2_apache_child_cleanup(                            \
		    data, &foo##_module, OAUTH2_PACKAGE_NAME_VERSION);         \
	}

apr_status_t oauth2_apache_parent_cleanup(void *data, module *m,
					  const char *package_name_version);

#define OAUTH2_APACHE_PARENT_CLEANUP(foo)                                      \
	static apr_status_t foo##_parent_cleanup(void *data)                   \
	{                                                                      \
		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,                       \
			     (const server_rec *)data, "%s: %s", __FUNCTION__, \
			     "enter");                                         \
		return oauth2_apache_parent_cleanup(                           \
		    data, &foo##_module, OAUTH2_PACKAGE_NAME_VERSION);         \
	}

/*
 * post config
 */

int oauth2_apache_post_config(apr_pool_t *pool, apr_pool_t *p1, apr_pool_t *p2,
			      server_rec *s, module *m,
			      const char *package_name_version,
			      apr_status_t (*parent_cleanup)(void *),
			      apr_status_t (*child_cleanup)(void *));

#define OAUTH2_APACHE_POST_CONFIG(foo) foo##_post_config

#define OAUTH2_APACHE_POST_CONFIG_IMPL(foo)                                    \
	static apr_status_t OAUTH2_APACHE_POST_CONFIG(foo)(                    \
	    apr_pool_t * pool, apr_pool_t * p1, apr_pool_t * p2,               \
	    server_rec * s)                                                    \
	{                                                                      \
		ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,                       \
			     (const server_rec *)s, "%s: %s", __FUNCTION__,    \
			     "enter");                                         \
		return oauth2_apache_post_config(                              \
		    pool, p1, p2, s, &foo##_module,                            \
		    OAUTH2_PACKAGE_NAME_VERSION, foo##_parent_cleanup,         \
		    foo##_child_cleanup);                                      \
	}

/*
 * directory config
 */

#define OAUTH2_APACHE_CMD_ARGS1(module, type, primitive, func, member)         \
	static const char *apache_##module##_set_##primitive(                  \
	    cmd_parms *cmd, void *m, const char *v1)                           \
	{                                                                      \
		oauth2_apache_cfg_srv_t *srv_cfg = ap_get_module_config(       \
		    cmd->server->module_config, &module##_module);             \
		type *cfg = (type *)m;                                         \
		(void)cfg;                                                     \
		return func(srv_cfg->log, member, v1);                         \
	}

#define OAUTH2_APACHE_CMD_ARGS2(module, type, primitive, func, member)         \
	static const char *apache_##module##_set_##primitive(                  \
	    cmd_parms *cmd, void *m, const char *v1, const char *v2)           \
	{                                                                      \
		oauth2_apache_cfg_srv_t *srv_cfg = ap_get_module_config(       \
		    cmd->server->module_config, &module##_module);             \
		type *cfg = (type *)m;                                         \
		(void)cfg;                                                     \
		return func(srv_cfg->log, member, v1, v2);                     \
	}

#define OAUTH2_APACHE_CMD_ARGS3(module, type, primitive, func, member)         \
	static const char *apache_##module##_set_##primitive(                  \
	    cmd_parms *cmd, void *m, const char *v1, const char *v2,           \
	    const char *v3)                                                    \
	{                                                                      \
		oauth2_apache_cfg_srv_t *srv_cfg = ap_get_module_config(       \
		    cmd->server->module_config, &module##_module);             \
		type *cfg = (type *)m;                                         \
		(void)cfg;                                                     \
		return func(srv_cfg->log, member, v1, v2, v3);                 \
	}

#define OAUTH2_APACHE_CMD_ARGSV4(module, type, primitive, func, member)        \
	static const char *apache_##module##_set_##primitive(                  \
	    cmd_parms *cmd, void *m, int argc, char *const argv[])             \
	{                                                                      \
		oauth2_apache_cfg_srv_t *srv_cfg = ap_get_module_config(       \
		    cmd->server->module_config, &module##_module);             \
		type *cfg = (type *)m;                                         \
		(void)cfg;                                                     \
		return func(srv_cfg->log, member, argc > 0 ? argv[0] : NULL,   \
			    argc > 1 ? argv[1] : NULL,                         \
			    argc > 2 ? argv[2] : NULL,                         \
			    argc > 3 ? argv[3] : NULL);                        \
	}

#define OAUTH2_APACHE_CMD_ARGS(module, nargs, cmd, member, desc)               \
	AP_INIT_TAKE##nargs(cmd, apache_##module##_set_##member, NULL,         \
			    RSRC_CONF | ACCESS_CONF | OR_AUTHCFG, desc)

#define OAUTH2_APACHE_DIR_CTX(type, method) oauth2_##type##_dir_##method

#define OAUTH2_APACHE_DIR_CTX_FUNCS(type)                                      \
	apr_status_t OAUTH2_APACHE_DIR_CTX(type, cleanup)(void *data)          \
	{                                                                      \
		oauth2_##type##_t *cfg = (oauth2_##type##_t *)data;            \
		oauth2_##type##_free(NULL, cfg);                               \
		return APR_SUCCESS;                                            \
	}                                                                      \
                                                                               \
	void *OAUTH2_APACHE_DIR_CTX(type, create)(apr_pool_t * pool,           \
						  char *path)                  \
	{                                                                      \
		oauth2_##type##_t *cfg = oauth2_##type##_create(NULL, path);   \
		apr_pool_cleanup_register(                                     \
		    pool, cfg, OAUTH2_APACHE_DIR_CTX(type, cleanup),           \
		    OAUTH2_APACHE_DIR_CTX(type, cleanup));                     \
		return cfg;                                                    \
	}                                                                      \
                                                                               \
	static void *OAUTH2_APACHE_DIR_CTX(type, merge)(apr_pool_t * pool,     \
							void *b, void *a)      \
	{                                                                      \
		oauth2_##type##_t *cfg =                                       \
		    OAUTH2_APACHE_DIR_CTX(type, create)(pool, NULL);           \
		oauth2_##type##_t *base = b;                                   \
		oauth2_##type##_t *add = a;                                    \
		oauth2_##type##_merge(NULL, cfg, base, add);                   \
		return cfg;                                                    \
	}

/*
 * server config
 */

typedef struct oauth2_apache_cfg_srv_t {
	oauth2_log_sink_t *sink;
	oauth2_log_t *log;
	bool is_child;
} oauth2_apache_cfg_srv_t;

void *oauth2_apache_cfg_srv_create(apr_pool_t *pool, server_rec *s,
				   oauth2_log_function_t server_log_cb);
void *oauth2_apache_cfg_srv_merge(apr_pool_t *pool, void *b, void *a);

/*
 * handlers
 */

#define OAUTH2_APACHE_HANDLERS(foo)                                            \
	OAUTH2_APACHE_CHILD_CLEANUP(foo)                                       \
	OAUTH2_APACHE_PARENT_CLEANUP(foo)                                      \
	OAUTH2_APACHE_POST_CONFIG_IMPL(foo)

/*
 * module config
 */

#define OAUTH2_APACHE_COMMANDS(foo) foo##_commands
#define OAUTH2_APACHE_REGISTER_HOOKS(foo) foo##_register_hooks

#define OAUTH2_APACHE_MODULE_DECLARE_EX(foo, dir_create, dir_merge)            \
                                                                               \
	void *oauth2_apache_##foo##_cfg_srv_create(apr_pool_t *pool,           \
						   server_rec *s)              \
	{                                                                      \
		return oauth2_apache_cfg_srv_create(pool, s,                   \
						    foo##_log_server);         \
	}                                                                      \
                                                                               \
	module AP_MODULE_DECLARE_DATA foo##_module = {                         \
	    STANDARD20_MODULE_STUFF,                                           \
	    dir_create,                                                        \
	    dir_merge,                                                         \
	    oauth2_apache_##foo##_cfg_srv_create,                              \
	    oauth2_apache_cfg_srv_merge,                                       \
	    OAUTH2_APACHE_COMMANDS(foo),                                       \
	    OAUTH2_APACHE_REGISTER_HOOKS(foo)};

#define OAUTH2_APACHE_MODULE_DECLARE(foo, type)                                \
                                                                               \
	OAUTH2_APACHE_DIR_CTX_FUNCS(type)                                      \
                                                                               \
	OAUTH2_APACHE_MODULE_DECLARE_EX(foo,                                   \
					OAUTH2_APACHE_DIR_CTX(type, create),   \
					OAUTH2_APACHE_DIR_CTX(type, merge))

/*
 * request context
 */

#define OAUTH2_APACHE_REQUEST_CTX(r, foo)                                      \
	oauth2_apache_request_context(                                         \
	    r, foo##_log_request,                                              \
	    "oauth2_" OAUTH2_TOSTRING(foo) "_module_user_data_key");

typedef struct oauth2_apache_request_ctx_t {
	oauth2_log_t *log;
	oauth2_http_request_t *request;
	request_rec *r;
} oauth2_apache_request_ctx_t;

oauth2_apache_request_ctx_t *
oauth2_apache_request_context(request_rec *r,
			      oauth2_log_function_t request_log_cb,
			      const char *user_data_key);

/*
 * misc
 */

bool oauth2_apache_http_request_set(oauth2_log_t *log,
				    oauth2_http_request_t *request,
				    request_rec *r);
int oauth2_apache_return_www_authenticate(oauth2_cfg_source_token_t *cfg,
					  oauth2_apache_request_ctx_t *ctx,
					  int status_code, const char *error,
					  const char *error_description);
bool oauth2_apache_request_header_set(oauth2_log_t *log, void *rec,
				      const char *name, const char *value);
void oauth2_apache_hdr_out_add(oauth2_log_t *log, const request_rec *r,
			       const char *name, const char *value);
void oauth2_apache_scrub_headers(oauth2_apache_request_ctx_t *ctx,
				 oauth2_cfg_target_pass_t *target_pass);
bool oauth2_apache_set_request_user(oauth2_cfg_target_pass_t *target_pass,
				    oauth2_apache_request_ctx_t *ctx,
				    json_t *json_token);
void oauth2_apache_target_pass(oauth2_apache_request_ctx_t *ctx,
			       oauth2_cfg_target_pass_t *target_pass,
			       const char *target_token, json_t *json_token);

bool oauth2_apache_http_response_set(oauth2_log_t *log,
				     oauth2_http_response_t *response,
				     request_rec *r);

void oauth2_apache_request_state_set_json(oauth2_apache_request_ctx_t *ctx,
					  const char *key, json_t *claims);
void oauth2_apache_request_state_get_json(oauth2_apache_request_ctx_t *ctx,
					  const char *key, json_t **claims);

typedef bool (*oauth2_apache_authz_match_claim_fn_type)(
    oauth2_apache_request_ctx_t *, const char *const, const json_t *const);

bool oauth2_apache_authz_match_claim(oauth2_apache_request_ctx_t *ctx,
				     const char *const attr_spec,
				     const json_t *const claims);
authz_status
oauth2_apache_authorize(oauth2_apache_request_ctx_t *ctx,
			const json_t *const claims, const char *require_args,
			oauth2_apache_authz_match_claim_fn_type match_claim_fn);

#endif /* _OAUTH2_APACHE_H_ */