1 module libssh.c_bindings.callbacks;
2 
3 import libssh.c_bindings.libssh;
4 
5 /**
6  * @defgroup libssh_callbacks The libssh callbacks
7  * @ingroup libssh
8  *
9  * Callback which can be replaced in libssh.
10  *
11  * @{
12  */
13 
14 /** @internal
15  * @brief callback to process simple codes
16  * @param code value to transmit
17  * @param user Userdata to pass in callback
18  */
19 alias ssh_callback_int = extern(C) void function(int code, void *user);
20 
21 /** @internal
22  * @brief callback for data received messages.
23  * @param data data retrieved from the socket or stream
24  * @param len number of bytes available from this stream
25  * @param user user-supplied pointer sent along with all callback messages
26  * @returns number of bytes processed by the callee. The remaining bytes will
27  * be sent in the next callback message, when more data is available.
28  */
29 alias ssh_callback_data = extern(C) int function(const void *data, size_t len, void *user);
30 
31 alias ssh_callback_int_int = extern(C) void function(int code, int errno_code, void *user);
32 
33 alias ssh_message_callback = extern(C) int function(ssh_session, ssh_message message, void *user);
34 alias ssh_channel_callback_int = extern(C) int function(ssh_channel channel, int code, void *user);
35 alias ssh_channel_callback_data = extern(C) int function(ssh_channel channel, int code, void *data, 
36     size_t len, void *user);
37 
38 /**
39  * @brief SSH log callback. All logging messages will go through this callback
40  * @param session Current session handler
41  * @param priority Priority of the log, the smaller being the more important
42  * @param message the actual message
43  * @param userdata Userdata to be passed to the callback function.
44  */
45 alias ssh_log_callback = extern(C) void function(ssh_session session, int priority, 
46     const char *message, void *userdata);
47 
48 /**
49  * @brief SSH log callback.
50  *
51  * All logging messages will go through this callback.
52  *
53  * @param priority  Priority of the log, the smaller being the more important.
54  *
55  * @param function  The function name calling the the logging fucntions.
56  *
57  * @param message   The actual message
58  *
59  * @param userdata Userdata to be passed to the callback function.
60  */
61 alias ssh_logging_callback = extern(C) void function(int priority, const char *function_, 
62     const char *buffer, void *userdata);
63 
64 /**
65  * @brief SSH Connection status callback.
66  * @param session Current session handler
67  * @param status Percentage of connection status, going from 0.0 to 1.0
68  * once connection is done.
69  * @param userdata Userdata to be passed to the callback function.
70  */
71 alias ssh_status_callback = extern(C) void function(ssh_session session, float status, 
72     void *userdata);
73 
74 /**
75  * @brief SSH global request callback. All global request will go through this
76  * callback.
77  * @param session Current session handler
78  * @param message the actual message
79  * @param userdata Userdata to be passed to the callback function.
80  */
81 alias ssh_global_request_callback = extern(C) void function(ssh_session session, 
82     ssh_message message, void *userdata);
83 
84 /**
85  * @brief Handles an SSH new channel open X11 request. This happens when the server
86  * sends back an X11 connection attempt. This is a client-side API
87  * @param session current session handler
88  * @param userdata Userdata to be passed to the callback function.
89  * @returns a valid ssh_channel handle if the request is to be allowed
90  * @returns NULL if the request should not be allowed
91  * @warning The channel pointer returned by this callback must be closed by the application.
92  */
93 alias ssh_channel_open_request_x11_callback = extern(C) ssh_channel function(ssh_session session, 
94     const char * originator_address, int originator_port, void *userdata);
95 
96 /**
97  * The structure to replace libssh functions with appropriate callbacks.
98  */
99 struct ssh_callbacks_struct {
100     /** DON'T SET THIS use ssh_callbacks_init() instead. */
101     size_t size;
102     /**
103    * User-provided data. User is free to set anything he wants here
104    */
105     void *userdata;
106     /**
107    * This functions will be called if e.g. a keyphrase is needed.
108    */
109     ssh_auth_callback auth_function;
110     /**
111    * This function will be called each time a loggable event happens.
112    */
113     ssh_log_callback log_function;
114     /**
115    * This function gets called during connection time to indicate the
116    * percentage of connection steps completed.
117    */
118     extern(C) void function(void *userdata, float status) connect_status_function;
119     /**
120    * This function will be called each time a global request is received.
121    */
122     ssh_global_request_callback global_request_function;
123     /** This function will be called when an incoming X11 request is received.
124    */
125     ssh_channel_open_request_x11_callback channel_open_request_x11_function;
126 };
127 alias ssh_callbacks = ssh_callbacks_struct*;
128 
129 /** These are callbacks used specifically in SSH servers.
130  */
131 
132 /**
133  * @brief SSH authentication callback.
134  * @param session Current session handler
135  * @param user User that wants to authenticate
136  * @param password Password used for authentication
137  * @param userdata Userdata to be passed to the callback function.
138  * @returns SSH_AUTH_SUCCESS Authentication is accepted.
139  * @returns SSH_AUTH_PARTIAL Partial authentication, more authentication means are needed.
140  * @returns SSH_AUTH_DENIED Authentication failed.
141  */
142 alias ssh_auth_password_callback = extern(C) int function(ssh_session session, const char *user, 
143     const char *password, void *userdata);
144 
145 /**
146  * @brief SSH authentication callback. Tries to authenticates user with the "none" method
147  * which is anonymous or passwordless.
148  * @param session Current session handler
149  * @param user User that wants to authenticate
150  * @param userdata Userdata to be passed to the callback function.
151  * @returns SSH_AUTH_SUCCESS Authentication is accepted.
152  * @returns SSH_AUTH_PARTIAL Partial authentication, more authentication means are needed.
153  * @returns SSH_AUTH_DENIED Authentication failed.
154  */
155 alias ssh_auth_none_callback = extern(C) int function(ssh_session session, const char *user, 
156     void *userdata);
157 
158 /**
159  * @brief SSH authentication callback. Tries to authenticates user with the "gssapi-with-mic" method
160  * @param session Current session handler
161  * @param user Username of the user (can be spoofed)
162  * @param principal Authenticated principal of the user, including realm.
163  * @param userdata Userdata to be passed to the callback function.
164  * @returns SSH_AUTH_SUCCESS Authentication is accepted.
165  * @returns SSH_AUTH_PARTIAL Partial authentication, more authentication means are needed.
166  * @returns SSH_AUTH_DENIED Authentication failed.
167  * @warning Implementations should verify that parameter user matches in some way the principal.
168  * user and principal can be different. Only the latter is guaranteed to be safe.
169  */
170 alias ssh_auth_gssapi_mic_callback = extern(C) int function(ssh_session session, const char *user, 
171     const char *principal, void *userdata);
172 
173 /**
174  * @brief SSH authentication callback.
175  * @param session Current session handler
176  * @param user User that wants to authenticate
177  * @param pubkey public key used for authentication
178  * @param signature_state SSH_PUBLICKEY_STATE_NONE if the key is not signed (simple public key probe),
179  *                          SSH_PUBLICKEY_STATE_VALID if the signature is valid. Others values should be
180  *                          replied with a SSH_AUTH_DENIED.
181  * @param userdata Userdata to be passed to the callback function.
182  * @returns SSH_AUTH_SUCCESS Authentication is accepted.
183  * @returns SSH_AUTH_PARTIAL Partial authentication, more authentication means are needed.
184  * @returns SSH_AUTH_DENIED Authentication failed.
185  */
186 alias ssh_auth_pubkey_callback = extern(C) int function(ssh_session session, const char *user, 
187     ssh_key_struct *pubkey, byte signature_state, void *userdata);
188 
189 /**
190  * @brief Handles an SSH service request
191  * @param session current session handler
192  * @param service name of the service (e.g. "ssh-userauth") requested
193  * @param userdata Userdata to be passed to the callback function.
194  * @returns 0 if the request is to be allowed
195  * @returns -1 if the request should not be allowed
196  */
197 alias ssh_service_request_callback = extern(C) int function(ssh_session session, 
198     const char *service, void *userdata);
199 
200 /**
201  * @brief Handles an SSH new channel open session request
202  * @param session current session handler
203  * @param userdata Userdata to be passed to the callback function.
204  * @returns a valid ssh_channel handle if the request is to be allowed
205  * @returns NULL if the request should not be allowed
206  * @warning The channel pointer returned by this callback must be closed by the application.
207  */
208 alias ssh_channel_open_request_session_callback = extern(C) ssh_channel function(
209     ssh_session session, void *userdata);
210 
211 /*
212  * @brief handle the beginning of a GSSAPI authentication, server side.
213  * @param session current session handler
214  * @param user the username of the client
215  * @param n_oid number of available oids
216  * @param oids OIDs provided by the client
217  * @returns an ssh_string containing the chosen OID, that's supported by both
218  * client and server.
219  * @warning It is not necessary to fill this callback in if libssh is linked
220  * with libgssapi.
221  */
222 alias ssh_gssapi_select_oid_callback = extern(C) ssh_string function(ssh_session session, 
223     const char *user, int n_oid, ssh_string *oids, void *userdata);
224 
225 /*
226  * @brief handle the negociation of a security context, server side.
227  * @param session current session handler
228  * @param[in] input_token input token provided by client
229  * @param[out] output_token output of the gssapi accept_sec_context method,
230  *              NULL after completion.
231  * @returns SSH_OK if the token was generated correctly or accept_sec_context
232  * returned GSS_S_COMPLETE
233  * @returns SSH_ERROR in case of error
234  * @warning It is not necessary to fill this callback in if libssh is linked
235  * with libgssapi.
236  */
237 alias ssh_gssapi_accept_sec_ctx_callback = extern(C) int function(ssh_session session, 
238     ssh_string input_token, ssh_string *output_token, void *userdata);
239 
240 /*
241  * @brief Verify and authenticates a MIC, server side.
242  * @param session current session handler
243  * @param[in] mic input mic to be verified provided by client
244  * @param[in] mic_buffer buffer of data to be signed.
245  * @param[in] mic_buffer_size size of mic_buffer
246  * @returns SSH_OK if the MIC was authenticated correctly
247  * @returns SSH_ERROR in case of error
248  * @warning It is not necessary to fill this callback in if libssh is linked
249  * with libgssapi.
250  */
251 alias ssh_gssapi_verify_mic_callback = extern(C) int function(ssh_session session, ssh_string mic, 
252     void *mic_buffer, size_t mic_buffer_size, void *userdata);
253 
254 
255 /**
256  * This structure can be used to implement a libssh server, with appropriate callbacks.
257  */
258 struct ssh_server_callbacks_struct {
259     /** DON'T SET THIS use ssh_callbacks_init() instead. */
260     size_t size;
261     /**
262    * User-provided data. User is free to set anything he wants here
263    */
264     void *userdata;
265     /** This function gets called when a client tries to authenticate through
266    * password method.
267    */
268     ssh_auth_password_callback auth_password_function;
269     
270     /** This function gets called when a client tries to authenticate through
271    * none method.
272    */
273     ssh_auth_none_callback auth_none_function;
274     
275     /** This function gets called when a client tries to authenticate through
276    * gssapi-mic method.
277    */
278     ssh_auth_gssapi_mic_callback auth_gssapi_mic_function;
279     
280     /** this function gets called when a client tries to authenticate or offer
281    * a public key.
282    */
283     ssh_auth_pubkey_callback auth_pubkey_function;
284     
285     /** This functions gets called when a service request is issued by the
286    * client
287    */
288     ssh_service_request_callback service_request_function;
289     /** This functions gets called when a new channel request is issued by
290    * the client
291    */
292     ssh_channel_open_request_session_callback channel_open_request_session_function;
293     /** This function will be called when a new gssapi authentication is attempted.
294    */
295     ssh_gssapi_select_oid_callback gssapi_select_oid_function;
296     /** This function will be called when a gssapi token comes in.
297    */
298     ssh_gssapi_accept_sec_ctx_callback gssapi_accept_sec_ctx_function;
299     /* This function will be called when a MIC needs to be verified.
300    */
301     ssh_gssapi_verify_mic_callback gssapi_verify_mic_function;
302 };
303 alias ssh_server_callbacks = ssh_server_callbacks_struct*;
304 
305 /**
306  * @brief Set the session server callback functions.
307  *
308  * This functions sets the callback structure to use your own callback
309  * functions for user authentication, new channels and requests.
310  *
311  * @code
312  * struct ssh_server_callbacks_struct cb = {
313  *   .userdata = data,
314  *   .auth_password_function = my_auth_function
315  * };
316  * ssh_callbacks_init(&cb);
317  * ssh_set_server_callbacks(session, &cb);
318  * @endcode
319  *
320  * @param  session      The session to set the callback structure.
321  *
322  * @param  cb           The callback structure itself.
323  *
324  * @return SSH_OK on success, SSH_ERROR on error.
325  */
326 extern(C) int ssh_set_server_callbacks(ssh_session session, ssh_server_callbacks cb);
327 
328 /**
329  * These are the callbacks exported by the socket structure
330  * They are called by the socket module when a socket event appears
331  */
332 struct ssh_socket_callbacks_struct {
333     /**
334    * User-provided data. User is free to set anything he wants here
335    */
336     void *userdata;
337     /**
338      * This function will be called each time data appears on socket. The data
339      * not consumed will appear on the next data event.
340      */
341     ssh_callback_data data;
342     /** This function will be called each time a controlflow state changes, i.e.
343    * the socket is available for reading or writing.
344    */
345     ssh_callback_int controlflow;
346     /** This function will be called each time an exception appears on socket. An
347    * exception can be a socket problem (timeout, ...) or an end-of-file.
348    */
349     ssh_callback_int_int exception;
350     /** This function is called when the ssh_socket_connect was used on the socket
351    * on nonblocking state, and the connection successed.
352    */
353     ssh_callback_int_int connected;
354 };
355 alias ssh_socket_callbacks = ssh_socket_callbacks_struct*;
356 
357 enum auto SSH_SOCKET_FLOW_WRITEWILLBLOCK = 1;
358 enum auto SSH_SOCKET_FLOW_WRITEWONTBLOCK = 2;
359 
360 enum auto SSH_SOCKET_EXCEPTION_EOF =       1;
361 enum auto SSH_SOCKET_EXCEPTION_ERROR =     2;
362 
363 enum auto SSH_SOCKET_CONNECTED_OK =         1;
364 enum auto SSH_SOCKET_CONNECTED_ERROR =      2;
365 enum auto SSH_SOCKET_CONNECTED_TIMEOUT =    3;
366 
367 /**
368  * @brief Initializes an ssh_callbacks_struct
369  * A call to this macro is mandatory when you have set a new
370  * ssh_callback_struct structure. Its goal is to maintain the binary
371  * compatibility with future versions of libssh as the structure
372  * evolves with time.
373  */
374 void ssh_callbacks_init(T)(ref T p) {
375     p.size = T.sizeof;
376 }
377 
378 /** @brief Prototype for a packet callback, to be called when a new packet arrives
379  * @param session The current session of the packet
380  * @param type packet type (see ssh2.h)
381  * @param packet buffer containing the packet, excluding size, type and padding fields
382  * @param user user argument to the callback
383  * and are called each time a packet shows up
384  * @returns SSH_PACKET_USED Packet was parsed and used
385  * @returns SSH_PACKET_NOT_USED Packet was not used or understood, processing must continue
386  */
387 alias ssh_packet_callback = extern(C) int function(ssh_session session, ubyte type, 
388     ssh_buffer packet, void *user);
389 
390 /** return values for a ssh_packet_callback */
391 /** Packet was used and should not be parsed by another callback */
392 enum auto SSH_PACKET_USED = 1;
393 /** Packet was not used and should be passed to any other callback
394  * available */
395 enum auto SSH_PACKET_NOT_USED = 2;
396 
397 struct ssh_packet_callbacks_struct {
398     /** Index of the first packet type being handled */
399     ubyte start;
400     /** Number of packets being handled by this callback struct */
401     ubyte n_callbacks;
402     /** A pointer to n_callbacks packet callbacks */
403     ssh_packet_callback *callbacks;
404     /**
405    * User-provided data. User is free to set anything he wants here
406    */
407     void *user;
408 };
409 alias ssh_packet_callbacks = ssh_packet_callbacks_struct*;
410 
411 /**
412  * @brief Set the session callback functions.
413  *
414  * This functions sets the callback structure to use your own callback
415  * functions for auth, logging and status.
416  *
417  * @code
418  * struct ssh_callbacks_struct cb = {
419  *   .userdata = data,
420  *   .auth_function = my_auth_function
421  * };
422  * ssh_callbacks_init(&cb);
423  * ssh_set_callbacks(session, &cb);
424  * @endcode
425  *
426  * @param  session      The session to set the callback structure.
427  *
428  * @param  cb           The callback structure itself.
429  *
430  * @return SSH_OK on success, SSH_ERROR on error.
431  */
432 extern(C) int ssh_set_callbacks(ssh_session session, ssh_callbacks cb);
433 
434 /**
435  * @brief SSH channel data callback. Called when data is available on a channel
436  * @param session Current session handler
437  * @param channel the actual channel
438  * @param data the data that has been read on the channel
439  * @param len the length of the data
440  * @param is_stderr is 0 for stdout or 1 for stderr
441  * @param userdata Userdata to be passed to the callback function.
442  * @returns number of bytes processed by the callee. The remaining bytes will
443  * be sent in the next callback message, when more data is available.
444  */
445 alias ssh_channel_data_callback = extern(C) int function(ssh_session session, ssh_channel channel, 
446     void *data, uint len, int is_stderr, void *userdata);
447 
448 /**
449  * @brief SSH channel eof callback. Called when a channel receives EOF
450  * @param session Current session handler
451  * @param channel the actual channel
452  * @param userdata Userdata to be passed to the callback function.
453  */
454 alias ssh_channel_eof_callback = extern(C) void function(ssh_session session, ssh_channel channel, 
455     void *userdata);
456 
457 /**
458  * @brief SSH channel close callback. Called when a channel is closed by remote peer
459  * @param session Current session handler
460  * @param channel the actual channel
461  * @param userdata Userdata to be passed to the callback function.
462  */
463 alias ssh_channel_close_callback = extern(C) void function(ssh_session session, ssh_channel channel, 
464     void *userdata);
465 
466 /**
467  * @brief SSH channel signal callback. Called when a channel has received a signal
468  * @param session Current session handler
469  * @param channel the actual channel
470  * @param signal the signal name (without the SIG prefix)
471  * @param userdata Userdata to be passed to the callback function.
472  */
473 alias ssh_channel_signal_callback = extern(C) void function(ssh_session session, 
474     ssh_channel channel, const char *signal, void *userdata);
475 
476 /**
477  * @brief SSH channel exit status callback. Called when a channel has received an exit status
478  * @param session Current session handler
479  * @param channel the actual channel
480  * @param userdata Userdata to be passed to the callback function.
481  */
482 alias ssh_channel_exit_status_callback = extern(C) void function(ssh_session session, 
483     ssh_channel channel, int exit_status, void *userdata);
484 
485 /**
486  * @brief SSH channel exit signal callback. Called when a channel has received an exit signal
487  * @param session Current session handler
488  * @param channel the actual channel
489  * @param signal the signal name (without the SIG prefix)
490  * @param core a boolean telling wether a core has been dumped or not
491  * @param errmsg the description of the exception
492  * @param lang the language of the description (format: RFC 3066)
493  * @param userdata Userdata to be passed to the callback function.
494  */
495 alias ssh_channel_exit_signal_callback = extern(C) void function(ssh_session session, 
496     ssh_channel channel, const char *signal, int core, const char *errmsg, const char *lang, 
497     void *userdata);
498 
499 /**
500  * @brief SSH channel PTY request from a client.
501  * @param channel the channel
502  * @param term The type of terminal emulation
503  * @param width width of the terminal, in characters
504  * @param height height of the terminal, in characters
505  * @param pxwidth width of the terminal, in pixels
506  * @param pxheight height of the terminal, in pixels
507  * @param userdata Userdata to be passed to the callback function.
508  * @returns 0 if the pty request is accepted
509  * @returns -1 if the request is denied
510  */
511 alias ssh_channel_pty_request_callback = extern(C) int function(ssh_session session, 
512     ssh_channel channel, const char *term, int width, int height, 
513     int pxwidth, int pwheight, void *userdata);
514 
515 /**
516  * @brief SSH channel Shell request from a client.
517  * @param channel the channel
518  * @param userdata Userdata to be passed to the callback function.
519  * @returns 0 if the shell request is accepted
520  * @returns 1 if the request is denied
521  */
522 alias ssh_channel_shell_request_callback = extern(C) int function(ssh_session session, 
523     ssh_channel channel, void *userdata);
524 
525 /**
526  * @brief SSH auth-agent-request from the client. This request is
527  * sent by a client when agent forwarding is available.
528  * Server is free to ignore this callback, no answer is expected.
529  * @param channel the channel
530  * @param userdata Userdata to be passed to the callback function.
531  */
532 alias ssh_channel_auth_agent_req_callback = extern(C) void function(ssh_session session, 
533     ssh_channel channel, void *userdata);
534 
535 /**
536  * @brief SSH X11 request from the client. This request is
537  * sent by a client when X11 forwarding is requested(and available).
538  * Server is free to ignore this callback, no answer is expected.
539  * @param channel the channel
540  * @param userdata Userdata to be passed to the callback function.
541  */
542 alias ssh_channel_x11_req_callback = extern(C) void function(ssh_session session, 
543     ssh_channel channel, int single_connection, const char *auth_protocol, 
544     const char *auth_cookie, uint screen_number, void *userdata);
545 
546 /**
547  * @brief SSH channel PTY windows change (terminal size) from a client.
548  * @param channel the channel
549  * @param width width of the terminal, in characters
550  * @param height height of the terminal, in characters
551  * @param pxwidth width of the terminal, in pixels
552  * @param pxheight height of the terminal, in pixels
553  * @param userdata Userdata to be passed to the callback function.
554  * @returns 0 if the pty request is accepted
555  * @returns -1 if the request is denied
556  */
557 alias ssh_channel_pty_window_change_callback = extern(C) int function(ssh_session session, 
558     ssh_channel channel, int width, int height, int pxwidth, int pwheight, void *userdata);
559 
560 /**
561  * @brief SSH channel Exec request from a client.
562  * @param channel the channel
563  * @param command the shell command to be executed
564  * @param userdata Userdata to be passed to the callback function.
565  * @returns 0 if the exec request is accepted
566  * @returns 1 if the request is denied
567  */
568 alias ssh_channel_exec_request_callback = extern(C) int function(ssh_session session, 
569     ssh_channel channel, const char *command, void *userdata);
570 
571 /**
572  * @brief SSH channel environment request from a client.
573  * @param channel the channel
574  * @param env_name name of the environment value to be set
575  * @param env_value value of the environment value to be set
576  * @param userdata Userdata to be passed to the callback function.
577  * @returns 0 if the env request is accepted
578  * @returns 1 if the request is denied
579  * @warning some environment variables can be dangerous if changed (e.g.
580  *          LD_PRELOAD) and should not be fulfilled.
581  */
582 alias ssh_channel_env_request_callback = extern(C) int function(ssh_session session, 
583     ssh_channel channel, const char *env_name, const char *env_value, void *userdata);
584 
585 /**
586  * @brief SSH channel subsystem request from a client.
587  * @param channel the channel
588  * @param subsystem the subsystem required
589  * @param userdata Userdata to be passed to the callback function.
590  * @returns 0 if the subsystem request is accepted
591  * @returns 1 if the request is denied
592  */
593 alias ssh_channel_subsystem_request_callback = extern(C) int function(ssh_session session, 
594     ssh_channel channel, const char *subsystem, void *userdata);
595 
596 struct ssh_channel_callbacks_struct {
597     /** DON'T SET THIS use ssh_callbacks_init() instead. */
598     size_t size;
599     /**
600    * User-provided data. User is free to set anything he wants here
601    */
602     void *userdata;
603     /**
604    * This functions will be called when there is data available.
605    */
606     ssh_channel_data_callback channel_data_function;
607     /**
608    * This functions will be called when the channel has received an EOF.
609    */
610     ssh_channel_eof_callback channel_eof_function;
611     /**
612    * This functions will be called when the channel has been closed by remote
613    */
614     ssh_channel_close_callback channel_close_function;
615     /**
616    * This functions will be called when a signal has been received
617    */
618     ssh_channel_signal_callback channel_signal_function;
619     /**
620    * This functions will be called when an exit status has been received
621    */
622     ssh_channel_exit_status_callback channel_exit_status_function;
623     /**
624    * This functions will be called when an exit signal has been received
625    */
626     ssh_channel_exit_signal_callback channel_exit_signal_function;
627     /**
628    * This function will be called when a client requests a PTY
629    */
630     ssh_channel_pty_request_callback channel_pty_request_function;
631     /**
632    * This function will be called when a client requests a shell
633    */
634     ssh_channel_shell_request_callback channel_shell_request_function;
635     /** This function will be called when a client requests agent
636    * authentication forwarding.
637    */
638     ssh_channel_auth_agent_req_callback channel_auth_agent_req_function;
639     /** This function will be called when a client requests X11
640    * forwarding.
641    */
642     ssh_channel_x11_req_callback channel_x11_req_function;
643     /** This function will be called when a client requests a
644    * window change.
645    */
646     ssh_channel_pty_window_change_callback channel_pty_window_change_function;
647     /** This function will be called when a client requests a
648    * command execution.
649    */
650     ssh_channel_exec_request_callback channel_exec_request_function;
651     /** This function will be called when a client requests an environment
652    * variable to be set.
653    */
654     ssh_channel_env_request_callback channel_env_request_function;
655     /** This function will be called when a client requests a subsystem
656    * (like sftp).
657    */
658     ssh_channel_subsystem_request_callback channel_subsystem_request_function;
659 };
660 alias ssh_channel_callbacks = ssh_channel_callbacks_struct *;
661 
662 /**
663  * @brief Set the channel callback functions.
664  *
665  * This functions sets the callback structure to use your own callback
666  * functions for channel data and exceptions
667  *
668  * @code
669  * struct ssh_channel_callbacks_struct cb = {
670  *   .userdata = data,
671  *   .channel_data = my_channel_data_function
672  * };
673  * ssh_callbacks_init(&cb);
674  * ssh_set_channel_callbacks(channel, &cb);
675  * @endcode
676  *
677  * @param  channel      The channel to set the callback structure.
678  *
679  * @param  cb           The callback structure itself.
680  *
681  * @return SSH_OK on success, SSH_ERROR on error.
682  */
683 extern(C) int ssh_set_channel_callbacks(ssh_channel channel, ssh_channel_callbacks cb);
684 
685 /** @} */
686 
687 /** @group libssh_threads
688  * @{
689  */
690 
691 alias ssh_thread_callback = extern(C) int function(void **lock);
692 
693 alias ssh_thread_id_callback = extern(C) uint function();
694 struct ssh_threads_callbacks_struct {
695     const(char)* type;
696     ssh_thread_callback mutex_init;
697     ssh_thread_callback mutex_destroy;
698     ssh_thread_callback mutex_lock;
699     ssh_thread_callback mutex_unlock;
700     ssh_thread_id_callback thread_id;
701 };
702 
703 /**
704  * @brief Set the thread callbacks structure.
705  *
706  * This is necessary if your program is using libssh in a multithreaded fashion.
707  * This function must be called first, outside of any threading context (in your
708  * main() function for instance), before you call ssh_init().
709  *
710  * @param[in] cb   A pointer to a ssh_threads_callbacks_struct structure, which
711  *                 contains the different callbacks to be set.
712  *
713  * @returns        Always returns SSH_OK.
714  *
715  * @see ssh_threads_callbacks_struct
716  * @see SSH_THREADS_PTHREAD
717  * @bug libgcrypt 1.6 and bigger backend does not support custom callback.
718  *      Using anything else than pthreads here will fail.
719  */
720 extern(C) int ssh_threads_set_callbacks(ssh_threads_callbacks_struct *cb);
721 
722 version (LibSSHWithPThreads) {
723     /**
724      * @brief returns a pointer on the pthread threads callbacks, to be used with
725      * ssh_threads_set_callbacks.
726      * @warning you have to link with the library ssh_threads.
727      * @see ssh_threads_set_callbacks
728      */
729     extern(C) ssh_threads_callbacks_struct *ssh_threads_get_pthread();
730 }
731 
732 /**
733  * @brief Get the noop threads callbacks structure
734  *
735  * This can be used with ssh_threads_set_callbacks. These callbacks do nothing
736  * and are being used by default.
737  *
738  * @return Always returns a valid pointer to the noop callbacks structure.
739  *
740  * @see ssh_threads_set_callbacks
741  */
742 extern(C) ssh_threads_callbacks_struct *ssh_threads_get_noop();
743 
744 /**
745  * @brief Set the logging callback function.
746  *
747  * @param[in]  cb  The callback to set.
748  *
749  * @return         0 on success, < 0 on errror.
750  */
751 extern(C) int ssh_set_log_callback(ssh_logging_callback cb);
752 
753 /**
754  * @brief Get the pointer to the logging callback function.
755  *
756  * @return The pointer the the callback or NULL if none set.
757  */
758 extern(C) ssh_logging_callback ssh_get_log_callback();