-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirtywork.hpp
70 lines (64 loc) · 2.24 KB
/
dirtywork.hpp
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
// Author: Alexandros Nicolaides
// Purpose: Elevate application privileges(UNIX only), detect host OS, determine app dependencies' file paths
// File: dirtywork.hpp
// Dependencies: None
#ifndef DIRTYWORK_HPP
#define DIRTYWORK_HPP
#if defined(__linux__) || defined(__APPLE__)
#include <limits.h> /* PATH_MAX defined there */
#include <unistd.h> /* getuid(), getcwd() */
#include <cstdlib> /* exit, EXIT_FAILURE */
#elif _WIN32
#undef UNICODE
#include <windows.h>
#endif
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Dirtywork {
public:
Dirtywork(char ** argv) :argv(argv) {
#if defined(__linux__) || defined(__APPLE__)
hostsPath = UNIX_PATH;
elevateUnix();
curDir = static_cast<string>(getcwd(buff, PATH_MAX + 1));
#elif _WIN32
// determines system directory for Microsoft Windows
if (GetSystemDirectory(infoBuf, INFO_BUFFER_SIZE))
hostsPath = static_cast<string>(infoBuf) + "\\drivers\\etc\\hosts";
// determines current directory of the binary(.exe)
if (GetCurrentDirectory(_MAX_PATH, infoBuf))
curDir = static_cast<string>(infoBuf);
#else
cout << "\nYour OS is not currently supported.The program is about to terminate." << endl;
exit(EXIT_FAILURE);
#endif
}
#if defined(__linux__) || defined(__APPLE__)
bool isRoot() const { return (!getuid()); }
void elevateUnix() const {
// attempts to run bin file with sudo
if (!isRoot()) {
system(("sudo " + static_cast<string>(argv[0])).c_str());
std::exit(EXIT_SUCCESS);
}
else
cout << "\nSuccess, the program has root access!" << endl;
}
#endif
string getCurDir() const { return curDir; }
string getHostsPath() const { return hostsPath; }
private:
const string UNIX_PATH = "/etc/hosts";
string hostsPath, curDir;
#if defined(__linux__) || defined(__APPLE__)
char buff[PATH_MAX + 1];
#elif _WIN32
const static unsigned short INFO_BUFFER_SIZE = 32767;
char infoBuf[INFO_BUFFER_SIZE];
#endif
char ** argv;
};
#endif