Skip to content

Commit a6ad61c

Browse files
committed
CreateDatabase etc. with trusted auth (DNET-284)
1 parent 6b2c0c2 commit a6ad61c

File tree

6 files changed

+101
-61
lines changed

6 files changed

+101
-61
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,59 @@ public void Dispose()
149149
if (!_disposed)
150150
{
151151
_disposed = true;
152-
Detach();
153-
_connection = null;
154-
_charset = null;
155-
_eventManager = null;
156-
_serverVersion = null;
157-
_dialect = 0;
158-
_handle = -1;
159-
_packetSize = 0;
160-
_warningMessage = null;
161-
_transactionCount = 0;
152+
153+
if (TransactionCount > 0)
154+
{
155+
throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
156+
}
157+
158+
try
159+
{
160+
CloseEventManager();
161+
162+
var detach = _handle != -1;
163+
if (detach)
164+
{
165+
XdrStream.Write(IscCodes.op_detach);
166+
XdrStream.Write(_handle);
167+
}
168+
XdrStream.Write(IscCodes.op_disconnect);
169+
XdrStream.Flush();
170+
if (detach)
171+
{
172+
ReadResponse();
173+
}
174+
175+
CloseConnection();
176+
177+
#warning Here
178+
_xdrStream?.Dispose();
179+
}
180+
catch (IOException ex)
181+
{
182+
try
183+
{
184+
CloseConnection();
185+
}
186+
catch (IOException ex2)
187+
{
188+
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2);
189+
}
190+
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
191+
}
192+
finally
193+
{
194+
_xdrStream = null;
195+
_connection = null;
196+
_charset = null;
197+
_eventManager = null;
198+
_serverVersion = null;
199+
_dialect = 0;
200+
_handle = -1;
201+
_packetSize = 0;
202+
_warningMessage = null;
203+
_transactionCount = 0;
204+
}
162205
}
163206
}
164207

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

218261
public virtual void Detach()
219262
{
220-
if (TransactionCount > 0)
221-
{
222-
throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
223-
}
224-
225-
try
226-
{
227-
CloseEventManager();
228-
229-
var detach = _handle != -1;
230-
if (detach)
231-
{
232-
XdrStream.Write(IscCodes.op_detach);
233-
XdrStream.Write(_handle);
234-
}
235-
XdrStream.Write(IscCodes.op_disconnect);
236-
XdrStream.Flush();
237-
if (detach)
238-
{
239-
ReadResponse();
240-
}
241-
242-
CloseConnection();
243-
244-
#warning Here
245-
_xdrStream?.Dispose();
246-
247-
#warning Same as in Dispose
248-
_transactionCount = 0;
249-
_handle = -1;
250-
_dialect = 0;
251-
_packetSize = 0;
252-
_xdrStream = null;
253-
_charset = null;
254-
_connection = null;
255-
_serverVersion = null;
256-
}
257-
catch (IOException ex)
258-
{
259-
try
260-
{
261-
CloseConnection();
262-
}
263-
catch (IOException ex2)
264-
{
265-
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2);
266-
}
267-
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
268-
}
263+
Dispose();
269264
}
270265

271266
protected void SafelyDetach()
@@ -313,6 +308,11 @@ protected void ProcessCreateResponse(GenericResponse response)
313308
_handle = response.ObjectHandle;
314309
}
315310

311+
public virtual void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
312+
{
313+
throw new NotSupportedException("Trusted Auth isn't supported on < FB2.1.");
314+
}
315+
316316
public virtual void DropDatabase()
317317
{
318318
try

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version11/GdsDatabase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ protected void ProcessTrustedAuthResponse(SspiHelper sspiHelper, ref IResponse r
104104
response = ReadResponse();
105105
}
106106
}
107+
108+
public override void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
109+
{
110+
using (var sspiHelper = new SspiHelper())
111+
{
112+
var authData = sspiHelper.InitializeClientSecurity();
113+
SendTrustedAuthToBuffer(dpb, authData);
114+
SendCreateToBuffer(dpb, database);
115+
XdrStream.Flush();
116+
117+
var response = ReadResponse();
118+
ProcessTrustedAuthResponse(sspiHelper, ref response);
119+
ProcessCreateResponse((GenericResponse)response);
120+
}
121+
}
107122
#endregion
108123

109124
#region Public methods

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version13/GdsDatabase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string d
104104
Attach(dpb, dataSource, port, database, cryptKey);
105105
}
106106

107+
public override void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
108+
{
109+
CreateDatabase(dpb, dataSource, port, database, cryptKey);
110+
}
111+
107112
public IResponse ProcessCryptCallbackResponseIfNeeded(IResponse response, byte[] cryptKey)
108113
{
109114
while (response is CryptKeyCallbackResponse cryptResponse)

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Native/FesDatabase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ public void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int p
169169
ProcessStatusVector(_statusVector);
170170
}
171171

172+
public void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
173+
{
174+
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
175+
}
176+
172177
public void DropDatabase()
173178
{
174179
ClearStatusVector();

Provider/src/FirebirdSql.Data.FirebirdClient/Common/IDatabase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal interface IDatabase : IDisposable
3939
void Detach();
4040

4141
void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
42+
void CreateDatabaseWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
4243
void DropDatabase();
4344

4445
TransactionBase BeginTransaction(TransactionParameterBuffer tpb);

Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionInternal.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,29 @@ public void CreateDatabase(DatabaseParameterBuffer dpb)
122122
{
123123
using (var db = ClientFactory.CreateDatabase(_options))
124124
{
125-
db.CreateDatabase(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
125+
if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
126+
{
127+
db.CreateDatabaseWithTrustedAuth(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
128+
}
129+
else
130+
{
131+
db.CreateDatabase(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
132+
}
126133
}
127134
}
128135

129136
public void DropDatabase()
130137
{
131138
using (var db = ClientFactory.CreateDatabase(_options))
132139
{
133-
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
140+
if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
141+
{
142+
db.AttachWithTrustedAuth(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
143+
}
144+
else
145+
{
146+
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
147+
}
134148
db.DropDatabase();
135149
}
136150
}

0 commit comments

Comments
 (0)