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
,
97 h
= CreateFile(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
99 if (h
== INVALID_HANDLE_VALUE
)
106 size
->lo
=GetFileSize(h
, &(size
->hi
));
108 if (mtime
|| atime
) {
109 FILETIME actime
, wrtime
;
110 GetFileTime(h
, NULL
, &actime
, &wrtime
);
112 TIME_WIN_TO_POSIX(actime
, *atime
);
114 TIME_WIN_TO_POSIX(wrtime
, *mtime
);
123 int read_from_file(RFile
*f
, void *buffer
, int length
)
127 ret
= ReadFile(f
->h
, buffer
, length
, &read
, NULL
);
129 return -1; /* error */
134 void close_rfile(RFile
*f
)
144 WFile
*open_new_file(char *name
, long perms
)
149 h
= CreateFile(name
, GENERIC_WRITE
, 0, NULL
,
150 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
151 if (h
== INVALID_HANDLE_VALUE
)
160 WFile
*open_existing_wfile(char *name
, uint64
*size
)
165 h
= CreateFile(name
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
166 OPEN_EXISTING
, 0, 0);
167 if (h
== INVALID_HANDLE_VALUE
)
174 size
->lo
=GetFileSize(h
, &(size
->hi
));
179 int write_to_file(WFile
*f
, void *buffer
, int length
)
183 ret
= WriteFile(f
->h
, buffer
, length
, &written
, NULL
);
185 return -1; /* error */
190 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
192 FILETIME actime
, wrtime
;
193 TIME_POSIX_TO_WIN(atime
, actime
);
194 TIME_POSIX_TO_WIN(mtime
, wrtime
);
195 SetFileTime(f
->h
, NULL
, &actime
, &wrtime
);
198 void close_wfile(WFile
*f
)
204 /* Seek offset bytes through file, from whence, where whence is
205 FROM_START, FROM_CURRENT, or FROM_END */
206 int seek_file(WFile
*f
, uint64 offset
, int whence
)
212 movemethod
= FILE_BEGIN
;
215 movemethod
= FILE_CURRENT
;
218 movemethod
= FILE_END
;
224 SetFilePointer(f
->h
, offset
.lo
, &(offset
.hi
), movemethod
);
226 if (GetLastError() != NO_ERROR
)
232 uint64
get_file_posn(WFile
*f
)
237 ret
.lo
= SetFilePointer(f
->h
, 0L, &(ret
.hi
), FILE_CURRENT
);
242 int file_type(char *name
)
245 attr
= GetFileAttributes(name
);
246 /* We know of no `weird' files under Windows. */
247 if (attr
== (DWORD
)-1)
248 return FILE_TYPE_NONEXISTENT
;
249 else if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
250 return FILE_TYPE_DIRECTORY
;
252 return FILE_TYPE_FILE
;
260 DirHandle
*open_directory(char *name
)
263 WIN32_FIND_DATA fdat
;
267 /* Enumerate files in dir `foo'. */
268 findfile
= dupcat(name
, "/*", NULL
);
269 h
= FindFirstFile(findfile
, &fdat
);
270 if (h
== INVALID_HANDLE_VALUE
)
274 ret
= snew(DirHandle
);
276 ret
->name
= dupstr(fdat
.cFileName
);
280 char *read_filename(DirHandle
*dir
)
285 WIN32_FIND_DATA fdat
;
286 int ok
= FindNextFile(dir
->h
, &fdat
);
290 dir
->name
= dupstr(fdat
.cFileName
);
294 if (dir
->name
[0] == '.' &&
295 (dir
->name
[1] == '\0' ||
296 (dir
->name
[1] == '.' && dir
->name
[2] == '\0'))) {
301 } while (!dir
->name
);
304 char *ret
= dir
->name
;
311 void close_directory(DirHandle
*dir
)
319 int test_wildcard(char *name
, int cmdline
)
322 WIN32_FIND_DATA fdat
;
324 /* First see if the exact name exists. */
325 if (GetFileAttributes(name
) != (DWORD
)-1)
326 return WCTYPE_FILENAME
;
328 /* Otherwise see if a wildcard match finds anything. */
329 fh
= FindFirstFile(name
, &fdat
);
330 if (fh
== INVALID_HANDLE_VALUE
)
331 return WCTYPE_NONEXISTENT
;
334 return WCTYPE_WILDCARD
;
337 struct WildcardMatcher
{
344 * Return a pointer to the portion of str that comes after the last
345 * slash (or backslash or colon, if `local' is TRUE).
347 static char *stripslashes(char *str
, int local
)
352 p
= strchr(str
, ':');
356 p
= strrchr(str
, '/');
360 p
= strrchr(str
, '\\');
367 WildcardMatcher
*begin_wildcard_matching(char *name
)
370 WIN32_FIND_DATA fdat
;
371 WildcardMatcher
*ret
;
374 h
= FindFirstFile(name
, &fdat
);
375 if (h
== INVALID_HANDLE_VALUE
)
378 ret
= snew(WildcardMatcher
);
380 ret
->srcpath
= dupstr(name
);
381 last
= stripslashes(ret
->srcpath
, 1);
383 if (fdat
.cFileName
[0] == '.' &&
384 (fdat
.cFileName
[1] == '\0' ||
385 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
388 ret
->name
= dupcat(ret
->srcpath
, fdat
.cFileName
, NULL
);
393 char *wildcard_get_filename(WildcardMatcher
*dir
)
396 WIN32_FIND_DATA fdat
;
397 int ok
= FindNextFile(dir
->h
, &fdat
);
402 if (fdat
.cFileName
[0] == '.' &&
403 (fdat
.cFileName
[1] == '\0' ||
404 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
407 dir
->name
= dupcat(dir
->srcpath
, fdat
.cFileName
, NULL
);
411 char *ret
= dir
->name
;
418 void finish_wildcard_matching(WildcardMatcher
*dir
)
427 int vet_filename(char *name
)
429 if (strchr(name
, '/') || strchr(name
, '\\') || strchr(name
, ':'))
432 if (!name
[strspn(name
, ".")]) /* entirely composed of dots */
438 int create_directory(char *name
)
440 return CreateDirectory(name
, NULL
) != 0;
443 char *dir_file_cat(char *dir
, char *file
)
445 return dupcat(dir
, "\\", file
, NULL
);
448 /* ----------------------------------------------------------------------
449 * Platform-specific network handling.
453 * Be told what socket we're supposed to be using.
455 static SOCKET sftp_ssh_socket
= INVALID_SOCKET
;
456 static HANDLE netevent
= INVALID_HANDLE_VALUE
;
457 char *do_select(SOCKET skt
, int startup
)
461 sftp_ssh_socket
= skt
;
463 sftp_ssh_socket
= INVALID_SOCKET
;
465 if (p_WSAEventSelect
) {
467 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
468 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
469 netevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
473 if (p_WSAEventSelect(skt
, netevent
, events
) == SOCKET_ERROR
) {
474 switch (p_WSAGetLastError()) {
476 return "Network is down";
478 return "WSAEventSelect(): unknown error";
484 extern int select_result(WPARAM
, LPARAM
);
486 int do_eventsel_loop(HANDLE other_event
)
488 int n
, nhandles
, nallhandles
, netindex
, otherindex
;
493 long now
= GETTICKCOUNT();
495 if (run_timers(now
, &next
)) {
496 ticks
= next
- GETTICKCOUNT();
497 if (ticks
< 0) ticks
= 0; /* just in case */
502 handles
= handle_get_events(&nhandles
);
503 handles
= sresize(handles
, nhandles
+2, HANDLE
);
504 nallhandles
= nhandles
;
506 if (netevent
!= INVALID_HANDLE_VALUE
)
507 handles
[netindex
= nallhandles
++] = netevent
;
510 if (other_event
!= INVALID_HANDLE_VALUE
)
511 handles
[otherindex
= nallhandles
++] = other_event
;
515 n
= WaitForMultipleObjects(nallhandles
, handles
, FALSE
, ticks
);
517 if ((unsigned)(n
- WAIT_OBJECT_0
) < (unsigned)nhandles
) {
518 handle_got_event(handles
[n
- WAIT_OBJECT_0
]);
519 } else if (netindex
>= 0 && n
== WAIT_OBJECT_0
+ netindex
) {
520 WSANETWORKEVENTS things
;
522 extern SOCKET
first_socket(int *), next_socket(int *);
523 extern int select_result(WPARAM
, LPARAM
);
527 * We must not call select_result() for any socket
528 * until we have finished enumerating within the
529 * tree. This is because select_result() may close
530 * the socket and modify the tree.
532 /* Count the active sockets. */
534 for (socket
= first_socket(&socketstate
);
535 socket
!= INVALID_SOCKET
;
536 socket
= next_socket(&socketstate
)) i
++;
538 /* Expand the buffer if necessary. */
539 sklist
= snewn(i
, SOCKET
);
541 /* Retrieve the sockets into sklist. */
543 for (socket
= first_socket(&socketstate
);
544 socket
!= INVALID_SOCKET
;
545 socket
= next_socket(&socketstate
)) {
546 sklist
[skcount
++] = socket
;
549 /* Now we're done enumerating; go through the list. */
550 for (i
= 0; i
< skcount
; i
++) {
553 wp
= (WPARAM
) socket
;
554 if (!p_WSAEnumNetworkEvents(socket
, NULL
, &things
)) {
555 static const struct { int bit
, mask
; } eventtypes
[] = {
556 {FD_CONNECT_BIT
, FD_CONNECT
},
557 {FD_READ_BIT
, FD_READ
},
558 {FD_CLOSE_BIT
, FD_CLOSE
},
559 {FD_OOB_BIT
, FD_OOB
},
560 {FD_WRITE_BIT
, FD_WRITE
},
561 {FD_ACCEPT_BIT
, FD_ACCEPT
},
565 noise_ultralight(socket
);
566 noise_ultralight(things
.lNetworkEvents
);
568 for (e
= 0; e
< lenof(eventtypes
); e
++)
569 if (things
.lNetworkEvents
& eventtypes
[e
].mask
) {
571 int err
= things
.iErrorCode
[eventtypes
[e
].bit
];
572 lp
= WSAMAKESELECTREPLY(eventtypes
[e
].mask
, err
);
573 select_result(wp
, lp
);
583 if (n
== WAIT_TIMEOUT
) {
586 now
= GETTICKCOUNT();
589 if (otherindex
>= 0 && n
== WAIT_OBJECT_0
+ otherindex
)
596 * Wait for some network data and process it.
598 * We have two variants of this function. One uses select() so that
599 * it's compatible with WinSock 1. The other uses WSAEventSelect
600 * and MsgWaitForMultipleObjects, so that we can consistently use
601 * WSAEventSelect throughout; this enables us to also implement
602 * ssh_sftp_get_cmdline() using a parallel mechanism.
604 int ssh_sftp_loop_iteration(void)
606 if (p_WSAEventSelect
== NULL
) {
609 long now
= GETTICKCOUNT();
611 if (sftp_ssh_socket
== INVALID_SOCKET
)
612 return -1; /* doom */
614 if (socket_writable(sftp_ssh_socket
))
615 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_WRITE
);
619 struct timeval tv
, *ptv
;
621 if (run_timers(now
, &next
)) {
622 ticks
= next
- GETTICKCOUNT();
624 ticks
= 1; /* just in case */
625 tv
.tv_sec
= ticks
/ 1000;
626 tv
.tv_usec
= ticks
% 1000 * 1000;
633 FD_SET(sftp_ssh_socket
, &readfds
);
634 ret
= p_select(1, &readfds
, NULL
, NULL
, ptv
);
637 return -1; /* doom */
641 now
= GETTICKCOUNT();
645 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_READ
);
649 return do_eventsel_loop(INVALID_HANDLE_VALUE
);
654 * Read a command line from standard input.
656 * In the presence of WinSock 2, we can use WSAEventSelect to
657 * mediate between the socket and stdin, meaning we can send
658 * keepalives and respond to server events even while waiting at
659 * the PSFTP command prompt. Without WS2, we fall back to a simple
662 struct command_read_ctx
{
667 static DWORD WINAPI
command_read_thread(void *param
)
669 struct command_read_ctx
*ctx
= (struct command_read_ctx
*) param
;
671 ctx
->line
= fgetline(stdin
);
673 SetEvent(ctx
->event
);
678 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
681 struct command_read_ctx actx
, *ctx
= &actx
;
684 fputs(prompt
, stdout
);
687 if ((sftp_ssh_socket
== INVALID_SOCKET
&& no_fds_ok
) ||
688 p_WSAEventSelect
== NULL
) {
689 return fgetline(stdin
); /* very simple */
693 * Create a second thread to read from stdin. Process network
694 * and timing events until it terminates.
696 ctx
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
699 if (!CreateThread(NULL
, 0, command_read_thread
,
700 ctx
, 0, &threadid
)) {
701 fprintf(stderr
, "Unable to create command input thread\n");
706 ret
= do_eventsel_loop(ctx
->event
);
708 /* Error return can only occur if netevent==NULL, and it ain't. */
715 /* ----------------------------------------------------------------------
716 * Main program. Parse arguments etc.
718 int main(int argc
, char *argv
[])
722 ret
= psftp_main(argc
, argv
);