1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/* Copyright (C) 2017 Brian P. Hinz
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package com.tigervnc.vncviewer;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.plaf.LayerUI;
import com.tigervnc.rfb.*;
import com.tigervnc.rfb.Point;
import com.tigervnc.rfb.Exception;
import static com.tigervnc.vncviewer.Parameters.*;
import static javax.swing.GroupLayout.*;
import static javax.swing.JOptionPane.*;
public class UserDialog implements UserPasswdGetter, UserMsgBox
{
public final void getUserPasswd(boolean secure, StringBuffer user, StringBuffer password)
{
String passwordFileStr = passwordFile.getValue();
if ((password == null) && sendLocalUsername.getValue()) {
user.append((String)System.getProperties().get("user.name"));
return;
}
if (user == null && !passwordFileStr.equals("")) {
InputStream fp = null;
try {
fp = new FileInputStream(passwordFileStr);
} catch(FileNotFoundException e) {
throw new Exception("Opening password file failed");
}
byte[] obfPwd = new byte[256];
try {
fp.read(obfPwd);
fp.close();
} catch(IOException e) {
throw new Exception("Failed to read VncPasswd file");
}
String PlainPasswd = VncAuth.unobfuscatePasswd(obfPwd);
password.append(PlainPasswd);
password.setLength(PlainPasswd.length());
return;
}
JDialog win;
JLabel banner;
JTextField username = null;
int y;
JPanel msg = new JPanel(null);
msg.setSize(410, 145);
banner = new JLabel();
banner.setBounds(0, 0, msg.getPreferredSize().width, 20);
banner.setHorizontalAlignment(JLabel.CENTER);
banner.setOpaque(true);
if (secure) {
banner.setText("This connection is secure");
banner.setBackground(Color.GREEN);
ImageIcon secure_icon =
new ImageIcon(VncViewer.class.getResource("secure.png"));
banner.setIcon(secure_icon);
} else {
banner.setText("This connection is not secure");
banner.setBackground(Color.RED);
ImageIcon insecure_icon =
new ImageIcon(VncViewer.class.getResource("insecure.png"));
banner.setIcon(insecure_icon);
}
msg.add(banner);
y = 20 + 10;
JButton icon = new JButton("?");
icon.setVerticalAlignment(JLabel.CENTER);
icon.setFont(new Font("Times", Font.BOLD, 34));
icon.setForeground(Color.BLUE);
icon.setBounds(10, y, 50, 50);
// the following disables the "?" icon without changing the color
UIDefaults defaults = UIManager.getLookAndFeelDefaults();
Painter painter = (Painter)defaults.get("Button[Enabled].backgroundPainter");
defaults.put("Button[Disabled].backgroundPainter", painter);
icon.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
icon.putClientProperty("Nimbus.Overrides", defaults);
icon.setEnabled(false);
msg.add(icon);
y += 5;
if (user != null && !sendLocalUsername.getValue()) {
JLabel userLabel = new JLabel("Username:");
userLabel.setBounds(70, y, msg.getSize().width-70-10, 20);
msg.add(userLabel);
y += 20 + 5;
username = new JTextField(30);
username.setBounds(70, y, msg.getSize().width-70-10, 25);
msg.add(username);
y += 25 + 5;
}
JLabel passwdLabel = new JLabel("Password:");
passwdLabel.setBounds(70, y, msg.getSize().width-70-10, 20);
msg.add(passwdLabel);
y += 20 + 5;
final JPasswordField passwd = new JPasswordField(30);
passwd.setBounds(70, y, msg.getSize().width-70-10, 25);
msg.add(passwd);
y += 25 + 5;
msg.setPreferredSize(new Dimension(410, y));
Object[] options = {"OK \u21B5", "Cancel"};
JOptionPane pane = new JOptionPane(msg,
PLAIN_MESSAGE,
OK_CANCEL_OPTION,
null, //do not use a custom Icon
options, //the titles of buttons
options[0]){//default button title
@Override
public void selectInitialValue() {
passwd.requestFocusInWindow();
}
};
pane.setBorder(new EmptyBorder(0,0,0,0));
Component c = pane.getComponent(pane.getComponentCount()-1);
((JComponent)c).setBorder(new EmptyBorder(0,0,10,10));
win = pane.createDialog("VNC Authentication");
win.setVisible(true);
if (pane.getValue() == null || pane.getValue().equals("Cancel"))
throw new Exception("Authentication cancelled");
if (user != null)
if (sendLocalUsername.getValue())
user.append((String)System.getProperties().get("user.name"));
else
user.append(username.getText());
if (password != null)
password.append(new String(passwd.getPassword()));
}
public boolean showMsgBox(int flags, String title, String text)
{
switch (flags & 0xf) {
case OK_CANCEL_OPTION:
return (showConfirmDialog(null, text, title, OK_CANCEL_OPTION) == OK_OPTION);
case YES_NO_OPTION:
return (showConfirmDialog(null, text, title, YES_NO_OPTION) == YES_OPTION);
default:
if (((flags & 0xf0) == ERROR_MESSAGE) ||
((flags & 0xf0) == WARNING_MESSAGE))
showMessageDialog(null, text, title, (flags & 0xf0));
else
showMessageDialog(null, text, title, PLAIN_MESSAGE);
return true;
}
}
}
|