001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch.GUI;
024:
025: import java.net.UnknownHostException;
026:
027: import net.fenyo.gnetwatch.AlgorithmException;
028: import net.fenyo.gnetwatch.GenericTools;
029: import net.fenyo.gnetwatch.targets.TargetIPv4Subnet;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: import org.eclipse.swt.*;
035: import org.eclipse.swt.widgets.*;
036: import org.eclipse.swt.layout.*;
037: import org.eclipse.swt.custom.*;
038: import org.eclipse.swt.events.*;
039: import org.eclipse.swt.graphics.*;
040: import org.eclipse.swt.browser.*;
041:
042: import org.snmp4j.security.*;
043:
044: /**
045: * This class manages the "credentials" dialog.
046: * @author Alexandre Fenyo
047: * @version $Id: DialogCredentials.java,v 1.15 2007/03/03 00:38:19 fenyo Exp $
048: */
049:
050: /*
051: * snmp :
052: * version
053: * security level
054: * retries
055: * timeout
056: * port
057: * max size request pdu
058: * v1 et v2c : community
059: * v3 : username / password pour authentication, password pour privacy
060: */
061:
062: public class DialogCredentials extends Dialog {
063: private static Log log = LogFactory.getLog(DialogCredentials.class);
064:
065: private final GUI gui;
066:
067: private final int bglevel = 100;
068:
069: private GridLayout layout = null;
070: private Composite groups_composite = null;
071: private Composite bottom_composite = null;
072: private RowLayout groups_composite_layout = null;
073: private RowLayout bottom_composite_layout = null;
074: private GridData groups_composite_grid_data = null;
075: private Group group_network_parameters = null,
076: group_credentials = null, group_credentials_v2c = null;
077: private GridLayout group_network_parameters_layout = null,
078: group_credentials_layout = null,
079: group_credentials_v2c_layout = null;
080: private Text group_credentials_value = null;
081: private Text group_credentials_value2 = null;
082: private Text group_credentials_value3 = null;
083: private Text group_credentials_v2c_value = null;
084:
085: private int version = 0; // SNMPv1
086: private int sec = SecurityLevel.AUTH_PRIV;
087: private int retries = 3;
088: private int timeout = 1500; // microsec
089: private int port = 161;
090: private String community = "public";
091: private String username = "";
092: private String password_auth = "";
093: private String password_priv = "";
094: private int pdu_max_size = 1000;
095:
096: /**
097: * Constructor.
098: * @param gui current GUI instance.
099: * @param parent parent shell.
100: */
101: public DialogCredentials(final GUI gui, final Shell parent) {
102: super (parent, 0);
103: this .gui = gui;
104: }
105:
106: /**
107: * Gets SNMP version.
108: * @param none.
109: * @return int SNMP version.
110: */
111: public int getVersion() {
112: return version;
113: }
114:
115: /**
116: * Gets security level.
117: * @param none.
118: * @return int security level.
119: */
120: public int getSec() {
121: return sec;
122: }
123:
124: /**
125: * Gets number of retries.
126: * @param none.
127: * @return int retries.
128: */
129: public int getRetries() {
130: return retries;
131: }
132:
133: /**
134: * Gets SNMP timeout per try.
135: * @param none.
136: * @return int timeout.
137: */
138: public int getTimeout() {
139: return timeout;
140: }
141:
142: /**
143: * Returns the SNMP agent UDP port.
144: * @param none.
145: * @return int SNMP agent UDP port.
146: */
147: public int getPort() {
148: return port;
149: }
150:
151: /**
152: * Returns the community string.
153: * @param none.
154: * @return String community string.
155: */
156: public String getCommunity() {
157: return community;
158: }
159:
160: /**
161: * Returns the username.
162: * @param none.
163: * @return String username.
164: */
165: public String getUsername() {
166: return username;
167: }
168:
169: /**
170: * Returns the password authentication.
171: * @param none.
172: * @return String password authentication.
173: */
174: public String getPasswordAuth() {
175: return password_auth;
176: }
177:
178: /**
179: * Returns the password privacy.
180: * @param none.
181: * @return String password privacy.
182: */
183: public String getPasswordPriv() {
184: return password_priv;
185: }
186:
187: /**
188: * Returns the maximum PDU size.
189: * @param none.
190: * @return int maximum PDU size.
191: */
192: public int getPDUMaxSize() {
193: return pdu_max_size;
194: }
195:
196: /**
197: * Sets the SNMP version.
198: * @param version SNMP version.
199: * @return void.
200: */
201: public void setVersion(final int version) {
202: this .version = version;
203: }
204:
205: /**
206: * Sets the security level.
207: * @param sec security level.
208: * @return void.
209: */
210: public void setSec(final int sec) {
211: this .sec = sec;
212: }
213:
214: /**
215: * Sets the number of SNMP retries.
216: * @param int number of SNMP retries.
217: * @return void.
218: */
219: public void setRetries(final int retries) {
220: this .retries = retries;
221: }
222:
223: /**
224: * Sets the SNMP timeout per try.
225: * @param timeout timeout per try.
226: * @return void.
227: */
228: public void setTimeout(final int timeout) {
229: this .timeout = timeout;
230: }
231:
232: /**
233: * Sets the SNMP agent UDP port.
234: * @param port SNMP agent UDP port.
235: * @return void.
236: */
237: public void setPort(final int port) {
238: this .port = port;
239: }
240:
241: /**
242: * Sets the community string.
243: * @param community community string.
244: * @return void.
245: */
246: public void setCommunity(final String community) {
247: this .community = community;
248: }
249:
250: /**
251: * Sets the username.
252: * @param username username.
253: * @return void.
254: */
255: public void setUsername(final String username) {
256: this .username = username;
257: }
258:
259: /**
260: * Sets the password authentication.
261: * @param password_auth password authentication.
262: * @return void.
263: */
264: public void setPasswordAuth(final String password_auth) {
265: this .password_auth = password_auth;
266: }
267:
268: /**
269: * Sets the password privacy.
270: * @param password_priv password privacy.;
271: * @return void.
272: */
273: public void setPasswordPriv(final String password_priv) {
274: this .password_priv = password_priv;
275: }
276:
277: /**
278: * Sets the PDU maximum size.
279: * @param pdu_max_size PDU maximum size.
280: * @return void.
281: */
282: public void setPDUMaxSize(final int pdu_max_size) {
283: this .pdu_max_size = pdu_max_size;
284: }
285:
286: /**
287: * Displays the dialog.
288: * @param none.
289: * @return void.
290: */
291: public void open() {
292: final Shell parent = getParent();
293: final Display display = parent.getDisplay();
294: final Shell shell = new Shell(parent, SWT.DIALOG_TRIM
295: | SWT.APPLICATION_MODAL);
296: shell.setText(gui.getConfig().getString("gnetwatch_cred"));
297: // shell.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
298:
299: // Your code goes here (widget creation, set result, etc).
300: // Composite for groups at left
301: layout = new GridLayout();
302: layout.numColumns = 1;
303: layout.marginHeight = 2;
304: layout.marginWidth = 2;
305: layout.verticalSpacing = 1;
306: shell.setLayout(layout);
307:
308: groups_composite = new Composite(shell, SWT.FLAT);
309: groups_composite_layout = new RowLayout(SWT.VERTICAL);
310: groups_composite_layout.fill = true;
311: groups_composite_layout.marginTop = 0;
312: groups_composite_layout.marginBottom = 0;
313: groups_composite.setLayout(groups_composite_layout);
314: groups_composite_grid_data = new GridData(
315: GridData.FILL_VERTICAL);
316: groups_composite.setLayoutData(groups_composite_grid_data);
317:
318: // Group for network parameters
319:
320: group_network_parameters = new Group(groups_composite,
321: SWT.SHADOW_ETCHED_IN);
322: group_network_parameters_layout = new GridLayout();
323: group_network_parameters_layout.numColumns = 2;
324: group_network_parameters
325: .setLayout(group_network_parameters_layout);
326: group_network_parameters.setText(gui.getConfig().getString(
327: "network_parameters"));
328:
329: final Label label_version = new Label(group_network_parameters,
330: SWT.SHADOW_IN);
331: label_version.setText(gui.getConfig().getString(
332: "protocol_version"));
333: label_version.setLayoutData(new GridData(
334: GridData.FILL_HORIZONTAL));
335: final Combo cversion = new Combo(group_network_parameters,
336: SWT.SHADOW_IN | SWT.READ_ONLY);
337: cversion.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
338: // version.setBackground(new Color(display, bglevel, bglevel, bglevel));
339: // version.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
340: cversion.add("SNMP v1");
341: cversion.add("SNMP v2c");
342: cversion.add("SNMP v3");
343: if (version == 0)
344: cversion.setText("SNMP v1");
345: else if (version == 1)
346: cversion.setText("SNMP v2c");
347: else
348: cversion.setText("SNMP v3");
349:
350: final Label label_sec = new Label(group_network_parameters,
351: SWT.SHADOW_IN);
352: label_sec.setText(gui.getConfig().getString("security_level"));
353: label_sec.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
354: final Combo csec = new Combo(group_network_parameters,
355: SWT.SHADOW_IN | SWT.READ_ONLY);
356: csec.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
357: csec.add("open");
358: csec.add("auth only");
359: csec.add("auth+priv");
360: if (sec == SecurityLevel.AUTH_PRIV)
361: csec.setText("auth+priv");
362: else if (sec == SecurityLevel.AUTH_NOPRIV)
363: csec.setText("auth only");
364: else
365: csec.setText("open");
366: label_sec.setEnabled(version == 2);
367: csec.setEnabled(version == 2);
368:
369: final Label label_packet_size = new Label(
370: group_network_parameters, SWT.SHADOW_IN);
371: label_packet_size.setText(gui.getConfig().getString(
372: "pdu_max_size"));
373: label_packet_size.setLayoutData(new GridData(
374: GridData.FILL_HORIZONTAL));
375: final Spinner packet_size = new Spinner(
376: group_network_parameters, SWT.WRAP);
377: packet_size
378: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
379: packet_size.setMinimum(0);
380: packet_size.setMaximum(10000);
381: packet_size.setSelection(1400);
382: // packet_size.setBackground(new Color(display, bglevel, bglevel, bglevel));
383: // packet_size.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
384:
385: final Label label_retries = new Label(group_network_parameters,
386: SWT.SHADOW_IN);
387: label_retries.setText(gui.getConfig().getString("retries"));
388: label_retries.setLayoutData(new GridData(
389: GridData.FILL_HORIZONTAL));
390: final Spinner retries = new Spinner(group_network_parameters,
391: SWT.WRAP);
392: retries.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
393: retries.setMinimum(0);
394: retries.setMaximum(15);
395: retries.setSelection(this .retries);
396:
397: final Label label_timeout = new Label(group_network_parameters,
398: SWT.SHADOW_IN);
399: label_timeout.setText(gui.getConfig().getString(
400: "timeout_microsec"));
401: label_timeout.setLayoutData(new GridData(
402: GridData.FILL_HORIZONTAL));
403: final Spinner timeout = new Spinner(group_network_parameters,
404: SWT.WRAP);
405: timeout.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
406: timeout.setMinimum(0);
407: timeout.setMaximum(10000);
408: timeout.setSelection(this .timeout);
409:
410: final Label label_port = new Label(group_network_parameters,
411: SWT.SHADOW_IN);
412: label_port.setText(gui.getConfig()
413: .getString("destination_port"));
414: label_port
415: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
416: final Spinner port = new Spinner(group_network_parameters,
417: SWT.WRAP);
418: port.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
419: port.setMinimum(1);
420: port.setMaximum(65535);
421: port.setSelection(this .port);
422:
423: // Group for SNMPv1 & SNMPv2c credentials
424:
425: group_credentials_v2c = new Group(groups_composite,
426: SWT.SHADOW_ETCHED_IN);
427: group_credentials_v2c_layout = new GridLayout();
428: group_credentials_v2c_layout.numColumns = 2;
429: group_credentials_v2c.setLayout(group_credentials_v2c_layout);
430: group_credentials_v2c.setText(gui.getConfig().getString(
431: "v1_v2c_cred"));
432: group_credentials_v2c.setEnabled(version != 2);
433:
434: final Label label1_v2c = new Label(group_credentials_v2c,
435: SWT.SHADOW_IN);
436: label1_v2c.setText(gui.getConfig()
437: .getString("community_string"));
438: label1_v2c
439: .setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
440: label1_v2c.setEnabled(version != 2);
441: group_credentials_v2c_value = new Text(group_credentials_v2c,
442: SWT.SINGLE);
443: group_credentials_v2c_value
444: .setLayoutData(new GridData(
445: GridData.HORIZONTAL_ALIGN_END
446: | GridData.FILL_VERTICAL));
447: group_credentials_v2c_value.setBackground(new Color(display,
448: bglevel, bglevel, bglevel));
449: group_credentials_v2c_value.setForeground(display
450: .getSystemColor(SWT.COLOR_WHITE));
451: group_credentials_v2c_value.setEnabled(version != 2);
452:
453: group_credentials_v2c_value.setText(community);
454: final GC gc = new GC(group_credentials_v2c_value);
455: gc.setFont(group_credentials_v2c_value.getFont());
456: ((GridData) (group_credentials_v2c_value.getLayoutData())).widthHint = gc
457: .stringExtent(" ").x;
458:
459: // Group for SNMPv3 credentials
460:
461: group_credentials = new Group(groups_composite,
462: SWT.SHADOW_ETCHED_IN);
463: group_credentials_layout = new GridLayout();
464: group_credentials_layout.numColumns = 2;
465: group_credentials.setLayout(group_credentials_layout);
466: group_credentials.setText(gui.getConfig().getString("v3_cred"));
467: group_credentials.setEnabled(version == 2);
468:
469: final Label label1 = new Label(group_credentials, SWT.SHADOW_IN);
470: label1.setText(gui.getConfig().getString("username"));
471: label1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
472: label1.setEnabled(version == 2);
473: group_credentials_value = new Text(group_credentials,
474: SWT.SINGLE);
475: group_credentials_value
476: .setLayoutData(new GridData(
477: GridData.HORIZONTAL_ALIGN_END
478: | GridData.FILL_VERTICAL));
479: group_credentials_value.setBackground(new Color(display,
480: bglevel, bglevel, bglevel));
481: group_credentials_value.setForeground(display
482: .getSystemColor(SWT.COLOR_WHITE));
483: group_credentials_value.setText(username);
484: ((GridData) (group_credentials_value.getLayoutData())).widthHint = gc
485: .stringExtent(" ").x;
486: group_credentials_value.setEnabled(version == 2);
487:
488: final Label label2 = new Label(group_credentials, SWT.SHADOW_IN);
489: label2.setText(gui.getConfig().getString(
490: "authentication_password"));
491: label2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
492: label2.setEnabled(version == 2);
493: group_credentials_value2 = new Text(group_credentials,
494: SWT.SINGLE);
495: group_credentials_value2
496: .setLayoutData(new GridData(
497: GridData.HORIZONTAL_ALIGN_END
498: | GridData.FILL_VERTICAL));
499: group_credentials_value2.setBackground(new Color(display,
500: bglevel, bglevel, bglevel));
501: group_credentials_value2.setForeground(display
502: .getSystemColor(SWT.COLOR_WHITE));
503: group_credentials_value2.setText(password_auth);
504: ((GridData) (group_credentials_value2.getLayoutData())).widthHint = gc
505: .stringExtent(" ").x;
506: group_credentials_value2.setEnabled(version == 2);
507:
508: final Label label3 = new Label(group_credentials, SWT.SHADOW_IN);
509: label3.setText(gui.getConfig().getString("privacy_password"));
510: label3.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
511: label3.setEnabled(version == 2);
512: group_credentials_value3 = new Text(group_credentials,
513: SWT.SINGLE);
514: group_credentials_value3
515: .setLayoutData(new GridData(
516: GridData.HORIZONTAL_ALIGN_END
517: | GridData.FILL_VERTICAL));
518: group_credentials_value3.setBackground(new Color(display,
519: bglevel, bglevel, bglevel));
520: group_credentials_value3.setForeground(display
521: .getSystemColor(SWT.COLOR_WHITE));
522: group_credentials_value3.setText(password_priv);
523: ((GridData) (group_credentials_value3.getLayoutData())).widthHint = gc
524: .stringExtent(" ").x;
525: group_credentials_value3.setEnabled(version == 2);
526:
527: // bottom buttons
528:
529: bottom_composite = new Composite(shell, SWT.FLAT);
530: bottom_composite_layout = new RowLayout();
531: bottom_composite_layout.fill = true;
532: bottom_composite_layout.marginTop = 0;
533: bottom_composite_layout.marginBottom = 0;
534: bottom_composite_layout.wrap = false;
535: bottom_composite_layout.pack = false;
536: bottom_composite_layout.justify = true;
537: bottom_composite_layout.type = SWT.HORIZONTAL;
538: bottom_composite_layout.marginLeft = 5;
539: bottom_composite_layout.marginTop = 5;
540: bottom_composite_layout.marginRight = 5;
541: bottom_composite_layout.marginBottom = 5;
542: bottom_composite_layout.spacing = 0;
543: bottom_composite.setLayout(bottom_composite_layout);
544: final GridData bottom_composite_grid_data = new GridData(
545: GridData.FILL_HORIZONTAL);
546: bottom_composite.setLayoutData(bottom_composite_grid_data);
547:
548: final Button button_ok = new Button(bottom_composite, SWT.PUSH);
549: button_ok.setText("Ok");
550: final DialogCredentials _this = this ;
551: button_ok.addSelectionListener(new SelectionListener() {
552: public void widgetDefaultSelected(SelectionEvent e) {
553: _this .version = cversion.getSelectionIndex();
554:
555: if (csec.getSelectionIndex() == 0)
556: _this .sec = SecurityLevel.NOAUTH_NOPRIV;
557: else if (csec.getSelectionIndex() == 1)
558: _this .sec = SecurityLevel.AUTH_NOPRIV;
559: else
560: _this .sec = SecurityLevel.AUTH_PRIV;
561:
562: _this .retries = retries.getSelection();
563: _this .timeout = timeout.getSelection();
564: _this .port = port.getSelection();
565: _this .community = group_credentials_v2c_value.getText();
566: _this .username = group_credentials_value.getText();
567: _this .password_auth = group_credentials_value2
568: .getText();
569: _this .password_priv = group_credentials_value3
570: .getText();
571: _this .pdu_max_size = packet_size.getSelection();
572:
573: shell.dispose();
574: }
575:
576: public void widgetSelected(SelectionEvent e) {
577: widgetDefaultSelected(e);
578: }
579: });
580:
581: final Button button_cancel = new Button(bottom_composite,
582: SWT.PUSH);
583: button_cancel.setText("Cancel");
584: button_cancel.addSelectionListener(new SelectionListener() {
585: public void widgetDefaultSelected(SelectionEvent e) {
586: shell.dispose();
587: }
588:
589: public void widgetSelected(SelectionEvent e) {
590: widgetDefaultSelected(e);
591: }
592: });
593:
594: cversion.addModifyListener(new ModifyListener() {
595: public void modifyText(final ModifyEvent e) {
596: if (cversion.getSelectionIndex() == 2) {
597: group_credentials_v2c.setEnabled(false);
598: label1_v2c.setEnabled(false);
599: group_credentials_v2c_value.setEnabled(false);
600:
601: label_sec.setEnabled(true);
602: csec.setEnabled(true);
603:
604: group_credentials.setEnabled(true);
605: label1.setEnabled(true);
606: label2.setEnabled(true);
607: label3.setEnabled(true);
608: group_credentials_value.setEnabled(true);
609: group_credentials_value2.setEnabled(true);
610: group_credentials_value3.setEnabled(true);
611:
612: groups_composite.pack(true);
613: } else {
614: group_credentials_v2c.setEnabled(true);
615: label1_v2c.setEnabled(true);
616: group_credentials_v2c_value.setEnabled(true);
617:
618: label_sec.setEnabled(false);
619: csec.setEnabled(false);
620:
621: group_credentials.setEnabled(false);
622: label1.setEnabled(false);
623: label2.setEnabled(false);
624: label3.setEnabled(false);
625: group_credentials_value.setEnabled(false);
626: group_credentials_value2.setEnabled(false);
627: group_credentials_value3.setEnabled(false);
628:
629: groups_composite.pack(true);
630: }
631: }
632: });
633:
634: shell.pack(true);
635: shell.open();
636: while (!shell.isDisposed())
637: if (!display.readAndDispatch())
638: display.sleep();
639: }
640: }
|