Skip to content

Commit 6f9798b

Browse files
authoredOct 31, 2017
Merge pull request #5477 from boundlessgeo/certs_not_removed_with_pki_postgis_on_win
[security] Set permission to certs to allow correct removing on win
2 parents ff83b9a + ab74991 commit 6f9798b

File tree

2 files changed

+52
-36
lines changed

2 files changed

+52
-36
lines changed
 

‎python/plugins/db_manager/db_plugins/postgis/connector.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def __init__(self, uri):
6363
try:
6464
self.connection = psycopg2.connect(expandedConnInfo)
6565
except self.connection_error_types() as e:
66+
# get credentials if cached or assking to the user no more than 3 times
6667
err = unicode(e)
6768
uri = self.uri()
6869
conninfo = uri.connectionInfo(False)
@@ -85,44 +86,13 @@ def __init__(self, uri):
8586
except self.connection_error_types() as e:
8687
if i == 2:
8788
raise ConnectionError(e)
88-
8989
err = unicode(e)
9090
finally:
91-
# remove certs (if any) of the expanded connectionInfo
92-
expandedUri = QgsDataSourceURI(newExpandedConnInfo)
93-
94-
sslCertFile = expandedUri.param("sslcert")
95-
if sslCertFile:
96-
sslCertFile = sslCertFile.replace("'", "")
97-
os.remove(sslCertFile)
98-
99-
sslKeyFile = expandedUri.param("sslkey")
100-
if sslKeyFile:
101-
sslKeyFile = sslKeyFile.replace("'", "")
102-
os.remove(sslKeyFile)
103-
104-
sslCAFile = expandedUri.param("sslrootcert")
105-
if sslCAFile:
106-
sslCAFile = sslCAFile.replace("'", "")
107-
os.remove(sslCAFile)
91+
# clear certs for each time trying to connect
92+
self._clearSslTempCertsIfAny(newExpandedConnInfo)
10893
finally:
109-
# remove certs (if any) of the expanded connectionInfo
110-
expandedUri = QgsDataSourceURI(expandedConnInfo)
111-
112-
sslCertFile = expandedUri.param("sslcert")
113-
if sslCertFile:
114-
sslCertFile = sslCertFile.replace("'", "")
115-
os.remove(sslCertFile)
116-
117-
sslKeyFile = expandedUri.param("sslkey")
118-
if sslKeyFile:
119-
sslKeyFile = sslKeyFile.replace("'", "")
120-
os.remove(sslKeyFile)
121-
122-
sslCAFile = expandedUri.param("sslrootcert")
123-
if sslCAFile:
124-
sslCAFile = sslCAFile.replace("'", "")
125-
os.remove(sslCAFile)
94+
# clear certs of the first connection try
95+
self._clearSslTempCertsIfAny(expandedConnInfo)
12696

12797
self.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
12898

@@ -138,6 +108,33 @@ def __init__(self, uri):
138108
def _connectionInfo(self):
139109
return str(self.uri().connectionInfo(True))
140110

111+
def _clearSslTempCertsIfAny(self, connectionInfo):
112+
# remove certs (if any) of the connectionInfo
113+
expandedUri = QgsDataSourceURI(connectionInfo)
114+
115+
def removeCert(certFile):
116+
certFile = certFile.replace("'", "")
117+
file = QFile(certFile)
118+
# set permission to allow removing on Win.
119+
# On linux and Mac if file is set with QFile::>ReadUser
120+
# does not create problem removin certs
121+
if not file.setPermissions(QFile.WriteOwner):
122+
raise Exception('Cannot change permissions on {}: error code: {}'.format(file.fileName(), file.error()))
123+
if not file.remove():
124+
raise Exception('Cannot remove {}: error code: {}'.format(file.fileName(), file.error()))
125+
126+
sslCertFile = expandedUri.param("sslcert")
127+
if sslCertFile:
128+
removeCert(sslCertFile)
129+
130+
sslKeyFile = expandedUri.param("sslkey")
131+
if sslKeyFile:
132+
removeCert(sslKeyFile)
133+
134+
sslCAFile = expandedUri.param("sslrootcert")
135+
if sslCAFile:
136+
removeCert(sslCAFile)
137+
141138
def _checkSpatial(self):
142139
""" check whether postgis_version is present in catalog """
143140
c = self._execute(None, u"SELECT COUNT(*) FROM pg_proc WHERE proname = 'postgis_version'")

‎src/providers/postgres/qgspostgresconn.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,26 @@ QgsPostgresConn::QgsPostgresConn( const QString& conninfo, bool readOnly, bool s
222222
{
223223
QString fileName = expandedUri.param( param );
224224
fileName.remove( "'" );
225-
QFile::remove( fileName );
225+
QFile file( fileName );
226+
// set minimal permission to allow removing on Win.
227+
// On linux and Mac if file is set with QFile::>ReadUser
228+
// does not create problem removin certs
229+
if ( !file.setPermissions( QFile::WriteOwner ) )
230+
{
231+
QString errorMsg = tr( "Cannot set WriteOwner permission to cert: %0 to allow removing it" ).arg( file.fileName() );
232+
PQfinish();
233+
QgsMessageLog::logMessage( tr( "Client security failure" ) + '\n' + errorMsg, tr( "PostGIS" ) );
234+
mRef = 0;
235+
return;
236+
}
237+
if ( !file.remove() )
238+
{
239+
QString errorMsg = tr( "Cannot remove cert: %0 to allow removing it" ).arg( file.fileName() );
240+
PQfinish();
241+
QgsMessageLog::logMessage( tr( "Client security failure" ) + '\n' + errorMsg, tr( "PostGIS" ) );
242+
mRef = 0;
243+
return;
244+
}
226245
}
227246
}
228247

0 commit comments

Comments
 (0)
Please sign in to comment.