/*
* Copyright (c) 2000 Markus Friedl. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "includes.h"
RCSID("$OpenBSD: sftp-server.c,v 1.8 2000/12/19 22:43:44 markus Exp $");
#include "ssh.h"
#include "buffer.h"
#include "bufaux.h"
#include "getput.h"
#include "xmalloc.h"
/* version */
#define SSH_FILEXFER_VERSION 2
/* client to server */
#define SSH_FXP_INIT 1
#define SSH_FXP_OPEN 3
#define SSH_FXP_CLOSE 4
#define SSH_FXP_READ 5
#define SSH_FXP_WRITE 6
#define SSH_FXP_LSTAT 7
#define SSH_FXP_FSTAT 8
#define SSH_FXP_SETSTAT 9
#define SSH_FXP_FSETSTAT 10
#define SSH_FXP_OPENDIR 11
#define SSH_FXP_READDIR 12
#define SSH_FXP_REMOVE 13
#define SSH_FXP_MKDIR 14
#define SSH_FXP_RMDIR 15
#define SSH_FXP_REALPATH 16
#define SSH_FXP_STAT 17
#define SSH_FXP_RENAME 18
/* server to client */
#define SSH_FXP_VERSION 2
#define SSH_FXP_STATUS 101
#define SSH_FXP_HANDLE 102
#define SSH_FXP_DATA 103
#define SSH_FXP_NAME 104
#define SSH_FXP_ATTRS 105
/* portable open modes */
#define SSH_FXF_READ 0x01
#define SSH_FXF_WRITE 0x02
#define SSH_FXF_APPEND 0x04
#define SSH_FXF_CREAT 0x08
#define SSH_FXF_TRUNC 0x10
#define SSH_FXF_EXCL 0x20
/* attributes */
#define SSH_FXA_HAVE_SIZE 0x01
#define SSH_FXA_HAVE_UGID 0x02
#define SSH_FXA_HAVE_PERM 0x04
#define SSH_FXA_HAVE_TIME 0x08
/* status messages */
#define SSH_FX_OK 0x00
#define SSH_FX_EOF 0x01
#define SSH_FX_NO_SUCH_FILE 0x02
#define SSH_FX_PERMISSION_DENIED 0x03
#define SSH_FX_FAILURE 0x04
#define SSH_FX_BAD_MESSAGE 0x05
#define SSH_FX_NO_CONNECTION 0x06
#define SSH_FX_CONNECTION_LOST 0x07
/* helper */
#define get_int() buffer_get_int(&iqueue);
#define get_string(lenp) buffer_get_string(&iqueue, lenp);
#define TRACE log
#ifdef HAVE___PROGNAME
extern char *__progname;
#else
char *__progname;
#endif
/* input and output queue */
Buffer iqueue;
Buffer oqueue;
/* portable attibutes, etc. */
typedef struct Attrib Attrib;
typedef struct Stat Stat;
struct Attrib
{
u_int32_t flags;
u_int32_t size_high;
u_int32_t size_low;
u_int64_t size;
u_int32_t uid;
u_int32_t gid;
u_int32_t perm;
u_int32_t atime;
u_int32_t mtime;
};
struct Stat
{
char *name;
char *long_name;
Attrib attrib;
};
int
errno_to_portable(int unixerrno)
{
int ret = 0;
switch (unixerrno) {
case 0:
ret = SSH_FX_OK;
break;
case ENOENT:
case ENOTDIR:
case EBADF:
case ELOOP:
ret = SSH_FX_NO_SUCH_FILE;
break;
case EPERM:
case EACCES:
case EFAULT:
ret = SSH_FX_PERMISSION_DENIED;
break;
case ENAMETOOLONG:
case EINVAL:
ret = SSH_FX_BAD_MESSAGE;
break;
default:
ret = SSH_FX_FAILURE;
break;
}
return ret;
}
int
flags_from_portable(int pflags)
{
int flags = 0;
if (pflags & SSH_FXF_READ &&
pflags & SSH_FXF_WRITE) {
flags = O_RDWR;
} else if (pflags & SSH_FXF_READ) {
flags = O_RDONLY;
} else if (pflags & SSH_FXF_WRITE) {
flags = O_WRONLY;
}
if (pflags & SSH_FXF_CREAT)
flags |= O_CREAT;
if (pflags & SSH_FXF_TRUNC)
flags |= O_TRUNC;
if (pflags & SSH_FXF_EXCL)
flags |= O_EXCL;
return flags;
}
void
attrib_clear(Attrib *a)
{
a->flags = 0;
a->size_low = 0;
a->size_high = 0;
a->size = 0;
a->uid = 0;
a->gid =