mirror of
https://git.libssh.org/projects/libssh.git
synced 2025-12-03 13:31:11 +03:00
Changed the current callback sys to be scalable
This commit is contained in:
@@ -23,21 +23,43 @@
|
|||||||
* This file includes the declarations for the libssh callback mechanism
|
* This file includes the declarations for the libssh callback mechanism
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef _SSH_CALLBACK_H
|
||||||
|
#define _SSH_CALLBACK_H
|
||||||
|
|
||||||
#include "libssh.h"
|
#include "libssh.h"
|
||||||
|
|
||||||
typedef int (*ssh_callback_int) (ssh_session session, void *user, int code);
|
/**
|
||||||
typedef int (*ssh_message_callback) (ssh_session, void *user, ssh_message message);
|
* @brief SSH authentication callback.
|
||||||
typedef int (*ssh_channel_callback_int) (ssh_channel channel, void *user, int code);
|
*
|
||||||
typedef int (*ssh_channel_callback_data) (ssh_channel channel, void *user, int code, void *data, int len);
|
* @param prompt Prompt to be displayed.
|
||||||
|
* @param buf Buffer to save the password. You should null-terminate it.
|
||||||
|
* @param len Length of the buffer.
|
||||||
|
* @param echo Enable or disable the echo of what you type.
|
||||||
|
* @param verify Should the password be verified?
|
||||||
|
* @param userdata Userdata to be passed to the callback function. Useful
|
||||||
|
* for GUI applications.
|
||||||
|
*
|
||||||
|
* @return 0 on success, < 0 on error.
|
||||||
|
*/
|
||||||
|
typedef int (*ssh_auth_callback) (const char *prompt, char *buf, size_t len,
|
||||||
|
int echo, int verify, void *userdata);
|
||||||
|
typedef void (*ssh_log_callback) (ssh_session session, int priority,
|
||||||
|
const char *message, void *userdata);
|
||||||
|
|
||||||
struct ssh_callbacks_struct {
|
struct ssh_callbacks_struct {
|
||||||
ssh_callback_int connection_progress;
|
size_t size; /* size of this structure */
|
||||||
void *connection_progress_user;
|
void *userdata; /* User-provided data */
|
||||||
ssh_channel_callback_int channel_write_confirm;
|
ssh_auth_callback auth_function; /* this functions will be called if e.g. a keyphrase is needed. */
|
||||||
void *channel_write_confirm_user;
|
ssh_log_callback log_function; //log callback
|
||||||
ssh_channel_callback_data channel_read_available;
|
void (*connect_status_function)(void *arg, float status); /* status callback function */
|
||||||
void *channel_read_available_user;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ssh_callbacks_struct * ssh_callbacks;
|
typedef struct ssh_callbacks_struct * ssh_callbacks;
|
||||||
|
|
||||||
|
LIBSSH_API int ssh_options_set_auth_callback(SSH_OPTIONS *opt, ssh_auth_callback cb,
|
||||||
|
void *userdata);
|
||||||
|
LIBSSH_API int ssh_options_set_log_function(SSH_OPTIONS *opt,
|
||||||
|
ssh_log_callback cb, void *userdata);
|
||||||
|
LIBSSH_API int ssh_options_set_status_callback(SSH_OPTIONS *opt, void (*callback)
|
||||||
|
(void *arg, float status), void *arg);
|
||||||
|
#endif /*_SSH_CALLBACK_H */
|
||||||
|
|||||||
@@ -292,25 +292,6 @@ enum ssh_scp_request_types {
|
|||||||
SSH_SCP_REQUEST_WARNING
|
SSH_SCP_REQUEST_WARNING
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief SSH authentication callback.
|
|
||||||
*
|
|
||||||
* @param prompt Prompt to be displayed.
|
|
||||||
* @param buf Buffer to save the password. You should null-terminate it.
|
|
||||||
* @param len Length of the buffer.
|
|
||||||
* @param echo Enable or disable the echo of what you type.
|
|
||||||
* @param verify Should the password be verified?
|
|
||||||
* @param userdata Userdata to be passed to the callback function. Useful
|
|
||||||
* for GUI applications.
|
|
||||||
*
|
|
||||||
* @return 0 on success, < 0 on error.
|
|
||||||
*/
|
|
||||||
typedef int (*ssh_auth_callback) (const char *prompt, char *buf, size_t len,
|
|
||||||
int echo, int verify, void *userdata);
|
|
||||||
typedef void (*ssh_log_callback) (ssh_session session, int priority,
|
|
||||||
const char *message, void *userdata);
|
|
||||||
|
|
||||||
|
|
||||||
LIBSSH_API void buffer_free(ssh_buffer buffer);
|
LIBSSH_API void buffer_free(ssh_buffer buffer);
|
||||||
LIBSSH_API void *buffer_get(ssh_buffer buffer);
|
LIBSSH_API void *buffer_get(ssh_buffer buffer);
|
||||||
LIBSSH_API uint32_t buffer_get_len(ssh_buffer buffer);
|
LIBSSH_API uint32_t buffer_get_len(ssh_buffer buffer);
|
||||||
@@ -406,23 +387,17 @@ LIBSSH_API int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **arg
|
|||||||
LIBSSH_API int ssh_options_parse_config(SSH_OPTIONS *opt, const char *filename);
|
LIBSSH_API int ssh_options_parse_config(SSH_OPTIONS *opt, const char *filename);
|
||||||
LIBSSH_API int ssh_options_set(ssh_options opt, enum ssh_options_e type,
|
LIBSSH_API int ssh_options_set(ssh_options opt, enum ssh_options_e type,
|
||||||
const void *value);
|
const void *value);
|
||||||
LIBSSH_API int ssh_options_set_auth_callback(SSH_OPTIONS *opt, ssh_auth_callback cb,
|
|
||||||
void *userdata);
|
|
||||||
LIBSSH_API int ssh_options_set_banner(SSH_OPTIONS *opt, const char *banner);
|
LIBSSH_API int ssh_options_set_banner(SSH_OPTIONS *opt, const char *banner);
|
||||||
LIBSSH_API int ssh_options_set_bind(SSH_OPTIONS *opt, const char *bindaddr, int port);
|
LIBSSH_API int ssh_options_set_bind(SSH_OPTIONS *opt, const char *bindaddr, int port);
|
||||||
LIBSSH_API int ssh_options_set_dsa_server_key(SSH_OPTIONS *opt, const char *dsakey);
|
LIBSSH_API int ssh_options_set_dsa_server_key(SSH_OPTIONS *opt, const char *dsakey);
|
||||||
LIBSSH_API int ssh_options_set_fd(SSH_OPTIONS *opt, socket_t fd);
|
LIBSSH_API int ssh_options_set_fd(SSH_OPTIONS *opt, socket_t fd);
|
||||||
LIBSSH_API int ssh_options_set_host(SSH_OPTIONS *opt, const char *host);
|
LIBSSH_API int ssh_options_set_host(SSH_OPTIONS *opt, const char *host);
|
||||||
LIBSSH_API int ssh_options_set_identity(SSH_OPTIONS *opt, const char *identity);
|
LIBSSH_API int ssh_options_set_identity(SSH_OPTIONS *opt, const char *identity);
|
||||||
LIBSSH_API int ssh_options_set_log_function(SSH_OPTIONS *opt,
|
|
||||||
ssh_log_callback cb, void *userdata);
|
|
||||||
LIBSSH_API int ssh_options_set_log_verbosity(SSH_OPTIONS *opt, int verbosity);
|
LIBSSH_API int ssh_options_set_log_verbosity(SSH_OPTIONS *opt, int verbosity);
|
||||||
LIBSSH_API int ssh_options_set_known_hosts_file(SSH_OPTIONS *opt, const char *dir);
|
LIBSSH_API int ssh_options_set_known_hosts_file(SSH_OPTIONS *opt, const char *dir);
|
||||||
LIBSSH_API int ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port);
|
LIBSSH_API int ssh_options_set_port(SSH_OPTIONS *opt, unsigned int port);
|
||||||
LIBSSH_API int ssh_options_set_rsa_server_key(SSH_OPTIONS *opt, const char *rsakey);
|
LIBSSH_API int ssh_options_set_rsa_server_key(SSH_OPTIONS *opt, const char *rsakey);
|
||||||
LIBSSH_API int ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir);
|
LIBSSH_API int ssh_options_set_ssh_dir(SSH_OPTIONS *opt, const char *dir);
|
||||||
LIBSSH_API int ssh_options_set_status_callback(SSH_OPTIONS *opt, void (*callback)
|
|
||||||
(void *arg, float status), void *arg);
|
|
||||||
LIBSSH_API int ssh_options_set_timeout(SSH_OPTIONS *opt, long seconds, long usec);
|
LIBSSH_API int ssh_options_set_timeout(SSH_OPTIONS *opt, long seconds, long usec);
|
||||||
LIBSSH_API int ssh_options_set_username(SSH_OPTIONS *opt, const char *username);
|
LIBSSH_API int ssh_options_set_username(SSH_OPTIONS *opt, const char *username);
|
||||||
LIBSSH_API int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list);
|
LIBSSH_API int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list);
|
||||||
|
|||||||
@@ -45,7 +45,7 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "libssh/libssh.h"
|
#include "libssh/libssh.h"
|
||||||
|
#include "libssh/callback.h"
|
||||||
/* some constants */
|
/* some constants */
|
||||||
#define MAX_PACKET_LEN 262144
|
#define MAX_PACKET_LEN 262144
|
||||||
#define ERROR_BUFFERLEN 1024
|
#define ERROR_BUFFERLEN 1024
|
||||||
@@ -285,10 +285,7 @@ struct ssh_options_struct {
|
|||||||
int use_nonexisting_algo; /* if user sets a not supported algorithm for kex, don't complain */
|
int use_nonexisting_algo; /* if user sets a not supported algorithm for kex, don't complain */
|
||||||
char *wanted_methods[10]; /* the kex methods can be choosed. better use the kex fonctions to do that */
|
char *wanted_methods[10]; /* the kex methods can be choosed. better use the kex fonctions to do that */
|
||||||
void *wanted_cookie; /* wants a specific cookie to be sent ? if null, generate a new one */
|
void *wanted_cookie; /* wants a specific cookie to be sent ? if null, generate a new one */
|
||||||
ssh_auth_callback auth_function; /* this functions will be called if e.g. a keyphrase is needed. */
|
ssh_callbacks callbacks; /* Callbacks to user functions */
|
||||||
void *auth_userdata;
|
|
||||||
void (*connect_status_function)(void *arg, float status); /* status callback function */
|
|
||||||
void *connect_status_arg; /* arbitrary argument */
|
|
||||||
long timeout; /* seconds */
|
long timeout; /* seconds */
|
||||||
long timeout_usec;
|
long timeout_usec;
|
||||||
int ssh2allowed;
|
int ssh2allowed;
|
||||||
@@ -296,8 +293,7 @@ struct ssh_options_struct {
|
|||||||
char *dsakey;
|
char *dsakey;
|
||||||
char *rsakey; /* host key for server implementation */
|
char *rsakey; /* host key for server implementation */
|
||||||
int log_verbosity;
|
int log_verbosity;
|
||||||
ssh_log_callback log_function; //log callback
|
|
||||||
void *log_userdata;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ssh_crypto_struct {
|
typedef struct ssh_crypto_struct {
|
||||||
|
|||||||
@@ -33,8 +33,8 @@
|
|||||||
#include "libssh/ssh2.h"
|
#include "libssh/ssh2.h"
|
||||||
|
|
||||||
#define set_status(opt,status) do {\
|
#define set_status(opt,status) do {\
|
||||||
if (opt->connect_status_function) \
|
if (opt->callbacks && opt->callbacks->connect_status_function) \
|
||||||
opt->connect_status_function(opt->connect_status_arg, status); \
|
opt->callbacks->connect_status_function(opt->callbacks->userdata, status); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -586,9 +586,9 @@ static int pem_get_password(char *buf, int size, int rwflag, void *userdata) {
|
|||||||
ssh_log(session, SSH_LOG_RARE,
|
ssh_log(session, SSH_LOG_RARE,
|
||||||
"Trying to call external authentication function");
|
"Trying to call external authentication function");
|
||||||
|
|
||||||
if (session && session->options->auth_function) {
|
if (session && session->options->callbacks->auth_function) {
|
||||||
if ((*session->options->auth_function)("Passphrase for private key:", buf, size, 0, 0,
|
if (session->options->callbacks->auth_function("Passphrase for private key:", buf, size, 0, 0,
|
||||||
session->options->auth_userdata ? session->options->auth_userdata : NULL) < 0) {
|
session->options->callbacks->userdata) < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -636,15 +636,14 @@ ssh_private_key privatekey_from_file(SSH_SESSION *session, const char *filename,
|
|||||||
|
|
||||||
ssh_log(session, SSH_LOG_RARE, "Trying to read %s, passphase=%s, authcb=%s",
|
ssh_log(session, SSH_LOG_RARE, "Trying to read %s, passphase=%s, authcb=%s",
|
||||||
filename, passphrase ? "true" : "false",
|
filename, passphrase ? "true" : "false",
|
||||||
session->options->auth_function ? "true" : "false");
|
session->options->callbacks->auth_function ? "true" : "false");
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case TYPE_DSS:
|
case TYPE_DSS:
|
||||||
if (passphrase == NULL) {
|
if (passphrase == NULL) {
|
||||||
if (session->options->auth_function) {
|
if (session->options->callbacks->auth_function) {
|
||||||
auth_cb = session->options->auth_function;
|
auth_cb = session->options->callbacks->auth_function;
|
||||||
if (session->options->auth_userdata) {
|
auth_ud = session->options->callbacks->userdata;
|
||||||
auth_ud = session->options->auth_userdata;
|
|
||||||
}
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
valid = read_dsa_privatekey(file, &dsa, auth_cb, auth_ud,
|
valid = read_dsa_privatekey(file, &dsa, auth_cb, auth_ud,
|
||||||
"Passphrase for private key:");
|
"Passphrase for private key:");
|
||||||
@@ -681,11 +680,9 @@ ssh_private_key privatekey_from_file(SSH_SESSION *session, const char *filename,
|
|||||||
break;
|
break;
|
||||||
case TYPE_RSA:
|
case TYPE_RSA:
|
||||||
if (passphrase == NULL) {
|
if (passphrase == NULL) {
|
||||||
if (session->options->auth_function) {
|
if (session->options->callbacks->auth_function) {
|
||||||
auth_cb = session->options->auth_function;
|
auth_cb = session->options->callbacks->auth_function;
|
||||||
if (session->options->auth_userdata) {
|
auth_ud = session->options->callbacks->userdata;
|
||||||
auth_ud = session->options->auth_userdata;
|
|
||||||
}
|
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
valid = read_rsa_privatekey(file, &rsa, auth_cb, auth_ud,
|
valid = read_rsa_privatekey(file, &rsa, auth_cb, auth_ud,
|
||||||
"Passphrase for private key:");
|
"Passphrase for private key:");
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ void ssh_log(SSH_SESSION *session, int verbosity, const char *format, ...) {
|
|||||||
vsnprintf(buffer, sizeof(buffer), format, va);
|
vsnprintf(buffer, sizeof(buffer), format, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
if (session->options->log_function) {
|
if (session->options->callbacks && session->options->callbacks->log_function) {
|
||||||
session->options->log_function(session, verbosity, buffer,
|
session->options->callbacks->log_function(session, verbosity, buffer,
|
||||||
session->options->log_userdata);
|
session->options->callbacks->userdata);
|
||||||
} else if (verbosity == SSH_LOG_FUNCTIONS) {
|
} else if (verbosity == SSH_LOG_FUNCTIONS) {
|
||||||
if (session->log_indent > 255) {
|
if (session->log_indent > 255) {
|
||||||
min = 255;
|
min = 255;
|
||||||
|
|||||||
@@ -154,15 +154,11 @@ SSH_OPTIONS *ssh_options_copy(SSH_OPTIONS *opt) {
|
|||||||
|
|
||||||
new->fd = opt->fd;
|
new->fd = opt->fd;
|
||||||
new->port = opt->port;
|
new->port = opt->port;
|
||||||
new->auth_function = opt->auth_function;
|
new->callbacks = opt->callbacks;
|
||||||
new->auth_userdata = opt->auth_userdata;
|
|
||||||
new->connect_status_function = opt->connect_status_function;
|
|
||||||
new->connect_status_arg = opt->connect_status_arg;
|
|
||||||
new->timeout = opt->timeout;
|
new->timeout = opt->timeout;
|
||||||
new->timeout_usec = opt->timeout_usec;
|
new->timeout_usec = opt->timeout_usec;
|
||||||
new->ssh2allowed = opt->ssh2allowed;
|
new->ssh2allowed = opt->ssh2allowed;
|
||||||
new->ssh1allowed = opt->ssh1allowed;
|
new->ssh1allowed = opt->ssh1allowed;
|
||||||
new->log_function = opt->log_function;
|
|
||||||
new->log_verbosity = opt->log_verbosity;
|
new->log_verbosity = opt->log_verbosity;
|
||||||
|
|
||||||
return new;
|
return new;
|
||||||
@@ -575,32 +571,6 @@ int ssh_options_set(ssh_options opt, enum ssh_options_e type,
|
|||||||
|
|
||||||
opt->log_verbosity = *x;
|
opt->log_verbosity = *x;
|
||||||
}
|
}
|
||||||
case SSH_OPTIONS_AUTH_CALLBACK:
|
|
||||||
if (value == NULL) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
opt->auth_function = (ssh_auth_callback) value;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SSH_OPTIONS_AUTH_USERDATA:
|
|
||||||
opt->auth_userdata = (void *) value;
|
|
||||||
break;
|
|
||||||
case SSH_OPTIONS_LOG_CALLBACK:
|
|
||||||
if (value == NULL) {
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
opt->log_function = (ssh_log_callback) value;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SSH_OPTIONS_LOG_USERDATA:
|
|
||||||
opt->auth_userdata = (void *) value;
|
|
||||||
break;
|
|
||||||
case SSH_OPTIONS_STATUS_CALLBACK:
|
|
||||||
/* TODO */
|
|
||||||
break;
|
|
||||||
case SSH_OPTIONS_STATUS_ARG:
|
|
||||||
/* TODO */
|
|
||||||
break;
|
|
||||||
case SSH_OPTIONS_CIPHERS_C_S:
|
case SSH_OPTIONS_CIPHERS_C_S:
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -955,12 +925,13 @@ int ssh_options_default_known_hosts_file(SSH_OPTIONS *opt) {
|
|||||||
*/
|
*/
|
||||||
int ssh_options_set_status_callback(SSH_OPTIONS *opt,
|
int ssh_options_set_status_callback(SSH_OPTIONS *opt,
|
||||||
void (*callback)(void *arg, float status), void *arg) {
|
void (*callback)(void *arg, float status), void *arg) {
|
||||||
if (opt == NULL || callback == NULL) {
|
if (opt == NULL || callback == NULL || opt->callbacks==NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
opt->connect_status_function = callback;
|
opt->callbacks->connect_status_function = callback;
|
||||||
opt->connect_status_arg = arg;
|
if(arg)
|
||||||
|
opt->callbacks->userdata=arg;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1032,12 +1003,13 @@ int ssh_options_allow_ssh2(SSH_OPTIONS *opt, int allow) {
|
|||||||
*/
|
*/
|
||||||
int ssh_options_set_log_function(SSH_OPTIONS *opt, ssh_log_callback cb,
|
int ssh_options_set_log_function(SSH_OPTIONS *opt, ssh_log_callback cb,
|
||||||
void *userdata) {
|
void *userdata) {
|
||||||
if (opt == NULL || cb == NULL) {
|
if (opt == NULL || cb == NULL || opt->callbacks==NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
opt->log_function = cb;
|
opt->callbacks->log_function = cb;
|
||||||
opt->log_userdata = userdata;
|
if(userdata)
|
||||||
|
opt->callbacks->userdata = userdata;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1283,12 +1255,13 @@ int ssh_options_getopt(SSH_OPTIONS *options, int *argcptr, char **argv) {
|
|||||||
*/
|
*/
|
||||||
int ssh_options_set_auth_callback(SSH_OPTIONS *opt, ssh_auth_callback cb,
|
int ssh_options_set_auth_callback(SSH_OPTIONS *opt, ssh_auth_callback cb,
|
||||||
void *userdata) {
|
void *userdata) {
|
||||||
if (opt == NULL || cb == NULL) {
|
if (opt == NULL || cb == NULL || opt->callbacks==NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
opt->auth_function = cb;
|
opt->callbacks->auth_function = cb;
|
||||||
opt->auth_userdata = userdata;
|
if(userdata != NULL)
|
||||||
|
opt->callbacks->userdata = userdata;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user