1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
3  *   SPDX-FileCopyrightText: 2014 Kevin Kofler <kevin.kofler@chello.at>
4  *   SPDX-FileCopyrightText: 2016 Philip Müller <philm@manjaro.org>
5  *   SPDX-FileCopyrightText: 2017 Alf Gaida <agaida@siduction.org>
6  *   SPDX-FileCopyrightText: 2019-2020 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 #include "MachineIdJob.h"
14 #include "Workers.h"
15 
16 #include "utils/CalamaresUtilsSystem.h"
17 #include "utils/Logger.h"
18 #include "utils/Variant.h"
19 
20 #include "GlobalStorage.h"
21 #include "JobQueue.h"
22 
23 #include <QFile>
24 
MachineIdJob(QObject * parent)25 MachineIdJob::MachineIdJob( QObject* parent )
26     : Calamares::CppJob( parent )
27 {
28 }
29 
30 
~MachineIdJob()31 MachineIdJob::~MachineIdJob() {}
32 
33 
34 QString
prettyName() const35 MachineIdJob::prettyName() const
36 {
37     return tr( "Generate machine-id." );
38 }
39 
40 Calamares::JobResult
exec()41 MachineIdJob::exec()
42 {
43     QString root;
44 
45     Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
46     if ( gs && gs->contains( "rootMountPoint" ) )
47     {
48         root = gs->value( "rootMountPoint" ).toString();
49     }
50     else
51     {
52         cWarning() << "No *rootMountPoint* defined.";
53         return Calamares::JobResult::internalError( tr( "Configuration Error" ),
54                                                     tr( "No root mount point is set for MachineId." ),
55                                                     Calamares::JobResult::InvalidConfiguration );
56     }
57 
58     QString target_systemd_machineid_file = QStringLiteral( "/etc/machine-id" );
59     QString target_dbus_machineid_file = QStringLiteral( "/var/lib/dbus/machine-id" );
60 
61     const CalamaresUtils::System* system = CalamaresUtils::System::instance();
62 
63     // Clear existing files
64     for ( const auto& entropy_file : m_entropy_files )
65     {
66         system->removeTargetFile( entropy_file );
67     }
68     if ( m_dbus )
69     {
70         system->removeTargetFile( target_dbus_machineid_file );
71     }
72     if ( m_systemd )
73     {
74         system->removeTargetFile( target_systemd_machineid_file );
75     }
76 
77     //Create new files
78     for ( const auto& entropy_file : m_entropy_files )
79     {
80         if ( !CalamaresUtils::System::instance()->createTargetParentDirs( entropy_file ) )
81         {
82             return Calamares::JobResult::error(
83                 QObject::tr( "Directory not found" ),
84                 QObject::tr( "Could not create new random file <pre>%1</pre>." ).arg( entropy_file ) );
85         }
86         auto r = MachineId::createEntropy( m_entropy_copy ? MachineId::EntropyGeneration::CopyFromHost
87                                                           : MachineId::EntropyGeneration::New,
88                                            root,
89                                            entropy_file );
90         if ( !r )
91         {
92             return r;
93         }
94     }
95     if ( m_systemd )
96     {
97         if ( !system->createTargetParentDirs( target_systemd_machineid_file ) )
98         {
99             cWarning() << "Could not create systemd data-directory.";
100         }
101         auto r = MachineId::createSystemdMachineId( root, target_systemd_machineid_file );
102         if ( !r )
103         {
104             return r;
105         }
106     }
107     if ( m_dbus )
108     {
109         if ( !system->createTargetParentDirs( target_dbus_machineid_file ) )
110         {
111             cWarning() << "Could not create DBus data-directory.";
112         }
113         if ( m_dbus_symlink && QFile::exists( root + target_systemd_machineid_file ) )
114         {
115             auto r = MachineId::createDBusLink( root, target_dbus_machineid_file, target_systemd_machineid_file );
116             if ( !r )
117             {
118                 return r;
119             }
120         }
121         else
122         {
123             auto r = MachineId::createDBusMachineId( root, target_dbus_machineid_file );
124             if ( !r )
125             {
126                 return r;
127             }
128         }
129     }
130 
131     return Calamares::JobResult::ok();
132 }
133 
134 
135 void
setConfigurationMap(const QVariantMap & map)136 MachineIdJob::setConfigurationMap( const QVariantMap& map )
137 {
138     m_systemd = CalamaresUtils::getBool( map, "systemd", false );
139 
140     m_dbus = CalamaresUtils::getBool( map, "dbus", false );
141     if ( map.contains( "dbus-symlink" ) )
142     {
143         m_dbus_symlink = CalamaresUtils::getBool( map, "dbus-symlink", false );
144     }
145     else if ( map.contains( "symlink" ) )
146     {
147         m_dbus_symlink = CalamaresUtils::getBool( map, "symlink", false );
148         cWarning() << "MachineId: configuration setting *symlink* is deprecated, use *dbus-symlink*.";
149     }
150     // else it's still false from the constructor
151 
152     // ignore it, though, if dbus is false
153     m_dbus_symlink = m_dbus && m_dbus_symlink;
154 
155     m_entropy_copy = CalamaresUtils::getBool( map, "entropy-copy", false );
156     m_entropy_files = CalamaresUtils::getStringList( map, "entropy-files" );
157     if ( CalamaresUtils::getBool( map, "entropy", false ) )
158     {
159         cWarning() << "MachineId:: configuration setting *entropy* is deprecated, use *entropy-files* instead.";
160         m_entropy_files.append( QStringLiteral( "/var/lib/urandom/random-seed" ) );
161     }
162     m_entropy_files.removeDuplicates();
163 }
164 
165 CALAMARES_PLUGIN_FACTORY_DEFINITION( MachineIdJobFactory, registerPlugin< MachineIdJob >(); )
166