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
|
/*
main.cpp - A Fltk based dialog for PIN entry.
Copyright (C) 2016 Anatoly madRat L. Berenblit
Written by Anatoly madRat L. Berenblit <madrat-@users.noreply.github.com>.
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; either version 2 of the
License, or (at your option) any later version.
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.
SPDX-License-Identifier: GPL-2.0+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define PGMNAME (PACKAGE_NAME"-fltk")
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <assert.h>
#include "memory.h"
#include <memory>
#include <pinentry.h>
#ifdef FALLBACK_CURSES
#include <pinentry-curses.h>
#endif
#include <string>
#include <string.h>
#include <stdexcept>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/fl_ask.H>
#include "pinwindow.h"
#include "passwindow.h"
#include "qualitypasswindow.h"
#define CONFIRM_STRING "Confirm"
#define REPEAT_ERROR_STRING "Texts do not match"
#define OK_STRING "OK"
#define CANCEL_STRING "Cancel"
char *application = NULL;
static std::string escape_accel_utf8(const char *s)
{
std::string result;
if (NULL != s)
{
result.reserve(strlen(s));
for (const char *p = s; *p; ++p)
{
if ('&' == *p)
result.push_back(*p);
result.push_back(*p);
}
}
return result;
}
class cancel_exception
{
};
static int get_quality(const char *passwd, void *ptr)
{
if (NULL == passwd || 0 == *passwd)
return 0;
pinentry_t* pe = reinterpret_cast<pinentry_t*>(ptr);
return pinentry_inq_quality(*pe, passwd, strlen(passwd));
}
bool is_short(const char *str)
{
return fl_utf_nb_char(reinterpret_cast<const unsigned char*>(str), strlen(str)) < 16;
}
bool is_empty(const char *str)
{
return (NULL == str) || (0 == *str);
}
static int fltk_cmd_handler(pinentry_t pe)
{
int ret = -1;
try
{
// TODO: Add parent window to pinentry-fltk window
//if (pe->parent_wid){}
std::string title = !is_empty(pe->title)?pe->title:PGMNAME;
std::string ok = escape_accel_utf8(pe->ok?pe->ok:(pe->default_ok?pe->default_ok:OK_STRING));
std::string cancel = escape_accel_utf8(pe->cancel?pe->cancel:(pe->default_cancel?pe->default_cancel:CANCEL_STRING));
if (!!pe->pin) // password (or confirmation)
{
std::unique_ptr<PinWindow> window;
bool isSimple = (NULL == pe->quality_bar) && // pinenty.h: If this is not NULL ...
is_empty(pe->error) && is_empty(pe->description) &&
is_short(pe->prompt);
if (isSimple)
{
assert(NULL == pe->description);
window.reset(PinWindow::create());
window->prompt(pe->prompt);
}
else
{
PassWindow *pass = NULL;
if (pe->quality_bar) // pinenty.h: If this is not NULL ...
{
QualityPassWindow *p = QualityPassWindow::create(get_quality, &pe);
window.reset(p);
pass = p;
p->quality(pe->quality_bar);
}
else
{
pass = PassWindow::create();
window.reset(pass);
}
if (NULL == pe->description)
{
pass->description(pe->prompt);
pass->prompt(" ");
}
else
{
pass->description(pe->description);
pass->prompt(escape_accel_utf8(pe->prompt).c_str());
}
pass->description(pe->description);
pass->prompt(escape_accel_utf8(pe->prompt).c_str());
if (NULL != pe->error)
pass->error(pe->error);
}
window->ok(ok.c_str());
window->cancel(cancel.c_str());
window->title(title.c_str());
window->showModal((NULL != application)?1:0, &application);
if (NULL == window->passwd())
throw cancel_exception();
const std::string password = window->passwd();
window.reset();
if (pe->repeat_passphrase)
{
const char *dont_match = NULL;
do
{
if (NULL == dont_match && is_short(pe->repeat_passphrase))
{
window.reset(PinWindow::create());
window->prompt(escape_accel_utf8(pe->repeat_passphrase).c_str());
}
else
{
PassWindow *pass = PassWindow::create();
window.reset(pass);
pass->description(pe->repeat_passphrase);
pass->prompt(" ");
pass->error(dont_match);
}
window->ok(ok.c_str());
window->cancel(cancel.c_str());
window->title(title.c_str());
window->showModal();
if (NULL == window->passwd())
throw cancel_exception();
if (password == window->passwd())
{
pe->repeat_okay = 1;
ret = 1;
break;
}
else
{
dont_match = (NULL!=pe->repeat_error_string)? pe->repeat_error_string:REPEAT_ERROR_STRING;
}
} while (true);
}
else
ret = 1;
pinentry_setbufferlen(pe, password.size()+1);
if (pe->pin)
{
memcpy(pe->pin, password.c_str(), password.size()+1);
pe->result = password.size();
ret = password.size();
}
}
else
{
// Confirmation or Message Dialog title, desc
Fl_Window dummy(0,0, 1,1);
dummy.border(0);
dummy.show((NULL != application)?1:0, &application);
dummy.hide();
fl_message_title(title.c_str());
int result = -1;
const char *message = (NULL != pe->description)?pe->description:CONFIRM_STRING;
if (pe->one_button)
{
fl_ok = ok.c_str();
fl_message("%s", message);
result = 1; // OK
}
else if (pe->notok)
{
switch (fl_choice("%s", ok.c_str(), cancel.c_str(), pe->notok, message))
{
case 0: result = 1; break;
case 2: result = 0; break;
default:
case 1: result = -1;break;
}
}
else
{
switch (fl_choice("%s", ok.c_str(), cancel.c_str(), NULL, message))
{
case 0: result = 1; break;
default:
case 1: result = -1;break;
}
}
// cancel -> pe->canceled = true, 0
// ok/y -> 1
// no -> 0
if (-1 == result)
pe->canceled = true;
ret = (1 == result);
}
Fl::check();
}
catch (const cancel_exception&)
{
ret = -1;
}
catch (...)
{
ret = -1;
}
// do_touch_file(pe); only for NCURSES?
return ret;
}
pinentry_cmd_handler_t pinentry_cmd_handler = fltk_cmd_handler;
int main(int argc, char *argv[])
{
application = *argv;
pinentry_init(PGMNAME);
#ifdef FALLBACK_CURSES
if (!pinentry_have_display(argc, argv))
pinentry_cmd_handler = curses_cmd_handler;
else
#endif
{
//FLTK understood only -D (--display)
// and should be converted into -di[splay]
const static struct option long_options[] =
{
{"display", required_argument, 0, 'D' },
{NULL, no_argument, 0, 0 }
};
for (int i = 0; i < argc-1; ++i)
{
switch (getopt_long(argc-i, argv+i, "D:", long_options, NULL))
{
case 'D':
{
char* emul[] = {application, (char*)"-display", optarg};
Fl::args(3, emul);
i = argc;
break;
}
default:
break;
}
}
}
pinentry_parse_opts(argc, argv);
return pinentry_loop() ?EXIT_FAILURE:EXIT_SUCCESS;
}
|