File: roothelper.cpp

package info (click to toggle)
cmst 2023.03.14-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,608 kB
  • sloc: cpp: 6,999; xml: 142; makefile: 12
file content (170 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**************************** roothelper.cpp ***************************

Code for the root helper registered on DBus.  When registered the CMST
provisioning editor will communicate with object using signals.  As the
name implies roothelper is run as the root user, since we need to read
and write to /var/lib/connman. This program is started by DBus and the
configuration files necessary to make that happen are in the system dir.

Copyright (C) 2013-2023
by: Andrew J. Bibb
License: MIT 

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"),to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included 
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
***********************************************************************/  

# include <QtCore/QDebug>
# include <QtDBus/QDBusConnection>
# include <QDir>
# include <QFile>
# include <QFileInfo>

# include "./roothelper.h"

//  header files generated by qmake from the xml file created by qdbuscpp2xml
# include "roothelper_adaptor.h"
# include "roothelper_interface.h"

//  constructor
RootHelper::RootHelper(QObject* parent)
    : QObject(parent)
{
  //  Create RootHelperadaptor  
  new RoothelperAdaptor(this);  
  
  // Data members
  b_connected = false;
  
  return;
}  
    
///////////////////// Public Slots /////////////////////////////////////
//
// Slot to start the event loop
void RootHelper::startHelper()
{
    // Try to register a service on the system bus
  if (! QDBusConnection::systemBus().registerService("org.cmst.roothelper")) {
    qDebug() << tr("Failed to register service org.cmst.roothelper - there may be another instance running.");
    QCoreApplication::instance()->exit(1);
  }
  
  // Try to register an object on the system bus
    if (! QDBusConnection::systemBus().registerObject("/", this)) {
      qDebug() << tr("Failed to register roothelper object on the system bus.");
      QCoreApplication::instance()->exit(2);
  }

  // if we made it this far we have a connection and are registered on the system bus.
  b_connected = true;
 
  return;
}

//
// Slot to get a list of all files in /var/lib/conmann the were created
// by CMST.  These files will end in .cmst.config
QStringList RootHelper::getFileList(const QString& path)
{ 
  // make sure the path is allowed
  if (! pathAllowed(path) ) return QStringList();
	
  // variables
  QDir dir = QDir(path);
  QStringList filters;
  filters << "*.cmst.config";

  // get a list of all .cmst.config files and return it
  return dir.entryList(filters, QDir::Files, QDir::Name);
}

//
// Slot to read a file from disk
QString RootHelper::readFile(const QString& path, const QString& fn)
{ 
  // make sure the path is allowed
  if (! pathAllowed(path) ) return QString();
	
  // open the file for reading  
  QFile infile(QString(path + "/%1.cmst.config").arg(sanitizeInput(fn)) ); 
  if (! infile.open(QIODevice::ReadOnly | QIODevice::Text))
    return QString();

  // read the file into a QByteArray
  QByteArray ba = infile.readAll();
  
  // cleanup and exit
  infile.close();
  return QString(ba); 
}

//
// Slot to delete a disk file
bool RootHelper::deleteFile(const QString& path, const QString& fn)
{
  // make sure the path is allowed
  if (! pathAllowed(path) ) return false;
	
  // delete the file and emit a signal with the result
  return QFile::remove(QString(path + "/%1.cmst.config").arg(sanitizeInput(fn)) );
}

//
// Slot to write the file to disk
qint64 RootHelper::saveFile(const QString& path, const QString& fn, const QString& data)
{ 
  // make sure the path is allowed
  if (! pathAllowed(path) ) return -1;
	
  // open the file for writing
  QFile outfile(QString(path + "/%1.cmst.config").arg(sanitizeInput(fn)) );
  if (! outfile.open(QIODevice::WriteOnly | QIODevice::Text))
    return 0;
  
  // write the file and emit a signal when done
  qint64 bytes = outfile.write(data.toLatin1() );
  
  // cleanup and exit
  outfile.close();
  return bytes;
}

/////////////////////////////////////////////// Private Functions //////////////////////////////////////////
//
// Function to take a file name, which may contain a path and extension, and return only the file name
QString RootHelper::sanitizeInput(QString instr)
{
  // return a null string if instr is empty
  if (instr.isEmpty() ) return QString();
  
  // extract the name and return it
  QFileInfo fi(instr);
  return fi.baseName();
}

//
// Function to determine if the path is allowed
bool RootHelper::pathAllowed(QString path)
{
  if (path == "/var/lib/connman") return true;
  else if (path == "/var/lib/connman-vpn") return true;
  
  return false;	
}