-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathngx_http_contrast_connector_module.c
250 lines (218 loc) · 6.55 KB
/
ngx_http_contrast_connector_module.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
247
248
/* Copyright (C) Contrast Security, Inc.
*
* The module will insert an http handler into the PRE-ACCESS phase of the
* http module. The handler will send request/response data to the Contrast
* rules engine for determination on if the request/response should proceed.
*/
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <sys/time.h>
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_log.h>
#include "module_version.h"
#include "ngx_http_contrast_connector_common.h"
static void *ngx_http_contrast_connector_create_loc_config(ngx_conf_t *cf);
static char *ngx_http_contrast_connector_merge_loc_config(ngx_conf_t *cf,
void * parent, void * child);
static ngx_int_t ngx_http_contrast_connector_module_init(ngx_conf_t *cf);
ngx_int_t ngx_http_contrast_output_filters_init(ngx_conf_t *cf);
/*
* get the current epoch time in millis
*/
ngx_inline int64_t
unix_millis()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
/*
* Convert an nginx str to a null terminated str.
*
* Uses pool *p for allocation.
*/
ngx_inline char *
ngx_str_to_char(const ngx_str_t *a, ngx_pool_t *p)
{
char *str = NULL;
if (a->len == 0) {
goto fail;
}
str = ngx_pnalloc(p, a->len + 1);
if (str == NULL) {
goto fail;
}
ngx_memcpy(str, a->data, a->len);
str[a->len] = '\0';
fail:
return str;
}
/*
* available commands for module
*/
static ngx_command_t ngx_http_contrast_connector_commands[] = {
{
ngx_string("contrast"),
NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_MAIN_CONF
| NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_contrast_connector_conf_t, enable),
NULL
},
{
ngx_string("contrast_debug"),
NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_MAIN_CONF
| NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_contrast_connector_conf_t, debug),
NULL
},
{
ngx_string("contrast_unix_socket"),
NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_MAIN_CONF
| NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_contrast_connector_conf_t, socket_path),
NULL
},
{
ngx_string("contrast_app_name"),
NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_MAIN_CONF
| NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_contrast_connector_conf_t, app_name),
NULL
},
ngx_null_command
};
/*
* module context structure
*/
static ngx_http_module_t ngx_http_contrast_connector_module_ctx = {
/* pre configuration */
NULL,
/* post configuration */
ngx_http_contrast_connector_module_init,
/* create main configuration */
NULL,
/* init main configuration */
NULL,
/* create server configuration */
NULL,
/* merge server configuration */
NULL,
/* create location configuration */
ngx_http_contrast_connector_create_loc_config,
/* merge location configuration */
ngx_http_contrast_connector_merge_loc_config
};
/*
* module definition structure
*/
ngx_module_t ngx_http_contrast_connector_module = {
NGX_MODULE_V1,
/* address of module context */
&ngx_http_contrast_connector_module_ctx,
/* module directives */
ngx_http_contrast_connector_commands,
/* module type */
NGX_HTTP_MODULE,
/* init master */
NULL,
/* init module */
NULL,
/* init process */
NULL,
/* init thread */
NULL,
/* exit thread */
NULL,
/* exit process */
NULL,
/* exit master */
NULL,
/* required */
NGX_MODULE_V1_PADDING
};
/*
* default socket to communicate with contrast service
*/
static const char DEFAULT_SOCKET[] = "/run/contrast-service.sock";
/*
* default name for applications in contrast service
* XXX: Should this be required? can it be determined from the HOST field?
*/
static const char DEFAULT_NAME[] = "unknown";
static void *
ngx_http_contrast_connector_create_loc_config(ngx_conf_t *cf)
{
ngx_http_contrast_connector_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_contrast_connector_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->enable = NGX_CONF_UNSET;
conf->debug = NGX_CONF_UNSET;
return conf;
}
static char *
ngx_http_contrast_connector_merge_loc_config(ngx_conf_t *cf,
void *parent, void *child)
{
ngx_http_contrast_connector_conf_t *prev = parent;
ngx_http_contrast_connector_conf_t *conf = child;
/* default to enable Contrast protection */
ngx_conf_merge_off_value(conf->enable, prev->enable, 1);
ngx_conf_merge_off_value(conf->debug, prev->debug, 0);
ngx_conf_merge_str_value(conf->socket_path, prev->socket_path,
DEFAULT_SOCKET);
ngx_conf_merge_str_value(conf->app_name, prev->app_name, DEFAULT_NAME);
contrast_log(NOTICE, cf->log, 0,
"location/appname: %s", conf->app_name.data);
contrast_log(NOTICE, cf->log, 0,
"analysis: %s", conf->enable ? "enabled" : "disabled");
contrast_log(NOTICE, cf->log, 0,
"analysis-engine socket_path: %s", conf->socket_path.data);
if (conf->debug) {
contrast_log(WARN, cf->log, 0, "!!! DEBUG ENABLED !!!");
}
return NGX_CONF_OK;
}
/*
* init module and place in the http phase chain
*/
static ngx_int_t
ngx_http_contrast_connector_module_init(ngx_conf_t * cf)
{
ngx_http_core_main_conf_t *main_conf = ngx_http_conf_get_module_main_conf(
cf, ngx_http_core_module);
if (main_conf == NULL) {
contrast_log(ERR, cf->log, 0, "Main conf was NULL");
return NGX_ERROR;
}
contrast_log(NOTICE, cf->log, 0,
"Initializing connector module (v%s)", CONTRAST_MODULE_VERSION);
/* attach url handler after the pre-access phase. */
ngx_http_handler_pt *h_preaccess = ngx_array_push(
&main_conf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
if (h_preaccess == NULL) {
contrast_log(ERR, cf->log, 0, "Preaccess handler was NULL");
return NGX_ERROR;
}
*h_preaccess = ngx_http_contrast_connector_preaccess_handler;
/* insert output body filter into output chain */
ngx_http_contrast_output_filters_init(cf);
contrast_log(NOTICE, cf->log, 0,
"Completed initialization of contrast connector module");
return NGX_OK;
}