X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=unix%2Fuxcons.c;h=716f3fc533d653ccb0b6fa9751124d3df0a7d22f;hb=095072fa46b2d7b8beafaddb2f873d2f500a1e10;hp=4c14eb67a49ebf3b8234deadcce6f7f66c73f1f9;hpb=9fce238b0029c6130a2335b9ce2489b152690bf2;p=PuTTY.git diff --git a/unix/uxcons.c b/unix/uxcons.c index 4c14eb67..716f3fc5 100644 --- a/unix/uxcons.c +++ b/unix/uxcons.c @@ -7,10 +7,15 @@ #include #include #include +#include #include #include #include +#include +#ifndef HAVE_NO_SYS_SELECT_H +#include +#endif #include "putty.h" #include "storage.h" @@ -70,12 +75,46 @@ void notify_remote_exit(void *frontend) { } -void timer_change_notify(long next) +void timer_change_notify(unsigned long next) { } -int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype, - char *keystr, char *fingerprint, +/* + * Wrapper around Unix read(2), suitable for use on a file descriptor + * that's been set into nonblocking mode. Handles EAGAIN/EWOULDBLOCK + * by means of doing a one-fd select and then trying again; all other + * errors (including errors from select) are returned to the caller. + */ +static int block_and_read(int fd, void *buf, size_t len) +{ + int ret; + + while ((ret = read(fd, buf, len)) < 0 && ( +#ifdef EAGAIN + (errno == EAGAIN) || +#endif +#ifdef EWOULDBLOCK + (errno == EWOULDBLOCK) || +#endif + 0)) { + + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + do { + ret = select(fd+1, &rfds, NULL, NULL, NULL); + } while (ret < 0 && errno == EINTR); + assert(ret != 0); + if (ret < 0) + return ret; + assert(FD_ISSET(fd, &rfds)); + } + + return ret; +} + +int verify_ssh_host_key(void *frontend, char *host, int port, + const char *keytype, char *keystr, char *fingerprint, void (*callback)(void *ctx, int result), void *ctx) { int ret; @@ -163,7 +202,7 @@ int verify_ssh_host_key(void *frontend, char *host, int port, char *keytype, newmode.c_lflag |= ECHO | ISIG | ICANON; tcsetattr(0, TCSANOW, &newmode); line[0] = '\0'; - if (read(0, line, sizeof(line) - 1) <= 0) + if (block_and_read(0, line, sizeof(line) - 1) <= 0) /* handled below */; tcsetattr(0, TCSANOW, &oldmode); } @@ -216,7 +255,60 @@ int askalg(void *frontend, const char *algtype, const char *algname, newmode.c_lflag |= ECHO | ISIG | ICANON; tcsetattr(0, TCSANOW, &newmode); line[0] = '\0'; - if (read(0, line, sizeof(line) - 1) <= 0) + if (block_and_read(0, line, sizeof(line) - 1) <= 0) + /* handled below */; + tcsetattr(0, TCSANOW, &oldmode); + } + + if (line[0] == 'y' || line[0] == 'Y') { + postmsg(&cf); + return 1; + } else { + fprintf(stderr, abandoned); + postmsg(&cf); + return 0; + } +} + +int askhk(void *frontend, const char *algname, const char *betteralgs, + void (*callback)(void *ctx, int result), void *ctx) +{ + static const char msg[] = + "The first host key type we have stored for this server\n" + "is %s, which is below the configured warning threshold.\n" + "The server also provides the following types of host key\n" + "above the threshold, which we do not have stored:\n" + "%s\n" + "Continue with connection? (y/n) "; + static const char msg_batch[] = + "The first host key type we have stored for this server\n" + "is %s, which is below the configured warning threshold.\n" + "The server also provides the following types of host key\n" + "above the threshold, which we do not have stored:\n" + "%s\n" + "Connection abandoned.\n"; + static const char abandoned[] = "Connection abandoned.\n"; + + char line[32]; + struct termios cf; + + premsg(&cf); + if (console_batch_mode) { + fprintf(stderr, msg_batch, algname, betteralgs); + return 0; + } + + fprintf(stderr, msg, algname, betteralgs); + fflush(stderr); + + { + struct termios oldmode, newmode; + tcgetattr(0, &oldmode); + newmode = oldmode; + newmode.c_lflag |= ECHO | ISIG | ICANON; + tcsetattr(0, TCSANOW, &newmode); + line[0] = '\0'; + if (block_and_read(0, line, sizeof(line) - 1) <= 0) /* handled below */; tcsetattr(0, TCSANOW, &oldmode); } @@ -235,7 +327,7 @@ int askalg(void *frontend, const char *algtype, const char *algname, * Ask whether to wipe a session log file before writing to it. * Returns 2 for wipe, 1 for append, 0 for cancel (don't log). */ -int askappend(void *frontend, Filename filename, +int askappend(void *frontend, Filename *filename, void (*callback)(void *ctx, int result), void *ctx) { static const char msgtemplate[] = @@ -256,11 +348,11 @@ int askappend(void *frontend, Filename filename, premsg(&cf); if (console_batch_mode) { - fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename.path); + fprintf(stderr, msgtemplate_batch, FILENAME_MAX, filename->path); fflush(stderr); return 0; } - fprintf(stderr, msgtemplate, FILENAME_MAX, filename.path); + fprintf(stderr, msgtemplate, FILENAME_MAX, filename->path); fflush(stderr); { @@ -270,7 +362,7 @@ int askappend(void *frontend, Filename filename, newmode.c_lflag |= ECHO | ISIG | ICANON; tcsetattr(0, TCSANOW, &newmode); line[0] = '\0'; - if (read(0, line, sizeof(line) - 1) <= 0) + if (block_and_read(0, line, sizeof(line) - 1) <= 0) /* handled below */; tcsetattr(0, TCSANOW, &oldmode); } @@ -321,10 +413,12 @@ void console_provide_logctx(void *logctx) void logevent(void *frontend, const char *string) { struct termios cf; - premsg(&cf); + if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) + premsg(&cf); if (console_logctx) log_eventlog(console_logctx, string); - postmsg(&cf); + if ((flags & FLAG_STDERR) && (flags & FLAG_VERBOSE)) + postmsg(&cf); } /* @@ -362,7 +456,8 @@ static void console_prompt_text(FILE *outfp, const char *data, int len) fflush(outfp); } -int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen) +int console_get_userpass_input(prompts_t *p, const unsigned char *in, + int inlen) { size_t curr_prompt; FILE *outfp = NULL; @@ -374,7 +469,7 @@ int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen) { int i; for (i = 0; i < p->n_prompts; i++) - memset(p->prompts[i]->result, 0, p->prompts[i]->result_len); + prompt_set_result(p->prompts[i], ""); } if (p->n_prompts && console_batch_mode) @@ -403,7 +498,7 @@ int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen) for (curr_prompt = 0; curr_prompt < p->n_prompts; curr_prompt++) { struct termios oldmode, newmode; - int i; + int len; prompt_t *pr = p->prompts[curr_prompt]; tcgetattr(infd, &oldmode); @@ -417,17 +512,34 @@ int console_get_userpass_input(prompts_t *p, unsigned char *in, int inlen) console_prompt_text(outfp, pr->prompt, strlen(pr->prompt)); - i = read(infd, pr->result, pr->result_len - 1); + len = 0; + while (1) { + int ret; + + prompt_ensure_result_size(pr, len * 5 / 4 + 512); + ret = read(infd, pr->result + len, pr->resultsize - len - 1); + if (ret <= 0) { + len = -1; + break; + } + len += ret; + if (pr->result[len - 1] == '\n') { + len--; + break; + } + } tcsetattr(infd, TCSANOW, &oldmode); - if (i > 0 && pr->result[i-1] == '\n') - i--; - pr->result[i] = '\0'; - if (!pr->echo) console_prompt_text(outfp, "\n", 1); + if (len < 0) { + console_close(outfp, infd); + return 0; /* failure due to read error */ + } + + pr->result[len] = '\0'; } console_close(outfp, infd);