From 6bc64c368d49d2c775fa4a0e94dc65fcee746710 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Mon, 11 Feb 2013 21:35:16 +0100 Subject: [PATCH] server: added channel callbacks Reviewed-by: Andreas Schneider --- include/libssh/callbacks.h | 36 +++++++++++++++++++++++++++++++++ include/libssh/messages.h | 1 + src/messages.c | 41 +++++++++++++++++++++++++------------- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/include/libssh/callbacks.h b/include/libssh/callbacks.h index 6dd90a85..45935206 100644 --- a/include/libssh/callbacks.h +++ b/include/libssh/callbacks.h @@ -439,6 +439,34 @@ typedef void (*ssh_channel_exit_signal_callback) (ssh_session session, const char *lang, void *userdata); +/** + * @brief SSH channel PTY request from a client. + * @param channel the channel + * @param width width of the terminal, in characters + * @param height height of the terminal, in characters + * @param pxwidth width of the terminal, in pixels + * @param pxheight height of the terminal, in pixels + * @param userdata Userdata to be passed to the callback function. + * @returns 0 if the pty request is accepted + * @returns -1 if the request is denied + */ +typedef int (*ssh_channel_pty_request_callback) (ssh_session session, + ssh_channel channel, + const char *term, + int width, int height, + int pxwidth, int pwheight, + void *userdata); + +/** + * @brief SSH channel Shell request from a client. + * @param channel the channel + * @param userdata Userdata to be passed to the callback function. + * @returns 0 if the pty request is accepted + * @returns 1 if the request is denied + */ +typedef int (*ssh_channel_shell_request_callback) (ssh_session session, + ssh_channel channel, + void *userdata); struct ssh_channel_callbacks_struct { /** DON'T SET THIS use ssh_callbacks_init() instead. */ size_t size; @@ -470,6 +498,14 @@ struct ssh_channel_callbacks_struct { * This functions will be called when an exit signal has been received */ ssh_channel_exit_signal_callback channel_exit_signal_function; + /** + * This function will be called when a client requests a PTY + */ + ssh_channel_pty_request_callback channel_pty_request_function; + /** + * This function will be called when a client requests a shell + */ + ssh_channel_shell_request_callback channel_shell_request_function; }; typedef struct ssh_channel_callbacks_struct *ssh_channel_callbacks; diff --git a/include/libssh/messages.h b/include/libssh/messages.h index f196c6f7..dbe437b2 100644 --- a/include/libssh/messages.h +++ b/include/libssh/messages.h @@ -100,5 +100,6 @@ int ssh_message_handle_channel_request(ssh_session session, ssh_channel channel, const char *request, uint8_t want_reply); void ssh_message_queue(ssh_session session, ssh_message message); ssh_message ssh_message_pop_head(ssh_session session); +int ssh_message_channel_request_open_reply_accept_channel(ssh_message msg, ssh_channel chan); #endif /* MESSAGES_H_ */ diff --git a/src/messages.c b/src/messages.c index 5fcdd04e..eab275b9 100644 --- a/src/messages.c +++ b/src/messages.c @@ -930,26 +930,18 @@ end: return SSH_PACKET_USED; } -/* TODO: make this function accept a ssh_channel */ -ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg) { +int ssh_message_channel_request_open_reply_accept_channel(ssh_message msg, ssh_channel chan) { ssh_session session; - ssh_channel chan = NULL; enter_function(); if (msg == NULL) { leave_function(); - return NULL; + return SSH_ERROR; } session = msg->session; - chan = ssh_channel_new(session); - if (chan == NULL) { - leave_function(); - return NULL; - } - chan->local_channel = ssh_channel_new_id(session); chan->local_maxpacket = 35000; chan->local_window = 32000; @@ -982,12 +974,33 @@ ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg) { } leave_function(); - return chan; -error: - ssh_channel_free(chan); + return SSH_OK; + error: leave_function(); - return NULL; + return SSH_ERROR; +} + + +ssh_channel ssh_message_channel_request_open_reply_accept(ssh_message msg) { + ssh_channel chan; + int rc; + + if (msg == NULL) { + return NULL; + } + + chan = ssh_channel_new(msg->session); + if (chan == NULL) { + return NULL; + } + rc = ssh_message_channel_request_open_reply_accept_channel(msg, chan); + if (rc < 0) { + ssh_channel_free(chan); + chan = NULL; + } + return chan; + } /**