Skip to content

Commit

Permalink
Rework handling of pg_service.conf, rely on a newly created pg_servic…
Browse files Browse the repository at this point in the history
…e.conf in the app system directory, support SSL keys authentication
  • Loading branch information
nirvn committed Jan 13, 2025
1 parent 3cccd35 commit 7c5ac06
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
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

0 comments on commit 7c5ac06

Please sign in to comment.