blob: cf6893c88a0a2cc75399c4d2a309979c7d6dc7c3 [file] [log] [blame]
Pierre Ossman5156d5e2011-03-09 09:42:34 +00001/* 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 Åstrandc359f362011-08-23 12:04:46 +000019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
Pierre Ossman5156d5e2011-03-09 09:42:34 +000023#include <assert.h>
24#include <stdio.h>
25#include <string.h>
26
Pierre Ossman34771ae2011-05-17 12:42:51 +000027#include <FL/Fl.H>
Pierre Ossman5156d5e2011-03-09 09:42:34 +000028#include <FL/fl_ask.H>
Pierre Ossman34771ae2011-05-17 12:42:51 +000029#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 Ossman5156d5e2011-03-09 09:42:34 +000035
36#include <rfb/util.h>
37#include <rfb/Password.h>
38#include <rfb/Exception.h>
39
40#include "i18n.h"
Pierre Ossmanc628ba42011-05-23 12:21:21 +000041#include "fltk_layout.h"
Pierre Ossman5156d5e2011-03-09 09:42:34 +000042#include "parameters.h"
43#include "UserDialog.h"
44
45using namespace rfb;
46
Pierre Ossman34771ae2011-05-17 12:42:51 +000047static int ret_val = 0;
48
49static void button_cb(Fl_Widget *widget, void *val) {
50 ret_val = (fl_intptr_t)val;
51 widget->window()->hide();
52}
53
Pierre Ossman5156d5e2011-03-09 09:42:34 +000054UserDialog::UserDialog()
55{
56}
57
58UserDialog::~UserDialog()
59{
60}
61
62void 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 Åstrand8c15d122011-07-14 11:25:40 +000072 fp = fopen(passwordFileStr.buf, "rb");
Pierre Ossman5156d5e2011-03-09 09:42:34 +000073 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
Pierre Ossman10a48102017-09-01 09:24:43 +020085 Fl_Window *win;
86 Fl_Input *username;
87 Fl_Secret_Input *passwd;
88 Fl_Box *icon;
89 Fl_Button *button;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000090
Pierre Ossman10a48102017-09-01 09:24:43 +020091 int y;
Pierre Ossman5156d5e2011-03-09 09:42:34 +000092
Pierre Ossman10a48102017-09-01 09:24:43 +020093 win = new Fl_Window(410, 145, _("VNC authentication"));
Pierre Ossman34771ae2011-05-17 12:42:51 +000094 win->callback(button_cb,(void *)0);
Pierre Ossman5156d5e2011-03-09 09:42:34 +000095
Pierre Ossman10a48102017-09-01 09:24:43 +020096 y = 10;
Pierre Ossman34771ae2011-05-17 12:42:51 +000097
Pierre Ossman10a48102017-09-01 09:24:43 +020098 icon = new Fl_Box(10, 10, 50, 50, "?");
Pierre Ossman34771ae2011-05-17 12:42:51 +000099 icon->box(FL_UP_BOX);
100 icon->labelfont(FL_TIMES_BOLD);
101 icon->labelsize(34);
102 icon->color(FL_WHITE);
103 icon->labelcolor(FL_BLUE);
104
Pierre Ossman10a48102017-09-01 09:24:43 +0200105 y += 5;
Pierre Ossman34771ae2011-05-17 12:42:51 +0000106
Pierre Ossman10a48102017-09-01 09:24:43 +0200107 if (user) {
108 (new Fl_Box(70, y, win->w()-70-10, 20, _("Username:")))
109 ->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
110 y += 20 + 5;
111 username = new Fl_Input(70, y, win->w()-70-10, 25);
112 y += 25 + 5;
113 }
114
115 (new Fl_Box(70, y, win->w()-70-10, 20, _("Password:")))
116 ->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
117 y += 20 + 5;
118 passwd = new Fl_Secret_Input(70, y, win->w()-70-10, 25);
119 y += 25 + 5;
120
121 y += 5;
122
123 button = new Fl_Return_Button(310, y, 90, 25, fl_ok);
Pierre Ossman34771ae2011-05-17 12:42:51 +0000124 button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
125 button->callback(button_cb, (void*)0);
126
Pierre Ossman10a48102017-09-01 09:24:43 +0200127 button = new Fl_Button(210, y, 90, 25, fl_cancel);
Pierre Ossman34771ae2011-05-17 12:42:51 +0000128 button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
129 button->callback(button_cb, (void*)1);
130 button->shortcut(FL_Escape);
131
Pierre Ossman10a48102017-09-01 09:24:43 +0200132 y += 25 + 10;
133
Pierre Ossman34771ae2011-05-17 12:42:51 +0000134 win->end();
135
Pierre Ossman10a48102017-09-01 09:24:43 +0200136 win->size(win->w(), y);
137
Pierre Ossman34771ae2011-05-17 12:42:51 +0000138 win->set_modal();
139
140 ret_val = -1;
141
142 win->show();
143 while (win->shown()) Fl::wait();
144
145 if (ret_val == 0) {
Pierre Ossman10a48102017-09-01 09:24:43 +0200146 if (user)
147 *user = strDup(username->value());
Pierre Ossman34771ae2011-05-17 12:42:51 +0000148 *password = strDup(passwd->value());
Pierre Ossman34771ae2011-05-17 12:42:51 +0000149 }
150
151 delete win;
Pierre Ossman10a48102017-09-01 09:24:43 +0200152
153 if (ret_val != 0)
154 throw rfb::Exception(_("Authentication cancelled"));
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000155}
156
157bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
158{
Pierre Ossmanc628ba42011-05-23 12:21:21 +0000159 char buffer[1024];
160
161 if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
162 return 0;
163
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000164 // FLTK doesn't give us a flexible choice of the icon, so we ignore those
165 // bits for now.
166
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000167 fl_message_title(title);
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000168
169 switch (flags & 0xf) {
170 case M_OKCANCEL:
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000171 return fl_choice("%s", NULL, fl_ok, fl_cancel, buffer) == 1;
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000172 case M_YESNO:
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000173 return fl_choice("%s", NULL, fl_yes, fl_no, buffer) == 1;
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000174 case M_OK:
175 default:
176 if (((flags & 0xf0) == M_ICONERROR) ||
177 ((flags & 0xf0) == M_ICONWARNING))
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000178 fl_alert("%s", buffer);
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000179 else
Pierre Ossmanf52740e2012-04-25 15:43:56 +0000180 fl_message("%s", buffer);
Pierre Ossman20ae1c82011-05-17 11:43:47 +0000181 return true;
182 }
Pierre Ossman5156d5e2011-03-09 09:42:34 +0000183
184 return false;
185}