Advertisement
Guest User

Untitled

a guest
Apr 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. location /protected/data/ {
  2. internal;
  3. alias /path/to/data/files/;
  4. }
  5.  
  6. location /download.php$ {
  7. fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
  8. fastcgi_param SCRIPT_FILENAME /scripts/download.php;
  9. fastcgi_param PHP_AUTH_USER $remote_user;
  10. fastcgi_param PHP_AUTH_PW $http_authorization;
  11. include fastcgi_params;
  12. }
  13.  
  14. function authenticate() {
  15. // I'm watching you.
  16. error_log("authreq: " . $_SERVER['REMOTE_ADDR']);
  17. // mark that we're seeing the login box.
  18. $_SESSION['AUTH'] = 1;
  19. // browser shows login box
  20. Header("WWW-Authenticate: Basic realm=LDAP credentials.");
  21. Header("HTTP/1.0 401 Unauthorized");
  22. die('Unauthorized.');
  23. }
  24.  
  25. function forbidden() {
  26. error_log("forbidden: " . $_SERVER['REMOTE_ADDR'] . ', user: ' . $_SERVER['PHP_AUTH_USER']);
  27. // avoid brute force attacks
  28. sleep(rand(0, 3));
  29. // re-display login form
  30. session_destroy();
  31. // don't give too much info (e.g. user does not exist / password is wrong)
  32. Header("HTTP/1.0 403 Forbidden");
  33. // yes I did put the same message.
  34. die('Unauthorized.');
  35. }
  36.  
  37. function ldap_auth() {
  38. $ldap_server = 'ldap://ldap.example.com/';
  39. $ldap_domain = 'dc=example,dc=com';
  40. $ldap_userbase = 'ou=Users,' . $ldap_domain;
  41. $ldap_user = 'uid=' . $_SERVER['PHP_AUTH_USER'] . ',' . $ldap_userbase;
  42. $ldap_pass = $_SERVER['PHP_AUTH_PW'];
  43.  
  44. // connect to ldap server
  45. $ldapconn = ldap_connect($ldap_server)
  46. or die("Could not connect to LDAP server.");
  47. ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3) ;
  48. if ($ldapconn) {
  49. // try to bind/authenticate against ldap
  50. $ldapbind = @ldap_bind($ldapconn, $ldap_user, $ldap_pass) || forbidden();
  51. // "LDAP bind successful...";
  52. error_log("success: " . $_SERVER['REMOTE_ADDR'] . ', user: ' . $_SERVER['PHP_AUTH_USER']);
  53. }
  54. ldap_close($ldapconn);
  55. }
  56.  
  57. if (@$_SESSION['AUTH'] != 1) {
  58. authenticate();
  59. }
  60.  
  61. if (empty($_SERVER['PHP_AUTH_USER'])) {
  62. authenticate();
  63. }
  64.  
  65. // check credentials on each access
  66. ldap_auth();
  67.  
  68. // Get requested file name
  69. // you can use the query string or a parameter
  70. // or the full request uri if you like.
  71. $path = $_GET["path"];
  72.  
  73. error_log("serving: " . $_SERVER['REMOTE_ADDR'] . ', user: ' . $_SERVER['PHP_AUTH_USER'] . ', path: ' . $path);
  74.  
  75. header("Content-Type: ", true);
  76. header("X-Accel-Redirect: /protected" . $path);
  77.  
  78. location /protected/data/ {
  79. internal;
  80. autoindex on;
  81. alias /path/to/data/files/;
  82. }
  83.  
  84. location /data/ {
  85. fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
  86. fastcgi_param SCRIPT_FILENAME /scripts/auth.php;
  87. fastcgi_param PHP_AUTH_USER $remote_user;
  88. fastcgi_param PHP_AUTH_PW $http_authorization;
  89. include fastcgi_params;
  90. }
  91.  
  92. // Get requested file name
  93. $path = $_SERVER["REQUEST_URI"];
  94. error_log("serving: " . $_SERVER['REMOTE_ADDR'] . ', user: ' . $_SERVER['PHP_AUTH_USER'] . ', path: ' . $path);
  95. header("Content-Type: ", true);
  96. header("X-Accel-Redirect: /protected" . $path);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement