122
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
122
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
412
|
13 #define NGX_HTTP_REALIP_XREALIP 0
|
|
14 #define NGX_HTTP_REALIP_XFWD 1
|
|
15 #define NGX_HTTP_REALIP_HEADER 2
|
|
16
|
|
17
|
122
|
18 typedef struct {
|
668
|
19 ngx_array_t *from; /* array of ngx_in_cidr_t */
|
412
|
20 ngx_uint_t type;
|
|
21 ngx_uint_t hash;
|
|
22 ngx_str_t header;
|
540
|
23 #if (NGX_HAVE_UNIX_DOMAIN)
|
544
|
24 ngx_uint_t unixsock; /* unsigned unixsock:2; */
|
540
|
25 #endif
|
122
|
26 } ngx_http_realip_loc_conf_t;
|
|
27
|
|
28
|
396
|
29 typedef struct {
|
|
30 ngx_connection_t *connection;
|
540
|
31 struct sockaddr *sockaddr;
|
|
32 socklen_t socklen;
|
396
|
33 ngx_str_t addr_text;
|
|
34 } ngx_http_realip_ctx_t;
|
|
35
|
|
36
|
122
|
37 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r);
|
540
|
38 static ngx_int_t ngx_http_realip_set_addr(ngx_http_request_t *r, u_char *ip,
|
|
39 size_t len);
|
396
|
40 static void ngx_http_realip_cleanup(void *data);
|
122
|
41 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
42 void *conf);
|
412
|
43 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
122
|
44 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf);
|
|
45 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
|
|
46 void *parent, void *child);
|
230
|
47 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
|
122
|
48
|
|
49
|
|
50 static ngx_command_t ngx_http_realip_commands[] = {
|
|
51
|
|
52 { ngx_string("set_real_ip_from"),
|
|
53 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
54 ngx_http_realip_from,
|
|
55 NGX_HTTP_LOC_CONF_OFFSET,
|
|
56 0,
|
|
57 NULL },
|
|
58
|
|
59 { ngx_string("real_ip_header"),
|
|
60 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
412
|
61 ngx_http_realip,
|
122
|
62 NGX_HTTP_LOC_CONF_OFFSET,
|
412
|
63 0,
|
|
64 NULL },
|
122
|
65
|
|
66 ngx_null_command
|
|
67 };
|
|
68
|
|
69
|
|
70
|
216
|
71 static ngx_http_module_t ngx_http_realip_module_ctx = {
|
122
|
72 NULL, /* preconfiguration */
|
230
|
73 ngx_http_realip_init, /* postconfiguration */
|
122
|
74
|
|
75 NULL, /* create main configuration */
|
|
76 NULL, /* init main configuration */
|
|
77
|
|
78 NULL, /* create server configuration */
|
|
79 NULL, /* merge server configuration */
|
|
80
|
|
81 ngx_http_realip_create_loc_conf, /* create location configuration */
|
|
82 ngx_http_realip_merge_loc_conf /* merge location configuration */
|
|
83 };
|
|
84
|
|
85
|
|
86 ngx_module_t ngx_http_realip_module = {
|
|
87 NGX_MODULE_V1,
|
|
88 &ngx_http_realip_module_ctx, /* module context */
|
|
89 ngx_http_realip_commands, /* module directives */
|
|
90 NGX_HTTP_MODULE, /* module type */
|
|
91 NULL, /* init master */
|
230
|
92 NULL, /* init module */
|
122
|
93 NULL, /* init process */
|
|
94 NULL, /* init thread */
|
|
95 NULL, /* exit thread */
|
|
96 NULL, /* exit process */
|
|
97 NULL, /* exit master */
|
|
98 NGX_MODULE_V1_PADDING
|
|
99 };
|
|
100
|
|
101
|
|
102 static ngx_int_t
|
|
103 ngx_http_realip_handler(ngx_http_request_t *r)
|
|
104 {
|
|
105 u_char *ip, *p;
|
|
106 size_t len;
|
412
|
107 ngx_uint_t i, hash;
|
|
108 ngx_list_part_t *part;
|
|
109 ngx_table_elt_t *header;
|
122
|
110 struct sockaddr_in *sin;
|
668
|
111 ngx_in_cidr_t *from;
|
396
|
112 ngx_connection_t *c;
|
|
113 ngx_http_realip_ctx_t *ctx;
|
122
|
114 ngx_http_realip_loc_conf_t *rlcf;
|
|
115
|
396
|
116 ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
|
|
117
|
|
118 if (ctx) {
|
272
|
119 return NGX_DECLINED;
|
122
|
120 }
|
|
121
|
|
122 rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module);
|
|
123
|
542
|
124 if (rlcf->from == NULL
|
|
125 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
126 && !rlcf->unixsock
|
|
127 #endif
|
|
128 )
|
|
129 {
|
272
|
130 return NGX_DECLINED;
|
122
|
131 }
|
|
132
|
412
|
133 switch (rlcf->type) {
|
|
134
|
|
135 case NGX_HTTP_REALIP_XREALIP:
|
|
136
|
122
|
137 if (r->headers_in.x_real_ip == NULL) {
|
272
|
138 return NGX_DECLINED;
|
122
|
139 }
|
|
140
|
|
141 len = r->headers_in.x_real_ip->value.len;
|
|
142 ip = r->headers_in.x_real_ip->value.data;
|
|
143
|
412
|
144 break;
|
|
145
|
|
146 case NGX_HTTP_REALIP_XFWD:
|
|
147
|
122
|
148 if (r->headers_in.x_forwarded_for == NULL) {
|
272
|
149 return NGX_DECLINED;
|
122
|
150 }
|
|
151
|
|
152 len = r->headers_in.x_forwarded_for->value.len;
|
|
153 ip = r->headers_in.x_forwarded_for->value.data;
|
|
154
|
286
|
155 for (p = ip + len - 1; p > ip; p--) {
|
122
|
156 if (*p == ' ' || *p == ',') {
|
286
|
157 p++;
|
|
158 len -= p - ip;
|
|
159 ip = p;
|
|
160 break;
|
122
|
161 }
|
|
162 }
|
412
|
163
|
|
164 break;
|
|
165
|
|
166 default: /* NGX_HTTP_REALIP_HEADER */
|
|
167
|
|
168 part = &r->headers_in.headers.part;
|
|
169 header = part->elts;
|
|
170
|
|
171 hash = rlcf->hash;
|
|
172 len = rlcf->header.len;
|
|
173 p = rlcf->header.data;
|
|
174
|
|
175 for (i = 0; /* void */ ; i++) {
|
|
176
|
|
177 if (i >= part->nelts) {
|
|
178 if (part->next == NULL) {
|
|
179 break;
|
|
180 }
|
|
181
|
|
182 part = part->next;
|
|
183 header = part->elts;
|
|
184 i = 0;
|
|
185 }
|
|
186
|
|
187 if (hash == header[i].hash
|
|
188 && len == header[i].key.len
|
|
189 && ngx_strncmp(p, header[i].lowcase_key, len) == 0)
|
|
190 {
|
|
191 len = header[i].value.len;
|
|
192 ip = header[i].value.data;
|
|
193
|
|
194 goto found;
|
|
195 }
|
|
196 }
|
|
197
|
|
198 return NGX_DECLINED;
|
122
|
199 }
|
|
200
|
412
|
201 found:
|
|
202
|
396
|
203 c = r->connection;
|
|
204
|
|
205 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "realip: \"%s\"", ip);
|
286
|
206
|
122
|
207 /* AF_INET only */
|
|
208
|
540
|
209 if (c->sockaddr->sa_family == AF_INET) {
|
|
210 sin = (struct sockaddr_in *) c->sockaddr;
|
|
211
|
|
212 from = rlcf->from->elts;
|
|
213 for (i = 0; i < rlcf->from->nelts; i++) {
|
|
214
|
|
215 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
216 "realip: %08XD %08XD %08XD",
|
|
217 sin->sin_addr.s_addr, from[i].mask, from[i].addr);
|
|
218
|
|
219 if ((sin->sin_addr.s_addr & from[i].mask) == from[i].addr) {
|
|
220 return ngx_http_realip_set_addr(r, ip, len);
|
|
221 }
|
|
222 }
|
|
223 }
|
|
224
|
|
225 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
226
|
|
227 if (c->sockaddr->sa_family == AF_UNIX && rlcf->unixsock) {
|
|
228 return ngx_http_realip_set_addr(r, ip, len);
|
448
|
229 }
|
|
230
|
540
|
231 #endif
|
122
|
232
|
540
|
233 return NGX_DECLINED;
|
|
234 }
|
122
|
235
|
|
236
|
540
|
237 static ngx_int_t
|
|
238 ngx_http_realip_set_addr(ngx_http_request_t *r, u_char *ip, size_t len)
|
|
239 {
|
|
240 u_char *p;
|
|
241 ngx_int_t rc;
|
|
242 ngx_addr_t addr;
|
|
243 ngx_connection_t *c;
|
|
244 ngx_pool_cleanup_t *cln;
|
|
245 ngx_http_realip_ctx_t *ctx;
|
286
|
246
|
540
|
247 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
|
|
248 if (cln == NULL) {
|
|
249 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
250 }
|
286
|
251
|
540
|
252 ctx = cln->data;
|
|
253 ngx_http_set_ctx(r, ctx, ngx_http_realip_module);
|
|
254
|
|
255 c = r->connection;
|
|
256
|
|
257 rc = ngx_parse_addr(c->pool, &addr, ip, len);
|
122
|
258
|
540
|
259 switch (rc) {
|
|
260 case NGX_DECLINED:
|
|
261 return NGX_DECLINED;
|
|
262 case NGX_ERROR:
|
|
263 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
264 default: /* NGX_OK */
|
|
265 break;
|
|
266 }
|
122
|
267
|
540
|
268 p = ngx_pnalloc(c->pool, len);
|
|
269 if (p == NULL) {
|
|
270 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
271 }
|
396
|
272
|
540
|
273 ngx_memcpy(p, ip, len);
|
396
|
274
|
540
|
275 cln->handler = ngx_http_realip_cleanup;
|
286
|
276
|
540
|
277 ctx->connection = c;
|
|
278 ctx->sockaddr = c->sockaddr;
|
|
279 ctx->socklen = c->socklen;
|
|
280 ctx->addr_text = c->addr_text;
|
122
|
281
|
540
|
282 c->sockaddr = addr.sockaddr;
|
|
283 c->socklen = addr.socklen;
|
|
284 c->addr_text.len = len;
|
|
285 c->addr_text.data = p;
|
122
|
286
|
272
|
287 return NGX_DECLINED;
|
122
|
288 }
|
|
289
|
|
290
|
396
|
291 static void
|
|
292 ngx_http_realip_cleanup(void *data)
|
|
293 {
|
|
294 ngx_http_realip_ctx_t *ctx = data;
|
|
295
|
540
|
296 ngx_connection_t *c;
|
396
|
297
|
|
298 c = ctx->connection;
|
|
299
|
540
|
300 c->sockaddr = ctx->sockaddr;
|
|
301 c->socklen = ctx->socklen;
|
396
|
302 c->addr_text = ctx->addr_text;
|
|
303 }
|
|
304
|
|
305
|
122
|
306 static char *
|
|
307 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
308 {
|
|
309 ngx_http_realip_loc_conf_t *rlcf = conf;
|
|
310
|
412
|
311 ngx_int_t rc;
|
|
312 ngx_str_t *value;
|
454
|
313 ngx_cidr_t cidr;
|
668
|
314 ngx_in_cidr_t *from;
|
122
|
315
|
540
|
316 value = cf->args->elts;
|
|
317
|
|
318 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
319
|
|
320 if (ngx_strcmp(value[1].data, "unix:") == 0) {
|
|
321 rlcf->unixsock = 1;
|
|
322 return NGX_CONF_OK;
|
|
323 }
|
|
324
|
|
325 #endif
|
|
326
|
122
|
327 if (rlcf->from == NULL) {
|
|
328 rlcf->from = ngx_array_create(cf->pool, 2,
|
668
|
329 sizeof(ngx_in_cidr_t));
|
122
|
330 if (rlcf->from == NULL) {
|
|
331 return NGX_CONF_ERROR;
|
|
332 }
|
|
333 }
|
|
334
|
|
335 from = ngx_array_push(rlcf->from);
|
|
336 if (from == NULL) {
|
|
337 return NGX_CONF_ERROR;
|
|
338 }
|
|
339
|
454
|
340 rc = ngx_ptocidr(&value[1], &cidr);
|
326
|
341
|
|
342 if (rc == NGX_ERROR) {
|
122
|
343 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
|
|
344 &value[1]);
|
|
345 return NGX_CONF_ERROR;
|
|
346 }
|
|
347
|
454
|
348 if (cidr.family != AF_INET) {
|
|
349 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
540
|
350 "\"set_real_ip_from\" supports IPv4 only");
|
454
|
351 return NGX_CONF_ERROR;
|
|
352 }
|
|
353
|
326
|
354 if (rc == NGX_DONE) {
|
|
355 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
356 "low address bits of %V are meaningless", &value[1]);
|
|
357 }
|
|
358
|
454
|
359 from->mask = cidr.u.in.mask;
|
|
360 from->addr = cidr.u.in.addr;
|
122
|
361
|
|
362 return NGX_CONF_OK;
|
|
363 }
|
|
364
|
|
365
|
412
|
366 static char *
|
|
367 ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
368 {
|
|
369 ngx_http_realip_loc_conf_t *rlcf = conf;
|
|
370
|
|
371 ngx_str_t *value;
|
|
372
|
|
373 value = cf->args->elts;
|
|
374
|
|
375 if (ngx_strcmp(value[1].data, "X-Real-IP") == 0) {
|
|
376 rlcf->type = NGX_HTTP_REALIP_XREALIP;
|
|
377 return NGX_CONF_OK;
|
|
378 }
|
|
379
|
|
380 if (ngx_strcmp(value[1].data, "X-Forwarded-For") == 0) {
|
|
381 rlcf->type = NGX_HTTP_REALIP_XFWD;
|
|
382 return NGX_CONF_OK;
|
|
383 }
|
|
384
|
|
385 rlcf->type = NGX_HTTP_REALIP_HEADER;
|
|
386 rlcf->hash = ngx_hash_strlow(value[1].data, value[1].data, value[1].len);
|
|
387 rlcf->header = value[1];
|
|
388
|
|
389 return NGX_CONF_OK;
|
|
390 }
|
|
391
|
|
392
|
122
|
393 static void *
|
|
394 ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
|
|
395 {
|
|
396 ngx_http_realip_loc_conf_t *conf;
|
|
397
|
|
398 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t));
|
|
399 if (conf == NULL) {
|
496
|
400 return NULL;
|
122
|
401 }
|
|
402
|
|
403 /*
|
|
404 * set by ngx_pcalloc():
|
|
405 *
|
|
406 * conf->from = NULL;
|
412
|
407 * conf->hash = 0;
|
|
408 * conf->header = { 0, NULL };
|
122
|
409 */
|
|
410
|
412
|
411 conf->type = NGX_CONF_UNSET_UINT;
|
544
|
412 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
413 conf->unixsock = 2;
|
|
414 #endif
|
122
|
415
|
|
416 return conf;
|
|
417 }
|
|
418
|
|
419
|
|
420 static char *
|
|
421 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
422 {
|
|
423 ngx_http_realip_loc_conf_t *prev = parent;
|
|
424 ngx_http_realip_loc_conf_t *conf = child;
|
|
425
|
|
426 if (conf->from == NULL) {
|
|
427 conf->from = prev->from;
|
544
|
428 }
|
|
429
|
540
|
430 #if (NGX_HAVE_UNIX_DOMAIN)
|
544
|
431 if (conf->unixsock == 2) {
|
|
432 conf->unixsock = (prev->unixsock == 2) ? 0 : prev->unixsock;
|
|
433 }
|
540
|
434 #endif
|
122
|
435
|
412
|
436 ngx_conf_merge_uint_value(conf->type, prev->type, NGX_HTTP_REALIP_XREALIP);
|
|
437
|
|
438 if (conf->header.len == 0) {
|
|
439 conf->hash = prev->hash;
|
|
440 conf->header = prev->header;
|
|
441 }
|
122
|
442
|
|
443 return NGX_CONF_OK;
|
|
444 }
|
|
445
|
|
446
|
|
447 static ngx_int_t
|
230
|
448 ngx_http_realip_init(ngx_conf_t *cf)
|
122
|
449 {
|
|
450 ngx_http_handler_pt *h;
|
|
451 ngx_http_core_main_conf_t *cmcf;
|
|
452
|
230
|
453 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
122
|
454
|
|
455 h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
|
|
456 if (h == NULL) {
|
|
457 return NGX_ERROR;
|
|
458 }
|
|
459
|
|
460 *h = ngx_http_realip_handler;
|
|
461
|
130
|
462 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
122
|
463 if (h == NULL) {
|
|
464 return NGX_ERROR;
|
|
465 }
|
|
466
|
|
467 *h = ngx_http_realip_handler;
|
|
468
|
|
469 return NGX_OK;
|
|
470 }
|