Recent

Author Topic: Apache authentication with mod_dbd, unixodbc and firebird.  (Read 1272 times)

Dimitrios Chr. Ioannidis

  • Full Member
  • ***
  • Posts: 133
    • Nephelae
Apache authentication with mod_dbd, unixodbc and firebird.
« on: January 15, 2016, 12:15:29 pm »
Hi all,

   the following UDF can be used to store user's passwords in a firebirdsql's table to provide apache's basic authentication and mod_dbd with unixodbc ( or odbc in win ) SHA1 authentication.

  Apache SHA1 Password format : "{SHA}" + Base64-encoded SHA-1 digest of the password.

Code: Pascal  [Select][+][-]
  1. library ApacheAuthUDF;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes,
  7.   strings,
  8.   sha1,
  9.   base64;
  10.  
  11.   procedure basic_auth_sha1_base64(StrToHash, HashedStr: PChar); cdecl; export;
  12.   var
  13.     Outstream: TStringStream;
  14.     Encoder: TBase64EncodingStream;
  15.     SHA1Digest: TSHA1Digest;
  16.   begin
  17.     Outstream := TStringStream.Create('');
  18.     try
  19.       Encoder := TBase64EncodingStream.Create(outstream);
  20.       try
  21.         SHA1Digest := SHA1String(StrPas(StrToHash));
  22.         Encoder.Write(SHA1Digest, Length(SHA1Digest));
  23.       finally
  24.         Encoder.Free;
  25.       end;
  26.       StrPCopy(HashedStr, Outstream.DataString);
  27.     finally
  28.       Outstream.Free;
  29.     end;
  30.   end;
  31.  
  32. exports
  33.   basic_auth_sha1_base64 Name 'basic_auth_sha1_base64';
  34.  
  35. begin
  36. end.
  37.  

SQL Declaration :

Code: Pascal  [Select][+][-]
  1. DECLARE EXTERNAL FUNCTION BASIC_AUTH_SHA1_BASE64
  2. cstring(256), cstring(256)
  3. RETURNS parameter 2
  4. ENTRY_POINT 'basic_auth_sha1_base64'
  5. MODULE_NAME 'ApacheAuthUDF';
  6.  

It can be used like this :

Code: XML  [Select][+][-]
  1. select BASIC_AUTH_SHA1_BASE64('12345') from rdb$database
  2.  

or

Code: XML  [Select][+][-]
  1. select BASIC_AUTH_SHA1_BASE64(PlainTextPassword) from users
  2.  

Hope it helps anyone who need to use apache's basic auth with firebirdsql.

PS:
 
  In the Virtual Host conf of the apache, you configure mod_dbd like this :

Code: XML  [Select][+][-]
  1. DBDriver odbc
  2. DBDParams "DATASOURCE=FBDatabase"
  3. #DBDKeep     10
  4. #DBDMax      10
  5. #DBDMin      10
  6.  
  7. <Directory />
  8. AuthType basic
  9. AuthName "Secured Realm"
  10. AuthBasicProvider dbd
  11. AuthDBDUserPWQuery "select '{SHA}' || password from users where username = %s"
  12. Require valid-user
  13. </Directory>
  14.  
« Last Edit: January 15, 2016, 12:21:56 pm by Dimitrios Chr. Ioannidis »
Dimitrios Chr. Ioannidis
http://www.nephelae.eu

 

TinyPortal © 2005-2018