You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDialog.cxx 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <FL/Fl.H>
  25. #include <FL/fl_ask.H>
  26. #include <FL/Fl_Window.H>
  27. #include <FL/Fl_Box.H>
  28. #include <FL/Fl_Input.H>
  29. #include <FL/Fl_Secret_Input.H>
  30. #include <FL/Fl_Button.H>
  31. #include <FL/Fl_Return_Button.H>
  32. #include <FL/Fl_Pixmap.H>
  33. #include <rfb/util.h>
  34. #include <rfb/Password.h>
  35. #include <rfb/Exception.h>
  36. #include "i18n.h"
  37. #include "fltk_layout.h"
  38. #include "parameters.h"
  39. #include "UserDialog.h"
  40. /* xpm:s predate const, so they have invalid definitions */
  41. #pragma GCC diagnostic push
  42. #pragma GCC diagnostic ignored "-Wwrite-strings"
  43. #include "../media/secure.xpm"
  44. #include "../media/insecure.xpm"
  45. #pragma GCC diagnostic pop
  46. using namespace rfb;
  47. static Fl_Pixmap secure_icon(secure);
  48. static Fl_Pixmap insecure_icon(insecure);
  49. static int ret_val = 0;
  50. static void button_cb(Fl_Widget *widget, void *val) {
  51. ret_val = (fl_intptr_t)val;
  52. widget->window()->hide();
  53. }
  54. UserDialog::UserDialog()
  55. {
  56. }
  57. UserDialog::~UserDialog()
  58. {
  59. }
  60. void UserDialog::getUserPasswd(bool secure, char** user, char** password)
  61. {
  62. CharArray passwordFileStr(passwordFile.getData());
  63. assert(password);
  64. if (!user && passwordFileStr.buf[0]) {
  65. ObfuscatedPasswd obfPwd(256);
  66. FILE* fp;
  67. fp = fopen(passwordFileStr.buf, "rb");
  68. if (!fp)
  69. throw rfb::Exception(_("Opening password file failed"));
  70. obfPwd.length = fread(obfPwd.buf, 1, obfPwd.length, fp);
  71. fclose(fp);
  72. PlainPasswd passwd(obfPwd);
  73. *password = passwd.takeBuf();
  74. return;
  75. }
  76. Fl_Window *win;
  77. Fl_Box *banner;
  78. Fl_Input *username;
  79. Fl_Secret_Input *passwd;
  80. Fl_Box *icon;
  81. Fl_Button *button;
  82. int y;
  83. win = new Fl_Window(410, 145, _("VNC authentication"));
  84. win->callback(button_cb,(void *)0);
  85. banner = new Fl_Box(0, 0, win->w(), 20);
  86. banner->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE|FL_ALIGN_IMAGE_NEXT_TO_TEXT);
  87. banner->box(FL_FLAT_BOX);
  88. if (secure) {
  89. banner->label(_("This connection is secure"));
  90. banner->color(FL_GREEN);
  91. banner->image(secure_icon);
  92. } else {
  93. banner->label(_("This connection is not secure"));
  94. banner->color(FL_RED);
  95. banner->image(insecure_icon);
  96. }
  97. y = 20 + 10;
  98. icon = new Fl_Box(10, y, 50, 50, "?");
  99. 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. y += 5;
  105. if (user) {
  106. (new Fl_Box(70, y, win->w()-70-10, 20, _("Username:")))
  107. ->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  108. y += 20 + 5;
  109. username = new Fl_Input(70, y, win->w()-70-10, 25);
  110. y += 25 + 5;
  111. } else {
  112. /*
  113. * Compiler is not bright enough to understand that
  114. * username won't be used further down...
  115. */
  116. username = NULL;
  117. }
  118. (new Fl_Box(70, y, win->w()-70-10, 20, _("Password:")))
  119. ->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  120. y += 20 + 5;
  121. passwd = new Fl_Secret_Input(70, y, win->w()-70-10, 25);
  122. y += 25 + 5;
  123. y += 5;
  124. button = new Fl_Return_Button(310, y, 90, 25, fl_ok);
  125. button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
  126. button->callback(button_cb, (void*)0);
  127. button = new Fl_Button(210, y, 90, 25, fl_cancel);
  128. button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
  129. button->callback(button_cb, (void*)1);
  130. button->shortcut(FL_Escape);
  131. y += 25 + 10;
  132. win->end();
  133. win->size(win->w(), y);
  134. win->set_modal();
  135. ret_val = -1;
  136. win->show();
  137. while (win->shown()) Fl::wait();
  138. if (ret_val == 0) {
  139. if (user)
  140. *user = strDup(username->value());
  141. *password = strDup(passwd->value());
  142. }
  143. delete win;
  144. if (ret_val != 0)
  145. throw rfb::Exception(_("Authentication cancelled"));
  146. }
  147. bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
  148. {
  149. char buffer[1024];
  150. if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
  151. return 0;
  152. // FLTK doesn't give us a flexible choice of the icon, so we ignore those
  153. // bits for now.
  154. fl_message_title(title);
  155. switch (flags & 0xf) {
  156. case M_OKCANCEL:
  157. return fl_choice("%s", NULL, fl_ok, fl_cancel, buffer) == 1;
  158. case M_YESNO:
  159. return fl_choice("%s", NULL, fl_yes, fl_no, buffer) == 1;
  160. case M_OK:
  161. default:
  162. if (((flags & 0xf0) == M_ICONERROR) ||
  163. ((flags & 0xf0) == M_ICONWARNING))
  164. fl_alert("%s", buffer);
  165. else
  166. fl_message("%s", buffer);
  167. return true;
  168. }
  169. return false;
  170. }