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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/Exception.h>
  34. #include <rfb/obfuscate.h>
  35. #include "fltk/layout.h"
  36. #include "fltk/util.h"
  37. #include "i18n.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, std::string* user,
  61. std::string* password)
  62. {
  63. const char *passwordFileName(passwordFile);
  64. assert(password);
  65. char *envUsername = getenv("VNC_USERNAME");
  66. char *envPassword = getenv("VNC_PASSWORD");
  67. if(user && envUsername && envPassword) {
  68. *user = envUsername;
  69. *password = envPassword;
  70. return;
  71. }
  72. if (!user && envPassword) {
  73. *password = envPassword;
  74. return;
  75. }
  76. if (!user && passwordFileName[0]) {
  77. std::vector<uint8_t> obfPwd(256);
  78. FILE* fp;
  79. fp = fopen(passwordFileName, "rb");
  80. if (!fp)
  81. throw rfb::Exception(_("Opening password file failed"));
  82. obfPwd.resize(fread(obfPwd.data(), 1, obfPwd.size(), fp));
  83. fclose(fp);
  84. *password = deobfuscate(obfPwd.data(), obfPwd.size());
  85. return;
  86. }
  87. Fl_Window *win;
  88. Fl_Box *banner;
  89. Fl_Input *username;
  90. Fl_Secret_Input *passwd;
  91. Fl_Box *icon;
  92. Fl_Button *button;
  93. int x, y;
  94. win = new Fl_Window(410, 0, _("VNC authentication"));
  95. win->callback(button_cb,(void *)0);
  96. banner = new Fl_Box(0, 0, win->w(), 20);
  97. banner->align(FL_ALIGN_CENTER|FL_ALIGN_INSIDE|FL_ALIGN_IMAGE_NEXT_TO_TEXT);
  98. banner->box(FL_FLAT_BOX);
  99. if (secure) {
  100. banner->label(_("This connection is secure"));
  101. banner->color(FL_GREEN);
  102. banner->image(secure_icon);
  103. } else {
  104. banner->label(_("This connection is not secure"));
  105. banner->color(FL_RED);
  106. banner->image(insecure_icon);
  107. }
  108. x = OUTER_MARGIN;
  109. y = banner->h() + OUTER_MARGIN;
  110. /* Mimic a fl_ask() box */
  111. icon = new Fl_Box(x, y, 50, 50, "?");
  112. icon->box(FL_UP_BOX);
  113. icon->labelfont(FL_TIMES_BOLD);
  114. icon->labelsize(34);
  115. icon->color(FL_WHITE);
  116. icon->labelcolor(FL_BLUE);
  117. x += icon->w() + INNER_MARGIN;
  118. y += INNER_MARGIN;
  119. if (user) {
  120. y += INPUT_LABEL_OFFSET;
  121. username = new Fl_Input(x, y, win->w()- x - OUTER_MARGIN,
  122. INPUT_HEIGHT, _("Username:"));
  123. username->align(FL_ALIGN_LEFT | FL_ALIGN_TOP);
  124. y += INPUT_HEIGHT + INNER_MARGIN;
  125. } else {
  126. /*
  127. * Compiler is not bright enough to understand that
  128. * username won't be used further down...
  129. */
  130. username = NULL;
  131. }
  132. y += INPUT_LABEL_OFFSET;
  133. passwd = new Fl_Secret_Input(x, y, win->w()- x - OUTER_MARGIN,
  134. INPUT_HEIGHT, _("Password:"));
  135. passwd->align(FL_ALIGN_LEFT | FL_ALIGN_TOP);
  136. y += INPUT_HEIGHT + INNER_MARGIN;
  137. x = win->w() - OUTER_MARGIN;
  138. y += OUTER_MARGIN - INNER_MARGIN;
  139. x -= BUTTON_WIDTH;
  140. button = new Fl_Return_Button(x, y, BUTTON_WIDTH,
  141. BUTTON_HEIGHT, fl_ok);
  142. button->callback(button_cb, (void*)0);
  143. x -= INNER_MARGIN;
  144. x -= BUTTON_WIDTH;
  145. button = new Fl_Button(x, y, BUTTON_WIDTH, BUTTON_HEIGHT, fl_cancel);
  146. button->callback(button_cb, (void*)1);
  147. button->shortcut(FL_Escape);
  148. x -= INNER_MARGIN;
  149. y += BUTTON_HEIGHT;
  150. y += OUTER_MARGIN;
  151. win->end();
  152. win->size(win->w(), y);
  153. win->set_modal();
  154. ret_val = -1;
  155. win->show();
  156. while (win->shown()) Fl::wait();
  157. if (ret_val == 0) {
  158. if (user)
  159. *user = username->value();
  160. *password = passwd->value();
  161. }
  162. delete win;
  163. if (ret_val != 0)
  164. throw rfb::Exception(_("Authentication cancelled"));
  165. }
  166. bool UserDialog::showMsgBox(int flags, const char* title, const char* text)
  167. {
  168. char buffer[1024];
  169. if (fltk_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
  170. return 0;
  171. // FLTK doesn't give us a flexible choice of the icon, so we ignore those
  172. // bits for now.
  173. fl_message_title(title);
  174. switch (flags & 0xf) {
  175. case M_OKCANCEL:
  176. return fl_choice("%s", NULL, fl_ok, fl_cancel, buffer) == 1;
  177. case M_YESNO:
  178. return fl_choice("%s", NULL, fl_yes, fl_no, buffer) == 1;
  179. case M_OK:
  180. default:
  181. if (((flags & 0xf0) == M_ICONERROR) ||
  182. ((flags & 0xf0) == M_ICONWARNING))
  183. fl_alert("%s", buffer);
  184. else
  185. fl_message("%s", buffer);
  186. return true;
  187. }
  188. return false;
  189. }