From 674a60748884dc55ee7091b7c23a41240e75f73c Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 1 Jun 2004 02:46:09 +0000 Subject: Makefile.in contains updated files required --HG-- extra : convert_revision : cc8a8c49dc70e632c352853a39801089b08149be --- svr-chansession.c | 931 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 931 insertions(+) create mode 100644 svr-chansession.c (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c new file mode 100644 index 0000000..dce0827 --- /dev/null +++ b/svr-chansession.c @@ -0,0 +1,931 @@ +/* + * Dropbear - a SSH2 server + * + * Copyright (c) 2002,2003 Matt Johnston + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ + +#include "includes.h" +#include "packet.h" +#include "buffer.h" +#include "session.h" +#include "dbutil.h" +#include "channel.h" +#include "chansession.h" +#include "sshpty.h" +#include "termcodes.h" +#include "ssh.h" +#include "random.h" +#include "utmp.h" +#include "x11fwd.h" +#include "agentfwd.h" + +/* Handles sessions (either shells or programs) requested by the client */ + +static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, + int iscmd, int issubsys); +static int sessionpty(struct ChanSess * chansess); +static int sessionsignal(struct ChanSess *chansess); +static int noptycommand(struct Channel *channel, struct ChanSess *chansess); +static int ptycommand(struct Channel *channel, struct ChanSess *chansess); +static int sessionwinchange(struct ChanSess *chansess); +static void execchild(struct ChanSess *chansess); +static void addchildpid(struct ChanSess *chansess, pid_t pid); +static void sesssigchild_handler(int val); +static void closechansess(struct Channel *channel); +static void newchansess(struct Channel *channel); +static void chansessionrequest(struct Channel *channel); + +static void send_exitsignalstatus(struct Channel *channel); +static int sesscheckclose(struct Channel *channel); + +const struct ChanType svrchansess = { + 0, /* sepfds */ + "session", /* name */ + newchansess, /* inithandler */ + sesscheckclose, /* checkclosehandler */ + chansessionrequest, /* reqhandler */ + closechansess, /* closehandler */ +}; + + + +/* required to clear environment */ +extern char** environ; + +static int sesscheckclose(struct Channel *channel) { + struct ChanSess *chansess = (struct ChanSess*)channel->typedata; + return chansess->exited; +} + +/* Set up the general chansession environment, in particular child-exit + * handling */ +void svr_chansessinitialise() { + + struct sigaction sa_chld; + + /* single child process intially */ + svr_ses.childpids = (struct ChildPid*)m_malloc(sizeof(struct ChildPid)); + svr_ses.childpids[0].pid = -1; /* unused */ + svr_ses.childpids[0].chansess = NULL; + svr_ses.childpidsize = 1; + sa_chld.sa_handler = sesssigchild_handler; + sa_chld.sa_flags = SA_NOCLDSTOP; + if (sigaction(SIGCHLD, &sa_chld, NULL) < 0) { + dropbear_exit("signal() error"); + } + +} + +/* handler for childs exiting, store the state for return to the client */ +static void sesssigchild_handler(int dummy) { + + int status; + pid_t pid; + unsigned int i; + struct ChanSess * chansess; + struct sigaction sa_chld; + + TRACE(("enter sigchld handler")); + while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { + + /* find the corresponding chansess */ + for (i = 0; i < svr_ses.childpidsize; i++) { + if (svr_ses.childpids[i].pid == pid) { + + chansess = svr_ses.childpids[i].chansess; + chansess->exited = 1; + if (WIFEXITED(status)) { + chansess->exitstatus = WEXITSTATUS(status); + } + if (WIFSIGNALED(status)) { + chansess->exitsignal = WTERMSIG(status); +#ifndef AIX + chansess->exitcore = WCOREDUMP(status); +#endif + } else { + /* we use this to determine how pid exited */ + chansess->exitsignal = -1; + } + } + } + } + sa_chld.sa_handler = sesssigchild_handler; + sa_chld.sa_flags = SA_NOCLDSTOP; + sigaction(SIGCHLD, &sa_chld, NULL); + TRACE(("leave sigchld handler")); +} + +/* send the exit status or the signal causing termination for a session */ +/* XXX server */ +static void send_exitsignalstatus(struct Channel *channel) { + + struct ChanSess *chansess = (struct ChanSess*)channel->typedata; + + if (chansess->exited) { + if (chansess->exitsignal > 0) { + send_msg_chansess_exitsignal(channel, chansess); + } else { + send_msg_chansess_exitstatus(channel, chansess); + } + } +} + +/* send the exitstatus to the client */ +static void send_msg_chansess_exitstatus(struct Channel * channel, + struct ChanSess * chansess) { + + assert(chansess->exited); + assert(chansess->exitsignal == -1); + + CHECKCLEARTOWRITE(); + + buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_REQUEST); + buf_putint(ses.writepayload, channel->remotechan); + buf_putstring(ses.writepayload, "exit-status", 11); + buf_putbyte(ses.writepayload, 0); /* boolean FALSE */ + buf_putint(ses.writepayload, chansess->exitstatus); + + encrypt_packet(); + +} + +/* send the signal causing the exit to the client */ +static void send_msg_chansess_exitsignal(struct Channel * channel, + struct ChanSess * chansess) { + + int i; + char* signame = NULL; + + assert(chansess->exited); + assert(chansess->exitsignal > 0); + + CHECKCLEARTOWRITE(); + + /* we check that we can match a signal name, otherwise + * don't send anything */ + for (i = 0; signames[i].name != NULL; i++) { + if (signames[i].signal == chansess->exitsignal) { + signame = signames[i].name; + break; + } + } + + if (signame == NULL) { + return; + } + + buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_REQUEST); + buf_putint(ses.writepayload, channel->remotechan); + buf_putstring(ses.writepayload, "exit-signal", 11); + buf_putbyte(ses.writepayload, 0); /* boolean FALSE */ + buf_putstring(ses.writepayload, signame, strlen(signame)); + buf_putbyte(ses.writepayload, chansess->exitcore); + buf_putstring(ses.writepayload, "", 0); /* error msg */ + buf_putstring(ses.writepayload, "", 0); /* lang */ + + encrypt_packet(); +} + +/* set up a session channel */ +static void newchansess(struct Channel *channel) { + + struct ChanSess *chansess; + + assert(channel->typedata == NULL); + + chansess = (struct ChanSess*)m_malloc(sizeof(struct ChanSess)); + chansess->cmd = NULL; + chansess->pid = 0; + + /* pty details */ + chansess->master = -1; + chansess->slave = -1; + chansess->tty = NULL; + chansess->term = NULL; + chansess->termw = 0; + chansess->termh = 0; + chansess->termc = 0; + chansess->termr = 0; + + chansess->exited = 0; + + channel->typedata = chansess; + +#ifndef DISABLE_X11FWD + chansess->x11fd = -1; + chansess->x11authprot = NULL; + chansess->x11authcookie = NULL; +#endif + +#ifndef DISABLE_AGENTFWD + chansess->agentfd = -1; + chansess->agentfile = NULL; + chansess->agentdir = NULL; +#endif + +} + +/* clean a session channel */ +static void closechansess(struct Channel *channel) { + + struct ChanSess *chansess; + unsigned int i; + struct logininfo *li; + + chansess = (struct ChanSess*)channel->typedata; + + send_exitsignalstatus(chansess); + + TRACE(("enter closechansess")); + if (chansess == NULL) { + TRACE(("leave closechansess: chansess == NULL")); + return; + } + + m_free(chansess->cmd); + m_free(chansess->term); + + if (chansess->tty) { + /* write the utmp/wtmp login record */ + li = login_alloc_entry(chansess->pid, svr_ses.authstate.username, + ses.remotehost, chansess->tty); + login_logout(li); + login_free_entry(li); + + pty_release(chansess->tty); + m_free(chansess->tty); + } + +#ifndef DISABLE_X11FWD + x11cleanup(chansess); +#endif + +#ifndef DISABLE_AGENTFWD + agentcleanup(chansess); +#endif + + /* clear child pid entries */ + for (i = 0; i < svr_ses.childpidsize; i++) { + if (svr_ses.childpids[i].chansess == chansess) { + assert(svr_ses.childpids[i].pid > 0); + TRACE(("closing pid %d", svr_ses.childpids[i].pid)); + TRACE(("exited = %d", chansess->exited)); + svr_ses.childpids[i].pid = -1; + svr_ses.childpids[i].chansess = NULL; + } + } + + m_free(chansess); + + TRACE(("leave closechansess")); +} + +/* Handle requests for a channel. These can be execution requests, + * or x11/authagent forwarding. These are passed to appropriate handlers */ +static void chansessionrequest(struct Channel *channel) { + + unsigned char * type; + unsigned int typelen; + unsigned char wantreply; + int ret = 1; + struct ChanSess *chansess; + + TRACE(("enter chansessionrequest")); + + assert(channel->type == CHANNEL_ID_SESSION); + + type = buf_getstring(ses.payload, &typelen); + wantreply = buf_getbyte(ses.payload); + + if (typelen > MAX_NAME_LEN) { + TRACE(("leave chansessionrequest: type too long")); /* XXX send error?*/ + goto out; + } + + chansess = (struct ChanSess*)channel->typedata; + assert(chansess != NULL); + TRACE(("type is %s", type)); + + if (strcmp(type, "window-change") == 0) { + ret = sessionwinchange(chansess); + } else if (strcmp(type, "shell") == 0) { + ret = sessioncommand(channel, chansess, 0, 0); + } else if (strcmp(type, "pty-req") == 0) { + ret = sessionpty(chansess); + } else if (strcmp(type, "exec") == 0) { + ret = sessioncommand(channel, chansess, 1, 0); + } else if (strcmp(type, "subsystem") == 0) { + ret = sessioncommand(channel, chansess, 1, 1); +#ifndef DISABLE_X11FWD + } else if (strcmp(type, "x11-req") == 0) { + ret = x11req(chansess); +#endif +#ifndef DISABLE_AGENTFWD + } else if (strcmp(type, "auth-agent-req@openssh.com") == 0) { + ret = agentreq(chansess); +#endif + } else if (strcmp(type, "signal") == 0) { + ret = sessionsignal(chansess); + } else { + /* etc, todo "env", "subsystem" */ + } + +out: + + if (wantreply) { + if (ret == 0) { + send_msg_channel_success(channel); + } else { + send_msg_channel_failure(channel); + } + } + + m_free(type); + TRACE(("leave chansessionrequest")); +} + + +/* Send a signal to a session's process as requested by the client*/ +static int sessionsignal(struct ChanSess *chansess) { + + int sig = 0; + unsigned char* signame; + int i; + + if (chansess->pid == 0) { + /* haven't got a process pid yet */ + return DROPBEAR_FAILURE; + } + + signame = buf_getstring(ses.payload, NULL); + + i = 0; + while (signames[i].name != 0) { + if (strcmp(signames[i].name, signame) == 0) { + sig = signames[i].signal; + break; + } + i++; + } + + m_free(signame); + + if (sig == 0) { + /* failed */ + return DROPBEAR_FAILURE; + } + + if (kill(chansess->pid, sig) < 0) { + return DROPBEAR_FAILURE; + } + + return DROPBEAR_SUCCESS; +} + +/* Let the process know that the window size has changed, as notified from the + * client. Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ +static int sessionwinchange(struct ChanSess *chansess) { + + if (chansess->master < 0) { + /* haven't got a pty yet */ + return DROPBEAR_FAILURE; + } + + chansess->termc = buf_getint(ses.payload); + chansess->termr = buf_getint(ses.payload); + chansess->termw = buf_getint(ses.payload); + chansess->termh = buf_getint(ses.payload); + + pty_change_window_size(chansess->master, chansess->termr, chansess->termc, + chansess->termw, chansess->termh); + + return DROPBEAR_FAILURE; +} + +/* Set up a session pty which will be used to execute the shell or program. + * The pty is allocated now, and kept for when the shell/program executes. + * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ +static int sessionpty(struct ChanSess * chansess) { + + unsigned int termlen; + unsigned char namebuf[65]; + struct termios termio; + + TRACE(("enter sessionpty")); + chansess->term = buf_getstring(ses.payload, &termlen); + if (termlen > MAX_TERM_LEN) { + /* TODO send disconnect ? */ + TRACE(("leave sessionpty: term len too long")); + return DROPBEAR_FAILURE; + } + chansess->termc = buf_getint(ses.payload); + chansess->termr = buf_getint(ses.payload); + chansess->termw = buf_getint(ses.payload); + chansess->termh = buf_getint(ses.payload); + + /* allocate the pty */ + assert(chansess->master == -1); /* haven't already got one */ + if (pty_allocate(&chansess->master, &chansess->slave, namebuf, 64) == 0) { + TRACE(("leave sessionpty: failed to allocate pty")); + return DROPBEAR_FAILURE; + } + + chansess->tty = (char*)strdup(namebuf); + if (!chansess->tty) { + dropbear_exit("out of memory"); /* TODO disconnect */ + } + + pty_setowner(svr_ses.authstate.pw, chansess->tty); + pty_change_window_size(chansess->master, chansess->termr, chansess->termc, + chansess->termw, chansess->termh); + + /* Term modes */ + /* We'll ignore errors and continue if we can't set modes. + * We're ignoring baud rates since they seem evil */ + if (tcgetattr(chansess->master, &termio) == 0) { + unsigned char opcode; + unsigned int value; + const struct TermCode * termcode; + unsigned int len; + + len = buf_getint(ses.payload); + if (len != ses.payload->len - ses.payload->pos) { + dropbear_exit("bad term mode string"); + } + + if (len == 0) { + TRACE(("empty terminal modes string")); + return DROPBEAR_SUCCESS; + } + + while (((opcode = buf_getbyte(ses.payload)) != 0x00) && + opcode <= 159) { + + /* must be before checking type, so that value is consumed even if + * we don't use it */ + value = buf_getint(ses.payload); + + /* handle types of code */ + if (opcode > MAX_TERMCODE) { + continue; + } + termcode = &termcodes[(unsigned int)opcode]; + + + switch (termcode->type) { + + case TERMCODE_NONE: + break; + + case TERMCODE_CONTROLCHAR: + termio.c_cc[termcode->mapcode] = value; + break; + + case TERMCODE_INPUT: + if (value) { + termio.c_iflag |= termcode->mapcode; + } else { + termio.c_iflag &= ~(termcode->mapcode); + } + break; + + case TERMCODE_OUTPUT: + if (value) { + termio.c_oflag |= termcode->mapcode; + } else { + termio.c_oflag &= ~(termcode->mapcode); + } + break; + + case TERMCODE_LOCAL: + if (value) { + termio.c_lflag |= termcode->mapcode; + } else { + termio.c_lflag &= ~(termcode->mapcode); + } + break; + + case TERMCODE_CONTROL: + if (value) { + termio.c_cflag |= termcode->mapcode; + } else { + termio.c_cflag &= ~(termcode->mapcode); + } + break; + + } + } + if (tcsetattr(chansess->master, TCSANOW, &termio) < 0) { + dropbear_log(LOG_INFO, "error setting terminal attributes"); + } + } + + TRACE(("leave sessionpty")); + return DROPBEAR_SUCCESS; +} + +/* Handle a command request from the client. This is used for both shell + * and command-execution requests, and passes the command to + * noptycommand or ptycommand as appropriate. + * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ +static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, + int iscmd, int issubsys) { + + unsigned int cmdlen; + + TRACE(("enter sessioncommand")); + + if (chansess->cmd != NULL) { + /* TODO - send error - multiple commands? */ + return DROPBEAR_FAILURE; + } + + if (iscmd) { + /* "exec" */ + chansess->cmd = buf_getstring(ses.payload, &cmdlen); + + if (cmdlen > MAX_CMD_LEN) { + /* TODO - send error - too long ? */ + return DROPBEAR_FAILURE; + } + if (issubsys) { +#ifdef SFTPSERVER_PATH + if ((cmdlen == 4) && strncmp(chansess->cmd, "sftp", 4) == 0) { + m_free(chansess->cmd); + chansess->cmd = strdup(SFTPSERVER_PATH); + } else +#endif + { + return DROPBEAR_FAILURE; + } + } + } + + if (chansess->term == NULL) { + /* no pty */ + return noptycommand(channel, chansess); + } else { + /* want pty */ + return ptycommand(channel, chansess); + } +} + +/* Execute a command and set up redirection of stdin/stdout/stderr without a + * pty. + * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ +static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { + + int infds[2]; + int outfds[2]; + int errfds[2]; + pid_t pid; + + TRACE(("enter noptycommand")); + + /* redirect stdin/stdout/stderr */ + if (pipe(infds) != 0) + return DROPBEAR_FAILURE; + if (pipe(outfds) != 0) + return DROPBEAR_FAILURE; + if (pipe(errfds) != 0) + return DROPBEAR_FAILURE; + + pid = fork(); + if (pid < 0) + return DROPBEAR_FAILURE; + + if (!pid) { + /* child */ + + /* redirect stdin/stdout */ +#define FDIN 0 +#define FDOUT 1 + if ((dup2(infds[FDIN], STDIN_FILENO) < 0) || + (dup2(outfds[FDOUT], STDOUT_FILENO) < 0) || + (dup2(errfds[FDOUT], STDERR_FILENO) < 0)) { + TRACE(("leave noptycommand: error redirecting FDs")); + return DROPBEAR_FAILURE; + } + + close(infds[FDOUT]); + close(infds[FDIN]); + close(outfds[FDIN]); + close(outfds[FDOUT]); + close(errfds[FDIN]); + close(errfds[FDOUT]); + + execchild(chansess); + /* not reached */ + + } else { + /* parent */ + TRACE(("continue noptycommand: parent")); + chansess->pid = pid; + + /* add a child pid */ + addchildpid(chansess, pid); + + close(infds[FDIN]); + close(outfds[FDOUT]); + close(errfds[FDOUT]); + channel->infd = infds[FDOUT]; + channel->outfd = outfds[FDIN]; + channel->errfd = errfds[FDIN]; + ses.maxfd = MAX(ses.maxfd, channel->infd); + ses.maxfd = MAX(ses.maxfd, channel->outfd); + ses.maxfd = MAX(ses.maxfd, channel->errfd); + + if ((fcntl(channel->outfd, F_SETFL, O_NONBLOCK) < 0) || + (fcntl(channel->infd, F_SETFL, O_NONBLOCK) < 0) || + (fcntl(channel->errfd, F_SETFL, O_NONBLOCK) < 0)) { + dropbear_exit("Couldn't set nonblocking"); + } + } +#undef FDIN +#undef FDOUT + + TRACE(("leave noptycommand")); + return DROPBEAR_SUCCESS; +} + +/* Execute a command or shell within a pty environment, and set up + * redirection as appropriate. + * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ +static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { + + pid_t pid; + struct logininfo *li; +#ifdef DO_MOTD + buffer * motdbuf = NULL; + int len; + struct stat sb; + char *hushpath = NULL; +#endif + + TRACE(("enter ptycommand")); + + /* we need to have a pty allocated */ + if (chansess->master == -1 || chansess->tty == NULL) { + dropbear_log(LOG_WARNING, "no pty was allocated, couldn't execute"); + return DROPBEAR_FAILURE; + } + + pid = fork(); + if (pid < 0) + return DROPBEAR_FAILURE; + + if (pid == 0) { + /* child */ + + /* redirect stdin/stdout/stderr */ + close(chansess->master); + + pty_make_controlling_tty(&chansess->slave, chansess->tty); + + if ((dup2(chansess->slave, STDIN_FILENO) < 0) || + (dup2(chansess->slave, STDERR_FILENO) < 0) || + (dup2(chansess->slave, STDOUT_FILENO) < 0)) { + TRACE(("leave ptycommand: error redirecting filedesc")); + return DROPBEAR_FAILURE; + } + + close(chansess->slave); + + /* write the utmp/wtmp login record - must be after changing the + * terminal used for stdout with the dup2 above */ + li= login_alloc_entry(getpid(), svr_ses.authstate.username, + ses.remotehost, chansess->tty); + login_login(li); + login_free_entry(li); + + m_free(chansess->tty); + +#ifdef DO_MOTD + if (ses.opts->domotd) { + /* don't show the motd if ~/.hushlogin exists */ + + /* 11 == strlen("/hushlogin\0") */ + len = strlen(svr_ses.authstate.pw->pw_dir) + 11; + + hushpath = m_malloc(len); + snprintf(hushpath, len, "%s/hushlogin", svr_ses.authstate.pw->pw_dir); + + if (stat(hushpath, &sb) < 0) { + /* more than a screenful is stupid IMHO */ + motdbuf = buf_new(80 * 25); + if (buf_readfile(motdbuf, MOTD_FILENAME) == DROPBEAR_SUCCESS) { + buf_setpos(motdbuf, 0); + while (motdbuf->pos != motdbuf->len) { + len = motdbuf->len - motdbuf->pos; + len = write(STDOUT_FILENO, + buf_getptr(motdbuf, len), len); + buf_incrpos(motdbuf, len); + } + } + buf_free(motdbuf); + } + m_free(hushpath); + } +#endif /* DO_MOTD */ + + execchild(chansess); + /* not reached */ + + } else { + /* parent */ + TRACE(("continue ptycommand: parent")); + chansess->pid = pid; + + /* add a child pid */ + addchildpid(chansess, pid); + + close(chansess->slave); + channel->infd = chansess->master; + channel->outfd = chansess->master; + /* don't need to set stderr here */ + ses.maxfd = MAX(ses.maxfd, chansess->master); + + if (fcntl(chansess->master, F_SETFL, O_NONBLOCK) < 0) { + dropbear_exit("Couldn't set nonblocking"); + } + + } + + TRACE(("leave ptycommand")); + return DROPBEAR_SUCCESS; +} + +/* Add the pid of a child to the list for exit-handling */ +static void addchildpid(struct ChanSess *chansess, pid_t pid) { + + unsigned int i; + for (i = 0; i < svr_ses.childpidsize; i++) { + if (svr_ses.childpids[i].pid == -1) { + break; + } + } + + /* need to increase size */ + if (i == svr_ses.childpidsize) { + svr_ses.childpids = (struct ChildPid*)m_realloc(svr_ses.childpids, + sizeof(struct ChildPid) * svr_ses.childpidsize+1); + svr_ses.childpidsize++; + } + + svr_ses.childpids[i].pid = pid; + svr_ses.childpids[i].chansess = chansess; + +} + +/* Clean up, drop to user privileges, set up the environment and execute + * the command/shell. This function does not return. */ +static void execchild(struct ChanSess *chansess) { + + char *argv[4]; + char * usershell; + char * baseshell; + unsigned int i; + + /* wipe the hostkey */ + sign_key_free(ses.opts->hostkey); + ses.opts->hostkey = NULL; + + /* overwrite the prng state */ + seedrandom(); + + /* close file descriptors except stdin/stdout/stderr + * Need to be sure FDs are closed here to avoid reading files as root */ + for (i = 3; i < (unsigned int)ses.maxfd; i++) { + if (m_close(i) == DROPBEAR_FAILURE) { + dropbear_exit("Error closing file desc"); + } + } + + /* clear environment */ + /* if we're debugging using valgrind etc, we need to keep the LD_PRELOAD + * etc. This is hazardous, so should only be used for debugging. */ +#ifndef DEBUG_VALGRIND +#ifdef HAVE_CLEARENV + clearenv(); +#else /* don't HAVE_CLEARENV */ + /* Yay for posix. */ + if (environ) { + environ[0] = NULL; + } +#endif /* HAVE_CLEARENV */ +#endif /* DEBUG_VALGRIND */ + + /* We can only change uid/gid as root ... */ + if (getuid() == 0) { + + if ((setgid(svr_ses.authstate.pw->pw_gid) < 0) || + (initgroups(svr_ses.authstate.pw->pw_name, + svr_ses.authstate.pw->pw_gid) < 0) || + (setuid(svr_ses.authstate.pw->pw_uid) < 0)) { + dropbear_exit("error changing user"); + } + } else { + /* ... but if the daemon is the same uid as the requested uid, we don't + * need to */ + + /* XXX - there is a minor issue here, in that if there are multiple + * usernames with the same uid, but differing groups, then the + * differing groups won't be set (as with initgroups()). The solution + * is for the sysadmin not to give out the UID twice */ + if (getuid() != svr_ses.authstate.pw->pw_uid) { + dropbear_exit("couldn't change user as non-root"); + } + } + + /* an empty shell should be interpreted as "/bin/sh" */ + if (svr_ses.authstate.pw->pw_shell[0] == '\0') { + usershell = "/bin/sh"; + } else { + usershell = svr_ses.authstate.pw->pw_shell; + } + + /* set env vars */ + addnewvar("USER", svr_ses.authstate.pw->pw_name); + addnewvar("LOGNAME", svr_ses.authstate.pw->pw_name); + addnewvar("HOME", svr_ses.authstate.pw->pw_dir); + addnewvar("SHELL", usershell); + if (chansess->term != NULL) { + addnewvar("TERM", chansess->term); + } + + /* change directory */ + if (chdir(svr_ses.authstate.pw->pw_dir) < 0) { + dropbear_exit("error changing directory"); + } + +#ifndef DISABLE_X11FWD + /* set up X11 forwarding if enabled */ + x11setauth(chansess); +#endif +#ifndef DISABLE_AGENTFWD + /* set up agent env variable */ + agentset(chansess); +#endif + + baseshell = basename(usershell); + + if (chansess->cmd != NULL) { + argv[0] = baseshell; + } else { + /* a login shell should be "-bash" for "/bin/bash" etc */ + int len = strlen(baseshell) + 2; /* 2 for "-" */ + argv[0] = (char*)m_malloc(len); + snprintf(argv[0], len, "-%s", baseshell); + } + + if (chansess->cmd != NULL) { + argv[1] = "-c"; + argv[2] = chansess->cmd; + argv[3] = NULL; + } else { + /* construct a shell of the form "-bash" etc */ + argv[1] = NULL; + } + + execv(usershell, argv); + + /* only reached on error */ + dropbear_exit("child failed"); +} + +/* add a new environment variable, allocating space for the entry */ +void addnewvar(const char* param, const char* var) { + + char* newvar; + int plen, vlen; + + plen = strlen(param); + vlen = strlen(var); + + newvar = m_malloc(plen + vlen + 2); /* 2 is for '=' and '\0' */ + memcpy(newvar, param, plen); + newvar[plen] = '='; + memcpy(&newvar[plen+1], var, vlen); + newvar[plen+vlen+1] = '\0'; + if (putenv(newvar) < 0) { + dropbear_exit("environ error"); + } +} -- cgit v1.2.3 From 615226304502fa609213917042bffce8c4c83241 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 1 Jun 2004 10:48:46 +0000 Subject: Sorted out the first channel init issues. --HG-- extra : convert_revision : 67676f36b78efac878c11943d78a5de827498d05 --- chansession.h | 5 ++--- common-channel.c | 9 ++++++--- common-session.c | 2 ++ debug.h | 2 +- session.h | 2 +- svr-chansession.c | 8 ++++---- svr-session.c | 7 +++++++ 7 files changed, 23 insertions(+), 12 deletions(-) (limited to 'svr-chansession.c') diff --git a/chansession.h b/chansession.h index 1e4ba9a..85dc9c1 100644 --- a/chansession.h +++ b/chansession.h @@ -68,16 +68,15 @@ struct ChildPid { }; -void newchansess(struct Channel * channel); void chansessionrequest(struct Channel * channel); -void closechansess(struct Channel * channel); -void svr_chansessinitialise(); void send_msg_chansess_exitstatus(struct Channel * channel, struct ChanSess * chansess); void send_msg_chansess_exitsignal(struct Channel * channel, struct ChanSess * chansess); void addnewvar(const char* param, const char* var); +void svr_chansessinitialise(); +extern const struct ChanType svrchansess; struct SigMap { int signal; diff --git a/common-channel.c b/common-channel.c index 8b5423a..135e098 100644 --- a/common-channel.c +++ b/common-channel.c @@ -61,7 +61,7 @@ static void closechanfd(struct Channel *channel, int fd, int how); #define FD_CLOSED (-1) /* Initialise all the channels */ -void chaninitialise(struct ChanType *chantypes[]) { +void chaninitialise(const struct ChanType *chantypes[]) { /* may as well create space for a single channel */ ses.channels = (struct Channel**)m_malloc(sizeof(struct Channel*)); @@ -737,7 +737,7 @@ void recv_msg_channel_open() { unsigned int typelen; unsigned int remotechan, transwindow, transmaxpacket; struct Channel *channel; - struct ChanType *chantype; + const struct ChanType *chantype; unsigned int errtype = SSH_OPEN_UNKNOWN_CHANNEL_TYPE; int ret; @@ -775,14 +775,16 @@ void recv_msg_channel_open() { channel = newchannel(remotechan, chantype, transwindow, transmaxpacket); if (channel == NULL) { + TRACE(("newchannel returned NULL")); goto failure; } if (channel->type->inithandler) { ret = channel->type->inithandler(channel); - if (ret >= 0) { + if (ret > 0) { errtype = ret; deletechannel(channel); + TRACE(("inithandler returned failure %d", ret)); goto failure; } } @@ -810,6 +812,7 @@ void recv_msg_channel_open() { goto cleanup; failure: + TRACE(("recv_msg_channel_open failure")); send_msg_channel_open_failure(remotechan, errtype, "", ""); cleanup: diff --git a/common-session.c b/common-session.c index 5e87c9a..fce301a 100644 --- a/common-session.c +++ b/common-session.c @@ -106,6 +106,8 @@ void common_session_init(int sock, runopts *opts) { ses.dh_K = NULL; ses.remoteident = NULL; + ses.chantypes = NULL; + TRACE(("leave session_init")); } diff --git a/debug.h b/debug.h index 2b4a3dc..75f9503 100644 --- a/debug.h +++ b/debug.h @@ -34,7 +34,7 @@ /* #define DEBUG_VALGRIND */ /* Define this to print trace statements - very verbose */ -/* #define DEBUG_TRACE */ +#define DEBUG_TRACE /* All functions writing to the cleartext payload buffer call * CHECKCLEARTOWRITE() before writing. This is only really useful if you're diff --git a/session.h b/session.h index 51e3ae4..e372232 100644 --- a/session.h +++ b/session.h @@ -134,7 +134,7 @@ struct sshsession { /* Channel related */ struct Channel ** channels; /* these pointers may be null */ unsigned int chansize; /* the number of Channel*s allocated for channels */ - struct ChanType **chantypes; /* The valid channel types */ + const struct ChanType **chantypes; /* The valid channel types */ /* TCP forwarding - where manage listeners */ diff --git a/svr-chansession.c b/svr-chansession.c index dce0827..2705069 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -50,7 +50,7 @@ static void execchild(struct ChanSess *chansess); static void addchildpid(struct ChanSess *chansess, pid_t pid); static void sesssigchild_handler(int val); static void closechansess(struct Channel *channel); -static void newchansess(struct Channel *channel); +static int newchansess(struct Channel *channel); static void chansessionrequest(struct Channel *channel); static void send_exitsignalstatus(struct Channel *channel); @@ -205,7 +205,7 @@ static void send_msg_chansess_exitsignal(struct Channel * channel, } /* set up a session channel */ -static void newchansess(struct Channel *channel) { +static int newchansess(struct Channel *channel) { struct ChanSess *chansess; @@ -241,6 +241,8 @@ static void newchansess(struct Channel *channel) { chansess->agentdir = NULL; #endif + return 0; + } /* clean a session channel */ @@ -310,8 +312,6 @@ static void chansessionrequest(struct Channel *channel) { TRACE(("enter chansessionrequest")); - assert(channel->type == CHANNEL_ID_SESSION); - type = buf_getstring(ses.payload, &typelen); wantreply = buf_getbyte(ses.payload); diff --git a/svr-session.c b/svr-session.c index 1aade1f..c6f05cc 100644 --- a/svr-session.c +++ b/svr-session.c @@ -40,6 +40,12 @@ static void svr_remoteclosed(); struct serversession svr_ses; +const struct ChanType *chantypes[] = { + &svrchansess, + NULL /* Null termination is mandatory. */ +}; + + void svr_session(int sock, runopts *opts, int childpipe, struct sockaddr* remoteaddr) { @@ -56,6 +62,7 @@ void svr_session(int sock, runopts *opts, int childpipe, /* Initialise server specific parts of the session */ svr_ses.childpipe = childpipe; authinitialise(); + chaninitialise(chantypes); svr_chansessinitialise(); if (gettimeofday(&timeout, 0) < 0) { -- cgit v1.2.3 From 513f947d62351e5af77676e20740232d753cd5b1 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Wed, 2 Jun 2004 04:59:49 +0000 Subject: Chantype handling is sorted --HG-- extra : convert_revision : 807efead6ecf690f147fd8145aa9d78ff894cdb2 --- channel.h | 5 +++-- common-channel.c | 19 +++++++++++------ common-chansession.c | 2 +- debug.h | 4 +++- localtcpfwd.c | 15 ++++++++++++- localtcpfwd.h | 2 +- remotetcpfwd.c | 1 + svr-chansession.c | 60 ++++++++++++++++++++++++++-------------------------- svr-session.c | 2 ++ 9 files changed, 67 insertions(+), 43 deletions(-) (limited to 'svr-chansession.c') diff --git a/channel.h b/channel.h index 750a9bb..e033164 100644 --- a/channel.h +++ b/channel.h @@ -81,7 +81,7 @@ struct Channel { int initconn; /* used for TCP forwarding, whether the channel has been fully initialised */ - struct ChanType* type; + const struct ChanType* type; }; @@ -100,7 +100,8 @@ void chaninitialise(); void chancleanup(); void setchannelfds(fd_set *readfd, fd_set *writefd); void channelio(fd_set *readfd, fd_set *writefd); -struct Channel* newchannel(unsigned int remotechan, struct ChanType *type, +struct Channel* newchannel(unsigned int remotechan, + const struct ChanType *type, unsigned int transwindow, unsigned int transmaxpacket); void recv_msg_channel_open(); diff --git a/common-channel.c b/common-channel.c index 135e098..4643fc2 100644 --- a/common-channel.c +++ b/common-channel.c @@ -96,7 +96,8 @@ void chancleanup() { /* If remotechan, transwindow and transmaxpacket are not know (for a new * outgoing connection, with them to be filled on confirmation), they should * all be set to 0 */ -struct Channel* newchannel(unsigned int remotechan, struct ChanType *type, +struct Channel* newchannel(unsigned int remotechan, + const struct ChanType *type, unsigned int transwindow, unsigned int transmaxpacket) { struct Channel * newchan; @@ -535,8 +536,6 @@ void recv_msg_channel_request() { dropbear_exit("Unknown channel"); } - TRACE(("chan type is %d", channel->type)); - if (channel->type->reqhandler) { channel->type->reqhandler(channel); } else { @@ -737,6 +736,7 @@ void recv_msg_channel_open() { unsigned int typelen; unsigned int remotechan, transwindow, transmaxpacket; struct Channel *channel; + const struct ChanType **cp; const struct ChanType *chantype; unsigned int errtype = SSH_OPEN_UNKNOWN_CHANNEL_TYPE; int ret; @@ -758,19 +758,24 @@ void recv_msg_channel_open() { goto failure; } - /* Get the channel type. This will depend if it is a client or a server, - * so we iterate through the connection-specific list which was - * set up when the connection started */ - for (chantype = ses.chantypes[0]; chantype != NULL; chantype++) { + /* Get the channel type. Client and server style invokation will set up a + * different list for ses.chantypes at startup. We just iterate through + * this list and find the matching name */ + for (cp = &ses.chantypes[0], chantype = (*cp); + chantype != NULL; + cp++, chantype = (*cp)) { if (strcmp(type, chantype->name) == 0) { break; } } if (chantype == NULL) { + TRACE(("No matching type for '%s'", type)); goto failure; } + TRACE(("matched type '%s'", type)); + /* create the channel */ channel = newchannel(remotechan, chantype, transwindow, transmaxpacket); diff --git a/common-chansession.c b/common-chansession.c index ad9c7ed..b350c6c 100644 --- a/common-chansession.c +++ b/common-chansession.c @@ -25,7 +25,7 @@ #include "chansession.h" /* Mapping of signal values to ssh signal strings */ -const extern struct SigMap signames[] = { +const struct SigMap signames[] = { {SIGABRT, "ABRT"}, {SIGALRM, "ALRM"}, {SIGFPE, "FPE"}, diff --git a/debug.h b/debug.h index 75f9503..f41a2e4 100644 --- a/debug.h +++ b/debug.h @@ -34,7 +34,9 @@ /* #define DEBUG_VALGRIND */ /* Define this to print trace statements - very verbose */ -#define DEBUG_TRACE +/* Caution: Don't use this in an unfriendly environment (ie unfirewalled), + * since the printing does not sanitise strings etc */ +/*#define DEBUG_TRACE*/ /* All functions writing to the cleartext payload buffer call * CHECKCLEARTOWRITE() before writing. This is only really useful if you're diff --git a/localtcpfwd.c b/localtcpfwd.c index 9894f46..bf89fa0 100644 --- a/localtcpfwd.c +++ b/localtcpfwd.c @@ -1,14 +1,27 @@ #include "includes.h" #include "session.h" #include "dbutil.h" +#include "channel.h" #include "localtcpfwd.h" #ifndef DISABLE_LOCALTCPFWD +static int newtcpdirect(struct Channel * channel); static int newtcp(const char * host, int port); +const struct ChanType chan_tcpdirect = { + 0, /* sepfds */ + "direct-tcpip", + newtcpdirect, /* init */ + NULL, /* checkclose */ + NULL, /* reqhandler */ + NULL /* closehandler */ +}; + + + /* Called upon creating a new direct tcp channel (ie we connect out to an * address */ -int newtcpdirect(struct Channel * channel) { +static int newtcpdirect(struct Channel * channel) { unsigned char* desthost = NULL; unsigned int destport; diff --git a/localtcpfwd.h b/localtcpfwd.h index 324cb55..65efa6e 100644 --- a/localtcpfwd.h +++ b/localtcpfwd.h @@ -28,7 +28,7 @@ #include "includes.h" #include "channel.h" -int newtcpdirect(struct Channel * channel); +extern const struct ChanType chan_tcpdirect; #endif #endif diff --git a/remotetcpfwd.c b/remotetcpfwd.c index c58b820..40a3a82 100644 --- a/remotetcpfwd.c +++ b/remotetcpfwd.c @@ -90,6 +90,7 @@ static void acceptremote(struct TCPListener *listener) { return; } + /* XXX XXX XXX - type here needs fixing */ if (send_msg_channel_open_init(fd, CHANNEL_ID_TCPFORWARDED, "forwarded-tcpip") == DROPBEAR_SUCCESS) { buf_putstring(ses.writepayload, tcpinfo->addr, diff --git a/svr-chansession.c b/svr-chansession.c index 2705069..f5b4308 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -56,16 +56,6 @@ static void chansessionrequest(struct Channel *channel); static void send_exitsignalstatus(struct Channel *channel); static int sesscheckclose(struct Channel *channel); -const struct ChanType svrchansess = { - 0, /* sepfds */ - "session", /* name */ - newchansess, /* inithandler */ - sesscheckclose, /* checkclosehandler */ - chansessionrequest, /* reqhandler */ - closechansess, /* closehandler */ -}; - - /* required to clear environment */ extern char** environ; @@ -75,25 +65,6 @@ static int sesscheckclose(struct Channel *channel) { return chansess->exited; } -/* Set up the general chansession environment, in particular child-exit - * handling */ -void svr_chansessinitialise() { - - struct sigaction sa_chld; - - /* single child process intially */ - svr_ses.childpids = (struct ChildPid*)m_malloc(sizeof(struct ChildPid)); - svr_ses.childpids[0].pid = -1; /* unused */ - svr_ses.childpids[0].chansess = NULL; - svr_ses.childpidsize = 1; - sa_chld.sa_handler = sesssigchild_handler; - sa_chld.sa_flags = SA_NOCLDSTOP; - if (sigaction(SIGCHLD, &sa_chld, NULL) < 0) { - dropbear_exit("signal() error"); - } - -} - /* handler for childs exiting, store the state for return to the client */ static void sesssigchild_handler(int dummy) { @@ -254,7 +225,7 @@ static void closechansess(struct Channel *channel) { chansess = (struct ChanSess*)channel->typedata; - send_exitsignalstatus(chansess); + send_exitsignalstatus(channel); TRACE(("enter closechansess")); if (chansess == NULL) { @@ -911,6 +882,35 @@ static void execchild(struct ChanSess *chansess) { dropbear_exit("child failed"); } +const struct ChanType svrchansess = { + 0, /* sepfds */ + "session", /* name */ + newchansess, /* inithandler */ + sesscheckclose, /* checkclosehandler */ + chansessionrequest, /* reqhandler */ + closechansess, /* closehandler */ +}; + + +/* Set up the general chansession environment, in particular child-exit + * handling */ +void svr_chansessinitialise() { + + struct sigaction sa_chld; + + /* single child process intially */ + svr_ses.childpids = (struct ChildPid*)m_malloc(sizeof(struct ChildPid)); + svr_ses.childpids[0].pid = -1; /* unused */ + svr_ses.childpids[0].chansess = NULL; + svr_ses.childpidsize = 1; + sa_chld.sa_handler = sesssigchild_handler; + sa_chld.sa_flags = SA_NOCLDSTOP; + if (sigaction(SIGCHLD, &sa_chld, NULL) < 0) { + dropbear_exit("signal() error"); + } + +} + /* add a new environment variable, allocating space for the entry */ void addnewvar(const char* param, const char* var) { diff --git a/svr-session.c b/svr-session.c index c6f05cc..8e8eaea 100644 --- a/svr-session.c +++ b/svr-session.c @@ -35,6 +35,7 @@ #include "channel.h" #include "chansession.h" #include "atomicio.h" +#include "localtcpfwd.h" static void svr_remoteclosed(); @@ -42,6 +43,7 @@ struct serversession svr_ses; const struct ChanType *chantypes[] = { &svrchansess, + &chan_tcpdirect, NULL /* Null termination is mandatory. */ }; -- cgit v1.2.3 From 444dbb5364798925a3cacddba7b1bb3041e41a23 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 3 Jun 2004 16:45:53 +0000 Subject: - Reworked non-channel fd handling to listener.c - More channel cleaning up --HG-- extra : convert_revision : 385ec76d0304b93e277d1cc193383db5fd773703 --- Makefile.in | 10 +- channel.h | 3 +- chansession.h | 5 +- common-channel.c | 25 ++--- listener.c | 123 +++++++++++++++++++++ listener.h | 37 +++++++ localtcpfwd.c | 155 -------------------------- localtcpfwd.h | 34 ------ options.h | 2 +- remotetcpfwd.c | 302 --------------------------------------------------- remotetcpfwd.h | 6 - session.h | 6 +- svr-chansession.c | 6 +- svr-session.c | 2 +- tcpfwd-direct.c | 154 ++++++++++++++++++++++++++ tcpfwd-direct.h | 34 ++++++ tcpfwd-remote.c | 319 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tcpfwd-remote.h | 6 + tcpfwd.c | 124 --------------------- tcpfwd.h | 37 ------- x11fwd.c | 72 +++++++----- x11fwd.h | 2 - 22 files changed, 748 insertions(+), 716 deletions(-) create mode 100644 listener.c create mode 100644 listener.h delete mode 100644 localtcpfwd.c delete mode 100644 localtcpfwd.h delete mode 100644 remotetcpfwd.c delete mode 100644 remotetcpfwd.h create mode 100644 tcpfwd-direct.c create mode 100644 tcpfwd-direct.h create mode 100644 tcpfwd-remote.c create mode 100644 tcpfwd-remote.h delete mode 100644 tcpfwd.c delete mode 100644 tcpfwd.h (limited to 'svr-chansession.c') diff --git a/Makefile.in b/Makefile.in index 10d3cc1..cc3127a 100644 --- a/Makefile.in +++ b/Makefile.in @@ -4,9 +4,9 @@ LTM=libtommath/libtommath.a COMMONOBJS=dbutil.o common-session.o common-packet.o common-algo.o buffer.o \ common-kex.o dss.o bignum.o \ signkey.o rsa.o random.o common-channel.o \ - common-chansession.o queue.o termcodes.o runopts.o \ - loginrec.o atomicio.o x11fwd.o agentfwd.o localtcpfwd.o compat.o \ - remotetcpfwd.o tcpfwd.o + common-chansession.o queue.o termcodes.o \ + loginrec.o atomicio.o x11fwd.o tcpfwd-direct.o compat.o \ + tcpfwd-remote.o listener.o SVROBJS=svr-kex.o svr-packet.o svr-algo.o svr-auth.o sshpty.o \ svr-authpasswd.o svr-authpubkey.o svr-session.o svr-service.o \ @@ -28,8 +28,8 @@ HEADERS=options.h dbutil.h session.h packet.h algo.h ssh.h buffer.h kex.h \ dss.h bignum.h signkey.h rsa.h random.h service.h auth.h authpasswd.h \ debug.h channel.h chansession.h debug.h config.h queue.h sshpty.h \ termcodes.h gendss.h genrsa.h authpubkey.h runopts.h includes.h \ - loginrec.h atomicio.h x11fwd.h agentfwd.h localtcpfwd.h compat.h \ - remotetcpfwd.h tcpfwd.h + loginrec.h atomicio.h x11fwd.h agentfwd.h tcpfwd-direct.h compat.h \ + tcpfwd-remote.h listener.h ALLOBJS=$(OBJS) $(DROPBEARKEYOBJS) $(DROPBEAROBJS) diff --git a/channel.h b/channel.h index e033164..b77a660 100644 --- a/channel.h +++ b/channel.h @@ -114,8 +114,7 @@ void recv_msg_channel_close(); void recv_msg_channel_eof(); #ifdef USE_LISTENERS -int send_msg_channel_open_init(int fd, struct ChanType *type, - const char * typestring); +int send_msg_channel_open_init(int fd, const struct ChanType *type); void recv_msg_channel_open_confirmation(); void recv_msg_channel_open_failure(); #endif diff --git a/chansession.h b/chansession.h index 85dc9c1..7879791 100644 --- a/chansession.h +++ b/chansession.h @@ -27,6 +27,7 @@ #include "loginrec.h" #include "channel.h" +#include "listener.h" struct ChanSess { @@ -47,7 +48,7 @@ struct ChanSess { unsigned char exitcore; #ifndef DISABLE_X11FWD - int x11fd; /* set to -1 to indicate forwarding not established */ + struct Listener * x11listener; int x11port; char * x11authprot; char * x11authcookie; @@ -56,7 +57,7 @@ struct ChanSess { #endif #ifndef DISABLE_AGENTFWD - int agentfd; + struct Listener * agentlistener; char * agentfile; char * agentdir; #endif diff --git a/common-channel.c b/common-channel.c index 4643fc2..63ed275 100644 --- a/common-channel.c +++ b/common-channel.c @@ -32,9 +32,9 @@ #include "dbutil.h" #include "channel.h" #include "ssh.h" -#include "localtcpfwd.h" -#include "remotetcpfwd.h" -#include "tcpfwd.h" +#include "tcpfwd-direct.h" +#include "tcpfwd-remote.h" +#include "listener.h" static void send_msg_channel_open_failure(unsigned int remotechan, int reason, const unsigned char *text, const unsigned char *lang); @@ -70,8 +70,8 @@ void chaninitialise(const struct ChanType *chantypes[]) { ses.chantypes = chantypes; -#ifdef USING_TCP_LISTENERS - tcp_fwd_initialise(); +#ifdef USING_LISTENERS + listeners_initialise(); #endif } @@ -219,9 +219,9 @@ void channelio(fd_set *readfd, fd_set *writefd) { } /* foreach channel */ - /* Not channel specific */ -#ifdef USING_TCP_LISTENERS - handle_tcp_fwd(readfd); + /* Listeners such as TCP, X11, agent-auth */ +#ifdef USING_LISTENERS + handle_listeners(readfd); #endif } @@ -429,8 +429,8 @@ void setchannelfds(fd_set *readfd, fd_set *writefd) { } /* foreach channel */ -#ifdef USING_TCP_LISTENERS - set_tcp_fwd_fds(readfd); +#ifdef USING_LISTENERS + set_listener_fds(readfd); #endif } @@ -895,8 +895,7 @@ static void send_msg_channel_open_confirmation(struct Channel* channel, * options, with the calling function calling encrypt_packet() after * completion. It is mandatory for the caller to encrypt_packet() if * DROPBEAR_SUCCESS is returned */ -int send_msg_channel_open_init(int fd, struct ChanType *type, - const char * typestring) { +int send_msg_channel_open_init(int fd, const struct ChanType *type) { struct Channel* chan; @@ -920,7 +919,7 @@ int send_msg_channel_open_init(int fd, struct ChanType *type, CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN); - buf_putstring(ses.writepayload, typestring, strlen(typestring)); + buf_putstring(ses.writepayload, type->name, strlen(type->name)); buf_putint(ses.writepayload, chan->index); buf_putint(ses.writepayload, RECV_MAXWINDOW); buf_putint(ses.writepayload, RECV_MAXPACKET); diff --git a/listener.c b/listener.c new file mode 100644 index 0000000..df66629 --- /dev/null +++ b/listener.c @@ -0,0 +1,123 @@ +#include "includes.h" +#include "listener.h" +#include "session.h" +#include "dbutil.h" + +void listener_initialise() { + + /* just one slot to start with */ + ses.listeners = (struct Listener**)m_malloc(sizeof(struct Listener*)); + ses.listensize = 1; + ses.listeners[0] = NULL; + +} + +void set_listener_fds(fd_set * readfds) { + + unsigned int i; + struct Listener *listener; + + /* check each in turn */ + for (i = 0; i < ses.listensize; i++) { + listener = ses.listeners[i]; + if (listener != NULL) { + FD_SET(listener->sock, readfds); + } + } +} + + +void handle_listeners(fd_set * readfds) { + + unsigned int i; + struct Listener *listener; + + /* check each in turn */ + for (i = 0; i < ses.listensize; i++) { + listener = ses.listeners[i]; + if (listener != NULL) { + if (FD_ISSET(listener->sock, readfds)) { + listener->accepter(listener); + } + } + } +} + + +/* accepter(int fd, void* typedata) is a function to accept connections, + * cleanup(void* typedata) happens when cleaning up */ +struct Listener* new_listener(int sock, int type, void* typedata, + void (*accepter)(struct Listener*), + void (*cleanup)(struct Listener*)) { + + unsigned int i, j; + struct Listener *newlisten = NULL; + /* try get a new structure to hold it */ + for (i = 0; i < ses.listensize; i++) { + if (ses.listeners[i] == NULL) { + break; + } + } + + /* or create a new one */ + if (i == ses.listensize) { + if (ses.listensize > MAX_LISTENERS) { + TRACE(("leave newlistener: too many already")); + close(sock); + return NULL; + } + + ses.listeners = (struct Listener**)m_realloc(ses.listeners, + (ses.listensize+LISTENER_EXTEND_SIZE) + *sizeof(struct Listener*)); + + ses.listensize += LISTENER_EXTEND_SIZE; + + for (j = i; j < ses.listensize; j++) { + ses.listeners[j] = NULL; + } + } + + ses.maxfd = MAX(ses.maxfd, sock); + + newlisten = (struct Listener*)m_malloc(sizeof(struct Listener)); + newlisten->index = i; + newlisten->type = type; + newlisten->typedata = typedata; + newlisten->sock = sock; + newlisten->accepter = accepter; + newlisten->cleanup = cleanup; + + ses.listeners[i] = newlisten; + return newlisten; +} + +/* Return the first listener which matches the type-specific comparison + * function. Particularly needed for global requests, like tcp */ +struct Listener * get_listener(int type, void* typedata, + int (*match)(void*, void*)) { + + unsigned int i; + struct Listener* listener; + + for (i = 0, listener = ses.listeners[i]; i < ses.listensize; i++) { + if (listener->type == type + && match(typedata, listener->typedata)) { + return listener; + } + } + + return NULL; +} + +void remove_listener(struct Listener* listener) { + + if (listener->cleanup) { + listener->cleanup(listener); + } + + close(listener->sock); + ses.listeners[listener->index] = NULL; + m_free(listener); + +} diff --git a/listener.h b/listener.h new file mode 100644 index 0000000..ab77351 --- /dev/null +++ b/listener.h @@ -0,0 +1,37 @@ +#ifndef _LISTENER_H +#define _LISTENER_H + +#define MAX_LISTENERS 20 +#define LISTENER_EXTEND_SIZE 1 + +struct Listener { + + int sock; + + int index; /* index in the array of listeners */ + + void (*accepter)(struct Listener*); + void (*cleanup)(struct Listener*); + + int type; /* CHANNEL_ID_X11, CHANNEL_ID_AGENT, + CHANNEL_ID_TCPDIRECT (for clients), + CHANNEL_ID_TCPFORWARDED (for servers) */ + + void *typedata; + +}; + +void listener_initialise(); +void handle_listeners(fd_set * readfds); +void set_listener_fds(fd_set * readfds); + +struct Listener* new_listener(int sock, int type, void* typedata, + void (*accepter)(struct Listener*), + void (*cleanup)(struct Listener*)); + +struct Listener * get_listener(int type, void* typedata, + int (*match)(void*, void*)); + +void remove_listener(struct Listener* listener); + +#endif /* _LISTENER_H */ diff --git a/localtcpfwd.c b/localtcpfwd.c deleted file mode 100644 index bf89fa0..0000000 --- a/localtcpfwd.c +++ /dev/null @@ -1,155 +0,0 @@ -#include "includes.h" -#include "session.h" -#include "dbutil.h" -#include "channel.h" -#include "localtcpfwd.h" - -#ifndef DISABLE_LOCALTCPFWD -static int newtcpdirect(struct Channel * channel); -static int newtcp(const char * host, int port); - -const struct ChanType chan_tcpdirect = { - 0, /* sepfds */ - "direct-tcpip", - newtcpdirect, /* init */ - NULL, /* checkclose */ - NULL, /* reqhandler */ - NULL /* closehandler */ -}; - - - -/* Called upon creating a new direct tcp channel (ie we connect out to an - * address */ -static int newtcpdirect(struct Channel * channel) { - - unsigned char* desthost = NULL; - unsigned int destport; - unsigned char* orighost = NULL; - unsigned int origport; - int sock; - int len; - int ret = DROPBEAR_FAILURE; - - if (ses.opts->nolocaltcp) { - TRACE(("leave newtcpdirect: local tcp forwarding disabled")); - goto out; - } - - desthost = buf_getstring(ses.payload, &len); - if (len > MAX_HOST_LEN) { - TRACE(("leave newtcpdirect: desthost too long")); - goto out; - } - - destport = buf_getint(ses.payload); - - orighost = buf_getstring(ses.payload, &len); - if (len > MAX_HOST_LEN) { - TRACE(("leave newtcpdirect: orighost too long")); - goto out; - } - - origport = buf_getint(ses.payload); - - /* best be sure */ - if (origport > 65535 || destport > 65535) { - TRACE(("leave newtcpdirect: port > 65535")); - goto out; - } - - sock = newtcp(desthost, destport); - if (sock < 0) { - TRACE(("leave newtcpdirect: sock failed")); - goto out; - } - - ses.maxfd = MAX(ses.maxfd, sock); - - /* Note that infd is actually the "outgoing" direction on the - * tcp connection, vice versa for outfd. - * We don't set outfd, that will get set after the connection's - * progress succeeds */ - channel->infd = sock; - channel->initconn = 1; - - ret = DROPBEAR_SUCCESS; - -out: - m_free(desthost); - m_free(orighost); - TRACE(("leave newtcpdirect: ret %d", ret)); - return ret; -} - -/* Initiate a new TCP connection - this is non-blocking, so the socket - * returned will need to be checked for success when it is first written. - * Similarities with OpenSSH's connect_to() are not coincidental. - * Returns -1 on failure */ -static int newtcp(const char * host, int port) { - - int sock = -1; - char portstring[6]; - struct addrinfo *res = NULL, *ai; - int val; - - struct addrinfo hints; - - TRACE(("enter newtcp")); - - memset(&hints, 0, sizeof(hints)); - /* TCP, either ip4 or ip6 */ - hints.ai_socktype = SOCK_STREAM; - hints.ai_family = PF_UNSPEC; - - snprintf(portstring, sizeof(portstring), "%d", port); - if (getaddrinfo(host, portstring, &hints, &res) != 0) { - if (res) { - freeaddrinfo(res); - } - TRACE(("leave newtcp: failed getaddrinfo")); - return -1; - } - - /* Use the first socket that works */ - for (ai = res; ai != NULL; ai = ai->ai_next) { - - if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) { - continue; - } - - sock = socket(ai->ai_family, SOCK_STREAM, 0); - if (sock < 0) { - TRACE(("TCP socket() failed")); - continue; - } - - if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { - close(sock); - TRACE(("TCP non-blocking failed")); - continue; - } - - /* non-blocking, so it might return without success (EINPROGRESS) */ - if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { - if (errno == EINPROGRESS) { - TRACE(("connect in progress")); - } else { - close(sock); - TRACE(("TCP connect failed")); - continue; - } - } - break; - } - - freeaddrinfo(res); - - if (ai == NULL) { - return -1; - } - - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val)); - return sock; -} -#endif /* DISABLE_LOCALTCPFWD */ diff --git a/localtcpfwd.h b/localtcpfwd.h deleted file mode 100644 index 65efa6e..0000000 --- a/localtcpfwd.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Dropbear - a SSH2 server - * - * Copyright (c) 2002,2003 Matt Johnston - * All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. */ -#ifndef _LOCALTCPFWD_H_ -#define _LOCALTCPFWD_H_ -#ifndef DISABLE_LOCALTCPFWD - -#include "includes.h" -#include "channel.h" - -extern const struct ChanType chan_tcpdirect; - -#endif -#endif diff --git a/options.h b/options.h index d1f1794..703fec0 100644 --- a/options.h +++ b/options.h @@ -296,7 +296,7 @@ #endif #ifndef ENABLE_LOCALTCPFWD -#define DISABLE_LOCALTCPFWD +#define DISABLE_TCPDIRECT #endif #ifndef ENABLE_REMOTETCPFWD diff --git a/remotetcpfwd.c b/remotetcpfwd.c deleted file mode 100644 index 40a3a82..0000000 --- a/remotetcpfwd.c +++ /dev/null @@ -1,302 +0,0 @@ -#include "includes.h" -#include "ssh.h" -#include "remotetcpfwd.h" -#include "dbutil.h" -#include "session.h" -#include "buffer.h" -#include "packet.h" -#include "tcpfwd.h" - -#ifndef DISABLE_REMOTETCPFWD - -struct RemoteTCP { - - unsigned char* addr; - unsigned int port; - -}; - -static void send_msg_request_success(); -static void send_msg_request_failure(); -static int cancelremotetcp(); -static int remotetcpreq(); -static int newlistener(unsigned char* bindaddr, unsigned int port); -static void acceptremote(struct TCPListener *listener); - -/* At the moment this is completely used for tcp code (with the name reflecting - * that). If new request types are added, this should be replaced with code - * similar to the request-switching in chansession.c */ -void recv_msg_global_request_remotetcp() { - - unsigned char* reqname = NULL; - unsigned int namelen; - unsigned int wantreply = 0; - int ret = DROPBEAR_FAILURE; - - TRACE(("enter recv_msg_global_request_remotetcp")); - - if (ses.opts->noremotetcp) { - TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled")); - goto out; - } - - reqname = buf_getstring(ses.payload, &namelen); - wantreply = buf_getbyte(ses.payload); - - if (namelen > MAXNAMLEN) { - TRACE(("name len is wrong: %d", namelen)); - goto out; - } - - if (strcmp("tcpip-forward", reqname) == 0) { - ret = remotetcpreq(); - } else if (strcmp("cancel-tcpip-forward", reqname) == 0) { - ret = cancelremotetcp(); - } else { - TRACE(("reqname isn't tcpip-forward: '%s'", reqname)); - } - -out: - if (wantreply) { - if (ret == DROPBEAR_SUCCESS) { - send_msg_request_success(); - } else { - send_msg_request_failure(); - } - } - - m_free(reqname); - - TRACE(("leave recv_msg_global_request")); -} - -static void acceptremote(struct TCPListener *listener) { - - int fd; - struct sockaddr addr; - int len; - char ipstring[NI_MAXHOST], portstring[NI_MAXSERV]; - struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); - - len = sizeof(addr); - - fd = accept(listener->sock, &addr, &len); - if (fd < 0) { - return; - } - - if (getnameinfo(&addr, len, ipstring, sizeof(ipstring), portstring, - sizeof(portstring), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { - return; - } - - /* XXX XXX XXX - type here needs fixing */ - if (send_msg_channel_open_init(fd, CHANNEL_ID_TCPFORWARDED, - "forwarded-tcpip") == DROPBEAR_SUCCESS) { - buf_putstring(ses.writepayload, tcpinfo->addr, - strlen(tcpinfo->addr)); - buf_putint(ses.writepayload, tcpinfo->port); - buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); - buf_putint(ses.writepayload, atol(portstring)); - encrypt_packet(); - } -} - -static void cleanupremote(struct TCPListener *listener) { - - struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); - - m_free(tcpinfo->addr); - m_free(tcpinfo); -} - -static void send_msg_request_success() { - - CHECKCLEARTOWRITE(); - buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS); - encrypt_packet(); - -} - -static void send_msg_request_failure() { - - CHECKCLEARTOWRITE(); - buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE); - encrypt_packet(); - -} - -static int matchtcp(void* typedata1, void* typedata2) { - - const struct RemoteTCP *info1 = (struct RemoteTCP*)typedata1; - const struct RemoteTCP *info2 = (struct RemoteTCP*)typedata2; - - return info1->port == info2->port - && (strcmp(info1->addr, info2->addr) == 0); -} - -static int cancelremotetcp() { - - int ret = DROPBEAR_FAILURE; - unsigned char * bindaddr = NULL; - unsigned int addrlen; - unsigned int port; - struct TCPListener * listener = NULL; - struct RemoteTCP tcpinfo; - - TRACE(("enter cancelremotetcp")); - - bindaddr = buf_getstring(ses.payload, &addrlen); - if (addrlen > MAX_IP_LEN) { - TRACE(("addr len too long: %d", addrlen)); - goto out; - } - - port = buf_getint(ses.payload); - - tcpinfo.addr = bindaddr; - tcpinfo.port = port; - listener = get_listener(CHANNEL_ID_TCPFORWARDED, &tcpinfo, matchtcp); - if (listener) { - remove_listener( listener ); - ret = DROPBEAR_SUCCESS; - } - -out: - m_free(bindaddr); - TRACE(("leave cancelremotetcp")); - return ret; -} - -static int remotetcpreq() { - - int ret = DROPBEAR_FAILURE; - unsigned char * bindaddr = NULL; - unsigned int addrlen; - unsigned int port; - - TRACE(("enter remotetcpreq")); - - bindaddr = buf_getstring(ses.payload, &addrlen); - if (addrlen > MAX_IP_LEN) { - TRACE(("addr len too long: %d", addrlen)); - goto out; - } - - port = buf_getint(ses.payload); - - if (port == 0) { - dropbear_log(LOG_INFO, "Server chosen tcpfwd ports are unsupported"); - goto out; - } - - if (port < 1 || port > 65535) { - TRACE(("invalid port: %d", port)); - goto out; - } - - /* XXX matt - server change - if (ses.authstate.pw->pw_uid != 0 - && port < IPPORT_RESERVED) { - TRACE(("can't assign port < 1024 for non-root")); - goto out; - } - */ - - ret = newlistener(bindaddr, port); - -out: - if (ret == DROPBEAR_FAILURE) { - /* we only free it if a listener wasn't created, since the listener - * has to remember it if it's to be cancelled */ - m_free(bindaddr); - } - TRACE(("leave remotetcpreq")); - return ret; -} - -static int newlistener(unsigned char* bindaddr, unsigned int port) { - - struct RemoteTCP * tcpinfo = NULL; - char portstring[6]; /* "65535\0" */ - struct addrinfo *res = NULL, *ai = NULL; - struct addrinfo hints; - int sock = -1; - int ret = DROPBEAR_FAILURE; - - TRACE(("enter newlistener")); - - /* first we try to bind, so don't need to do so much cleanup on failure */ - snprintf(portstring, sizeof(portstring), "%d", port); - memset(&hints, 0x0, sizeof(hints)); - hints.ai_socktype = SOCK_STREAM; - hints.ai_family = PF_INET; - hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; - - if (getaddrinfo(bindaddr, portstring, &hints, &res) < 0) { - TRACE(("leave newlistener: getaddrinfo failed: %s", - strerror(errno))); - goto done; - } - - /* find the first one which works */ - for (ai = res; ai != NULL; ai = ai->ai_next) { - if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) { - continue; - } - - sock = socket(ai->ai_family, SOCK_STREAM, 0); - if (sock < 0) { - TRACE(("socket failed: %s", strerror(errno))); - goto fail; - } - - if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { - TRACE(("bind failed: %s", strerror(errno))); - goto fail; - } - - if (listen(sock, 20) < 0) { - TRACE(("listen failed: %s", strerror(errno))); - goto fail; - } - - if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { - TRACE(("fcntl nonblocking failed: %s", strerror(errno))); - goto fail; - } - - /* success */ - break; - -fail: - close(sock); - } - - - if (ai == NULL) { - TRACE(("no successful sockets")); - goto done; - } - - tcpinfo = (struct RemoteTCP*)m_malloc(sizeof(struct RemoteTCP)); - tcpinfo->addr = bindaddr; - tcpinfo->port = port; - - ret = new_fwd(sock, CHANNEL_ID_TCPFORWARDED, tcpinfo, - acceptremote, cleanupremote); - - if (ret == DROPBEAR_FAILURE) { - m_free(tcpinfo); - } - -done: - if (res) { - freeaddrinfo(res); - } - - TRACE(("leave newlistener")); - return ret; -} - -#endif /* DISABLE_REMOTETCPFWD */ diff --git a/remotetcpfwd.h b/remotetcpfwd.h deleted file mode 100644 index 64dbed3..0000000 --- a/remotetcpfwd.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _REMOTETCPFWD_H -#define _REMOTETCPFWD_H - -void recv_msg_global_request_remotetcp(); - -#endif /* _REMOTETCPFWD_H */ diff --git a/session.h b/session.h index e372232..0cb2eaa 100644 --- a/session.h +++ b/session.h @@ -33,7 +33,7 @@ #include "channel.h" #include "queue.h" #include "runopts.h" -#include "remotetcpfwd.h" +#include "listener.h" extern int sessinitdone; /* Is set to 0 somewhere */ extern int exitflag; @@ -139,8 +139,8 @@ struct sshsession { /* TCP forwarding - where manage listeners */ #ifndef DISABLE_REMOTETCPFWD - struct TCPListener ** tcplisteners; - unsigned int tcplistensize; + struct Listener ** listeners; + unsigned int listensize; #endif }; diff --git a/svr-chansession.c b/svr-chansession.c index f5b4308..4dd4228 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -201,13 +201,13 @@ static int newchansess(struct Channel *channel) { channel->typedata = chansess; #ifndef DISABLE_X11FWD - chansess->x11fd = -1; + chansess->x11listener = NULL; chansess->x11authprot = NULL; chansess->x11authcookie = NULL; #endif #ifndef DISABLE_AGENTFWD - chansess->agentfd = -1; + chansess->agentlistener = NULL; chansess->agentfile = NULL; chansess->agentdir = NULL; #endif @@ -881,7 +881,7 @@ static void execchild(struct ChanSess *chansess) { /* only reached on error */ dropbear_exit("child failed"); } - + const struct ChanType svrchansess = { 0, /* sepfds */ "session", /* name */ diff --git a/svr-session.c b/svr-session.c index 8e8eaea..2a97f94 100644 --- a/svr-session.c +++ b/svr-session.c @@ -35,7 +35,7 @@ #include "channel.h" #include "chansession.h" #include "atomicio.h" -#include "localtcpfwd.h" +#include "tcpfwd-direct.h" static void svr_remoteclosed(); diff --git a/tcpfwd-direct.c b/tcpfwd-direct.c new file mode 100644 index 0000000..81cd415 --- /dev/null +++ b/tcpfwd-direct.c @@ -0,0 +1,154 @@ +#include "includes.h" +#include "session.h" +#include "dbutil.h" +#include "channel.h" +#include "tcpfwd-direct.h" + +#ifndef DISABLE_TCPFWD_DIRECT +static int newtcpdirect(struct Channel * channel); +static int newtcp(const char * host, int port); + +const struct ChanType chan_tcpdirect = { + 0, /* sepfds */ + "direct-tcpip", + newtcpdirect, /* init */ + NULL, /* checkclose */ + NULL, /* reqhandler */ + NULL /* closehandler */ +}; + + +/* Called upon creating a new direct tcp channel (ie we connect out to an + * address */ +static int newtcpdirect(struct Channel * channel) { + + unsigned char* desthost = NULL; + unsigned int destport; + unsigned char* orighost = NULL; + unsigned int origport; + int sock; + int len; + int ret = DROPBEAR_FAILURE; + + if (ses.opts->nolocaltcp) { + TRACE(("leave newtcpdirect: local tcp forwarding disabled")); + goto out; + } + + desthost = buf_getstring(ses.payload, &len); + if (len > MAX_HOST_LEN) { + TRACE(("leave newtcpdirect: desthost too long")); + goto out; + } + + destport = buf_getint(ses.payload); + + orighost = buf_getstring(ses.payload, &len); + if (len > MAX_HOST_LEN) { + TRACE(("leave newtcpdirect: orighost too long")); + goto out; + } + + origport = buf_getint(ses.payload); + + /* best be sure */ + if (origport > 65535 || destport > 65535) { + TRACE(("leave newtcpdirect: port > 65535")); + goto out; + } + + sock = newtcp(desthost, destport); + if (sock < 0) { + TRACE(("leave newtcpdirect: sock failed")); + goto out; + } + + ses.maxfd = MAX(ses.maxfd, sock); + + /* Note that infd is actually the "outgoing" direction on the + * tcp connection, vice versa for outfd. + * We don't set outfd, that will get set after the connection's + * progress succeeds */ + channel->infd = sock; + channel->initconn = 1; + + ret = DROPBEAR_SUCCESS; + +out: + m_free(desthost); + m_free(orighost); + TRACE(("leave newtcpdirect: ret %d", ret)); + return ret; +} + +/* Initiate a new TCP connection - this is non-blocking, so the socket + * returned will need to be checked for success when it is first written. + * Similarities with OpenSSH's connect_to() are not coincidental. + * Returns -1 on failure */ +static int newtcp(const char * host, int port) { + + int sock = -1; + char portstring[6]; + struct addrinfo *res = NULL, *ai; + int val; + + struct addrinfo hints; + + TRACE(("enter newtcp")); + + memset(&hints, 0, sizeof(hints)); + /* TCP, either ip4 or ip6 */ + hints.ai_socktype = SOCK_STREAM; + hints.ai_family = PF_UNSPEC; + + snprintf(portstring, sizeof(portstring), "%d", port); + if (getaddrinfo(host, portstring, &hints, &res) != 0) { + if (res) { + freeaddrinfo(res); + } + TRACE(("leave newtcp: failed getaddrinfo")); + return -1; + } + + /* Use the first socket that works */ + for (ai = res; ai != NULL; ai = ai->ai_next) { + + if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) { + continue; + } + + sock = socket(ai->ai_family, SOCK_STREAM, 0); + if (sock < 0) { + TRACE(("TCP socket() failed")); + continue; + } + + if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { + close(sock); + TRACE(("TCP non-blocking failed")); + continue; + } + + /* non-blocking, so it might return without success (EINPROGRESS) */ + if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { + if (errno == EINPROGRESS) { + TRACE(("connect in progress")); + } else { + close(sock); + TRACE(("TCP connect failed")); + continue; + } + } + break; + } + + freeaddrinfo(res); + + if (ai == NULL) { + return -1; + } + + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val)); + return sock; +} +#endif /* DISABLE_TCPFWD_DIRECT */ diff --git a/tcpfwd-direct.h b/tcpfwd-direct.h new file mode 100644 index 0000000..20cd278 --- /dev/null +++ b/tcpfwd-direct.h @@ -0,0 +1,34 @@ +/* + * Dropbear - a SSH2 server + * + * Copyright (c) 2002,2003 Matt Johnston + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ +#ifndef _TCPFWD_DIRECT_H_ +#define _TCPFWD_DIRECT_H_ +#ifndef DISABLE_TCFWD_DIRECT + +#include "includes.h" +#include "channel.h" + +extern const struct ChanType chan_tcpdirect; + +#endif +#endif diff --git a/tcpfwd-remote.c b/tcpfwd-remote.c new file mode 100644 index 0000000..9996dbd --- /dev/null +++ b/tcpfwd-remote.c @@ -0,0 +1,319 @@ +#include "includes.h" +#include "ssh.h" +#include "tcpfwd-remote.h" +#include "dbutil.h" +#include "session.h" +#include "buffer.h" +#include "packet.h" +#include "listener.h" + +#ifndef DISABLE_REMOTETCPFWD + +struct RemoteTCP { + + unsigned char* addr; + unsigned int port; + +}; + +static void send_msg_request_success(); +static void send_msg_request_failure(); +static int cancelremotetcp(); +static int remotetcpreq(); +static int listen_tcpfwd(unsigned char* bindaddr, unsigned int port); +static void acceptremote(struct Listener *listener); + +/* At the moment this is completely used for tcp code (with the name reflecting + * that). If new request types are added, this should be replaced with code + * similar to the request-switching in chansession.c */ +void recv_msg_global_request_remotetcp() { + + unsigned char* reqname = NULL; + unsigned int namelen; + unsigned int wantreply = 0; + int ret = DROPBEAR_FAILURE; + + TRACE(("enter recv_msg_global_request_remotetcp")); + + if (ses.opts->noremotetcp) { + TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled")); + goto out; + } + + reqname = buf_getstring(ses.payload, &namelen); + wantreply = buf_getbyte(ses.payload); + + if (namelen > MAXNAMLEN) { + TRACE(("name len is wrong: %d", namelen)); + goto out; + } + + if (strcmp("tcpip-forward", reqname) == 0) { + ret = remotetcpreq(); + } else if (strcmp("cancel-tcpip-forward", reqname) == 0) { + ret = cancelremotetcp(); + } else { + TRACE(("reqname isn't tcpip-forward: '%s'", reqname)); + } + +out: + if (wantreply) { + if (ret == DROPBEAR_SUCCESS) { + send_msg_request_success(); + } else { + send_msg_request_failure(); + } + } + + m_free(reqname); + + TRACE(("leave recv_msg_global_request")); +} + +static const struct ChanType chan_tcpremote = { + 0, /* sepfds */ + "forwarded-tcpip", + NULL, + NULL, + NULL, + NULL +}; + + +static void acceptremote(struct Listener *listener) { + + int fd; + struct sockaddr addr; + int len; + char ipstring[NI_MAXHOST], portstring[NI_MAXSERV]; + struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); + + len = sizeof(addr); + + fd = accept(listener->sock, &addr, &len); + if (fd < 0) { + return; + } + + if (getnameinfo(&addr, len, ipstring, sizeof(ipstring), portstring, + sizeof(portstring), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { + return; + } + + if (send_msg_channel_open_init(fd, &chan_tcpremote) == DROPBEAR_SUCCESS) { + + buf_putstring(ses.writepayload, tcpinfo->addr, + strlen(tcpinfo->addr)); + buf_putint(ses.writepayload, tcpinfo->port); + buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); + buf_putint(ses.writepayload, atol(portstring)); + encrypt_packet(); + + } else { + /* XXX debug? */ + close(fd); + } +} + +static void cleanupremote(struct Listener *listener) { + + struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); + + m_free(tcpinfo->addr); + m_free(tcpinfo); +} + +static void send_msg_request_success() { + + CHECKCLEARTOWRITE(); + buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS); + encrypt_packet(); + +} + +static void send_msg_request_failure() { + + CHECKCLEARTOWRITE(); + buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE); + encrypt_packet(); + +} + +static int matchtcp(void* typedata1, void* typedata2) { + + const struct RemoteTCP *info1 = (struct RemoteTCP*)typedata1; + const struct RemoteTCP *info2 = (struct RemoteTCP*)typedata2; + + return info1->port == info2->port + && (strcmp(info1->addr, info2->addr) == 0); +} + +static int cancelremotetcp() { + + int ret = DROPBEAR_FAILURE; + unsigned char * bindaddr = NULL; + unsigned int addrlen; + unsigned int port; + struct Listener * listener = NULL; + struct RemoteTCP tcpinfo; + + TRACE(("enter cancelremotetcp")); + + bindaddr = buf_getstring(ses.payload, &addrlen); + if (addrlen > MAX_IP_LEN) { + TRACE(("addr len too long: %d", addrlen)); + goto out; + } + + port = buf_getint(ses.payload); + + tcpinfo.addr = bindaddr; + tcpinfo.port = port; + listener = get_listener(CHANNEL_ID_TCPFORWARDED, &tcpinfo, matchtcp); + if (listener) { + remove_listener( listener ); + ret = DROPBEAR_SUCCESS; + } + +out: + m_free(bindaddr); + TRACE(("leave cancelremotetcp")); + return ret; +} + +static int remotetcpreq() { + + int ret = DROPBEAR_FAILURE; + unsigned char * bindaddr = NULL; + unsigned int addrlen; + unsigned int port; + + TRACE(("enter remotetcpreq")); + + bindaddr = buf_getstring(ses.payload, &addrlen); + if (addrlen > MAX_IP_LEN) { + TRACE(("addr len too long: %d", addrlen)); + goto out; + } + + port = buf_getint(ses.payload); + + if (port == 0) { + dropbear_log(LOG_INFO, "Server chosen tcpfwd ports are unsupported"); + goto out; + } + + if (port < 1 || port > 65535) { + TRACE(("invalid port: %d", port)); + goto out; + } + + /* XXX matt - server change + if (ses.authstate.pw->pw_uid != 0 + && port < IPPORT_RESERVED) { + TRACE(("can't assign port < 1024 for non-root")); + goto out; + } + */ + + ret = listen_tcpfwd(bindaddr, port); + +out: + if (ret == DROPBEAR_FAILURE) { + /* we only free it if a listener wasn't created, since the listener + * has to remember it if it's to be cancelled */ + m_free(bindaddr); + } + TRACE(("leave remotetcpreq")); + return ret; +} + +static int listen_tcpfwd(unsigned char* bindaddr, unsigned int port) { + + struct RemoteTCP * tcpinfo = NULL; + char portstring[6]; /* "65535\0" */ + struct addrinfo *res = NULL, *ai = NULL; + struct addrinfo hints; + int sock = -1; + struct Listener *listener = NULL; + + TRACE(("enter listen_tcpfwd")); + + /* first we try to bind, so don't need to do so much cleanup on failure */ + snprintf(portstring, sizeof(portstring), "%d", port); + memset(&hints, 0x0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_family = PF_INET; + hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; + + if (getaddrinfo(bindaddr, portstring, &hints, &res) < 0) { + TRACE(("leave listen_tcpfwd: getaddrinfo failed: %s", + strerror(errno))); + goto done; + } + + /* find the first one which works */ + for (ai = res; ai != NULL; ai = ai->ai_next) { + if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) { + continue; + } + + sock = socket(ai->ai_family, SOCK_STREAM, 0); + if (sock < 0) { + TRACE(("socket failed: %s", strerror(errno))); + goto fail; + } + + if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { + TRACE(("bind failed: %s", strerror(errno))); + goto fail; + } + + if (listen(sock, 20) < 0) { + TRACE(("listen failed: %s", strerror(errno))); + goto fail; + } + + if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { + TRACE(("fcntl nonblocking failed: %s", strerror(errno))); + goto fail; + } + + /* success */ + break; + +fail: + close(sock); + } + + + if (ai == NULL) { + TRACE(("no successful sockets")); + goto done; + } + + tcpinfo = (struct RemoteTCP*)m_malloc(sizeof(struct RemoteTCP)); + tcpinfo->addr = bindaddr; + tcpinfo->port = port; + + listener = new_listener(sock, CHANNEL_ID_TCPFORWARDED, tcpinfo, + acceptremote, cleanupremote); + + if (listener == NULL) { + m_free(tcpinfo); + } + +done: + if (res) { + freeaddrinfo(res); + } + + TRACE(("leave listen_tcpfwd")); + if (listener == NULL) { + return DROPBEAR_FAILURE; + } else { + return DROPBEAR_SUCCESS; + } +} + +#endif /* DISABLE_REMOTETCPFWD */ diff --git a/tcpfwd-remote.h b/tcpfwd-remote.h new file mode 100644 index 0000000..64dbed3 --- /dev/null +++ b/tcpfwd-remote.h @@ -0,0 +1,6 @@ +#ifndef _REMOTETCPFWD_H +#define _REMOTETCPFWD_H + +void recv_msg_global_request_remotetcp(); + +#endif /* _REMOTETCPFWD_H */ diff --git a/tcpfwd.c b/tcpfwd.c deleted file mode 100644 index d95970a..0000000 --- a/tcpfwd.c +++ /dev/null @@ -1,124 +0,0 @@ -#include "includes.h" -#include "tcpfwd.h" -#include "session.h" -#include "dbutil.h" - -void tcp_fwd_initialise() { - - /* just one slot to start with */ - ses.tcplisteners = - (struct TCPListener**)m_malloc(sizeof(struct TCPListener*)); - ses.tcplistensize = 1; - ses.tcplisteners[0] = NULL; - -} - -void set_tcp_fwd_fds(fd_set * readfds) { - - unsigned int i; - struct TCPListener *listener; - - /* check each in turn */ - for (i = 0; i < ses.tcplistensize; i++) { - listener = ses.tcplisteners[i]; - if (listener != NULL) { - FD_SET(listener->sock, readfds); - } - } -} - - -void handle_tcp_fwd(fd_set * readfds) { - - unsigned int i; - struct TCPListener *listener; - - /* check each in turn */ - for (i = 0; i < ses.tcplistensize; i++) { - listener = ses.tcplisteners[i]; - if (listener != NULL) { - if (FD_ISSET(listener->sock, readfds)) { - listener->accepter(listener); - } - } - } -} - - -/* accepter(int fd, void* typedata) is a function to accept connections, - * cleanup(void* typedata) happens when cleaning up */ -int new_fwd(int sock, int type, void* typedata, - void (*accepter)(struct TCPListener*), - void (*cleanup)(struct TCPListener*)) { - - unsigned int i, j; - struct TCPListener *newtcp = NULL; - /* try get a new structure to hold it */ - for (i = 0; i < ses.tcplistensize; i++) { - if (ses.tcplisteners[i] == NULL) { - break; - } - } - - /* or create a new one */ - if (i == ses.tcplistensize) { - if (ses.tcplistensize > MAX_TCPLISTENERS) { - TRACE(("leave newlistener: too many already")); - close(sock); - return DROPBEAR_FAILURE; - } - - ses.tcplisteners = (struct TCPListener**)m_realloc(ses.tcplisteners, - (ses.tcplistensize+TCP_EXTEND_SIZE) - *sizeof(struct TCPListener*)); - - ses.tcplistensize += TCP_EXTEND_SIZE; - - for (j = i; j < ses.tcplistensize; j++) { - ses.tcplisteners[j] = NULL; - } - } - - ses.maxfd = MAX(ses.maxfd, sock); - - newtcp = (struct TCPListener*)m_malloc(sizeof(struct TCPListener)); - newtcp->index = i; - newtcp->type = type; - newtcp->typedata = typedata; - newtcp->sock = sock; - newtcp->accepter = accepter; - newtcp->cleanup = cleanup; - - ses.tcplisteners[i] = newtcp; - return DROPBEAR_SUCCESS; -} - -/* Return the first listener which matches the type-specific comparison - * function */ -struct TCPListener * get_listener(int type, void* typedata, - int (*match)(void*, void*)) { - - unsigned int i; - struct TCPListener* listener; - - for (i = 0, listener = ses.tcplisteners[i]; i < ses.tcplistensize; i++) { - if (listener->type == type - && match(typedata, listener->typedata)) { - return listener; - } - } - - return NULL; -} - -void remove_listener(struct TCPListener* listener) { - - if (listener->cleanup) { - listener->cleanup(listener); - } - - close(listener->sock); - ses.tcplisteners[listener->index] = NULL; - m_free(listener); - -} diff --git a/tcpfwd.h b/tcpfwd.h deleted file mode 100644 index 85d7373..0000000 --- a/tcpfwd.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _TCPFWD_H -#define _TCPFWD_H - -#define MAX_TCPLISTENERS 20 -#define TCP_EXTEND_SIZE 1 - -struct TCPListener { - - int sock; - - int index; /* index in the array of listeners */ - - void (*accepter)(struct TCPListener*); - void (*cleanup)(struct TCPListener*); - - int type; /* CHANNEL_ID_X11, CHANNEL_ID_AGENT, - CHANNEL_ID_TCPDIRECT (for clients), - CHANNEL_ID_TCPFORWARDED (for servers) */ - - void *typedata; - -}; - -void tcp_fwd_initialise(); -void handle_tcp_fwd(fd_set * readfds); -void set_tcp_fwd_fds(fd_set * readfds); - -int new_fwd(int sock, int type, void* typedata, - void (*accepter)(struct TCPListener*), - void (*cleanup)(struct TCPListener*)); - -struct TCPListener * get_listener(int type, void* typedata, - int (*match)(void*, void*)); - -void remove_listener(struct TCPListener* listener); - -#endif /* _TCPFWD_H */ diff --git a/x11fwd.c b/x11fwd.c index 229b0df..e1b6961 100644 --- a/x11fwd.c +++ b/x11fwd.c @@ -37,6 +37,8 @@ #define X11BASEPORT 6000 #define X11BINDBASE 6010 +static void x11accept(struct Listener* listener); +static void x11cleanup(struct Listener *listener); static int bindport(int fd); static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr); @@ -44,8 +46,10 @@ static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr); /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ int x11req(struct ChanSess * chansess) { + int fd; + /* we already have an x11 connection */ - if (chansess->x11fd != -1) { + if (chansess->x11listener != NULL) { return DROPBEAR_FAILURE; } @@ -55,62 +59,71 @@ int x11req(struct ChanSess * chansess) { chansess->x11screennum = buf_getint(ses.payload); /* create listening socket */ - chansess->x11fd = socket(PF_INET, SOCK_STREAM, 0); - if (chansess->x11fd < 0) { + fd = socket(PF_INET, SOCK_STREAM, 0); + if (fd < 0) { goto fail; } /* allocate port and bind */ - chansess->x11port = bindport(chansess->x11fd); + chansess->x11port = bindport(fd); if (chansess->x11port < 0) { goto fail; } /* listen */ - if (listen(chansess->x11fd, 20) < 0) { + if (listen(fd, 20) < 0) { goto fail; } /* set non-blocking */ - if (fcntl(chansess->x11fd, F_SETFL, O_NONBLOCK) < 0) { + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { goto fail; } - /* channel.c's channel fd code will handle the socket now */ - - /* set the maxfd so that select() loop will notice it */ - ses.maxfd = MAX(ses.maxfd, chansess->x11fd); + /* listener code will handle the socket now. + * No cleanup handler needed, since listener_remove only happens + * from our cleanup anyway */ + chansess->x11listener = new_listener( fd, 0, chansess, x11accept, NULL); + if (chansess->x11listener == NULL) { + goto fail; + } return DROPBEAR_SUCCESS; fail: /* cleanup */ - x11cleanup(chansess); + m_free(chansess->x11authprot); + m_free(chansess->x11authcookie); + close(fd); return DROPBEAR_FAILURE; } /* accepts a new X11 socket */ /* returns DROPBEAR_FAILURE or DROPBEAR_SUCCESS */ -int x11accept(struct ChanSess * chansess) { +static void x11accept(struct Listener* listener) { int fd; struct sockaddr_in addr; int len; + int ret; len = sizeof(addr); - fd = accept(chansess->x11fd, (struct sockaddr*)&addr, &len); + fd = accept(listener->sock, (struct sockaddr*)&addr, &len); if (fd < 0) { - return DROPBEAR_FAILURE; + return; } /* if single-connection we close it up */ - if (chansess->x11singleconn) { - x11cleanup(chansess); + if (((struct ChanSess *)(listener->typedata))->x11singleconn) { + x11cleanup(listener); } - return send_msg_channel_open_x11(fd, &addr); + ret = send_msg_channel_open_x11(fd, &addr); + if (ret == DROPBEAR_FAILURE) { + close(fd); + } } /* This is called after switching to the user, and sets up the xauth @@ -121,7 +134,7 @@ void x11setauth(struct ChanSess *chansess) { FILE * authprog; int val; - if (chansess->x11fd == -1) { + if (chansess->x11listener == NULL) { return; } @@ -154,24 +167,31 @@ void x11setauth(struct ChanSess *chansess) { } } -void x11cleanup(struct ChanSess * chansess) { +static void x11cleanup(struct Listener *listener) { - if (chansess->x11fd == -1) { - return; - } + struct ChanSess *chansess = (struct ChanSess*)listener->typedata; m_free(chansess->x11authprot); m_free(chansess->x11authcookie); - close(chansess->x11fd); - chansess->x11fd = -1; + remove_listener(listener); + chansess->x11listener = NULL; } +static const struct ChanType chan_x11 = { + 0, /* sepfds */ + "x11", + NULL, /* inithandler */ + NULL, /* checkclose */ + NULL, /* reqhandler */ + NULL /* closehandler */ +}; + + static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr) { char* ipstring; - if (send_msg_channel_open_init(fd, CHANNEL_ID_X11, "x11") - == DROPBEAR_SUCCESS) { + if (send_msg_channel_open_init(fd, &chan_x11) == DROPBEAR_SUCCESS) { ipstring = inet_ntoa(addr->sin_addr); buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); buf_putint(ses.writepayload, addr->sin_port); diff --git a/x11fwd.h b/x11fwd.h index 889033f..6cf4b96 100644 --- a/x11fwd.h +++ b/x11fwd.h @@ -30,8 +30,6 @@ #include "channel.h" int x11req(struct ChanSess * chansess); -int x11accept(struct ChanSess * chansess); -void x11cleanup(struct ChanSess * chansess); void x11setauth(struct ChanSess *chansess); #endif /* DROPBEAR_X11FWD */ -- cgit v1.2.3 From 9f369bc42ff9e01ab9d72edd74b1d838b69c1c78 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 3 Jun 2004 17:22:48 +0000 Subject: Mostly done with the listener changeover --HG-- extra : convert_revision : 1a4dca8836a2a04a21fb675c718a549a8d445d25 --- agentfwd.h | 3 +- dbutil.c | 12 +++++- dbutil.h | 1 + svr-agentfwd.c | 114 ++++++++++++++++++++++++++++++++---------------------- svr-auth.c | 8 ++-- svr-chansession.c | 4 +- tcpfwd-direct.c | 2 +- tcpfwd-remote.c | 2 +- x11fwd.c | 3 +- x11fwd.h | 1 + 10 files changed, 90 insertions(+), 60 deletions(-) (limited to 'svr-chansession.c') diff --git a/agentfwd.h b/agentfwd.h index 0b42cb2..a0f675d 100644 --- a/agentfwd.h +++ b/agentfwd.h @@ -30,9 +30,8 @@ #include "channel.h" int agentreq(struct ChanSess * chansess); -int agentaccept(struct ChanSess * chansess); -void agentcleanup(struct ChanSess * chansess); void agentsetauth(struct ChanSess *chansess); +void agentcleanup(struct ChanSess * chansess); void agentset(struct ChanSess *chansess); #ifdef __hpux diff --git a/dbutil.c b/dbutil.c index e03d648..2494f38 100644 --- a/dbutil.c +++ b/dbutil.c @@ -158,7 +158,7 @@ char* getaddrhostname(struct sockaddr * addr) { retstring = host->h_name; } - return strdup(retstring); + return m_strdup(retstring); } #ifdef DEBUG_TRACE void printhex(unsigned char* buf, int len) { @@ -263,6 +263,16 @@ void * m_malloc(size_t size) { } +void * m_strdup(const char * str) { + char* ret; + + ret = strdup(str); + if (ret == NULL) { + dropbear_exit("m_strdup failed"); + } + return ret; +} + void __m_free(void* ptr) { if (ptr != NULL) { free(ptr); diff --git a/dbutil.h b/dbutil.h index d13b13e..3888452 100644 --- a/dbutil.h +++ b/dbutil.h @@ -51,6 +51,7 @@ int buf_readfile(buffer* buf, const char* filename); int m_close(int fd); void * m_malloc(size_t size); +void * m_strdup(const char * str); void * m_realloc(void* ptr, size_t size); #define m_free(X) __m_free(X); (X) = NULL; void __m_free(void* ptr); diff --git a/svr-agentfwd.c b/svr-agentfwd.c index f8af30e..4c49e45 100644 --- a/svr-agentfwd.c +++ b/svr-agentfwd.c @@ -38,45 +38,52 @@ #include "packet.h" #include "buffer.h" #include "random.h" +#include "listener.h" #define AGENTDIRPREFIX "/tmp/dropbear-" static int send_msg_channel_open_agent(int fd); -static int bindagent(struct ChanSess * chansess); +static int bindagent(int fd, struct ChanSess * chansess); +static void agentaccept(struct Listener * listener); /* Handles client requests to start agent forwarding, sets up listening socket. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ int agentreq(struct ChanSess * chansess) { - if (chansess->agentfd != -1) { + int fd; + + if (chansess->agentlistener != NULL) { return DROPBEAR_FAILURE; } /* create listening socket */ - chansess->agentfd = socket(PF_UNIX, SOCK_STREAM, 0); - if (chansess->agentfd < 0) { + fd = socket(PF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { goto fail; } /* create the unix socket dir and file */ - if (bindagent(chansess) == DROPBEAR_FAILURE) { + if (bindagent(fd, chansess) == DROPBEAR_FAILURE) { return DROPBEAR_FAILURE; } /* listen */ - if (listen(chansess->agentfd, 20) < 0) { + if (listen(fd, 20) < 0) { goto fail; } /* set non-blocking */ - if (fcntl(chansess->agentfd, F_SETFL, O_NONBLOCK) < 0) { + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { goto fail; } - /* channel.c's channel fd code will handle the socket now */ + /* pass if off to listener */ + chansess->agentlistener = new_listener( fd, 0, chansess, + agentaccept, NULL); - /* set the maxfd so that select() loop will notice it */ - ses.maxfd = MAX(ses.maxfd, chansess->agentfd); + if (chansess->agentlistener == NULL) { + goto fail; + } return DROPBEAR_SUCCESS; @@ -90,16 +97,18 @@ fail: /* accepts a connection on the forwarded socket and opens a new channel for it * back to the client */ /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ -int agentaccept(struct ChanSess * chansess) { +static void agentaccept(struct Listener * listener) { int fd; - fd = accept(chansess->agentfd, NULL, NULL); + fd = accept(listener->sock, NULL, NULL); if (fd < 0) { - return DROPBEAR_FAILURE; + return; } - return send_msg_channel_open_agent(fd); + if (send_msg_channel_open_agent(listener->sock) != DROPBEAR_SUCCESS) { + close(fd); + } } @@ -110,7 +119,7 @@ void agentset(struct ChanSess * chansess) { char *path = NULL; int len; - if (chansess->agentfd == -1) { + if (chansess->agentlistener == NULL) { return; } @@ -131,46 +140,57 @@ void agentcleanup(struct ChanSess * chansess) { gid_t gid; int len; - if (chansess->agentfd == -1) { - return; + if (chansess->agentlistener != NULL) { + remove_listener(chansess->agentlistener); + chansess->agentlistener = NULL; } - close(chansess->agentfd); + if (chansess->agentfile && chansess->agentdir) { - /* Remove the dir as the user. That way they can't cause problems except - * for themselves */ - uid = getuid(); - gid = getgid(); - if ((setegid(svr_ses.authstate.pw->pw_gid)) < 0 || - (seteuid(svr_ses.authstate.pw->pw_uid)) < 0) { - dropbear_exit("failed to set euid"); - } + /* Remove the dir as the user. That way they can't cause problems except + * for themselves */ + uid = getuid(); + gid = getgid(); + if ((setegid(svr_ses.authstate.pw->pw_gid)) < 0 || + (seteuid(svr_ses.authstate.pw->pw_uid)) < 0) { + dropbear_exit("failed to set euid"); + } - /* 2 for "/" and "\0" */ - len = strlen(chansess->agentdir) + strlen(chansess->agentfile) + 2; + /* 2 for "/" and "\0" */ + len = strlen(chansess->agentdir) + strlen(chansess->agentfile) + 2; - path = m_malloc(len); - snprintf(path, len, "%s/%s", chansess->agentdir, chansess->agentfile); - unlink(path); - m_free(path); + path = m_malloc(len); + snprintf(path, len, "%s/%s", chansess->agentdir, chansess->agentfile); + unlink(path); + m_free(path); - rmdir(chansess->agentdir); + rmdir(chansess->agentdir); - if ((seteuid(uid)) < 0 || - (setegid(gid)) < 0) { - dropbear_exit("failed to revert euid"); - } + if ((seteuid(uid)) < 0 || + (setegid(gid)) < 0) { + dropbear_exit("failed to revert euid"); + } - m_free(chansess->agentfile); - m_free(chansess->agentdir); + m_free(chansess->agentfile); + m_free(chansess->agentdir); + } } +static const struct ChanType chan_agent = { + 0, /* sepfds */ + "auth-agent@openssh.com", + NULL, + NULL, + NULL, + NULL +}; + + /* helper for accepting an agent request */ static int send_msg_channel_open_agent(int fd) { - if (send_msg_channel_open_init(fd, CHANNEL_ID_AGENT, - "auth-agent@openssh.com") == DROPBEAR_SUCCESS) { + if (send_msg_channel_open_init(fd, &chan_agent) == DROPBEAR_SUCCESS) { encrypt_packet(); return DROPBEAR_SUCCESS; } else { @@ -180,7 +200,7 @@ static int send_msg_channel_open_agent(int fd) { /* helper for creating the agent socket-file returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ -static int bindagent(struct ChanSess * chansess) { +static int bindagent(int fd, struct ChanSess * chansess) { struct sockaddr_un addr; unsigned int prefix; @@ -225,13 +245,13 @@ bindsocket: * between subsequent user processes reusing socket fds (odds are now * 1/(2^64) */ genrandom((unsigned char*)&prefix, sizeof(prefix)); - snprintf(sockfile, sizeof(sockfile), "auth-%.8x-%d", prefix, - chansess->agentfd); + snprintf(sockfile, sizeof(sockfile), "auth-%.8x-%d", prefix, fd); + snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s", path, sockfile); - if (bind(chansess->agentfd, (struct sockaddr*)&addr, sizeof(addr)) == 0) { - chansess->agentdir = strdup(path); - chansess->agentfile = strdup(sockfile); + if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) { + chansess->agentdir = m_strdup(path); + chansess->agentfile = m_strdup(sockfile); ret = DROPBEAR_SUCCESS; } diff --git a/svr-auth.c b/svr-auth.c index 46ca70a..f6adb05 100644 --- a/svr-auth.c +++ b/svr-auth.c @@ -151,7 +151,7 @@ void recv_msg_userauth_request() { if (methodlen == AUTH_METHOD_PASSWORD_LEN && strncmp(methodname, AUTH_METHOD_PASSWORD, AUTH_METHOD_PASSWORD_LEN) == 0) { - passwordauth(username, userlen); + passwordauth(); goto out; } } @@ -162,7 +162,7 @@ void recv_msg_userauth_request() { if (methodlen == AUTH_METHOD_PUBKEY_LEN && strncmp(methodname, AUTH_METHOD_PUBKEY, AUTH_METHOD_PUBKEY_LEN) == 0) { - pubkeyauth(username, userlen); + pubkeyauth(); goto out; } #endif @@ -200,7 +200,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { } authclear(); svr_ses.authstate.pw = getpwnam((char*)username); - svr_ses.authstate.username = strdup(username); + svr_ses.authstate.username = m_strdup(username); m_free(svr_ses.authstate.printableuser); } @@ -214,7 +214,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { } /* We can set it once we know its a real user */ - svr_ses.authstate.printableuser = strdup(svr_ses.authstate.pw->pw_name); + svr_ses.authstate.printableuser = m_strdup(svr_ses.authstate.pw->pw_name); /* check for non-root if desired */ if (ses.opts->norootlogin && svr_ses.authstate.pw->pw_uid == 0) { diff --git a/svr-chansession.c b/svr-chansession.c index 4dd4228..c6526dc 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -419,7 +419,7 @@ static int sessionpty(struct ChanSess * chansess) { return DROPBEAR_FAILURE; } - chansess->tty = (char*)strdup(namebuf); + chansess->tty = (char*)m_strdup(namebuf); if (!chansess->tty) { dropbear_exit("out of memory"); /* TODO disconnect */ } @@ -541,7 +541,7 @@ static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, #ifdef SFTPSERVER_PATH if ((cmdlen == 4) && strncmp(chansess->cmd, "sftp", 4) == 0) { m_free(chansess->cmd); - chansess->cmd = strdup(SFTPSERVER_PATH); + chansess->cmd = m_strdup(SFTPSERVER_PATH); } else #endif { diff --git a/tcpfwd-direct.c b/tcpfwd-direct.c index 81cd415..1131602 100644 --- a/tcpfwd-direct.c +++ b/tcpfwd-direct.c @@ -9,7 +9,7 @@ static int newtcpdirect(struct Channel * channel); static int newtcp(const char * host, int port); const struct ChanType chan_tcpdirect = { - 0, /* sepfds */ + 1, /* sepfds */ "direct-tcpip", newtcpdirect, /* init */ NULL, /* checkclose */ diff --git a/tcpfwd-remote.c b/tcpfwd-remote.c index 9996dbd..880044f 100644 --- a/tcpfwd-remote.c +++ b/tcpfwd-remote.c @@ -71,7 +71,7 @@ out: } static const struct ChanType chan_tcpremote = { - 0, /* sepfds */ + 1, /* sepfds */ "forwarded-tcpip", NULL, NULL, diff --git a/x11fwd.c b/x11fwd.c index e1b6961..12a8f21 100644 --- a/x11fwd.c +++ b/x11fwd.c @@ -38,7 +38,6 @@ #define X11BINDBASE 6010 static void x11accept(struct Listener* listener); -static void x11cleanup(struct Listener *listener); static int bindport(int fd); static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr); @@ -167,7 +166,7 @@ void x11setauth(struct ChanSess *chansess) { } } -static void x11cleanup(struct Listener *listener) { +void x11cleanup(struct Listener *listener) { struct ChanSess *chansess = (struct ChanSess*)listener->typedata; diff --git a/x11fwd.h b/x11fwd.h index 6cf4b96..13cd11f 100644 --- a/x11fwd.h +++ b/x11fwd.h @@ -31,6 +31,7 @@ int x11req(struct ChanSess * chansess); void x11setauth(struct ChanSess *chansess); +void x11cleanup(struct Listener *listener); #endif /* DROPBEAR_X11FWD */ #endif /* _X11FWD_H_ */ -- cgit v1.2.3 From 62aab2227c2e1aca8e9b807bc203a1d6ecb14daf Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 20 Jul 2004 12:05:00 +0000 Subject: switching to global vars --HG-- extra : convert_revision : 800073097767c2ac153ab834cbcf0121cb765118 --- Makefile.in | 2 +- TODO | 2 + common-session.c | 11 ++---- dbutil.h | 1 - main.c | 27 ++++++------- runopts.h | 33 ++++++++++++---- session.h | 8 +--- svr-auth.c | 21 +++++----- svr-chansession.c | 7 ++-- svr-kex.c | 7 ++-- svr-runopts.c | 115 +++++++++++++++++++++++------------------------------- svr-session.c | 12 +++--- tcpfwd-direct.c | 3 +- tcpfwd-remote.c | 3 +- 14 files changed, 125 insertions(+), 127 deletions(-) (limited to 'svr-chansession.c') diff --git a/Makefile.in b/Makefile.in index 67e5e5f..4d6d342 100644 --- a/Makefile.in +++ b/Makefile.in @@ -6,7 +6,7 @@ COMMONOBJS=dbutil.o common-session.o common-packet.o common-algo.o buffer.o \ signkey.o rsa.o random.o common-channel.o \ common-chansession.o queue.o termcodes.o \ loginrec.o atomicio.o svr-x11fwd.o tcpfwd-direct.o compat.o \ - tcpfwd-remote.o listener.o process-packet.o + tcpfwd-remote.o listener.o process-packet.o common-runopts.o SVROBJS=svr-kex.o svr-algo.o svr-auth.o sshpty.o \ svr-authpasswd.o svr-authpubkey.o svr-session.o svr-service.o \ diff --git a/TODO b/TODO index 64cbd5e..8a567c2 100644 --- a/TODO +++ b/TODO @@ -24,4 +24,6 @@ Things which need doing: - CTR mode, SSH_MSG_IGNORE sending to improve CBC security - DH Group Exchange possibly +- Use m_burn for clearing sensitive items in LTM/LTC + - fix scp.c for IRIX diff --git a/common-session.c b/common-session.c index b7793f9..6e37e29 100644 --- a/common-session.c +++ b/common-session.c @@ -35,14 +35,14 @@ #include "channel.h" #include "atomicio.h" -struct sshsession ses; +struct sshsession ses; /* GLOBAL */ /* need to know if the session struct has been initialised, this way isn't the * cleanest, but works OK */ -int sessinitdone = 0; +int sessinitdone = 0; /* GLOBAL */ /* this is set when we get SIGINT or SIGTERM, the handler is in main.c */ -int exitflag = 0; +int exitflag = 0; /* GLOBAL */ static int ident_readln(int fd, char* buf, int count); @@ -51,7 +51,7 @@ void(*session_remoteclosed)() = NULL; /* called only at the start of a session, set up initial state */ -void common_session_init(int sock, runopts *opts) { +void common_session_init(int sock) { TRACE(("enter session_init")); @@ -61,8 +61,6 @@ void common_session_init(int sock, runopts *opts) { ses.sock = sock; ses.maxfd = sock; - ses.opts = opts; - ses.connecttimeout = 0; kexinitialise(); /* initialise the kex state */ @@ -128,7 +126,6 @@ void common_session_cleanup() { } m_free(ses.session_id); - freerunopts(ses.opts); m_burn(ses.keys, sizeof(struct key_context)); m_free(ses.keys); diff --git a/dbutil.h b/dbutil.h index 3888452..3da6b2f 100644 --- a/dbutil.h +++ b/dbutil.h @@ -32,7 +32,6 @@ #ifndef DISABLE_SYSLOG void startsyslog(); #endif -extern int usingsyslog; extern void (*_dropbear_exit)(int exitcode, const char* format, va_list param); extern void (*_dropbear_log)(int priority, const char* format, va_list param); diff --git a/main.c b/main.c index 0087895..b9ff7aa 100644 --- a/main.c +++ b/main.c @@ -29,7 +29,7 @@ #include "signkey.h" #include "runopts.h" -static int listensockets(int *sock, runopts * opts, int *maxfd); +static int listensockets(int *sock, int *maxfd); static void sigchld_handler(int dummy); static void sigsegv_handler(int); static void sigintterm_handler(int fish); @@ -53,7 +53,6 @@ int main(int argc, char ** argv) int remoteaddrlen; int listensocks[MAX_LISTEN_ADDR]; unsigned int listensockcount = 0; - runopts * opts; FILE * pidfile; int childsock; @@ -66,13 +65,13 @@ int main(int argc, char ** argv) _dropbear_log = svr_dropbear_log; /* get commandline options */ - opts = svr_getopts(argc, argv); + svr_getopts(argc, argv); /* fork */ - if (opts->forkbg) { + if (svr_opts.forkbg) { int closefds = 0; #ifndef DEBUG_TRACE - if (!usingsyslog) { + if (!svr_opts.usingsyslog) { closefds = 1; } #endif @@ -83,13 +82,13 @@ int main(int argc, char ** argv) } #ifndef DISABLE_SYSLOG - if (usingsyslog) { + if (svr_opts.usingsyslog) { startsyslog(); } #endif /* should be done after syslog is working */ - if (opts->forkbg) { + if (svr_opts.forkbg) { dropbear_log(LOG_INFO, "Running in background"); } else { dropbear_log(LOG_INFO, "Not forking"); @@ -128,7 +127,7 @@ int main(int argc, char ** argv) /* Set up the listening sockets */ /* XXX XXX ports */ - listensockcount = listensockets(listensocks, opts, &maxsock); + listensockcount = listensockets(listensocks, &maxsock); /* incoming connection select loop */ for(;;) { @@ -242,7 +241,7 @@ int main(int argc, char ** argv) dropbear_exit("Couldn't close socket"); } /* start the session */ - svr_session(childsock, opts, childpipe[1], &remoteaddr); + svr_session(childsock, childpipe[1], &remoteaddr); /* don't return */ assert(0); } @@ -288,7 +287,7 @@ static void sigintterm_handler(int fish) { } /* Set up listening sockets for all the requested ports */ -static int listensockets(int *sock, runopts * opts, int *maxfd) { +static int listensockets(int *sock, int *maxfd) { int listensock; /* listening fd */ struct sockaddr_in listen_addr; @@ -296,7 +295,7 @@ static int listensockets(int *sock, runopts * opts, int *maxfd) { unsigned int i; int val; - for (i = 0; i < opts->portcount; i++) { + for (i = 0; i < svr_opts.portcount; i++) { /* iterate through all the sockets to listen on */ listensock = socket(PF_INET, SOCK_STREAM, 0); @@ -319,13 +318,13 @@ static int listensockets(int *sock, runopts * opts, int *maxfd) { memset((void*)&listen_addr, 0x0, sizeof(listen_addr)); listen_addr.sin_family = AF_INET; - listen_addr.sin_port = htons(opts->ports[i]); + listen_addr.sin_port = htons(svr_opts.ports[i]); listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); memset(&(listen_addr.sin_zero), '\0', 8); if (bind(listensock, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) { - dropbear_exit("Bind failed port %d", opts->ports[i]); + dropbear_exit("Bind failed port %d", svr_opts.ports[i]); } /* listen */ @@ -342,5 +341,5 @@ static int listensockets(int *sock, runopts * opts, int *maxfd) { *maxfd = MAX(listensock, *maxfd); } - return opts->portcount; + return svr_opts.portcount; } diff --git a/runopts.h b/runopts.h index cdf3af9..1bf1539 100644 --- a/runopts.h +++ b/runopts.h @@ -29,12 +29,23 @@ #include "signkey.h" #include "buffer.h" -struct SvrRunOpts { +typedef struct runopts { + + int nolocaltcp; + int noremotetcp; + +} runopts; + +extern runopts opts; + +typedef struct svr_runopts { char * rsakeyfile; char * dsskeyfile; char * bannerfile; + int forkbg; + int usingsyslog; /* ports is an array of the portcount listening ports */ uint16_t *ports; @@ -56,17 +67,23 @@ struct SvrRunOpts { int noauthpass; int norootpass; - int nolocaltcp; - int noremotetcp; - sign_key *hostkey; buffer * banner; -}; +} svr_runopts; + +extern svr_runopts svr_opts; + +void svr_getopts(int argc, char ** argv); + +/* Uncompleted XXX matt */ +typedef struct cli_runopts { + + int todo; -typedef struct SvrRunOpts runopts; +} cli_runopts; -runopts * getrunopts(int argc, char ** argv); -void freerunopts(runopts* opts); +extern cli_runopts cli_opts; +void cli_getopts(int argc, char ** argv); #endif /* _RUNOPTS_H_ */ diff --git a/session.h b/session.h index 1106144..cc8340d 100644 --- a/session.h +++ b/session.h @@ -32,14 +32,13 @@ #include "auth.h" #include "channel.h" #include "queue.h" -#include "runopts.h" #include "listener.h" #include "packet.h" extern int sessinitdone; /* Is set to 0 somewhere */ extern int exitflag; -void common_session_init(int sock, runopts *opts); +void common_session_init(int sock); void common_session_cleanup(); void checktimeouts(); void session_identification(); @@ -47,8 +46,7 @@ void session_identification(); extern void(*session_remoteclosed)(); /* Server */ -void svr_session(int sock, runopts *opts, int childpipe, - struct sockaddr *remoteaddr); +void svr_session(int sock, int childpipe, struct sockaddr *remoteaddr); void svr_dropbear_exit(int exitcode, const char* format, va_list param); void svr_dropbear_log(int priority, const char* format, va_list param); @@ -82,8 +80,6 @@ struct sshsession { /* Is it a client or server? */ unsigned char isserver; - runopts * opts; /* runtime options, incl hostkey, banner etc */ - long connecttimeout; /* time to disconnect if we have a timeout (for userauth etc), or 0 for no timeout */ diff --git a/svr-auth.c b/svr-auth.c index 386c3e1..7b588c0 100644 --- a/svr-auth.c +++ b/svr-auth.c @@ -34,6 +34,7 @@ #include "auth.h" #include "authpasswd.h" #include "authpubkey.h" +#include "runopts.h" static void authclear(); static int checkusername(unsigned char *username, unsigned int userlen); @@ -61,7 +62,7 @@ static void authclear() { svr_ses.authstate.authtypes |= AUTH_TYPE_PUBKEY; #endif #ifdef DROPBEAR_PASSWORD_AUTH - if (!ses.opts->noauthpass) { + if (svr_opts.noauthpass) { svr_ses.authstate.authtypes |= AUTH_TYPE_PASSWORD; } #endif @@ -73,7 +74,7 @@ static void authclear() { static void send_msg_userauth_banner() { TRACE(("enter send_msg_userauth_banner")); - if (ses.opts->banner == NULL) { + if (svr_opts.banner == NULL) { TRACE(("leave send_msg_userauth_banner: banner is NULL")); return; } @@ -81,13 +82,13 @@ static void send_msg_userauth_banner() { CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_BANNER); - buf_putstring(ses.writepayload, buf_getptr(ses.opts->banner, - ses.opts->banner->len), ses.opts->banner->len); + buf_putstring(ses.writepayload, buf_getptr(svr_opts.banner, + svr_opts.banner->len), svr_opts.banner->len); buf_putstring(ses.writepayload, "en", 2); encrypt_packet(); - buf_free(ses.opts->banner); - ses.opts->banner = NULL; + buf_free(svr_opts.banner); + svr_opts.banner = NULL; TRACE(("leave send_msg_userauth_banner")); } @@ -107,7 +108,7 @@ void recv_msg_userauth_request() { } /* send the banner if it exists, it will only exist once */ - if (ses.opts->banner) { + if (svr_opts.banner) { send_msg_userauth_banner(); } @@ -145,8 +146,8 @@ void recv_msg_userauth_request() { } #ifdef DROPBEAR_PASSWORD_AUTH - if (!ses.opts->noauthpass && - !(ses.opts->norootpass && svr_ses.authstate.pw->pw_uid == 0) ) { + if (!svr_opts.noauthpass && + !(svr_opts.norootpass && svr_ses.authstate.pw->pw_uid == 0) ) { /* user wants to try password auth */ if (methodlen == AUTH_METHOD_PASSWORD_LEN && strncmp(methodname, AUTH_METHOD_PASSWORD, @@ -217,7 +218,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { svr_ses.authstate.printableuser = m_strdup(svr_ses.authstate.pw->pw_name); /* check for non-root if desired */ - if (ses.opts->norootlogin && svr_ses.authstate.pw->pw_uid == 0) { + if (svr_opts.norootlogin && svr_ses.authstate.pw->pw_uid == 0) { TRACE(("leave checkusername: root login disabled")); dropbear_log(LOG_WARNING, "root login rejected"); send_msg_userauth_failure(0, 1); diff --git a/svr-chansession.c b/svr-chansession.c index c6526dc..cbc7ebe 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -36,6 +36,7 @@ #include "utmp.h" #include "x11fwd.h" #include "agentfwd.h" +#include "runopts.h" /* Handles sessions (either shells or programs) requested by the client */ @@ -690,7 +691,7 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { m_free(chansess->tty); #ifdef DO_MOTD - if (ses.opts->domotd) { + if (svr_opts.domotd) { /* don't show the motd if ~/.hushlogin exists */ /* 11 == strlen("/hushlogin\0") */ @@ -776,8 +777,8 @@ static void execchild(struct ChanSess *chansess) { unsigned int i; /* wipe the hostkey */ - sign_key_free(ses.opts->hostkey); - ses.opts->hostkey = NULL; + sign_key_free(svr_opts.hostkey); + svr_opts.hostkey = NULL; /* overwrite the prng state */ seedrandom(); diff --git a/svr-kex.c b/svr-kex.c index 80f7c0a..4dfa6a7 100644 --- a/svr-kex.c +++ b/svr-kex.c @@ -32,6 +32,7 @@ #include "packet.h" #include "bignum.h" #include "random.h" +#include "runopts.h" static void send_msg_kexdh_reply(mp_int *dh_e); @@ -125,7 +126,7 @@ static void send_msg_kexdh_reply(mp_int *dh_e) { /* Create the remainder of the hash buffer, to generate the exchange hash */ /* K_S, the host key */ - buf_put_pub_key(ses.kexhashbuf, ses.opts->hostkey, + buf_put_pub_key(ses.kexhashbuf, svr_opts.hostkey, ses.newkeys->algo_hostkey); /* e, exchange value sent by the client */ buf_putmpint(ses.kexhashbuf, dh_e); @@ -153,7 +154,7 @@ static void send_msg_kexdh_reply(mp_int *dh_e) { /* we can start creating the kexdh_reply packet */ CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY); - buf_put_pub_key(ses.writepayload, ses.opts->hostkey, + buf_put_pub_key(ses.writepayload, svr_opts.hostkey, ses.newkeys->algo_hostkey); /* put f */ @@ -161,7 +162,7 @@ static void send_msg_kexdh_reply(mp_int *dh_e) { mp_clear(&dh_f); /* calc the signature */ - buf_put_sign(ses.writepayload, ses.opts->hostkey, + buf_put_sign(ses.writepayload, svr_opts.hostkey, ses.newkeys->algo_hostkey, ses.hash, SHA1_HASH_SIZE); /* the SSH_MSG_KEXDH_REPLY is done */ diff --git a/svr-runopts.c b/svr-runopts.c index f7a427f..1f5b0c6 100644 --- a/svr-runopts.c +++ b/svr-runopts.c @@ -29,6 +29,8 @@ #include "dbutil.h" #include "algo.h" +svr_runopts svr_opts; /* GLOBAL */ + static sign_key * loadhostkeys(const char * dsskeyfile, const char * rsakeyfile); static int readhostkey(const char * filename, sign_key * hostkey, int type); @@ -84,38 +86,34 @@ static void printhelp(const char * progname) { DROPBEAR_MAX_PORTS, DROPBEAR_PORT); } -/* returns NULL on failure, or a pointer to a freshly allocated - * runopts structure */ -runopts * svr_getopts(int argc, char ** argv) { +void svr_getopts(int argc, char ** argv) { unsigned int i; char ** next = 0; - runopts * opts; unsigned int portnum = 0; char *portstring[DROPBEAR_MAX_PORTS]; unsigned int longport; /* see printhelp() for options */ - opts = (runopts*)m_malloc(sizeof(runopts)); - opts->rsakeyfile = NULL; - opts->dsskeyfile = NULL; - opts->bannerfile = NULL; - opts->banner = NULL; - opts->forkbg = 1; - opts->norootlogin = 0; - opts->noauthpass = 0; - opts->norootpass = 0; - opts->nolocaltcp = 0; - opts->noremotetcp = 0; + svr_opts.rsakeyfile = NULL; + svr_opts.dsskeyfile = NULL; + svr_opts.bannerfile = NULL; + svr_opts.banner = NULL; + svr_opts.forkbg = 1; + svr_opts.norootlogin = 0; + svr_opts.noauthpass = 0; + svr_opts.norootpass = 0; + opts.nolocaltcp = 0; + opts.noremotetcp = 0; /* not yet - opts->ipv4 = 1; - opts->ipv6 = 1; + svr_opts.ipv4 = 1; + svr_opts.ipv6 = 1; */ #ifdef DO_MOTD - opts->domotd = 1; + svr_opts.domotd = 1; #endif #ifndef DISABLE_SYSLOG - usingsyslog = 1; + svr_opts.usingsyslog = 1; #endif for (i = 1; i < (unsigned int)argc; i++) { @@ -131,34 +129,34 @@ runopts * svr_getopts(int argc, char ** argv) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'b': - next = &opts->bannerfile; + next = &svr_opts.bannerfile; break; #ifdef DROPBEAR_DSS case 'd': - next = &opts->dsskeyfile; + next = &svr_opts.dsskeyfile; break; #endif #ifdef DROPBEAR_RSA case 'r': - next = &opts->rsakeyfile; + next = &svr_opts.rsakeyfile; break; #endif case 'F': - opts->forkbg = 0; + svr_opts.forkbg = 0; break; #ifndef DISABLE_SYSLOG case 'E': - usingsyslog = 0; + svr_opts.usingsyslog = 0; break; #endif #ifndef DISABLE_LOCALTCPFWD case 'j': - opts->nolocaltcp = 1; + opts.nolocaltcp = 1; break; #endif #ifndef DISABLE_REMOTETCPFWD case 'k': - opts->noremotetcp = 1; + opts.noremotetcp = 1; break; #endif case 'p': @@ -171,18 +169,18 @@ runopts * svr_getopts(int argc, char ** argv) { #ifdef DO_MOTD /* motd is displayed by default, -m turns it off */ case 'm': - opts->domotd = 0; + svr_opts.domotd = 0; break; #endif case 'w': - opts->norootlogin = 1; + svr_opts.norootlogin = 1; break; #ifdef DROPBEAR_PASSWORD_AUTH case 's': - opts->noauthpass = 1; + svr_opts.noauthpass = 1; break; case 'g': - opts->norootpass = 1; + svr_opts.norootpass = 1; break; #endif case 'h': @@ -191,10 +189,10 @@ runopts * svr_getopts(int argc, char ** argv) { break; /* case '4': - opts->ipv4 = 0; + svr_opts.ipv4 = 0; break; case '6': - opts->ipv6 = 0; + svr_opts.ipv6 = 0; break; */ default: @@ -206,19 +204,19 @@ runopts * svr_getopts(int argc, char ** argv) { } } - if (opts->dsskeyfile == NULL) { - opts->dsskeyfile = DSS_PRIV_FILENAME; + if (svr_opts.dsskeyfile == NULL) { + svr_opts.dsskeyfile = DSS_PRIV_FILENAME; } - if (opts->rsakeyfile == NULL) { - opts->rsakeyfile = RSA_PRIV_FILENAME; + if (svr_opts.rsakeyfile == NULL) { + svr_opts.rsakeyfile = RSA_PRIV_FILENAME; } - opts->hostkey = loadhostkeys(opts->dsskeyfile, opts->rsakeyfile); + svr_opts.hostkey = loadhostkeys(svr_opts.dsskeyfile, svr_opts.rsakeyfile); - if (opts->bannerfile) { + if (svr_opts.bannerfile) { struct stat buf; - if (stat(opts->bannerfile, &buf) != 0) { + if (stat(svr_opts.bannerfile, &buf) != 0) { dropbear_exit("Error opening banner file '%s'", - opts->bannerfile); + svr_opts.bannerfile); } if (buf.st_size > MAX_BANNER_SIZE) { @@ -226,16 +224,16 @@ runopts * svr_getopts(int argc, char ** argv) { MAX_BANNER_SIZE); } - opts->banner = buf_new(buf.st_size); - if (buf_readfile(opts->banner, opts->bannerfile)!=DROPBEAR_SUCCESS) { + svr_opts.banner = buf_new(buf.st_size); + if (buf_readfile(svr_opts.banner, svr_opts.bannerfile)!=DROPBEAR_SUCCESS) { dropbear_exit("Error reading banner file '%s'", - opts->bannerfile); + svr_opts.bannerfile); } - buf_setpos(opts->banner, 0); + buf_setpos(svr_opts.banner, 0); } /* not yet - if (!(opts->ipv4 || opts->ipv6)) { + if (!(svr_opts.ipv4 || svr_opts.ipv6)) { fprintf(stderr, "You can't disable ipv4 and ipv6.\n"); exit(1); } @@ -244,17 +242,17 @@ runopts * svr_getopts(int argc, char ** argv) { /* create the array of listening ports */ if (portnum == 0) { /* non specified */ - opts->portcount = 1; - opts->ports = m_malloc(sizeof(uint16_t)); - opts->ports[0] = DROPBEAR_PORT; + svr_opts.portcount = 1; + svr_opts.ports = m_malloc(sizeof(uint16_t)); + svr_opts.ports[0] = DROPBEAR_PORT; } else { - opts->portcount = portnum; - opts->ports = (uint16_t*)m_malloc(sizeof(uint16_t)*portnum); + svr_opts.portcount = portnum; + svr_opts.ports = (uint16_t*)m_malloc(sizeof(uint16_t)*portnum); for (i = 0; i < portnum; i++) { if (portstring[i]) { longport = atoi(portstring[i]); if (longport <= 65535 && longport > 0) { - opts->ports[i] = (uint16_t)longport; + svr_opts.ports[i] = (uint16_t)longport; continue; } } @@ -263,23 +261,8 @@ runopts * svr_getopts(int argc, char ** argv) { } } - return opts; } -void freerunopts(runopts* opts) { - - if (!opts) { - return; - } - - if (opts->hostkey) { - sign_key_free(opts->hostkey); - opts->hostkey = NULL; - } - - m_free(opts->ports); - m_free(opts); -} /* returns success or failure */ static int readhostkey(const char * filename, sign_key * hostkey, int type) { diff --git a/svr-session.c b/svr-session.c index d3094ea..927a1c1 100644 --- a/svr-session.c +++ b/svr-session.c @@ -39,10 +39,11 @@ #include "service.h" #include "auth.h" #include "tcpfwd-remote.h" +#include "runopts.h" static void svr_remoteclosed(); -struct serversession svr_ses; +struct serversession svr_ses; /* GLOBAL */ static const packettype svr_packettypes[] = { /* TYPE, AUTHREQUIRED, FUNCTION */ @@ -69,15 +70,14 @@ static const struct ChanType *svr_chantypes[] = { NULL /* Null termination is mandatory. */ }; -void svr_session(int sock, runopts *opts, int childpipe, - struct sockaddr* remoteaddr) { +void svr_session(int sock, int childpipe, struct sockaddr* remoteaddr) { fd_set readfd, writefd; struct timeval timeout; int val; crypto_init(); - common_session_init(sock, opts); + common_session_init(sock); ses.remoteaddr = remoteaddr; ses.remotehost = getaddrhostname(remoteaddr); @@ -227,7 +227,7 @@ void svr_dropbear_log(int priority, const char* format, va_list param) { vsnprintf(printbuf, sizeof(printbuf), format, param); #ifndef DISABLE_SYSLOG - if (usingsyslog) { + if (svr_opts.usingsyslog) { syslog(priority, "%s", printbuf); } #endif @@ -238,7 +238,7 @@ void svr_dropbear_log(int priority, const char* format, va_list param) { havetrace = 1; #endif - if (!usingsyslog || havetrace) + if (!svr_opts.usingsyslog || havetrace) { timesec = time(NULL); if (strftime(datestr, sizeof(datestr), "%b %d %H:%M:%S", diff --git a/tcpfwd-direct.c b/tcpfwd-direct.c index 1131602..b6a2178 100644 --- a/tcpfwd-direct.c +++ b/tcpfwd-direct.c @@ -3,6 +3,7 @@ #include "dbutil.h" #include "channel.h" #include "tcpfwd-direct.h" +#include "runopts.h" #ifndef DISABLE_TCPFWD_DIRECT static int newtcpdirect(struct Channel * channel); @@ -30,7 +31,7 @@ static int newtcpdirect(struct Channel * channel) { int len; int ret = DROPBEAR_FAILURE; - if (ses.opts->nolocaltcp) { + if (opts.nolocaltcp) { TRACE(("leave newtcpdirect: local tcp forwarding disabled")); goto out; } diff --git a/tcpfwd-remote.c b/tcpfwd-remote.c index 16b1105..d0a67a1 100644 --- a/tcpfwd-remote.c +++ b/tcpfwd-remote.c @@ -6,6 +6,7 @@ #include "buffer.h" #include "packet.h" #include "listener.h" +#include "runopts.h" #ifndef DISABLE_REMOTETCPFWD @@ -35,7 +36,7 @@ void recv_msg_global_request_remotetcp() { TRACE(("enter recv_msg_global_request_remotetcp")); - if (ses.opts->noremotetcp) { + if (opts.noremotetcp) { TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled")); goto out; } -- cgit v1.2.3 From a76b1ba06868c1743837a5267efcbf2e07c9d81d Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 27 Jul 2004 16:30:46 +0000 Subject: Progressing client support --HG-- extra : convert_revision : 48946be1cef774d1c33b0f78689962b18720c627 --- Makefile.in | 13 ++--- auth.h | 30 +++++++++-- cli-auth.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ cli-authpasswd.c | 36 +++++++++++++ cli-kex.c | 1 + cli-main.c | 49 +++++++++++++++++- cli-service.c | 62 +++++++++++++++++++++++ cli-session.c | 92 +++++++++++++++++++++++++++------ common-kex.c | 42 +++++++++------- common-runopts.c | 28 +++++++++++ common-session.c | 6 +-- dbmulti.c | 8 +++ debug.h | 2 +- kex.h | 9 ++-- options.h | 23 ++------- packet.c | 6 +-- runopts.h | 6 ++- scp.c | 7 ++- service.h | 3 +- session.h | 38 ++++++++++---- svr-agentfwd.c | 8 +-- svr-auth.c | 72 +++++++++++++------------- svr-authpasswd.c | 12 ++--- svr-authpubkey.c | 22 ++++---- svr-chansession.c | 32 ++++++------ svr-runopts.c | 4 +- svr-service.c | 3 +- svr-session.c | 13 ++--- 28 files changed, 599 insertions(+), 176 deletions(-) create mode 100644 cli-auth.c create mode 100644 cli-authpasswd.c create mode 100644 cli-service.c create mode 100644 common-runopts.c (limited to 'svr-chansession.c') diff --git a/Makefile.in b/Makefile.in index 645da6d..b8de23e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -27,7 +27,7 @@ SVROBJS=svr-kex.o svr-algo.o svr-auth.o sshpty.o \ svr-chansession.o svr-runopts.o svr-agentfwd.o svr-main.o svr-x11fwd.o CLIOBJS=cli-algo.o cli-main.o cli-auth.o cli-authpasswd.o cli-kex.o \ - cli-session.o cli-service.o + cli-session.o cli-service.o cli-runopts.o CLISVROBJS=common-session.o packet.o common-algo.o common-kex.o \ common-channel.o common-chansession.o termcodes.o loginrec.o \ @@ -140,11 +140,12 @@ dbclient: $(dbclientobjs) dropbearkey: $(dropbearkeyobjs) dropbearconvert: $(dropbearconvertobjs) -dropbear dbclient dropbearkey dropbearconvert: $(HEADERS) $(LTC) $(LTM) +dropbear dbclient dropbearkey dropbearconvert: $(HEADERS) $(LTC) $(LTM) \ + Makefile $(LD) $(LDFLAGS) -o $(SPREFIX)$@$(EXEEXT) $($@objs) $(LIBS) # scp doesn't use the libs so is special. -scp: $(SCPOBJS) $(HEADERS) +scp: $(SCPOBJS) $(HEADERS) Makefile $(LD) $(LDFLAGS) -o $(SPREFIX)$@$(EXEEXT) $(SCPOBJS) @@ -155,16 +156,16 @@ ifeq ($(MULTI),1) CFLAGS+=$(addprefix -DDBMULTI_, $(PROGRAMS)) -DDROPBEAR_MULTI endif -dropbearmulti: $(HEADERS) $(MULTIOBJS) $(LTC) $(LTM) +dropbearmulti: $(HEADERS) $(MULTIOBJS) $(LTC) $(LTM) Makefile $(LD) $(LDFLAGS) -o $(SPREFIX)$@$(EXEEXT) $(MULTIOBJS) $(LIBS) @echo @echo "You should now create symlinks to the programs you have included" @echo "ie 'ln -s dropbearmulti dropbear'" -$(LTC): $(HEADERS) +$(LTC): options.h cd libtomcrypt && $(MAKE) clean && $(MAKE) -$(LTM): $(HEADERS) +$(LTM): options.h cd libtommath && $(MAKE) ltc-clean: diff --git a/auth.h b/auth.h index 8d2db3e..df8ae0c 100644 --- a/auth.h +++ b/auth.h @@ -27,12 +27,28 @@ #include "includes.h" -void authinitialise(); +void svr_authinitialise(); +void cli_authinitialise(); +void svr_auth_password(); +void svr_auth_pubkey(); + +int cli_auth_password(); +int cli_auth_pubkey(); + +/* Server functions */ void recv_msg_userauth_request(); void send_msg_userauth_failure(int partial, int incrfail); void send_msg_userauth_success(); +/* Client functions */ +void recv_msg_userauth_failure(); +void recv_msg_userauth_success(); +void cli_get_user(); +void cli_auth_getmethods(); +void cli_auth_try(); + + #define MAX_USERNAME_LEN 25 /* arbitrary for the moment */ #define AUTH_TYPE_PUBKEY 1 << 0 @@ -46,17 +62,23 @@ void send_msg_userauth_success(); #define AUTH_METHOD_PASSWORD "password" #define AUTH_METHOD_PASSWORD_LEN 8 +/* This structure is shared between server and client - it contains + * relatively little extraneous bits when used for the client rather than the + * server */ struct AuthState { char *username; /* This is the username the client presents to check. It is updated each run through, used for auth checking */ - char *printableuser; /* stripped of control chars, used for logs etc */ - struct passwd * pw; unsigned char authtypes; /* Flags indicating which auth types are still valid */ unsigned int failcount; /* Number of (failed) authentication attempts.*/ - unsigned authdone : 1; /* 0 if we haven't authed, 1 if we have */ + unsigned authdone : 1; /* 0 if we haven't authed, 1 if we have. Applies for + client and server (though has differing [obvious] + meanings). */ + /* These are only used for the server */ + char *printableuser; /* stripped of control chars, used for logs etc */ + struct passwd * pw; }; diff --git a/cli-auth.c b/cli-auth.c new file mode 100644 index 0000000..952546e --- /dev/null +++ b/cli-auth.c @@ -0,0 +1,148 @@ +#include "includes.h" +#include "session.h" +#include "auth.h" +#include "dbutil.h" +#include "buffer.h" +#include "ssh.h" +#include "packet.h" +#include "runopts.h" + +void cli_authinitialise() { + + memset(&ses.authstate, 0, sizeof(ses.authstate)); +} + + +void cli_get_user() { + + uid_t uid; + struct passwd *pw; + + TRACE(("enter cli_get_user")); + if (cli_opts.username != NULL) { + ses.authstate.username = cli_opts.username; + } else { + uid = getuid(); + + pw = getpwuid(uid); + if (pw == NULL || pw->pw_name == NULL) { + dropbear_exit("Couldn't find username for current user"); + } + + ses.authstate.username = m_strdup(pw->pw_name); + } + TRACE(("leave cli_get_user: %s", cli_ses.username)); +} + +/* Send a "none" auth request to get available methods */ +void cli_auth_getmethods() { + + TRACE(("enter cli_auth_getmethods")); + + CHECKCLEARTOWRITE(); + + buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST); + buf_putstring(ses.writepayload, ses.authstate.username, + strlen(ses.authstate.username)); + buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION, + SSH_SERVICE_CONNECTION_LEN); + buf_putstring(ses.writepayload, "none", 4); /* 'none' method */ + + encrypt_packet(); + cli_ses.state = USERAUTH_METHODS_SENT; + TRACE(("leave cli_auth_getmethods")); + +} + +void recv_msg_userauth_failure() { + + unsigned char * methods = NULL; + unsigned char * tok = NULL; + unsigned int methlen = 0; + unsigned int partial = 0; + unsigned int i = 0; + + TRACE(("<- MSG_USERAUTH_FAILURE")); + TRACE(("enter recv_msg_userauth_failure")); + + methods = buf_getstring(ses.payload, &methlen); + + partial = buf_getbyte(ses.payload); + + if (partial) { + dropbear_log(LOG_INFO, "Authentication partially succeeded, more attempts required"); + } else { + ses.authstate.failcount++; + } + + TRACE(("Methods (len %d): '%s'", methlen, methods)); + + ses.authstate.authdone=0; + ses.authstate.authtypes=0; + + /* Split with nulls rather than commas */ + for (i = 0; i < methlen; i++) { + if (methods[i] == ',') { + methods[i] = '\0'; + } + } + + tok = methods; /* tok stores the next method we'll compare */ + for (i = 0; i <= methlen; i++) { + if (methods[i] == '\0') { + TRACE(("auth method '%s'\n", tok)); +#ifdef DROPBEAR_PUBKEY_AUTH + if (strncmp(AUTH_METHOD_PUBKEY, tok, + AUTH_METHOD_PUBKEY_LEN) == 0) { + ses.authstate.authtypes |= AUTH_TYPE_PUBKEY; + } +#endif +#ifdef DROPBEAR_PASSWORD_AUTH + if (strncmp(AUTH_METHOD_PASSWORD, tok, + AUTH_METHOD_PASSWORD_LEN) == 0) { + ses.authstate.authtypes |= AUTH_TYPE_PASSWORD; + } +#endif + tok = &methods[i]; /* Must make sure we don't use it after + the last loop, since it'll point + to something undefined */ + } + } + + cli_ses.state = USERAUTH_FAIL_RCVD; + + TRACE(("leave recv_msg_userauth_failure")); +} + +void recv_msg_userauth_success() { + TRACE(("received msg_userauth_success")); + ses.authstate.authdone = 1; +} + +void cli_auth_try() { + + TRACE(("enter cli_auth_try")); + int finished = 0; + + CHECKCLEARTOWRITE(); + + /* XXX We hardcode that we try a pubkey first */ +#ifdef DROPBEAR_PUBKEY_AUTH + if (ses.authstate.authtypes & AUTH_TYPE_PUBKEY) { + finished = cli_auth_pubkey(); + } +#endif + +#ifdef DROPBEAR_PASSWORD_AUTH + if (!finished && ses.authstate.authtypes & AUTH_TYPE_PASSWORD) { + finished = cli_auth_password(); + } +#endif + + if (!finished) { + dropbear_exit("No auth methods could be used."); + } + + cli_ses.state = USERAUTH_REQ_SENT; + TRACE(("leave cli_auth_try")); +} diff --git a/cli-authpasswd.c b/cli-authpasswd.c new file mode 100644 index 0000000..6185334 --- /dev/null +++ b/cli-authpasswd.c @@ -0,0 +1,36 @@ +#include "includes.h" +#include "buffer.h" +#include "dbutil.h" +#include "session.h" +#include "ssh.h" + +int cli_auth_password() { + + char* password = NULL; + TRACE(("enter cli_auth_password")); + + CHECKCLEARTOWRITE(); + password = getpass("Password: "); + + buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST); + + buf_putstring(ses.writepayload, ses.authstate.username, + strlen(ses.authstate.username)); + + buf_putstring(ses.writepayload, SSH_SERVICE_CONNECTION, + SSH_SERVICE_CONNECTION_LEN); + + buf_putstring(ses.writepayload, AUTH_METHOD_PASSWORD, + AUTH_METHOD_PASSWORD_LEN); + + buf_putbyte(ses.writepayload, 0); /* FALSE - so says the spec */ + + buf_putstring(ses.writepayload, password, strlen(password)); + + encrypt_packet(); + m_burn(password, strlen(password)); + + TRACE(("leave cli_auth_password")); + return 1; /* Password auth can always be tried */ + +} diff --git a/cli-kex.c b/cli-kex.c index 4d26332..2577caf 100644 --- a/cli-kex.c +++ b/cli-kex.c @@ -34,6 +34,7 @@ #include "bignum.h" #include "random.h" #include "runopts.h" +#include "signkey.h" diff --git a/cli-main.c b/cli-main.c index 2460060..c5d5b3e 100644 --- a/cli-main.c +++ b/cli-main.c @@ -1,6 +1,17 @@ -#include +#include "includes.h" +#include "dbutil.h" +#include "runopts.h" +#include "session.h" +static void cli_dropbear_exit(int exitcode, const char* format, va_list param); +static void cli_dropbear_log(int priority, const char* format, va_list param); + +#if defined(DBMULTI_dbclient) || !defined(DROPBEAR_MULTI) +#if defined(DBMULTI_dbclient) && defined(DROPBEAR_MULTI) +int cli_main(int argc, char ** argv) { +#else int main(int argc, char ** argv) { +#endif int sock; char* error = NULL; @@ -12,6 +23,9 @@ int main(int argc, char ** argv) { cli_getopts(argc, argv); + TRACE(("user='%s' host='%s' port='%s'", cli_opts.username, + cli_opts.remotehost, cli_opts.remoteport)); + sock = connect_remote(cli_opts.remotehost, cli_opts.remoteport, 0, &error); @@ -23,7 +37,7 @@ int main(int argc, char ** argv) { len = strlen(cli_opts.remotehost); len += 10; /* 16 bit port and leeway*/ hostandport = (char*)m_malloc(len); - snprintf(hostandport, len, "%s%d", + snprintf(hostandport, len, "%s:%s", cli_opts.remotehost, cli_opts.remoteport); cli_session(sock, hostandport); @@ -31,3 +45,34 @@ int main(int argc, char ** argv) { /* not reached */ return -1; } +#endif /* DBMULTI stuff */ + +static void cli_dropbear_exit(int exitcode, const char* format, va_list param) { + + char fmtbuf[300]; + + if (!sessinitdone) { + snprintf(fmtbuf, sizeof(fmtbuf), "exited: %s", + format); + } else { + snprintf(fmtbuf, sizeof(fmtbuf), + "connection to %s@%s:%s exited: %s", + cli_opts.username, cli_opts.remotehost, + cli_opts.remoteport, format); + } + + _dropbear_log(LOG_INFO, fmtbuf, param); + + common_session_cleanup(); + exit(exitcode); +} + +static void cli_dropbear_log(int priority, const char* format, va_list param) { + + char printbuf[1024]; + + vsnprintf(printbuf, sizeof(printbuf), format, param); + + fprintf(stderr, "Dropbear: %s\n", printbuf); + +} diff --git a/cli-service.c b/cli-service.c new file mode 100644 index 0000000..c873919 --- /dev/null +++ b/cli-service.c @@ -0,0 +1,62 @@ +#include "includes.h" +#include "service.h" +#include "dbutil.h" +#include "packet.h" +#include "buffer.h" +#include "session.h" +#include "ssh.h" + +void send_msg_service_request(char* servicename) { + + TRACE(("enter send_msg_service_request: servicename='%s'", servicename)); + + CHECKCLEARTOWRITE(); + + buf_putbyte(ses.payload, SSH_MSG_SERVICE_REQUEST); + buf_putstring(ses.payload, servicename, strlen(servicename)); + + encrypt_packet(); + TRACE(("leave send_msg_service_request")); +} + +/* This just sets up the state variables right for the main client session loop + * to deal with */ +void recv_msg_service_accept() { + + unsigned char* servicename; + unsigned int len; + + TRACE(("enter recv_msg_service_accept")); + + servicename = buf_getstring(ses.payload, &len); + + /* ssh-userauth */ + if (cli_ses.state = SERVICE_AUTH_REQ_SENT + && len == SSH_SERVICE_USERAUTH_LEN + && strncmp(SSH_SERVICE_USERAUTH, servicename, len) == 0) { + + cli_ses.state = SERVICE_AUTH_ACCEPT_RCVD; + m_free(servicename); + TRACE(("leave recv_msg_service_accept: done ssh-userauth")); + return; + } + + /* ssh-connection */ + if (cli_ses.state = SERVICE_CONN_REQ_SENT + && len == SSH_SERVICE_CONNECTION_LEN + && strncmp(SSH_SERVICE_CONNECTION, servicename, len) == 0) { + + if (ses.authstate.authdone != 1) { + dropbear_exit("request for connection before auth"); + } + + cli_ses.state = SERVICE_CONN_ACCEPT_RCVD; + m_free(servicename); + TRACE(("leave recv_msg_service_accept: done ssh-connection")); + return; + } + + dropbear_exit("unrecognised service accept"); + /* m_free(servicename); not reached */ + +} diff --git a/cli-session.c b/cli-session.c index d5afaf9..9f80bd1 100644 --- a/cli-session.c +++ b/cli-session.c @@ -8,9 +8,11 @@ #include "tcpfwd-remote.h" #include "channel.h" #include "random.h" +#include "service.h" static void cli_remoteclosed(); static void cli_sessionloop(); +static void cli_session_init(); struct clientsession cli_ses; /* GLOBAL */ @@ -28,6 +30,8 @@ static const packettype cli_packettypes[] = { {SSH_MSG_CHANNEL_CLOSE, recv_msg_channel_close}, {SSH_MSG_CHANNEL_OPEN_CONFIRMATION, recv_msg_channel_open_confirmation}, {SSH_MSG_CHANNEL_OPEN_FAILURE, recv_msg_channel_open_failure}, + {SSH_MSG_USERAUTH_FAILURE, recv_msg_userauth_failure}, + {SSH_MSG_USERAUTH_SUCCESS, recv_msg_userauth_success}, {0, 0} /* End */ }; @@ -37,6 +41,7 @@ static const struct ChanType *cli_chantypes[] = { * that forwarding */ NULL /* Null termination */ }; + void cli_session(int sock, char* remotehost) { crypto_init(); @@ -44,11 +49,9 @@ void cli_session(int sock, char* remotehost) { chaninitialise(cli_chantypes); - /* For printing "remote host closed" for the user */ - session_remoteclosed = cli_remoteclosed; - /* packet handlers */ - ses.packettypes = cli_packettypes; + /* Set up cli_ses vars */ + cli_session_init(); /* Ready to go */ sessinitdone = 1; @@ -66,26 +69,85 @@ void cli_session(int sock, char* remotehost) { /* Not reached */ +} + +static void cli_session_init() { + cli_ses.state = STATE_NOTHING; + cli_ses.kex_state = KEX_NOTHING; + + /* For printing "remote host closed" for the user */ + ses.remoteclosed = cli_remoteclosed; + ses.buf_match_algo = cli_buf_match_algo; + + /* packet handlers */ + ses.packettypes = cli_packettypes; } +/* This function drives the progress of the session - it initiates KEX, + * service, userauth and channel requests */ static void cli_sessionloop() { - switch (cli_ses.state) { + TRACE(("enter cli_sessionloop")); + + if (cli_ses.kex_state == KEX_NOTHING && ses.kexstate.recvkexinit) { + cli_ses.state = KEXINIT_RCVD; + } - KEXINIT_RCVD: - /* We initiate the KEX. If DH wasn't the correct type, the KEXINIT - * negotiation would have failed. */ - send_msg_kexdh_init(); - cli_ses.state = KEXDH_INIT_SENT; - break; + if (cli_ses.state == KEXINIT_RCVD) { - default: - break; + /* We initiate the KEXDH. If DH wasn't the correct type, the KEXINIT + * negotiation would have failed. */ + send_msg_kexdh_init(); + cli_ses.kex_state = KEXDH_INIT_SENT; + TRACE(("leave cli_sessionloop: done with KEXINIT_RCVD")); + return; } - if (cli_ses.donefirstkex && !cli_ses.authdone) { + /* A KEX has finished, so we should go back to our KEX_NOTHING state */ + if (cli_ses.kex_state != KEX_NOTHING && ses.kexstate.recvkexinit == 0 + && ses.kexstate.sentkexinit == 0) { + cli_ses.kex_state = KEX_NOTHING; + } + + /* We shouldn't do anything else if a KEX is in progress */ + if (cli_ses.kex_state != KEX_NOTHING) { + TRACE(("leave cli_sessionloop: kex_state != KEX_NOTHING")); + return; + } + /* We should exit if we haven't donefirstkex: we shouldn't reach here + * in normal operation */ + if (ses.kexstate.donefirstkex == 0) { + TRACE(("XXX XXX might be bad! leave cli_sessionloop: haven't donefirstkex")); + } + + switch (cli_ses.state) { + + case STATE_NOTHING: + /* We've got the transport layer sorted, we now need to request + * userauth */ + send_msg_service_request(SSH_SERVICE_USERAUTH); + cli_ses.state = SERVICE_AUTH_REQ_SENT; + return; + + /* userauth code */ + case SERVICE_AUTH_ACCEPT_RCVD: + cli_get_user(); + cli_auth_getmethods(); + cli_ses.state = USERAUTH_METHODS_SENT; + return; + + case USERAUTH_FAIL_RCVD: + cli_auth_try(); + return; + + /* XXX more here needed */ + + + default: + break; + } } @@ -97,5 +159,5 @@ static void cli_remoteclosed() { * already sent/received disconnect message(s) ??? */ close(ses.sock); ses.sock = -1; - dropbear_exit("%s closed the connection", ses.remotehost); + dropbear_exit("remote closed the connection"); } diff --git a/common-kex.c b/common-kex.c index 21accb6..9847a48 100644 --- a/common-kex.c +++ b/common-kex.c @@ -5,7 +5,7 @@ * This code is copied from the larger file "kex.c" * some functions are verbatim, others are generalized --mihnea * - * Copyright (c) 2002,2003 Matt Johnston + * Copyright (c) 2002-2004 Matt Johnston * Portions Copyright (c) 2004 by Mihnea Stoenescu * All rights reserved. * @@ -54,10 +54,12 @@ const unsigned char dh_p_val[] = { const int DH_G_VAL = 2; +static void kexinitialise(); static void gen_new_keys(); #ifndef DISABLE_ZLIB static void gen_new_zstreams(); #endif +static void read_kex_algos(); /* helper function for gen_new_keys */ static void hashkeys(unsigned char *out, int outlen, const hash_state * hs, unsigned const char X); @@ -145,6 +147,7 @@ void send_msg_newkeys() { kexinitialise(); /* we've finished with this kex */ TRACE((" -> DATAALLOWED=1")); ses.dataallowed = 1; /* we can send other packets again now */ + ses.kexstate.donefirstkex = 1; } else { ses.kexstate.sentnewkeys = 1; TRACE(("SENTNEWKEYS=1")); @@ -168,6 +171,7 @@ void recv_msg_newkeys() { kexinitialise(); /* we've finished with this kex */ TRACE((" -> DATAALLOWED=1")); ses.dataallowed = 1; /* we can send other packets again now */ + ses.kexstate.donefirstkex = 1; } else { TRACE(("RECVNEWKEYS=1")); ses.kexstate.recvnewkeys = 1; @@ -177,8 +181,15 @@ void recv_msg_newkeys() { } -/* Duplicated verbatim from kex.c --mihnea */ -void kexinitialise() { +/* Set up the kex for the first time */ +void kexfirstinitialise() { + + ses.kexstate.donefirstkex = 0; + kexinitialise(); +} + +/* Reset the kex state, ready for a new negotiation */ +static void kexinitialise() { struct timeval tv; @@ -404,7 +415,7 @@ void recv_msg_kexinit() { #ifdef DROPBEAR_CLIENT /* read the peer's choice of algos */ - read_kex_algos(cli_buf_match_algo); + read_kex_algos(); /* V_C, the client's version string (CR and NL excluded) */ buf_putstring(ses.kexhashbuf, @@ -423,14 +434,13 @@ void recv_msg_kexinit() { buf_getptr(ses.payload, ses.payload->len), ses.payload->len); - cli_ses.state = KEXINIT_RCVD; #endif } else { /* SERVER */ #ifdef DROPBEAR_SERVER /* read the peer's choice of algos */ - read_kex_algos(svr_buf_match_algo); + read_kex_algos(); /* V_C, the client's version string (CR and NL excluded) */ buf_putstring(ses.kexhashbuf, ses.remoteident, strlen((char*)ses.remoteident)); @@ -583,9 +593,7 @@ void kexdh_comb_key(mp_int *dh_pub_us, mp_int *dh_priv, mp_int *dh_pub_them, /* read the other side's algo list. buf_match_algo is a callback to match * algos for the client or server. */ -void read_kex_algos( - algo_type*(buf_match_algo)(buffer*buf, algo_type localalgos[], - int *goodguess)) { +static void read_kex_algos() { algo_type * algo; char * erralgo = NULL; @@ -599,7 +607,7 @@ void read_kex_algos( ses.newkeys = (struct key_context*)m_malloc(sizeof(struct key_context)); /* kex_algorithms */ - algo = buf_match_algo(ses.payload, sshkex, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshkex, &goodguess); allgood &= goodguess; if (algo == NULL) { erralgo = "kex"; @@ -608,7 +616,7 @@ void read_kex_algos( ses.newkeys->algo_kex = algo->val; /* server_host_key_algorithms */ - algo = buf_match_algo(ses.payload, sshhostkey, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshhostkey, &goodguess); allgood &= goodguess; if (algo == NULL) { erralgo = "hostkey"; @@ -617,7 +625,7 @@ void read_kex_algos( ses.newkeys->algo_hostkey = algo->val; /* encryption_algorithms_client_to_server */ - algo = buf_match_algo(ses.payload, sshciphers, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshciphers, &goodguess); if (algo == NULL) { erralgo = "enc c->s"; goto error; @@ -625,7 +633,7 @@ void read_kex_algos( ses.newkeys->recv_algo_crypt = (struct dropbear_cipher*)algo->data; /* encryption_algorithms_server_to_client */ - algo = buf_match_algo(ses.payload, sshciphers, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshciphers, &goodguess); if (algo == NULL) { erralgo = "enc s->c"; goto error; @@ -633,7 +641,7 @@ void read_kex_algos( ses.newkeys->trans_algo_crypt = (struct dropbear_cipher*)algo->data; /* mac_algorithms_client_to_server */ - algo = buf_match_algo(ses.payload, sshhashes, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshhashes, &goodguess); if (algo == NULL) { erralgo = "mac c->s"; goto error; @@ -641,7 +649,7 @@ void read_kex_algos( ses.newkeys->recv_algo_mac = (struct dropbear_hash*)algo->data; /* mac_algorithms_server_to_client */ - algo = buf_match_algo(ses.payload, sshhashes, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshhashes, &goodguess); if (algo == NULL) { erralgo = "mac s->c"; goto error; @@ -649,7 +657,7 @@ void read_kex_algos( ses.newkeys->trans_algo_mac = (struct dropbear_hash*)algo->data; /* compression_algorithms_client_to_server */ - algo = buf_match_algo(ses.payload, sshcompress, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshcompress, &goodguess); if (algo == NULL) { erralgo = "comp c->s"; goto error; @@ -657,7 +665,7 @@ void read_kex_algos( ses.newkeys->recv_algo_comp = algo->val; /* compression_algorithms_server_to_client */ - algo = buf_match_algo(ses.payload, sshcompress, &goodguess); + algo = ses.buf_match_algo(ses.payload, sshcompress, &goodguess); if (algo == NULL) { erralgo = "comp s->c"; goto error; diff --git a/common-runopts.c b/common-runopts.c new file mode 100644 index 0000000..097ab12 --- /dev/null +++ b/common-runopts.c @@ -0,0 +1,28 @@ +/* + * Dropbear - a SSH2 server + * + * Copyright (c) 2002,2003 Matt Johnston + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ + +#include "includes.h" +#include "runopts.h" + +runopts opts; /* GLOBAL */ diff --git a/common-session.c b/common-session.c index 70ddbfe..93e7c74 100644 --- a/common-session.c +++ b/common-session.c @@ -45,8 +45,6 @@ int sessinitdone = 0; /* GLOBAL */ /* this is set when we get SIGINT or SIGTERM, the handler is in main.c */ int exitflag = 0; /* GLOBAL */ -void(*session_remoteclosed)() = NULL; - static void checktimeouts(); static int ident_readln(int fd, char* buf, int count); @@ -63,7 +61,7 @@ void common_session_init(int sock, char* remotehost) { ses.connecttimeout = 0; - kexinitialise(); /* initialise the kex state */ + kexfirstinitialise(); /* initialise the kex state */ chaninitialise(); /* initialise the channel state */ ses.writepayload = buf_new(MAX_TRANS_PAYLOAD_LEN); @@ -104,8 +102,6 @@ void common_session_init(int sock, char* remotehost) { ses.dh_K = NULL; ses.remoteident = NULL; - ses.authdone = 0; - ses.chantypes = NULL; ses.allowprivport = 0; diff --git a/dbmulti.c b/dbmulti.c index 8f39227..24adce1 100644 --- a/dbmulti.c +++ b/dbmulti.c @@ -19,6 +19,11 @@ int main(int argc, char ** argv) { return dropbear_main(argc, argv); } #endif +#ifdef DBMULTI_dbclient + if (strcmp(progname, "dbclient") == 0) { + return cli_main(argc, argv); + } +#endif #ifdef DBMULTI_dropbearkey if (strcmp(progname, "dropbearkey") == 0) { return dropbearkey_main(argc, argv); @@ -41,6 +46,9 @@ int main(int argc, char ** argv) { #ifdef DBMULTI_dropbear "'dropbear' - the Dropbear server\n" #endif +#ifdef DBMULTI_dbclient + "'dbclient' - the Dropbear client\n" +#endif #ifdef DBMULTI_dropbearkey "'dropbearkey' - the key generator\n" #endif diff --git a/debug.h b/debug.h index 736690a..d619a58 100644 --- a/debug.h +++ b/debug.h @@ -36,7 +36,7 @@ /* Define this to print trace statements - very verbose */ /* Caution: Don't use this in an unfriendly environment (ie unfirewalled), * since the printing does not sanitise strings etc */ -//#define DEBUG_TRACE +#define DEBUG_TRACE /* All functions writing to the cleartext payload buffer call * CHECKCLEARTOWRITE() before writing. This is only really useful if you're diff --git a/kex.h b/kex.h index e6c8ac1..01626ed 100644 --- a/kex.h +++ b/kex.h @@ -32,15 +32,11 @@ void send_msg_kexinit(); void recv_msg_kexinit(); void send_msg_newkeys(); void recv_msg_newkeys(); -void kexinitialise(); +void kexfirstinitialise(); void gen_kexdh_vals(mp_int *dh_pub, mp_int *dh_priv); void kexdh_comb_key(mp_int *dh_pub_us, mp_int *dh_priv, mp_int *dh_pub_them, sign_key *hostkey); -void read_kex_algos( - algo_type*(buf_match_algo)(buffer*buf, algo_type localalgos[], - int *goodguess)); - void recv_msg_kexdh_init(); // server void send_msg_kexdh_init(); // client @@ -59,6 +55,9 @@ struct KEXState { unsigned sentnewkeys : 1; /* set once we've send/recv'ed MSG_NEWKEYS*/ unsigned recvnewkeys : 1; + unsigned donefirstkex : 1; /* Set to 1 after the first kex has completed, + ie the transport layer has been set up */ + long lastkextime; /* time of the last kex */ unsigned int datatrans; /* data transmitted since last kex */ unsigned int datarecv; /* data received since last kex */ diff --git a/options.h b/options.h index 3aeac0f..ee0ee60 100644 --- a/options.h +++ b/options.h @@ -29,8 +29,6 @@ * Define compile-time options below - the "#ifndef DROPBEAR_XXX .... #endif" * parts are to allow for commandline -DDROPBEAR_XXX options etc. ******************************************************************/ -#define DROPBEAR_SERVER -//#define DROPBEAR_CLIENT #ifndef DROPBEAR_PORT #define DROPBEAR_PORT 22 @@ -48,7 +46,6 @@ * perhaps 20% slower for pubkey operations (it is probably worth experimenting * if you want to use this) */ /*#define NO_FAST_EXPTMOD*/ -#define DROPBEAR_SMALL_CODE /* Enable X11 Forwarding */ #define ENABLE_X11FWD @@ -114,7 +111,7 @@ /* Authentication types to enable, at least one required. RFC Draft requires pubkey auth, and recommends password */ #define DROPBEAR_PASSWORD_AUTH -#define DROPBEAR_PUBKEY_AUTH +//#define DROPBEAR_PUBKEY_AUTH /* Random device to use - you must specify _one only_. * DEV_RANDOM is recommended on hosts with a good /dev/urandom, otherwise use @@ -162,20 +159,8 @@ /* This is used by the scp binary when used as a client binary */ #define _PATH_SSH_PROGRAM "/usr/bin/ssh" -/* Multi-purpose binary configuration - if you want to make the combined - * binary, first define DROPBEAR_MULTI, and then define which of the three - * components you want. You should then compile Dropbear with - * "make clean; make dropbearmulti". You'll need to install the binary - * manually, see MULTI for details */ - -/* #define DROPBEAR_MULTI */ - -/* The three multi binaries: dropbear, dropbearkey, dropbearconvert - * Comment out these if you don't want some of them */ -#define DBMULTI_DROPBEAR -#define DBMULTI_KEY -#define DBMULTI_CONVERT - +/* Multi-purpose binary configuration has now moved. Look at the top + * of the Makefile for instructions, or INSTALL */ /******************************************************************* * You shouldn't edit below here unless you know you need to. @@ -246,7 +231,7 @@ #define DROPBEAR_COMP_ZLIB 1 /* Required for pubkey auth */ -#ifdef DROPBEAR_PUBKEY_AUTH +#if defined(DROPBEAR_PUBKEY_AUTH) || defined(DROPBEAR_CLIENT) #define DROPBEAR_SIGNKEY_VERIFY #endif diff --git a/packet.c b/packet.c index 886fe67..997bc6f 100644 --- a/packet.c +++ b/packet.c @@ -73,7 +73,7 @@ void write_packet() { } if (written == 0) { - session_remoteclosed(); + ses.remoteclosed(); } if (written == len) { @@ -122,7 +122,7 @@ void read_packet() { len = read(ses.sock, buf_getptr(ses.readbuf, maxlen), maxlen); if (len == 0) { - session_remoteclosed(); + ses.remoteclosed(); } if (len < 0) { @@ -171,7 +171,7 @@ static void read_packet_init() { len = read(ses.sock, buf_getwriteptr(ses.readbuf, maxlen), maxlen); if (len == 0) { - session_remoteclosed(); + ses.remoteclosed(); } if (len < 0) { if (errno == EINTR) { diff --git a/runopts.h b/runopts.h index 1bf1539..125d737 100644 --- a/runopts.h +++ b/runopts.h @@ -79,7 +79,11 @@ void svr_getopts(int argc, char ** argv); /* Uncompleted XXX matt */ typedef struct cli_runopts { - int todo; + char *remotehost; + char *remoteport; + + char *username; + /* XXX TODO */ } cli_runopts; diff --git a/scp.c b/scp.c index b23dec6..b6eec88 100644 --- a/scp.c +++ b/scp.c @@ -229,8 +229,12 @@ void tolocal(int, char *[]); void toremote(char *, int, char *[]); void usage(void); -int +#if defined(DBMULTI_scp) || !defined(DROPBEAR_MULTI) +#if defined(DBMULTI_scp) && defined(DROPBEAR_MULTI) +int scp_main(int argc, char **argv) +#else main(int argc, char **argv) +#endif { int ch, fflag, tflag, status; double speed; @@ -379,6 +383,7 @@ main(int argc, char **argv) } exit(errs != 0); } +#endif /* DBMULTI stuff */ void toremote(char *targ, int argc, char **argv) diff --git a/service.h b/service.h index 0dfcd6e..5f50347 100644 --- a/service.h +++ b/service.h @@ -25,6 +25,7 @@ #ifndef _SERVICE_H_ #define _SERVICE_H_ -void recv_msg_service_request(); +void recv_msg_service_request(); /* Server */ +void send_msg_service_request(); /* Client */ #endif /* _SERVICE_H_ */ diff --git a/session.h b/session.h index d929c1c..4a7e0ac 100644 --- a/session.h +++ b/session.h @@ -45,7 +45,6 @@ void common_session_cleanup(); void checktimeouts(); void session_identification(); -extern void(*session_remoteclosed)(); /* Server */ void svr_session(int sock, int childpipe, char *remotehost); @@ -135,13 +134,18 @@ struct sshsession { buffer* transkexinit; /* the kexinit packet we send should be kept so we can add it to the hash when generating keys */ + algo_type*(*buf_match_algo)(buffer*buf, algo_type localalgos[], + int *goodguess); /* The function to use to choose which algorithm + to use from the ones presented by the remote + side. Is specific to the client/server mode, + hence the function-pointer callback.*/ - unsigned char authdone; /* Indicates when authentication has been - completed. This applies to both client and - server - in the server it gets set to 1 when - authentication is successful, in the client it - is set when the server has told us that auth - succeeded */ + void(*remoteclosed)(); /* A callback to handle closure of the + remote connection */ + + + struct AuthState authstate; /* Common amongst client and server, since most + struct elements are common */ /* Channel related */ struct Channel ** channels; /* these pointers may be null */ @@ -165,7 +169,6 @@ struct serversession { /* Server specific options */ int childpipe; /* kept open until we successfully authenticate */ /* userauth */ - struct AuthState authstate; struct ChildPid * childpids; /* array of mappings childpid<->channel */ unsigned int childpidsize; @@ -173,17 +176,30 @@ struct serversession { }; typedef enum { - NOTHING, + KEX_NOTHING, KEXINIT_RCVD, KEXDH_INIT_SENT, - KEXDH_REPLY_RCVD, + KEXDONE, + +} cli_kex_state; + +typedef enum { + STATE_NOTHING, + SERVICE_AUTH_REQ_SENT, + SERVICE_AUTH_ACCEPT_RCVD, + SERVICE_CONN_REQ_SENT, + SERVICE_CONN_ACCEPT_RCVD, + USERAUTH_METHODS_SENT, + USERAUTH_REQ_SENT, + USERAUTH_FAIL_RCVD, } cli_state; struct clientsession { mp_int *dh_e, *dh_x; /* Used during KEX */ - cli_state state; /* Used to progress the KEX/auth/channelsession etc */ + cli_kex_state kex_state; /* Used for progressing KEX */ + cli_state state; /* Used to progress auth/channelsession etc */ int something; /* XXX */ unsigned donefirstkex : 1; /* Set when we set sentnewkeys, never reset */ diff --git a/svr-agentfwd.c b/svr-agentfwd.c index fd068fe..0dad2a4 100644 --- a/svr-agentfwd.c +++ b/svr-agentfwd.c @@ -152,8 +152,8 @@ void agentcleanup(struct ChanSess * chansess) { * for themselves */ uid = getuid(); gid = getgid(); - if ((setegid(svr_ses.authstate.pw->pw_gid)) < 0 || - (seteuid(svr_ses.authstate.pw->pw_uid)) < 0) { + if ((setegid(ses.authstate.pw->pw_gid)) < 0 || + (seteuid(ses.authstate.pw->pw_uid)) < 0) { dropbear_exit("failed to set euid"); } @@ -215,8 +215,8 @@ static int bindagent(int fd, struct ChanSess * chansess) { /* drop to user privs to make the dir/file */ uid = getuid(); gid = getgid(); - if ((setegid(svr_ses.authstate.pw->pw_gid)) < 0 || - (seteuid(svr_ses.authstate.pw->pw_uid)) < 0) { + if ((setegid(ses.authstate.pw->pw_gid)) < 0 || + (seteuid(ses.authstate.pw->pw_uid)) < 0) { dropbear_exit("failed to set euid"); } diff --git a/svr-auth.c b/svr-auth.c index 7b588c0..0f0ef67 100644 --- a/svr-auth.c +++ b/svr-auth.c @@ -41,9 +41,9 @@ static int checkusername(unsigned char *username, unsigned int userlen); static void send_msg_userauth_banner(); /* initialise the first time for a session, resetting all parameters */ -void authinitialise() { +void svr_authinitialise() { - svr_ses.authstate.failcount = 0; + ses.authstate.failcount = 0; authclear(); } @@ -53,17 +53,13 @@ void authinitialise() { * on initialisation */ static void authclear() { - ses.authdone = 0; - svr_ses.authstate.pw = NULL; - svr_ses.authstate.username = NULL; - svr_ses.authstate.printableuser = NULL; - svr_ses.authstate.authtypes = 0; + memset(&ses.authstate, 0, sizeof(ses.authstate)); #ifdef DROPBEAR_PUBKEY_AUTH - svr_ses.authstate.authtypes |= AUTH_TYPE_PUBKEY; + ses.authstate.authtypes |= AUTH_TYPE_PUBKEY; #endif #ifdef DROPBEAR_PASSWORD_AUTH if (svr_opts.noauthpass) { - svr_ses.authstate.authtypes |= AUTH_TYPE_PASSWORD; + ses.authstate.authtypes |= AUTH_TYPE_PASSWORD; } #endif @@ -103,7 +99,7 @@ void recv_msg_userauth_request() { TRACE(("enter recv_msg_userauth_request")); /* ignore packets if auth is already done */ - if (ses.authdone == 1) { + if (ses.authstate.authdone == 1) { return; } @@ -147,12 +143,12 @@ void recv_msg_userauth_request() { #ifdef DROPBEAR_PASSWORD_AUTH if (!svr_opts.noauthpass && - !(svr_opts.norootpass && svr_ses.authstate.pw->pw_uid == 0) ) { + !(svr_opts.norootpass && ses.authstate.pw->pw_uid == 0) ) { /* user wants to try password auth */ if (methodlen == AUTH_METHOD_PASSWORD_LEN && strncmp(methodname, AUTH_METHOD_PASSWORD, AUTH_METHOD_PASSWORD_LEN) == 0) { - passwordauth(); + svr_auth_password(); goto out; } } @@ -163,7 +159,7 @@ void recv_msg_userauth_request() { if (methodlen == AUTH_METHOD_PUBKEY_LEN && strncmp(methodname, AUTH_METHOD_PUBKEY, AUTH_METHOD_PUBKEY_LEN) == 0) { - pubkeyauth(); + svr_auth_pubkey(); goto out; } #endif @@ -192,21 +188,21 @@ static int checkusername(unsigned char *username, unsigned int userlen) { } /* new user or username has changed */ - if (svr_ses.authstate.username == NULL || - strcmp(username, svr_ses.authstate.username) != 0) { + if (ses.authstate.username == NULL || + strcmp(username, ses.authstate.username) != 0) { /* the username needs resetting */ - if (svr_ses.authstate.username != NULL) { + if (ses.authstate.username != NULL) { dropbear_log(LOG_WARNING, "client trying multiple usernames"); - m_free(svr_ses.authstate.username); + m_free(ses.authstate.username); } authclear(); - svr_ses.authstate.pw = getpwnam((char*)username); - svr_ses.authstate.username = m_strdup(username); - m_free(svr_ses.authstate.printableuser); + ses.authstate.pw = getpwnam((char*)username); + ses.authstate.username = m_strdup(username); + m_free(ses.authstate.printableuser); } /* check that user exists */ - if (svr_ses.authstate.pw == NULL) { + if (ses.authstate.pw == NULL) { TRACE(("leave checkusername: user '%s' doesn't exist", username)); dropbear_log(LOG_WARNING, "login attempt for nonexistent user"); @@ -215,10 +211,10 @@ static int checkusername(unsigned char *username, unsigned int userlen) { } /* We can set it once we know its a real user */ - svr_ses.authstate.printableuser = m_strdup(svr_ses.authstate.pw->pw_name); + ses.authstate.printableuser = m_strdup(ses.authstate.pw->pw_name); /* check for non-root if desired */ - if (svr_opts.norootlogin && svr_ses.authstate.pw->pw_uid == 0) { + if (svr_opts.norootlogin && ses.authstate.pw->pw_uid == 0) { TRACE(("leave checkusername: root login disabled")); dropbear_log(LOG_WARNING, "root login rejected"); send_msg_userauth_failure(0, 1); @@ -226,18 +222,18 @@ static int checkusername(unsigned char *username, unsigned int userlen) { } /* check for an empty password */ - if (svr_ses.authstate.pw->pw_passwd[0] == '\0') { + if (ses.authstate.pw->pw_passwd[0] == '\0') { TRACE(("leave checkusername: empty pword")); dropbear_log(LOG_WARNING, "user '%s' has blank password, rejected", - svr_ses.authstate.printableuser); + ses.authstate.printableuser); send_msg_userauth_failure(0, 1); return DROPBEAR_FAILURE; } - TRACE(("shell is %s", svr_ses.authstate.pw->pw_shell)); + TRACE(("shell is %s", ses.authstate.pw->pw_shell)); /* check that the shell is set */ - usershell = svr_ses.authstate.pw->pw_shell; + usershell = ses.authstate.pw->pw_shell; if (usershell[0] == '\0') { /* empty shell in /etc/passwd means /bin/sh according to passwd(5) */ usershell = "/bin/sh"; @@ -258,7 +254,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { endusershell(); TRACE(("no matching shell")); dropbear_log(LOG_WARNING, "user '%s' has invalid shell, rejected", - svr_ses.authstate.printableuser); + ses.authstate.printableuser); send_msg_userauth_failure(0, 1); return DROPBEAR_FAILURE; @@ -266,7 +262,7 @@ goodshell: endusershell(); TRACE(("matching shell")); - TRACE(("uid = %d", svr_ses.authstate.pw->pw_uid)); + TRACE(("uid = %d", ses.authstate.pw->pw_uid)); TRACE(("leave checkusername")); return DROPBEAR_SUCCESS; @@ -290,14 +286,14 @@ void send_msg_userauth_failure(int partial, int incrfail) { /* put a list of allowed types */ typebuf = buf_new(30); /* long enough for PUBKEY and PASSWORD */ - if (svr_ses.authstate.authtypes & AUTH_TYPE_PUBKEY) { + if (ses.authstate.authtypes & AUTH_TYPE_PUBKEY) { buf_putbytes(typebuf, AUTH_METHOD_PUBKEY, AUTH_METHOD_PUBKEY_LEN); - if (svr_ses.authstate.authtypes & AUTH_TYPE_PASSWORD) { + if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) { buf_putbyte(typebuf, ','); } } - if (svr_ses.authstate.authtypes & AUTH_TYPE_PASSWORD) { + if (ses.authstate.authtypes & AUTH_TYPE_PASSWORD) { buf_putbytes(typebuf, AUTH_METHOD_PASSWORD, AUTH_METHOD_PASSWORD_LEN); } @@ -311,18 +307,18 @@ void send_msg_userauth_failure(int partial, int incrfail) { if (incrfail) { usleep(300000); /* XXX improve this */ - svr_ses.authstate.failcount++; + ses.authstate.failcount++; } - if (svr_ses.authstate.failcount >= MAX_AUTH_TRIES) { + if (ses.authstate.failcount >= MAX_AUTH_TRIES) { char * userstr; /* XXX - send disconnect ? */ TRACE(("Max auth tries reached, exiting")); - if (svr_ses.authstate.printableuser == NULL) { + if (ses.authstate.printableuser == NULL) { userstr = "is invalid"; } else { - userstr = svr_ses.authstate.printableuser; + userstr = ses.authstate.printableuser; } dropbear_exit("Max auth tries reached - user %s", userstr); } @@ -340,9 +336,9 @@ void send_msg_userauth_success() { buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_SUCCESS); encrypt_packet(); - ses.authdone = 1; + ses.authstate.authdone = 1; - if (svr_ses.authstate.pw->pw_uid == 0) { + if (ses.authstate.pw->pw_uid == 0) { ses.allowprivport = 1; } diff --git a/svr-authpasswd.c b/svr-authpasswd.c index 859cfd5..f161b69 100644 --- a/svr-authpasswd.c +++ b/svr-authpasswd.c @@ -35,7 +35,7 @@ /* Process a password auth request, sending success or failure messages as * appropriate */ -void passwordauth() { +void svr_auth_password() { #ifdef HAVE_SHADOW_H struct spwd *spasswd; @@ -47,10 +47,10 @@ void passwordauth() { unsigned char changepw; - passwdcrypt = svr_ses.authstate.pw->pw_passwd; + passwdcrypt = ses.authstate.pw->pw_passwd; #ifdef HAVE_SHADOW_H /* get the shadow password if possible */ - spasswd = getspnam(svr_ses.authstate.pw->pw_name); + spasswd = getspnam(ses.authstate.pw->pw_name); if (spasswd != NULL && spasswd->sp_pwdp != NULL) { passwdcrypt = spasswd->sp_pwdp; } @@ -66,7 +66,7 @@ void passwordauth() { * in auth.c */ if (passwdcrypt[0] == '\0') { dropbear_log(LOG_WARNING, "user '%s' has blank password, rejected", - svr_ses.authstate.printableuser); + ses.authstate.printableuser); send_msg_userauth_failure(0, 1); return; } @@ -92,12 +92,12 @@ void passwordauth() { /* successful authentication */ dropbear_log(LOG_NOTICE, "password auth succeeded for '%s'", - svr_ses.authstate.printableuser); + ses.authstate.printableuser); send_msg_userauth_success(); } else { dropbear_log(LOG_WARNING, "bad password attempt for '%s'", - svr_ses.authstate.printableuser); + ses.authstate.printableuser); send_msg_userauth_failure(0, 1); } diff --git a/svr-authpubkey.c b/svr-authpubkey.c index e3e6947..9c58a8d 100644 --- a/svr-authpubkey.c +++ b/svr-authpubkey.c @@ -50,7 +50,7 @@ static int getauthline(buffer * line, FILE * authfile); /* process a pubkey auth request, sending success or failure message as * appropriate */ -void pubkeyauth() { +void svr_auth_pubkey() { unsigned char testkey; /* whether we're just checking if a key is usable */ unsigned char* algo = NULL; /* pubkey algo */ @@ -113,12 +113,12 @@ void pubkeyauth() { signbuf->len) == DROPBEAR_SUCCESS) { dropbear_log(LOG_NOTICE, "pubkey auth succeeded for '%s' with key %s", - svr_ses.authstate.printableuser, fp); + ses.authstate.printableuser, fp); send_msg_userauth_success(); } else { dropbear_log(LOG_WARNING, "pubkey auth bad signature for '%s' with key %s", - svr_ses.authstate.printableuser, fp); + ses.authstate.printableuser, fp); send_msg_userauth_failure(0, 1); } m_free(fp); @@ -178,7 +178,7 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, if (have_algo(algo, algolen, sshhostkey) == DROPBEAR_FAILURE) { dropbear_log(LOG_WARNING, "pubkey auth attempt with unknown algo for '%s'", - svr_ses.authstate.printableuser); + ses.authstate.printableuser); goto out; } @@ -190,12 +190,12 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, /* we don't need to check pw and pw_dir for validity, since * its been done in checkpubkeyperms. */ - len = strlen(svr_ses.authstate.pw->pw_dir); + len = strlen(ses.authstate.pw->pw_dir); /* allocate max required pathname storage, * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */ filename = m_malloc(len + 22); snprintf(filename, len + 22, "%s/.ssh/authorized_keys", - svr_ses.authstate.pw->pw_dir); + ses.authstate.pw->pw_dir); /* open the file */ authfile = fopen(filename, "r"); @@ -352,19 +352,19 @@ static int checkpubkeyperms() { TRACE(("enter checkpubkeyperms")); - assert(svr_ses.authstate.pw); - if (svr_ses.authstate.pw->pw_dir == NULL) { + assert(ses.authstate.pw); + if (ses.authstate.pw->pw_dir == NULL) { goto out; } - if ((len = strlen(svr_ses.authstate.pw->pw_dir)) == 0) { + if ((len = strlen(ses.authstate.pw->pw_dir)) == 0) { goto out; } /* allocate max required pathname storage, * = path + "/.ssh/authorized_keys" + '\0' = pathlen + 22 */ filename = m_malloc(len + 22); - strncpy(filename, svr_ses.authstate.pw->pw_dir, len+1); + strncpy(filename, ses.authstate.pw->pw_dir, len+1); /* check ~ */ if (checkfileperm(filename) != DROPBEAR_SUCCESS) { @@ -406,7 +406,7 @@ static int checkfileperm(char * filename) { return DROPBEAR_FAILURE; } /* check ownership - user or root only*/ - if (filestat.st_uid != svr_ses.authstate.pw->pw_uid + if (filestat.st_uid != ses.authstate.pw->pw_uid && filestat.st_uid != 0) { TRACE(("leave checkfileperm: wrong ownership")); return DROPBEAR_FAILURE; diff --git a/svr-chansession.c b/svr-chansession.c index cbc7ebe..50e0a5e 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -239,7 +239,7 @@ static void closechansess(struct Channel *channel) { if (chansess->tty) { /* write the utmp/wtmp login record */ - li = login_alloc_entry(chansess->pid, svr_ses.authstate.username, + li = login_alloc_entry(chansess->pid, ses.authstate.username, ses.remotehost, chansess->tty); login_logout(li); login_free_entry(li); @@ -425,7 +425,7 @@ static int sessionpty(struct ChanSess * chansess) { dropbear_exit("out of memory"); /* TODO disconnect */ } - pty_setowner(svr_ses.authstate.pw, chansess->tty); + pty_setowner(ses.authstate.pw, chansess->tty); pty_change_window_size(chansess->master, chansess->termr, chansess->termc, chansess->termw, chansess->termh); @@ -683,7 +683,7 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { /* write the utmp/wtmp login record - must be after changing the * terminal used for stdout with the dup2 above */ - li= login_alloc_entry(getpid(), svr_ses.authstate.username, + li= login_alloc_entry(getpid(), ses.authstate.username, ses.remotehost, chansess->tty); login_login(li); login_free_entry(li); @@ -695,10 +695,10 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { /* don't show the motd if ~/.hushlogin exists */ /* 11 == strlen("/hushlogin\0") */ - len = strlen(svr_ses.authstate.pw->pw_dir) + 11; + len = strlen(ses.authstate.pw->pw_dir) + 11; hushpath = m_malloc(len); - snprintf(hushpath, len, "%s/hushlogin", svr_ses.authstate.pw->pw_dir); + snprintf(hushpath, len, "%s/hushlogin", ses.authstate.pw->pw_dir); if (stat(hushpath, &sb) < 0) { /* more than a screenful is stupid IMHO */ @@ -808,10 +808,10 @@ static void execchild(struct ChanSess *chansess) { /* We can only change uid/gid as root ... */ if (getuid() == 0) { - if ((setgid(svr_ses.authstate.pw->pw_gid) < 0) || - (initgroups(svr_ses.authstate.pw->pw_name, - svr_ses.authstate.pw->pw_gid) < 0) || - (setuid(svr_ses.authstate.pw->pw_uid) < 0)) { + if ((setgid(ses.authstate.pw->pw_gid) < 0) || + (initgroups(ses.authstate.pw->pw_name, + ses.authstate.pw->pw_gid) < 0) || + (setuid(ses.authstate.pw->pw_uid) < 0)) { dropbear_exit("error changing user"); } } else { @@ -822,29 +822,29 @@ static void execchild(struct ChanSess *chansess) { * usernames with the same uid, but differing groups, then the * differing groups won't be set (as with initgroups()). The solution * is for the sysadmin not to give out the UID twice */ - if (getuid() != svr_ses.authstate.pw->pw_uid) { + if (getuid() != ses.authstate.pw->pw_uid) { dropbear_exit("couldn't change user as non-root"); } } /* an empty shell should be interpreted as "/bin/sh" */ - if (svr_ses.authstate.pw->pw_shell[0] == '\0') { + if (ses.authstate.pw->pw_shell[0] == '\0') { usershell = "/bin/sh"; } else { - usershell = svr_ses.authstate.pw->pw_shell; + usershell = ses.authstate.pw->pw_shell; } /* set env vars */ - addnewvar("USER", svr_ses.authstate.pw->pw_name); - addnewvar("LOGNAME", svr_ses.authstate.pw->pw_name); - addnewvar("HOME", svr_ses.authstate.pw->pw_dir); + addnewvar("USER", ses.authstate.pw->pw_name); + addnewvar("LOGNAME", ses.authstate.pw->pw_name); + addnewvar("HOME", ses.authstate.pw->pw_dir); addnewvar("SHELL", usershell); if (chansess->term != NULL) { addnewvar("TERM", chansess->term); } /* change directory */ - if (chdir(svr_ses.authstate.pw->pw_dir) < 0) { + if (chdir(ses.authstate.pw->pw_dir) < 0) { dropbear_exit("error changing directory"); } diff --git a/svr-runopts.c b/svr-runopts.c index 1f5b0c6..c79208c 100644 --- a/svr-runopts.c +++ b/svr-runopts.c @@ -106,8 +106,8 @@ void svr_getopts(int argc, char ** argv) { opts.nolocaltcp = 0; opts.noremotetcp = 0; /* not yet - svr_opts.ipv4 = 1; - svr_opts.ipv6 = 1; + opts.ipv4 = 1; + opts.ipv6 = 1; */ #ifdef DO_MOTD svr_opts.domotd = 1; diff --git a/svr-service.c b/svr-service.c index 7b717bf..e823490 100644 --- a/svr-service.c +++ b/svr-service.c @@ -56,7 +56,7 @@ void recv_msg_service_request() { /* ssh-connection */ if (len == SSH_SERVICE_CONNECTION_LEN && (strncmp(SSH_SERVICE_CONNECTION, name, len) == 0)) { - if (ses.authdone != 1) { + if (ses.authstate.authdone != 1) { dropbear_exit("request for connection before auth"); } @@ -70,7 +70,6 @@ void recv_msg_service_request() { /* TODO this should be a MSG_DISCONNECT */ dropbear_exit("unrecognised SSH_MSG_SERVICE_REQUEST"); - TRACE(("leave recv_msg_service_request")); } diff --git a/svr-session.c b/svr-session.c index cb309fe..80c622a 100644 --- a/svr-session.c +++ b/svr-session.c @@ -79,7 +79,7 @@ void svr_session(int sock, int childpipe, char* remotehost) { /* Initialise server specific parts of the session */ svr_ses.childpipe = childpipe; - authinitialise(); + svr_authinitialise(); chaninitialise(svr_chantypes); svr_chansessinitialise(); @@ -90,10 +90,11 @@ void svr_session(int sock, int childpipe, char* remotehost) { ses.connecttimeout = timeout.tv_sec + AUTH_TIMEOUT; /* set up messages etc */ - session_remoteclosed = svr_remoteclosed; + ses.remoteclosed = svr_remoteclosed; /* packet handlers */ ses.packettypes = svr_packettypes; + ses.buf_match_algo = svr_buf_match_algo; /* We're ready to go now */ sessinitdone = 1; @@ -123,16 +124,16 @@ void svr_dropbear_exit(int exitcode, const char* format, va_list param) { /* before session init */ snprintf(fmtbuf, sizeof(fmtbuf), "premature exit: %s", format); - } else if (svr_ses.authstate.authdone) { + } else if (ses.authstate.authdone) { /* user has authenticated */ snprintf(fmtbuf, sizeof(fmtbuf), "exit after auth (%s): %s", - svr_ses.authstate.printableuser, format); - } else if (svr_ses.authstate.printableuser) { + ses.authstate.printableuser, format); + } else if (ses.authstate.printableuser) { /* we have a potential user */ snprintf(fmtbuf, sizeof(fmtbuf), "exit before auth (user '%s', %d fails): %s", - svr_ses.authstate.printableuser, svr_ses.authstate.failcount, format); + ses.authstate.printableuser, ses.authstate.failcount, format); } else { /* before userauth */ snprintf(fmtbuf, sizeof(fmtbuf), -- cgit v1.2.3 From 051b7454f80a52d2b0bea2e34562949a3bc70fe4 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Sun, 1 Aug 2004 08:54:01 +0000 Subject: - Added terminal mode handling etc for the client, and window change - Refactored the terminal-mode handling for the server - Improved session closing for the client --HG-- extra : convert_revision : 9d19b4f22c39798af5f3f24c2022f8caec4919e8 --- chansession.h | 4 +- cli-auth.c | 2 + cli-chansession.c | 125 +++++++++++++++++++++++++++++----- cli-main.c | 7 +- cli-runopts.c | 3 +- cli-session.c | 29 ++++++++ dbutil.c | 2 + options.h | 2 +- runopts.h | 1 + session.h | 1 + signkey.c | 1 - svr-chansession.c | 196 ++++++++++++++++++++++++++++-------------------------- 12 files changed, 255 insertions(+), 118 deletions(-) (limited to 'svr-chansession.c') diff --git a/chansession.h b/chansession.h index 7879791..0930d9d 100644 --- a/chansession.h +++ b/chansession.h @@ -39,7 +39,6 @@ struct ChanSess { int slave; unsigned char * tty; unsigned char * term; - unsigned int termw, termh, termc, termr; /* width, height, col, rows */ /* exit details */ int exited; @@ -76,6 +75,9 @@ void send_msg_chansess_exitsignal(struct Channel * channel, struct ChanSess * chansess); void addnewvar(const char* param, const char* var); +void cli_send_chansess_request(); +void cli_tty_cleanup(); + void svr_chansessinitialise(); extern const struct ChanType svrchansess; diff --git a/cli-auth.c b/cli-auth.c index 37e7814..e081587 100644 --- a/cli-auth.c +++ b/cli-auth.c @@ -7,6 +7,8 @@ #include "packet.h" #include "runopts.h" +#undef DROPBEAR_PUBKEY_AUTH + void cli_authinitialise() { memset(&ses.authstate, 0, sizeof(ses.authstate)); diff --git a/cli-chansession.c b/cli-chansession.c index 852bffa..be6fc14 100644 --- a/cli-chansession.c +++ b/cli-chansession.c @@ -6,6 +6,7 @@ #include "channel.h" #include "ssh.h" #include "runopts.h" +#include "termcodes.h" static void cli_closechansess(struct Channel *channel); static int cli_initchansess(struct Channel *channel); @@ -16,7 +17,7 @@ static void send_chansess_pty_req(struct Channel *channel); static void send_chansess_shell_req(struct Channel *channel); static void cli_tty_setup(); -static void cli_tty_cleanup(); +void cli_tty_cleanup(); static const struct ChanType clichansess = { 0, /* sepfds */ @@ -50,7 +51,6 @@ static void start_channel_request(struct Channel *channel, } - /* Taken from OpenSSH's sshtty.c: * RCSID("OpenBSD: sshtty.c,v 1.5 2003/09/19 17:43:35 markus Exp "); */ static void cli_tty_setup() { @@ -91,12 +91,13 @@ static void cli_tty_setup() { TRACE(("leave cli_tty_setup")); } -static void cli_tty_cleanup() { +void cli_tty_cleanup() { TRACE(("enter cli_tty_cleanup")); if (cli_ses.tty_raw_mode == 0) { TRACE(("leave cli_tty_cleanup: not in raw mode")); + return; } if (tcsetattr(STDIN_FILENO, TCSADRAIN, &cli_ses.saved_tio) == -1) { @@ -108,30 +109,123 @@ static void cli_tty_cleanup() { TRACE(("leave cli_tty_cleanup")); } +static void put_termcodes() { + + TRACE(("enter put_termcodes")); + + struct termios tio; + unsigned int sshcode; + const struct TermCode *termcode; + unsigned int value; + unsigned int mapcode; + + unsigned int bufpos1, bufpos2; + + if (tcgetattr(STDIN_FILENO, &tio) == -1) { + dropbear_log(LOG_WARNING, "Failed reading termmodes"); + buf_putint(ses.writepayload, 1); /* Just the terminator */ + buf_putbyte(ses.writepayload, 0); /* TTY_OP_END */ + return; + } + + bufpos1 = ses.writepayload->pos; + buf_putint(ses.writepayload, 0); /* A placeholder for the final length */ + + /* As with Dropbear server, we ignore baud rates for now */ + for (sshcode = 1; sshcode < MAX_TERMCODE; sshcode++) { + + termcode = &termcodes[sshcode]; + mapcode = termcode->mapcode; + + switch (termcode->type) { + + case TERMCODE_NONE: + continue; + + case TERMCODE_CONTROLCHAR: + value = tio.c_cc[mapcode]; + break; + + case TERMCODE_INPUT: + value = tio.c_iflag & mapcode; + break; + + case TERMCODE_OUTPUT: + value = tio.c_oflag & mapcode; + break; + + case TERMCODE_LOCAL: + value = tio.c_lflag & mapcode; + break; + + case TERMCODE_CONTROL: + value = tio.c_cflag & mapcode; + break; + + default: + continue; + + } + + /* If we reach here, we have something to say */ + buf_putbyte(ses.writepayload, sshcode); + buf_putint(ses.writepayload, value); + } + + buf_putbyte(ses.writepayload, 0); /* THE END, aka TTY_OP_END */ + + /* Put the string length at the start of the buffer */ + bufpos2 = ses.writepayload->pos; + + buf_setpos(ses.writepayload, bufpos1); /* Jump back */ + buf_putint(ses.writepayload, bufpos2 - bufpos1); /* len(termcodes) */ + buf_setpos(ses.writepayload, bufpos2); /* Back where we were */ + + TRACE(("leave put_termcodes")); +} + +static void put_winsize() { + + struct winsize ws; + + if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) { + /* Some sane defaults */ + ws.ws_row = 25; + ws.ws_col = 80; + ws.ws_xpixel = 0; + ws.ws_ypixel = 0; + } + + buf_putint(ses.writepayload, ws.ws_col); /* Cols */ + buf_putint(ses.writepayload, ws.ws_row); /* Rows */ + buf_putint(ses.writepayload, ws.ws_xpixel); /* Width */ + buf_putint(ses.writepayload, ws.ws_ypixel); /* Height */ + +} + static void send_chansess_pty_req(struct Channel *channel) { - unsigned char* termmodes = "\0"; unsigned char* term = NULL; - int termc = 80, termr = 25, termw = 0, termh = 0; /* XXX TODO matt */ TRACE(("enter send_chansess_pty_req")); + start_channel_request(channel, "pty-req"); + /* Don't want replies */ + buf_putbyte(ses.writepayload, 0); + + /* Get the terminal */ term = getenv("TERM"); if (term == NULL) { - term = "vt100"; + term = "vt100"; /* Seems a safe default */ } - - /* XXX TODO */ - buf_putbyte(ses.writepayload, 0); /* Don't want replies */ buf_putstring(ses.writepayload, term, strlen(term)); - buf_putint(ses.writepayload, termc); /* Cols */ - buf_putint(ses.writepayload, termr); /* Rows */ - buf_putint(ses.writepayload, termw); /* Width */ - buf_putint(ses.writepayload, termh); /* Height */ - buf_putstring(ses.writepayload, termmodes, 1); /* XXX TODO */ - //m_free(termmodes); + /* Window size */ + put_winsize(); + + /* Terminal mode encoding */ + put_termcodes(); encrypt_packet(); TRACE(("leave send_chansess_pty_req")); @@ -171,7 +265,6 @@ static int cli_initchansess(struct Channel *channel) { send_chansess_pty_req(channel); } - cli_opts.cmd = "df"; send_chansess_shell_req(channel); if (cli_opts.wantpty) { diff --git a/cli-main.c b/cli-main.c index c5d5b3e..106cc64 100644 --- a/cli-main.c +++ b/cli-main.c @@ -61,9 +61,12 @@ static void cli_dropbear_exit(int exitcode, const char* format, va_list param) { cli_opts.remoteport, format); } + /* Do the cleanup first, since then the terminal will be reset */ + cli_session_cleanup(); + common_session_cleanup(); + _dropbear_log(LOG_INFO, fmtbuf, param); - common_session_cleanup(); exit(exitcode); } @@ -73,6 +76,6 @@ static void cli_dropbear_log(int priority, const char* format, va_list param) { vsnprintf(printbuf, sizeof(printbuf), format, param); - fprintf(stderr, "Dropbear: %s\n", printbuf); + fprintf(stderr, "%s: %s\n", cli_opts.progname, printbuf); } diff --git a/cli-runopts.c b/cli-runopts.c index 137e0da..2811ef3 100644 --- a/cli-runopts.c +++ b/cli-runopts.c @@ -55,11 +55,12 @@ void cli_getopts(int argc, char ** argv) { char* userhostarg = NULL; /* see printhelp() for options */ + cli_opts.progname = argv[0]; cli_opts.remotehost = NULL; cli_opts.remoteport = NULL; cli_opts.username = NULL; cli_opts.cmd = NULL; - cli_opts.wantpty = 0; + cli_opts.wantpty = 1; opts.nolocaltcp = 0; opts.noremotetcp = 0; /* not yet diff --git a/cli-session.c b/cli-session.c index 258d5c4..42770f5 100644 --- a/cli-session.c +++ b/cli-session.c @@ -9,10 +9,13 @@ #include "channel.h" #include "random.h" #include "service.h" +#include "runopts.h" +#include "chansession.h" static void cli_remoteclosed(); static void cli_sessionloop(); static void cli_session_init(); +static void cli_finished(); struct clientsession cli_ses; /* GLOBAL */ @@ -163,6 +166,12 @@ static void cli_sessionloop() { cli_ses.state = SESSION_RUNNING; return; + case SESSION_RUNNING: + if (ses.chancount < 1) { + cli_finished(); + } + return; + /* XXX more here needed */ @@ -174,6 +183,26 @@ static void cli_sessionloop() { } +void cli_session_cleanup() { + + if (!sessinitdone) { + return; + } + cli_tty_cleanup(); + +} + +static void cli_finished() { + + cli_session_cleanup(); + common_session_cleanup(); + fprintf(stderr, "Connection to %s@%s:%s closed.\n", cli_opts.username, + cli_opts.remotehost, cli_opts.remoteport); + exit(EXIT_SUCCESS); +} + + + /* called when the remote side closes the connection */ static void cli_remoteclosed() { diff --git a/dbutil.c b/dbutil.c index 1c0648c..a89c3c8 100644 --- a/dbutil.c +++ b/dbutil.c @@ -103,6 +103,7 @@ void dropbear_log(int priority, const char* format, ...) { #ifdef DEBUG_TRACE void dropbear_trace(const char* format, ...) { +#if 0 va_list param; va_start(param, format); @@ -110,6 +111,7 @@ void dropbear_trace(const char* format, ...) { vfprintf(stderr, format, param); fprintf(stderr, "\n"); va_end(param); +#endif } #endif /* DEBUG_TRACE */ diff --git a/options.h b/options.h index ee0ee60..1ab16c7 100644 --- a/options.h +++ b/options.h @@ -111,7 +111,7 @@ /* Authentication types to enable, at least one required. RFC Draft requires pubkey auth, and recommends password */ #define DROPBEAR_PASSWORD_AUTH -//#define DROPBEAR_PUBKEY_AUTH +#define DROPBEAR_PUBKEY_AUTH /* Random device to use - you must specify _one only_. * DEV_RANDOM is recommended on hosts with a good /dev/urandom, otherwise use diff --git a/runopts.h b/runopts.h index b2fc149..26d15ef 100644 --- a/runopts.h +++ b/runopts.h @@ -79,6 +79,7 @@ void svr_getopts(int argc, char ** argv); /* Uncompleted XXX matt */ typedef struct cli_runopts { + char *progname; char *remotehost; char *remoteport; diff --git a/session.h b/session.h index 1000c5e..1ce944a 100644 --- a/session.h +++ b/session.h @@ -55,6 +55,7 @@ void svr_dropbear_log(int priority, const char* format, va_list param); void cli_session(int sock, char *remotehost); void cli_dropbear_exit(int exitcode, const char* format, va_list param); void cli_dropbear_log(int priority, const char* format, va_list param); +void cli_session_cleanup(); struct key_context { diff --git a/signkey.c b/signkey.c index faf3739..2fd301f 100644 --- a/signkey.c +++ b/signkey.c @@ -53,7 +53,6 @@ int buf_get_pub_key(buffer *buf, sign_key *key, int *type) { unsigned int len; TRACE(("enter buf_get_pub_key")); - printhex(buf_getptr(buf, 0x99), 0x99); ident = buf_getstring(buf, &len); diff --git a/svr-chansession.c b/svr-chansession.c index 50e0a5e..9639961 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -56,6 +56,7 @@ static void chansessionrequest(struct Channel *channel); static void send_exitsignalstatus(struct Channel *channel); static int sesscheckclose(struct Channel *channel); +static void get_termmodes(struct ChanSess *chansess); /* required to clear environment */ @@ -192,10 +193,6 @@ static int newchansess(struct Channel *channel) { chansess->slave = -1; chansess->tty = NULL; chansess->term = NULL; - chansess->termw = 0; - chansess->termh = 0; - chansess->termc = 0; - chansess->termr = 0; chansess->exited = 0; @@ -376,22 +373,111 @@ static int sessionsignal(struct ChanSess *chansess) { * client. Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ static int sessionwinchange(struct ChanSess *chansess) { + int termc, termr, termw, termh; + if (chansess->master < 0) { /* haven't got a pty yet */ return DROPBEAR_FAILURE; } - chansess->termc = buf_getint(ses.payload); - chansess->termr = buf_getint(ses.payload); - chansess->termw = buf_getint(ses.payload); - chansess->termh = buf_getint(ses.payload); + termc = buf_getint(ses.payload); + termr = buf_getint(ses.payload); + termw = buf_getint(ses.payload); + termh = buf_getint(ses.payload); - pty_change_window_size(chansess->master, chansess->termr, chansess->termc, - chansess->termw, chansess->termh); + pty_change_window_size(chansess->master, termr, termc, termw, termh); return DROPBEAR_FAILURE; } +static void get_termmodes(struct ChanSess *chansess) { + + struct termios termio; + unsigned char opcode; + unsigned int value; + const struct TermCode * termcode; + unsigned int len; + + TRACE(("enter get_termmodes")); + + /* Term modes */ + /* We'll ignore errors and continue if we can't set modes. + * We're ignoring baud rates since they seem evil */ + if (tcgetattr(chansess->master, &termio) == -1) { + return; + } + + len = buf_getint(ses.payload); + if (len != ses.payload->len - ses.payload->pos) { + dropbear_exit("bad term mode string"); + } + + if (len == 0) { + TRACE(("leave get_termmodes: empty terminal modes string")); + } + + while (((opcode = buf_getbyte(ses.payload)) != 0x00) && opcode <= 159) { + + /* must be before checking type, so that value is consumed even if + * we don't use it */ + value = buf_getint(ses.payload); + + /* handle types of code */ + if (opcode > MAX_TERMCODE) { + continue; + } + termcode = &termcodes[(unsigned int)opcode]; + + + switch (termcode->type) { + + case TERMCODE_NONE: + break; + + case TERMCODE_CONTROLCHAR: + termio.c_cc[termcode->mapcode] = value; + break; + + case TERMCODE_INPUT: + if (value) { + termio.c_iflag |= termcode->mapcode; + } else { + termio.c_iflag &= ~(termcode->mapcode); + } + break; + + case TERMCODE_OUTPUT: + if (value) { + termio.c_oflag |= termcode->mapcode; + } else { + termio.c_oflag &= ~(termcode->mapcode); + } + break; + + case TERMCODE_LOCAL: + if (value) { + termio.c_lflag |= termcode->mapcode; + } else { + termio.c_lflag &= ~(termcode->mapcode); + } + break; + + case TERMCODE_CONTROL: + if (value) { + termio.c_cflag |= termcode->mapcode; + } else { + termio.c_cflag &= ~(termcode->mapcode); + } + break; + + } + } + if (tcsetattr(chansess->master, TCSANOW, &termio) < 0) { + dropbear_log(LOG_INFO, "error setting terminal attributes"); + } + TRACE(("leave get_termmodes")); +} + /* Set up a session pty which will be used to execute the shell or program. * The pty is allocated now, and kept for when the shell/program executes. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ @@ -399,7 +485,6 @@ static int sessionpty(struct ChanSess * chansess) { unsigned int termlen; unsigned char namebuf[65]; - struct termios termio; TRACE(("enter sessionpty")); chansess->term = buf_getstring(ses.payload, &termlen); @@ -408,10 +493,6 @@ static int sessionpty(struct ChanSess * chansess) { TRACE(("leave sessionpty: term len too long")); return DROPBEAR_FAILURE; } - chansess->termc = buf_getint(ses.payload); - chansess->termr = buf_getint(ses.payload); - chansess->termw = buf_getint(ses.payload); - chansess->termh = buf_getint(ses.payload); /* allocate the pty */ assert(chansess->master == -1); /* haven't already got one */ @@ -426,89 +507,12 @@ static int sessionpty(struct ChanSess * chansess) { } pty_setowner(ses.authstate.pw, chansess->tty); - pty_change_window_size(chansess->master, chansess->termr, chansess->termc, - chansess->termw, chansess->termh); - /* Term modes */ - /* We'll ignore errors and continue if we can't set modes. - * We're ignoring baud rates since they seem evil */ - if (tcgetattr(chansess->master, &termio) == 0) { - unsigned char opcode; - unsigned int value; - const struct TermCode * termcode; - unsigned int len; - - len = buf_getint(ses.payload); - if (len != ses.payload->len - ses.payload->pos) { - dropbear_exit("bad term mode string"); - } - - if (len == 0) { - TRACE(("empty terminal modes string")); - return DROPBEAR_SUCCESS; - } - - while (((opcode = buf_getbyte(ses.payload)) != 0x00) && - opcode <= 159) { + /* Set up the rows/col counts */ + sessionwinchange(chansess); - /* must be before checking type, so that value is consumed even if - * we don't use it */ - value = buf_getint(ses.payload); - - /* handle types of code */ - if (opcode > MAX_TERMCODE) { - continue; - } - termcode = &termcodes[(unsigned int)opcode]; - - - switch (termcode->type) { - - case TERMCODE_NONE: - break; - - case TERMCODE_CONTROLCHAR: - termio.c_cc[termcode->mapcode] = value; - break; - - case TERMCODE_INPUT: - if (value) { - termio.c_iflag |= termcode->mapcode; - } else { - termio.c_iflag &= ~(termcode->mapcode); - } - break; - - case TERMCODE_OUTPUT: - if (value) { - termio.c_oflag |= termcode->mapcode; - } else { - termio.c_oflag &= ~(termcode->mapcode); - } - break; - - case TERMCODE_LOCAL: - if (value) { - termio.c_lflag |= termcode->mapcode; - } else { - termio.c_lflag &= ~(termcode->mapcode); - } - break; - - case TERMCODE_CONTROL: - if (value) { - termio.c_cflag |= termcode->mapcode; - } else { - termio.c_cflag &= ~(termcode->mapcode); - } - break; - - } - } - if (tcsetattr(chansess->master, TCSANOW, &termio) < 0) { - dropbear_log(LOG_INFO, "error setting terminal attributes"); - } - } + /* Read the terminal modes */ + get_termmodes(chansess); TRACE(("leave sessionpty")); return DROPBEAR_SUCCESS; -- cgit v1.2.3 From a712baa8e566bfd8403a3e2bfdf350a0dc50ea9f Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 10 Aug 2004 17:09:52 +0000 Subject: just checkpointing --HG-- extra : convert_revision : fbbf404290f3fea3dfa9f6f53eba9389057e9044 --- Makefile.in | 11 +- cli-authpubkey.c | 10 +- cli-runopts.c | 18 ++++ cli-session.c | 5 +- cli-tcpfwd.c | 34 ++++++ common-channel.c | 43 -------- dbutil.c | 173 +++++++++++++++++++++++------ dbutil.h | 6 +- listener.c | 42 +++++--- listener.h | 10 +- options.h | 3 + svr-agentfwd.c | 8 +- svr-chansession.c | 2 + svr-main.c | 82 +++++--------- svr-session.c | 6 +- svr-tcpfwd.c | 196 +++++++++++++++++++++++++++++++++ svr-x11fwd.c | 8 +- tcp-accept.c | 88 +++++++++++++++ tcp-accept.h | 19 ++++ tcp-connect.c | 75 +++++++++++++ tcp-connect.h | 35 ++++++ tcpfwd-direct.c | 159 --------------------------- tcpfwd-direct.h | 34 ------ tcpfwd-remote.c | 317 ------------------------------------------------------ tcpfwd-remote.h | 6 -- 25 files changed, 706 insertions(+), 684 deletions(-) create mode 100644 cli-tcpfwd.c create mode 100644 svr-tcpfwd.c create mode 100644 tcp-accept.c create mode 100644 tcp-accept.h create mode 100644 tcp-connect.c create mode 100644 tcp-connect.h delete mode 100644 tcpfwd-direct.c delete mode 100644 tcpfwd-direct.h delete mode 100644 tcpfwd-remote.c delete mode 100644 tcpfwd-remote.h (limited to 'svr-chansession.c') diff --git a/Makefile.in b/Makefile.in index 2bffe90..af24bb0 100644 --- a/Makefile.in +++ b/Makefile.in @@ -24,15 +24,16 @@ COMMONOBJS=dbutil.o buffer.o \ SVROBJS=svr-kex.o svr-algo.o svr-auth.o sshpty.o \ svr-authpasswd.o svr-authpubkey.o svr-session.o svr-service.o \ - svr-chansession.o svr-runopts.o svr-agentfwd.o svr-main.o svr-x11fwd.o + svr-chansession.o svr-runopts.o svr-agentfwd.o svr-main.o svr-x11fwd.o\ + svr-tcpfwd.o CLIOBJS=cli-algo.o cli-main.o cli-auth.o cli-authpasswd.o cli-kex.o \ cli-session.o cli-service.o cli-runopts.o cli-chansession.o \ - cli-authpubkey.o + cli-authpubkey.o cli-tcpfwd.o CLISVROBJS=common-session.o packet.o common-algo.o common-kex.o \ common-channel.o common-chansession.o termcodes.o loginrec.o \ - tcpfwd-direct.o tcpfwd-remote.o listener.o process-packet.o \ + tcp-accept.o tcp-connect.o listener.o process-packet.o \ common-runopts.o KEYOBJS=dropbearkey.o gendss.o genrsa.o @@ -45,8 +46,8 @@ HEADERS=options.h dbutil.h session.h packet.h algo.h ssh.h buffer.h kex.h \ dss.h bignum.h signkey.h rsa.h random.h service.h auth.h authpasswd.h \ debug.h channel.h chansession.h config.h queue.h sshpty.h \ termcodes.h gendss.h genrsa.h authpubkey.h runopts.h includes.h \ - loginrec.h atomicio.h x11fwd.h agentfwd.h tcpfwd-direct.h compat.h \ - tcpfwd-remote.h listener.h + loginrec.h atomicio.h x11fwd.h agentfwd.h tcp-accept.h compat.h \ + tcp-connect.h listener.h dropbearobjs=$(COMMONOBJS) $(CLISVROBJS) $(SVROBJS) dbclientobjs=$(COMMONOBJS) $(CLISVROBJS) $(CLIOBJS) diff --git a/cli-authpubkey.c b/cli-authpubkey.c index 6b6ab51..33514ce 100644 --- a/cli-authpubkey.c +++ b/cli-authpubkey.c @@ -13,17 +13,22 @@ static void send_msg_userauth_pubkey(sign_key *key, int type, int realsign); void cli_pubkeyfail() { struct PubkeyList *keyitem; + struct PubkeyList **previtem; TRACE(("enter cli_pubkeyfail")); + previtem = &cli_opts.pubkeys; + /* Find the key we failed with, and remove it */ for (keyitem = cli_opts.pubkeys; keyitem != NULL; keyitem = keyitem->next) { - if (keyitem->next == cli_ses.lastpubkey) { - keyitem->next = cli_ses.lastpubkey->next; + if (keyitem == cli_ses.lastpubkey) { + *previtem = keyitem->next; } + previtem = &keyitem; } sign_key_free(cli_ses.lastpubkey->key); /* It won't be used again */ m_free(cli_ses.lastpubkey); + TRACE(("leave cli_pubkeyfail")); } @@ -145,6 +150,7 @@ int cli_auth_pubkey() { /* Send a trial request */ send_msg_userauth_pubkey(cli_opts.pubkeys->key, cli_opts.pubkeys->type, 0); + cli_ses.lastpubkey = cli_opts.pubkeys; TRACE(("leave cli_auth_pubkey-success")); return 1; } else { diff --git a/cli-runopts.c b/cli-runopts.c index eb718a4..4c84cc8 100644 --- a/cli-runopts.c +++ b/cli-runopts.c @@ -47,6 +47,12 @@ static void printhelp() { "-T Don't allocate a pty\n" #ifdef DROPBEAR_PUBKEY_AUTH "-i (multiple allowed)\n" +#endif +#ifndef DISABLE_REMOTETCPFWD + "-L Local port forwarding\n" +#endif +#ifndef DISABLE_TCPFWD_DIRECT + "-R Remote port forwarding\n" #endif ,DROPBEAR_VERSION, cli_opts.progname); } @@ -59,6 +65,12 @@ void cli_getopts(int argc, char ** argv) { #ifdef DROPBEAR_PUBKEY_AUTH int nextiskey = 0; /* A flag if the next argument is a keyfile */ #endif +#ifdef DROPBEAR_CLI_LOCALTCP + int nextislocal = 0; +#endif +#ifdef DROPBEAR_CLI_REMOTETCP + int nextisremote = 0; +#endif @@ -71,6 +83,12 @@ void cli_getopts(int argc, char ** argv) { cli_opts.wantpty = 9; /* 9 means "it hasn't been touched", gets set later */ #ifdef DROPBEAR_PUBKEY_AUTH cli_opts.pubkeys = NULL; +#endif +#ifdef DROPBEAR_CLI_LOCALTCP + cli_opts.localports = NULL; +#endif +#ifdef DROPBEAR_CLI_REMOTETCP + cli_opts.remoteports = NULL; #endif opts.nolocaltcp = 0; opts.noremotetcp = 0; diff --git a/cli-session.c b/cli-session.c index 973e9c7..22f7001 100644 --- a/cli-session.c +++ b/cli-session.c @@ -4,8 +4,8 @@ #include "kex.h" #include "ssh.h" #include "packet.h" -#include "tcpfwd-direct.h" -#include "tcpfwd-remote.h" +#include "tcp-accept.h" +#include "tcp-connect.h" #include "channel.h" #include "random.h" #include "service.h" @@ -31,7 +31,6 @@ static const packettype cli_packettypes[] = { {SSH_MSG_KEXDH_REPLY, recv_msg_kexdh_reply}, /* client */ {SSH_MSG_NEWKEYS, recv_msg_newkeys}, {SSH_MSG_SERVICE_ACCEPT, recv_msg_service_accept}, /* client */ - {SSH_MSG_GLOBAL_REQUEST, recv_msg_global_request_remotetcp}, {SSH_MSG_CHANNEL_REQUEST, recv_msg_channel_request}, {SSH_MSG_CHANNEL_OPEN, recv_msg_channel_open}, {SSH_MSG_CHANNEL_EOF, recv_msg_channel_eof}, diff --git a/cli-tcpfwd.c b/cli-tcpfwd.c new file mode 100644 index 0000000..3dc6e20 --- /dev/null +++ b/cli-tcpfwd.c @@ -0,0 +1,34 @@ +#include "includes.h" +#include "options.h" +#include "tcp-accept.h" +#include "tcp-connect.h" +#include "channel.h" + +static const struct ChanType cli_chan_tcplocal = { + 1, /* sepfds */ + "direct-tcpip", + NULL, + NULL, + NULL +}; + + + + +static int cli_localtcp(char* port) { + + struct TCPListener* tcpinfo = NULL; + + tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener*)); + tcpinfo->addr = NULL; + tcpinfo->port = port; + tcpinfo->chantype = &cli_chan_tcplocal; + + ret = listen_tcpfwd(tcpinfo); + + if (ret == DROPBEAR_FAILURE) { + DROPBEAR_LOG(LOG_WARNING, "Failed to listen on port %s", port); + m_free(tcpinfo); + } + return ret; +} diff --git a/common-channel.c b/common-channel.c index fbfb00b..5079031 100644 --- a/common-channel.c +++ b/common-channel.c @@ -32,8 +32,6 @@ #include "dbutil.h" #include "channel.h" #include "ssh.h" -#include "tcpfwd-direct.h" -#include "tcpfwd-remote.h" #include "listener.h" static void send_msg_channel_open_failure(unsigned int remotechan, int reason, @@ -307,13 +305,6 @@ static void send_msg_channel_close(struct Channel *channel) { if (channel->type->closehandler) { channel->type->closehandler(channel); } -#if 0 - if (channel->type == CHANNEL_ID_SESSION) { - send_exitsignalstatus(channel); - - closechansess(channel); - } -#endif CHECKCLEARTOWRITE(); @@ -545,23 +536,6 @@ void recv_msg_channel_request() { send_msg_channel_failure(channel); } -#if 0 - /* handle according to channel type */ - switch (channel->type) { - - case CHANNEL_ID_SESSION: - TRACE(("continue recv_msg_channel_request: session request")); - /* XXX server */ - /* Here we need to do things channel-specific style. Function - * pointer callbacks perhaps */ - chansessionrequest(channel); - break; - - default: - send_msg_channel_failure(channel); - } -#endif - TRACE(("leave recv_msg_channel_request")); } @@ -797,23 +771,6 @@ void recv_msg_channel_open() { } } -#if 0 - /* type specific initialisation */ - if (typeval == CHANNEL_ID_SESSION) { - newchansess(channel); -#ifndef DISABLE_LOCALTCPFWD - } else if (typeval == CHANNEL_ID_TCPDIRECT) { - if (ses.opts->nolocaltcp) { - errtype = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; - } else if (newtcpdirect(channel) == DROPBEAR_FAILURE) { - errtype = SSH_OPEN_CONNECT_FAILED; - deletechannel(channel); - goto failure; - } -#endif - } -#endif - /* success */ send_msg_channel_open_confirmation(channel, channel->recvwindow, channel->recvmaxpacket); diff --git a/dbutil.c b/dbutil.c index dd68416..a377da2 100644 --- a/dbutil.c +++ b/dbutil.c @@ -113,8 +113,109 @@ void dropbear_trace(const char* format, ...) { } #endif /* DEBUG_TRACE */ +/* Listen on address:port. Unless address is NULL, in which case listen on + * everything (ie 0.0.0.0, or ::1 - note that this is IPv? agnostic. Linux is + * broken with respect to listening to v6 or v4, so the addresses you get when + * people connect will be wrong. It doesn't break things, just looks quite + * ugly. Returns the number of sockets bound on success, or -1 on failure. On + * failure, if errstring wasn't NULL, it'll be a newly malloced error + * string.*/ +int dropbear_listen(const char* address, const char* port, + int *socks, unsigned int sockcount, char **errstring, int *maxfd) { + + struct addrinfo hints, *res, *res0; + int err; + unsigned int nsock; + struct linger linger; + int val; + int sock; + + TRACE(("enter dropbear_listen")); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */ + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + err = getaddrinfo(address, port, &hints, &res0); + + if (err) { + if (errstring != NULL && *errstring == NULL) { + int len; + len = 20 + strlen(gai_strerror(err)); + *errstring = (char*)m_malloc(len); + snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err)); + } + TRACE(("leave dropbear_listen: failed resolving")); + return -1; + } + + + nsock = 0; + for (res = res0; res != NULL && nsock < sockcount; + res = res->ai_next) { + + /* Get a socket */ + socks[nsock] = socket(res->ai_family, res->ai_socktype, + res->ai_protocol); + + sock = socks[nsock]; /* For clarity */ + + if (sock < 0) { + err = errno; + TRACE(("socket() failed")); + continue; + } + + /* Various useful socket options */ + val = 1; + /* set to reuse, quick timeout */ + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &val, sizeof(val)); + linger.l_onoff = 1; + linger.l_linger = 5; + setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&linger, sizeof(linger)); + + /* disable nagle */ + setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val)); + + if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { + err = errno; + close(sock); + TRACE(("bind() failed")); + continue; + } + + if (listen(sock, 20) < 0) { + err = errno; + close(sock); + TRACE(("listen() failed")); + continue; + } + + *maxfd = MAX(*maxfd, sock); + + nsock++; + } + + if (nsock == 0) { + if (errstring != NULL && *errstring == NULL) { + int len; + len = 20 + strlen(strerror(err)); + *errstring = (char*)m_malloc(len); + snprintf(*errstring, len, "Error connecting: %s", strerror(err)); + TRACE(("leave dropbear_listen: failure, %s", strerror(err))); + return -1; + } + } + + TRACE(("leave dropbear_listen: success, %d socks bound", nsock)); + return nsock; +} + /* Connect via TCP to a host. Connection will try ipv4 or ipv6, will - * return immediately if nonblocking is set */ + * return immediately if nonblocking is set. On failure, if errstring + * wasn't null, it will be a newly malloced error message */ + +/* TODO: maxfd */ int connect_remote(const char* remotehost, const char* remoteport, int nonblocking, char ** errstring) { @@ -197,58 +298,70 @@ int connect_remote(const char* remotehost, const char* remoteport, } freeaddrinfo(res0); + if (sock > 0 && errstring != NULL && *errstring != NULL) { + m_free(*errstring); + } - TRACE(("leave connect_remote: sock %d", sock)); + TRACE(("leave connect_remote: sock %d\n", sock)); return sock; } /* Return a string representation of the socket address passed. The return * value is allocated with malloc() */ -unsigned char * getaddrstring(struct sockaddr * addr) { +unsigned char * getaddrstring(struct sockaddr_storage* addr, int withport) { - char *retstring; + char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; + char *retstring = NULL; + int ret; + unsigned int len; - /* space for "255.255.255.255:65535\0" = 22 */ - retstring = m_malloc(22); + len = sizeof(struct sockaddr_storage); - switch (addr->sa_family) { - case PF_INET: - snprintf(retstring, 22, "%s:%hu", - inet_ntoa(((struct sockaddr_in *)addr)->sin_addr), - ((struct sockaddr_in *)addr)->sin_port); - break; + ret = getnameinfo((struct sockaddr*)addr, len, hbuf, sizeof(hbuf), + sbuf, sizeof(sbuf), NI_NUMERICSERV | NI_NUMERICHOST); - default: - /* XXX ipv6 */ - strcpy(retstring, "Bad protocol"); + if (ret != 0) { + /* This is a fairly bad failure - it'll fallback to IP if it + * just can't resolve */ + dropbear_exit("failed lookup (%d, %d)", ret, errno); + } + if (withport) { + len = strlen(hbuf) + 2 + strlen(sbuf); + retstring = (char*)m_malloc(len); + snprintf(retstring, len, "%s:%s", hbuf, sbuf); + } else { + retstring = m_strdup(hbuf); } + return retstring; } /* Get the hostname corresponding to the address addr. On failure, the IP * address is returned. The return value is allocated with strdup() */ -char* getaddrhostname(struct sockaddr * addr) { +char* getaddrhostname(struct sockaddr_storage * addr) { - struct hostent *host = NULL; - char * retstring; + char hbuf[NI_MAXHOST]; + char sbuf[NI_MAXSERV]; + int ret; + unsigned int len; -#ifdef DO_HOST_LOOKUP - host = gethostbyaddr((char*)&((struct sockaddr_in*)addr)->sin_addr, - sizeof(struct in_addr), AF_INET); -#endif - - if (host == NULL) { - /* return the address */ - retstring = inet_ntoa(((struct sockaddr_in *)addr)->sin_addr); - } else { - /* return the hostname */ - retstring = host->h_name; + len = sizeof(struct sockaddr_storage); + + ret = getnameinfo((struct sockaddr*)addr, len, hbuf, sizeof(hbuf), + sbuf, sizeof(sbuf), NI_NUMERICSERV); + + if (ret != 0) { + /* On some systems (Darwin does it) we get EINTR from getnameinfo + * somehow. Eew. So we'll just return the IP, since that doesn't seem + * to exhibit that behaviour. */ + return getaddrstring(addr, 0); } - return m_strdup(retstring); + return m_strdup(hbuf); } + #ifdef DEBUG_TRACE void printhex(unsigned char* buf, int len) { diff --git a/dbutil.h b/dbutil.h index 21a9955..ce0f311 100644 --- a/dbutil.h +++ b/dbutil.h @@ -44,10 +44,12 @@ void dropbear_trace(const char* format, ...); void printhex(unsigned char* buf, int len); #endif char * stripcontrol(const char * text); -unsigned char * getaddrstring(struct sockaddr * addr); +unsigned char * getaddrstring(struct sockaddr_storage* addr, int withport); +int dropbear_listen(const char* address, const char* port, + int *socks, unsigned int sockcount, char **errstring, int *maxfd); int connect_remote(const char* remotehost, const char* remoteport, int nonblocking, char ** errstring); -char* getaddrhostname(struct sockaddr * addr); +char* getaddrhostname(struct sockaddr_storage * addr); int buf_readfile(buffer* buf, const char* filename); int buf_getline(buffer * line, FILE * authfile); diff --git a/listener.c b/listener.c index 7296a61..3022bd5 100644 --- a/listener.c +++ b/listener.c @@ -14,15 +14,16 @@ void listeners_initialise() { void set_listener_fds(fd_set * readfds) { - unsigned int i; + unsigned int i, j; struct Listener *listener; /* check each in turn */ for (i = 0; i < ses.listensize; i++) { listener = ses.listeners[i]; if (listener != NULL) { - TRACE(("set listener fd %d", listener->sock)); - FD_SET(listener->sock, readfds); + for (j = 0; j < listener->nsocks; j++) { + FD_SET(listener->socks[j], readfds); + } } } } @@ -30,16 +31,19 @@ void set_listener_fds(fd_set * readfds) { void handle_listeners(fd_set * readfds) { - unsigned int i; + unsigned int i, j; struct Listener *listener; + int sock; /* check each in turn */ for (i = 0; i < ses.listensize; i++) { listener = ses.listeners[i]; if (listener != NULL) { - TRACE(("handle listener num %d fd %d", i, listener->sock)); - if (FD_ISSET(listener->sock, readfds)) { - listener->accepter(listener); + for (j = 0; j < listener->nsocks; j++) { + sock = listener->socks[j]; + if (FD_ISSET(sock, readfds)) { + listener->accepter(listener, sock); + } } } } @@ -48,8 +52,9 @@ void handle_listeners(fd_set * readfds) { /* accepter(int fd, void* typedata) is a function to accept connections, * cleanup(void* typedata) happens when cleaning up */ -struct Listener* new_listener(int sock, int type, void* typedata, - void (*accepter)(struct Listener*), +struct Listener* new_listener(int socks[], unsigned int nsocks, + int type, void* typedata, + void (*accepter)(struct Listener*, int sock), void (*cleanup)(struct Listener*)) { unsigned int i, j; @@ -65,7 +70,9 @@ struct Listener* new_listener(int sock, int type, void* typedata, if (i == ses.listensize) { if (ses.listensize > MAX_LISTENERS) { TRACE(("leave newlistener: too many already")); - close(sock); + for (j = 0; j < nsocks; j++) { + close(socks[i]); + } return NULL; } @@ -80,15 +87,18 @@ struct Listener* new_listener(int sock, int type, void* typedata, } } - ses.maxfd = MAX(ses.maxfd, sock); + for (j = 0; j < nsocks; j++) { + ses.maxfd = MAX(ses.maxfd, socks[j]); + } - TRACE(("new listener num %d fd %d", i, sock)); + TRACE(("new listener num %d ", i)); newlisten = (struct Listener*)m_malloc(sizeof(struct Listener)); newlisten->index = i; newlisten->type = type; newlisten->typedata = typedata; - newlisten->sock = sock; + newlisten->nsocks = nsocks; + memcpy(newlisten->socks, socks, nsocks * sizeof(int)); newlisten->accepter = accepter; newlisten->cleanup = cleanup; @@ -116,11 +126,15 @@ struct Listener * get_listener(int type, void* typedata, void remove_listener(struct Listener* listener) { + unsigned int j; + if (listener->cleanup) { listener->cleanup(listener); } - close(listener->sock); + for (j = 0; j < listener->nsocks; j++) { + close(listener->socks[j]); + } ses.listeners[listener->index] = NULL; m_free(listener); diff --git a/listener.h b/listener.h index bda24ff..c634ead 100644 --- a/listener.h +++ b/listener.h @@ -6,11 +6,12 @@ struct Listener { - int sock; + int socks[DROPBEAR_MAX_SOCKS]; + unsigned int nsocks; int index; /* index in the array of listeners */ - void (*accepter)(struct Listener*); + void (*accepter)(struct Listener*, int sock); void (*cleanup)(struct Listener*); int type; /* CHANNEL_ID_X11, CHANNEL_ID_AGENT, @@ -25,8 +26,9 @@ void listeners_initialise(); void handle_listeners(fd_set * readfds); void set_listener_fds(fd_set * readfds); -struct Listener* new_listener(int sock, int type, void* typedata, - void (*accepter)(struct Listener*), +struct Listener* new_listener(int socks[], unsigned int nsocks, + int type, void* typedata, + void (*accepter)(struct Listener*, int sock), void (*cleanup)(struct Listener*)); struct Listener * get_listener(int type, void* typedata, diff --git a/options.h b/options.h index d4abf09..99115db 100644 --- a/options.h +++ b/options.h @@ -280,6 +280,9 @@ /* For a 4096 bit DSS key, empirically determined to be 1590 bytes */ #define MAX_PRIVKEY_SIZE 1600 +#define DROPBEAR_MAX_SOCKS 2 /* IPv4, IPv6 are all we'll get for now. Revisit + in a few years time.... */ + #ifndef ENABLE_X11FWD #define DISABLE_X11FWD #endif diff --git a/svr-agentfwd.c b/svr-agentfwd.c index 0dad2a4..b588586 100644 --- a/svr-agentfwd.c +++ b/svr-agentfwd.c @@ -44,7 +44,7 @@ static int send_msg_channel_open_agent(int fd); static int bindagent(int fd, struct ChanSess * chansess); -static void agentaccept(struct Listener * listener); +static void agentaccept(struct Listener * listener, int sock); /* Handles client requests to start agent forwarding, sets up listening socket. * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ @@ -78,7 +78,7 @@ int agentreq(struct ChanSess * chansess) { } /* pass if off to listener */ - chansess->agentlistener = new_listener( fd, 0, chansess, + chansess->agentlistener = new_listener( &fd, 1, 0, chansess, agentaccept, NULL); if (chansess->agentlistener == NULL) { @@ -97,11 +97,11 @@ fail: /* accepts a connection on the forwarded socket and opens a new channel for it * back to the client */ /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ -static void agentaccept(struct Listener * listener) { +static void agentaccept(struct Listener * listener, int sock) { int fd; - fd = accept(listener->sock, NULL, NULL); + fd = accept(sock, NULL, NULL); if (fd < 0) { TRACE(("accept failed")); return; diff --git a/svr-chansession.c b/svr-chansession.c index 9639961..a0e877c 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -408,6 +408,8 @@ static void get_termmodes(struct ChanSess *chansess) { } len = buf_getint(ses.payload); + TRACE(("term mode str %d p->l %d p->p %d", + len, ses.payload->len , ses.payload->pos)); if (len != ses.payload->len - ses.payload->pos) { dropbear_exit("bad term mode string"); } diff --git a/svr-main.c b/svr-main.c index 312e47c..6a49626 100644 --- a/svr-main.c +++ b/svr-main.c @@ -29,7 +29,7 @@ #include "signkey.h" #include "runopts.h" -static int listensockets(int *sock, int *maxfd); +static int listensockets(int *sock, int sockcount, int *maxfd); static void sigchld_handler(int dummy); static void sigsegv_handler(int); static void sigintterm_handler(int fish); @@ -49,10 +49,10 @@ int main(int argc, char ** argv) unsigned int i, j; int val; int maxsock = -1; - struct sockaddr remoteaddr; + struct sockaddr_storage remoteaddr; int remoteaddrlen; int listensocks[MAX_LISTEN_ADDR]; - unsigned int listensockcount = 0; + int listensockcount = 0; FILE * pidfile; int childsock; @@ -127,7 +127,10 @@ int main(int argc, char ** argv) /* Set up the listening sockets */ /* XXX XXX ports */ - listensockcount = listensockets(listensocks, &maxsock); + listensockcount = listensockets(listensocks, MAX_LISTEN_ADDR, &maxsock); + if (listensockcount < 0) { + dropbear_exit("No listening ports available."); + } /* incoming connection select loop */ for(;;) { @@ -138,7 +141,7 @@ int main(int argc, char ** argv) seltimeout.tv_usec = 0; /* listening sockets */ - for (i = 0; i < listensockcount; i++) { + for (i = 0; i < (unsigned int)listensockcount; i++) { FD_SET(listensocks[i], &fds); } @@ -179,12 +182,12 @@ int main(int argc, char ** argv) } /* handle each socket which has something to say */ - for (i = 0; i < listensockcount; i++) { + for (i = 0; i < (unsigned int)listensockcount; i++) { if (!FD_ISSET(listensocks[i], &fds)) continue; /* child connection XXX - ip6 stuff here */ - remoteaddrlen = sizeof(struct sockaddr_in); + remoteaddrlen = sizeof(remoteaddr); childsock = accept(listensocks[i], &remoteaddr, &remoteaddrlen); if (childsock < 0) { @@ -222,7 +225,7 @@ int main(int argc, char ** argv) monstartup((u_long)&_start, (u_long)&etext); #endif /* DEBUG_FORKGPROF */ - addrstring = getaddrstring(&remoteaddr); + addrstring = getaddrstring(&remoteaddr, 1); dropbear_log(LOG_INFO, "Child connection from %s", addrstring); m_free(addrstring); @@ -231,7 +234,7 @@ int main(int argc, char ** argv) } /* make sure we close sockets */ - for (i = 0; i < listensockcount; i++) { + for (i = 0; i < (unsigned int)listensockcount; i++) { if (m_close(listensocks[i]) == DROPBEAR_FAILURE) { dropbear_exit("Couldn't close socket"); } @@ -289,59 +292,30 @@ static void sigintterm_handler(int fish) { } /* Set up listening sockets for all the requested ports */ -static int listensockets(int *sock, int *maxfd) { +static int listensockets(int *sock, int sockcount, int *maxfd) { - int listensock; /* listening fd */ - struct sockaddr_in listen_addr; - struct linger linger; unsigned int i; - int val; + char portstring[6]; + char* errstring = NULL; + unsigned int sockpos = 0; + int nsock; for (i = 0; i < svr_opts.portcount; i++) { - /* iterate through all the sockets to listen on */ - listensock = socket(PF_INET, SOCK_STREAM, 0); - if (listensock < 0) { - dropbear_exit("Failed to create socket"); - } - - val = 1; - /* set to reuse, quick timeout */ - setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, - (void*) &val, sizeof(val)); - linger.l_onoff = 1; - linger.l_linger = 5; - setsockopt(listensock, SOL_SOCKET, SO_LINGER, - (void*)&linger, sizeof(linger)); - - /* disable nagle */ - setsockopt(listensock, IPPROTO_TCP, TCP_NODELAY, - (void*)&val, sizeof(val)); - - memset((void*)&listen_addr, 0x0, sizeof(listen_addr)); - listen_addr.sin_family = AF_INET; - listen_addr.sin_port = htons(svr_opts.ports[i]); - listen_addr.sin_addr.s_addr = htonl(INADDR_ANY); - memset(&(listen_addr.sin_zero), '\0', 8); - - if (bind(listensock, (struct sockaddr *)&listen_addr, - sizeof(listen_addr)) < 0) { - dropbear_exit("Bind failed port %d", svr_opts.ports[i]); - } + snprintf(portstring, sizeof(portstring), "%d", svr_opts.ports[i]); + nsock = dropbear_listen(NULL, portstring, &sock[sockpos], + sockcount - sockpos, + &errstring, maxfd); - /* listen */ - if (listen(listensock, 20) < 0) { /* TODO set listen count */ - dropbear_exit("Listen failed"); + if (nsock < 0) { + dropbear_log(LOG_WARNING, "Failed listening on port %s: %s", + portstring, errstring); + m_free(errstring); + continue; } - /* nonblock */ - if (fcntl(listensock, F_SETFL, O_NONBLOCK) < 0) { - dropbear_exit("Failed to set non-blocking"); - } + sockpos += nsock; - sock[i] = listensock; - *maxfd = MAX(listensock, *maxfd); } - - return svr_opts.portcount; + return sockpos; } diff --git a/svr-session.c b/svr-session.c index e63ba32..d46adf4 100644 --- a/svr-session.c +++ b/svr-session.c @@ -35,10 +35,10 @@ #include "channel.h" #include "chansession.h" #include "atomicio.h" -#include "tcpfwd-direct.h" +#include "tcp-accept.h" +#include "tcp-connect.h" #include "service.h" #include "auth.h" -#include "tcpfwd-remote.h" #include "runopts.h" static void svr_remoteclosed(); @@ -65,7 +65,7 @@ static const packettype svr_packettypes[] = { static const struct ChanType *svr_chantypes[] = { &svrchansess, - &chan_tcpdirect, + &svr_chan_tcpdirect, NULL /* Null termination is mandatory. */ }; diff --git a/svr-tcpfwd.c b/svr-tcpfwd.c new file mode 100644 index 0000000..499ee46 --- /dev/null +++ b/svr-tcpfwd.c @@ -0,0 +1,196 @@ +#include "includes.h" +#include "ssh.h" +#include "tcp-accept.h" +#include "tcp-connect.h" +#include "dbutil.h" +#include "session.h" +#include "buffer.h" +#include "packet.h" +#include "listener.h" +#include "runopts.h" + +#ifndef DISABLE_SVR_REMOTETCPFWD + +static void send_msg_request_success(); +static void send_msg_request_failure(); +static int svr_cancelremotetcp(); +static int svr_remotetcpreq(); + + +const struct ChanType svr_chan_tcpdirect = { + 1, /* sepfds */ + "direct-tcpip", + newtcpdirect, /* init */ + NULL, /* checkclose */ + NULL, /* reqhandler */ + NULL /* closehandler */ +}; + +static const struct ChanType svr_chan_tcpremote = { + 1, /* sepfds */ + "forwarded-tcpip", + NULL, + NULL, + NULL, + NULL +}; + +/* At the moment this is completely used for tcp code (with the name reflecting + * that). If new request types are added, this should be replaced with code + * similar to the request-switching in chansession.c */ +void recv_msg_global_request_remotetcp() { + + unsigned char* reqname = NULL; + unsigned int namelen; + unsigned int wantreply = 0; + int ret = DROPBEAR_FAILURE; + + TRACE(("enter recv_msg_global_request_remotetcp")); + + if (opts.noremotetcp) { + TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled")); + goto out; + } + + reqname = buf_getstring(ses.payload, &namelen); + wantreply = buf_getbyte(ses.payload); + + if (namelen > MAXNAMLEN) { + TRACE(("name len is wrong: %d", namelen)); + goto out; + } + + if (strcmp("tcpip-forward", reqname) == 0) { + ret = svr_remotetcpreq(); + } else if (strcmp("cancel-tcpip-forward", reqname) == 0) { + ret = svr_cancelremotetcp(); + } else { + TRACE(("reqname isn't tcpip-forward: '%s'", reqname)); + } + +out: + if (wantreply) { + if (ret == DROPBEAR_SUCCESS) { + send_msg_request_success(); + } else { + send_msg_request_failure(); + } + } + + m_free(reqname); + + TRACE(("leave recv_msg_global_request")); +} + + +static void send_msg_request_success() { + + CHECKCLEARTOWRITE(); + buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS); + encrypt_packet(); + +} + +static void send_msg_request_failure() { + + CHECKCLEARTOWRITE(); + buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE); + encrypt_packet(); + +} + +static int matchtcp(void* typedata1, void* typedata2) { + + const struct TCPListener *info1 = (struct TCPListener*)typedata1; + const struct TCPListener *info2 = (struct TCPListener*)typedata2; + + return (info1->port == info2->port) + && (info1->chantype == info2->chantype) + && (strcmp(info1->addr, info2->addr) == 0); +} + +static int svr_cancelremotetcp() { + + int ret = DROPBEAR_FAILURE; + unsigned char * bindaddr = NULL; + unsigned int addrlen; + unsigned int port; + struct Listener * listener = NULL; + struct TCPListener tcpinfo; + + TRACE(("enter cancelremotetcp")); + + bindaddr = buf_getstring(ses.payload, &addrlen); + if (addrlen > MAX_IP_LEN) { + TRACE(("addr len too long: %d", addrlen)); + goto out; + } + + port = buf_getint(ses.payload); + + tcpinfo.addr = bindaddr; + tcpinfo.port = port; + listener = get_listener(CHANNEL_ID_TCPFORWARDED, &tcpinfo, matchtcp); + if (listener) { + remove_listener( listener ); + ret = DROPBEAR_SUCCESS; + } + +out: + m_free(bindaddr); + TRACE(("leave cancelremotetcp")); + return ret; +} + +static int svr_remotetcpreq() { + + int ret = DROPBEAR_FAILURE; + unsigned char * bindaddr = NULL; + unsigned int addrlen; + struct TCPListener *tcpinfo = NULL; + unsigned int port; + + TRACE(("enter remotetcpreq")); + + bindaddr = buf_getstring(ses.payload, &addrlen); + if (addrlen > MAX_IP_LEN) { + TRACE(("addr len too long: %d", addrlen)); + goto out; + } + + port = buf_getint(ses.payload); + + if (port == 0) { + dropbear_log(LOG_INFO, "Server chosen tcpfwd ports are unsupported"); + goto out; + } + + if (port < 1 || port > 65535) { + TRACE(("invalid port: %d", port)); + goto out; + } + + if (!ses.allowprivport && port < IPPORT_RESERVED) { + TRACE(("can't assign port < 1024 for non-root")); + goto out; + } + + tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener)); + tcpinfo->addr = bindaddr; + tcpinfo->port = port; + tcpinfo->localport = -1; + tcpinfo->chantype = &svr_chan_tcpremote; + + ret = listen_tcpfwd(tcpinfo); + +out: + if (ret == DROPBEAR_FAILURE) { + /* we only free it if a listener wasn't created, since the listener + * has to remember it if it's to be cancelled */ + m_free(tcpinfo->addr); + m_free(tcpinfo); + } + TRACE(("leave remotetcpreq")); + return ret; +} +#endif diff --git a/svr-x11fwd.c b/svr-x11fwd.c index aa0ba2d..0f4f71e 100644 --- a/svr-x11fwd.c +++ b/svr-x11fwd.c @@ -37,7 +37,7 @@ #define X11BASEPORT 6000 #define X11BINDBASE 6010 -static void x11accept(struct Listener* listener); +static void x11accept(struct Listener* listener, int sock); static int bindport(int fd); static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr); @@ -82,7 +82,7 @@ int x11req(struct ChanSess * chansess) { /* listener code will handle the socket now. * No cleanup handler needed, since listener_remove only happens * from our cleanup anyway */ - chansess->x11listener = new_listener( fd, 0, chansess, x11accept, NULL); + chansess->x11listener = new_listener( &fd, 1, 0, chansess, x11accept, NULL); if (chansess->x11listener == NULL) { goto fail; } @@ -100,7 +100,7 @@ fail: /* accepts a new X11 socket */ /* returns DROPBEAR_FAILURE or DROPBEAR_SUCCESS */ -static void x11accept(struct Listener* listener) { +static void x11accept(struct Listener* listener, int sock) { int fd; struct sockaddr_in addr; @@ -110,7 +110,7 @@ static void x11accept(struct Listener* listener) { len = sizeof(addr); - fd = accept(listener->sock, (struct sockaddr*)&addr, &len); + fd = accept(sock, (struct sockaddr*)&addr, &len); if (fd < 0) { return; } diff --git a/tcp-accept.c b/tcp-accept.c new file mode 100644 index 0000000..1fb80dd --- /dev/null +++ b/tcp-accept.c @@ -0,0 +1,88 @@ +#include "includes.h" +#include "ssh.h" +#include "tcp-accept.h" +#include "dbutil.h" +#include "session.h" +#include "buffer.h" +#include "packet.h" +#include "listener.h" +#include "runopts.h" + +#ifndef DISABLE_TCP_ACCEPT + +static void accept_tcp(struct Listener *listener, int sock) { + + int fd; + struct sockaddr_storage addr; + int len; + char ipstring[NI_MAXHOST], portstring[NI_MAXSERV]; + struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata); + + len = sizeof(addr); + + fd = accept(sock, (struct sockaddr*)&addr, &len); + if (fd < 0) { + return; + } + + if (getnameinfo((struct sockaddr*)&addr, len, ipstring, sizeof(ipstring), + portstring, sizeof(portstring), + NI_NUMERICHOST | NI_NUMERICSERV) != 0) { + return; + } + + if (send_msg_channel_open_init(fd, tcpinfo->chantype) == DROPBEAR_SUCCESS) { + + buf_putstring(ses.writepayload, tcpinfo->addr, strlen(tcpinfo->addr)); + buf_putint(ses.writepayload, tcpinfo->port); + buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); + buf_putint(ses.writepayload, atol(portstring)); + encrypt_packet(); + + } else { + /* XXX debug? */ + close(fd); + } +} + +static void cleanup_tcp(struct Listener *listener) { + + struct TCPListener *tcpinfo = (struct TCPListener*)(listener->typedata); + + m_free(tcpinfo->addr); + m_free(tcpinfo); +} + + +int listen_tcpfwd(struct TCPListener* tcpinfo) { + + char portstring[6]; /* "65535\0" */ + int socks[DROPBEAR_MAX_SOCKS]; + struct Listener *listener = NULL; + int nsocks; + + TRACE(("enter listen_tcpfwd")); + + /* first we try to bind, so don't need to do so much cleanup on failure */ + snprintf(portstring, sizeof(portstring), "%d", tcpinfo->port); + nsocks = dropbear_listen(tcpinfo->addr, portstring, socks, + DROPBEAR_MAX_SOCKS, NULL, &ses.maxfd); + if (nsocks < 0) { + TRACE(("leave listen_tcpfwd: dropbear_listen failed")); + return DROPBEAR_FAILURE; + } + + listener = new_listener(socks, nsocks, CHANNEL_ID_TCPFORWARDED, tcpinfo, + accept_tcp, cleanup_tcp); + + if (listener == NULL) { + m_free(tcpinfo); + TRACE(("leave listen_tcpfwd: listener failed")); + return DROPBEAR_FAILURE; + } + + TRACE(("leave listen_tcpfwd: success")); + return DROPBEAR_SUCCESS; +} + +#endif /* DISABLE_REMOTETCPFWD */ diff --git a/tcp-accept.h b/tcp-accept.h new file mode 100644 index 0000000..96ddb76 --- /dev/null +++ b/tcp-accept.h @@ -0,0 +1,19 @@ +#ifndef _REMOTETCPFWD_H +#define _REMOTETCPFWD_H + +struct TCPListener { + + /* Local ones */ + unsigned char *localaddr; /* Can be NULL */ + unsigned int localport; + /* Remote ones: */ + unsigned char *remoteaddr; + unsigned int remoteport; + const struct ChanType *chantype; + +}; + +void recv_msg_global_request_remotetcp(); +int listen_tcpfwd(struct TCPListener* tcpinfo); + +#endif /* _REMOTETCPFWD_H */ diff --git a/tcp-connect.c b/tcp-connect.c new file mode 100644 index 0000000..00dbd8a --- /dev/null +++ b/tcp-connect.c @@ -0,0 +1,75 @@ +#include "includes.h" +#include "session.h" +#include "dbutil.h" +#include "channel.h" +#include "tcp-connect.h" +#include "runopts.h" + +#ifndef DISABLE_TCP_CONNECT + +/* Called upon creating a new direct tcp channel (ie we connect out to an + * address */ +int newtcpdirect(struct Channel * channel) { + + unsigned char* desthost = NULL; + unsigned int destport; + unsigned char* orighost = NULL; + unsigned int origport; + char portstring[6]; + int sock; + int len; + int ret = DROPBEAR_FAILURE; + + if (opts.nolocaltcp) { + TRACE(("leave newtcpdirect: local tcp forwarding disabled")); + goto out; + } + + desthost = buf_getstring(ses.payload, &len); + if (len > MAX_HOST_LEN) { + TRACE(("leave newtcpdirect: desthost too long")); + goto out; + } + + destport = buf_getint(ses.payload); + + orighost = buf_getstring(ses.payload, &len); + if (len > MAX_HOST_LEN) { + TRACE(("leave newtcpdirect: orighost too long")); + goto out; + } + + origport = buf_getint(ses.payload); + + /* best be sure */ + if (origport > 65535 || destport > 65535) { + TRACE(("leave newtcpdirect: port > 65535")); + goto out; + } + + snprintf(portstring, sizeof(portstring), "%d", destport); + sock = connect_remote(desthost, portstring, 1, NULL); + if (sock < 0) { + TRACE(("leave newtcpdirect: sock failed")); + goto out; + } + + ses.maxfd = MAX(ses.maxfd, sock); + + /* Note that infd is actually the "outgoing" direction on the + * tcp connection, vice versa for outfd. + * We don't set outfd, that will get set after the connection's + * progress succeeds */ + channel->infd = sock; + channel->initconn = 1; + + ret = DROPBEAR_SUCCESS; + +out: + m_free(desthost); + m_free(orighost); + TRACE(("leave newtcpdirect: ret %d", ret)); + return ret; +} + +#endif /* DISABLE_TCPFWD_DIRECT */ diff --git a/tcp-connect.h b/tcp-connect.h new file mode 100644 index 0000000..40ce22b --- /dev/null +++ b/tcp-connect.h @@ -0,0 +1,35 @@ +/* + * Dropbear - a SSH2 server + * + * Copyright (c) 2002,2003 Matt Johnston + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ +#ifndef _TCPFWD_DIRECT_H_ +#define _TCPFWD_DIRECT_H_ +#ifndef DISABLE_TCFWD_DIRECT + +#include "includes.h" +#include "channel.h" + +extern const struct ChanType svr_chan_tcpdirect; +int newtcpdirect(struct Channel * channel); + +#endif +#endif diff --git a/tcpfwd-direct.c b/tcpfwd-direct.c deleted file mode 100644 index b611283..0000000 --- a/tcpfwd-direct.c +++ /dev/null @@ -1,159 +0,0 @@ -#include "includes.h" -#include "session.h" -#include "dbutil.h" -#include "channel.h" -#include "tcpfwd-direct.h" -#include "runopts.h" - -#ifndef DISABLE_TCPFWD_DIRECT -static int newtcpdirect(struct Channel * channel); -static int newtcp(const char * host, int port); - -const struct ChanType chan_tcpdirect = { - 1, /* sepfds */ - "direct-tcpip", - newtcpdirect, /* init */ - NULL, /* checkclose */ - NULL, /* reqhandler */ - NULL /* closehandler */ -}; - - -/* Called upon creating a new direct tcp channel (ie we connect out to an - * address */ -static int newtcpdirect(struct Channel * channel) { - - unsigned char* desthost = NULL; - unsigned int destport; - unsigned char* orighost = NULL; - unsigned int origport; - char portstring[6]; - int sock; - int len; - int ret = DROPBEAR_FAILURE; - - if (opts.nolocaltcp) { - TRACE(("leave newtcpdirect: local tcp forwarding disabled")); - goto out; - } - - desthost = buf_getstring(ses.payload, &len); - if (len > MAX_HOST_LEN) { - TRACE(("leave newtcpdirect: desthost too long")); - goto out; - } - - destport = buf_getint(ses.payload); - - orighost = buf_getstring(ses.payload, &len); - if (len > MAX_HOST_LEN) { - TRACE(("leave newtcpdirect: orighost too long")); - goto out; - } - - origport = buf_getint(ses.payload); - - /* best be sure */ - if (origport > 65535 || destport > 65535) { - TRACE(("leave newtcpdirect: port > 65535")); - goto out; - } - - snprintf(portstring, sizeof(portstring), "%d", destport); - sock = connect_remote(desthost, portstring, 1, NULL); - if (sock < 0) { - TRACE(("leave newtcpdirect: sock failed")); - goto out; - } - - ses.maxfd = MAX(ses.maxfd, sock); - - /* Note that infd is actually the "outgoing" direction on the - * tcp connection, vice versa for outfd. - * We don't set outfd, that will get set after the connection's - * progress succeeds */ - channel->infd = sock; - channel->initconn = 1; - - ret = DROPBEAR_SUCCESS; - -out: - m_free(desthost); - m_free(orighost); - TRACE(("leave newtcpdirect: ret %d", ret)); - return ret; -} - -/* Initiate a new TCP connection - this is non-blocking, so the socket - * returned will need to be checked for success when it is first written. - * Similarities with OpenSSH's connect_to() are not coincidental. - * Returns -1 on failure */ -#if 0 -static int newtcp(const char * host, int port) { - - int sock = -1; - char portstring[6]; - struct addrinfo *res = NULL, *ai; - int val; - - struct addrinfo hints; - - TRACE(("enter newtcp")); - - memset(&hints, 0, sizeof(hints)); - /* TCP, either ip4 or ip6 */ - hints.ai_socktype = SOCK_STREAM; - hints.ai_family = PF_UNSPEC; - - snprintf(portstring, sizeof(portstring), "%d", port); - if (getaddrinfo(host, portstring, &hints, &res) != 0) { - if (res) { - freeaddrinfo(res); - } - TRACE(("leave newtcp: failed getaddrinfo")); - return -1; - } - - /* Use the first socket that works */ - for (ai = res; ai != NULL; ai = ai->ai_next) { - - if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) { - continue; - } - - sock = socket(ai->ai_family, SOCK_STREAM, 0); - if (sock < 0) { - TRACE(("TCP socket() failed")); - continue; - } - - if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { - close(sock); - TRACE(("TCP non-blocking failed")); - continue; - } - - /* non-blocking, so it might return without success (EINPROGRESS) */ - if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) { - if (errno == EINPROGRESS) { - TRACE(("connect in progress")); - } else { - close(sock); - TRACE(("TCP connect failed")); - continue; - } - } - break; - } - - freeaddrinfo(res); - - if (ai == NULL) { - return -1; - } - - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&val, sizeof(val)); - return sock; -} -#endif -#endif /* DISABLE_TCPFWD_DIRECT */ diff --git a/tcpfwd-direct.h b/tcpfwd-direct.h deleted file mode 100644 index 20cd278..0000000 --- a/tcpfwd-direct.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Dropbear - a SSH2 server - * - * Copyright (c) 2002,2003 Matt Johnston - * All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. */ -#ifndef _TCPFWD_DIRECT_H_ -#define _TCPFWD_DIRECT_H_ -#ifndef DISABLE_TCFWD_DIRECT - -#include "includes.h" -#include "channel.h" - -extern const struct ChanType chan_tcpdirect; - -#endif -#endif diff --git a/tcpfwd-remote.c b/tcpfwd-remote.c deleted file mode 100644 index d0a67a1..0000000 --- a/tcpfwd-remote.c +++ /dev/null @@ -1,317 +0,0 @@ -#include "includes.h" -#include "ssh.h" -#include "tcpfwd-remote.h" -#include "dbutil.h" -#include "session.h" -#include "buffer.h" -#include "packet.h" -#include "listener.h" -#include "runopts.h" - -#ifndef DISABLE_REMOTETCPFWD - -struct RemoteTCP { - - unsigned char* addr; - unsigned int port; - -}; - -static void send_msg_request_success(); -static void send_msg_request_failure(); -static int cancelremotetcp(); -static int remotetcpreq(); -static int listen_tcpfwd(unsigned char* bindaddr, unsigned int port); -static void acceptremote(struct Listener *listener); - -/* At the moment this is completely used for tcp code (with the name reflecting - * that). If new request types are added, this should be replaced with code - * similar to the request-switching in chansession.c */ -void recv_msg_global_request_remotetcp() { - - unsigned char* reqname = NULL; - unsigned int namelen; - unsigned int wantreply = 0; - int ret = DROPBEAR_FAILURE; - - TRACE(("enter recv_msg_global_request_remotetcp")); - - if (opts.noremotetcp) { - TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled")); - goto out; - } - - reqname = buf_getstring(ses.payload, &namelen); - wantreply = buf_getbyte(ses.payload); - - if (namelen > MAXNAMLEN) { - TRACE(("name len is wrong: %d", namelen)); - goto out; - } - - if (strcmp("tcpip-forward", reqname) == 0) { - ret = remotetcpreq(); - } else if (strcmp("cancel-tcpip-forward", reqname) == 0) { - ret = cancelremotetcp(); - } else { - TRACE(("reqname isn't tcpip-forward: '%s'", reqname)); - } - -out: - if (wantreply) { - if (ret == DROPBEAR_SUCCESS) { - send_msg_request_success(); - } else { - send_msg_request_failure(); - } - } - - m_free(reqname); - - TRACE(("leave recv_msg_global_request")); -} - -static const struct ChanType chan_tcpremote = { - 1, /* sepfds */ - "forwarded-tcpip", - NULL, - NULL, - NULL, - NULL -}; - - -static void acceptremote(struct Listener *listener) { - - int fd; - struct sockaddr addr; - int len; - char ipstring[NI_MAXHOST], portstring[NI_MAXSERV]; - struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); - - len = sizeof(addr); - - fd = accept(listener->sock, &addr, &len); - if (fd < 0) { - return; - } - - if (getnameinfo(&addr, len, ipstring, sizeof(ipstring), portstring, - sizeof(portstring), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { - return; - } - - if (send_msg_channel_open_init(fd, &chan_tcpremote) == DROPBEAR_SUCCESS) { - - buf_putstring(ses.writepayload, tcpinfo->addr, - strlen(tcpinfo->addr)); - buf_putint(ses.writepayload, tcpinfo->port); - buf_putstring(ses.writepayload, ipstring, strlen(ipstring)); - buf_putint(ses.writepayload, atol(portstring)); - encrypt_packet(); - - } else { - /* XXX debug? */ - close(fd); - } -} - -static void cleanupremote(struct Listener *listener) { - - struct RemoteTCP *tcpinfo = (struct RemoteTCP*)(listener->typedata); - - m_free(tcpinfo->addr); - m_free(tcpinfo); -} - -static void send_msg_request_success() { - - CHECKCLEARTOWRITE(); - buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_SUCCESS); - encrypt_packet(); - -} - -static void send_msg_request_failure() { - - CHECKCLEARTOWRITE(); - buf_putbyte(ses.writepayload, SSH_MSG_REQUEST_FAILURE); - encrypt_packet(); - -} - -static int matchtcp(void* typedata1, void* typedata2) { - - const struct RemoteTCP *info1 = (struct RemoteTCP*)typedata1; - const struct RemoteTCP *info2 = (struct RemoteTCP*)typedata2; - - return info1->port == info2->port - && (strcmp(info1->addr, info2->addr) == 0); -} - -static int cancelremotetcp() { - - int ret = DROPBEAR_FAILURE; - unsigned char * bindaddr = NULL; - unsigned int addrlen; - unsigned int port; - struct Listener * listener = NULL; - struct RemoteTCP tcpinfo; - - TRACE(("enter cancelremotetcp")); - - bindaddr = buf_getstring(ses.payload, &addrlen); - if (addrlen > MAX_IP_LEN) { - TRACE(("addr len too long: %d", addrlen)); - goto out; - } - - port = buf_getint(ses.payload); - - tcpinfo.addr = bindaddr; - tcpinfo.port = port; - listener = get_listener(CHANNEL_ID_TCPFORWARDED, &tcpinfo, matchtcp); - if (listener) { - remove_listener( listener ); - ret = DROPBEAR_SUCCESS; - } - -out: - m_free(bindaddr); - TRACE(("leave cancelremotetcp")); - return ret; -} - -static int remotetcpreq() { - - int ret = DROPBEAR_FAILURE; - unsigned char * bindaddr = NULL; - unsigned int addrlen; - unsigned int port; - - TRACE(("enter remotetcpreq")); - - bindaddr = buf_getstring(ses.payload, &addrlen); - if (addrlen > MAX_IP_LEN) { - TRACE(("addr len too long: %d", addrlen)); - goto out; - } - - port = buf_getint(ses.payload); - - if (port == 0) { - dropbear_log(LOG_INFO, "Server chosen tcpfwd ports are unsupported"); - goto out; - } - - if (port < 1 || port > 65535) { - TRACE(("invalid port: %d", port)); - goto out; - } - - if (!ses.allowprivport && port < IPPORT_RESERVED) { - TRACE(("can't assign port < 1024 for non-root")); - goto out; - } - - ret = listen_tcpfwd(bindaddr, port); - -out: - if (ret == DROPBEAR_FAILURE) { - /* we only free it if a listener wasn't created, since the listener - * has to remember it if it's to be cancelled */ - m_free(bindaddr); - } - TRACE(("leave remotetcpreq")); - return ret; -} - -static int listen_tcpfwd(unsigned char* bindaddr, unsigned int port) { - - struct RemoteTCP * tcpinfo = NULL; - char portstring[6]; /* "65535\0" */ - struct addrinfo *res = NULL, *ai = NULL; - struct addrinfo hints; - int sock = -1; - struct Listener *listener = NULL; - - TRACE(("enter listen_tcpfwd")); - - /* first we try to bind, so don't need to do so much cleanup on failure */ - snprintf(portstring, sizeof(portstring), "%d", port); - memset(&hints, 0x0, sizeof(hints)); - hints.ai_socktype = SOCK_STREAM; - hints.ai_family = PF_INET; - hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; - - if (getaddrinfo(bindaddr, portstring, &hints, &res) < 0) { - TRACE(("leave listen_tcpfwd: getaddrinfo failed: %s", - strerror(errno))); - goto done; - } - - /* find the first one which works */ - for (ai = res; ai != NULL; ai = ai->ai_next) { - if (ai->ai_family != PF_INET && ai->ai_family != PF_INET6) { - continue; - } - - sock = socket(ai->ai_family, SOCK_STREAM, 0); - if (sock < 0) { - TRACE(("socket failed: %s", strerror(errno))); - goto fail; - } - - if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) { - TRACE(("bind failed: %s", strerror(errno))); - goto fail; - } - - if (listen(sock, 20) < 0) { - TRACE(("listen failed: %s", strerror(errno))); - goto fail; - } - - if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) { - TRACE(("fcntl nonblocking failed: %s", strerror(errno))); - goto fail; - } - - /* success */ - break; - -fail: - close(sock); - } - - - if (ai == NULL) { - TRACE(("no successful sockets")); - goto done; - } - - tcpinfo = (struct RemoteTCP*)m_malloc(sizeof(struct RemoteTCP)); - tcpinfo->addr = bindaddr; - tcpinfo->port = port; - - listener = new_listener(sock, CHANNEL_ID_TCPFORWARDED, tcpinfo, - acceptremote, cleanupremote); - - if (listener == NULL) { - m_free(tcpinfo); - } - -done: - if (res) { - freeaddrinfo(res); - } - - TRACE(("leave listen_tcpfwd")); - if (listener == NULL) { - return DROPBEAR_FAILURE; - } else { - return DROPBEAR_SUCCESS; - } -} - -#endif /* DISABLE_REMOTETCPFWD */ diff --git a/tcpfwd-remote.h b/tcpfwd-remote.h deleted file mode 100644 index 64dbed3..0000000 --- a/tcpfwd-remote.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _REMOTETCPFWD_H -#define _REMOTETCPFWD_H - -void recv_msg_global_request_remotetcp(); - -#endif /* _REMOTETCPFWD_H */ -- cgit v1.2.3 From 8e1ec24f55be1a9af2595a58bb4b805bebd7fa9d Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 12 Aug 2004 16:41:58 +0000 Subject: Merging in the changes from 0.41-0.43 main Dropbear tree --HG-- extra : convert_revision : 4c3428781bc8faf0fd7cadd7099fbd7f4ea386e7 --- buffer.c | 9 +- channel.h | 3 + cli-tcpfwd.c | 9 +- common-channel.c | 27 +++++- configure.in | 2 +- dbutil.c | 6 +- debian/README.runit | 46 +++++++++ debian/changelog | 52 +++++++++++ debian/control | 24 +++-- debian/copyright.in | 6 +- debian/dropbear.README.Debian | 41 +++++++++ debian/dropbear.conffiles | 3 + debian/dropbear.docs | 3 + debian/dropbear.init | 60 ++++-------- debian/dropbear.postinst | 68 ++++++++++++++ debian/dropbear.postrm | 12 +++ debian/dropbear.prerm | 11 +++ debian/implicit | 79 ++++++++++++++++ debian/rules | 210 +++++++++++++++++------------------------- debian/service/log | 2 + debian/service/run | 3 + dropbearkey.c | 1 - dss.c | 4 +- gendss.c | 1 - includes.h | 6 +- options.h | 7 +- packet.c | 4 +- random.c | 5 +- rsa.c | 16 ++-- signkey.c | 4 +- svr-agentfwd.c | 4 +- svr-auth.c | 4 +- svr-authpasswd.c | 8 +- svr-authpubkey.c | 2 +- svr-chansession.c | 35 +++++-- svr-tcpfwd.c | 9 +- svr-x11fwd.c | 4 +- 37 files changed, 554 insertions(+), 236 deletions(-) create mode 100644 debian/README.runit create mode 100644 debian/dropbear.README.Debian create mode 100644 debian/dropbear.conffiles create mode 100644 debian/dropbear.docs create mode 100644 debian/dropbear.postinst create mode 100644 debian/dropbear.postrm create mode 100644 debian/dropbear.prerm create mode 100644 debian/implicit create mode 100644 debian/service/log create mode 100644 debian/service/run (limited to 'svr-chansession.c') diff --git a/buffer.c b/buffer.c index 7181fca..df608d9 100644 --- a/buffer.c +++ b/buffer.c @@ -34,8 +34,8 @@ #define BUF_MAX_INCR 1000000000 #define BUF_MAX_SIZE 1000000000 -/* avoid excessively large numbers, > 5000 bit */ -#define BUF_MAX_MPINT (5000 / 8) +/* avoid excessively large numbers, > ~8192 bits */ +#define BUF_MAX_MPINT (8240 / 8) /* Create (malloc) a new buffer of size */ buffer* buf_new(unsigned int size) { @@ -76,7 +76,8 @@ void buf_burn(buffer* buf) { } -/* resize a buffer, pos and len will be repositioned if required */ +/* resize a buffer, pos and len will be repositioned if required when + * downsizing */ void buf_resize(buffer *buf, unsigned int newsize) { if (newsize > BUF_MAX_SIZE) { @@ -151,6 +152,8 @@ void buf_incrpos(buffer* buf, int incr) { /* Get a byte from the buffer and increment the pos */ unsigned char buf_getbyte(buffer* buf) { + /* This check is really just ==, but the >= allows us to check for the + * assert()able case of pos > len, which should _never_ happen. */ if (buf->pos >= buf->len) { dropbear_exit("bad buf_getbyte"); } diff --git a/channel.h b/channel.h index e1bdae2..2289de1 100644 --- a/channel.h +++ b/channel.h @@ -41,6 +41,9 @@ #define SSH_OPEN_UNKNOWN_CHANNEL_TYPE 3 #define SSH_OPEN_RESOURCE_SHORTAGE 4 +/* Not a real type */ +#define SSH_OPEN_IN_PROGRESS 99 + #define MAX_CHANNELS 60 /* simple mem restriction, includes each tcp/x11 connection, so can't be _too_ small */ diff --git a/cli-tcpfwd.c b/cli-tcpfwd.c index 52268b9..8d8e605 100644 --- a/cli-tcpfwd.c +++ b/cli-tcpfwd.c @@ -120,7 +120,7 @@ static int newtcpforwarded(struct Channel * channel) { struct TCPFwdList * iter = NULL; char portstring[NI_MAXSERV]; int sock; - int ret = DROPBEAR_FAILURE; + int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; /* We don't care what address they connected to */ buf_eatstring(ses.payload); @@ -148,6 +148,7 @@ static int newtcpforwarded(struct Channel * channel) { sock = connect_remote(iter->connectaddr, portstring, 1, NULL); if (sock < 0) { TRACE(("leave newtcpdirect: sock failed")); + err = SSH_OPEN_CONNECT_FAILED; goto out; } @@ -160,9 +161,9 @@ static int newtcpforwarded(struct Channel * channel) { channel->infd = sock; channel->initconn = 1; - ret = DROPBEAR_SUCCESS; + err = SSH_OPEN_IN_PROGRESS; out: - TRACE(("leave newtcpdirect: ret %d", ret)); - return ret; + TRACE(("leave newtcpdirect: err %d", err)); + return err; } diff --git a/common-channel.c b/common-channel.c index 5079031..64ea466 100644 --- a/common-channel.c +++ b/common-channel.c @@ -172,6 +172,7 @@ void channelio(fd_set *readfd, fd_set *writefd) { struct Channel *channel; unsigned int i; + int ret; /* iterate through all the possible channels */ for (i = 0; i < ses.chansize; i++) { @@ -196,8 +197,15 @@ void channelio(fd_set *readfd, fd_set *writefd) { * see if it has errors */ if (channel->infd >= 0 && channel->infd != channel->outfd && FD_ISSET(channel->infd, readfd)) { - int ret; - ret = write(channel->infd, NULL, 0); + if (channel->initconn) { + /* Handling for "in progress" connection - this is needed + * to avoid spinning 100% CPU when we connect to a server + * which doesn't send anything (tcpfwding) */ + checkinitdone(channel); + continue; /* Important not to use the channel after + checkinitdone(), as it may be NULL */ + } + ret = write(channel->infd, NULL, 0); /* Fake write */ if (ret < 0 && errno != EINTR && errno != EAGAIN) { closeinfd(channel); } @@ -209,9 +217,8 @@ void channelio(fd_set *readfd, fd_set *writefd) { checkinitdone(channel); continue; /* Important not to use the channel after checkinitdone(), as it may be NULL */ - } else { - writechannel(channel); } + writechannel(channel); } /* now handle any of the channel-closing type stuff */ @@ -285,10 +292,14 @@ static void checkinitdone(struct Channel *channel) { if (getsockopt(channel->infd, SOL_SOCKET, SO_ERROR, &val, &vallen) || val != 0) { + send_msg_channel_open_failure(channel->remotechan, + SSH_OPEN_CONNECT_FAILED, "", ""); close(channel->infd); deletechannel(channel); TRACE(("leave checkinitdone: fail")); } else { + send_msg_channel_open_confirmation(channel, channel->recvwindow, + channel->recvmaxpacket); channel->outfd = channel->infd; channel->initconn = 0; TRACE(("leave checkinitdone: success")); @@ -489,6 +500,7 @@ static void removechannel(struct Channel * channel) { TRACE(("channel index is %d", channel->index)); buf_free(channel->writebuf); + channel->writebuf = NULL; /* close the FDs in case they haven't been done * yet (ie they were shutdown etc */ @@ -497,6 +509,7 @@ static void removechannel(struct Channel * channel) { if (channel->errfd >= 0) { close(channel->errfd); } + channel->typedata = NULL; deletechannel(channel); @@ -587,6 +600,7 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, TRACE(("leave send_msg_channel_data: read err %d", channel->index)); } buf_free(buf); + buf = NULL; return; } buf_incrlen(buf, len); @@ -601,6 +615,7 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, buf_putstring(ses.writepayload, buf_getptr(buf, len), len); buf_free(buf); + buf = NULL; channel->transwindow -= len; @@ -764,6 +779,10 @@ void recv_msg_channel_open() { if (channel->type->inithandler) { ret = channel->type->inithandler(channel); if (ret > 0) { + if (ret == SSH_OPEN_IN_PROGRESS) { + /* We'll send the confirmation later */ + goto cleanup; + } errtype = ret; deletechannel(channel); TRACE(("inithandler returned failure %d", ret)); diff --git a/configure.in b/configure.in index bffd0da..10988c2 100644 --- a/configure.in +++ b/configure.in @@ -169,7 +169,7 @@ AC_ARG_ENABLE(shadow, # Checks for header files. AC_HEADER_STDC AC_HEADER_SYS_WAIT -AC_CHECK_HEADERS([fcntl.h limits.h netinet/in.h netinet/tcp.h stdlib.h string.h sys/socket.h sys/time.h termios.h unistd.h crypt.h pty.h ioctl.h libutil.h libgen.h inttypes.h stropts.h utmp.h utmpx.h lastlog.h paths.h util.h netdb.h sys/dirent.h]) +AC_CHECK_HEADERS([fcntl.h limits.h netinet/in.h netinet/tcp.h stdlib.h string.h sys/socket.h sys/time.h termios.h unistd.h crypt.h pty.h ioctl.h libutil.h libgen.h inttypes.h stropts.h utmp.h utmpx.h lastlog.h paths.h util.h netdb.h]) # Checks for typedefs, structures, and compiler characteristics. AC_C_CONST diff --git a/dbutil.c b/dbutil.c index 5436cbb..30b5708 100644 --- a/dbutil.c +++ b/dbutil.c @@ -121,7 +121,7 @@ void dropbear_trace(const char* format, ...) { int dropbear_listen(const char* address, const char* port, int *socks, unsigned int sockcount, char **errstring, int *maxfd) { - struct addrinfo hints, *res, *res0; + struct addrinfo hints, *res = NULL, *res0 = NULL; int err; unsigned int nsock; struct linger linger; @@ -273,7 +273,7 @@ int connect_remote(const char* remotehost, const char* remoteport, } if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { - if (errno == EINPROGRESS) { + if (errno == EINPROGRESS && nonblocking) { TRACE(("Connect in progress")); break; } else { @@ -287,7 +287,7 @@ int connect_remote(const char* remotehost, const char* remoteport, break; /* Success */ } - if (sock < 0) { + if (sock < 0 && !(errno == EINPROGRESS && nonblocking)) { /* Failed */ if (errstring != NULL && *errstring == NULL) { int len; diff --git a/debian/README.runit b/debian/README.runit new file mode 100644 index 0000000..4ac2814 --- /dev/null +++ b/debian/README.runit @@ -0,0 +1,46 @@ +Using the dropbear SSH server with runit's services supervision +--------------------------------------------------------------- + +The dropbear SSH server is perfectly suited to be run under runit's +service supervision, and this package already has prepared an adequate +service directory. Follow these steps to enable the dropbear service +using the runit package. + +If not yet installed on your system, install the runit package, and make +sure its service supervision is enabled (it's by default) + + # apt-get install runit + +Make sure the dropbear service normally handled through the sysv init +script is stopped + + # /etc/init.d/dropbear stop + +Create the system user ``dropbearlog'' which will run the logger service, +and own the logs + + # adduser --system --home /var/log/dropbear --no-create-home dropbearlog + +Create the log directory and make the newly created system user the owner +of this directory + + # mkdir -p /var/log/dropbear && chown dropbearlog /var/log/dropbear + +Optionally adjust the configuration of the dropbear service by editing the +run script + + # vi /etc/dropbear/run + +Finally enable the service by linking dropbear's service directory to +/var/service/. The service will be started within five seconds, and +automatically at boot time. The sysv init script is disabled; see the +runsvctrl(8) program for information on how to control services handled by +runit. See the svlogd(8) program on how to configure the log service. + + # ln -s /etc/dropbear /var/service/ + +Optionally check the status of the service a few seconds later + + # runsvstat -l /var/service/dropbear + + -- Gerrit Pape , Sun, 16 May 2004 15:52:34 +0000 diff --git a/debian/changelog b/debian/changelog index cb7253f..d9da388 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,55 @@ +dropbear (0.43-1) unstable; urgency=high + + * New upstream release 0.43 + * SECURITY: Don't attempt to free uninitialised buffers in DSS verification + code + * Handle portforwarding to servers which don't send any initial data + (Closes: #258426) + + -- Matt Johnston Fri, 16 July 2004 17:44:54 +0800 + +dropbear (0.42-1) unstable; urgency=low + + * New upstream release 0.42 + + -- Matt Johnston Wed, 16 June 2004 12:44:54 +0800 + +dropbear (0.41-3) unstable; urgency=low + + * 1st upload to the Debian archive (closes: #216553). + * debian/diff/cvs-20040520.diff: new; stable cvs snapshot. + * debian/rules: new target patch: apply diffs in debian/diff/, reverse + apply in target clean; install man pages. + * debian/control: Priority: optional. + + -- Gerrit Pape Sun, 23 May 2004 08:32:37 +0000 + +dropbear (0.41-2) unstable; urgency=low + + * new maintainer. + * debian/control: no longer Build-Depends: debhelper; Build-Depends: + libz-dev; Standards-Version: 3.6.1.0; Suggests: runit; update + descriptions. + * debian/rules: stop using debhelper, use implicit rules; cleanup; + install dropbearconvert into /usr/lib/dropbear/. + * debian/impicit: new; implicit rules. + * debian/copyright.in: adapt. + * debian/dropbear.init: minor adaptions; test for dropbear service + directory. + * debian/README.runit: new; how to use dropbear with runit. + * debian/README.Debian, debian/docs: rename to debian/dropbear.*. + * debian/dropbear.docs: add debian/README.runit + * debian/conffiles: rename to debian/dropbear.conffiles; add init + script, and run scripts. + * debian/postinst: rename to debian/dropbear.postinst; adapt; use + invloke-rc.d dropbear start. + * debian/dropbear.prerm: new; invoke-rc.d dropbear stop. + * debian/postrm: rename to debian/dropbear.postrm; adapt; clean up + service directories. + * debian/compat, debian/dirs, dropbear.default: remove; obsolete. + + -- Gerrit Pape Sun, 16 May 2004 16:50:55 +0000 + dropbear (0.41-1) unstable; urgency=low * Updated to 0.41 release. diff --git a/debian/control b/debian/control index e528454..33c717c 100644 --- a/debian/control +++ b/debian/control @@ -1,14 +1,20 @@ Source: dropbear Section: net -Priority: standard -Maintainer: Grahame Bowland -Build-Depends: debhelper (>> 4.0.0), zlib1g-dev -Standards-Version: 3.5.8 +Priority: optional +Maintainer: Gerrit Pape +Build-Depends: libz-dev +Standards-Version: 3.6.1.0 Package: dropbear Architecture: any -Depends: ${shlibs:Depends} ${misc:Depends} -Suggests: ssh -Description: a minimal SSH2 server - A small secure shell version 2 server. - +Depends: ${shlibs:Depends} +Suggests: ssh, runit +Description: lightweight SSH2 server + dropbear is a SSH 2 server designed to be small enough to be used in small + memory environments, while still being functional and secure enough for + general use. + . + It implements most required features of the SSH 2 protocol, and other + features such as X11 and authentication agent forwarding. + . + See http://matt.ucc.asn.au/dropbear/dropbear.html diff --git a/debian/copyright.in b/debian/copyright.in index 015d9ab..79526d3 100644 --- a/debian/copyright.in +++ b/debian/copyright.in @@ -1,9 +1,11 @@ This package was debianized by Grahame Bowland on -Tue, 17 Jun 2003 15:04:47 +0800. +Tue, 17 Jun 2003 15:04:47 +0800, maintained temporarily by Matt Johnston +, and was adopted by Gerrit Pape on +Sun, 16 May 2004 14:38:33 +0000. It was downloaded from http://matt.ucc.asn.au/dropbear/ -Upstream Author(s): Matt Johnston +Upstream Author: Matt Johnston Copyright: diff --git a/debian/dropbear.README.Debian b/debian/dropbear.README.Debian new file mode 100644 index 0000000..8cdac38 --- /dev/null +++ b/debian/dropbear.README.Debian @@ -0,0 +1,41 @@ +Dropbear for Debian +------------------- + +This package will attempt to listen on port 22. If the OpenSSH +package ("ssh") is installed, the file /etc/default/dropbear +will be set up so that the server does not start by default. + +You can run Dropbear concurrently with OpenSSH 'sshd' by +modifying /etc/default/dropbear so that "NO_START" is set to +"0" and changing the port number that Dropbear runs on. Follow +the instructions in the file. + +This package suggests you install the "ssh" package. This package +provides the "ssh" client program, as well as the "/usr/bin/scp" +binary you will need to be able to retrieve files from a server +running Dropbear via SCP. + +Replacing OpenSSH "sshd" with Dropbear +-------------------------------------- + +You will still want to have the "ssh" package installed, as it +provides the "ssh" and "scp" binaries. When you install this +package, it checks for existing OpenSSH host keys and if found, +converts them to the Dropbear format. + +If this appears to have worked, you should be able to change over +by following these steps: + +1. Stop the OpenSSH server + % /etc/init.d/ssh stop +2. Prevent the OpenSSH server from starting in the future + % touch /etc/ssh/sshd_not_to_be_run +3. Modify the Dropbear defaults file, set NO_START to 0 and + ensure DROPBEAR_PORT is set to 22. + % editor /etc/default/dropbear +4. Restart the Dropbear server. + % /etc/init.d/dropbear restart + +See the Dropbear homepage for more information: + http://matt.ucc.asn.au/dropbear/dropbear.html + diff --git a/debian/dropbear.conffiles b/debian/dropbear.conffiles new file mode 100644 index 0000000..6919006 --- /dev/null +++ b/debian/dropbear.conffiles @@ -0,0 +1,3 @@ +/etc/init.d/dropbear +/etc/dropbear/run +/etc/dropbear/log/run diff --git a/debian/dropbear.docs b/debian/dropbear.docs new file mode 100644 index 0000000..599d48c --- /dev/null +++ b/debian/dropbear.docs @@ -0,0 +1,3 @@ +README +TODO +debian/README.runit diff --git a/debian/dropbear.init b/debian/dropbear.init index 25eda9f..d9578db 100644 --- a/debian/dropbear.init +++ b/debian/dropbear.init @@ -1,15 +1,4 @@ -#! /bin/sh -# -# skeleton example file to build /etc/init.d/ scripts. -# This file should be used to construct scripts for /etc/init.d. -# -# Written by Miquel van Smoorenburg . -# Modified for Debian -# by Ian Murdock . -# -# Version: @(#)skeleton 1.9 26-Feb-2001 miquels@cistron.nl -# - +#!/bin/sh # # Do not configure this file. Edit /etc/default/dropbear instead! # @@ -22,54 +11,45 @@ DESC="Dropbear SSH server" DROPBEAR_PORT=22 DROPBEAR_EXTRA_ARGS= NO_START=0 -set -e -test -f /etc/default/dropbear && . /etc/default/dropbear - -if [ -n "$DROPBEAR_BANNER" ]; then - DROPBEAR_EXTRA_ARGS="$DROPBEAR_EXTRA_ARGS -b $DROPBEAR_BANNER" -fi -if [ -z "$DROPBEAR_RSAKEY" ]; then - DROPBEAR_RSAKEY="/etc/dropbear/dropbear_rsa_host_key" -fi - -if [ -z "$DROPBEAR_DSSKEY" ]; then - DROPBEAR_DSSKEY="/etc/dropbear/dropbear_dss_host_key" -fi +set -e -test "$NO_START" != "0" && exit 0 +test ! -r /etc/default/dropbear || . /etc/default/dropbear +test "$NO_START" = "0" || exit 0 +test -x "$DAEMON" || exit 0 +test ! -h /var/service/dropbear || exit 0 -test -x $DAEMON || exit 0 +test -z "$DROPBEAR_BANNER" || \ + DROPBEAR_EXTRA_ARGS="$DROPBEAR_EXTRA_ARGS -b $DROPBEAR_BANNER" +test -n "$DROPBEAR_RSAKEY" || \ + DROPBEAR_RSAKEY="/etc/dropbear/dropbear_rsa_host_key" +test -n "$DROPBEAR_DSSKEY" || \ + DROPBEAR_DSSKEY="/etc/dropbear/dropbear_dss_host_key" case "$1" in start) echo -n "Starting $DESC: " - start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \ - --exec $DAEMON -- -d $DROPBEAR_DSSKEY -r $DROPBEAR_RSAKEY -p $DROPBEAR_PORT $DROPBEAR_EXTRA_ARGS + start-stop-daemon --start --quiet --pidfile /var/run/"$NAME".pid \ + --exec "$DAEMON" -- -d "$DROPBEAR_DSSKEY" -r "$DROPBEAR_RSAKEY" \ + -p "$DROPBEAR_PORT" $DROPBEAR_EXTRA_ARGS echo "$NAME." ;; stop) echo -n "Stopping $DESC: " - start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/$NAME.pid + start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/"$NAME".pid echo "$NAME." ;; restart|force-reload) - # - # If the "reload" option is implemented, move the "force-reload" - # option to the "reload" entry above. If not, "force-reload" is - # just the same as "restart". - # echo -n "Restarting $DESC: " - start-stop-daemon --stop --quiet --oknodo --pidfile \ - /var/run/$NAME.pid + start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/"$NAME".pid sleep 1 - start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \ - --exec $DAEMON -- -d $DROPBEAR_DSSKEY -r $DROPBEAR_RSAKEY -p $DROPBEAR_PORT $DROPBEAR_EXTRA_ARGS + start-stop-daemon --start --quiet --pidfile /var/run/"$NAME".pid \ + --exec "$DAEMON" -- -d "$DROPBEAR_DSSKEY" -r "$DROPBEAR_RSAKEY" \ + -p "$DROPBEAR_PORT" $DROPBEAR_EXTRA_ARGS echo "$NAME." ;; *) N=/etc/init.d/$NAME - # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 echo "Usage: $N {start|stop|restart|force-reload}" >&2 exit 1 ;; diff --git a/debian/dropbear.postinst b/debian/dropbear.postinst new file mode 100644 index 0000000..749ffd1 --- /dev/null +++ b/debian/dropbear.postinst @@ -0,0 +1,68 @@ +#!/bin/sh +set -e + +test "$1" = 'configure' || exit 0 +test -n "$2" || chown log /etc/dropbear/log/main || true + +if test ! -e /etc/dropbear/dropbear_rsa_host_key; then + if test -f /etc/ssh/ssh_host_rsa_key; then + echo "Converting existing OpenSSH RSA host key to Dropbear format." + /usr/lib/dropbear/dropbearconvert openssh dropbear \ + /etc/ssh/ssh_host_rsa_key /etc/dropbear/dropbear_rsa_host_key + else + echo "Generating Dropbear RSA key. Please wait." + dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key + fi +fi +if test ! -e /etc/dropbear/dropbear_dss_host_key; then + if test -f /etc/ssh/ssh_host_dsa_key; then + echo "Converting existing OpenSSH RSA host key to Dropbear format." + /usr/lib/dropbear/dropbearconvert openssh dropbear \ + /etc/ssh/ssh_host_dsa_key /etc/dropbear/dropbear_dss_host_key + else + echo "Generating Dropbear DSS key. Please wait." + dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key + fi +fi +if test ! -s /etc/default/dropbear; then + # check whether OpenSSH seems to be installed. + if test -x /usr/sbin/sshd; then + cat <>/etc/default/dropbear <>/etc/default/dropbear </dev/null + if test -x /usr/sbin/invoke-rc.d; then + invoke-rc.d dropbear start + else + /etc/init.d/dropbear start + fi +fi diff --git a/debian/dropbear.postrm b/debian/dropbear.postrm new file mode 100644 index 0000000..d09dab0 --- /dev/null +++ b/debian/dropbear.postrm @@ -0,0 +1,12 @@ +#! /bin/sh +set -e + +test "$1" = 'purge' || exit 0 +if test -e /etc/dropbear; then + rm -f /etc/dropbear/dropbear_rsa_host_key + rm -f /etc/dropbear/dropbear_dss_host_key + rmdir --ignore-fail-on-non-empty /etc/dropbear +fi +update-rc.d dropbear remove >/dev/null +rm -f /etc/default/dropbear +rm -rf /etc/dropbear/supervise /etc/dropbear/log/supervise diff --git a/debian/dropbear.prerm b/debian/dropbear.prerm new file mode 100644 index 0000000..89bb5b6 --- /dev/null +++ b/debian/dropbear.prerm @@ -0,0 +1,11 @@ +#!/bin/sh +set -u + +test "$1" = 'remove' || test "$1" = 'deconfigure' || exit 0 +if test -x /etc/init.d/dropbear; then + if test -x /usr/sbin/invoke-rc.d; then + invoke-rc.d dropbear stop + else + /etc/init.d/dropbear stop + fi +fi diff --git a/debian/implicit b/debian/implicit new file mode 100644 index 0000000..d28b629 --- /dev/null +++ b/debian/implicit @@ -0,0 +1,79 @@ +# $Id: implicit,v 1.1 2004/06/16 05:08:32 matt Exp $ + +.PHONY: deb-checkdir deb-checkuid + +deb-checkdir: + @test -e debian/control || sh -cx '! : wrong directory' +deb-checkuid: + @test "`id -u`" -eq 0 || sh -cx '! : need root privileges' + +%.deb: %.deb-docs %.deb-DEBIAN + @rm -f $*.deb $*.deb-checkdir $*.deb-docs $*.deb-docs-base \ + $*.deb-docs-docs $*.deb-docs-examples $*.deb-DEBIAN \ + $*.deb-DEBIAN-dir $*.deb-DEBIAN-scripts $*.deb-DEBIAN-md5sums + +%.deb-checkdir: + @test -d debian/$* || sh -cx '! : directory debian/$* missing' + @test "`id -u`" -eq 0 || sh -cx '! : need root privileges' + +%.deb-docs-base: + : implicit + @rm -f debian/$*/usr/share/doc/$*/* || : + @install -d -m0755 debian/$*/usr/share/doc/$* + : debian/$*/usr/share/doc/$*/ + @sh -cx 'install -m0644 debian/copyright debian/$*/usr/share/doc/$*/' + @sh -cx 'install -m0644 debian/changelog \ + debian/$*/usr/share/doc/$*/changelog.Debian' + @test ! -r changelog || \ + sh -cx 'install -m0644 changelog debian/$*/usr/share/doc/$*/' + @test -r debian/$*/usr/share/doc/$*/changelog || \ + sh -cx 'mv debian/$*/usr/share/doc/$*/changelog.Debian \ + debian/$*/usr/share/doc/$*/changelog' + @gzip -9 debian/$*/usr/share/doc/$*/changelog* +%.deb-docs-docs: + @for i in `cat debian/$*.docs 2>/dev/null || :`; do \ + sh -cx "install -m0644 $$i debian/$*/usr/share/doc/$*/" || exit 1; \ + done + @test ! -r debian/$*.README.Debian || \ + sh -cx 'install -m0644 debian/$*.README.Debian \ + debian/$*/usr/share/doc/$*/README.Debian' + @if test -r debian/$*.NEWS.Debian; then \ + sh -cx 'install -m0644 debian/$*.NEWS.Debian \ + debian/$*/usr/share/doc/$*/NEWS.Debian && \ + gzip -9 debian/$*/usr/share/doc/$*/NEWS.Debian'; \ + fi +%.deb-docs-examples: + @rm -rf debian/$*/usr/share/doc/$*/examples + : debian/$*/usr/share/doc/$*/examples/ + @test ! -r debian/$*.examples || \ + install -d -m0755 debian/$*/usr/share/doc/$*/examples + @for i in `cat debian/$*.examples 2>/dev/null || :`; do \ + sh -cx "install -m0644 $$i debian/$*/usr/share/doc/$*/examples/" \ + || exit 1; \ + done +%.deb-docs: %.deb-checkdir %.deb-docs-base %.deb-docs-docs %.deb-docs-examples + : debian/$*/usr/share/doc/$*/ ok + +%.deb-DEBIAN-base: + @rm -rf debian/$*/DEBIAN + : debian/$*/DEBIAN/ + @install -d -m0755 debian/$*/DEBIAN + @for i in conffiles shlibs; do \ + test ! -r debian/$*.$$i || \ + sh -cx "install -m0644 debian/$*.$$i debian/$*/DEBIAN/$$i" \ + || exit 1; \ + done +%.deb-DEBIAN-scripts: + @for i in preinst prerm postinst postrm; do \ + test ! -r debian/$*.$$i || \ + sh -cx "install -m0755 debian/$*.$$i debian/$*/DEBIAN/$$i" \ + || exit 1; \ + done +%.deb-DEBIAN-md5sums: + : debian/$*/DEBIAN/md5sums + @rm -f debian/$*/DEBIAN/md5sums + @cd debian/$* && find * -path 'DEBIAN' -prune -o \ + -type f -exec md5sum {} >>DEBIAN/md5sums \; +%.deb-DEBIAN: %.deb-checkdir %.deb-DEBIAN-base %.deb-DEBIAN-scripts \ + %.deb-DEBIAN-md5sums + : debian/$*/DEBIAN/ ok diff --git a/debian/rules b/debian/rules index 4d73093..ee7b14a 100644 --- a/debian/rules +++ b/debian/rules @@ -1,134 +1,96 @@ #!/usr/bin/make -f -# Sample debian/rules that uses debhelper. -# GNU copyright 1997 to 1999 by Joey Hess. -# -# Modified to make a template file for a multi-binary package with separated -# build-arch and build-indep targets by Bill Allombert 2001 -# Uncomment this to turn on verbose mode. -#export DH_VERBOSE=1 +#export DH_OPTIONS +DEB_HOST_GNU_TYPE ?=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE) +DEB_BUILD_GNU_TYPE ?=$(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE) -# This has to be exported to make some magic below work. -export DH_OPTIONS - -# These are used for cross-compiling and for saving the configure script -# from having to guess our platform (since we know it already) -DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE) -DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE) - - -CFLAGS = -Wall -g +STRIP =strip +ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS))) + STRIP =: nostrip +endif +CFLAGS =-Wall -g ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS))) - CFLAGS += -O0 + CFLAGS +=-O0 else - CFLAGS += -O2 -endif -ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS))) - INSTALL_PROGRAM += -s + CFLAGS +=-O2 endif -config.status: configure - dh_testdir - # Add here commands to configure the package. - CFLAGS='-DSFTPSERVER_PATH="\"/usr/lib/sftp-server\""' ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info - - -#Architecture -build: build-arch #build-indep - -build-arch: build-arch-stamp -build-arch-stamp: config.status - - # Add here commands to compile the arch part of the package. - $(MAKE) CC=gcc LD=gcc - -build-indep: build-indep-stamp -build-indep-stamp: config.status - - # Add here commands to compile the indep part of the package. - #$(MAKE) doc - -clean: - dh_testdir - dh_testroot - rm -f build-arch-stamp build-indep-stamp config-stamp - - # Add here commands to clean up after the build process. - -$(MAKE) clean -ifneq "$(wildcard /usr/share/misc/config.sub)" "" - cp -f /usr/share/misc/config.sub config.sub -endif -ifneq "$(wildcard /usr/share/misc/config.guess)" "" - cp -f /usr/share/misc/config.guess config.guess +CC =gcc +ifneq (,$(findstring diet,$(DEB_BUILD_OPTIONS))) + CC =diet -v -Os gcc endif - - dh_clean - -install: install-indep install-arch -install-indep: - dh_testdir - dh_testroot - dh_clean -k -i - dh_installdirs -i - - # Add here commands to install the indep part of the package into - # debian/-doc. - #INSTALLDOC# - - dh_install -i - -install-arch: - dh_testdir - dh_testroot - dh_clean -k -a - dh_installdirs -a - dh_installdirs /etc/dropbear - - # Add here commands to install the arch part of the package into - # debian/tmp. - $(MAKE) install prefix=$(CURDIR)/debian/dropbear/usr - - dh_install -a -# Must not depend on anything. This is to be called by -# binary-arch/binary-multi -# in another 'make' thread. -binary-common: - cat $(CURDIR)/debian/copyright.in $(CURDIR)/LICENSE > $(CURDIR)/debian/copyright - dh_testdir - dh_testroot - dh_installchangelogs CHANGES - dh_installdocs - dh_installexamples -# dh_installmenu -# dh_installdebconf -# dh_installlogrotate -# dh_installemacsen -# dh_installpam -# dh_installmime - dh_installinit -# dh_installcron -# dh_installinfo - dh_installman - dh_link - dh_strip - dh_compress - dh_fixperms -# dh_perl -# dh_python - dh_makeshlibs - dh_installdeb - dh_gencontrol - dh_md5sums - dh_builddeb -# Build architecture independant packages using the common target. -binary-indep: build-indep install-indep - $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common - -# Build architecture dependant packages using the common target. -binary-arch: build-arch install-arch - $(MAKE) -f debian/rules DH_OPTIONS=-a binary-common - -binary: binary-arch #binary-indep -.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch +DIR=`pwd`/debian/dropbear + +patch: deb-checkdir patch-stamp +patch-stamp: +# no patches for now +# for i in debian/diff/*.diff; do patch -p0 <$$i || exit 1; done + touch patch-stamp + +config.status: patch-stamp configure + CFLAGS="$(CFLAGS)"' -DSFTPSERVER_PATH="\"/usr/lib/sftp-server\""' \ + ./configure --host="$(DEB_HOST_GNU_TYPE)" \ + --build="$(DEB_BUILD_GNU_TYPE)" --prefix=/usr \ + --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info + +build: deb-checkdir build-stamp +build-stamp: config.status + $(MAKE) CC="$(CC)" LD="$(CC)" + touch build-stamp + +clean: deb-checkdir deb-checkuid + -$(MAKE) distclean +# test ! -e patch-stamp || \ +# for i in debian/diff/*.diff; do patch -p0 -R <$$i; done + rm -f patch-stamp build-stamp config.log config.status + rm -rf "$(DIR)" + rm -f debian/files debian/substvars debian/copyright changelog + +install: deb-checkdir deb-checkuid build-stamp + rm -rf "$(DIR)" + install -d -m0755 "$(DIR)"/etc/dropbear + # programs + install -d -m0755 "$(DIR)"/usr/sbin + install -m0755 dropbear "$(DIR)"/usr/sbin/dropbear + install -d -m0755 "$(DIR)"/usr/bin + install -m0755 dropbearkey "$(DIR)"/usr/bin/dropbearkey + install -d -m0755 "$(DIR)"/usr/lib/dropbear + install -m0755 dropbearconvert \ + "$(DIR)"/usr/lib/dropbear/dropbearconvert + $(STRIP) -R .comment -R .note "$(DIR)"/usr/sbin/* \ + "$(DIR)"/usr/bin/* "$(DIR)"/usr/lib/dropbear/* + # init and run scripts + install -d -m0755 "$(DIR)"/etc/init.d + install -m0755 debian/dropbear.init "$(DIR)"/etc/init.d/dropbear + install -m0755 debian/service/run "$(DIR)"/etc/dropbear/run + install -d -m0755 "$(DIR)"/etc/dropbear/log + install -m0755 debian/service/log "$(DIR)"/etc/dropbear/log/run + ln -s /var/log/dropbear "$(DIR)"/etc/dropbear/log/main + ln -s /var/run/dropbear "$(DIR)"/etc/dropbear/supervise + ln -s /var/run/dropbear.log "$(DIR)"/etc/dropbear/log/supervise + # man pages + install -d -m0755 "$(DIR)"/usr/share/man/man8 + for i in dropbear.8 dropbearkey.8; do \ + install -m644 $$i "$(DIR)"/usr/share/man/man8/ || exit 1; \ + done + gzip -9 "$(DIR)"/usr/share/man/man8/*.8 + # copyright, changelog + cat debian/copyright.in LICENSE >debian/copyright + ln -s CHANGES changelog + +binary-indep: + +binary-arch: install dropbear.deb + test "$(CC)" != 'gcc' || \ + dpkg-shlibdeps "$(DIR)"/usr/sbin/* "$(DIR)"/usr/bin/* \ + "$(DIR)"/usr/lib/dropbear/* + dpkg-gencontrol -isp -pdropbear -P"$(DIR)" + dpkg -b "$(DIR)" .. + +binary: binary-arch binary-indep + +.PHONY: patch build clean install binary-indep binary-arch binary + +include debian/implicit diff --git a/debian/service/log b/debian/service/log new file mode 100644 index 0000000..2ffb13d --- /dev/null +++ b/debian/service/log @@ -0,0 +1,2 @@ +#!/bin/sh +exec chpst -udropbearlog svlogd -tt ./main diff --git a/debian/service/run b/debian/service/run new file mode 100644 index 0000000..f208085 --- /dev/null +++ b/debian/service/run @@ -0,0 +1,3 @@ +#!/bin/sh +exec 2>&1 +exec dropbear -d ./dropbear_dss_host_key -r ./dropbear_rsa_host_key -F -E -p 22 diff --git a/dropbearkey.c b/dropbearkey.c index eac0823..5d4475b 100644 --- a/dropbearkey.c +++ b/dropbearkey.c @@ -45,7 +45,6 @@ * */ #include "includes.h" -#include "runopts.h" #include "signkey.h" #include "buffer.h" #include "dbutil.h" diff --git a/dss.c b/dss.c index 74b92c7..9b56f10 100644 --- a/dss.c +++ b/dss.c @@ -171,6 +171,8 @@ int buf_dss_verify(buffer* buf, dss_key *key, const unsigned char* data, TRACE(("enter buf_dss_verify")); assert(key != NULL); + m_mp_init_multi(&val1, &val2, &val3, &val4, NULL); + /* get blob, check length */ string = buf_getstring(buf, &stringlen); if (stringlen != 2*SHA1_HASH_SIZE) { @@ -182,8 +184,6 @@ int buf_dss_verify(buffer* buf, dss_key *key, const unsigned char* data, sha1_process(&hs, data, len); sha1_done(&hs, msghash); - m_mp_init_multi(&val1, &val2, &val3, &val4, NULL); - /* create the signature - s' and r' are the received signatures in buf */ /* w = (s')-1 mod q */ /* let val1 = s' */ diff --git a/gendss.c b/gendss.c index 3e9db09..5a440a1 100644 --- a/gendss.c +++ b/gendss.c @@ -31,7 +31,6 @@ #include "gendss.h" #include "dss.h" -#define PSIZE 128 /* 1024 bit*/ #define QSIZE 20 /* 160 bit */ #ifdef DROPBEAR_DSS diff --git a/includes.h b/includes.h index 52c48ed..b37422b 100644 --- a/includes.h +++ b/includes.h @@ -38,7 +38,6 @@ #include #include #include -#include #include #include @@ -56,6 +55,7 @@ #include #include #include +#include #include @@ -111,10 +111,6 @@ #include #endif -#ifdef HAVE_SYS_DIRENT_H -#include -#endif - #include "libtomcrypt/mycrypt_custom.h" #include "libtommath/tommath.h" diff --git a/options.h b/options.h index f0831b9..c687a8c 100644 --- a/options.h +++ b/options.h @@ -47,6 +47,11 @@ * if you want to use this) */ /*#define NO_FAST_EXPTMOD*/ +/* Set this if you want to use the DROPBEAR_SMALL_CODE option. This can save +several kB in binary size, however will make the symmetrical ciphers (AES, DES +etc) slower (perhaps by 50%). Recommended for most small systems. */ +#define DROPBEAR_SMALL_CODE + /* Enable X11 Forwarding - server only */ #define ENABLE_X11FWD @@ -175,7 +180,7 @@ *******************************************************************/ #ifndef DROPBEAR_VERSION -#define DROPBEAR_VERSION "0.41-and-client" +#define DROPBEAR_VERSION "0.45-beta1" #endif #define LOCAL_IDENT "SSH-2.0-dropbear_" DROPBEAR_VERSION diff --git a/packet.c b/packet.c index 997bc6f..5e8e14d 100644 --- a/packet.c +++ b/packet.c @@ -50,7 +50,7 @@ static void buf_compress(buffer * dest, buffer * src, unsigned int len); void write_packet() { int len, written; - buffer * writebuf; + buffer * writebuf = NULL; TRACE(("enter write_packet")); assert(!isempty(&ses.writequeue)); @@ -80,6 +80,7 @@ void write_packet() { /* We've finished with the packet, free it */ dequeue(&ses.writequeue); buf_free(writebuf); + writebuf = NULL; } else { /* More packet left to write, leave it in the queue for later */ buf_incrpos(writebuf, written); @@ -503,6 +504,7 @@ void encrypt_packet() { /* clearwritebuf is finished with */ buf_free(clearwritebuf); + clearwritebuf = NULL; /* enqueue the packet for sending */ buf_setpos(writebuf, 0); diff --git a/random.c b/random.c index 725b29c..65a9c64 100644 --- a/random.c +++ b/random.c @@ -60,7 +60,7 @@ static void readrand(unsigned char* buf, unsigned int buflen) { #ifdef DROPBEAR_DEV_URANDOM readfd = open(DEV_URANDOM, O_RDONLY); - if (!readfd) { + if (readfd < 0) { dropbear_exit("couldn't open random device"); } #endif @@ -71,7 +71,8 @@ static void readrand(unsigned char* buf, unsigned int buflen) { strlcpy(egdsock.sun_path, DROPBEAR_EGD_SOCKET, sizeof(egdsock.sun_path)); - if ((readfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) { + readfd = socket(PF_UNIX, SOCK_STREAM, 0); + if (readfd < 0) { dropbear_exit("couldn't open random device"); } /* todo - try various common locations */ diff --git a/rsa.c b/rsa.c index 2d63c02..1130e93 100644 --- a/rsa.c +++ b/rsa.c @@ -244,8 +244,11 @@ int buf_rsa_verify(buffer * buf, rsa_key *key, const unsigned char* data, } out: - mp_clear_multi(rsa_em, &rsa_mdash, &rsa_s, NULL); - m_free(rsa_em); + if (rsa_em) { + mp_clear(rsa_em); + m_free(rsa_em); + } + mp_clear_multi(&rsa_mdash, &rsa_s, NULL); TRACE(("leave buf_rsa_verify: ret %d", ret)); return ret; @@ -260,15 +263,16 @@ void buf_put_rsa_sign(buffer* buf, rsa_key *key, const unsigned char* data, unsigned int nsize, ssize; unsigned int i; mp_int rsa_s; - mp_int *rsa_em; + mp_int *rsa_em = NULL; TRACE(("enter buf_put_rsa_sign")); assert(key != NULL); rsa_em = rsa_pad_em(key, data, len); - /* the actual signing of the padded data */ m_mp_init(&rsa_s); + + /* the actual signing of the padded data */ /* s = em^d mod n */ if (mp_exptmod(rsa_em, key->d, key->n, &rsa_s) != MP_OKAY) { dropbear_exit("rsa error"); @@ -322,10 +326,10 @@ static mp_int * rsa_pad_em(rsa_key * key, {0x00, 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14}; #define RSA_ASN1_MAGIC_LEN 16 - buffer * rsa_EM; + buffer * rsa_EM = NULL; hash_state hs; unsigned int nsize; - mp_int * rsa_em; + mp_int * rsa_em = NULL; assert(key != NULL); assert(data != NULL); diff --git a/signkey.c b/signkey.c index 3efcc2b..7ae08b8 100644 --- a/signkey.c +++ b/signkey.c @@ -194,7 +194,7 @@ void buf_put_pub_key(buffer* buf, sign_key *key, int type) { buffer *pubkeys; TRACE(("enter buf_put_pub_key")); - pubkeys = buf_new(1000); + pubkeys = buf_new(MAX_PUBKEY_SIZE); #ifdef DROPBEAR_DSS if (type == DROPBEAR_SIGNKEY_DSS) { @@ -356,7 +356,7 @@ void buf_put_sign(buffer* buf, sign_key *key, int type, buffer *sigblob; - sigblob = buf_new(1000); + sigblob = buf_new(MAX_PUBKEY_SIZE); #ifdef DROPBEAR_DSS if (type == DROPBEAR_SIGNKEY_DSS) { diff --git a/svr-agentfwd.c b/svr-agentfwd.c index b588586..4e9aa56 100644 --- a/svr-agentfwd.c +++ b/svr-agentfwd.c @@ -64,7 +64,7 @@ int agentreq(struct ChanSess * chansess) { /* create the unix socket dir and file */ if (bindagent(fd, chansess) == DROPBEAR_FAILURE) { - return DROPBEAR_FAILURE; + goto fail; } /* listen */ @@ -146,7 +146,7 @@ void agentcleanup(struct ChanSess * chansess) { chansess->agentlistener = NULL; } - if (chansess->agentfile && chansess->agentdir) { + if (chansess->agentfile != NULL && chansess->agentdir != NULL) { /* Remove the dir as the user. That way they can't cause problems except * for themselves */ diff --git a/svr-auth.c b/svr-auth.c index 314171f..ae7ead2 100644 --- a/svr-auth.c +++ b/svr-auth.c @@ -91,7 +91,7 @@ static void send_msg_userauth_banner() { * checking, and handle success or failure */ void recv_msg_userauth_request() { - unsigned char *username, *servicename, *methodname; + unsigned char *username = NULL, *servicename = NULL, *methodname = NULL; unsigned int userlen, servicelen, methodlen; TRACE(("enter recv_msg_userauth_request")); @@ -275,7 +275,7 @@ goodshell: * failures */ void send_msg_userauth_failure(int partial, int incrfail) { - buffer *typebuf; + buffer *typebuf = NULL; TRACE(("enter send_msg_userauth_failure")); diff --git a/svr-authpasswd.c b/svr-authpasswd.c index 7c6c7b7..6f7c909 100644 --- a/svr-authpasswd.c +++ b/svr-authpasswd.c @@ -37,14 +37,14 @@ void svr_auth_password() { #ifdef HAVE_SHADOW_H - struct spwd *spasswd; + struct spwd *spasswd = NULL; #endif - char * passwdcrypt; /* the crypt from /etc/passwd or /etc/shadow */ - char * testcrypt; /* crypt generated from the user's password sent */ + char * passwdcrypt = NULL; /* the crypt from /etc/passwd or /etc/shadow */ + char * testcrypt = NULL; /* crypt generated from the user's password sent */ unsigned char * password; unsigned int passwordlen; - unsigned char changepw; + unsigned int changepw; passwdcrypt = ses.authstate.pw->pw_passwd; #ifdef HAVE_SHADOW_H diff --git a/svr-authpubkey.c b/svr-authpubkey.c index 9205078..14b5a78 100644 --- a/svr-authpubkey.c +++ b/svr-authpubkey.c @@ -53,7 +53,7 @@ void svr_auth_pubkey() { unsigned char testkey; /* whether we're just checking if a key is usable */ unsigned char* algo = NULL; /* pubkey algo */ unsigned int algolen; - unsigned char* keyblob; + unsigned char* keyblob = NULL; unsigned int keybloblen; buffer * signbuf = NULL; sign_key * key = NULL; diff --git a/svr-chansession.c b/svr-chansession.c index a0e877c..01612f4 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -273,7 +273,7 @@ static void closechansess(struct Channel *channel) { * or x11/authagent forwarding. These are passed to appropriate handlers */ static void chansessionrequest(struct Channel *channel) { - unsigned char * type; + unsigned char * type = NULL; unsigned int typelen; unsigned char wantreply; int ret = 1; @@ -320,7 +320,7 @@ static void chansessionrequest(struct Channel *channel) { out: if (wantreply) { - if (ret == 0) { + if (ret == DROPBEAR_SUCCESS) { send_msg_channel_success(channel); } else { send_msg_channel_failure(channel); @@ -336,7 +336,7 @@ out: static int sessionsignal(struct ChanSess *chansess) { int sig = 0; - unsigned char* signame; + unsigned char* signame = NULL; int i; if (chansess->pid == 0) { @@ -528,11 +528,14 @@ static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, int iscmd, int issubsys) { unsigned int cmdlen; + int ret; TRACE(("enter sessioncommand")); if (chansess->cmd != NULL) { - /* TODO - send error - multiple commands? */ + /* Note that only one command can _succeed_. The client might try + * one command (which fails), then try another. Ie fallback + * from sftp to scp */ return DROPBEAR_FAILURE; } @@ -541,6 +544,7 @@ static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, chansess->cmd = buf_getstring(ses.payload, &cmdlen); if (cmdlen > MAX_CMD_LEN) { + m_free(chansess->cmd); /* TODO - send error - too long ? */ return DROPBEAR_FAILURE; } @@ -552,6 +556,7 @@ static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, } else #endif { + m_free(chansess->cmd); return DROPBEAR_FAILURE; } } @@ -559,11 +564,16 @@ static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, if (chansess->term == NULL) { /* no pty */ - return noptycommand(channel, chansess); + ret = noptycommand(channel, chansess); } else { /* want pty */ - return ptycommand(channel, chansess); + ret = ptycommand(channel, chansess); + } + + if (ret == DROPBEAR_FAILURE) { + m_free(chansess->cmd); } + return ret; } /* Execute a command and set up redirection of stdin/stdout/stderr without a @@ -650,7 +660,7 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { pid_t pid; - struct logininfo *li; + struct logininfo *li = NULL; #ifdef DO_MOTD buffer * motdbuf = NULL; int len; @@ -778,8 +788,8 @@ static void addchildpid(struct ChanSess *chansess, pid_t pid) { static void execchild(struct ChanSess *chansess) { char *argv[4]; - char * usershell; - char * baseshell; + char * usershell = NULL; + char * baseshell = NULL; unsigned int i; /* wipe the hostkey */ @@ -863,6 +873,11 @@ static void execchild(struct ChanSess *chansess) { agentset(chansess); #endif + /* Re-enable SIGPIPE for the executed process */ + if (signal(SIGPIPE, SIG_DFL) == SIG_ERR) { + dropbear_exit("signal() error"); + } + baseshell = basename(usershell); if (chansess->cmd != NULL) { @@ -921,7 +936,7 @@ void svr_chansessinitialise() { /* add a new environment variable, allocating space for the entry */ void addnewvar(const char* param, const char* var) { - char* newvar; + char* newvar = NULL; int plen, vlen; plen = strlen(param); diff --git a/svr-tcpfwd.c b/svr-tcpfwd.c index 0eccae4..0f22a23 100644 --- a/svr-tcpfwd.c +++ b/svr-tcpfwd.c @@ -208,7 +208,7 @@ static int newtcpdirect(struct Channel * channel) { char portstring[NI_MAXSERV]; int sock; int len; - int ret = DROPBEAR_FAILURE; + int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; if (opts.nolocaltcp) { TRACE(("leave newtcpdirect: local tcp forwarding disabled")); @@ -240,6 +240,7 @@ static int newtcpdirect(struct Channel * channel) { snprintf(portstring, sizeof(portstring), "%d", destport); sock = connect_remote(desthost, portstring, 1, NULL); if (sock < 0) { + err = SSH_OPEN_CONNECT_FAILED; TRACE(("leave newtcpdirect: sock failed")); goto out; } @@ -253,13 +254,13 @@ static int newtcpdirect(struct Channel * channel) { channel->infd = sock; channel->initconn = 1; - ret = DROPBEAR_SUCCESS; + err = SSH_OPEN_IN_PROGRESS; out: m_free(desthost); m_free(orighost); - TRACE(("leave newtcpdirect: ret %d", ret)); - return ret; + TRACE(("leave newtcpdirect: err %d", err)); + return err; } #endif diff --git a/svr-x11fwd.c b/svr-x11fwd.c index 0f4f71e..a8d1cd5 100644 --- a/svr-x11fwd.c +++ b/svr-x11fwd.c @@ -131,7 +131,7 @@ static void x11accept(struct Listener* listener, int sock) { void x11setauth(struct ChanSess *chansess) { char display[20]; /* space for "localhost:12345.123" */ - FILE * authprog; + FILE * authprog = NULL; int val; if (chansess->x11listener == NULL) { @@ -187,7 +187,7 @@ static const struct ChanType chan_x11 = { static int send_msg_channel_open_x11(int fd, struct sockaddr_in* addr) { - char* ipstring; + char* ipstring = NULL; if (send_msg_channel_open_init(fd, &chan_x11) == DROPBEAR_SUCCESS) { ipstring = inet_ntoa(addr->sin_addr); -- cgit v1.2.3 From 954a8dce0fdd5f4d16eeb5436e4a8659e91e1cbd Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 17 Aug 2004 09:56:23 +0000 Subject: fix for AIX not having WCOREDUMP --HG-- extra : convert_revision : 9a728aa6db6d1105267c377fa3d5448ee5f5a4ca --- svr-chansession.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index 01612f4..ffc50de 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -92,6 +92,8 @@ static void sesssigchild_handler(int dummy) { chansess->exitsignal = WTERMSIG(status); #ifndef AIX chansess->exitcore = WCOREDUMP(status); +#else + chansess->exitcore = 0; #endif } else { /* we use this to determine how pid exited */ -- cgit v1.2.3 From 4dd70c433e99c6a38d9547ef7c20b2aaccb75c6e Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Wed, 18 Aug 2004 16:42:08 +0000 Subject: Mention the race condition between setting the childpid entry, and the child exiting. --HG-- extra : convert_revision : 0acd2d97675d6a45fa1664790b4b9c891461de10 --- svr-chansession.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index ffc50de..d56fb2c 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -78,7 +78,6 @@ static void sesssigchild_handler(int dummy) { TRACE(("enter sigchld handler")); while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { - /* find the corresponding chansess */ for (i = 0; i < svr_ses.childpidsize; i++) { if (svr_ses.childpids[i].pid == pid) { @@ -630,7 +629,10 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { TRACE(("continue noptycommand: parent")); chansess->pid = pid; - /* add a child pid */ + /* add a child pid - Beware: there's a race between this, and the + * exec() called from the child. If the child finishes before we've + * done this (ie if it was a shell builtin and fast), we won't return a + * proper return code. For now, we ignore this case. */ addchildpid(chansess, pid); close(infds[FDIN]); -- cgit v1.2.3 From 51a74b47995cfe24ef190b7e254f768eb92735f5 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 26 Aug 2004 13:16:40 +0000 Subject: - added circular buffering for channels - added stderr support for the client - cleaned up a bunch of "unused" warnings, duplicated header definitions - added exit-status support for the client --HG-- extra : convert_revision : 5bdf806d8b440c87f7235414662f4189195618f4 --- TODO | 2 + buffer.c | 2 +- channel.h | 12 ++--- chansession.h | 5 -- circbuffer.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ circbuffer.h | 50 ++++++++++++++++++++ cli-chansession.c | 36 ++++++++++++-- cli-main.c | 3 +- cli-session.c | 5 +- cli-tcpfwd.c | 2 +- common-channel.c | 67 ++++++++++++++------------ common-session.c | 4 +- dbutil.c | 3 +- includes.h | 10 ++++ session.h | 5 +- svr-agentfwd.c | 2 +- svr-chansession.c | 10 +++- svr-main.c | 7 ++- tcpfwd.h | 3 +- 19 files changed, 302 insertions(+), 64 deletions(-) create mode 100644 circbuffer.c create mode 100644 circbuffer.h (limited to 'svr-chansession.c') diff --git a/TODO b/TODO index 0af56c9..4ac3757 100644 --- a/TODO +++ b/TODO @@ -2,6 +2,8 @@ Current: Things which might need doing: +- exit with returned exit codes? + - errfd needs fixing - Make options.h generated from configure perhaps? diff --git a/buffer.c b/buffer.c index df608d9..793eee1 100644 --- a/buffer.c +++ b/buffer.c @@ -1,5 +1,5 @@ /* - * Dropbear - a SSH2 server + * Dropbear SSH * * Copyright (c) 2002,2003 Matt Johnston * All rights reserved. diff --git a/channel.h b/channel.h index 3afd9f1..8a494c7 100644 --- a/channel.h +++ b/channel.h @@ -45,16 +45,15 @@ /* Not a real type */ #define SSH_OPEN_IN_PROGRESS 99 -#define MAX_CHANNELS 60 /* simple mem restriction, includes each tcp/x11 +#define MAX_CHANNELS 100 /* simple mem restriction, includes each tcp/x11 connection, so can't be _too_ small */ #define CHAN_EXTEND_SIZE 3 /* how many extra slots to add when we need more */ -#define RECV_MAXWINDOW 6000 /* tweak */ -#define RECV_WINDOWEXTEND (RECV_MAXWINDOW/2) /* We send a "window extend" every - RECV_WINDOWEXTEND bytes */ -#define RECV_MAXPACKET 1400 /* tweak */ -#define RECV_MINWINDOW 19000 /* when we get below this, we send a windowadjust */ +#define RECV_MAXWINDOW 4000 /* tweak */ +#define RECV_WINDOWEXTEND 500 /* We send a "window extend" every + RECV_WINDOWEXTEND bytes */ +#define RECV_MAXPACKET RECV_MAXWINDOW /* tweak */ struct ChanType; @@ -63,6 +62,7 @@ struct Channel { unsigned int index; /* the local channel index */ unsigned int remotechan; unsigned int recvwindow, transwindow; + unsigned int recvdonelen; unsigned int recvmaxpacket, transmaxpacket; void* typedata; /* a pointer to type specific data */ int infd; /* data to send over the wire */ diff --git a/chansession.h b/chansession.h index 3fb1901..84ca55a 100644 --- a/chansession.h +++ b/chansession.h @@ -68,11 +68,6 @@ struct ChildPid { }; -void chansessionrequest(struct Channel * channel); -void send_msg_chansess_exitstatus(struct Channel * channel, - struct ChanSess * chansess); -void send_msg_chansess_exitsignal(struct Channel * channel, - struct ChanSess * chansess); void addnewvar(const char* param, const char* var); void cli_send_chansess_request(); diff --git a/circbuffer.c b/circbuffer.c new file mode 100644 index 0000000..a2edcbb --- /dev/null +++ b/circbuffer.c @@ -0,0 +1,138 @@ +/* + * Dropbear SSH + * + * Copyright (c) 2002-2004 Matt Johnston + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ + +#include "includes.h" +#include "dbutil.h" +#include "circbuffer.h" + +#define MAX_CBUF_SIZE 100000000 + +circbuffer * cbuf_new(unsigned int size) { + + circbuffer *cbuf = NULL; + + if (size > MAX_CBUF_SIZE) { + dropbear_exit("bad cbuf size"); + } + + cbuf = (circbuffer*)m_malloc(sizeof(circbuffer)); + cbuf->data = (unsigned char*)m_malloc(size); + cbuf->used = 0; + cbuf->readpos = 0; + cbuf->writepos = 0; + cbuf->size = size; + + return cbuf; +} + +void cbuf_free(circbuffer * cbuf) { + + m_free(cbuf->data); + m_free(cbuf); +} + +unsigned int cbuf_getused(circbuffer * cbuf) { + + return cbuf->used; + +} + +unsigned int cbuf_getavail(circbuffer * cbuf) { + + return cbuf->size - cbuf->used; + +} + +unsigned int cbuf_readlen(circbuffer *cbuf) { + + assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size); + assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); + + if (cbuf->used == 0) { + TRACE(("cbuf_readlen: unused buffer")); + return 0; + } + + if (cbuf->readpos < cbuf->writepos) { + return cbuf->writepos - cbuf->readpos; + } + + return cbuf->size - cbuf->readpos; +} + +unsigned int cbuf_writelen(circbuffer *cbuf) { + + assert(cbuf->used <= cbuf->size); + assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size); + assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); + + if (cbuf->used == cbuf->size) { + TRACE(("cbuf_writelen: full buffer")); + return 0; /* full */ + } + + if (cbuf->writepos < cbuf->readpos) { + return cbuf->readpos - cbuf->writepos; + } + + return cbuf->size - cbuf->writepos; +} + +unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len) { + if (len > cbuf_readlen(cbuf)) { + dropbear_exit("bad cbuf read"); + } + + return &cbuf->data[cbuf->readpos]; +} + +unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len) { + + if (len > cbuf_writelen(cbuf)) { + dropbear_exit("bad cbuf write"); + } + + return &cbuf->data[cbuf->writepos]; +} + +void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) { + if (len > cbuf_writelen(cbuf)) { + dropbear_exit("bad cbuf write"); + } + + cbuf->used += len; + assert(cbuf->used <= cbuf->size); + cbuf->writepos = (cbuf->writepos + len) % cbuf->size; +} + + +void cbuf_incrread(circbuffer *cbuf, unsigned int len) { + if (len > cbuf_readlen(cbuf)) { + dropbear_exit("bad cbuf read"); + } + + assert(cbuf->used >= len); + cbuf->used -= len; + cbuf->readpos = (cbuf->readpos + len) % cbuf->size; +} diff --git a/circbuffer.h b/circbuffer.h new file mode 100644 index 0000000..21c5134 --- /dev/null +++ b/circbuffer.h @@ -0,0 +1,50 @@ +/* + * Dropbear SSH + * + * Copyright (c) 2002-2004 Matt Johnston + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ + +#ifndef _CIRCBUFFER_H_ +#define _CIRCBUFFER_H_ +struct circbuf { + + unsigned int size; + unsigned int readpos; + unsigned int writepos; + unsigned int used; + unsigned char* data; +}; + +typedef struct circbuf circbuffer; + +circbuffer * cbuf_new(unsigned int size); +void cbuf_free(circbuffer * cbuf); + +unsigned int cbuf_getused(circbuffer * cbuf); /* how much data stored */ +unsigned int cbuf_getavail(circbuffer * cbuf); /* how much we can write */ +unsigned int cbuf_readlen(circbuffer *cbuf); /* max linear read len */ +unsigned int cbuf_writelen(circbuffer *cbuf); /* max linear write len */ + +unsigned char* cbuf_readptr(circbuffer *cbuf, unsigned int len); +unsigned char* cbuf_writeptr(circbuffer *cbuf, unsigned int len); +void cbuf_incrwrite(circbuffer *cbuf, unsigned int len); +void cbuf_incrread(circbuffer *cbuf, unsigned int len); +#endif diff --git a/cli-chansession.c b/cli-chansession.c index df6eb45..50226dd 100644 --- a/cli-chansession.c +++ b/cli-chansession.c @@ -32,9 +32,11 @@ #include "ssh.h" #include "runopts.h" #include "termcodes.h" +#include "chansession.h" static void cli_closechansess(struct Channel *channel); static int cli_initchansess(struct Channel *channel); +static void cli_chansessreq(struct Channel *channel); static void start_channel_request(struct Channel *channel, unsigned char *type); @@ -42,19 +44,43 @@ static void send_chansess_pty_req(struct Channel *channel); static void send_chansess_shell_req(struct Channel *channel); static void cli_tty_setup(); -void cli_tty_cleanup(); const struct ChanType clichansess = { 0, /* sepfds */ "session", /* name */ cli_initchansess, /* inithandler */ NULL, /* checkclosehandler */ - NULL, /* reqhandler */ + cli_chansessreq, /* reqhandler */ cli_closechansess, /* closehandler */ }; +static void cli_chansessreq(struct Channel *channel) { + + unsigned char* type = NULL; + int wantreply; + + TRACE(("enter cli_chansessreq")); + + type = buf_getstring(ses.payload, NULL); + wantreply = buf_getbyte(ses.payload); + + if (strcmp(type, "exit-status") != 0) { + TRACE(("unknown request '%s'", type)); + send_msg_channel_failure(channel); + goto out; + } + + /* We'll just trust what they tell us */ + cli_ses.retval = buf_getint(ses.payload); + TRACE(("got exit-status of '%d'", cli_ses.retval)); + +out: + m_free(type); +} + + /* If the main session goes, we close it up */ -static void cli_closechansess(struct Channel *channel) { +static void cli_closechansess(struct Channel *UNUSED(channel)) { /* This channel hasn't gone yet, so we have > 1 */ if (ses.chancount > 1) { @@ -228,7 +254,7 @@ static void put_winsize() { } -static void sigwinch_handler(int dummy) { +static void sigwinch_handler(int UNUSED(unused)) { cli_ses.winchange = 1; @@ -317,7 +343,7 @@ static int cli_initchansess(struct Channel *channel) { channel->infd = STDOUT_FILENO; channel->outfd = STDIN_FILENO; channel->errfd = STDERR_FILENO; - channel->extrabuf = buf_new(RECV_MAXWINDOW); + channel->extrabuf = cbuf_new(RECV_MAXWINDOW); if (cli_opts.wantpty) { send_chansess_pty_req(channel); diff --git a/cli-main.c b/cli-main.c index 659177e..34a1e42 100644 --- a/cli-main.c +++ b/cli-main.c @@ -96,7 +96,8 @@ static void cli_dropbear_exit(int exitcode, const char* format, va_list param) { exit(exitcode); } -static void cli_dropbear_log(int priority, const char* format, va_list param) { +static void cli_dropbear_log(int UNUSED(priority), + const char* format, va_list param) { char printbuf[1024]; diff --git a/cli-session.c b/cli-session.c index b51e20f..671735c 100644 --- a/cli-session.c +++ b/cli-session.c @@ -118,6 +118,9 @@ static void cli_session_init() { cli_ses.stdincopy = dup(STDIN_FILENO); cli_ses.stdinflags = fcntl(STDIN_FILENO, F_GETFL, 0); + cli_ses.retval = EXIT_SUCCESS; /* Assume it's clean if we don't get a + specific exit status */ + /* Auth */ cli_ses.lastpubkey = NULL; cli_ses.lastauthtype = NULL; @@ -261,7 +264,7 @@ static void cli_finished() { common_session_cleanup(); fprintf(stderr, "Connection to %s@%s:%s closed.\n", cli_opts.username, cli_opts.remotehost, cli_opts.remoteport); - exit(EXIT_SUCCESS); + exit(cli_ses.retval); } diff --git a/cli-tcpfwd.c b/cli-tcpfwd.c index 747d8db..4dbef01 100644 --- a/cli-tcpfwd.c +++ b/cli-tcpfwd.c @@ -89,7 +89,7 @@ static int cli_localtcp(unsigned int listenport, const char* remoteaddr, remoteport)); tcpinfo = (struct TCPListener*)m_malloc(sizeof(struct TCPListener*)); - tcpinfo->sendaddr = remoteaddr; + tcpinfo->sendaddr = m_strdup(remoteaddr); tcpinfo->sendport = remoteport; tcpinfo->listenport = listenport; tcpinfo->chantype = &cli_chan_tcplocal; diff --git a/common-channel.c b/common-channel.c index 41a7208..a4cc44b 100644 --- a/common-channel.c +++ b/common-channel.c @@ -40,7 +40,7 @@ static void send_msg_channel_open_failure(unsigned int remotechan, int reason, static void send_msg_channel_open_confirmation(struct Channel* channel, unsigned int recvwindow, unsigned int recvmaxpacket); -static void writechannel(struct Channel *channel); +static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf); static void send_msg_channel_window_adjust(struct Channel *channel, unsigned int incr); static void send_msg_channel_data(struct Channel *channel, int isextended, @@ -151,6 +151,7 @@ struct Channel* newchannel(unsigned int remotechan, newchan->writebuf = cbuf_new(RECV_MAXWINDOW); newchan->extrabuf = NULL; /* The user code can set it up */ newchan->recvwindow = RECV_MAXWINDOW; + newchan->recvdonelen = 0; newchan->recvmaxpacket = RECV_MAXPACKET; ses.channels[i] = newchan; @@ -220,7 +221,13 @@ void channelio(fd_set *readfd, fd_set *writefd) { continue; /* Important not to use the channel after checkinitdone(), as it may be NULL */ } - writechannel(channel); + writechannel(channel, channel->infd, channel->writebuf); + } + + /* stderr for client mode */ + if (channel->extrabuf != NULL + && channel->errfd >= 0 && FD_ISSET(channel->errfd, writefd)) { + writechannel(channel, channel->errfd, channel->extrabuf); } /* now handle any of the channel-closing type stuff */ @@ -350,33 +357,30 @@ static void send_msg_channel_eof(struct Channel *channel) { /* Called to write data out to the local side of the channel. * Only called when we know we can write to a channel, writes as much as * possible */ -static void writechannel(struct Channel* channel) { +static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { int len, maxlen; - circbuffer *cbuf; TRACE(("enter writechannel")); - cbuf = channel->writebuf; maxlen = cbuf_readlen(cbuf); - TRACE(("maxlen = %d", maxlen)); - /* Write the data out */ - len = write(channel->infd, cbuf_readptr(cbuf, maxlen), maxlen); + len = write(fd, cbuf_readptr(cbuf, maxlen), maxlen); if (len <= 0) { if (len < 0 && errno != EINTR) { - /* no more to write */ + /* no more to write - we close it even if the fd was stderr, since + * that's a nasty failure too */ closeinfd(channel); } TRACE(("leave writechannel: len <= 0")); return; } - TRACE(("len = %d", len)); cbuf_incrread(cbuf, len); + channel->recvdonelen += len; - if (len == maxlen && channel->recveof) { + if (fd == channel->infd && len == maxlen && channel->recveof) { /* Check if we're closing up */ closeinfd(channel); return; @@ -385,12 +389,17 @@ static void writechannel(struct Channel* channel) { } /* Window adjust handling */ - if (channel->recvwindow < (RECV_MAXWINDOW - RECV_WINDOWEXTEND)) { + if (channel->recvdonelen >= RECV_WINDOWEXTEND) { /* Set it back to max window */ - send_msg_channel_window_adjust(channel, RECV_MAXWINDOW - - channel->recvwindow); - channel->recvwindow = RECV_MAXWINDOW; + send_msg_channel_window_adjust(channel, channel->recvdonelen); + channel->recvwindow += channel->recvdonelen; + channel->recvdonelen = 0; } + + assert(channel->recvwindow <= RECV_MAXWINDOW); + assert(channel->recvwindow <= cbuf_getavail(channel->writebuf)); + assert(channel->extrabuf == NULL || + channel->recvwindow <= cbuf_getavail(channel->extrabuf)); TRACE(("leave writechannel")); @@ -424,6 +433,8 @@ void setchannelfds(fd_set *readfd, fd_set *writefd) { /* For checking FD status (ie closure etc) - we don't actually * read data from infd */ + TRACE(("infd = %d, outfd %d, bufused %d", channel->infd, channel->outfd, + cbuf_getused(channel->writebuf) )); if (channel->infd >= 0 && channel->infd != channel->outfd) { FD_SET(channel->infd, readfd); } @@ -435,12 +446,10 @@ void setchannelfds(fd_set *readfd, fd_set *writefd) { FD_SET(channel->infd, writefd); } - /* if (channel->extrabuf != NULL && channel->errfd >= 0 - && cbuf_getavail(channel->extrabuf) > 0 ) { + && cbuf_getused(channel->extrabuf) > 0 ) { FD_SET(channel->errfd, writefd); } - */ } /* foreach channel */ @@ -468,7 +477,9 @@ void recv_msg_channel_eof() { } channel->recveof = 1; - if (cbuf_getused(channel->writebuf) == 0) { + if (cbuf_getused(channel->writebuf) == 0 + && (channel->extrabuf == NULL + || cbuf_getused(channel->extrabuf) == 0)) { closeinfd(channel); } @@ -678,36 +689,34 @@ void common_recv_msg_channel_data(struct Channel *channel, int fd, datalen = buf_getint(ses.payload); - TRACE(("datalen = %d", datalen)); - /* if the client is going to send us more data than we've allocated, then - * it has ignored the windowsize, so we "MAY ignore all extra data" */ maxdata = cbuf_getavail(cbuf); - TRACE(("maxdata = %d", maxdata)); + + /* Whilst the spec says we "MAY ignore data past the end" this could + * lead to corrupted file transfers etc (chunks missed etc). It's better to + * just die horribly */ if (datalen > maxdata) { - TRACE(("Warning: recv_msg_channel_data: extra data past window")); - datalen = maxdata; + dropbear_exit("Oversized packet"); } - /* We may have to run throught twice, if the buffer wraps around. Can't * just "leave it for next time" like with writechannel, since this * is payload data */ len = datalen; while (len > 0) { buflen = cbuf_writelen(cbuf); - TRACE(("buflen = %d", buflen)); buflen = MIN(buflen, len); - TRACE(("buflenmin = %d", buflen)); memcpy(cbuf_writeptr(cbuf, buflen), buf_getptr(ses.payload, buflen), buflen); cbuf_incrwrite(cbuf, buflen); + buf_incrpos(ses.payload, buflen); len -= buflen; - TRACE(("len = %d", buflen)); } + assert(channel->recvwindow > datalen); channel->recvwindow -= datalen; + assert(channel->recvwindow <= RECV_MAXWINDOW); TRACE(("leave recv_msg_channel_data")); } diff --git a/common-session.c b/common-session.c index 1888eb7..f220753 100644 --- a/common-session.c +++ b/common-session.c @@ -35,6 +35,8 @@ #include "channel.h" #include "atomicio.h" +static void checktimeouts(); +static int ident_readln(int fd, char* buf, int count); struct sshsession ses; /* GLOBAL */ @@ -46,8 +48,6 @@ int sessinitdone = 0; /* GLOBAL */ int exitflag = 0; /* GLOBAL */ -static void checktimeouts(); -static int ident_readln(int fd, char* buf, int count); /* called only at the start of a session, set up initial state */ void common_session_init(int sock, char* remotehost) { diff --git a/dbutil.c b/dbutil.c index 0896bc9..a1ec3ed 100644 --- a/dbutil.c +++ b/dbutil.c @@ -111,7 +111,7 @@ static void generic_dropbear_exit(int exitcode, const char* format, exit(exitcode); } -static void generic_dropbear_log(int priority, const char* format, +static void generic_dropbear_log(int UNUSED(priority), const char* format, va_list param) { char printbuf[1024]; @@ -146,7 +146,6 @@ void dropbear_trace(const char* format, ...) { fprintf(stderr, "TRACE: "); vfprintf(stderr, format, param); fprintf(stderr, "\n"); - fflush(stderr); va_end(param); } #endif /* DEBUG_TRACE */ diff --git a/includes.h b/includes.h index b37422b..ceeb561 100644 --- a/includes.h +++ b/includes.h @@ -128,4 +128,14 @@ typedef u_int16_t uint16_t; #define LOG_AUTHPRIV LOG_AUTH #endif +/* so we can avoid warnings about unused params (ie in signal handlers etc) */ +#ifdef UNUSED +#elif defined(__GNUC__) +# define UNUSED(x) UNUSED_ ## x __attribute__((unused)) +#elif defined(__LCLINT__) +# define UNUSED(x) /*@unused@*/ x +#else +# define UNUSED(x) x +#endif + #endif /* _INCLUDES_H_ */ diff --git a/session.h b/session.h index 6afca85..40889a1 100644 --- a/session.h +++ b/session.h @@ -43,7 +43,6 @@ extern int exitflag; void common_session_init(int sock, char* remotehost); void session_loop(void(*loophandler)()); void common_session_cleanup(); -void checktimeouts(); void session_identification(); @@ -54,8 +53,6 @@ void svr_dropbear_log(int priority, const char* format, va_list param); /* Client */ void cli_session(int sock, char *remotehost); -void cli_dropbear_exit(int exitcode, const char* format, va_list param); -void cli_dropbear_log(int priority, const char* format, va_list param); void cli_session_cleanup(); void cleantext(unsigned char* dirtytext); @@ -220,6 +217,8 @@ struct clientsession { int lastauthtype; /* either AUTH_TYPE_PUBKEY or AUTH_TYPE_PASSWORD, for the last type of auth we tried */ struct PubkeyList *lastpubkey; + + int retval; /* What the command exit status was - we emulate it */ #if 0 TODO struct AgentkeyList *agentkeys; /* Keys to use for public-key auth */ diff --git a/svr-agentfwd.c b/svr-agentfwd.c index 4e9aa56..2674138 100644 --- a/svr-agentfwd.c +++ b/svr-agentfwd.c @@ -97,7 +97,7 @@ fail: /* accepts a connection on the forwarded socket and opens a new channel for it * back to the client */ /* returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ -static void agentaccept(struct Listener * listener, int sock) { +static void agentaccept(struct Listener *UNUSED(listener), int sock) { int fd; diff --git a/svr-chansession.c b/svr-chansession.c index d56fb2c..8c9fa3b 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -55,6 +55,10 @@ static int newchansess(struct Channel *channel); static void chansessionrequest(struct Channel *channel); static void send_exitsignalstatus(struct Channel *channel); +static void send_msg_chansess_exitstatus(struct Channel * channel, + struct ChanSess * chansess); +static void send_msg_chansess_exitsignal(struct Channel * channel, + struct ChanSess * chansess); static int sesscheckclose(struct Channel *channel); static void get_termmodes(struct ChanSess *chansess); @@ -68,7 +72,7 @@ static int sesscheckclose(struct Channel *channel) { } /* handler for childs exiting, store the state for return to the client */ -static void sesssigchild_handler(int dummy) { +static void sesssigchild_handler(int UNUSED(dummy)) { int status; pid_t pid; @@ -498,7 +502,9 @@ static int sessionpty(struct ChanSess * chansess) { } /* allocate the pty */ - assert(chansess->master == -1); /* haven't already got one */ + if (chansess->master != -1) { + dropbear_exit("multiple pty requests"); + } if (pty_allocate(&chansess->master, &chansess->slave, namebuf, 64) == 0) { TRACE(("leave sessionpty: failed to allocate pty")); return DROPBEAR_FAILURE; diff --git a/svr-main.c b/svr-main.c index 55c4106..7dc3722 100644 --- a/svr-main.c +++ b/svr-main.c @@ -123,7 +123,6 @@ void main_noinetd() { pid_t childpid; int childpipe[2]; - struct sigaction sa_chld; /* fork */ if (svr_opts.forkbg) { int closefds = 0; @@ -303,7 +302,7 @@ void main_noinetd() { /* catch + reap zombie children */ -static void sigchld_handler(int fish) { +static void sigchld_handler(int UNUSED(unused)) { struct sigaction sa_chld; while(waitpid(-1, NULL, WNOHANG) > 0); @@ -316,14 +315,14 @@ static void sigchld_handler(int fish) { } /* catch any segvs */ -static void sigsegv_handler(int fish) { +static void sigsegv_handler(int UNUSED(unused)) { fprintf(stderr, "Aiee, segfault! You should probably report " "this as a bug to the developer\n"); exit(EXIT_FAILURE); } /* catch ctrl-c or sigterm */ -static void sigintterm_handler(int fish) { +static void sigintterm_handler(int UNUSED(unused)) { exitflag = 1; } diff --git a/tcpfwd.h b/tcpfwd.h index 569d59d..504a8d3 100644 --- a/tcpfwd.h +++ b/tcpfwd.h @@ -47,7 +47,7 @@ struct TCPListener { /* A link in a list of forwards */ struct TCPFwdList { - char* connectaddr; + const unsigned char* connectaddr; unsigned int connectport; unsigned int listenport; struct TCPFwdList * next; @@ -60,6 +60,7 @@ extern const struct ChanType svr_chan_tcpdirect; /* Client */ void setup_localtcp(); +void setup_remotetcp(); extern const struct ChanType cli_chan_tcpremote; /* Common */ -- cgit v1.2.3 From 69282617fd9c40395d57edf0ed6587def6131de7 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Fri, 27 Aug 2004 14:39:01 +0000 Subject: merge of 50be59810e462f9f44f55e421227d6aa0b31982b and 69b007796063cb5f042be7cca2d479e90db869c3 --HG-- extra : convert_revision : 5d0dfaa8c0ee6c728a3b4f0f0855199ba729db83 --- cli-chansession.c | 6 ++++++ cli-main.c | 4 ++++ common-channel.c | 51 ++++++++++++++++++++++++++++++++------------------- dbutil.c | 10 ++++++++++ dbutil.h | 1 + debug.h | 8 +++++--- svr-agentfwd.c | 4 +--- svr-chansession.c | 13 +++++-------- svr-x11fwd.c | 4 +--- 9 files changed, 65 insertions(+), 36 deletions(-) (limited to 'svr-chansession.c') diff --git a/cli-chansession.c b/cli-chansession.c index 50226dd..35be671 100644 --- a/cli-chansession.c +++ b/cli-chansession.c @@ -341,8 +341,14 @@ static int cli_initchansess(struct Channel *channel) { channel->infd = STDOUT_FILENO; + setnonblocking(STDOUT_FILENO); + channel->outfd = STDIN_FILENO; + setnonblocking(STDIN_FILENO); + channel->errfd = STDERR_FILENO; + setnonblocking(STDERR_FILENO); + channel->extrabuf = cbuf_new(RECV_MAXWINDOW); if (cli_opts.wantpty) { diff --git a/cli-main.c b/cli-main.c index 34a1e42..def2c72 100644 --- a/cli-main.c +++ b/cli-main.c @@ -52,6 +52,10 @@ int main(int argc, char ** argv) { TRACE(("user='%s' host='%s' port='%s'", cli_opts.username, cli_opts.remotehost, cli_opts.remoteport)); + if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { + dropbear_exit("signal() error"); + } + sock = connect_remote(cli_opts.remotehost, cli_opts.remoteport, 0, &error); diff --git a/common-channel.c b/common-channel.c index a4cc44b..d30528c 100644 --- a/common-channel.c +++ b/common-channel.c @@ -192,7 +192,8 @@ void channelio(fd_set *readfd, fd_set *writefd) { } /* read from program/pipe stderr */ - if (channel->errfd >= 0 && FD_ISSET(channel->errfd, readfd)) { + if (channel->extrabuf == NULL && + channel->errfd >= 0 && FD_ISSET(channel->errfd, readfd)) { send_msg_channel_data(channel, 1, SSH_EXTENDED_DATA_STDERR); } @@ -245,6 +246,14 @@ void channelio(fd_set *readfd, fd_set *writefd) { /* do all the EOF/close type stuff checking for a channel */ static void checkclose(struct Channel *channel) { + TRACE(("checkclose: infd %d, outfd %d, errfd %d, sentclosed %d, recvclosed %d", + channel->infd, channel->outfd, + channel->errfd, channel->sentclosed, channel->recvclosed)); + TRACE(("writebuf %d extrabuf %s extrabuf %d", + cbuf_getused(channel->writebuf), + channel->writebuf, + channel->writebuf ? 0 : cbuf_getused(channel->extrabuf))); + if (!channel->sentclosed) { /* check for exited - currently only used for server sessions, @@ -257,13 +266,13 @@ static void checkclose(struct Channel *channel) { if (!channel->senteof && channel->outfd == FD_CLOSED - && channel->errfd == FD_CLOSED) { + && (channel->extrabuf != NULL || channel->errfd == FD_CLOSED)) { send_msg_channel_eof(channel); } if (channel->infd == FD_CLOSED - && channel->outfd == FD_CLOSED - && channel->errfd == FD_CLOSED) { + && channel->outfd == FD_CLOSED + && (channel->extrabuf != NULL || channel->errfd == FD_CLOSED)) { send_msg_channel_close(channel); } } @@ -383,9 +392,8 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { if (fd == channel->infd && len == maxlen && channel->recveof) { /* Check if we're closing up */ closeinfd(channel); - return; TRACE(("leave writechannel: recveof set")); - + return; } /* Window adjust handling */ @@ -433,7 +441,9 @@ void setchannelfds(fd_set *readfd, fd_set *writefd) { /* For checking FD status (ie closure etc) - we don't actually * read data from infd */ - TRACE(("infd = %d, outfd %d, bufused %d", channel->infd, channel->outfd, + TRACE(("infd = %d, outfd %d, errfd %d, bufused %d", + channel->infd, channel->outfd, + channel->errfd, cbuf_getused(channel->writebuf) )); if (channel->infd >= 0 && channel->infd != channel->outfd) { FD_SET(channel->infd, readfd); @@ -534,9 +544,8 @@ static void removechannel(struct Channel * channel) { * yet (ie they were shutdown etc */ close(channel->infd); close(channel->outfd); - if (channel->errfd >= 0) { - close(channel->errfd); - } + close(channel->errfd); + channel->typedata = NULL; deletechannel(channel); @@ -619,16 +628,19 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, } /* read the data */ + TRACE(("maxlen %d", maxlen)); buf = buf_new(maxlen); + TRACE(("buf pos %d data %x", buf->pos, buf->data)); len = read(fd, buf_getwriteptr(buf, maxlen), maxlen); if (len <= 0) { /* on error/eof, send eof */ if (len == 0 || errno != EINTR) { closeoutfd(channel, fd); - TRACE(("leave send_msg_channel_data: read err %d", channel->index)); } buf_free(buf); buf = NULL; + TRACE(("leave send_msg_channel_data: read err or EOF for fd %d", + channel->index)); return; } buf_incrlen(buf, len); @@ -714,7 +726,7 @@ void common_recv_msg_channel_data(struct Channel *channel, int fd, len -= buflen; } - assert(channel->recvwindow > datalen); + assert(channel->recvwindow >= datalen); channel->recvwindow -= datalen; assert(channel->recvwindow <= RECV_MAXWINDOW); @@ -927,10 +939,7 @@ int send_msg_channel_open_init(int fd, const struct ChanType *type) { } /* set fd non-blocking */ - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { - TRACE(("leave send_msg_channel_open_init() - FAILED in fcntl()")); - return DROPBEAR_FAILURE; - } + setnonblocking(fd); chan->infd = chan->outfd = fd; ses.maxfd = MAX(ses.maxfd, fd); @@ -1034,15 +1043,19 @@ static void closechanfd(struct Channel *channel, int fd, int how) { closein = closeout = 1; } - if (closeout && fd == channel->errfd) { - channel->errfd = FD_CLOSED; - } if (closeout && fd == channel->outfd) { channel->outfd = FD_CLOSED; } + if (closeout && (channel->extrabuf == NULL) && (fd == channel->errfd)) { + channel->errfd = FD_CLOSED; + } + if (closein && fd == channel->infd) { channel->infd = FD_CLOSED; } + if (closein && (channel->extrabuf != NULL) && (fd == channel->errfd)) { + channel->errfd = FD_CLOSED; + } } #endif /* USING_LISTENERS */ diff --git a/dbutil.c b/dbutil.c index a1ec3ed..c126a2f 100644 --- a/dbutil.c +++ b/dbutil.c @@ -595,3 +595,13 @@ void m_burn(void *data, unsigned int len) { } } + +void setnonblocking(int fd) { + + TRACE(("setnonblocking: %d", fd)); + + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { + dropbear_exit("Couldn't set nonblocking"); + } + TRACE(("leave setnonblocking")); +} diff --git a/dbutil.h b/dbutil.h index 0409e36..6363f70 100644 --- a/dbutil.h +++ b/dbutil.h @@ -61,6 +61,7 @@ void * m_realloc(void* ptr, size_t size); #define m_free(X) __m_free(X); (X) = NULL; void __m_free(void* ptr); void m_burn(void* data, unsigned int len); +void setnonblocking(int fd); /* Used to force mp_ints to be initialised */ #define DEF_MP_INT(X) mp_int X = {0, 0, 0, NULL} diff --git a/debug.h b/debug.h index cc161dc..37f51ac 100644 --- a/debug.h +++ b/debug.h @@ -33,12 +33,13 @@ * etc. Don't use this normally, it might cause problems */ /* #define DEBUG_VALGRIND */ -/* Define this to compile in trace debugging printf()s. You'll need to - * run programs with "-v" to turn this on. +/* Define this to compile in trace debugging printf()s. + * You'll need to run programs with "-v" to turn this on. + * * Caution: Don't use this in an unfriendly environment (ie unfirewalled), * since the printing may not sanitise strings etc. This will add a reasonable * amount to your executable size. */ - #define DEBUG_TRACE +/* #define DEBUG_TRACE */ /* All functions writing to the cleartext payload buffer call * CHECKCLEARTOWRITE() before writing. This is only really useful if you're @@ -49,6 +50,7 @@ /* Define this, compile with -pg and set GMON_OUT_PREFIX=gmon to get gmon * output when Dropbear forks. This will allow it gprof to be used. * It's useful to run dropbear -F, so you don't fork as much */ +/* (This is Linux specific) */ /*#define DEBUG_FORKGPROF*/ /* A couple of flags, not usually useful, and mightn't do anything */ diff --git a/svr-agentfwd.c b/svr-agentfwd.c index 2674138..60c23f7 100644 --- a/svr-agentfwd.c +++ b/svr-agentfwd.c @@ -73,9 +73,7 @@ int agentreq(struct ChanSess * chansess) { } /* set non-blocking */ - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { - goto fail; - } + setnonblocking(fd); /* pass if off to listener */ chansess->agentlistener = new_listener( &fd, 1, 0, chansess, diff --git a/svr-chansession.c b/svr-chansession.c index 8c9fa3b..be6678d 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -651,11 +651,10 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { ses.maxfd = MAX(ses.maxfd, channel->outfd); ses.maxfd = MAX(ses.maxfd, channel->errfd); - if ((fcntl(channel->outfd, F_SETFL, O_NONBLOCK) < 0) || - (fcntl(channel->infd, F_SETFL, O_NONBLOCK) < 0) || - (fcntl(channel->errfd, F_SETFL, O_NONBLOCK) < 0)) { - dropbear_exit("Couldn't set nonblocking"); - } + setnonblocking(channel->outfd); + setnonblocking(channel->infd); + setnonblocking(channel->errfd); + } #undef FDIN #undef FDOUT @@ -761,9 +760,7 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { /* don't need to set stderr here */ ses.maxfd = MAX(ses.maxfd, chansess->master); - if (fcntl(chansess->master, F_SETFL, O_NONBLOCK) < 0) { - dropbear_exit("Couldn't set nonblocking"); - } + setnonblocking(chansess->master); } diff --git a/svr-x11fwd.c b/svr-x11fwd.c index 75e94b8..45c9060 100644 --- a/svr-x11fwd.c +++ b/svr-x11fwd.c @@ -75,9 +75,7 @@ int x11req(struct ChanSess * chansess) { } /* set non-blocking */ - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { - goto fail; - } + setnonblocking(fd); /* listener code will handle the socket now. * No cleanup handler needed, since listener_remove only happens -- cgit v1.2.3 From cb0657bdf3491bb86de929d86409791d306b5f64 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 31 Aug 2004 08:52:41 +0000 Subject: WCOREDUMP finally --HG-- extra : convert_revision : 12aa4fdb328febedfd3fd50d1dae783d8a0dab14 --- svr-chansession.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index be6678d..e9a3c87 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -93,7 +93,7 @@ static void sesssigchild_handler(int UNUSED(dummy)) { } if (WIFSIGNALED(status)) { chansess->exitsignal = WTERMSIG(status); -#ifndef AIX +#if !defined(AIX) && defined(WCOREDUMP) chansess->exitcore = WCOREDUMP(status); #else chansess->exitcore = 0; -- cgit v1.2.3 From c2af67efd3e75fcb33eefb5992d1d053de282ae5 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 21 Sep 2004 10:08:21 +0000 Subject: propagate of 82bb923d0154750ef716b66b498561f882891946 and f51a272341ee12268fe7028bc2f2bad66c603069 from branch 'matt.dbclient.work' to 'matt.dbclient.rez' --HG-- branch : private-rez extra : convert_revision : 440ee4177122c8a873ebf8984a90f96a9bd8a56a --- chansession.h | 13 +++++--- session.h | 5 +++ svr-chansession.c | 92 ++++++++++++++++++++++++++++++++++++------------------- 3 files changed, 74 insertions(+), 36 deletions(-) (limited to 'svr-chansession.c') diff --git a/chansession.h b/chansession.h index 84ca55a..213c285 100644 --- a/chansession.h +++ b/chansession.h @@ -29,6 +29,14 @@ #include "channel.h" #include "listener.h" +struct exitinfo { + + int exitpid; /* -1 if not exited */ + int exitstatus; + int exitsignal; + int exitcore; +}; + struct ChanSess { unsigned char * cmd; /* command to exec */ @@ -41,10 +49,7 @@ struct ChanSess { unsigned char * term; /* exit details */ - int exited; - int exitstatus; - int exitsignal; - unsigned char exitcore; + struct exitinfo exit; #ifndef DISABLE_X11FWD struct Listener * x11listener; diff --git a/session.h b/session.h index 40889a1..9142882 100644 --- a/session.h +++ b/session.h @@ -36,6 +36,7 @@ #include "listener.h" #include "packet.h" #include "tcpfwd.h" +#include "chansession.h" extern int sessinitdone; /* Is set to 0 somewhere */ extern int exitflag; @@ -176,6 +177,10 @@ struct serversession { struct ChildPid * childpids; /* array of mappings childpid<->channel */ unsigned int childpidsize; + /* Used to avoid a race in the exit returncode handling - see + * svr-chansession.c for details */ + struct exitinfo lastexit; + }; typedef enum { diff --git a/svr-chansession.c b/svr-chansession.c index e9a3c87..c3356d6 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -68,17 +68,24 @@ extern char** environ; static int sesscheckclose(struct Channel *channel) { struct ChanSess *chansess = (struct ChanSess*)channel->typedata; - return chansess->exited; + return chansess->exit.exitpid >= 0; } -/* handler for childs exiting, store the state for return to the client */ +/* Handler for childs exiting, store the state for return to the client */ + +/* There's a particular race we have to watch out for: if the forked child + * executes, exits, and this signal-handler is called, all before the parent + * gets to run, then the childpids[] array won't have the pid in it. Hence we + * use the svr_ses.lastexit struct to hold the exit, which is then compared by + * the parent when it runs. This work correctly at least in the case of a + * single shell spawned (ie the usual case) */ static void sesssigchild_handler(int UNUSED(dummy)) { int status; pid_t pid; unsigned int i; - struct ChanSess * chansess; struct sigaction sa_chld; + struct exitinfo *exit = NULL; TRACE(("enter sigchld handler")); while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { @@ -86,25 +93,36 @@ static void sesssigchild_handler(int UNUSED(dummy)) { for (i = 0; i < svr_ses.childpidsize; i++) { if (svr_ses.childpids[i].pid == pid) { - chansess = svr_ses.childpids[i].chansess; - chansess->exited = 1; - if (WIFEXITED(status)) { - chansess->exitstatus = WEXITSTATUS(status); - } - if (WIFSIGNALED(status)) { - chansess->exitsignal = WTERMSIG(status); + exit = &svr_ses.childpids[i].chansess->exit; + break; + } + } + + /* If the pid wasn't matched, then we might have hit the race mentioned + * above. So we just store the info for the parent to deal with */ + if (i == svr_ses.childpidsize) { + exit = &svr_ses.lastexit; + } + + exit->exitpid = pid; + if (WIFEXITED(status)) { + exit->exitstatus = WEXITSTATUS(status); + } + if (WIFSIGNALED(status)) { + exit->exitsignal = WTERMSIG(status); #if !defined(AIX) && defined(WCOREDUMP) - chansess->exitcore = WCOREDUMP(status); + exit->exitcore = WCOREDUMP(status); #else - chansess->exitcore = 0; + exit->exitcore = 0; #endif - } else { - /* we use this to determine how pid exited */ - chansess->exitsignal = -1; - } - } + } else { + /* we use this to determine how pid exited */ + exit->exitsignal = -1; } + exit = NULL; } + + sa_chld.sa_handler = sesssigchild_handler; sa_chld.sa_flags = SA_NOCLDSTOP; sigaction(SIGCHLD, &sa_chld, NULL); @@ -117,8 +135,8 @@ static void send_exitsignalstatus(struct Channel *channel) { struct ChanSess *chansess = (struct ChanSess*)channel->typedata; - if (chansess->exited) { - if (chansess->exitsignal > 0) { + if (chansess->exit.exitpid >= 0) { + if (chansess->exit.exitsignal > 0) { send_msg_chansess_exitsignal(channel, chansess); } else { send_msg_chansess_exitstatus(channel, chansess); @@ -130,8 +148,8 @@ static void send_exitsignalstatus(struct Channel *channel) { static void send_msg_chansess_exitstatus(struct Channel * channel, struct ChanSess * chansess) { - assert(chansess->exited); - assert(chansess->exitsignal == -1); + assert(chansess->exit.exitpid != -1); + assert(chansess->exit.exitsignal == -1); CHECKCLEARTOWRITE(); @@ -139,7 +157,7 @@ static void send_msg_chansess_exitstatus(struct Channel * channel, buf_putint(ses.writepayload, channel->remotechan); buf_putstring(ses.writepayload, "exit-status", 11); buf_putbyte(ses.writepayload, 0); /* boolean FALSE */ - buf_putint(ses.writepayload, chansess->exitstatus); + buf_putint(ses.writepayload, chansess->exit.exitstatus); encrypt_packet(); @@ -152,15 +170,15 @@ static void send_msg_chansess_exitsignal(struct Channel * channel, int i; char* signame = NULL; - assert(chansess->exited); - assert(chansess->exitsignal > 0); + assert(chansess->exit.exitpid != -1); + assert(chansess->exit.exitsignal > 0); CHECKCLEARTOWRITE(); /* we check that we can match a signal name, otherwise * don't send anything */ for (i = 0; signames[i].name != NULL; i++) { - if (signames[i].signal == chansess->exitsignal) { + if (signames[i].signal == chansess->exit.exitsignal) { signame = signames[i].name; break; } @@ -175,7 +193,7 @@ static void send_msg_chansess_exitsignal(struct Channel * channel, buf_putstring(ses.writepayload, "exit-signal", 11); buf_putbyte(ses.writepayload, 0); /* boolean FALSE */ buf_putstring(ses.writepayload, signame, strlen(signame)); - buf_putbyte(ses.writepayload, chansess->exitcore); + buf_putbyte(ses.writepayload, chansess->exit.exitcore); buf_putstring(ses.writepayload, "", 0); /* error msg */ buf_putstring(ses.writepayload, "", 0); /* lang */ @@ -199,7 +217,7 @@ static int newchansess(struct Channel *channel) { chansess->tty = NULL; chansess->term = NULL; - chansess->exited = 0; + chansess->exit.exitpid = -1; channel->typedata = chansess; @@ -263,7 +281,7 @@ static void closechansess(struct Channel *channel) { if (svr_ses.childpids[i].chansess == chansess) { assert(svr_ses.childpids[i].pid > 0); TRACE(("closing pid %d", svr_ses.childpids[i].pid)); - TRACE(("exited = %d", chansess->exited)); + TRACE(("exitpid = %d", chansess->exit.exitpid)); svr_ses.childpids[i].pid = -1; svr_ses.childpids[i].chansess = NULL; } @@ -592,6 +610,7 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { int outfds[2]; int errfds[2]; pid_t pid; + unsigned int i; TRACE(("enter noptycommand")); @@ -635,12 +654,21 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { TRACE(("continue noptycommand: parent")); chansess->pid = pid; - /* add a child pid - Beware: there's a race between this, and the - * exec() called from the child. If the child finishes before we've - * done this (ie if it was a shell builtin and fast), we won't return a - * proper return code. For now, we ignore this case. */ addchildpid(chansess, pid); + if (svr_ses.lastexit.exitpid != -1) { + /* The child probably exited and the signal handler triggered + * possibly before we got around to adding the childpid. So we fill + * out it's data manually */ + for (i = 0; i < svr_ses.childpidsize; i++) { + if (svr_ses.childpids[i].pid == pid) { + svr_ses.childpids[i].chansess->exit = svr_ses.lastexit; + svr_ses.lastexit.exitpid = -1; + } + } + } + + close(infds[FDIN]); close(outfds[FDOUT]); close(errfds[FDOUT]); -- cgit v1.2.3 From 5c6e8b833eaa6ff63cf1394222abcbb48c78a400 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Sun, 17 Oct 2004 08:19:47 +0000 Subject: Initialise the "lastexit" variable so that we don't get session channels terminating prematurely. --HG-- extra : convert_revision : 5cb8d5a7c8fe405fa4d49906f66a61590ae4a087 --- svr-chansession.c | 1 + 1 file changed, 1 insertion(+) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index c3356d6..f1e9c88 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -960,6 +960,7 @@ void svr_chansessinitialise() { svr_ses.childpids[0].pid = -1; /* unused */ svr_ses.childpids[0].chansess = NULL; svr_ses.childpidsize = 1; + svr_ses.lastexit.exitpid = -1; /* Nothing has exited yet */ sa_chld.sa_handler = sesssigchild_handler; sa_chld.sa_flags = SA_NOCLDSTOP; if (sigaction(SIGCHLD, &sa_chld, NULL) < 0) { -- cgit v1.2.3 From 8c1a429c446d00ad2030814e9f3d7afccc08ddf8 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Sun, 2 Jan 2005 20:25:56 +0000 Subject: Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place --HG-- extra : convert_revision : d928bc851e32be7bd429bf7504b148c0e4bf7e2f --- buffer.c | 4 +- circbuffer.c | 4 +- cli-algo.c | 2 +- cli-auth.c | 30 ++++++------ cli-authpasswd.c | 4 +- cli-authpubkey.c | 32 ++++++------- cli-channel.c | 8 ++-- cli-chansession.c | 34 +++++++------- cli-kex.c | 34 +++++++------- cli-main.c | 2 +- cli-runopts.c | 22 ++++----- cli-service.c | 10 ++-- cli-session.c | 22 ++++----- cli-tcpfwd.c | 22 ++++----- common-algo.c | 2 +- common-channel.c | 136 +++++++++++++++++++++++++++--------------------------- common-kex.c | 64 ++++++++++++------------- common-session.c | 28 +++++------ dbutil.c | 42 ++++++++--------- debug.h | 4 +- dropbearkey.c | 4 +- dss.c | 24 +++++----- listener.c | 4 +- packet.c | 34 +++++++------- process-packet.c | 14 +++--- queue.c | 6 +-- rsa.c | 50 ++++++++++---------- signkey.c | 36 +++++++-------- svr-agentfwd.c | 2 +- svr-algo.c | 2 +- svr-auth.c | 44 +++++++++--------- svr-authpam.c | 20 ++++---- svr-authpubkey.c | 38 +++++++-------- svr-chansession.c | 54 +++++++++++----------- svr-kex.c | 8 ++-- svr-main.c | 6 +-- svr-runopts.c | 4 +- svr-service.c | 8 ++-- svr-tcpfwd.c | 38 +++++++-------- svr-x11fwd.c | 2 +- tcp-accept.c | 8 ++-- 41 files changed, 456 insertions(+), 456 deletions(-) (limited to 'svr-chansession.c') diff --git a/buffer.c b/buffer.c index 793eee1..dff861f 100644 --- a/buffer.c +++ b/buffer.c @@ -258,7 +258,7 @@ void buf_putbytes(buffer *buf, const unsigned char *bytes, unsigned int len) { void buf_putmpint(buffer* buf, mp_int * mp) { unsigned int len, pad = 0; - TRACE(("enter buf_putmpint")); + TRACE(("enter buf_putmpint")) assert(mp != NULL); @@ -294,7 +294,7 @@ void buf_putmpint(buffer* buf, mp_int * mp) { buf_incrwritepos(buf, len-pad); } - TRACE(("leave buf_putmpint")); + TRACE(("leave buf_putmpint")) } /* Retrieve an mp_int from the buffer. diff --git a/circbuffer.c b/circbuffer.c index a2edcbb..6dc9179 100644 --- a/circbuffer.c +++ b/circbuffer.c @@ -70,7 +70,7 @@ unsigned int cbuf_readlen(circbuffer *cbuf) { assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); if (cbuf->used == 0) { - TRACE(("cbuf_readlen: unused buffer")); + TRACE(("cbuf_readlen: unused buffer")) return 0; } @@ -88,7 +88,7 @@ unsigned int cbuf_writelen(circbuffer *cbuf) { assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); if (cbuf->used == cbuf->size) { - TRACE(("cbuf_writelen: full buffer")); + TRACE(("cbuf_writelen: full buffer")) return 0; /* full */ } diff --git a/cli-algo.c b/cli-algo.c index 5edd6a1..ec3a1ff 100644 --- a/cli-algo.c +++ b/cli-algo.c @@ -46,7 +46,7 @@ algo_type * cli_buf_match_algo(buffer* buf, algo_type localalgos[], /* get the comma-separated list from the buffer ie "algo1,algo2,algo3" */ algolist = buf_getstring(buf, &len); - TRACE(("cli_buf_match_algo: %s", algolist)); + TRACE(("cli_buf_match_algo: %s", algolist)) if (len > MAX_PROPOSED_ALGO*(MAX_NAME_LEN+1)) { goto out; /* just a sanity check, no other use */ } diff --git a/cli-auth.c b/cli-auth.c index 9dedfa8..dfd9bbb 100644 --- a/cli-auth.c +++ b/cli-auth.c @@ -42,7 +42,7 @@ void cli_authinitialise() { /* Send a "none" auth request to get available methods */ void cli_auth_getmethods() { - TRACE(("enter cli_auth_getmethods")); + TRACE(("enter cli_auth_getmethods")) CHECKCLEARTOWRITE(); @@ -54,7 +54,7 @@ void cli_auth_getmethods() { buf_putstring(ses.writepayload, "none", 4); /* 'none' method */ encrypt_packet(); - TRACE(("leave cli_auth_getmethods")); + TRACE(("leave cli_auth_getmethods")) } @@ -64,9 +64,9 @@ void recv_msg_userauth_banner() { unsigned int bannerlen; unsigned int i, linecount; - TRACE(("enter recv_msg_userauth_banner")); + TRACE(("enter recv_msg_userauth_banner")) if (ses.authstate.authdone) { - TRACE(("leave recv_msg_userauth_banner: banner after auth done")); + TRACE(("leave recv_msg_userauth_banner: banner after auth done")) return; } @@ -74,7 +74,7 @@ void recv_msg_userauth_banner() { buf_eatstring(ses.payload); /* The language string */ if (bannerlen > MAX_BANNER_SIZE) { - TRACE(("recv_msg_userauth_banner: bannerlen too long: %d", bannerlen)); + TRACE(("recv_msg_userauth_banner: bannerlen too long: %d", bannerlen)) goto out; } @@ -96,7 +96,7 @@ void recv_msg_userauth_banner() { out: m_free(banner); - TRACE(("leave recv_msg_userauth_banner")); + TRACE(("leave recv_msg_userauth_banner")) } @@ -108,12 +108,12 @@ void recv_msg_userauth_failure() { unsigned int partial = 0; unsigned int i = 0; - TRACE(("<- MSG_USERAUTH_FAILURE")); - TRACE(("enter recv_msg_userauth_failure")); + TRACE(("<- MSG_USERAUTH_FAILURE")) + TRACE(("enter recv_msg_userauth_failure")) if (cli_ses.state != USERAUTH_REQ_SENT) { /* Perhaps we should be more fatal? */ - TRACE(("But we didn't send a userauth request!!!!!!")); + TRACE(("But we didn't send a userauth request!!!!!!")) return; } @@ -135,7 +135,7 @@ void recv_msg_userauth_failure() { ses.authstate.failcount++; } - TRACE(("Methods (len %d): '%s'", methlen, methods)); + TRACE(("Methods (len %d): '%s'", methlen, methods)) ses.authstate.authdone=0; ses.authstate.authtypes=0; @@ -150,7 +150,7 @@ void recv_msg_userauth_failure() { tok = methods; /* tok stores the next method we'll compare */ for (i = 0; i <= methlen; i++) { if (methods[i] == '\0') { - TRACE(("auth method '%s'", tok)); + TRACE(("auth method '%s'", tok)) #ifdef ENABLE_CLI_PUBKEY_AUTH if (strncmp(AUTH_METHOD_PUBKEY, tok, AUTH_METHOD_PUBKEY_LEN) == 0) { @@ -173,18 +173,18 @@ void recv_msg_userauth_failure() { cli_ses.state = USERAUTH_FAIL_RCVD; - TRACE(("leave recv_msg_userauth_failure")); + TRACE(("leave recv_msg_userauth_failure")) } void recv_msg_userauth_success() { - TRACE(("received msg_userauth_success")); + TRACE(("received msg_userauth_success")) ses.authstate.authdone = 1; cli_ses.state = USERAUTH_SUCCESS_RCVD; } void cli_auth_try() { - TRACE(("enter cli_auth_try")); + TRACE(("enter cli_auth_try")) int finished = 0; CHECKCLEARTOWRITE(); @@ -208,5 +208,5 @@ void cli_auth_try() { dropbear_exit("No auth methods could be used."); } - TRACE(("leave cli_auth_try")); + TRACE(("leave cli_auth_try")) } diff --git a/cli-authpasswd.c b/cli-authpasswd.c index 724203b..02bce18 100644 --- a/cli-authpasswd.c +++ b/cli-authpasswd.c @@ -33,7 +33,7 @@ int cli_auth_password() { char* password = NULL; - TRACE(("enter cli_auth_password")); + TRACE(("enter cli_auth_password")) CHECKCLEARTOWRITE(); password = getpass("Password: "); @@ -56,7 +56,7 @@ int cli_auth_password() { encrypt_packet(); m_burn(password, strlen(password)); - TRACE(("leave cli_auth_password")); + TRACE(("leave cli_auth_password")) return 1; /* Password auth can always be tried */ } diff --git a/cli-authpubkey.c b/cli-authpubkey.c index d308dc3..61b17d9 100644 --- a/cli-authpubkey.c +++ b/cli-authpubkey.c @@ -41,7 +41,7 @@ void cli_pubkeyfail() { struct PubkeyList *keyitem; struct PubkeyList **previtem; - TRACE(("enter cli_pubkeyfail")); + TRACE(("enter cli_pubkeyfail")) previtem = &cli_opts.pubkeys; /* Find the key we failed with, and remove it */ @@ -55,7 +55,7 @@ void cli_pubkeyfail() { sign_key_free(cli_ses.lastpubkey->key); /* It won't be used again */ m_free(cli_ses.lastpubkey); - TRACE(("leave cli_pubkeyfail")); + TRACE(("leave cli_pubkeyfail")) } void recv_msg_userauth_pk_ok() { @@ -67,11 +67,11 @@ void recv_msg_userauth_pk_ok() { int keytype; unsigned int remotelen; - TRACE(("enter recv_msg_userauth_pk_ok")); + TRACE(("enter recv_msg_userauth_pk_ok")) algotype = buf_getstring(ses.payload, &algolen); keytype = signkey_type_from_name(algotype, algolen); - TRACE(("recv_msg_userauth_pk_ok: type %d", keytype)); + TRACE(("recv_msg_userauth_pk_ok: type %d", keytype)) m_free(algotype); keybuf = buf_new(MAX_PUBKEY_SIZE); @@ -84,7 +84,7 @@ void recv_msg_userauth_pk_ok() { if (keyitem->type != keytype) { /* Types differed */ - TRACE(("types differed")); + TRACE(("types differed")) continue; } @@ -98,14 +98,14 @@ void recv_msg_userauth_pk_ok() { if (keybuf->len-4 != remotelen) { - TRACE(("lengths differed: localh %d remote %d", keybuf->len, remotelen)); + TRACE(("lengths differed: localh %d remote %d", keybuf->len, remotelen)) /* Lengths differed */ continue; } if (memcmp(buf_getptr(keybuf, remotelen), buf_getptr(ses.payload, remotelen), remotelen) != 0) { /* Data didn't match this key */ - TRACE(("data differed")); + TRACE(("data differed")) continue; } @@ -114,15 +114,15 @@ void recv_msg_userauth_pk_ok() { } if (keyitem != NULL) { - TRACE(("matching key")); + TRACE(("matching key")) /* XXX TODO: if it's an encrypted key, here we ask for their * password */ send_msg_userauth_pubkey(keyitem->key, keytype, 1); } else { - TRACE(("That was whacky. We got told that a key was valid, but it didn't match our list. Sounds like dodgy code on Dropbear's part")); + TRACE(("That was whacky. We got told that a key was valid, but it didn't match our list. Sounds like dodgy code on Dropbear's part")) } - TRACE(("leave recv_msg_userauth_pk_ok")); + TRACE(("leave recv_msg_userauth_pk_ok")) } /* TODO: make it take an agent reference to use as well */ @@ -132,7 +132,7 @@ static void send_msg_userauth_pubkey(sign_key *key, int type, int realsign) { int algolen; buffer* sigbuf = NULL; - TRACE(("enter send_msg_userauth_pubkey")); + TRACE(("enter send_msg_userauth_pubkey")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_REQUEST); @@ -154,7 +154,7 @@ static void send_msg_userauth_pubkey(sign_key *key, int type, int realsign) { buf_put_pub_key(ses.writepayload, key, type); if (realsign) { - TRACE(("realsign")); + TRACE(("realsign")) /* We put the signature as well - this contains string(session id), then * the contents of the write payload to this point */ sigbuf = buf_new(4 + SHA1_HASH_SIZE + ses.writepayload->len); @@ -165,22 +165,22 @@ static void send_msg_userauth_pubkey(sign_key *key, int type, int realsign) { } encrypt_packet(); - TRACE(("leave send_msg_userauth_pubkey")); + TRACE(("leave send_msg_userauth_pubkey")) } int cli_auth_pubkey() { - TRACE(("enter cli_auth_pubkey")); + TRACE(("enter cli_auth_pubkey")) if (cli_opts.pubkeys != NULL) { /* Send a trial request */ send_msg_userauth_pubkey(cli_opts.pubkeys->key, cli_opts.pubkeys->type, 0); cli_ses.lastpubkey = cli_opts.pubkeys; - TRACE(("leave cli_auth_pubkey-success")); + TRACE(("leave cli_auth_pubkey-success")) return 1; } else { - TRACE(("leave cli_auth_pubkey-failure")); + TRACE(("leave cli_auth_pubkey-failure")) return 0; } } diff --git a/cli-channel.c b/cli-channel.c index ea4af69..42e165b 100644 --- a/cli-channel.c +++ b/cli-channel.c @@ -37,7 +37,7 @@ void recv_msg_channel_extended_data() { struct Channel *channel; unsigned int datatype; - TRACE(("enter recv_msg_channel_extended_data")); + TRACE(("enter recv_msg_channel_extended_data")) chan = buf_getint(ses.payload); channel = getchannel(chan); @@ -47,7 +47,7 @@ void recv_msg_channel_extended_data() { } if (channel->type != &clichansess) { - TRACE(("leave recv_msg_channel_extended_data: chantype is wrong")); + TRACE(("leave recv_msg_channel_extended_data: chantype is wrong")) return; /* we just ignore it */ } @@ -55,11 +55,11 @@ void recv_msg_channel_extended_data() { if (datatype != SSH_EXTENDED_DATA_STDERR) { TRACE(("leave recv_msg_channel_extended_data: wrong datatype: %d", - datatype)); + datatype)) return; } common_recv_msg_channel_data(channel, channel->errfd, channel->extrabuf); - TRACE(("leave recv_msg_channel_extended_data")); + TRACE(("leave recv_msg_channel_extended_data")) } diff --git a/cli-chansession.c b/cli-chansession.c index 35be671..76e9dfa 100644 --- a/cli-chansession.c +++ b/cli-chansession.c @@ -59,20 +59,20 @@ static void cli_chansessreq(struct Channel *channel) { unsigned char* type = NULL; int wantreply; - TRACE(("enter cli_chansessreq")); + TRACE(("enter cli_chansessreq")) type = buf_getstring(ses.payload, NULL); wantreply = buf_getbyte(ses.payload); if (strcmp(type, "exit-status") != 0) { - TRACE(("unknown request '%s'", type)); + TRACE(("unknown request '%s'", type)) send_msg_channel_failure(channel); goto out; } /* We'll just trust what they tell us */ cli_ses.retval = buf_getint(ses.payload); - TRACE(("got exit-status of '%d'", cli_ses.retval)); + TRACE(("got exit-status of '%d'", cli_ses.retval)) out: m_free(type); @@ -108,10 +108,10 @@ static void cli_tty_setup() { struct termios tio; - TRACE(("enter cli_pty_setup")); + TRACE(("enter cli_pty_setup")) if (cli_ses.tty_raw_mode == 1) { - TRACE(("leave cli_tty_setup: already in raw mode!")); + TRACE(("leave cli_tty_setup: already in raw mode!")) return; } @@ -139,15 +139,15 @@ static void cli_tty_setup() { } cli_ses.tty_raw_mode = 1; - TRACE(("leave cli_tty_setup")); + TRACE(("leave cli_tty_setup")) } void cli_tty_cleanup() { - TRACE(("enter cli_tty_cleanup")); + TRACE(("enter cli_tty_cleanup")) if (cli_ses.tty_raw_mode == 0) { - TRACE(("leave cli_tty_cleanup: not in raw mode")); + TRACE(("leave cli_tty_cleanup: not in raw mode")) return; } @@ -157,12 +157,12 @@ void cli_tty_cleanup() { cli_ses.tty_raw_mode = 0; } - TRACE(("leave cli_tty_cleanup")); + TRACE(("leave cli_tty_cleanup")) } static void put_termcodes() { - TRACE(("enter put_termcodes")); + TRACE(("enter put_termcodes")) struct termios tio; unsigned int sshcode; @@ -232,7 +232,7 @@ static void put_termcodes() { buf_putint(ses.writepayload, bufpos2 - bufpos1 - 4); /* len(termcodes) */ buf_setpos(ses.writepayload, bufpos2); /* Back where we were */ - TRACE(("leave put_termcodes")); + TRACE(("leave put_termcodes")) } static void put_winsize() { @@ -284,7 +284,7 @@ static void send_chansess_pty_req(struct Channel *channel) { unsigned char* term = NULL; - TRACE(("enter send_chansess_pty_req")); + TRACE(("enter send_chansess_pty_req")) start_channel_request(channel, "pty-req"); @@ -310,14 +310,14 @@ static void send_chansess_pty_req(struct Channel *channel) { if (signal(SIGWINCH, sigwinch_handler) == SIG_ERR) { dropbear_exit("signal error"); } - TRACE(("leave send_chansess_pty_req")); + TRACE(("leave send_chansess_pty_req")) } static void send_chansess_shell_req(struct Channel *channel) { unsigned char* reqtype = NULL; - TRACE(("enter send_chansess_shell_req")); + TRACE(("enter send_chansess_shell_req")) if (cli_opts.cmd) { reqtype = "exec"; @@ -334,7 +334,7 @@ static void send_chansess_shell_req(struct Channel *channel) { } encrypt_packet(); - TRACE(("leave send_chansess_shell_req")); + TRACE(("leave send_chansess_shell_req")) } static int cli_initchansess(struct Channel *channel) { @@ -367,7 +367,7 @@ static int cli_initchansess(struct Channel *channel) { void cli_send_chansess_request() { - TRACE(("enter cli_send_chansess_request")); + TRACE(("enter cli_send_chansess_request")) if (send_msg_channel_open_init(STDIN_FILENO, &clichansess) == DROPBEAR_FAILURE) { dropbear_exit("Couldn't open initial channel"); @@ -375,6 +375,6 @@ void cli_send_chansess_request() { /* No special channel request data */ encrypt_packet(); - TRACE(("leave cli_send_chansess_request")); + TRACE(("leave cli_send_chansess_request")) } diff --git a/cli-kex.c b/cli-kex.c index c8949ef..03a0670 100644 --- a/cli-kex.c +++ b/cli-kex.c @@ -65,14 +65,14 @@ void recv_msg_kexdh_reply() { unsigned char* keyblob = NULL; - TRACE(("enter recv_msg_kexdh_reply")); + TRACE(("enter recv_msg_kexdh_reply")) if (cli_ses.kex_state != KEXDH_INIT_SENT) { dropbear_exit("Received out-of-order kexdhreply"); } m_mp_init(&dh_f); type = ses.newkeys->algo_hostkey; - TRACE(("type is %d", type)); + TRACE(("type is %d", type)) hostkey = new_sign_key(); keybloblen = buf_getint(ses.payload); @@ -84,12 +84,12 @@ void recv_msg_kexdh_reply() { } if (buf_get_pub_key(ses.payload, hostkey, &type) != DROPBEAR_SUCCESS) { - TRACE(("failed getting pubkey")); + TRACE(("failed getting pubkey")) dropbear_exit("Bad KEX packet"); } if (buf_getmpint(ses.payload, &dh_f) != DROPBEAR_SUCCESS) { - TRACE(("failed getting mpint")); + TRACE(("failed getting mpint")) dropbear_exit("Bad KEX packet"); } @@ -109,7 +109,7 @@ void recv_msg_kexdh_reply() { send_msg_newkeys(); ses.requirenext = SSH_MSG_NEWKEYS; - TRACE(("leave recv_msg_kexdh_init")); + TRACE(("leave recv_msg_kexdh_init")) } static void ask_to_confirm(unsigned char* keyblob, unsigned int keybloblen) { @@ -156,7 +156,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { if (errno != EEXIST) { dropbear_log(LOG_INFO, "Warning: failed creating ~/.ssh: %s", strerror(errno)); - TRACE(("mkdir didn't work: %s", strerror(errno))); + TRACE(("mkdir didn't work: %s", strerror(errno))) ask_to_confirm(keyblob, keybloblen); goto out; /* only get here on success */ } @@ -170,14 +170,14 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { } else { /* We mightn't have been able to open it if it was read-only */ if (errno == EACCES || errno == EROFS) { - TRACE(("trying readonly: %s", strerror(errno))); + TRACE(("trying readonly: %s", strerror(errno))) readonly = 1; hostsfile = fopen(filename, "r"); } } if (hostsfile == NULL) { - TRACE(("hostsfile didn't open: %s", strerror(errno))); + TRACE(("hostsfile didn't open: %s", strerror(errno))) ask_to_confirm(keyblob, keybloblen); goto out; /* We only get here on success */ } @@ -188,7 +188,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { do { if (buf_getline(line, hostsfile) == DROPBEAR_FAILURE) { - TRACE(("failed reading line: prob EOF")); + TRACE(("failed reading line: prob EOF")) break; } @@ -197,32 +197,32 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { * buf_getfoo() past the end and die horribly - the base64 parsing * code is what tiptoes up to the end nicely */ if (line->len < (hostlen+30) ) { - TRACE(("line is too short to be sensible")); + TRACE(("line is too short to be sensible")) continue; } /* Compare hostnames */ if (strncmp(cli_opts.remotehost, buf_getptr(line, hostlen), hostlen) != 0) { - TRACE(("hosts don't match")); + TRACE(("hosts don't match")) continue; } buf_incrpos(line, hostlen); if (buf_getbyte(line) != ' ') { /* there wasn't a space after the hostname, something dodgy */ - TRACE(("missing space afte matching hostname")); + TRACE(("missing space afte matching hostname")) continue; } if ( strncmp(buf_getptr(line, algolen), algoname, algolen) != 0) { - TRACE(("algo doesn't match")); + TRACE(("algo doesn't match")) continue; } buf_incrpos(line, algolen); if (buf_getbyte(line) != ' ') { - TRACE(("missing space after algo")); + TRACE(("missing space after algo")) continue; } @@ -231,7 +231,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { if (ret == DROPBEAR_SUCCESS) { /* Good matching key */ - TRACE(("good matching key")); + TRACE(("good matching key")) goto out; } @@ -244,7 +244,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { /* If we get here, they said yes */ if (readonly) { - TRACE(("readonly")); + TRACE(("readonly")) goto out; } @@ -257,7 +257,7 @@ static void checkhostkey(unsigned char* keyblob, unsigned int keybloblen) { buf_putbytes(line, algoname, algolen); buf_putbyte(line, ' '); len = line->size - line->pos; - TRACE(("keybloblen %d, len %d", keybloblen, len)); + TRACE(("keybloblen %d, len %d", keybloblen, len)) /* The only failure with base64 is buffer_overflow, but buf_getwriteptr * will die horribly in the case anyway */ base64_encode(keyblob, keybloblen, buf_getwriteptr(line, len), &len); diff --git a/cli-main.c b/cli-main.c index def2c72..3f767c9 100644 --- a/cli-main.c +++ b/cli-main.c @@ -50,7 +50,7 @@ int main(int argc, char ** argv) { cli_getopts(argc, argv); TRACE(("user='%s' host='%s' port='%s'", cli_opts.username, - cli_opts.remotehost, cli_opts.remoteport)); + cli_opts.remotehost, cli_opts.remoteport)) if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { dropbear_exit("signal() error"); diff --git a/cli-runopts.c b/cli-runopts.c index b8ecdc0..3ac5c2b 100644 --- a/cli-runopts.c +++ b/cli-runopts.c @@ -116,7 +116,7 @@ void cli_getopts(int argc, char ** argv) { #endif #ifdef ENABLE_CLI_REMOTETCPFWD if (nextisremote) { - TRACE(("nextisremote true")); + TRACE(("nextisremote true")) addforward(argv[i], &cli_opts.remotefwds); nextisremote = 0; continue; @@ -124,7 +124,7 @@ void cli_getopts(int argc, char ** argv) { #endif #ifdef ENABLE_CLI_LOCALTCPFWD if (nextislocal) { - TRACE(("nextislocal true")); + TRACE(("nextislocal true")) addforward(argv[i], &cli_opts.localfwds); nextislocal = 0; continue; @@ -214,7 +214,7 @@ void cli_getopts(int argc, char ** argv) { continue; /* next argument */ } else { - TRACE(("non-flag arg: '%s'", argv[i])); + TRACE(("non-flag arg: '%s'", argv[i])) /* Either the hostname or commands */ @@ -343,7 +343,7 @@ static void addforward(char* origstr, struct TCPFwdList** fwdlist) { struct TCPFwdList* newfwd = NULL; char * str = NULL; - TRACE(("enter addforward")); + TRACE(("enter addforward")) /* We probably don't want to be editing argvs */ str = m_strdup(origstr); @@ -352,7 +352,7 @@ static void addforward(char* origstr, struct TCPFwdList** fwdlist) { connectaddr = strchr(str, ':'); if (connectaddr == NULL) { - TRACE(("connectaddr == NULL")); + TRACE(("connectaddr == NULL")) goto fail; } @@ -361,7 +361,7 @@ static void addforward(char* origstr, struct TCPFwdList** fwdlist) { connectport = strchr(connectaddr, ':'); if (connectport == NULL) { - TRACE(("connectport == NULL")); + TRACE(("connectport == NULL")) goto fail; } @@ -374,32 +374,32 @@ static void addforward(char* origstr, struct TCPFwdList** fwdlist) { * the check later only checks for >= MAX_PORT */ newfwd->listenport = strtol(listenport, NULL, 10); if (errno != 0) { - TRACE(("bad listenport strtol")); + TRACE(("bad listenport strtol")) goto fail; } newfwd->connectport = strtol(connectport, NULL, 10); if (errno != 0) { - TRACE(("bad connectport strtol")); + TRACE(("bad connectport strtol")) goto fail; } newfwd->connectaddr = connectaddr; if (newfwd->listenport > 65535) { - TRACE(("listenport > 65535")); + TRACE(("listenport > 65535")) goto badport; } if (newfwd->connectport > 65535) { - TRACE(("connectport > 65535")); + TRACE(("connectport > 65535")) goto badport; } newfwd->next = *fwdlist; *fwdlist = newfwd; - TRACE(("leave addforward: done")); + TRACE(("leave addforward: done")) return; fail: diff --git a/cli-service.c b/cli-service.c index d14c77f..87b6ed2 100644 --- a/cli-service.c +++ b/cli-service.c @@ -33,7 +33,7 @@ void send_msg_service_request(char* servicename) { - TRACE(("enter send_msg_service_request: servicename='%s'", servicename)); + TRACE(("enter send_msg_service_request: servicename='%s'", servicename)) CHECKCLEARTOWRITE(); @@ -41,7 +41,7 @@ void send_msg_service_request(char* servicename) { buf_putstring(ses.writepayload, servicename, strlen(servicename)); encrypt_packet(); - TRACE(("leave send_msg_service_request")); + TRACE(("leave send_msg_service_request")) } /* This just sets up the state variables right for the main client session loop @@ -51,7 +51,7 @@ void recv_msg_service_accept() { unsigned char* servicename; unsigned int len; - TRACE(("enter recv_msg_service_accept")); + TRACE(("enter recv_msg_service_accept")) servicename = buf_getstring(ses.payload, &len); @@ -62,7 +62,7 @@ void recv_msg_service_accept() { cli_ses.state = SERVICE_AUTH_ACCEPT_RCVD; m_free(servicename); - TRACE(("leave recv_msg_service_accept: done ssh-userauth")); + TRACE(("leave recv_msg_service_accept: done ssh-userauth")) return; } @@ -77,7 +77,7 @@ void recv_msg_service_accept() { cli_ses.state = SERVICE_CONN_ACCEPT_RCVD; m_free(servicename); - TRACE(("leave recv_msg_service_accept: done ssh-connection")); + TRACE(("leave recv_msg_service_accept: done ssh-connection")) return; } diff --git a/cli-session.c b/cli-session.c index 0ea33af..4d6a645 100644 --- a/cli-session.c +++ b/cli-session.c @@ -139,7 +139,7 @@ static void cli_session_init() { * service, userauth and channel requests */ static void cli_sessionloop() { - TRACE(("enter cli_sessionloop")); + TRACE(("enter cli_sessionloop")) if (ses.lastpacket == SSH_MSG_KEXINIT && cli_ses.kex_state == KEX_NOTHING) { cli_ses.kex_state = KEXINIT_RCVD; @@ -151,7 +151,7 @@ static void cli_sessionloop() { * negotiation would have failed. */ send_msg_kexdh_init(); cli_ses.kex_state = KEXDH_INIT_SENT; - TRACE(("leave cli_sessionloop: done with KEXINIT_RCVD")); + TRACE(("leave cli_sessionloop: done with KEXINIT_RCVD")) return; } @@ -163,14 +163,14 @@ static void cli_sessionloop() { /* We shouldn't do anything else if a KEX is in progress */ if (cli_ses.kex_state != KEX_NOTHING) { - TRACE(("leave cli_sessionloop: kex_state != KEX_NOTHING")); + TRACE(("leave cli_sessionloop: kex_state != KEX_NOTHING")) return; } /* We should exit if we haven't donefirstkex: we shouldn't reach here * in normal operation */ if (ses.kexstate.donefirstkex == 0) { - TRACE(("XXX XXX might be bad! leave cli_sessionloop: haven't donefirstkex")); + TRACE(("XXX XXX might be bad! leave cli_sessionloop: haven't donefirstkex")) return; } @@ -181,32 +181,32 @@ static void cli_sessionloop() { * userauth */ send_msg_service_request(SSH_SERVICE_USERAUTH); cli_ses.state = SERVICE_AUTH_REQ_SENT; - TRACE(("leave cli_sessionloop: sent userauth service req")); + TRACE(("leave cli_sessionloop: sent userauth service req")) return; /* userauth code */ case SERVICE_AUTH_ACCEPT_RCVD: cli_auth_getmethods(); cli_ses.state = USERAUTH_REQ_SENT; - TRACE(("leave cli_sessionloop: sent userauth methods req")); + TRACE(("leave cli_sessionloop: sent userauth methods req")) return; case USERAUTH_FAIL_RCVD: cli_auth_try(); cli_ses.state = USERAUTH_REQ_SENT; - TRACE(("leave cli_sessionloop: cli_auth_try")); + TRACE(("leave cli_sessionloop: cli_auth_try")) return; /* case USERAUTH_SUCCESS_RCVD: send_msg_service_request(SSH_SERVICE_CONNECTION); cli_ses.state = SERVICE_CONN_REQ_SENT; - TRACE(("leave cli_sessionloop: sent ssh-connection service req")); + TRACE(("leave cli_sessionloop: sent ssh-connection service req")) return; case SERVICE_CONN_ACCEPT_RCVD: cli_send_chansess_request(); - TRACE(("leave cli_sessionloop: cli_send_chansess_request")); + TRACE(("leave cli_sessionloop: cli_send_chansess_request")) cli_ses.state = SESSION_RUNNING; return; */ @@ -219,7 +219,7 @@ static void cli_sessionloop() { setup_remotetcp(); #endif cli_send_chansess_request(); - TRACE(("leave cli_sessionloop: cli_send_chansess_request")); + TRACE(("leave cli_sessionloop: cli_send_chansess_request")) cli_ses.state = SESSION_RUNNING; return; @@ -240,7 +240,7 @@ static void cli_sessionloop() { break; } - TRACE(("leave cli_sessionloop: fell out")); + TRACE(("leave cli_sessionloop: fell out")) } diff --git a/cli-tcpfwd.c b/cli-tcpfwd.c index b4d99e9..aa5b720 100644 --- a/cli-tcpfwd.c +++ b/cli-tcpfwd.c @@ -62,10 +62,10 @@ void setup_localtcp() { int ret; - TRACE(("enter setup_localtcp")); + TRACE(("enter setup_localtcp")) if (cli_opts.localfwds == NULL) { - TRACE(("cli_opts.localfwds == NULL")); + TRACE(("cli_opts.localfwds == NULL")) } while (cli_opts.localfwds != NULL) { @@ -81,7 +81,7 @@ void setup_localtcp() { cli_opts.localfwds = cli_opts.localfwds->next; } - TRACE(("leave setup_localtcp")); + TRACE(("leave setup_localtcp")) } @@ -105,7 +105,7 @@ static int cli_localtcp(unsigned int listenport, const char* remoteaddr, if (ret == DROPBEAR_FAILURE) { m_free(tcpinfo); } - TRACE(("leave cli_localtcp: %d", ret)); + TRACE(("leave cli_localtcp: %d", ret)) return ret; } #endif /* ENABLE_CLI_LOCALTCPFWD */ @@ -113,7 +113,7 @@ static int cli_localtcp(unsigned int listenport, const char* remoteaddr, #ifdef ENABLE_CLI_REMOTETCPFWD static void send_msg_global_request_remotetcp(int port) { - TRACE(("enter send_msg_global_request_remotetcp")); + TRACE(("enter send_msg_global_request_remotetcp")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_GLOBAL_REQUEST); @@ -124,17 +124,17 @@ static void send_msg_global_request_remotetcp(int port) { encrypt_packet(); - TRACE(("leave send_msg_global_request_remotetcp")); + TRACE(("leave send_msg_global_request_remotetcp")) } void setup_remotetcp() { struct TCPFwdList * iter = NULL; - TRACE(("enter setup_remotetcp")); + TRACE(("enter setup_remotetcp")) if (cli_opts.remotefwds == NULL) { - TRACE(("cli_opts.remotefwds == NULL")); + TRACE(("cli_opts.remotefwds == NULL")) } iter = cli_opts.remotefwds; @@ -143,7 +143,7 @@ void setup_remotetcp() { send_msg_global_request_remotetcp(iter->listenport); iter = iter->next; } - TRACE(("leave setup_remotetcp")); + TRACE(("leave setup_remotetcp")) } static int newtcpforwarded(struct Channel * channel) { @@ -179,7 +179,7 @@ static int newtcpforwarded(struct Channel * channel) { snprintf(portstring, sizeof(portstring), "%d", iter->connectport); sock = connect_remote(iter->connectaddr, portstring, 1, NULL); if (sock < 0) { - TRACE(("leave newtcpdirect: sock failed")); + TRACE(("leave newtcpdirect: sock failed")) err = SSH_OPEN_CONNECT_FAILED; goto out; } @@ -196,7 +196,7 @@ static int newtcpforwarded(struct Channel * channel) { err = SSH_OPEN_IN_PROGRESS; out: - TRACE(("leave newtcpdirect: err %d", err)); + TRACE(("leave newtcpdirect: err %d", err)) return err; } #endif /* ENABLE_CLI_REMOTETCPFWD */ diff --git a/common-algo.c b/common-algo.c index 22cdc70..1975864 100644 --- a/common-algo.c +++ b/common-algo.c @@ -202,6 +202,6 @@ void buf_put_algolist(buffer * buf, algo_type localalgos[]) { } str[pos]=0; /* Debug this */ - TRACE(("buf_put_algolist: %s", str)); + TRACE(("buf_put_algolist: %s", str)) buf_putstring(buf, str, pos); } diff --git a/common-channel.c b/common-channel.c index 1d703a2..6f73fab 100644 --- a/common-channel.c +++ b/common-channel.c @@ -81,15 +81,15 @@ void chancleanup() { unsigned int i; - TRACE(("enter chancleanup")); + TRACE(("enter chancleanup")) for (i = 0; i < ses.chansize; i++) { if (ses.channels[i] != NULL) { - TRACE(("channel %d closing", i)); + TRACE(("channel %d closing", i)) removechannel(ses.channels[i]); } } m_free(ses.channels); - TRACE(("leave chancleanup")); + TRACE(("leave chancleanup")) } /* Create a new channel entry, send a reply confirm or failure */ @@ -103,7 +103,7 @@ struct Channel* newchannel(unsigned int remotechan, struct Channel * newchan; unsigned int i, j; - TRACE(("enter newchannel")); + TRACE(("enter newchannel")) /* first see if we can use existing channels */ for (i = 0; i < ses.chansize; i++) { @@ -115,7 +115,7 @@ struct Channel* newchannel(unsigned int remotechan, /* otherwise extend the list */ if (i == ses.chansize) { if (ses.chansize >= MAX_CHANNELS) { - TRACE(("leave newchannel: max chans reached")); + TRACE(("leave newchannel: max chans reached")) return NULL; } @@ -157,7 +157,7 @@ struct Channel* newchannel(unsigned int remotechan, ses.channels[i] = newchan; ses.chancount++; - TRACE(("leave newchannel")); + TRACE(("leave newchannel")) return newchan; } @@ -246,13 +246,13 @@ void channelio(fd_set *readfd, fd_set *writefd) { /* do all the EOF/close type stuff checking for a channel */ static void checkclose(struct Channel *channel) { - TRACE(("checkclose: infd %d, outfd %d, errfd %d, sentclosed %d, recvclosed %d", + TRACE(("checkclose: infd %d, outfd %d, errfd %d, sentclosed %d, recvclosed %d", channel->infd, channel->outfd, - channel->errfd, channel->sentclosed, channel->recvclosed)); + channel->errfd, channel->sentclosed, channel->recvclosed)) TRACE(("writebuf %d extrabuf %s extrabuf %d", cbuf_getused(channel->writebuf), channel->writebuf, - channel->writebuf ? 0 : cbuf_getused(channel->extrabuf))); + channel->writebuf ? 0 : cbuf_getused(channel->extrabuf))) if (!channel->sentclosed) { @@ -289,7 +289,7 @@ static void checkclose(struct Channel *channel) { */ if (channel->recvclosed) { if (! channel->sentclosed) { - TRACE(("Sending MSG_CHANNEL_CLOSE in response to same.")); + TRACE(("Sending MSG_CHANNEL_CLOSE in response to same.")) send_msg_channel_close(channel); } removechannel(channel); @@ -306,7 +306,7 @@ static void checkinitdone(struct Channel *channel) { int val; socklen_t vallen = sizeof(val); - TRACE(("enter checkinitdone")); + TRACE(("enter checkinitdone")) if (getsockopt(channel->infd, SOL_SOCKET, SO_ERROR, &val, &vallen) || val != 0) { @@ -314,13 +314,13 @@ static void checkinitdone(struct Channel *channel) { SSH_OPEN_CONNECT_FAILED, "", ""); close(channel->infd); deletechannel(channel); - TRACE(("leave checkinitdone: fail")); + TRACE(("leave checkinitdone: fail")) } else { send_msg_channel_open_confirmation(channel, channel->recvwindow, channel->recvmaxpacket); channel->outfd = channel->infd; channel->initconn = 0; - TRACE(("leave checkinitdone: success")); + TRACE(("leave checkinitdone: success")) } } @@ -329,7 +329,7 @@ static void checkinitdone(struct Channel *channel) { /* Send the close message and set the channel as closed */ static void send_msg_channel_close(struct Channel *channel) { - TRACE(("enter send_msg_channel_close")); + TRACE(("enter send_msg_channel_close")) /* XXX server */ if (channel->type->closehandler) { channel->type->closehandler(channel); @@ -344,13 +344,13 @@ static void send_msg_channel_close(struct Channel *channel) { channel->senteof = 1; channel->sentclosed = 1; - TRACE(("leave send_msg_channel_close")); + TRACE(("leave send_msg_channel_close")) } /* call this when trans/eof channels are closed */ static void send_msg_channel_eof(struct Channel *channel) { - TRACE(("enter send_msg_channel_eof")); + TRACE(("enter send_msg_channel_eof")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_EOF); @@ -360,7 +360,7 @@ static void send_msg_channel_eof(struct Channel *channel) { channel->senteof = 1; - TRACE(("leave send_msg_channel_eof")); + TRACE(("leave send_msg_channel_eof")) } /* Called to write data out to the local side of the channel. @@ -370,7 +370,7 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { int len, maxlen; - TRACE(("enter writechannel")); + TRACE(("enter writechannel")) maxlen = cbuf_readlen(cbuf); @@ -382,7 +382,7 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { * that's a nasty failure too */ closeinfd(channel); } - TRACE(("leave writechannel: len <= 0")); + TRACE(("leave writechannel: len <= 0")) return; } @@ -392,7 +392,7 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { if (fd == channel->infd && len == maxlen && channel->recveof) { /* Check if we're closing up */ closeinfd(channel); - TRACE(("leave writechannel: recveof set")); + TRACE(("leave writechannel: recveof set")) return; } @@ -410,7 +410,7 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { channel->recvwindow <= cbuf_getavail(channel->extrabuf)); - TRACE(("leave writechannel")); + TRACE(("leave writechannel")) } /* Set the file descriptors for the main select in session.c @@ -444,7 +444,7 @@ void setchannelfds(fd_set *readfd, fd_set *writefd) { TRACE(("infd = %d, outfd %d, errfd %d, bufused %d", channel->infd, channel->outfd, channel->errfd, - cbuf_getused(channel->writebuf) )); + cbuf_getused(channel->writebuf) )) if (channel->infd >= 0 && channel->infd != channel->outfd) { FD_SET(channel->infd, readfd); } @@ -477,7 +477,7 @@ void recv_msg_channel_eof() { unsigned int chan; struct Channel * channel; - TRACE(("enter recv_msg_channel_eof")); + TRACE(("enter recv_msg_channel_eof")) chan = buf_getint(ses.payload); channel = getchannel(chan); @@ -493,7 +493,7 @@ void recv_msg_channel_eof() { closeinfd(channel); } - TRACE(("leave recv_msg_channel_eof")); + TRACE(("leave recv_msg_channel_eof")) } @@ -503,10 +503,10 @@ void recv_msg_channel_close() { unsigned int chan; struct Channel * channel; - TRACE(("enter recv_msg_channel_close")); + TRACE(("enter recv_msg_channel_close")) chan = buf_getint(ses.payload); - TRACE(("close channel = %d", chan)); + TRACE(("close channel = %d", chan)) channel = getchannel(chan); if (channel == NULL) { @@ -521,15 +521,15 @@ void recv_msg_channel_close() { removechannel(channel); } - TRACE(("leave recv_msg_channel_close")); + TRACE(("leave recv_msg_channel_close")) } /* Remove a channel entry, this is only executed after both sides have sent * channel close */ static void removechannel(struct Channel * channel) { - TRACE(("enter removechannel")); - TRACE(("channel index is %d", channel->index)); + TRACE(("enter removechannel")) + TRACE(("channel index is %d", channel->index)) cbuf_free(channel->writebuf); channel->writebuf = NULL; @@ -550,7 +550,7 @@ static void removechannel(struct Channel * channel) { deletechannel(channel); - TRACE(("leave removechannel")); + TRACE(("leave removechannel")) } /* Remove a channel entry */ @@ -570,7 +570,7 @@ void recv_msg_channel_request() { unsigned int chan; struct Channel *channel; - TRACE(("enter recv_msg_channel_request")); + TRACE(("enter recv_msg_channel_request")) chan = buf_getint(ses.payload); channel = getchannel(chan); @@ -586,7 +586,7 @@ void recv_msg_channel_request() { send_msg_channel_failure(channel); } - TRACE(("leave recv_msg_channel_request")); + TRACE(("leave recv_msg_channel_request")) } @@ -603,8 +603,8 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, unsigned int maxlen; int fd; -/* TRACE(("enter send_msg_channel_data")); - TRACE(("extended = %d type = %d", isextended, exttype));*/ +/* TRACE(("enter send_msg_channel_data")) + TRACE(("extended = %d type = %d", isextended, exttype))*/ CHECKCLEARTOWRITE(); @@ -623,14 +623,14 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, maxlen = MIN(maxlen, ses.writepayload->size - 1 - 4 - 4 - (isextended ? 4 : 0)); if (maxlen == 0) { - TRACE(("leave send_msg_channel_data: no window")); + TRACE(("leave send_msg_channel_data: no window")) return; /* the data will get written later */ } /* read the data */ - TRACE(("maxlen %d", maxlen)); + TRACE(("maxlen %d", maxlen)) buf = buf_new(maxlen); - TRACE(("buf pos %d data %x", buf->pos, buf->data)); + TRACE(("buf pos %d data %x", buf->pos, buf->data)) len = read(fd, buf_getwriteptr(buf, maxlen), maxlen); if (len <= 0) { /* on error/eof, send eof */ @@ -660,7 +660,7 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, channel->transwindow -= len; encrypt_packet(); - TRACE(("leave send_msg_channel_data")); + TRACE(("leave send_msg_channel_data")) } /* We receive channel data */ @@ -689,7 +689,7 @@ void common_recv_msg_channel_data(struct Channel *channel, int fd, unsigned int buflen; unsigned int len; - TRACE(("enter recv_msg_channel_data")); + TRACE(("enter recv_msg_channel_data")) if (channel->recveof) { dropbear_exit("received data after eof"); @@ -730,7 +730,7 @@ void common_recv_msg_channel_data(struct Channel *channel, int fd, channel->recvwindow -= datalen; assert(channel->recvwindow <= RECV_MAXWINDOW); - TRACE(("leave recv_msg_channel_data")); + TRACE(("leave recv_msg_channel_data")) } /* Increment the outgoing data window for a channel - the remote end limits @@ -750,7 +750,7 @@ void recv_msg_channel_window_adjust() { } incr = buf_getint(ses.payload); - TRACE(("received window increment %d", incr)); + TRACE(("received window increment %d", incr)) incr = MIN(incr, MAX_TRANS_WIN_INCR); channel->transwindow += incr; @@ -763,7 +763,7 @@ void recv_msg_channel_window_adjust() { static void send_msg_channel_window_adjust(struct Channel* channel, unsigned int incr) { - TRACE(("sending window adjust %d", incr)); + TRACE(("sending window adjust %d", incr)) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_WINDOW_ADJUST); @@ -787,7 +787,7 @@ void recv_msg_channel_open() { int ret; - TRACE(("enter recv_msg_channel_open")); + TRACE(("enter recv_msg_channel_open")) /* get the packet contents */ type = buf_getstring(ses.payload, &typelen); @@ -815,17 +815,17 @@ void recv_msg_channel_open() { } if (chantype == NULL) { - TRACE(("No matching type for '%s'", type)); + TRACE(("No matching type for '%s'", type)) goto failure; } - TRACE(("matched type '%s'", type)); + TRACE(("matched type '%s'", type)) /* create the channel */ channel = newchannel(remotechan, chantype, transwindow, transmaxpacket); if (channel == NULL) { - TRACE(("newchannel returned NULL")); + TRACE(("newchannel returned NULL")) goto failure; } @@ -838,7 +838,7 @@ void recv_msg_channel_open() { } errtype = ret; deletechannel(channel); - TRACE(("inithandler returned failure %d", ret)); + TRACE(("inithandler returned failure %d", ret)) goto failure; } } @@ -849,39 +849,39 @@ void recv_msg_channel_open() { goto cleanup; failure: - TRACE(("recv_msg_channel_open failure")); + TRACE(("recv_msg_channel_open failure")) send_msg_channel_open_failure(remotechan, errtype, "", ""); cleanup: m_free(type); - TRACE(("leave recv_msg_channel_open")); + TRACE(("leave recv_msg_channel_open")) } /* Send a failure message */ void send_msg_channel_failure(struct Channel *channel) { - TRACE(("enter send_msg_channel_failure")); + TRACE(("enter send_msg_channel_failure")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_FAILURE); buf_putint(ses.writepayload, channel->remotechan); encrypt_packet(); - TRACE(("leave send_msg_channel_failure")); + TRACE(("leave send_msg_channel_failure")) } /* Send a success message */ void send_msg_channel_success(struct Channel *channel) { - TRACE(("enter send_msg_channel_success")); + TRACE(("enter send_msg_channel_success")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_SUCCESS); buf_putint(ses.writepayload, channel->remotechan); encrypt_packet(); - TRACE(("leave send_msg_channel_success")); + TRACE(("leave send_msg_channel_success")) } /* Send a channel open failure message, with a corresponding reason @@ -889,7 +889,7 @@ void send_msg_channel_success(struct Channel *channel) { static void send_msg_channel_open_failure(unsigned int remotechan, int reason, const unsigned char *text, const unsigned char *lang) { - TRACE(("enter send_msg_channel_open_failure")); + TRACE(("enter send_msg_channel_open_failure")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN_FAILURE); @@ -899,7 +899,7 @@ static void send_msg_channel_open_failure(unsigned int remotechan, buf_putstring(ses.writepayload, lang, strlen((char*)lang)); encrypt_packet(); - TRACE(("leave send_msg_channel_open_failure")); + TRACE(("leave send_msg_channel_open_failure")) } /* Confirm a channel open, and let the remote end know what number we've @@ -908,7 +908,7 @@ static void send_msg_channel_open_confirmation(struct Channel* channel, unsigned int recvwindow, unsigned int recvmaxpacket) { - TRACE(("enter send_msg_channel_open_confirmation")); + TRACE(("enter send_msg_channel_open_confirmation")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_OPEN_CONFIRMATION); @@ -918,7 +918,7 @@ static void send_msg_channel_open_confirmation(struct Channel* channel, buf_putint(ses.writepayload, recvmaxpacket); encrypt_packet(); - TRACE(("leave send_msg_channel_open_confirmation")); + TRACE(("leave send_msg_channel_open_confirmation")) } #if defined(USING_LISTENERS) || defined(DROPBEAR_CLIENT) @@ -931,10 +931,10 @@ int send_msg_channel_open_init(int fd, const struct ChanType *type) { struct Channel* chan; - TRACE(("enter send_msg_channel_open_init()")); + TRACE(("enter send_msg_channel_open_init()")) chan = newchannel(0, type, 0, 0); if (!chan) { - TRACE(("leave send_msg_channel_open_init() - FAILED in newchannel()")); + TRACE(("leave send_msg_channel_open_init() - FAILED in newchannel()")) return DROPBEAR_FAILURE; } @@ -953,7 +953,7 @@ int send_msg_channel_open_init(int fd, const struct ChanType *type) { buf_putint(ses.writepayload, RECV_MAXWINDOW); buf_putint(ses.writepayload, RECV_MAXPACKET); - TRACE(("leave send_msg_channel_open_init()")); + TRACE(("leave send_msg_channel_open_init()")) return DROPBEAR_SUCCESS; } @@ -965,7 +965,7 @@ void recv_msg_channel_open_confirmation() { struct Channel * channel; int ret; - TRACE(("enter recv_msg_channel_open_confirmation")); + TRACE(("enter recv_msg_channel_open_confirmation")) chan = buf_getint(ses.payload); channel = getchannel(chan); @@ -977,19 +977,19 @@ void recv_msg_channel_open_confirmation() { channel->transwindow = buf_getint(ses.payload); channel->transmaxpacket = buf_getint(ses.payload); - TRACE(("new chan remote %d localho %d", channel->remotechan, chan)); + TRACE(("new chan remote %d localho %d", channel->remotechan, chan)) /* Run the inithandler callback */ if (channel->type->inithandler) { ret = channel->type->inithandler(channel); if (ret > 0) { removechannel(channel); - TRACE(("inithandler returned failure %d", ret)); + TRACE(("inithandler returned failure %d", ret)) } } - TRACE(("leave recv_msg_channel_open_confirmation")); + TRACE(("leave recv_msg_channel_open_confirmation")) } /* Notification that our channel open request failed */ @@ -1013,17 +1013,17 @@ static void closeoutfd(struct Channel * channel, int fd) { /* don't close it if it is the same as infd, * unless infd is already set -1 */ - TRACE(("enter closeoutfd")); + TRACE(("enter closeoutfd")) closechanfd(channel, fd, 0); - TRACE(("leave closeoutfd")); + TRACE(("leave closeoutfd")) } /* close a stdin fd */ static void closeinfd(struct Channel * channel) { - TRACE(("enter closeinfd")); + TRACE(("enter closeinfd")) closechanfd(channel, channel->infd, 1); - TRACE(("leave closeinfd")); + TRACE(("leave closeinfd")) } /* close a fd, how is 0 for stdout/stderr, 1 for stdin */ diff --git a/common-kex.c b/common-kex.c index 38ce090..97e341d 100644 --- a/common-kex.c +++ b/common-kex.c @@ -114,8 +114,8 @@ void send_msg_kexinit() { encrypt_packet(); ses.dataallowed = 0; /* don't send other packets during kex */ - TRACE(("DATAALLOWED=0")); - TRACE(("-> KEXINIT")); + TRACE(("DATAALLOWED=0")) + TRACE(("-> KEXINIT")) ses.kexstate.sentkexinit = 1; } @@ -128,7 +128,7 @@ void send_msg_kexinit() { /* Bring new keys into use after a key exchange, and let the client know*/ void send_msg_newkeys() { - TRACE(("enter send_msg_newkeys")); + TRACE(("enter send_msg_newkeys")) /* generate the kexinit request */ CHECKCLEARTOWRITE(); @@ -138,42 +138,42 @@ void send_msg_newkeys() { /* set up our state */ if (ses.kexstate.recvnewkeys) { - TRACE(("while RECVNEWKEYS=1")); + TRACE(("while RECVNEWKEYS=1")) gen_new_keys(); kexinitialise(); /* we've finished with this kex */ - TRACE((" -> DATAALLOWED=1")); + TRACE((" -> DATAALLOWED=1")) ses.dataallowed = 1; /* we can send other packets again now */ ses.kexstate.donefirstkex = 1; } else { ses.kexstate.sentnewkeys = 1; - TRACE(("SENTNEWKEYS=1")); + TRACE(("SENTNEWKEYS=1")) } - TRACE(("-> MSG_NEWKEYS")); - TRACE(("leave send_msg_newkeys")); + TRACE(("-> MSG_NEWKEYS")) + TRACE(("leave send_msg_newkeys")) } /* Bring the new keys into use after a key exchange */ void recv_msg_newkeys() { - TRACE(("<- MSG_NEWKEYS")); - TRACE(("enter recv_msg_newkeys")); + TRACE(("<- MSG_NEWKEYS")) + TRACE(("enter recv_msg_newkeys")) /* simply check if we've sent SSH_MSG_NEWKEYS, and if so, * switch to the new keys */ if (ses.kexstate.sentnewkeys) { - TRACE(("while SENTNEWKEYS=1")); + TRACE(("while SENTNEWKEYS=1")) gen_new_keys(); kexinitialise(); /* we've finished with this kex */ - TRACE((" -> DATAALLOWED=1")); + TRACE((" -> DATAALLOWED=1")) ses.dataallowed = 1; /* we can send other packets again now */ ses.kexstate.donefirstkex = 1; } else { - TRACE(("RECVNEWKEYS=1")); + TRACE(("RECVNEWKEYS=1")) ses.kexstate.recvnewkeys = 1; } - TRACE(("leave recv_msg_newkeys")); + TRACE(("leave recv_msg_newkeys")) } @@ -189,7 +189,7 @@ static void kexinitialise() { struct timeval tv; - TRACE(("kexinitialise()")); + TRACE(("kexinitialise()")) /* sent/recv'd MSG_KEXINIT */ ses.kexstate.sentkexinit = 0; @@ -262,7 +262,7 @@ void gen_new_keys() { unsigned int C2S_keysize, S2C_keysize; char mactransletter, macrecvletter; /* Client or server specific */ - TRACE(("enter gen_new_keys")); + TRACE(("enter gen_new_keys")) /* the dh_K and hash are the start of all hashes, we make use of that */ sha1_init(&hs); @@ -329,7 +329,7 @@ void gen_new_keys() { ses.keys = ses.newkeys; ses.newkeys = NULL; - TRACE(("leave gen_new_keys")); + TRACE(("leave gen_new_keys")) } #ifndef DISABLE_ZLIB @@ -393,8 +393,8 @@ static void gen_new_zstreams() { /* Belongs in common_kex.c where it should be moved after review */ void recv_msg_kexinit() { - TRACE(("<- KEXINIT")); - TRACE(("enter recv_msg_kexinit")); + TRACE(("<- KEXINIT")) + TRACE(("enter recv_msg_kexinit")) /* start the kex hash */ ses.kexhashbuf = buf_new(MAX_KEXHASHBUF); @@ -402,7 +402,7 @@ void recv_msg_kexinit() { if (!ses.kexstate.sentkexinit) { /* we need to send a kex packet */ send_msg_kexinit(); - TRACE(("continue recv_msg_kexinit: sent kexinit")); + TRACE(("continue recv_msg_kexinit: sent kexinit")) } @@ -459,7 +459,7 @@ void recv_msg_kexinit() { ses.kexstate.recvkexinit = 1; // ses.expecting = 0; // client matt - TRACE(("leave recv_msg_kexinit")); + TRACE(("leave recv_msg_kexinit")) } /* Initialises and generate one side of the diffie-hellman key exchange values. @@ -473,7 +473,7 @@ void gen_kexdh_vals(mp_int *dh_pub, mp_int *dh_priv) { unsigned char randbuf[DH_P_LEN]; int dh_q_len; - TRACE(("enter send_msg_kexdh_reply")); + TRACE(("enter send_msg_kexdh_reply")) m_mp_init_multi(&dh_g, &dh_p, &dh_q, NULL); @@ -619,7 +619,7 @@ static void read_kex_algos() { erralgo = "kex"; goto error; } - TRACE(("kex algo %s", algo->name)); + TRACE(("kex algo %s", algo->name)) ses.newkeys->algo_kex = algo->val; /* server_host_key_algorithms */ @@ -629,7 +629,7 @@ static void read_kex_algos() { erralgo = "hostkey"; goto error; } - TRACE(("hostkey algo %s", algo->name)); + TRACE(("hostkey algo %s", algo->name)) ses.newkeys->algo_hostkey = algo->val; /* encryption_algorithms_client_to_server */ @@ -638,7 +638,7 @@ static void read_kex_algos() { erralgo = "enc c->s"; goto error; } - TRACE(("c2s is %s", c2s_cipher_algo->name)); + TRACE(("c2s is %s", c2s_cipher_algo->name)) /* encryption_algorithms_server_to_client */ s2c_cipher_algo = ses.buf_match_algo(ses.payload, sshciphers, &goodguess); @@ -646,7 +646,7 @@ static void read_kex_algos() { erralgo = "enc s->c"; goto error; } - TRACE(("s2c is %s", s2c_cipher_algo->name)); + TRACE(("s2c is %s", s2c_cipher_algo->name)) /* mac_algorithms_client_to_server */ c2s_hash_algo = ses.buf_match_algo(ses.payload, sshhashes, &goodguess); @@ -717,12 +717,12 @@ static void read_kex_algos() { ses.newkeys->trans_algo_comp = s2c_comp_algo->val; } - TRACE(("enc algo recv %s", algo->name)); - TRACE(("enc algo trans %s", algo->name)); - TRACE(("mac algo recv %s", algo->name)); - TRACE(("mac algo trans %s", algo->name)); - TRACE(("comp algo recv %s", algo->name)); - TRACE(("comp algo trans %s", algo->name)); + TRACE(("enc algo recv %s", algo->name)) + TRACE(("enc algo trans %s", algo->name)) + TRACE(("mac algo recv %s", algo->name)) + TRACE(("mac algo trans %s", algo->name)) + TRACE(("comp algo recv %s", algo->name)) + TRACE(("comp algo trans %s", algo->name)) /* reserved for future extensions */ buf_getint(ses.payload); diff --git a/common-session.c b/common-session.c index 2914069..06cdbd1 100644 --- a/common-session.c +++ b/common-session.c @@ -52,7 +52,7 @@ int exitflag = 0; /* GLOBAL */ /* called only at the start of a session, set up initial state */ void common_session_init(int sock, char* remotehost) { - TRACE(("enter session_init")); + TRACE(("enter session_init")) ses.remotehost = remotehost; @@ -110,7 +110,7 @@ void common_session_init(int sock, char* remotehost) { ses.allowprivport = 0; - TRACE(("leave session_init")); + TRACE(("leave session_init")) } void session_loop(void(*loophandler)()) { @@ -162,7 +162,7 @@ void session_loop(void(*loophandler)()) { if (val == 0) { /* timeout */ - TRACE(("select timeout")); + TRACE(("select timeout")) continue; } @@ -201,11 +201,11 @@ void session_loop(void(*loophandler)()) { /* clean up a session on exit */ void common_session_cleanup() { - TRACE(("enter session_cleanup")); + TRACE(("enter session_cleanup")) /* we can't cleanup if we don't know the session state */ if (!sessinitdone) { - TRACE(("leave session_cleanup: !sessinitdone")); + TRACE(("leave session_cleanup: !sessinitdone")) return; } @@ -215,7 +215,7 @@ void common_session_cleanup() { chancleanup(); - TRACE(("leave session_cleanup")); + TRACE(("leave session_cleanup")) } @@ -252,7 +252,7 @@ void session_identification() { } if (!done) { - TRACE(("err: %s for '%s'\n", strerror(errno), linebuf)); + TRACE(("err: %s for '%s'\n", strerror(errno), linebuf)) dropbear_exit("Failed to get remote version"); } else { /* linebuf is already null terminated */ @@ -260,7 +260,7 @@ void session_identification() { memcpy(ses.remoteident, linebuf, len); } - TRACE(("remoteident: %s", ses.remoteident)); + TRACE(("remoteident: %s", ses.remoteident)) } @@ -274,7 +274,7 @@ static int ident_readln(int fd, char* buf, int count) { fd_set fds; struct timeval timeout; - TRACE(("enter ident_readln")); + TRACE(("enter ident_readln")) if (count < 1) { return -1; @@ -295,7 +295,7 @@ static int ident_readln(int fd, char* buf, int count) { if (errno == EINTR) { continue; } - TRACE(("leave ident_readln: select error")); + TRACE(("leave ident_readln: select error")) return -1; } @@ -313,12 +313,12 @@ static int ident_readln(int fd, char* buf, int count) { if (errno == EINTR) { continue; /* not a real error */ } - TRACE(("leave ident_readln: read error")); + TRACE(("leave ident_readln: read error")) return -1; } if (num == 0) { /* EOF */ - TRACE(("leave ident_readln: EOF")); + TRACE(("leave ident_readln: EOF")) return -1; } if (in == '\n') { @@ -334,7 +334,7 @@ static int ident_readln(int fd, char* buf, int count) { } buf[pos] = '\0'; - TRACE(("leave ident_readln: return %d", pos+1)); + TRACE(("leave ident_readln: return %d", pos+1)) return pos+1; } @@ -363,7 +363,7 @@ static void checktimeouts() { if (!ses.kexstate.sentkexinit && (secs - ses.kexstate.lastkextime >= KEX_REKEY_TIMEOUT || ses.kexstate.datarecv+ses.kexstate.datatrans >= KEX_REKEY_DATA)){ - TRACE(("rekeying after timeout or max data reached")); + TRACE(("rekeying after timeout or max data reached")) send_msg_kexinit(); } } diff --git a/dbutil.c b/dbutil.c index c77386f..3ead979 100644 --- a/dbutil.c +++ b/dbutil.c @@ -164,17 +164,17 @@ int dropbear_listen(const char* address, const char* port, int val; int sock; - TRACE(("enter dropbear_listen")); + TRACE(("enter dropbear_listen")) memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* TODO: let them flag v4 only etc */ hints.ai_socktype = SOCK_STREAM; if (address && address[0] == '\0') { - TRACE(("dropbear_listen: local loopback")); + TRACE(("dropbear_listen: local loopback")) address = NULL; } else { - TRACE(("dropbear_listen: not local loopback")); + TRACE(("dropbear_listen: not local loopback")) hints.ai_flags = AI_PASSIVE; } err = getaddrinfo(address, port, &hints, &res0); @@ -186,7 +186,7 @@ int dropbear_listen(const char* address, const char* port, *errstring = (char*)m_malloc(len); snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err)); } - TRACE(("leave dropbear_listen: failed resolving")); + TRACE(("leave dropbear_listen: failed resolving")) return -1; } @@ -203,7 +203,7 @@ int dropbear_listen(const char* address, const char* port, if (sock < 0) { err = errno; - TRACE(("socket() failed")); + TRACE(("socket() failed")) continue; } @@ -221,14 +221,14 @@ int dropbear_listen(const char* address, const char* port, if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) { err = errno; close(sock); - TRACE(("bind(%s) failed", port)); + TRACE(("bind(%s) failed", port)) continue; } if (listen(sock, 20) < 0) { err = errno; close(sock); - TRACE(("listen() failed")); + TRACE(("listen() failed")) continue; } @@ -243,12 +243,12 @@ int dropbear_listen(const char* address, const char* port, len = 20 + strlen(strerror(err)); *errstring = (char*)m_malloc(len); snprintf(*errstring, len, "Error listening: %s", strerror(err)); - TRACE(("leave dropbear_listen: failure, %s", strerror(err))); + TRACE(("leave dropbear_listen: failure, %s", strerror(err))) return -1; } } - TRACE(("leave dropbear_listen: success, %d socks bound", nsock)); + TRACE(("leave dropbear_listen: success, %d socks bound", nsock)) return nsock; } @@ -264,7 +264,7 @@ int connect_remote(const char* remotehost, const char* remoteport, int sock; int err; - TRACE(("enter connect_remote")); + TRACE(("enter connect_remote")) if (errstring != NULL) { *errstring = NULL; @@ -282,7 +282,7 @@ int connect_remote(const char* remotehost, const char* remoteport, *errstring = (char*)m_malloc(len); snprintf(*errstring, len, "Error resolving: %s", gai_strerror(err)); } - TRACE(("Error resolving: %s", gai_strerror(err))); + TRACE(("Error resolving: %s", gai_strerror(err))) return -1; } @@ -303,14 +303,14 @@ int connect_remote(const char* remotehost, const char* remoteport, if (errstring != NULL && *errstring == NULL) { *errstring = m_strdup("Failed non-blocking"); } - TRACE(("Failed non-blocking: %s", strerror(errno))); + TRACE(("Failed non-blocking: %s", strerror(errno))) continue; } } if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { if (errno == EINPROGRESS && nonblocking) { - TRACE(("Connect in progress")); + TRACE(("Connect in progress")) break; } else { err = errno; @@ -331,7 +331,7 @@ int connect_remote(const char* remotehost, const char* remoteport, *errstring = (char*)m_malloc(len); snprintf(*errstring, len, "Error connecting: %s", strerror(err)); } - TRACE(("Error connecting: %s", strerror(err))); + TRACE(("Error connecting: %s", strerror(err))) } else { /* Success */ /* (err is used as a dummy var here) */ @@ -343,7 +343,7 @@ int connect_remote(const char* remotehost, const char* remoteport, m_free(*errstring); } - TRACE(("leave connect_remote: sock %d\n", sock)); + TRACE(("leave connect_remote: sock %d\n", sock)) return sock; } @@ -503,7 +503,7 @@ int buf_getline(buffer * line, FILE * authfile) { int c = EOF; - TRACE(("enter buf_getline")); + TRACE(("enter buf_getline")) buf_setpos(line, 0); buf_setlen(line, 0); @@ -518,7 +518,7 @@ int buf_getline(buffer * line, FILE * authfile) { buf_putbyte(line, (unsigned char)c); } - TRACE(("leave getauthline: line too long")); + TRACE(("leave getauthline: line too long")) /* We return success, but the line length will be zeroed - ie we just * ignore that line */ buf_setlen(line, 0); @@ -528,10 +528,10 @@ out: /* if we didn't read anything before EOF or error, exit */ if (c == EOF && line->pos == 0) { - TRACE(("leave buf_getline: failure")); + TRACE(("leave buf_getline: failure")) return DROPBEAR_FAILURE; } else { - TRACE(("leave buf_getline: success")); + TRACE(("leave buf_getline: success")) buf_setpos(line, 0); return DROPBEAR_SUCCESS; } @@ -618,10 +618,10 @@ void m_burn(void *data, unsigned int len) { void setnonblocking(int fd) { - TRACE(("setnonblocking: %d", fd)); + TRACE(("setnonblocking: %d", fd)) if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { dropbear_exit("Couldn't set nonblocking"); } - TRACE(("leave setnonblocking")); + TRACE(("leave setnonblocking")) } diff --git a/debug.h b/debug.h index 37f51ac..7b1e2b5 100644 --- a/debug.h +++ b/debug.h @@ -39,7 +39,7 @@ * Caution: Don't use this in an unfriendly environment (ie unfirewalled), * since the printing may not sanitise strings etc. This will add a reasonable * amount to your executable size. */ -/* #define DEBUG_TRACE */ + //#define DEBUG_TRACE /* All functions writing to the cleartext payload buffer call * CHECKCLEARTOWRITE() before writing. This is only really useful if you're @@ -60,7 +60,7 @@ /* you don't need to touch this block */ #ifdef DEBUG_TRACE -#define TRACE(X) (dropbear_trace X) +#define TRACE(X) dropbear_trace X; #else /*DEBUG_TRACE*/ #define TRACE(X) #endif /*DEBUG_TRACE*/ diff --git a/dropbearkey.c b/dropbearkey.c index 364b765..8ceefdc 100644 --- a/dropbearkey.c +++ b/dropbearkey.c @@ -166,13 +166,13 @@ int main(int argc, char ** argv) { #ifdef DROPBEAR_RSA if (strncmp(typetext, "rsa", 3) == 0) { keytype = DROPBEAR_SIGNKEY_RSA; - TRACE(("type is rsa")); + TRACE(("type is rsa")) } #endif #ifdef DROPBEAR_DSS if (strncmp(typetext, "dss", 3) == 0) { keytype = DROPBEAR_SIGNKEY_DSS; - TRACE(("type is dss")); + TRACE(("type is dss")) } #endif } diff --git a/dss.c b/dss.c index 0cb437c..6429ede 100644 --- a/dss.c +++ b/dss.c @@ -45,7 +45,7 @@ * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ int buf_get_dss_pub_key(buffer* buf, dss_key *key) { - TRACE(("enter buf_get_dss_pub_key")); + TRACE(("enter buf_get_dss_pub_key")) assert(key != NULL); key->p = m_malloc(sizeof(mp_int)); key->q = m_malloc(sizeof(mp_int)); @@ -59,17 +59,17 @@ int buf_get_dss_pub_key(buffer* buf, dss_key *key) { || buf_getmpint(buf, key->q) == DROPBEAR_FAILURE || buf_getmpint(buf, key->g) == DROPBEAR_FAILURE || buf_getmpint(buf, key->y) == DROPBEAR_FAILURE) { - TRACE(("leave buf_get_dss_pub_key: failed reading mpints")); + TRACE(("leave buf_get_dss_pub_key: failed reading mpints")) return DROPBEAR_FAILURE; } if (mp_count_bits(key->p) < MIN_DSS_KEYLEN) { dropbear_log(LOG_WARNING, "DSS key too short"); - TRACE(("leave buf_get_dss_pub_key: short key")); + TRACE(("leave buf_get_dss_pub_key: short key")) return DROPBEAR_FAILURE; } - TRACE(("leave buf_get_dss_pub_key: success")); + TRACE(("leave buf_get_dss_pub_key: success")) return DROPBEAR_SUCCESS; } @@ -98,9 +98,9 @@ int buf_get_dss_priv_key(buffer* buf, dss_key *key) { /* Clear and free the memory used by a public or private key */ void dss_key_free(dss_key *key) { - TRACE(("enter dsa_key_free")); + TRACE(("enter dsa_key_free")) if (key == NULL) { - TRACE(("enter dsa_key_free: key == NULL")); + TRACE(("enter dsa_key_free: key == NULL")) return; } if (key->p) { @@ -124,7 +124,7 @@ void dss_key_free(dss_key *key) { m_free(key->x); } m_free(key); - TRACE(("leave dsa_key_free")); + TRACE(("leave dsa_key_free")) } /* put the dss public key into the buffer in the required format: @@ -171,7 +171,7 @@ int buf_dss_verify(buffer* buf, dss_key *key, const unsigned char* data, char * string = NULL; int stringlen; - TRACE(("enter buf_dss_verify")); + TRACE(("enter buf_dss_verify")) assert(key != NULL); m_mp_init_multi(&val1, &val2, &val3, &val4, NULL); @@ -195,7 +195,7 @@ int buf_dss_verify(buffer* buf, dss_key *key, const unsigned char* data, goto out; } if (mp_cmp(&val1, key->q) != MP_LT) { - TRACE(("verify failed, s' >= q")); + TRACE(("verify failed, s' >= q")) goto out; } /* let val2 = w = (s')^-1 mod q*/ @@ -220,7 +220,7 @@ int buf_dss_verify(buffer* buf, dss_key *key, const unsigned char* data, goto out; } if (mp_cmp(&val1, key->q) != MP_LT) { - TRACE(("verify failed, r' >= q")); + TRACE(("verify failed, r' >= q")) goto out; } /* let val4 = u2 = ((r')w) mod q */ @@ -315,7 +315,7 @@ void buf_put_dss_sign(buffer* buf, dss_key *key, const unsigned char* data, DEF_MP_INT(dss_s); hash_state hs; - TRACE(("enter buf_put_dss_sign")); + TRACE(("enter buf_put_dss_sign")) assert(key != NULL); /* hash the data */ @@ -422,7 +422,7 @@ void buf_put_dss_sign(buffer* buf, dss_key *key, const unsigned char* data, /* create the signature to return */ - TRACE(("leave buf_put_dss_sign")); + TRACE(("leave buf_put_dss_sign")) } #endif /* DROPBEAR_DSS */ diff --git a/listener.c b/listener.c index 1a6da04..dd90c6b 100644 --- a/listener.c +++ b/listener.c @@ -93,7 +93,7 @@ struct Listener* new_listener(int socks[], unsigned int nsocks, /* or create a new one */ if (i == ses.listensize) { if (ses.listensize > MAX_LISTENERS) { - TRACE(("leave newlistener: too many already")); + TRACE(("leave newlistener: too many already")) for (j = 0; j < nsocks; j++) { close(socks[i]); } @@ -115,7 +115,7 @@ struct Listener* new_listener(int socks[], unsigned int nsocks, ses.maxfd = MAX(ses.maxfd, socks[j]); } - TRACE(("new listener num %d ", i)); + TRACE(("new listener num %d ", i)) newlisten = (struct Listener*)m_malloc(sizeof(struct Listener)); newlisten->index = i; diff --git a/packet.c b/packet.c index 5e8e14d..56b31c2 100644 --- a/packet.c +++ b/packet.c @@ -52,7 +52,7 @@ void write_packet() { int len, written; buffer * writebuf = NULL; - TRACE(("enter write_packet")); + TRACE(("enter write_packet")) assert(!isempty(&ses.writequeue)); /* Get the next buffer in the queue of encrypted packets to write*/ @@ -65,7 +65,7 @@ void write_packet() { if (written < 0) { if (errno == EINTR) { - TRACE(("leave writepacket: EINTR")); + TRACE(("leave writepacket: EINTR")) return; } else { dropbear_exit("error writing"); @@ -86,7 +86,7 @@ void write_packet() { buf_incrpos(writebuf, written); } - TRACE(("leave write_packet")); + TRACE(("leave write_packet")) } /* Non-blocking function reading available portion of a packet into the @@ -98,7 +98,7 @@ void read_packet() { unsigned int maxlen; unsigned char blocksize; - TRACE(("enter read_packet")); + TRACE(("enter read_packet")) blocksize = ses.keys->recv_algo_crypt->blocksize; if (ses.readbuf == NULL || ses.readbuf->len < blocksize) { @@ -111,7 +111,7 @@ void read_packet() { /* If we don't have the length of decryptreadbuf, we didn't read * a whole blocksize and should exit */ if (ses.decryptreadbuf->len == 0) { - TRACE(("leave read_packet: packetinit done")); + TRACE(("leave read_packet: packetinit done")) return; } } @@ -128,7 +128,7 @@ void read_packet() { if (len < 0) { if (errno == EINTR || errno == EAGAIN) { - TRACE(("leave read_packet: EINTR or EAGAIN")); + TRACE(("leave read_packet: EINTR or EAGAIN")) return; } else { dropbear_exit("error reading: %s", strerror(errno)); @@ -143,7 +143,7 @@ void read_packet() { /* The main select() loop process_packet() to * handle the packet contents... */ } - TRACE(("leave read_packet")); + TRACE(("leave read_packet")) } /* Function used to read the initial portion of a packet, and determine the @@ -176,7 +176,7 @@ static void read_packet_init() { } if (len < 0) { if (errno == EINTR) { - TRACE(("leave read_packet_init: EINTR")); + TRACE(("leave read_packet_init: EINTR")) return; } dropbear_exit("error reading: %s", strerror(errno)); @@ -230,7 +230,7 @@ void decrypt_packet() { unsigned int padlen; unsigned int len; - TRACE(("enter decrypt_packet")); + TRACE(("enter decrypt_packet")) blocksize = ses.keys->recv_algo_crypt->blocksize; macsize = ses.keys->recv_algo_mac->hashsize; @@ -305,7 +305,7 @@ void decrypt_packet() { ses.recvseq++; - TRACE(("leave decrypt_packet")); + TRACE(("leave decrypt_packet")) } /* Checks the mac in hashbuf, for the data in readbuf. @@ -413,8 +413,8 @@ void encrypt_packet() { buffer * writebuf; /* the packet which will go on the wire */ buffer * clearwritebuf; /* unencrypted, possibly compressed */ - TRACE(("enter encrypt_packet()")); - TRACE(("encrypt_packet type is %d", ses.writepayload->data[0])); + TRACE(("enter encrypt_packet()")) + TRACE(("encrypt_packet type is %d", ses.writepayload->data[0])) blocksize = ses.keys->trans_algo_crypt->blocksize; macsize = ses.keys->trans_algo_mac->hashsize; @@ -514,7 +514,7 @@ void encrypt_packet() { ses.kexstate.datatrans += writebuf->len; ses.transseq++; - TRACE(("leave encrypt_packet()")); + TRACE(("leave encrypt_packet()")) } @@ -526,7 +526,7 @@ static void writemac(buffer * outputbuffer, buffer * clearwritebuf) { unsigned long hashsize; hmac_state hmac; - TRACE(("enter writemac")); + TRACE(("enter writemac")) macsize = ses.keys->trans_algo_mac->hashsize; @@ -561,7 +561,7 @@ static void writemac(buffer * outputbuffer, buffer * clearwritebuf) { } buf_incrwritepos(outputbuffer, macsize); } - TRACE(("leave writemac")); + TRACE(("leave writemac")) } #ifndef DISABLE_ZLIB @@ -572,7 +572,7 @@ static void buf_compress(buffer * dest, buffer * src, unsigned int len) { unsigned int endpos = src->pos + len; int result; - TRACE(("enter buf_compress")); + TRACE(("enter buf_compress")) while (1) { @@ -606,6 +606,6 @@ static void buf_compress(buffer * dest, buffer * src, unsigned int len) { buf_resize(dest, dest->size + ZLIB_COMPRESS_INCR); } - TRACE(("leave buf_compress")); + TRACE(("leave buf_compress")) } #endif diff --git a/process-packet.c b/process-packet.c index bdec032..07fc130 100644 --- a/process-packet.c +++ b/process-packet.c @@ -45,10 +45,10 @@ void process_packet() { unsigned char type; unsigned int i; - TRACE(("enter process_packet")); + TRACE(("enter process_packet")) type = buf_getbyte(ses.payload); - TRACE(("process_packet: packet type = %d", type)); + TRACE(("process_packet: packet type = %d", type)) ses.lastpacket = type; @@ -57,12 +57,12 @@ void process_packet() { case SSH_MSG_IGNORE: case SSH_MSG_DEBUG: - TRACE(("received SSH_MSG_IGNORE or SSH_MSG_DEBUG")); + TRACE(("received SSH_MSG_IGNORE or SSH_MSG_DEBUG")) goto out; case SSH_MSG_UNIMPLEMENTED: /* debugging XXX */ - TRACE(("SSH_MSG_UNIMPLEMENTED")); + TRACE(("SSH_MSG_UNIMPLEMENTED")) dropbear_exit("received SSH_MSG_UNIMPLEMENTED"); case SSH_MSG_DISCONNECT: @@ -87,7 +87,7 @@ void process_packet() { /* Check if we should ignore this packet. Used currently only for * KEX code, with first_kex_packet_follows */ if (ses.ignorenext) { - TRACE(("Ignoring packet, type = %d", type)); + TRACE(("Ignoring packet, type = %d", type)) ses.ignorenext = 0; goto out; } @@ -115,7 +115,7 @@ void process_packet() { /* TODO do something more here? */ - TRACE(("preauth unknown packet")); + TRACE(("preauth unknown packet")) recv_unimplemented(); out: @@ -123,7 +123,7 @@ out: buf_free(ses.payload); ses.payload = NULL; - TRACE(("leave process_packet")); + TRACE(("leave process_packet")) } diff --git a/queue.c b/queue.c index a1352dc..caf6145 100644 --- a/queue.c +++ b/queue.c @@ -52,7 +52,7 @@ void* dequeue(struct Queue* queue) { } else { queue->head = NULL; queue->tail = NULL; - TRACE(("empty queue dequeing")); + TRACE(("empty queue dequeing")) } m_free(oldhead); @@ -70,7 +70,7 @@ void enqueue(struct Queue* queue, void* item) { struct Link* newlink; - TRACE(("enter enqueue")); + TRACE(("enter enqueue")) newlink = (struct Link*)m_malloc(sizeof(struct Link)); newlink->item = item; @@ -85,5 +85,5 @@ void enqueue(struct Queue* queue, void* item) { queue->head = newlink; } queue->count++; - TRACE(("leave enqueue")); + TRACE(("leave enqueue")) } diff --git a/rsa.c b/rsa.c index 7e891e0..1ac0357 100644 --- a/rsa.c +++ b/rsa.c @@ -47,7 +47,7 @@ static mp_int * rsa_pad_em(rsa_key * key, * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ int buf_get_rsa_pub_key(buffer* buf, rsa_key *key) { - TRACE(("enter buf_get_rsa_pub_key")); + TRACE(("enter buf_get_rsa_pub_key")) assert(key != NULL); key->e = m_malloc(sizeof(mp_int)); key->n = m_malloc(sizeof(mp_int)); @@ -60,7 +60,7 @@ int buf_get_rsa_pub_key(buffer* buf, rsa_key *key) { if (buf_getmpint(buf, key->e) == DROPBEAR_FAILURE || buf_getmpint(buf, key->n) == DROPBEAR_FAILURE) { - TRACE(("leave buf_get_rsa_pub_key: failure")); + TRACE(("leave buf_get_rsa_pub_key: failure")) return DROPBEAR_FAILURE; } @@ -69,7 +69,7 @@ int buf_get_rsa_pub_key(buffer* buf, rsa_key *key) { return DROPBEAR_FAILURE; } - TRACE(("leave buf_get_rsa_pub_key: success")); + TRACE(("leave buf_get_rsa_pub_key: success")) return DROPBEAR_SUCCESS; } @@ -81,17 +81,17 @@ int buf_get_rsa_priv_key(buffer* buf, rsa_key *key) { assert(key != NULL); - TRACE(("enter buf_get_rsa_priv_key")); + TRACE(("enter buf_get_rsa_priv_key")) if (buf_get_rsa_pub_key(buf, key) == DROPBEAR_FAILURE) { - TRACE(("leave buf_get_rsa_priv_key: pub: ret == DROPBEAR_FAILURE")); + TRACE(("leave buf_get_rsa_priv_key: pub: ret == DROPBEAR_FAILURE")) return DROPBEAR_FAILURE; } key->d = m_malloc(sizeof(mp_int)); m_mp_init(key->d); if (buf_getmpint(buf, key->d) == DROPBEAR_FAILURE) { - TRACE(("leave buf_get_rsa_priv_key: d: ret == DROPBEAR_FAILURE")); + TRACE(("leave buf_get_rsa_priv_key: d: ret == DROPBEAR_FAILURE")) return DROPBEAR_FAILURE; } @@ -105,17 +105,17 @@ int buf_get_rsa_priv_key(buffer* buf, rsa_key *key) { m_mp_init_multi(key->p, key->q, NULL); if (buf_getmpint(buf, key->p) == DROPBEAR_FAILURE) { - TRACE(("leave buf_get_rsa_priv_key: p: ret == DROPBEAR_FAILURE")); + TRACE(("leave buf_get_rsa_priv_key: p: ret == DROPBEAR_FAILURE")) return DROPBEAR_FAILURE; } if (buf_getmpint(buf, key->q) == DROPBEAR_FAILURE) { - TRACE(("leave buf_get_rsa_priv_key: q: ret == DROPBEAR_FAILURE")); + TRACE(("leave buf_get_rsa_priv_key: q: ret == DROPBEAR_FAILURE")) return DROPBEAR_FAILURE; } } - TRACE(("leave buf_get_rsa_priv_key")); + TRACE(("leave buf_get_rsa_priv_key")) return DROPBEAR_SUCCESS; } @@ -123,10 +123,10 @@ int buf_get_rsa_priv_key(buffer* buf, rsa_key *key) { /* Clear and free the memory used by a public or private key */ void rsa_key_free(rsa_key *key) { - TRACE(("enter rsa_key_free")); + TRACE(("enter rsa_key_free")) if (key == NULL) { - TRACE(("leave rsa_key_free: key == NULL")); + TRACE(("leave rsa_key_free: key == NULL")) return; } if (key->d) { @@ -150,7 +150,7 @@ void rsa_key_free(rsa_key *key) { m_free(key->q); } m_free(key); - TRACE(("leave rsa_key_free")); + TRACE(("leave rsa_key_free")) } /* Put the public rsa key into the buffer in the required format: @@ -161,21 +161,21 @@ void rsa_key_free(rsa_key *key) { */ void buf_put_rsa_pub_key(buffer* buf, rsa_key *key) { - TRACE(("enter buf_put_rsa_pub_key")); + TRACE(("enter buf_put_rsa_pub_key")) assert(key != NULL); buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN); buf_putmpint(buf, key->e); buf_putmpint(buf, key->n); - TRACE(("leave buf_put_rsa_pub_key")); + TRACE(("leave buf_put_rsa_pub_key")) } /* Same as buf_put_rsa_pub_key, but with the private "x" key appended */ void buf_put_rsa_priv_key(buffer* buf, rsa_key *key) { - TRACE(("enter buf_put_rsa_priv_key")); + TRACE(("enter buf_put_rsa_priv_key")) assert(key != NULL); buf_put_rsa_pub_key(buf, key); @@ -190,7 +190,7 @@ void buf_put_rsa_priv_key(buffer* buf, rsa_key *key) { } - TRACE(("leave buf_put_rsa_priv_key")); + TRACE(("leave buf_put_rsa_priv_key")) } @@ -206,7 +206,7 @@ int buf_rsa_verify(buffer * buf, rsa_key *key, const unsigned char* data, mp_int *rsa_em = NULL; int ret = DROPBEAR_FAILURE; - TRACE(("enter buf_rsa_verify")); + TRACE(("enter buf_rsa_verify")) assert(key != NULL); @@ -214,19 +214,19 @@ int buf_rsa_verify(buffer * buf, rsa_key *key, const unsigned char* data, slen = buf_getint(buf); if (slen != (unsigned int)mp_unsigned_bin_size(key->n)) { - TRACE(("bad size")); + TRACE(("bad size")) goto out; } if (mp_read_unsigned_bin(&rsa_s, buf_getptr(buf, buf->len - buf->pos), buf->len - buf->pos) != MP_OKAY) { - TRACE(("failed reading rsa_s")); + TRACE(("failed reading rsa_s")) goto out; } /* check that s <= n-1 */ if (mp_cmp(&rsa_s, key->n) != MP_LT) { - TRACE(("s > n-1")); + TRACE(("s > n-1")) goto out; } @@ -234,13 +234,13 @@ int buf_rsa_verify(buffer * buf, rsa_key *key, const unsigned char* data, rsa_em = rsa_pad_em(key, data, len); if (mp_exptmod(&rsa_s, key->e, key->n, &rsa_mdash) != MP_OKAY) { - TRACE(("failed exptmod rsa_s")); + TRACE(("failed exptmod rsa_s")) goto out; } if (mp_cmp(rsa_em, &rsa_mdash) == MP_EQ) { /* signature is valid */ - TRACE(("success!")); + TRACE(("success!")) ret = DROPBEAR_SUCCESS; } @@ -250,7 +250,7 @@ out: m_free(rsa_em); } mp_clear_multi(&rsa_mdash, &rsa_s, NULL); - TRACE(("leave buf_rsa_verify: ret %d", ret)); + TRACE(("leave buf_rsa_verify: ret %d", ret)) return ret; } @@ -266,7 +266,7 @@ void buf_put_rsa_sign(buffer* buf, rsa_key *key, const unsigned char* data, DEF_MP_INT(rsa_s); mp_int *rsa_em = NULL; - TRACE(("enter buf_put_rsa_sign")); + TRACE(("enter buf_put_rsa_sign")) assert(key != NULL); rsa_em = rsa_pad_em(key, data, len); @@ -306,7 +306,7 @@ void buf_put_rsa_sign(buffer* buf, rsa_key *key, const unsigned char* data, #endif - TRACE(("leave buf_put_rsa_sign")); + TRACE(("leave buf_put_rsa_sign")) } /* Creates the message value as expected by PKCS, see rfc2437 etc */ diff --git a/signkey.c b/signkey.c index 2c8da55..b6b8bdc 100644 --- a/signkey.c +++ b/signkey.c @@ -94,7 +94,7 @@ int buf_get_pub_key(buffer *buf, sign_key *key, int *type) { int keytype; int ret = DROPBEAR_FAILURE; - TRACE(("enter buf_get_pub_key")); + TRACE(("enter buf_get_pub_key")) ident = buf_getstring(buf, &len); keytype = signkey_type_from_name(ident, len); @@ -130,7 +130,7 @@ int buf_get_pub_key(buffer *buf, sign_key *key, int *type) { } #endif - TRACE(("leave buf_get_pub_key")); + TRACE(("leave buf_get_pub_key")) return ret; @@ -146,14 +146,14 @@ int buf_get_priv_key(buffer *buf, sign_key *key, int *type) { int keytype; int ret = DROPBEAR_FAILURE; - TRACE(("enter buf_get_priv_key")); + TRACE(("enter buf_get_priv_key")) ident = buf_getstring(buf, &len); keytype = signkey_type_from_name(ident, len); m_free(ident); if (*type != DROPBEAR_SIGNKEY_ANY && *type != keytype) { - TRACE(("wrong key type: %d %d", *type, keytype)); + TRACE(("wrong key type: %d %d", *type, keytype)) return DROPBEAR_FAILURE; } @@ -183,7 +183,7 @@ int buf_get_priv_key(buffer *buf, sign_key *key, int *type) { } #endif - TRACE(("leave buf_get_priv_key")); + TRACE(("leave buf_get_priv_key")) return ret; @@ -194,7 +194,7 @@ void buf_put_pub_key(buffer* buf, sign_key *key, int type) { buffer *pubkeys; - TRACE(("enter buf_put_pub_key")); + TRACE(("enter buf_put_pub_key")) pubkeys = buf_new(MAX_PUBKEY_SIZE); #ifdef DROPBEAR_DSS @@ -216,26 +216,26 @@ void buf_put_pub_key(buffer* buf, sign_key *key, int type) { pubkeys->len); buf_free(pubkeys); - TRACE(("leave buf_put_pub_key")); + TRACE(("leave buf_put_pub_key")) } /* type is either DROPBEAR_SIGNKEY_DSS or DROPBEAR_SIGNKEY_RSA */ void buf_put_priv_key(buffer* buf, sign_key *key, int type) { - TRACE(("enter buf_put_priv_key")); - TRACE(("type is %d", type)); + TRACE(("enter buf_put_priv_key")) + TRACE(("type is %d", type)) #ifdef DROPBEAR_DSS if (type == DROPBEAR_SIGNKEY_DSS) { buf_put_dss_priv_key(buf, key->dsskey); - TRACE(("leave buf_put_priv_key: dss done")); + TRACE(("leave buf_put_priv_key: dss done")) return; } #endif #ifdef DROPBEAR_RSA if (type == DROPBEAR_SIGNKEY_RSA) { buf_put_rsa_priv_key(buf, key->rsakey); - TRACE(("leave buf_put_priv_key: rsa done")); + TRACE(("leave buf_put_priv_key: rsa done")) return; } #endif @@ -244,7 +244,7 @@ void buf_put_priv_key(buffer* buf, sign_key *key, int type) { void sign_key_free(sign_key *key) { - TRACE(("enter sign_key_free")); + TRACE(("enter sign_key_free")) #ifdef DROPBEAR_DSS dss_key_free(key->dsskey); @@ -256,7 +256,7 @@ void sign_key_free(sign_key *key) { #endif m_free(key); - TRACE(("leave sign_key_free")); + TRACE(("leave sign_key_free")) } static char hexdig(unsigned char x) { @@ -393,7 +393,7 @@ int buf_verify(buffer * buf, sign_key *key, const unsigned char *data, unsigned char * ident = NULL; unsigned int identlen = 0; - TRACE(("enter buf_verify")); + TRACE(("enter buf_verify")) bloblen = buf_getint(buf); ident = buf_getstring(buf, &identlen); @@ -442,17 +442,17 @@ int cmp_base64_key(const unsigned char* keyblob, unsigned int keybloblen, if (base64_decode(buf_getptr(line, len), len, buf_getwriteptr(decodekey, decodekey->size), &decodekeylen) != CRYPT_OK) { - TRACE(("checkpubkey: base64 decode failed")); + TRACE(("checkpubkey: base64 decode failed")) goto out; } - TRACE(("checkpubkey: base64_decode success")); + TRACE(("checkpubkey: base64_decode success")) buf_incrlen(decodekey, decodekeylen); /* compare the keys */ if ( ( decodekeylen != keybloblen ) || memcmp( buf_getptr(decodekey, decodekey->len), keyblob, decodekey->len) != 0) { - TRACE(("checkpubkey: compare failed")); + TRACE(("checkpubkey: compare failed")) goto out; } @@ -461,7 +461,7 @@ int cmp_base64_key(const unsigned char* keyblob, unsigned int keybloblen, filealgolen = buf_getint(decodekey); filealgo = buf_getptr(decodekey, filealgolen); if (filealgolen != algolen || memcmp(filealgo, algoname, algolen) != 0) { - TRACE(("checkpubkey: algo match failed")); + TRACE(("checkpubkey: algo match failed")) goto out; } diff --git a/svr-agentfwd.c b/svr-agentfwd.c index 60c23f7..5127158 100644 --- a/svr-agentfwd.c +++ b/svr-agentfwd.c @@ -101,7 +101,7 @@ static void agentaccept(struct Listener *UNUSED(listener), int sock) { fd = accept(sock, NULL, NULL); if (fd < 0) { - TRACE(("accept failed")); + TRACE(("accept failed")) return; } diff --git a/svr-algo.c b/svr-algo.c index 5559da6..c0b7823 100644 --- a/svr-algo.c +++ b/svr-algo.c @@ -47,7 +47,7 @@ algo_type * svr_buf_match_algo(buffer* buf, algo_type localalgos[], /* get the comma-separated list from the buffer ie "algo1,algo2,algo3" */ algolist = buf_getstring(buf, &len); /* Debug this */ - TRACE(("buf_match_algo: %s", algolist)); + TRACE(("buf_match_algo: %s", algolist)) if (len > MAX_PROPOSED_ALGO*(MAX_NAME_LEN+1)) { goto out; /* just a sanity check, no other use */ } diff --git a/svr-auth.c b/svr-auth.c index 5eb3e27..f0fca38 100644 --- a/svr-auth.c +++ b/svr-auth.c @@ -67,9 +67,9 @@ static void authclear() { * ignore this, but possibly serves as a legal "no trespassing" sign */ static void send_msg_userauth_banner() { - TRACE(("enter send_msg_userauth_banner")); + TRACE(("enter send_msg_userauth_banner")) if (svr_opts.banner == NULL) { - TRACE(("leave send_msg_userauth_banner: banner is NULL")); + TRACE(("leave send_msg_userauth_banner: banner is NULL")) return; } @@ -84,7 +84,7 @@ static void send_msg_userauth_banner() { buf_free(svr_opts.banner); svr_opts.banner = NULL; - TRACE(("leave send_msg_userauth_banner")); + TRACE(("leave send_msg_userauth_banner")) } /* handle a userauth request, check validity, pass to password or pubkey @@ -94,11 +94,11 @@ void recv_msg_userauth_request() { unsigned char *username = NULL, *servicename = NULL, *methodname = NULL; unsigned int userlen, servicelen, methodlen; - TRACE(("enter recv_msg_userauth_request")); + TRACE(("enter recv_msg_userauth_request")) /* ignore packets if auth is already done */ if (ses.authstate.authdone == 1) { - TRACE(("leave recv_msg_userauth_request: authdone already")); + TRACE(("leave recv_msg_userauth_request: authdone already")) return; } @@ -128,7 +128,7 @@ void recv_msg_userauth_request() { if (methodlen == AUTH_METHOD_NONE_LEN && strncmp(methodname, AUTH_METHOD_NONE, AUTH_METHOD_NONE_LEN) == 0) { - TRACE(("recv_msg_userauth_request: 'none' request")); + TRACE(("recv_msg_userauth_request: 'none' request")) send_msg_userauth_failure(0, 0); goto out; } @@ -136,7 +136,7 @@ void recv_msg_userauth_request() { /* check username is good before continuing */ if (checkusername(username, userlen) == DROPBEAR_FAILURE) { /* username is invalid/no shell/etc - send failure */ - TRACE(("sending checkusername failure")); + TRACE(("sending checkusername failure")) send_msg_userauth_failure(0, 1); goto out; } @@ -195,7 +195,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { char* listshell = NULL; char* usershell = NULL; - TRACE(("enter checkusername")); + TRACE(("enter checkusername")) if (userlen > MAX_USERNAME_LEN) { return DROPBEAR_FAILURE; } @@ -217,7 +217,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { /* check that user exists */ if (ses.authstate.pw == NULL) { - TRACE(("leave checkusername: user '%s' doesn't exist", username)); + TRACE(("leave checkusername: user '%s' doesn't exist", username)) dropbear_log(LOG_WARNING, "login attempt for nonexistent user from %s", svr_ses.addrstring); @@ -230,7 +230,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { /* check for non-root if desired */ if (svr_opts.norootlogin && ses.authstate.pw->pw_uid == 0) { - TRACE(("leave checkusername: root login disabled")); + TRACE(("leave checkusername: root login disabled")) dropbear_log(LOG_WARNING, "root login rejected"); send_msg_userauth_failure(0, 1); return DROPBEAR_FAILURE; @@ -238,14 +238,14 @@ static int checkusername(unsigned char *username, unsigned int userlen) { /* check for an empty password */ if (ses.authstate.pw->pw_passwd[0] == '\0') { - TRACE(("leave checkusername: empty pword")); + TRACE(("leave checkusername: empty pword")) dropbear_log(LOG_WARNING, "user '%s' has blank password, rejected", ses.authstate.printableuser); send_msg_userauth_failure(0, 1); return DROPBEAR_FAILURE; } - TRACE(("shell is %s", ses.authstate.pw->pw_shell)); + TRACE(("shell is %s", ses.authstate.pw->pw_shell)) /* check that the shell is set */ usershell = ses.authstate.pw->pw_shell; @@ -259,7 +259,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { * is platform-specific) */ setusershell(); while ((listshell = getusershell()) != NULL) { - TRACE(("test shell is '%s'", listshell)); + TRACE(("test shell is '%s'", listshell)) if (strcmp(listshell, usershell) == 0) { /* have a match */ goto goodshell; @@ -267,7 +267,7 @@ static int checkusername(unsigned char *username, unsigned int userlen) { } /* no matching shell */ endusershell(); - TRACE(("no matching shell")); + TRACE(("no matching shell")) dropbear_log(LOG_WARNING, "user '%s' has invalid shell, rejected", ses.authstate.printableuser); send_msg_userauth_failure(0, 1); @@ -275,10 +275,10 @@ static int checkusername(unsigned char *username, unsigned int userlen) { goodshell: endusershell(); - TRACE(("matching shell")); + TRACE(("matching shell")) - TRACE(("uid = %d", ses.authstate.pw->pw_uid)); - TRACE(("leave checkusername")); + TRACE(("uid = %d", ses.authstate.pw->pw_uid)) + TRACE(("leave checkusername")) return DROPBEAR_SUCCESS; } @@ -292,7 +292,7 @@ void send_msg_userauth_failure(int partial, int incrfail) { buffer *typebuf = NULL; - TRACE(("enter send_msg_userauth_failure")); + TRACE(("enter send_msg_userauth_failure")) CHECKCLEARTOWRITE(); @@ -331,7 +331,7 @@ void send_msg_userauth_failure(int partial, int incrfail) { if (ses.authstate.failcount >= MAX_AUTH_TRIES) { char * userstr; /* XXX - send disconnect ? */ - TRACE(("Max auth tries reached, exiting")); + TRACE(("Max auth tries reached, exiting")) if (ses.authstate.printableuser == NULL) { userstr = "is invalid"; @@ -342,13 +342,13 @@ void send_msg_userauth_failure(int partial, int incrfail) { userstr, svr_ses.addrstring); } - TRACE(("leave send_msg_userauth_failure")); + TRACE(("leave send_msg_userauth_failure")) } /* Send a success message to the user, and set the "authdone" flag */ void send_msg_userauth_success() { - TRACE(("enter send_msg_userauth_success")); + TRACE(("enter send_msg_userauth_success")) CHECKCLEARTOWRITE(); @@ -368,6 +368,6 @@ void send_msg_userauth_success() { * logins - a nasty situation. */ m_close(svr_ses.childpipe); - TRACE(("leave send_msg_userauth_success")); + TRACE(("leave send_msg_userauth_success")) } diff --git a/svr-authpam.c b/svr-authpam.c index 4937fa6..4b16616 100644 --- a/svr-authpam.c +++ b/svr-authpam.c @@ -57,7 +57,7 @@ pamConvFunc(int num_msg, const char* message = (*msg)->msg; - TRACE(("enter pamConvFunc")); + TRACE(("enter pamConvFunc")) if (num_msg != 1) { /* If you're getting here - Dropbear probably can't support your pam @@ -67,11 +67,11 @@ pamConvFunc(int num_msg, return PAM_CONV_ERR; } - TRACE(("msg_style is %d", (*msg)->msg_style)); + TRACE(("msg_style is %d", (*msg)->msg_style)) if (message) { - TRACE(("message is '%s'", message)); + TRACE(("message is '%s'", message)) } else { - TRACE(("null message")); + TRACE(("null message")) } switch((*msg)->msg_style) { @@ -79,7 +79,7 @@ pamConvFunc(int num_msg, case PAM_PROMPT_ECHO_OFF: if (strcmp(message, "Password:") != 0) { - TRACE(("PAM_PROMPT_ECHO_OFF: unrecognized prompt")); + TRACE(("PAM_PROMPT_ECHO_OFF: unrecognized prompt")) rc = PAM_CONV_ERR; break; } @@ -102,7 +102,7 @@ pamConvFunc(int num_msg, if ((strcmp(message, "login: " ) != 0) && (strcmp(message, "login:" ) != 0) && (strcmp(message, "Please enter username: " ) != 0)) { - TRACE(("PAM_PROMPT_ECHO_ON: unrecognized prompt")); + TRACE(("PAM_PROMPT_ECHO_ON: unrecognized prompt")) rc = PAM_CONV_ERR; break; } @@ -117,17 +117,17 @@ pamConvFunc(int num_msg, /* Safe to just use the direct pointer (no strdup) since * it shouldn't be getting munged at all */ resp->resp = m_strdup(userDatap->user); - TRACE(("userDatap->user='%s'", userDatap->user)); + TRACE(("userDatap->user='%s'", userDatap->user)) (*respp) = resp; break; default: - TRACE(("Unknown message type")); + TRACE(("Unknown message type")) rc = PAM_CONV_ERR; break; } - TRACE(("leave pamConvFunc, rc %d", rc)); + TRACE(("leave pamConvFunc, rc %d", rc)) return rc; } @@ -224,7 +224,7 @@ cleanup: m_free(password); } if (pamHandlep != NULL) { - TRACE(("pam_end")); + TRACE(("pam_end")) (void) pam_end(pamHandlep, 0 /* pam_status */); } } diff --git a/svr-authpubkey.c b/svr-authpubkey.c index 5052b10..5daba0f 100644 --- a/svr-authpubkey.c +++ b/svr-authpubkey.c @@ -60,7 +60,7 @@ void svr_auth_pubkey() { char* fp = NULL; int type = -1; - TRACE(("enter pubkeyauth")); + TRACE(("enter pubkeyauth")) /* 0 indicates user just wants to check if key can be used, 1 is an * actual attempt*/ @@ -127,7 +127,7 @@ out: sign_key_free(key); key = NULL; } - TRACE(("leave pubkeyauth")); + TRACE(("leave pubkeyauth")) } /* Reply that the key is valid for auth, this is sent when the user sends @@ -136,7 +136,7 @@ out: static void send_msg_userauth_pk_ok(unsigned char* algo, unsigned int algolen, unsigned char* keyblob, unsigned int keybloblen) { - TRACE(("enter send_msg_userauth_pk_ok")); + TRACE(("enter send_msg_userauth_pk_ok")) CHECKCLEARTOWRITE(); buf_putbyte(ses.writepayload, SSH_MSG_USERAUTH_PK_OK); @@ -144,7 +144,7 @@ static void send_msg_userauth_pk_ok(unsigned char* algo, unsigned int algolen, buf_putstring(ses.writepayload, keyblob, keybloblen); encrypt_packet(); - TRACE(("leave send_msg_userauth_pk_ok")); + TRACE(("leave send_msg_userauth_pk_ok")) } @@ -160,7 +160,7 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, buffer * line = NULL; unsigned int len, pos; - TRACE(("enter checkpubkey")); + TRACE(("enter checkpubkey")) /* check that we can use the algo */ if (have_algo(algo, algolen, sshhostkey) == DROPBEAR_FAILURE) { @@ -172,7 +172,7 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, /* check file permissions, also whether file exists */ if (checkpubkeyperms() == DROPBEAR_FAILURE) { - TRACE(("bad authorized_keys permissions, or file doesn't exist")); + TRACE(("bad authorized_keys permissions, or file doesn't exist")) goto out; } @@ -190,7 +190,7 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, if (authfile == NULL) { goto out; } - TRACE(("checkpubkey: opened authorized_keys OK")); + TRACE(("checkpubkey: opened authorized_keys OK")) line = buf_new(MAX_AUTHKEYS_LINE); @@ -199,12 +199,12 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, if (buf_getline(line, authfile) == DROPBEAR_FAILURE) { /* EOF reached */ - TRACE(("checkpubkey: authorized_keys EOF reached")); + TRACE(("checkpubkey: authorized_keys EOF reached")) break; } if (line->len < MIN_AUTHKEYS_LINE) { - TRACE(("checkpubkey: line too short")); + TRACE(("checkpubkey: line too short")) continue; /* line is too short for it to be a valid key */ } @@ -217,7 +217,7 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, /* check for space (' ') character */ if (buf_getbyte(line) != ' ') { - TRACE(("checkpubkey: space character expected, isn't there")); + TRACE(("checkpubkey: space character expected, isn't there")) continue; } @@ -229,7 +229,7 @@ static int checkpubkey(unsigned char* algo, unsigned int algolen, buf_setpos(line, pos); buf_setlen(line, line->pos + len); - TRACE(("checkpubkey: line pos = %d len = %d", line->pos, line->len)); + TRACE(("checkpubkey: line pos = %d len = %d", line->pos, line->len)) ret = cmp_base64_key(keyblob, keybloblen, algo, algolen, line); if (ret == DROPBEAR_SUCCESS) { @@ -248,7 +248,7 @@ out: buf_free(line); } m_free(filename); - TRACE(("leave checkpubkey: ret=%d", ret)); + TRACE(("leave checkpubkey: ret=%d", ret)) return ret; } @@ -264,7 +264,7 @@ static int checkpubkeyperms() { int ret = DROPBEAR_FAILURE; unsigned int len; - TRACE(("enter checkpubkeyperms")); + TRACE(("enter checkpubkeyperms")) assert(ses.authstate.pw); if (ses.authstate.pw->pw_dir == NULL) { @@ -303,7 +303,7 @@ static int checkpubkeyperms() { out: m_free(filename); - TRACE(("leave checkpubkeyperms")); + TRACE(("leave checkpubkeyperms")) return ret; } @@ -313,24 +313,24 @@ out: static int checkfileperm(char * filename) { struct stat filestat; - TRACE(("enter checkfileperm(%s)", filename)); + TRACE(("enter checkfileperm(%s)", filename)) if (stat(filename, &filestat) != 0) { - TRACE(("leave checkfileperm: stat() != 0")); + TRACE(("leave checkfileperm: stat() != 0")) return DROPBEAR_FAILURE; } /* check ownership - user or root only*/ if (filestat.st_uid != ses.authstate.pw->pw_uid && filestat.st_uid != 0) { - TRACE(("leave checkfileperm: wrong ownership")); + TRACE(("leave checkfileperm: wrong ownership")) return DROPBEAR_FAILURE; } /* check permissions - don't want group or others +w */ if (filestat.st_mode & (S_IWGRP | S_IWOTH)) { - TRACE(("leave checkfileperm: wrong perms")); + TRACE(("leave checkfileperm: wrong perms")) return DROPBEAR_FAILURE; } - TRACE(("leave checkfileperm: success")); + TRACE(("leave checkfileperm: success")) return DROPBEAR_SUCCESS; } diff --git a/svr-chansession.c b/svr-chansession.c index f1e9c88..c365114 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -87,7 +87,7 @@ static void sesssigchild_handler(int UNUSED(dummy)) { struct sigaction sa_chld; struct exitinfo *exit = NULL; - TRACE(("enter sigchld handler")); + TRACE(("enter sigchld handler")) while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { /* find the corresponding chansess */ for (i = 0; i < svr_ses.childpidsize; i++) { @@ -126,7 +126,7 @@ static void sesssigchild_handler(int UNUSED(dummy)) { sa_chld.sa_handler = sesssigchild_handler; sa_chld.sa_flags = SA_NOCLDSTOP; sigaction(SIGCHLD, &sa_chld, NULL); - TRACE(("leave sigchld handler")); + TRACE(("leave sigchld handler")) } /* send the exit status or the signal causing termination for a session */ @@ -248,9 +248,9 @@ static void closechansess(struct Channel *channel) { send_exitsignalstatus(channel); - TRACE(("enter closechansess")); + TRACE(("enter closechansess")) if (chansess == NULL) { - TRACE(("leave closechansess: chansess == NULL")); + TRACE(("leave closechansess: chansess == NULL")) return; } @@ -280,8 +280,8 @@ static void closechansess(struct Channel *channel) { for (i = 0; i < svr_ses.childpidsize; i++) { if (svr_ses.childpids[i].chansess == chansess) { assert(svr_ses.childpids[i].pid > 0); - TRACE(("closing pid %d", svr_ses.childpids[i].pid)); - TRACE(("exitpid = %d", chansess->exit.exitpid)); + TRACE(("closing pid %d", svr_ses.childpids[i].pid)) + TRACE(("exitpid = %d", chansess->exit.exitpid)) svr_ses.childpids[i].pid = -1; svr_ses.childpids[i].chansess = NULL; } @@ -289,7 +289,7 @@ static void closechansess(struct Channel *channel) { m_free(chansess); - TRACE(("leave closechansess")); + TRACE(("leave closechansess")) } /* Handle requests for a channel. These can be execution requests, @@ -302,19 +302,19 @@ static void chansessionrequest(struct Channel *channel) { int ret = 1; struct ChanSess *chansess; - TRACE(("enter chansessionrequest")); + TRACE(("enter chansessionrequest")) type = buf_getstring(ses.payload, &typelen); wantreply = buf_getbyte(ses.payload); if (typelen > MAX_NAME_LEN) { - TRACE(("leave chansessionrequest: type too long")); /* XXX send error?*/ + TRACE(("leave chansessionrequest: type too long")) /* XXX send error?*/ goto out; } chansess = (struct ChanSess*)channel->typedata; assert(chansess != NULL); - TRACE(("type is %s", type)); + TRACE(("type is %s", type)) if (strcmp(type, "window-change") == 0) { ret = sessionwinchange(chansess); @@ -351,7 +351,7 @@ out: } m_free(type); - TRACE(("leave chansessionrequest")); + TRACE(("leave chansessionrequest")) } @@ -421,7 +421,7 @@ static void get_termmodes(struct ChanSess *chansess) { const struct TermCode * termcode; unsigned int len; - TRACE(("enter get_termmodes")); + TRACE(("enter get_termmodes")) /* Term modes */ /* We'll ignore errors and continue if we can't set modes. @@ -438,7 +438,7 @@ static void get_termmodes(struct ChanSess *chansess) { } if (len == 0) { - TRACE(("leave get_termmodes: empty terminal modes string")); + TRACE(("leave get_termmodes: empty terminal modes string")) } while (((opcode = buf_getbyte(ses.payload)) != 0x00) && opcode <= 159) { @@ -500,7 +500,7 @@ static void get_termmodes(struct ChanSess *chansess) { if (tcsetattr(chansess->master, TCSANOW, &termio) < 0) { dropbear_log(LOG_INFO, "error setting terminal attributes"); } - TRACE(("leave get_termmodes")); + TRACE(("leave get_termmodes")) } /* Set up a session pty which will be used to execute the shell or program. @@ -511,11 +511,11 @@ static int sessionpty(struct ChanSess * chansess) { unsigned int termlen; unsigned char namebuf[65]; - TRACE(("enter sessionpty")); + TRACE(("enter sessionpty")) chansess->term = buf_getstring(ses.payload, &termlen); if (termlen > MAX_TERM_LEN) { /* TODO send disconnect ? */ - TRACE(("leave sessionpty: term len too long")); + TRACE(("leave sessionpty: term len too long")) return DROPBEAR_FAILURE; } @@ -524,7 +524,7 @@ static int sessionpty(struct ChanSess * chansess) { dropbear_exit("multiple pty requests"); } if (pty_allocate(&chansess->master, &chansess->slave, namebuf, 64) == 0) { - TRACE(("leave sessionpty: failed to allocate pty")); + TRACE(("leave sessionpty: failed to allocate pty")) return DROPBEAR_FAILURE; } @@ -541,7 +541,7 @@ static int sessionpty(struct ChanSess * chansess) { /* Read the terminal modes */ get_termmodes(chansess); - TRACE(("leave sessionpty")); + TRACE(("leave sessionpty")) return DROPBEAR_SUCCESS; } @@ -555,7 +555,7 @@ static int sessioncommand(struct Channel *channel, struct ChanSess *chansess, unsigned int cmdlen; int ret; - TRACE(("enter sessioncommand")); + TRACE(("enter sessioncommand")) if (chansess->cmd != NULL) { /* Note that only one command can _succeed_. The client might try @@ -612,7 +612,7 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { pid_t pid; unsigned int i; - TRACE(("enter noptycommand")); + TRACE(("enter noptycommand")) /* redirect stdin/stdout/stderr */ if (pipe(infds) != 0) @@ -635,7 +635,7 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { if ((dup2(infds[FDIN], STDIN_FILENO) < 0) || (dup2(outfds[FDOUT], STDOUT_FILENO) < 0) || (dup2(errfds[FDOUT], STDERR_FILENO) < 0)) { - TRACE(("leave noptycommand: error redirecting FDs")); + TRACE(("leave noptycommand: error redirecting FDs")) return DROPBEAR_FAILURE; } @@ -651,7 +651,7 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { } else { /* parent */ - TRACE(("continue noptycommand: parent")); + TRACE(("continue noptycommand: parent")) chansess->pid = pid; addchildpid(chansess, pid); @@ -687,7 +687,7 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { #undef FDIN #undef FDOUT - TRACE(("leave noptycommand")); + TRACE(("leave noptycommand")) return DROPBEAR_SUCCESS; } @@ -705,7 +705,7 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { char *hushpath = NULL; #endif - TRACE(("enter ptycommand")); + TRACE(("enter ptycommand")) /* we need to have a pty allocated */ if (chansess->master == -1 || chansess->tty == NULL) { @@ -728,7 +728,7 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { if ((dup2(chansess->slave, STDIN_FILENO) < 0) || (dup2(chansess->slave, STDERR_FILENO) < 0) || (dup2(chansess->slave, STDOUT_FILENO) < 0)) { - TRACE(("leave ptycommand: error redirecting filedesc")); + TRACE(("leave ptycommand: error redirecting filedesc")) return DROPBEAR_FAILURE; } @@ -776,7 +776,7 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { } else { /* parent */ - TRACE(("continue ptycommand: parent")); + TRACE(("continue ptycommand: parent")) chansess->pid = pid; /* add a child pid */ @@ -792,7 +792,7 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { } - TRACE(("leave ptycommand")); + TRACE(("leave ptycommand")) return DROPBEAR_SUCCESS; } diff --git a/svr-kex.c b/svr-kex.c index 47c540e..a9954bb 100644 --- a/svr-kex.c +++ b/svr-kex.c @@ -46,7 +46,7 @@ void recv_msg_kexdh_init() { DEF_MP_INT(dh_e); - TRACE(("enter recv_msg_kexdh_init")); + TRACE(("enter recv_msg_kexdh_init")) if (!ses.kexstate.recvkexinit) { dropbear_exit("Premature kexdh_init message received"); } @@ -60,7 +60,7 @@ void recv_msg_kexdh_init() { send_msg_newkeys(); ses.requirenext = SSH_MSG_NEWKEYS; - TRACE(("leave recv_msg_kexdh_init")); + TRACE(("leave recv_msg_kexdh_init")) } /* Generate our side of the diffie-hellman key exchange value (dh_f), and @@ -74,7 +74,7 @@ static void send_msg_kexdh_reply(mp_int *dh_e) { DEF_MP_INT(dh_y); DEF_MP_INT(dh_f); - TRACE(("enter send_msg_kexdh_reply")); + TRACE(("enter send_msg_kexdh_reply")) m_mp_init_multi(&dh_y, &dh_f, NULL); gen_kexdh_vals(&dh_f, &dh_y); @@ -99,6 +99,6 @@ static void send_msg_kexdh_reply(mp_int *dh_e) { /* the SSH_MSG_KEXDH_REPLY is done */ encrypt_packet(); - TRACE(("leave send_msg_kexdh_reply")); + TRACE(("leave send_msg_kexdh_reply")) } diff --git a/svr-main.c b/svr-main.c index 48e6042..e39f3d9 100644 --- a/svr-main.c +++ b/svr-main.c @@ -247,7 +247,7 @@ void main_noinetd() { } if (pipe(childpipe) < 0) { - TRACE(("error creating child pipe")); + TRACE(("error creating child pipe")) close(childsock); continue; } @@ -369,11 +369,11 @@ static int listensockets(int *sock, int sockcount, int *maxfd) { unsigned int sockpos = 0; int nsock; - TRACE(("listensockets: %d to try\n", svr_opts.portcount)); + TRACE(("listensockets: %d to try\n", svr_opts.portcount)) for (i = 0; i < svr_opts.portcount; i++) { - TRACE(("listening on '%s'", svr_opts.ports[i])); + TRACE(("listening on '%s'", svr_opts.ports[i])) nsock = dropbear_listen(NULL, svr_opts.ports[i], &sock[sockpos], sockcount - sockpos, diff --git a/svr-runopts.c b/svr-runopts.c index 06ffafc..375797b 100644 --- a/svr-runopts.c +++ b/svr-runopts.c @@ -270,7 +270,7 @@ void loadhostkeys() { int ret; int type; - TRACE(("enter loadhostkeys")); + TRACE(("enter loadhostkeys")) svr_opts.hostkey = new_sign_key(); @@ -300,5 +300,5 @@ void loadhostkeys() { dropbear_exit("No hostkeys available"); } - TRACE(("leave loadhostkeys")); + TRACE(("leave loadhostkeys")) } diff --git a/svr-service.c b/svr-service.c index e823490..2c78e7d 100644 --- a/svr-service.c +++ b/svr-service.c @@ -39,7 +39,7 @@ void recv_msg_service_request() { unsigned char * name; unsigned int len; - TRACE(("enter recv_msg_service_request")); + TRACE(("enter recv_msg_service_request")) name = buf_getstring(ses.payload, &len); @@ -49,7 +49,7 @@ void recv_msg_service_request() { send_msg_service_accept(name, len); m_free(name); - TRACE(("leave recv_msg_service_request: done ssh-userauth")); + TRACE(("leave recv_msg_service_request: done ssh-userauth")) return; } @@ -62,7 +62,7 @@ void recv_msg_service_request() { send_msg_service_accept(name, len); m_free(name); - TRACE(("leave recv_msg_service_request: done ssh-connection")); + TRACE(("leave recv_msg_service_request: done ssh-connection")) return; } @@ -75,7 +75,7 @@ void recv_msg_service_request() { static void send_msg_service_accept(unsigned char *name, int len) { - TRACE(("accepting service %s", name)); + TRACE(("accepting service %s", name)) CHECKCLEARTOWRITE(); diff --git a/svr-tcpfwd.c b/svr-tcpfwd.c index 68ceba5..7fbc609 100644 --- a/svr-tcpfwd.c +++ b/svr-tcpfwd.c @@ -70,10 +70,10 @@ void recv_msg_global_request_remotetcp() { unsigned int wantreply = 0; int ret = DROPBEAR_FAILURE; - TRACE(("enter recv_msg_global_request_remotetcp")); + TRACE(("enter recv_msg_global_request_remotetcp")) if (opts.noremotetcp) { - TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled")); + TRACE(("leave recv_msg_global_request_remotetcp: remote tcp forwarding disabled")) goto out; } @@ -81,7 +81,7 @@ void recv_msg_global_request_remotetcp() { wantreply = buf_getbyte(ses.payload); if (namelen > MAXNAMLEN) { - TRACE(("name len is wrong: %d", namelen)); + TRACE(("name len is wrong: %d", namelen)) goto out; } @@ -90,7 +90,7 @@ void recv_msg_global_request_remotetcp() { } else if (strcmp("cancel-tcpip-forward", reqname) == 0) { ret = svr_cancelremotetcp(); } else { - TRACE(("reqname isn't tcpip-forward: '%s'", reqname)); + TRACE(("reqname isn't tcpip-forward: '%s'", reqname)) } out: @@ -104,7 +104,7 @@ out: m_free(reqname); - TRACE(("leave recv_msg_global_request")); + TRACE(("leave recv_msg_global_request")) } @@ -143,11 +143,11 @@ static int svr_cancelremotetcp() { struct Listener * listener = NULL; struct TCPListener tcpinfo; - TRACE(("enter cancelremotetcp")); + TRACE(("enter cancelremotetcp")) bindaddr = buf_getstring(ses.payload, &addrlen); if (addrlen > MAX_IP_LEN) { - TRACE(("addr len too long: %d", addrlen)); + TRACE(("addr len too long: %d", addrlen)) goto out; } @@ -163,7 +163,7 @@ static int svr_cancelremotetcp() { out: m_free(bindaddr); - TRACE(("leave cancelremotetcp")); + TRACE(("leave cancelremotetcp")) return ret; } @@ -175,12 +175,12 @@ static int svr_remotetcpreq() { struct TCPListener *tcpinfo = NULL; unsigned int port; - TRACE(("enter remotetcpreq")); + TRACE(("enter remotetcpreq")) /* NOTE: at this stage, we ignore bindaddr. see below and listen_tcpfwd */ bindaddr = buf_getstring(ses.payload, &addrlen); if (addrlen > MAX_IP_LEN) { - TRACE(("addr len too long: %d", addrlen)); + TRACE(("addr len too long: %d", addrlen)) goto out; } @@ -192,12 +192,12 @@ static int svr_remotetcpreq() { } if (port < 1 || port > 65535) { - TRACE(("invalid port: %d", port)); + TRACE(("invalid port: %d", port)) goto out; } if (!ses.allowprivport && port < IPPORT_RESERVED) { - TRACE(("can't assign port < 1024 for non-root")); + TRACE(("can't assign port < 1024 for non-root")) goto out; } @@ -218,7 +218,7 @@ out: m_free(tcpinfo->sendaddr); m_free(tcpinfo); } - TRACE(("leave remotetcpreq")); + TRACE(("leave remotetcpreq")) return ret; } @@ -236,13 +236,13 @@ static int newtcpdirect(struct Channel * channel) { int err = SSH_OPEN_ADMINISTRATIVELY_PROHIBITED; if (opts.nolocaltcp) { - TRACE(("leave newtcpdirect: local tcp forwarding disabled")); + TRACE(("leave newtcpdirect: local tcp forwarding disabled")) goto out; } desthost = buf_getstring(ses.payload, &len); if (len > MAX_HOST_LEN) { - TRACE(("leave newtcpdirect: desthost too long")); + TRACE(("leave newtcpdirect: desthost too long")) goto out; } @@ -250,7 +250,7 @@ static int newtcpdirect(struct Channel * channel) { orighost = buf_getstring(ses.payload, &len); if (len > MAX_HOST_LEN) { - TRACE(("leave newtcpdirect: orighost too long")); + TRACE(("leave newtcpdirect: orighost too long")) goto out; } @@ -258,7 +258,7 @@ static int newtcpdirect(struct Channel * channel) { /* best be sure */ if (origport > 65535 || destport > 65535) { - TRACE(("leave newtcpdirect: port > 65535")); + TRACE(("leave newtcpdirect: port > 65535")) goto out; } @@ -266,7 +266,7 @@ static int newtcpdirect(struct Channel * channel) { sock = connect_remote(desthost, portstring, 1, NULL); if (sock < 0) { err = SSH_OPEN_CONNECT_FAILED; - TRACE(("leave newtcpdirect: sock failed")); + TRACE(("leave newtcpdirect: sock failed")) goto out; } @@ -284,7 +284,7 @@ static int newtcpdirect(struct Channel * channel) { out: m_free(desthost); m_free(orighost); - TRACE(("leave newtcpdirect: err %d", err)); + TRACE(("leave newtcpdirect: err %d", err)) return err; } diff --git a/svr-x11fwd.c b/svr-x11fwd.c index 45c9060..e15fb82 100644 --- a/svr-x11fwd.c +++ b/svr-x11fwd.c @@ -170,7 +170,7 @@ void x11cleanup(struct ChanSess *chansess) { m_free(chansess->x11authprot); m_free(chansess->x11authcookie); - TRACE(("chansess %s", chansess)); + TRACE(("chansess %s", chansess)) if (chansess->x11listener != NULL) { remove_listener(chansess->x11listener); chansess->x11listener = NULL; diff --git a/tcp-accept.c b/tcp-accept.c index c143319..e75224e 100644 --- a/tcp-accept.c +++ b/tcp-accept.c @@ -87,7 +87,7 @@ int listen_tcpfwd(struct TCPListener* tcpinfo) { int nsocks; char* errstring = NULL; - TRACE(("enter listen_tcpfwd")); + TRACE(("enter listen_tcpfwd")) /* first we try to bind, so don't need to do so much cleanup on failure */ snprintf(portstring, sizeof(portstring), "%d", tcpinfo->listenport); @@ -100,7 +100,7 @@ int listen_tcpfwd(struct TCPListener* tcpinfo) { if (nsocks < 0) { dropbear_log(LOG_INFO, "TCP forward failed: %s", errstring); m_free(errstring); - TRACE(("leave listen_tcpfwd: dropbear_listen failed")); + TRACE(("leave listen_tcpfwd: dropbear_listen failed")) return DROPBEAR_FAILURE; } @@ -109,11 +109,11 @@ int listen_tcpfwd(struct TCPListener* tcpinfo) { if (listener == NULL) { m_free(tcpinfo); - TRACE(("leave listen_tcpfwd: listener failed")); + TRACE(("leave listen_tcpfwd: listener failed")) return DROPBEAR_FAILURE; } - TRACE(("leave listen_tcpfwd: success")); + TRACE(("leave listen_tcpfwd: success")) return DROPBEAR_SUCCESS; } -- cgit v1.2.3 From 6013d993b98c747941ba343f67e138a21ed470e9 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Wed, 19 Jan 2005 11:41:04 +0000 Subject: put back 'return;' on empty terminal mode strings which was erroneously removed in 3a6c6eeefe450a64e244ee39030938ddaacb8987 --HG-- extra : convert_revision : 75ed8083a683ebdb2fceca4a856d803fdeeb60af --- svr-chansession.c | 1 + 1 file changed, 1 insertion(+) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index c365114..c04d592 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -439,6 +439,7 @@ static void get_termmodes(struct ChanSess *chansess) { if (len == 0) { TRACE(("leave get_termmodes: empty terminal modes string")) + return; } while (((opcode = buf_getbyte(ses.payload)) != 0x00) && opcode <= 159) { -- cgit v1.2.3 From f45eafe342aa07dfdc7a83e865b642f9b8ef2fa0 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Sun, 13 Mar 2005 13:58:14 +0000 Subject: * fix longstanding bug with connections being closed on failure to connect to auth socket (server) * differentiate between get_byte and get_bool * get rid of some // comments * general tidying --HG-- extra : convert_revision : fb8d188ce33b6b45804a5ce51b9f601f83bdf3d7 --- buffer.c | 10 ++++++++++ buffer.h | 1 + channel.h | 2 +- cli-auth.c | 2 +- cli-channel.c | 5 +---- cli-chansession.c | 2 +- common-channel.c | 43 ++++++++++++++----------------------------- common-kex.c | 3 +-- debug.h | 2 +- dss.c | 2 ++ kex.h | 6 +++--- scp.c | 3 --- svr-authpam.c | 2 +- svr-authpasswd.c | 2 +- svr-authpubkey.c | 2 +- svr-chansession.c | 2 +- svr-tcpfwd.c | 2 +- svr-x11fwd.c | 2 +- 18 files changed, 42 insertions(+), 51 deletions(-) (limited to 'svr-chansession.c') diff --git a/buffer.c b/buffer.c index dff861f..97045ff 100644 --- a/buffer.c +++ b/buffer.c @@ -160,6 +160,16 @@ unsigned char buf_getbyte(buffer* buf) { return buf->data[buf->pos++]; } +/* Get a bool from the buffer and increment the pos */ +unsigned char buf_getbool(buffer* buf) { + + unsigned char b; + b = buf_getbyte(buf); + if (b != 0) + b = 1; + return b; +} + /* put a byte, incrementing the length if required */ void buf_putbyte(buffer* buf, unsigned char val) { diff --git a/buffer.h b/buffer.h index e1031a1..f9aa6fa 100644 --- a/buffer.h +++ b/buffer.h @@ -50,6 +50,7 @@ void buf_setpos(buffer* buf, unsigned int pos); void buf_incrpos(buffer* buf, int incr); /* -ve is ok, to go backwards */ void buf_incrwritepos(buffer* buf, unsigned int incr); unsigned char buf_getbyte(buffer* buf); +unsigned char buf_getbool(buffer* buf); void buf_putbyte(buffer* buf, unsigned char val); unsigned char* buf_getptr(buffer* buf, unsigned int len); unsigned char* buf_getwriteptr(buffer* buf, unsigned int len); diff --git a/channel.h b/channel.h index 225fafb..a2fe87a 100644 --- a/channel.h +++ b/channel.h @@ -100,7 +100,7 @@ void chaninitialise(); void chancleanup(); void setchannelfds(fd_set *readfd, fd_set *writefd); void channelio(fd_set *readfd, fd_set *writefd); -struct Channel* getchannel(unsigned int chan); +struct Channel* getchannel(); struct Channel* newchannel(unsigned int remotechan, const struct ChanType *type, unsigned int transwindow, unsigned int transmaxpacket); diff --git a/cli-auth.c b/cli-auth.c index dfd9bbb..fc51061 100644 --- a/cli-auth.c +++ b/cli-auth.c @@ -127,7 +127,7 @@ void recv_msg_userauth_failure() { methods = buf_getstring(ses.payload, &methlen); - partial = buf_getbyte(ses.payload); + partial = buf_getbool(ses.payload); if (partial) { dropbear_log(LOG_INFO, "Authentication partially succeeded, more attempts required"); diff --git a/cli-channel.c b/cli-channel.c index 42e165b..1bd49ab 100644 --- a/cli-channel.c +++ b/cli-channel.c @@ -33,15 +33,12 @@ /* We receive channel data - only used by the client chansession code*/ void recv_msg_channel_extended_data() { - unsigned int chan; struct Channel *channel; unsigned int datatype; TRACE(("enter recv_msg_channel_extended_data")) - chan = buf_getint(ses.payload); - channel = getchannel(chan); - + channel = getchannel(); if (channel == NULL) { dropbear_exit("Unknown channel"); } diff --git a/cli-chansession.c b/cli-chansession.c index 76e9dfa..a8363ac 100644 --- a/cli-chansession.c +++ b/cli-chansession.c @@ -62,7 +62,7 @@ static void cli_chansessreq(struct Channel *channel) { TRACE(("enter cli_chansessreq")) type = buf_getstring(ses.payload, NULL); - wantreply = buf_getbyte(ses.payload); + wantreply = buf_getbool(ses.payload); if (strcmp(type, "exit-status") != 0) { TRACE(("unknown request '%s'", type)) diff --git a/common-channel.c b/common-channel.c index 6f73fab..bb7928c 100644 --- a/common-channel.c +++ b/common-channel.c @@ -162,8 +162,13 @@ struct Channel* newchannel(unsigned int remotechan, return newchan; } -/* Get the channel structure corresponding to a channel number */ -struct Channel* getchannel(unsigned int chan) { +/* Returns the channel structure corresponding to the channel in the current + * data packet (ses.payload must be positioned appropriately) */ +struct Channel* getchannel() { + + unsigned int chan; + + chan = buf_getint(ses.payload); if (chan >= ses.chansize || ses.channels[chan] == NULL) { return NULL; } @@ -474,14 +479,11 @@ void setchannelfds(fd_set *readfd, fd_set *writefd) { * etc) FD is also EOF */ void recv_msg_channel_eof() { - unsigned int chan; struct Channel * channel; TRACE(("enter recv_msg_channel_eof")) - chan = buf_getint(ses.payload); - channel = getchannel(chan); - + channel = getchannel(); if (channel == NULL) { dropbear_exit("EOF for unknown channel"); } @@ -500,15 +502,11 @@ void recv_msg_channel_eof() { /* Handle channel closure(), respond in kind and close the channels */ void recv_msg_channel_close() { - unsigned int chan; struct Channel * channel; TRACE(("enter recv_msg_channel_close")) - chan = buf_getint(ses.payload); - TRACE(("close channel = %d", chan)) - channel = getchannel(chan); - + channel = getchannel(); if (channel == NULL) { /* disconnect ? */ dropbear_exit("Close for unknown channel"); @@ -567,14 +565,11 @@ static void deletechannel(struct Channel *channel) { * such as chansession or x11fwd */ void recv_msg_channel_request() { - unsigned int chan; struct Channel *channel; TRACE(("enter recv_msg_channel_request")) - chan = buf_getint(ses.payload); - channel = getchannel(chan); - + channel = getchannel(); if (channel == NULL) { /* disconnect ? */ dropbear_exit("Unknown channel"); @@ -666,12 +661,9 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, /* We receive channel data */ void recv_msg_channel_data() { - unsigned int chan; struct Channel *channel; - chan = buf_getint(ses.payload); - channel = getchannel(chan); - + channel = getchannel(); if (channel == NULL) { dropbear_exit("Unknown channel"); } @@ -738,13 +730,10 @@ void common_recv_msg_channel_data(struct Channel *channel, int fd, * as data is sent, and incremented upon receiving window-adjust messages */ void recv_msg_channel_window_adjust() { - unsigned int chan; struct Channel * channel; unsigned int incr; - chan = buf_getint(ses.payload); - channel = getchannel(chan); - + channel = getchannel(); if (channel == NULL) { dropbear_exit("Unknown channel"); } @@ -961,14 +950,12 @@ int send_msg_channel_open_init(int fd, const struct ChanType *type) { * successful*/ void recv_msg_channel_open_confirmation() { - unsigned int chan; struct Channel * channel; int ret; TRACE(("enter recv_msg_channel_open_confirmation")) - chan = buf_getint(ses.payload); - channel = getchannel(chan); + channel = getchannel(); if (channel == NULL) { dropbear_exit("Unknown channel"); } @@ -995,11 +982,9 @@ void recv_msg_channel_open_confirmation() { /* Notification that our channel open request failed */ void recv_msg_channel_open_failure() { - unsigned int chan; struct Channel * channel; - chan = buf_getbyte(ses.payload); - channel = getchannel(chan); + channel = getchannel(); if (channel == NULL) { dropbear_exit("Unknown channel"); } diff --git a/common-kex.c b/common-kex.c index 97e341d..a2336c5 100644 --- a/common-kex.c +++ b/common-kex.c @@ -457,7 +457,6 @@ void recv_msg_kexinit() { /* the rest of ses.kexhashbuf will be done after DH exchange */ ses.kexstate.recvkexinit = 1; -// ses.expecting = 0; // client matt TRACE(("leave recv_msg_kexinit")) } @@ -683,7 +682,7 @@ static void read_kex_algos() { buf_eatstring(ses.payload); /* first_kex_packet_follows */ - if (buf_getbyte(ses.payload)) { + if (buf_getbool(ses.payload)) { ses.kexstate.firstfollows = 1; /* if the guess wasn't good, we ignore the packet sent */ if (!allgood) { diff --git a/debug.h b/debug.h index 7b1e2b5..93cb891 100644 --- a/debug.h +++ b/debug.h @@ -39,7 +39,7 @@ * Caution: Don't use this in an unfriendly environment (ie unfirewalled), * since the printing may not sanitise strings etc. This will add a reasonable * amount to your executable size. */ - //#define DEBUG_TRACE +/*#define DEBUG_TRACE */ /* All functions writing to the cleartext payload buffer call * CHECKCLEARTOWRITE() before writing. This is only really useful if you're diff --git a/dss.c b/dss.c index 6429ede..7b3d9d4 100644 --- a/dss.c +++ b/dss.c @@ -261,6 +261,7 @@ out: } #endif /* DROPBEAR_SIGNKEY_VERIFY */ +#ifdef DSS_PROTOK /* convert an unsigned mp into an array of bytes, malloced. * This array must be freed after use, len contains the length of the array, * if len != NULL */ @@ -279,6 +280,7 @@ static unsigned char* mptobytes(mp_int *mp, int *len) { } return ret; } +#endif /* Sign the data presented with key, writing the signature contents * to the buffer diff --git a/kex.h b/kex.h index 01626ed..92b6c42 100644 --- a/kex.h +++ b/kex.h @@ -37,10 +37,10 @@ void gen_kexdh_vals(mp_int *dh_pub, mp_int *dh_priv); void kexdh_comb_key(mp_int *dh_pub_us, mp_int *dh_priv, mp_int *dh_pub_them, sign_key *hostkey); -void recv_msg_kexdh_init(); // server +void recv_msg_kexdh_init(); /* server */ -void send_msg_kexdh_init(); // client -void recv_msg_kexdh_reply(); // client +void send_msg_kexdh_init(); /* client */ +void recv_msg_kexdh_reply(); /* client */ extern const unsigned char dh_p_val[]; #define DH_P_LEN 128 /* The length of the dh_p_val array */ diff --git a/scp.c b/scp.c index e356b8b..ccb6c2a 100644 --- a/scp.c +++ b/scp.c @@ -244,9 +244,6 @@ main(int argc, char **argv) extern char *optarg; extern int optind; - /* hack, seems to work */ -// __progname = argv[0]; - args.list = NULL; addargs(&args, "ssh"); /* overwritten with ssh_program */ addargs(&args, "-x"); diff --git a/svr-authpam.c b/svr-authpam.c index e045b74..fe1f123 100644 --- a/svr-authpam.c +++ b/svr-authpam.c @@ -155,7 +155,7 @@ void svr_auth_pam() { unsigned char changepw; /* check if client wants to change password */ - changepw = buf_getbyte(ses.payload); + changepw = buf_getbool(ses.payload); if (changepw) { /* not implemented by this server */ send_msg_userauth_failure(0, 1); diff --git a/svr-authpasswd.c b/svr-authpasswd.c index 4348817..5be1e2a 100644 --- a/svr-authpasswd.c +++ b/svr-authpasswd.c @@ -71,7 +71,7 @@ void svr_auth_password() { } /* check if client wants to change password */ - changepw = buf_getbyte(ses.payload); + changepw = buf_getbool(ses.payload); if (changepw) { /* not implemented by this server */ send_msg_userauth_failure(0, 1); diff --git a/svr-authpubkey.c b/svr-authpubkey.c index 5daba0f..dcd59f0 100644 --- a/svr-authpubkey.c +++ b/svr-authpubkey.c @@ -64,7 +64,7 @@ void svr_auth_pubkey() { /* 0 indicates user just wants to check if key can be used, 1 is an * actual attempt*/ - testkey = (buf_getbyte(ses.payload) == 0); + testkey = (buf_getbool(ses.payload) == 0); algo = buf_getstring(ses.payload, &algolen); keybloblen = buf_getint(ses.payload); diff --git a/svr-chansession.c b/svr-chansession.c index c04d592..90c82a7 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -305,7 +305,7 @@ static void chansessionrequest(struct Channel *channel) { TRACE(("enter chansessionrequest")) type = buf_getstring(ses.payload, &typelen); - wantreply = buf_getbyte(ses.payload); + wantreply = buf_getbool(ses.payload); if (typelen > MAX_NAME_LEN) { TRACE(("leave chansessionrequest: type too long")) /* XXX send error?*/ diff --git a/svr-tcpfwd.c b/svr-tcpfwd.c index 7fbc609..3acc4ff 100644 --- a/svr-tcpfwd.c +++ b/svr-tcpfwd.c @@ -78,7 +78,7 @@ void recv_msg_global_request_remotetcp() { } reqname = buf_getstring(ses.payload, &namelen); - wantreply = buf_getbyte(ses.payload); + wantreply = buf_getbool(ses.payload); if (namelen > MAXNAMLEN) { TRACE(("name len is wrong: %d", namelen)) diff --git a/svr-x11fwd.c b/svr-x11fwd.c index e15fb82..cbc8a79 100644 --- a/svr-x11fwd.c +++ b/svr-x11fwd.c @@ -52,7 +52,7 @@ int x11req(struct ChanSess * chansess) { return DROPBEAR_FAILURE; } - chansess->x11singleconn = buf_getbyte(ses.payload); + chansess->x11singleconn = buf_getbool(ses.payload); chansess->x11authprot = buf_getstring(ses.payload, NULL); chansess->x11authcookie = buf_getstring(ses.payload, NULL); chansess->x11screennum = buf_getint(ses.payload); -- cgit v1.2.3 From a8785cf71955edac8e54c09287bfb97ffcf760c4 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Fri, 27 May 2005 16:34:57 +0000 Subject: Seperate out exit errors from setgid()/initgroups(), and errors from setuid(), to make debugging a bit easier --HG-- extra : convert_revision : ea1f23ab2f94d0416e0bac38f63c9850396184c4 --- svr-chansession.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index 90c82a7..a040a47 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -862,8 +862,10 @@ static void execchild(struct ChanSess *chansess) { if ((setgid(ses.authstate.pw->pw_gid) < 0) || (initgroups(ses.authstate.pw->pw_name, - ses.authstate.pw->pw_gid) < 0) || - (setuid(ses.authstate.pw->pw_uid) < 0)) { + ses.authstate.pw->pw_gid) < 0)) { + dropbear_exit("error changing user group"); + } + if (setuid(ses.authstate.pw->pw_uid) < 0) { dropbear_exit("error changing user"); } } else { -- cgit v1.2.3 From 975b94c5f2f5bbb8aa455445ffb26092d01568e5 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 2 Jun 2005 22:53:18 +0000 Subject: * fix off-by-one closing open FDs --HG-- extra : convert_revision : cb23685eb24523d372c07830faea3a96e7c37917 --- svr-chansession.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index a040a47..1704c6e 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -837,7 +837,7 @@ static void execchild(struct ChanSess *chansess) { /* close file descriptors except stdin/stdout/stderr * Need to be sure FDs are closed here to avoid reading files as root */ - for (i = 3; i < (unsigned int)ses.maxfd; i++) { + for (i = 3; i <= (unsigned int)ses.maxfd; i++) { if (m_close(i) == DROPBEAR_FAILURE) { dropbear_exit("Error closing file desc"); } -- cgit v1.2.3 From 53681cbdb64fbd4a4dc18be76781c94a468a6042 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Mon, 5 Sep 2005 15:16:10 +0000 Subject: * use own assertions which should get logged properly --HG-- extra : convert_revision : 3dc365619f0840ab5781660b1257a9f22c05d3fe --- buffer.c | 4 ++-- circbuffer.c | 14 +++++++------- common-channel.c | 14 +++++++------- common-session.c | 2 +- dbutil.c | 4 ++++ dbutil.h | 4 ++++ dss.c | 16 ++++++++-------- keyimport.c | 18 +++++++++--------- packet.c | 10 +++++----- queue.c | 4 ++-- rsa.c | 20 ++++++++++---------- svr-authpubkey.c | 1 - svr-chansession.c | 14 +++++++------- svr-main.c | 2 +- 14 files changed, 67 insertions(+), 60 deletions(-) (limited to 'svr-chansession.c') diff --git a/buffer.c b/buffer.c index 97045ff..579fa6f 100644 --- a/buffer.c +++ b/buffer.c @@ -153,7 +153,7 @@ void buf_incrpos(buffer* buf, int incr) { unsigned char buf_getbyte(buffer* buf) { /* This check is really just ==, but the >= allows us to check for the - * assert()able case of pos > len, which should _never_ happen. */ + * bad case of pos > len, which should _never_ happen. */ if (buf->pos >= buf->len) { dropbear_exit("bad buf_getbyte"); } @@ -270,7 +270,7 @@ void buf_putmpint(buffer* buf, mp_int * mp) { unsigned int len, pad = 0; TRACE(("enter buf_putmpint")) - assert(mp != NULL); + dropbear_assert(mp != NULL); if (SIGN(mp) == MP_NEG) { dropbear_exit("negative bignum"); diff --git a/circbuffer.c b/circbuffer.c index 6dc9179..e70087a 100644 --- a/circbuffer.c +++ b/circbuffer.c @@ -66,8 +66,8 @@ unsigned int cbuf_getavail(circbuffer * cbuf) { unsigned int cbuf_readlen(circbuffer *cbuf) { - assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size); - assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); + dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size); + dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); if (cbuf->used == 0) { TRACE(("cbuf_readlen: unused buffer")) @@ -83,9 +83,9 @@ unsigned int cbuf_readlen(circbuffer *cbuf) { unsigned int cbuf_writelen(circbuffer *cbuf) { - assert(cbuf->used <= cbuf->size); - assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size); - assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); + dropbear_assert(cbuf->used <= cbuf->size); + dropbear_assert(((2*cbuf->size)+cbuf->writepos-cbuf->readpos)%cbuf->size == cbuf->used%cbuf->size); + dropbear_assert(((2*cbuf->size)+cbuf->readpos-cbuf->writepos)%cbuf->size == (cbuf->size-cbuf->used)%cbuf->size); if (cbuf->used == cbuf->size) { TRACE(("cbuf_writelen: full buffer")) @@ -122,7 +122,7 @@ void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) { } cbuf->used += len; - assert(cbuf->used <= cbuf->size); + dropbear_assert(cbuf->used <= cbuf->size); cbuf->writepos = (cbuf->writepos + len) % cbuf->size; } @@ -132,7 +132,7 @@ void cbuf_incrread(circbuffer *cbuf, unsigned int len) { dropbear_exit("bad cbuf read"); } - assert(cbuf->used >= len); + dropbear_assert(cbuf->used >= len); cbuf->used -= len; cbuf->readpos = (cbuf->readpos + len) % cbuf->size; } diff --git a/common-channel.c b/common-channel.c index dfff1b4..12e7aa7 100644 --- a/common-channel.c +++ b/common-channel.c @@ -409,9 +409,9 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { channel->recvdonelen = 0; } - assert(channel->recvwindow <= RECV_MAXWINDOW); - assert(channel->recvwindow <= cbuf_getavail(channel->writebuf)); - assert(channel->extrabuf == NULL || + dropbear_assert(channel->recvwindow <= RECV_MAXWINDOW); + dropbear_assert(channel->recvwindow <= cbuf_getavail(channel->writebuf)); + dropbear_assert(channel->extrabuf == NULL || channel->recvwindow <= cbuf_getavail(channel->extrabuf)); @@ -603,14 +603,14 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, CHECKCLEARTOWRITE(); - assert(!channel->sentclosed); + dropbear_assert(!channel->sentclosed); if (isextended) { fd = channel->errfd; } else { fd = channel->outfd; } - assert(fd >= 0); + dropbear_assert(fd >= 0); maxlen = MIN(channel->transwindow, channel->transmaxpacket); /* -(1+4+4) is SSH_MSG_CHANNEL_DATA, channel number, string length, and @@ -718,9 +718,9 @@ void common_recv_msg_channel_data(struct Channel *channel, int fd, len -= buflen; } - assert(channel->recvwindow >= datalen); + dropbear_assert(channel->recvwindow >= datalen); channel->recvwindow -= datalen; - assert(channel->recvwindow <= RECV_MAXWINDOW); + dropbear_assert(channel->recvwindow <= RECV_MAXWINDOW); TRACE(("leave recv_msg_channel_data")) } diff --git a/common-session.c b/common-session.c index 06cdbd1..a8cab87 100644 --- a/common-session.c +++ b/common-session.c @@ -126,7 +126,7 @@ void session_loop(void(*loophandler)()) { timeout.tv_usec = 0; FD_ZERO(&writefd); FD_ZERO(&readfd); - assert(ses.payload == NULL); + dropbear_assert(ses.payload == NULL); if (ses.sock != -1) { FD_SET(ses.sock, &readfd); if (!isempty(&ses.writequeue)) { diff --git a/dbutil.c b/dbutil.c index 45c720e..23e46cc 100644 --- a/dbutil.c +++ b/dbutil.c @@ -110,6 +110,10 @@ static void generic_dropbear_exit(int exitcode, const char* format, exit(exitcode); } +void fail_assert(const char* expr, const char* file, int line) { + dropbear_exit("failed assertion (%s:%d): `%s'", file, line, expr); +} + static void generic_dropbear_log(int UNUSED(priority), const char* format, va_list param) { diff --git a/dbutil.h b/dbutil.h index d904949..eef75e3 100644 --- a/dbutil.h +++ b/dbutil.h @@ -39,6 +39,7 @@ extern void (*_dropbear_log)(int priority, const char* format, va_list param); void dropbear_exit(const char* format, ...); void dropbear_close(const char* format, ...); void dropbear_log(int priority, const char* format, ...); +void fail_assert(const char* expr, const char* file, int line); #ifdef DEBUG_TRACE void dropbear_trace(const char* format, ...); void printhex(const char * label, const unsigned char * buf, int len); @@ -66,4 +67,7 @@ void setnonblocking(int fd); /* Used to force mp_ints to be initialised */ #define DEF_MP_INT(X) mp_int X = {0, 0, 0, NULL} +/* Dropbear assertion */ +#define dropbear_assert(X) do { if (!(X)) { fail_assert(#X, __FILE__, __LINE__); } } while (0) + #endif /* _DBUTIL_H_ */ diff --git a/dss.c b/dss.c index cb877ca..84a093c 100644 --- a/dss.c +++ b/dss.c @@ -46,7 +46,7 @@ int buf_get_dss_pub_key(buffer* buf, dss_key *key) { TRACE(("enter buf_get_dss_pub_key")) - assert(key != NULL); + dropbear_assert(key != NULL); key->p = m_malloc(sizeof(mp_int)); key->q = m_malloc(sizeof(mp_int)); key->g = m_malloc(sizeof(mp_int)); @@ -80,7 +80,7 @@ int buf_get_dss_priv_key(buffer* buf, dss_key *key) { int ret = DROPBEAR_FAILURE; - assert(key != NULL); + dropbear_assert(key != NULL); ret = buf_get_dss_pub_key(buf, key); if (ret == DROPBEAR_FAILURE) { @@ -137,7 +137,7 @@ void dss_key_free(dss_key *key) { */ void buf_put_dss_pub_key(buffer* buf, dss_key *key) { - assert(key != NULL); + dropbear_assert(key != NULL); buf_putstring(buf, SSH_SIGNKEY_DSS, SSH_SIGNKEY_DSS_LEN); buf_putmpint(buf, key->p); buf_putmpint(buf, key->q); @@ -149,7 +149,7 @@ void buf_put_dss_pub_key(buffer* buf, dss_key *key) { /* Same as buf_put_dss_pub_key, but with the private "x" key appended */ void buf_put_dss_priv_key(buffer* buf, dss_key *key) { - assert(key != NULL); + dropbear_assert(key != NULL); buf_put_dss_pub_key(buf, key); buf_putmpint(buf, key->x); @@ -172,7 +172,7 @@ int buf_dss_verify(buffer* buf, dss_key *key, const unsigned char* data, int stringlen; TRACE(("enter buf_dss_verify")) - assert(key != NULL); + dropbear_assert(key != NULL); m_mp_init_multi(&val1, &val2, &val3, &val4, NULL); @@ -310,7 +310,7 @@ void buf_put_dss_sign(buffer* buf, dss_key *key, const unsigned char* data, hash_state hs; TRACE(("enter buf_put_dss_sign")) - assert(key != NULL); + dropbear_assert(key != NULL); /* hash the data */ sha1_init(&hs); @@ -380,7 +380,7 @@ void buf_put_dss_sign(buffer* buf, dss_key *key, const unsigned char* data, buf_putint(buf, 2*SHA1_HASH_SIZE); writelen = mp_unsigned_bin_size(&dss_r); - assert(writelen <= SHA1_HASH_SIZE); + dropbear_assert(writelen <= SHA1_HASH_SIZE); /* need to pad to 160 bits with leading zeros */ for (i = 0; i < SHA1_HASH_SIZE - writelen; i++) { buf_putbyte(buf, 0); @@ -393,7 +393,7 @@ void buf_put_dss_sign(buffer* buf, dss_key *key, const unsigned char* data, buf_incrwritepos(buf, writelen); writelen = mp_unsigned_bin_size(&dss_s); - assert(writelen <= SHA1_HASH_SIZE); + dropbear_assert(writelen <= SHA1_HASH_SIZE); /* need to pad to 160 bits with leading zeros */ for (i = 0; i < SHA1_HASH_SIZE - writelen; i++) { buf_putbyte(buf, 0); diff --git a/keyimport.c b/keyimport.c index 9f2305d..589b9cb 100644 --- a/keyimport.c +++ b/keyimport.c @@ -203,7 +203,7 @@ static void base64_encode_fp(FILE * fp, unsigned char *data, unsigned long outlen; int rawcpl; rawcpl = cpl * 3 / 4; - assert((unsigned int)cpl < sizeof(out)); + dropbear_assert((unsigned int)cpl < sizeof(out)); while (datalen > 0) { n = (datalen < rawcpl ? datalen : rawcpl); @@ -714,7 +714,7 @@ static int openssh_write(const char *filename, sign_key *key, } #endif - assert(keytype != -1); + dropbear_assert(keytype != -1); /* * Fetch the key blobs. @@ -913,7 +913,7 @@ static int openssh_write(const char *filename, sign_key *key, * with the same value. Those are all removed and the rest is * returned. */ - assert(pos == len); + dropbear_assert(pos == len); while (pos < outlen) { outblob[pos++] = outlen - len; } @@ -1491,7 +1491,7 @@ sign_key *sshcom_read(const char *filename, char *passphrase) privlen = pos - publen; } - assert(privlen > 0); /* should have bombed by now if not */ + dropbear_assert(privlen > 0); /* should have bombed by now if not */ retkey = snew(struct ssh2_userkey); retkey->alg = alg; @@ -1557,7 +1557,7 @@ int sshcom_write(const char *filename, sign_key *key, pos += ssh2_read_mpint(privblob+pos, privlen-pos, &q); pos += ssh2_read_mpint(privblob+pos, privlen-pos, &iqmp); - assert(e.start && iqmp.start); /* can't go wrong */ + dropbear_assert(e.start && iqmp.start); /* can't go wrong */ numbers[0] = e; numbers[1] = d; @@ -1581,7 +1581,7 @@ int sshcom_write(const char *filename, sign_key *key, pos = 0; pos += ssh2_read_mpint(privblob+pos, privlen-pos, &x); - assert(y.start && x.start); /* can't go wrong */ + dropbear_assert(y.start && x.start); /* can't go wrong */ numbers[0] = p; numbers[1] = g; @@ -1593,7 +1593,7 @@ int sshcom_write(const char *filename, sign_key *key, initial_zero = 1; type = "dl-modp{sign{dsa-nist-sha1},dh{plain}}"; } else { - assert(0); /* zoinks! */ + dropbear_assert(0); /* zoinks! */ } /* @@ -1637,13 +1637,13 @@ int sshcom_write(const char *filename, sign_key *key, } ciphertext = (char *)outblob+lenpos+4; cipherlen = pos - (lenpos+4); - assert(!passphrase || cipherlen % 8 == 0); + dropbear_assert(!passphrase || cipherlen % 8 == 0); /* Wrap up the encrypted blob string. */ PUT_32BIT(outblob+lenpos, cipherlen); /* And finally fill in the total length field. */ PUT_32BIT(outblob+4, pos); - assert(pos < outlen); + dropbear_assert(pos < outlen); /* * Encrypt the key. diff --git a/packet.c b/packet.c index 6c90a76..b2c6174 100644 --- a/packet.c +++ b/packet.c @@ -53,13 +53,13 @@ void write_packet() { buffer * writebuf = NULL; TRACE(("enter write_packet")) - assert(!isempty(&ses.writequeue)); + dropbear_assert(!isempty(&ses.writequeue)); /* Get the next buffer in the queue of encrypted packets to write*/ writebuf = (buffer*)examine(&ses.writequeue); len = writebuf->len - writebuf->pos; - assert(len > 0); + dropbear_assert(len > 0); /* Try to write as much as possible */ written = write(ses.sock, buf_getptr(writebuf, len), len); @@ -118,7 +118,7 @@ void read_packet() { /* Attempt to read the remainder of the packet, note that there * mightn't be any available (EAGAIN) */ - assert(ses.readbuf != NULL); + dropbear_assert(ses.readbuf != NULL); maxlen = ses.readbuf->len - ses.readbuf->pos; len = read(ses.sock, buf_getptr(ses.readbuf, maxlen), maxlen); @@ -162,7 +162,7 @@ static void read_packet_init() { if (ses.readbuf == NULL) { /* start of a new packet */ ses.readbuf = buf_new(INIT_READBUF); - assert(ses.decryptreadbuf == NULL); + dropbear_assert(ses.decryptreadbuf == NULL); ses.decryptreadbuf = buf_new(blocksize); } @@ -600,7 +600,7 @@ static void buf_compress(buffer * dest, buffer * src, unsigned int len) { break; } - assert(ses.keys->trans_zstream->avail_out == 0); + dropbear_assert(ses.keys->trans_zstream->avail_out == 0); /* the buffer has been filled, we must extend. This only happens in * unusual circumstances where the data grows in size after deflate(), diff --git a/queue.c b/queue.c index caf6145..7a80124 100644 --- a/queue.c +++ b/queue.c @@ -42,7 +42,7 @@ void* dequeue(struct Queue* queue) { void* ret; struct Link* oldhead; - assert(!isempty(queue)); + dropbear_assert(!isempty(queue)); ret = queue->head->item; oldhead = queue->head; @@ -62,7 +62,7 @@ void* dequeue(struct Queue* queue) { void *examine(struct Queue* queue) { - assert(!isempty(queue)); + dropbear_assert(!isempty(queue)); return queue->head->item; } diff --git a/rsa.c b/rsa.c index 7248bed..cc16fa0 100644 --- a/rsa.c +++ b/rsa.c @@ -49,7 +49,7 @@ static void rsa_pad_em(rsa_key * key, int buf_get_rsa_pub_key(buffer* buf, rsa_key *key) { TRACE(("enter buf_get_rsa_pub_key")) - assert(key != NULL); + dropbear_assert(key != NULL); key->e = m_malloc(sizeof(mp_int)); key->n = m_malloc(sizeof(mp_int)); m_mp_init_multi(key->e, key->n, NULL); @@ -80,7 +80,7 @@ int buf_get_rsa_pub_key(buffer* buf, rsa_key *key) { * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ int buf_get_rsa_priv_key(buffer* buf, rsa_key *key) { - assert(key != NULL); + dropbear_assert(key != NULL); TRACE(("enter buf_get_rsa_priv_key")) @@ -163,7 +163,7 @@ void rsa_key_free(rsa_key *key) { void buf_put_rsa_pub_key(buffer* buf, rsa_key *key) { TRACE(("enter buf_put_rsa_pub_key")) - assert(key != NULL); + dropbear_assert(key != NULL); buf_putstring(buf, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN); buf_putmpint(buf, key->e); @@ -178,7 +178,7 @@ void buf_put_rsa_priv_key(buffer* buf, rsa_key *key) { TRACE(("enter buf_put_rsa_priv_key")) - assert(key != NULL); + dropbear_assert(key != NULL); buf_put_rsa_pub_key(buf, key); buf_putmpint(buf, key->d); @@ -209,7 +209,7 @@ int buf_rsa_verify(buffer * buf, rsa_key *key, const unsigned char* data, TRACE(("enter buf_rsa_verify")) - assert(key != NULL); + dropbear_assert(key != NULL); m_mp_init_multi(&rsa_mdash, &rsa_s, &rsa_em, NULL); @@ -267,7 +267,7 @@ void buf_put_rsa_sign(buffer* buf, rsa_key *key, const unsigned char* data, unsigned char *tmpbuf; TRACE(("enter buf_put_rsa_sign")) - assert(key != NULL); + dropbear_assert(key != NULL); m_mp_init_multi(&rsa_s, &rsa_tmp1, &rsa_tmp2, &rsa_tmp3, NULL); @@ -320,7 +320,7 @@ void buf_put_rsa_sign(buffer* buf, rsa_key *key, const unsigned char* data, buf_putint(buf, nsize); /* pad out s to same length as n */ ssize = mp_unsigned_bin_size(&rsa_s); - assert(ssize <= nsize); + dropbear_assert(ssize <= nsize); for (i = 0; i < nsize-ssize; i++) { buf_putbyte(buf, 0x00); } @@ -365,8 +365,8 @@ static void rsa_pad_em(rsa_key * key, hash_state hs; unsigned int nsize; - assert(key != NULL); - assert(data != NULL); + dropbear_assert(key != NULL); + dropbear_assert(data != NULL); nsize = mp_unsigned_bin_size(key->n); rsa_EM = buf_new(nsize-1); @@ -387,7 +387,7 @@ static void rsa_pad_em(rsa_key * key, sha1_done(&hs, buf_getwriteptr(rsa_EM, SHA1_HASH_SIZE)); buf_incrwritepos(rsa_EM, SHA1_HASH_SIZE); - assert(rsa_EM->pos == rsa_EM->size); + dropbear_assert(rsa_EM->pos == rsa_EM->size); /* Create the mp_int from the encoded bytes */ buf_setpos(rsa_EM, 0); diff --git a/svr-authpubkey.c b/svr-authpubkey.c index dcd59f0..a5520e5 100644 --- a/svr-authpubkey.c +++ b/svr-authpubkey.c @@ -266,7 +266,6 @@ static int checkpubkeyperms() { TRACE(("enter checkpubkeyperms")) - assert(ses.authstate.pw); if (ses.authstate.pw->pw_dir == NULL) { goto out; } diff --git a/svr-chansession.c b/svr-chansession.c index 1704c6e..6828537 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -148,8 +148,8 @@ static void send_exitsignalstatus(struct Channel *channel) { static void send_msg_chansess_exitstatus(struct Channel * channel, struct ChanSess * chansess) { - assert(chansess->exit.exitpid != -1); - assert(chansess->exit.exitsignal == -1); + dropbear_assert(chansess->exit.exitpid != -1); + dropbear_assert(chansess->exit.exitsignal == -1); CHECKCLEARTOWRITE(); @@ -170,8 +170,8 @@ static void send_msg_chansess_exitsignal(struct Channel * channel, int i; char* signame = NULL; - assert(chansess->exit.exitpid != -1); - assert(chansess->exit.exitsignal > 0); + dropbear_assert(chansess->exit.exitpid != -1); + dropbear_assert(chansess->exit.exitsignal > 0); CHECKCLEARTOWRITE(); @@ -205,7 +205,7 @@ static int newchansess(struct Channel *channel) { struct ChanSess *chansess; - assert(channel->typedata == NULL); + dropbear_assert(channel->typedata == NULL); chansess = (struct ChanSess*)m_malloc(sizeof(struct ChanSess)); chansess->cmd = NULL; @@ -279,7 +279,7 @@ static void closechansess(struct Channel *channel) { /* clear child pid entries */ for (i = 0; i < svr_ses.childpidsize; i++) { if (svr_ses.childpids[i].chansess == chansess) { - assert(svr_ses.childpids[i].pid > 0); + dropbear_assert(svr_ses.childpids[i].pid > 0); TRACE(("closing pid %d", svr_ses.childpids[i].pid)) TRACE(("exitpid = %d", chansess->exit.exitpid)) svr_ses.childpids[i].pid = -1; @@ -313,7 +313,7 @@ static void chansessionrequest(struct Channel *channel) { } chansess = (struct ChanSess*)channel->typedata; - assert(chansess != NULL); + dropbear_assert(chansess != NULL); TRACE(("type is %s", type)) if (strcmp(type, "window-change") == 0) { diff --git a/svr-main.c b/svr-main.c index e39f3d9..46f6078 100644 --- a/svr-main.c +++ b/svr-main.c @@ -284,7 +284,7 @@ void main_noinetd() { getaddrhostname(&remoteaddr), addrstring); /* don't return */ - assert(0); + dropbear_assert(0); } /* parent */ -- cgit v1.2.3 From b131f74533ea87d635b62874c35240b8390825e9 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 20 Oct 2005 16:53:12 +0000 Subject: * rename infd/outfd to writefd/readfd, to avoid confusion --HG-- extra : convert_revision : e3e7dc2cf75ad60c83a5b4307c210fee2fe90434 --- channel.h | 6 +-- cli-chansession.c | 4 +- cli-tcpfwd.c | 6 +-- common-channel.c | 132 +++++++++++++++++++++++++++--------------------------- svr-chansession.c | 16 +++---- svr-tcpfwd.c | 6 +-- 6 files changed, 83 insertions(+), 87 deletions(-) (limited to 'svr-chansession.c') diff --git a/channel.h b/channel.h index 872bb25..7030a0f 100644 --- a/channel.h +++ b/channel.h @@ -65,9 +65,9 @@ struct Channel { unsigned int recvdonelen; unsigned int recvmaxpacket, transmaxpacket; void* typedata; /* a pointer to type specific data */ - int infd; /* data to send over the wire */ - int outfd; /* data for consumption, what was in writebuf */ - int errfd; /* used like infd or errfd, depending if it's client or server. + int writefd; /* read from wire, written to insecure side */ + int readfd; /* read from insecure size, written to wire */ + int errfd; /* used like writefd or readfd, depending if it's client or server. Doesn't exactly belong here, but is cleaner here */ circbuffer *writebuf; /* data from the wire, for local consumption */ circbuffer *extrabuf; /* extended-data for the program - used like writebuf diff --git a/cli-chansession.c b/cli-chansession.c index a8363ac..6d358b7 100644 --- a/cli-chansession.c +++ b/cli-chansession.c @@ -340,10 +340,10 @@ static void send_chansess_shell_req(struct Channel *channel) { static int cli_initchansess(struct Channel *channel) { - channel->infd = STDOUT_FILENO; + channel->writefd = STDOUT_FILENO; setnonblocking(STDOUT_FILENO); - channel->outfd = STDIN_FILENO; + channel->readfd = STDIN_FILENO; setnonblocking(STDIN_FILENO); channel->errfd = STDERR_FILENO; diff --git a/cli-tcpfwd.c b/cli-tcpfwd.c index 300a2fa..6832cbf 100644 --- a/cli-tcpfwd.c +++ b/cli-tcpfwd.c @@ -186,11 +186,9 @@ static int newtcpforwarded(struct Channel * channel) { ses.maxfd = MAX(ses.maxfd, sock); - /* Note that infd is actually the "outgoing" direction on the - * tcp connection, vice versa for outfd. - * We don't set outfd, that will get set after the connection's + /* We don't set readfd, that will get set after the connection's * progress succeeds */ - channel->infd = sock; + channel->writefd = sock; channel->initconn = 1; err = SSH_OPEN_IN_PROGRESS; diff --git a/common-channel.c b/common-channel.c index e71a7b7..54829e5 100644 --- a/common-channel.c +++ b/common-channel.c @@ -52,8 +52,8 @@ static void deletechannel(struct Channel *channel); static void checkinitdone(struct Channel *channel); static void checkclose(struct Channel *channel); -static void closeinfd(struct Channel * channel); -static void closeoutfd(struct Channel * channel, int fd); +static void closewritefd(struct Channel * channel); +static void closereadfd(struct Channel * channel, int fd); static void closechanfd(struct Channel *channel, int fd, int how); #define FD_UNINIT (-2) @@ -143,8 +143,8 @@ struct Channel* newchannel(unsigned int remotechan, newchan->transmaxpacket = transmaxpacket; newchan->typedata = NULL; - newchan->infd = FD_UNINIT; - newchan->outfd = FD_UNINIT; + newchan->writefd = FD_UNINIT; + newchan->readfd = FD_UNINIT; newchan->errfd = FD_CLOSED; /* this isn't always set to start with */ newchan->initconn = 0; newchan->await_open = 0; @@ -177,7 +177,7 @@ struct Channel* getchannel() { } /* Iterate through the channels, performing IO if available */ -void channelio(fd_set *readfd, fd_set *writefd) { +void channelio(fd_set *readfds, fd_set *writefds) { struct Channel *channel; unsigned int i; @@ -192,21 +192,21 @@ void channelio(fd_set *readfd, fd_set *writefd) { continue; } - /* read from program/pipe stdout */ - if (channel->outfd >= 0 && FD_ISSET(channel->outfd, readfd)) { + /* read data and send it over the wire */ + if (channel->readfd >= 0 && FD_ISSET(channel->readfd, readfds)) { send_msg_channel_data(channel, 0, 0); } - /* read from program/pipe stderr */ + /* read stderr data and send it over the wire */ if (channel->extrabuf == NULL && - channel->errfd >= 0 && FD_ISSET(channel->errfd, readfd)) { + channel->errfd >= 0 && FD_ISSET(channel->errfd, readfds)) { send_msg_channel_data(channel, 1, SSH_EXTENDED_DATA_STDERR); } - /* if we can read from the infd, it might be closed, so we try to + /* if we can read from the writefd, it might be closed, so we try to * see if it has errors */ - if (channel->infd >= 0 && channel->infd != channel->outfd - && FD_ISSET(channel->infd, readfd)) { + if (channel->writefd >= 0 && channel->writefd != channel->readfd + && FD_ISSET(channel->writefd, readfds)) { if (channel->initconn) { /* Handling for "in progress" connection - this is needed * to avoid spinning 100% CPU when we connect to a server @@ -215,25 +215,25 @@ void channelio(fd_set *readfd, fd_set *writefd) { continue; /* Important not to use the channel after checkinitdone(), as it may be NULL */ } - ret = write(channel->infd, NULL, 0); /* Fake write */ + ret = write(channel->writefd, NULL, 0); /* Fake write */ if (ret < 0 && errno != EINTR && errno != EAGAIN) { - closeinfd(channel); + closewritefd(channel); } } /* write to program/pipe stdin */ - if (channel->infd >= 0 && FD_ISSET(channel->infd, writefd)) { + if (channel->writefd >= 0 && FD_ISSET(channel->writefd, writefds)) { if (channel->initconn) { checkinitdone(channel); continue; /* Important not to use the channel after checkinitdone(), as it may be NULL */ } - writechannel(channel, channel->infd, channel->writebuf); + writechannel(channel, channel->writefd, channel->writebuf); } /* stderr for client mode */ if (channel->extrabuf != NULL - && channel->errfd >= 0 && FD_ISSET(channel->errfd, writefd)) { + && channel->errfd >= 0 && FD_ISSET(channel->errfd, writefds)) { writechannel(channel, channel->errfd, channel->extrabuf); } @@ -244,7 +244,7 @@ void channelio(fd_set *readfd, fd_set *writefd) { /* Listeners such as TCP, X11, agent-auth */ #ifdef USING_LISTENERS - handle_listeners(readfd); + handle_listeners(readfds); #endif } @@ -252,8 +252,8 @@ void channelio(fd_set *readfd, fd_set *writefd) { /* do all the EOF/close type stuff checking for a channel */ static void checkclose(struct Channel *channel) { - TRACE(("checkclose: infd %d, outfd %d, errfd %d, sentclosed %d, recvclosed %d", - channel->infd, channel->outfd, + TRACE(("checkclose: writefd %d, readfd %d, errfd %d, sentclosed %d, recvclosed %d", + channel->writefd, channel->readfd, channel->errfd, channel->sentclosed, channel->recvclosed)) TRACE(("writebuf %d extrabuf %s extrabuf %d", cbuf_getused(channel->writebuf), @@ -266,18 +266,18 @@ static void checkclose(struct Channel *channel) { * if the shell has exited etc */ if (channel->type->checkclose) { if (channel->type->checkclose(channel)) { - closeinfd(channel); + closewritefd(channel); } } if (!channel->senteof - && channel->outfd == FD_CLOSED + && channel->readfd == FD_CLOSED && (channel->extrabuf != NULL || channel->errfd == FD_CLOSED)) { send_msg_channel_eof(channel); } - if (channel->infd == FD_CLOSED - && channel->outfd == FD_CLOSED + if (channel->writefd == FD_CLOSED + && channel->readfd == FD_CLOSED && (channel->extrabuf != NULL || channel->errfd == FD_CLOSED)) { send_msg_channel_close(channel); } @@ -314,17 +314,17 @@ static void checkinitdone(struct Channel *channel) { TRACE(("enter checkinitdone")) - if (getsockopt(channel->infd, SOL_SOCKET, SO_ERROR, &val, &vallen) + if (getsockopt(channel->writefd, SOL_SOCKET, SO_ERROR, &val, &vallen) || val != 0) { send_msg_channel_open_failure(channel->remotechan, SSH_OPEN_CONNECT_FAILED, "", ""); - close(channel->infd); + close(channel->writefd); deletechannel(channel); TRACE(("leave checkinitdone: fail")) } else { send_msg_channel_open_confirmation(channel, channel->recvwindow, channel->recvmaxpacket); - channel->outfd = channel->infd; + channel->readfd = channel->writefd; channel->initconn = 0; TRACE(("leave checkinitdone: success")) } @@ -386,7 +386,7 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { if (len < 0 && errno != EINTR) { /* no more to write - we close it even if the fd was stderr, since * that's a nasty failure too */ - closeinfd(channel); + closewritefd(channel); } TRACE(("leave writechannel: len <= 0")) return; @@ -395,9 +395,9 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { cbuf_incrread(cbuf, len); channel->recvdonelen += len; - if (fd == channel->infd && len == maxlen && channel->recveof) { + if (fd == channel->writefd && len == maxlen && channel->recveof) { /* Check if we're closing up */ - closeinfd(channel); + closewritefd(channel); TRACE(("leave writechannel: recveof set")) return; } @@ -421,7 +421,7 @@ static void writechannel(struct Channel* channel, int fd, circbuffer *cbuf) { /* Set the file descriptors for the main select in session.c * This avoid channels which don't have any window available, are closed, etc*/ -void setchannelfds(fd_set *readfd, fd_set *writefd) { +void setchannelfds(fd_set *readfds, fd_set *writefds) { unsigned int i; struct Channel * channel; @@ -436,41 +436,41 @@ void setchannelfds(fd_set *readfd, fd_set *writefd) { /* Stuff to put over the wire */ if (channel->transwindow > 0) { - if (channel->outfd >= 0) { - FD_SET(channel->outfd, readfd); + if (channel->readfd >= 0) { + FD_SET(channel->readfd, readfds); } if (channel->extrabuf == NULL && channel->errfd >= 0) { - FD_SET(channel->errfd, readfd); + FD_SET(channel->errfd, readfds); } } /* For checking FD status (ie closure etc) - we don't actually - * read data from infd */ - TRACE(("infd = %d, outfd %d, errfd %d, bufused %d", - channel->infd, channel->outfd, + * read data from writefd */ + TRACE(("writefd = %d, readfd %d, errfd %d, bufused %d", + channel->writefd, channel->readfd, channel->errfd, cbuf_getused(channel->writebuf) )) - if (channel->infd >= 0 && channel->infd != channel->outfd) { - FD_SET(channel->infd, readfd); + if (channel->writefd >= 0 && channel->writefd != channel->readfd) { + FD_SET(channel->writefd, readfds); } /* Stuff from the wire, to local program/shell/user etc */ - if ((channel->infd >= 0 && cbuf_getused(channel->writebuf) > 0 ) + if ((channel->writefd >= 0 && cbuf_getused(channel->writebuf) > 0 ) || channel->initconn) { - FD_SET(channel->infd, writefd); + FD_SET(channel->writefd, writefds); } if (channel->extrabuf != NULL && channel->errfd >= 0 && cbuf_getused(channel->extrabuf) > 0 ) { - FD_SET(channel->errfd, writefd); + FD_SET(channel->errfd, writefds); } } /* foreach channel */ #ifdef USING_LISTENERS - set_listener_fds(readfd); + set_listener_fds(readfds); #endif } @@ -493,7 +493,7 @@ void recv_msg_channel_eof() { if (cbuf_getused(channel->writebuf) == 0 && (channel->extrabuf == NULL || cbuf_getused(channel->extrabuf) == 0)) { - closeinfd(channel); + closewritefd(channel); } TRACE(("leave recv_msg_channel_eof")) @@ -541,8 +541,8 @@ static void removechannel(struct Channel * channel) { /* close the FDs in case they haven't been done * yet (ie they were shutdown etc */ - close(channel->infd); - close(channel->outfd); + close(channel->writefd); + close(channel->readfd); close(channel->errfd); channel->typedata = NULL; @@ -609,7 +609,7 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, if (isextended) { fd = channel->errfd; } else { - fd = channel->outfd; + fd = channel->readfd; } dropbear_assert(fd >= 0); @@ -631,7 +631,7 @@ static void send_msg_channel_data(struct Channel *channel, int isextended, if (len <= 0) { /* on error/eof, send eof */ if (len == 0 || errno != EINTR) { - closeoutfd(channel, fd); + closereadfd(channel, fd); } buf_free(buf); buf = NULL; @@ -669,7 +669,7 @@ void recv_msg_channel_data() { dropbear_exit("Unknown channel"); } - common_recv_msg_channel_data(channel, channel->infd, channel->writebuf); + common_recv_msg_channel_data(channel, channel->writefd, channel->writebuf); } /* Shared for data and stderr data - when we receive data, put it in a buffer @@ -689,7 +689,7 @@ void common_recv_msg_channel_data(struct Channel *channel, int fd, } if (fd < 0) { - dropbear_exit("received data with bad infd"); + dropbear_exit("received data with bad writefd"); } datalen = buf_getint(ses.payload); @@ -931,7 +931,7 @@ int send_msg_channel_open_init(int fd, const struct ChanType *type) { /* set fd non-blocking */ setnonblocking(fd); - chan->infd = chan->outfd = fd; + chan->writefd = chan->readfd = fd; ses.maxfd = MAX(ses.maxfd, fd); chan->await_open = 1; @@ -1008,21 +1008,21 @@ void recv_msg_channel_open_failure() { #endif /* USING_LISTENERS */ /* close a stdout/stderr fd */ -static void closeoutfd(struct Channel * channel, int fd) { +static void closereadfd(struct Channel * channel, int fd) { - /* don't close it if it is the same as infd, - * unless infd is already set -1 */ - TRACE(("enter closeoutfd")) + /* don't close it if it is the same as writefd, + * unless writefd is already set -1 */ + TRACE(("enter closereadfd")) closechanfd(channel, fd, 0); - TRACE(("leave closeoutfd")) + TRACE(("leave closereadfd")) } /* close a stdin fd */ -static void closeinfd(struct Channel * channel) { +static void closewritefd(struct Channel * channel) { - TRACE(("enter closeinfd")) - closechanfd(channel, channel->infd, 1); - TRACE(("leave closeinfd")) + TRACE(("enter closewritefd")) + closechanfd(channel, channel->writefd, 1); + TRACE(("leave closewritefd")) } /* close a fd, how is 0 for stdout/stderr, 1 for stdin */ @@ -1044,15 +1044,15 @@ static void closechanfd(struct Channel *channel, int fd, int how) { closein = closeout = 1; } - if (closeout && fd == channel->outfd) { - channel->outfd = FD_CLOSED; + if (closeout && fd == channel->readfd) { + channel->readfd = FD_CLOSED; } if (closeout && (channel->extrabuf == NULL) && (fd == channel->errfd)) { channel->errfd = FD_CLOSED; } - if (closein && fd == channel->infd) { - channel->infd = FD_CLOSED; + if (closein && fd == channel->writefd) { + channel->writefd = FD_CLOSED; } if (closein && (channel->extrabuf != NULL) && (fd == channel->errfd)) { channel->errfd = FD_CLOSED; @@ -1060,8 +1060,8 @@ static void closechanfd(struct Channel *channel, int fd, int how) { /* if we called shutdown on it and all references are gone, then we * need to close() it to stop it lingering */ - if (channel->type->sepfds && channel->outfd == FD_CLOSED - && channel->infd == FD_CLOSED && channel->errfd == FD_CLOSED) { + if (channel->type->sepfds && channel->readfd == FD_CLOSED + && channel->writefd == FD_CLOSED && channel->errfd == FD_CLOSED) { close(fd); } } diff --git a/svr-chansession.c b/svr-chansession.c index 6828537..5ddb8d4 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -673,15 +673,15 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { close(infds[FDIN]); close(outfds[FDOUT]); close(errfds[FDOUT]); - channel->infd = infds[FDOUT]; - channel->outfd = outfds[FDIN]; + channel->writefd = infds[FDOUT]; + channel->readfd = outfds[FDIN]; channel->errfd = errfds[FDIN]; - ses.maxfd = MAX(ses.maxfd, channel->infd); - ses.maxfd = MAX(ses.maxfd, channel->outfd); + ses.maxfd = MAX(ses.maxfd, channel->writefd); + ses.maxfd = MAX(ses.maxfd, channel->readfd); ses.maxfd = MAX(ses.maxfd, channel->errfd); - setnonblocking(channel->outfd); - setnonblocking(channel->infd); + setnonblocking(channel->readfd); + setnonblocking(channel->writefd); setnonblocking(channel->errfd); } @@ -784,8 +784,8 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { addchildpid(chansess, pid); close(chansess->slave); - channel->infd = chansess->master; - channel->outfd = chansess->master; + channel->writefd = chansess->master; + channel->readfd = chansess->master; /* don't need to set stderr here */ ses.maxfd = MAX(ses.maxfd, chansess->master); diff --git a/svr-tcpfwd.c b/svr-tcpfwd.c index 3acc4ff..ecfaf2b 100644 --- a/svr-tcpfwd.c +++ b/svr-tcpfwd.c @@ -272,11 +272,9 @@ static int newtcpdirect(struct Channel * channel) { ses.maxfd = MAX(ses.maxfd, sock); - /* Note that infd is actually the "outgoing" direction on the - * tcp connection, vice versa for outfd. - * We don't set outfd, that will get set after the connection's + /* We don't set readfd, that will get set after the connection's * progress succeeds */ - channel->infd = sock; + channel->writefd = sock; channel->initconn = 1; err = SSH_OPEN_IN_PROGRESS; -- cgit v1.2.3 From a75c71299b137ce09567c634035d352259b35c1e Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Thu, 8 Dec 2005 11:45:33 +0000 Subject: * fix incorrect array sizing --HG-- extra : convert_revision : e1322d52370db709625e024975f7c97fb8ed58c7 --- svr-chansession.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'svr-chansession.c') diff --git a/svr-chansession.c b/svr-chansession.c index 5ddb8d4..03ac40a 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -810,7 +810,7 @@ static void addchildpid(struct ChanSess *chansess, pid_t pid) { /* need to increase size */ if (i == svr_ses.childpidsize) { svr_ses.childpids = (struct ChildPid*)m_realloc(svr_ses.childpids, - sizeof(struct ChildPid) * svr_ses.childpidsize+1); + sizeof(struct ChildPid) * (svr_ses.childpidsize+1)); svr_ses.childpidsize++; } -- cgit v1.2.3 From 1eb9209afef2046bbf74f8d2ffb0f357bdb992bb Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Sat, 11 Feb 2006 15:15:37 +0000 Subject: Only read /dev/random once when the program starts rather than for every connection, to "conserve entropy". --HG-- extra : convert_revision : 21df240b71c0af8454725dec9abb428dd4bb97a2 --- cli-session.c | 8 +++----- random.c | 31 ++++++++++++++++++++++++++++--- random.h | 1 + svr-chansession.c | 2 +- svr-main.c | 4 +++- svr-session.c | 6 +++--- 6 files changed, 39 insertions(+), 13 deletions(-) (limited to 'svr-chansession.c') diff --git a/cli-session.c b/cli-session.c index 0e906e6..35510fa 100644 --- a/cli-session.c +++ b/cli-session.c @@ -76,12 +76,14 @@ static const struct ChanType *cli_chantypes[] = { void cli_session(int sock, char* remotehost) { + seedrandom(); + crypto_init(); + common_session_init(sock, remotehost); chaninitialise(cli_chantypes); - /* Set up cli_ses vars */ cli_session_init(); @@ -91,12 +93,8 @@ void cli_session(int sock, char* remotehost) { /* Exchange identification */ session_identification(); - seedrandom(); - send_msg_kexinit(); - /* XXX here we do stuff differently */ - session_loop(cli_sessionloop); /* Not reached */ diff --git a/random.c b/random.c index d58c8a8..cbbe016 100644 --- a/random.c +++ b/random.c @@ -30,8 +30,8 @@ static int donerandinit = 0; /* this is used to generate unique output from the same hashpool */ -static unsigned int counter = 0; -#define MAX_COUNTER 1000000/* the max value for the counter, so it won't loop */ +static uint32_t counter = 0; +#define MAX_COUNTER 1<<31 /* the max value for the counter, so it won't loop */ static unsigned char hashpool[SHA1_HASH_SIZE]; @@ -132,7 +132,8 @@ void seedrandom() { hash_state hs; - /* initialise so compilers will be happy about hashing it */ + /* initialise so that things won't warn about + * hashing an undefined buffer */ if (!donerandinit) { m_burn(hashpool, sizeof(hashpool)); } @@ -150,6 +151,30 @@ void seedrandom() { donerandinit = 1; } +/* hash the current random pool with some unique identifiers + * for this process and point-in-time. this is used to separate + * the random pools for fork()ed processes. */ +void reseedrandom() { + + pid_t pid; + struct timeval tv; + + if (!donerandinit) { + dropbear_exit("seedrandom not done"); + } + + pid = getpid(); + gettimeofday(&tv, NULL); + + hash_state hs; + unsigned char hash[SHA1_HASH_SIZE]; + sha1_init(&hs); + sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); + sha1_process(&hs, (void*)&pid, sizeof(pid)); + sha1_process(&hs, (void*)&tv, sizeof(tv)); + sha1_done(&hs, hashpool); +} + /* return len bytes of pseudo-random data */ void genrandom(unsigned char* buf, unsigned int len) { diff --git a/random.h b/random.h index 5ec1f24..84a0a39 100644 --- a/random.h +++ b/random.h @@ -28,6 +28,7 @@ struct mp_int; void seedrandom(); +void reseedrandom(); void genrandom(unsigned char* buf, int len); void addrandom(unsigned char* buf, int len); void gen_random_mpint(mp_int *max, mp_int *rand); diff --git a/svr-chansession.c b/svr-chansession.c index 03ac40a..a645f69 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -833,7 +833,7 @@ static void execchild(struct ChanSess *chansess) { svr_opts.hostkey = NULL; /* overwrite the prng state */ - seedrandom(); + reseedrandom(); /* close file descriptors except stdin/stdout/stderr * Need to be sure FDs are closed here to avoid reading files as root */ diff --git a/svr-main.c b/svr-main.c index 4641e24..aef00f6 100644 --- a/svr-main.c +++ b/svr-main.c @@ -83,7 +83,7 @@ static void main_inetd() { int remoteaddrlen; char * addrstring = NULL; - /* Set up handlers, syslog */ + /* Set up handlers, syslog, seed random */ commonsetup(); remoteaddrlen = sizeof(remoteaddr); @@ -359,6 +359,8 @@ static void commonsetup() { /* Now we can setup the hostkeys - needs to be after logging is on, * otherwise we might end up blatting error messages to the socket */ loadhostkeys(); + + seedrandom(); } /* Set up listening sockets for all the requested ports */ diff --git a/svr-session.c b/svr-session.c index 408209d..70029f8 100644 --- a/svr-session.c +++ b/svr-session.c @@ -78,7 +78,9 @@ void svr_session(int sock, int childpipe, char* remotehost, char *addrstring) { struct timeval timeout; - + + reseedrandom(); + crypto_init(); common_session_init(sock, remotehost); @@ -110,8 +112,6 @@ void svr_session(int sock, int childpipe, /* exchange identification, version etc */ session_identification(); - seedrandom(); - /* start off with key exchange */ send_msg_kexinit(); -- cgit v1.2.3 From af485f89cd5240df2f4fa72bd46465b0e38247d1 Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Tue, 28 Feb 2006 04:03:49 +0000 Subject: Merge of the uClinux-dist patch, should work in inetd mode --HG-- extra : convert_revision : 976bfa569f133be97d6f57c89bffa0311a142be3 --- scp.c | 32 ++++++++++++++++++++++++++++++++ svr-chansession.c | 13 +++++++++++++ 2 files changed, 45 insertions(+) (limited to 'svr-chansession.c') diff --git a/scp.c b/scp.c index ccb6c2a..ffc4deb 100644 --- a/scp.c +++ b/scp.c @@ -166,8 +166,22 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) close(reserved[0]); close(reserved[1]); + // uClinux needs to build the args here before vforking, + // otherwise we do it later on. +#ifdef __uClinux__ + args.list[0] = ssh_program; + if (remuser != NULL) + addargs(&args, "-l%s", remuser); + addargs(&args, "%s", host); + addargs(&args, "%s", cmd); +#endif /* __uClinux__ */ + /* Fork a child to execute the command on the remote host using ssh. */ +#ifdef __uClinux__ + do_cmd_pid = vfork(); +#else do_cmd_pid = fork(); +#endif /* __uClinux__ */ if (do_cmd_pid == 0) { /* Child. */ close(pin[1]); @@ -177,6 +191,7 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) close(pin[0]); close(pout[1]); +#ifndef __uClinux__ args.list[0] = ssh_program; if (remuser != NULL) { addargs(&args, "-l"); @@ -184,6 +199,7 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) } addargs(&args, "%s", host); addargs(&args, "%s", cmd); +#endif execvp(ssh_program, args.list); perror(ssh_program); @@ -192,6 +208,22 @@ do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout, int argc) fprintf(stderr, "Fatal error: fork: %s\n", strerror(errno)); exit(1); } + +#ifdef __uClinux__ + /* clean up command */ + /* pop cmd */ + free(args->list[--args->num]); + args->list[args->num]=NULL; + /* pop host */ + free(args->list[--args->num-1]); + args->list[args->num]=NULL; + /* pop user */ + if (remuser != NULL) { + free(args->list[--args->num-1]); + args->list[args->num]=NULL; + } +#endif /* __uClinux__ + /* Parent. Close the other side, and return the local side. */ close(pin[0]); *fdout = pin[1]; diff --git a/svr-chansession.c b/svr-chansession.c index 03ac40a..9ea6ac5 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -623,7 +623,12 @@ static int noptycommand(struct Channel *channel, struct ChanSess *chansess) { if (pipe(errfds) != 0) return DROPBEAR_FAILURE; +#ifdef __uClinux__ + pid = vfork(); +#else pid = fork(); +#endif + if (pid < 0) return DROPBEAR_FAILURE; @@ -714,7 +719,11 @@ static int ptycommand(struct Channel *channel, struct ChanSess *chansess) { return DROPBEAR_FAILURE; } +#ifdef __uClinux__ + pid = vfork(); +#else pid = fork(); +#endif if (pid < 0) return DROPBEAR_FAILURE; @@ -828,12 +837,16 @@ static void execchild(struct ChanSess *chansess) { char * baseshell = NULL; unsigned int i; + /* with uClinux we'll have vfork()ed, so don't want to overwrite the + * hostkey. can't think of a workaround to clear it */ +#ifndef __uClinux__ /* wipe the hostkey */ sign_key_free(svr_opts.hostkey); svr_opts.hostkey = NULL; /* overwrite the prng state */ seedrandom(); +#endif /* close file descriptors except stdin/stdout/stderr * Need to be sure FDs are closed here to avoid reading files as root */ -- cgit v1.2.3 From 422f4f2b41e9623af73f2ab69e4ab2ed8098551c Mon Sep 17 00:00:00 2001 From: Matt Johnston Date: Wed, 8 Mar 2006 12:41:27 +0000 Subject: * Per-IP connection unauthed connection limits * m_close() exits fatally on failure * other cleanups --HG-- extra : convert_revision : bed6155e95a293c9fce7e889d283b5958f3035dc --- dbutil.c | 13 +++---- dbutil.h | 2 +- options.h | 7 ++++ svr-chansession.c | 4 +-- svr-main.c | 106 +++++++++++++++++++++++++++++++++--------------------- 5 files changed, 80 insertions(+), 52 deletions(-) (limited to 'svr-chansession.c') diff --git a/dbutil.c b/dbutil.c index 696bac3..15f51ba 100644 --- a/dbutil.c +++ b/dbutil.c @@ -588,20 +588,17 @@ out: } #endif -/* loop until the socket is closed (in case of EINTR) or - * we get and error. - * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ -int m_close(int fd) { +/* make sure that the socket closes */ +void m_close(int fd) { int val; do { val = close(fd); } while (val < 0 && errno == EINTR); - if (val == 0 || errno == EBADF) { - return DROPBEAR_SUCCESS; - } else { - return DROPBEAR_FAILURE; + if (val < 0 && errno != EBADF) { + /* Linux says EIO can happen */ + dropbear_exit("Error closing fd %d, %s", fd, strerror(errno)); } } diff --git a/dbutil.h b/dbutil.h index eef75e3..d74e17e 100644 --- a/dbutil.h +++ b/dbutil.h @@ -55,7 +55,7 @@ char* getaddrhostname(struct sockaddr_storage * addr); int buf_readfile(buffer* buf, const char* filename); int buf_getline(buffer * line, FILE * authfile); -int m_close(int fd); +void m_close(int fd); void * m_malloc(size_t size); void * m_strdup(const char * str); void * m_realloc(void* ptr, size_t size); diff --git a/options.h b/options.h index 1fa7d3f..1feae40 100644 --- a/options.h +++ b/options.h @@ -161,6 +161,13 @@ etc) slower (perhaps by 50%). Recommended for most small systems. */ /* Specify the number of clients we will allow to be connected but * not yet authenticated. After this limit, connections are rejected */ +/* The first setting is per-IP, to avoid denial of service */ +#ifndef MAX_UNAUTH_PER_IP +#define MAX_UNAUTH_PER_IP 5 +#endif + +/* And then a global limit to avoid chewing memory if connections + * come from many IPs */ #ifndef MAX_UNAUTH_CLIENTS #define MAX_UNAUTH_CLIENTS 30 #endif diff --git a/svr-chansession.c b/svr-chansession.c index 03ac40a..9a0932e 100644 --- a/svr-chansession.c +++ b/svr-chansession.c @@ -838,9 +838,7 @@ static void execchild(struct ChanSess *chansess) { /* close file descriptors except stdin/stdout/stderr * Need to be sure FDs are closed here to avoid reading files as root */ for (i = 3; i <= (unsigned int)ses.maxfd; i++) { - if (m_close(i) == DROPBEAR_FAILURE) { - dropbear_exit("Error closing file desc"); - } + m_close(i); } /* clear environment */ diff --git a/svr-main.c b/svr-main.c index 4641e24..5b23340 100644 --- a/svr-main.c +++ b/svr-main.c @@ -29,7 +29,7 @@ #include "signkey.h" #include "runopts.h" -static int listensockets(int *sock, int sockcount, int *maxfd); +static size_t listensockets(int *sock, size_t sockcount, int *maxfd); static void sigchld_handler(int dummy); static void sigsegv_handler(int); static void sigintterm_handler(int fish); @@ -41,8 +41,6 @@ static void main_noinetd(); #endif static void commonsetup(); -static int childpipes[MAX_UNAUTH_CLIENTS]; - #if defined(DBMULTI_dropbear) || !defined(DROPBEAR_MULTI) #if defined(DBMULTI_dropbear) && defined(DROPBEAR_MULTI) int dropbear_main(int argc, char ** argv) @@ -80,7 +78,7 @@ int main(int argc, char ** argv) static void main_inetd() { struct sockaddr_storage remoteaddr; - int remoteaddrlen; + socklen_t remoteaddrlen; char * addrstring = NULL; /* Set up handlers, syslog */ @@ -116,14 +114,14 @@ void main_noinetd() { unsigned int i, j; int val; int maxsock = -1; - struct sockaddr_storage remoteaddr; - int remoteaddrlen; int listensocks[MAX_LISTEN_ADDR]; - int listensockcount = 0; + size_t listensockcount = 0; FILE *pidfile = NULL; + int childpipes[MAX_UNAUTH_CLIENTS]; + char * preauth_addrs[MAX_UNAUTH_CLIENTS]; + int childsock; - pid_t childpid; int childpipe[2]; /* fork */ @@ -160,11 +158,13 @@ void main_noinetd() { for (i = 0; i < MAX_UNAUTH_CLIENTS; i++) { childpipes[i] = -1; } + bzero(preauth_addrs, sizeof(preauth_addrs)); /* Set up the listening sockets */ /* XXX XXX ports */ listensockcount = listensockets(listensocks, MAX_LISTEN_ADDR, &maxsock); - if (listensockcount < 0) { + if (listensockcount == 0) + { dropbear_exit("No listening ports available."); } @@ -177,7 +177,7 @@ void main_noinetd() { seltimeout.tv_usec = 0; /* listening sockets */ - for (i = 0; i < (unsigned int)listensockcount; i++) { + for (i = 0; i < listensockcount; i++) { FD_SET(listensocks[i], &fds); } @@ -208,17 +208,27 @@ void main_noinetd() { dropbear_exit("Listening socket error"); } - /* close fds which have been authed or closed - auth.c handles + /* close fds which have been authed or closed - svr-auth.c handles * closing the auth sockets on success */ for (i = 0; i < MAX_UNAUTH_CLIENTS; i++) { if (childpipes[i] >= 0 && FD_ISSET(childpipes[i], &fds)) { - close(childpipes[i]); + m_close(childpipes[i]); childpipes[i] = -1; + m_free(preauth_addrs[i]); } } /* handle each socket which has something to say */ - for (i = 0; i < (unsigned int)listensockcount; i++) { + for (i = 0; i < listensockcount; i++) { + + struct sockaddr_storage remoteaddr; + socklen_t remoteaddrlen = 0; + size_t num_unauthed_for_addr = 0; + size_t num_unauthed_total = 0; + char * remote_addr_str = NULL; + pid_t fork_ret = 0; + size_t conn_idx = 0; + if (!FD_ISSET(listensocks[i], &fds)) continue; @@ -231,28 +241,47 @@ void main_noinetd() { continue; } - /* check for max number of connections not authorised */ + /* Limit the number of unauthenticated connections per IP */ + remote_addr_str = getaddrstring(&remoteaddr, 0); + + num_unauthed_for_addr = 0; + num_unauthed_total = 0; for (j = 0; j < MAX_UNAUTH_CLIENTS; j++) { - if (childpipes[j] < 0) { - break; + if (childpipes[j] >= 0) { + num_unauthed_total++; + if (strcmp(remote_addr_str, preauth_addrs[j]) == 0) { + num_unauthed_for_addr++; + } + } else { + /* a free slot */ + conn_idx = j; } } - if (j == MAX_UNAUTH_CLIENTS) { - /* no free connections */ - /* TODO - possibly log, though this would be an easy way - * to fill logs/disk */ - close(childsock); - continue; + if (num_unauthed_total >= MAX_UNAUTH_CLIENTS + || num_unauthed_for_addr >= MAX_UNAUTH_PER_IP) { + goto out; } if (pipe(childpipe) < 0) { TRACE(("error creating child pipe")) - close(childsock); - continue; + goto out; } - if ((childpid = fork()) == 0) { + fork_ret = fork(); + if (fork_ret < 0) { + dropbear_log(LOG_WARNING, "error forking: %s", strerror(errno)); + goto out; + + } else if (fork_ret > 0) { + + /* parent */ + childpipes[conn_idx] = childpipe[0]; + m_close(childpipe[1]); + preauth_addrs[conn_idx] = remote_addr_str; + remote_addr_str = NULL; + + } else { /* child */ char * addrstring = NULL; @@ -261,6 +290,7 @@ void main_noinetd() { monstartup((u_long)&_start, (u_long)&etext); #endif /* DEBUG_FORKGPROF */ + m_free(remote_addr_str); addrstring = getaddrstring(&remoteaddr, 1); dropbear_log(LOG_INFO, "Child connection from %s", addrstring); @@ -269,15 +299,11 @@ void main_noinetd() { } /* make sure we close sockets */ - for (i = 0; i < (unsigned int)listensockcount; i++) { - if (m_close(listensocks[i]) == DROPBEAR_FAILURE) { - dropbear_exit("Couldn't close socket"); - } + for (i = 0; i < listensockcount; i++) { + m_close(listensocks[i]); } - if (m_close(childpipe[0]) == DROPBEAR_FAILURE) { - dropbear_exit("Couldn't close socket"); - } + m_close(childpipe[0]); /* start the session */ svr_session(childsock, childpipe[1], @@ -286,12 +312,12 @@ void main_noinetd() { /* don't return */ dropbear_assert(0); } - - /* parent */ - childpipes[j] = childpipe[0]; - if (m_close(childpipe[1]) == DROPBEAR_FAILURE - || m_close(childsock) == DROPBEAR_FAILURE) { - dropbear_exit("Couldn't close socket"); + +out: + /* This section is important for the parent too */ + m_close(childsock); + if (remote_addr_str) { + m_free(remote_addr_str); } } } /* for(;;) loop */ @@ -362,11 +388,11 @@ static void commonsetup() { } /* Set up listening sockets for all the requested ports */ -static int listensockets(int *sock, int sockcount, int *maxfd) { +static size_t listensockets(int *sock, size_t sockcount, int *maxfd) { unsigned int i; char* errstring = NULL; - unsigned int sockpos = 0; + size_t sockpos = 0; int nsock; TRACE(("listensockets: %d to try\n", svr_opts.portcount)) -- cgit v1.2.3