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
, Conf
*conf
)
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) do { \
74 uli.QuadPart = ((ULONGLONG)(t) + 11644473600ull) * 10000000ull; \
75 (ft).dwLowDateTime = uli.LowPart; \
76 (ft).dwHighDateTime = uli.HighPart; \
78 #define TIME_WIN_TO_POSIX(ft, t) do { \
80 uli.LowPart = (ft).dwLowDateTime; \
81 uli.HighPart = (ft).dwHighDateTime; \
82 uli.QuadPart = uli.QuadPart / 10000000ull - 11644473600ull; \
83 (t) = (unsigned long) uli.QuadPart; \
90 RFile
*open_existing_file(char *name
, uint64
*size
,
91 unsigned long *mtime
, unsigned long *atime
)
96 h
= CreateFile(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
98 if (h
== INVALID_HANDLE_VALUE
)
105 size
->lo
=GetFileSize(h
, &(size
->hi
));
107 if (mtime
|| atime
) {
108 FILETIME actime
, wrtime
;
109 GetFileTime(h
, NULL
, &actime
, &wrtime
);
111 TIME_WIN_TO_POSIX(actime
, *atime
);
113 TIME_WIN_TO_POSIX(wrtime
, *mtime
);
119 int read_from_file(RFile
*f
, void *buffer
, int length
)
123 ret
= ReadFile(f
->h
, buffer
, length
, &read
, NULL
);
125 return -1; /* error */
130 void close_rfile(RFile
*f
)
140 WFile
*open_new_file(char *name
)
145 h
= CreateFile(name
, GENERIC_WRITE
, 0, NULL
,
146 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
147 if (h
== INVALID_HANDLE_VALUE
)
156 WFile
*open_existing_wfile(char *name
, uint64
*size
)
161 h
= CreateFile(name
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
162 OPEN_EXISTING
, 0, 0);
163 if (h
== INVALID_HANDLE_VALUE
)
170 size
->lo
=GetFileSize(h
, &(size
->hi
));
175 int write_to_file(WFile
*f
, void *buffer
, int length
)
179 ret
= WriteFile(f
->h
, buffer
, length
, &written
, NULL
);
181 return -1; /* error */
186 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
188 FILETIME actime
, wrtime
;
189 TIME_POSIX_TO_WIN(atime
, actime
);
190 TIME_POSIX_TO_WIN(mtime
, wrtime
);
191 SetFileTime(f
->h
, NULL
, &actime
, &wrtime
);
194 void close_wfile(WFile
*f
)
200 /* Seek offset bytes through file, from whence, where whence is
201 FROM_START, FROM_CURRENT, or FROM_END */
202 int seek_file(WFile
*f
, uint64 offset
, int whence
)
208 movemethod
= FILE_BEGIN
;
211 movemethod
= FILE_CURRENT
;
214 movemethod
= FILE_END
;
220 SetFilePointer(f
->h
, offset
.lo
, &(offset
.hi
), movemethod
);
222 if (GetLastError() != NO_ERROR
)
228 uint64
get_file_posn(WFile
*f
)
233 ret
.lo
= SetFilePointer(f
->h
, 0L, &(ret
.hi
), FILE_CURRENT
);
238 int file_type(char *name
)
241 attr
= GetFileAttributes(name
);
242 /* We know of no `weird' files under Windows. */
243 if (attr
== (DWORD
)-1)
244 return FILE_TYPE_NONEXISTENT
;
245 else if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
246 return FILE_TYPE_DIRECTORY
;
248 return FILE_TYPE_FILE
;
256 DirHandle
*open_directory(char *name
)
259 WIN32_FIND_DATA fdat
;
263 /* Enumerate files in dir `foo'. */
264 findfile
= dupcat(name
, "/*", NULL
);
265 h
= FindFirstFile(findfile
, &fdat
);
266 if (h
== INVALID_HANDLE_VALUE
)
270 ret
= snew(DirHandle
);
272 ret
->name
= dupstr(fdat
.cFileName
);
276 char *read_filename(DirHandle
*dir
)
281 WIN32_FIND_DATA fdat
;
282 int ok
= FindNextFile(dir
->h
, &fdat
);
286 dir
->name
= dupstr(fdat
.cFileName
);
290 if (dir
->name
[0] == '.' &&
291 (dir
->name
[1] == '\0' ||
292 (dir
->name
[1] == '.' && dir
->name
[2] == '\0'))) {
297 } while (!dir
->name
);
300 char *ret
= dir
->name
;
307 void close_directory(DirHandle
*dir
)
315 int test_wildcard(char *name
, int cmdline
)
318 WIN32_FIND_DATA fdat
;
320 /* First see if the exact name exists. */
321 if (GetFileAttributes(name
) != (DWORD
)-1)
322 return WCTYPE_FILENAME
;
324 /* Otherwise see if a wildcard match finds anything. */
325 fh
= FindFirstFile(name
, &fdat
);
326 if (fh
== INVALID_HANDLE_VALUE
)
327 return WCTYPE_NONEXISTENT
;
330 return WCTYPE_WILDCARD
;
333 struct WildcardMatcher
{
340 * Return a pointer to the portion of str that comes after the last
341 * slash (or backslash or colon, if `local' is TRUE).
343 static char *stripslashes(char *str
, int local
)
348 p
= strchr(str
, ':');
352 p
= strrchr(str
, '/');
356 p
= strrchr(str
, '\\');
363 WildcardMatcher
*begin_wildcard_matching(char *name
)
366 WIN32_FIND_DATA fdat
;
367 WildcardMatcher
*ret
;
370 h
= FindFirstFile(name
, &fdat
);
371 if (h
== INVALID_HANDLE_VALUE
)
374 ret
= snew(WildcardMatcher
);
376 ret
->srcpath
= dupstr(name
);
377 last
= stripslashes(ret
->srcpath
, 1);
379 if (fdat
.cFileName
[0] == '.' &&
380 (fdat
.cFileName
[1] == '\0' ||
381 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
384 ret
->name
= dupcat(ret
->srcpath
, fdat
.cFileName
, NULL
);
389 char *wildcard_get_filename(WildcardMatcher
*dir
)
392 WIN32_FIND_DATA fdat
;
393 int ok
= FindNextFile(dir
->h
, &fdat
);
398 if (fdat
.cFileName
[0] == '.' &&
399 (fdat
.cFileName
[1] == '\0' ||
400 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
403 dir
->name
= dupcat(dir
->srcpath
, fdat
.cFileName
, NULL
);
407 char *ret
= dir
->name
;
414 void finish_wildcard_matching(WildcardMatcher
*dir
)
423 int vet_filename(char *name
)
425 if (strchr(name
, '/') || strchr(name
, '\\') || strchr(name
, ':'))
428 if (!name
[strspn(name
, ".")]) /* entirely composed of dots */
434 int create_directory(char *name
)
436 return CreateDirectory(name
, NULL
) != 0;
439 char *dir_file_cat(char *dir
, char *file
)
441 return dupcat(dir
, "\\", file
, NULL
);
444 /* ----------------------------------------------------------------------
445 * Platform-specific network handling.
449 * Be told what socket we're supposed to be using.
451 static SOCKET sftp_ssh_socket
= INVALID_SOCKET
;
452 static HANDLE netevent
= INVALID_HANDLE_VALUE
;
453 char *do_select(SOCKET skt
, int startup
)
457 sftp_ssh_socket
= skt
;
459 sftp_ssh_socket
= INVALID_SOCKET
;
461 if (p_WSAEventSelect
) {
463 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
464 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
465 netevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
469 if (p_WSAEventSelect(skt
, netevent
, events
) == SOCKET_ERROR
) {
470 switch (p_WSAGetLastError()) {
472 return "Network is down";
474 return "WSAEventSelect(): unknown error";
480 extern int select_result(WPARAM
, LPARAM
);
482 int do_eventsel_loop(HANDLE other_event
)
484 int n
, nhandles
, nallhandles
, netindex
, otherindex
;
489 long now
= GETTICKCOUNT();
491 if (run_timers(now
, &next
)) {
492 ticks
= next
- GETTICKCOUNT();
493 if (ticks
< 0) ticks
= 0; /* just in case */
498 handles
= handle_get_events(&nhandles
);
499 handles
= sresize(handles
, nhandles
+2, HANDLE
);
500 nallhandles
= nhandles
;
502 if (netevent
!= INVALID_HANDLE_VALUE
)
503 handles
[netindex
= nallhandles
++] = netevent
;
506 if (other_event
!= INVALID_HANDLE_VALUE
)
507 handles
[otherindex
= nallhandles
++] = other_event
;
511 n
= WaitForMultipleObjects(nallhandles
, handles
, FALSE
, ticks
);
513 if ((unsigned)(n
- WAIT_OBJECT_0
) < (unsigned)nhandles
) {
514 handle_got_event(handles
[n
- WAIT_OBJECT_0
]);
515 } else if (netindex
>= 0 && n
== WAIT_OBJECT_0
+ netindex
) {
516 WSANETWORKEVENTS things
;
518 extern SOCKET
first_socket(int *), next_socket(int *);
519 extern int select_result(WPARAM
, LPARAM
);
523 * We must not call select_result() for any socket
524 * until we have finished enumerating within the
525 * tree. This is because select_result() may close
526 * the socket and modify the tree.
528 /* Count the active sockets. */
530 for (socket
= first_socket(&socketstate
);
531 socket
!= INVALID_SOCKET
;
532 socket
= next_socket(&socketstate
)) i
++;
534 /* Expand the buffer if necessary. */
535 sklist
= snewn(i
, SOCKET
);
537 /* Retrieve the sockets into sklist. */
539 for (socket
= first_socket(&socketstate
);
540 socket
!= INVALID_SOCKET
;
541 socket
= next_socket(&socketstate
)) {
542 sklist
[skcount
++] = socket
;
545 /* Now we're done enumerating; go through the list. */
546 for (i
= 0; i
< skcount
; i
++) {
549 wp
= (WPARAM
) socket
;
550 if (!p_WSAEnumNetworkEvents(socket
, NULL
, &things
)) {
551 static const struct { int bit
, mask
; } eventtypes
[] = {
552 {FD_CONNECT_BIT
, FD_CONNECT
},
553 {FD_READ_BIT
, FD_READ
},
554 {FD_CLOSE_BIT
, FD_CLOSE
},
555 {FD_OOB_BIT
, FD_OOB
},
556 {FD_WRITE_BIT
, FD_WRITE
},
557 {FD_ACCEPT_BIT
, FD_ACCEPT
},
561 noise_ultralight(socket
);
562 noise_ultralight(things
.lNetworkEvents
);
564 for (e
= 0; e
< lenof(eventtypes
); e
++)
565 if (things
.lNetworkEvents
& eventtypes
[e
].mask
) {
567 int err
= things
.iErrorCode
[eventtypes
[e
].bit
];
568 lp
= WSAMAKESELECTREPLY(eventtypes
[e
].mask
, err
);
569 select_result(wp
, lp
);
579 if (n
== WAIT_TIMEOUT
) {
582 now
= GETTICKCOUNT();
585 if (otherindex
>= 0 && n
== WAIT_OBJECT_0
+ otherindex
)
592 * Wait for some network data and process it.
594 * We have two variants of this function. One uses select() so that
595 * it's compatible with WinSock 1. The other uses WSAEventSelect
596 * and MsgWaitForMultipleObjects, so that we can consistently use
597 * WSAEventSelect throughout; this enables us to also implement
598 * ssh_sftp_get_cmdline() using a parallel mechanism.
600 int ssh_sftp_loop_iteration(void)
602 if (p_WSAEventSelect
== NULL
) {
605 long now
= GETTICKCOUNT();
607 if (sftp_ssh_socket
== INVALID_SOCKET
)
608 return -1; /* doom */
610 if (socket_writable(sftp_ssh_socket
))
611 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_WRITE
);
615 struct timeval tv
, *ptv
;
617 if (run_timers(now
, &next
)) {
618 ticks
= next
- GETTICKCOUNT();
620 ticks
= 1; /* just in case */
621 tv
.tv_sec
= ticks
/ 1000;
622 tv
.tv_usec
= ticks
% 1000 * 1000;
629 FD_SET(sftp_ssh_socket
, &readfds
);
630 ret
= p_select(1, &readfds
, NULL
, NULL
, ptv
);
633 return -1; /* doom */
637 now
= GETTICKCOUNT();
641 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_READ
);
645 return do_eventsel_loop(INVALID_HANDLE_VALUE
);
650 * Read a command line from standard input.
652 * In the presence of WinSock 2, we can use WSAEventSelect to
653 * mediate between the socket and stdin, meaning we can send
654 * keepalives and respond to server events even while waiting at
655 * the PSFTP command prompt. Without WS2, we fall back to a simple
658 struct command_read_ctx
{
663 static DWORD WINAPI
command_read_thread(void *param
)
665 struct command_read_ctx
*ctx
= (struct command_read_ctx
*) param
;
667 ctx
->line
= fgetline(stdin
);
669 SetEvent(ctx
->event
);
674 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
677 struct command_read_ctx actx
, *ctx
= &actx
;
680 fputs(prompt
, stdout
);
683 if ((sftp_ssh_socket
== INVALID_SOCKET
&& no_fds_ok
) ||
684 p_WSAEventSelect
== NULL
) {
685 return fgetline(stdin
); /* very simple */
689 * Create a second thread to read from stdin. Process network
690 * and timing events until it terminates.
692 ctx
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
695 if (!CreateThread(NULL
, 0, command_read_thread
,
696 ctx
, 0, &threadid
)) {
697 fprintf(stderr
, "Unable to create command input thread\n");
702 ret
= do_eventsel_loop(ctx
->event
);
704 /* Error return can only occur if netevent==NULL, and it ain't. */
711 /* ----------------------------------------------------------------------
712 * Main program. Parse arguments etc.
714 int main(int argc
, char *argv
[])
718 ret
= psftp_main(argc
, argv
);