~ubuntu-branches/ubuntu/utopic/pgadmin3/utopic-proposed

« back to all changes in this revision

Viewing changes to pgadmin/schema/pgServer.cpp

  • Committer: Package Import Robot
  • Author(s): Christoph Berg
  • Date: 2013-09-10 16:16:38 UTC
  • mfrom: (1.3.4)
  • Revision ID: package-import@ubuntu.com-20130910161638-wwup1q553ylww7dr
Tags: 1.18.0-1
* New upstream release.
* Don't install /usr/bin/png2c anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
//
3
3
// pgAdmin III - PostgreSQL Tools
4
4
//
5
 
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
 
5
// Copyright (C) 2002 - 2013, The pgAdmin Development Team
6
6
// This software is released under the PostgreSQL Licence
7
7
//
8
8
// pgServer.cpp - PostgreSQL Server
38
38
#include "frm/frmReport.h"
39
39
#include "dlg/dlgServer.h"
40
40
 
 
41
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
42
#include "utils/sshTunnel.h"
 
43
#endif
 
44
 
41
45
#define DEFAULT_PG_DATABASE wxT("postgres")
42
46
 
43
 
pgServer::pgServer(const wxString &newName, const wxString &newHostAddr, const wxString &newDescription, const wxString &newService, const wxString &newDatabase, const wxString &newUsername, int newPort, bool _storePwd, const wxString &newRolename, bool _restore, int _ssl, const wxString &_colour, const wxString &_group)
 
47
pgServer::pgServer(const wxString &newName, const wxString &newHostAddr, const wxString &newDescription, const wxString &newService,
 
48
                   const wxString &newDatabase, const wxString &newUsername, int newPort, bool _storePwd, const wxString &newRolename, bool _restore,
 
49
                   int _ssl, const wxString &_colour, const wxString &_group, bool _sshTunnel, const wxString &newTunnelHost, const wxString &newTunnelUserName,
 
50
                   bool _authModePwd, const wxString &newTunnelPassword, const wxString &newPublicKey, const wxString &newIdentity)
44
51
        : pgObject(serverFactory, newName)
45
52
{
46
53
        description = newDescription;
65
72
        restore = _restore;
66
73
        superUser = false;
67
74
        createPrivilege = false;
 
75
        sshTunnel = _sshTunnel;
 
76
 
 
77
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
78
        // SSH Tunnel
 
79
        tunnelObj = NULL;
 
80
        tunnelHost = newTunnelHost;
 
81
        tunnelUserName = newTunnelUserName;
 
82
        authModePwd = _authModePwd;
 
83
        tunnelPassword = newTunnelPassword;
 
84
        publicKeyFile = newPublicKey;
 
85
        identityFile = newIdentity;
 
86
#endif
 
87
 
68
88
#ifdef WIN32
69
89
        scmHandle = 0;
70
90
        serviceHandle = 0;
76
96
        if (conn)
77
97
                delete conn;
78
98
 
 
99
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
100
        if(tunnelObj)
 
101
        {
 
102
                if(tunnelObj->IsAlive())
 
103
                {
 
104
                        tunnelObj->Cleanup();
 
105
                }
 
106
                tunnelObj = NULL;
 
107
        }
 
108
#endif
 
109
 
79
110
#ifdef WIN32
80
111
        if (serviceHandle)
81
112
                CloseServiceHandle(serviceHandle);
188
219
                dbName = GetDatabaseName();
189
220
                oid = dbOid;
190
221
        }
191
 
        pgConn *conn = new pgConn(GetName(), service, hostaddr, dbName, username, password, port, rolename, ssl, oid, applicationname, sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
 
222
 
 
223
        pgConn *conn = NULL;
 
224
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
225
        if(sshTunnel)
 
226
        {
 
227
                conn = new pgConn(local_listenhost, service, hostaddr, dbName, username, password, local_listenport, rolename, ssl, oid, applicationname, sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
 
228
        }
 
229
        else
 
230
#endif
 
231
        {
 
232
                conn = new pgConn(GetName(), service, hostaddr, dbName, username, password, port, rolename, ssl, oid, applicationname, sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
 
233
        }
192
234
 
193
235
        if (conn && conn->GetStatus() != PGCONN_OK)
194
236
        {
215
257
 
216
258
bool pgServer::Disconnect(frmMain *form)
217
259
{
 
260
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
261
        if(tunnelObj)
 
262
        {
 
263
                if(tunnelObj->IsAlive())
 
264
                {
 
265
                        tunnelObj->Cleanup();
 
266
                }
 
267
                tunnelObj = NULL;
 
268
        }
 
269
#endif
 
270
 
218
271
        if (conn)
219
272
        {
220
273
                delete conn;
611
664
}
612
665
 
613
666
 
614
 
int pgServer::Connect(frmMain *form, bool askPassword, const wxString &pwd, bool forceStorePassword)
 
667
int pgServer::Connect(frmMain *form, bool askPassword, const wxString &pwd, bool forceStorePassword, bool askTunnelPassword)
615
668
{
616
669
        wxLogInfo(wxT("Attempting to create a connection object..."));
617
670
 
626
679
                }
627
680
                if (askPassword)
628
681
                {
629
 
                        if (!passwordValid || !GetPasswordIsStored() || !GetStorePwd())
 
682
                        if ((sshTunnel || !passwordValid || !GetPasswordIsStored() || !GetStorePwd()) && GetSSLCert() == wxEmptyString)
630
683
                        {
631
684
                                wxString txt;
632
685
                                txt.Printf(_("Please enter password for user %s\non server %s (%s)"), username.c_str(), description.c_str(), GetName().c_str());
633
 
                                dlgConnect dlg(form, txt, GetStorePwd());
 
686
                                dlgConnect *dlg = NULL;
 
687
                                // if sshTunnel is true then we have to hide 'Stored Password' option
 
688
                                if(sshTunnel)
 
689
                                        dlg = new dlgConnect(NULL, txt, false);
 
690
                                else
 
691
                                        dlg = new dlgConnect(form, txt, GetStorePwd());
634
692
 
635
 
                                switch (dlg.Go())
 
693
                                switch (dlg->Go())
636
694
                                {
637
695
                                        case wxID_OK:
638
696
                                                // Give the UI a chance to redraw
656
714
                                                return PGCONN_BAD;
657
715
                                }
658
716
 
659
 
                                iSetStorePwd(dlg.GetStorePwd());
660
 
                                password = dlg.GetPassword();
 
717
                                iSetStorePwd(dlg->GetStorePwd());
 
718
                                password = dlg->GetPassword();
661
719
                                storePassword = true;
 
720
                                if(dlg)
 
721
                                {
 
722
                                        delete dlg;
 
723
                                        dlg = NULL;
 
724
                                }
662
725
                        }
663
726
                }
664
727
                else
666
729
 
667
730
                form->StartMsg(_("Connecting to database"));
668
731
 
 
732
                wxString host;
 
733
                int iPort;
 
734
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
735
                if(sshTunnel)
 
736
                {
 
737
                        //Ask Tunnel Password
 
738
                        if(askTunnelPassword)
 
739
                        {
 
740
                                wxString txt;
 
741
                                if(GetAuthModePwd())
 
742
                                {
 
743
                                        txt.Printf(_("Please enter the SSH tunnel password for user %s\non server %s"), tunnelUserName.c_str(), tunnelHost.c_str());
 
744
                                }
 
745
                                else
 
746
                                {
 
747
                                        txt.Printf(_("Please enter the pass phrase for the identity file\n%s"), identityFile.c_str());
 
748
                                }
 
749
                                dlgConnect dlg(NULL, txt, false);
 
750
 
 
751
                                switch (dlg.Go())
 
752
                                {
 
753
                                        case wxID_OK:
 
754
                                                // Give the UI a chance to redraw
 
755
                                                wxSafeYield();
 
756
                                                wxMilliSleep(100);
 
757
                                                wxSafeYield();
 
758
                                                tunnelPassword = dlg.GetPassword();
 
759
                                                break;
 
760
                                        case wxID_CANCEL:
 
761
                                        case -1:
 
762
                                        default:
 
763
                                                // Give the UI a chance to redraw
 
764
                                                wxSafeYield();
 
765
                                                wxMilliSleep(100);
 
766
                                                wxSafeYield();
 
767
                                                form->EndMsg(false);
 
768
                                                return PGCONN_ABORTED;
 
769
                                }
 
770
                        }
 
771
                        // Create SSH Tunnel if required
 
772
                        if(!tunnelObj)
 
773
                        {
 
774
                                if(!createSSHTunnel())
 
775
                                {
 
776
                                        form->EndMsg(false);
 
777
                                        return PGCONN_SSHTUNNEL_ERROR;
 
778
                                }
 
779
                        }
 
780
 
 
781
                        host = local_listenhost;
 
782
                        iPort = local_listenport;
 
783
                }
 
784
                else
 
785
#endif
 
786
                {
 
787
                        host = GetName();
 
788
                        iPort = port;
 
789
                }
 
790
 
669
791
                if (database.IsEmpty())
670
792
                {
671
 
                        conn = new pgConn(GetName(), service, hostaddr, DEFAULT_PG_DATABASE, username, password, port, rolename, ssl, 0, appearanceFactory->GetLongAppName() + _(" - Browser"), sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
 
793
                        conn = new pgConn(host, service, hostaddr, DEFAULT_PG_DATABASE, username, password, iPort, rolename, ssl, 0, appearanceFactory->GetLongAppName() + _(" - Browser"), sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
672
794
                        if (conn->GetStatus() == PGCONN_OK)
673
795
                                database = DEFAULT_PG_DATABASE;
674
796
                        else if (conn->GetStatus() == PGCONN_BAD && conn->GetLastError().Find(
675
797
                                     wxT("database \"") DEFAULT_PG_DATABASE wxT("\" does not exist")) >= 0)
676
798
                        {
677
799
                                delete conn;
678
 
                                conn = new pgConn(GetName(), service, hostaddr, wxT("template1"), username, password, port, rolename, ssl, 0, appearanceFactory->GetLongAppName() + _(" - Browser"), sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
 
800
                                conn = new pgConn(host, service, hostaddr, wxT("template1"), username, password, iPort, rolename, ssl, 0, appearanceFactory->GetLongAppName() + _(" - Browser"), sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
679
801
                                if (conn && conn->GetStatus() == PGCONN_OK)
680
802
                                        database = wxT("template1");
681
803
                        }
682
804
                }
683
805
                else
684
806
                {
685
 
                        conn = new pgConn(GetName(), service, hostaddr, database, username, password, port, rolename, ssl, 0, appearanceFactory->GetLongAppName() + _(" - Browser"), sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
 
807
                        conn = new pgConn(host, service, hostaddr, database, username, password, iPort, rolename, ssl, 0, appearanceFactory->GetLongAppName() + _(" - Browser"), sslcert, sslkey, sslrootcert, sslcrl, sslcompression);
686
808
                        if (!conn)
687
809
                        {
688
810
                                form->EndMsg(false);
1008
1130
                                properties->AppendItem(_("Port"), wxEmptyString);
1009
1131
                        else
1010
1132
                                properties->AppendItem(_("Port"), (long)GetPort());
1011
 
#ifdef SSL
 
1133
#ifdef PG_SSL
1012
1134
                        if (GetConnected())
1013
1135
                        {
1014
1136
                                properties->AppendItem(_("Encryption"),
1057
1179
                properties->AppendItem(_("Username"), GetUsername());
1058
1180
                if (!GetRolename().IsEmpty())
1059
1181
                        properties->AppendItem(_("Default role"), GetRolename());
 
1182
 
1060
1183
                properties->AppendYesNoItem(_("Store password?"), GetStorePwd());
1061
1184
                properties->AppendYesNoItem(_("Restore environment?"), GetRestore());
1062
1185
                if (GetConnected())
1094
1217
 
1095
1218
                if (!GetDbRestriction().IsEmpty())
1096
1219
                        properties->AppendItem(_("DB restriction"), GetDbRestriction());
 
1220
 
 
1221
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
1222
                if(sshTunnel)
 
1223
                {
 
1224
                        properties->AppendItem(_("SSH tunneling?"), (sshTunnel ? _("Yes") : _("No")));
 
1225
                        properties->AppendItem(_("Tunnel host"), GetTunnelHost());
 
1226
                        properties->AppendItem(_("Tunnel username"), GetTunnelUserName());
 
1227
                        properties->AppendItem(_("Authentication mode"), (GetAuthModePwd() ? _("Password") : _("Identity file")));
 
1228
                        if(!GetAuthModePwd())
 
1229
                        {
 
1230
                                properties->AppendItem(_("Identity file path"), GetIdentityFile());
 
1231
                        }
 
1232
                }
 
1233
#endif
1097
1234
        }
1098
1235
 
1099
1236
        if(!GetConnected())
1105
1242
        }
1106
1243
}
1107
1244
 
 
1245
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
1246
 
 
1247
bool pgServer::createSSHTunnel()
 
1248
{
 
1249
        bool retVal = false;
 
1250
 
 
1251
        tunnelObj = new CSSHTunnelThread(tunnelHost, GetName(), port, tunnelUserName, tunnelPassword, publicKeyFile,
 
1252
                                         identityFile, authModePwd ? AUTH_PASSWORD : AUTH_PUBLICKEY);
 
1253
 
 
1254
        if(tunnelObj)
 
1255
        {
 
1256
                if(tunnelObj->Initialize())
 
1257
                {
 
1258
                        if ( tunnelObj->Create() != wxTHREAD_NO_ERROR )
 
1259
                        {
 
1260
                                delete tunnelObj;
 
1261
                                tunnelObj = NULL;
 
1262
                                wxLogError(_("SSH Error: Unable to create SSH Tunnling Thread"));
 
1263
                        }
 
1264
                        else
 
1265
                        {
 
1266
                                if (tunnelObj->Run() != wxTHREAD_NO_ERROR )
 
1267
                                {
 
1268
                                        delete tunnelObj;
 
1269
                                        tunnelObj = NULL;
 
1270
                                        wxLogError(_("SSH Error: Unable to start SSH Tunnling Thread"));
 
1271
                                }
 
1272
 
 
1273
                                SetLocalListenHost(tunnelObj->GetLocalListenIP());
 
1274
                                SetLocalListenPort(tunnelObj->GetLocalListenPort());
 
1275
                                retVal = true;
 
1276
                        }
 
1277
                }
 
1278
                else
 
1279
                {
 
1280
                        delete tunnelObj;
 
1281
                        tunnelObj = NULL;
 
1282
                }
 
1283
        }
 
1284
 
 
1285
        return retVal;
 
1286
}
 
1287
#endif
1108
1288
 
1109
1289
void pgServer::ShowStatistics(frmMain *form, ctlListView *statistics)
1110
1290
{
1216
1396
        return false;
1217
1397
}
1218
1398
 
1219
 
 
1220
1399
pgServerCollection::pgServerCollection(pgaFactory *factory)
1221
1400
        : pgCollection(factory)
1222
1401
{
1289
1468
        wxString key, servername, hostaddr, description, service, database, username, lastDatabase, lastSchema;
1290
1469
        wxString storePwd, rolename, restore, serviceID, discoveryID, dbRestriction, colour;
1291
1470
        wxString group, sslcert, sslkey, sslrootcert, sslcrl, sslcompression;
 
1471
 
 
1472
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
1473
        wxString sshTunnel, authModePwd, tunnelHost, tunnelUserName, tunnelPassword, publicKeyFile, identityFile;
 
1474
#endif
1292
1475
        pgServer *server = 0;
1293
1476
 
1294
1477
        wxArrayString discoveredServers;
1327
1510
                settings->Read(key + wxT("SSLRootCert"), &sslrootcert, wxEmptyString);
1328
1511
                settings->Read(key + wxT("SSLCrl"), &sslcrl, wxEmptyString);
1329
1512
                settings->Read(key + wxT("SSLCompression"), &sslcompression, wxT("true"));
1330
 
 
 
1513
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
1514
                settings->Read(key + wxT("SSHTunnel"), &sshTunnel, wxT("false"));
 
1515
                settings->Read(key + wxT("TunnelHost"), &tunnelHost, wxEmptyString);
 
1516
                settings->Read(key + wxT("TunnelUserName"), &tunnelUserName, wxEmptyString);
 
1517
                settings->Read(key + wxT("TunnelModePwd"), &authModePwd, wxT("true"));
 
1518
                settings->Read(key + wxT("PublicKeyFile"), &publicKeyFile, wxEmptyString);
 
1519
                settings->Read(key + wxT("IdentityFile"), &identityFile, wxEmptyString);
 
1520
#endif
1331
1521
                // Sanitize the colour
1332
1522
                colour = colour.Trim();
1333
1523
 
1350
1540
                }
1351
1541
 
1352
1542
                // SSL mode
1353
 
#ifdef SSL
 
1543
#ifdef PG_SSL
1354
1544
                settings->Read(key + wxT("SSL"), &ssl, 0);
1355
1545
#endif
1356
1546
 
1361
1551
                }
1362
1552
 
1363
1553
                // Add the Server node
1364
 
                server = new pgServer(servername, hostaddr, description, service, database, username, port, StrToBool(storePwd), rolename, StrToBool(restore), ssl);
 
1554
#if defined(HAVE_OPENSSL_CRYPTO) || defined(HAVE_GCRYPT)
 
1555
                server = new pgServer(servername, hostaddr, description, service, database, username, port, StrToBool(storePwd), rolename, StrToBool(restore), ssl,
 
1556
                                      colour, group, StrToBool(sshTunnel), tunnelHost, tunnelUserName, StrToBool(authModePwd), tunnelPassword, publicKeyFile, identityFile);
 
1557
#else
 
1558
                server = new pgServer(servername, hostaddr, description, service, database, username, port, StrToBool(storePwd), rolename, StrToBool(restore), ssl,
 
1559
                                      colour, group);
 
1560
#endif
1365
1561
                server->iSetLastDatabase(lastDatabase);
1366
1562
                server->iSetLastSchema(lastSchema);
1367
1563
                server->iSetService(service);
1369
1565
                server->iSetDiscoveryID(discoveryID);
1370
1566
                server->iSetDiscovered(false);
1371
1567
                server->iSetDbRestriction(dbRestriction);
1372
 
                server->iSetColour(colour);
1373
 
                server->iSetGroup(group);
1374
1568
                server->iSetServerIndex(loop);
1375
1569
                server->SetSSLCert(sslcert);
1376
1570
                server->SetSSLKey(sslkey);
1740
1934
 
1741
1935
                                break;
1742
1936
                        }
 
1937
                        case PGCONN_SSHTUNNEL_ERROR:
 
1938
                        {
 
1939
                                delete server;
 
1940
                                break;
 
1941
                        }
1743
1942
                        default:
1744
1943
                        {
1745
1944
                                wxLogInfo(__("pgServer object didn't initialise because the user aborted."));