sftpserver.c   sftpserver.c 
skipping to change at line 24 skipping to change at line 24
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILI TY * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILI TY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details. * License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with the SSH Library; see the file COPYING. If not, write to * along with the SSH Library; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA. * MA 02111-1307, USA.
*/ */
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#ifndef _WIN32 #ifndef _WIN32
#include <arpa/inet.h> #include <arpa/inet.h>
#endif #endif
#include "libssh/libssh.h" #include "libssh/libssh.h"
#include "libssh/sftp.h" #include "libssh/sftp.h"
#include "libssh/ssh2.h" #include "libssh/ssh2.h"
#include "libssh/priv.h" #include "libssh/priv.h"
#include "libssh/buffer.h"
#include "libssh/misc.h"
SFTP_CLIENT_MESSAGE *sftp_get_client_message(SFTP_SESSION *sftp) { sftp_client_message sftp_get_client_message(sftp_session sftp) {
SFTP_PACKET *packet; sftp_packet packet;
SFTP_CLIENT_MESSAGE *msg; sftp_client_message msg;
BUFFER *payload; ssh_buffer payload;
STRING *tmp; ssh_string tmp;
msg = malloc(sizeof (SFTP_CLIENT_MESSAGE)); msg = malloc(sizeof (struct sftp_client_message_struct));
if (msg == NULL) { if (msg == NULL) {
return NULL; return NULL;
} }
ZERO_STRUCTP(msg); ZERO_STRUCTP(msg);
packet = sftp_packet_read(sftp); packet = sftp_packet_read(sftp);
if (packet == NULL) { if (packet == NULL) {
sftp_client_message_free(msg); sftp_client_message_free(msg);
return NULL; return NULL;
} }
skipping to change at line 215 skipping to change at line 216
} }
msg->flags = ntohl(msg->flags); msg->flags = ntohl(msg->flags);
msg->offset = ntohll(msg->offset); msg->offset = ntohll(msg->offset);
msg->len = ntohl(msg->len); msg->len = ntohl(msg->len);
sftp_packet_free(packet); sftp_packet_free(packet);
return msg; return msg;
} }
void sftp_client_message_free(SFTP_CLIENT_MESSAGE *msg) { void sftp_client_message_free(sftp_client_message msg) {
if (msg == NULL) { if (msg == NULL) {
return; return;
} }
SAFE_FREE(msg->filename); SAFE_FREE(msg->filename);
string_free(msg->data); string_free(msg->data);
string_free(msg->handle); string_free(msg->handle);
sftp_attributes_free(msg->attr); sftp_attributes_free(msg->attr);
ZERO_STRUCTP(msg); ZERO_STRUCTP(msg);
SAFE_FREE(msg); SAFE_FREE(msg);
} }
int sftp_reply_name(SFTP_CLIENT_MESSAGE *msg, const char *name, int sftp_reply_name(sftp_client_message msg, const char *name,
SFTP_ATTRIBUTES *attr) { sftp_attributes attr) {
BUFFER *out; ssh_buffer out;
STRING *file; ssh_string file;
out = buffer_new(); out = buffer_new();
if (out == NULL) { if (out == NULL) {
return -1; return -1;
} }
file = string_from_char(name); file = string_from_char(name);
if (file == NULL) { if (file == NULL) {
buffer_free(out); buffer_free(out);
return -1; return -1;
skipping to change at line 261 skipping to change at line 262
buffer_free(out); buffer_free(out);
string_free(file); string_free(file);
return -1; return -1;
} }
buffer_free(out); buffer_free(out);
string_free(file); string_free(file);
return 0; return 0;
} }
int sftp_reply_handle(SFTP_CLIENT_MESSAGE *msg, STRING *handle){ int sftp_reply_handle(sftp_client_message msg, ssh_string handle){
BUFFER *out; ssh_buffer out;
out = buffer_new(); out = buffer_new();
if (out == NULL) { if (out == NULL) {
return -1; return -1;
} }
if (buffer_add_u32(out, msg->id) < 0 || if (buffer_add_u32(out, msg->id) < 0 ||
buffer_add_ssh_string(out, handle) < 0 || buffer_add_ssh_string(out, handle) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_HANDLE, out) < 0) { sftp_packet_write(msg->sftp, SSH_FXP_HANDLE, out) < 0) {
buffer_free(out); buffer_free(out);
return -1; return -1;
} }
buffer_free(out); buffer_free(out);
return 0; return 0;
} }
int sftp_reply_attr(SFTP_CLIENT_MESSAGE *msg, SFTP_ATTRIBUTES *attr) { int sftp_reply_attr(sftp_client_message msg, sftp_attributes attr) {
BUFFER *out; ssh_buffer out;
out = buffer_new(); out = buffer_new();
if (out == NULL) { if (out == NULL) {
return -1; return -1;
} }
if (buffer_add_u32(out, msg->id) < 0 || if (buffer_add_u32(out, msg->id) < 0 ||
buffer_add_attributes(out, attr) < 0 || buffer_add_attributes(out, attr) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_ATTRS, out) < 0) { sftp_packet_write(msg->sftp, SSH_FXP_ATTRS, out) < 0) {
buffer_free(out); buffer_free(out);
return -1; return -1;
} }
buffer_free(out); buffer_free(out);
return 0; return 0;
} }
int sftp_reply_names_add(SFTP_CLIENT_MESSAGE *msg, const char *file, int sftp_reply_names_add(sftp_client_message msg, const char *file,
const char *longname, SFTP_ATTRIBUTES *attr) { const char *longname, sftp_attributes attr) {
STRING *name; ssh_string name;
name = string_from_char(file); name = string_from_char(file);
if (name == NULL) { if (name == NULL) {
return -1; return -1;
} }
if (msg->attrbuf == NULL) { if (msg->attrbuf == NULL) {
msg->attrbuf = buffer_new(); msg->attrbuf = buffer_new();
if (msg->attrbuf == NULL) { if (msg->attrbuf == NULL) {
string_free(name); string_free(name);
skipping to change at line 337 skipping to change at line 338
buffer_add_attributes(msg->attrbuf,attr) < 0) { buffer_add_attributes(msg->attrbuf,attr) < 0) {
string_free(name); string_free(name);
return -1; return -1;
} }
string_free(name); string_free(name);
msg->attr_num++; msg->attr_num++;
return 0; return 0;
} }
int sftp_reply_names(SFTP_CLIENT_MESSAGE *msg) { int sftp_reply_names(sftp_client_message msg) {
BUFFER *out; ssh_buffer out;
out = buffer_new(); out = buffer_new();
if (out == NULL) { if (out == NULL) {
buffer_free(msg->attrbuf); buffer_free(msg->attrbuf);
return -1; return -1;
} }
if (buffer_add_u32(out, msg->id) < 0 || if (buffer_add_u32(out, msg->id) < 0 ||
buffer_add_u32(out, htonl(msg->attr_num)) < 0 || buffer_add_u32(out, htonl(msg->attr_num)) < 0 ||
buffer_add_data(out, buffer_get(msg->attrbuf), buffer_add_data(out, buffer_get(msg->attrbuf),
skipping to change at line 365 skipping to change at line 366
buffer_free(out); buffer_free(out);
buffer_free(msg->attrbuf); buffer_free(msg->attrbuf);
msg->attr_num = 0; msg->attr_num = 0;
msg->attrbuf = NULL; msg->attrbuf = NULL;
return 0; return 0;
} }
int sftp_reply_status(SFTP_CLIENT_MESSAGE *msg, u32 status, int sftp_reply_status(sftp_client_message msg, uint32_t status,
const char *message) { const char *message) {
BUFFER *out; ssh_buffer out;
STRING *s; ssh_string s;
out = buffer_new(); out = buffer_new();
if (out == NULL) { if (out == NULL) {
return -1; return -1;
} }
s = string_from_char(message ? message : ""); s = string_from_char(message ? message : "");
if (s == NULL) { if (s == NULL) {
buffer_free(out); buffer_free(out);
return -1; return -1;
skipping to change at line 397 skipping to change at line 398
string_free(s); string_free(s);
return -1; return -1;
} }
buffer_free(out); buffer_free(out);
string_free(s); string_free(s);
return 0; return 0;
} }
int sftp_reply_data(SFTP_CLIENT_MESSAGE *msg, const void *data, int len) { int sftp_reply_data(sftp_client_message msg, const void *data, int len) {
BUFFER *out; ssh_buffer out;
out = buffer_new(); out = buffer_new();
if (out == NULL) { if (out == NULL) {
return -1; return -1;
} }
if (buffer_add_u32(out, msg->id) < 0 || if (buffer_add_u32(out, msg->id) < 0 ||
buffer_add_u32(out, ntohl(len)) < 0 || buffer_add_u32(out, ntohl(len)) < 0 ||
buffer_add_data(out, data, len) < 0 || buffer_add_data(out, data, len) < 0 ||
sftp_packet_write(msg->sftp, SSH_FXP_DATA, out) < 0) { sftp_packet_write(msg->sftp, SSH_FXP_DATA, out) < 0) {
skipping to change at line 423 skipping to change at line 424
return 0; return 0;
} }
/* /*
* This function will return you a new handle to give the client. * This function will return you a new handle to give the client.
* the function accepts an info that can be retrieved later with * the function accepts an info that can be retrieved later with
* the handle. Care is given that a corrupted handle won't give a * the handle. Care is given that a corrupted handle won't give a
* valid info (or worse). * valid info (or worse).
*/ */
STRING *sftp_handle_alloc(SFTP_SESSION *sftp, void *info) { ssh_string sftp_handle_alloc(sftp_session sftp, void *info) {
STRING *ret; ssh_string ret;
u32 val; uint32_t val;
int i; int i;
if (sftp->handles == NULL) { if (sftp->handles == NULL) {
sftp->handles = malloc(sizeof(void *) * SFTP_HANDLES); sftp->handles = malloc(sizeof(void *) * SFTP_HANDLES);
if (sftp->handles == NULL) { if (sftp->handles == NULL) {
return NULL; return NULL;
} }
memset(sftp->handles, 0, sizeof(void *) * SFTP_HANDLES); memset(sftp->handles, 0, sizeof(void *) * SFTP_HANDLES);
} }
skipping to change at line 452 skipping to change at line 453
if (i == SFTP_HANDLES) { if (i == SFTP_HANDLES) {
return NULL; /* no handle available */ return NULL; /* no handle available */
} }
val = i; val = i;
ret = string_new(4); ret = string_new(4);
if (ret == NULL) { if (ret == NULL) {
return NULL; return NULL;
} }
memcpy(ret->string, &val, sizeof(u32)); memcpy(string_data(ret), &val, sizeof(uint32_t));
sftp->handles[i] = info; sftp->handles[i] = info;
return ret; return ret;
} }
void *sftp_handle(SFTP_SESSION *sftp, STRING *handle){ void *sftp_handle(sftp_session sftp, ssh_string handle){
u32 val; uint32_t val;
if (sftp->handles == NULL) { if (sftp->handles == NULL) {
return NULL; return NULL;
} }
if (string_len(handle) != sizeof(u32)) { if (string_len(handle) != sizeof(uint32_t)) {
return NULL; return NULL;
} }
memcpy(&val, handle->string, sizeof(u32)); memcpy(&val, string_data(handle), sizeof(uint32_t));
if (val > SFTP_HANDLES) { if (val > SFTP_HANDLES) {
return NULL; return NULL;
} }
return sftp->handles[val]; return sftp->handles[val];
} }
void sftp_handle_remove(SFTP_SESSION *sftp, void *handle) { void sftp_handle_remove(sftp_session sftp, void *handle) {
int i; int i;
for (i = 0; i < SFTP_HANDLES; i++) { for (i = 0; i < SFTP_HANDLES; i++) {
if (sftp->handles[i] == handle) { if (sftp->handles[i] == handle) {
sftp->handles[i] = NULL; sftp->handles[i] = NULL;
break; break;
} }
} }
} }
 End of changes. 19 change blocks. 
35 lines changed or deleted 36 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/