]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: Add channel_set_xtype()
authordjm@openbsd.org <djm@openbsd.org>
Fri, 6 Jan 2023 02:42:34 +0000 (02:42 +0000)
committerDamien Miller <djm@mindrot.org>
Fri, 6 Jan 2023 05:21:40 +0000 (16:21 +1100)
This sets an "extended" channel type after channel creation (e.g.
"session:subsystem:sftp") that will be used for setting channel inactivity
timeouts.

ok markus dtucker

OpenBSD-Commit-ID: 42564aa92345045b4a74300528f960416a15d4ca

channels.c
channels.h
session.c

index 9332d603a0f6ec4bf375d6d657eaf28233796e00..7bc6c7e41b8a7b8df03214956aa39ec51148e600 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: channels.c,v 1.424 2023/01/06 02:41:49 djm Exp $ */
+/* $OpenBSD: channels.c,v 1.425 2023/01/06 02:42:34 djm Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -296,6 +296,24 @@ channel_lookup(struct ssh *ssh, int id)
        return NULL;
 }
 
+/*
+ * Sets "extended type" of a channel; used by session layer to add additional
+ * information about channel types (e.g. shell, login, subsystem) that can then
+ * be used to select timeouts.
+ */
+void
+channel_set_xtype(struct ssh *ssh, int id, const char *xctype)
+{
+       Channel *c;
+
+       if ((c = channel_by_id(ssh, id)) == NULL)
+               fatal_f("missing channel %d", id);
+       if (c->xctype != NULL)
+               free(c->xctype);
+       c->xctype = xstrdup(xctype);
+       debug2_f("labeled channel %d as %s", id, xctype);
+}
+
 /*
  * Register filedescriptors for a channel, used when allocating a channel or
  * when the channel consumer/producer is ready, e.g. shell exec'd
@@ -656,6 +674,8 @@ channel_free(struct ssh *ssh, Channel *c)
        c->path = NULL;
        free(c->listening_addr);
        c->listening_addr = NULL;
+       free(c->xctype);
+       c->xctype = NULL;
        while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
                if (cc->abandon_cb != NULL)
                        cc->abandon_cb(ssh, c, cc->ctx);
@@ -871,9 +891,9 @@ channel_format_status(const Channel *c)
 {
        char *ret = NULL;
 
-       xasprintf(&ret, "t%d %s%u i%u/%zu o%u/%zu e[%s]/%zu "
+       xasprintf(&ret, "t%d [%s] %s%u i%u/%zu o%u/%zu e[%s]/%zu "
            "fd %d/%d/%d sock %d cc %d io 0x%02x/0x%02x",
-           c->type,
+           c->type, c->xctype != NULL ? c->xctype : c->ctype,
            c->have_remote_id ? "r" : "nr", c->remote_id,
            c->istate, sshbuf_len(c->input),
            c->ostate, sshbuf_len(c->output),
index bcffa6cf62974e7c3430d0f18e597e7edc6ad821..b8daaea3c9de013b2540ef7887757bcbc47b5a64 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: channels.h,v 1.145 2023/01/06 02:39:59 djm Exp $ */
+/* $OpenBSD: channels.h,v 1.146 2023/01/06 02:42:34 djm Exp $ */
 
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -175,7 +175,8 @@ struct Channel {
        int     extended_usage;
        int     single_connection;
 
-       char   *ctype;          /* type */
+       char   *ctype;          /* const type - NB. not freed on channel_free */
+       char   *xctype;         /* extended type */
 
        /* callback */
        channel_open_fn         *open_confirm;
@@ -282,6 +283,7 @@ void         channel_free(struct ssh *, Channel *);
 void    channel_free_all(struct ssh *);
 void    channel_stop_listening(struct ssh *);
 void    channel_force_close(struct ssh *, Channel *, int);
+void    channel_set_xtype(struct ssh *, int, const char *);
 
 void    channel_send_open(struct ssh *, int);
 void    channel_request_start(struct ssh *, int, char *, int);
index 5b5711cf8a1c242d0aaf0bcb77aaeefeb9b15307..f30d7ac519ace290332333731233a2f51e9e35b0 100644 (file)
--- a/session.c
+++ b/session.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: session.c,v 1.332 2023/01/06 02:41:49 djm Exp $ */
+/* $OpenBSD: session.c,v 1.333 2023/01/06 02:42:34 djm Exp $ */
 /*
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  *                    All rights reserved
@@ -1955,7 +1955,7 @@ session_subsystem_req(struct ssh *ssh, Session *s)
 {
        struct stat st;
        int r, success = 0;
-       char *prog, *cmd;
+       char *prog, *cmd, *type;
        u_int i;
 
        if ((r = sshpkt_get_cstring(ssh, &s->subsys, NULL)) != 0 ||
@@ -1978,6 +1978,10 @@ session_subsystem_req(struct ssh *ssh, Session *s)
                                s->is_subsystem = SUBSYSTEM_EXT;
                                debug("subsystem: exec() %s", cmd);
                        }
+                       xasprintf(&type, "session:subsystem:%s",
+                           options.subsystem_name[i]);
+                       channel_set_xtype(ssh, s->chanid, type);
+                       free(type);
                        success = do_exec(ssh, s, cmd) == 0;
                        break;
                }
@@ -2033,6 +2037,9 @@ session_shell_req(struct ssh *ssh, Session *s)
 
        if ((r = sshpkt_get_end(ssh)) != 0)
                sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
+
+       channel_set_xtype(ssh, s->chanid, "session:shell");
+
        return do_exec(ssh, s, NULL) == 0;
 }
 
@@ -2047,6 +2054,8 @@ session_exec_req(struct ssh *ssh, Session *s)
            (r = sshpkt_get_end(ssh)) != 0)
                sshpkt_fatal(ssh, r, "%s: parse packet", __func__);
 
+       channel_set_xtype(ssh, s->chanid, "session:command");
+
        success = do_exec(ssh, s, command) == 0;
        free(command);
        return success;