#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <assert.h>
-#include <unistd.h>
#include "sftp.h"
#include "int64.h"
return p;
}
+/* Allocate the concatenation of N strings. Terminate arg list with NULL. */
+char *dupcat(char *s1, ...) {
+ int len;
+ char *p, *q, *sn;
+ va_list ap;
+
+ len = strlen(s1);
+ va_start(ap, s1);
+ while (1) {
+ sn = va_arg(ap, char *);
+ if (!sn)
+ break;
+ len += strlen(sn);
+ }
+ va_end(ap);
+
+ p = smalloc(len+1);
+ strcpy(p, s1);
+ q = p + strlen(p);
+
+ va_start(ap, s1);
+ while (1) {
+ sn = va_arg(ap, char *);
+ if (!sn)
+ break;
+ strcpy(q, sn);
+ q += strlen(q);
+ }
+ va_end(ap);
+
+ return p;
+}
+
/* ----------------------------------------------------------------------
* sftp client state.
*/
*/
/*
- * Canonify a pathname starting from the pwd.
+ * Attempt to canonify a pathname starting from the pwd. If
+ * canonification fails, at least fall back to returning a _valid_
+ * pathname (though it may be ugly, eg /home/simon/../foobar).
*/
char *canonify(char *name) {
- if (name[0] == '/')
- return fxp_realpath(name, NULL);
- else
- return fxp_realpath(pwd, name);
+ char *fullname, *canonname;
+ if (name[0] == '/') {
+ fullname = dupstr(name);
+ } else {
+ fullname = dupcat(pwd, "/", name, NULL);
+ }
+ canonname = fxp_realpath(name);
+ if (canonname) {
+ sfree(fullname);
+ return canonname;
+ } else
+ return fullname;
}
/* ----------------------------------------------------------------------
char *dir;
if (cmd->nwords < 2)
- dir = fxp_realpath(".", NULL);
+ dir = dupstr(homedir);
else
dir = canonify(cmd->words[1]);
fname = cmd->words[1];
origoutfname = (cmd->nwords == 2 ? cmd->words[1] : cmd->words[2]);
outfname = canonify(origoutfname);
-~|~ if (!outfname) {
+ if (!outfname) {
printf("%s: %s\n", origoutfname, fxp_error());
return 0;
}
char buffer[4096];
int len;
- len = fread(buffer, 1, len, fp);
+ len = fread(buffer, 1, sizeof(buffer), fp);
if (len == -1) {
printf("error while reading local file\n");
break;
} else if (len == 0) {
break;
}
- if (!fxp_write(fh, buffer, offset, sizeof(buffer))) {
+ if (!fxp_write(fh, buffer, offset, len)) {
printf("error while writing: %s\n", fxp_error());
break;
}
/*
* Find out where our home directory is.
*/
- homedir = fxp_realpath(".", NULL);
+ homedir = fxp_realpath(".");
if (!homedir) {
fprintf(stderr,
"Warning: failed to resolve home directory: %s\n",
close(0); dup2(to[0], 0); close(to[1]);
close(1); dup2(from[1], 1); close(from[0]);
execl("/home/simon/src/openssh/openssh_cvs/prefix/bin/ssh", "ssh", "-2", "simon@localhost", "-s", "sftp", NULL);
+ //execl("/root/ssh-research/ssh-2.4.0/apps/ssh/sftp-server2", "sftp-server2", NULL);
+ //execl("/usr/lib/sftp-server", "sftp-server", NULL);
assert(0); /* bomb out if not */
} else {
/* We are parent. Close wrong end of each pipe, assign to glob vars. */
}
/*
- * Canonify a pathname. Concatenate the two given path elements
- * with a separating slash, unless the second is NULL.
+ * Canonify a pathname.
*/
-char *fxp_realpath(char *path, char *path2) {
+char *fxp_realpath(char *path) {
struct sftp_packet *pktin, *pktout;
int id;
sftp_pkt_adduint32(pktout, 0x123); /* request id */
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_str(pktout, path);
- if (path2) {
- sftp_pkt_addstring_str(pktout, "/");
- sftp_pkt_addstring_str(pktout, path2);
- }
sftp_send(pktout);
pktin = sftp_recv();
id = sftp_pkt_getuint32(pktin);
}
handle = smalloc(sizeof(struct fxp_handle));
handle->hstring = mkstr(hstring, len);
+ handle->hlen = len;
sftp_pkt_free(pktin);
return handle;
} else {
}
handle = smalloc(sizeof(struct fxp_handle));
handle->hstring = mkstr(hstring, len);
+ handle->hlen = len;
sftp_pkt_free(pktin);
return handle;
} else {
pktout = sftp_pkt_init(SSH_FXP_CLOSE);
sftp_pkt_adduint32(pktout, 0x789); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
pktin = sftp_recv();
id = sftp_pkt_getuint32(pktin);
pktout = sftp_pkt_init(SSH_FXP_READ);
sftp_pkt_adduint32(pktout, 0xBCD); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_adduint32(pktout, len);
sftp_send(pktout);
pktout = sftp_pkt_init(SSH_FXP_READDIR);
sftp_pkt_adduint32(pktout, 0xABC); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_send(pktout);
pktin = sftp_recv();
id = sftp_pkt_getuint32(pktin);
pktout = sftp_pkt_init(SSH_FXP_WRITE);
sftp_pkt_adduint32(pktout, 0xDCB); /* request id */
- sftp_pkt_addstring(pktout, handle->hstring);
+ sftp_pkt_addstring_start(pktout);
+ sftp_pkt_addstring_data(pktout, handle->hstring, handle->hlen);
sftp_pkt_adduint64(pktout, offset);
sftp_pkt_addstring_start(pktout);
sftp_pkt_addstring_data(pktout, buffer, len);