Debian Codegraph @

src/service/bootmakerservice.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2016 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: GPL-3.0-only
4
5 #include "bootmakerservice.h"
6 #include "bootmakerservice_p.h"
7
8 #include <QDebug>
9 #include <QThread>
10 #include <QCoreApplication>
11 #include <QDBusContext>
12 #include <QDBusConnection>
13 #include <QDBusConnectionInterface>
14 #include <QStandardPaths>
15
16 #include <sys/types.h>
17 #include <dirent.h>
18 #include <errno.h>
19 #include <vector>
20 #include <string>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24
25 #include <backend/bootmaker.h>
26
27 #if defined (Q_OS_LINUX) || defined (Q_OS_UNIX) || defined (Q_OS_MAC)
28 #include <polkit-qt5-1/PolkitQt1/Authority>
29 #include <polkit-qt5-1/PolkitQt1/Subject>
30 #endif
31
32 const QString s_PolkitActionCreate = "com.deepin.bootmaker.create";
33 const QString s_PolkitActionReboot = "com.deepin.bootmaker.reboot";
34
35 /**
36 @brief Polkit action authorization check.
37 Use com.deepin.bootmaker.policy config file.
38 Default action id: "com.deepin.bootmaker"
39 @note Available on linux/unix/macos platform.
40 @return check passed.
41 */
42 bool checkAuthorization(qint64 pid, const QString &action)
43 {
44 #if defined (Q_OS_LINUX) || defined (Q_OS_UNIX) || defined (Q_OS_MAC)
45 PolkitQt1::Authority::Result ret = PolkitQt1::Authority::instance()->checkAuthorizationSync(
46 action,
47 PolkitQt1::UnixProcessSubject(pid),
48 PolkitQt1::Authority::AllowUserInteraction);
49
50 if (PolkitQt1::Authority::Yes == ret) {
51 return true;
52 } else {
53 qWarning() << qPrintable("Policy authorization check failed!");
54 return false;
55 }
56 #else
57 return true;
58 #endif
59 }
60
61 #if 0 // Not use now
62 int getProcIdByExeName(std::string execName)
63 {
64 int pid = -1;
65
66 // Open the /proc directory
67 DIR *dp = opendir("/proc");
68 if (dp != NULL) {
69 // Enumerate all entries in directory until process found
70 struct dirent *dirp;
71 while (pid < 0 && (dirp = readdir(dp))) {
72 // Skip non-numeric entries
73 int id = atoi(dirp->d_name);
74 if (id > 0) {
75 // Read contents of virtual /proc/{pid}/cmdline file
76 auto exeSymlinkPath = std::string("/proc/") + dirp->d_name + "/exe";
77 char *actualpath = realpath(exeSymlinkPath.c_str(), NULL);
78 if (actualpath) {
79 // Compare against requested process name
80 if (execName == actualpath) {
81 pid = id;
82 }
83 }
84 }
85 }
86 }
87
88 closedir(dp);
89
90 return pid;
91 }
92 #endif
93
94 static QString getProcIdExe(qint64 id)
95 {
96 QString execName;
97 if (id > 0) {
98 // Read contents of virtual /proc/{pid}/cmdline file
99 QString exeSymlinkPath = QString("/proc/%1/exe").arg(id);
100 char *actualpath = realpath(exeSymlinkPath.toStdString().c_str(), NULL);
101 execName = QString(actualpath);
102 }
103 return execName;
104 }
105
106 BootMakerService::BootMakerService(QObject *parent) :
107 QObject(parent), d_ptr(new BootMakerServicePrivate(this))
108 {
109 Q_D(BootMakerService);
110 d->bm = new BootMaker();
111 QThread *bmThread = new QThread();
112 d->bm->moveToThread(bmThread);
113 bmThread->start();
114
115 connect(d->bm, &BootMaker::removablePartitionsChanged,
116 this, [ = ](const QList<DeviceInfo> &addlist, const QList<DeviceInfo>& dellist) {
117 emit DeviceListChanged(deviceListToJson(addlist), deviceListToJson(dellist));
118 });
119 connect(d->bm, &BootMaker::finished,
120 this, &BootMakerService::Finished);
121 // connect(d->bm, &BootMaker::checkFileResult,
122 // this, &BootMakerService::CheckFileResult);
123 connect(d->bm, &BootMaker::reportProgress,
124 this, &BootMakerService::ReportProgress);
125 connect(d->bm, &BootMaker::reportProgress1,
126 this, &BootMakerService::ReportProgress1);
127 // connect(this, &BootMakerService::startInstall,
128 // d->bm, &BootMaker::install);
129
130 connect(d->bm, &BootMaker::finished,
131 this, [ = ](int errcode, const QString &) {
132 QThread::msleep(1000);
133 qApp->exit(errcode);
134 });
135
136 connect(this, &BootMakerService::s_StartBootMarker,
137 d->bm, &BootMaker::start);
138 }
139
140 BootMakerService::~BootMakerService()
141 {
142
143 }
144
145 void BootMakerService::Reboot()
146 {
147 Q_D(BootMakerService);
148 if (checkAuthorization(d->dbusCallerPid(), s_PolkitActionReboot))
149 d->bm->reboot();
150 }
151
152 void BootMakerService::Start()
153 {
154 Q_D(BootMakerService);
155 if (!d->checkCaller()) {
156 return;
157 }
158
159 emit s_StartBootMarker();
160 }
161
162 void BootMakerService::Stop()
163 {
164 Q_D(BootMakerService);
165 if (!d->checkCaller()) {
166 return;
167 }
168
169 qDebug() << "service exit by call Stop";
170 qApp->exit(0);
171 }
172
173 //!
174 //! \brief BootMakerService::DeviceList
175 //! \return
176 //! return json of devicelist
177 QString BootMakerService::DeviceList()
178 {
179 qDebug() << "BootMakerService DeviceList";
180 Q_D(BootMakerService);
181 if (!d->checkCaller()) {
182 return "";
183 }
184 return deviceListToJson(d->bm->deviceList());
185 }
186
187 bool BootMakerService::Install(const QString &image, const QString &device, const QString &partition, bool formatDevice)
188 {
189 Q_D(BootMakerService);
190 if (!d->checkCaller()) {
191 return false;
192 }
193
194 if (!d->disableCheck && !checkAuthorization(d->dbusCallerPid(), s_PolkitActionCreate)) {
195 return false;
196 }
197
198 qDebug() << "install image:" << image << " device:" << device << " partition:" << partition;
199 emit d->bm->startInstall(image, device, partition, formatDevice);
200 return true;
201 }
202
203 bool BootMakerService::CheckFile(const QString &filepath)
204 {
205 Q_D(BootMakerService);
206
207 // if (!d->checkCaller()) {
208 // return false;
209 // }
210 return d->bm->checkfile(filepath);
211 // emit d->bm->startCheckfile(filepath);
212 // return true;
213 }
214
215 bool BootMakerServicePrivate::checkCaller()
216 {
217 if (disableCheck) {
218 return true;
219 }
220
221 Q_Q(BootMakerService);
222 if (!q->calledFromDBus()) {
223 return false;
224 }
225
226 qint64 callerPid = dbusCallerPid();
227 QString callerExe = getProcIdExe(callerPid);
228 QString dbmExe = QStandardPaths::findExecutable("deepin-boot-maker", {"/usr/bin"});
229
230 qDebug() << "callerPid is: " << callerPid << "callerExe is:" << callerExe;
231
232 if (callerExe != dbmExe) {
233 qDebug() << QString("caller not authorized") ;
234 return false;
235 }
236 qDebug() << QString("caller authorized");
237
238 return true;
239 }
240
241 /**
242 @return DBus interface caller pid
243 If the call is not from dbus (from UT), return 0
244 */
245 qint64 BootMakerServicePrivate::dbusCallerPid()
246 {
247 Q_Q(BootMakerService);
248 if (!q->calledFromDBus()) {
249 return 0;
250 }
251
252 auto interface = q->connection().interface();
253 if (interface) {
254 return static_cast<qint64>(interface->servicePid(q->message().service()).value());
255 }
256
257 return 0;
258 }