Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework pg_service.conf handling to support SSL keys authentication #5855

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,60 @@ int main( int argc, char **argv )
delete[] newPaths;
}

#if defined( Q_OS_ANDROID ) || defined( Q_OS_IOS )
for ( const QString &dataDir : dataDirs )
{
const QFileInfo pgServiceFileInfo( QStringLiteral( "%1/pg_service.conf" ).arg( dataDir ) );
if ( pgServiceFileInfo.exists() && pgServiceFileInfo.isReadable() )
{
const QString systemLocalDataPath = platformUtils->systemLocalDataLocation( QString() );

QFile pgServiceFile( QStringLiteral( "%1/pg_service.conf" ).arg( dataDir ) );
pgServiceFile.open( QFile::ReadOnly | QFile::Text );
QTextStream textStream( &pgServiceFile );
QString psServiceFileContent = textStream.readAll();
pgServiceFile.close();

const QStringList keys = QStringList() << QStringLiteral( "sslrootcert" ) << QStringLiteral( "sslcert" ) << QStringLiteral( "sslkey" );
for ( const QString &key : keys )
{
const QRegularExpression rx( QStringLiteral( "%1=(.*)" ).arg( key ) );
QRegularExpressionMatchIterator matchIt = rx.globalMatch( psServiceFileContent );
while ( matchIt.hasNext() )
{
const QRegularExpressionMatch match = matchIt.next();
const QString fileName = match.captured( 1 ).trimmed();

// Check if the file is relative to the pg_service.conf, in which case copy to user-owned location, use absolute path, and change permissions
const QString filePath = QStringLiteral( "%1/%2" ).arg( dataDir, fileName );
const QFileInfo fileInfo( filePath );
if ( QFileInfo::exists( filePath ) )
{
const QString newFilePath = QStringLiteral( "%1/%2" ).arg( systemLocalDataPath, fileName );
if ( QFileInfo::exists( newFilePath ) )
{
QFile newFile( newFilePath );
newFile.remove();
}
QFile::copy( filePath, newFilePath );
QFile::setPermissions( newFilePath, QFileDevice::ReadOwner | QFileDevice::WriteOwner );
psServiceFileContent.replace( QStringLiteral( "%1=%2" ).arg( key, match.captured( 1 ) ), QStringLiteral( "%1=%2" ).arg( key, newFilePath ) );
}
}
}

const QString localPgServiceFileName = QStringLiteral( "%1/pg_service.conf" ).arg( systemLocalDataPath );
QFile localPgServiceFile( localPgServiceFileName );
localPgServiceFile.open( QFile::WriteOnly );
localPgServiceFile.write( psServiceFileContent.toUtf8() );
localPgServiceFile.close();

setenv( "PGSYSCONFDIR", systemLocalDataPath.toUtf8(), true );
break;
}
}
#endif

#if WITH_SENTRY
sentry_wrapper::install_message_handler();
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/core/platforms/platformutilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ QString PlatformUtilities::systemSharedDataLocation() const

QString PlatformUtilities::systemLocalDataLocation( const QString &subDir ) const
{
return QStandardPaths::writableLocation( QStandardPaths::AppDataLocation ) + '/' + subDir;
return QStandardPaths::writableLocation( QStandardPaths::AppDataLocation ) + ( !subDir.isEmpty() ? '/' + subDir : QString() );
}

bool PlatformUtilities::hasQgsProject() const
Expand Down
2 changes: 1 addition & 1 deletion src/core/platforms/platformutilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class QFIELD_CORE_EXPORT PlatformUtilities : public QObject
* this includes local qfieldcloud data or sample projects.
* A \a subDir is appended to the path.
*/
virtual QString systemLocalDataLocation( const QString &subDir ) const;
virtual QString systemLocalDataLocation( const QString &subDir = QString() ) const;

/**
* Returns TRUE is a project file has been provided and should be opened at launch.
Expand Down
12 changes: 0 additions & 12 deletions src/core/qgismobileapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,6 @@ QgisMobileapp::QgisMobileapp( QgsApplication *app, QObject *parent )

if ( !dataDirs.isEmpty() )
{
#if defined( Q_OS_ANDROID ) || defined( Q_OS_IOS )
for ( const QString &dataDir : dataDirs )
{
QFileInfo pgServiceFile( QStringLiteral( "%1/pg_service.conf" ).arg( dataDir ) );
if ( pgServiceFile.exists() && pgServiceFile.isReadable() )
{
setenv( "PGSYSCONFDIR", dataDir.toUtf8(), true );
break;
}
}
#endif

QgsApplication::instance()->authManager()->setPasswordHelperEnabled( false );
QgsApplication::instance()->authManager()->setMasterPassword( QString( "qfield" ) );
// import authentication method configurations
Expand Down
Loading