2 * winsftp.c: the Windows-specific parts of PSFTP and PSCP.
12 char *get_ttymode(void *frontend
, const char *mode
) { return NULL
; }
14 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
17 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
19 ret
= console_get_userpass_input(p
, in
, inlen
);
23 void platform_get_x11_auth(struct X11Display
*display
, const Config
*cfg
)
25 /* Do nothing, therefore no auth. */
27 const int platform_uses_x11_unix_by_default
= TRUE
;
29 /* ----------------------------------------------------------------------
30 * File access abstraction.
34 * Set local current directory. Returns NULL on success, or else an
35 * error message which must be freed after printing.
37 char *psftp_lcd(char *dir
)
41 if (!SetCurrentDirectory(dir
)) {
44 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
45 FORMAT_MESSAGE_FROM_SYSTEM
|
46 FORMAT_MESSAGE_IGNORE_INSERTS
,
48 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
49 (LPTSTR
)&message
, 0, NULL
);
50 i
= strcspn((char *)message
, "\n");
51 ret
= dupprintf("%.*s", i
, (LPCTSTR
)message
);
59 * Get local current directory. Returns a string which must be
62 char *psftp_getcwd(void)
64 char *ret
= snewn(256, char);
65 int len
= GetCurrentDirectory(256, ret
);
67 ret
= sresize(ret
, len
, char);
68 GetCurrentDirectory(len
, ret
);
72 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
73 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
74 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
75 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
81 RFile
*open_existing_file(char *name
, uint64
*size
,
82 unsigned long *mtime
, unsigned long *atime
)
87 h
= CreateFile(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
89 if (h
== INVALID_HANDLE_VALUE
)
96 size
->lo
=GetFileSize(h
, &(size
->hi
));
99 FILETIME actime
, wrtime
;
100 GetFileTime(h
, NULL
, &actime
, &wrtime
);
102 TIME_WIN_TO_POSIX(actime
, *atime
);
104 TIME_WIN_TO_POSIX(wrtime
, *mtime
);
110 int read_from_file(RFile
*f
, void *buffer
, int length
)
114 ret
= ReadFile(f
->h
, buffer
, length
, &read
, NULL
);
116 return -1; /* error */
121 void close_rfile(RFile
*f
)
131 WFile
*open_new_file(char *name
)
136 h
= CreateFile(name
, GENERIC_WRITE
, 0, NULL
,
137 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
138 if (h
== INVALID_HANDLE_VALUE
)
147 WFile
*open_existing_wfile(char *name
, uint64
*size
)
152 h
= CreateFile(name
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
153 OPEN_EXISTING
, 0, 0);
154 if (h
== INVALID_HANDLE_VALUE
)
161 size
->lo
=GetFileSize(h
, &(size
->hi
));
166 int write_to_file(WFile
*f
, void *buffer
, int length
)
170 ret
= WriteFile(f
->h
, buffer
, length
, &written
, NULL
);
172 return -1; /* error */
177 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
179 FILETIME actime
, wrtime
;
180 TIME_POSIX_TO_WIN(atime
, actime
);
181 TIME_POSIX_TO_WIN(mtime
, wrtime
);
182 SetFileTime(f
->h
, NULL
, &actime
, &wrtime
);
185 void close_wfile(WFile
*f
)
191 /* Seek offset bytes through file, from whence, where whence is
192 FROM_START, FROM_CURRENT, or FROM_END */
193 int seek_file(WFile
*f
, uint64 offset
, int whence
)
199 movemethod
= FILE_BEGIN
;
202 movemethod
= FILE_CURRENT
;
205 movemethod
= FILE_END
;
211 SetFilePointer(f
->h
, offset
.lo
, &(offset
.hi
), movemethod
);
213 if (GetLastError() != NO_ERROR
)
219 uint64
get_file_posn(WFile
*f
)
224 ret
.lo
= SetFilePointer(f
->h
, 0L, &(ret
.hi
), FILE_CURRENT
);
229 int file_type(char *name
)
232 attr
= GetFileAttributes(name
);
233 /* We know of no `weird' files under Windows. */
234 if (attr
== (DWORD
)-1)
235 return FILE_TYPE_NONEXISTENT
;
236 else if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
237 return FILE_TYPE_DIRECTORY
;
239 return FILE_TYPE_FILE
;
247 DirHandle
*open_directory(char *name
)
250 WIN32_FIND_DATA fdat
;
254 /* Enumerate files in dir `foo'. */
255 findfile
= dupcat(name
, "/*", NULL
);
256 h
= FindFirstFile(findfile
, &fdat
);
257 if (h
== INVALID_HANDLE_VALUE
)
261 ret
= snew(DirHandle
);
263 ret
->name
= dupstr(fdat
.cFileName
);
267 char *read_filename(DirHandle
*dir
)
272 WIN32_FIND_DATA fdat
;
273 int ok
= FindNextFile(dir
->h
, &fdat
);
277 dir
->name
= dupstr(fdat
.cFileName
);
281 if (dir
->name
[0] == '.' &&
282 (dir
->name
[1] == '\0' ||
283 (dir
->name
[1] == '.' && dir
->name
[2] == '\0'))) {
288 } while (!dir
->name
);
291 char *ret
= dir
->name
;
298 void close_directory(DirHandle
*dir
)
306 int test_wildcard(char *name
, int cmdline
)
309 WIN32_FIND_DATA fdat
;
311 /* First see if the exact name exists. */
312 if (GetFileAttributes(name
) != (DWORD
)-1)
313 return WCTYPE_FILENAME
;
315 /* Otherwise see if a wildcard match finds anything. */
316 fh
= FindFirstFile(name
, &fdat
);
317 if (fh
== INVALID_HANDLE_VALUE
)
318 return WCTYPE_NONEXISTENT
;
321 return WCTYPE_WILDCARD
;
324 struct WildcardMatcher
{
331 * Return a pointer to the portion of str that comes after the last
332 * slash (or backslash or colon, if `local' is TRUE).
334 static char *stripslashes(char *str
, int local
)
339 p
= strchr(str
, ':');
343 p
= strrchr(str
, '/');
347 p
= strrchr(str
, '\\');
354 WildcardMatcher
*begin_wildcard_matching(char *name
)
357 WIN32_FIND_DATA fdat
;
358 WildcardMatcher
*ret
;
361 h
= FindFirstFile(name
, &fdat
);
362 if (h
== INVALID_HANDLE_VALUE
)
365 ret
= snew(WildcardMatcher
);
367 ret
->srcpath
= dupstr(name
);
368 last
= stripslashes(ret
->srcpath
, 1);
370 if (fdat
.cFileName
[0] == '.' &&
371 (fdat
.cFileName
[1] == '\0' ||
372 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
375 ret
->name
= dupcat(ret
->srcpath
, fdat
.cFileName
, NULL
);
380 char *wildcard_get_filename(WildcardMatcher
*dir
)
383 WIN32_FIND_DATA fdat
;
384 int ok
= FindNextFile(dir
->h
, &fdat
);
389 if (fdat
.cFileName
[0] == '.' &&
390 (fdat
.cFileName
[1] == '\0' ||
391 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
394 dir
->name
= dupcat(dir
->srcpath
, fdat
.cFileName
, NULL
);
398 char *ret
= dir
->name
;
405 void finish_wildcard_matching(WildcardMatcher
*dir
)
414 int vet_filename(char *name
)
416 if (strchr(name
, '/') || strchr(name
, '\\') || strchr(name
, ':'))
419 if (!name
[strspn(name
, ".")]) /* entirely composed of dots */
425 int create_directory(char *name
)
427 return CreateDirectory(name
, NULL
) != 0;
430 char *dir_file_cat(char *dir
, char *file
)
432 return dupcat(dir
, "\\", file
, NULL
);
435 /* ----------------------------------------------------------------------
436 * Platform-specific network handling.
440 * Be told what socket we're supposed to be using.
442 static SOCKET sftp_ssh_socket
= INVALID_SOCKET
;
443 static HANDLE netevent
= INVALID_HANDLE_VALUE
;
444 char *do_select(SOCKET skt
, int startup
)
448 sftp_ssh_socket
= skt
;
450 sftp_ssh_socket
= INVALID_SOCKET
;
452 if (p_WSAEventSelect
) {
454 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
455 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
456 netevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
460 if (p_WSAEventSelect(skt
, netevent
, events
) == SOCKET_ERROR
) {
461 switch (p_WSAGetLastError()) {
463 return "Network is down";
465 return "WSAEventSelect(): unknown error";
471 extern int select_result(WPARAM
, LPARAM
);
473 int do_eventsel_loop(HANDLE other_event
)
475 int n
, nhandles
, nallhandles
, netindex
, otherindex
;
480 long now
= GETTICKCOUNT();
482 if (run_timers(now
, &next
)) {
483 ticks
= next
- GETTICKCOUNT();
484 if (ticks
< 0) ticks
= 0; /* just in case */
489 handles
= handle_get_events(&nhandles
);
490 handles
= sresize(handles
, nhandles
+2, HANDLE
);
491 nallhandles
= nhandles
;
493 if (netevent
!= INVALID_HANDLE_VALUE
)
494 handles
[netindex
= nallhandles
++] = netevent
;
497 if (other_event
!= INVALID_HANDLE_VALUE
)
498 handles
[otherindex
= nallhandles
++] = other_event
;
502 n
= WaitForMultipleObjects(nallhandles
, handles
, FALSE
, ticks
);
504 if ((unsigned)(n
- WAIT_OBJECT_0
) < (unsigned)nhandles
) {
505 handle_got_event(handles
[n
- WAIT_OBJECT_0
]);
506 } else if (netindex
>= 0 && n
== WAIT_OBJECT_0
+ netindex
) {
507 WSANETWORKEVENTS things
;
509 extern SOCKET
first_socket(int *), next_socket(int *);
510 extern int select_result(WPARAM
, LPARAM
);
514 * We must not call select_result() for any socket
515 * until we have finished enumerating within the
516 * tree. This is because select_result() may close
517 * the socket and modify the tree.
519 /* Count the active sockets. */
521 for (socket
= first_socket(&socketstate
);
522 socket
!= INVALID_SOCKET
;
523 socket
= next_socket(&socketstate
)) i
++;
525 /* Expand the buffer if necessary. */
526 sklist
= snewn(i
, SOCKET
);
528 /* Retrieve the sockets into sklist. */
530 for (socket
= first_socket(&socketstate
);
531 socket
!= INVALID_SOCKET
;
532 socket
= next_socket(&socketstate
)) {
533 sklist
[skcount
++] = socket
;
536 /* Now we're done enumerating; go through the list. */
537 for (i
= 0; i
< skcount
; i
++) {
540 wp
= (WPARAM
) socket
;
541 if (!p_WSAEnumNetworkEvents(socket
, NULL
, &things
)) {
542 static const struct { int bit
, mask
; } eventtypes
[] = {
543 {FD_CONNECT_BIT
, FD_CONNECT
},
544 {FD_READ_BIT
, FD_READ
},
545 {FD_CLOSE_BIT
, FD_CLOSE
},
546 {FD_OOB_BIT
, FD_OOB
},
547 {FD_WRITE_BIT
, FD_WRITE
},
548 {FD_ACCEPT_BIT
, FD_ACCEPT
},
552 noise_ultralight(socket
);
553 noise_ultralight(things
.lNetworkEvents
);
555 for (e
= 0; e
< lenof(eventtypes
); e
++)
556 if (things
.lNetworkEvents
& eventtypes
[e
].mask
) {
558 int err
= things
.iErrorCode
[eventtypes
[e
].bit
];
559 lp
= WSAMAKESELECTREPLY(eventtypes
[e
].mask
, err
);
560 select_result(wp
, lp
);
570 if (n
== WAIT_TIMEOUT
) {
573 now
= GETTICKCOUNT();
576 if (otherindex
>= 0 && n
== WAIT_OBJECT_0
+ otherindex
)
583 * Wait for some network data and process it.
585 * We have two variants of this function. One uses select() so that
586 * it's compatible with WinSock 1. The other uses WSAEventSelect
587 * and MsgWaitForMultipleObjects, so that we can consistently use
588 * WSAEventSelect throughout; this enables us to also implement
589 * ssh_sftp_get_cmdline() using a parallel mechanism.
591 int ssh_sftp_loop_iteration(void)
593 if (p_WSAEventSelect
== NULL
) {
596 long now
= GETTICKCOUNT();
598 if (sftp_ssh_socket
== INVALID_SOCKET
)
599 return -1; /* doom */
601 if (socket_writable(sftp_ssh_socket
))
602 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_WRITE
);
606 struct timeval tv
, *ptv
;
608 if (run_timers(now
, &next
)) {
609 ticks
= next
- GETTICKCOUNT();
611 ticks
= 1; /* just in case */
612 tv
.tv_sec
= ticks
/ 1000;
613 tv
.tv_usec
= ticks
% 1000 * 1000;
620 FD_SET(sftp_ssh_socket
, &readfds
);
621 ret
= p_select(1, &readfds
, NULL
, NULL
, ptv
);
624 return -1; /* doom */
628 now
= GETTICKCOUNT();
632 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_READ
);
636 return do_eventsel_loop(INVALID_HANDLE_VALUE
);
641 * Read a command line from standard input.
643 * In the presence of WinSock 2, we can use WSAEventSelect to
644 * mediate between the socket and stdin, meaning we can send
645 * keepalives and respond to server events even while waiting at
646 * the PSFTP command prompt. Without WS2, we fall back to a simple
649 struct command_read_ctx
{
654 static DWORD WINAPI
command_read_thread(void *param
)
656 struct command_read_ctx
*ctx
= (struct command_read_ctx
*) param
;
658 ctx
->line
= fgetline(stdin
);
660 SetEvent(ctx
->event
);
665 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
668 struct command_read_ctx actx
, *ctx
= &actx
;
671 fputs(prompt
, stdout
);
674 if ((sftp_ssh_socket
== INVALID_SOCKET
&& no_fds_ok
) ||
675 p_WSAEventSelect
== NULL
) {
676 return fgetline(stdin
); /* very simple */
680 * Create a second thread to read from stdin. Process network
681 * and timing events until it terminates.
683 ctx
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
686 if (!CreateThread(NULL
, 0, command_read_thread
,
687 ctx
, 0, &threadid
)) {
688 fprintf(stderr
, "Unable to create command input thread\n");
693 ret
= do_eventsel_loop(ctx
->event
);
695 /* Error return can only occur if netevent==NULL, and it ain't. */
702 /* ----------------------------------------------------------------------
703 * Main program. Parse arguments etc.
705 int main(int argc
, char *argv
[])
709 ret
= psftp_main(argc
, argv
);