-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_gen_timed.cpp
207 lines (178 loc) · 7.13 KB
/
key_gen_timed.cpp
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
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <ctime>
#include <string>
#include <algorithm>
// Sleeping
#include <chrono>
#include <thread>
#include "utils.hpp"
// https://www.cplusplus.com/reference/cstdlib/rand/
std::vector<std::vector<int>> read_day_times(std::string filename_allow_day_times)
{
std::vector<std::vector<int>> day_times;
std::optional<std::vector<std::string>> lines = stelijah::read_file(filename_allow_day_times);
if(!lines)
{
std::cout << "\n<< Warning: Times file [" << filename_allow_day_times << "] not found. Password can be acquired any time of the day.\n" << std::endl;
}
else
{
for (int i = 0; i < lines.value().size(); i++)
{
std::vector<int> day_times_current;
std::string line = lines.value()[i];
std::optional<std::vector<std::string>> splitted_day_times = stelijah::split(line, " ");
day_times_current.push_back(std::stoi(splitted_day_times.value()[1]));
day_times_current.push_back(std::stoi(splitted_day_times.value()[2]));
day_times.push_back(day_times_current);
}
}
// for (int i = 0; i < day_times.size(); i++)
// {
// std::cout << day_times[i][0] << ", " << day_times[i][1] << "\n";
// }
return day_times;
}
/* If date alone exists in file; push zero with it */
std::vector<std::vector<std::string>> read_banned_days(std::string filename_allow_days)
{
std::vector<std::vector<std::string>> banned_days;
std::optional<std::vector<std::string>> lines2 = stelijah::read_file(filename_allow_days);
if(!lines2)
{
std::cout << "\n<< Warning: Dates file [" << filename_allow_days << "] not found. No banned days.\n" << std::endl;
}
else
{
for (int i = 0; i < lines2.value().size(); i++)
{
std::string line = lines2.value()[i];
std::optional<std::vector<std::string>> splitted_ban = stelijah::split(line, " ");
if(splitted_ban)
{
if (splitted_ban.value().size() == 1)
{
splitted_ban.value().push_back("0");
}
banned_days.push_back(splitted_ban.value());
}
}
}
// std::cout << std::endl;
// for (int i = 0; i < banned_days.size(); i++)
// {
// std::cout << banned_days[i][0] << ", " << banned_days[i][1] << "\n";
// }
return banned_days;
}
int main(int argc, char* argv[])
{
std::string filename_allow_days = "/etc/password_allowance_dates";
std::string filename_allow_day_times = "/etc/password_allowance_time_of_day";
int default_sleep_time_minutes = 30;
int day_begin = 0;
int day_end = 0;
// 0. Print help menu:
if (argc >= 2)
{
for (int i = 0; i < argc; ++i)
{
if (stelijah::clean_string(argv[i]) == "-h" ||
stelijah::clean_string(argv[i]) == "--help")
{
std::cout << "\n=== About program ===" << std::endl
<< " Print sudo password to screen if:" << std::endl
<< " 1. Time is within [" << filename_allow_day_times << "]" << std::endl
<< " 2. Date is within [" << filename_allow_days << "]. Min wait time of [" << default_sleep_time_minutes << "] min." << std::endl;
return 0;
}
}
}
// 1. Read first file
std::vector<std::vector<int>> day_times = read_day_times(filename_allow_day_times);
// 2. Read second file
std::vector<std::vector<std::string>> banned_days = read_banned_days(filename_allow_days);
// 3. Calculate current time:
std::time_t tnow = std::time(0);
std::tm* date = std::localtime(&tnow);
//int minute_current = date->tm_min;
int hour_current = date->tm_hour;
int day_current = date->tm_wday; // 0 is Sunday, 6 is Saturday
int year_current = date->tm_year + 1900;
int month_current = date->tm_mon + 1;
int mday_current = date->tm_mday; // e.g. 31
// std::cout << "\n- current year = " << year_current << "\n";
// std::cout << "- current mon = " << month_current << "\n";
// std::cout << "- current mday = " << mday_current << "\n";
// std::cout << "- current hour_current = " << hour_current << "\n";
// std::cout << "- current day_current = " << day_current << "\n\n";
// 4. First check time of day:
bool is_pass_time_of_day = true;
int minimum_time = day_times[day_current][0];
int maximum_time = day_times[day_current][1];
// std::cout << "minimum_time = " << minimum_time << "\n";
// std::cout << "maximum_time = " << maximum_time << "\n\n";
if ((minimum_time == 0 && maximum_time == 0) ||
(hour_current < minimum_time || hour_current > maximum_time))
{
is_pass_time_of_day = false;
}
// 5. Then check the day:
bool day_filter = false;
stelijah::password my_password;
if (is_pass_time_of_day)
{
for (int i = 0; i < banned_days.size(); i++)
{
std::vector<std::string> parts = banned_days[i];
int num_year = std::stoi(parts[0].substr(0, 4));
int num_mon = std::stoi(parts[0].substr(4, 2));
int num_day = std::stoi(parts[0].substr(6, 2));
int waiting_minutes = std::stoi(parts[1]);
// std::cout << "year = " << num_year << '\n';
// std::cout << "mon = " << num_mon << '\n';
// std::cout << "day = " << num_day << '\n\n';
if (year_current == num_year)
{
if (month_current == num_mon)
{
if (mday_current == num_day)
{
if (waiting_minutes == 0)
{
default_sleep_time_minutes = 360;
}
else if (waiting_minutes >= default_sleep_time_minutes)
{
default_sleep_time_minutes = waiting_minutes;
}
day_filter = true;
//break;
}
}
}
}
}
if(is_pass_time_of_day)
{
//std::cout << "<< Time covered by the file [" << filename_allow_day_times << "]." << std::endl;
if (day_filter)
{
//std::cout << "<< Today is listed in [" << filename_allow_days << "]\n";
//std::cout << "<< You will need to wait for [" << default_sleep_time_minutes << "] minutes (" << default_sleep_time_minutes/60 << " hours).\n";
std::this_thread::sleep_for(std::chrono::minutes(default_sleep_time_minutes));
}
// else
// {
// std::cout << "<< Today not listed in [" << filename_allow_days << "]." << std::endl << std::endl;
// }
std::string PASSWORD = my_password.get_password();
std::cout << PASSWORD << std::endl;
return 1;
}
// std::cout << "<< Time not covered by the file [" << filename_allow_day_times << "].\n" << std::endl << std::endl;
return 0;
} /* main() */