libssh  0.8.3
The SSH library
callbacks.h
1 /*
2  * This file is part of the SSH Library
3  *
4  * Copyright (c) 2009 Aris Adamantiadis <aris@0xbadc0de.be>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /* callback.h
22  * This file includes the public declarations for the libssh callback mechanism
23  */
24 
25 #ifndef _SSH_CALLBACK_H
26 #define _SSH_CALLBACK_H
27 
28 #include <libssh/libssh.h>
29 #include <string.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
49 typedef void (*ssh_callback_int) (int code, void *user);
50 
59 typedef int (*ssh_callback_data) (const void *data, size_t len, void *user);
60 
61 typedef void (*ssh_callback_int_int) (int code, int errno_code, void *user);
62 
63 typedef int (*ssh_message_callback) (ssh_session, ssh_message message, void *user);
64 typedef int (*ssh_channel_callback_int) (ssh_channel channel, int code, void *user);
65 typedef int (*ssh_channel_callback_data) (ssh_channel channel, int code, void *data, size_t len, void *user);
66 
74 typedef void (*ssh_log_callback) (ssh_session session, int priority,
75  const char *message, void *userdata);
76 
90 typedef void (*ssh_logging_callback) (int priority,
91  const char *function,
92  const char *buffer,
93  void *userdata);
94 
102 typedef void (*ssh_status_callback) (ssh_session session, float status,
103  void *userdata);
104 
112 typedef void (*ssh_global_request_callback) (ssh_session session,
113  ssh_message message, void *userdata);
114 
125  const char * originator_address, int originator_port, void *userdata);
126 
137  void *userdata);
138 
144  size_t size;
148  void *userdata;
152  ssh_auth_callback auth_function;
161  void (*connect_status_function)(void *userdata, float status);
172 };
173 typedef struct ssh_callbacks_struct *ssh_callbacks;
174 
188 typedef int (*ssh_auth_password_callback) (ssh_session session, const char *user, const char *password,
189  void *userdata);
190 
201 typedef int (*ssh_auth_none_callback) (ssh_session session, const char *user, void *userdata);
202 
215 typedef int (*ssh_auth_gssapi_mic_callback) (ssh_session session, const char *user, const char *principal,
216  void *userdata);
217 
231 typedef int (*ssh_auth_pubkey_callback) (ssh_session session, const char *user, struct ssh_key_struct *pubkey,
232  char signature_state, void *userdata);
233 
234 
244 typedef int (*ssh_service_request_callback) (ssh_session session, const char *service, void *userdata);
245 
255 
256 /*
257  * @brief handle the beginning of a GSSAPI authentication, server side.
258  * @param session current session handler
259  * @param user the username of the client
260  * @param n_oid number of available oids
261  * @param oids OIDs provided by the client
262  * @returns an ssh_string containing the chosen OID, that's supported by both
263  * client and server.
264  * @warning It is not necessary to fill this callback in if libssh is linked
265  * with libgssapi.
266  */
267 typedef ssh_string (*ssh_gssapi_select_oid_callback) (ssh_session session, const char *user,
268  int n_oid, ssh_string *oids, void *userdata);
269 
270 /*
271  * @brief handle the negociation of a security context, server side.
272  * @param session current session handler
273  * @param[in] input_token input token provided by client
274  * @param[out] output_token output of the gssapi accept_sec_context method,
275  * NULL after completion.
276  * @returns SSH_OK if the token was generated correctly or accept_sec_context
277  * returned GSS_S_COMPLETE
278  * @returns SSH_ERROR in case of error
279  * @warning It is not necessary to fill this callback in if libssh is linked
280  * with libgssapi.
281  */
282 typedef int (*ssh_gssapi_accept_sec_ctx_callback) (ssh_session session,
283  ssh_string input_token, ssh_string *output_token, void *userdata);
284 
285 /*
286  * @brief Verify and authenticates a MIC, server side.
287  * @param session current session handler
288  * @param[in] mic input mic to be verified provided by client
289  * @param[in] mic_buffer buffer of data to be signed.
290  * @param[in] mic_buffer_size size of mic_buffer
291  * @returns SSH_OK if the MIC was authenticated correctly
292  * @returns SSH_ERROR in case of error
293  * @warning It is not necessary to fill this callback in if libssh is linked
294  * with libgssapi.
295  */
296 typedef int (*ssh_gssapi_verify_mic_callback) (ssh_session session,
297  ssh_string mic, void *mic_buffer, size_t mic_buffer_size, void *userdata);
298 
299 
306  size_t size;
310  void *userdata;
315 
320 
325 
330 
341  ssh_gssapi_select_oid_callback gssapi_select_oid_function;
344  ssh_gssapi_accept_sec_ctx_callback gssapi_accept_sec_ctx_function;
345  /* This function will be called when a MIC needs to be verified.
346  */
347  ssh_gssapi_verify_mic_callback gssapi_verify_mic_function;
348 };
350 
372 LIBSSH_API int ssh_set_server_callbacks(ssh_session session, ssh_server_callbacks cb);
373 
382  void *userdata;
387  ssh_callback_data data;
391  ssh_callback_int controlflow;
395  ssh_callback_int_int exception;
399  ssh_callback_int_int connected;
400 };
402 
403 #define SSH_SOCKET_FLOW_WRITEWILLBLOCK 1
404 #define SSH_SOCKET_FLOW_WRITEWONTBLOCK 2
405 
406 #define SSH_SOCKET_EXCEPTION_EOF 1
407 #define SSH_SOCKET_EXCEPTION_ERROR 2
408 
409 #define SSH_SOCKET_CONNECTED_OK 1
410 #define SSH_SOCKET_CONNECTED_ERROR 2
411 #define SSH_SOCKET_CONNECTED_TIMEOUT 3
412 
420 #define ssh_callbacks_init(p) do {\
421  (p)->size=sizeof(*(p)); \
422 } while(0);
423 
433 #define ssh_callbacks_exists(p,c) (\
434  (p != NULL) && ( (char *)&((p)-> c) < (char *)(p) + (p)->size ) && \
435  ((p)-> c != NULL) \
436  )
437 
454 #define ssh_callbacks_execute_list(list, cbtype, c, ...) \
455  do { \
456  struct ssh_iterator *i = ssh_list_get_iterator(list); \
457  cbtype cb; \
458  while (i != NULL){ \
459  cb = ssh_iterator_value(cbtype, i); \
460  if (ssh_callbacks_exists(cb, c)) \
461  cb-> c (__VA_ARGS__, cb->userdata); \
462  i = i->next; \
463  } \
464  } while(0)
465 
486 #define ssh_callbacks_iterate(_cb_list, _cb_type, _cb_name) \
487  do { \
488  struct ssh_iterator *_cb_i = ssh_list_get_iterator(_cb_list); \
489  _cb_type _cb; \
490  for (; _cb_i != NULL; _cb_i = _cb_i->next) { \
491  _cb = ssh_iterator_value(_cb_type, _cb_i); \
492  if (ssh_callbacks_exists(_cb, _cb_name))
493 
494 #define ssh_callbacks_iterate_exec(_cb_name, ...) \
495  _cb->_cb_name(__VA_ARGS__, _cb->userdata)
496 
497 #define ssh_callbacks_iterate_end() \
498  } \
499  } while(0)
500 
510 typedef int (*ssh_packet_callback) (ssh_session session, uint8_t type, ssh_buffer packet, void *user);
511 
514 #define SSH_PACKET_USED 1
515 
517 #define SSH_PACKET_NOT_USED 2
518 
519 
527 #define SSH_PACKET_CALLBACK(name) \
528  int name (ssh_session session, uint8_t type, ssh_buffer packet, void *user)
529 
532  uint8_t start;
534  uint8_t n_callbacks;
540  void *user;
541 };
542 
544 
566 LIBSSH_API int ssh_set_callbacks(ssh_session session, ssh_callbacks cb);
567 
579 typedef int (*ssh_channel_data_callback) (ssh_session session,
580  ssh_channel channel,
581  void *data,
582  uint32_t len,
583  int is_stderr,
584  void *userdata);
585 
592 typedef void (*ssh_channel_eof_callback) (ssh_session session,
593  ssh_channel channel,
594  void *userdata);
595 
602 typedef void (*ssh_channel_close_callback) (ssh_session session,
603  ssh_channel channel,
604  void *userdata);
605 
613 typedef void (*ssh_channel_signal_callback) (ssh_session session,
614  ssh_channel channel,
615  const char *signal,
616  void *userdata);
617 
625  ssh_channel channel,
626  int exit_status,
627  void *userdata);
628 
640  ssh_channel channel,
641  const char *signal,
642  int core,
643  const char *errmsg,
644  const char *lang,
645  void *userdata);
646 
660  ssh_channel channel,
661  const char *term,
662  int width, int height,
663  int pxwidth, int pwheight,
664  void *userdata);
665 
674  ssh_channel channel,
675  void *userdata);
684  ssh_channel channel,
685  void *userdata);
686 
694 typedef void (*ssh_channel_x11_req_callback) (ssh_session session,
695  ssh_channel channel,
696  int single_connection,
697  const char *auth_protocol,
698  const char *auth_cookie,
699  uint32_t screen_number,
700  void *userdata);
713  ssh_channel channel,
714  int width, int height,
715  int pxwidth, int pwheight,
716  void *userdata);
717 
727  ssh_channel channel,
728  const char *command,
729  void *userdata);
730 
743  ssh_channel channel,
744  const char *env_name,
745  const char *env_value,
746  void *userdata);
756  ssh_channel channel,
757  const char *subsystem,
758  void *userdata);
759 
773  ssh_channel channel,
774  size_t bytes,
775  void *userdata);
776 
779  size_t size;
783  void *userdata;
844 };
845 
847 
871 LIBSSH_API int ssh_set_channel_callbacks(ssh_channel channel,
873 
890 LIBSSH_API int ssh_add_channel_callbacks(ssh_channel channel,
892 
905 LIBSSH_API int ssh_remove_channel_callbacks(ssh_channel channel,
907 
914 typedef int (*ssh_thread_callback) (void **lock);
915 
916 typedef unsigned long (*ssh_thread_id_callback) (void);
918  const char *type;
919  ssh_thread_callback mutex_init;
920  ssh_thread_callback mutex_destroy;
921  ssh_thread_callback mutex_lock;
922  ssh_thread_callback mutex_unlock;
923  ssh_thread_id_callback thread_id;
924 };
925 
944  *cb);
945 
952 LIBSSH_API struct ssh_threads_callbacks_struct *ssh_threads_get_pthread(void);
953 
964 LIBSSH_API struct ssh_threads_callbacks_struct *ssh_threads_get_noop(void);
965 
973 LIBSSH_API int ssh_set_log_callback(ssh_logging_callback cb);
974 
981 
983 #ifdef __cplusplus
984 }
985 #endif
986 
987 #endif /*_SSH_CALLBACK_H */
988 
989 /* @} */
ssh_service_request_callback service_request_function
Definition: callbacks.h:334
ssh_channel_exit_status_callback channel_exit_status_function
Definition: callbacks.h:803
void(* ssh_status_callback)(ssh_session session, float status, void *userdata)
SSH Connection status callback.
Definition: callbacks.h:102
ssh_channel_env_request_callback channel_env_request_function
Definition: callbacks.h:835
void(* ssh_channel_auth_agent_req_callback)(ssh_session session, ssh_channel channel, void *userdata)
SSH auth-agent-request from the client. This request is sent by a client when agent forwarding is ava...
Definition: callbacks.h:683
Definition: pki.h:42
ssh_channel_pty_window_change_callback channel_pty_window_change_function
Definition: callbacks.h:827
Definition: callbacks.h:378
ssh_log_callback log_function
Definition: callbacks.h:156
uint8_t start
Definition: callbacks.h:532
int(* ssh_channel_pty_window_change_callback)(ssh_session session, ssh_channel channel, int width, int height, int pxwidth, int pwheight, void *userdata)
SSH channel PTY windows change (terminal size) from a client.
Definition: callbacks.h:712
Definition: callbacks.h:777
Definition: callbacks.h:142
int(* ssh_channel_data_callback)(ssh_session session, ssh_channel channel, void *data, uint32_t len, int is_stderr, void *userdata)
SSH channel data callback. Called when data is available on a channel.
Definition: callbacks.h:579
int(* ssh_service_request_callback)(ssh_session session, const char *service, void *userdata)
Handles an SSH service request.
Definition: callbacks.h:244
size_t size
Definition: callbacks.h:306
ssh_channel_open_request_auth_agent_callback channel_open_request_auth_agent_function
Definition: callbacks.h:171
ssh_callback_data data
Definition: callbacks.h:387
ssh_gssapi_select_oid_callback gssapi_select_oid_function
Definition: callbacks.h:341
ssh_channel_signal_callback channel_signal_function
Definition: callbacks.h:799
Definition: messages.h:84
ssh_channel_write_wontblock_callback channel_write_wontblock_function
Definition: callbacks.h:843
int(* ssh_auth_none_callback)(ssh_session session, const char *user, void *userdata)
SSH authentication callback. Tries to authenticates user with the "none" method which is anonymous or...
Definition: callbacks.h:201
int(* ssh_auth_password_callback)(ssh_session session, const char *user, const char *password, void *userdata)
SSH authentication callback.
Definition: callbacks.h:188
Definition: channels.h:57
ssh_auth_gssapi_mic_callback auth_gssapi_mic_function
Definition: callbacks.h:324
size_t size
Definition: callbacks.h:779
void * userdata
Definition: callbacks.h:148
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)
SSH channel PTY request from a client.
Definition: callbacks.h:659
ssh_channel_shell_request_callback channel_shell_request_function
Definition: callbacks.h:815
int(* ssh_packet_callback)(ssh_session session, uint8_t type, ssh_buffer packet, void *user)
Prototype for a packet callback, to be called when a new packet arrives.
Definition: callbacks.h:510
void(* connect_status_function)(void *userdata, float status)
Definition: callbacks.h:161
ssh_auth_callback auth_function
Definition: callbacks.h:152
ssh_channel_x11_req_callback channel_x11_req_function
Definition: callbacks.h:823
ssh_channel_exit_signal_callback channel_exit_signal_function
Definition: callbacks.h:807
LIBSSH_API int ssh_set_callbacks(ssh_session session, ssh_callbacks cb)
Set the session callback functions.
Definition: callbacks.c:48
Definition: callbacks.h:530
Definition: session.h:102
ssh_channel(* ssh_channel_open_request_auth_agent_callback)(ssh_session session, void *userdata)
Handles an SSH new channel open "auth-agent" request. This happens when the server sends back an "aut...
Definition: callbacks.h:136
ssh_channel_open_request_x11_callback channel_open_request_x11_function
Definition: callbacks.h:168
ssh_packet_callback * callbacks
Definition: callbacks.h:536
ssh_channel_pty_request_callback channel_pty_request_function
Definition: callbacks.h:811
ssh_channel_subsystem_request_callback channel_subsystem_request_function
Definition: callbacks.h:839
ssh_channel(* ssh_channel_open_request_session_callback)(ssh_session session, void *userdata)
Handles an SSH new channel open session request.
Definition: callbacks.h:254
Definition: callbacks.h:917
int(* ssh_channel_env_request_callback)(ssh_session session, ssh_channel channel, const char *env_name, const char *env_value, void *userdata)
SSH channel environment request from a client.
Definition: callbacks.h:742
void(* ssh_global_request_callback)(ssh_session session, ssh_message message, void *userdata)
SSH global request callback. All global request will go through this callback.
Definition: callbacks.h:112
uint8_t n_callbacks
Definition: callbacks.h:534
ssh_channel_close_callback channel_close_function
Definition: callbacks.h:795
ssh_auth_password_callback auth_password_function
Definition: callbacks.h:314
LIBSSH_API int ssh_set_channel_callbacks(ssh_channel channel, ssh_channel_callbacks cb)
Set the channel callback functions.
Definition: callbacks.c:104
void * userdata
Definition: callbacks.h:382
void * user
Definition: callbacks.h:540
void * userdata
Definition: callbacks.h:310
LIBSSH_API int ssh_set_server_callbacks(ssh_session session, ssh_server_callbacks cb)
Set the session server callback functions.
Definition: callbacks.c:133
Definition: callbacks.h:304
void(* ssh_logging_callback)(int priority, const char *function, const char *buffer, void *userdata)
SSH log callback.
Definition: callbacks.h:90
ssh_auth_pubkey_callback auth_pubkey_function
Definition: callbacks.h:329
void(* ssh_channel_x11_req_callback)(ssh_session session, ssh_channel channel, int single_connection, const char *auth_protocol, const char *auth_cookie, uint32_t screen_number, void *userdata)
SSH X11 request from the client. This request is sent by a client when X11 forwarding is requested(an...
Definition: callbacks.h:694
ssh_callback_int controlflow
Definition: callbacks.h:391
void(* ssh_channel_eof_callback)(ssh_session session, ssh_channel channel, void *userdata)
SSH channel eof callback. Called when a channel receives EOF.
Definition: callbacks.h:592
Definition: string.h:29
int(* ssh_channel_shell_request_callback)(ssh_session session, ssh_channel channel, void *userdata)
SSH channel Shell request from a client.
Definition: callbacks.h:673
LIBSSH_API ssh_logging_callback ssh_get_log_callback(void)
Get the pointer to the logging callback function.
Definition: log.c:210
int(* ssh_auth_pubkey_callback)(ssh_session session, const char *user, struct ssh_key_struct *pubkey, char signature_state, void *userdata)
SSH authentication callback.
Definition: callbacks.h:231
ssh_global_request_callback global_request_function
Definition: callbacks.h:165
LIBSSH_API int ssh_add_channel_callbacks(ssh_channel channel, ssh_channel_callbacks cb)
Add channel callback functions.
Definition: callbacks.c:109
LIBSSH_API int ssh_remove_channel_callbacks(ssh_channel channel, ssh_channel_callbacks cb)
Remove a channel callback.
Definition: callbacks.c:114
ssh_gssapi_accept_sec_ctx_callback gssapi_accept_sec_ctx_function
Definition: callbacks.h:344
ssh_channel_eof_callback channel_eof_function
Definition: callbacks.h:791
void(* ssh_channel_close_callback)(ssh_session session, ssh_channel channel, void *userdata)
SSH channel close callback. Called when a channel is closed by remote peer.
Definition: callbacks.h:602
void(* ssh_channel_exit_signal_callback)(ssh_session session, ssh_channel channel, const char *signal, int core, const char *errmsg, const char *lang, void *userdata)
SSH channel exit signal callback. Called when a channel has received an exit signal.
Definition: callbacks.h:639
ssh_channel_exec_request_callback channel_exec_request_function
Definition: callbacks.h:831
LIBSSH_API int ssh_set_log_callback(ssh_logging_callback cb)
Set the logging callback function.
Definition: log.c:200
ssh_auth_none_callback auth_none_function
Definition: callbacks.h:319
void(* ssh_log_callback)(ssh_session session, int priority, const char *message, void *userdata)
SSH log callback. All logging messages will go through this callback.
Definition: callbacks.h:74
int(* ssh_channel_subsystem_request_callback)(ssh_session session, ssh_channel channel, const char *subsystem, void *userdata)
SSH channel subsystem request from a client.
Definition: callbacks.h:755
void(* ssh_channel_signal_callback)(ssh_session session, ssh_channel channel, const char *signal, void *userdata)
SSH channel signal callback. Called when a channel has received a signal.
Definition: callbacks.h:613
void(* ssh_channel_exit_status_callback)(ssh_session session, ssh_channel channel, int exit_status, void *userdata)
SSH channel exit status callback. Called when a channel has received an exit status.
Definition: callbacks.h:624
ssh_callback_int_int exception
Definition: callbacks.h:395
LIBSSH_API int ssh_threads_set_callbacks(struct ssh_threads_callbacks_struct *cb)
Set the thread callbacks structure.
Definition: threads.c:71
int(* ssh_auth_gssapi_mic_callback)(ssh_session session, const char *user, const char *principal, void *userdata)
SSH authentication callback. Tries to authenticates user with the "gssapi-with-mic" method...
Definition: callbacks.h:215
int(* ssh_channel_write_wontblock_callback)(ssh_session session, ssh_channel channel, size_t bytes, void *userdata)
SSH channel write will not block (flow control).
Definition: callbacks.h:772
size_t size
Definition: callbacks.h:144
ssh_callback_int_int connected
Definition: callbacks.h:399
ssh_channel_data_callback channel_data_function
Definition: callbacks.h:787
ssh_channel_auth_agent_req_callback channel_auth_agent_req_function
Definition: callbacks.h:819
ssh_channel_open_request_session_callback channel_open_request_session_function
Definition: callbacks.h:338
void * userdata
Definition: callbacks.h:783
ssh_channel(* ssh_channel_open_request_x11_callback)(ssh_session session, const char *originator_address, int originator_port, void *userdata)
Handles an SSH new channel open X11 request. This happens when the server sends back an X11 connectio...
Definition: callbacks.h:124
int(* ssh_channel_exec_request_callback)(ssh_session session, ssh_channel channel, const char *command, void *userdata)
SSH channel Exec request from a client.
Definition: callbacks.h:726
Definition: buffer.c:47