Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_http.h>
0011 
0012 
0013 #define NGX_HTTP_REALIP_XREALIP  0
0014 #define NGX_HTTP_REALIP_XFWD     1
0015 #define NGX_HTTP_REALIP_HEADER   2
0016 #define NGX_HTTP_REALIP_PROXY    3
0017 
0018 
0019 typedef struct {
0020     ngx_array_t       *from;     /* array of ngx_cidr_t */
0021     ngx_uint_t         type;
0022     ngx_uint_t         hash;
0023     ngx_str_t          header;
0024     ngx_flag_t         recursive;
0025 } ngx_http_realip_loc_conf_t;
0026 
0027 
0028 typedef struct {
0029     ngx_connection_t  *connection;
0030     struct sockaddr   *sockaddr;
0031     socklen_t          socklen;
0032     ngx_str_t          addr_text;
0033 } ngx_http_realip_ctx_t;
0034 
0035 
0036 static ngx_int_t ngx_http_realip_handler(ngx_http_request_t *r);
0037 static ngx_int_t ngx_http_realip_set_addr(ngx_http_request_t *r,
0038     ngx_addr_t *addr);
0039 static void ngx_http_realip_cleanup(void *data);
0040 static char *ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd,
0041     void *conf);
0042 static char *ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0043 static void *ngx_http_realip_create_loc_conf(ngx_conf_t *cf);
0044 static char *ngx_http_realip_merge_loc_conf(ngx_conf_t *cf,
0045     void *parent, void *child);
0046 static ngx_int_t ngx_http_realip_add_variables(ngx_conf_t *cf);
0047 static ngx_int_t ngx_http_realip_init(ngx_conf_t *cf);
0048 static ngx_http_realip_ctx_t *ngx_http_realip_get_module_ctx(
0049     ngx_http_request_t *r);
0050 
0051 
0052 static ngx_int_t ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
0053     ngx_http_variable_value_t *v, uintptr_t data);
0054 static ngx_int_t ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
0055     ngx_http_variable_value_t *v, uintptr_t data);
0056 
0057 
0058 static ngx_command_t  ngx_http_realip_commands[] = {
0059 
0060     { ngx_string("set_real_ip_from"),
0061       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0062       ngx_http_realip_from,
0063       NGX_HTTP_LOC_CONF_OFFSET,
0064       0,
0065       NULL },
0066 
0067     { ngx_string("real_ip_header"),
0068       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
0069       ngx_http_realip,
0070       NGX_HTTP_LOC_CONF_OFFSET,
0071       0,
0072       NULL },
0073 
0074     { ngx_string("real_ip_recursive"),
0075       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
0076       ngx_conf_set_flag_slot,
0077       NGX_HTTP_LOC_CONF_OFFSET,
0078       offsetof(ngx_http_realip_loc_conf_t, recursive),
0079       NULL },
0080 
0081       ngx_null_command
0082 };
0083 
0084 
0085 
0086 static ngx_http_module_t  ngx_http_realip_module_ctx = {
0087     ngx_http_realip_add_variables,         /* preconfiguration */
0088     ngx_http_realip_init,                  /* postconfiguration */
0089 
0090     NULL,                                  /* create main configuration */
0091     NULL,                                  /* init main configuration */
0092 
0093     NULL,                                  /* create server configuration */
0094     NULL,                                  /* merge server configuration */
0095 
0096     ngx_http_realip_create_loc_conf,       /* create location configuration */
0097     ngx_http_realip_merge_loc_conf         /* merge location configuration */
0098 };
0099 
0100 
0101 ngx_module_t  ngx_http_realip_module = {
0102     NGX_MODULE_V1,
0103     &ngx_http_realip_module_ctx,           /* module context */
0104     ngx_http_realip_commands,              /* module directives */
0105     NGX_HTTP_MODULE,                       /* module type */
0106     NULL,                                  /* init master */
0107     NULL,                                  /* init module */
0108     NULL,                                  /* init process */
0109     NULL,                                  /* init thread */
0110     NULL,                                  /* exit thread */
0111     NULL,                                  /* exit process */
0112     NULL,                                  /* exit master */
0113     NGX_MODULE_V1_PADDING
0114 };
0115 
0116 
0117 static ngx_http_variable_t  ngx_http_realip_vars[] = {
0118 
0119     { ngx_string("realip_remote_addr"), NULL,
0120       ngx_http_realip_remote_addr_variable, 0, 0, 0 },
0121 
0122     { ngx_string("realip_remote_port"), NULL,
0123       ngx_http_realip_remote_port_variable, 0, 0, 0 },
0124 
0125     { ngx_null_string, NULL, NULL, 0, 0, 0 }
0126 };
0127 
0128 
0129 static ngx_int_t
0130 ngx_http_realip_handler(ngx_http_request_t *r)
0131 {
0132     u_char                      *p;
0133     size_t                       len;
0134     ngx_str_t                   *value;
0135     ngx_uint_t                   i, hash;
0136     ngx_addr_t                   addr;
0137     ngx_array_t                 *xfwd;
0138     ngx_list_part_t             *part;
0139     ngx_table_elt_t             *header;
0140     ngx_connection_t            *c;
0141     ngx_http_realip_ctx_t       *ctx;
0142     ngx_http_realip_loc_conf_t  *rlcf;
0143 
0144     rlcf = ngx_http_get_module_loc_conf(r, ngx_http_realip_module);
0145 
0146     if (rlcf->from == NULL) {
0147         return NGX_DECLINED;
0148     }
0149 
0150     ctx = ngx_http_realip_get_module_ctx(r);
0151 
0152     if (ctx) {
0153         return NGX_DECLINED;
0154     }
0155 
0156     switch (rlcf->type) {
0157 
0158     case NGX_HTTP_REALIP_XREALIP:
0159 
0160         if (r->headers_in.x_real_ip == NULL) {
0161             return NGX_DECLINED;
0162         }
0163 
0164         value = &r->headers_in.x_real_ip->value;
0165         xfwd = NULL;
0166 
0167         break;
0168 
0169     case NGX_HTTP_REALIP_XFWD:
0170 
0171         xfwd = &r->headers_in.x_forwarded_for;
0172 
0173         if (xfwd->elts == NULL) {
0174             return NGX_DECLINED;
0175         }
0176 
0177         value = NULL;
0178 
0179         break;
0180 
0181     case NGX_HTTP_REALIP_PROXY:
0182 
0183         value = &r->connection->proxy_protocol_addr;
0184 
0185         if (value->len == 0) {
0186             return NGX_DECLINED;
0187         }
0188 
0189         xfwd = NULL;
0190 
0191         break;
0192 
0193     default: /* NGX_HTTP_REALIP_HEADER */
0194 
0195         part = &r->headers_in.headers.part;
0196         header = part->elts;
0197 
0198         hash = rlcf->hash;
0199         len = rlcf->header.len;
0200         p = rlcf->header.data;
0201 
0202         for (i = 0; /* void */ ; i++) {
0203 
0204             if (i >= part->nelts) {
0205                 if (part->next == NULL) {
0206                     break;
0207                 }
0208 
0209                 part = part->next;
0210                 header = part->elts;
0211                 i = 0;
0212             }
0213 
0214             if (hash == header[i].hash
0215                 && len == header[i].key.len
0216                 && ngx_strncmp(p, header[i].lowcase_key, len) == 0)
0217             {
0218                 value = &header[i].value;
0219                 xfwd = NULL;
0220 
0221                 goto found;
0222             }
0223         }
0224 
0225         return NGX_DECLINED;
0226     }
0227 
0228 found:
0229 
0230     c = r->connection;
0231 
0232     addr.sockaddr = c->sockaddr;
0233     addr.socklen = c->socklen;
0234     /* addr.name = c->addr_text; */
0235 
0236     if (ngx_http_get_forwarded_addr(r, &addr, xfwd, value, rlcf->from,
0237                                     rlcf->recursive)
0238         != NGX_DECLINED)
0239     {
0240         if (rlcf->type == NGX_HTTP_REALIP_PROXY) {
0241             ngx_inet_set_port(addr.sockaddr, c->proxy_protocol_port);
0242         }
0243 
0244         return ngx_http_realip_set_addr(r, &addr);
0245     }
0246 
0247     return NGX_DECLINED;
0248 }
0249 
0250 
0251 static ngx_int_t
0252 ngx_http_realip_set_addr(ngx_http_request_t *r, ngx_addr_t *addr)
0253 {
0254     size_t                  len;
0255     u_char                 *p;
0256     u_char                  text[NGX_SOCKADDR_STRLEN];
0257     ngx_connection_t       *c;
0258     ngx_pool_cleanup_t     *cln;
0259     ngx_http_realip_ctx_t  *ctx;
0260 
0261     cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_realip_ctx_t));
0262     if (cln == NULL) {
0263         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0264     }
0265 
0266     ctx = cln->data;
0267 
0268     c = r->connection;
0269 
0270     len = ngx_sock_ntop(addr->sockaddr, addr->socklen, text,
0271                         NGX_SOCKADDR_STRLEN, 0);
0272     if (len == 0) {
0273         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0274     }
0275 
0276     p = ngx_pnalloc(c->pool, len);
0277     if (p == NULL) {
0278         return NGX_HTTP_INTERNAL_SERVER_ERROR;
0279     }
0280 
0281     ngx_memcpy(p, text, len);
0282 
0283     cln->handler = ngx_http_realip_cleanup;
0284     ngx_http_set_ctx(r, ctx, ngx_http_realip_module);
0285 
0286     ctx->connection = c;
0287     ctx->sockaddr = c->sockaddr;
0288     ctx->socklen = c->socklen;
0289     ctx->addr_text = c->addr_text;
0290 
0291     c->sockaddr = addr->sockaddr;
0292     c->socklen = addr->socklen;
0293     c->addr_text.len = len;
0294     c->addr_text.data = p;
0295 
0296     return NGX_DECLINED;
0297 }
0298 
0299 
0300 static void
0301 ngx_http_realip_cleanup(void *data)
0302 {
0303     ngx_http_realip_ctx_t *ctx = data;
0304 
0305     ngx_connection_t  *c;
0306 
0307     c = ctx->connection;
0308 
0309     c->sockaddr = ctx->sockaddr;
0310     c->socklen = ctx->socklen;
0311     c->addr_text = ctx->addr_text;
0312 }
0313 
0314 
0315 static char *
0316 ngx_http_realip_from(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0317 {
0318     ngx_http_realip_loc_conf_t *rlcf = conf;
0319 
0320     ngx_int_t                rc;
0321     ngx_str_t               *value;
0322     ngx_cidr_t              *cidr;
0323 
0324     value = cf->args->elts;
0325 
0326     if (rlcf->from == NULL) {
0327         rlcf->from = ngx_array_create(cf->pool, 2,
0328                                       sizeof(ngx_cidr_t));
0329         if (rlcf->from == NULL) {
0330             return NGX_CONF_ERROR;
0331         }
0332     }
0333 
0334     cidr = ngx_array_push(rlcf->from);
0335     if (cidr == NULL) {
0336         return NGX_CONF_ERROR;
0337     }
0338 
0339 #if (NGX_HAVE_UNIX_DOMAIN)
0340 
0341     if (ngx_strcmp(value[1].data, "unix:") == 0) {
0342         cidr->family = AF_UNIX;
0343         return NGX_CONF_OK;
0344     }
0345 
0346 #endif
0347 
0348     rc = ngx_ptocidr(&value[1], cidr);
0349 
0350     if (rc == NGX_ERROR) {
0351         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"",
0352                            &value[1]);
0353         return NGX_CONF_ERROR;
0354     }
0355 
0356     if (rc == NGX_DONE) {
0357         ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
0358                            "low address bits of %V are meaningless", &value[1]);
0359     }
0360 
0361     return NGX_CONF_OK;
0362 }
0363 
0364 
0365 static char *
0366 ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0367 {
0368     ngx_http_realip_loc_conf_t *rlcf = conf;
0369 
0370     ngx_str_t  *value;
0371 
0372     if (rlcf->type != NGX_CONF_UNSET_UINT) {
0373         return "is duplicate";
0374     }
0375 
0376     value = cf->args->elts;
0377 
0378     if (ngx_strcmp(value[1].data, "X-Real-IP") == 0) {
0379         rlcf->type = NGX_HTTP_REALIP_XREALIP;
0380         return NGX_CONF_OK;
0381     }
0382 
0383     if (ngx_strcmp(value[1].data, "X-Forwarded-For") == 0) {
0384         rlcf->type = NGX_HTTP_REALIP_XFWD;
0385         return NGX_CONF_OK;
0386     }
0387 
0388     if (ngx_strcmp(value[1].data, "proxy_protocol") == 0) {
0389         rlcf->type = NGX_HTTP_REALIP_PROXY;
0390         return NGX_CONF_OK;
0391     }
0392 
0393     rlcf->type = NGX_HTTP_REALIP_HEADER;
0394     rlcf->hash = ngx_hash_strlow(value[1].data, value[1].data, value[1].len);
0395     rlcf->header = value[1];
0396 
0397     return NGX_CONF_OK;
0398 }
0399 
0400 
0401 static void *
0402 ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
0403 {
0404     ngx_http_realip_loc_conf_t  *conf;
0405 
0406     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t));
0407     if (conf == NULL) {
0408         return NULL;
0409     }
0410 
0411     /*
0412      * set by ngx_pcalloc():
0413      *
0414      *     conf->from = NULL;
0415      *     conf->hash = 0;
0416      *     conf->header = { 0, NULL };
0417      */
0418 
0419     conf->type = NGX_CONF_UNSET_UINT;
0420     conf->recursive = NGX_CONF_UNSET;
0421 
0422     return conf;
0423 }
0424 
0425 
0426 static char *
0427 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
0428 {
0429     ngx_http_realip_loc_conf_t  *prev = parent;
0430     ngx_http_realip_loc_conf_t  *conf = child;
0431 
0432     if (conf->from == NULL) {
0433         conf->from = prev->from;
0434     }
0435 
0436     ngx_conf_merge_uint_value(conf->type, prev->type, NGX_HTTP_REALIP_XREALIP);
0437     ngx_conf_merge_value(conf->recursive, prev->recursive, 0);
0438 
0439     if (conf->header.len == 0) {
0440         conf->hash = prev->hash;
0441         conf->header = prev->header;
0442     }
0443 
0444     return NGX_CONF_OK;
0445 }
0446 
0447 
0448 static ngx_int_t
0449 ngx_http_realip_add_variables(ngx_conf_t *cf)
0450 {
0451     ngx_http_variable_t  *var, *v;
0452 
0453     for (v = ngx_http_realip_vars; v->name.len; v++) {
0454         var = ngx_http_add_variable(cf, &v->name, v->flags);
0455         if (var == NULL) {
0456             return NGX_ERROR;
0457         }
0458 
0459         var->get_handler = v->get_handler;
0460         var->data = v->data;
0461     }
0462 
0463     return NGX_OK;
0464 }
0465 
0466 
0467 static ngx_int_t
0468 ngx_http_realip_init(ngx_conf_t *cf)
0469 {
0470     ngx_http_handler_pt        *h;
0471     ngx_http_core_main_conf_t  *cmcf;
0472 
0473     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
0474 
0475     h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
0476     if (h == NULL) {
0477         return NGX_ERROR;
0478     }
0479 
0480     *h = ngx_http_realip_handler;
0481 
0482     h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
0483     if (h == NULL) {
0484         return NGX_ERROR;
0485     }
0486 
0487     *h = ngx_http_realip_handler;
0488 
0489     return NGX_OK;
0490 }
0491 
0492 
0493 static ngx_http_realip_ctx_t *
0494 ngx_http_realip_get_module_ctx(ngx_http_request_t *r)
0495 {
0496     ngx_pool_cleanup_t     *cln;
0497     ngx_http_realip_ctx_t  *ctx;
0498 
0499     ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
0500 
0501     if (ctx == NULL && (r->internal || r->filter_finalize)) {
0502 
0503         /*
0504          * if module context was reset, the original address
0505          * can still be found in the cleanup handler
0506          */
0507 
0508         for (cln = r->pool->cleanup; cln; cln = cln->next) {
0509             if (cln->handler == ngx_http_realip_cleanup) {
0510                 ctx = cln->data;
0511                 break;
0512             }
0513         }
0514     }
0515 
0516     return ctx;
0517 }
0518 
0519 
0520 static ngx_int_t
0521 ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
0522     ngx_http_variable_value_t *v, uintptr_t data)
0523 {
0524     ngx_str_t              *addr_text;
0525     ngx_http_realip_ctx_t  *ctx;
0526 
0527     ctx = ngx_http_realip_get_module_ctx(r);
0528 
0529     addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text;
0530 
0531     v->len = addr_text->len;
0532     v->valid = 1;
0533     v->no_cacheable = 0;
0534     v->not_found = 0;
0535     v->data = addr_text->data;
0536 
0537     return NGX_OK;
0538 }
0539 
0540 
0541 static ngx_int_t
0542 ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
0543     ngx_http_variable_value_t *v, uintptr_t data)
0544 {
0545     ngx_uint_t              port;
0546     struct sockaddr        *sa;
0547     ngx_http_realip_ctx_t  *ctx;
0548 
0549     ctx = ngx_http_realip_get_module_ctx(r);
0550 
0551     sa = ctx ? ctx->sockaddr : r->connection->sockaddr;
0552 
0553     v->len = 0;
0554     v->valid = 1;
0555     v->no_cacheable = 0;
0556     v->not_found = 0;
0557 
0558     v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
0559     if (v->data == NULL) {
0560         return NGX_ERROR;
0561     }
0562 
0563     port = ngx_inet_get_port(sa);
0564 
0565     if (port > 0 && port < 65536) {
0566         v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
0567     }
0568 
0569     return NGX_OK;
0570 }