summaryrefslogtreecommitdiffstats
path: root/src/sysinfo.cpp
blob: 4797a86d77a1baff93d8063bf208452ad4e8a22b (plain) (blame)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "sysinfo.h"

// ------------------------------------------------------------------------------------------------
/**
 * A empty constructor.
 */
SysInfo::SysInfo() {
}
// ------------------------------------------------------------------------------------------------
/**
 * A empty destructor.
 */
SysInfo::~SysInfo() {
}
// ------------------------------------------------------------------------------------------------
/**
 * This method returns system informations.
 *
 * This method returns system informations according to the parameter.
 * This method can be called from the JavascriptInterface class with the
 * method JavascriptInterface::getSysInfo(const QString& info).
 *
 * @param infoName
 *   Is of type QString. Defines which method will be called. Possible values are:
 *     - mac
 *     - ip
 *     - mbserial
 *     - usb
 *
 * @return QString
 *   the output of the called method or "info_error" if an error occurred
 *   (e. g. invalid parameter).
 *
 * @see JavascriptInterface::getSysInfo(const QString& info)
 */
const QString SysInfo::getInfo(const QString& infoName) {
   qxtLog->debug() << "[sysinfo] requested " << infoName;
   if (infoName == QString("mac"))
      return getMACAddress();
   else if (infoName == QString("ip"))
      return getIPAddress();
   else if (infoName == QString("all"))
      return getAllInfos();
   else if (infoName == QString("mbserial"))
      return getMainboardSerial();
   else if (infoName == QString("usb"))
      return getUsbVendorIdProductIdSerialNumber();
   else if (infoName == QString("json"))
      return getNames();
   /* unknown code */
   qxtLog->debug() << "[sysinfo] unknown requested";
   return "info_error";
}
// ------------------------------------------------------------------------------------------------
/**
 * This method returns the clients MAC-Address.
 *
 * This method returns the clients MAC-Address of the "eth0" interface.
 * The MAC-Address is used as part of the data to compute the
 * hardwarehash of the client machine. To call this method use the
 * SysInfo::getInfo(const QString& infoName) method with
 * the parameter "mac"
 *
 * @return QString
 *   the MAC-Address or "no_eth0" if an error occurred.
 *
 * @see fbgui::generatePOSTData()
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString SysInfo::getMACAddress() {
   // Returns MAC address of eth0 for now
   QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0"));
   if (!qni.isValid()) {
      qxtLog->debug() << "[sysinfo] MAC Address: No interface matching \"eth0\" found.";
      return "no_eth0";
   }
   //eth0_index = qni.index();
   return qni.hardwareAddress();
}
// ------------------------------------------------------------------------------------------------
/**
 * This method returns the clients IP-Address.
 *
 * This method returns the clients IP-Address of the "eth0" interface.
 * To call this method use the
 * SysInfo::getInfo(const QString& infoName) method with
 * the parameter "ip"
 *
 * @return QString
 *   the IP-Address or "ip_error" if an error occurred.
 *
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString SysInfo::getIPAddress() {
   // Again for eth0 only at the moment.
   // TODO: this doesn't quite work yet...
   QNetworkInterface qni = QNetworkInterface::interfaceFromName(QString("eth0"));
   QList<QHostAddress> addrlist = qni.allAddresses();
   // This is does not return the right IP atm...
   foreach(QHostAddress addr, addrlist)
      {
         if (addr.protocol() == QAbstractSocket::IPv4Protocol && addr != QHostAddress::LocalHost) {
            return addr.toString();
         }
      }
   // still here?
   qxtLog->debug() << "[sysinfo] IP Address: ip_error";
   return "ip_error";
}
// ------------------------------------------------------------------------------------------------
/**
 * just a test method for json.
 */
const QByteArray SysInfo::getNames() {

   QVariantMap foo;
   foo.insert("name", "foo");
   foo.insert("type", 123);

   QVariantMap fooo;
   fooo.insert("name", "boo");
   fooo.insert("type", 321);

   QVariantList jsonV;
   jsonV << foo << fooo;

   QJson::Serializer serializer;
   QByteArray json = serializer.serialize(jsonV);

   qxtLog->debug() << json;
   return json;

}
// ------------------------------------------------------------------------------------------------
/**
 * just a test method for json.
 */
QString SysInfo::getAllInfos() {
   QVariantMap infos;
   infos.insert("mac", getMACAddress());
   infos.insert("ip", getIPAddress());
   infos.insert("whoami", getScriptOutput("whoami"));
   //infos.insert("pwd", getScriptOutput("pwd"));

   //QJson::Serializer serializer;
   QByteArray json = serializer.serialize(infos);

   qxtLog->debug() << json;
   return json;
}
// ------------------------------------------------------------------------------------------------

/**
 * This method returns the Mainboard Serial Number.
 *
 * This method returns the Mainboard Serial Number. The mainboard serial
 * number is used as part of the data to compute the hardwarehash of the
 * client machine. To call this method use the
 * SysInfo::getInfo(const QString& infoName) method with
 * the parameter "mbserial"
 *
 * @return QString
 *   the mainboard serial or "mainboard_serial_error" if an error occurred.
 *
 * @see fbgui::generatePOSTData()
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString SysInfo::getMainboardSerial() {
   QString out = "";
   struct sysfs_class_device *class_device = sysfs_open_class_device("dmi", "id");
   struct dlist *attrlist = sysfs_get_classdev_attributes(class_device);
   struct sysfs_device *device = sysfs_get_classdev_device(class_device);

   if (attrlist != NULL) {
      struct sysfs_attribute *attr = NULL;
      dlist_for_each_data(attrlist, attr, struct sysfs_attribute) {
         QVariantMap a;
         if(QString(attr->name) == QString("board_serial")) {
            out = QString(attr->value);
         }
      }
      qxtLog->debug() << "[sysinfo] Mainboard Serial: " + out;
      return out;
   }
   qxtLog->debug()
         << "[sysinfo] Mainboard Serial: attrlist is null! return: mainboard_serial_error";
   sysfs_close_class_device(class_device);
   return "mainboard_serial_error";
}
// ------------------------------------------------------------------------------------------------
/**
 * This method returns inforamtions about connected usb devices.
 *
 * This method returns the inforamtions about connected usb devices
 * as a json formated string.
 * Those informations are:
 *   - the vendor
 *   - the vendorID
 *   - the product
 *   - the productID
 *   - the manufacturer
 *   - the serial number
 *   To call this method use the SysInfo::getInfo(const QString& infoName)
 *   method with the parameter "usb"
 *
 * @return QString
 *   all above described informations as a json formated string or "error"
 *   if an error occurred.
 *
 * @see SysInfo::getInfo(const QString& infoName)
 */
const QString SysInfo::getUsbVendorIdProductIdSerialNumber() {
   QString tag = "[sysinfo] Usb Serial:";
   QString out = "";
   QVariantList list;

   libusb_device **devs;
   libusb_context *ctx = NULL; //a libusb session
   ssize_t cnt; //holding number of devices in list
   int r = 1;
   r = libusb_init(&ctx);
   if (r < 0) {
      qxtLog->debug() << tag + "failed to initialise libusb";
      return "error";
   }
   cnt = libusb_get_device_list(ctx, &devs); //get the list of devices
   if (cnt < 0) {
      qxtLog->debug() << tag + "Get Device Error"; //there was an error
   }
   qxtLog->debug() << tag + cnt + " Devices in list."; //print total number of usb devices
   ssize_t i; //for iterating through the list#
   for (i = 0; i < cnt; i++) {
      //printdev(devs[i]); //print specs of this device
      QVariantMap infos;
      libusb_device *dev = devs[i];
      libusb_device_descriptor desc;
      int re = libusb_get_device_descriptor(dev, &desc);
      if (re < 0) {
         qxtLog->debug() << tag + "failed to get device descriptor";
         return "error";
      }
      infos.insert("vendorId", desc.idVendor);
      infos.insert("productId", desc.idProduct);
      unsigned char string[256];
      libusb_device_handle *handle;
      re = libusb_open(dev, &handle);
      if (re != 0) {
         qxtLog->debug() << tag + "failed to get handler / fail to open device";
         return "error";
      }
      re = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
      if (re < 0) {
         qxtLog->debug() << tag + "failed to get SerialNumber";
         return "error";
      }
      infos.insert("serialnumber", QString((const char *) string));
      re = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
      if (re < 0) {
         qxtLog->debug() << tag + "failed to get Product";
         return "error";
      }
      infos.insert("product", QString((const char *) string));
      re = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
      if (re < 0) {
         qxtLog->debug() << tag + "failed to get Product";
         return "error";
      }
      infos.insert("manuacturer", QString((const char *) string));

      list << infos;
      libusb_close(handle);
   }
   libusb_free_device_list(devs, 1); //free the list, unref the devices in it
   libusb_exit(ctx); //close the session

   QByteArray json = serializer.serialize(list);
   qxtLog->debug() << tag + "json object: " + json;
   return json;
}

// ------------------------------------------------------------------------------------------------
/**
 * This method returns the output of the provided script.
 *
 * This method returns the output of the provided script.
 * Script could be any command.
 * This method is not used so far.
 *
 * @param cmd
 *   Is of type QString. The command which will be executed
 *
 * @return QString
 *   output of the script.
 */
QString SysInfo::getScriptOutput(QString cmd) {
   QProcess *process = new QProcess();
   qxtLog->debug() << "[sysinfo] Script Output: try to open: " << cmd;
   process->start(cmd, QIODevice::ReadOnly);

   if (!process->waitForStarted())
      qxtLog->debug() << "[sysinfo] Script Output: process couldn't get opened";

   QString output;
   process->waitForFinished();

   QTextStream *txt_stream = new QTextStream(process);

   while (!txt_stream->atEnd()) {
      qxtLog->debug() << "[sysinfo] Script Output: read output: ";
      QString tmp_str = txt_stream->readLine();
      output += tmp_str;
      qxtLog->debug() << "[sysinfo] Script Output: " << tmp_str;
   }
   qxtLog->debug() << "[sysinfo] Script Output: process finished: ";
   return output;
}