Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Commit

Permalink
Add support for sslverify connection parameter in libpq 8.4 that cont…
Browse files Browse the repository at this point in the history
…rols

how server certificates are validated by the client when it connects.

This includes a new connection option in the Server dialog.


git-svn-id: svn://svn.pgadmin.org/trunk@7715 a7884b65-44f6-0310-8a51-81a127f17b15
  • Loading branch information
mhagander committed Mar 17, 2009
1 parent a236406 commit 8ec4339
Show file tree
Hide file tree
Showing 21 changed files with 3,475 additions and 3,301 deletions.
44 changes: 35 additions & 9 deletions pgadmin/db/pgConn.cpp
Expand Up @@ -48,7 +48,7 @@ static void pgNoticeProcessor(void *arg, const char *message)
((pgConn*)arg)->Notice(message);
}

pgConn::pgConn(const wxString& server, const wxString& database, const wxString& username, const wxString& password, int port, int sslmode, OID oid)
pgConn::pgConn(const wxString& server, const wxString& database, const wxString& username, const wxString& password, int port, int sslmode, int sslverifymode, OID oid)
{
wxString msg, hostip, hostname;

Expand All @@ -58,6 +58,7 @@ pgConn::pgConn(const wxString& server, const wxString& database, const wxString&
save_password = password;
save_port = port;
save_sslmode = sslmode;
save_sslverifymode = sslverifymode;
save_oid = oid;

memset(features, 0, sizeof(features));
Expand Down Expand Up @@ -160,6 +161,15 @@ pgConn::pgConn(const wxString& server, const wxString& database, const wxString&
case 2: connstr.Append(wxT(" requiressl=0")); break;
}
}
if (libpqVersion >= 8.4)
{
switch (sslverifymode)
{
case 1: connstr.Append(wxT(" sslverify=cn")); break;
case 2: connstr.Append(wxT(" sslverify=cert")); break;
case 3: connstr.Append(wxT(" sslverify=none")); break;
}
}
connstr.Trim(false);

// Open the connection
Expand Down Expand Up @@ -252,7 +262,7 @@ void pgConn::Close()

pgConn *pgConn::Duplicate()
{
return new pgConn(wxString(save_server), wxString(save_database), wxString(save_username), wxString(save_password), save_port, save_sslmode, save_oid);
return new pgConn(wxString(save_server), wxString(save_database), wxString(save_username), wxString(save_password), save_port, save_sslmode, save_sslverifymode, save_oid);
}

// Return the SSL mode name
Expand All @@ -262,19 +272,30 @@ wxString pgConn::GetSslModeName()
{
case 1:
return wxT("require");
break;
case 2:
return wxT("prefer");
break;
case 3:
return wxT("allow");
break;
case 4:
return wxT("disable");
break;
default:
return wxT("prefer");
break;
}
}

// Return the SSL verify mode name
wxString pgConn::GetSslVerifyModeName()
{
switch (save_sslverifymode)
{
case 1:
return wxT("cn");
case 2:
return wxT("cert");
case 3:
return wxT("none");
default:
return wxT("cn");
}
}

Expand Down Expand Up @@ -481,8 +502,13 @@ void pgConn::ExamineLibpqVersion()
{
if (!strcmp(co->keyword, "sslmode"))
{
libpqVersion=7.4;
break;
if (libpqVersion < 7.4)
libpqVersion=7.4;
}
if (!strcmp(co->keyword, "sslverify"))
{
if (libpqVersion < 8.4)
libpqVersion=8.4;
}
co++;
}
Expand Down
26 changes: 22 additions & 4 deletions pgadmin/debugger/dbgPgConn.cpp
Expand Up @@ -42,19 +42,19 @@
//
// The constructor creates a new thread and connects to the specified server

dbgPgConn::dbgPgConn(frmDebugger *frame, const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode )
dbgPgConn::dbgPgConn(frmDebugger *frame, const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode, int sslverify )
: m_frame(frame)
{
Init( server, database, userName, password, port, sslmode, true );
Init( server, database, userName, password, port, sslmode, sslverify, true );
}

dbgPgConn::dbgPgConn(frmDebugger *frame, const dbgConnProp & props, bool startThread )
: m_frame(frame)
{
Init( props.m_host, props.m_database, props.m_userName, props.m_password, props.m_port, props.m_sslMode, startThread );
Init( props.m_host, props.m_database, props.m_userName, props.m_password, props.m_port, props.m_sslMode, props.m_sslVerify, startThread );
}

void dbgPgConn::Init( const wxString &server, const wxString &database, const wxString &username, const wxString &password, const wxString &port, int sslmode, bool startThread )
void dbgPgConn::Init( const wxString &server, const wxString &database, const wxString &username, const wxString &password, const wxString &port, int sslmode, int sslverify, bool startThread )
{
m_pgConn = NULL;
m_majorVersion = 0;
Expand Down Expand Up @@ -192,6 +192,24 @@ void dbgPgConn::Init( const wxString &server, const wxString &database, const wx
default:
break;
}

switch (sslverify)
{
case 1:
connectParams.Append(wxT(" sslverify=cn"));
break;

case 2:
connectParams.Append(wxT(" sslverify=cert"));
break;

case 3:
connectParams.Append(wxT(" sslverify=none"));
break;

default:
break;
}

connectParams.Trim(true);
connectParams.Trim(false);
Expand Down
6 changes: 3 additions & 3 deletions pgadmin/dlg/dlgSelectConnection.cpp
Expand Up @@ -203,10 +203,10 @@ pgConn *dlgSelectConnection::CreateConn()
}
}

pgConn *dlgSelectConnection::CreateConn(wxString& server, wxString& dbname, wxString& username, int port, int sslmode, bool writeMRU)
pgConn *dlgSelectConnection::CreateConn(wxString& server, wxString& dbname, wxString& username, int port, int sslmode, int sslverify, bool writeMRU)
{
pgConn *newconn;
newconn = new pgConn(server, dbname, username, wxT(""), port, sslmode);
newconn = new pgConn(server, dbname, username, wxT(""), port, sslmode, sslverify);
if (newconn->GetStatus() != PGCONN_OK &&
newconn->GetLastError().Cmp(wxString(PQnoPasswordSupplied, wxConvUTF8)) == 0)
{
Expand All @@ -220,7 +220,7 @@ pgConn *dlgSelectConnection::CreateConn(wxString& server, wxString& dbname, wxSt
if (dlg.Go() != wxID_OK)
return NULL;

newconn = new pgConn(server, dbname, username, dlg.GetPassword(), port, sslmode);
newconn = new pgConn(server, dbname, username, dlg.GetPassword(), port, sslmode, sslverify);
}

if (newconn)
Expand Down
21 changes: 20 additions & 1 deletion pgadmin/dlg/dlgServer.cpp
Expand Up @@ -29,6 +29,7 @@
#define cbDatabase CTRL_COMBOBOX("cbDatabase")
#define txtPort CTRL_TEXT("txtPort")
#define cbSSL CTRL_COMBOBOX("cbSSL")
#define cbSSLverify CTRL_COMBOBOX("cbSSLverify")
#define txtUsername CTRL_TEXT("txtUsername")
#define stTryConnect CTRL_STATIC("stTryConnect")
#define chkTryConnect CTRL_CHECKBOX("chkTryConnect")
Expand All @@ -53,6 +54,7 @@ BEGIN_EVENT_TABLE(dlgServer, dlgProperty)
EVT_TEXT(XRCID("txtUsername"), dlgProperty::OnChange)
EVT_TEXT(XRCID("txtDbRestriction"), dlgServer::OnChangeRestr)
EVT_COMBOBOX(XRCID("cbSSL"), dlgProperty::OnChange)
EVT_COMBOBOX(XRCID("cbSSLverify"), dlgProperty::OnChange)
EVT_CHECKBOX(XRCID("chkStorePwd"), dlgProperty::OnChange)
EVT_CHECKBOX(XRCID("chkRestore"), dlgProperty::OnChange)
EVT_CHECKBOX(XRCID("chkTryConnect"), dlgServer::OnChangeTryConnect)
Expand Down Expand Up @@ -85,6 +87,8 @@ dlgServer::dlgServer(pgaFactory *f, frmMain *frame, pgServer *node)
txtPort->SetValue(NumToStr((long)settings->GetLastPort()));
if (!cbSSL->IsEmpty())
cbSSL->SetSelection(settings->GetLastSSL());
if (!cbSSLverify->IsEmpty())
cbSSLverify->SetSelection(settings->GetLastSSLverify());
txtUsername->SetValue(settings->GetLastUsername());

chkTryConnect->SetValue(true);
Expand All @@ -105,6 +109,7 @@ dlgServer::~dlgServer()
settings->SetLastDatabase(cbDatabase->GetValue());
settings->SetLastPort(StrToLong(txtPort->GetValue()));
settings->SetLastSSL(cbSSL->GetCurrentSelection());
settings->SetLastSSLverify(cbSSLverify->GetCurrentSelection());
settings->SetLastUsername(txtUsername->GetValue());
}
}
Expand Down Expand Up @@ -142,6 +147,7 @@ void dlgServer::OnOK(wxCommandEvent &ev)
}
server->iSetPort(StrToLong(txtPort->GetValue()));
server->iSetSSL(cbSSL->GetCurrentSelection());
server->iSetSSLverify(cbSSLverify->GetCurrentSelection());
server->iSetDatabase(cbDatabase->GetValue());
server->iSetUsername(txtUsername->GetValue());
server->iSetStorePwd(chkStorePwd->GetValue());
Expand Down Expand Up @@ -215,6 +221,7 @@ int dlgServer::GoNew()
int dlgServer::Go(bool modal)
{
cbSSL->Append(wxT(" "));
cbSSLverify->Append(wxT(" "));

#ifdef SSL
cbSSL->Append(_("require"));
Expand All @@ -225,6 +232,13 @@ int dlgServer::Go(bool modal)
cbSSL->Append(_("allow"));
cbSSL->Append(_("disable"));
}

if (pgConn::GetLibpqVersion() >= 8.4)
{
cbSSLverify->Append(_("Full verification"));
cbSSLverify->Append(_("Certificate only"));
cbSSLverify->Append(_("No verification"));
}
#endif

if (server)
Expand All @@ -235,6 +249,7 @@ int dlgServer::Go(bool modal)
txtService->SetValue(server->GetServiceID());
txtPort->SetValue(NumToStr((long)server->GetPort()));
cbSSL->SetSelection(server->GetSSL());
cbSSLverify->SetSelection(server->GetSSLverify());
cbDatabase->SetValue(server->GetDatabaseName());
txtUsername->SetValue(server->GetUsername());
chkStorePwd->SetValue(server->GetStorePwd());
Expand All @@ -250,6 +265,7 @@ int dlgServer::Go(bool modal)
cbDatabase->Disable();
txtPort->Disable();
cbSSL->Disable();
cbSSLverify->Disable();
txtUsername->Disable();
chkStorePwd->Disable();
}
Expand Down Expand Up @@ -282,7 +298,8 @@ pgObject *dlgServer::CreateObject(pgCollection *collection)
pgObject *obj=new pgServer(GetName(), txtDescription->GetValue(), cbDatabase->GetValue(),
txtUsername->GetValue(), StrToLong(txtPort->GetValue()),
chkTryConnect->GetValue() && chkStorePwd->GetValue(),
chkRestore->GetValue(), cbSSL->GetCurrentSelection(), txtColour->GetValue());
chkRestore->GetValue(), cbSSL->GetCurrentSelection(),
cbSSLverify->GetCurrentSelection(), txtColour->GetValue());

return obj;
}
Expand Down Expand Up @@ -320,6 +337,7 @@ void dlgServer::CheckChange()
|| cbDatabase->GetValue() != server->GetDatabaseName()
|| txtUsername->GetValue() != server->GetUsername()
|| cbSSL->GetCurrentSelection() != server->GetSSL()
|| cbSSLverify->GetCurrentSelection() != server->GetSSLverify()
|| chkStorePwd->GetValue() != server->GetStorePwd()
|| chkRestore->GetValue() != server->GetRestore()
|| txtDbRestriction->GetValue() != server->GetDbRestriction()
Expand All @@ -332,6 +350,7 @@ void dlgServer::CheckChange()
#else
bool isPipe = (name.IsEmpty() || name.StartsWith(wxT("/")));
cbSSL->Enable(!isPipe);
cbSSLverify->Enable(!isPipe);
#endif
CheckValid(enable, !txtDescription->GetValue().IsEmpty(), _("Please specify description."));
CheckValid(enable, StrToLong(txtPort->GetValue()) > 0, _("Please specify port."));
Expand Down
1 change: 1 addition & 0 deletions pgadmin/frm/frmBackup.cpp
Expand Up @@ -80,6 +80,7 @@ frmBackup::frmBackup(frmMain *form, pgObject *obj) : ExternProcessDialog(form)

// Pass the SSL mode via the environment
environment.Add(wxT("PGSSLMODE=") + object->GetServer()->GetConnection()->GetSslModeName());
environment.Add(wxT("PGSSLVERIFY=") + object->GetServer()->GetConnection()->GetSslVerifyModeName());

// Icon
SetIcon(wxIcon(backup_xpm));
Expand Down
5 changes: 3 additions & 2 deletions pgadmin/frm/frmBackupGlobals.cpp
Expand Up @@ -61,7 +61,8 @@ frmBackupGlobals::frmBackupGlobals(frmMain *form, pgObject *obj) : ExternProcess
environment.Add(wxT("PGPASSWORD=") + ((pgServer *)object)->GetPassword());

// Pass the SSL mode via the environment
environment.Add(wxT("PGSSLMODE=") + ((pgServer *)object)->GetConnection()->GetSslModeName());
environment.Add(wxT("PGSSLMODE=") + ((pgServer *)object)->GetConnection()->GetSslModeName());
environment.Add(wxT("PGSSLVERIFY=") + ((pgServer *)object)->GetConnection()->GetSslVerifyModeName());
}
else
{
Expand All @@ -70,7 +71,7 @@ frmBackupGlobals::frmBackupGlobals(frmMain *form, pgObject *obj) : ExternProcess

// Pass the SSL mode via the environment
environment.Add(wxT("PGSSLMODE=") + object->GetServer()->GetConnection()->GetSslModeName());

environment.Add(wxT("PGSSLVERIFY=") + object->GetServer()->GetConnection()->GetSslVerifyModeName());
}

// Icon
Expand Down
3 changes: 2 additions & 1 deletion pgadmin/frm/frmBackupServer.cpp
Expand Up @@ -59,7 +59,8 @@ frmBackupServer::frmBackupServer(frmMain *form, pgObject *obj) : ExternProcessDi
environment.Add(wxT("PGPASSWORD=") + ((pgServer *)object)->GetPassword());

// Pass the SSL mode via the environment
environment.Add(wxT("PGSSLMODE=") + ((pgServer *)object)->GetConnection()->GetSslModeName());
environment.Add(wxT("PGSSLMODE=") + ((pgServer *)object)->GetConnection()->GetSslModeName());
environment.Add(wxT("PGSSLVERIFY=") + ((pgServer *)object)->GetConnection()->GetSslVerifyModeName());

// Icon
SetIcon(wxIcon(backup_xpm));
Expand Down
1 change: 1 addition & 0 deletions pgadmin/frm/frmMain.cpp
Expand Up @@ -1049,6 +1049,7 @@ void frmMain::StoreServers()
settings->Write(key + wxT("DbRestriction"), server->GetDbRestriction());
settings->Write(key + wxT("Colour"), server->GetColour());
settings->Write(key + wxT("SSL"), server->GetSSL());
settings->Write(key + wxT("SSLverify"), server->GetSSLverify());

pgCollection *coll=browser->FindCollection(databaseFactory, server->GetId());
if (coll)
Expand Down
1 change: 1 addition & 0 deletions pgadmin/frm/frmRestore.cpp
Expand Up @@ -115,6 +115,7 @@ frmRestore::frmRestore(frmMain *_form, pgObject *obj) : ExternProcessDialog(form

// Pass the SSL mode via the environment
environment.Add(wxT("PGSSLMODE=") + server->GetConnection()->GetSslModeName());
environment.Add(wxT("PGSSLVERIFY=") + server->GetConnection()->GetSslVerifyModeName());

wxCommandEvent ev;
OnChangeName(ev);
Expand Down
6 changes: 4 additions & 2 deletions pgadmin/frm/plugins.cpp
Expand Up @@ -243,15 +243,17 @@ wxWindow *pluginUtilityFactory::StartDialog(frmMain *form, pgObject *obj)
wxSetEnv(wxT("PGPASSWORD"), obj->GetConnection()->GetPassword());

// Pass the SSL mode via the environment
wxSetEnv(wxT("PGSSLMODE"), obj->GetConnection()->GetSslModeName());
wxSetEnv(wxT("PGSSLMODE"), obj->GetConnection()->GetSslModeName());
wxSetEnv(wxT("PGSSLVERIFY"), obj->GetConnection()->GetSslVerifyModeName());
}
else
{
// Blank the rest
execCmd.Replace(wxT("$$HOSTNAME"), wxEmptyString);
execCmd.Replace(wxT("$$HOSTADDR"), wxEmptyString);
execCmd.Replace(wxT("$$PORT"), wxEmptyString);
execCmd.Replace(wxT("$$SSLMODE"), wxEmptyString);
execCmd.Replace(wxT("$$SSLMODE"), wxEmptyString);
execCmd.Replace(wxT("$$SSLVERIFY"), wxEmptyString);
execCmd.Replace(wxT("$$DATABASE"), wxEmptyString);
execCmd.Replace(wxT("$$USERNAME"), wxEmptyString);
execCmd.Replace(wxT("$$PASSWORD"), wxEmptyString);
Expand Down
6 changes: 4 additions & 2 deletions pgadmin/include/db/pgConn.h
Expand Up @@ -81,7 +81,7 @@ typedef struct pgError {
class pgConn
{
public:
pgConn(const wxString& server = wxT(""), const wxString& database = wxT(""), const wxString& username = wxT(""), const wxString& password = wxT(""), int port = 5432, int sslmode=0, OID oid=0);
pgConn(const wxString& server = wxT(""), const wxString& database = wxT(""), const wxString& username = wxT(""), const wxString& password = wxT(""), int port = 5432, int sslmode=0, int sslverify=0, OID oid=0);
~pgConn();

bool HasPrivilege(const wxString &objTyp, const wxString &objName, const wxString &priv);
Expand Down Expand Up @@ -117,7 +117,9 @@ class pgConn
wxString GetTTY() const { return wxString(PQtty(conn), *conv); }
wxString GetOptions() const { return wxString(PQoptions(conn), *conv); }
int GetSslMode() const { return save_sslmode; }
int GetSslVerifyMode() const { return save_sslverifymode;}
wxString GetSslModeName();
wxString GetSslVerifyModeName();
int GetBackendPID() const { return PQbackendPID(conn); }
int GetStatus() const;
int GetLastResultStatus() const { return lastResultStatus; }
Expand Down Expand Up @@ -172,7 +174,7 @@ class pgConn
wxString reservedNamespaces;

wxString save_server, save_database, save_username, save_password;
int save_port, save_sslmode;
int save_port, save_sslmode, save_sslverifymode;
OID save_oid;
};

Expand Down
1 change: 1 addition & 0 deletions pgadmin/include/debugger/dbgConnProp.h
Expand Up @@ -32,6 +32,7 @@ class dbgConnProp
wxString m_port; // Port number
wxString m_debugPort; // Port number for debugger connection
int m_sslMode; // SSL Mode
int m_sslVerify; // SSL Certificate Verify Mode
};

#endif
5 changes: 3 additions & 2 deletions pgadmin/include/debugger/dbgPgConn.h
Expand Up @@ -57,7 +57,8 @@ class dbgPgConn
const wxString &username = wxT( "" ),
const wxString &password = wxT( "" ),
const wxString &port = wxT( "5432" ),
int sslmode = 0 );
int sslmode = 0,
int sslverify = 0 );

dbgPgConn( frmDebugger *frame, const dbgConnProp & props, bool startThread = true );

Expand All @@ -83,7 +84,7 @@ class dbgPgConn

private:

void Init( const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode, bool startThread );
void Init( const wxString &server, const wxString &database, const wxString &userName, const wxString &password, const wxString &port, int sslmode, int sslverify, bool startThread );

PGconn *m_pgConn; // libpq connection handler
dbgPgThread *m_workerThread; // Worker thread (this thread interacts with the server)
Expand Down

0 comments on commit 8ec4339

Please sign in to comment.