/** * nginx handler to feed request body (if any) to IronBee * * @param[in] r the nginx request object * @return NGX_DECLINED for normal operation * @return NGX_DONE if body is not yet available (processing will resume * on new data) * @return Error status if set by IronBee on sight of request data. */ ngx_int_t ngxib_handler(ngx_http_request_t *r) { ngx_chain_t *link; ngxib_req_ctx *ctx; ngx_int_t rv = NGX_DECLINED; ngx_http_request_body_t *rb; /* Don't process internal requests */ if (r->internal) return rv; ctx = ngx_http_get_module_ctx(r, ngx_ironbee_module); if (ctx->body_done) return rv; /* We already completed handling of no-body requests * when we looked at headers */ if (!ngxib_has_request_body(r, ctx)) return rv; ngx_regex_malloc_init(r->pool); /* We can now read the body. * This may come asynchronously in many chunks, so we need * to check for AGAIN and return DONE if waiting. * We pass it a handler to go round again while waiting. * * TODO: figure out how to pass data to ironbee asynchronously */ rv = ngx_http_read_client_request_body(r, ngxib_post_handler); if (rv == NGX_AGAIN) { ctx->body_wait = 1; cleanup_return NGX_DONE; } /* We now have the request body. Feed it to ironbee */ rb = r->request_body; if (!rb) { ib_log_error_tx(ctx->tx, "Failed to read request body."); cleanup_return NGX_HTTP_INTERNAL_SERVER_ERROR; } if (!rb->bufs) { /* I think this shouldn't happen */ /* But rethink if this turns up in logs when all is fine */ ib_log_error_tx(ctx->tx, "Probable error reading request body."); } if (rb->temp_file && (rb->temp_file->file.fd != NGX_INVALID_FILE)) { /* Reader has put request body in temp file */ off_t count = 0; u_char buf[BUFSIZE]; size_t buf_len; ib_log_debug_tx(ctx->tx, "Reading request body in temp file."); while (buf_len = ngx_read_file(&rb->temp_file->file, buf, BUFSIZE, count), buf_len > 0) { ib_log_debug_tx(ctx->tx, "Feeding %zd bytes request data to ironbee.", buf_len); ib_state_notify_request_body_data(ctx->tx->ib, ctx->tx, (const char*)buf, buf_len); count += buf_len; } if ((int)buf_len == NGX_ERROR) { ib_log_error_tx(ctx->tx, "Failed to read request body in temp file."); } } for (link = rb->bufs; link != NULL; link = link->next) { size_t len = (link->buf->last - link->buf->pos); ib_log_debug_tx(ctx->tx, "Feeding %zd bytes request data to ironbee.", len); if (len > 0) { ib_state_notify_request_body_data(ctx->tx->ib, ctx->tx, (const char*)link->buf->pos, len); } } ctx->body_done = 1; ib_state_notify_request_finished(ctx->tx->ib, ctx->tx); /* If IronBee signaled an error, we can return it */ if (STATUS_IS_ERROR(ctx->status)) { rv = ctx->status; ctx->internal_errordoc = 1; ib_log_error_tx(ctx->tx, "IronBee set %d reading request body.", (int)rv); } cleanup_return rv; }
/* TODO: don't malloc name - on the stack */ chc_status_t con_create(unsigned int fd, struct syd_obj **obj) { STATUS_INIT(status); int snprintf_result = 0; char *name = NULL; struct con_context *context = NULL; struct syd_obj *local_obj = NULL; if (NULL == obj) { STATUS_LABEL(status, CHC_CON_INVALID); goto cleanup; } context = (struct con_context *)vzalloc(sizeof(*context)); if (NULL == context) { STATUS_LABEL(status, CHC_CON_VZALLOC); goto cleanup; } context->fd = fd; name = (char *)vzalloc(sizeof(COR_MAX_NAME)); if (NULL == name) { STATUS_LABEL(status, CHC_CON_VZALLOC); goto cleanup; } /* TODO: validate null termination */ snprintf_result = snprintf(name, COR_MAX_NAME, CON_PATTERN, fd); if (0 > snprintf_result) { STATUS_LABEL(status, CHC_CON_SNPRINTF); goto cleanup; } local_obj = (struct syd_obj *)vzalloc(sizeof(*local_obj)); if (NULL == local_obj) { STATUS_LABEL(status, CHC_CON_VZALLOC); goto cleanup; } STATUS_ASSIGN(status, syd_create(name, context, &g_con_ops)); if (STATUS_IS_ERROR(status)) { goto cleanup; } STATUS_LABEL(status, CHC_SUCCESS); cleanup: if (STATUS_IS_SUCCESS(status)) { *obj = local_obj; } else { if (NULL != local_obj) { vfree(local_obj); } if (NULL != context) { vfree(context); } if (NULL != name) { vfree(name); } } return status; }
/** * A body filter to intercept response body and feed it to Ironbee, * and to buffer the data if required by Ironbee configuration. * * @param[in] r The nginx request object. * @param[in] in The data to filter. * @return status propagated from next filter, or OK/Error */ static ngx_int_t ironbee_body_out(ngx_http_request_t *r, ngx_chain_t *in) { ngx_log_t *prev_log; ngxib_req_ctx *ctx; ngx_chain_t *link; ib_status_t rc; ib_num_t num; ngx_int_t rv = NGX_OK; ib_txdata_t itxdata; if (r->internal) return ngx_http_next_body_filter(r, in); prev_log = ngxib_log(r->connection->log); ctx = ngx_http_get_module_ctx(r, ngx_ironbee_module); assert((ctx != NULL) && (ctx->tx != NULL)); ib_log_debug_tx(ctx->tx, "ironbee_body_out"); if (in == NULL) { /* FIXME: could this happen in circumstances when we should * notify Ironbee of end-of-response ? */ ib_log_debug_tx(ctx->tx, "ironbee_body_out: input was null"); cleanup_return(prev_log) ngx_http_next_body_filter(r, in); } ctx = ngx_http_get_module_ctx(r, ngx_ironbee_module); if (ctx->output_filter_done) { ib_log_debug_tx(ctx->tx, "ironbee_body_out: already done"); cleanup_return(prev_log) ngx_http_next_body_filter(r, in); } if (!ctx->output_filter_init) { ctx->output_filter_init = 1; if (ctx->internal_errordoc) { /* If it's our own errordoc, pass it straight through */ /* Should we log anything here? The error will already * have been logged. */ ctx->output_buffering = IOBUF_NOBUF; ctx->response_buf = NULL; ib_log_debug_tx(ctx->tx, "ironbee_body_out: in internal errordoc"); } else { /* Determine whether we're configured to buffer */ rc = ib_context_get(ctx->tx->ctx, "buffer_res", ib_ftype_num_out(&num), NULL); ib_log_debug_tx(ctx->tx, "ironbee_body_out: buffer_res is %d", (int)num); if (rc != IB_OK) ib_log_error_tx(ctx->tx, "Can't determine output buffer configuration!"); if (num == 0) { ib_log_debug_tx(ctx->tx, "ironbee_body_out: NOBUF"); ctx->output_buffering = IOBUF_NOBUF; ctx->response_buf = NULL; } else { /* If we're buffering, initialise the buffer */ ib_log_debug_tx(ctx->tx, "ironbee_body_out: BUFFER"); ctx->output_buffering = IOBUF_BUFFER; } } } ngx_regex_malloc_init(r->pool); for (link = in; link != NULL; link = link->next) { /* Feed the data to ironbee */ itxdata.data = link->buf->pos; itxdata.dlen = link->buf->last - link->buf->pos; ib_log_debug_tx(ctx->tx, "ironbee_body_out: %d bytes", (int)itxdata.dlen); if (itxdata.dlen > 0) { ib_state_notify_response_body_data(ironbee, ctx->tx, &itxdata); } /* If Ironbee just signalled an error, switch to discard data mode, * and dump anything we already have buffered, */ if ( (STATUS_IS_ERROR(ctx->status)) && !ctx->internal_errordoc && (ctx->output_buffering != IOBUF_DISCARD) ) { ib_log_debug_tx(ctx->tx, "ironbee_body_out: error %d", ctx->status); free_chain(r->pool, ctx->response_buf); ctx->response_buf = NULL; ctx->output_buffering = IOBUF_DISCARD; } else if (ctx->output_buffering == IOBUF_BUFFER) { /* Copy any data to our buffer */ if (ctx->response_buf == NULL) { ctx->response_buf = ngx_pcalloc(r->pool, sizeof(ngx_chain_t)); ctx->response_ptr = ctx->response_buf; } else { ctx->response_ptr->next = ngx_pcalloc(r->pool, sizeof(ngx_chain_t)); ctx->response_ptr = ctx->response_ptr->next; } /* Not sure if any data types need setaside, but let's be safe */ #if NO_COPY_REQUIRED ctx->response_ptr->buf = link->buf; #else if (itxdata.dlen > 0) { ctx->response_ptr->buf = ngx_create_temp_buf(r->pool, itxdata.dlen); memcpy(ctx->response_ptr->buf->pos, link->buf->pos, itxdata.dlen); ctx->response_ptr->buf->last += itxdata.dlen; } else { ctx->response_ptr->buf = ngx_palloc(r->pool, sizeof(ngx_buf_t)); memcpy(ctx->response_ptr->buf, link->buf, sizeof(ngx_buf_t)); } #endif } if (link->buf->last_buf) { ib_log_debug_tx(ctx->tx, "ironbee_body_out: last_buf"); ctx->output_filter_done = 1; } } if (ctx->output_buffering == IOBUF_NOBUF) { /* Normal operation - pass it down the chain */ ib_log_debug_tx(ctx->tx, "ironbee_body_out: passing on"); ctx->start_response = 1; rv = ngx_http_next_body_filter(r, in); } else if (ctx->output_buffering == IOBUF_BUFFER) { ib_log_debug_tx(ctx->tx, "ironbee_body_out: buffering"); if (ctx->output_filter_done) { /* We can pass on the buffered data all at once */ ib_log_debug_tx(ctx->tx, "ironbee_body_out: passing buffer"); ctx->start_response = 1; rv = ngx_http_next_body_filter(r, ctx->response_buf); } } else if (ctx->output_buffering == IOBUF_DISCARD) { ib_log_debug_tx(ctx->tx, "ironbee_body_out: discarding"); if (ctx->output_filter_done) { /* Pass a last bucket with no data */ //ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, // "ironbee_body_out: passing NULL last-buffer"); //ctx->response_buf = ngx_pcalloc(r->pool, sizeof(ngx_chain_t)); //ctx->response_buf->buf = ngx_calloc_buf(r->pool); //ctx->response_buf->buf->last_buf = ctx->response_buf->buf->last_in_chain = 1; //rv = ngx_http_next_body_filter(r, ctx->response_buf); /* FIXME: Is setting rv enough to serve error page */ rv = ctx->status; } } if (ctx->output_filter_done) { ib_log_debug_tx(ctx->tx, "ironbee_body_out: notify_postprocess"); rc = ib_state_notify_postprocess(ironbee, ctx->tx); if ((rv == NGX_OK) && (rc != IB_OK)) { rv = NGX_HTTP_INTERNAL_SERVER_ERROR; } rc = ib_state_notify_logging(ironbee, ctx->tx); if ((rv == NGX_OK) && (rc != IB_OK)) { rv = NGX_HTTP_INTERNAL_SERVER_ERROR; } } cleanup_return(prev_log) rv; }
/** * nginx post-read-request handler to feed request line and headers to Ironbee. * * @param[in] r The nginx request object. * @return Declined (ignoreme) or error status. */ static ngx_int_t ironbee_post_read_request(ngx_http_request_t *r) { ngx_log_t *prev_log; ngxib_req_ctx *ctx; ib_conn_t *iconn; ib_parsed_req_line_t *rline; ib_parsed_header_wrapper_t *ibhdrs; ib_status_t rc; ngx_list_part_t *part; ngx_table_elt_t *hdr; unsigned int i; /* Don't process internal requests */ if (r->internal) return NGX_DECLINED; prev_log = ngxib_log(r->connection->log); ngx_regex_malloc_init(r->pool); ctx = ngx_pcalloc(r->pool, sizeof(ngxib_req_ctx)); ctx->r = r; ngx_http_set_ctx(r, ctx, ngx_ironbee_module); iconn = ngxib_conn_get(ctx, ironbee); ib_tx_create(&ctx->tx, iconn, ctx); /* Notify Ironbee of request line and headers */ rc = ib_parsed_req_line_create(ctx->tx, &rline, (const char*)r->request_line.data, r->request_line.len, (const char*)r->method_name.data, r->method_name.len, (const char*)r->unparsed_uri.data, r->unparsed_uri.len, (const char*)r->http_protocol.data, r->http_protocol.len); if (rc != IB_OK) cleanup_return(prev_log) NGX_ERROR; ib_state_notify_request_started(ironbee, ctx->tx, rline); rc = ib_parsed_name_value_pair_list_wrapper_create(&ibhdrs, ctx->tx); if (rc != IB_OK) cleanup_return(prev_log) NGX_ERROR; for (part = &r->headers_in.headers.part; part != NULL; part = part->next) { hdr = part->elts; for (i = 0; i < part->nelts; ++i) { ib_parsed_name_value_pair_list_add(ibhdrs, (const char*)hdr->key.data, hdr->key.len, (const char*)hdr->value.data, hdr->value.len); ++hdr; } } rc = ib_state_notify_request_header_data(ironbee, ctx->tx, ibhdrs); if (rc != IB_OK) cleanup_return(prev_log) NGX_ERROR; rc = ib_state_notify_request_header_finished(ironbee, ctx->tx); if (rc != IB_OK) cleanup_return(prev_log) NGX_ERROR; if (!ngxib_has_request_body(r, ctx)) { rc = ib_state_notify_request_finished(ironbee, ctx->tx); if (rc != IB_OK) cleanup_return(prev_log) NGX_ERROR; } ctx->hdrs_in = 1; if (STATUS_IS_ERROR(ctx->status)) { ctx->internal_errordoc = 1; cleanup_return(prev_log) ctx->status; } cleanup_return(prev_log) NGX_DECLINED; }
/** * nginx handler to feed request body (if any) to Ironbee * * @param[in] r the nginx request object * @return NGX_DECLINED for normal operation * @return NGX_DONE if body is not yet available (processing will restart * on new data) * @return Error status if set by Ironbee on sight of request data. */ ngx_int_t ngxib_handler(ngx_http_request_t *r) { ngx_log_t *prev_log; ib_txdata_t itxdata; ngx_chain_t *link; ngxib_req_ctx *ctx; ngx_int_t rv = NGX_DECLINED; ngx_http_request_body_t *rb; /* Don't process internal requests */ if (r->internal) return rv; ctx = ngx_http_get_module_ctx(r, ngx_ironbee_module); if (ctx->body_done) return rv; if (!has_request_body(r, ctx)) return rv; prev_log = ngxib_log(r->connection->log); ngx_regex_malloc_init(r->pool); /* We can now read the body. * This may come asynchronously in many chunks, so we need * to check for AGAIN and return DONE if waiting. * We pass it a handler to go round again while waiting. * * TODO: figure out how to pass data to ironbee asynchronously */ rv = ngx_http_read_client_request_body(r, ngxib_post_handler); if (rv == NGX_AGAIN) { ctx->body_wait = 1; cleanup_return(prev_log) NGX_DONE; } /* We now have the request body. Feed it to ironbee */ rb = r->request_body; if (!rb) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Error reading request body"); cleanup_return(prev_log) NGX_HTTP_INTERNAL_SERVER_ERROR; } if (!rb->bufs) { /* I think this shouldn't happen */ /* But rethink if this turns up in logs when all is fine */ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Probable error reading request body"); } if (rb->temp_file && (rb->temp_file->file.fd != NGX_INVALID_FILE)) { /* Reader has put request body in temp file */ off_t count = 0; u_char buf[BUFSIZE]; ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Reading request body in temp file"); while (itxdata.dlen = ngx_read_file(&rb->temp_file->file, buf, BUFSIZE, count), (int)itxdata.dlen > 0) { itxdata.data = buf; ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Feeding %d bytes request data to ironbee", itxdata.dlen); ib_state_notify_request_body_data(ngxib_engine(), ctx->tx, &itxdata); count += itxdata.dlen; } if ((int)itxdata.dlen == NGX_ERROR) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Error reading request body in temp file"); } } for (link = rb->bufs; link != NULL; link = link->next) { itxdata.data = link->buf->pos; itxdata.dlen = (link->buf->last - link->buf->pos); ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Feeding %d bytes request data to ironbee", itxdata.dlen); if (itxdata.dlen > 0) { ib_state_notify_request_body_data(ngxib_engine(), ctx->tx, &itxdata); } } ctx->body_done = 1; ib_state_notify_request_finished(ngxib_engine(), ctx->tx); /* If Ironbee signalled an error, we can return it */ if (STATUS_IS_ERROR(ctx->status)) { rv = ctx->status; ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Ironbee set %d reading request body", rv); } cleanup_return(prev_log) rv; }