1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 *
23 * ident	"%Z%%M%	%I%	%E% SMI"
24 *
25 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
26 * Use is subject to license terms.
27 *
28 * pmAuthOptions.java
29 * Prompt for root password from printmgr.
30 * This a helper for printmgr which echoes YES, NO, or CANCEL to stdout.
31 */
32
33package com.sun.admin.pm.client;
34
35import java.awt.*;
36import java.awt.event.*;
37import java.util.*;
38import javax.swing.*;
39import com.sun.admin.pm.server.*;
40
41
42public class pmAuthOptions {
43
44    public static void main(String[] args) {
45
46	boolean done = false;
47	String rv = "CANCEL";
48
49	    pmAuthMessage o = new pmAuthMessage(null,
50		pmUtility.getResource("Authentication.required"),
51		pmUtility.getResource("Root.access.is.required"));
52	    o.setVisible(true);
53	    switch (o.getValue()) {
54		case JOptionPane.YES_OPTION:
55			break;
56
57		case JOptionPane.NO_OPTION:
58			System.out.println("NO");
59			System.exit(0);
60			break;
61
62		case JOptionPane.CANCEL_OPTION:
63		default:
64			System.out.println("CANCEL");
65			System.exit(0);
66			break;
67	    }
68
69	while (!done) {
70	    pmAuthLogin d = new pmAuthLogin(null,
71			    pmUtility.getResource("Root.authentication"),
72			    pmUtility.getResource("Enter.root.password"));
73	    d.setVisible(true);
74	    if (d.getValue() != JOptionPane.OK_OPTION)
75		done = true;
76	    else {
77		boolean ok = false;
78		String pw = new String(d.getPassword());
79		try {
80		    PrinterUtil.checkRootPasswd(pw);
81		    ok = true;
82		} catch (Exception x) {
83
84		}
85		if (!ok) {
86		    pmOKCancelDialog m = new pmOKCancelDialog(null,
87				    pmUtility.getResource("Error"),
88				    pmUtility.getResource("Invalid.password"));
89		    m.setVisible(true);
90		    if (m.getValue() != JOptionPane.OK_OPTION)
91			done = true;
92		} else {
93		    done = true;
94		    rv = "YES";
95		}
96	    }
97	}
98
99	System.out.println(rv);
100	System.exit(0);
101    }
102
103
104}
105
106
107/*
108 */
109
110class pmAuthLogin extends pmDialog {
111    private String theTag = null;
112
113    protected pmButton okButton = null;
114    protected pmButton cancelButton = null;
115
116    public pmAuthLogin(JFrame f, String title, String msg) {
117
118	super(f, title, true);		// modal
119
120        JLabel l;
121        JPanel p;
122
123        // initialize constraints
124        GridBagConstraints c = new GridBagConstraints();
125        c.gridx = 0;
126        c.gridy = GridBagConstraints.RELATIVE;
127        c.gridwidth = 1;
128        c.gridheight = 1;
129        c.insets = new Insets(10, 10, 10, 10);
130        c.anchor = GridBagConstraints.EAST;
131
132        // top panel contains the desired message
133        p = new JPanel();
134        p.setLayout(new GridBagLayout());
135
136        l = new JLabel(msg, SwingConstants.CENTER);
137        p.add(l, c);
138        this.getContentPane().add(p, "North");
139
140
141        // middle panel contains username and password
142        p = new JPanel();
143        p.setLayout(new GridBagLayout());
144
145        l = new JLabel(pmUtility.getResource("Hostname:"),
146                        SwingConstants.RIGHT);
147        p.add(l, c);
148
149        l = new JLabel(pmUtility.getResource("Password:"),
150                        SwingConstants.RIGHT);
151        p.add(l, c);
152
153        passwordField = new JPasswordField(12);
154        passwordField.addActionListener(new ActionListener() {
155            public void actionPerformed(ActionEvent e) {
156                okPressed();
157            }
158        });
159        l.setLabelFor(passwordField);
160
161        // for consistency, don't implement this until all are...
162        // l.setDisplayedMnemonic(
163	// 	pmUtility.getIntResource("Password.mnemonic"));
164
165        c.gridx = 1;
166        c.weightx = 1.0;
167
168        c.anchor = GridBagConstraints.WEST;
169
170	String hostname = null;
171	try {
172		hostname = (java.net.InetAddress.getLocalHost()).getHostName();
173	} catch (java.net.UnknownHostException uhx) {
174		System.out.println(uhx);
175	}
176
177        l = new JLabel(hostname, SwingConstants.LEFT);
178        p.add(l, c);
179
180
181        c.fill = GridBagConstraints.HORIZONTAL;
182        c.anchor = GridBagConstraints.CENTER;
183        c.gridy = GridBagConstraints.RELATIVE;
184
185        p.add(passwordField, c);
186        passwordField.setEchoChar('*');
187
188        this.getContentPane().add(p, "Center");
189
190        // bottom panel contains buttons
191        c.gridx = 0;
192        c.weightx = 1.0;
193        c.weighty = 0.0;
194        c.gridwidth = GridBagConstraints.REMAINDER;
195        c.fill = GridBagConstraints.HORIZONTAL;
196        c.anchor = GridBagConstraints.CENTER;
197
198        JPanel thePanel = new JPanel();
199
200        okButton = new pmButton(
201            pmUtility.getResource("OK"));
202        okButton.setMnemonic(
203            pmUtility.getIntResource("OK.mnemonic"));
204        okButton.addActionListener(new ActionListener() {
205            public void actionPerformed(ActionEvent evt) {
206                okPressed();
207            }
208        });
209        thePanel.add(okButton, c);
210
211        cancelButton = new pmButton(
212            pmUtility.getResource("Cancel"));
213        cancelButton.setMnemonic(
214            pmUtility.getIntResource("Cancel.mnemonic"));
215        cancelButton.addActionListener(new ActionListener() {
216            public void actionPerformed(ActionEvent evt) {
217                cancelPressed();
218            }
219        });
220        thePanel.add(cancelButton, c);
221
222        this.getContentPane().add(thePanel, "South");
223
224        addWindowListener(new WindowAdapter() {
225            public void windowClosing(WindowEvent e) {
226                returnValue = JOptionPane.CANCEL_OPTION;
227                setVisible(false);
228            }
229        });
230
231        // handle Esc as cancel in any case
232        this.getRootPane().registerKeyboardAction(new ActionListener() {
233            public void actionPerformed(ActionEvent e) {
234                Debug.message("CLNT:  default cancel action");
235                cancelPressed();
236            }},
237            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
238            JComponent.WHEN_IN_FOCUSED_WINDOW);
239
240        // lay out the dialog
241        this.pack();
242
243        // set focus and defaults after packing...
244        // this.getRootPane().setDefaultButton(okButton);
245        okButton.setAsDefaultButton();
246
247        passwordField.requestFocus();
248    }
249
250    public int getValue() {
251        return returnValue;
252    }
253
254
255    public void okPressed() {
256        returnValue = JOptionPane.OK_OPTION;
257        setVisible(false);
258    }
259
260    public void cancelPressed() {
261       	returnValue = JOptionPane.CANCEL_OPTION;
262       	setVisible(false);
263    }
264
265
266    public void clearPressed() {
267        passwordField.setText("");
268    }
269
270    public char[] getPassword() {
271	return passwordField.getPassword();
272    }
273
274
275    public JPasswordField passwordField = null;
276
277    protected int returnValue = JOptionPane.CANCEL_OPTION;
278
279}
280
281
282class pmAuthMessage extends pmDialog {
283    private String theTag = null;
284
285    protected pmButton authButton = null;
286    protected pmButton cancelButton = null;
287    protected pmButton contButton = null;
288
289    public pmAuthMessage(JFrame f, String title, String msg) {
290
291	super(f, title, true);		// modal
292
293        JPanel p;
294
295        // initialize constraints
296        GridBagConstraints c = new GridBagConstraints();
297        c.gridx = 0;
298        c.gridy = GridBagConstraints.RELATIVE;
299        c.gridwidth = 1;
300        c.gridheight = 1;
301        c.insets = new Insets(10, 10, 10, 10);
302        c.anchor = GridBagConstraints.EAST;
303
304        // top panel contains the desired message
305        p = new JPanel();
306        p.setLayout(new GridBagLayout());
307
308
309        JList l = new JList() {
310            public boolean isFocusable() {
311                return false;
312            }
313        };
314	// pathetic hacks to make the list look the same as a label
315	JLabel tmp = new JLabel();
316        l.setBackground(tmp.getBackground());
317        l.setForeground(tmp.getForeground());
318	l.setFont(tmp.getFont());
319	tmp = null;
320	Vector v = new Vector();
321        if (msg != null) {
322            StringTokenizer st = new StringTokenizer(msg, "\n", false);
323            try {
324                while (st.hasMoreTokens())
325                    v.addElement(st.nextToken());
326            } catch (Exception x) {
327            }
328            l.setListData(v);
329        }
330
331
332        p.add(l, c);
333        this.getContentPane().add(p, "North");
334
335
336        // bottom panel contains buttons
337        c.gridx = 0;
338        c.weightx = 1.0;
339        c.weighty = 0.0;
340        c.gridwidth = GridBagConstraints.REMAINDER;
341        c.fill = GridBagConstraints.HORIZONTAL;
342        c.anchor = GridBagConstraints.CENTER;
343
344        JPanel thePanel = new JPanel();
345
346        authButton = new pmButton(pmUtility.getResource("Authenticate"));
347        authButton.setMnemonic(
348			pmUtility.getIntResource("Authenticate.mnemonic"));
349        authButton.addActionListener(new ActionListener() {
350            public void actionPerformed(ActionEvent evt) {
351                authPressed();
352            }
353        });
354        thePanel.add(authButton, c);
355
356        contButton = new pmButton(pmUtility.getResource("Continue"));
357        contButton.setMnemonic(pmUtility.getIntResource("Continue.mnemonic"));
358        contButton.addActionListener(new ActionListener() {
359            public void actionPerformed(ActionEvent evt) {
360                contPressed();
361            }
362        });
363        thePanel.add(contButton, c);
364
365        cancelButton = new pmButton(pmUtility.getResource("Cancel"));
366        cancelButton.setMnemonic(pmUtility.getIntResource("Cancel.mnemonic"));
367        cancelButton.addActionListener(new ActionListener() {
368            public void actionPerformed(ActionEvent evt) {
369                cancelPressed();
370            }
371        });
372        thePanel.add(cancelButton, c);
373
374        this.getContentPane().add(thePanel, "South");
375
376        addWindowListener(new WindowAdapter() {
377            public void windowClosing(WindowEvent e) {
378                returnValue = JOptionPane.CANCEL_OPTION;
379                setVisible(false);
380            }
381        });
382
383        // handle Esc as cancel in any case
384        this.getRootPane().registerKeyboardAction(new ActionListener() {
385            public void actionPerformed(ActionEvent e) {
386                Debug.message("CLNT:  default cancel action");
387                cancelPressed();
388            }},
389            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
390            JComponent.WHEN_IN_FOCUSED_WINDOW);
391
392        // lay out the dialog
393        this.pack();
394
395        // set focus and defaults after packing...
396        authButton.setAsDefaultButton();
397
398    }
399
400    public int getValue() {
401        return returnValue;
402    }
403
404
405    public void authPressed() {
406        returnValue = JOptionPane.YES_OPTION;
407        setVisible(false);
408    }
409
410    public void cancelPressed() {
411       	returnValue = JOptionPane.CANCEL_OPTION;
412       	setVisible(false);
413    }
414
415
416    public void contPressed() {
417       	returnValue = JOptionPane.NO_OPTION;
418       	setVisible(false);
419    }
420
421    protected int returnValue = JOptionPane.CANCEL_OPTION;
422
423}
424