Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 1 | /* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
Peter Åstrand | c359f36 | 2011-08-23 12:04:46 +0000 | [diff] [blame] | 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 27 | #include <FL/Fl.H> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 28 | #include <FL/fl_ask.H> |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 29 | #include <FL/Fl_Window.H> |
| 30 | #include <FL/Fl_Box.H> |
| 31 | #include <FL/Fl_Input.H> |
| 32 | #include <FL/Fl_Secret_Input.H> |
| 33 | #include <FL/Fl_Button.H> |
| 34 | #include <FL/Fl_Return_Button.H> |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 35 | #include <FL/Fl_Pixmap.H> |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 36 | |
| 37 | #include <rfb/util.h> |
| 38 | #include <rfb/Password.h> |
| 39 | #include <rfb/Exception.h> |
| 40 | |
| 41 | #include "i18n.h" |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 42 | #include "fltk_layout.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 43 | #include "parameters.h" |
| 44 | #include "UserDialog.h" |
| 45 | |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 46 | /* xpm:s predate const, so they have invalid definitions */ |
| 47 | #pragma GCC diagnostic push |
| 48 | #pragma GCC diagnostic ignored "-Wwrite-strings" |
| 49 | #include "../media/secure.xpm" |
| 50 | #include "../media/insecure.xpm" |
| 51 | #pragma GCC diagnostic pop |
| 52 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 53 | using namespace rfb; |
| 54 | |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 55 | static Fl_Pixmap secure_icon(secure); |
| 56 | static Fl_Pixmap insecure_icon(insecure); |
| 57 | |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 58 | static int ret_val = 0; |
| 59 | |
| 60 | static void button_cb(Fl_Widget *widget, void *val) { |
| 61 | ret_val = (fl_intptr_t)val; |
| 62 | widget->window()->hide(); |
| 63 | } |
| 64 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 65 | UserDialog::UserDialog() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | UserDialog::~UserDialog() |
| 70 | { |
| 71 | } |
| 72 | |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 73 | void UserDialog::getUserPasswd(bool secure, char** user, char** password) |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 74 | { |
| 75 | CharArray passwordFileStr(passwordFile.getData()); |
| 76 | |
| 77 | assert(password); |
| 78 | |
| 79 | if (!user && passwordFileStr.buf[0]) { |
| 80 | ObfuscatedPasswd obfPwd(256); |
| 81 | FILE* fp; |
| 82 | |
Peter Åstrand | 8c15d12 | 2011-07-14 11:25:40 +0000 | [diff] [blame] | 83 | fp = fopen(passwordFileStr.buf, "rb"); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 84 | if (!fp) |
| 85 | throw rfb::Exception(_("Opening password file failed")); |
| 86 | |
| 87 | obfPwd.length = fread(obfPwd.buf, 1, obfPwd.length, fp); |
| 88 | fclose(fp); |
| 89 | |
| 90 | PlainPasswd passwd(obfPwd); |
| 91 | *password = passwd.takeBuf(); |
| 92 | |
| 93 | return; |
| 94 | } |
| 95 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 96 | Fl_Window *win; |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 97 | Fl_Box *banner; |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 98 | Fl_Input *username; |
| 99 | Fl_Secret_Input *passwd; |
| 100 | Fl_Box *icon; |
| 101 | Fl_Button *button; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 102 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 103 | int y; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 104 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 105 | win = new Fl_Window(410, 145, _("VNC authentication")); |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 106 | win->callback(button_cb,(void *)0); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 107 | |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 108 | banner = new Fl_Box(0, 0, win->w(), 20); |
| 109 | banner->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE|FL_ALIGN_IMAGE_NEXT_TO_TEXT); |
| 110 | banner->box(FL_FLAT_BOX); |
| 111 | if (secure) { |
| 112 | banner->label(_("This connection is secure")); |
| 113 | banner->color(FL_GREEN); |
| 114 | banner->image(secure_icon); |
| 115 | } else { |
| 116 | banner->label(_("This connection is not secure")); |
| 117 | banner->color(FL_RED); |
| 118 | banner->image(insecure_icon); |
| 119 | } |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 120 | |
Pierre Ossman | daf3d88 | 2017-09-01 11:14:35 +0200 | [diff] [blame] | 121 | y = 20 + 10; |
| 122 | |
| 123 | icon = new Fl_Box(10, y, 50, 50, "?"); |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 124 | icon->box(FL_UP_BOX); |
| 125 | icon->labelfont(FL_TIMES_BOLD); |
| 126 | icon->labelsize(34); |
| 127 | icon->color(FL_WHITE); |
| 128 | icon->labelcolor(FL_BLUE); |
| 129 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 130 | y += 5; |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 131 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 132 | if (user) { |
| 133 | (new Fl_Box(70, y, win->w()-70-10, 20, _("Username:"))) |
| 134 | ->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); |
| 135 | y += 20 + 5; |
| 136 | username = new Fl_Input(70, y, win->w()-70-10, 25); |
| 137 | y += 25 + 5; |
Pierre Ossman | 9c88e0d | 2018-09-13 12:30:30 +0200 | [diff] [blame] | 138 | } else { |
| 139 | /* |
| 140 | * Compiler is not bright enough to understand that |
| 141 | * username won't be used further down... |
| 142 | */ |
| 143 | username = NULL; |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | (new Fl_Box(70, y, win->w()-70-10, 20, _("Password:"))) |
| 147 | ->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); |
| 148 | y += 20 + 5; |
| 149 | passwd = new Fl_Secret_Input(70, y, win->w()-70-10, 25); |
| 150 | y += 25 + 5; |
| 151 | |
| 152 | y += 5; |
| 153 | |
| 154 | button = new Fl_Return_Button(310, y, 90, 25, fl_ok); |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 155 | button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP); |
| 156 | button->callback(button_cb, (void*)0); |
| 157 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 158 | button = new Fl_Button(210, y, 90, 25, fl_cancel); |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 159 | button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP); |
| 160 | button->callback(button_cb, (void*)1); |
| 161 | button->shortcut(FL_Escape); |
| 162 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 163 | y += 25 + 10; |
| 164 | |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 165 | win->end(); |
| 166 | |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 167 | win->size(win->w(), y); |
| 168 | |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 169 | win->set_modal(); |
| 170 | |
| 171 | ret_val = -1; |
| 172 | |
| 173 | win->show(); |
| 174 | while (win->shown()) Fl::wait(); |
| 175 | |
| 176 | if (ret_val == 0) { |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 177 | if (user) |
| 178 | *user = strDup(username->value()); |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 179 | *password = strDup(passwd->value()); |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | delete win; |
Pierre Ossman | 10a4810 | 2017-09-01 09:24:43 +0200 | [diff] [blame] | 183 | |
| 184 | if (ret_val != 0) |
| 185 | throw rfb::Exception(_("Authentication cancelled")); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool UserDialog::showMsgBox(int flags, const char* title, const char* text) |
| 189 | { |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 190 | char buffer[1024]; |
| 191 | |
| 192 | if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer)) |
| 193 | return 0; |
| 194 | |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 195 | // FLTK doesn't give us a flexible choice of the icon, so we ignore those |
| 196 | // bits for now. |
| 197 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 198 | fl_message_title(title); |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 199 | |
| 200 | switch (flags & 0xf) { |
| 201 | case M_OKCANCEL: |
Pierre Ossman | f52740e | 2012-04-25 15:43:56 +0000 | [diff] [blame] | 202 | return fl_choice("%s", NULL, fl_ok, fl_cancel, buffer) == 1; |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 203 | case M_YESNO: |
Pierre Ossman | f52740e | 2012-04-25 15:43:56 +0000 | [diff] [blame] | 204 | return fl_choice("%s", NULL, fl_yes, fl_no, buffer) == 1; |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 205 | case M_OK: |
| 206 | default: |
| 207 | if (((flags & 0xf0) == M_ICONERROR) || |
| 208 | ((flags & 0xf0) == M_ICONWARNING)) |
Pierre Ossman | f52740e | 2012-04-25 15:43:56 +0000 | [diff] [blame] | 209 | fl_alert("%s", buffer); |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 210 | else |
Pierre Ossman | f52740e | 2012-04-25 15:43:56 +0000 | [diff] [blame] | 211 | fl_message("%s", buffer); |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 212 | return true; |
| 213 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 214 | |
| 215 | return false; |
| 216 | } |