-
Notifications
You must be signed in to change notification settings - Fork 818
/
NpgsqlConnector.cs
1143 lines (949 loc) · 39.2 KB
/
NpgsqlConnector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2002 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
// Connector.cs
// ------------------------------------------------------------------
// Project
// Npgsql
// Status
// 0.00.0000 - 06/17/2002 - ulrich sprick - created
// - 06/??/2004 - Glen Parker<glenebob@nwlink.com> rewritten
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using Mono.Security.Protocol.Tls;
using NpgsqlTypes;
namespace Npgsql
{
/// <summary>
/// Represents the method that allows the application to provide a certificate collection to be used for SSL clien authentication
/// </summary>
/// <param name="certificates">A <see cref="System.Security.Cryptography.X509Certificates.X509CertificateCollection">X509CertificateCollection</see> to be filled with one or more client certificates.</param>
public delegate void ProvideClientCertificatesCallback(X509CertificateCollection certificates);
/// <summary>
/// !!! Helper class, for compilation only.
/// Connector implements the logic for the Connection Objects to
/// access the physical connection to the database, and isolate
/// the application developer from connection pooling internals.
/// </summary>
internal class NpgsqlConnector
{
// Immutable.
private readonly NpgsqlConnectionStringBuilder settings;
/// <summary>
/// Occurs on NoticeResponses from the PostgreSQL backend.
/// </summary>
internal event NoticeEventHandler Notice;
/// <summary>
/// Occurs on NotificationResponses from the PostgreSQL backend.
/// </summary>
internal event NotificationEventHandler Notification;
/// <summary>
/// Called to provide client certificates for SSL handshake.
/// </summary>
internal event ProvideClientCertificatesCallback ProvideClientCertificatesCallback;
/// <summary>
/// Mono.Security.Protocol.Tls.CertificateSelectionCallback delegate.
/// </summary>
internal event CertificateSelectionCallback CertificateSelectionCallback;
/// <summary>
/// Mono.Security.Protocol.Tls.CertificateValidationCallback delegate.
/// </summary>
internal event CertificateValidationCallback CertificateValidationCallback;
/// <summary>
/// Mono.Security.Protocol.Tls.PrivateKeySelectionCallback delegate.
/// </summary>
internal event PrivateKeySelectionCallback PrivateKeySelectionCallback;
private ConnectionState _connection_state;
// The physical network connection to the backend.
private Stream _stream;
private Socket _socket;
// Mediator which will hold data generated from backend.
private readonly NpgsqlMediator _mediator;
private ProtocolVersion _backendProtocolVersion;
private Version _serverVersion;
// Values for possible CancelRequest messages.
private NpgsqlBackEndKeyData _backend_keydata;
// Flag for transaction status.
// private Boolean _inTransaction = false;
private NpgsqlTransaction _transaction = null;
private Boolean _supportsPrepare = false;
private Boolean _supportsSavepoint = false;
private NpgsqlBackendTypeMapping _oidToNameMapping = null;
private Boolean _isInitialized;
private readonly Boolean _pooled;
private readonly Boolean _shared;
private NpgsqlState _state;
private Int32 _planIndex;
private Int32 _portalIndex;
private const String _planNamePrefix = "npgsqlplan";
private const String _portalNamePrefix = "npgsqlportal";
private Thread _notificationThread;
// The AutoResetEvent to synchronize processing threads.
internal AutoResetEvent _notificationAutoResetEvent;
// Counter of notification thread start/stop requests in order to
internal Int16 _notificationThreadStopCount;
private Exception _notificationException;
internal ForwardsOnlyDataReader CurrentReader;
// Some kinds of messages only get one response, and do not
// expect a ready_for_query response.
private bool _requireReadyForQuery = true;
private bool? _useConformantStrings;
private readonly Dictionary<string, NpgsqlParameterStatus> _serverParameters =
new Dictionary<string, NpgsqlParameterStatus>(StringComparer.InvariantCultureIgnoreCase);
// For IsValid test
private readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
#if WINDOWS && UNMANAGED
private SSPIHandler _sspi;
internal SSPIHandler SSPI
{
get { return _sspi; }
set { _sspi = value; }
}
#endif
public NpgsqlConnector(NpgsqlConnection Connection)
: this(Connection.CopyConnectionStringBuilder(), Connection.Pooling, false)
{}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="Shared">Controls whether the connector can be shared.</param>
public NpgsqlConnector(NpgsqlConnectionStringBuilder ConnectionString, bool Pooled, bool Shared)
{
this.settings = ConnectionString;
_connection_state = ConnectionState.Closed;
_pooled = Pooled;
_shared = Shared;
_isInitialized = false;
_state = NpgsqlClosedState.Instance;
_mediator = new NpgsqlMediator();
_oidToNameMapping = new NpgsqlBackendTypeMapping();
_planIndex = 0;
_portalIndex = 0;
_notificationThreadStopCount = 1;
_notificationAutoResetEvent = new AutoResetEvent(true);
}
//Finalizer should never be used, but if some incident has left to a connector being abandoned (most likely
//case being a user not cleaning up a connection properly) then this way we can at least reduce the damage.
//~NpgsqlConnector()
//{
// Close();
//}
internal String Host
{
get { return settings.Host; }
}
internal Int32 Port
{
get { return settings.Port; }
}
internal String Database
{
get { return settings.ContainsKey(Keywords.Database) ? settings.Database : settings.UserName; }
}
internal String UserName
{
get { return settings.UserName; }
}
internal byte[] Password
{
get { return settings.PasswordAsByteArray; }
}
internal Boolean SSL
{
get { return settings.SSL; }
}
internal SslMode SslMode
{
get { return settings.SslMode; }
}
internal Int32 ConnectionTimeout
{
get { return settings.Timeout; }
}
internal Int32 CommandTimeout
{
get { return settings.CommandTimeout; }
}
internal Boolean Enlist
{
get { return settings.Enlist; }
}
public bool UseExtendedTypes
{
get { return settings.UseExtendedTypes; }
}
internal Boolean IntegratedSecurity
{
get { return settings.IntegratedSecurity; }
}
/// <summary>
/// Gets the current state of the connection.
/// </summary>
internal ConnectionState State
{
get
{
if (_connection_state != ConnectionState.Closed && CurrentReader != null && !CurrentReader._cleanedUp)
{
return ConnectionState.Open | ConnectionState.Fetching;
}
return _connection_state;
}
}
/// <summary>
/// Return Connection String.
/// </summary>
internal string ConnectionString
{
get { return settings.ConnectionString; }
}
// State
internal void Query(NpgsqlCommand queryCommand)
{
CurrentState.Query(this, queryCommand);
}
internal IEnumerable<IServerResponseObject> QueryEnum(NpgsqlCommand queryCommand)
{
if (CurrentReader != null)
{
if (!CurrentReader._cleanedUp)
{
throw new InvalidOperationException(
"There is already an open DataReader associated with this Command which must be closed first.");
}
CurrentReader.Close();
}
return CurrentState.QueryEnum(this, queryCommand);
}
internal void Authenticate(byte[] password)
{
CurrentState.Authenticate(this, password);
}
internal void Parse(NpgsqlParse parse)
{
CurrentState.Parse(this, parse);
}
internal void Flush()
{
CurrentState.Flush(this);
}
internal void TestConnector()
{
CurrentState.TestConnector(this);
}
internal NpgsqlRowDescription Sync()
{
return CurrentState.Sync(this);
}
internal void Bind(NpgsqlBind bind)
{
CurrentState.Bind(this, bind);
}
internal void Describe(NpgsqlDescribe describe)
{
CurrentState.Describe(this, describe);
}
internal void Execute(NpgsqlExecute execute)
{
CurrentState.Execute(this, execute);
}
internal IEnumerable<IServerResponseObject> ExecuteEnum(NpgsqlExecute execute)
{
return CurrentState.ExecuteEnum(this, execute);
}
/// <summary>
/// This method checks if the connector is still ok.
/// We try to send a simple query text, select 1 as ConnectionTest;
/// </summary>
internal Boolean IsValid()
{
try
{
// Here we use a fake NpgsqlCommand, just to send the test query string.
// Get random test value.
Byte[] testBytes = new Byte[2];
rng.GetNonZeroBytes(testBytes);
String testValue = String.Format("Npgsql{0}{1}", testBytes[0], testBytes[1]);
//Query(new NpgsqlCommand("select 1 as ConnectionTest", this));
string compareValue = string.Empty;
using(NpgsqlCommand cmd = new NpgsqlCommand("select '" + testValue + "'", this))
{
compareValue = (string) cmd.ExecuteScalar();
}
if (compareValue != testValue)
return false;
// Clear mediator.
Mediator.ResetResponses();
this.RequireReadyForQuery = true;
}
catch
{
return false;
}
return true;
}
/// <summary>
/// This method is responsible for releasing all resources associated with this Connector.
/// </summary>
internal void ReleaseResources()
{
if (_connection_state != ConnectionState.Closed)
{
ReleasePlansPortals();
ReleaseRegisteredListen();
}
}
internal void ReleaseRegisteredListen()
{
//Query(new NpgsqlCommand("unlisten *", this));
using(NpgsqlCommand cmd = new NpgsqlCommand("unlisten *", this))
{
Query(cmd);
}
}
/// <summary>
/// This method is responsible to release all portals used by this Connector.
/// </summary>
internal void ReleasePlansPortals()
{
Int32 i = 0;
if (_planIndex > 0)
{
for (i = 1; i <= _planIndex; i++)
{
try
{
//Query(new NpgsqlCommand(String.Format("deallocate \"{0}\";", _planNamePrefix + i), this));
using(NpgsqlCommand cmd = new NpgsqlCommand(String.Format("deallocate \"{0}\";", _planNamePrefix + i.ToString()), this))
{
Query(cmd);
}
}
// Ignore any error which may occur when releasing portals as this portal name may not be valid anymore. i.e.: the portal name was used on a prepared query which had errors.
catch(Exception) {}
}
}
_portalIndex = 0;
_planIndex = 0;
}
internal void FireNotice(NpgsqlError e)
{
if (Notice != null)
{
try
{
Notice(this, new NpgsqlNoticeEventArgs(e));
}
catch
{
} //Eat exceptions from user code.
}
}
internal void FireNotification(NpgsqlNotificationEventArgs e)
{
if (Notification != null)
{
try
{
Notification(this, e);
}
catch
{
} //Eat exceptions from user code.
}
}
/// <summary>
/// Default SSL CertificateSelectionCallback implementation.
/// </summary>
internal X509Certificate DefaultCertificateSelectionCallback(X509CertificateCollection clientCertificates,
X509Certificate serverCertificate, string targetHost,
X509CertificateCollection serverRequestedCertificates)
{
if (CertificateSelectionCallback != null)
{
return CertificateSelectionCallback(clientCertificates, serverCertificate, targetHost, serverRequestedCertificates);
}
else
{
return null;
}
}
/// <summary>
/// Default SSL CertificateValidationCallback implementation.
/// </summary>
internal bool DefaultCertificateValidationCallback(X509Certificate certificate, int[] certificateErrors)
{
if (CertificateValidationCallback != null)
{
return CertificateValidationCallback(certificate, certificateErrors);
}
else
{
return true;
}
}
/// <summary>
/// Default SSL PrivateKeySelectionCallback implementation.
/// </summary>
internal AsymmetricAlgorithm DefaultPrivateKeySelectionCallback(X509Certificate certificate, string targetHost)
{
if (PrivateKeySelectionCallback != null)
{
return PrivateKeySelectionCallback(certificate, targetHost);
}
else
{
return null;
}
}
/// <summary>
/// Default SSL ProvideClientCertificatesCallback implementation.
/// </summary>
internal void DefaultProvideClientCertificatesCallback(X509CertificateCollection certificates)
{
if (ProvideClientCertificatesCallback != null)
{
ProvideClientCertificatesCallback(certificates);
}
}
/// <summary>
/// Version of backend server this connector is connected to.
/// </summary>
internal Version ServerVersion
{
get { return _serverVersion; }
set { _serverVersion = value; }
}
/// <summary>
/// Backend protocol version in use by this connector.
/// </summary>
internal ProtocolVersion BackendProtocolVersion
{
get { return _backendProtocolVersion; }
set { _backendProtocolVersion = value; }
}
/// <summary>
/// The physical connection stream to the backend.
/// </summary>
internal Stream Stream
{
get { return _stream; }
set { _stream = value; }
}
/// <summary>
/// The physical connection socket to the backend.
/// </summary>
internal Socket Socket
{
get { return _socket; }
set { _socket = value; }
}
/// <summary>
/// Reports if this connector is fully connected.
/// </summary>
internal Boolean IsInitialized
{
get { return _isInitialized; }
set { _isInitialized = value; }
}
internal NpgsqlState CurrentState
{
get { return _state; }
set { _state = value; }
}
internal bool Pooled
{
get { return _pooled; }
}
internal bool Shared
{
get { return _shared; }
}
internal NpgsqlBackEndKeyData BackEndKeyData
{
get { return _backend_keydata; }
set { _backend_keydata = value; }
}
internal NpgsqlBackendTypeMapping OidToNameMapping
{
get { return _oidToNameMapping; }
}
internal Version CompatVersion
{
get
{
return settings.Compatible;
}
}
/// <summary>
/// The connection mediator.
/// </summary>
internal NpgsqlMediator Mediator
{
get { return _mediator; }
}
/// <summary>
/// Report if the connection is in a transaction.
/// </summary>
internal NpgsqlTransaction Transaction
{
get { return _transaction; }
set { _transaction = value; }
}
internal Boolean SupportsApplicationName
{
get { return ServerVersion >= new Version(9, 0, 0); }
}
/// <summary>
/// Report whether the current connection can support prepare functionality.
/// </summary>
internal Boolean SupportsPrepare
{
get { return _supportsPrepare; }
set { _supportsPrepare = value; }
}
internal Boolean SupportsSavepoint
{
get { return _supportsSavepoint; }
set { _supportsSavepoint = value; }
}
/// <summary>
/// This method is required to set all the version dependent features flags.
/// SupportsPrepare means the server can use prepared query plans (7.3+)
/// </summary>
// FIXME - should be private
internal void ProcessServerVersion()
{
this._supportsPrepare = (ServerVersion >= new Version(7, 3, 0));
this._supportsSavepoint = (ServerVersion >= new Version(8, 0, 0));
}
/*/// <value>Counts the numbers of Connections that share
/// this Connector. Used in Release() to decide wether this
/// connector is to be moved to the PooledConnectors list.</value>
// internal int mShareCount;*/
/// <summary>
/// Opens the physical connection to the server.
/// </summary>
/// <remarks>Usually called by the RequestConnector
/// Method of the connection pool manager.</remarks>
internal void Open()
{
ServerVersion = null;
// If Connection.ConnectionString specifies a protocol version, we will
// not try to fall back to version 2 on failure.
_backendProtocolVersion = (settings.Protocol == ProtocolVersion.Unknown)
? ProtocolVersion.Version3
: settings.Protocol;
// Reset state to initialize new connector in pool.
CurrentState = NpgsqlClosedState.Instance;
// Get a raw connection, possibly SSL...
CurrentState.Open(this);
try
{
// Establish protocol communication and handle authentication...
CurrentState.Startup(this);
}
catch (NpgsqlException ne)
{
// Check for protocol not supported. If we have been told what protocol to use,
// we will not try this step.
if (settings.Protocol != ProtocolVersion.Unknown)
{
throw;
}
// If we attempted protocol version 3, it may be possible to drop back to version 2.
if (BackendProtocolVersion != ProtocolVersion.Version3)
{
throw;
}
NpgsqlError Error0 = (NpgsqlError) ne.Errors[0];
// If NpgsqlError..ctor() encounters a version 2 error,
// it will set its own protocol version to version 2. That way, we can tell
// easily if the error was a FATAL: protocol error.
if (Error0.BackendProtocolVersion != ProtocolVersion.Version2)
{
throw;
}
// Try using the 2.0 protocol.
_mediator.ResetResponses();
BackendProtocolVersion = ProtocolVersion.Version2;
CurrentState = NpgsqlClosedState.Instance;
// Get a raw connection, possibly SSL...
CurrentState.Open(this);
// Establish protocol communication and handle authentication...
CurrentState.Startup(this);
}
// Change the state of connection to open and ready.
_connection_state = ConnectionState.Open;
CurrentState = NpgsqlReadyState.Instance;
// Fall back to the old way, SELECT VERSION().
// This should not happen for protocol version 3+.
if (ServerVersion == null)
{
//NpgsqlCommand command = new NpgsqlCommand("set DATESTYLE TO ISO;select version();", this);
//ServerVersion = new Version(PGUtil.ExtractServerVersion((string) command.ExecuteScalar()));
using(NpgsqlCommand command = new NpgsqlCommand("set DATESTYLE TO ISO;select version();", this))
{
ServerVersion = new Version(PGUtil.ExtractServerVersion((string) command.ExecuteScalar()));
}
}
// Adjust client encoding.
NpgsqlParameterStatus clientEncodingParam = null;
if(
!ServerParameters.TryGetValue("client_encoding", out clientEncodingParam) ||
(!string.Equals(clientEncodingParam.ParameterValue, "UTF8", StringComparison.OrdinalIgnoreCase) && !string.Equals(clientEncodingParam.ParameterValue, "UNICODE", StringComparison.OrdinalIgnoreCase))
)
new NpgsqlCommand("SET CLIENT_ENCODING TO UTF8", this).ExecuteBlind();
if (!string.IsNullOrEmpty(settings.SearchPath))
{
/*NpgsqlParameter p = new NpgsqlParameter("p", DbType.String);
p.Value = settings.SearchPath;
NpgsqlCommand commandSearchPath = new NpgsqlCommand("SET SEARCH_PATH TO :p,public", this);
commandSearchPath.Parameters.Add(p);
commandSearchPath.ExecuteNonQuery();*/
/*NpgsqlParameter p = new NpgsqlParameter("p", DbType.String);
p.Value = settings.SearchPath;
NpgsqlCommand commandSearchPath = new NpgsqlCommand("SET SEARCH_PATH TO :p,public", this);
commandSearchPath.Parameters.Add(p);
commandSearchPath.ExecuteNonQuery();*/
// TODO: Add proper message when finding a semicolon in search_path.
// This semicolon could lead to a sql injection security hole as someone could write in connection string:
// searchpath=public;delete from table; and it would be executed.
if (settings.SearchPath.Contains(";"))
{
throw new InvalidOperationException();
}
// This is using string concatenation because set search_path doesn't allow type casting. ::text
NpgsqlCommand commandSearchPath = new NpgsqlCommand("SET SEARCH_PATH=" + settings.SearchPath, this);
commandSearchPath.ExecuteBlind();
}
if (!string.IsNullOrEmpty(settings.ApplicationName))
{
if (!SupportsApplicationName)
{
//TODO
//throw new InvalidOperationException(resman.GetString("Exception_ApplicationNameNotSupported"));
throw new InvalidOperationException("ApplicationName not supported.");
}
if (settings.ApplicationName.Contains(";"))
{
throw new InvalidOperationException();
}
NpgsqlCommand commandApplicationName = new NpgsqlCommand("SET APPLICATION_NAME='" + settings.ApplicationName + "'", this);
commandApplicationName.ExecuteBlind();
}
/*
* Try to set SSL negotiation to 0. As of 2010-03-29, recent problems in SSL library implementations made
* postgresql to add a parameter to set a value when to do this renegotiation or 0 to disable it.
* Currently, Npgsql has a problem with renegotiation so, we are trying to disable it here.
* This only works on postgresql servers where the ssl renegotiation settings is supported of course.
* See http://lists.pgfoundry.org/pipermail/npgsql-devel/2010-February/001065.html for more information.
*/
try
{
NpgsqlCommand commandSslrenegotiation = new NpgsqlCommand("SET ssl_renegotiation_limit=0", this);
commandSslrenegotiation.ExecuteBlind();
}
catch {}
/*
* Set precision digits to maximum value possible. For postgresql before 9 it was 2, after that, it is 3.
* This way, we set first to 2 and then to 3. If there is an error because of 3, it will have been set to 2 at least.
* Check bug report #1010992 for more information.
*/
try
{
NpgsqlCommand commandSingleDoublePrecision = new NpgsqlCommand("SET extra_float_digits=2;SET extra_float_digits=3;", this);
commandSingleDoublePrecision.ExecuteBlind();
}
catch {}
/*
* Set lc_monetary format to 'C' ir order to get a culture agnostic representation of money.
* I noticed that on Windows, even when the lc_monetary is English_United States.UTF-8, negative
* money is formatted as ($value) with parentheses to indicate negative value.
* By going with a culture agnostic format, we get a consistent behavior.
*/
try
{
NpgsqlCommand commandMonetaryFormatC = new NpgsqlCommand("SET lc_monetary='C';", this);
commandMonetaryFormatC.ExecuteBlind();
}
catch
{
}
// Make a shallow copy of the type mapping that the connector will own.
// It is possible that the connector may add types to its private
// mapping that will not be valid to another connector, even
// if connected to the same backend version.
_oidToNameMapping = NpgsqlTypesHelper.CreateAndLoadInitialTypesMapping(this).Clone();
ProcessServerVersion();
// The connector is now fully initialized. Beyond this point, it is
// safe to release it back to the pool rather than closing it.
IsInitialized = true;
}
/// <summary>
/// Closes the physical connection to the server.
/// </summary>
internal void Close()
{
try
{
if (_connection_state != ConnectionState.Closed)
{
_connection_state = ConnectionState.Closed;
this.CurrentState.Close(this);
_serverParameters.Clear();
ServerVersion = null;
}
}
catch
{
}
}
internal void CancelRequest()
{
NpgsqlConnector cancelConnector = new NpgsqlConnector(settings, false, false);
cancelConnector._backend_keydata = BackEndKeyData;
try
{
// Get a raw connection, possibly SSL...
cancelConnector.CurrentState.Open(cancelConnector);
// Cancel current request.
cancelConnector.CurrentState.CancelRequest(cancelConnector);
}
finally
{
cancelConnector.CurrentState.Close(cancelConnector);
}
}
///<summary>
/// Returns next portal index.
///</summary>
internal String NextPortalName()
{
return _portalNamePrefix + Interlocked.Increment(ref _portalIndex).ToString();
}
///<summary>
/// Returns next plan index.
///</summary>
internal String NextPlanName()
{
return _planNamePrefix + Interlocked.Increment(ref _planIndex).ToString();
}
internal void RemoveNotificationThread()
{
// Wait notification thread finish its work.
_notificationAutoResetEvent.WaitOne();
// Kill notification thread.
_notificationThread.Abort();
_notificationThread = null;
// Special case in order to not get problems with thread synchronization.
// It will be turned to 0 when synch thread is created.
_notificationThreadStopCount = 1;
}
internal void AddNotificationThread()
{
_notificationThreadStopCount = 0;
_notificationAutoResetEvent.Set();
NpgsqlContextHolder contextHolder = new NpgsqlContextHolder(this, CurrentState);
_notificationThread = new Thread(new ThreadStart(contextHolder.ProcessServerMessages));
_notificationThread.Start();
}
//Use with using(){} to perform the sentry pattern
//on stopping and starting notification thread
//(The sentry pattern is a generalisation of RAII where we
//have a pair of actions - one "undoing" the previous
//and we want to execute the first and second around other code,
//then we treat it much like resource mangement in RAII.
//try{}finally{} also does execute-around, but sentry classes
//have some extra flexibility (e.g. they can be "owned" by
//another object and then cleaned up when that object is
//cleaned up), and can act as the sole gate-way
//to the code in question, guaranteeing that using code can't be written
//so that the "undoing" is forgotten.
internal class NotificationThreadBlock : IDisposable
{
private NpgsqlConnector _connector;
public NotificationThreadBlock(NpgsqlConnector connector)
{
(_connector = connector).StopNotificationThread();
}
public void Dispose()
{
if (_connector != null)
{
_connector.ResumeNotificationThread();
}
_connector = null;
}
}
internal NotificationThreadBlock BlockNotificationThread()
{
return new NotificationThreadBlock(this);
}
private void StopNotificationThread()
{
// first check to see if an exception has
// been thrown by the notification thread.
if (_notificationException != null)
throw _notificationException;
_notificationThreadStopCount++;
if (_notificationThreadStopCount == 1) // If this call was the first to increment.
{
_notificationAutoResetEvent.WaitOne();
}
}
private void ResumeNotificationThread()
{
_notificationThreadStopCount--;
if (_notificationThreadStopCount == 0)
{