1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
3  *   SPDX-FileCopyrightText: 2014-2015 Teo Mrnjavac <teo@kde.org>
4  *   SPDX-FileCopyrightText: 2019 Gabriel Craciunescu <crazy@frugalware.org>
5  *   SPDX-FileCopyrightText: 2019 Dominic Hayes <ferenosdev@outlook.com>
6  *   SPDX-FileCopyrightText: 2017-2018 Adriaan de Groot <groot@kde.org>
7  *   SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  *   Calamares is Free Software: see the License-Identifier above.
10  *
11  *
12  */
13 
14 #include "Settings.h"
15 
16 #include "CalamaresConfig.h"
17 #include "utils/Dirs.h"
18 #include "utils/Logger.h"
19 #include "utils/Yaml.h"
20 
21 #include <QDir>
22 #include <QFile>
23 #include <QPair>
24 
25 static bool
hasValue(const YAML::Node & v)26 hasValue( const YAML::Node& v )
27 {
28     return v.IsDefined() && !v.IsNull();
29 }
30 
31 /** @brief Helper function to grab a QString out of the config, and to warn if not present. */
32 static QString
requireString(const YAML::Node & config,const char * key)33 requireString( const YAML::Node& config, const char* key )
34 {
35     auto v = config[ key ];
36     if ( hasValue( v ) )
37     {
38         return QString::fromStdString( v.as< std::string >() );
39     }
40     else
41     {
42         cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing.";
43         return QString();
44     }
45 }
46 
47 /** @brief Helper function to grab a bool out of the config, and to warn if not present. */
48 static bool
requireBool(const YAML::Node & config,const char * key,bool d)49 requireBool( const YAML::Node& config, const char* key, bool d )
50 {
51     auto v = config[ key ];
52     if ( hasValue( v ) )
53     {
54         return v.as< bool >();
55     }
56     else
57     {
58         cWarning() << Logger::SubEntry << "Required settings.conf key" << key << "is missing.";
59         return d;
60     }
61 }
62 
63 namespace Calamares
64 {
65 
InstanceDescription(const Calamares::ModuleSystem::InstanceKey & key)66 InstanceDescription::InstanceDescription( const Calamares::ModuleSystem::InstanceKey& key )
67     : m_instanceKey( key )
68     , m_weight( -1 )
69 {
70     if ( !isValid() )
71     {
72         m_weight = 0;
73     }
74     else
75     {
76         m_configFileName = key.module() + QStringLiteral( ".conf" );
77     }
78 }
79 
80 InstanceDescription
fromSettings(const QVariantMap & m)81 InstanceDescription::fromSettings( const QVariantMap& m )
82 {
83     InstanceDescription r(
84         Calamares::ModuleSystem::InstanceKey( m.value( "module" ).toString(), m.value( "id" ).toString() ) );
85     if ( r.isValid() )
86     {
87         if ( m.value( "weight" ).isValid() )
88         {
89             int w = qBound( 1, m.value( "weight" ).toInt(), 100 );
90             r.m_weight = w;
91         }
92 
93         QString c = m.value( "config" ).toString();
94         if ( !c.isEmpty() )
95         {
96             r.m_configFileName = c;
97         }
98     }
99     return r;
100 }
101 
102 Settings* Settings::s_instance = nullptr;
103 
104 Settings*
instance()105 Settings::instance()
106 {
107     if ( !s_instance )
108     {
109         cWarning() << "Getting nullptr Settings instance.";
110     }
111     return s_instance;
112 }
113 
114 static void
interpretModulesSearch(const bool debugMode,const QStringList & rawPaths,QStringList & output)115 interpretModulesSearch( const bool debugMode, const QStringList& rawPaths, QStringList& output )
116 {
117     for ( const auto& path : rawPaths )
118     {
119         if ( path == "local" )
120         {
121             // If we're running in debug mode, we assume we might also be
122             // running from the build dir, so we add a maximum priority
123             // module search path in the build dir.
124             if ( debugMode )
125             {
126                 QString buildDirModules
127                     = QDir::current().absolutePath() + QDir::separator() + "src" + QDir::separator() + "modules";
128                 if ( QDir( buildDirModules ).exists() )
129                 {
130                     output.append( buildDirModules );
131                 }
132             }
133 
134             // Install path is set in CalamaresAddPlugin.cmake
135             output.append( CalamaresUtils::systemLibDir().absolutePath() + QDir::separator() + "calamares"
136                            + QDir::separator() + "modules" );
137         }
138         else
139         {
140             QDir d( path );
141             if ( d.exists() && d.isReadable() )
142             {
143                 output.append( d.absolutePath() );
144             }
145             else
146             {
147                 cDebug() << Logger::SubEntry << "module-search entry non-existent" << path;
148             }
149         }
150     }
151 }
152 
153 static void
interpretInstances(const YAML::Node & node,Settings::InstanceDescriptionList & customInstances)154 interpretInstances( const YAML::Node& node, Settings::InstanceDescriptionList& customInstances )
155 {
156     // Parse the custom instances section
157     if ( node )
158     {
159         QVariant instancesV = CalamaresUtils::yamlToVariant( node ).toList();
160         if ( instancesV.type() == QVariant::List )
161         {
162             const auto instances = instancesV.toList();
163             for ( const QVariant& instancesVListItem : instances )
164             {
165                 if ( instancesVListItem.type() != QVariant::Map )
166                 {
167                     continue;
168                 }
169                 auto description = InstanceDescription::fromSettings( instancesVListItem.toMap() );
170                 if ( !description.isValid() )
171                 {
172                     cWarning() << "Invalid entry in *instances*" << instancesVListItem;
173                 }
174                 // Append it **anyway**, since this will bail out after Settings is constructed
175                 customInstances.append( description );
176             }
177         }
178     }
179 }
180 
181 static void
interpretSequence(const YAML::Node & node,Settings::ModuleSequence & moduleSequence)182 interpretSequence( const YAML::Node& node, Settings::ModuleSequence& moduleSequence )
183 {
184     // Parse the modules sequence section
185     if ( node )
186     {
187         QVariant sequenceV = CalamaresUtils::yamlToVariant( node );
188         if ( !( sequenceV.type() == QVariant::List ) )
189         {
190             throw YAML::Exception( YAML::Mark(), "sequence key does not have a list-value" );
191         }
192 
193         const auto sequence = sequenceV.toList();
194         for ( const QVariant& sequenceVListItem : sequence )
195         {
196             if ( sequenceVListItem.type() != QVariant::Map )
197             {
198                 continue;
199             }
200             QString thisActionS = sequenceVListItem.toMap().firstKey();
201             ModuleSystem::Action thisAction;
202             if ( thisActionS == "show" )
203             {
204                 thisAction = ModuleSystem::Action::Show;
205             }
206             else if ( thisActionS == "exec" )
207             {
208                 thisAction = ModuleSystem::Action::Exec;
209             }
210             else
211             {
212                 cDebug() << "Unknown action in *sequence*" << thisActionS;
213                 continue;
214             }
215 
216             QStringList thisActionRoster = sequenceVListItem.toMap().value( thisActionS ).toStringList();
217             Calamares::ModuleSystem::InstanceKeyList roster;
218             roster.reserve( thisActionRoster.count() );
219             for ( const auto& s : thisActionRoster )
220             {
221                 auto instanceKey = Calamares::ModuleSystem::InstanceKey::fromString( s );
222                 if ( !instanceKey.isValid() )
223                 {
224                     cWarning() << "Invalid instance in *sequence*" << s;
225                 }
226                 roster.append( instanceKey );
227             }
228             moduleSequence.append( qMakePair( thisAction, roster ) );
229         }
230     }
231     else
232     {
233         throw YAML::Exception( YAML::Mark(), "sequence key is missing" );
234     }
235 }
236 
Settings(bool debugMode)237 Settings::Settings( bool debugMode )
238     : QObject()
239     , m_debug( debugMode )
240     , m_doChroot( true )
241     , m_promptInstall( false )
242     , m_disableCancel( false )
243     , m_disableCancelDuringExec( false )
244 {
245     cWarning() << "Using bogus Calamares settings in"
246                << ( debugMode ? QStringLiteral( "debug" ) : QStringLiteral( "regular" ) ) << "mode";
247     s_instance = this;
248 }
249 
Settings(const QString & settingsFilePath,bool debugMode)250 Settings::Settings( const QString& settingsFilePath, bool debugMode )
251     : QObject()
252     , m_debug( debugMode )
253     , m_doChroot( true )
254     , m_promptInstall( false )
255     , m_disableCancel( false )
256     , m_disableCancelDuringExec( false )
257 {
258     cDebug() << "Using Calamares settings file at" << settingsFilePath;
259     QFile file( settingsFilePath );
260     if ( file.exists() && file.open( QFile::ReadOnly | QFile::Text ) )
261     {
262         setConfiguration( file.readAll(), file.fileName() );
263     }
264     else
265     {
266         cWarning() << "Cannot read settings file" << file.fileName();
267     }
268 
269     s_instance = this;
270 }
271 
272 void
reconcileInstancesAndSequence()273 Settings::reconcileInstancesAndSequence()
274 {
275     // Since moduleFinder captures targetKey by reference, we can
276     //   update targetKey to change what the finder lambda looks for.
277     Calamares::ModuleSystem::InstanceKey targetKey;
278     auto moduleFinder = [&targetKey]( const InstanceDescription& d ) { return d.isValid() && d.key() == targetKey; };
279 
280     // Check the sequence against the existing instances (which so far are only custom)
281     for ( const auto& step : m_modulesSequence )
282     {
283         for ( const auto& instanceKey : step.second )
284         {
285             targetKey = instanceKey;
286             const auto it = std::find_if( m_moduleInstances.constBegin(), m_moduleInstances.constEnd(), moduleFinder );
287             if ( it == m_moduleInstances.constEnd() )
288             {
289                 if ( instanceKey.isCustom() )
290                 {
291                     cWarning() << "Custom instance key" << instanceKey << "is not listed in the *instances*";
292                 }
293                 m_moduleInstances.append( InstanceDescription( instanceKey ) );
294             }
295         }
296     }
297 }
298 
299 void
setConfiguration(const QByteArray & ba,const QString & explainName)300 Settings::setConfiguration( const QByteArray& ba, const QString& explainName )
301 {
302     try
303     {
304         YAML::Node config = YAML::Load( ba.constData() );
305         Q_ASSERT( config.IsMap() );
306 
307         interpretModulesSearch(
308             debugMode(), CalamaresUtils::yamlToStringList( config[ "modules-search" ] ), m_modulesSearchPaths );
309         interpretInstances( config[ "instances" ], m_moduleInstances );
310         interpretSequence( config[ "sequence" ], m_modulesSequence );
311 
312         m_brandingComponentName = requireString( config, "branding" );
313         m_promptInstall = requireBool( config, "prompt-install", false );
314         m_doChroot = !requireBool( config, "dont-chroot", false );
315         m_isSetupMode = requireBool( config, "oem-setup", !m_doChroot );
316         m_disableCancel = requireBool( config, "disable-cancel", false );
317         m_disableCancelDuringExec = requireBool( config, "disable-cancel-during-exec", false );
318         m_hideBackAndNextDuringExec = requireBool( config, "hide-back-and-next-during-exec", false );
319         m_quitAtEnd = requireBool( config, "quit-at-end", false );
320 
321         reconcileInstancesAndSequence();
322     }
323     catch ( YAML::Exception& e )
324     {
325         CalamaresUtils::explainYamlException( e, ba, explainName );
326     }
327 }
328 
329 QStringList
modulesSearchPaths() const330 Settings::modulesSearchPaths() const
331 {
332     return m_modulesSearchPaths;
333 }
334 
335 
336 Settings::InstanceDescriptionList
moduleInstances() const337 Settings::moduleInstances() const
338 {
339     return m_moduleInstances;
340 }
341 
342 
343 Settings::ModuleSequence
modulesSequence() const344 Settings::modulesSequence() const
345 {
346     return m_modulesSequence;
347 }
348 
349 
350 QString
brandingComponentName() const351 Settings::brandingComponentName() const
352 {
353     return m_brandingComponentName;
354 }
355 
356 static QStringList
settingsFileCandidates(bool assumeBuilddir)357 settingsFileCandidates( bool assumeBuilddir )
358 {
359     static const char settings[] = "settings.conf";
360 
361     QStringList settingsPaths;
362     if ( CalamaresUtils::isAppDataDirOverridden() )
363     {
364         settingsPaths << CalamaresUtils::appDataDir().absoluteFilePath( settings );
365     }
366     else
367     {
368         if ( assumeBuilddir )
369         {
370             settingsPaths << QDir::current().absoluteFilePath( settings );
371         }
372         if ( CalamaresUtils::haveExtraDirs() )
373             for ( auto s : CalamaresUtils::extraConfigDirs() )
374             {
375                 settingsPaths << ( s + settings );
376             }
377         settingsPaths << CMAKE_INSTALL_FULL_SYSCONFDIR "/calamares/settings.conf";  // String concat
378         settingsPaths << CalamaresUtils::appDataDir().absoluteFilePath( settings );
379     }
380 
381     return settingsPaths;
382 }
383 
384 Settings*
init(bool debugMode)385 Settings::init( bool debugMode )
386 {
387     if ( s_instance )
388     {
389         cWarning() << "Calamares::Settings already created";
390         return s_instance;
391     }
392 
393     QStringList settingsFileCandidatesByPriority = settingsFileCandidates( debugMode );
394 
395     QFileInfo settingsFile;
396     bool found = false;
397 
398     foreach ( const QString& path, settingsFileCandidatesByPriority )
399     {
400         QFileInfo pathFi( path );
401         if ( pathFi.exists() && pathFi.isReadable() )
402         {
403             settingsFile = pathFi;
404             found = true;
405             break;
406         }
407     }
408 
409     if ( !found || !settingsFile.exists() || !settingsFile.isReadable() )
410     {
411         cError() << "Cowardly refusing to continue startup without settings."
412                  << Logger::DebugList( settingsFileCandidatesByPriority );
413         if ( CalamaresUtils::isAppDataDirOverridden() )
414         {
415             cError() << "FATAL: explicitly configured application data directory is missing settings.conf";
416         }
417         else
418         {
419             cError() << "FATAL: none of the expected configuration file paths exist.";
420         }
421         ::exit( EXIT_FAILURE );
422     }
423 
424     auto* settings = new Calamares::Settings( settingsFile.absoluteFilePath(), debugMode );  // Creates singleton
425     if ( settings->modulesSequence().count() < 1 )
426     {
427         cError() << "FATAL: no sequence set.";
428         ::exit( EXIT_FAILURE );
429     }
430 
431     return settings;
432 }
433 
434 Settings*
init(const QString & path)435 Settings::init( const QString& path )
436 {
437     if ( s_instance )
438     {
439         cWarning() << "Calamares::Settings already created";
440         return s_instance;
441     }
442     return new Calamares::Settings( path, true );
443 }
444 
445 bool
isValid() const446 Settings::isValid() const
447 {
448     if ( brandingComponentName().isEmpty() )
449     {
450         cWarning() << "No branding component is set";
451         return false;
452     }
453 
454     const auto invalidDescriptor = []( const InstanceDescription& d ) { return !d.isValid(); };
455     const auto invalidDescriptorIt
456         = std::find_if( m_moduleInstances.constBegin(), m_moduleInstances.constEnd(), invalidDescriptor );
457     if ( invalidDescriptorIt != m_moduleInstances.constEnd() )
458     {
459         cWarning() << "Invalid module instance in *instances* or *sequence*";
460         return false;
461     }
462 
463     return true;
464 }
465 
466 }  // namespace Calamares
467