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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (C) 2017 Brian P. Hinz
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  16. * USA.
  17. */
  18. package com.tigervnc.vncviewer;
  19. import java.awt.*;
  20. import java.awt.event.*;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.util.*;
  27. import javax.swing.*;
  28. import javax.swing.event.*;
  29. import javax.swing.border.*;
  30. import javax.swing.plaf.LayerUI;
  31. import com.tigervnc.rfb.*;
  32. import com.tigervnc.rfb.Point;
  33. import com.tigervnc.rfb.Exception;
  34. import static com.tigervnc.vncviewer.Parameters.*;
  35. import static javax.swing.GroupLayout.*;
  36. import static javax.swing.JOptionPane.*;
  37. public class UserDialog implements UserPasswdGetter, UserMsgBox
  38. {
  39. private class MyLayerUI extends LayerUI {
  40. // Using a JButton for the "?" icon yields the best look, but there
  41. // does not seem to be any reasonable way to disable a JButton without
  42. // also changing the color. This wrapper just intercepts any mouse
  43. // click events so that the button just looks like an icon.
  44. @Override
  45. public void eventDispatched(AWTEvent e, JLayer l) {
  46. if (e instanceof InputEvent)
  47. ((InputEvent) e).consume();
  48. }
  49. @Override
  50. public void installUI(JComponent c) {
  51. super.installUI(c);
  52. if (c instanceof JLayer) {
  53. JLayer<?> layer = (JLayer<?>)c;
  54. layer.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK);
  55. }
  56. }
  57. @Override
  58. protected void processMouseEvent(MouseEvent e, JLayer l) {
  59. super.processMouseEvent(e, l);
  60. }
  61. }
  62. public final void getUserPasswd(boolean secure, StringBuffer user, StringBuffer password)
  63. {
  64. String passwordFileStr = passwordFile.getValue();
  65. if ((password == null) && sendLocalUsername.getValue()) {
  66. user.append((String)System.getProperties().get("user.name"));
  67. return;
  68. }
  69. if (user == null && !passwordFileStr.equals("")) {
  70. InputStream fp = null;
  71. try {
  72. fp = new FileInputStream(passwordFileStr);
  73. } catch(FileNotFoundException e) {
  74. throw new Exception("Opening password file failed");
  75. }
  76. byte[] obfPwd = new byte[256];
  77. try {
  78. fp.read(obfPwd);
  79. fp.close();
  80. } catch(IOException e) {
  81. throw new Exception("Failed to read VncPasswd file");
  82. }
  83. String PlainPasswd = VncAuth.unobfuscatePasswd(obfPwd);
  84. password.append(PlainPasswd);
  85. password.setLength(PlainPasswd.length());
  86. return;
  87. }
  88. JDialog win;
  89. JLabel banner;
  90. JTextField username = null;
  91. JLayer icon;
  92. int y;
  93. JPanel msg = new JPanel(null);
  94. msg.setSize(410, 145);
  95. banner = new JLabel();
  96. banner.setBounds(0, 0, msg.getPreferredSize().width, 20);
  97. banner.setHorizontalAlignment(JLabel.CENTER);
  98. banner.setOpaque(true);
  99. if (secure) {
  100. banner.setText("This connection is secure");
  101. banner.setBackground(Color.GREEN);
  102. ImageIcon secure_icon =
  103. new ImageIcon(VncViewer.class.getResource("secure.png"));
  104. banner.setIcon(secure_icon);
  105. } else {
  106. banner.setText("This connection is not secure");
  107. banner.setBackground(Color.RED);
  108. ImageIcon insecure_icon =
  109. new ImageIcon(VncViewer.class.getResource("insecure.png"));
  110. banner.setIcon(insecure_icon);
  111. }
  112. msg.add(banner);
  113. y = 20 + 10;
  114. JButton iconb = new JButton("?");
  115. iconb.setVerticalAlignment(JLabel.CENTER);
  116. iconb.setFont(new Font("Times", Font.BOLD, 34));
  117. iconb.setForeground(Color.BLUE);
  118. LayerUI ui = new MyLayerUI();
  119. icon = new JLayer(iconb, ui);
  120. icon.setBounds(10, y, 50, 50);
  121. msg.add(icon);
  122. y += 5;
  123. if (user != null && !sendLocalUsername.getValue()) {
  124. JLabel userLabel = new JLabel("Username:");
  125. userLabel.setBounds(70, y, msg.getSize().width-70-10, 20);
  126. msg.add(userLabel);
  127. y += 20 + 5;
  128. username = new JTextField(30);
  129. username.setBounds(70, y, msg.getSize().width-70-10, 25);
  130. msg.add(username);
  131. y += 25 + 5;
  132. }
  133. JLabel passwdLabel = new JLabel("Password:");
  134. passwdLabel.setBounds(70, y, msg.getSize().width-70-10, 20);
  135. msg.add(passwdLabel);
  136. y += 20 + 5;
  137. final JPasswordField passwd = new JPasswordField(30);
  138. passwd.setBounds(70, y, msg.getSize().width-70-10, 25);
  139. msg.add(passwd);
  140. y += 25 + 5;
  141. msg.setPreferredSize(new Dimension(410, y));
  142. Object[] options = {"OK \u21B5", "Cancel"};
  143. JOptionPane pane = new JOptionPane(msg,
  144. PLAIN_MESSAGE,
  145. OK_CANCEL_OPTION,
  146. null, //do not use a custom Icon
  147. options, //the titles of buttons
  148. options[0]){//default button title
  149. @Override
  150. public void selectInitialValue() {
  151. passwd.requestFocusInWindow();
  152. }
  153. };
  154. pane.setBorder(new EmptyBorder(0,0,0,0));
  155. Component c = pane.getComponent(pane.getComponentCount()-1);
  156. ((JComponent)c).setBorder(new EmptyBorder(0,0,10,10));
  157. win = pane.createDialog("VNC Authentication");
  158. win.setVisible(true);
  159. if (pane.getValue() == null || pane.getValue().equals("Cancel"))
  160. throw new Exception("Authentication cancelled");
  161. if (user != null)
  162. if (sendLocalUsername.getValue())
  163. user.append((String)System.getProperties().get("user.name"));
  164. else
  165. user.append(username.getText());
  166. if (password != null)
  167. password.append(new String(passwd.getPassword()));
  168. }
  169. public boolean showMsgBox(int flags, String title, String text)
  170. {
  171. switch (flags & 0xf) {
  172. case OK_CANCEL_OPTION:
  173. return (showConfirmDialog(null, text, title, OK_CANCEL_OPTION) == OK_OPTION);
  174. case YES_NO_OPTION:
  175. return (showConfirmDialog(null, text, title, YES_NO_OPTION) == YES_OPTION);
  176. default:
  177. if (((flags & 0xf0) == ERROR_MESSAGE) ||
  178. ((flags & 0xf0) == WARNING_MESSAGE))
  179. showMessageDialog(null, text, title, (flags & 0xf0));
  180. else
  181. showMessageDialog(null, text, title, PLAIN_MESSAGE);
  182. return true;
  183. }
  184. }
  185. }