Skip to content

Commit

Permalink
CreateDatabase etc. with trusted auth (DNET-284)
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Jan 28, 2019
1 parent 6b2c0c2 commit a6ad61c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 61 deletions.
Expand Up @@ -149,16 +149,59 @@ public void Dispose()
if (!_disposed)
{
_disposed = true;
Detach();
_connection = null;
_charset = null;
_eventManager = null;
_serverVersion = null;
_dialect = 0;
_handle = -1;
_packetSize = 0;
_warningMessage = null;
_transactionCount = 0;

if (TransactionCount > 0)
{
throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
}

try
{
CloseEventManager();

var detach = _handle != -1;
if (detach)
{
XdrStream.Write(IscCodes.op_detach);
XdrStream.Write(_handle);
}
XdrStream.Write(IscCodes.op_disconnect);
XdrStream.Flush();
if (detach)
{
ReadResponse();
}

CloseConnection();

#warning Here
_xdrStream?.Dispose();
}
catch (IOException ex)
{
try
{
CloseConnection();
}
catch (IOException ex2)
{
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2);
}
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
}
finally
{
_xdrStream = null;
_connection = null;
_charset = null;
_eventManager = null;
_serverVersion = null;
_dialect = 0;
_handle = -1;
_packetSize = 0;
_warningMessage = null;
_transactionCount = 0;
}
}
}

Expand Down Expand Up @@ -217,55 +260,7 @@ public virtual void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string da

public virtual void Detach()
{
if (TransactionCount > 0)
{
throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
}

try
{
CloseEventManager();

var detach = _handle != -1;
if (detach)
{
XdrStream.Write(IscCodes.op_detach);
XdrStream.Write(_handle);
}
XdrStream.Write(IscCodes.op_disconnect);
XdrStream.Flush();
if (detach)
{
ReadResponse();
}

CloseConnection();

#warning Here
_xdrStream?.Dispose();

#warning Same as in Dispose
_transactionCount = 0;
_handle = -1;
_dialect = 0;
_packetSize = 0;
_xdrStream = null;
_charset = null;
_connection = null;
_serverVersion = null;
}
catch (IOException ex)
{
try
{
CloseConnection();
}
catch (IOException ex2)
{
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2);
}
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
}
Dispose();
}

protected void SafelyDetach()
Expand Down Expand Up @@ -313,6 +308,11 @@ protected void ProcessCreateResponse(GenericResponse response)
_handle = response.ObjectHandle;
}

public virtual void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
throw new NotSupportedException("Trusted Auth isn't supported on < FB2.1.");
}

public virtual void DropDatabase()
{
try
Expand Down
Expand Up @@ -104,6 +104,21 @@ protected void ProcessTrustedAuthResponse(SspiHelper sspiHelper, ref IResponse r
response = ReadResponse();
}
}

public override void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
using (var sspiHelper = new SspiHelper())
{
var authData = sspiHelper.InitializeClientSecurity();
SendTrustedAuthToBuffer(dpb, authData);
SendCreateToBuffer(dpb, database);
XdrStream.Flush();

var response = ReadResponse();
ProcessTrustedAuthResponse(sspiHelper, ref response);
ProcessCreateResponse((GenericResponse)response);
}
}
#endregion

#region Public methods
Expand Down
Expand Up @@ -104,6 +104,11 @@ public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string d
Attach(dpb, dataSource, port, database, cryptKey);
}

public override void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
CreateDatabase(dpb, dataSource, port, database, cryptKey);
}

public IResponse ProcessCryptCallbackResponseIfNeeded(IResponse response, byte[] cryptKey)
{
while (response is CryptKeyCallbackResponse cryptResponse)
Expand Down
Expand Up @@ -169,6 +169,11 @@ public void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int p
ProcessStatusVector(_statusVector);
}

public void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
}

public void DropDatabase()
{
ClearStatusVector();
Expand Down
Expand Up @@ -39,6 +39,7 @@ internal interface IDatabase : IDisposable
void Detach();

void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
void DropDatabase();

TransactionBase BeginTransaction(TransactionParameterBuffer tpb);
Expand Down
Expand Up @@ -122,15 +122,29 @@ public void CreateDatabase(DatabaseParameterBuffer dpb)
{
using (var db = ClientFactory.CreateDatabase(_options))
{
db.CreateDatabase(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
{
db.CreateDatabaseWithTrustedAuth(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
}
else
{
db.CreateDatabase(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
}
}
}

public void DropDatabase()
{
using (var db = ClientFactory.CreateDatabase(_options))
{
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
{
db.AttachWithTrustedAuth(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
}
else
{
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
}
db.DropDatabase();
}
}
Expand Down

0 comments on commit a6ad61c

Please sign in to comment.