📄 ftp.c
字号:
/*
* WININET - Ftp implementation
*
* Copyright 1999 Corel Corporation
* Copyright 2004 Mike McCormack for CodeWeavers
* Copyright 2004 Kevin Koltzau
*
* Ulrich Czekalla
* Noureddine Jemmali
*
* Copyright 2000 Andreas Mohr
* Copyright 2002 Jaco Greeff
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "wine/port.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <time.h>
#include <assert.h>
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "wininet.h"
#include "winnls.h"
#include "winerror.h"
#include "winreg.h"
#include "winternl.h"
#include "shlwapi.h"
#include "wine/debug.h"
#include "internet.h"
WINE_DEFAULT_DEBUG_CHANNEL(wininet);
#define DATA_PACKET_SIZE 0x2000
#define szCRLF "\r\n"
#define MAX_BACKLOG 5
typedef enum {
/* FTP commands with arguments. */
FTP_CMD_ACCT,
FTP_CMD_CWD,
FTP_CMD_DELE,
FTP_CMD_MKD,
FTP_CMD_PASS,
FTP_CMD_PORT,
FTP_CMD_RETR,
FTP_CMD_RMD,
FTP_CMD_RNFR,
FTP_CMD_RNTO,
FTP_CMD_STOR,
FTP_CMD_TYPE,
FTP_CMD_USER,
FTP_CMD_SIZE,
/* FTP commands without arguments. */
FTP_CMD_ABOR,
FTP_CMD_LIST,
FTP_CMD_NLST,
FTP_CMD_PASV,
FTP_CMD_PWD,
FTP_CMD_QUIT,
} FTP_COMMAND;
static const CHAR *szFtpCommands[] = {
"ACCT",
"CWD",
"DELE",
"MKD",
"PASS",
"PORT",
"RETR",
"RMD",
"RNFR",
"RNTO",
"STOR",
"TYPE",
"USER",
"SIZE",
"ABOR",
"LIST",
"NLST",
"PASV",
"PWD",
"QUIT",
};
static const CHAR szMonths[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
static const WCHAR szNoAccount[] = {'n','o','a','c','c','o','u','n','t','\0'};
static void FTP_CloseFileTransferHandle(LPWININETHANDLEHEADER hdr);
static void FTP_CloseSessionHandle(LPWININETHANDLEHEADER hdr);
static void FTP_CloseFindNextHandle(LPWININETHANDLEHEADER hdr);
static BOOL FTP_SendCommand(INT nSocket, FTP_COMMAND ftpCmd, LPCWSTR lpszParam,
INTERNET_STATUS_CALLBACK lpfnStatusCB, LPWININETHANDLEHEADER hdr, DWORD dwContext);
static BOOL FTP_SendStore(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
static BOOL FTP_GetDataSocket(LPWININETFTPSESSIONW lpwfs, LPINT nDataSocket);
static BOOL FTP_SendData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, HANDLE hFile);
static INT FTP_ReceiveResponse(LPWININETFTPSESSIONW lpwfs, DWORD dwContext);
static DWORD FTP_SendRetrieve(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD dwType);
static BOOL FTP_RetrieveFileData(LPWININETFTPSESSIONW lpwfs, INT nDataSocket, DWORD nBytes, HANDLE hFile);
static BOOL FTP_InitListenSocket(LPWININETFTPSESSIONW lpwfs);
static BOOL FTP_ConnectToHost(LPWININETFTPSESSIONW lpwfs);
static BOOL FTP_SendPassword(LPWININETFTPSESSIONW lpwfs);
static BOOL FTP_SendAccount(LPWININETFTPSESSIONW lpwfs);
static BOOL FTP_SendType(LPWININETFTPSESSIONW lpwfs, DWORD dwType);
static BOOL FTP_GetFileSize(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszRemoteFile, DWORD *dwSize);
static BOOL FTP_SendPort(LPWININETFTPSESSIONW lpwfs);
static BOOL FTP_DoPassive(LPWININETFTPSESSIONW lpwfs);
static BOOL FTP_SendPortOrPasv(LPWININETFTPSESSIONW lpwfs);
static BOOL FTP_ParsePermission(LPCSTR lpszPermission, LPFILEPROPERTIESW lpfp);
static BOOL FTP_ParseNextFile(INT nSocket, LPCWSTR lpszSearchFile, LPFILEPROPERTIESW fileprop);
static BOOL FTP_ParseDirectory(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
LPFILEPROPERTIESW *lpafp, LPDWORD dwfp);
static HINTERNET FTP_ReceiveFileList(LPWININETFTPSESSIONW lpwfs, INT nSocket, LPCWSTR lpszSearchFile,
LPWIN32_FIND_DATAW lpFindFileData, DWORD dwContext);
static DWORD FTP_SetResponseError(DWORD dwResponse);
/***********************************************************************
* FtpPutFileA (WININET.@)
*
* Uploads a file to the FTP server
*
* RETURNS
* TRUE on success
* FALSE on failure
*
*/
BOOL WINAPI FtpPutFileA(HINTERNET hConnect, LPCSTR lpszLocalFile,
LPCSTR lpszNewRemoteFile, DWORD dwFlags, DWORD dwContext)
{
LPWSTR lpwzLocalFile;
LPWSTR lpwzNewRemoteFile;
BOOL ret;
lpwzLocalFile = lpszLocalFile?WININET_strdup_AtoW(lpszLocalFile):NULL;
lpwzNewRemoteFile = lpszNewRemoteFile?WININET_strdup_AtoW(lpszNewRemoteFile):NULL;
ret = FtpPutFileW(hConnect, lpwzLocalFile, lpwzNewRemoteFile,
dwFlags, dwContext);
HeapFree(GetProcessHeap(), 0, lpwzLocalFile);
HeapFree(GetProcessHeap(), 0, lpwzNewRemoteFile);
return ret;
}
/***********************************************************************
* FtpPutFileW (WININET.@)
*
* Uploads a file to the FTP server
*
* RETURNS
* TRUE on success
* FALSE on failure
*
*/
BOOL WINAPI FtpPutFileW(HINTERNET hConnect, LPCWSTR lpszLocalFile,
LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD dwContext)
{
LPWININETFTPSESSIONW lpwfs;
LPWININETAPPINFOW hIC = NULL;
BOOL r = FALSE;
lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
{
INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
goto lend;
}
hIC = (LPWININETAPPINFOW) lpwfs->hdr.lpwhparent;
if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
{
WORKREQUEST workRequest;
struct WORKREQ_FTPPUTFILEW *req = &workRequest.u.FtpPutFileW;
workRequest.asyncall = FTPPUTFILEW;
workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
req->lpszLocalFile = WININET_strdupW(lpszLocalFile);
req->lpszNewRemoteFile = WININET_strdupW(lpszNewRemoteFile);
req->dwFlags = dwFlags;
req->dwContext = dwContext;
r = INTERNET_AsyncCall(&workRequest);
}
else
{
r = FTP_FtpPutFileW(lpwfs, lpszLocalFile,
lpszNewRemoteFile, dwFlags, dwContext);
}
lend:
if( lpwfs )
WININET_Release( &lpwfs->hdr );
return r;
}
/***********************************************************************
* FTP_FtpPutFileW (Internal)
*
* Uploads a file to the FTP server
*
* RETURNS
* TRUE on success
* FALSE on failure
*
*/
BOOL WINAPI FTP_FtpPutFileW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszLocalFile,
LPCWSTR lpszNewRemoteFile, DWORD dwFlags, DWORD dwContext)
{
HANDLE hFile = NULL;
BOOL bSuccess = FALSE;
LPWININETAPPINFOW hIC = NULL;
INT nResCode;
TRACE(" lpszLocalFile(%s) lpszNewRemoteFile(%s)\n", debugstr_w(lpszLocalFile), debugstr_w(lpszNewRemoteFile));
if (!lpszLocalFile || !lpszNewRemoteFile)
{
INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
assert( WH_HFTPSESSION == lpwfs->hdr.htype);
/* Clear any error information */
INTERNET_SetLastError(0);
/* Open file to be uploaded */
if (INVALID_HANDLE_VALUE ==
(hFile = CreateFileW(lpszLocalFile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)))
{
INTERNET_SetLastError(ERROR_FILE_NOT_FOUND);
goto lend;
}
hIC = (LPWININETAPPINFOW) lpwfs->hdr.lpwhparent;
SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
if (FTP_SendStore(lpwfs, lpszNewRemoteFile, dwFlags))
{
INT nDataSocket;
/* Get data socket to server */
if (FTP_GetDataSocket(lpwfs, &nDataSocket))
{
FTP_SendData(lpwfs, nDataSocket, hFile);
closesocket(nDataSocket);
nResCode = FTP_ReceiveResponse(lpwfs, dwContext);
if (nResCode)
{
if (nResCode == 226)
bSuccess = TRUE;
else
FTP_SetResponseError(nResCode);
}
}
}
lend:
if (lpwfs->lstnSocket != -1)
closesocket(lpwfs->lstnSocket);
if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
{
INTERNET_ASYNC_RESULT iar;
iar.dwResult = (DWORD)bSuccess;
iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
&iar, sizeof(INTERNET_ASYNC_RESULT));
}
if (hFile)
CloseHandle(hFile);
return bSuccess;
}
/***********************************************************************
* FtpSetCurrentDirectoryA (WININET.@)
*
* Change the working directory on the FTP server
*
* RETURNS
* TRUE on success
* FALSE on failure
*
*/
BOOL WINAPI FtpSetCurrentDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
{
LPWSTR lpwzDirectory;
BOOL ret;
lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
ret = FtpSetCurrentDirectoryW(hConnect, lpwzDirectory);
HeapFree(GetProcessHeap(), 0, lpwzDirectory);
return ret;
}
/***********************************************************************
* FtpSetCurrentDirectoryW (WININET.@)
*
* Change the working directory on the FTP server
*
* RETURNS
* TRUE on success
* FALSE on failure
*
*/
BOOL WINAPI FtpSetCurrentDirectoryW(HINTERNET hConnect, LPCWSTR lpszDirectory)
{
LPWININETFTPSESSIONW lpwfs;
LPWININETAPPINFOW hIC = NULL;
BOOL r = FALSE;
lpwfs = (LPWININETFTPSESSIONW) WININET_GetObject( hConnect );
if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
{
INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
goto lend;
}
TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
hIC = (LPWININETAPPINFOW) lpwfs->hdr.lpwhparent;
if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
{
WORKREQUEST workRequest;
struct WORKREQ_FTPSETCURRENTDIRECTORYW *req;
workRequest.asyncall = FTPSETCURRENTDIRECTORYW;
workRequest.hdr = WININET_AddRef( &lpwfs->hdr );
req = &workRequest.u.FtpSetCurrentDirectoryW;
req->lpszDirectory = WININET_strdupW(lpszDirectory);
r = INTERNET_AsyncCall(&workRequest);
}
else
{
r = FTP_FtpSetCurrentDirectoryW(lpwfs, lpszDirectory);
}
lend:
if( lpwfs )
WININET_Release( &lpwfs->hdr );
return r;
}
/***********************************************************************
* FTP_FtpSetCurrentDirectoryW (Internal)
*
* Change the working directory on the FTP server
*
* RETURNS
* TRUE on success
* FALSE on failure
*
*/
BOOL WINAPI FTP_FtpSetCurrentDirectoryW(LPWININETFTPSESSIONW lpwfs, LPCWSTR lpszDirectory)
{
INT nResCode;
LPWININETAPPINFOW hIC = NULL;
DWORD bSuccess = FALSE;
TRACE("lpszDirectory(%s)\n", debugstr_w(lpszDirectory));
if (NULL == lpwfs || WH_HFTPSESSION != lpwfs->hdr.htype)
{
INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
return FALSE;
}
/* Clear any error information */
INTERNET_SetLastError(0);
hIC = (LPWININETAPPINFOW) lpwfs->hdr.lpwhparent;
if (!FTP_SendCommand(lpwfs->sndSocket, FTP_CMD_CWD, lpszDirectory,
lpwfs->hdr.lpfnStatusCB, &lpwfs->hdr, lpwfs->hdr.dwContext))
goto lend;
nResCode = FTP_ReceiveResponse(lpwfs, lpwfs->hdr.dwContext);
if (nResCode)
{
if (nResCode == 250)
bSuccess = TRUE;
else
FTP_SetResponseError(nResCode);
}
lend:
if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
{
INTERNET_ASYNC_RESULT iar;
iar.dwResult = (DWORD)bSuccess;
iar.dwError = bSuccess ? ERROR_SUCCESS : ERROR_INTERNET_EXTENDED_ERROR;
SendAsyncCallback(&lpwfs->hdr, lpwfs->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE,
&iar, sizeof(INTERNET_ASYNC_RESULT));
}
return bSuccess;
}
/***********************************************************************
* FtpCreateDirectoryA (WININET.@)
*
* Create new directory on the FTP server
*
* RETURNS
* TRUE on success
* FALSE on failure
*
*/
BOOL WINAPI FtpCreateDirectoryA(HINTERNET hConnect, LPCSTR lpszDirectory)
{
LPWSTR lpwzDirectory;
BOOL ret;
lpwzDirectory = lpszDirectory?WININET_strdup_AtoW(lpszDirectory):NULL;
ret = FtpCreateDirectoryW(hConnect, lpwzDirectory);
HeapFree(GetProcessHeap(), 0, lpwzDirectory);
return ret;
}
/***********************************************************************
* FtpCreateDirectoryW (WININET.@)
*
* Create new directory on the FTP server
*
* RETURNS
* TRUE on success
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -