mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-09 17:38:00 +00:00
And just to prove that psftp.c really is now platform-independent
... here's a Unix port of PSFTP. Woo. (Oddly PSCP looks to be somewhat harder; there's more Windows code interleaved than there was in PSFTP.) [originally from svn r3419]
This commit is contained in:
parent
e0801815c8
commit
66fa6f320e
2
Recipe
2
Recipe
@ -159,7 +159,7 @@ puttytel : [X] UXTERM uxmisc misc ldisc settings pty uxsel be_nossh uxstore
|
||||
|
||||
plink : [U] uxplink uxcons NONSSH UXSSH be_all logging UXMISC signal ux_x11
|
||||
|
||||
# psftp : [U] psftp uxsftp console UXSSH be_none SFTP UXMISC
|
||||
psftp : [U] psftp uxsftp uxcons UXSSH be_none SFTP UXMISC
|
||||
|
||||
PuTTY : [M] terminal wcwidth ldiscucs logging be_all mac macdlg macevlog
|
||||
+ macterm macucs mac_res.rsrc testback NONSSH MACSSH MACMISC CHARSET
|
||||
|
19
psftp.c
19
psftp.c
@ -894,20 +894,20 @@ int sftp_cmd_chmod(struct sftp_command *cmd)
|
||||
default:
|
||||
printf("chmod: file mode '%.*s' contains unrecognised"
|
||||
" user/group/other specifier '%c'\n",
|
||||
strcspn(modebegin, ","), modebegin, *mode);
|
||||
(int)strcspn(modebegin, ","), modebegin, *mode);
|
||||
return 0;
|
||||
}
|
||||
mode++;
|
||||
}
|
||||
if (!*mode || *mode == ',') {
|
||||
printf("chmod: file mode '%.*s' is incomplete\n",
|
||||
strcspn(modebegin, ","), modebegin);
|
||||
(int)strcspn(modebegin, ","), modebegin);
|
||||
return 0;
|
||||
}
|
||||
action = *mode++;
|
||||
if (!*mode || *mode == ',') {
|
||||
printf("chmod: file mode '%.*s' is incomplete\n",
|
||||
strcspn(modebegin, ","), modebegin);
|
||||
(int)strcspn(modebegin, ","), modebegin);
|
||||
return 0;
|
||||
}
|
||||
perms = 0;
|
||||
@ -922,7 +922,7 @@ int sftp_cmd_chmod(struct sftp_command *cmd)
|
||||
(subset & 06777) != 02070) {
|
||||
printf("chmod: file mode '%.*s': set[ug]id bit should"
|
||||
" be used with exactly one of u or g only\n",
|
||||
strcspn(modebegin, ","), modebegin);
|
||||
(int)strcspn(modebegin, ","), modebegin);
|
||||
return 0;
|
||||
}
|
||||
perms |= 06000;
|
||||
@ -930,7 +930,7 @@ int sftp_cmd_chmod(struct sftp_command *cmd)
|
||||
default:
|
||||
printf("chmod: file mode '%.*s' contains unrecognised"
|
||||
" permission specifier '%c'\n",
|
||||
strcspn(modebegin, ","), modebegin, *mode);
|
||||
(int)strcspn(modebegin, ","), modebegin, *mode);
|
||||
return 0;
|
||||
}
|
||||
mode++;
|
||||
@ -938,7 +938,7 @@ int sftp_cmd_chmod(struct sftp_command *cmd)
|
||||
if (!(subset & 06777) && (perms &~ subset)) {
|
||||
printf("chmod: file mode '%.*s' contains no user/group/other"
|
||||
" specifier and permissions other than 't' \n",
|
||||
strcspn(modebegin, ","), modebegin);
|
||||
(int)strcspn(modebegin, ","), modebegin);
|
||||
return 0;
|
||||
}
|
||||
perms &= subset;
|
||||
@ -1740,6 +1740,7 @@ static int psftp_connect(char *userhost, char *user, int portnumber)
|
||||
{
|
||||
char *host, *realhost;
|
||||
const char *err;
|
||||
void *logctx;
|
||||
|
||||
/* Separate host and username */
|
||||
host = userhost;
|
||||
@ -1925,7 +1926,11 @@ int psftp_main(int argc, char *argv[])
|
||||
char *batchfile = NULL;
|
||||
int errors = 0;
|
||||
|
||||
flags = FLAG_STDERR | FLAG_INTERACTIVE | FLAG_SYNCAGENT;
|
||||
flags = FLAG_STDERR | FLAG_INTERACTIVE
|
||||
#ifdef FLAG_SYNCAGENT
|
||||
| FLAG_SYNCAGENT
|
||||
#endif
|
||||
;
|
||||
cmdline_tooltype = TOOLTYPE_FILETRANSFER;
|
||||
ssh_get_line = &console_get_line;
|
||||
sk_init();
|
||||
|
6
psftp.h
6
psftp.h
@ -25,4 +25,10 @@ char *psftp_lcd(char *newdir);
|
||||
*/
|
||||
int ssh_sftp_loop_iteration(void);
|
||||
|
||||
/*
|
||||
* The main program in psftp.c. Called from main() in the platform-
|
||||
* specific code, after doing any platform-specific initialisation.
|
||||
*/
|
||||
int psftp_main(int argc, char *argv[]);
|
||||
|
||||
#endif /* PUTTY_PSFTP_H */
|
||||
|
5
sftp.c
5
sftp.c
@ -67,10 +67,12 @@ static struct sftp_packet *sftp_pkt_init(int pkt_type)
|
||||
sftp_pkt_addbyte(pkt, (unsigned char) pkt_type);
|
||||
return pkt;
|
||||
}
|
||||
/*
|
||||
static void sftp_pkt_addbool(struct sftp_packet *pkt, unsigned char value)
|
||||
{
|
||||
sftp_pkt_adddata(pkt, &value, 1);
|
||||
}
|
||||
*/
|
||||
static void sftp_pkt_adduint32(struct sftp_packet *pkt,
|
||||
unsigned long value)
|
||||
{
|
||||
@ -282,7 +284,6 @@ static tree234 *sftp_requests;
|
||||
|
||||
static struct sftp_request *sftp_alloc_request(void)
|
||||
{
|
||||
const unsigned CHANNEL_NUMBER_OFFSET = 256;
|
||||
unsigned low, high, mid;
|
||||
int tsize;
|
||||
struct sftp_request *r;
|
||||
@ -296,7 +297,7 @@ static struct sftp_request *sftp_alloc_request(void)
|
||||
* B-tree to find the largest ID which is in a contiguous
|
||||
* sequence from the beginning. (Precisely everything in that
|
||||
* sequence must have ID equal to its tree index plus
|
||||
* SEQUENCE_NUMBER_OFFSET.)
|
||||
* REQUEST_ID_OFFSET.)
|
||||
*/
|
||||
tsize = count234(sftp_requests);
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
Makefile.gtk Makefile
|
||||
local
|
||||
plink pterm putty puttytel
|
||||
plink pterm putty puttytel psftp
|
||||
*.log
|
||||
|
11
unix/unix.h
11
unix/unix.h
@ -51,8 +51,6 @@ unsigned long getticks(void); /* based on gettimeofday(2) */
|
||||
#define WCHAR wchar_t
|
||||
#define BYTE unsigned char
|
||||
|
||||
GLOBAL void *logctx;
|
||||
|
||||
/* Things pty.c needs from pterm.c */
|
||||
char *get_x_display(void *frontend);
|
||||
int font_dimension(void *frontend, int which);/* 0 for width, 1 for height */
|
||||
@ -128,4 +126,13 @@ int init_ucs(struct unicode_data *ucsdata,
|
||||
*/
|
||||
int sk_getxdmdata(void *sock, unsigned long *ip, int *port);
|
||||
|
||||
/*
|
||||
* General helpful Unix stuff: more helpful version of the FD_SET
|
||||
* macro, which also handles maxfd.
|
||||
*/
|
||||
#define FD_SET_MAX(fd, max, set) do { \
|
||||
FD_SET(fd, &set); \
|
||||
if (max < fd + 1) max = fd + 1; \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
int console_batch_mode = FALSE;
|
||||
|
||||
static void *console_logctx = NULL;
|
||||
|
||||
/*
|
||||
* Clean up and exit.
|
||||
*/
|
||||
@ -258,9 +260,15 @@ void old_keyfile_warning(void)
|
||||
fputs(message, stderr);
|
||||
}
|
||||
|
||||
void console_provide_logctx(void *logctx)
|
||||
{
|
||||
console_logctx = logctx;
|
||||
}
|
||||
|
||||
void logevent(void *frontend, const char *string)
|
||||
{
|
||||
log_eventlog(logctx, string);
|
||||
if (console_logctx)
|
||||
log_eventlog(console_logctx, string);
|
||||
}
|
||||
|
||||
int console_get_line(const char *prompt, char *str,
|
||||
|
@ -14,12 +14,6 @@
|
||||
#include <pwd.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
/* More helpful version of the FD_SET macro, to also handle maxfd. */
|
||||
#define FD_SET_MAX(fd, max, set) do { \
|
||||
FD_SET(fd, &set); \
|
||||
if (max < fd + 1) max = fd + 1; \
|
||||
} while (0)
|
||||
|
||||
#define PUTTY_DO_GLOBALS /* actually _define_ globals */
|
||||
#include "putty.h"
|
||||
#include "storage.h"
|
||||
@ -287,7 +281,7 @@ int main(int argc, char **argv)
|
||||
int connopen;
|
||||
int exitcode;
|
||||
int errors;
|
||||
void *ldisc;
|
||||
void *ldisc, *logctx;
|
||||
|
||||
ssh_get_line = console_get_line;
|
||||
|
||||
@ -573,6 +567,7 @@ int main(int argc, char **argv)
|
||||
* Start up the connection.
|
||||
*/
|
||||
logctx = log_init(NULL, &cfg);
|
||||
console_provide_logctx(logctx);
|
||||
{
|
||||
const char *error;
|
||||
char *realhost;
|
||||
|
224
unix/uxsftp.c
Normal file
224
unix/uxsftp.c
Normal file
@ -0,0 +1,224 @@
|
||||
/*
|
||||
* uxsftp.c: the Unix-specific parts of PSFTP.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <pwd.h>
|
||||
|
||||
#include "putty.h"
|
||||
#include "psftp.h"
|
||||
|
||||
/*
|
||||
* In PSFTP our selects are synchronous, so these functions are
|
||||
* empty stubs.
|
||||
*/
|
||||
int uxsel_input_add(int fd, int rwx) { return 0; }
|
||||
void uxsel_input_remove(int id) { }
|
||||
|
||||
char *x_get_default(const char *key)
|
||||
{
|
||||
return NULL; /* this is a stub */
|
||||
}
|
||||
|
||||
void platform_get_x11_auth(char *display, int *protocol,
|
||||
unsigned char *data, int *datalen)
|
||||
{
|
||||
/* Do nothing, therefore no auth. */
|
||||
}
|
||||
|
||||
/*
|
||||
* Default settings that are specific to PSFTP.
|
||||
*/
|
||||
char *platform_default_s(const char *name)
|
||||
{
|
||||
if (!strcmp(name, "UserName")) {
|
||||
/*
|
||||
* Remote login username will default to the local username.
|
||||
*/
|
||||
struct passwd *p;
|
||||
uid_t uid = getuid();
|
||||
char *user, *ret = NULL;
|
||||
|
||||
/*
|
||||
* First, find who we think we are using getlogin. If this
|
||||
* agrees with our uid, we'll go along with it. This should
|
||||
* allow sharing of uids between several login names whilst
|
||||
* coping correctly with people who have su'ed.
|
||||
*/
|
||||
user = getlogin();
|
||||
setpwent();
|
||||
if (user)
|
||||
p = getpwnam(user);
|
||||
else
|
||||
p = NULL;
|
||||
if (p && p->pw_uid == uid) {
|
||||
/*
|
||||
* The result of getlogin() really does correspond to
|
||||
* our uid. Fine.
|
||||
*/
|
||||
ret = user;
|
||||
} else {
|
||||
/*
|
||||
* If that didn't work, for whatever reason, we'll do
|
||||
* the simpler version: look up our uid in the password
|
||||
* file and map it straight to a name.
|
||||
*/
|
||||
p = getpwuid(uid);
|
||||
ret = p->pw_name;
|
||||
}
|
||||
endpwent();
|
||||
|
||||
return ret;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int platform_default_i(const char *name, int def)
|
||||
{
|
||||
return def;
|
||||
}
|
||||
|
||||
FontSpec platform_default_fontspec(const char *name)
|
||||
{
|
||||
FontSpec ret;
|
||||
*ret.name = '\0';
|
||||
return ret;
|
||||
}
|
||||
|
||||
Filename platform_default_filename(const char *name)
|
||||
{
|
||||
Filename ret;
|
||||
if (!strcmp(name, "LogFileName"))
|
||||
strcpy(ret.path, "putty.log");
|
||||
else
|
||||
*ret.path = '\0';
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set local current directory. Returns NULL on success, or else an
|
||||
* error message which must be freed after printing.
|
||||
*/
|
||||
char *psftp_lcd(char *dir)
|
||||
{
|
||||
if (chdir(dir) < 0)
|
||||
return dupprintf("%s: chdir: %s", dir, strerror(errno));
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get local current directory. Returns a string which must be
|
||||
* freed.
|
||||
*/
|
||||
char *psftp_getcwd(void)
|
||||
{
|
||||
char *buffer, *ret;
|
||||
int size = 256;
|
||||
|
||||
buffer = snewn(size, char);
|
||||
while (1) {
|
||||
ret = getcwd(buffer, size);
|
||||
if (ret != NULL)
|
||||
return ret;
|
||||
if (errno != ERANGE) {
|
||||
sfree(buffer);
|
||||
return dupprintf("[cwd unavailable: %s]", strerror(errno));
|
||||
}
|
||||
/*
|
||||
* Otherwise, ERANGE was returned, meaning the buffer
|
||||
* wasn't big enough.
|
||||
*/
|
||||
size = size * 3 / 2;
|
||||
buffer = sresize(buffer, size, char);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Wait for some network data and process it.
|
||||
*/
|
||||
int ssh_sftp_loop_iteration(void)
|
||||
{
|
||||
fd_set rset, wset, xset;
|
||||
int i, fdcount, fdsize, *fdlist;
|
||||
int fd, fdstate, rwx, ret, maxfd;
|
||||
|
||||
fdlist = NULL;
|
||||
fdcount = fdsize = 0;
|
||||
|
||||
/* Count the currently active fds. */
|
||||
i = 0;
|
||||
for (fd = first_fd(&fdstate, &rwx); fd >= 0;
|
||||
fd = next_fd(&fdstate, &rwx)) i++;
|
||||
|
||||
if (i < 1)
|
||||
return -1; /* doom */
|
||||
|
||||
/* Expand the fdlist buffer if necessary. */
|
||||
if (i > fdsize) {
|
||||
fdsize = i + 16;
|
||||
fdlist = sresize(fdlist, fdsize, int);
|
||||
}
|
||||
|
||||
FD_ZERO(&rset);
|
||||
FD_ZERO(&wset);
|
||||
FD_ZERO(&xset);
|
||||
maxfd = 0;
|
||||
|
||||
/*
|
||||
* Add all currently open fds to the select sets, and store
|
||||
* them in fdlist as well.
|
||||
*/
|
||||
fdcount = 0;
|
||||
for (fd = first_fd(&fdstate, &rwx); fd >= 0;
|
||||
fd = next_fd(&fdstate, &rwx)) {
|
||||
fdlist[fdcount++] = fd;
|
||||
if (rwx & 1)
|
||||
FD_SET_MAX(fd, maxfd, rset);
|
||||
if (rwx & 2)
|
||||
FD_SET_MAX(fd, maxfd, wset);
|
||||
if (rwx & 4)
|
||||
FD_SET_MAX(fd, maxfd, xset);
|
||||
}
|
||||
|
||||
do {
|
||||
ret = select(maxfd, &rset, &wset, &xset, NULL);
|
||||
} while (ret < 0 && errno == EINTR);
|
||||
|
||||
if (ret < 0) {
|
||||
perror("select");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < fdcount; i++) {
|
||||
fd = fdlist[i];
|
||||
/*
|
||||
* We must process exceptional notifications before
|
||||
* ordinary readability ones, or we may go straight
|
||||
* past the urgent marker.
|
||||
*/
|
||||
if (FD_ISSET(fd, &xset))
|
||||
select_result(fd, 4);
|
||||
if (FD_ISSET(fd, &rset))
|
||||
select_result(fd, 1);
|
||||
if (FD_ISSET(fd, &wset))
|
||||
select_result(fd, 2);
|
||||
}
|
||||
|
||||
sfree(fdlist);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Main program: do platform-specific initialisation and then call
|
||||
* psftp_main().
|
||||
*/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uxsel_init();
|
||||
return psftp_main(argc, argv);
|
||||
}
|
Loading…
Reference in New Issue
Block a user