diff --git a/README b/README index 73d81a37..c8191cea 100644 --- a/README +++ b/README @@ -10,6 +10,8 @@ Version 0.4 Fixed leak in sftp_symlink_ex(), result for READLINK and REALPATH not freed unless there was an error. + Added libssh2_banner_set(), specify an arbitrary banner to send on introduction. + Version 0.3 ----------- diff --git a/include/libssh2.h b/include/libssh2.h index d1347c53..f4312dfa 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -43,7 +43,7 @@ #include #define LIBSSH2_VERSION "0.3" -#define LIBSSH2_APINO 200412211608 +#define LIBSSH2_APINO 200412241457 /* Part of every banner, user specified or not */ #define LIBSSH2_SSH_BANNER "SSH-2.0-libssh2_" LIBSSH2_VERSION @@ -225,6 +225,7 @@ LIBSSH2_API LIBSSH2_SESSION *libssh2_session_init_ex(LIBSSH2_ALLOC_FUNC((*my_all LIBSSH2_API void **libssh2_session_abstract(LIBSSH2_SESSION *session); LIBSSH2_API void *libssh2_session_callback_set(LIBSSH2_SESSION *session, int cbtype, void *callback); +LIBSSH2_API int libssh2_banner_set(LIBSSH2_SESSION *session, char *banner); LIBSSH2_API int libssh2_session_startup(LIBSSH2_SESSION *session, int socket); LIBSSH2_API int libssh2_session_disconnect_ex(LIBSSH2_SESSION *session, int reason, char *description, char *lang); diff --git a/src/session.c b/src/session.c index 48b83b10..58526b96 100644 --- a/src/session.c +++ b/src/session.c @@ -128,6 +128,37 @@ static int libssh2_banner_send(LIBSSH2_SESSION *session) } /* }}} */ +/* {{{ libssh2_banner_set + * Set the local banner + */ +LIBSSH2_API int libssh2_banner_set(LIBSSH2_SESSION *session, char *banner) +{ + int banner_len = banner ? strlen(banner) : 0; + + if (session->local.banner) { + LIBSSH2_FREE(session, session->local.banner); + session->local.banner = NULL; + } + + if (!banner_len) { + return 0; + } + + session->local.banner = LIBSSH2_ALLOC(session, banner_len + 3); + if (!session->local.banner) { + libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for local banner", 0); + return -1; + } + + memcpy(session->local.banner, banner, banner_len); + session->local.banner[banner_len++] = '\r'; + session->local.banner[banner_len++] = '\n'; + session->local.banner[banner_len++] = '\0'; + + return 0; +} +/* }}} */ + /* {{{ proto libssh2_session_init * Allocate and initialize a libssh2 session structure * Allows for malloc callbacks in case the calling program has its own memory manager @@ -392,10 +423,6 @@ LIBSSH2_API void libssh2_session_free(LIBSSH2_SESSION *session) LIBSSH2_FREE(session, tmp); } - if (session->local.banner) { - LIBSSH2_FREE(session, session->local.banner); - } - LIBSSH2_FREE(session, session); } /* }}} */