1 /*
2  * Copyright (c) 2016-2018 Nitrokey UG
3  *
4  * This file is part of Nitrokey App.
5  *
6  * Nitrokey App is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * Nitrokey App is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Nitrokey App. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * SPDX-License-Identifier: GPL-3.0
20  */
21 
22 #include "systemutils.h"
23 #include <algorithm>
24 #include <cctype>
25 #include <fstream>
26 #include <iostream>
27 #include <vector>
28 using namespace std;
29 
30 namespace systemutils {
31 
isNitroDevice(string dev)32 bool isNitroDevice(string dev) {
33   // returns true if under /sys/block/dev/vendor is name nitro
34   const string nitroName = "NitroKey";
35   try {
36     string path = string("/sys/block/") + dev + string("/device/vendor");
37     ifstream vendorInfo(path.c_str());
38     string vendor;
39     vendorInfo >> vendor;
40     if (vendor.find(nitroName) != string::npos)
41       return true;
42   } catch (...) {
43   }
44   return false;
45 }
46 
getEncryptedDevice()47 string getEncryptedDevice() {
48   // return full encrypted device path
49   // return empty string if one or none devices are connected
50   // This approach would only work if encrypted device has letter after
51   // not encrypted
52   std::vector<string> devices, nitrodevices;
53 
54   ifstream partitionsFile("/proc/partitions");
55   string minor, major, size, device;
56   while (partitionsFile) {
57     partitionsFile >> major >> minor >> size >> device;
58     if (isdigit(*(device.end() - 1)))
59       continue;
60     devices.push_back(device);
61   }
62 
63   for (size_t i = 0; i < devices.size(); ++i) {
64     if (isNitroDevice(devices[i]))
65       nitrodevices.push_back(devices[i]);
66   }
67 
68   if (nitrodevices.size() <= 1)
69     return "";
70   sort(nitrodevices.begin(), nitrodevices.end());
71   return string("/dev/") + nitrodevices.back();
72 }
73 
getMntPoint(string)74 string getMntPoint(string) {
75   return string();
76 }
77 }
78