aboutsummaryrefslogtreecommitdiff
path: root/libssh/sftpserver.c
blob: 9384ce22954caa5339503ecece7f521fac3c83b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * sftpserver.c - server based function for the sftp protocol
 *
 * This file is part of the SSH Library
 *
 * Copyright (c) 2005      by Aris Adamantiadis
 *
 * The SSH 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.
 *
 * The SSH 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 the SSH Library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 * vim: ts=2 sw=2 et cindent
 */

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "libssh/libssh.h"
#include "libssh/sftp.h"
#include "libssh/ssh2.h"
#include "libssh/priv.h"

SFTP_CLIENT_MESSAGE *sftp_get_client_message(SFTP_SESSION *sftp) {
  SFTP_PACKET *packet;
  SFTP_CLIENT_MESSAGE *msg;
  BUFFER *payload;
  STRING *tmp;

  msg = malloc(sizeof (SFTP_CLIENT_MESSAGE));
  if (msg == NULL) {
    return NULL;
  }
  ZERO_STRUCTP(msg);

  packet = sftp_packet_read(sftp);
  if (packet == NULL) {
    sftp_client_message_free(msg);
    return NULL;
  }

  payload = packet->payload;
  msg->type = packet->type;
  msg->sftp = sftp;

  buffer_get_u32(payload, &msg->id);

  switch(msg->type) {
    case SSH_FXP_CLOSE:
    case SSH_FXP_READDIR:
      msg->handle = buffer_get_ssh_string(payload);
      if (msg->handle == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      break;
    case SSH_FXP_READ:
      msg->handle = buffer_get_ssh_string(payload);
      if (msg->handle == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      buffer_get_u64(payload, &msg->offset);
      buffer_get_u32(payload, &msg->len);
      break;
    case SSH_FXP_WRITE:
      msg->handle = buffer_get_ssh_string(payload);
      if (msg->handle == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      buffer_get_u64(payload, &msg->offset);
      msg->data = buffer_get_ssh_string(payload);
      if (msg->data == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      break;
    case SSH_FXP_REMOVE:
    case SSH_FXP_RMDIR:
    case SSH_FXP_OPENDIR:
    case SSH_FXP_READLINK:
    case SSH_FXP_REALPATH:
      tmp = buffer_get_ssh_string(payload);
      if (tmp == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->filename = string_to_char(tmp);
      string_free(tmp);
      if (msg->filename == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      break;
    case SSH_FXP_RENAME:
    case SSH_FXP_SYMLINK:
      tmp = buffer_get_ssh_string(payload);
      if (tmp == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->filename = string_to_char(tmp);
      string_free(tmp);
      if (msg->filename == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->data = buffer_get_ssh_string(payload);
      if (msg->data == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      break;
    case SSH_FXP_MKDIR:
    case SSH_FXP_SETSTAT:
      tmp = buffer_get_ssh_string(payload);
      if (tmp == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->filename=string_to_char(tmp);
      string_free(tmp);
      if (msg->filename == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->attr = sftp_parse_attr(sftp, payload, 0);
      if (msg->attr == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      break;
    case SSH_FXP_FSETSTAT:
      msg->handle = buffer_get_ssh_string(payload);
      if (msg->handle == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->attr = sftp_parse_attr(sftp, payload, 0);
      if (msg->attr == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      break;
    case SSH_FXP_LSTAT:
    case SSH_FXP_STAT:
      tmp = buffer_get_ssh_string(payload);
      if (tmp == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->filename = string_to_char(tmp);
      string_free(tmp);
      if (msg->filename == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      if(sftp->version > 3) {
        buffer_get_u32(payload,&msg->flags);
      }
      break;
    case SSH_FXP_OPEN:
      tmp=buffer_get_ssh_string(payload);
      if (tmp == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      msg->filename = string_to_char(tmp);
      string_free(tmp);
      if (msg->filename == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      buffer_get_u32(payload,&msg->flags);
      msg->attr = sftp_parse_attr(sftp, payload, 0);
      if (msg->attr == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
    case SSH_FXP_FSTAT:
      msg->handle = buffer_get_ssh_string(payload);
      if (msg->handle == NULL) {
        sftp_client_message_free(msg);
        return NULL;
      }
      buffer_get_u32(payload, &msg->flags);
      break;
    default:
      fprintf(stderr, "Received unhandled sftp message %d\n", msg->type);
  }

  msg->flags = ntohl(msg->flags);
  msg->offset = ntohll(msg->offset);
  msg->len = ntohl(msg->len);
  sftp_packet_free(packet);

  return msg;
}

void sftp_client_message_free(SFTP_CLIENT_MESSAGE *msg) {
  if (msg == NULL) {
    return;
  }

  SAFE_FREE(msg->filename);
  string_free(msg->data);
  string_free(msg->handle);
  sftp_attributes_free(msg->attr);

  ZERO_STRUCTP(msg);
  SAFE_FREE(msg);
}

int sftp_reply_name(SFTP_CLIENT_MESSAGE *msg, const char *name,
    SFTP_ATTRIBUTES *attr) {
  BUFFER *out;
  STRING *file;

  out = buffer_new();
  if (out == NULL) {
    return -1;
  }

  file = string_from_char(name);
  if (file == NULL) {
    buffer_free(out);
    return -1;
  }

  if (buffer_add_u32(out, msg->id) < 0 ||
      buffer_add_u32(out, htonl(1)) < 0 ||
      buffer_add_ssh_string(out, file) < 0 ||
      buffer_add_ssh_string(out, file) < 0 || /* The protocol is broken here between 3 & 4 */
      buffer_add_attributes(out, attr) < 0 ||
      sftp_packet_write(msg->sftp, SSH_FXP_NAME, out) < 0) {
    buffer_free(out);
    string_free(file);
    return -1;
  }
  buffer_free(out);
  string_free(file);

  return 0;
}

int sftp_reply_handle(SFTP_CLIENT_MESSAGE *msg, STRING *handle){
  BUFFER *out;

  out = buffer_new();
  if (out == NULL) {
    return -1;
  }

  if (buffer_add_u32(out, msg->id) < 0 ||
      buffer_add_ssh_string(out, handle) < 0 ||
      sftp_packet_write(msg->sftp, SSH_FXP_HANDLE, out) < 0) {
    buffer_free(out);
    return -1;
  }
  buffer_free(out);

  return 0;
}

int sftp_reply_attr(SFTP_CLIENT_MESSAGE *msg, SFTP_ATTRIBUTES *attr) {
  BUFFER *out;

  out = buffer_new();
  if (out == NULL) {
    return -1;
  }

  if (buffer_add_u32(out, msg->id) < 0 ||
      buffer_add_attributes(out, attr) < 0 ||
      sftp_packet_write(msg->sftp, SSH_FXP_ATTRS, out) < 0) {
    buffer_free(out);
    return -1;
  }
  buffer_free(out);

  return 0;
}

int sftp_reply_names_add(SFTP_CLIENT_MESSAGE *msg, const char *file,
    const char *longname, SFTP_ATTRIBUTES *attr) {
  STRING *name;

  name = string_from_char(file);
  if (name == NULL) {
    return -1;
  }

  if (msg->attrbuf == NULL) {
    msg->attrbuf = buffer_new();
    if (msg->attrbuf == NULL) {
      string_free(name);
      return -1;
    }
  }

  if (buffer_add_ssh_string(msg->attrbuf, name) < 0) {
    string_free(name);
    return -1;
  }

  string_free(name);
  name = string_from_char(longname);
  if (name == NULL) {
    return -1;
  }
  if (buffer_add_ssh_string(msg->attrbuf,name) < 0 ||
      buffer_add_attributes(msg->attrbuf,attr) < 0) {
    string_free(name);
    return -1;
  }
  string_free(name);
  msg->attr_num++;

  return 0;
}

int sftp_reply_names(SFTP_CLIENT_MESSAGE *msg) {
  BUFFER *out;

  out = buffer_new();
  if (out == NULL) {
    buffer_free(msg->attrbuf);
    return -1;
  }

  if (buffer_add_u32(out, msg->id) < 0 ||
      buffer_add_u32(out, htonl(msg->attr_num)) < 0 ||
      buffer_add_data(out, buffer_get(msg->attrbuf),
        buffer_get_len(msg->attrbuf)) < 0 ||
      sftp_packet_write(msg->sftp, SSH_FXP_NAME, out) < 0) {
    buffer_free(out);
    buffer_free(msg->attrbuf);
    return -1;
  }

  buffer_free(out);
  buffer_free(msg->attrbuf);

  msg->attr_num = 0;
  msg->attrbuf = NULL;

  return 0;
}

int sftp_reply_status(SFTP_CLIENT_MESSAGE *msg, u32 status,
    const char *message) {
  BUFFER *out;
  STRING *s;

  out = buffer_new();
  if (out == NULL) {
    return -1;
  }

  s = string_from_char(message ? message : "");
  if (s == NULL) {
    buffer_free(out);
    return -1;
  }

  if (buffer_add_u32(out, msg->id) < 0 ||
      buffer_add_u32(out, htonl(status)) < 0 ||
      buffer_add_ssh_string(out, s) < 0 ||
      buffer_add_u32(out, 0) < 0 || /* language string */
      sftp_packet_write(msg->sftp, SSH_FXP_STATUS, out) < 0) {
    buffer_free(out);
    string_free(s);
    return -1;
  }

  buffer_free(out);
  string_free(s);

  return 0;
}

int sftp_reply_data(SFTP_CLIENT_MESSAGE *msg, const void *data, int len) {
  BUFFER *out;

  out = buffer_new();
  if (out == NULL) {
    return -1;
  }

  if (buffer_add_u32(out, msg->id) < 0 ||
      buffer_add_u32(out, ntohl(len)) < 0 ||
      buffer_add_data(out, data, len) < 0 ||
      sftp_packet_write(msg->sftp, SSH_FXP_DATA, out) < 0) {
    buffer_free(out);
    return -1;
  }
  buffer_free(out);

  return 0;
}

/*
 * This function will return you a new handle to give the client.
 * the function accepts an info that can be retrieved later with
 * the handle. Care is given that a corrupted handle won't give a
 * valid info (or worse).
 */
STRING *sftp_handle_alloc(SFTP_SESSION *sftp, void *info) {
  STRING *ret;
  u32 val;
  int i;

  if (sftp->handles == NULL) {
    sftp->handles = malloc(sizeof(void *) * SFTP_HANDLES);
    if (sftp->handles == NULL) {
      return NULL;
    }
    memset(sftp->handles, 0, sizeof(void *) * SFTP_HANDLES);
  }

  for (i = 0; i < SFTP_HANDLES; i++) {
    if (sftp->handles[i] == NULL) {
      break;
    }
  }

  if (i == SFTP_HANDLES) {
    return NULL; /* no handle available */
  }

  val = i;
  ret = string_new(4);
  if (ret == NULL) {
    return NULL;
  }

  memcpy(ret->string, &val, sizeof(u32));
  sftp->handles[i] = info;

  return ret;
}

void *sftp_handle(SFTP_SESSION *sftp, STRING *handle){
    u32 val;
    if(!sftp->handles)
        return NULL;
    if(string_len(handle)!=sizeof(val))
        return NULL;
    memcpy(&val,handle->string,sizeof(u32));
    if(val>SFTP_HANDLES)
        return NULL;
    return sftp->handles[val];
}

void sftp_handle_remove(SFTP_SESSION *sftp, void *handle){
    int i;
    for(i=0;i<SFTP_HANDLES;++i){
        if(sftp->handles[i]==handle){
            sftp->handles[i]=NULL;
            break;
        }
    }
}