1 16 package net.sf.jftp.net; 17 18 import java.io.BufferedInputStream ; 19 import java.io.BufferedOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.StreamTokenizer ; 26 import java.util.Date ; 27 import java.util.Vector ; 28 29 import jcifs.smb.NtlmAuthenticator; 30 import jcifs.smb.NtlmPasswordAuthentication; 31 import jcifs.smb.SmbAuthException; 32 import jcifs.smb.SmbFile; 33 import jcifs.smb.SmbFileOutputStream; 34 import net.sf.jftp.config.Settings; 35 import net.sf.jftp.system.StringUtils; 36 import net.sf.jftp.system.logging.Log; 37 38 39 public class SmbConnection extends NtlmAuthenticator implements BasicConnection 40 { 41 public static int smbBuffer = 128000; 42 private String path = ""; 43 private String pwd = "smb://"; 44 private Vector listeners = new Vector (); 45 private String [] files; 46 private String [] size = new String [0]; 47 private int[] perms = null; 48 private String user; 49 private String pass; 50 private String host; 51 private String domain = null; 52 private String baseFile; 53 private int fileCount; 54 private boolean isDirUpload = false; 55 private boolean shortProgress = false; 56 private boolean dummy = false; 57 58 public SmbConnection() 59 { 60 } 61 62 public SmbConnection(String url, String domain, String user, String pass, 63 ConnectionListener l) 64 { 65 if(l != null) 66 { 67 listeners.add(l); 68 } 69 70 this.user = user; 71 this.pass = pass; 72 75 if(domain.equals("NONE")) 77 { 78 domain = null; 79 } 80 81 this.domain = domain; 82 83 if(url.equals("(LAN)")) 85 { 86 chdir(getPWD()); 87 } 88 else 89 { 90 chdir(url); 91 } 92 93 } 95 96 protected NtlmPasswordAuthentication getNtlmPasswordAuthentication() 97 { 98 try 100 { 101 return new NtlmPasswordAuthentication(domain, user, pass); 102 } 103 catch(Exception ex) 104 { 105 Log.debug("Error logging in: " + ex); 106 ex.printStackTrace(); 107 108 return null; 109 } 110 } 111 112 private NtlmPasswordAuthentication getAuth() 113 { 114 return getNtlmPasswordAuthentication(); 115 } 116 117 public int removeFileOrDir(String file) 118 { 119 file = toSMB(file); 120 121 try 123 { 124 SmbFile f = new SmbFile(file, getAuth()); 125 126 if(f.exists() && f.isDirectory()) 127 { 128 cleanSmbDir(file); 129 } 130 131 f.delete(); 132 } 133 catch(Exception ex) 134 { 135 Log.debug("Removal failed (" + ex + ")."); 136 137 return -1; 138 } 139 140 return 1; 141 } 142 143 private void cleanSmbDir(String dir) throws Exception 144 { 145 dir = toSMB(dir); 146 147 SmbFile f2 = new SmbFile(dir, getAuth()); 148 String [] tmp = f2.list(); 149 150 if(tmp == null) 151 { 152 return; 153 } 154 155 for(int i = 0; i < tmp.length; i++) 156 { 157 SmbFile f3 = new SmbFile(dir + tmp[i], getAuth()); 158 159 if(f3.isDirectory()) 160 { 161 cleanSmbDir(dir + tmp[i]); 163 f3.delete(); 164 } 165 else 166 { 167 f3.delete(); 169 } 170 } 171 } 172 173 public void sendRawCommand(String cmd) 174 { 175 } 176 177 public void disconnect() 178 { 179 } 180 181 public boolean isConnected() 182 { 183 return true; 184 } 185 186 public String getPWD() 187 { 188 return toSMB(pwd); 190 } 191 192 public boolean cdup() 193 { 194 String tmp = pwd; 195 196 if(pwd.endsWith("/") && !pwd.equals("smb://")) 197 { 198 tmp = pwd.substring(0, pwd.lastIndexOf("/")); 199 } 200 201 return chdir(tmp.substring(0, tmp.lastIndexOf("/") + 1)); 202 } 203 204 public boolean mkdir(String dirName) 205 { 206 try 207 { 208 if(!dirName.endsWith("/")) 209 { 210 dirName = dirName + "/"; 211 } 212 213 dirName = toSMB(dirName); 214 215 SmbFile f = new SmbFile(dirName, getAuth()); 216 f.mkdir(); 217 218 fireDirectoryUpdate(); 219 220 return true; 221 } 222 catch(Exception ex) 223 { 224 Log.debug("Failed to create directory (" + ex + ")."); 225 226 return false; 227 } 228 } 229 230 public void list() throws IOException 231 { 232 } 233 234 public boolean chdir(String p) 235 { 236 return chdir(p, true); 237 } 238 239 public boolean chdir(String p, boolean refresh) 240 { 241 try 242 { 243 if(p.endsWith("..")) 244 { 245 return cdup(); 246 } 247 248 String tmp = toSMB(p); 249 250 if(!tmp.endsWith("/")) 251 { 252 tmp = tmp + "/"; 253 } 254 255 SmbFile f = new SmbFile(tmp, getAuth()); 257 f.list(); 258 259 pwd = tmp; 260 261 if(refresh) 264 { 265 fireDirectoryUpdate(); 266 } 267 268 dummy = false; 270 271 return true; 272 } 273 catch(Exception ex) 274 { 275 if((ex.getMessage().indexOf("MSBROWSE") > 0) && !dummy) 276 { 277 Log.debug("\nCould not find a master server."); 278 Log.debug("Please make sure you have the local IP set to the interface you want to use, and if"); 279 Log.debug("that does not work try \"<default>\"..."); 280 Log.debug("If you still can not find a master make sure that there is one your LAN and submit a bug report."); 281 dummy = true; 282 } 283 else 284 { 285 Log.debug("Could not change directory (" + ex + ")."); 286 } 287 288 return false; 289 } 290 } 291 292 public boolean chdirNoRefresh(String p) 293 { 294 return chdir(p, false); 295 } 296 297 public String getLocalPath() 298 { 299 return path; 300 } 301 302 public boolean setLocalPath(String p) 303 { 304 if(StringUtils.isRelative(p)) 305 { 306 p = path + p; 307 } 308 309 p = p.replace('\\', '/'); 310 311 File f = new File (p); 313 314 if(f.exists()) 315 { 316 try 317 { 318 path = f.getCanonicalPath(); 319 path = path.replace('\\', '/'); 320 321 if(!path.endsWith("/")) 322 { 323 path = path + "/"; 324 } 325 326 } 328 catch(IOException ex) 329 { 330 Log.debug("Error: can not get pathname (local)!"); 331 332 return false; 333 } 334 } 335 else 336 { 337 Log.debug("(local) No such path: \"" + p + "\""); 338 339 return false; 340 } 341 342 return true; 343 } 344 345 public String [] sortLs() 346 { 347 try 348 { 349 chdirNoRefresh(getPWD()); 351 352 SmbFile fx = new SmbFile(pwd, getAuth()); 353 354 SmbFile[] f = fx.listFiles(); 357 358 files = new String [f.length]; 361 size = new String [f.length]; 362 perms = new int[f.length]; 363 364 int i; 365 366 for(i = 0; i < f.length; i++) 367 { 368 files[i] = f[i].toURL().toString(); 369 370 int x = 0; 371 372 for(int j = 0; j < files[i].length(); j++) 373 { 374 if(files[i].charAt(j) == '/') 375 { 376 x++; 377 } 378 } 379 380 if(files[i].endsWith("/") && (x > 3)) 381 { 382 files[i] = StringUtils.getDir(files[i]); 383 } 384 else if(x > 3) 385 { 386 files[i] = StringUtils.getFile(files[i]); 387 } 388 389 size[i] = "" + f[i].length(); 390 391 if(f[i].canRead()) 392 { 393 perms[i] = FtpConnection.R; 394 } 395 else if(f[i].canWrite()) 396 { 397 perms[i] = FtpConnection.R; 398 } 399 else 400 { 401 perms[i] = FtpConnection.DENIED; 402 } 403 } 404 405 return files; 407 } 408 catch(Exception ex) 409 { 410 if(ex instanceof SmbAuthException) 411 { 412 Log.debug("Access denied: " + ex); 413 414 } 418 else 419 { 420 Log.debug("Error: " + ex); 421 } 422 423 return new String [0]; 424 } 425 } 426 427 public String [] sortSize() 428 { 429 return size; 430 } 431 432 public int[] getPermissions() 433 { 434 return perms; 435 } 436 437 public int handleUpload(String f) 438 { 439 if(Settings.getEnableSmbMultiThreading()) 440 { 441 SmbTransfer t = new SmbTransfer(getPWD(), getLocalPath(), f, user, 442 pass, domain, listeners, 443 Transfer.UPLOAD); 444 } 445 else 446 { 447 upload(f); 448 } 449 450 return 0; 451 } 452 453 public int handleDownload(String f) 454 { 455 if(Settings.getEnableSmbMultiThreading()) 456 { 457 SmbTransfer t = new SmbTransfer(getPWD(), getLocalPath(), f, user, 458 pass, domain, listeners, 459 Transfer.DOWNLOAD); 460 } 461 else 462 { 463 download(f); 464 } 465 466 return 0; 467 } 468 469 public int upload(String f) 470 { 471 String file = toSMB(f); 472 473 if(file.endsWith("/")) 474 { 475 String out = StringUtils.getDir(file); 476 uploadDir(file, getLocalPath() + out); 477 fireActionFinished(this); 478 } 479 else 480 { 481 String outfile = StringUtils.getFile(file); 482 483 work(getLocalPath() + outfile, file); 485 fireActionFinished(this); 486 } 487 488 return 0; 489 } 490 491 public int download(String f) 492 { 493 String file = toSMB(f); 494 495 if(file.endsWith("/")) 496 { 497 String out = StringUtils.getDir(file); 498 downloadDir(file, getLocalPath() + out); 499 fireActionFinished(this); 500 } 501 else 502 { 503 String outfile = StringUtils.getFile(file); 504 505 work(file, getLocalPath() + outfile); 507 fireActionFinished(this); 508 } 509 510 return 0; 511 } 512 513 private void downloadDir(String dir, String out) 514 { 515 try 516 { 517 fileCount = 0; 519 shortProgress = true; 520 baseFile = StringUtils.getDir(dir); 521 522 SmbFile f2 = new SmbFile(dir, getAuth()); 523 String [] tmp = f2.list(); 524 525 if(tmp == null) 526 { 527 return; 528 } 529 530 File fx = new File (out); 531 fx.mkdir(); 532 533 for(int i = 0; i < tmp.length; i++) 534 { 535 tmp[i] = tmp[i].replace('\\', '/'); 536 537 SmbFile f3 = new SmbFile(dir + tmp[i], getAuth()); 539 540 if(f3.isDirectory()) 541 { 542 if(!tmp[i].endsWith("/")) 543 { 544 tmp[i] = tmp[i] + "/"; 545 } 546 547 downloadDir(dir + tmp[i], out + tmp[i]); 548 } 549 else 550 { 551 fileCount++; 552 fireProgressUpdate(baseFile, 553 DataConnection.GETDIR + ":" + fileCount, 554 -1); 555 work(dir + tmp[i], out + tmp[i]); 556 } 557 } 558 559 fireProgressUpdate(baseFile, 560 DataConnection.DFINISHED + ":" + fileCount, -1); 561 } 562 catch(Exception ex) 563 { 564 ex.printStackTrace(); 565 566 Log.debug("Transfer error: " + ex); 568 fireProgressUpdate(baseFile, 569 DataConnection.FAILED + ":" + fileCount, -1); 570 } 571 572 shortProgress = false; 573 } 574 575 private void uploadDir(String dir, String out) 576 { 577 try 578 { 579 isDirUpload = true; 581 fileCount = 0; 582 shortProgress = true; 583 baseFile = StringUtils.getDir(dir); 584 585 File f2 = new File (out); 586 String [] tmp = f2.list(); 587 588 if(tmp == null) 589 { 590 return; 591 } 592 593 SmbFile fx = new SmbFile(dir, getAuth()); 594 fx.mkdir(); 595 596 for(int i = 0; i < tmp.length; i++) 597 { 598 tmp[i] = tmp[i].replace('\\', '/'); 599 600 File f3 = new File (out + tmp[i]); 602 603 if(f3.isDirectory()) 604 { 605 if(!tmp[i].endsWith("/")) 606 { 607 tmp[i] = tmp[i] + "/"; 608 } 609 610 uploadDir(dir + tmp[i], out + tmp[i]); 611 } 612 else 613 { 614 fileCount++; 615 fireProgressUpdate(baseFile, 616 DataConnection.PUTDIR + ":" + fileCount, 617 -1); 618 work(out + tmp[i], dir + tmp[i]); 619 } 620 } 621 622 fireProgressUpdate(baseFile, 623 DataConnection.DFINISHED + ":" + fileCount, -1); 624 } 625 catch(Exception ex) 626 { 627 ex.printStackTrace(); 628 629 Log.debug("Transfer error: " + ex); 631 fireProgressUpdate(baseFile, 632 DataConnection.FAILED + ":" + fileCount, -1); 633 } 634 635 isDirUpload = false; 636 shortProgress = true; 637 } 638 639 private String toSMB(String f) 640 { 641 String file; 642 643 if(f.lastIndexOf("smb://") > 0) 644 { 645 f = f.substring(f.lastIndexOf("smb://")); 646 } 647 648 if(f.startsWith("smb://")) 649 { 650 file = f; 651 } 652 else 653 { 654 file = getPWD() + f; 655 } 656 657 file = file.replace('\\', '/'); 658 659 return file; 661 } 662 663 private void work(String file, String outfile) 664 { 665 BufferedOutputStream out = null; 666 BufferedInputStream in = null; 667 668 try 669 { 670 boolean outflag = false; 671 672 if(outfile.startsWith("smb://")) 673 { 674 outflag = true; 675 out = new BufferedOutputStream (new SmbFileOutputStream(new SmbFile(outfile, 676 getAuth()))); 677 } 678 else 679 { 680 out = new BufferedOutputStream (new FileOutputStream (outfile)); 681 } 682 683 if(file.startsWith("smb://")) 685 { 686 in = new BufferedInputStream (new SmbFile(file, getAuth()).getInputStream()); 687 } 688 else 689 { 690 in = new BufferedInputStream (new FileInputStream (file)); 691 } 692 693 byte[] buf = new byte[smbBuffer]; 694 int len = 0; 695 int reallen = 0; 696 697 while(true) 699 { 700 len = in.read(buf); 701 702 if(len == StreamTokenizer.TT_EOF) 704 { 705 break; 706 } 707 708 out.write(buf, 0, len); 709 reallen += len; 710 711 if(outflag) 713 { 714 fireProgressUpdate(StringUtils.getFile(outfile), 715 DataConnection.PUT, reallen); 716 } 717 else 718 { 719 fireProgressUpdate(StringUtils.getFile(file), 720 DataConnection.GET, reallen); 721 } 722 } 723 724 fireProgressUpdate(file, DataConnection.FINISHED, -1); 725 } 726 catch(IOException ex) 727 { 728 Log.debug("Error with file IO (" + ex + ")!"); 729 fireProgressUpdate(file, DataConnection.FAILED, -1); 730 } 731 finally 732 { 733 try 734 { 735 out.flush(); 736 out.close(); 737 in.close(); 738 } 739 catch(Exception ex) 740 { 741 ex.printStackTrace(); 742 } 743 } 744 } 745 746 private void update(String file, String type, int bytes) 747 { 748 if(listeners == null) 749 { 750 return; 751 } 752 else 753 { 754 for(int i = 0; i < listeners.size(); i++) 755 { 756 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i); 757 listener.updateProgress(file, type, bytes); 758 } 759 } 760 } 761 762 public void addConnectionListener(ConnectionListener l) 763 { 764 listeners.add(l); 765 } 766 767 public void setConnectionListeners(Vector l) 768 { 769 listeners = l; 770 } 771 772 773 public void fireDirectoryUpdate() 774 { 775 if(listeners == null) 776 { 777 return; 778 } 779 else 780 { 781 for(int i = 0; i < listeners.size(); i++) 782 { 783 ((ConnectionListener) listeners.elementAt(i)).updateRemoteDirectory(this); 784 } 785 } 786 } 787 788 public boolean login(String user, String pass) 789 { 790 return true; 791 } 792 793 794 public void fireProgressUpdate(String file, String type, int bytes) 795 { 796 if(listeners == null) 798 { 799 return; 800 } 801 else 802 { 803 for(int i = 0; i < listeners.size(); i++) 804 { 805 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i); 806 807 if(shortProgress && Settings.shortProgress) 808 { 809 if(type.startsWith(DataConnection.DFINISHED)) 810 { 811 listener.updateProgress(baseFile, 812 DataConnection.DFINISHED + ":" + 813 fileCount, bytes); 814 } 815 else if(isDirUpload) 816 { 817 listener.updateProgress(baseFile, 818 DataConnection.PUTDIR + ":" + 819 fileCount, bytes); 820 } 821 else 822 { 823 listener.updateProgress(baseFile, 824 DataConnection.GETDIR + ":" + 825 fileCount, bytes); 826 } 827 } 828 else 829 { 830 listener.updateProgress(file, type, bytes); 831 } 832 } 833 } 834 } 835 836 public void fireActionFinished(SmbConnection con) 837 { 838 if(listeners == null) 839 { 840 return; 841 } 842 else 843 { 844 for(int i = 0; i < listeners.size(); i++) 845 { 846 ((ConnectionListener) listeners.elementAt(i)).actionFinished(con); 847 } 848 } 849 } 850 851 public int upload(String file, InputStream i) 852 { 853 BufferedOutputStream out = null; 854 BufferedInputStream in = null; 855 856 try 857 { 858 file = toSMB(file); 859 860 out = new BufferedOutputStream (new SmbFileOutputStream(new SmbFile(file, 861 getAuth()))); 862 in = new BufferedInputStream (i); 863 864 byte[] buf = new byte[smbBuffer]; 865 int len = 0; 866 int reallen = 0; 867 868 while(true) 869 { 870 len = in.read(buf); 871 872 if(len == StreamTokenizer.TT_EOF) 873 { 874 break; 875 } 876 877 out.write(buf, 0, len); 878 reallen += len; 879 880 fireProgressUpdate(StringUtils.getFile(file), 881 DataConnection.PUT, reallen); 882 } 883 884 fireProgressUpdate(file, DataConnection.FINISHED, -1); 885 886 return 0; 887 } 888 catch(IOException ex) 889 { 890 ex.printStackTrace(); 891 Log.debug("Error with file IO (" + ex + ")!"); 892 fireProgressUpdate(file, DataConnection.FAILED, -1); 893 } 894 finally 895 { 896 try 897 { 898 out.flush(); 899 out.close(); 900 in.close(); 901 } 902 catch(Exception ex) 903 { 904 ex.printStackTrace(); 905 } 906 } 907 908 return -1; 909 } 910 911 public InputStream getDownloadInputStream(String file) 912 { 913 file = toSMB(file); 914 915 try 916 { 917 return new BufferedInputStream (new SmbFile(file, getAuth()).getInputStream()); 918 } 919 catch(Exception ex) 920 { 921 ex.printStackTrace(); 922 Log.debug(ex.toString() + 923 " @SmbConnection::getDownloadInputStream"); 924 925 return null; 926 } 927 } 928 929 public Date [] sortDates() 930 { 931 return null; 932 } 933 934 public boolean rename(String file, String to) 935 { 936 try 937 { 938 file = toSMB(file); 939 to = toSMB(to); 940 941 SmbFile src = new SmbFile(file, getAuth()); 942 943 src.renameTo(new SmbFile(to, getAuth())); 944 945 return true; 946 } 947 catch(Exception ex) 948 { 949 ex.printStackTrace(); 950 Log.debug(ex.toString() + " @SmbConnection::rename"); 951 952 return false; 953 } 954 } 955 } 956 | Popular Tags |