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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. }
  112. (new Fl_Box(70, y, win->w()-70-10, 20, _("Password:")))
  113. ->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE);
  114. y += 20 + 5;
  115. passwd = new Fl_Secret_Input(70, y, win->w()-70-10, 25);
  116. y += 25 + 5;
  117. y += 5;
  118. button = new Fl_Return_Button(310, y, 90, 25, fl_ok);
  119. button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
  120. button->callback(button_cb, (void*)0);
  121. button = new Fl_Button(210, y, 90, 25, fl_cancel);
  122. button->align(FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
  123. button->callback(button_cb, (void*)1);
  124. button->shortcut(FL_Escape);
  125. y += 25 + 10;
  126. win->end();
  127. win->size(win->w(), y);
  128. win->set_modal();
  129. ret_val = -1;
  130. win->show();
  131. while (win->shown()) Fl::wait();
  132. if (ret_val == 0) {
  133. if (user)
  134. *user = strDup(username->value());
  135. *password = strDup(passwd->value());
  136. }
  137. delete win;
  138. if (ret_val != 0)
  139. throw rfb::Exception(_("Authentication cancelled"));
  140. }
  141. bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
  142. {
  143. char buffer[1024];
  144. if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
  145. return 0;
  146. // FLTK doesn't give us a flexible choice of the icon, so we ignore those
  147. // bits for now.
  148. fl_message_title(title);
  149. switch (flags & 0xf) {
  150. case M_OKCANCEL:
  151. return fl_choice("%s", NULL, fl_ok, fl_cancel, buffer) == 1;
  152. case M_YESNO:
  153. return fl_choice("%s", NULL, fl_yes, fl_no, buffer) == 1;
  154. case M_OK:
  155. default:
  156. if (((flags & 0xf0) == M_ICONERROR) ||
  157. ((flags & 0xf0) == M_ICONWARNING))
  158. fl_alert("%s", buffer);
  159. else
  160. fl_message("%s", buffer);
  161. return true;
  162. }
  163. return false;
  164. }