blob: 0eeb73c8baa602151535a9bdd34980a40a5e5d35 [file] [log] [blame]
Damien Millerc7dba122013-08-21 02:41:15 +10001/* $OpenBSD: sftp-client.c,v 1.102 2013/08/08 05:04:03 djm Exp $ */
Damien Miller33804262001-02-04 23:20:18 +11002/*
Damien Miller4e60ed72004-02-17 17:07:59 +11003 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
Damien Miller33804262001-02-04 23:20:18 +11004 *
Damien Miller4e60ed72004-02-17 17:07:59 +11005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller33804262001-02-04 23:20:18 +11008 *
Damien Miller4e60ed72004-02-17 17:07:59 +11009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller33804262001-02-04 23:20:18 +110016 */
17
18/* XXX: memleaks */
19/* XXX: signed vs unsigned */
Damien Miller3db5f532002-02-13 14:10:32 +110020/* XXX: remove all logging, only return status codes */
Damien Miller33804262001-02-04 23:20:18 +110021/* XXX: copy between two remote sites */
22
23#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110024
25#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100026#include <sys/param.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100027#ifdef HAVE_SYS_STATVFS_H
Damien Millerd671e5a2008-05-19 14:53:33 +100028#include <sys/statvfs.h>
Darren Tucker5b2e2ba2008-06-08 09:25:28 +100029#endif
Damien Millerd7834352006-08-05 12:39:39 +100030#include "openbsd-compat/sys-queue.h"
Damien Millerf17883e2006-03-15 11:45:54 +110031#ifdef HAVE_SYS_STAT_H
32# include <sys/stat.h>
33#endif
Damien Miller9aec9192006-08-05 10:57:45 +100034#ifdef HAVE_SYS_TIME_H
35# include <sys/time.h>
36#endif
Damien Millerd7834352006-08-05 12:39:39 +100037#include <sys/uio.h>
Damien Miller57cf6382006-07-10 21:13:46 +100038
Darren Tucker1b0dd172009-10-07 08:37:48 +110039#include <dirent.h>
Darren Tucker39972492006-07-12 22:22:46 +100040#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100041#include <fcntl.h>
42#include <signal.h>
Damien Millerd7834352006-08-05 12:39:39 +100043#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100044#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100046#include <unistd.h>
Damien Miller16a13332002-02-13 14:03:56 +110047
Damien Miller33804262001-02-04 23:20:18 +110048#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100049#include "buffer.h"
Damien Miller33804262001-02-04 23:20:18 +110050#include "log.h"
51#include "atomicio.h"
Damien Miller62d57f62003-01-10 21:43:24 +110052#include "progressmeter.h"
Damien Miller3f941882006-03-31 23:13:02 +110053#include "misc.h"
Damien Miller33804262001-02-04 23:20:18 +110054
55#include "sftp.h"
56#include "sftp-common.h"
57#include "sftp-client.h"
58
Darren Tuckercdf547a2004-05-24 10:12:19 +100059extern volatile sig_atomic_t interrupted;
Damien Miller62d57f62003-01-10 21:43:24 +110060extern int showprogress;
61
Damien Miller0c8d8f62006-03-15 11:34:25 +110062/* Minimum amount of data to read at a time */
Damien Miller16a13332002-02-13 14:03:56 +110063#define MIN_READ_SIZE 512
64
Darren Tucker1b0dd172009-10-07 08:37:48 +110065/* Maximum depth to descend in directory trees */
66#define MAX_DIR_DEPTH 64
67
Damien Miller3db5f532002-02-13 14:10:32 +110068struct sftp_conn {
69 int fd_in;
70 int fd_out;
71 u_int transfer_buflen;
72 u_int num_requests;
73 u_int version;
74 u_int msg_id;
Damien Millerd671e5a2008-05-19 14:53:33 +100075#define SFTP_EXT_POSIX_RENAME 0x00000001
76#define SFTP_EXT_STATVFS 0x00000002
77#define SFTP_EXT_FSTATVFS 0x00000004
Darren Tuckeraf1f9092010-12-05 09:02:47 +110078#define SFTP_EXT_HARDLINK 0x00000008
Damien Miller7a3e1d02008-03-27 10:59:57 +110079 u_int exts;
Damien Miller65e42f82010-09-24 22:15:11 +100080 u_int64_t limit_kbps;
81 struct bwlimit bwlimit_in, bwlimit_out;
Damien Miller3db5f532002-02-13 14:10:32 +110082};
Ben Lindstrom288cc392001-02-09 02:58:04 +000083
Darren Tuckerc22f0902009-10-07 08:24:19 +110084static char *
Damien Miller65e42f82010-09-24 22:15:11 +100085get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
86 const char *errfmt, ...) __attribute__((format(printf, 4, 5)));
87
88/* ARGSUSED */
89static int
90sftpio(void *_bwlimit, size_t amount)
91{
92 struct bwlimit *bwlimit = (struct bwlimit *)_bwlimit;
93
94 bandwidth_limit(bwlimit, amount);
95 return 0;
96}
Darren Tuckerc22f0902009-10-07 08:24:19 +110097
Ben Lindstrombba81212001-06-25 05:01:22 +000098static void
Damien Miller65e42f82010-09-24 22:15:11 +100099send_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +1100100{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100101 u_char mlen[4];
Damien Miller58ca98b2006-04-23 12:06:35 +1000102 struct iovec iov[2];
Damien Miller33804262001-02-04 23:20:18 +1100103
Damien Miller54446182006-01-02 23:40:50 +1100104 if (buffer_len(m) > SFTP_MAX_MSG_LENGTH)
Damien Millera7f3aaa2003-01-10 21:43:58 +1100105 fatal("Outbound message too long %u", buffer_len(m));
Damien Miller33804262001-02-04 23:20:18 +1100106
Damien Millera7f3aaa2003-01-10 21:43:58 +1100107 /* Send length first */
Damien Miller3f941882006-03-31 23:13:02 +1100108 put_u32(mlen, buffer_len(m));
Damien Miller58ca98b2006-04-23 12:06:35 +1000109 iov[0].iov_base = mlen;
110 iov[0].iov_len = sizeof(mlen);
111 iov[1].iov_base = buffer_ptr(m);
112 iov[1].iov_len = buffer_len(m);
Damien Millerd7834352006-08-05 12:39:39 +1000113
Damien Miller65e42f82010-09-24 22:15:11 +1000114 if (atomiciov6(writev, conn->fd_out, iov, 2,
Damien Miller0d032412013-07-25 11:56:52 +1000115 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_out) !=
Damien Miller65e42f82010-09-24 22:15:11 +1000116 buffer_len(m) + sizeof(mlen))
Damien Millera7f3aaa2003-01-10 21:43:58 +1100117 fatal("Couldn't send packet: %s", strerror(errno));
118
119 buffer_clear(m);
Damien Miller33804262001-02-04 23:20:18 +1100120}
121
Ben Lindstrombba81212001-06-25 05:01:22 +0000122static void
Damien Miller65e42f82010-09-24 22:15:11 +1000123get_msg(struct sftp_conn *conn, Buffer *m)
Damien Miller33804262001-02-04 23:20:18 +1100124{
Damien Millera7f3aaa2003-01-10 21:43:58 +1100125 u_int msg_len;
Damien Miller33804262001-02-04 23:20:18 +1100126
Damien Millera7f3aaa2003-01-10 21:43:58 +1100127 buffer_append_space(m, 4);
Damien Miller65e42f82010-09-24 22:15:11 +1000128 if (atomicio6(read, conn->fd_in, buffer_ptr(m), 4,
129 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in) != 4) {
Damien Millerb253cc42005-05-26 12:23:44 +1000130 if (errno == EPIPE)
131 fatal("Connection closed");
132 else
133 fatal("Couldn't read packet: %s", strerror(errno));
134 }
Damien Miller33804262001-02-04 23:20:18 +1100135
Damien Millera7f3aaa2003-01-10 21:43:58 +1100136 msg_len = buffer_get_int(m);
Damien Miller54446182006-01-02 23:40:50 +1100137 if (msg_len > SFTP_MAX_MSG_LENGTH)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000138 fatal("Received message too long %u", msg_len);
Damien Miller33804262001-02-04 23:20:18 +1100139
Damien Millera7f3aaa2003-01-10 21:43:58 +1100140 buffer_append_space(m, msg_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000141 if (atomicio6(read, conn->fd_in, buffer_ptr(m), msg_len,
142 conn->limit_kbps > 0 ? sftpio : NULL, &conn->bwlimit_in)
143 != msg_len) {
Damien Millerb253cc42005-05-26 12:23:44 +1000144 if (errno == EPIPE)
145 fatal("Connection closed");
146 else
147 fatal("Read packet: %s", strerror(errno));
148 }
Damien Miller33804262001-02-04 23:20:18 +1100149}
150
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Miller65e42f82010-09-24 22:15:11 +1000152send_string_request(struct sftp_conn *conn, u_int id, u_int code, char *s,
Damien Miller33804262001-02-04 23:20:18 +1100153 u_int len)
154{
155 Buffer msg;
156
157 buffer_init(&msg);
158 buffer_put_char(&msg, code);
159 buffer_put_int(&msg, id);
160 buffer_put_string(&msg, s, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000161 send_msg(conn, &msg);
162 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100163 buffer_free(&msg);
164}
165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void
Damien Miller65e42f82010-09-24 22:15:11 +1000167send_string_attrs_request(struct sftp_conn *conn, u_int id, u_int code,
168 char *s, u_int len, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100169{
170 Buffer msg;
171
172 buffer_init(&msg);
173 buffer_put_char(&msg, code);
174 buffer_put_int(&msg, id);
175 buffer_put_string(&msg, s, len);
176 encode_attrib(&msg, a);
Damien Miller65e42f82010-09-24 22:15:11 +1000177 send_msg(conn, &msg);
178 debug3("Sent message fd %d T:%u I:%u", conn->fd_out, code, id);
Damien Miller33804262001-02-04 23:20:18 +1100179 buffer_free(&msg);
180}
181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static u_int
Damien Miller65e42f82010-09-24 22:15:11 +1000183get_status(struct sftp_conn *conn, u_int expected_id)
Damien Miller33804262001-02-04 23:20:18 +1100184{
185 Buffer msg;
186 u_int type, id, status;
187
188 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000189 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100190 type = buffer_get_char(&msg);
191 id = buffer_get_int(&msg);
192
193 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000194 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100195 if (type != SSH2_FXP_STATUS)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000196 fatal("Expected SSH2_FXP_STATUS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100197 SSH2_FXP_STATUS, type);
198
199 status = buffer_get_int(&msg);
200 buffer_free(&msg);
201
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000202 debug3("SSH2_FXP_STATUS %u", status);
Damien Miller33804262001-02-04 23:20:18 +1100203
Damien Miller65e42f82010-09-24 22:15:11 +1000204 return status;
Damien Miller33804262001-02-04 23:20:18 +1100205}
206
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static char *
Damien Miller65e42f82010-09-24 22:15:11 +1000208get_handle(struct sftp_conn *conn, u_int expected_id, u_int *len,
209 const char *errfmt, ...)
Damien Miller33804262001-02-04 23:20:18 +1100210{
211 Buffer msg;
212 u_int type, id;
Darren Tuckerc22f0902009-10-07 08:24:19 +1100213 char *handle, errmsg[256];
214 va_list args;
215 int status;
216
217 va_start(args, errfmt);
218 if (errfmt != NULL)
219 vsnprintf(errmsg, sizeof(errmsg), errfmt, args);
220 va_end(args);
Damien Miller33804262001-02-04 23:20:18 +1100221
222 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000223 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100224 type = buffer_get_char(&msg);
225 id = buffer_get_int(&msg);
226
227 if (id != expected_id)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100228 fatal("%s: ID mismatch (%u != %u)",
229 errfmt == NULL ? __func__ : errmsg, id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100230 if (type == SSH2_FXP_STATUS) {
Darren Tuckerc22f0902009-10-07 08:24:19 +1100231 status = buffer_get_int(&msg);
232 if (errfmt != NULL)
233 error("%s: %s", errmsg, fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100234 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100235 return(NULL);
236 } else if (type != SSH2_FXP_HANDLE)
Darren Tuckerc22f0902009-10-07 08:24:19 +1100237 fatal("%s: Expected SSH2_FXP_HANDLE(%u) packet, got %u",
238 errfmt == NULL ? __func__ : errmsg, SSH2_FXP_HANDLE, type);
Damien Miller33804262001-02-04 23:20:18 +1100239
240 handle = buffer_get_string(&msg, len);
241 buffer_free(&msg);
242
243 return(handle);
244}
245
Ben Lindstrombba81212001-06-25 05:01:22 +0000246static Attrib *
Damien Miller65e42f82010-09-24 22:15:11 +1000247get_decode_stat(struct sftp_conn *conn, u_int expected_id, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100248{
249 Buffer msg;
250 u_int type, id;
251 Attrib *a;
252
253 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000254 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100255
256 type = buffer_get_char(&msg);
257 id = buffer_get_int(&msg);
258
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000259 debug3("Received stat reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100260 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000261 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100262 if (type == SSH2_FXP_STATUS) {
263 int status = buffer_get_int(&msg);
264
Ben Lindstromc8d1c302001-03-17 00:34:46 +0000265 if (quiet)
266 debug("Couldn't stat remote file: %s", fx2txt(status));
267 else
268 error("Couldn't stat remote file: %s", fx2txt(status));
Darren Tuckercd516ef2004-12-06 22:43:43 +1100269 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100270 return(NULL);
271 } else if (type != SSH2_FXP_ATTRS) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000272 fatal("Expected SSH2_FXP_ATTRS(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100273 SSH2_FXP_ATTRS, type);
274 }
275 a = decode_attrib(&msg);
276 buffer_free(&msg);
277
278 return(a);
279}
280
Damien Millerd671e5a2008-05-19 14:53:33 +1000281static int
Damien Miller65e42f82010-09-24 22:15:11 +1000282get_decode_statvfs(struct sftp_conn *conn, struct sftp_statvfs *st,
283 u_int expected_id, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000284{
285 Buffer msg;
286 u_int type, id, flag;
287
288 buffer_init(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000289 get_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000290
291 type = buffer_get_char(&msg);
292 id = buffer_get_int(&msg);
293
294 debug3("Received statvfs reply T:%u I:%u", type, id);
295 if (id != expected_id)
296 fatal("ID mismatch (%u != %u)", id, expected_id);
297 if (type == SSH2_FXP_STATUS) {
298 int status = buffer_get_int(&msg);
299
300 if (quiet)
301 debug("Couldn't statvfs: %s", fx2txt(status));
302 else
303 error("Couldn't statvfs: %s", fx2txt(status));
304 buffer_free(&msg);
305 return -1;
306 } else if (type != SSH2_FXP_EXTENDED_REPLY) {
307 fatal("Expected SSH2_FXP_EXTENDED_REPLY(%u) packet, got %u",
308 SSH2_FXP_EXTENDED_REPLY, type);
309 }
310
311 bzero(st, sizeof(*st));
Darren Tucker7b598892008-06-09 22:49:36 +1000312 st->f_bsize = buffer_get_int64(&msg);
313 st->f_frsize = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000314 st->f_blocks = buffer_get_int64(&msg);
315 st->f_bfree = buffer_get_int64(&msg);
316 st->f_bavail = buffer_get_int64(&msg);
317 st->f_files = buffer_get_int64(&msg);
318 st->f_ffree = buffer_get_int64(&msg);
319 st->f_favail = buffer_get_int64(&msg);
Darren Tucker294b8412008-06-08 12:57:08 +1000320 st->f_fsid = buffer_get_int64(&msg);
Darren Tucker7b598892008-06-09 22:49:36 +1000321 flag = buffer_get_int64(&msg);
322 st->f_namemax = buffer_get_int64(&msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000323
324 st->f_flag = (flag & SSH2_FXE_STATVFS_ST_RDONLY) ? ST_RDONLY : 0;
325 st->f_flag |= (flag & SSH2_FXE_STATVFS_ST_NOSUID) ? ST_NOSUID : 0;
326
327 buffer_free(&msg);
328
329 return 0;
330}
331
Damien Miller3db5f532002-02-13 14:10:32 +1100332struct sftp_conn *
Damien Miller65e42f82010-09-24 22:15:11 +1000333do_init(int fd_in, int fd_out, u_int transfer_buflen, u_int num_requests,
334 u_int64_t limit_kbps)
Damien Miller33804262001-02-04 23:20:18 +1100335{
Damien Miller65e42f82010-09-24 22:15:11 +1000336 u_int type;
Damien Miller33804262001-02-04 23:20:18 +1100337 Buffer msg;
Damien Miller3db5f532002-02-13 14:10:32 +1100338 struct sftp_conn *ret;
Damien Miller33804262001-02-04 23:20:18 +1100339
Damien Miller65e42f82010-09-24 22:15:11 +1000340 ret = xmalloc(sizeof(*ret));
341 ret->fd_in = fd_in;
342 ret->fd_out = fd_out;
343 ret->transfer_buflen = transfer_buflen;
344 ret->num_requests = num_requests;
345 ret->exts = 0;
346 ret->limit_kbps = 0;
347
Damien Miller33804262001-02-04 23:20:18 +1100348 buffer_init(&msg);
349 buffer_put_char(&msg, SSH2_FXP_INIT);
350 buffer_put_int(&msg, SSH2_FILEXFER_VERSION);
Damien Miller65e42f82010-09-24 22:15:11 +1000351 send_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100352
353 buffer_clear(&msg);
354
Damien Miller65e42f82010-09-24 22:15:11 +1000355 get_msg(ret, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100356
Kevin Stevesef4eea92001-02-05 12:42:17 +0000357 /* Expecting a VERSION reply */
Damien Miller33804262001-02-04 23:20:18 +1100358 if ((type = buffer_get_char(&msg)) != SSH2_FXP_VERSION) {
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000359 error("Invalid packet back from SSH2_FXP_INIT (type %u)",
Damien Miller33804262001-02-04 23:20:18 +1100360 type);
361 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100362 return(NULL);
Damien Miller33804262001-02-04 23:20:18 +1100363 }
Damien Miller65e42f82010-09-24 22:15:11 +1000364 ret->version = buffer_get_int(&msg);
Damien Miller33804262001-02-04 23:20:18 +1100365
Damien Miller65e42f82010-09-24 22:15:11 +1000366 debug2("Remote version: %u", ret->version);
Damien Miller33804262001-02-04 23:20:18 +1100367
368 /* Check for extensions */
369 while (buffer_len(&msg) > 0) {
370 char *name = buffer_get_string(&msg, NULL);
371 char *value = buffer_get_string(&msg, NULL);
Darren Tuckera64ab332008-06-13 07:01:29 +1000372 int known = 0;
Damien Miller33804262001-02-04 23:20:18 +1100373
Damien Millerd671e5a2008-05-19 14:53:33 +1000374 if (strcmp(name, "posix-rename@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000375 strcmp(value, "1") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000376 ret->exts |= SFTP_EXT_POSIX_RENAME;
Darren Tuckera64ab332008-06-13 07:01:29 +1000377 known = 1;
378 } else if (strcmp(name, "statvfs@openssh.com") == 0 &&
379 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000380 ret->exts |= SFTP_EXT_STATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000381 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100382 } else if (strcmp(name, "fstatvfs@openssh.com") == 0 &&
Darren Tuckera64ab332008-06-13 07:01:29 +1000383 strcmp(value, "2") == 0) {
Damien Miller65e42f82010-09-24 22:15:11 +1000384 ret->exts |= SFTP_EXT_FSTATVFS;
Darren Tuckera64ab332008-06-13 07:01:29 +1000385 known = 1;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100386 } else if (strcmp(name, "hardlink@openssh.com") == 0 &&
387 strcmp(value, "1") == 0) {
388 ret->exts |= SFTP_EXT_HARDLINK;
389 known = 1;
Darren Tuckera64ab332008-06-13 07:01:29 +1000390 }
391 if (known) {
392 debug2("Server supports extension \"%s\" revision %s",
393 name, value);
394 } else {
395 debug2("Unrecognised server extension \"%s\"", name);
396 }
Darren Tuckera627d422013-06-02 07:31:17 +1000397 free(name);
398 free(value);
Damien Miller33804262001-02-04 23:20:18 +1100399 }
400
401 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100402
Damien Miller3db5f532002-02-13 14:10:32 +1100403 /* Some filexfer v.0 servers don't support large packets */
Damien Miller65e42f82010-09-24 22:15:11 +1000404 if (ret->version == 0)
Ben Lindstroma1d81142002-04-02 20:58:11 +0000405 ret->transfer_buflen = MIN(ret->transfer_buflen, 20480);
Damien Miller3db5f532002-02-13 14:10:32 +1100406
Damien Miller65e42f82010-09-24 22:15:11 +1000407 ret->limit_kbps = limit_kbps;
408 if (ret->limit_kbps > 0) {
409 bandwidth_limit_init(&ret->bwlimit_in, ret->limit_kbps,
410 ret->transfer_buflen);
411 bandwidth_limit_init(&ret->bwlimit_out, ret->limit_kbps,
412 ret->transfer_buflen);
413 }
414
415 return ret;
Damien Miller3db5f532002-02-13 14:10:32 +1100416}
417
418u_int
419sftp_proto_version(struct sftp_conn *conn)
420{
Damien Miller65e42f82010-09-24 22:15:11 +1000421 return conn->version;
Damien Miller33804262001-02-04 23:20:18 +1100422}
423
424int
Damien Miller3db5f532002-02-13 14:10:32 +1100425do_close(struct sftp_conn *conn, char *handle, u_int handle_len)
Damien Miller33804262001-02-04 23:20:18 +1100426{
427 u_int id, status;
428 Buffer msg;
429
430 buffer_init(&msg);
431
Damien Miller3db5f532002-02-13 14:10:32 +1100432 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100433 buffer_put_char(&msg, SSH2_FXP_CLOSE);
434 buffer_put_int(&msg, id);
435 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000436 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000437 debug3("Sent message SSH2_FXP_CLOSE I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100438
Damien Miller65e42f82010-09-24 22:15:11 +1000439 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100440 if (status != SSH2_FX_OK)
441 error("Couldn't close file: %s", fx2txt(status));
442
443 buffer_free(&msg);
444
Damien Miller65e42f82010-09-24 22:15:11 +1000445 return status;
Damien Miller33804262001-02-04 23:20:18 +1100446}
447
Damien Miller4870afd2001-03-14 10:27:09 +1100448
Ben Lindstrombba81212001-06-25 05:01:22 +0000449static int
Damien Miller3db5f532002-02-13 14:10:32 +1100450do_lsreaddir(struct sftp_conn *conn, char *path, int printflag,
Damien Miller4870afd2001-03-14 10:27:09 +1100451 SFTP_DIRENT ***dir)
Damien Miller33804262001-02-04 23:20:18 +1100452{
453 Buffer msg;
Damien Millereccb9de2005-06-17 12:59:34 +1000454 u_int count, type, id, handle_len, i, expected_id, ents = 0;
Damien Miller33804262001-02-04 23:20:18 +1100455 char *handle;
456
Damien Miller3db5f532002-02-13 14:10:32 +1100457 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100458
459 buffer_init(&msg);
460 buffer_put_char(&msg, SSH2_FXP_OPENDIR);
461 buffer_put_int(&msg, id);
462 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000463 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100464
Damien Miller65e42f82010-09-24 22:15:11 +1000465 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +1100466 "remote readdir(\"%s\")", path);
Damien Miller57c38ac2011-09-22 21:42:45 +1000467 if (handle == NULL) {
468 buffer_free(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +1000469 return -1;
Damien Miller57c38ac2011-09-22 21:42:45 +1000470 }
Damien Miller33804262001-02-04 23:20:18 +1100471
Damien Miller4870afd2001-03-14 10:27:09 +1100472 if (dir) {
473 ents = 0;
474 *dir = xmalloc(sizeof(**dir));
475 (*dir)[0] = NULL;
476 }
Damien Miller4870afd2001-03-14 10:27:09 +1100477
Darren Tuckercdf547a2004-05-24 10:12:19 +1000478 for (; !interrupted;) {
Damien Miller3db5f532002-02-13 14:10:32 +1100479 id = expected_id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +1100480
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000481 debug3("Sending SSH2_FXP_READDIR I:%u", id);
Damien Miller33804262001-02-04 23:20:18 +1100482
483 buffer_clear(&msg);
484 buffer_put_char(&msg, SSH2_FXP_READDIR);
485 buffer_put_int(&msg, id);
486 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000487 send_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100488
489 buffer_clear(&msg);
490
Damien Miller65e42f82010-09-24 22:15:11 +1000491 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100492
493 type = buffer_get_char(&msg);
494 id = buffer_get_int(&msg);
495
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000496 debug3("Received reply T:%u I:%u", type, id);
Damien Miller33804262001-02-04 23:20:18 +1100497
498 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000499 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100500
501 if (type == SSH2_FXP_STATUS) {
502 int status = buffer_get_int(&msg);
503
504 debug3("Received SSH2_FXP_STATUS %d", status);
505
506 if (status == SSH2_FX_EOF) {
507 break;
508 } else {
509 error("Couldn't read directory: %s",
510 fx2txt(status));
Damien Miller3db5f532002-02-13 14:10:32 +1100511 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000512 free(handle);
Damien Miller57c38ac2011-09-22 21:42:45 +1000513 buffer_free(&msg);
Ben Lindstrom10ac33f2001-02-10 21:53:40 +0000514 return(status);
Damien Miller33804262001-02-04 23:20:18 +1100515 }
516 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000517 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100518 SSH2_FXP_NAME, type);
519
520 count = buffer_get_int(&msg);
Damien Millerd7686fd2001-02-10 00:40:03 +1100521 if (count == 0)
522 break;
523 debug3("Received %d SSH2_FXP_NAME responses", count);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100524 for (i = 0; i < count; i++) {
Damien Miller33804262001-02-04 23:20:18 +1100525 char *filename, *longname;
526 Attrib *a;
527
528 filename = buffer_get_string(&msg, NULL);
529 longname = buffer_get_string(&msg, NULL);
530 a = decode_attrib(&msg);
531
Damien Miller4870afd2001-03-14 10:27:09 +1100532 if (printflag)
533 printf("%s\n", longname);
534
Darren Tucker1b0dd172009-10-07 08:37:48 +1100535 /*
536 * Directory entries should never contain '/'
537 * These can be used to attack recursive ops
538 * (e.g. send '../../../../etc/passwd')
539 */
540 if (strchr(filename, '/') != NULL) {
541 error("Server sent suspect path \"%s\" "
542 "during readdir of \"%s\"", filename, path);
543 goto next;
544 }
545
Damien Miller4870afd2001-03-14 10:27:09 +1100546 if (dir) {
Damien Miller36812092006-03-26 14:22:47 +1100547 *dir = xrealloc(*dir, ents + 2, sizeof(**dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100548 (*dir)[ents] = xmalloc(sizeof(***dir));
549 (*dir)[ents]->filename = xstrdup(filename);
550 (*dir)[ents]->longname = xstrdup(longname);
551 memcpy(&(*dir)[ents]->a, a, sizeof(*a));
552 (*dir)[++ents] = NULL;
553 }
Darren Tucker1b0dd172009-10-07 08:37:48 +1100554 next:
Darren Tuckera627d422013-06-02 07:31:17 +1000555 free(filename);
556 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100557 }
558 }
559
560 buffer_free(&msg);
Damien Miller3db5f532002-02-13 14:10:32 +1100561 do_close(conn, handle, handle_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000562 free(handle);
Damien Miller33804262001-02-04 23:20:18 +1100563
Darren Tuckercdf547a2004-05-24 10:12:19 +1000564 /* Don't return partial matches on interrupt */
565 if (interrupted && dir != NULL && *dir != NULL) {
566 free_sftp_dirents(*dir);
567 *dir = xmalloc(sizeof(**dir));
568 **dir = NULL;
569 }
570
Damien Miller65e42f82010-09-24 22:15:11 +1000571 return 0;
Damien Miller33804262001-02-04 23:20:18 +1100572}
573
574int
Damien Miller3db5f532002-02-13 14:10:32 +1100575do_readdir(struct sftp_conn *conn, char *path, SFTP_DIRENT ***dir)
Damien Miller4870afd2001-03-14 10:27:09 +1100576{
Damien Miller3db5f532002-02-13 14:10:32 +1100577 return(do_lsreaddir(conn, path, 0, dir));
Damien Miller4870afd2001-03-14 10:27:09 +1100578}
579
580void free_sftp_dirents(SFTP_DIRENT **s)
581{
582 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100583
584 for (i = 0; s[i]; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000585 free(s[i]->filename);
586 free(s[i]->longname);
587 free(s[i]);
Damien Miller4870afd2001-03-14 10:27:09 +1100588 }
Darren Tuckera627d422013-06-02 07:31:17 +1000589 free(s);
Damien Miller4870afd2001-03-14 10:27:09 +1100590}
591
592int
Damien Miller3db5f532002-02-13 14:10:32 +1100593do_rm(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100594{
595 u_int status, id;
596
597 debug2("Sending SSH2_FXP_REMOVE \"%s\"", path);
598
Damien Miller3db5f532002-02-13 14:10:32 +1100599 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000600 send_string_request(conn, id, SSH2_FXP_REMOVE, path, strlen(path));
601 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100602 if (status != SSH2_FX_OK)
603 error("Couldn't delete file: %s", fx2txt(status));
604 return(status);
605}
606
607int
Darren Tucker1b0dd172009-10-07 08:37:48 +1100608do_mkdir(struct sftp_conn *conn, char *path, Attrib *a, int printflag)
Damien Miller33804262001-02-04 23:20:18 +1100609{
610 u_int status, id;
611
Damien Miller3db5f532002-02-13 14:10:32 +1100612 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000613 send_string_attrs_request(conn, id, SSH2_FXP_MKDIR, path,
Damien Miller33804262001-02-04 23:20:18 +1100614 strlen(path), a);
615
Damien Miller65e42f82010-09-24 22:15:11 +1000616 status = get_status(conn, id);
Darren Tucker1b0dd172009-10-07 08:37:48 +1100617 if (status != SSH2_FX_OK && printflag)
Damien Miller33804262001-02-04 23:20:18 +1100618 error("Couldn't create directory: %s", fx2txt(status));
619
620 return(status);
621}
622
623int
Damien Miller3db5f532002-02-13 14:10:32 +1100624do_rmdir(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100625{
626 u_int status, id;
627
Damien Miller3db5f532002-02-13 14:10:32 +1100628 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000629 send_string_request(conn, id, SSH2_FXP_RMDIR, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100630 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100631
Damien Miller65e42f82010-09-24 22:15:11 +1000632 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100633 if (status != SSH2_FX_OK)
634 error("Couldn't remove directory: %s", fx2txt(status));
635
636 return(status);
637}
638
639Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100640do_stat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100641{
642 u_int id;
643
Damien Miller3db5f532002-02-13 14:10:32 +1100644 id = conn->msg_id++;
645
Damien Miller65e42f82010-09-24 22:15:11 +1000646 send_string_request(conn, id,
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000647 conn->version == 0 ? SSH2_FXP_STAT_VERSION_0 : SSH2_FXP_STAT,
Damien Miller3db5f532002-02-13 14:10:32 +1100648 path, strlen(path));
649
Damien Miller65e42f82010-09-24 22:15:11 +1000650 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100651}
652
653Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100654do_lstat(struct sftp_conn *conn, char *path, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100655{
656 u_int id;
657
Damien Miller3db5f532002-02-13 14:10:32 +1100658 if (conn->version == 0) {
659 if (quiet)
660 debug("Server version does not support lstat operation");
661 else
Damien Miller996acd22003-04-09 20:59:48 +1000662 logit("Server version does not support lstat operation");
Ben Lindstromf26ff5b2002-04-02 21:00:31 +0000663 return(do_stat(conn, path, quiet));
Damien Miller3db5f532002-02-13 14:10:32 +1100664 }
665
666 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000667 send_string_request(conn, id, SSH2_FXP_LSTAT, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100668 strlen(path));
669
Damien Miller65e42f82010-09-24 22:15:11 +1000670 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100671}
672
Damien Millercfe23d32008-02-10 22:20:44 +1100673#ifdef notyet
Damien Miller33804262001-02-04 23:20:18 +1100674Attrib *
Damien Miller3db5f532002-02-13 14:10:32 +1100675do_fstat(struct sftp_conn *conn, char *handle, u_int handle_len, int quiet)
Damien Miller33804262001-02-04 23:20:18 +1100676{
677 u_int id;
678
Damien Miller3db5f532002-02-13 14:10:32 +1100679 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000680 send_string_request(conn, id, SSH2_FXP_FSTAT, handle,
Damien Miller3db5f532002-02-13 14:10:32 +1100681 handle_len);
682
Damien Miller65e42f82010-09-24 22:15:11 +1000683 return(get_decode_stat(conn, id, quiet));
Damien Miller33804262001-02-04 23:20:18 +1100684}
Damien Millercfe23d32008-02-10 22:20:44 +1100685#endif
Damien Miller33804262001-02-04 23:20:18 +1100686
687int
Damien Miller3db5f532002-02-13 14:10:32 +1100688do_setstat(struct sftp_conn *conn, char *path, Attrib *a)
Damien Miller33804262001-02-04 23:20:18 +1100689{
690 u_int status, id;
691
Damien Miller3db5f532002-02-13 14:10:32 +1100692 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000693 send_string_attrs_request(conn, id, SSH2_FXP_SETSTAT, path,
Damien Miller33804262001-02-04 23:20:18 +1100694 strlen(path), a);
695
Damien Miller65e42f82010-09-24 22:15:11 +1000696 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100697 if (status != SSH2_FX_OK)
698 error("Couldn't setstat on \"%s\": %s", path,
699 fx2txt(status));
700
701 return(status);
702}
703
704int
Damien Miller3db5f532002-02-13 14:10:32 +1100705do_fsetstat(struct sftp_conn *conn, char *handle, u_int handle_len,
Damien Miller33804262001-02-04 23:20:18 +1100706 Attrib *a)
707{
708 u_int status, id;
709
Damien Miller3db5f532002-02-13 14:10:32 +1100710 id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000711 send_string_attrs_request(conn, id, SSH2_FXP_FSETSTAT, handle,
Damien Miller33804262001-02-04 23:20:18 +1100712 handle_len, a);
713
Damien Miller65e42f82010-09-24 22:15:11 +1000714 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100715 if (status != SSH2_FX_OK)
716 error("Couldn't fsetstat: %s", fx2txt(status));
717
718 return(status);
719}
720
721char *
Damien Miller3db5f532002-02-13 14:10:32 +1100722do_realpath(struct sftp_conn *conn, char *path)
Damien Miller33804262001-02-04 23:20:18 +1100723{
724 Buffer msg;
725 u_int type, expected_id, count, id;
726 char *filename, *longname;
727 Attrib *a;
728
Damien Miller3db5f532002-02-13 14:10:32 +1100729 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000730 send_string_request(conn, id, SSH2_FXP_REALPATH, path,
Damien Miller3db5f532002-02-13 14:10:32 +1100731 strlen(path));
Damien Miller33804262001-02-04 23:20:18 +1100732
733 buffer_init(&msg);
734
Damien Miller65e42f82010-09-24 22:15:11 +1000735 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +1100736 type = buffer_get_char(&msg);
737 id = buffer_get_int(&msg);
738
739 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000740 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller33804262001-02-04 23:20:18 +1100741
742 if (type == SSH2_FXP_STATUS) {
743 u_int status = buffer_get_int(&msg);
744
745 error("Couldn't canonicalise: %s", fx2txt(status));
Damien Miller49566312010-06-26 09:38:23 +1000746 buffer_free(&msg);
747 return NULL;
Damien Miller33804262001-02-04 23:20:18 +1100748 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000749 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +1100750 SSH2_FXP_NAME, type);
751
752 count = buffer_get_int(&msg);
753 if (count != 1)
754 fatal("Got multiple names (%d) from SSH_FXP_REALPATH", count);
755
756 filename = buffer_get_string(&msg, NULL);
757 longname = buffer_get_string(&msg, NULL);
758 a = decode_attrib(&msg);
759
Darren Tucker4908d442012-07-02 22:15:38 +1000760 debug3("SSH_FXP_REALPATH %s -> %s size %lu", path, filename,
761 (unsigned long)a->size);
Damien Miller33804262001-02-04 23:20:18 +1100762
Darren Tuckera627d422013-06-02 07:31:17 +1000763 free(longname);
Damien Miller33804262001-02-04 23:20:18 +1100764
765 buffer_free(&msg);
766
767 return(filename);
768}
769
770int
Damien Millerc7dba122013-08-21 02:41:15 +1000771do_rename(struct sftp_conn *conn, char *oldpath, char *newpath,
772 int force_legacy)
Damien Miller33804262001-02-04 23:20:18 +1100773{
774 Buffer msg;
775 u_int status, id;
Damien Millerc7dba122013-08-21 02:41:15 +1000776 int use_ext = (conn->exts & SFTP_EXT_POSIX_RENAME) && !force_legacy;
Damien Miller33804262001-02-04 23:20:18 +1100777
778 buffer_init(&msg);
779
780 /* Send rename request */
Damien Miller3db5f532002-02-13 14:10:32 +1100781 id = conn->msg_id++;
Damien Millerc7dba122013-08-21 02:41:15 +1000782 if (use_ext) {
Damien Miller7a3e1d02008-03-27 10:59:57 +1100783 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
784 buffer_put_int(&msg, id);
785 buffer_put_cstring(&msg, "posix-rename@openssh.com");
786 } else {
787 buffer_put_char(&msg, SSH2_FXP_RENAME);
788 buffer_put_int(&msg, id);
789 }
Damien Miller33804262001-02-04 23:20:18 +1100790 buffer_put_cstring(&msg, oldpath);
791 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000792 send_msg(conn, &msg);
Damien Miller7a3e1d02008-03-27 10:59:57 +1100793 debug3("Sent message %s \"%s\" -> \"%s\"",
Damien Millerc7dba122013-08-21 02:41:15 +1000794 use_ext ? "posix-rename@openssh.com" : "SSH2_FXP_RENAME",
795 oldpath, newpath);
Damien Miller33804262001-02-04 23:20:18 +1100796 buffer_free(&msg);
797
Damien Miller65e42f82010-09-24 22:15:11 +1000798 status = get_status(conn, id);
Damien Miller33804262001-02-04 23:20:18 +1100799 if (status != SSH2_FX_OK)
Damien Miller3db5f532002-02-13 14:10:32 +1100800 error("Couldn't rename file \"%s\" to \"%s\": %s", oldpath,
801 newpath, fx2txt(status));
Damien Miller33804262001-02-04 23:20:18 +1100802
803 return(status);
804}
805
806int
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100807do_hardlink(struct sftp_conn *conn, char *oldpath, char *newpath)
808{
809 Buffer msg;
810 u_int status, id;
811
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100812 if ((conn->exts & SFTP_EXT_HARDLINK) == 0) {
813 error("Server does not support hardlink@openssh.com extension");
814 return -1;
815 }
816
Damien Miller3decdba2011-09-22 21:41:05 +1000817 buffer_init(&msg);
818
819 /* Send link request */
820 id = conn->msg_id++;
Darren Tuckeraf1f9092010-12-05 09:02:47 +1100821 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
822 buffer_put_int(&msg, id);
823 buffer_put_cstring(&msg, "hardlink@openssh.com");
824 buffer_put_cstring(&msg, oldpath);
825 buffer_put_cstring(&msg, newpath);
826 send_msg(conn, &msg);
827 debug3("Sent message hardlink@openssh.com \"%s\" -> \"%s\"",
828 oldpath, newpath);
829 buffer_free(&msg);
830
831 status = get_status(conn, id);
832 if (status != SSH2_FX_OK)
833 error("Couldn't link file \"%s\" to \"%s\": %s", oldpath,
834 newpath, fx2txt(status));
835
836 return(status);
837}
838
839int
Damien Miller3db5f532002-02-13 14:10:32 +1100840do_symlink(struct sftp_conn *conn, char *oldpath, char *newpath)
Damien Miller058316f2001-03-08 10:08:49 +1100841{
842 Buffer msg;
843 u_int status, id;
844
Damien Miller3db5f532002-02-13 14:10:32 +1100845 if (conn->version < 3) {
846 error("This server does not support the symlink operation");
847 return(SSH2_FX_OP_UNSUPPORTED);
848 }
849
Damien Miller058316f2001-03-08 10:08:49 +1100850 buffer_init(&msg);
851
Darren Tuckerdca6a4d2004-04-19 22:10:52 +1000852 /* Send symlink request */
Damien Miller3db5f532002-02-13 14:10:32 +1100853 id = conn->msg_id++;
Damien Miller058316f2001-03-08 10:08:49 +1100854 buffer_put_char(&msg, SSH2_FXP_SYMLINK);
855 buffer_put_int(&msg, id);
856 buffer_put_cstring(&msg, oldpath);
857 buffer_put_cstring(&msg, newpath);
Damien Miller65e42f82010-09-24 22:15:11 +1000858 send_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100859 debug3("Sent message SSH2_FXP_SYMLINK \"%s\" -> \"%s\"", oldpath,
860 newpath);
861 buffer_free(&msg);
862
Damien Miller65e42f82010-09-24 22:15:11 +1000863 status = get_status(conn, id);
Damien Miller058316f2001-03-08 10:08:49 +1100864 if (status != SSH2_FX_OK)
Ben Lindstrom8e879cf2002-11-09 15:48:49 +0000865 error("Couldn't symlink file \"%s\" to \"%s\": %s", oldpath,
Damien Miller3db5f532002-02-13 14:10:32 +1100866 newpath, fx2txt(status));
Damien Miller058316f2001-03-08 10:08:49 +1100867
868 return(status);
869}
870
Damien Millercfe23d32008-02-10 22:20:44 +1100871#ifdef notyet
Damien Miller058316f2001-03-08 10:08:49 +1100872char *
Damien Miller3db5f532002-02-13 14:10:32 +1100873do_readlink(struct sftp_conn *conn, char *path)
Damien Miller058316f2001-03-08 10:08:49 +1100874{
875 Buffer msg;
876 u_int type, expected_id, count, id;
877 char *filename, *longname;
878 Attrib *a;
879
Damien Miller3db5f532002-02-13 14:10:32 +1100880 expected_id = id = conn->msg_id++;
Damien Miller65e42f82010-09-24 22:15:11 +1000881 send_string_request(conn, id, SSH2_FXP_READLINK, path, strlen(path));
Damien Miller058316f2001-03-08 10:08:49 +1100882
883 buffer_init(&msg);
884
Damien Miller65e42f82010-09-24 22:15:11 +1000885 get_msg(conn, &msg);
Damien Miller058316f2001-03-08 10:08:49 +1100886 type = buffer_get_char(&msg);
887 id = buffer_get_int(&msg);
888
889 if (id != expected_id)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000890 fatal("ID mismatch (%u != %u)", id, expected_id);
Damien Miller058316f2001-03-08 10:08:49 +1100891
892 if (type == SSH2_FXP_STATUS) {
893 u_int status = buffer_get_int(&msg);
894
895 error("Couldn't readlink: %s", fx2txt(status));
Damien Miller3decdba2011-09-22 21:41:05 +1000896 buffer_free(&msg);
Damien Miller058316f2001-03-08 10:08:49 +1100897 return(NULL);
898 } else if (type != SSH2_FXP_NAME)
Ben Lindstromb1f483f2002-06-23 21:27:18 +0000899 fatal("Expected SSH2_FXP_NAME(%u) packet, got %u",
Damien Miller058316f2001-03-08 10:08:49 +1100900 SSH2_FXP_NAME, type);
901
902 count = buffer_get_int(&msg);
903 if (count != 1)
904 fatal("Got multiple names (%d) from SSH_FXP_READLINK", count);
905
906 filename = buffer_get_string(&msg, NULL);
907 longname = buffer_get_string(&msg, NULL);
908 a = decode_attrib(&msg);
909
910 debug3("SSH_FXP_READLINK %s -> %s", path, filename);
911
Darren Tuckera627d422013-06-02 07:31:17 +1000912 free(longname);
Damien Miller058316f2001-03-08 10:08:49 +1100913
914 buffer_free(&msg);
915
916 return(filename);
917}
Damien Millercfe23d32008-02-10 22:20:44 +1100918#endif
Damien Miller058316f2001-03-08 10:08:49 +1100919
Damien Millerd671e5a2008-05-19 14:53:33 +1000920int
Darren Tucker7b598892008-06-09 22:49:36 +1000921do_statvfs(struct sftp_conn *conn, const char *path, struct sftp_statvfs *st,
Damien Millerd671e5a2008-05-19 14:53:33 +1000922 int quiet)
923{
924 Buffer msg;
925 u_int id;
926
927 if ((conn->exts & SFTP_EXT_STATVFS) == 0) {
928 error("Server does not support statvfs@openssh.com extension");
929 return -1;
930 }
931
932 id = conn->msg_id++;
933
934 buffer_init(&msg);
935 buffer_clear(&msg);
936 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
937 buffer_put_int(&msg, id);
938 buffer_put_cstring(&msg, "statvfs@openssh.com");
939 buffer_put_cstring(&msg, path);
Damien Miller65e42f82010-09-24 22:15:11 +1000940 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000941 buffer_free(&msg);
942
Damien Miller65e42f82010-09-24 22:15:11 +1000943 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000944}
945
946#ifdef notyet
947int
948do_fstatvfs(struct sftp_conn *conn, const char *handle, u_int handle_len,
Darren Tucker7b598892008-06-09 22:49:36 +1000949 struct sftp_statvfs *st, int quiet)
Damien Millerd671e5a2008-05-19 14:53:33 +1000950{
951 Buffer msg;
952 u_int id;
953
954 if ((conn->exts & SFTP_EXT_FSTATVFS) == 0) {
955 error("Server does not support fstatvfs@openssh.com extension");
956 return -1;
957 }
958
959 id = conn->msg_id++;
960
961 buffer_init(&msg);
962 buffer_clear(&msg);
963 buffer_put_char(&msg, SSH2_FXP_EXTENDED);
964 buffer_put_int(&msg, id);
965 buffer_put_cstring(&msg, "fstatvfs@openssh.com");
966 buffer_put_string(&msg, handle, handle_len);
Damien Miller65e42f82010-09-24 22:15:11 +1000967 send_msg(conn, &msg);
Damien Millerd671e5a2008-05-19 14:53:33 +1000968 buffer_free(&msg);
969
Damien Miller65e42f82010-09-24 22:15:11 +1000970 return get_decode_statvfs(conn, st, id, quiet);
Damien Millerd671e5a2008-05-19 14:53:33 +1000971}
972#endif
973
Damien Miller16a13332002-02-13 14:03:56 +1100974static void
Damien Miller65e42f82010-09-24 22:15:11 +1000975send_read_request(struct sftp_conn *conn, u_int id, u_int64_t offset,
976 u_int len, char *handle, u_int handle_len)
Damien Miller16a13332002-02-13 14:03:56 +1100977{
978 Buffer msg;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000979
Damien Miller16a13332002-02-13 14:03:56 +1100980 buffer_init(&msg);
981 buffer_clear(&msg);
982 buffer_put_char(&msg, SSH2_FXP_READ);
983 buffer_put_int(&msg, id);
984 buffer_put_string(&msg, handle, handle_len);
985 buffer_put_int64(&msg, offset);
986 buffer_put_int(&msg, len);
Damien Miller65e42f82010-09-24 22:15:11 +1000987 send_msg(conn, &msg);
Damien Miller16a13332002-02-13 14:03:56 +1100988 buffer_free(&msg);
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000989}
Damien Miller16a13332002-02-13 14:03:56 +1100990
Damien Miller058316f2001-03-08 10:08:49 +1100991int
Damien Miller3db5f532002-02-13 14:10:32 +1100992do_download(struct sftp_conn *conn, char *remote_path, char *local_path,
Damien Miller0d032412013-07-25 11:56:52 +1000993 Attrib *a, int pflag, int resume)
Damien Miller33804262001-02-04 23:20:18 +1100994{
Darren Tucker1b0dd172009-10-07 08:37:48 +1100995 Attrib junk;
Damien Miller16a13332002-02-13 14:03:56 +1100996 Buffer msg;
997 char *handle;
Damien Miller0d032412013-07-25 11:56:52 +1000998 int local_fd = -1, status = 0, write_error;
999 int read_error, write_errno, reordered = 0;
1000 u_int64_t offset = 0, size, highwater;
Damien Millereccb9de2005-06-17 12:59:34 +10001001 u_int handle_len, mode, type, id, buflen, num_req, max_req;
Damien Miller62d57f62003-01-10 21:43:24 +11001002 off_t progress_counter;
Damien Miller0d032412013-07-25 11:56:52 +10001003 struct stat st;
Damien Miller16a13332002-02-13 14:03:56 +11001004 struct request {
1005 u_int id;
1006 u_int len;
1007 u_int64_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001008 TAILQ_ENTRY(request) tq;
Damien Miller16a13332002-02-13 14:03:56 +11001009 };
1010 TAILQ_HEAD(reqhead, request) requests;
1011 struct request *req;
1012
1013 TAILQ_INIT(&requests);
Damien Miller33804262001-02-04 23:20:18 +11001014
Darren Tucker1b0dd172009-10-07 08:37:48 +11001015 if (a == NULL && (a = do_stat(conn, remote_path, 0)) == NULL)
1016 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001017
Damien Miller9e720282008-06-29 22:46:35 +10001018 /* Do not preserve set[ug]id here, as we do not preserve ownership */
Damien Miller33804262001-02-04 23:20:18 +11001019 if (a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
Damien Miller770b3742003-01-08 14:04:53 +11001020 mode = a->perm & 0777;
Damien Miller33804262001-02-04 23:20:18 +11001021 else
1022 mode = 0666;
1023
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001024 if ((a->flags & SSH2_FILEXFER_ATTR_PERMISSIONS) &&
Damien Miller5fa01fd2003-01-14 22:24:47 +11001025 (!S_ISREG(a->perm))) {
1026 error("Cannot download non-regular file: %s", remote_path);
Ben Lindstromc8d1c302001-03-17 00:34:46 +00001027 return(-1);
1028 }
1029
Damien Miller16a13332002-02-13 14:03:56 +11001030 if (a->flags & SSH2_FILEXFER_ATTR_SIZE)
1031 size = a->size;
1032 else
1033 size = 0;
1034
Damien Miller3db5f532002-02-13 14:10:32 +11001035 buflen = conn->transfer_buflen;
Damien Miller33804262001-02-04 23:20:18 +11001036 buffer_init(&msg);
1037
1038 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001039 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001040 buffer_put_char(&msg, SSH2_FXP_OPEN);
1041 buffer_put_int(&msg, id);
1042 buffer_put_cstring(&msg, remote_path);
1043 buffer_put_int(&msg, SSH2_FXF_READ);
1044 attrib_clear(&junk); /* Send empty attributes */
1045 encode_attrib(&msg, &junk);
Damien Miller65e42f82010-09-24 22:15:11 +10001046 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001047 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001048
Damien Miller65e42f82010-09-24 22:15:11 +10001049 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001050 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001051 if (handle == NULL) {
1052 buffer_free(&msg);
Damien Miller33804262001-02-04 23:20:18 +11001053 return(-1);
1054 }
1055
Tim Riceed899eb2013-07-25 15:40:00 -07001056 local_fd = open(local_path, O_WRONLY | O_CREAT | (resume ? 0 : O_TRUNC),
Darren Tucker3750fce2013-06-02 07:52:21 +10001057 mode | S_IWUSR);
Damien Miller3db5f532002-02-13 14:10:32 +11001058 if (local_fd == -1) {
1059 error("Couldn't open local file \"%s\" for writing: %s",
1060 local_path, strerror(errno));
Damien Miller0d032412013-07-25 11:56:52 +10001061 goto fail;
1062 }
1063 offset = highwater = 0;
1064 if (resume) {
1065 if (fstat(local_fd, &st) == -1) {
1066 error("Unable to stat local file \"%s\": %s",
1067 local_path, strerror(errno));
1068 goto fail;
1069 }
1070 if ((size_t)st.st_size > size) {
1071 error("Unable to resume download of \"%s\": "
1072 "local file is larger than remote", local_path);
1073 fail:
1074 do_close(conn, handle, handle_len);
1075 buffer_free(&msg);
1076 free(handle);
1077 return -1;
1078 }
1079 offset = highwater = st.st_size;
Damien Miller3db5f532002-02-13 14:10:32 +11001080 }
1081
Damien Miller33804262001-02-04 23:20:18 +11001082 /* Read from remote and write to local */
Damien Miller0d032412013-07-25 11:56:52 +10001083 write_error = read_error = write_errno = num_req = 0;
Damien Miller16a13332002-02-13 14:03:56 +11001084 max_req = 1;
Damien Miller0d032412013-07-25 11:56:52 +10001085 progress_counter = offset;
Damien Miller62d57f62003-01-10 21:43:24 +11001086
Damien Miller9ba30692004-03-08 23:12:02 +11001087 if (showprogress && size != 0)
1088 start_progress_meter(remote_path, size, &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001089
Damien Miller16a13332002-02-13 14:03:56 +11001090 while (num_req > 0 || max_req > 0) {
Damien Miller33804262001-02-04 23:20:18 +11001091 char *data;
Damien Miller16a13332002-02-13 14:03:56 +11001092 u_int len;
Damien Miller33804262001-02-04 23:20:18 +11001093
Darren Tuckercdf547a2004-05-24 10:12:19 +10001094 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001095 * Simulate EOF on interrupt: stop sending new requests and
Darren Tuckercdf547a2004-05-24 10:12:19 +10001096 * allow outstanding requests to drain gracefully
1097 */
1098 if (interrupted) {
1099 if (num_req == 0) /* If we haven't started yet... */
1100 break;
1101 max_req = 0;
1102 }
1103
Damien Miller16a13332002-02-13 14:03:56 +11001104 /* Send some more requests */
1105 while (num_req < max_req) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001106 debug3("Request range %llu -> %llu (%d/%d)",
Ben Lindstromd45f28c2002-03-22 01:00:57 +00001107 (unsigned long long)offset,
1108 (unsigned long long)offset + buflen - 1,
1109 num_req, max_req);
Damien Miller16a13332002-02-13 14:03:56 +11001110 req = xmalloc(sizeof(*req));
Damien Miller3db5f532002-02-13 14:10:32 +11001111 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001112 req->len = buflen;
1113 req->offset = offset;
1114 offset += buflen;
1115 num_req++;
1116 TAILQ_INSERT_TAIL(&requests, req, tq);
Damien Miller65e42f82010-09-24 22:15:11 +10001117 send_read_request(conn, req->id, req->offset,
Damien Miller16a13332002-02-13 14:03:56 +11001118 req->len, handle, handle_len);
1119 }
Damien Miller33804262001-02-04 23:20:18 +11001120
1121 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001122 get_msg(conn, &msg);
Damien Miller33804262001-02-04 23:20:18 +11001123 type = buffer_get_char(&msg);
1124 id = buffer_get_int(&msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001125 debug3("Received reply T:%u I:%u R:%d", type, id, max_req);
Damien Miller33804262001-02-04 23:20:18 +11001126
Damien Miller16a13332002-02-13 14:03:56 +11001127 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001128 for (req = TAILQ_FIRST(&requests);
Damien Miller16a13332002-02-13 14:03:56 +11001129 req != NULL && req->id != id;
1130 req = TAILQ_NEXT(req, tq))
1131 ;
1132 if (req == NULL)
1133 fatal("Unexpected reply %u", id);
1134
1135 switch (type) {
1136 case SSH2_FXP_STATUS:
1137 status = buffer_get_int(&msg);
1138 if (status != SSH2_FX_EOF)
1139 read_error = 1;
1140 max_req = 0;
1141 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001142 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001143 num_req--;
1144 break;
1145 case SSH2_FXP_DATA:
1146 data = buffer_get_string(&msg, &len);
Ben Lindstromeb505452002-03-22 01:03:15 +00001147 debug3("Received data %llu -> %llu",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001148 (unsigned long long)req->offset,
Ben Lindstromeb505452002-03-22 01:03:15 +00001149 (unsigned long long)req->offset + len - 1);
Damien Miller16a13332002-02-13 14:03:56 +11001150 if (len > req->len)
1151 fatal("Received more data than asked for "
Ben Lindstrom93576d92002-12-23 02:06:19 +00001152 "%u > %u", len, req->len);
Damien Miller16a13332002-02-13 14:03:56 +11001153 if ((lseek(local_fd, req->offset, SEEK_SET) == -1 ||
Darren Tucker9f63f222003-07-03 13:46:56 +10001154 atomicio(vwrite, local_fd, data, len) != len) &&
Damien Miller16a13332002-02-13 14:03:56 +11001155 !write_error) {
1156 write_errno = errno;
1157 write_error = 1;
1158 max_req = 0;
Damien Miller33804262001-02-04 23:20:18 +11001159 }
Damien Miller0d032412013-07-25 11:56:52 +10001160 else if (!reordered && req->offset <= highwater)
1161 highwater = req->offset + len;
1162 else if (!reordered && req->offset > highwater)
1163 reordered = 1;
Damien Miller62d57f62003-01-10 21:43:24 +11001164 progress_counter += len;
Darren Tuckera627d422013-06-02 07:31:17 +10001165 free(data);
Damien Miller16a13332002-02-13 14:03:56 +11001166
1167 if (len == req->len) {
1168 TAILQ_REMOVE(&requests, req, tq);
Darren Tuckera627d422013-06-02 07:31:17 +10001169 free(req);
Damien Miller16a13332002-02-13 14:03:56 +11001170 num_req--;
1171 } else {
1172 /* Resend the request for the missing data */
1173 debug3("Short data block, re-requesting "
Ben Lindstromeb505452002-03-22 01:03:15 +00001174 "%llu -> %llu (%2d)",
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001175 (unsigned long long)req->offset + len,
Ben Lindstrom83b79e42002-03-22 01:05:27 +00001176 (unsigned long long)req->offset +
1177 req->len - 1, num_req);
Damien Miller3db5f532002-02-13 14:10:32 +11001178 req->id = conn->msg_id++;
Damien Miller16a13332002-02-13 14:03:56 +11001179 req->len -= len;
1180 req->offset += len;
Damien Miller65e42f82010-09-24 22:15:11 +10001181 send_read_request(conn, req->id,
Damien Miller3db5f532002-02-13 14:10:32 +11001182 req->offset, req->len, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001183 /* Reduce the request size */
1184 if (len < buflen)
1185 buflen = MAX(MIN_READ_SIZE, len);
1186 }
1187 if (max_req > 0) { /* max_req = 0 iff EOF received */
1188 if (size > 0 && offset > size) {
1189 /* Only one request at a time
1190 * after the expected EOF */
1191 debug3("Finish at %llu (%2d)",
Ben Lindstromeb505452002-03-22 01:03:15 +00001192 (unsigned long long)offset,
1193 num_req);
Damien Miller16a13332002-02-13 14:03:56 +11001194 max_req = 1;
Darren Tuckercdf547a2004-05-24 10:12:19 +10001195 } else if (max_req <= conn->num_requests) {
Damien Miller16a13332002-02-13 14:03:56 +11001196 ++max_req;
1197 }
1198 }
1199 break;
1200 default:
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001201 fatal("Expected SSH2_FXP_DATA(%u) packet, got %u",
Damien Miller33804262001-02-04 23:20:18 +11001202 SSH2_FXP_DATA, type);
1203 }
Damien Miller33804262001-02-04 23:20:18 +11001204 }
Damien Miller33804262001-02-04 23:20:18 +11001205
Damien Miller62d57f62003-01-10 21:43:24 +11001206 if (showprogress && size)
1207 stop_progress_meter();
1208
Damien Miller16a13332002-02-13 14:03:56 +11001209 /* Sanity check */
1210 if (TAILQ_FIRST(&requests) != NULL)
1211 fatal("Transfer complete, but requests still in queue");
Damien Miller0d032412013-07-25 11:56:52 +10001212 /* Truncate at highest contiguous point to avoid holes on interrupt */
1213 if (read_error || write_error || interrupted) {
1214 if (reordered && resume) {
1215 error("Unable to resume download of \"%s\": "
1216 "server reordered requests", local_path);
1217 }
1218 debug("truncating at %llu", (unsigned long long)highwater);
1219 ftruncate(local_fd, highwater);
1220 }
Damien Miller16a13332002-02-13 14:03:56 +11001221 if (read_error) {
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001222 error("Couldn't read from remote file \"%s\" : %s",
Damien Miller3db5f532002-02-13 14:10:32 +11001223 remote_path, fx2txt(status));
1224 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001225 } else if (write_error) {
Damien Miller3db5f532002-02-13 14:10:32 +11001226 error("Couldn't write to \"%s\": %s", local_path,
1227 strerror(write_errno));
1228 status = -1;
1229 do_close(conn, handle, handle_len);
Damien Miller16a13332002-02-13 14:03:56 +11001230 } else {
Damien Miller3db5f532002-02-13 14:10:32 +11001231 status = do_close(conn, handle, handle_len);
Damien Miller0d032412013-07-25 11:56:52 +10001232 if (interrupted)
1233 status = -1;
Damien Miller16a13332002-02-13 14:03:56 +11001234 /* Override umask and utimes if asked */
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001235#ifdef HAVE_FCHMOD
Damien Miller16a13332002-02-13 14:03:56 +11001236 if (pflag && fchmod(local_fd, mode) == -1)
Damien Millera8e06ce2003-11-21 23:48:55 +11001237#else
Damien Miller16a13332002-02-13 14:03:56 +11001238 if (pflag && chmod(local_path, mode) == -1)
Ben Lindstrom6dc75f52001-02-17 16:47:47 +00001239#endif /* HAVE_FCHMOD */
Damien Miller16a13332002-02-13 14:03:56 +11001240 error("Couldn't set mode on \"%s\": %s", local_path,
Ben Lindstrom93576d92002-12-23 02:06:19 +00001241 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001242 if (pflag && (a->flags & SSH2_FILEXFER_ATTR_ACMODTIME)) {
1243 struct timeval tv[2];
1244 tv[0].tv_sec = a->atime;
1245 tv[1].tv_sec = a->mtime;
1246 tv[0].tv_usec = tv[1].tv_usec = 0;
1247 if (utimes(local_path, tv) == -1)
1248 error("Can't set times on \"%s\": %s",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001249 local_path, strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001250 }
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001251 }
Damien Millerd7686fd2001-02-10 00:40:03 +11001252 close(local_fd);
1253 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +10001254 free(handle);
Damien Miller3db5f532002-02-13 14:10:32 +11001255
1256 return(status);
Damien Miller33804262001-02-04 23:20:18 +11001257}
1258
Darren Tucker1b0dd172009-10-07 08:37:48 +11001259static int
1260download_dir_internal(struct sftp_conn *conn, char *src, char *dst,
Damien Miller0d032412013-07-25 11:56:52 +10001261 Attrib *dirattrib, int pflag, int printflag, int depth, int resume)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001262{
1263 int i, ret = 0;
1264 SFTP_DIRENT **dir_entries;
1265 char *filename, *new_src, *new_dst;
1266 mode_t mode = 0777;
1267
1268 if (depth >= MAX_DIR_DEPTH) {
1269 error("Maximum directory depth exceeded: %d levels", depth);
1270 return -1;
1271 }
1272
1273 if (dirattrib == NULL &&
1274 (dirattrib = do_stat(conn, src, 1)) == NULL) {
1275 error("Unable to stat remote directory \"%s\"", src);
1276 return -1;
1277 }
1278 if (!S_ISDIR(dirattrib->perm)) {
1279 error("\"%s\" is not a directory", src);
1280 return -1;
1281 }
1282 if (printflag)
1283 printf("Retrieving %s\n", src);
1284
1285 if (dirattrib->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)
1286 mode = dirattrib->perm & 01777;
1287 else {
1288 debug("Server did not send permissions for "
1289 "directory \"%s\"", dst);
1290 }
1291
1292 if (mkdir(dst, mode) == -1 && errno != EEXIST) {
1293 error("mkdir %s: %s", dst, strerror(errno));
1294 return -1;
1295 }
1296
1297 if (do_readdir(conn, src, &dir_entries) == -1) {
1298 error("%s: Failed to get directory contents", src);
1299 return -1;
1300 }
1301
1302 for (i = 0; dir_entries[i] != NULL && !interrupted; i++) {
1303 filename = dir_entries[i]->filename;
1304
1305 new_dst = path_append(dst, filename);
1306 new_src = path_append(src, filename);
1307
1308 if (S_ISDIR(dir_entries[i]->a.perm)) {
1309 if (strcmp(filename, ".") == 0 ||
1310 strcmp(filename, "..") == 0)
1311 continue;
1312 if (download_dir_internal(conn, new_src, new_dst,
1313 &(dir_entries[i]->a), pflag, printflag,
Damien Miller0d032412013-07-25 11:56:52 +10001314 depth + 1, resume) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001315 ret = -1;
1316 } else if (S_ISREG(dir_entries[i]->a.perm) ) {
1317 if (do_download(conn, new_src, new_dst,
Damien Miller0d032412013-07-25 11:56:52 +10001318 &(dir_entries[i]->a), pflag, resume) == -1) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001319 error("Download of file %s to %s failed",
1320 new_src, new_dst);
1321 ret = -1;
1322 }
1323 } else
1324 logit("%s: not a regular file\n", new_src);
1325
Darren Tuckera627d422013-06-02 07:31:17 +10001326 free(new_dst);
1327 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001328 }
1329
1330 if (pflag) {
1331 if (dirattrib->flags & SSH2_FILEXFER_ATTR_ACMODTIME) {
1332 struct timeval tv[2];
1333 tv[0].tv_sec = dirattrib->atime;
1334 tv[1].tv_sec = dirattrib->mtime;
1335 tv[0].tv_usec = tv[1].tv_usec = 0;
1336 if (utimes(dst, tv) == -1)
1337 error("Can't set times on \"%s\": %s",
1338 dst, strerror(errno));
1339 } else
1340 debug("Server did not send times for directory "
1341 "\"%s\"", dst);
1342 }
1343
1344 free_sftp_dirents(dir_entries);
1345
1346 return ret;
1347}
1348
1349int
1350download_dir(struct sftp_conn *conn, char *src, char *dst,
Damien Miller0d032412013-07-25 11:56:52 +10001351 Attrib *dirattrib, int pflag, int printflag, int resume)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001352{
1353 char *src_canon;
1354 int ret;
1355
1356 if ((src_canon = do_realpath(conn, src)) == NULL) {
1357 error("Unable to canonicalise path \"%s\"", src);
1358 return -1;
1359 }
1360
1361 ret = download_dir_internal(conn, src_canon, dst,
Damien Miller0d032412013-07-25 11:56:52 +10001362 dirattrib, pflag, printflag, 0, resume);
Darren Tuckera627d422013-06-02 07:31:17 +10001363 free(src_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001364 return ret;
1365}
1366
Damien Miller33804262001-02-04 23:20:18 +11001367int
Damien Miller3db5f532002-02-13 14:10:32 +11001368do_upload(struct sftp_conn *conn, char *local_path, char *remote_path,
1369 int pflag)
Damien Miller33804262001-02-04 23:20:18 +11001370{
Damien Milleracdf25b2008-02-10 22:27:24 +11001371 int local_fd;
1372 int status = SSH2_FX_OK;
Damien Miller5873dfd2002-02-13 14:04:37 +11001373 u_int handle_len, id, type;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001374 off_t offset, progress_counter;
Damien Miller8829d362002-02-08 22:04:05 +11001375 char *handle, *data;
Damien Miller33804262001-02-04 23:20:18 +11001376 Buffer msg;
1377 struct stat sb;
1378 Attrib a;
Damien Miller16a13332002-02-13 14:03:56 +11001379 u_int32_t startid;
1380 u_int32_t ackid;
Damien Miller5873dfd2002-02-13 14:04:37 +11001381 struct outstanding_ack {
1382 u_int id;
1383 u_int len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001384 off_t offset;
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001385 TAILQ_ENTRY(outstanding_ack) tq;
Damien Miller5873dfd2002-02-13 14:04:37 +11001386 };
1387 TAILQ_HEAD(ackhead, outstanding_ack) acks;
Damien Miller7cf17eb2004-06-15 10:28:56 +10001388 struct outstanding_ack *ack = NULL;
Damien Miller5873dfd2002-02-13 14:04:37 +11001389
1390 TAILQ_INIT(&acks);
Damien Miller33804262001-02-04 23:20:18 +11001391
1392 if ((local_fd = open(local_path, O_RDONLY, 0)) == -1) {
1393 error("Couldn't open local file \"%s\" for reading: %s",
1394 local_path, strerror(errno));
1395 return(-1);
1396 }
1397 if (fstat(local_fd, &sb) == -1) {
1398 error("Couldn't fstat local file \"%s\": %s",
1399 local_path, strerror(errno));
1400 close(local_fd);
1401 return(-1);
1402 }
Damien Miller5fa01fd2003-01-14 22:24:47 +11001403 if (!S_ISREG(sb.st_mode)) {
1404 error("%s is not a regular file", local_path);
1405 close(local_fd);
1406 return(-1);
1407 }
Damien Miller33804262001-02-04 23:20:18 +11001408 stat_to_attrib(&sb, &a);
1409
1410 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1411 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1412 a.perm &= 0777;
1413 if (!pflag)
1414 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
1415
1416 buffer_init(&msg);
1417
1418 /* Send open request */
Damien Miller3db5f532002-02-13 14:10:32 +11001419 id = conn->msg_id++;
Damien Miller33804262001-02-04 23:20:18 +11001420 buffer_put_char(&msg, SSH2_FXP_OPEN);
1421 buffer_put_int(&msg, id);
1422 buffer_put_cstring(&msg, remote_path);
1423 buffer_put_int(&msg, SSH2_FXF_WRITE|SSH2_FXF_CREAT|SSH2_FXF_TRUNC);
1424 encode_attrib(&msg, &a);
Damien Miller65e42f82010-09-24 22:15:11 +10001425 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001426 debug3("Sent message SSH2_FXP_OPEN I:%u P:%s", id, remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001427
1428 buffer_clear(&msg);
1429
Damien Miller65e42f82010-09-24 22:15:11 +10001430 handle = get_handle(conn, id, &handle_len,
Darren Tuckerc22f0902009-10-07 08:24:19 +11001431 "remote open(\"%s\")", remote_path);
Damien Miller33804262001-02-04 23:20:18 +11001432 if (handle == NULL) {
1433 close(local_fd);
1434 buffer_free(&msg);
Damien Milleracdf25b2008-02-10 22:27:24 +11001435 return -1;
Damien Miller33804262001-02-04 23:20:18 +11001436 }
1437
Damien Miller16a13332002-02-13 14:03:56 +11001438 startid = ackid = id + 1;
Damien Miller3db5f532002-02-13 14:10:32 +11001439 data = xmalloc(conn->transfer_buflen);
Damien Miller8829d362002-02-08 22:04:05 +11001440
Damien Miller33804262001-02-04 23:20:18 +11001441 /* Read from local and write to remote */
Darren Tuckerc9a19912013-06-02 08:37:05 +10001442 offset = progress_counter = 0;
Damien Miller62d57f62003-01-10 21:43:24 +11001443 if (showprogress)
Darren Tuckerc9a19912013-06-02 08:37:05 +10001444 start_progress_meter(local_path, sb.st_size,
1445 &progress_counter);
Damien Miller62d57f62003-01-10 21:43:24 +11001446
Damien Miller9f0f5c62001-12-21 14:45:46 +11001447 for (;;) {
Damien Miller33804262001-02-04 23:20:18 +11001448 int len;
Damien Miller33804262001-02-04 23:20:18 +11001449
1450 /*
Darren Tuckerfc959702004-07-17 16:12:08 +10001451 * Can't use atomicio here because it returns 0 on EOF,
Darren Tuckercdf547a2004-05-24 10:12:19 +10001452 * thus losing the last block of the file.
Darren Tuckerfc959702004-07-17 16:12:08 +10001453 * Simulate an EOF on interrupt, allowing ACKs from the
Darren Tuckercdf547a2004-05-24 10:12:19 +10001454 * server to drain.
Damien Miller33804262001-02-04 23:20:18 +11001455 */
Damien Milleracdf25b2008-02-10 22:27:24 +11001456 if (interrupted || status != SSH2_FX_OK)
Darren Tuckercdf547a2004-05-24 10:12:19 +10001457 len = 0;
1458 else do
Damien Miller3db5f532002-02-13 14:10:32 +11001459 len = read(local_fd, data, conn->transfer_buflen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001460 while ((len == -1) &&
1461 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
Damien Miller33804262001-02-04 23:20:18 +11001462
1463 if (len == -1)
1464 fatal("Couldn't read from \"%s\": %s", local_path,
1465 strerror(errno));
Damien Miller16a13332002-02-13 14:03:56 +11001466
1467 if (len != 0) {
Damien Miller5873dfd2002-02-13 14:04:37 +11001468 ack = xmalloc(sizeof(*ack));
1469 ack->id = ++id;
1470 ack->offset = offset;
1471 ack->len = len;
1472 TAILQ_INSERT_TAIL(&acks, ack, tq);
1473
Damien Miller16a13332002-02-13 14:03:56 +11001474 buffer_clear(&msg);
1475 buffer_put_char(&msg, SSH2_FXP_WRITE);
Damien Miller5873dfd2002-02-13 14:04:37 +11001476 buffer_put_int(&msg, ack->id);
Damien Miller16a13332002-02-13 14:03:56 +11001477 buffer_put_string(&msg, handle, handle_len);
1478 buffer_put_int64(&msg, offset);
1479 buffer_put_string(&msg, data, len);
Damien Miller65e42f82010-09-24 22:15:11 +10001480 send_msg(conn, &msg);
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001481 debug3("Sent message SSH2_FXP_WRITE I:%u O:%llu S:%u",
Ben Lindstrom93576d92002-12-23 02:06:19 +00001482 id, (unsigned long long)offset, len);
Damien Miller5873dfd2002-02-13 14:04:37 +11001483 } else if (TAILQ_FIRST(&acks) == NULL)
Damien Miller33804262001-02-04 23:20:18 +11001484 break;
1485
Damien Miller5873dfd2002-02-13 14:04:37 +11001486 if (ack == NULL)
1487 fatal("Unexpected ACK %u", id);
1488
Ben Lindstrom6328ab32002-03-22 02:54:23 +00001489 if (id == startid || len == 0 ||
Damien Miller3db5f532002-02-13 14:10:32 +11001490 id - ackid >= conn->num_requests) {
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001491 u_int r_id;
Ben Lindstrom06e95152002-04-06 04:16:45 +00001492
Damien Miller5873dfd2002-02-13 14:04:37 +11001493 buffer_clear(&msg);
Damien Miller65e42f82010-09-24 22:15:11 +10001494 get_msg(conn, &msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001495 type = buffer_get_char(&msg);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001496 r_id = buffer_get_int(&msg);
Damien Miller5873dfd2002-02-13 14:04:37 +11001497
1498 if (type != SSH2_FXP_STATUS)
1499 fatal("Expected SSH2_FXP_STATUS(%d) packet, "
1500 "got %d", SSH2_FXP_STATUS, type);
1501
1502 status = buffer_get_int(&msg);
1503 debug3("SSH2_FXP_STATUS %d", status);
1504
1505 /* Find the request in our queue */
Darren Tucker47eede72005-03-14 23:08:12 +11001506 for (ack = TAILQ_FIRST(&acks);
Ben Lindstrom06e95152002-04-06 04:16:45 +00001507 ack != NULL && ack->id != r_id;
Damien Miller5873dfd2002-02-13 14:04:37 +11001508 ack = TAILQ_NEXT(ack, tq))
1509 ;
1510 if (ack == NULL)
Ben Lindstromb1f483f2002-06-23 21:27:18 +00001511 fatal("Can't find request for ID %u", r_id);
Damien Miller5873dfd2002-02-13 14:04:37 +11001512 TAILQ_REMOVE(&acks, ack, tq);
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001513 debug3("In write loop, ack for %u %u bytes at %lld",
1514 ack->id, ack->len, (long long)ack->offset);
Damien Miller16a13332002-02-13 14:03:56 +11001515 ++ackid;
Darren Tuckerc9a19912013-06-02 08:37:05 +10001516 progress_counter += ack->len;
Darren Tuckera627d422013-06-02 07:31:17 +10001517 free(ack);
Damien Miller33804262001-02-04 23:20:18 +11001518 }
Damien Miller33804262001-02-04 23:20:18 +11001519 offset += len;
Damien Miller8b3fdfb2007-09-17 16:12:03 +10001520 if (offset < 0)
1521 fatal("%s: offset < 0", __func__);
Damien Miller33804262001-02-04 23:20:18 +11001522 }
Damien Milleracdf25b2008-02-10 22:27:24 +11001523 buffer_free(&msg);
1524
Damien Miller62d57f62003-01-10 21:43:24 +11001525 if (showprogress)
1526 stop_progress_meter();
Darren Tuckera627d422013-06-02 07:31:17 +10001527 free(data);
Damien Miller33804262001-02-04 23:20:18 +11001528
Damien Milleracdf25b2008-02-10 22:27:24 +11001529 if (status != SSH2_FX_OK) {
1530 error("Couldn't write to remote file \"%s\": %s",
1531 remote_path, fx2txt(status));
1532 status = -1;
1533 }
1534
Damien Miller33804262001-02-04 23:20:18 +11001535 if (close(local_fd) == -1) {
1536 error("Couldn't close local file \"%s\": %s", local_path,
1537 strerror(errno));
Damien Millerd7686fd2001-02-10 00:40:03 +11001538 status = -1;
Damien Miller33804262001-02-04 23:20:18 +11001539 }
1540
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001541 /* Override umask and utimes if asked */
1542 if (pflag)
Damien Miller3db5f532002-02-13 14:10:32 +11001543 do_fsetstat(conn, handle, handle_len, &a);
Ben Lindstrom9d4f2c82001-02-15 03:22:45 +00001544
Damien Milleracdf25b2008-02-10 22:27:24 +11001545 if (do_close(conn, handle, handle_len) != SSH2_FX_OK)
1546 status = -1;
Darren Tuckera627d422013-06-02 07:31:17 +10001547 free(handle);
Damien Milleracdf25b2008-02-10 22:27:24 +11001548
1549 return status;
Damien Miller33804262001-02-04 23:20:18 +11001550}
Darren Tucker1b0dd172009-10-07 08:37:48 +11001551
1552static int
1553upload_dir_internal(struct sftp_conn *conn, char *src, char *dst,
1554 int pflag, int printflag, int depth)
1555{
1556 int ret = 0, status;
1557 DIR *dirp;
1558 struct dirent *dp;
1559 char *filename, *new_src, *new_dst;
1560 struct stat sb;
1561 Attrib a;
1562
1563 if (depth >= MAX_DIR_DEPTH) {
1564 error("Maximum directory depth exceeded: %d levels", depth);
1565 return -1;
1566 }
1567
1568 if (stat(src, &sb) == -1) {
1569 error("Couldn't stat directory \"%s\": %s",
1570 src, strerror(errno));
1571 return -1;
1572 }
1573 if (!S_ISDIR(sb.st_mode)) {
1574 error("\"%s\" is not a directory", src);
1575 return -1;
1576 }
1577 if (printflag)
1578 printf("Entering %s\n", src);
1579
1580 attrib_clear(&a);
1581 stat_to_attrib(&sb, &a);
1582 a.flags &= ~SSH2_FILEXFER_ATTR_SIZE;
1583 a.flags &= ~SSH2_FILEXFER_ATTR_UIDGID;
1584 a.perm &= 01777;
1585 if (!pflag)
1586 a.flags &= ~SSH2_FILEXFER_ATTR_ACMODTIME;
Damien Miller0d032412013-07-25 11:56:52 +10001587
Darren Tucker1b0dd172009-10-07 08:37:48 +11001588 status = do_mkdir(conn, dst, &a, 0);
1589 /*
1590 * we lack a portable status for errno EEXIST,
1591 * so if we get a SSH2_FX_FAILURE back we must check
1592 * if it was created successfully.
1593 */
1594 if (status != SSH2_FX_OK) {
1595 if (status != SSH2_FX_FAILURE)
1596 return -1;
Damien Miller0d032412013-07-25 11:56:52 +10001597 if (do_stat(conn, dst, 0) == NULL)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001598 return -1;
1599 }
1600
1601 if ((dirp = opendir(src)) == NULL) {
1602 error("Failed to open dir \"%s\": %s", src, strerror(errno));
1603 return -1;
1604 }
Damien Miller0d032412013-07-25 11:56:52 +10001605
Darren Tucker1b0dd172009-10-07 08:37:48 +11001606 while (((dp = readdir(dirp)) != NULL) && !interrupted) {
1607 if (dp->d_ino == 0)
1608 continue;
1609 filename = dp->d_name;
1610 new_dst = path_append(dst, filename);
1611 new_src = path_append(src, filename);
1612
Darren Tucker438b4732009-10-11 21:52:10 +11001613 if (lstat(new_src, &sb) == -1) {
1614 logit("%s: lstat failed: %s", filename,
1615 strerror(errno));
1616 ret = -1;
1617 } else if (S_ISDIR(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001618 if (strcmp(filename, ".") == 0 ||
1619 strcmp(filename, "..") == 0)
1620 continue;
1621
1622 if (upload_dir_internal(conn, new_src, new_dst,
Damien Millerc4bb91c2010-08-03 16:04:22 +10001623 pflag, printflag, depth + 1) == -1)
Darren Tucker1b0dd172009-10-07 08:37:48 +11001624 ret = -1;
Darren Tucker438b4732009-10-11 21:52:10 +11001625 } else if (S_ISREG(sb.st_mode)) {
Darren Tucker1b0dd172009-10-07 08:37:48 +11001626 if (do_upload(conn, new_src, new_dst, pflag) == -1) {
1627 error("Uploading of file %s to %s failed!",
1628 new_src, new_dst);
1629 ret = -1;
1630 }
1631 } else
1632 logit("%s: not a regular file\n", filename);
Darren Tuckera627d422013-06-02 07:31:17 +10001633 free(new_dst);
1634 free(new_src);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001635 }
1636
1637 do_setstat(conn, dst, &a);
1638
1639 (void) closedir(dirp);
1640 return ret;
1641}
1642
1643int
1644upload_dir(struct sftp_conn *conn, char *src, char *dst, int printflag,
1645 int pflag)
1646{
1647 char *dst_canon;
1648 int ret;
1649
1650 if ((dst_canon = do_realpath(conn, dst)) == NULL) {
1651 error("Unable to canonicalise path \"%s\"", dst);
1652 return -1;
1653 }
1654
1655 ret = upload_dir_internal(conn, src, dst_canon, pflag, printflag, 0);
Darren Tuckera627d422013-06-02 07:31:17 +10001656 free(dst_canon);
Darren Tucker1b0dd172009-10-07 08:37:48 +11001657 return ret;
1658}
1659
1660char *
1661path_append(char *p1, char *p2)
1662{
1663 char *ret;
1664 size_t len = strlen(p1) + strlen(p2) + 2;
1665
1666 ret = xmalloc(len);
1667 strlcpy(ret, p1, len);
1668 if (p1[0] != '\0' && p1[strlen(p1) - 1] != '/')
1669 strlcat(ret, "/", len);
1670 strlcat(ret, p2, len);
1671
1672 return(ret);
1673}
1674