diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3eb5129 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.16.5) + +project(mod_xsendfile) + +link_directories(${APACHE2_ROOT}/bin) +link_directories(${APACHE2_ROOT}/lib) + +include_directories(${APACHE2_ROOT}/include) + +add_library(mod_xsendfile MODULE mod_xsendfile.c) + +if (_DEBUG) +add_definitions(-D_DEBUG=${_DEBUG}) +endif() + +target_link_libraries(mod_xsendfile libhttpd libaprutil-1 libapr-1) +set_target_properties(mod_xsendfile PROPERTIES PREFIX "" SUFFIX ".so") + +if (NOT INSTALL_DIRECTORY) + set(INSTALL_DIRECTORY ${APACHE2_ROOT}/modules) +endif () + +install(TARGETS mod_xsendfile DESTINATION ${INSTALL_DIRECTORY}) diff --git a/bin/Apache24/Win32/mod_xsendfile.so b/bin/Apache24/Win32/mod_xsendfile.so index 3ac95f3..8bfbe7b 100644 Binary files a/bin/Apache24/Win32/mod_xsendfile.so and b/bin/Apache24/Win32/mod_xsendfile.so differ diff --git a/bin/Apache24/Win64/mod_xsendfile.so b/bin/Apache24/Win64/mod_xsendfile.so index 3dc8777..742b589 100644 Binary files a/bin/Apache24/Win64/mod_xsendfile.so and b/bin/Apache24/Win64/mod_xsendfile.so differ diff --git a/docs/Readme.html b/docs/Readme.html deleted file mode 100644 index 26c2eff..0000000 --- a/docs/Readme.html +++ /dev/null @@ -1,427 +0,0 @@ - - - - - mod_xsendfile for Apache2 - - - - -
-
-

mod_xsendfile for Apache2

-
- -
-

Overview

- -

mod_xsendfile is a small Apache2 module that - processes X-SENDFILE headers output handlers might have registered.

- -

If it encounters the presence of such header it will discard all output and send the file specified by that header instead using Apache internals including all optimizations like caching-headers and sendfile or mmap if configured.

- -

It is useful for processing script-output of e.g. php, perl, python or - any *cgi.

- -

Useful?

- -

Yep, it is useful.

- - - -

Benefits

- -
- -
-

Installation

-
    -
  1. Grab the source.
  2. -
  3. Compile and install -
      -
    • In general:
      - apxs -cia mod_xsendfile.c
    • -
    • Debian/Ubuntu uses apxs2:
      - apxs2 -cia mod_xsendfile.c
    • -
    • Mac users might want to build fat binaries:
      - apxs -cia -Wc,"-arch i386 -arch x86_64" -Wl,"-arch i386 -arch x86_64" mod_xsendfile.c
    • -
    -
  4. -
  5. Restart apache
  6. -
  7. That's all.
  8. -
-
- -
-

Configuration

- -

Headers

- - -

XSendFile

- - - - - - - - - - - - - - - - - - - - -
DescriptionEnables or disables header processing
SyntaxXSendFile on|off
DefaultXSendFile off
Contextserver config, virtual host, directory, .htaccess
- -

Setting XSendFile on will enable processing.

- -

The file specified in X-SENDFILE header will be sent instead of the handler output.

- -

The value (file name) given by the header is assmumed to be url-encoded, i.e. unescaping/url-decoding will be performed. See XSendFileUnescape.
- If you happen to store files using already url-encoded file names, you must "double" encode the names... %20 -> %2520

- -

If the response lacks the X-SENDFILE header the module will not perform any processing.

- -

XSendFileIgnoreEtag

- - - - - - - - - - - - - - - - - - - - -
DescriptionIgnore script provided Etag headers
SyntaxXSendFileIgnoreEtag on|off
DefaultXSendFileIgnoreEtag off
Contextserver config, virtual host, directory, .htaccess
- -

Setting XSendFileIgnoreEtag on will ignore all ETag headers the original output handler may have set.
- This is helpful for applications that will generate such headers even for empty content.

- -

XSendFileIgnoreLastModified

- - - - - - - - - - - - - - - - - - - - -
DescriptionIgnore script provided LastModified headers
SyntaxXSendFileIgnoreLastModified on|off
DefaultXSendFileIgnoreLastModified off
Contextserver config, virtual host, directory, .htaccess
- -

Setting XSendFileIgnoreLastModified on will ignore all Last-Modified headers the original output handler may have set.
- This is helpful for applications that will generate such headers even for empty content.

- -

XSendFileUnescape

- - - - - - - - - - - - - - - - - - - - -
DescriptionUnescape/url-decode the value of the header
SyntaxXSendFileUnescape on|off
DefaultXSendFileUnescape on
Contextserver config, virtual host, directory, .htaccess
- -

Setting XSendFileUnescape off will restore the pre-1.0 behavior of using the raw header value, instead of trying to unescape/url-decode first.
- Headers may only contain a certain ASCII subset, as dictated by the corresponding RFCs/protocol. Hence you should escape/url-encode (and have XSendFile unescape/url-decode) the header value. Failing to keep within the bounds of that ASCII subset might cause errors, depending on your application framework.

-

Hence this setting is meant only for backwards-compatibility with legacy applications expecting the old behavior; new applications should url-encode the value correctly and leave XSendFileUnescape on. Of course, if your paths are always ASCII, then (usually) no special encoding is required.

- -

XSendFilePath

- - - - - - - - - - - - - - - - - - - - -
DescriptionWhite-list more paths
SyntaxXSendFilePath <absolute path> [AllowFileDelete]
DefaultNone
Contextserver config, virtual host, directory
- -

XSendFilePath allow you to add additional paths to some kind of white list. All files within these paths are allowed to get served through mod_xsendfile.

-

Provide an absolute path as Parameter to this directive.

-

If the optional AllowFileDelete flag is specified, then files under this path can be served using the X-SENDFILE-TEMPORARY header, and will then be deleted once the file is delievered. - Hence you should only set the AllowFileDelete flag for paths that do not hold any files that shouldn't be deleted!

-

You may provide more than one path.

-

Remarks - Relative paths

-

The current working directory (if it can be determined) will be always checked first.

-

If you provide relative paths via the X-SendFile header, then all whitelist items will be checked until a seamingly valid combination is found, i.e. the result is within the bounds of the whitelist item; it isn't checked at this point if the path in question actually exists.
- Considering you whitelisted /tmp/pool and /tmp/pool2 and your script working directory is /var/www.

-

X-SendFile: file

-
    -
  1. /var/www/file - Within bounds of /var/www, OK
  2. -
-

X-SendFile: ../pool2/file

-
    -
  1. /var/www/../pool2/file = /var/pool2/file - Not within bounds of /var/www
  2. -
  3. /tmp/pool/../pool2/file = /tmp/pool2/file - Not within bounds of /tmp/pool
  4. -
  5. /tmp/pool2/../pool2/file = /tmp/pool2/file - Within bounds of /tmp/pool2, OK
  6. -
-

You still can only access paths that are whitelisted. However you have might expect a different behavior here, hence the documentation.

-

Please note: It is recommended to always use absolute paths.

- -

Remarks - Inheritance

-

The white list "inherits" entries from higher level configuration.
-

XSendFilePath /tmp
-<VirtualHost *>
-  ServerName someserver
-  XSendFilePath /home/userxyz
-</VirtualHost>
-<VirtualHost *>
-  ServerName anotherserver
-  XSendFilePath /var/www/somesite/
-  <Directory /var/www/somesite/fastcgis>
-    XSendFilePath /var/www/shared
-  </Directory>
-</VirtualHost>
-

Above example will give: -

-

*) Scripts, in this context, mean the actual script-starters. E.g. PHP as a handler will use the .php itself, while in CGI mode refers to the starter.

-

Windows users must include the drive letter to those paths as well. Tests show that it has to be in upper-case.

- -

Example

- -

.htaccess

- -
<Files out.php>
-XSendFile on
-</Files>
- -

out.php

- -

<?php
- -
...
- - if (
$user->isLoggedIn())
- - {
- -     
header("X-Sendfile: $path_to_somefile");
- -     
header("Content-Type: application/octet-stream");
- -     
header("Content-Disposition: attachment; filename=\"$somefile\"");
- -     exit;
- - }
- -
?>
- -
<h1>Permission denied</h1>
- - <p>Login first!</p>

- -

Limitations / Issues / Security considerations

- - -
- -
-

Credits

- -

The idea comes from lighttpd - - A fast web server - with minimal memory footprint.

- -

The module itself was inspired by many other Apache2 modules - such as mod_rewrite, mod_headers and obviously core.c.

- -

License

- -

Copyright 2006-2012 by Nils Maier

- -

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.

- -

You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0

- -

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

- -

See the License for the specific language governing permissions and - limitations under the License.

- -

Changes

-

Version 1.0

- -

Version 0.12

- -

Version 0.11.1

- -

Version 0.11

- -

Version 0.10

- -

Version 0.9

- -

Version 0.8

- -
-
- - diff --git a/docs/Readme.md b/docs/Readme.md new file mode 100644 index 0000000..fb21d5d --- /dev/null +++ b/docs/Readme.md @@ -0,0 +1,453 @@ +
+ +
+ +# mod_xsendfile for Apache2 + +
+ +
+ +## Overview + +mod_xsendfile is a small Apache2 module that processes X-SENDFILE headers output handlers might have registered. + +If it encounters the presence of such header it will discard all output and send the file specified by that header instead using Apache internals including all optimizations like caching-headers and sendfile or mmap if configured. + +It is useful for processing script-output of e.g. php, perl, python or any *cgi. + +## Useful? + +Yep, it is useful. + +* Some applications require checking for special privileges. +* Other have to lookup values first (e.g.. from a DB) in order to correctly process a download request. +* Or store values (download-counters come into mind). +* etc. + +### Benefits + +* Uses apache internals +* Optimal delivery through sendfile and mmap (if available). +* Sets correct cache headers such as Etag and If-Modified-Since as if the file was statically served. +* Processes cache headers such as If-None-Match or If-Modified-Since. +* Support for ranges. + +
+ +
+ +## Installation + +1. Grab the source. +2. Compile and install + * In general: + `apxs -cia mod_xsendfile.c` + * Debian/Ubuntu uses apxs2: + `apxs2 -cia mod_xsendfile.c` + * Mac users might want to build fat binaries: + `apxs -cia -Wc,"-arch i386 -arch x86_64" -Wl,"-arch i386 -arch x86_64" mod_xsendfile.c` +3. Restart apache +4. That's all. + +
+ +
+ +## Configuration + +### Headers + +* `X-SENDFILE` - Send the file referenced by this headers instead of the current response body +* `X-SENDFILE-TEMPORARY` - Like `X-SENDFILE`, but the file will be deleted afterwards. The file must originate from a path that has the `AllowFileDelete` flag set. + +### XSendFile + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DescriptionEnables or disables header processing
SyntaxXSendFile on|off
DefaultXSendFile off
Contextserver config, virtual host, directory, .htaccess
+ +Setting `XSendFile on` will enable processing. + +The file specified in `X-SENDFILE` header will be sent instead of the handler output. + +The value (file name) given by the header is assmumed to be url-encoded, i.e. unescaping/url-decoding will be performed. See [XSendFileUnescape](#XSendFileUnescape). +If you happen to store files using already url-encoded file names, you must "double" encode the names... `%20 -> %2520` + +If the response lacks the `X-SENDFILE` header the module will not perform any processing. + +### XSendFileIgnoreEtag + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DescriptionIgnore script provided Etag headers
SyntaxXSendFileIgnoreEtag on|off
DefaultXSendFileIgnoreEtag off
Contextserver config, virtual host, directory, .htaccess
+ +Setting `XSendFileIgnoreEtag on` will ignore all ETag headers the original output handler may have set. +This is helpful for applications that will generate such headers even for empty content. + +### XSendFileIgnoreLastModified + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DescriptionIgnore script provided LastModified headers
SyntaxXSendFileIgnoreLastModified on|off
DefaultXSendFileIgnoreLastModified off
Contextserver config, virtual host, directory, .htaccess
+ +Setting `XSendFileIgnoreLastModified on` will ignore all Last-Modified headers the original output handler may have set. +This is helpful for applications that will generate such headers even for empty content. + +### XSendFileUnescape + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DescriptionUnescape/url-decode the value of the header
SyntaxXSendFileUnescape on|off
DefaultXSendFileUnescape on
Contextserver config, virtual host, directory, .htaccess
+ +Setting `XSendFileUnescape off` will restore the pre-1.0 behavior of using the raw header value, instead of trying to unescape/url-decode first. +Headers may only contain a certain ASCII subset, as dictated by the corresponding RFCs/protocol. Hence you should escape/url-encode (and have XSendFile unescape/url-decode) the header value. Failing to keep within the bounds of that ASCII subset might cause errors, depending on your application framework. + +Hence this setting is meant only for backwards-compatibility with legacy applications expecting the old behavior; new applications should url-encode the value correctly and leave `XSendFileUnescape on`. Of course, if your paths are always ASCII, then (usually) no special encoding is required. + +### XSendFilePath + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DescriptionWhite-list more paths
SyntaxXSendFilePath `` [`AllowFileDelete`]
DefaultNone
Contextserver config, virtual host, directory
+ +XSendFilePath allow you to add additional paths to some kind of white list. All files within these paths are allowed to get served through mod_xsendfile. + +Provide an absolute path as Parameter to this directive. + +If the optional `AllowFileDelete` flag is specified, then files under this path can be served using the `X-SENDFILE-TEMPORARY` header, and will then be deleted once the file is delievered. Hence you should only set the `AllowFileDelete` flag for paths that do not hold any files that shouldn't be deleted! + +You may provide more than one path. + +#### Remarks - Relative paths + +The current working directory (if it can be determined) will be always checked first. + +If you provide relative paths via the X-SendFile header, then all whitelist items will be checked until a seamingly valid combination is found, i.e. the result is within the bounds of the whitelist item; it isn't checked at this point if the path in question actually exists. +Considering you whitelisted `/tmp/pool` and `/tmp/pool2` and your script working directory is `/var/www`. + +`X-SendFile: file` + +1. `/var/www/file` - Within bounds of `/var/www`, OK + +`X-SendFile: ../pool2/file` + +1. `/var/www/../pool2/file = /var/pool2/file` - Not within bounds of `/var/www` +2. `/tmp/pool/../pool2/file = /tmp/pool2/file` - Not within bounds of `/tmp/pool` +3. `/tmp/pool2/../pool2/file = /tmp/pool2/file` - Within bounds of `/tmp/pool2`, OK + +You still can only access paths that are whitelisted. However you have might expect a different behavior here, hence the documentation. + +**Please note:** It is recommended to always use absolute paths. + +#### Remarks - Inheritance + +The white list "inherits" entries from higher level configuration. + +
XSendFilePath /tmp
+
+  ServerName someserver
+  XSendFilePath /home/userxyz
+
+
+  ServerName anotherserver
+  XSendFilePath /var/www/somesite/
+  
+    XSendFilePath /var/www/shared
+  
+
+ +Above example will give: + +* * + * `/tmp` +* someserver + * `/tmp` + * `/home/userxyz` +* another + * `/tmp` + * `/var/www/somesite` + * `/var/www/shared` (for scripts* located in /var/www/somesite/fastcgis) + +*) Scripts, in this context, mean the actual script-starters. E.g. PHP as a handler will use the .php itself, while in CGI mode refers to the starter. + +_Windows_ users must include the drive letter to those paths as well. Tests show that it has to be in upper-case. + +### Example + +`.htaccess` + +

+XSendFile on
+
+ +`out.php` + +`... +if ($user->isLoggedIn()) +{ +    header("X-Sendfile: $path_to_somefile"); +    header("Content-Type: application/octet-stream"); +    header("Content-Disposition: attachment; filename=\"$somefile\""); +    exit; +} +?> +

Permission denied

+

Login first!

` + +## Limitations / Issues / Security considerations + +* The `Content-Encoding` header - if present - will be dropped, as the module cannot know if it was set by intention of the programmer or the handler. E.g. php with output compression enabled will set this header, but the replacement file send via mod_xsendfile is most likely not compressed. +* The header (X-SENDFILE) is not case-sensitive. +* **X-Sendfile will also happily send files that are otherwise protected (e.g. Deny from all).** + +
+ +
+ +## Credits + +The idea comes from [lighttpd](http://www.lighttpd.net/) - A fast web server with minimal memory footprint. + +The module itself was inspired by many other Apache2 modules such as mod_rewrite, mod_headers and obviously core.c. + +## License + +**Copyright 2006-2012 by Nils Maier** + +Licensed under the _Apache License, Version 2.0_ (the "License"); you may not use this file except in compliance with the License. + +You may obtain a copy of the License at +[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0) + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +See the License for the specific language governing permissions and limitations under the License. + +## Changes + +### Version 1.0 + +* Unescape/url-decode header value to support non-ascii file names +* `XSendFileUnescape` setting, to support legacy applications +* `X-SENDFILE-TEMPORARY` header and corresponding `AllowFileDelete` flag +* Fix: Actually look into the backend-provided headers for Last-Modified + +### Version 0.12 + +* Now incorrect headers will be dropped early + +### Version 0.11.1 + +* Fixed some documentation bugs +* Built win32 binaries against latest httpd using MSVC9 +* Updated MSVC Project files + +### Version 0.11 + +* Fixed large file support + +### Version 0.10 + +* Won't override Etag/Last-Modified if already set. +* New Configuration directive: XSendFileIgnoreEtag +* New Configuration directive: XSendFileIgnoreLastModified +* New Configuration directive: XSendFilePath +* Removed Configuration directive: XSendFileAllowAbove + Use XSendFilePath instead. +* Improved header handling for FastCGI/CGI output (removing duplicate headers). + +### Version 0.9 + +* New configuration directive: XSendFileAllowAbove +* Initial FastCGI/CGI support +* Filter only added when needed + +### Version 0.8 + +* This is the initial public release. + +
+ +
diff --git a/mod_xsendfile.c b/mod_xsendfile.c index 863ed26..23f1a11 100644 --- a/mod_xsendfile.c +++ b/mod_xsendfile.c @@ -72,6 +72,7 @@ typedef struct xsendfile_conf_t { xsendfile_conf_active_t ignoreETag; xsendfile_conf_active_t ignoreLM; xsendfile_conf_active_t unescape; + const char *rootpath; apr_array_header_t *paths; apr_array_header_t *temporaryPaths; } xsendfile_conf_t; @@ -92,6 +93,8 @@ static xsendfile_conf_t *xsendfile_config_create(apr_pool_t *p) { conf->enabled = XSENDFILE_UNSET; + // optional settings + conf->rootpath = NULL; conf->paths = apr_array_make(p, 1, sizeof(xsendfile_path_t)); return conf; @@ -102,6 +105,7 @@ static void *xsendfile_config_server_create(apr_pool_t *p, server_rec *s) { } #define XSENDFILE_CFLAG(x) conf->x = overrides->x != XSENDFILE_UNSET ? overrides->x : base->x +#define XSENDFILE_CPATH(x) conf->x = overrides->x != NULL ? overrides->x : base->x static void *xsendfile_config_merge(apr_pool_t *p, void *basev, void *overridesv) { xsendfile_conf_t *base = (xsendfile_conf_t *)basev; @@ -115,6 +119,8 @@ static void *xsendfile_config_merge(apr_pool_t *p, void *basev, void *overridesv XSENDFILE_CFLAG(ignoreLM); XSENDFILE_CFLAG(unescape); + // optional settings + XSENDFILE_CPATH(rootpath); conf->paths = apr_array_append(p, overrides->paths, base->paths); return (void*)conf; @@ -175,6 +181,17 @@ static const char *xsendfile_cmd_path(cmd_parms *cmd, void *pdc, return NULL; } +static const char *xsendfile_root_path(cmd_parms *cmd, void *pdc, + const char *path, + const char *allowFileDelete) { + xsendfile_conf_t *conf = (xsendfile_conf_t*)ap_get_module_config( + cmd->server->module_config, + &xsendfile_module + ); + conf->rootpath = apr_pstrdup(cmd->pool, path); + return NULL; +} + /* little helper function to get the original request path code borrowed from request.c and util_script.c @@ -231,6 +248,13 @@ static const char *ap_xsendfile_get_orginal_path(request_rec *rec) { return rv; } +/* + Root path based conf or environ. +*/ +static const char *ap_xsendfile_get_root_path(xsendfile_conf_t *conf, request_rec *rec) { + return conf->rootpath != NULL ? conf->rootpath : ap_xsendfile_get_orginal_path(rec); +} + /* little helper function to build the file path if available */ @@ -246,7 +270,7 @@ static apr_status_t ap_xsendfile_get_filepath(request_rec *r, patharr = conf->paths; if (!shouldDeleteFile) { - const char *root = ap_xsendfile_get_orginal_path(r); + const char *root = ap_xsendfile_get_root_path(conf, r); if (root) { xsendfile_path_t *newpath; @@ -283,7 +307,15 @@ static apr_status_t ap_xsendfile_get_filepath(request_rec *r, APR_FILEPATH_TRUENAME | APR_FILEPATH_NOTABOVEROOT, r->pool )) == OK) { - break; + // The path is only valid if it exists. + // If it doesn't continue until you find a valid path on the white-list. + apr_finfo_t info; + rv = apr_stat(&info, *path, APR_FINFO_CSIZE, r->pool); + if (rv == OK) { + break; + } else { + continue; + } } } if (rv != OK) { @@ -683,6 +715,13 @@ static const command_rec xsendfile_command_table[] = { RSRC_CONF|ACCESS_CONF, "Allow to serve files from that Path. Must be absolute" ), + AP_INIT_TAKE12( + "XSendFileRootPath", + xsendfile_root_path, + NULL, + RSRC_CONF|ACCESS_CONF, + "Allow to serve files relative that root." + ), { NULL } }; static void xsendfile_register_hooks(apr_pool_t *p) { @@ -710,4 +749,4 @@ module AP_MODULE_DECLARE_DATA xsendfile_module = { xsendfile_register_hooks }; -/* vim: set ts=2 sw=2 et : */ +/* vim: set ts=2 sw=2 et : */ \ No newline at end of file