Add option to require encrypted connection.

This commit is contained in:
Richard Frith-Macdonald 2022-06-08 16:43:13 +01:00
parent a8f405c563
commit 34aec94427
4 changed files with 54 additions and 1 deletions

View file

@ -1,3 +1,13 @@
2022-06-08 Richard Frith-Macdonald <rfm@gnu.org>
* SQLClient.h: Declare new (-setOptions:) method.
* SQLClient.m: Implement stub for new method and add code to call it
to register any optional configuration, passing the configuration
dictionary as a parameter.
* Postgres.m: Implement new method to store configuration information
and use sslmode option if (and only if) it is set to require an
encrypted connection.
2020-09-01 Wolfgang Lux <wolfgang.lux@gmail.com>
* SQLClient.m (release): Reinstate fix to avoid deadlock while

View file

@ -55,6 +55,9 @@
#include <libpq-fe.h>
@interface SQLClientPostgres : SQLClient
{
NSDictionary *options;
}
@end
@interface SQLClientPostgres(Embedded)
@ -76,11 +79,13 @@ typedef struct {
int _backendPID;
int _descriptor; // For monitoring in run loop
NSRunLoop *_runLoop; // For listen/unlisten monitoring
NSDictionary *_options;
} ConnectionInfo;
#define cInfo ((ConnectionInfo*)(self->extra))
#define backendPID (cInfo->_backendPID)
#define connection (cInfo->_connection)
#define options (cInfo->_options)
static NSDate *future = nil;
static NSNull *null = nil;
@ -388,6 +393,7 @@ connectQuote(NSString *str)
NSString *host = nil;
NSString *port = nil;
NSString *dbase = [self database];
NSString *sslmode = [options objectForKey: @"sslmode"];
NSString *str;
NSRange r;
NSRange pwRange = NSMakeRange(NSNotFound, 0);
@ -451,6 +457,15 @@ connectQuote(NSString *str)
[m appendString: @" application_name="];
[m appendString: str];
}
if ([sslmode isEqual: @"require"])
{
str = connectQuote(@"require");
if (str != nil)
{
[m appendString: @" sslmode="];
[m appendString: str];
}
}
if ([self debugging] > 0)
{
@ -1718,6 +1733,7 @@ static inline unsigned int trim(char *str, unsigned len)
{
[self disconnect];
}
RELEASE(options);
NSZoneFree(NSDefaultMallocZone(), extra);
}
[super dealloc];
@ -1783,6 +1799,16 @@ static inline unsigned int trim(char *str, unsigned len)
return s;
}
- (void) setOptions: (NSDictionary*)o
{
if (0 == extra)
{
extra = NSZoneMalloc(NSDefaultMallocZone(), sizeof(ConnectionInfo));
memset(extra, '\0', sizeof(ConnectionInfo));
cInfo->_descriptor = -1;
}
ASSIGNCOPY(options, o);
}
@end
#if defined(GNUSTEP_BASE_LIBRARY) && !defined(__MINGW__)

View file

@ -1095,6 +1095,15 @@ SQLCLIENT_PRIVATE
*/
- (void) setName: (NSString*)s;
/** Sets any backend specific parameters for the database connection.
* The base class implementation does nothing; subclasses are expected
* to store any optional configuration information that they wish to use
* themselves.<br />
* This is called automatically to configure the connection ...
* you normally shouldn't need to call it yourself.
*/
- (void) setOptions: (NSDictionary*)o;
/**
* Set the database password for this object.<br />
* This is called automatically to configure the connection ...

View file

@ -2727,6 +2727,11 @@ static int poolConnections = 0;
[lock unlock];
}
- (void) setOptions: (NSDictionary*)o
{
return; // Abstract class does not use options
}
- (void) setPassword: (NSString*)s
{
[lock lock];
@ -3327,7 +3332,8 @@ static int poolConnections = 0;
* explicitly linked into the bundle, but in others it
* requires them to not be linked. To handle that, we create
* two versions of each bundle, the seond version has _libs
* appended to the bundle name, and has the extra libraries linked.
* appended to the bundle name, and has the extra libraries
* linked.
*/
path = [path stringByDeletingPathExtension];
path = [path stringByAppendingString: @"_libs"];
@ -3405,6 +3411,8 @@ static int poolConnections = 0;
}
}
[self setPassword: s];
[self setOptions: (nil == d) ? o : d];
}
NS_HANDLER
{