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 | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 35 | |
| 36 | #include <rfb/util.h> |
| 37 | #include <rfb/Password.h> |
| 38 | #include <rfb/Exception.h> |
| 39 | |
| 40 | #include "i18n.h" |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 41 | #include "fltk_layout.h" |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 42 | #include "parameters.h" |
| 43 | #include "UserDialog.h" |
| 44 | |
| 45 | using namespace rfb; |
| 46 | |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 47 | static int ret_val = 0; |
| 48 | |
| 49 | static void button_cb(Fl_Widget *widget, void *val) { |
| 50 | ret_val = (fl_intptr_t)val; |
| 51 | widget->window()->hide(); |
| 52 | } |
| 53 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 54 | UserDialog::UserDialog() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | UserDialog::~UserDialog() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | void UserDialog::getUserPasswd(char** user, char** password) |
| 63 | { |
| 64 | CharArray passwordFileStr(passwordFile.getData()); |
| 65 | |
| 66 | assert(password); |
| 67 | |
| 68 | if (!user && passwordFileStr.buf[0]) { |
| 69 | ObfuscatedPasswd obfPwd(256); |
| 70 | FILE* fp; |
| 71 | |
Peter Åstrand | 8c15d12 | 2011-07-14 11:25:40 +0000 | [diff] [blame] | 72 | fp = fopen(passwordFileStr.buf, "rb"); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 73 | if (!fp) |
| 74 | throw rfb::Exception(_("Opening password file failed")); |
| 75 | |
| 76 | obfPwd.length = fread(obfPwd.buf, 1, obfPwd.length, fp); |
| 77 | fclose(fp); |
| 78 | |
| 79 | PlainPasswd passwd(obfPwd); |
| 80 | *password = passwd.takeBuf(); |
| 81 | |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (!user) { |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 86 | fl_message_title(_("VNC authentication")); |
| 87 | *password = strDup(fl_password(_("Password:"), "")); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 88 | if (!*password) |
| 89 | throw rfb::Exception(_("Authentication cancelled")); |
| 90 | |
| 91 | return; |
| 92 | } |
| 93 | |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 94 | // Largely copied from FLTK so that we get the same look and feel |
| 95 | // as the simpler password input. |
| 96 | Fl_Window *win = new Fl_Window(410, 145, _("VNC authentication")); |
| 97 | win->callback(button_cb,(void *)0); |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 98 | |
Pierre Ossman | 34771ae | 2011-05-17 12:42:51 +0000 | [diff] [blame] | 99 | Fl_Input *username = new Fl_Input(70, 25, 300, 25, _("Username:")); |
| 100 | username->align(FL_ALIGN_TOP_LEFT); |
| 101 | |
| 102 | Fl_Secret_Input *passwd = new Fl_Secret_Input(70, 70, 300, 25, _("Password:")); |
| 103 | passwd->align(FL_ALIGN_TOP_LEFT); |
| 104 | |
| 105 | Fl_Box *icon = new Fl_Box(10, 10, 50, 50, "?"); |
| 106 | icon->box(FL_UP_BOX); |
| 107 | icon->labelfont(FL_TIMES_BOLD); |
| 108 | icon->labelsize(34); |
| 109 | icon->color(FL_WHITE); |
| 110 | icon->labelcolor(FL_BLUE); |
| 111 | |
| 112 | Fl_Button *button; |
| 113 | |
| 114 | button = new Fl_Return_Button(310, 110, 90, 25, fl_ok); |
| 115 | button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP); |
| 116 | button->callback(button_cb, (void*)0); |
| 117 | |
| 118 | button = new Fl_Button(210, 110, 90, 25, fl_cancel); |
| 119 | button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP); |
| 120 | button->callback(button_cb, (void*)1); |
| 121 | button->shortcut(FL_Escape); |
| 122 | |
| 123 | win->end(); |
| 124 | |
| 125 | win->set_modal(); |
| 126 | |
| 127 | ret_val = -1; |
| 128 | |
| 129 | win->show(); |
| 130 | while (win->shown()) Fl::wait(); |
| 131 | |
| 132 | if (ret_val == 0) { |
| 133 | *user = strDup(username->value()); |
| 134 | *password = strDup(passwd->value()); |
| 135 | } else { |
| 136 | *user = strDup(""); |
| 137 | *password = strDup(""); |
| 138 | } |
| 139 | |
| 140 | delete win; |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool UserDialog::showMsgBox(int flags, const char* title, const char* text) |
| 144 | { |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 145 | char buffer[1024]; |
| 146 | |
| 147 | if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer)) |
| 148 | return 0; |
| 149 | |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 150 | // FLTK doesn't give us a flexible choice of the icon, so we ignore those |
| 151 | // bits for now. |
| 152 | |
| 153 | // FIXME: Filter out % from input text |
| 154 | |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 155 | fl_message_title(title); |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 156 | |
| 157 | switch (flags & 0xf) { |
| 158 | case M_OKCANCEL: |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 159 | return fl_choice(buffer, NULL, fl_ok, fl_cancel) == 1; |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 160 | case M_YESNO: |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 161 | return fl_choice(buffer, NULL, fl_yes, fl_no) == 1; |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 162 | case M_OK: |
| 163 | default: |
| 164 | if (((flags & 0xf0) == M_ICONERROR) || |
| 165 | ((flags & 0xf0) == M_ICONWARNING)) |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 166 | fl_alert(buffer); |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 167 | else |
Pierre Ossman | c628ba4 | 2011-05-23 12:21:21 +0000 | [diff] [blame] | 168 | fl_message(buffer); |
Pierre Ossman | 20ae1c8 | 2011-05-17 11:43:47 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
Pierre Ossman | 5156d5e | 2011-03-09 09:42:34 +0000 | [diff] [blame] | 171 | |
| 172 | return false; |
| 173 | } |