-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_token.cpp
More file actions
419 lines (339 loc) · 13.2 KB
/
Copy pathset_token.cpp
File metadata and controls
419 lines (339 loc) · 13.2 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <filesystem>
#include "config.hpp"
#include "inifile.hpp"
namespace fs = std::filesystem;
void chmod777(const std::string& filePath) {
std::error_code ec; // To handle potential errors without exceptions
// Set the permissions to 777
fs::permissions(filePath,
fs::perms::owner_all |
fs::perms::group_all |
fs::perms::others_all,
fs::perm_options::replace,
ec);
if (ec) {
// If an error occurred, print the error message
std::cerr << "Error setting permissions: " << ec.message() << std::endl;
} else {
std::cout << "Permissions set to 777 for: " << filePath << std::endl;
}
}
std::string getAppPath(const std::string& appName){
// Get the current working directory
fs::path currentPath = fs::current_path();
// Navigate up to the parent of the current path, then to 'mypath'
fs::path basePath = currentPath.parent_path().parent_path();
// Form the path for ExecStart
fs::path execStartPath = basePath / "dist" / appName;
// Construct mypath string
std::string mypath = execStartPath.string();
return mypath;
}
void setupService(const std::string& apppath,const std::string& appdir,const std::string& configFilePath) {
// Get the current working directory
std::string apppathwithargs = apppath + " -f "+configFilePath;
fs::path appdirPath = appdir;
// Name of the service file
std::string serviceFilename = "pos.service";
fs::path serviceFilePath = appdirPath / serviceFilename;
// Create and open a text file
std::ofstream serviceFile(serviceFilePath);
// Check if the file stream is ready for writing
if (!serviceFile) {
std::cerr << "Failed to create " << serviceFilePath << std::endl;
return;
}//82e750bfd8246d90134cc359e6e59949e788f6ee5ed2282b758df646e30dff3c
// Write the contents of the systemd service file
serviceFile << "[Unit]\n";
serviceFile << "Description=POS application startup script\n\n";
serviceFile << "[Service]\n";
serviceFile << "EnvironmentFile=/home/ubuntu/pos/env/env.txt\n";
serviceFile << "Type=simple\n";
serviceFile << "User=ubuntu\n";
serviceFile << "ExecStart=" << apppathwithargs << "\n";
serviceFile << "Restart=no\n\n";
serviceFile << "[Install]\n";
serviceFile << "WantedBy=multi-user.target\n";
// Close the file
serviceFile.close();
// Copy the service file to /etc/systemd/system
std::string copyCmd = "sudo cp " + serviceFilePath.string() + " /etc/systemd/system/";
int result = system(copyCmd.c_str());
if (result != 0) {
std::cerr << "Failed to copy service file to /etc/systemd/system" << std::endl;
} else {
std::cout << "Service file installed successfully." << std::endl;
}
}
void grantRightsToApp(std::string appPath){
std::cout << "Grant app rights to" <<appPath<<std::endl;
// Copy the service file to /etc/systemd/system
std::string copyCmd = "sudo setcap 'cap_dac_override=eip' "+appPath;
int result = system(copyCmd.c_str());
if (result != 0) {
std::cerr << "Failed to give app rights to"<<appPath << std::endl;
} else {
std::cout << "App rights set successfully." << std::endl;
}
}
int removeTokenIfExists(const Config& appConfig){
// Define the file path
std::string filePath = appConfig.getPosDirectory()+"secrettoken.txt";
// Check if file exists and delete it if it does
if (fs::exists(filePath)) {
// Try to remove the file
bool removed = fs::remove(filePath);
if (removed) {
std::cout << "File deleted successfully." << std::endl;
} else {
std::cout << "Failed to delete the file." << std::endl;
}
} else {
std::cout << "File does not exist." << std::endl;
}
return 0;
}
int createFolder(std::string dir){
// Check if the directory exists
if (!fs::exists(dir)) {
// Create the directory
if (fs::create_directory(dir)) {
std::cout << "Directory created successfully." << std::endl;
return 0;
} else {
std::cout << "Failed to create directory." << std::endl;
return 1;
}
} else {
std::cout << "Directory already exists." << std::endl;
return 0;
}
}
// Function to prompt user and get a string
std::string getUserInput(const std::string& prompt) {
std::string input;
std::cout << prompt;
std::getline(std::cin, input); // Use getline to read the whole line
return input;
}
bool isFullPath(const std::string& path) {
// Check if path contains any directory delimiters
return path.find('/') != std::string::npos || path.find('\\') != std::string::npos;
}
std::string inputSecretToken(){
// Prompt for user input
std::string userString = getUserInput("Please enter your secret token: ");
return userString;
}
/**
* Saves a secret token to a file with restricted permissions.
*
* The function retrieves the secret token from user input,
* saves it to a designated file within the application's configuration directory,
* and sets the file permissions to restrict access to the owner only.
*
* @param appConfig The configuration object which includes the application's directory paths.
* @return int Returns 0 if the token is successfully saved, otherwise returns 1.
*/
int saveSecretToken(const Config& appConfig){
std::cout << "Check if token exists"<<std::endl;
removeTokenIfExists(appConfig);
// Define the file path
std::string filePath = appConfig.getPosDirectory()+"secrettoken.txt";
auto input=inputSecretToken();
// Open a file in write mode
std::ofstream outFile(filePath);
if (!outFile) {
std::cerr << "Error: Unable to open file at " << filePath << std::endl;
return 1;
}
// Write the secret token to the file
outFile << input << std::endl;
// Close the file
outFile.close();
// Set file permissions to read and write for owner only
chmod(filePath.c_str(), S_IRUSR | S_IWUSR);
chmod777(filePath);
std::cout << "Token saved to " << filePath << std::endl;
return 0;
}
int createPosFolder() {
std::string folderPath = "/root/pos";
bool exists=fs::exists(folderPath);
// Check if the directory exists
if (!fs::exists(folderPath)) {
std::cout << "createPosFolder folder not existing" << std::endl;
// Create the directory since it does not exist
if (fs::create_directory(folderPath)) {
std::cout << "Pos directory created successfully." << std::endl;
} else {
std::cout << "Failed to create pos directory." << std::endl;
}
} else {
std::cout << "Pos directory already exists." << std::endl;
}
return 0;
}
int createPosLogsFolder() {
std::string folderPath = "/home/ubuntu/pos/logs";
bool exists=fs::exists(folderPath);
// Check if the directory exists
if (!fs::exists(folderPath)) {
std::cout << "createPosFolder folder not existing" << std::endl;
// Create the directory since it does not exist
if (fs::create_directory(folderPath)) {
std::cout << "Pos directory created successfully." << std::endl;
} else {
std::cout << "Failed to create pos directory." << std::endl;
}
} else {
std::cout << "Pos directory already exists." << std::endl;
}
return 0;
}
int createPosEnvFolder() {
std::string folderPath = "/home/ubuntu/pos/env";
bool exists=fs::exists(folderPath);
// Check if the directory exists
if (!fs::exists(folderPath)) {
std::cout << "createPosFolder folder not existing" << std::endl;
// Create the directory since it does not exist
if (fs::create_directory(folderPath)) {
std::cout << "Env directory created successfully." << std::endl;
} else {
std::cout << "Failed to create env directory." << std::endl;
}
} else {
std::cout << "Env directory already exists." << std::endl;
}
return 0;
}
// Function to execute a systemd command
bool executeSystemdCommand(const std::string& command) {
int result = system(command.c_str());
if (result != 0) {
std::cerr << "Command failed: " << command << std::endl;
return false;
}
return true;
}
// Main function to reload systemd, enable and start a service, and then check its status
bool reloadSystemdService(const std::string& serviceName) {
// Sanitize the input to prevent injection attacks
for (char c : serviceName) {
if (!(isalnum(c) || c == '-' || c == '_')) {
std::cerr << "Invalid service name: " << serviceName << std::endl;
return false; // Return failure on invalid input
}
}
std::string reloadCmd = "sudo systemctl daemon-reload";
std::string enableCmd = "sudo systemctl enable " + serviceName;
if (!executeSystemdCommand(reloadCmd)) return false;
if (!executeSystemdCommand(enableCmd)) return false;
return true;
}
// Main function to reload systemd, enable and start a service, and then check its status
bool startSystemdService(const std::string& serviceName) {
// Sanitize the input to prevent injection attacks
for (char c : serviceName) {
if (!(isalnum(c) || c == '-' || c == '_')) {
std::cerr << "Invalid service name: " << serviceName << std::endl;
return false; // Return failure on invalid input
}
}
std::string startCmd = "sudo systemctl start " + serviceName;
std::string statusCmd = "sudo systemctl status " + serviceName + " --no-pager";
if (!executeSystemdCommand(startCmd)) return false;
// Execute the status command and directly output its result to the terminal
std::cout << "Current status of the service:" << std::endl;
return executeSystemdCommand(statusCmd);
}
bool createAndWriteFile(const std::string& path, const std::string& content) {
// Create an ofstream instance that will create the file if it doesn't exist
std::ofstream outputFile(path);
// Check if the file was opened successfully
if (!outputFile.is_open()) {
std::cerr << "Failed to open or create the file: " << path << std::endl;
return false;
}
// Write the content to the file
outputFile << content;
if (outputFile.fail()) {
std::cerr << "Failed to write to the file: " << path << std::endl;
outputFile.close();
return false;
}
// Close the file
outputFile.close();
return true;
}
/**
* Main entry point for the application which handles initializing configurations,
* saving a secret token, and setting up a service based on the application configuration.
*
* The main function performs the following steps:
* - Initializes the application configuration.
* - Retrieves a secret token from the user.
* - Saves the secret token securely.
* - Sets up the main service of the application.
*
* @param argc Number of command-line arguments.
* @param argv Array of command-line arguments.
* @return int Returns 0 upon successful completion of all tasks.
*/
int main(int argc, char* argv[]) {
std::cout << "SET TOKEN version 0.8"<<std::endl;
std::string configFilePath;
bool restartServer=true;
// Parse command-line arguments
if (argc==1){
std::cerr << "Usage: " << argv[0] << " -f <config_file_path>" << std::endl;
return 1;
}
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-f" && i + 1 < argc) { // Make sure we do not go out of bounds
configFilePath = argv[++i]; // Increment 'i' to skip the file path in the next loop iteration
}else if (arg == "--nostart") {
restartServer = false; // Set noStart flag
} else {
std::cerr << "Usage: " << argv[0] << " -f <config_file_path>" << std::endl;
return 1;
}
}
//search in pos folder if not a full abs path
if (!isFullPath(configFilePath)){
configFilePath="/home/ubuntu/pos/conf/"+configFilePath;
}
//read config ini file
auto [readConfigResult,configFile]=readIniFile(configFilePath);
if (readConfigResult>0){
std::cerr << "Config file not found at "<< configFilePath<<". Check within /home/ubuntu/pos/conf or try with an absolute path." << std::endl;
return 1;
}
std::cout << "Config file : "<<configFilePath<<std::endl;
std::cout << "Start server at the end : "<<restartServer<<std::endl;
std::cout << "Creating folders"<<std::endl;
createPosFolder();
createPosLogsFolder();
createPosEnvFolder();
createAndWriteFile("/home/ubuntu/pos/env/env.txt","");
Config appConfig;
std::string apppath=configFile.appDir+configFile.serverExecutable;
std::cout << "POS Server path : "<<apppath<<std::endl;
grantRightsToApp(apppath);
std::cout << "Ask Token"<<std::endl;
saveSecretToken(appConfig); ;
std::cout << "Saving service"<<std::endl;
setupService(apppath,configFile.appDir, configFilePath);
std::cout << "Reloading service manager"<<std::endl;
reloadSystemdService("pos");
if(restartServer){
startSystemdService("pos");
}
return 0;
}