1 12 package org.eclipse.team.internal.ccvs.ssh2; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.ByteArrayOutputStream ; 16 import java.io.File ; 17 import java.io.IOException ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.Iterator ; 21 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 import org.eclipse.jface.dialogs.MessageDialog; 26 import org.eclipse.jface.preference.IPreferenceStore; 27 import org.eclipse.jface.preference.PreferencePage; 28 import org.eclipse.jface.viewers.ColumnWeightData; 29 import org.eclipse.jface.viewers.IStructuredContentProvider; 30 import org.eclipse.jface.viewers.IStructuredSelection; 31 import org.eclipse.jface.viewers.ITableLabelProvider; 32 import org.eclipse.jface.viewers.LabelProvider; 33 import org.eclipse.jface.viewers.TableLayout; 34 import org.eclipse.jface.viewers.TableViewer; 35 import org.eclipse.jface.viewers.Viewer; 36 import org.eclipse.osgi.util.NLS; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.custom.BusyIndicator; 39 import org.eclipse.swt.custom.TableEditor; 40 import org.eclipse.swt.events.FocusEvent; 41 import org.eclipse.swt.events.FocusListener; 42 import org.eclipse.swt.events.ModifyEvent; 43 import org.eclipse.swt.events.ModifyListener; 44 import org.eclipse.swt.events.SelectionAdapter; 45 import org.eclipse.swt.events.SelectionEvent; 46 import org.eclipse.swt.graphics.Image; 47 import org.eclipse.swt.layout.GridData; 48 import org.eclipse.swt.layout.GridLayout; 49 import org.eclipse.swt.widgets.Button; 50 import org.eclipse.swt.widgets.Composite; 51 import org.eclipse.swt.widgets.Control; 52 import org.eclipse.swt.widgets.DirectoryDialog; 53 import org.eclipse.swt.widgets.Display; 54 import org.eclipse.swt.widgets.Event; 55 import org.eclipse.swt.widgets.FileDialog; 56 import org.eclipse.swt.widgets.Label; 57 import org.eclipse.swt.widgets.Listener; 58 import org.eclipse.swt.widgets.Shell; 59 import org.eclipse.swt.widgets.TabFolder; 60 import org.eclipse.swt.widgets.TabItem; 61 import org.eclipse.swt.widgets.Table; 62 import org.eclipse.swt.widgets.TableColumn; 63 import org.eclipse.swt.widgets.Text; 64 import org.eclipse.team.internal.ui.SWTUtils; 65 import org.eclipse.ui.IWorkbench; 66 import org.eclipse.ui.IWorkbenchPreferencePage; 67 import org.eclipse.ui.PlatformUI; 68 69 import com.jcraft.jsch.Channel; 70 import com.jcraft.jsch.ChannelSftp; 71 import com.jcraft.jsch.HostKey; 72 import com.jcraft.jsch.HostKeyRepository; 73 import com.jcraft.jsch.JSch; 74 import com.jcraft.jsch.JSchException; 75 import com.jcraft.jsch.KeyPair; 76 import com.jcraft.jsch.Session; 77 import com.jcraft.jsch.SftpATTRS; 78 import com.jcraft.jsch.SftpException; 79 80 public class CVSSSH2PreferencePage extends PreferencePage 81 implements IWorkbenchPreferencePage { 82 83 85 private static final String SSH2_PREFERENCE_PAGE_CONTEXT = "org.eclipse.team.cvs.ui.ssh2_preference_page_context"; 87 private Label ssh2HomeLabel; 88 private Label privateKeyLabel; 89 private Text ssh2HomeText; 90 private Text privateKeyText; 91 private Button privateKeyAdd; 92 93 94 private Button ssh2HomeBrowse; 95 private Button keyGenerateDSA; 96 private Button keyGenerateRSA; 97 private Button keyLoad; 98 private Button keyExport; 99 private Button saveKeyPair; 100 private Label keyCommentLabel; 101 private Text keyCommentText; 102 private Label keyFingerPrintLabel; 103 private Text keyFingerPrintText; 104 private Label keyPassphrase1Label; 105 private Text keyPassphrase1Text; 106 private Label keyPassphrase2Label; 107 private Text keyPassphrase2Text; 108 private Label publicKeylabel; 109 private Text publicKeyText; 110 private KeyPair kpair=null; 111 private String kpairComment; 112 113 public static final String AUTH_SCHEME = ""; public static final URL FAKE_URL; 115 116 static { 117 URL temp = null; 118 try{ 119 temp = new URL ("http://org.eclipse.team.cvs.ssh2"); }catch (MalformedURLException e){} 121 FAKE_URL = temp; 122 } 123 124 public CVSSSH2PreferencePage() { 125 IPreferenceStore store=CVSSSH2Plugin.getDefault().getPreferenceStore(); 127 setPreferenceStore(store); 128 setDescription(CVSSSH2Messages.CVSSSH2PreferencePage_18); 129 } 130 131 protected Control createContents(Composite parent) { 132 Composite container = new Composite(parent, SWT.NULL); 133 GridLayout layout = new GridLayout(); 134 container.setLayout(layout); 135 initializeDialogUnits(container); 136 137 TabFolder tabFolder = new TabFolder(container, SWT.NONE); 138 tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH)); 139 140 TabItem tabItem = new TabItem(tabFolder, SWT.NONE); 141 tabItem.setText(CVSSSH2Messages.CVSSSH2PreferencePage_19); 142 tabItem.setControl(createGeneralPage(tabFolder)); 143 144 tabItem = new TabItem(tabFolder, SWT.NONE); 145 tabItem.setText(CVSSSH2Messages.CVSSSH2PreferencePage_21); 146 tabItem.setControl(createKeyManagementPage(tabFolder)); 147 148 tabItem = new TabItem(tabFolder, SWT.NONE); 149 tabItem.setText(CVSSSH2Messages.CVSSSH2PreferencePage_133); 150 tabItem.setControl(createHostKeyManagementPage(tabFolder)); 151 152 initControls(); 153 154 Dialog.applyDialogFont(parent); 155 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), SSH2_PREFERENCE_PAGE_CONTEXT); 156 return container; 157 } 158 159 private Control createGeneralPage(Composite parent) { 160 Composite group=new Composite(parent, SWT.NULL); 161 GridLayout layout=new GridLayout(); 162 layout.numColumns=3; 163 group.setLayout(layout); 164 GridData data = new GridData(); 165 data.horizontalAlignment = GridData.FILL; 166 group.setLayoutData(data); 167 168 createSpacer(group, 3); 169 170 ssh2HomeLabel=new Label(group, SWT.NONE); 171 ssh2HomeLabel.setText(CVSSSH2Messages.CVSSSH2PreferencePage_23); 172 173 ssh2HomeText=new Text(group, SWT.SINGLE | SWT.BORDER); 174 ssh2HomeText.setFont(group.getFont()); 175 GridData gd=new GridData(GridData.FILL_HORIZONTAL); 176 gd.horizontalSpan=1; 177 ssh2HomeText.setLayoutData(gd); 178 179 ssh2HomeBrowse=new Button(group, SWT.NULL); 180 ssh2HomeBrowse.setText(CVSSSH2Messages.CVSSSH2PreferencePage_24); 181 gd=new GridData(GridData.HORIZONTAL_ALIGN_FILL); 182 gd.horizontalSpan=1; 183 ssh2HomeBrowse.setLayoutData(gd); 184 185 createSpacer(group, 3); 186 187 privateKeyLabel=new Label(group, SWT.NONE); 188 privateKeyLabel.setText(CVSSSH2Messages.CVSSSH2PreferencePage_25); 189 190 privateKeyText=new Text(group, SWT.SINGLE | SWT.BORDER); 191 privateKeyText.setFont(group.getFont()); 192 gd=new GridData(GridData.FILL_HORIZONTAL); 193 gd.horizontalSpan=1; 194 privateKeyText.setLayoutData(gd); 195 196 privateKeyAdd=new Button(group, SWT.NULL); 197 privateKeyAdd.setText(CVSSSH2Messages.CVSSSH2PreferencePage_26); 198 gd=new GridData(GridData.HORIZONTAL_ALIGN_FILL); 199 gd.horizontalSpan=1; 200 privateKeyAdd.setLayoutData(gd); 201 202 ssh2HomeBrowse.addSelectionListener(new SelectionAdapter(){ 203 public void widgetSelected(SelectionEvent e){ 204 String home=ssh2HomeText.getText(); 205 206 if(!new File (home).exists()){ 207 while(true){ 208 int foo=home.lastIndexOf(java.io.File.separator, home.length()); 209 if(foo==-1)break; 210 home=home.substring(0, foo); 211 if(new File (home).exists())break; 212 } 213 } 214 215 DirectoryDialog dd=new DirectoryDialog(getShell()); 216 dd.setFilterPath(home); 217 dd.setMessage(CVSSSH2Messages.CVSSSH2PreferencePage_27); 218 String dir=dd.open(); 219 if(dir==null){ return; 221 } 222 ssh2HomeText.setText(dir); 223 } 224 }); 225 226 privateKeyAdd.addSelectionListener(new SelectionAdapter(){ 227 public void widgetSelected(SelectionEvent e){ 228 String home=ssh2HomeText.getText(); 229 230 FileDialog fd=new FileDialog(getShell(), SWT.OPEN|SWT.MULTI); 231 fd.setFilterPath(home); 232 Object o=fd.open(); 233 if(o==null){ return; 235 } 236 String [] files=fd.getFileNames(); 237 String keys=privateKeyText.getText(); 238 String dir=fd.getFilterPath(); 239 if(dir.equals(home)){dir="";} else{dir+=java.io.File.separator;} 241 242 for(int i=0; i<files.length; i++){ 243 String foo=files[i]; 244 if(keys.length()!=0)keys=keys+","; keys=keys+dir+foo; 246 } 247 privateKeyText.setText(keys); 248 } 249 }); 250 251 return group; 252 } 253 254 private Control createKeyManagementPage(Composite parent) { 255 int columnSpan=3; 256 Composite group=new Composite(parent, SWT.NULL); 257 GridLayout layout=new GridLayout(); 258 layout.numColumns=3; 259 group.setLayout(layout); 260 GridData gd = new GridData(); 261 gd.horizontalAlignment = GridData.FILL; 262 group.setLayoutData(gd); 263 264 keyGenerateDSA=new Button(group, SWT.NULL); 265 keyGenerateDSA.setText(CVSSSH2Messages.CVSSSH2PreferencePage_131); 266 gd=new GridData(); 267 gd.horizontalSpan=1; 268 keyGenerateDSA.setLayoutData(gd); 269 270 keyGenerateRSA=new Button(group, SWT.NULL); 271 keyGenerateRSA.setText(CVSSSH2Messages.CVSSSH2PreferencePage_132); 272 gd=new GridData(); 273 gd.horizontalSpan=1; 274 keyGenerateRSA.setLayoutData(gd); 275 276 keyLoad=new Button(group, SWT.NULL); 277 keyLoad.setText(CVSSSH2Messages.CVSSSH2PreferencePage_128); 278 gd=new GridData(); 279 gd.horizontalSpan=1; 280 keyLoad.setLayoutData(gd); 281 282 publicKeylabel=new Label(group, SWT.NONE); 283 publicKeylabel.setText(CVSSSH2Messages.CVSSSH2PreferencePage_39); 284 gd=new GridData(); 285 gd.horizontalSpan=columnSpan; 286 publicKeylabel.setLayoutData(gd); 287 288 publicKeyText=new Text(group,SWT.MULTI|SWT.BORDER|SWT.V_SCROLL|SWT.WRAP); 289 publicKeyText.setText(""); publicKeyText.setEditable(false); 291 gd=new GridData(); 292 gd.horizontalSpan=columnSpan; 293 gd.horizontalAlignment = GridData.FILL; 294 gd.verticalAlignment = GridData.FILL; 295 gd.grabExcessHorizontalSpace = true; 296 gd.grabExcessVerticalSpace = true; 297 publicKeyText.setLayoutData(gd); 298 299 keyFingerPrintLabel=new Label(group, SWT.NONE); 300 keyFingerPrintLabel.setText(CVSSSH2Messages.CVSSSH2PreferencePage_41); 301 keyFingerPrintText=new Text(group, SWT.SINGLE | SWT.BORDER); 302 keyFingerPrintText.setFont(group.getFont()); 303 keyFingerPrintText.setEditable(false); 304 gd=new GridData(GridData.FILL_HORIZONTAL); 305 gd.horizontalSpan=2; 306 keyFingerPrintText.setLayoutData(gd); 307 308 keyCommentLabel=new Label(group, SWT.NONE); 309 keyCommentLabel.setText(CVSSSH2Messages.CVSSSH2PreferencePage_42); 310 keyCommentText=new Text(group, SWT.SINGLE | SWT.BORDER); 311 keyCommentText.setFont(group.getFont()); 312 gd=new GridData(GridData.FILL_HORIZONTAL); 313 gd.horizontalSpan=2; 314 keyCommentText.setLayoutData(gd); 315 316 keyCommentText.addModifyListener(new ModifyListener(){ 317 public void modifyText(ModifyEvent e){ 318 if(kpair==null)return; 319 try{ 320 ByteArrayOutputStream out=new ByteArrayOutputStream (); 321 kpair.writePublicKey(out, keyCommentText.getText()); 322 out.close(); 323 publicKeyText.setText(out.toString()); 324 } 325 catch(IOException ee){} 326 }}); 327 328 keyPassphrase1Label=new Label(group, SWT.NONE); 329 keyPassphrase1Label.setText(CVSSSH2Messages.CVSSSH2PreferencePage_43); 330 keyPassphrase1Text=new Text(group, SWT.SINGLE | SWT.BORDER); 331 keyPassphrase1Text.setFont(group.getFont()); 332 keyPassphrase1Text.setEchoChar('*'); 333 gd=new GridData(GridData.FILL_HORIZONTAL); 334 gd.horizontalSpan=2; 335 keyPassphrase1Text.setLayoutData(gd); 336 337 keyPassphrase2Label=new Label(group, SWT.NONE); 338 keyPassphrase2Label.setText(CVSSSH2Messages.CVSSSH2PreferencePage_44); 339 keyPassphrase2Text=new Text(group, SWT.SINGLE | SWT.BORDER); 340 keyPassphrase2Text.setFont(group.getFont()); 341 keyPassphrase2Text.setEchoChar('*'); 342 gd=new GridData(GridData.FILL_HORIZONTAL); 343 gd.horizontalSpan=2; 344 keyPassphrase2Text.setLayoutData(gd); 345 346 keyPassphrase1Text.addModifyListener(new ModifyListener(){ 347 public void modifyText(ModifyEvent e){ 348 String pass1=keyPassphrase1Text.getText(); 349 String pass2=keyPassphrase2Text.getText(); 350 if(kpair!=null && pass1.equals(pass2)){ 351 saveKeyPair.setEnabled(true); 352 } 353 else{ 354 saveKeyPair.setEnabled(false); 355 } 356 if(pass2.length()==0){ 357 setErrorMessage(null); 358 return; 359 } 360 if(pass1.equals(pass2)){ 361 setErrorMessage(null); 362 } 363 else{ 364 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_48); 365 } 366 } 367 }); 368 369 keyPassphrase2Text.addModifyListener(new ModifyListener(){ 370 public void modifyText(ModifyEvent e){ 371 String pass1=keyPassphrase1Text.getText(); 372 String pass2=keyPassphrase2Text.getText(); 373 if(kpair!=null && pass1.equals(pass2)){ 374 saveKeyPair.setEnabled(true); 375 } 376 else{ 377 saveKeyPair.setEnabled(false); 378 } 379 if(pass2.length()<pass1.length()){ 380 if(pass1.startsWith(pass2)){ 381 setErrorMessage(null); 382 } 383 else{ 384 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_48); 385 } 386 return; 387 } 388 if(pass1.equals(pass2)){ 389 setErrorMessage(null); 390 } 391 else{ 392 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_48); 393 } 394 } 395 }); 396 397 keyPassphrase2Text.addFocusListener(new FocusListener(){ 398 public void focusGained(FocusEvent e){ 399 String pass1=keyPassphrase1Text.getText(); 400 String pass2=keyPassphrase2Text.getText(); 401 if(pass2.length()<pass1.length()){ 402 if(pass1.startsWith(pass2)){ 403 setErrorMessage(null); 404 } 405 else{ 406 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_48); 407 } 408 return; 409 } 410 if(pass1.equals(pass2)){ 411 setErrorMessage(null); 412 } 413 else{ 414 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_48); 415 } 416 } 417 public void focusLost(FocusEvent e){ 418 String pass1=keyPassphrase1Text.getText(); 419 String pass2=keyPassphrase2Text.getText(); 420 if(pass1.equals(pass2)){ 421 setErrorMessage(null); 422 } 423 else{ 424 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_48); 425 } 426 } 427 }); 428 429 Composite buttons=new Composite(group, SWT.NONE); 430 layout=new GridLayout(2, true); 431 layout.marginWidth=0; 432 buttons.setLayout(layout); 433 gd=new GridData(GridData.HORIZONTAL_ALIGN_END); 434 gd.horizontalSpan=columnSpan; 435 buttons.setLayoutData(gd); 436 437 keyExport=new Button(buttons, SWT.NULL); 438 keyExport.setText(CVSSSH2Messages.CVSSSH2PreferencePage_105); 439 gd=new GridData(GridData.FILL_BOTH); 440 keyExport.setLayoutData(gd); 441 442 saveKeyPair=new Button(buttons, SWT.NULL); 443 saveKeyPair.setText(CVSSSH2Messages.CVSSSH2PreferencePage_45); 444 gd=new GridData(GridData.FILL_BOTH); 445 saveKeyPair.setLayoutData(gd); 446 447 SelectionAdapter keygenadapter=new SelectionAdapter(){ 448 public void widgetSelected(SelectionEvent e){ 449 JSch jsch=JSchSession.getJSch(); 450 boolean ok=true; 451 String _type=""; 453 try{ 454 int type=0; 455 if(e.widget==keyGenerateDSA){ 456 type=KeyPair.DSA; 457 _type=ISSHContants.DSA; 458 } 459 else if(e.widget==keyGenerateRSA){ 460 type=KeyPair.RSA; 461 _type=ISSHContants.RSA; 462 } 463 else{ 464 return; 465 } 466 467 final KeyPair[] _kpair=new KeyPair[1]; 468 final JSch _jsch=jsch; 469 final int __type=type; 470 final JSchException[] _e=new JSchException[1]; 471 BusyIndicator.showWhile(getShell().getDisplay(), 472 new Runnable (){ 473 public void run(){ 474 try{ 475 _kpair[0]=KeyPair.genKeyPair(_jsch, __type); 476 }catch(JSchException e){ 477 _e[0]=e; 478 } 479 }} 480 ); 481 if(_e[0]!=null){ 482 throw _e[0]; 483 } 484 kpair=_kpair[0]; 485 486 ByteArrayOutputStream out=new ByteArrayOutputStream (); 487 kpairComment=_type+"-1024"; kpair.writePublicKey(out, kpairComment); 489 out.close(); 490 publicKeyText.setText(out.toString()); 491 keyFingerPrintText.setText(kpair.getFingerPrint()); 492 keyCommentText.setText(kpairComment); 493 keyPassphrase1Text.setText(""); keyPassphrase2Text.setText(""); updateControls(); 496 } 497 catch(IOException ee){ 498 ok=false; 499 } 500 catch(JSchException ee){ 501 ok=false; 502 } 503 if(!ok){ 504 MessageDialog.openError(getShell(), 505 CVSSSH2Messages.CVSSSH2PreferencePage_error, 506 CVSSSH2Messages.CVSSSH2PreferencePage_47); 507 } 508 } 509 }; 510 keyGenerateDSA.addSelectionListener(keygenadapter); 511 keyGenerateRSA.addSelectionListener(keygenadapter); 512 513 keyLoad.addSelectionListener(new SelectionAdapter(){ 514 public void widgetSelected(SelectionEvent e){ 515 boolean ok=true; 516 String home=ssh2HomeText.getText(); 517 FileDialog fd=new FileDialog(getShell(), SWT.OPEN); 518 fd.setFilterPath(home); 519 Object o=fd.open(); 520 if(o==null){ return; 522 } 523 String pkey=fd.getFileName(); 524 String pkeyab=(new File (fd.getFilterPath(), pkey)).getAbsolutePath(); 525 try{ 526 JSch jsch=JSchSession.getJSch(); 527 KeyPair _kpair=KeyPair.load(jsch, pkeyab); 528 PassphrasePrompt prompt=null; 529 while(_kpair.isEncrypted()){ 530 if(prompt==null){ 531 prompt=new PassphrasePrompt(NLS.bind(CVSSSH2Messages.CVSSSH2PreferencePage_126, new String [] { pkey })); 532 } 533 Display.getDefault().syncExec(prompt); 534 String passphrase=prompt.getPassphrase(); 535 if(passphrase==null) break; 536 if(_kpair.decrypt(passphrase)){ 537 break; 538 } 539 MessageDialog.openError(getShell(), 540 CVSSSH2Messages.CVSSSH2PreferencePage_error, 541 NLS.bind(CVSSSH2Messages.CVSSSH2PreferencePage_129, new String [] { pkey })); 542 } 543 if(_kpair.isEncrypted()){ 544 return; 545 } 546 kpair=_kpair; 547 String _type=(kpair.getKeyType()==KeyPair.DSA)?ISSHContants.DSA:ISSHContants.RSA; 548 ByteArrayOutputStream out=new ByteArrayOutputStream (); 549 kpairComment=_type+"-1024"; kpair.writePublicKey(out, kpairComment); 551 out.close(); 552 publicKeyText.setText(out.toString()); 553 keyFingerPrintText.setText(kpair.getFingerPrint()); 554 keyCommentText.setText(kpairComment); 555 keyPassphrase1Text.setText(""); keyPassphrase2Text.setText(""); updateControls(); 558 } 559 catch(IOException ee){ 560 ok=false; 561 } 562 catch(JSchException ee){ 563 ok=false; 564 } 565 if(!ok){ 566 MessageDialog.openError(getShell(), 567 CVSSSH2Messages.CVSSSH2PreferencePage_error, 568 CVSSSH2Messages.CVSSSH2PreferencePage_130); 569 } 570 } 571 }); 572 573 keyExport.addSelectionListener(new SelectionAdapter(){ 574 public void widgetSelected(SelectionEvent e){ 575 if(kpair==null)return; 576 577 setErrorMessage(null); 578 579 final String [] target=new String [1]; 580 final String title=CVSSSH2Messages.CVSSSH2PreferencePage_106; 581 final String message=CVSSSH2Messages.CVSSSH2PreferencePage_107; 582 Display.getDefault().syncExec(new Runnable (){ 583 public void run(){ 584 Display display=Display.getCurrent(); 585 Shell shell=new Shell(display); 586 ExportDialog dialog=new ExportDialog(shell, title, message); 587 dialog.open(); 588 shell.dispose(); 589 target[0]=dialog.getTarget(); 590 }}); 591 if(target[0]==null){ 592 return; 593 } 594 String user=""; String host=""; int port=22; 597 598 if(target[0].indexOf('@')>0){ 599 user=target[0].substring(0, target[0].indexOf('@')); 600 host=target[0].substring(target[0].indexOf('@')+1); 601 } 602 if(host.indexOf(':')>0){ 603 try{port=Integer.parseInt(host.substring(host.indexOf(':')+1));} 604 catch(NumberFormatException ee) { 605 port=-1; 606 } 607 host=host.substring(0, host.indexOf(':')); 608 } 609 610 if(user.length()==0 || 611 host.length()==0 || 612 port==-1){ 613 setErrorMessage(NLS.bind(CVSSSH2Messages.CVSSSH2PreferencePage_108, new String [] { target[0] })); 614 return; 615 } 616 617 String options=""; try{ 619 ByteArrayOutputStream bos=new ByteArrayOutputStream (); 620 if(options.length()!=0){ 621 try{bos.write((options+" ").getBytes());} catch(IOException eeee){} 623 } 624 kpair.writePublicKey(bos, kpairComment); 625 bos.close(); 626 export_via_sftp(user, host, port, 627 ".ssh/authorized_keys", bos.toByteArray()); 629 } 630 catch(IOException ee){ 631 } 632 catch(JSchException ee){ 633 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_111); 634 } 635 }}); 636 637 saveKeyPair.addSelectionListener(new SelectionAdapter(){ 638 public void widgetSelected(SelectionEvent e){ 639 if(kpair==null)return; 640 641 String pass=keyPassphrase1Text.getText(); 642 648 if(pass.length()==0){ 649 if(!MessageDialog.openConfirm(getShell(), 650 CVSSSH2Messages.CVSSSH2PreferencePage_confirmation, 651 CVSSSH2Messages.CVSSSH2PreferencePage_49 652 )){ 653 return ; 654 } 655 } 656 657 kpair.setPassphrase(pass); 658 659 String home=ssh2HomeText.getText(); 660 661 File _home=new File (home); 662 663 if(!_home.exists()){ 664 if(!MessageDialog.openConfirm(getShell(), 665 CVSSSH2Messages.CVSSSH2PreferencePage_confirmation, 666 NLS.bind(CVSSSH2Messages.CVSSSH2PreferencePage_50, new String [] { home }) 667 )){ 668 return ; 669 } 670 if(!_home.mkdirs()){ 671 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_100+home); 672 return; 673 } 674 } 675 676 FileDialog fd=new FileDialog(getShell(), SWT.SAVE); 677 fd.setFilterPath(home); 678 String file=(kpair.getKeyType()==KeyPair.RSA) ? "id_rsa" : "id_dsa"; fd.setFileName(file); 680 file=fd.open(); 681 if(file==null){ return; 683 } 684 685 if(new File (file).exists()){ 686 if(!MessageDialog.openConfirm(getShell(), 687 CVSSSH2Messages.CVSSSH2PreferencePage_confirmation, NLS.bind(CVSSSH2Messages.CVSSSH2PreferencePage_53, new String [] { file }) 689 )){ 690 return; 691 } 692 } 693 694 boolean ok=true; 695 try{ 696 kpair.writePrivateKey(file); 697 kpair.writePublicKey(file+".pub", kpairComment); } 699 catch(Exception ee){ 700 ok=false; 701 } 702 703 if(ok){ 704 MessageDialog.openInformation(getShell(), 705 CVSSSH2Messages.CVSSSH2PreferencePage_information, 706 CVSSSH2Messages.CVSSSH2PreferencePage_55+ 707 "\n"+ CVSSSH2Messages.CVSSSH2PreferencePage_57+file+ 709 "\n"+ CVSSSH2Messages.CVSSSH2PreferencePage_59+ 711 file+ 712 ".pub"); } 714 } 715 }); 716 717 return group; 718 } 719 720 private TableViewer viewer; 721 private Button removeHostKeyButton; 722 class TableLabelProvider extends LabelProvider implements ITableLabelProvider { 723 public String getColumnText(Object element, int columnIndex) { 724 HostKey entry = (HostKey)element; 725 switch (columnIndex) { 726 case 0: 727 return entry.getHost(); 728 case 1: 729 return entry.getType(); 730 case 2: 731 return entry.getFingerPrint(JSchSession.getJSch()); 732 default: 733 return null; 734 } 735 } 736 public Image getColumnImage(Object element, int columnIndex) { 737 return null; 738 } 739 }; 740 741 private Control createHostKeyManagementPage(Composite parent) { 742 Composite group=new Composite(parent, SWT.NULL); 743 GridLayout layout=new GridLayout(); 744 layout.marginWidth = 0; 745 layout.marginHeight = 0; 746 layout.numColumns = 2; 747 group.setLayout(layout); 748 GridData gd = new GridData(); 749 gd.horizontalAlignment = GridData.FILL; 750 gd.verticalAlignment = GridData.FILL; 751 group.setLayoutData(gd); 752 753 Label label=new Label(group, SWT.NONE); 754 label.setText(CVSSSH2Messages.CVSSSH2PreferencePage_139); 755 gd=new GridData(); 756 gd.horizontalSpan=2; 757 label.setLayoutData(gd); 758 759 viewer = new TableViewer(group, SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 760 Table table = viewer.getTable(); 761 new TableEditor(table); 762 table.setHeaderVisible(true); 763 table.setLinesVisible(true); 764 gd = new GridData(GridData.FILL_BOTH); 765 gd.widthHint = convertWidthInCharsToPixels(30); 766 772 gd.heightHint = 100; 773 table.setLayoutData(gd); 774 table.addListener(SWT.Selection, new Listener() { 775 public void handleEvent(Event e) { 776 handleSelection(); 777 } 778 }); 779 new TableColumn(table, SWT.NULL); 781 new TableColumn(table, SWT.NULL); 782 new TableColumn(table, SWT.NULL); 783 TableColumn[] columns = table.getColumns(); 784 columns[0].setText(CVSSSH2Messages.CVSSSH2PreferencePage_134); 785 columns[1].setText(CVSSSH2Messages.CVSSSH2PreferencePage_135); 786 columns[2].setText(CVSSSH2Messages.CVSSSH2PreferencePage_136); 787 viewer.setColumnProperties(new String [] { 788 CVSSSH2Messages.CVSSSH2PreferencePage_134, CVSSSH2Messages.CVSSSH2PreferencePage_135, CVSSSH2Messages.CVSSSH2PreferencePage_136}); 791 viewer.setLabelProvider(new TableLabelProvider()); 792 viewer.setContentProvider(new IStructuredContentProvider() { 793 public void dispose() { 794 } 795 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 796 } 797 public Object [] getElements(Object inputElement) { 798 if (inputElement == null) return null; 799 return (Object [])inputElement; 800 } 801 }); 802 TableLayout tl = new TableLayout(); 803 tl.addColumnData(new ColumnWeightData(30)); 804 tl.addColumnData(new ColumnWeightData(20)); 805 tl.addColumnData(new ColumnWeightData(70)); 806 table.setLayout(tl); 807 808 Composite buttons = new Composite(group, SWT.NULL); 809 buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 810 buttons.setLayout(new GridLayout()); 811 812 removeHostKeyButton = new Button(buttons, SWT.PUSH); 813 removeHostKeyButton.setText(CVSSSH2Messages.CVSSSH2PreferencePage_138); 814 int buttonWidth= SWTUtils.calculateControlSize(SWTUtils.createDialogPixelConverter(parent), new Button [] { removeHostKeyButton }); 815 removeHostKeyButton.setLayoutData(SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.END, SWT.CENTER, false, false)); 816 removeHostKeyButton.setEnabled(false); 817 removeHostKeyButton.addListener(SWT.Selection, new Listener() { 818 public void handleEvent(Event e) { 819 removeHostKey(); 820 } 821 }); 822 823 Dialog.applyDialogFont(parent); 824 825 JSchSession.loadKnownHosts(); 826 HostKeyRepository hkr=JSchSession.getJSch().getHostKeyRepository(); 827 viewer.setInput(hkr.getHostKey()); 828 handleSelection(); 829 830 return group; 831 } 832 833 private void handleSelection() { 834 boolean empty = viewer.getSelection().isEmpty(); 835 removeHostKeyButton.setEnabled(!empty); 836 } 837 private void removeHostKey(){ 838 IStructuredSelection selection = (IStructuredSelection)viewer.getSelection(); 839 HostKeyRepository hkr=JSchSession.getJSch().getHostKeyRepository(); 840 for (Iterator iterator = selection.iterator(); iterator.hasNext();) { 841 HostKey hostkey = (HostKey) iterator.next(); 842 hkr.remove(hostkey.getHost(), hostkey.getType()); 843 viewer.remove(hostkey); 844 } 845 } 846 private void export_via_sftp(String user, String host, int port, String target, byte[] pkey) throws JSchException{ 847 try{ 848 849 866 867 IProgressMonitor pm=new org.eclipse.core.runtime.NullProgressMonitor(); 868 Session session=JSchSession.getSession(null, user, "", host, port, pm).getSession(); if(session.getServerVersion().indexOf("OpenSSH")==-1){ setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_110); 871 return; 872 } 873 Channel channel=session.openChannel("sftp"); channel.connect(); 875 ChannelSftp c=(ChannelSftp)channel; 876 877 c.pwd(); SftpATTRS attr=null; 879 880 try{ attr=c.stat(".ssh"); } catch(SftpException ee){ } 882 if(attr==null){ 883 try{ c.mkdir(".ssh"); } catch(SftpException ee){ 885 setErrorMessage(ee.message); 886 return; 887 } 888 } 889 try{ c.cd(".ssh"); } catch(SftpException ee){ 891 setErrorMessage(ee.message); 892 return; 893 } 894 895 try{ 896 ByteArrayInputStream bis=new ByteArrayInputStream (pkey); 897 c.put(bis, "authorized_keys", null, ChannelSftp.APPEND); bis.close(); 899 checkPermission(c, "authorized_keys"); checkPermission(c, "."); c.cd(".."); checkPermission(c, "."); } 904 catch(SftpException ee){ 905 } 907 908 MessageDialog.openInformation(getShell(), 909 CVSSSH2Messages.CVSSSH2PreferencePage_information, 910 NLS.bind(CVSSSH2Messages.CVSSSH2PreferencePage_109, 911 (user+"@"+host+(port==22 ? "" : ":"+port)+":~/.ssh/authorized_keys"))); 913 c.disconnect(); 914 } 916 catch(IOException eee){ 917 setErrorMessage(eee.toString()); 918 } 919 } 920 921 private void checkPermission(ChannelSftp c, String path) throws SftpException{ 922 SftpATTRS attr=c.stat(path); 923 int permissions=attr.getPermissions(); 924 if((permissions&00022)!=0){ 925 permissions&=~00022; 926 c.chmod(permissions,path); 927 } 928 } 929 private void updateControls() { 930 boolean enable=(kpair!=null); 931 publicKeylabel.setEnabled(enable); 932 publicKeyText.setEnabled(enable); 933 keyFingerPrintLabel.setEnabled(enable); 934 keyFingerPrintText.setEnabled(enable); 935 keyCommentLabel.setEnabled(enable); 936 keyCommentText.setEnabled(enable); 937 keyPassphrase1Label.setEnabled(enable); 938 keyPassphrase1Text.setEnabled(enable); 939 keyPassphrase2Label.setEnabled(enable); 940 keyPassphrase2Text.setEnabled(enable); 941 keyExport.setEnabled(enable); 942 saveKeyPair.setEnabled(enable); 943 } 944 945 public void init(IWorkbench workbench) { 946 } 949 950 public void initialize() { 951 initControls(); 952 } 953 954 private void initControls(){ 955 IPreferenceStore store=CVSSSH2Plugin.getDefault().getPreferenceStore(); 956 ssh2HomeText.setText(store.getString(ISSHContants.KEY_SSH2HOME)); 957 privateKeyText.setText(store.getString(ISSHContants.KEY_PRIVATEKEY)); 958 updateControls(); 959 } 960 public boolean performOk() { 961 boolean result = super.performOk(); 962 if (result) { 963 setErrorMessage(null); 964 String home = ssh2HomeText.getText(); 965 File _home = new File (home); 966 if (!_home.exists()) { 967 if (MessageDialog.openQuestion(getShell(), CVSSSH2Messages.CVSSSH2PreferencePage_question, 968 NLS.bind(CVSSSH2Messages.CVSSSH2PreferencePage_99, new String [] { home }) 969 )) { 970 if (!(_home.mkdirs())) { 971 setErrorMessage(CVSSSH2Messages.CVSSSH2PreferencePage_100 + home); 972 return false; 973 } 974 } 975 } 976 977 IPreferenceStore store = CVSSSH2Plugin.getDefault().getPreferenceStore(); 978 store.setValue(ISSHContants.KEY_SSH2HOME, home); 979 store.setValue(ISSHContants.KEY_PRIVATEKEY, privateKeyText.getText()); 980 } 981 CVSSSH2Plugin.getDefault().savePluginPreferences(); 982 return result; 983 } 984 985 public void performApply() { 986 performOk(); 987 } 988 989 protected void performDefaults(){ 990 super.performDefaults(); 991 IPreferenceStore store = CVSSSH2Plugin.getDefault().getPreferenceStore(); 992 store.setToDefault(ISSHContants.KEY_SSH2HOME); 993 store.setToDefault(ISSHContants.KEY_PRIVATEKEY); 994 store.setToDefault(ISSHContants.KEY_PROXY); 995 store.setToDefault(ISSHContants.KEY_PROXY_TYPE); 996 store.setToDefault(ISSHContants.KEY_PROXY_HOST); 997 store.setToDefault(ISSHContants.KEY_PROXY_PORT); 998 store.setToDefault(ISSHContants.KEY_PROXY_AUTH); 999 initControls(); 1000 updateControls(); 1001 } 1002 1003 protected void createSpacer(Composite composite, int columnSpan) { 1004 Label label=new Label(composite, SWT.NONE); 1005 GridData gd=new GridData(); 1006 gd.horizontalSpan=columnSpan; 1007 label.setLayoutData(gd); 1008 } 1009} 1010 1011class ExportDialog extends Dialog { 1012 protected Text field; 1013 protected String target=null; 1014 protected String title=null; 1015 protected String message=null; 1016 1017 public ExportDialog(Shell parentShell, String title, String message) { 1018 super(parentShell); 1019 this.title=title; 1020 this.message=message; 1021 } 1022 1023 protected void configureShell(Shell newShell) { 1024 super.configureShell(newShell); 1025 newShell.setText(title); 1026 } 1027 1028 public void create() { 1029 super.create(); 1030 field.setFocus(); 1031 } 1032 1033 protected Control createDialogArea(Composite parent) { 1034 1035 parent = new Composite(parent, SWT.NONE); 1036 GridLayout layout = new GridLayout(); 1037 layout.numColumns = 1; 1038 parent.setLayout(layout); 1039 parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 1040 1041 Composite main=new Composite(parent, SWT.NONE); 1042 layout=new GridLayout(); 1043 layout.numColumns=3; 1044 main.setLayout(layout); 1045 main.setLayoutData(new GridData(GridData.FILL_BOTH)); 1046 1047 if (message!=null) { 1048 Label messageLabel=new Label(main, SWT.WRAP); 1049 messageLabel.setText(message); 1050 GridData data=new GridData(GridData.FILL_HORIZONTAL); 1051 data.horizontalSpan=3; 1052 messageLabel.setLayoutData(data); 1053 } 1054 1055 createTargetFields(main); 1056 Dialog.applyDialogFont(main); 1057 return main; 1058 } 1059 1060 protected void createTargetFields(Composite parent) { 1061 new Label(parent, SWT.NONE).setText(CVSSSH2Messages.CVSSSH2PreferencePage_125); 1062 1063 field=new Text(parent, SWT.BORDER); 1064 GridData data=new GridData(GridData.FILL_HORIZONTAL); 1065 data.widthHint=convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 1066 data.horizontalSpan=2; 1067 field.setLayoutData(data); 1068 } 1069 1070 public String getTarget() { 1071 return target; 1072 } 1073 1074 protected void okPressed() { 1075 String _target=field.getText(); 1076 if(_target==null || _target.length()==0){ 1077 return; 1078 } 1079 target=_target; 1080 super.okPressed(); 1081 } 1082 1083 protected void cancelPressed() { 1084 target=null; 1085 super.cancelPressed(); 1086 } 1087} 1088 1089class PassphrasePrompt implements Runnable { 1090 private String message; 1091 private String passphrase; 1092 PassphrasePrompt(String message){ 1093 this.message=message; 1094 } 1095 public void run(){ 1096 Display display=Display.getCurrent(); 1097 Shell shell=new Shell(display); 1098 PassphraseDialog dialog=new PassphraseDialog(shell, message); 1099 dialog.open(); 1100 shell.dispose(); 1101 passphrase=dialog.getPassphrase(); 1102 } 1103 public String getPassphrase(){ 1104 return passphrase; 1105 } 1106} 1107 1108class PassphraseDialog extends Dialog { 1109 protected Text passphraseField; 1110 protected String passphrase = null; 1111 protected String message = null; 1112 1113 public PassphraseDialog(Shell parentShell, String message) { 1114 super(parentShell); 1115 this.message = message; 1116 } 1117 1118 protected void configureShell(Shell newShell) { 1119 super.configureShell(newShell); 1120 newShell.setText(message); 1121 } 1122 1123 public void create() { 1124 super.create(); 1125 passphraseField.setFocus(); 1126 } 1127 1128 protected Control createDialogArea(Composite parent) { 1129 Composite main=new Composite(parent, SWT.NONE); 1130 1131 GridLayout layout=new GridLayout(); 1132 layout.numColumns=3; 1133 main.setLayout(layout); 1134 main.setLayoutData(new GridData(GridData.FILL_BOTH)); 1135 1136 if (message!=null) { 1137 Label messageLabel=new Label(main, SWT.WRAP); 1138 messageLabel.setText(message); 1139 GridData data=new GridData(GridData.FILL_HORIZONTAL); 1140 data.horizontalSpan=3; 1141 messageLabel.setLayoutData(data); 1142 } 1143 1144 createPassphraseFields(main); 1145 return main; 1146 } 1147 1148 protected void createPassphraseFields(Composite parent) { 1149 new Label(parent, SWT.NONE).setText(CVSSSH2Messages.CVSSSH2PreferencePage_127); 1150 passphraseField=new Text(parent, SWT.BORDER); 1151 GridData data=new GridData(GridData.FILL_HORIZONTAL); 1152 data.widthHint=convertHorizontalDLUsToPixels(IDialogConstants.ENTRY_FIELD_WIDTH); 1153 passphraseField.setLayoutData(data); 1154 passphraseField.setEchoChar('*'); 1155 1156 new Label(parent, SWT.NONE); 1157 } 1158 1159 public String getPassphrase() { 1160 return passphrase; 1161 } 1162 1163 protected void okPressed() { 1164 String _passphrase = passphraseField.getText(); 1165 if(_passphrase==null || _passphrase.length()==0){ 1166 return; 1167 } 1168 passphrase=_passphrase; 1169 super.okPressed(); 1170 } 1171 protected void cancelPressed() { 1172 passphrase=null; 1173 super.cancelPressed(); 1174 } 1175} 1176 | Popular Tags |