src/http/modules/ngx_http_secure_link_module.c - nginx-1.7.10

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>
  8. #include <ngx_md5.h>


  9. typedef struct {
  10.     ngx_http_complex_value_t  *variable;
  11.     ngx_http_complex_value_t  *md5;
  12.     ngx_str_t                  secret;
  13. } ngx_http_secure_link_conf_t;


  14. typedef struct {
  15.     ngx_str_t                  expires;
  16. } ngx_http_secure_link_ctx_t;


  17. static ngx_int_t ngx_http_secure_link_old_variable(ngx_http_request_t *r,
  18.     ngx_http_secure_link_conf_t *conf, ngx_http_variable_value_t *v,
  19.     uintptr_t data);
  20. static ngx_int_t ngx_http_secure_link_expires_variable(ngx_http_request_t *r,
  21.     ngx_http_variable_value_t *v, uintptr_t data);
  22. static void *ngx_http_secure_link_create_conf(ngx_conf_t *cf);
  23. static char *ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent,
  24.     void *child);
  25. static ngx_int_t ngx_http_secure_link_add_variables(ngx_conf_t *cf);


  26. static ngx_command_t  ngx_http_secure_link_commands[] = {

  27.     { ngx_string("secure_link"),
  28.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  29.       ngx_http_set_complex_value_slot,
  30.       NGX_HTTP_LOC_CONF_OFFSET,
  31.       offsetof(ngx_http_secure_link_conf_t, variable),
  32.       NULL },

  33.     { ngx_string("secure_link_md5"),
  34.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  35.       ngx_http_set_complex_value_slot,
  36.       NGX_HTTP_LOC_CONF_OFFSET,
  37.       offsetof(ngx_http_secure_link_conf_t, md5),
  38.       NULL },

  39.     { ngx_string("secure_link_secret"),
  40.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  41.       ngx_conf_set_str_slot,
  42.       NGX_HTTP_LOC_CONF_OFFSET,
  43.       offsetof(ngx_http_secure_link_conf_t, secret),
  44.       NULL },

  45.       ngx_null_command
  46. };


  47. static ngx_http_module_t  ngx_http_secure_link_module_ctx = {
  48.     ngx_http_secure_link_add_variables,    /* preconfiguration */
  49.     NULL,                                  /* postconfiguration */

  50.     NULL,                                  /* create main configuration */
  51.     NULL,                                  /* init main configuration */

  52.     NULL,                                  /* create server configuration */
  53.     NULL,                                  /* merge server configuration */

  54.     ngx_http_secure_link_create_conf,      /* create location configuration */
  55.     ngx_http_secure_link_merge_conf        /* merge location configuration */
  56. };


  57. ngx_module_t  ngx_http_secure_link_module = {
  58.     NGX_MODULE_V1,
  59.     &ngx_http_secure_link_module_ctx,      /* module context */
  60.     ngx_http_secure_link_commands,         /* module directives */
  61.     NGX_HTTP_MODULE,                       /* module type */
  62.     NULL,                                  /* init master */
  63.     NULL,                                  /* init module */
  64.     NULL,                                  /* init process */
  65.     NULL,                                  /* init thread */
  66.     NULL,                                  /* exit thread */
  67.     NULL,                                  /* exit process */
  68.     NULL,                                  /* exit master */
  69.     NGX_MODULE_V1_PADDING
  70. };


  71. static ngx_str_t  ngx_http_secure_link_name = ngx_string("secure_link");
  72. static ngx_str_t  ngx_http_secure_link_expires_name =
  73.     ngx_string("secure_link_expires");


  74. static ngx_int_t
  75. ngx_http_secure_link_variable(ngx_http_request_t *r,
  76.     ngx_http_variable_value_t *v, uintptr_t data)
  77. {
  78.     u_char                       *p, *last;
  79.     ngx_str_t                     val, hash;
  80.     time_t                        expires;
  81.     ngx_md5_t                     md5;
  82.     ngx_http_secure_link_ctx_t   *ctx;
  83.     ngx_http_secure_link_conf_t  *conf;
  84.     u_char                        hash_buf[16], md5_buf[16];

  85.     conf = ngx_http_get_module_loc_conf(r, ngx_http_secure_link_module);

  86.     if (conf->secret.data) {
  87.         return ngx_http_secure_link_old_variable(r, conf, v, data);
  88.     }

  89.     if (conf->variable == NULL || conf->md5 == NULL) {
  90.         goto not_found;
  91.     }

  92.     if (ngx_http_complex_value(r, conf->variable, &val) != NGX_OK) {
  93.         return NGX_ERROR;
  94.     }

  95.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  96.                    "secure link: \"%V\"", &val);

  97.     last = val.data + val.len;

  98.     p = ngx_strlchr(val.data, last, ',');
  99.     expires = 0;

  100.     if (p) {
  101.         val.len = p++ - val.data;

  102.         expires = ngx_atotm(p, last - p);
  103.         if (expires <= 0) {
  104.             goto not_found;
  105.         }

  106.         ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_secure_link_ctx_t));
  107.         if (ctx == NULL) {
  108.             return NGX_ERROR;
  109.         }

  110.         ngx_http_set_ctx(r, ctx, ngx_http_secure_link_module);

  111.         ctx->expires.len = last - p;
  112.         ctx->expires.data = p;
  113.     }

  114.     if (val.len > 24) {
  115.         goto not_found;
  116.     }

  117.     hash.len = 16;
  118.     hash.data = hash_buf;

  119.     if (ngx_decode_base64url(&hash, &val) != NGX_OK) {
  120.         goto not_found;
  121.     }

  122.     if (hash.len != 16) {
  123.         goto not_found;
  124.     }

  125.     if (ngx_http_complex_value(r, conf->md5, &val) != NGX_OK) {
  126.         return NGX_ERROR;
  127.     }

  128.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  129.                    "secure link md5: \"%V\"", &val);

  130.     ngx_md5_init(&md5);
  131.     ngx_md5_update(&md5, val.data, val.len);
  132.     ngx_md5_final(md5_buf, &md5);

  133.     if (ngx_memcmp(hash_buf, md5_buf, 16) != 0) {
  134.         goto not_found;
  135.     }

  136.     v->data = (u_char *) ((expires && expires < ngx_time()) ? "0" : "1");
  137.     v->len = 1;
  138.     v->valid = 1;
  139.     v->no_cacheable = 0;
  140.     v->not_found = 0;

  141.     return NGX_OK;

  142. not_found:

  143.     v->not_found = 1;

  144.     return NGX_OK;
  145. }


  146. static ngx_int_t
  147. ngx_http_secure_link_old_variable(ngx_http_request_t *r,
  148.     ngx_http_secure_link_conf_t *conf, ngx_http_variable_value_t *v,
  149.     uintptr_t data)
  150. {
  151.     u_char      *p, *start, *end, *last;
  152.     size_t       len;
  153.     ngx_int_t    n;
  154.     ngx_uint_t   i;
  155.     ngx_md5_t    md5;
  156.     u_char       hash[16];

  157.     p = &r->unparsed_uri.data[1];
  158.     last = r->unparsed_uri.data + r->unparsed_uri.len;

  159.     while (p < last) {
  160.         if (*p++ == '/') {
  161.             start = p;
  162.             goto md5_start;
  163.         }
  164.     }

  165.     goto not_found;

  166. md5_start:

  167.     while (p < last) {
  168.         if (*p++ == '/') {
  169.             end = p - 1;
  170.             goto url_start;
  171.         }
  172.     }

  173.     goto not_found;

  174. url_start:

  175.     len = last - p;

  176.     if (end - start != 32 || len == 0) {
  177.         goto not_found;
  178.     }

  179.     ngx_md5_init(&md5);
  180.     ngx_md5_update(&md5, p, len);
  181.     ngx_md5_update(&md5, conf->secret.data, conf->secret.len);
  182.     ngx_md5_final(hash, &md5);

  183.     for (i = 0; i < 16; i++) {
  184.         n = ngx_hextoi(&start[2 * i], 2);
  185.         if (n == NGX_ERROR || n != hash[i]) {
  186.             goto not_found;
  187.         }
  188.     }

  189.     v->len = len;
  190.     v->valid = 1;
  191.     v->no_cacheable = 0;
  192.     v->not_found = 0;
  193.     v->data = p;

  194.     return NGX_OK;

  195. not_found:

  196.     v->not_found = 1;

  197.     return NGX_OK;
  198. }


  199. static ngx_int_t
  200. ngx_http_secure_link_expires_variable(ngx_http_request_t *r,
  201.     ngx_http_variable_value_t *v, uintptr_t data)
  202. {
  203.     ngx_http_secure_link_ctx_t  *ctx;

  204.     ctx = ngx_http_get_module_ctx(r, ngx_http_secure_link_module);

  205.     if (ctx) {
  206.         v->len = ctx->expires.len;
  207.         v->valid = 1;
  208.         v->no_cacheable = 0;
  209.         v->not_found = 0;
  210.         v->data = ctx->expires.data;

  211.     } else {
  212.         v->not_found = 1;
  213.     }

  214.     return NGX_OK;
  215. }


  216. static void *
  217. ngx_http_secure_link_create_conf(ngx_conf_t *cf)
  218. {
  219.     ngx_http_secure_link_conf_t  *conf;

  220.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_secure_link_conf_t));
  221.     if (conf == NULL) {
  222.         return NULL;
  223.     }

  224.     /*
  225.      * set by ngx_pcalloc():
  226.      *
  227.      *     conf->variable = NULL;
  228.      *     conf->md5 = NULL;
  229.      *     conf->secret = { 0, NULL };
  230.      */

  231.     return conf;
  232. }


  233. static char *
  234. ngx_http_secure_link_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  235. {
  236.     ngx_http_secure_link_conf_t *prev = parent;
  237.     ngx_http_secure_link_conf_t *conf = child;

  238.     if (conf->secret.data) {
  239.         if (conf->variable || conf->md5) {
  240.             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  241.                                "\"secure_link_secret\" cannot be mixed with "
  242.                                "\"secure_link\" and \"secure_link_md5\"");
  243.             return NGX_CONF_ERROR;
  244.         }

  245.         return NGX_CONF_OK;
  246.     }

  247.     if (conf->variable == NULL) {
  248.         conf->variable = prev->variable;
  249.     }

  250.     if (conf->md5 == NULL) {
  251.         conf->md5 = prev->md5;
  252.     }

  253.     if (conf->variable == NULL && conf->md5 == NULL) {
  254.         conf->secret = prev->secret;
  255.     }

  256.     return NGX_CONF_OK;
  257. }


  258. static ngx_int_t
  259. ngx_http_secure_link_add_variables(ngx_conf_t *cf)
  260. {
  261.     ngx_http_variable_t  *var;

  262.     var = ngx_http_add_variable(cf, &ngx_http_secure_link_name, 0);
  263.     if (var == NULL) {
  264.         return NGX_ERROR;
  265.     }

  266.     var->get_handler = ngx_http_secure_link_variable;

  267.     var = ngx_http_add_variable(cf, &ngx_http_secure_link_expires_name, 0);
  268.     if (var == NULL) {
  269.         return NGX_ERROR;
  270.     }

  271.     var->get_handler = ngx_http_secure_link_expires_variable;

  272.     return NGX_OK;
  273. }