1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Add a directory 'contrib/cygtermd', containing the source code for my

hacky helper program to let PuTTY act as a local pterm-oid on
Cygwin-enabled Windows systems.

[originally from svn r9191]
This commit is contained in:
Simon Tatham 2011-07-10 14:22:32 +00:00
parent c8f2b65d16
commit b642aa086a
11 changed files with 1660 additions and 0 deletions

View File

@ -0,0 +1,2 @@
cygtermd.exe: main.c sel.c telnet.c pty.c malloc.c
gcc -o cygtermd.exe main.c sel.c telnet.c pty.c malloc.c

11
contrib/cygtermd/README Normal file
View File

@ -0,0 +1,11 @@
This directory contains 'cygtermd', a small and specialist Telnet
server designed to act as middleware between PuTTY and a Cygwin shell
session running on the same machine, so that PuTTY can act as an
xterm-alike for Cygwin.
To install it, you must compile it from source using Cygwin gcc,
install it in Cygwin's /bin, and configure PuTTY to use it as a local
proxy process. For detailed instructions, see the PuTTY Wishlist page
at
http://www.chiark.greenend.org.uk/~sgtatham/putty/wishlist/cygwin-terminal-window.html

174
contrib/cygtermd/main.c Normal file
View File

@ -0,0 +1,174 @@
/*
* Main program.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "sel.h"
#include "pty.h"
#include "telnet.h"
int signalpipe[2];
sel *asel;
sel_rfd *netr, *ptyr, *sigr;
int ptyfd;
sel_wfd *netw, *ptyw;
Telnet telnet;
#define BUF 65536
void sigchld(int signum)
{
write(signalpipe[1], "C", 1);
}
void fatal(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "FIXME: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
void net_readdata(sel_rfd *rfd, void *data, size_t len)
{
if (len == 0)
exit(0); /* EOF on network - client went away */
telnet_from_net(telnet, data, len);
if (sel_write(netw, NULL, 0) > BUF)
sel_rfd_freeze(ptyr);
if (sel_write(ptyw, NULL, 0) > BUF)
sel_rfd_freeze(netr);
}
void net_readerr(sel_rfd *rfd, int error)
{
fprintf(stderr, "standard input: read: %s\n", strerror(errno));
exit(1);
}
void net_written(sel_wfd *wfd, size_t bufsize)
{
if (bufsize < BUF)
sel_rfd_unfreeze(ptyr);
}
void net_writeerr(sel_wfd *wfd, int error)
{
fprintf(stderr, "standard input: write: %s\n", strerror(errno));
exit(1);
}
void pty_readdata(sel_rfd *rfd, void *data, size_t len)
{
if (len == 0)
exit(0); /* EOF on pty */
telnet_from_pty(telnet, data, len);
if (sel_write(netw, NULL, 0) > BUF)
sel_rfd_freeze(ptyr);
if (sel_write(ptyw, NULL, 0) > BUF)
sel_rfd_freeze(netr);
}
void pty_readerr(sel_rfd *rfd, int error)
{
if (error == EIO) /* means EOF, on a pty */
exit(0);
fprintf(stderr, "pty: read: %s\n", strerror(errno));
exit(1);
}
void pty_written(sel_wfd *wfd, size_t bufsize)
{
if (bufsize < BUF)
sel_rfd_unfreeze(netr);
}
void pty_writeerr(sel_wfd *wfd, int error)
{
fprintf(stderr, "pty: write: %s\n", strerror(errno));
exit(1);
}
void sig_readdata(sel_rfd *rfd, void *data, size_t len)
{
char *p = data;
while (len > 0) {
if (*p == 'C') {
int status;
pid_t pid = waitpid(-1, &status, WNOHANG);
if (WIFEXITED(status) || WIFSIGNALED(status))
exit(0); /* child process vanished */
}
}
}
void sig_readerr(sel_rfd *rfd, int error)
{
fprintf(stderr, "signal pipe: read: %s\n", strerror(errno));
exit(1);
}
int main(int argc, char **argv)
{
int ret;
int shell_started = 0;
char *directory = NULL;
char **program_args = NULL;
if (argc > 1 && argv[1][0]) {
directory = argv[1];
argc--, argv++;
}
if (argc > 1) {
program_args = argv + 1;
}
pty_preinit();
asel = sel_new(NULL);
netr = sel_rfd_add(asel, 0, net_readdata, net_readerr, NULL);
netw = sel_wfd_add(asel, 1, net_written, net_writeerr, NULL);
ptyr = sel_rfd_add(asel, -1, pty_readdata, pty_readerr, NULL);
ptyw = sel_wfd_add(asel, -1, pty_written, pty_writeerr, NULL);
telnet = telnet_new(netw, ptyw);
if (pipe(signalpipe) < 0) {
perror("pipe");
return 1;
}
sigr = sel_rfd_add(asel, signalpipe[0], sig_readdata,
sig_readerr, NULL);
signal(SIGCHLD, sigchld);
do {
struct shell_data shdata;
ret = sel_iterate(asel, -1);
if (!shell_started && telnet_shell_ok(telnet, &shdata)) {
ptyfd = run_program_in_pty(&shdata, directory, program_args);
sel_rfd_setfd(ptyr, ptyfd);
sel_wfd_setfd(ptyw, ptyfd);
shell_started = 1;
}
} while (ret == 0);
return 0;
}

43
contrib/cygtermd/malloc.c Normal file
View File

@ -0,0 +1,43 @@
/*
* malloc.c: implementation of malloc.h
*/
#include <stdlib.h>
#include <string.h>
#include "malloc.h"
extern void fatal(const char *, ...);
void *smalloc(size_t size) {
void *p;
p = malloc(size);
if (!p) {
fatal("out of memory");
}
return p;
}
void sfree(void *p) {
if (p) {
free(p);
}
}
void *srealloc(void *p, size_t size) {
void *q;
if (p) {
q = realloc(p, size);
} else {
q = malloc(size);
}
if (!q)
fatal("out of memory");
return q;
}
char *dupstr(const char *s) {
char *r = smalloc(1+strlen(s));
strcpy(r,s);
return r;
}

56
contrib/cygtermd/malloc.h Normal file
View File

@ -0,0 +1,56 @@
/*
* malloc.h: safe wrappers around malloc, realloc, free, strdup
*/
#ifndef UMLWRAP_MALLOC_H
#define UMLWRAP_MALLOC_H
#include <stddef.h>
/*
* smalloc should guarantee to return a useful pointer - Halibut
* can do nothing except die when it's out of memory anyway.
*/
void *smalloc(size_t size);
/*
* srealloc should guaranteeably be able to realloc NULL
*/
void *srealloc(void *p, size_t size);
/*
* sfree should guaranteeably deal gracefully with freeing NULL
*/
void sfree(void *p);
/*
* dupstr is like strdup, but with the never-return-NULL property
* of smalloc (and also reliably defined in all environments :-)
*/
char *dupstr(const char *s);
/*
* snew allocates one instance of a given type, and casts the
* result so as to type-check that you're assigning it to the
* right kind of pointer. Protects against allocation bugs
* involving allocating the wrong size of thing.
*/
#define snew(type) \
( (type *) smalloc (sizeof (type)) )
/*
* snewn allocates n instances of a given type, for arrays.
*/
#define snewn(number, type) \
( (type *) smalloc ((number) * sizeof (type)) )
/*
* sresize wraps realloc so that you specify the new number of
* elements and the type of the element, with the same type-
* checking advantages. Also type-checks the input pointer.
*/
#define sresize(array, number, type) \
( (void)sizeof((array)-(type *)0), \
(type *) srealloc ((array), (number) * sizeof (type)) )
#endif /* UMLWRAP_MALLOC_H */

188
contrib/cygtermd/pty.c Normal file
View File

@ -0,0 +1,188 @@
/*
* pty.c - pseudo-terminal handling
*/
#define _XOPEN_SOURCE
#include <features.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <pwd.h>
#include "pty.h"
#include "malloc.h"
static char ptyname[FILENAME_MAX];
int master = -1;
void pty_preinit(void)
{
/*
* Allocate the pty.
*/
master = open("/dev/ptmx", O_RDWR);
if (master < 0) {
perror("/dev/ptmx: open");
exit(1);
}
if (grantpt(master) < 0) {
perror("grantpt");
exit(1);
}
if (unlockpt(master) < 0) {
perror("unlockpt");
exit(1);
}
}
void pty_resize(int w, int h)
{
struct winsize sz;
assert(master >= 0);
sz.ws_row = h;
sz.ws_col = w;
sz.ws_xpixel = sz.ws_ypixel = 0;
ioctl(master, TIOCSWINSZ, &sz);
}
int run_program_in_pty(const struct shell_data *shdata,
char *directory, char **program_args)
{
int slave, pid;
char *fallback_args[2];
assert(master >= 0);
ptyname[FILENAME_MAX-1] = '\0';
strncpy(ptyname, ptsname(master), FILENAME_MAX-1);
#if 0
{
struct winsize ws;
struct termios ts;
/*
* FIXME: think up some good defaults here
*/
if (!ioctl(0, TIOCGWINSZ, &ws))
ioctl(master, TIOCSWINSZ, &ws);
if (!tcgetattr(0, &ts))
tcsetattr(master, TCSANOW, &ts);
}
#endif
slave = open(ptyname, O_RDWR | O_NOCTTY);
if (slave < 0) {
perror("slave pty: open");
return 1;
}
/*
* Fork and execute the command.
*/
pid = fork();
if (pid < 0) {
perror("fork");
return 1;
}
if (pid == 0) {
int i, fd;
/*
* We are the child.
*/
close(master);
fcntl(slave, F_SETFD, 0); /* don't close on exec */
dup2(slave, 0);
dup2(slave, 1);
if (slave != 0 && slave != 1)
close(slave);
dup2(1, 2);
setsid();
setpgrp();
i = 0;
#ifdef TIOCNOTTY
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
ioctl(fd, TIOCNOTTY, &i);
close(fd);
}
#endif
#ifdef TIOCSCTTY
ioctl(0, TIOCSCTTY, &i);
#endif
tcsetpgrp(0, getpgrp());
for (i = 0; i < shdata->nenvvars; i++)
putenv(shdata->envvars[i]);
if (shdata->termtype)
putenv(shdata->termtype);
if (directory)
chdir(directory);
/*
* Use the provided shell program name, if the user gave
* one. Failing that, use $SHELL; failing that, look up
* the user's default shell in the password file; failing
* _that_, revert to the bog-standard /bin/sh.
*/
if (!program_args) {
char *shell;
shell = getenv("SHELL");
if (!shell) {
const char *login;
uid_t uid;
struct passwd *pwd;
/*
* For maximum generality in the face of multiple
* /etc/passwd entries with different login names and
* shells but a shared uid, we start by using
* getpwnam(getlogin()) if it's available - but we
* insist that its uid must match our real one, or we
* give up and fall back to getpwuid(getuid()).
*/
uid = getuid();
login = getlogin();
if (login && (pwd = getpwnam(login)) && pwd->pw_uid == uid)
shell = pwd->pw_shell;
else if ((pwd = getpwuid(uid)))
shell = pwd->pw_shell;
}
if (!shell)
shell = "/bin/sh";
fallback_args[0] = shell;
fallback_args[1] = NULL;
program_args = fallback_args;
}
execv(program_args[0], program_args);
/*
* If we're here, exec has gone badly foom.
*/
perror("exec");
exit(127);
}
close(slave);
return master;
}

28
contrib/cygtermd/pty.h Normal file
View File

@ -0,0 +1,28 @@
/*
* pty.h - FIXME
*/
#ifndef FIXME_PTY_H
#define FIXME_PTY_H
#include "telnet.h" /* for struct shdata */
/*
* Called at program startup to actually allocate a pty, so that
* we can start passing in resize events as soon as they arrive.
*/
void pty_preinit(void);
/*
* Set the terminal size for the pty.
*/
void pty_resize(int w, int h);
/*
* Start a program in a subprocess running in the pty we allocated.
* Returns the fd of the pty master.
*/
int run_program_in_pty(const struct shell_data *shdata,
char *directory, char **program_args);
#endif /* FIXME_PTY_H */

386
contrib/cygtermd/sel.c Normal file
View File

@ -0,0 +1,386 @@
/*
* sel.c: implementation of sel.h.
*/
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include "sel.h"
#include "malloc.h"
/* ----------------------------------------------------------------------
* Chunk of code lifted from PuTTY's misc.c to manage buffers of
* data to be written to an fd.
*/
#define BUFFER_GRANULE 512
typedef struct bufchain_tag {
struct bufchain_granule *head, *tail;
size_t buffersize; /* current amount of buffered data */
} bufchain;
struct bufchain_granule {
struct bufchain_granule *next;
size_t buflen, bufpos;
char buf[BUFFER_GRANULE];
};
static void bufchain_init(bufchain *ch)
{
ch->head = ch->tail = NULL;
ch->buffersize = 0;
}
static void bufchain_clear(bufchain *ch)
{
struct bufchain_granule *b;
while (ch->head) {
b = ch->head;
ch->head = ch->head->next;
sfree(b);
}
ch->tail = NULL;
ch->buffersize = 0;
}
static size_t bufchain_size(bufchain *ch)
{
return ch->buffersize;
}
static void bufchain_add(bufchain *ch, const void *data, size_t len)
{
const char *buf = (const char *)data;
if (len == 0) return;
ch->buffersize += len;
if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) {
size_t copylen = BUFFER_GRANULE - ch->tail->buflen;
if (copylen > len)
copylen = len;
memcpy(ch->tail->buf + ch->tail->buflen, buf, copylen);
buf += copylen;
len -= copylen;
ch->tail->buflen += copylen;
}
while (len > 0) {
struct bufchain_granule *newbuf;
size_t grainlen = BUFFER_GRANULE;
if (grainlen > len)
grainlen = len;
newbuf = snew(struct bufchain_granule);
newbuf->bufpos = 0;
newbuf->buflen = grainlen;
memcpy(newbuf->buf, buf, grainlen);
buf += grainlen;
len -= grainlen;
if (ch->tail)
ch->tail->next = newbuf;
else
ch->head = ch->tail = newbuf;
newbuf->next = NULL;
ch->tail = newbuf;
}
}
static void bufchain_consume(bufchain *ch, size_t len)
{
struct bufchain_granule *tmp;
assert(ch->buffersize >= len);
while (len > 0) {
size_t remlen = len;
assert(ch->head != NULL);
if (remlen >= ch->head->buflen - ch->head->bufpos) {
remlen = ch->head->buflen - ch->head->bufpos;
tmp = ch->head;
ch->head = tmp->next;
sfree(tmp);
if (!ch->head)
ch->tail = NULL;
} else
ch->head->bufpos += remlen;
ch->buffersize -= remlen;
len -= remlen;
}
}
static void bufchain_prefix(bufchain *ch, void **data, size_t *len)
{
*len = ch->head->buflen - ch->head->bufpos;
*data = ch->head->buf + ch->head->bufpos;
}
/* ----------------------------------------------------------------------
* The actual implementation of the sel interface.
*/
struct sel {
void *ctx;
sel_rfd *rhead, *rtail;
sel_wfd *whead, *wtail;
};
struct sel_rfd {
sel *parent;
sel_rfd *prev, *next;
sel_readdata_fn_t readdata;
sel_readerr_fn_t readerr;
void *ctx;
int fd;
int frozen;
};
struct sel_wfd {
sel *parent;
sel_wfd *prev, *next;
sel_written_fn_t written;
sel_writeerr_fn_t writeerr;
void *ctx;
int fd;
bufchain buf;
};
sel *sel_new(void *ctx)
{
sel *sel = snew(struct sel);
sel->ctx = ctx;
sel->rhead = sel->rtail = NULL;
sel->whead = sel->wtail = NULL;
return sel;
}
sel_wfd *sel_wfd_add(sel *sel, int fd,
sel_written_fn_t written, sel_writeerr_fn_t writeerr,
void *ctx)
{
sel_wfd *wfd = snew(sel_wfd);
wfd->written = written;
wfd->writeerr = writeerr;
wfd->ctx = ctx;
wfd->fd = fd;
bufchain_init(&wfd->buf);
wfd->next = NULL;
wfd->prev = sel->wtail;
if (sel->wtail)
sel->wtail->next = wfd;
else
sel->whead = wfd;
sel->wtail = wfd;
wfd->parent = sel;
return wfd;
}
sel_rfd *sel_rfd_add(sel *sel, int fd,
sel_readdata_fn_t readdata, sel_readerr_fn_t readerr,
void *ctx)
{
sel_rfd *rfd = snew(sel_rfd);
rfd->readdata = readdata;
rfd->readerr = readerr;
rfd->ctx = ctx;
rfd->fd = fd;
rfd->frozen = 0;
rfd->next = NULL;
rfd->prev = sel->rtail;
if (sel->rtail)
sel->rtail->next = rfd;
else
sel->rhead = rfd;
sel->rtail = rfd;
rfd->parent = sel;
return rfd;
}
size_t sel_write(sel_wfd *wfd, const void *data, size_t len)
{
bufchain_add(&wfd->buf, data, len);
return bufchain_size(&wfd->buf);
}
void sel_wfd_setfd(sel_wfd *wfd, int fd)
{
wfd->fd = fd;
}
void sel_rfd_setfd(sel_rfd *rfd, int fd)
{
rfd->fd = fd;
}
void sel_rfd_freeze(sel_rfd *rfd)
{
rfd->frozen = 1;
}
void sel_rfd_unfreeze(sel_rfd *rfd)
{
rfd->frozen = 0;
}
int sel_wfd_delete(sel_wfd *wfd)
{
sel *sel = wfd->parent;
int ret;
if (wfd->prev)
wfd->prev->next = wfd->next;
else
sel->whead = wfd->next;
if (wfd->next)
wfd->next->prev = wfd->prev;
else
sel->wtail = wfd->prev;
bufchain_clear(&wfd->buf);
ret = wfd->fd;
sfree(wfd);
return ret;
}
int sel_rfd_delete(sel_rfd *rfd)
{
sel *sel = rfd->parent;
int ret;
if (rfd->prev)
rfd->prev->next = rfd->next;
else
sel->rhead = rfd->next;
if (rfd->next)
rfd->next->prev = rfd->prev;
else
sel->rtail = rfd->prev;
ret = rfd->fd;
sfree(rfd);
return ret;
}
void sel_free(sel *sel)
{
while (sel->whead)
sel_wfd_delete(sel->whead);
while (sel->rhead)
sel_rfd_delete(sel->rhead);
sfree(sel);
}
void *sel_get_ctx(sel *sel) { return sel->ctx; }
void sel_set_ctx(sel *sel, void *ctx) { sel->ctx = ctx; }
void *sel_wfd_get_ctx(sel_wfd *wfd) { return wfd->ctx; }
void sel_wfd_set_ctx(sel_wfd *wfd, void *ctx) { wfd->ctx = ctx; }
void *sel_rfd_get_ctx(sel_rfd *rfd) { return rfd->ctx; }
void sel_rfd_set_ctx(sel_rfd *rfd, void *ctx) { rfd->ctx = ctx; }
int sel_iterate(sel *sel, long timeout)
{
sel_rfd *rfd;
sel_wfd *wfd;
fd_set rset, wset;
int maxfd = 0;
struct timeval tv, *ptv;
char buf[65536];
int ret;
FD_ZERO(&rset);
FD_ZERO(&wset);
for (rfd = sel->rhead; rfd; rfd = rfd->next) {
if (rfd->fd >= 0 && !rfd->frozen) {
FD_SET(rfd->fd, &rset);
if (maxfd < rfd->fd + 1)
maxfd = rfd->fd + 1;
}
}
for (wfd = sel->whead; wfd; wfd = wfd->next) {
if (wfd->fd >= 0 && bufchain_size(&wfd->buf)) {
FD_SET(wfd->fd, &wset);
if (maxfd < wfd->fd + 1)
maxfd = wfd->fd + 1;
}
}
if (timeout < 0) {
ptv = NULL;
} else {
ptv = &tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = 1000 * (timeout % 1000);
}
do {
ret = select(maxfd, &rset, &wset, NULL, ptv);
} while (ret < 0 && (errno == EINTR || errno == EAGAIN));
if (ret < 0)
return errno;
/*
* Just in case one of the callbacks destroys an rfd or wfd we
* had yet to get round to, we must loop from the start every
* single time. Algorithmically irritating, but necessary
* unless we want to store the rfd structures in a heavyweight
* tree sorted by fd. And let's face it, if we cared about
* good algorithmic complexity it's not at all clear we'd be
* using select in the first place.
*/
do {
for (wfd = sel->whead; wfd; wfd = wfd->next)
if (wfd->fd >= 0 && FD_ISSET(wfd->fd, &wset)) {
void *data;
size_t len;
FD_CLR(wfd->fd, &wset);
bufchain_prefix(&wfd->buf, &data, &len);
ret = write(wfd->fd, data, len);
assert(ret != 0);
if (ret < 0) {
if (wfd->writeerr)
wfd->writeerr(wfd, errno);
} else {
bufchain_consume(&wfd->buf, len);
if (wfd->written)
wfd->written(wfd, bufchain_size(&wfd->buf));
}
break;
}
} while (wfd);
do {
for (rfd = sel->rhead; rfd; rfd = rfd->next)
if (rfd->fd >= 0 && !rfd->frozen && FD_ISSET(rfd->fd, &rset)) {
FD_CLR(rfd->fd, &rset);
ret = read(rfd->fd, buf, sizeof(buf));
if (ret < 0) {
if (rfd->readerr)
rfd->readerr(rfd, errno);
} else {
if (rfd->readdata)
rfd->readdata(rfd, buf, ret);
}
break;
}
} while (rfd);
return 0;
}

161
contrib/cygtermd/sel.h Normal file
View File

@ -0,0 +1,161 @@
/*
* sel.h: subsystem to manage the grubby details of a select loop,
* buffering data to write, and performing the actual writes and
* reads.
*/
#ifndef FIXME_SEL_H
#define FIXME_SEL_H
typedef struct sel sel;
typedef struct sel_wfd sel_wfd;
typedef struct sel_rfd sel_rfd;
/*
* Callback called when some data is written to a wfd. "bufsize"
* is the remaining quantity of data buffered in that wfd.
*/
typedef void (*sel_written_fn_t)(sel_wfd *wfd, size_t bufsize);
/*
* Callback called when an error occurs on a wfd, preventing
* further writing to it. "error" is the errno value.
*/
typedef void (*sel_writeerr_fn_t)(sel_wfd *wfd, int error);
/*
* Callback called when some data is read from an rfd. On EOF,
* this will be called with len==0.
*/
typedef void (*sel_readdata_fn_t)(sel_rfd *rfd, void *data, size_t len);
/*
* Callback called when an error occurs on an rfd, preventing
* further reading from it. "error" is the errno value.
*/
typedef void (*sel_readerr_fn_t)(sel_rfd *rfd, int error);
/*
* Create a sel structure, which will oversee a select loop.
*
* "ctx" is user-supplied data stored in the sel structure; it can
* be read and written with sel_get_ctx() and sel_set_ctx().
*/
sel *sel_new(void *ctx);
/*
* Add a new fd for writing. Returns a sel_wfd which identifies
* that fd in the sel structure, e.g. for putting data into its
* output buffer.
*
* "ctx" is user-supplied data stored in the sel structure; it can
* be read and written with sel_wfd_get_ctx() and sel_wfd_set_ctx().
*
* "written" and "writeerr" are called from the event loop when
* things happen.
*
* The fd passed in can be -1, in which case it will be assumed to
* be unwritable at all times. An actual fd can be passed in later
* using sel_wfd_setfd.
*/
sel_wfd *sel_wfd_add(sel *sel, int fd,
sel_written_fn_t written, sel_writeerr_fn_t writeerr,
void *ctx);
/*
* Add a new fd for reading. Returns a sel_rfd which identifies
* that fd in the sel structure.
*
* "ctx" is user-supplied data stored in the sel structure; it can
* be read and written with sel_rfd_get_ctx() and sel_rfd_set_ctx().
*
* "readdata" and "readerr" are called from the event loop when
* things happen. "ctx" is passed to both of them.
*/
sel_rfd *sel_rfd_add(sel *sel, int fd,
sel_readdata_fn_t readdata, sel_readerr_fn_t readerr,
void *ctx);
/*
* Write data into the output buffer of a wfd. Returns the new
* size of the output buffer. (You can call it with len==0 if you
* just want to know the buffer size; in that situation data==NULL
* is also safe.)
*/
size_t sel_write(sel_wfd *wfd, const void *data, size_t len);
/*
* Freeze and unfreeze an rfd. When frozen, sel will temporarily
* not attempt to read from it, but all its state is retained so
* it can be conveniently unfrozen later. (You might use this
* facility, for instance, if what you were doing with the
* incoming data could only accept it at a certain rate: freeze
* the rfd when you've got lots of backlog, and unfreeze it again
* when things get calmer.)
*/
void sel_rfd_freeze(sel_rfd *rfd);
void sel_rfd_unfreeze(sel_rfd *rfd);
/*
* Delete a wfd structure from its containing sel. Returns the
* underlying fd, which the client may now consider itself to own
* once more.
*/
int sel_wfd_delete(sel_wfd *wfd);
/*
* Delete an rfd structure from its containing sel. Returns the
* underlying fd, which the client may now consider itself to own
* once more.
*/
int sel_rfd_delete(sel_rfd *rfd);
/*
* NOT IMPLEMENTED YET: useful functions here might be ones which
* enumerated all the wfds/rfds in a sel structure in some
* fashion, so you could go through them and remove them all while
* doing sensible things to them. Or, at the very least, just
* return an arbitrary one of the wfds/rfds.
*/
/*
* Free a sel structure and all its remaining wfds and rfds.
*/
void sel_free(sel *sel);
/*
* Read and write the ctx parameters in sel, sel_wfd and sel_rfd.
*/
void *sel_get_ctx(sel *sel);
void sel_set_ctx(sel *sel, void *ctx);
void *sel_wfd_get_ctx(sel_wfd *wfd);
void sel_wfd_set_ctx(sel_wfd *wfd, void *ctx);
void *sel_rfd_get_ctx(sel_rfd *rfd);
void sel_rfd_set_ctx(sel_rfd *rfd, void *ctx);
/*
* Run one iteration of the sel event loop, calling callbacks as
* necessary. Returns zero on success; in the event of a fatal
* error, returns the errno value.
*
* "timeout" is a value in microseconds to limit the length of the
* select call. Less than zero means to wait indefinitely.
*/
int sel_iterate(sel *sel, long timeout);
/*
* Change the underlying fd in a wfd. If set to -1, no write
* attempts will take place and the wfd's buffer will simply store
* everything passed to sel_write(). If later set to something
* other than -1, all that buffered data will become eligible for
* real writing.
*/
void sel_wfd_setfd(sel_wfd *wfd, int fd);
/*
* Change the underlying fd in a rfd. If set to -1, no read
* attempts will take place.
*/
void sel_rfd_setfd(sel_rfd *rfd, int fd);
#endif /* FIXME_SEL_H */

570
contrib/cygtermd/telnet.c Normal file
View File

@ -0,0 +1,570 @@
/*
* Simple Telnet server code, adapted from PuTTY's own Telnet
* client code for use as a Cygwin local pty proxy.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sel.h"
#include "telnet.h"
#include "malloc.h"
#include "pty.h"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#define IAC 255 /* interpret as command: */
#define DONT 254 /* you are not to use option */
#define DO 253 /* please, you use option */
#define WONT 252 /* I won't use option */
#define WILL 251 /* I will use option */
#define SB 250 /* interpret as subnegotiation */
#define SE 240 /* end sub negotiation */
#define GA 249 /* you may reverse the line */
#define EL 248 /* erase the current line */
#define EC 247 /* erase the current character */
#define AYT 246 /* are you there */
#define AO 245 /* abort output--but let prog finish */
#define IP 244 /* interrupt process--permanently */
#define BREAK 243 /* break */
#define DM 242 /* data mark--for connect. cleaning */
#define NOP 241 /* nop */
#define EOR 239 /* end of record (transparent mode) */
#define ABORT 238 /* Abort process */
#define SUSP 237 /* Suspend process */
#define xEOF 236 /* End of file: EOF is already used... */
#define TELOPTS(X) \
X(BINARY, 0) /* 8-bit data path */ \
X(ECHO, 1) /* echo */ \
X(RCP, 2) /* prepare to reconnect */ \
X(SGA, 3) /* suppress go ahead */ \
X(NAMS, 4) /* approximate message size */ \
X(STATUS, 5) /* give status */ \
X(TM, 6) /* timing mark */ \
X(RCTE, 7) /* remote controlled transmission and echo */ \
X(NAOL, 8) /* negotiate about output line width */ \
X(NAOP, 9) /* negotiate about output page size */ \
X(NAOCRD, 10) /* negotiate about CR disposition */ \
X(NAOHTS, 11) /* negotiate about horizontal tabstops */ \
X(NAOHTD, 12) /* negotiate about horizontal tab disposition */ \
X(NAOFFD, 13) /* negotiate about formfeed disposition */ \
X(NAOVTS, 14) /* negotiate about vertical tab stops */ \
X(NAOVTD, 15) /* negotiate about vertical tab disposition */ \
X(NAOLFD, 16) /* negotiate about output LF disposition */ \
X(XASCII, 17) /* extended ascic character set */ \
X(LOGOUT, 18) /* force logout */ \
X(BM, 19) /* byte macro */ \
X(DET, 20) /* data entry terminal */ \
X(SUPDUP, 21) /* supdup protocol */ \
X(SUPDUPOUTPUT, 22) /* supdup output */ \
X(SNDLOC, 23) /* send location */ \
X(TTYPE, 24) /* terminal type */ \
X(EOR, 25) /* end or record */ \
X(TUID, 26) /* TACACS user identification */ \
X(OUTMRK, 27) /* output marking */ \
X(TTYLOC, 28) /* terminal location number */ \
X(3270REGIME, 29) /* 3270 regime */ \
X(X3PAD, 30) /* X.3 PAD */ \
X(NAWS, 31) /* window size */ \
X(TSPEED, 32) /* terminal speed */ \
X(LFLOW, 33) /* remote flow control */ \
X(LINEMODE, 34) /* Linemode option */ \
X(XDISPLOC, 35) /* X Display Location */ \
X(OLD_ENVIRON, 36) /* Old - Environment variables */ \
X(AUTHENTICATION, 37) /* Authenticate */ \
X(ENCRYPT, 38) /* Encryption option */ \
X(NEW_ENVIRON, 39) /* New - Environment variables */ \
X(TN3270E, 40) /* TN3270 enhancements */ \
X(XAUTH, 41) \
X(CHARSET, 42) /* Character set */ \
X(RSP, 43) /* Remote serial port */ \
X(COM_PORT_OPTION, 44) /* Com port control */ \
X(SLE, 45) /* Suppress local echo */ \
X(STARTTLS, 46) /* Start TLS */ \
X(KERMIT, 47) /* Automatic Kermit file transfer */ \
X(SEND_URL, 48) \
X(FORWARD_X, 49) \
X(PRAGMA_LOGON, 138) \
X(SSPI_LOGON, 139) \
X(PRAGMA_HEARTBEAT, 140) \
X(EXOPL, 255) /* extended-options-list */
#define telnet_enum(x,y) TELOPT_##x = y,
enum { TELOPTS(telnet_enum) dummy=0 };
#undef telnet_enum
#define TELQUAL_IS 0 /* option is... */
#define TELQUAL_SEND 1 /* send option */
#define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
#define BSD_VAR 1
#define BSD_VALUE 0
#define RFC_VAR 0
#define RFC_VALUE 1
#define CR 13
#define LF 10
#define NUL 0
#define iswritable(x) ( (x) != IAC && (x) != CR )
static char *telopt(int opt)
{
#define telnet_str(x,y) case TELOPT_##x: return #x;
switch (opt) {
TELOPTS(telnet_str)
default:
return "<unknown>";
}
#undef telnet_str
}
static void telnet_size(void *handle, int width, int height);
struct Opt {
int send; /* what we initially send */
int nsend; /* -ve send if requested to stop it */
int ack, nak; /* +ve and -ve acknowledgements */
int option; /* the option code */
int index; /* index into telnet->opt_states[] */
enum {
REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
} initial_state;
};
enum {
OPTINDEX_NAWS,
OPTINDEX_TSPEED,
OPTINDEX_TTYPE,
OPTINDEX_OENV,
OPTINDEX_NENV,
OPTINDEX_ECHO,
OPTINDEX_WE_SGA,
OPTINDEX_THEY_SGA,
OPTINDEX_WE_BIN,
OPTINDEX_THEY_BIN,
NUM_OPTS
};
static const struct Opt o_naws =
{ DO, DONT, WILL, WONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED };
static const struct Opt o_ttype =
{ DO, DONT, WILL, WONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED };
static const struct Opt o_oenv =
{ DO, DONT, WILL, WONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE };
static const struct Opt o_nenv =
{ DO, DONT, WILL, WONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED };
static const struct Opt o_echo =
{ WILL, WONT, DO, DONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED };
static const struct Opt o_they_sga =
{ DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED };
static const struct Opt o_we_sga =
{ WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED };
static const struct Opt *const opts[] = {
&o_echo, &o_we_sga, &o_they_sga, &o_naws, &o_ttype, &o_oenv, &o_nenv, NULL
};
struct telnet_tag {
int opt_states[NUM_OPTS];
int sb_opt, sb_len;
unsigned char *sb_buf;
int sb_size;
enum {
TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
} state;
sel_wfd *net, *pty;
/*
* Options we must finish processing before launching the shell
*/
int old_environ_done, new_environ_done, ttype_done;
/*
* Ready to start shell?
*/
int shell_ok;
int envvarsize;
struct shell_data shdata;
};
#define TELNET_MAX_BACKLOG 4096
#define SB_DELTA 1024
static void send_opt(Telnet telnet, int cmd, int option)
{
unsigned char b[3];
b[0] = IAC;
b[1] = cmd;
b[2] = option;
sel_write(telnet->net, (char *)b, 3);
}
static void deactivate_option(Telnet telnet, const struct Opt *o)
{
if (telnet->opt_states[o->index] == REQUESTED ||
telnet->opt_states[o->index] == ACTIVE)
send_opt(telnet, o->nsend, o->option);
telnet->opt_states[o->index] = REALLY_INACTIVE;
}
/*
* Generate side effects of enabling or disabling an option.
*/
static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled)
{
}
static void activate_option(Telnet telnet, const struct Opt *o)
{
if (o->option == TELOPT_NEW_ENVIRON ||
o->option == TELOPT_OLD_ENVIRON ||
o->option == TELOPT_TTYPE) {
char buf[6];
buf[0] = IAC;
buf[1] = SB;
buf[2] = o->option;
buf[3] = TELQUAL_SEND;
buf[4] = IAC;
buf[5] = SE;
sel_write(telnet->net, buf, 6);
}
option_side_effects(telnet, o, 1);
}
static void done_option(Telnet telnet, int option)
{
if (option == TELOPT_OLD_ENVIRON)
telnet->old_environ_done = 1;
else if (option == TELOPT_NEW_ENVIRON)
telnet->new_environ_done = 1;
else if (option == TELOPT_TTYPE)
telnet->ttype_done = 1;
if (telnet->old_environ_done && telnet->new_environ_done &&
telnet->ttype_done) {
telnet->shell_ok = 1;
}
}
static void refused_option(Telnet telnet, const struct Opt *o)
{
done_option(telnet, o->option);
if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
telnet->opt_states[o_oenv.index] == INACTIVE) {
send_opt(telnet, WILL, TELOPT_OLD_ENVIRON);
telnet->opt_states[o_oenv.index] = REQUESTED;
telnet->old_environ_done = 0;
}
option_side_effects(telnet, o, 0);
}
static void proc_rec_opt(Telnet telnet, int cmd, int option)
{
const struct Opt *const *o;
for (o = opts; *o; o++) {
if ((*o)->option == option && (*o)->ack == cmd) {
switch (telnet->opt_states[(*o)->index]) {
case REQUESTED:
telnet->opt_states[(*o)->index] = ACTIVE;
activate_option(telnet, *o);
break;
case ACTIVE:
break;
case INACTIVE:
telnet->opt_states[(*o)->index] = ACTIVE;
send_opt(telnet, (*o)->send, option);
activate_option(telnet, *o);
break;
case REALLY_INACTIVE:
send_opt(telnet, (*o)->nsend, option);
break;
}
return;
} else if ((*o)->option == option && (*o)->nak == cmd) {
switch (telnet->opt_states[(*o)->index]) {
case REQUESTED:
telnet->opt_states[(*o)->index] = INACTIVE;
refused_option(telnet, *o);
break;
case ACTIVE:
telnet->opt_states[(*o)->index] = INACTIVE;
send_opt(telnet, (*o)->nsend, option);
option_side_effects(telnet, *o, 0);
break;
case INACTIVE:
case REALLY_INACTIVE:
break;
}
return;
}
}
/*
* If we reach here, the option was one we weren't prepared to
* cope with. If the request was positive (WILL or DO), we send
* a negative ack to indicate refusal. If the request was
* negative (WONT / DONT), we must do nothing.
*/
if (cmd == WILL || cmd == DO)
send_opt(telnet, (cmd == WILL ? DONT : WONT), option);
}
static void process_subneg(Telnet telnet)
{
unsigned char b[2048], *p, *q;
int var, value, n;
char *e;
switch (telnet->sb_opt) {
case TELOPT_OLD_ENVIRON:
case TELOPT_NEW_ENVIRON:
if (telnet->sb_buf[0] == TELQUAL_IS) {
if (telnet->sb_opt == TELOPT_NEW_ENVIRON) {
var = RFC_VAR;
value = RFC_VALUE;
} else {
if (telnet->sb_len > 1 && !(telnet->sb_buf[0] &~ 1)) {
var = telnet->sb_buf[0];
value = BSD_VAR ^ BSD_VALUE ^ var;
} else {
var = BSD_VAR;
value = BSD_VALUE;
}
}
}
n = 1;
while (n < telnet->sb_len && telnet->sb_buf[n] == var) {
int varpos, varlen, valpos, vallen;
char *result;
varpos = ++n;
while (n < telnet->sb_len && telnet->sb_buf[n] != value)
n++;
if (n == telnet->sb_len)
break;
varlen = n - varpos;
valpos = ++n;
while (n < telnet->sb_len && telnet->sb_buf[n] != var)
n++;
vallen = n - valpos;
result = snewn(varlen + vallen + 2, char);
sprintf(result, "%.*s=%.*s",
varlen, telnet->sb_buf+varpos,
vallen, telnet->sb_buf+valpos);
if (telnet->shdata.nenvvars >= telnet->envvarsize) {
telnet->envvarsize = telnet->shdata.nenvvars * 3 / 2 + 16;
telnet->shdata.envvars = sresize(telnet->shdata.envvars,
telnet->envvarsize, char *);
}
telnet->shdata.envvars[telnet->shdata.nenvvars++] = result;
}
done_option(telnet, telnet->sb_opt);
break;
case TELOPT_TTYPE:
if (telnet->sb_len >= 1 && telnet->sb_buf[0] == TELQUAL_IS) {
telnet->shdata.termtype = snewn(5 + telnet->sb_len, char);
strcpy(telnet->shdata.termtype, "TERM=");
for (n = 0; n < telnet->sb_len-1; n++) {
char c = telnet->sb_buf[n+1];
if (c >= 'A' && c <= 'Z')
c = c + 'a' - 'A';
telnet->shdata.termtype[n+5] = c;
}
telnet->shdata.termtype[telnet->sb_len+5-1] = '\0';
}
done_option(telnet, telnet->sb_opt);
break;
case TELOPT_NAWS:
if (telnet->sb_len == 4) {
int w, h;
w = (unsigned char)telnet->sb_buf[0];
w = (w << 8) | (unsigned char)telnet->sb_buf[1];
h = (unsigned char)telnet->sb_buf[2];
h = (h << 8) | (unsigned char)telnet->sb_buf[3];
pty_resize(w, h);
}
break;
}
}
void telnet_from_net(Telnet telnet, char *buf, int len)
{
while (len--) {
int c = (unsigned char) *buf++;
switch (telnet->state) {
case TOP_LEVEL:
case SEENCR:
/*
* PuTTY sends Telnet's new line sequence (CR LF on
* the wire) in response to the return key. We must
* therefore treat that as equivalent to CR NUL, and
* send CR to the pty.
*/
if ((c == NUL || c == '\n') && telnet->state == SEENCR)
telnet->state = TOP_LEVEL;
else if (c == IAC)
telnet->state = SEENIAC;
else {
char cc = c;
sel_write(telnet->pty, &cc, 1);
telnet->state = SEENCR;
}
break;
case SEENIAC:
if (c == DO)
telnet->state = SEENDO;
else if (c == DONT)
telnet->state = SEENDONT;
else if (c == WILL)
telnet->state = SEENWILL;
else if (c == WONT)
telnet->state = SEENWONT;
else if (c == SB)
telnet->state = SEENSB;
else if (c == DM)
telnet->state = TOP_LEVEL;
else {
/* ignore everything else; print it if it's IAC */
if (c == IAC) {
char cc = c;
sel_write(telnet->pty, &cc, 1);
}
telnet->state = TOP_LEVEL;
}
break;
case SEENWILL:
proc_rec_opt(telnet, WILL, c);
telnet->state = TOP_LEVEL;
break;
case SEENWONT:
proc_rec_opt(telnet, WONT, c);
telnet->state = TOP_LEVEL;
break;
case SEENDO:
proc_rec_opt(telnet, DO, c);
telnet->state = TOP_LEVEL;
break;
case SEENDONT:
proc_rec_opt(telnet, DONT, c);
telnet->state = TOP_LEVEL;
break;
case SEENSB:
telnet->sb_opt = c;
telnet->sb_len = 0;
telnet->state = SUBNEGOT;
break;
case SUBNEGOT:
if (c == IAC)
telnet->state = SUBNEG_IAC;
else {
subneg_addchar:
if (telnet->sb_len >= telnet->sb_size) {
telnet->sb_size += SB_DELTA;
telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size,
unsigned char);
}
telnet->sb_buf[telnet->sb_len++] = c;
telnet->state = SUBNEGOT; /* in case we came here by goto */
}
break;
case SUBNEG_IAC:
if (c != SE)
goto subneg_addchar; /* yes, it's a hack, I know, but... */
else {
process_subneg(telnet);
telnet->state = TOP_LEVEL;
}
break;
}
}
}
Telnet telnet_new(sel_wfd *net, sel_wfd *pty)
{
Telnet telnet;
telnet = snew(struct telnet_tag);
telnet->sb_buf = NULL;
telnet->sb_size = 0;
telnet->state = TOP_LEVEL;
telnet->net = net;
telnet->pty = pty;
telnet->shdata.envvars = NULL;
telnet->shdata.nenvvars = telnet->envvarsize = 0;
telnet->shdata.termtype = NULL;
/*
* Initialise option states.
*/
{
const struct Opt *const *o;
for (o = opts; *o; o++) {
telnet->opt_states[(*o)->index] = (*o)->initial_state;
if (telnet->opt_states[(*o)->index] == REQUESTED)
send_opt(telnet, (*o)->send, (*o)->option);
}
}
telnet->old_environ_done = 1; /* initially don't want to bother */
telnet->new_environ_done = 0;
telnet->ttype_done = 0;
telnet->shell_ok = 0;
return telnet;
}
void telnet_free(Telnet telnet)
{
sfree(telnet->sb_buf);
sfree(telnet);
}
void telnet_from_pty(Telnet telnet, char *buf, int len)
{
unsigned char *p, *end;
static const unsigned char iac[2] = { IAC, IAC };
static const unsigned char cr[2] = { CR, NUL };
#if 0
static const unsigned char nl[2] = { CR, LF };
#endif
p = (unsigned char *)buf;
end = (unsigned char *)(buf + len);
while (p < end) {
unsigned char *q = p;
while (p < end && iswritable(*p))
p++;
sel_write(telnet->net, (char *)q, p - q);
while (p < end && !iswritable(*p)) {
sel_write(telnet->net, (char *)(*p == IAC ? iac : cr), 2);
p++;
}
}
}
int telnet_shell_ok(Telnet telnet, struct shell_data *shdata)
{
if (telnet->shell_ok)
*shdata = telnet->shdata; /* structure copy */
return telnet->shell_ok;
}

41
contrib/cygtermd/telnet.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Header declaring Telnet-handling functions.
*/
#ifndef FIXME_TELNET_H
#define FIXME_TELNET_H
#include "sel.h"
typedef struct telnet_tag *Telnet;
struct shell_data {
char **envvars; /* array of "VAR=value" terms */
int nenvvars;
char *termtype;
};
/*
* Create and destroy a Telnet structure.
*/
Telnet telnet_new(sel_wfd *net, sel_wfd *pty);
void telnet_free(Telnet telnet);
/*
* Process data read from the pty.
*/
void telnet_from_pty(Telnet telnet, char *buf, int len);
/*
* Process Telnet protocol data received from the network.
*/
void telnet_from_net(Telnet telnet, char *buf, int len);
/*
* Return true if pre-shell-startup negotiations are complete and
* it's safe to start the shell subprocess now. On a true return,
* also fills in the shell_data structure.
*/
int telnet_shell_ok(Telnet telnet, struct shell_data *shdata);
#endif /* FIXME_TELNET_H */