aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libssh/callback.h42
-rw-r--r--include/libssh/libssh.h25
-rw-r--r--include/libssh/priv.h10
-rw-r--r--libssh/client.c4
-rw-r--r--libssh/keyfiles.c25
-rw-r--r--libssh/log.c6
-rw-r--r--libssh/options.c53
7 files changed, 64 insertions, 101 deletions
diff --git a/include/libssh/callback.h b/include/libssh/callback.h
index 759db0eb..3e1a1fab 100644
--- a/include/libssh/callback.h
+++ b/include/libssh/callback.h
@@ -23,21 +23,43 @@
* This file includes the declarations for the libssh callback mechanism
*/
+#ifndef _SSH_CALLBACK_H
+#define _SSH_CALLBACK_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);
-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);
+/**
+ * @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);
struct ssh_callbacks_struct {
- ssh_callback_int connection_progress;
- void *connection_progress_user;
- ssh_channel_callback_int channel_write_confirm;
- void *channel_write_confirm_user;
- ssh_channel_callback_data channel_read_available;
- void *channel_read_available_user;
+ size_t size; /* size of this structure */
+ void *userdata; /* User-provided data */
+ ssh_auth_callback auth_function; /* this functions will be called if e.g. a keyphrase is needed. */
+ ssh_log_callback log_function; //log callback
+ void (*connect_status_function)(void *arg, float status); /* status callback function */
};
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 */
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
index 0b1bf496..10103e55 100644
--- a/include/libssh/libssh.h
+++ b/include/libssh/libssh.h
@@ -292,25 +292,6 @@ enum ssh_scp_request_types {
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_get(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_set(ssh_options opt, enum ssh_options_e type,
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_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_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_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_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_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_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_username(SSH_OPTIONS *opt, const char *username);
LIBSSH_API int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list);
diff --git a/include/libssh/priv.h b/include/libssh/priv.h
index 1d5414bf..87bf1bfc 100644
--- a/include/libssh/priv.h
+++ b/include/libssh/priv.h
@@ -45,7 +45,7 @@
#include "config.h"
#include "libssh/libssh.h"
-
+#include "libssh/callback.h"
/* some constants */
#define MAX_PACKET_LEN 262144
#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 */
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 */
- ssh_auth_callback auth_function; /* this functions will be called if e.g. a keyphrase is needed. */
- void *auth_userdata;
- void (*connect_status_function)(void *arg, float status); /* status callback function */
- void *connect_status_arg; /* arbitrary argument */
+ ssh_callbacks callbacks; /* Callbacks to user functions */
long timeout; /* seconds */
long timeout_usec;
int ssh2allowed;
@@ -296,8 +293,7 @@ struct ssh_options_struct {
char *dsakey;
char *rsakey; /* host key for server implementation */
int log_verbosity;
- ssh_log_callback log_function; //log callback
- void *log_userdata;
+
};
typedef struct ssh_crypto_struct {
diff --git a/libssh/client.c b/libssh/client.c
index cbc70e31..79c82497 100644
--- a/libssh/client.c
+++ b/libssh/client.c
@@ -33,8 +33,8 @@
#include "libssh/ssh2.h"
#define set_status(opt,status) do {\
- if (opt->connect_status_function) \
- opt->connect_status_function(opt->connect_status_arg, status); \
+ if (opt->callbacks && opt->callbacks->connect_status_function) \
+ opt->callbacks->connect_status_function(opt->callbacks->userdata, status); \
} while (0)
/**
diff --git a/libssh/keyfiles.c b/libssh/keyfiles.c
index c651d4c1..e2dfaf76 100644
--- a/libssh/keyfiles.c
+++ b/libssh/keyfiles.c
@@ -586,9 +586,9 @@ static int pem_get_password(char *buf, int size, int rwflag, void *userdata) {
ssh_log(session, SSH_LOG_RARE,
"Trying to call external authentication function");
- if (session && session->options->auth_function) {
- if ((*session->options->auth_function)("Passphrase for private key:", buf, size, 0, 0,
- session->options->auth_userdata ? session->options->auth_userdata : NULL) < 0) {
+ if (session && session->options->callbacks->auth_function) {
+ if (session->options->callbacks->auth_function("Passphrase for private key:", buf, size, 0, 0,
+ session->options->callbacks->userdata) < 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",
filename, passphrase ? "true" : "false",
- session->options->auth_function ? "true" : "false");
+ session->options->callbacks->auth_function ? "true" : "false");
switch (type) {
case TYPE_DSS:
if (passphrase == NULL) {
- if (session->options->auth_function) {
- auth_cb = session->options->auth_function;
- if (session->options->auth_userdata) {
- auth_ud = session->options->auth_userdata;
- }
+ if (session->options->callbacks->auth_function) {
+ auth_cb = session->options->callbacks->auth_function;
+ auth_ud = session->options->callbacks->userdata;
+
#ifdef HAVE_LIBGCRYPT
valid = read_dsa_privatekey(file, &dsa, auth_cb, auth_ud,
"Passphrase for private key:");
@@ -681,11 +680,9 @@ ssh_private_key privatekey_from_file(SSH_SESSION *session, const char *filename,
break;
case TYPE_RSA:
if (passphrase == NULL) {
- if (session->options->auth_function) {
- auth_cb = session->options->auth_function;
- if (session->options->auth_userdata) {
- auth_ud = session->options->auth_userdata;
- }
+ if (session->options->callbacks->auth_function) {
+ auth_cb = session->options->callbacks->auth_function;
+ auth_ud = session->options->callbacks->userdata;
#ifdef HAVE_LIBGCRYPT
valid = read_rsa_privatekey(file, &rsa, auth_cb, auth_ud,
"Passphrase for private key:");
diff --git a/libssh/log.c b/libssh/log.c
index 4a3b275b..f2a4b8b9 100644
--- a/libssh/log.c
+++ b/libssh/log.c
@@ -55,9 +55,9 @@ void ssh_log(SSH_SESSION *session, int verbosity, const char *format, ...) {
vsnprintf(buffer, sizeof(buffer), format, va);
va_end(va);
- if (session->options->log_function) {
- session->options->log_function(session, verbosity, buffer,
- session->options->log_userdata);
+ if (session->options->callbacks && session->options->callbacks->log_function) {
+ session->options->callbacks->log_function(session, verbosity, buffer,
+ session->options->callbacks->userdata);
} else if (verbosity == SSH_LOG_FUNCTIONS) {
if (session->log_indent > 255) {
min = 255;
diff --git a/libssh/options.c b/libssh/options.c
index c97ba564..56f18400 100644
--- a/libssh/options.c
+++ b/libssh/options.c
@@ -154,15 +154,11 @@ SSH_OPTIONS *ssh_options_copy(SSH_OPTIONS *opt) {
new->fd = opt->fd;
new->port = opt->port;
- new->auth_function = opt->auth_function;
- new->auth_userdata = opt->auth_userdata;
- new->connect_status_function = opt->connect_status_function;
- new->connect_status_arg = opt->connect_status_arg;
+ new->callbacks = opt->callbacks;
new->timeout = opt->timeout;
new->timeout_usec = opt->timeout_usec;
new->ssh2allowed = opt->ssh2allowed;
new->ssh1allowed = opt->ssh1allowed;
- new->log_function = opt->log_function;
new->log_verbosity = opt->log_verbosity;
return new;
@@ -575,32 +571,6 @@ int ssh_options_set(ssh_options opt, enum ssh_options_e type,
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:
if (value == NULL) {
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,
void (*callback)(void *arg, float status), void *arg) {
- if (opt == NULL || callback == NULL) {
+ if (opt == NULL || callback == NULL || opt->callbacks==NULL) {
return -1;
}
- opt->connect_status_function = callback;
- opt->connect_status_arg = arg;
+ opt->callbacks->connect_status_function = callback;
+ if(arg)
+ opt->callbacks->userdata=arg;
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,
void *userdata) {
- if (opt == NULL || cb == NULL) {
+ if (opt == NULL || cb == NULL || opt->callbacks==NULL) {
return -1;
}
- opt->log_function = cb;
- opt->log_userdata = userdata;
+ opt->callbacks->log_function = cb;
+ if(userdata)
+ opt->callbacks->userdata = userdata;
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,
void *userdata) {
- if (opt == NULL || cb == NULL) {
+ if (opt == NULL || cb == NULL || opt->callbacks==NULL) {
return -1;
}
- opt->auth_function = cb;
- opt->auth_userdata = userdata;
+ opt->callbacks->auth_function = cb;
+ if(userdata != NULL)
+ opt->callbacks->userdata = userdata;
return 0;
}