1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.daemon.sftp;
27
28 import com.sshtools.daemon.platform;
29 import com.sshtools.daemon.session;
30 import com.sshtools.daemon.subsystem;
31
32 import com.sshtools.j2ssh;
33 import com.sshtools.j2ssh.connection;
34 import com.sshtools.j2ssh.io;
35 import com.sshtools.j2ssh.sftp;
36 import com.sshtools.j2ssh.subsystem;
37
38 import org.apache.commons.logging;
39
40 import java.io;
41
42
43 /**
44 *
45 *
46 * @author $author$
47 * @version $Revision: 1.15 $
48 */
49 public class SftpSubsystemServer extends SubsystemServer {
50 /** */
51 public static final int VERSION_1 = 1;
52
53 /** */
54 public static final int VERSION_2 = 2;
55
56 /** */
57 public static final int VERSION_3 = 3;
58
59 /** */
60 public static final int VERSION_4 = 4;
61 private static Log log = LogFactory.getLog(SftpSubsystemServer.class);
62 private NativeFileSystemProvider nfs;
63
64 /**
65 * Creates a new SftpSubsystemServer object.
66 */
67 public SftpSubsystemServer() {
68 registerMessage(SshFxpInit.SSH_FXP_INIT, SshFxpInit.class);
69 registerMessage(SshFxpMkdir.SSH_FXP_MKDIR, SshFxpMkdir.class);
70 registerMessage(SshFxpRealPath.SSH_FXP_REALPATH, SshFxpRealPath.class);
71 registerMessage(SshFxpOpenDir.SSH_FXP_OPENDIR, SshFxpOpenDir.class);
72 registerMessage(SshFxpOpen.SSH_FXP_OPEN, SshFxpOpen.class);
73 registerMessage(SshFxpRead.SSH_FXP_READ, SshFxpRead.class);
74 registerMessage(SshFxpWrite.SSH_FXP_WRITE, SshFxpWrite.class);
75 registerMessage(SshFxpReadDir.SSH_FXP_READDIR, SshFxpReadDir.class);
76 registerMessage(SshFxpClose.SSH_FXP_CLOSE, SshFxpClose.class);
77 registerMessage(SshFxpLStat.SSH_FXP_LSTAT, SshFxpLStat.class);
78 registerMessage(SshFxpStat.SSH_FXP_STAT, SshFxpStat.class);
79 registerMessage(SshFxpRemove.SSH_FXP_REMOVE, SshFxpRemove.class);
80 registerMessage(SshFxpRename.SSH_FXP_RENAME, SshFxpRename.class);
81 registerMessage(SshFxpRmdir.SSH_FXP_RMDIR, SshFxpRmdir.class);
82 registerMessage(SshFxpSetStat.SSH_FXP_SETSTAT, SshFxpSetStat.class);
83 registerMessage(SshFxpFStat.SSH_FXP_FSTAT, SshFxpFStat.class);
84 registerMessage(SshFxpFSetStat.SSH_FXP_FSETSTAT, SshFxpFSetStat.class);
85 registerMessage(SshFxpReadlink.SSH_FXP_READLINK, SshFxpReadlink.class);
86 registerMessage(SshFxpSymlink.SSH_FXP_SYMLINK, SshFxpSymlink.class);
87 }
88
89 /**
90 *
91 *
92 * @param session
93 */
94 public void setSession(SessionChannelServer session) {
95 session.addEventListener(new ChannelEventListener() {
96 public void onChannelOpen(Channel channel) {
97 }
98
99 public void onChannelEOF(Channel channel) {
100 try {
101 SftpSubsystemServer.this.session.close();
102 } catch (IOException ex) {
103 }
104 }
105
106 public void onChannelClose(Channel channel) {
107 }
108
109 public void onDataReceived(Channel channel, byte[] data) {
110 }
111
112 public void onDataSent(Channel channel, byte[] data) {
113 }
114 });
115 super.setSession(session);
116 }
117
118 /**
119 *
120 *
121 * @param msg
122 */
123 protected void onMessageReceived(SubsystemMessage msg) {
124 switch (msg.getMessageType()) {
125 case SshFxpInit.SSH_FXP_INIT: {
126 onInitialize((SshFxpInit) msg);
127
128 break;
129 }
130
131 case SshFxpMkdir.SSH_FXP_MKDIR: {
132 onMakeDirectory((SshFxpMkdir) msg);
133
134 break;
135 }
136
137 case SshFxpRealPath.SSH_FXP_REALPATH: {
138 onRealPath((SshFxpRealPath) msg);
139
140 break;
141 }
142
143 case SshFxpOpenDir.SSH_FXP_OPENDIR: {
144 onOpenDirectory((SshFxpOpenDir) msg);
145
146 break;
147 }
148
149 case SshFxpOpen.SSH_FXP_OPEN: {
150 onOpenFile((SshFxpOpen) msg);
151
152 break;
153 }
154
155 case SshFxpRead.SSH_FXP_READ: {
156 onReadFile((SshFxpRead) msg);
157
158 break;
159 }
160
161 case SshFxpWrite.SSH_FXP_WRITE: {
162 onWriteFile((SshFxpWrite) msg);
163
164 break;
165 }
166
167 case SshFxpReadDir.SSH_FXP_READDIR: {
168 onReadDirectory((SshFxpReadDir) msg);
169
170 break;
171 }
172
173 case SshFxpLStat.SSH_FXP_LSTAT: {
174 onLStat((SshFxpLStat) msg);
175
176 break;
177 }
178
179 case SshFxpStat.SSH_FXP_STAT: {
180 onStat((SshFxpStat) msg);
181
182 break;
183 }
184
185 case SshFxpFStat.SSH_FXP_FSTAT: {
186 onFStat((SshFxpFStat) msg);
187
188 break;
189 }
190
191 case SshFxpClose.SSH_FXP_CLOSE: {
192 onCloseFile((SshFxpClose) msg);
193
194 break;
195 }
196
197 case SshFxpRemove.SSH_FXP_REMOVE: {
198 onRemoveFile((SshFxpRemove) msg);
199
200 break;
201 }
202
203 case SshFxpRename.SSH_FXP_RENAME: {
204 onRenameFile((SshFxpRename) msg);
205
206 break;
207 }
208
209 case SshFxpRmdir.SSH_FXP_RMDIR: {
210 onRemoveDirectory((SshFxpRmdir) msg);
211
212 break;
213 }
214
215 case SshFxpSetStat.SSH_FXP_SETSTAT: {
216 onSetAttributes((SshFxpSetStat) msg);
217
218 break;
219 }
220
221 case SshFxpFSetStat.SSH_FXP_FSETSTAT: {
222 onSetAttributes((SshFxpFSetStat) msg);
223
224 break;
225 }
226
227 case SshFxpReadlink.SSH_FXP_READLINK: {
228 onReadlink((SshFxpReadlink) msg);
229
230 break;
231 }
232
233 case SshFxpSymlink.SSH_FXP_SYMLINK: {
234 onSymlink((SshFxpSymlink) msg);
235
236 break;
237 }
238
239 default: {
240 }
241 }
242 }
243
244 private void onSetAttributes(SshFxpSetStat msg) {
245 SubsystemMessage reply;
246
247 try {
248 nfs.setFileAttributes(checkDefaultPath(msg.getPath()),
249 msg.getAttributes());
250 reply = new SshFxpStatus(msg.getId(),
251 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
252 "The attributes were set", "");
253 } catch (FileNotFoundException fnfe) {
254 reply = new SshFxpStatus(msg.getId(),
255 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
256 fnfe.getMessage(), "");
257 } catch (PermissionDeniedException pde) {
258 reply = new SshFxpStatus(msg.getId(),
259 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
260 pde.getMessage(), "");
261 } catch (IOException ioe) {
262 reply = new SshFxpStatus(msg.getId(),
263 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
264 ioe.getMessage(), "");
265 }
266
267 sendMessage(reply);
268 }
269
270 private void onSetAttributes(SshFxpFSetStat msg) {
271 SubsystemMessage reply;
272
273 try {
274 nfs.setFileAttributes(msg.getHandle(), msg.getAttributes());
275 reply = new SshFxpStatus(msg.getId(),
276 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
277 "The attributes were set", "");
278 } catch (InvalidHandleException ihe) {
279 reply = new SshFxpStatus(msg.getId(),
280 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
281 ihe.getMessage(), "");
282 } catch (PermissionDeniedException pde) {
283 reply = new SshFxpStatus(msg.getId(),
284 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
285 pde.getMessage(), "");
286 } catch (IOException ioe) {
287 reply = new SshFxpStatus(msg.getId(),
288 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
289 ioe.getMessage(), "");
290 }
291
292 sendMessage(reply);
293 }
294
295 private void onReadlink(SshFxpReadlink msg) {
296 SubsystemMessage reply;
297
298 try {
299 /*
300 File f = nfs.readSymbolicLink(VirtualFileSystem.translateVFSPath(
301 msg.getPath()));
302 SftpFile[] files = new SftpFile[1];
303 files[0] = new SftpFile(VirtualFileSystem.translateNFSPath(
304 f.getCanonicalPath()),
305 nfs.getFileAttributes(f.getCanonicalPath()));
306 reply = new SshFxpName(msg.getId(), files);
307 */
308 reply = new SshFxpName(msg.getId(),
309 new SftpFile[] {
310 nfs.readSymbolicLink(checkDefaultPath(msg.getPath()))
311 });
312 } catch (FileNotFoundException ioe) {
313 reply = new SshFxpStatus(msg.getId(),
314 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
315 ioe.getMessage(), "");
316 } catch (PermissionDeniedException pde) {
317 reply = new SshFxpStatus(msg.getId(),
318 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
319 pde.getMessage(), "");
320 } catch (UnsupportedFileOperationException uso) {
321 reply = new SshFxpStatus(msg.getId(),
322 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OP_UNSUPPORTED),
323 uso.getMessage(), "");
324 } catch (IOException ioe2) {
325 reply = new SshFxpStatus(msg.getId(),
326 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
327 ioe2.getMessage(), "");
328 }
329
330 sendMessage(reply);
331 }
332
333 private void onSymlink(SshFxpSymlink msg) {
334 SubsystemMessage reply;
335
336 try {
337 /*
338 nfs.createSymbolicLink(VirtualFileSystem.translateVFSPath(
339 msg.getLinkPath()),
340 VirtualFileSystem.translateVFSPath(msg.getTargetPath()));
341 */
342 nfs.createSymbolicLink(checkDefaultPath(msg.getLinkPath()),
343 checkDefaultPath(msg.getTargetPath()));
344 reply = new SshFxpStatus(msg.getId(),
345 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
346 "The symbolic link was created", "");
347 } catch (FileNotFoundException ioe) {
348 reply = new SshFxpStatus(msg.getId(),
349 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
350 ioe.getMessage(), "");
351 } catch (PermissionDeniedException pde) {
352 reply = new SshFxpStatus(msg.getId(),
353 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
354 pde.getMessage(), "");
355 } catch (IOException ioe2) {
356 reply = new SshFxpStatus(msg.getId(),
357 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
358 ioe2.getMessage(), "");
359 } catch (UnsupportedFileOperationException uso) {
360 reply = new SshFxpStatus(msg.getId(),
361 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OP_UNSUPPORTED),
362 uso.getMessage(), "");
363 }
364
365 sendMessage(reply);
366 }
367
368 private void onRemoveDirectory(SshFxpRmdir msg) {
369 SubsystemMessage reply;
370
371 try {
372 /*
373 nfs.removeDirectory(VirtualFileSystem.translateVFSPath(
374 msg.getPath()));
375 */
376 nfs.removeDirectory(checkDefaultPath(msg.getPath()));
377 reply = new SshFxpStatus(msg.getId(),
378 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
379 "The directory was removed", "");
380 } catch (FileNotFoundException ioe) {
381 reply = new SshFxpStatus(msg.getId(),
382 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
383 ioe.getMessage(), "");
384 } catch (IOException ioe2) {
385 reply = new SshFxpStatus(msg.getId(),
386 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
387 ioe2.getMessage(), "");
388 } catch (PermissionDeniedException pde) {
389 reply = new SshFxpStatus(msg.getId(),
390 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
391 pde.getMessage(), "");
392 }
393
394 sendMessage(reply);
395 }
396
397 private void onRenameFile(SshFxpRename msg) {
398 SubsystemMessage reply;
399
400 try {
401 /*
402 nfs.renameFile(VirtualFileSystem.translateVFSPath(msg.getOldPath()),
403 VirtualFileSystem.translateVFSPath(msg.getNewPath()));
404 */
405 nfs.renameFile(checkDefaultPath(msg.getOldPath()),
406 checkDefaultPath(msg.getNewPath()));
407 reply = new SshFxpStatus(msg.getId(),
408 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
409 "The file was removed", "");
410 } catch (FileNotFoundException ioe) {
411 reply = new SshFxpStatus(msg.getId(),
412 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
413 ioe.getMessage(), "");
414 } catch (IOException ioe2) {
415 reply = new SshFxpStatus(msg.getId(),
416 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
417 ioe2.getMessage(), "");
418 } catch (PermissionDeniedException pde) {
419 reply = new SshFxpStatus(msg.getId(),
420 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
421 pde.getMessage(), "");
422 }
423
424 sendMessage(reply);
425 }
426
427 private void onRemoveFile(SshFxpRemove msg) {
428 SubsystemMessage reply;
429
430 try {
431 /*
432 nfs.removeFile(VirtualFileSystem.translateVFSPath(msg.getFilename()));
433 */
434 nfs.removeFile(checkDefaultPath(msg.getFilename()));
435 reply = new SshFxpStatus(msg.getId(),
436 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
437 "The file was removed", "");
438 } catch (FileNotFoundException ioe) {
439 reply = new SshFxpStatus(msg.getId(),
440 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
441 ioe.getMessage(), "");
442 } catch (IOException ioe2) {
443 reply = new SshFxpStatus(msg.getId(),
444 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
445 ioe2.getMessage(), "");
446 } catch (PermissionDeniedException pde) {
447 reply = new SshFxpStatus(msg.getId(),
448 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
449 pde.getMessage(), "");
450 }
451
452 sendMessage(reply);
453 }
454
455 private void onOpenFile(SshFxpOpen msg) {
456 SubsystemMessage reply;
457
458 try {
459 reply = new SshFxpHandle(msg.getId(),
460 nfs.openFile(checkDefaultPath(msg.getFilename()),
461 msg.getPflags(), msg.getAttributes()));
462 } catch (FileNotFoundException ioe) {
463 reply = new SshFxpStatus(msg.getId(),
464 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
465 ioe.getMessage(), "");
466 } catch (IOException ioe2) {
467 reply = new SshFxpStatus(msg.getId(),
468 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
469 ioe2.getMessage(), "");
470 } catch (PermissionDeniedException pde) {
471 reply = new SshFxpStatus(msg.getId(),
472 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
473 pde.getMessage(), "");
474 }
475
476 sendMessage(reply);
477 }
478
479 private void onReadFile(SshFxpRead msg) {
480 SubsystemMessage reply;
481
482 try {
483 reply = new SshFxpData(msg.getId(),
484 nfs.readFile(msg.getHandle(), msg.getOffset(),
485 msg.getLength()));
486 } catch (EOFException eof) {
487 reply = new SshFxpStatus(msg.getId(),
488 new UnsignedInteger32(SshFxpStatus.STATUS_FX_EOF),
489 eof.getMessage(), "");
490 } catch (InvalidHandleException ihe) {
491 reply = new SshFxpStatus(msg.getId(),
492 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
493 ihe.getMessage(), "");
494 } catch (IOException ioe2) {
495 reply = new SshFxpStatus(msg.getId(),
496 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
497 ioe2.getMessage(), "");
498 }
499
500 sendMessage(reply);
501 }
502
503 private void onWriteFile(SshFxpWrite msg) {
504 SubsystemMessage reply;
505
506 try {
507 nfs.writeFile(msg.getHandle(), msg.getOffset(), msg.getData(), 0,
508 msg.getData().length);
509 reply = new SshFxpStatus(msg.getId(),
510 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
511 "The write completed successfully", "");
512 } catch (InvalidHandleException ihe) {
513 reply = new SshFxpStatus(msg.getId(),
514 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
515 ihe.getMessage(), "");
516 } catch (IOException ioe2) {
517 reply = new SshFxpStatus(msg.getId(),
518 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
519 ioe2.getMessage(), "");
520 }
521
522 sendMessage(reply);
523 }
524
525 private void onCloseFile(SshFxpClose msg) {
526 SubsystemMessage reply;
527
528 try {
529 nfs.closeFile(msg.getHandle());
530 reply = new SshFxpStatus(msg.getId(),
531 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
532 "The operation completed", "");
533 } catch (InvalidHandleException ihe) {
534 reply = new SshFxpStatus(msg.getId(),
535 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
536 ihe.getMessage(), "");
537 } catch (IOException ioe2) {
538 reply = new SshFxpStatus(msg.getId(),
539 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
540 ioe2.getMessage(), "");
541 }
542
543 sendMessage(reply);
544 }
545
546 private void onFStat(SshFxpFStat msg) {
547 SubsystemMessage reply;
548
549 try {
550 reply = new SshFxpAttrs(msg.getId(),
551 nfs.getFileAttributes(msg.getHandle()));
552 } catch (InvalidHandleException ihe) {
553 reply = new SshFxpStatus(msg.getId(),
554 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
555 ihe.getMessage(), "");
556 } catch (IOException ioe2) {
557 reply = new SshFxpStatus(msg.getId(),
558 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
559 ioe2.getMessage(), "");
560 }
561
562 sendMessage(reply);
563 }
564
565 private void onStat(SshFxpStat msg) {
566 SubsystemMessage reply;
567
568 try {
569 String path = checkDefaultPath(msg.getPath());
570
571 if (nfs.fileExists(path)) {
572 SftpFile[] files = new SftpFile[1];
573 reply = new SshFxpAttrs(msg.getId(),
574 nfs.getFileAttributes(
575 /*nfs.getCanonicalPath(*/
576 msg.getPath() /*)*/));
577 } else {
578 reply = new SshFxpStatus(msg.getId(),
579 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
580 path + " is not a valid file path", "");
581 }
582 } catch (FileNotFoundException ioe) {
583 reply = new SshFxpStatus(msg.getId(),
584 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
585 ioe.getMessage(), "");
586 } catch (IOException ioe2) {
587 reply = new SshFxpStatus(msg.getId(),
588 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
589 ioe2.getMessage(), "");
590 }
591
592 sendMessage(reply);
593 }
594
595 private void onLStat(SshFxpLStat msg) {
596 SubsystemMessage reply;
597
598 try {
599 String path = checkDefaultPath(msg.getPath());
600
601 if (nfs.fileExists(path)) {
602 SftpFile[] files = new SftpFile[1];
603 reply = new SshFxpAttrs(msg.getId(),
604 nfs.getFileAttributes(nfs.getCanonicalPath(
605 msg.getPath())));
606 } else {
607 reply = new SshFxpStatus(msg.getId(),
608 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
609 path + " is not a valid file path", "");
610 }
611 } catch (FileNotFoundException ioe) {
612 reply = new SshFxpStatus(msg.getId(),
613 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
614 ioe.getMessage(), "");
615 } catch (IOException ioe2) {
616 reply = new SshFxpStatus(msg.getId(),
617 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
618 ioe2.getMessage(), "");
619 }
620
621 sendMessage(reply);
622 }
623
624 private void onReadDirectory(SshFxpReadDir msg) {
625 SubsystemMessage reply;
626
627 try {
628 /*
629 File[] files = nfs.readDirectory(msg.getHandle());
630 SftpFile[] sftpfiles = new SftpFile[files.length];
631 for (int i = 0; i < files.length; i++) {
632 sftpfiles[i] = new SftpFile(files[i].getName(),
633 nfs.getFileAttributes(files[i].getCanonicalPath()));
634 }
635 */
636 SftpFile[] sftpfiles = nfs.readDirectory(msg.getHandle());
637 reply = new SshFxpName(msg.getId(), sftpfiles);
638 } catch (FileNotFoundException ioe) {
639 reply = new SshFxpStatus(msg.getId(),
640 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
641 ioe.getMessage(), "");
642 } catch (InvalidHandleException ihe) {
643 reply = new SshFxpStatus(msg.getId(),
644 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
645 ihe.getMessage(), "");
646 } catch (EOFException eof) {
647 reply = new SshFxpStatus(msg.getId(),
648 new UnsignedInteger32(SshFxpStatus.STATUS_FX_EOF),
649 eof.getMessage(), "");
650 } catch (IOException ioe2) {
651 reply = new SshFxpStatus(msg.getId(),
652 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
653 ioe2.getMessage(), "");
654 }
655
656 sendMessage(reply);
657 }
658
659 private void onOpenDirectory(SshFxpOpenDir msg) {
660 SubsystemMessage reply;
661
662 try {
663 /*
664 String path = VirtualFileSystem.translateVFSPath(msg.getPath());
665 */
666 String path = checkDefaultPath(msg.getPath());
667 reply = new SshFxpHandle(msg.getId(), nfs.openDirectory(path));
668 } catch (FileNotFoundException ioe) {
669 reply = new SshFxpStatus(msg.getId(),
670 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
671 ioe.getMessage(), "");
672 } catch (IOException ioe2) {
673 reply = new SshFxpStatus(msg.getId(),
674 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
675 ioe2.getMessage(), "");
676 } catch (PermissionDeniedException pde) {
677 reply = new SshFxpStatus(msg.getId(),
678 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
679 pde.getMessage(), "");
680 }
681
682 sendMessage(reply);
683 }
684
685 private void onRealPath(SshFxpRealPath msg) {
686 SubsystemMessage reply;
687
688 try {
689 /*
690 String path = VirtualFileSystem.translateVFSPath(msg.getPath());
691 path = VirtualFileSystem.translateNFSPath(path);
692 */
693 String path = nfs.getRealPath(checkDefaultPath(msg.getPath()));
694
695 if (path != null) {
696 SftpFile[] files = new SftpFile[1];
697 files[0] = new SftpFile(path);
698 reply = new SshFxpName(msg.getId(), files);
699 } else {
700 reply = new SshFxpStatus(msg.getId(),
701 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
702 msg.getPath() +
703 " could not be translated into a system dependent path",
704 "");
705 }
706 } catch (FileNotFoundException ioe) {
707 reply = new SshFxpStatus(msg.getId(),
708 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
709 ioe.getMessage(), "");
710 } catch (IOException ioe) {
711 reply = new SshFxpStatus(msg.getId(),
712 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
713 ioe.getMessage(), "");
714 }
715
716 sendMessage(reply);
717 }
718
719 private void onMakeDirectory(SshFxpMkdir msg) {
720 SubsystemMessage reply;
721
722 try {
723 /*
724 String path = VirtualFileSystem.translateVFSPath(msg.getPath());
725 */
726 String path = checkDefaultPath(msg.getPath());
727
728 if (nfs.makeDirectory(path)) {
729 reply = new SshFxpStatus(msg.getId(),
730 new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
731 "The operation completed sucessfully", "");
732 } else {
733 // Send an error back to the client
734 reply = new SshFxpStatus(msg.getId(),
735 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
736 "The operation failed", "");
737 }
738 } catch (FileNotFoundException ioe) {
739 reply = new SshFxpStatus(msg.getId(),
740 new UnsignedInteger32(SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
741 ioe.getMessage(), "");
742 } catch (PermissionDeniedException pde) {
743 reply = new SshFxpStatus(msg.getId(),
744 new UnsignedInteger32(SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
745 pde.getMessage(), "");
746 } catch (IOException ioe) {
747 reply = new SshFxpStatus(msg.getId(),
748 new UnsignedInteger32(SshFxpStatus.STATUS_FX_FAILURE),
749 ioe.getMessage(), "");
750 }
751
752 sendMessage(reply);
753 }
754
755 private String checkDefaultPath(String path) throws IOException {
756 // Use the users home directory if no path is supplied
757 if (path.equals("")) {
758 return nfs.getDefaultPath(SshThread.getCurrentThreadUser());
759 } else {
760 return path;
761 }
762 }
763
764 private void onInitialize(SshFxpInit msg) {
765 // Get the native file system
766 nfs = NativeFileSystemProvider.getInstance();
767
768 // Determine the users home directory
769 if (msg.getVersion().intValue() == VERSION_3) {
770 SshFxpVersion reply = new SshFxpVersion(new UnsignedInteger32(
771 VERSION_3), null);
772 sendMessage(reply);
773 } else {
774 // Wrong version
775 }
776 }
777 }