2 * winsftp.c: the Windows-specific parts of PSFTP and PSCP.
11 char *get_ttymode(void *frontend
, const char *mode
) { return NULL
; }
13 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
16 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
18 ret
= console_get_userpass_input(p
, in
, inlen
);
22 /* ----------------------------------------------------------------------
23 * File access abstraction.
27 * Set local current directory. Returns NULL on success, or else an
28 * error message which must be freed after printing.
30 char *psftp_lcd(char *dir
)
34 if (!SetCurrentDirectory(dir
)) {
37 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
38 FORMAT_MESSAGE_FROM_SYSTEM
|
39 FORMAT_MESSAGE_IGNORE_INSERTS
,
41 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
),
42 (LPTSTR
)&message
, 0, NULL
);
43 i
= strcspn((char *)message
, "\n");
44 ret
= dupprintf("%.*s", i
, (LPCTSTR
)message
);
52 * Get local current directory. Returns a string which must be
55 char *psftp_getcwd(void)
57 char *ret
= snewn(256, char);
58 int len
= GetCurrentDirectory(256, ret
);
60 ret
= sresize(ret
, len
, char);
61 GetCurrentDirectory(len
, ret
);
65 #define TIME_POSIX_TO_WIN(t, ft) (*(LONGLONG*)&(ft) = \
66 ((LONGLONG) (t) + (LONGLONG) 11644473600) * (LONGLONG) 10000000)
67 #define TIME_WIN_TO_POSIX(ft, t) ((t) = (unsigned long) \
68 ((*(LONGLONG*)&(ft)) / (LONGLONG) 10000000 - (LONGLONG) 11644473600))
74 RFile
*open_existing_file(char *name
, uint64
*size
,
75 unsigned long *mtime
, unsigned long *atime
)
80 h
= CreateFile(name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
,
82 if (h
== INVALID_HANDLE_VALUE
)
89 size
->lo
=GetFileSize(h
, &(size
->hi
));
92 FILETIME actime
, wrtime
;
93 GetFileTime(h
, NULL
, &actime
, &wrtime
);
95 TIME_WIN_TO_POSIX(actime
, *atime
);
97 TIME_WIN_TO_POSIX(wrtime
, *mtime
);
103 int read_from_file(RFile
*f
, void *buffer
, int length
)
106 ret
= ReadFile(f
->h
, buffer
, length
, &read
, NULL
);
108 return -1; /* error */
113 void close_rfile(RFile
*f
)
123 WFile
*open_new_file(char *name
)
128 h
= CreateFile(name
, GENERIC_WRITE
, 0, NULL
,
129 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
130 if (h
== INVALID_HANDLE_VALUE
)
139 WFile
*open_existing_wfile(char *name
, uint64
*size
)
144 h
= CreateFile(name
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
145 OPEN_EXISTING
, 0, 0);
146 if (h
== INVALID_HANDLE_VALUE
)
153 size
->lo
=GetFileSize(h
, &(size
->hi
));
158 int write_to_file(WFile
*f
, void *buffer
, int length
)
161 ret
= WriteFile(f
->h
, buffer
, length
, &written
, NULL
);
163 return -1; /* error */
168 void set_file_times(WFile
*f
, unsigned long mtime
, unsigned long atime
)
170 FILETIME actime
, wrtime
;
171 TIME_POSIX_TO_WIN(atime
, actime
);
172 TIME_POSIX_TO_WIN(mtime
, wrtime
);
173 SetFileTime(f
->h
, NULL
, &actime
, &wrtime
);
176 void close_wfile(WFile
*f
)
182 /* Seek offset bytes through file, from whence, where whence is
183 FROM_START, FROM_CURRENT, or FROM_END */
184 int seek_file(WFile
*f
, uint64 offset
, int whence
)
190 movemethod
= FILE_BEGIN
;
193 movemethod
= FILE_CURRENT
;
196 movemethod
= FILE_END
;
202 SetFilePointer(f
->h
, offset
.lo
, &(offset
.hi
), movemethod
);
204 if (GetLastError() != NO_ERROR
)
210 uint64
get_file_posn(WFile
*f
)
215 ret
.lo
= SetFilePointer(f
->h
, 0L, &(ret
.hi
), FILE_CURRENT
);
220 int file_type(char *name
)
223 attr
= GetFileAttributes(name
);
224 /* We know of no `weird' files under Windows. */
225 if (attr
== (DWORD
)-1)
226 return FILE_TYPE_NONEXISTENT
;
227 else if (attr
& FILE_ATTRIBUTE_DIRECTORY
)
228 return FILE_TYPE_DIRECTORY
;
230 return FILE_TYPE_FILE
;
238 DirHandle
*open_directory(char *name
)
241 WIN32_FIND_DATA fdat
;
245 /* Enumerate files in dir `foo'. */
246 findfile
= dupcat(name
, "/*", NULL
);
247 h
= FindFirstFile(findfile
, &fdat
);
248 if (h
== INVALID_HANDLE_VALUE
)
252 ret
= snew(DirHandle
);
254 ret
->name
= dupstr(fdat
.cFileName
);
258 char *read_filename(DirHandle
*dir
)
263 WIN32_FIND_DATA fdat
;
264 int ok
= FindNextFile(dir
->h
, &fdat
);
268 dir
->name
= dupstr(fdat
.cFileName
);
272 if (dir
->name
[0] == '.' &&
273 (dir
->name
[1] == '\0' ||
274 (dir
->name
[1] == '.' && dir
->name
[2] == '\0'))) {
279 } while (!dir
->name
);
282 char *ret
= dir
->name
;
289 void close_directory(DirHandle
*dir
)
297 int test_wildcard(char *name
, int cmdline
)
300 WIN32_FIND_DATA fdat
;
302 /* First see if the exact name exists. */
303 if (GetFileAttributes(name
) != (DWORD
)-1)
304 return WCTYPE_FILENAME
;
306 /* Otherwise see if a wildcard match finds anything. */
307 fh
= FindFirstFile(name
, &fdat
);
308 if (fh
== INVALID_HANDLE_VALUE
)
309 return WCTYPE_NONEXISTENT
;
312 return WCTYPE_WILDCARD
;
315 struct WildcardMatcher
{
322 * Return a pointer to the portion of str that comes after the last
323 * slash (or backslash or colon, if `local' is TRUE).
325 static char *stripslashes(char *str
, int local
)
330 p
= strchr(str
, ':');
334 p
= strrchr(str
, '/');
338 p
= strrchr(str
, '\\');
345 WildcardMatcher
*begin_wildcard_matching(char *name
)
348 WIN32_FIND_DATA fdat
;
349 WildcardMatcher
*ret
;
352 h
= FindFirstFile(name
, &fdat
);
353 if (h
== INVALID_HANDLE_VALUE
)
356 ret
= snew(WildcardMatcher
);
358 ret
->srcpath
= dupstr(name
);
359 last
= stripslashes(ret
->srcpath
, 1);
361 if (fdat
.cFileName
[0] == '.' &&
362 (fdat
.cFileName
[1] == '\0' ||
363 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
366 ret
->name
= dupcat(ret
->srcpath
, fdat
.cFileName
, NULL
);
371 char *wildcard_get_filename(WildcardMatcher
*dir
)
374 WIN32_FIND_DATA fdat
;
375 int ok
= FindNextFile(dir
->h
, &fdat
);
380 if (fdat
.cFileName
[0] == '.' &&
381 (fdat
.cFileName
[1] == '\0' ||
382 (fdat
.cFileName
[1] == '.' && fdat
.cFileName
[2] == '\0')))
385 dir
->name
= dupcat(dir
->srcpath
, fdat
.cFileName
, NULL
);
389 char *ret
= dir
->name
;
396 void finish_wildcard_matching(WildcardMatcher
*dir
)
405 int vet_filename(char *name
)
407 if (strchr(name
, '/') || strchr(name
, '\\') || strchr(name
, ':'))
410 if (!name
[strspn(name
, ".")]) /* entirely composed of dots */
416 int create_directory(char *name
)
418 return CreateDirectory(name
, NULL
) != 0;
421 char *dir_file_cat(char *dir
, char *file
)
423 return dupcat(dir
, "\\", file
, NULL
);
426 /* ----------------------------------------------------------------------
427 * Platform-specific network handling.
431 * Be told what socket we're supposed to be using.
433 static SOCKET sftp_ssh_socket
= INVALID_SOCKET
;
434 static HANDLE netevent
= INVALID_HANDLE_VALUE
;
435 char *do_select(SOCKET skt
, int startup
)
439 sftp_ssh_socket
= skt
;
441 sftp_ssh_socket
= INVALID_SOCKET
;
443 if (p_WSAEventSelect
) {
445 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
446 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
447 netevent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
451 if (p_WSAEventSelect(skt
, netevent
, events
) == SOCKET_ERROR
) {
452 switch (p_WSAGetLastError()) {
454 return "Network is down";
456 return "WSAEventSelect(): unknown error";
462 extern int select_result(WPARAM
, LPARAM
);
464 int do_eventsel_loop(HANDLE other_event
)
466 int n
, nhandles
, nallhandles
, netindex
, otherindex
;
471 long now
= GETTICKCOUNT();
473 if (run_timers(now
, &next
)) {
474 ticks
= next
- GETTICKCOUNT();
475 if (ticks
< 0) ticks
= 0; /* just in case */
480 handles
= handle_get_events(&nhandles
);
481 handles
= sresize(handles
, nhandles
+2, HANDLE
);
482 nallhandles
= nhandles
;
484 if (netevent
!= INVALID_HANDLE_VALUE
)
485 handles
[netindex
= nallhandles
++] = netevent
;
488 if (other_event
!= INVALID_HANDLE_VALUE
)
489 handles
[otherindex
= nallhandles
++] = other_event
;
493 n
= WaitForMultipleObjects(nallhandles
, handles
, FALSE
, ticks
);
495 if ((unsigned)(n
- WAIT_OBJECT_0
) < (unsigned)nhandles
) {
496 handle_got_event(handles
[n
- WAIT_OBJECT_0
]);
497 } else if (netindex
>= 0 && n
== WAIT_OBJECT_0
+ netindex
) {
498 WSANETWORKEVENTS things
;
500 extern SOCKET
first_socket(int *), next_socket(int *);
501 extern int select_result(WPARAM
, LPARAM
);
505 * We must not call select_result() for any socket
506 * until we have finished enumerating within the
507 * tree. This is because select_result() may close
508 * the socket and modify the tree.
510 /* Count the active sockets. */
512 for (socket
= first_socket(&socketstate
);
513 socket
!= INVALID_SOCKET
;
514 socket
= next_socket(&socketstate
)) i
++;
516 /* Expand the buffer if necessary. */
517 sklist
= snewn(i
, SOCKET
);
519 /* Retrieve the sockets into sklist. */
521 for (socket
= first_socket(&socketstate
);
522 socket
!= INVALID_SOCKET
;
523 socket
= next_socket(&socketstate
)) {
524 sklist
[skcount
++] = socket
;
527 /* Now we're done enumerating; go through the list. */
528 for (i
= 0; i
< skcount
; i
++) {
531 wp
= (WPARAM
) socket
;
532 if (!p_WSAEnumNetworkEvents(socket
, NULL
, &things
)) {
533 static const struct { int bit
, mask
; } eventtypes
[] = {
534 {FD_CONNECT_BIT
, FD_CONNECT
},
535 {FD_READ_BIT
, FD_READ
},
536 {FD_CLOSE_BIT
, FD_CLOSE
},
537 {FD_OOB_BIT
, FD_OOB
},
538 {FD_WRITE_BIT
, FD_WRITE
},
539 {FD_ACCEPT_BIT
, FD_ACCEPT
},
543 noise_ultralight(socket
);
544 noise_ultralight(things
.lNetworkEvents
);
546 for (e
= 0; e
< lenof(eventtypes
); e
++)
547 if (things
.lNetworkEvents
& eventtypes
[e
].mask
) {
549 int err
= things
.iErrorCode
[eventtypes
[e
].bit
];
550 lp
= WSAMAKESELECTREPLY(eventtypes
[e
].mask
, err
);
551 select_result(wp
, lp
);
561 if (n
== WAIT_TIMEOUT
) {
564 now
= GETTICKCOUNT();
567 if (otherindex
>= 0 && n
== WAIT_OBJECT_0
+ otherindex
)
574 * Wait for some network data and process it.
576 * We have two variants of this function. One uses select() so that
577 * it's compatible with WinSock 1. The other uses WSAEventSelect
578 * and MsgWaitForMultipleObjects, so that we can consistently use
579 * WSAEventSelect throughout; this enables us to also implement
580 * ssh_sftp_get_cmdline() using a parallel mechanism.
582 int ssh_sftp_loop_iteration(void)
584 if (p_WSAEventSelect
== NULL
) {
587 long now
= GETTICKCOUNT();
589 if (sftp_ssh_socket
== INVALID_SOCKET
)
590 return -1; /* doom */
592 if (socket_writable(sftp_ssh_socket
))
593 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_WRITE
);
597 struct timeval tv
, *ptv
;
599 if (run_timers(now
, &next
)) {
600 ticks
= next
- GETTICKCOUNT();
602 ticks
= 1; /* just in case */
603 tv
.tv_sec
= ticks
/ 1000;
604 tv
.tv_usec
= ticks
% 1000 * 1000;
611 FD_SET(sftp_ssh_socket
, &readfds
);
612 ret
= p_select(1, &readfds
, NULL
, NULL
, ptv
);
615 return -1; /* doom */
619 now
= GETTICKCOUNT();
623 select_result((WPARAM
) sftp_ssh_socket
, (LPARAM
) FD_READ
);
627 return do_eventsel_loop(INVALID_HANDLE_VALUE
);
632 * Read a command line from standard input.
634 * In the presence of WinSock 2, we can use WSAEventSelect to
635 * mediate between the socket and stdin, meaning we can send
636 * keepalives and respond to server events even while waiting at
637 * the PSFTP command prompt. Without WS2, we fall back to a simple
640 struct command_read_ctx
{
645 static DWORD WINAPI
command_read_thread(void *param
)
647 struct command_read_ctx
*ctx
= (struct command_read_ctx
*) param
;
649 ctx
->line
= fgetline(stdin
);
651 SetEvent(ctx
->event
);
656 char *ssh_sftp_get_cmdline(char *prompt
, int no_fds_ok
)
659 struct command_read_ctx actx
, *ctx
= &actx
;
662 fputs(prompt
, stdout
);
665 if ((sftp_ssh_socket
== INVALID_SOCKET
&& no_fds_ok
) ||
666 p_WSAEventSelect
== NULL
) {
667 return fgetline(stdin
); /* very simple */
671 * Create a second thread to read from stdin. Process network
672 * and timing events until it terminates.
674 ctx
->event
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
677 if (!CreateThread(NULL
, 0, command_read_thread
,
678 ctx
, 0, &threadid
)) {
679 fprintf(stderr
, "Unable to create command input thread\n");
684 ret
= do_eventsel_loop(ctx
->event
);
686 /* Error return can only occur if netevent==NULL, and it ain't. */
693 /* ----------------------------------------------------------------------
694 * Main program. Parse arguments etc.
696 int main(int argc
, char *argv
[])
700 ret
= psftp_main(argc
, argv
);