Merge pull request #551 from falemagn/pull-reqs/cca01a4_WOLFSSH_FTP_FSETSTAT

pull/584/head
John Safranek 2023-09-15 15:15:16 -07:00 committed by GitHub
commit 79bc747b46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 136 additions and 2 deletions

View File

@ -1473,6 +1473,11 @@ int wolfSSH_SFTP_read(WOLFSSH* ssh)
wolfSSH_SFTP_buffer_data(&state->buffer),
wolfSSH_SFTP_buffer_size(&state->buffer));
break;
case WOLFSSH_FTP_FSETSTAT:
ret = wolfSSH_SFTP_RecvFSetSTAT(ssh, state->reqId,
wolfSSH_SFTP_buffer_data(&state->buffer),
wolfSSH_SFTP_buffer_size(&state->buffer));
break;
#endif
#ifndef NO_WOLFSSH_DIR
@ -5002,8 +5007,10 @@ static int SFTP_SetFileAttributes(WOLFSSH* ssh, char* name, WS_SFTP_FILEATRB* at
#endif
/* check if time attribute present */
if (atr->flags & WOLFSSH_FILEATRB_TIME) {
/* @TODO set time */
if (ret == WS_SUCCESS && (atr->flags & WOLFSSH_FILEATRB_TIME)) {
if (WSETTIME(ssh->fs, name, atr->atime, atr->mtime) != 0) {
ret = WS_BAD_FILE_E;
}
}
/* check if extended attributes are present */
@ -5016,6 +5023,47 @@ static int SFTP_SetFileAttributes(WOLFSSH* ssh, char* name, WS_SFTP_FILEATRB* at
}
/* sets a files attributes
* returns WS_SUCCESS on success */
static int SFTP_SetFileAttributesHandle(WOLFSSH* ssh, WFD handle, WS_SFTP_FILEATRB* atr)
{
int ret = WS_SUCCESS;
/* check if size attribute present */
if (atr->flags & WOLFSSH_FILEATRB_SIZE) {
/* @TODO set file size */
}
/* check if uid and gid attribute present */
if (atr->flags & WOLFSSH_FILEATRB_UIDGID) {
/* @TODO set group and user id */
}
#ifndef USE_WINDOWS_API
/* check if permissions attribute present */
if (atr->flags & WOLFSSH_FILEATRB_PERM) {
if (WFCHMOD(ssh->fs, handle, atr->per) != 0) {
ret = WS_BAD_FILE_E;
}
}
#endif
/* check if time attribute present */
if (ret == WS_SUCCESS && (atr->flags & WOLFSSH_FILEATRB_TIME)) {
if (WFSETTIME(ssh->fs, handle, atr->atime, atr->mtime) != 0) {
ret = WS_BAD_FILE_E;
}
}
/* check if extended attributes are present */
if (atr->flags & WOLFSSH_FILEATRB_EXT) {
/* @TODO handle extensions */
}
(void)ssh;
return ret ;
}
/* Handles a packet sent to set attributes of path
*
* returns WS_SUCCESS on success
@ -5097,6 +5145,86 @@ int wolfSSH_SFTP_RecvSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
return ret;
}
/* Handles a packet sent to set attributes of file handle
*
* returns WS_SUCCESS on success
*/
int wolfSSH_SFTP_RecvFSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
{
WS_SFTP_FILEATRB atr;
int ret = WS_SUCCESS;
WFD fd;
word32 sz;
word32 idx = 0;
byte* out = NULL;
word32 outSz = 0;
char suc[] = "Set Attirbutes";
char ser[] = "Unable to set attributes error";
char per[] = "Unable to parse attributes error";
char* res = suc;
byte type = WOLFSSH_FTP_OK;
if (ssh == NULL) {
return WS_BAD_ARGUMENT;
}
WLOG(WS_LOG_SFTP, "Receiving WOLFSSH_FTP_FSETSTAT");
if (maxSz < UINT32_SZ) {
/* not enough for an ato32 call */
return WS_BUFFER_E;
}
/* get file handle */
ato32(data + idx, &sz); idx += UINT32_SZ;
if (sz + idx > maxSz || sz > WOLFSSH_MAX_HANDLE) {
return WS_BUFFER_E;
}
WMEMSET((byte*)&fd, 0, sizeof(WFD));
WMEMCPY((byte*)&fd, data + idx, sz); idx += sz;
if (ret == WS_SUCCESS &&
SFTP_ParseAtributes_buffer(ssh, &atr, data, &idx, maxSz) != 0) {
type = WOLFSSH_FTP_FAILURE;
res = per;
ret = WS_BAD_FILE_E;
}
/* try to set file attributes and send status back to client */
if (ret == WS_SUCCESS && (ret = SFTP_SetFileAttributesHandle(ssh, fd, &atr))
!= WS_SUCCESS) {
/* tell peer that was not ok */
WLOG(WS_LOG_SFTP, "Unable to get set attributes of open file");
type = WOLFSSH_FTP_FAILURE;
res = ser;
ret = WS_BAD_FILE_E;
}
if (wolfSSH_SFTP_CreateStatus(ssh, type, reqId, res, "English", NULL,
&outSz) != WS_SIZE_ONLY) {
return WS_FATAL_ERROR;
}
out = (byte*)WMALLOC(outSz, ssh->ctx->heap, DYNTYPE_BUFFER);
if (out == NULL) {
return WS_MEMORY_E;
}
if (wolfSSH_SFTP_CreateStatus(ssh, type, reqId, res, "English", out,
&outSz) != WS_SUCCESS) {
WFREE(out, ssh->ctx->heap, DYNTYPE_BUFFER);
return WS_FATAL_ERROR;
}
/* set send out buffer, "out" is taken by ssh */
wolfSSH_SFTP_RecvSetSend(ssh, out, outSz);
return ret;
}
#endif /* _WIN32_WCE */
#endif /* !NO_WOLFSSH_SERVER */

View File

@ -370,6 +370,8 @@ extern "C" {
#define WREWIND(fs,s) rewind((s))
#define WSEEK_END SEEK_END
#define WBADFILE NULL
#define WSETTIME(fs,f,a,m) (0)
#define WFSETTIME(fs,fd,a,m) (0)
#ifdef WOLFSSL_VXWORKS
#define WUTIMES(f,t) (WS_SUCCESS)
#elif defined(USE_WINDOWS_API)
@ -380,8 +382,10 @@ extern "C" {
#ifndef USE_WINDOWS_API
#define WCHMOD(fs,f,m) chmod((f),(m))
#define WFCHMOD(fs,fd,m) fchmod((fd),(m))
#else
#define WCHMOD(fs,f,m) _chmod((f),(m))
#define WFCHMOD(fs,fd,m) _fchmod((fd),(m))
#endif
#if (defined(WOLFSSH_SCP) || \

View File

@ -254,6 +254,8 @@ WOLFSSH_LOCAL int wolfSSH_SFTP_RecvSetSTAT(WOLFSSH* ssh, int reqId, byte* data,
word32 maxSz);
WOLFSSH_LOCAL int wolfSSH_SFTP_RecvFSTAT(WOLFSSH* ssh, int reqId, byte* data,
word32 maxSz);
WOLFSSH_LOCAL int wolfSSH_SFTP_RecvFSetSTAT(WOLFSSH* ssh, int reqId, byte* data,
word32 maxSz);
#ifndef NO_WOLFSSH_DIR
WOLFSSH_LOCAL int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, byte* data,