channels.h revision 69587
165668Skris/* 265668Skris * Author: Tatu Ylonen <ylo@cs.hut.fi> 365668Skris * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 465668Skris * All rights reserved 565668Skris * 665668Skris * As far as I am concerned, the code I have written for this software 765668Skris * can be used freely for any purpose. Any derived versions of this 865668Skris * software must be clearly marked as such, and if the derived work is 965668Skris * incompatible with the protocol description in the RFC file, it must be 1065668Skris * called by a name other than "ssh" or "Secure Shell". 1165668Skris */ 1265668Skris/* 1365668Skris * Copyright (c) 2000 Markus Friedl. All rights reserved. 1465668Skris * 1565668Skris * Redistribution and use in source and binary forms, with or without 1665668Skris * modification, are permitted provided that the following conditions 1765668Skris * are met: 1865668Skris * 1. Redistributions of source code must retain the above copyright 1965668Skris * notice, this list of conditions and the following disclaimer. 2065668Skris * 2. Redistributions in binary form must reproduce the above copyright 2165668Skris * notice, this list of conditions and the following disclaimer in the 2265668Skris * documentation and/or other materials provided with the distribution. 2365668Skris * 2465668Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2565668Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2665668Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2765668Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2865668Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2965668Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3065668Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3165668Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3265668Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3365668Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3465668Skris */ 3569587Sgreen/* RCSID("$OpenBSD: channels.h,v 1.22 2000/10/27 07:48:22 markus Exp $"); */ 3657429Smarkm 3757429Smarkm#ifndef CHANNELS_H 3857429Smarkm#define CHANNELS_H 3957429Smarkm 4057429Smarkm/* Definitions for channel types. */ 4157429Smarkm#define SSH_CHANNEL_FREE 0 /* This channel is free (unused). */ 4257429Smarkm#define SSH_CHANNEL_X11_LISTENER 1 /* Listening for inet X11 conn. */ 4357429Smarkm#define SSH_CHANNEL_PORT_LISTENER 2 /* Listening on a port. */ 4457429Smarkm#define SSH_CHANNEL_OPENING 3 /* waiting for confirmation */ 4557429Smarkm#define SSH_CHANNEL_OPEN 4 /* normal open two-way channel */ 4657429Smarkm#define SSH_CHANNEL_CLOSED 5 /* waiting for close confirmation */ 4760573Skris#define SSH_CHANNEL_AUTH_SOCKET 6 /* authentication socket */ 4860573Skris#define SSH_CHANNEL_X11_OPEN 7 /* reading first X11 packet */ 4960573Skris#define SSH_CHANNEL_INPUT_DRAINING 8 /* sending remaining data to conn */ 5060573Skris#define SSH_CHANNEL_OUTPUT_DRAINING 9 /* sending remaining data to app */ 5160573Skris#define SSH_CHANNEL_LARVAL 10 /* larval session */ 5260573Skris#define SSH_CHANNEL_MAX_TYPE 11 5357429Smarkm 5457429Smarkm/* 5557429Smarkm * Data structure for channel data. This is iniailized in channel_allocate 5657429Smarkm * and cleared in channel_free. 5757429Smarkm */ 5865668Skrisstruct Channel; 5965668Skristypedef struct Channel Channel; 6065668Skris 6160573Skristypedef void channel_callback_fn(int id, void *arg); 6265668Skristypedef int channel_filter_fn(struct Channel *c, char *buf, int len); 6357429Smarkm 6465668Skrisstruct Channel { 6557429Smarkm int type; /* channel type/state */ 6657429Smarkm int self; /* my own channel identifier */ 6757429Smarkm int remote_id; /* channel identifier for remote peer */ 6857429Smarkm /* peer can be reached over encrypted connection, via packet-sent */ 6957429Smarkm int istate; /* input from channel (state of receive half) */ 7057429Smarkm int ostate; /* output to channel (state of transmit half) */ 7160573Skris int flags; /* close sent/rcvd */ 7260573Skris int rfd; /* read fd */ 7360573Skris int wfd; /* write fd */ 7460573Skris int efd; /* extended fd */ 7560573Skris int sock; /* sock fd */ 7657429Smarkm Buffer input; /* data read from socket, to be sent over 7757429Smarkm * encrypted connection */ 7857429Smarkm Buffer output; /* data received over encrypted connection for 7957429Smarkm * send on socket */ 8060573Skris Buffer extended; 8157429Smarkm char path[200]; /* path for unix domain sockets, or host name 8257429Smarkm * for forwards */ 8357429Smarkm int listening_port; /* port being listened for forwards */ 8457429Smarkm int host_port; /* remote port to connect for forwards */ 8557429Smarkm char *remote_name; /* remote hostname */ 8660573Skris 8760573Skris int remote_window; 8860573Skris int remote_maxpacket; 8960573Skris int local_window; 9060573Skris int local_window_max; 9160573Skris int local_consumed; 9260573Skris int local_maxpacket; 9360573Skris int extended_usage; 9460573Skris 9560573Skris char *ctype; /* type */ 9660573Skris 9760573Skris /* callback */ 9860573Skris channel_callback_fn *cb_fn; 9960573Skris void *cb_arg; 10060573Skris int cb_event; 10160573Skris channel_callback_fn *dettach_user; 10260573Skris 10365668Skris /* filter */ 10465668Skris channel_filter_fn *input_filter; 10565668Skris}; 10665668Skris 10760573Skris#define CHAN_EXTENDED_IGNORE 0 10860573Skris#define CHAN_EXTENDED_READ 1 10960573Skris#define CHAN_EXTENDED_WRITE 2 11060573Skris 11165668Skris/* default window/packet sizes for tcp/x11-fwd-channel */ 11265668Skris#define CHAN_SES_WINDOW_DEFAULT (32*1024) 11365668Skris#define CHAN_SES_PACKET_DEFAULT (CHAN_SES_WINDOW_DEFAULT/2) 11465668Skris#define CHAN_TCP_WINDOW_DEFAULT (32*1024) 11565668Skris#define CHAN_TCP_PACKET_DEFAULT (CHAN_TCP_WINDOW_DEFAULT/2) 11665668Skris#define CHAN_X11_WINDOW_DEFAULT (4*1024) 11765668Skris#define CHAN_X11_PACKET_DEFAULT (CHAN_X11_WINDOW_DEFAULT/2) 11865668Skris 11965668Skris 12060573Skrisvoid channel_open(int id); 12160573Skrisvoid channel_request(int id, char *service, int wantconfirm); 12260573Skrisvoid channel_request_start(int id, char *service, int wantconfirm); 12360573Skrisvoid channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg); 12460573Skrisvoid channel_register_cleanup(int id, channel_callback_fn *fn); 12565668Skrisvoid channel_register_filter(int id, channel_filter_fn *fn); 12660573Skrisvoid channel_cancel_cleanup(int id); 12760573SkrisChannel *channel_lookup(int id); 12860573Skris 12960573Skrisint 13060573Skrischannel_new(char *ctype, int type, int rfd, int wfd, int efd, 13169587Sgreen int window, int maxpack, int extended_usage, char *remote_name, 13269587Sgreen int nonblock); 13369587Sgreenvoid 13469587Sgreenchannel_set_fds(int id, int rfd, int wfd, int efd, 13569587Sgreen int extusage, int nonblock); 13660573Skris 13769587Sgreenvoid deny_input_open(int type, int plen, void *ctxt); 13860573Skris 13969587Sgreenvoid channel_input_channel_request(int type, int plen, void *ctxt); 14069587Sgreenvoid channel_input_close(int type, int plen, void *ctxt); 14169587Sgreenvoid channel_input_close_confirmation(int type, int plen, void *ctxt); 14269587Sgreenvoid channel_input_data(int type, int plen, void *ctxt); 14369587Sgreenvoid channel_input_extended_data(int type, int plen, void *ctxt); 14469587Sgreenvoid channel_input_ieof(int type, int plen, void *ctxt); 14569587Sgreenvoid channel_input_oclose(int type, int plen, void *ctxt); 14669587Sgreenvoid channel_input_open_confirmation(int type, int plen, void *ctxt); 14769587Sgreenvoid channel_input_open_failure(int type, int plen, void *ctxt); 14869587Sgreenvoid channel_input_port_open(int type, int plen, void *ctxt); 14969587Sgreenvoid channel_input_window_adjust(int type, int plen, void *ctxt); 15069587Sgreenvoid channel_input_open(int type, int plen, void *ctxt); 15169587Sgreen 15260573Skris/* Sets specific protocol options. */ 15360573Skrisvoid channel_set_options(int hostname_in_open); 15460573Skris 15560573Skris/* 15660573Skris * Allocate a new channel object and set its type and socket. Remote_name 15760573Skris * must have been allocated with xmalloc; this will free it when the channel 15860573Skris * is freed. 15960573Skris */ 16060573Skrisint channel_allocate(int type, int sock, char *remote_name); 16160573Skris 16260573Skris/* Free the channel and close its socket. */ 16360573Skrisvoid channel_free(int channel); 16460573Skris 16560573Skris/* Add any bits relevant to channels in select bitmasks. */ 16660573Skrisvoid channel_prepare_select(fd_set * readset, fd_set * writeset); 16760573Skris 16860573Skris/* 16960573Skris * After select, perform any appropriate operations for channels which have 17060573Skris * events pending. 17160573Skris */ 17260573Skrisvoid channel_after_select(fd_set * readset, fd_set * writeset); 17360573Skris 17460573Skris/* If there is data to send to the connection, send some of it now. */ 17560573Skrisvoid channel_output_poll(void); 17660573Skris 17760573Skris/* Returns true if no channel has too much buffered data. */ 17860573Skrisint channel_not_very_much_buffered_data(void); 17960573Skris 18060573Skris/* This closes any sockets that are listening for connections; this removes 18160573Skris any unix domain sockets. */ 18260573Skrisvoid channel_stop_listening(void); 18360573Skris 18460573Skris/* 18560573Skris * Closes the sockets of all channels. This is used to close extra file 18660573Skris * descriptors after a fork. 18760573Skris */ 18860573Skrisvoid channel_close_all(void); 18960573Skris 19060573Skris/* Returns the maximum file descriptor number used by the channels. */ 19160573Skrisint channel_max_fd(void); 19260573Skris 19360573Skris/* Returns true if there is still an open channel over the connection. */ 19460573Skrisint channel_still_open(void); 19560573Skris 19660573Skris/* 19760573Skris * Returns a string containing a list of all open channels. The list is 19860573Skris * suitable for displaying to the user. It uses crlf instead of newlines. 19960573Skris * The caller should free the string with xfree. 20060573Skris */ 20160573Skrischar *channel_open_message(void); 20260573Skris 20360573Skris/* 20460573Skris * Initiate forwarding of connections to local port "port" through the secure 20560573Skris * channel to host:port from remote side. This never returns if there was an 20660573Skris * error. 20760573Skris */ 20860573Skrisvoid 20960573Skrischannel_request_local_forwarding(u_short port, const char *host, 21060573Skris u_short remote_port, int gateway_ports); 21160573Skris 21260573Skris/* 21360573Skris * Initiate forwarding of connections to port "port" on remote host through 21460573Skris * the secure channel to host:port from local side. This never returns if 21560573Skris * there was an error. This registers that open requests for that port are 21660573Skris * permitted. 21760573Skris */ 21860573Skrisvoid 21960573Skrischannel_request_remote_forwarding(u_short port, const char *host, 22060573Skris u_short remote_port); 22160573Skris 22260573Skris/* 22360573Skris * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually 22460573Skris * called by the server, because the user could connect to any port anyway, 22560573Skris * and the server has no way to know but to trust the client anyway. 22660573Skris */ 22760573Skrisvoid channel_permit_all_opens(void); 22860573Skris 22960573Skris/* 23060573Skris * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates 23160573Skris * listening for the port, and sends back a success reply (or disconnect 23260573Skris * message if there was an error). This never returns if there was an error. 23360573Skris */ 23460573Skrisvoid channel_input_port_forward_request(int is_root, int gateway_ports); 23560573Skris 23660573Skris/* 23760573Skris * Creates a port for X11 connections, and starts listening for it. Returns 23860573Skris * the display name, or NULL if an error was encountered. 23960573Skris */ 24060573Skrischar *x11_create_display(int screen); 24160573Skris 24260573Skris/* 24360573Skris * Creates an internet domain socket for listening for X11 connections. 24460573Skris * Returns a suitable value for the DISPLAY variable, or NULL if an error 24560573Skris * occurs. 24660573Skris */ 24760573Skrischar *x11_create_display_inet(int screen, int x11_display_offset); 24860573Skris 24960573Skris/* 25060573Skris * This is called when SSH_SMSG_X11_OPEN is received. The packet contains 25160573Skris * the remote channel number. We should do whatever we want, and respond 25260573Skris * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE. 25360573Skris */ 25469587Sgreenvoid x11_input_open(int type, int plen, void *ctxt); 25560573Skris 25660573Skris/* 25760573Skris * Requests forwarding of X11 connections. This should be called on the 25860573Skris * client only. 25960573Skris */ 26060573Skrisvoid x11_request_forwarding(void); 26160573Skris 26260573Skris/* 26360573Skris * Requests forwarding for X11 connections, with authentication spoofing. 26460573Skris * This should be called in the client only. 26560573Skris */ 26660573Skrisvoid 26760573Skrisx11_request_forwarding_with_spoofing(int client_session_id, 26860573Skris const char *proto, const char *data); 26960573Skris 27060573Skris/* Sends a message to the server to request authentication fd forwarding. */ 27160573Skrisvoid auth_request_forwarding(void); 27260573Skris 27360573Skris/* 27460573Skris * Returns the name of the forwarded authentication socket. Returns NULL if 27560573Skris * there is no forwarded authentication socket. The returned value points to 27660573Skris * a static buffer. 27760573Skris */ 27860573Skrischar *auth_get_socket_name(void); 27960573Skris 28060573Skris/* 28161199Skris * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server. 28260573Skris * This starts forwarding authentication requests. 28360573Skris */ 28461199Skrisint auth_input_request_forwarding(struct passwd * pw); 28560573Skris 28660573Skris/* This is called to process an SSH_SMSG_AGENT_OPEN message. */ 28769587Sgreenvoid auth_input_open_request(int type, int plen, void *ctxt); 28860573Skris 28960573Skris/* XXX */ 29060573Skrisint channel_connect_to(const char *host, u_short host_port); 29160573Skrisint x11_connect_display(void); 29260573Skris 29357429Smarkm#endif 294