This repository has been archived by the owner on Sep 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recall.cpp
213 lines (180 loc) · 5.58 KB
/
recall.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
208
209
210
211
212
213
/*
Recall – a shot at regenerating partially forgotten passphrases
Copyright © 2015 Repentinus <repentinus at fsfe plus recall dot org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <iostream>
#include <cstring>
#include <sstream>
#include <atomic>
#include <cerrno>
#include <memory>
#include <string>
#include <vector>
#include <set>
#include <sys/mman.h>
#include <signal.h>
#include <boost/program_options.hpp>
#include <boost/format.hpp>
#include <boost/locale.hpp>
using namespace std;
using namespace boost::locale;
using namespace boost::program_options;
#ifndef LC_PATH
#define LC_PATH "."
#endif
#ifndef LC_DOMAIN
#define LC_DOMAIN "Recall"
#endif
namespace {
atomic_int gather_input;
struct sigaction* default_handler;
struct sigaction ng_handler;
struct sigaction lg_handler;
}
unique_ptr <vector <set <string> > > get_input();
unique_ptr <vector <string> > obtain_matches(
vector <set <string> > const&, bool (*)(string const&, string const&), bool, string const&);
bool test(string const&, string const&);
extern "C" {
void next_group_handler(int);
void last_group_handler(int);
}
int main(int const argc, char const* const* const argv)
{
generator gen;
gen.add_messages_path(LC_PATH);
gen.add_messages_domain(LC_DOMAIN);
locale::global(gen(""));
cout.imbue(locale());
cout << translate(
"Recall – a shot at regenerating partially forgotten passphrases\n"
"Copyright © 2015 Repentinus <repentinus at fsfe plus recall dot org>\n"
"Licensed under GPLv2 <https://www.gnu.org/licenses/gpl-2.0.html>\n"
) << endl;
options_description cmd_description(translate("Recognized options"));
bool return_on_first = false;
string key_id;
cmd_description.add_options()
("help", gettext("produce this message"))
("return-on-first, 1", value<bool>(&return_on_first)->default_value(true), gettext("return on first match"))
("key-id", value<string>(&key_id)->default_value(""), gettext("key to be tested"))
;
variables_map vm;
store(command_line_parser(argc, argv).options(cmd_description).run(), vm);
notify(vm);
if (vm.count("help")) {
cout << cmd_description << endl;
return 0;
}
if (mlockall(MCL_FUTURE | MCL_CURRENT)) {
cerr << "TERMINAL:" << " " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
cout << "Gathering input…" << endl;
unique_ptr <vector <set <string> > > fragments = get_input();
cout << "Wrapping up…" << endl;
unique_ptr <vector <string> > matches = obtain_matches(*fragments, test, return_on_first, key_id);
cout << "The following matches were found:" << endl;
for (auto &x : *matches) {
cout << x << endl;
}
return 0;
}
void last_group_handler(int)
{
gather_input = 0;
sigaction(SIGINT, default_handler, nullptr);
cout << endl;
}
void next_group_handler(int)
{
gather_input = 1;
sigaction(SIGINT, &lg_handler, nullptr);
cout << endl;
}
unique_ptr <vector <set <string> > > get_input()
{
unique_ptr<vector <set <string> > > fragments = make_unique<vector <set <string> > >();
ng_handler.sa_handler = next_group_handler;
lg_handler.sa_handler = last_group_handler;
if (sigaction(SIGINT, &lg_handler, default_handler)) {
cerr << "TERMINAL:" << " " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
sigset_t sigterm;
sigemptyset(&sigterm);
sigaddset(&sigterm, SIGTERM);
gather_input = 1;
while (gather_input) {
if (gather_input == 1) {
fragments->push_back(set <string>());
}
string s;
getline(cin, s);
if (sigprocmask(SIG_BLOCK, &sigterm, nullptr)) {
cerr << "TERMINAL:" << " " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
if (cin.fail()) {
cin.clear();
} else if (s.size()) {
fragments->back().insert(s);
if (gather_input == 1) {
gather_input = 2;
sigaction(SIGINT, &ng_handler, nullptr);
}
}
if (sigprocmask(SIG_UNBLOCK, &sigterm, nullptr)) {
cerr << "TERMINAL:" << " " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
}
return fragments;
}
unique_ptr <vector <string> > obtain_matches(
vector <set <string> > const& fragments, bool (*test)(string const&, string const&), bool return_on_first, string const& key_id)
{
unique_ptr <vector <string> > matches = make_unique<vector <string> >();
vector <vector <set <string>::iterator> > S;
for (auto i = fragments.begin(); i != fragments.end(); ++i) {
if (!i->empty()) {
S.push_back(vector <set <string>::iterator> {i->begin(), i->begin(), i->end()});
}
}
while(S.size() && S.front()[0] != S.front()[2]) {
stringstream ss;
for (auto i = S.begin(); i != S.end(); ++i) {
ss << *((*i)[0]);
}
if (test(ss.str(), key_id)) {
matches->push_back(ss.str());
if (return_on_first) {
return matches;
}
}
auto i = --S.end();
for (++((*i)[0]); (*i)[0] == (*i)[2];) {
if (i == S.begin()) {
break;
}
(*i)[0] = (*i)[1];
++((*(--i))[0]);
}
}
return matches;
}
bool test(string const& s, string const& key_id)
{
string t = boost::str(boost::format("echo \"%1%\" | gpg -q --passphrase-fd 0 --output /dev/null --yes --local-user %2% --pinentry-mode loopback --sign /dev/null") % s % key_id);
return !system(t.c_str());
}