Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

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_http_null_variable
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_url_t             u;
0323     ngx_cidr_t            c, *cidr;
0324     ngx_uint_t            i;
0325     struct sockaddr_in   *sin;
0326 #if (NGX_HAVE_INET6)
0327     struct sockaddr_in6  *sin6;
0328 #endif
0329 
0330     value = cf->args->elts;
0331 
0332     if (rlcf->from == NULL) {
0333         rlcf->from = ngx_array_create(cf->pool, 2,
0334                                       sizeof(ngx_cidr_t));
0335         if (rlcf->from == NULL) {
0336             return NGX_CONF_ERROR;
0337         }
0338     }
0339 
0340 #if (NGX_HAVE_UNIX_DOMAIN)
0341 
0342     if (ngx_strcmp(value[1].data, "unix:") == 0) {
0343         cidr = ngx_array_push(rlcf->from);
0344         if (cidr == NULL) {
0345             return NGX_CONF_ERROR;
0346         }
0347 
0348         cidr->family = AF_UNIX;
0349         return NGX_CONF_OK;
0350     }
0351 
0352 #endif
0353 
0354     rc = ngx_ptocidr(&value[1], &c);
0355 
0356     if (rc != NGX_ERROR) {
0357         if (rc == NGX_DONE) {
0358             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
0359                                "low address bits of %V are meaningless",
0360                                &value[1]);
0361         }
0362 
0363         cidr = ngx_array_push(rlcf->from);
0364         if (cidr == NULL) {
0365             return NGX_CONF_ERROR;
0366         }
0367 
0368         *cidr = c;
0369 
0370         return NGX_CONF_OK;
0371     }
0372 
0373     ngx_memzero(&u, sizeof(ngx_url_t));
0374     u.host = value[1];
0375 
0376     if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
0377         if (u.err) {
0378             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0379                                "%s in set_real_ip_from \"%V\"",
0380                                u.err, &u.host);
0381         }
0382 
0383         return NGX_CONF_ERROR;
0384     }
0385 
0386     cidr = ngx_array_push_n(rlcf->from, u.naddrs);
0387     if (cidr == NULL) {
0388         return NGX_CONF_ERROR;
0389     }
0390 
0391     ngx_memzero(cidr, u.naddrs * sizeof(ngx_cidr_t));
0392 
0393     for (i = 0; i < u.naddrs; i++) {
0394         cidr[i].family = u.addrs[i].sockaddr->sa_family;
0395 
0396         switch (cidr[i].family) {
0397 
0398 #if (NGX_HAVE_INET6)
0399         case AF_INET6:
0400             sin6 = (struct sockaddr_in6 *) u.addrs[i].sockaddr;
0401             cidr[i].u.in6.addr = sin6->sin6_addr;
0402             ngx_memset(cidr[i].u.in6.mask.s6_addr, 0xff, 16);
0403             break;
0404 #endif
0405 
0406         default: /* AF_INET */
0407             sin = (struct sockaddr_in *) u.addrs[i].sockaddr;
0408             cidr[i].u.in.addr = sin->sin_addr.s_addr;
0409             cidr[i].u.in.mask = 0xffffffff;
0410             break;
0411         }
0412     }
0413 
0414     return NGX_CONF_OK;
0415 }
0416 
0417 
0418 static char *
0419 ngx_http_realip(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0420 {
0421     ngx_http_realip_loc_conf_t *rlcf = conf;
0422 
0423     ngx_str_t  *value;
0424 
0425     if (rlcf->type != NGX_CONF_UNSET_UINT) {
0426         return "is duplicate";
0427     }
0428 
0429     value = cf->args->elts;
0430 
0431     if (ngx_strcmp(value[1].data, "X-Real-IP") == 0) {
0432         rlcf->type = NGX_HTTP_REALIP_XREALIP;
0433         return NGX_CONF_OK;
0434     }
0435 
0436     if (ngx_strcmp(value[1].data, "X-Forwarded-For") == 0) {
0437         rlcf->type = NGX_HTTP_REALIP_XFWD;
0438         return NGX_CONF_OK;
0439     }
0440 
0441     if (ngx_strcmp(value[1].data, "proxy_protocol") == 0) {
0442         rlcf->type = NGX_HTTP_REALIP_PROXY;
0443         return NGX_CONF_OK;
0444     }
0445 
0446     rlcf->type = NGX_HTTP_REALIP_HEADER;
0447     rlcf->hash = ngx_hash_strlow(value[1].data, value[1].data, value[1].len);
0448     rlcf->header = value[1];
0449 
0450     return NGX_CONF_OK;
0451 }
0452 
0453 
0454 static void *
0455 ngx_http_realip_create_loc_conf(ngx_conf_t *cf)
0456 {
0457     ngx_http_realip_loc_conf_t  *conf;
0458 
0459     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_realip_loc_conf_t));
0460     if (conf == NULL) {
0461         return NULL;
0462     }
0463 
0464     /*
0465      * set by ngx_pcalloc():
0466      *
0467      *     conf->from = NULL;
0468      *     conf->hash = 0;
0469      *     conf->header = { 0, NULL };
0470      */
0471 
0472     conf->type = NGX_CONF_UNSET_UINT;
0473     conf->recursive = NGX_CONF_UNSET;
0474 
0475     return conf;
0476 }
0477 
0478 
0479 static char *
0480 ngx_http_realip_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
0481 {
0482     ngx_http_realip_loc_conf_t  *prev = parent;
0483     ngx_http_realip_loc_conf_t  *conf = child;
0484 
0485     if (conf->from == NULL) {
0486         conf->from = prev->from;
0487     }
0488 
0489     ngx_conf_merge_uint_value(conf->type, prev->type, NGX_HTTP_REALIP_XREALIP);
0490     ngx_conf_merge_value(conf->recursive, prev->recursive, 0);
0491 
0492     if (conf->header.len == 0) {
0493         conf->hash = prev->hash;
0494         conf->header = prev->header;
0495     }
0496 
0497     return NGX_CONF_OK;
0498 }
0499 
0500 
0501 static ngx_int_t
0502 ngx_http_realip_add_variables(ngx_conf_t *cf)
0503 {
0504     ngx_http_variable_t  *var, *v;
0505 
0506     for (v = ngx_http_realip_vars; v->name.len; v++) {
0507         var = ngx_http_add_variable(cf, &v->name, v->flags);
0508         if (var == NULL) {
0509             return NGX_ERROR;
0510         }
0511 
0512         var->get_handler = v->get_handler;
0513         var->data = v->data;
0514     }
0515 
0516     return NGX_OK;
0517 }
0518 
0519 
0520 static ngx_int_t
0521 ngx_http_realip_init(ngx_conf_t *cf)
0522 {
0523     ngx_http_handler_pt        *h;
0524     ngx_http_core_main_conf_t  *cmcf;
0525 
0526     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
0527 
0528     h = ngx_array_push(&cmcf->phases[NGX_HTTP_POST_READ_PHASE].handlers);
0529     if (h == NULL) {
0530         return NGX_ERROR;
0531     }
0532 
0533     *h = ngx_http_realip_handler;
0534 
0535     h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
0536     if (h == NULL) {
0537         return NGX_ERROR;
0538     }
0539 
0540     *h = ngx_http_realip_handler;
0541 
0542     return NGX_OK;
0543 }
0544 
0545 
0546 static ngx_http_realip_ctx_t *
0547 ngx_http_realip_get_module_ctx(ngx_http_request_t *r)
0548 {
0549     ngx_pool_cleanup_t     *cln;
0550     ngx_http_realip_ctx_t  *ctx;
0551 
0552     ctx = ngx_http_get_module_ctx(r, ngx_http_realip_module);
0553 
0554     if (ctx == NULL && (r->internal || r->filter_finalize)) {
0555 
0556         /*
0557          * if module context was reset, the original address
0558          * can still be found in the cleanup handler
0559          */
0560 
0561         for (cln = r->pool->cleanup; cln; cln = cln->next) {
0562             if (cln->handler == ngx_http_realip_cleanup) {
0563                 ctx = cln->data;
0564                 break;
0565             }
0566         }
0567     }
0568 
0569     return ctx;
0570 }
0571 
0572 
0573 static ngx_int_t
0574 ngx_http_realip_remote_addr_variable(ngx_http_request_t *r,
0575     ngx_http_variable_value_t *v, uintptr_t data)
0576 {
0577     ngx_str_t              *addr_text;
0578     ngx_http_realip_ctx_t  *ctx;
0579 
0580     ctx = ngx_http_realip_get_module_ctx(r);
0581 
0582     addr_text = ctx ? &ctx->addr_text : &r->connection->addr_text;
0583 
0584     v->len = addr_text->len;
0585     v->valid = 1;
0586     v->no_cacheable = 0;
0587     v->not_found = 0;
0588     v->data = addr_text->data;
0589 
0590     return NGX_OK;
0591 }
0592 
0593 
0594 static ngx_int_t
0595 ngx_http_realip_remote_port_variable(ngx_http_request_t *r,
0596     ngx_http_variable_value_t *v, uintptr_t data)
0597 {
0598     ngx_uint_t              port;
0599     struct sockaddr        *sa;
0600     ngx_http_realip_ctx_t  *ctx;
0601 
0602     ctx = ngx_http_realip_get_module_ctx(r);
0603 
0604     sa = ctx ? ctx->sockaddr : r->connection->sockaddr;
0605 
0606     v->len = 0;
0607     v->valid = 1;
0608     v->no_cacheable = 0;
0609     v->not_found = 0;
0610 
0611     v->data = ngx_pnalloc(r->pool, sizeof("65535") - 1);
0612     if (v->data == NULL) {
0613         return NGX_ERROR;
0614     }
0615 
0616     port = ngx_inet_get_port(sa);
0617 
0618     if (port > 0 && port < 65536) {
0619         v->len = ngx_sprintf(v->data, "%ui", port) - v->data;
0620     }
0621 
0622     return NGX_OK;
0623 }