Print this page
OS-2204 SunSSH has a maximum of 10 multiplexed sessions

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/ssh/sshd/session.c
          +++ new/usr/src/cmd/ssh/sshd/session.c
↓ open down ↓ 27 lines elided ↑ open up ↑
  28   28   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29   29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30   30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31   31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32   32   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33   33   */
  34   34  /*
  35   35   * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  36   36   * Use is subject to license terms.
  37   37   */
       38 +/*
       39 + * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
       40 + */
  38   41  
  39   42  #include "includes.h"
  40   43  RCSID("$OpenBSD: session.c,v 1.150 2002/09/16 19:55:33 stevesk Exp $");
  41   44  
  42   45  #ifdef HAVE_DEFOPEN
  43   46  #include <deflt.h>
  44   47  #include <ulimit.h>
  45   48  #endif /* HAVE_DEFOPEN */
  46   49  
  47   50  #ifdef HAVE_LIBGEN_H
  48   51  #include <libgen.h>
  49   52  #endif
  50   53  
  51   54  #include <priv.h>
  52   55  
  53   56  #include "ssh.h"
  54   57  #include "ssh1.h"
  55   58  #include "ssh2.h"
  56   59  #include "xmalloc.h"
       60 +#include "sys-queue.h"
  57   61  #include "sshpty.h"
  58   62  #include "packet.h"
  59   63  #include "buffer.h"
  60   64  #include "mpaux.h"
  61   65  #include "uidswap.h"
  62   66  #include "compat.h"
  63   67  #include "channels.h"
  64   68  #include "bufaux.h"
  65   69  #include "auth.h"
  66   70  #include "auth-options.h"
↓ open down ↓ 65 lines elided ↑ open up ↑
 132  136  
 133  137  #ifdef GSSAPI
 134  138  extern Gssctxt *xxx_gssctxt;
 135  139  #endif /* GSSAPI */
 136  140  
 137  141  /* original command from peer. */
 138  142  const char *original_command = NULL;
 139  143  
 140  144  /* data */
 141  145  #define MAX_SESSIONS 10
 142      -Session sessions[MAX_SESSIONS];
      146 +static int sessions_next_id = 0;
      147 +static LIST_HEAD(sessions_head, Session) sessions =
      148 +    LIST_HEAD_INITIALIZER(sessions);
 143  149  
 144  150  #define SUBSYSTEM_NONE          0
 145  151  #define SUBSYSTEM_EXT           1
 146  152  #define SUBSYSTEM_INT_SFTP      2
 147  153  
 148  154  #ifdef HAVE_LOGIN_CAP
 149  155  login_cap_t *lc;
 150  156  #endif
 151  157  
 152  158  /* Name and directory of socket for authentication agent forwarding. */
↓ open down ↓ 1367 lines elided ↑ open up ↑
1520 1526          argv[2] = (char *) command;
1521 1527          argv[3] = NULL;
1522 1528          execve(shell, argv, env);
1523 1529          perror(shell);
1524 1530          exit(1);
1525 1531  }
1526 1532  
1527 1533  Session *
1528 1534  session_new(void)
1529 1535  {
1530      -        int i;
1531      -        static int did_init = 0;
1532      -        if (!did_init) {
1533      -                debug("session_new: init");
1534      -                for (i = 0; i < MAX_SESSIONS; i++) {
1535      -                        sessions[i].used = 0;
1536      -                }
1537      -                did_init = 1;
1538      -        }
1539      -        for (i = 0; i < MAX_SESSIONS; i++) {
1540      -                Session *s = &sessions[i];
1541      -                if (! s->used) {
1542      -                        memset(s, 0, sizeof(*s));
1543      -                        s->chanid = -1;
1544      -                        s->ptyfd = -1;
1545      -                        s->ttyfd = -1;
1546      -                        s->used = 1;
1547      -                        s->self = i;
1548      -                        s->env = NULL;
1549      -                        debug("session_new: session %d", i);
1550      -                        return s;
1551      -                }
1552      -        }
1553      -        return NULL;
     1536 +        Session *s;
     1537 +
     1538 +        debug("session_new: init");
     1539 +
     1540 +        s = xmalloc(sizeof (*s));
     1541 +        memset(s, 0, sizeof(*s));
     1542 +        s->chanid = -1;
     1543 +        s->ptyfd = -1;
     1544 +        s->ttyfd = -1;
     1545 +        s->self = sessions_next_id++;
     1546 +        s->env = NULL;
     1547 +        LIST_INSERT_HEAD(&sessions, s, list_entry);
     1548 +
     1549 +        debug("session_new: session %d", s->self);
     1550 +
     1551 +        return (s);
1554 1552  }
1555 1553  
1556 1554  static void
1557 1555  session_dump(void)
1558 1556  {
1559      -        int i;
1560      -        for (i = 0; i < MAX_SESSIONS; i++) {
1561      -                Session *s = &sessions[i];
1562      -                debug("dump: used %d session %d %p channel %d pid %ld",
1563      -                    s->used,
     1557 +        Session *s;
     1558 +        LIST_FOREACH(s, &sessions, list_entry) {
     1559 +                debug("dump: session %d %p channel %d pid %ld",
1564 1560                      s->self,
1565 1561                      s,
1566 1562                      s->chanid,
1567 1563                      (long)s->pid);
1568 1564          }
1569 1565  }
1570 1566  
1571 1567  int
1572 1568  session_open(Authctxt *authctxt, int chanid)
1573 1569  {
↓ open down ↓ 9 lines elided ↑ open up ↑
1583 1579                  fatal("no user for session %d", s->self);
1584 1580          debug("session_open: session %d: link with channel %d", s->self, chanid);
1585 1581          s->chanid = chanid;
1586 1582          return 1;
1587 1583  }
1588 1584  
1589 1585  #ifndef lint
1590 1586  Session *
1591 1587  session_by_tty(char *tty)
1592 1588  {
1593      -        int i;
1594      -        for (i = 0; i < MAX_SESSIONS; i++) {
1595      -                Session *s = &sessions[i];
1596      -                if (s->used && s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
1597      -                        debug("session_by_tty: session %d tty %s", i, tty);
     1589 +        Session *s;
     1590 +        LIST_FOREACH(s, &sessions, list_entry) {
     1591 +                if (s->ttyfd != -1 && strcmp(s->tty, tty) == 0) {
     1592 +                        debug("session_by_tty: session %d tty %s", s->self, tty);
1598 1593                          return s;
1599 1594                  }
1600 1595          }
1601 1596          debug("session_by_tty: unknown tty %.100s", tty);
1602 1597          session_dump();
1603 1598          return NULL;
1604 1599  }
1605 1600  #endif /* lint */
1606 1601  
1607 1602  static Session *
1608 1603  session_by_channel(int id)
1609 1604  {
1610      -        int i;
1611      -        for (i = 0; i < MAX_SESSIONS; i++) {
1612      -                Session *s = &sessions[i];
1613      -                if (s->used && s->chanid == id) {
1614      -                        debug("session_by_channel: session %d channel %d", i, id);
     1605 +        Session *s;
     1606 +        LIST_FOREACH(s, &sessions, list_entry) {
     1607 +                if (s->chanid == id) {
     1608 +                        debug("session_by_channel: session %d channel %d", s->self, id);
1615 1609                          return s;
1616 1610                  }
1617 1611          }
1618 1612          debug("session_by_channel: unknown channel %d", id);
1619 1613          session_dump();
1620 1614          return NULL;
1621 1615  }
1622 1616  
1623 1617  static Session *
1624 1618  session_by_pid(pid_t pid)
1625 1619  {
1626      -        int i;
     1620 +        Session *s;
1627 1621          debug("session_by_pid: pid %ld", (long)pid);
1628      -        for (i = 0; i < MAX_SESSIONS; i++) {
1629      -                Session *s = &sessions[i];
1630      -                if (s->used && s->pid == pid)
     1622 +        LIST_FOREACH(s, &sessions, list_entry) {
     1623 +                if (s->pid == pid)
1631 1624                          return s;
1632 1625          }
1633 1626          error("session_by_pid: unknown pid %ld", (long)pid);
1634 1627          session_dump();
1635 1628          return NULL;
1636 1629  }
1637 1630  
1638 1631  static int
1639 1632  session_window_change_req(Session *s)
1640 1633  {
↓ open down ↓ 639 lines elided ↑ open up ↑
2280 2273                  xfree(s->display);
2281 2274          if (s->auth_display)
2282 2275                  xfree(s->auth_display);
2283 2276          if (s->auth_data)
2284 2277                  xfree(s->auth_data);
2285 2278          if (s->auth_proto)
2286 2279                  xfree(s->auth_proto);
2287 2280          if (s->command)
2288 2281                  xfree(s->command);
2289 2282          session_free_env(&s->env);
2290      -        s->used = 0;
     2283 +
     2284 +        LIST_REMOVE(s, list_entry);
2291 2285          session_proctitle(s);
     2286 +        xfree(s);
2292 2287  }
2293 2288  
2294 2289  void
2295 2290  session_close_by_pid(pid_t pid, int status)
2296 2291  {
2297 2292          Session *s = session_by_pid(pid);
2298 2293          if (s == NULL) {
2299 2294                  debug("session_close_by_pid: no session for pid %ld",
2300 2295                      (long)pid);
2301 2296                  return;
↓ open down ↓ 32 lines elided ↑ open up ↑
2334 2329          }
2335 2330          /* detach by removing callback */
2336 2331          channel_cancel_cleanup(s->chanid);
2337 2332          s->chanid = -1;
2338 2333          session_close(s);
2339 2334  }
2340 2335  
2341 2336  void
2342 2337  session_destroy_all(void (*closefunc)(Session *))
2343 2338  {
2344      -        int i;
2345      -        for (i = 0; i < MAX_SESSIONS; i++) {
2346      -                Session *s = &sessions[i];
2347      -                if (s->used) {
2348      -                        if (closefunc != NULL)
2349      -                                closefunc(s);
2350      -                        else
2351      -                                session_close(s);
2352      -                }
     2339 +        Session *s;
     2340 +        LIST_FOREACH(s, &sessions, list_entry) {
     2341 +                if (closefunc != NULL)
     2342 +                        closefunc(s);
     2343 +                else
     2344 +                        session_close(s);
2353 2345          }
2354 2346  }
2355 2347  
2356 2348  static char *
2357 2349  session_tty_list(void)
2358 2350  {
     2351 +        Session *s;
2359 2352          static char buf[1024];
2360      -        int i;
2361 2353          buf[0] = '\0';
2362      -        for (i = 0; i < MAX_SESSIONS; i++) {
2363      -                Session *s = &sessions[i];
2364      -                if (s->used && s->ttyfd != -1) {
     2354 +        LIST_FOREACH(s, &sessions, list_entry) {
     2355 +                if (s->ttyfd != -1) {
2365 2356                          if (buf[0] != '\0')
2366 2357                                  strlcat(buf, ",", sizeof buf);
2367 2358                          strlcat(buf, strrchr(s->tty, '/') + 1, sizeof buf);
2368 2359                  }
2369 2360          }
2370 2361          if (buf[0] == '\0')
2371 2362                  strlcpy(buf, "notty", sizeof buf);
2372 2363          return buf;
2373 2364  }
2374 2365  
↓ open down ↓ 267 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX